Removed W->A from DEFWND_ImmIsUIMessageW.
[wine] / dlls / x11drv / x11drv_main.c
1 /*
2  * X11DRV initialization code
3  *
4  * Copyright 1998 Patrik Stridvall
5  * Copyright 2000 Alexandre Julliard
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include "config.h"
23
24 #include <fcntl.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #ifdef HAVE_SYS_TIME_H
30 # include <sys/time.h>
31 #endif
32 #ifdef HAVE_UNISTD_H
33 # include <unistd.h>
34 #endif
35 #include <X11/cursorfont.h>
36 #include <X11/Xlib.h>
37 #ifdef HAVE_XKB
38 #include <X11/XKBlib.h>
39 #endif
40
41 #define BOOL X_BOOL
42 #define BYTE X_BYTE
43 #define INT8 X_INT8
44 #define INT16 X_INT16
45 #define INT32 X_INT32
46 #define INT64 X_INT64
47 #define LONG64 X_LONG64
48 #include <X11/Xproto.h>
49 #undef BOOL
50 #undef BYTE
51 #undef INT8
52 #undef INT16
53 #undef INT32
54 #undef INT64
55 #undef LONG64
56
57 #include "windef.h"
58 #include "winbase.h"
59 #include "wine/winbase16.h"
60 #include "winreg.h"
61
62 #include "user.h"
63 #include "win.h"
64 #include "x11drv.h"
65 #include "xvidmode.h"
66 #include "xrandr.h"
67 #include "dga2.h"
68 #include "wine/server.h"
69 #include "wine/debug.h"
70
71 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
72
73 static CRITICAL_SECTION X11DRV_CritSection;
74 static CRITICAL_SECTION_DEBUG critsect_debug =
75 {
76     0, 0, &X11DRV_CritSection,
77     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
78       0, 0, { 0, (DWORD)(__FILE__ ": X11DRV_CritSection") }
79 };
80 static CRITICAL_SECTION X11DRV_CritSection = { &critsect_debug, -1, 0, 0, 0, 0 };
81
82 Screen *screen;
83 Visual *visual;
84 unsigned int screen_width;
85 unsigned int screen_height;
86 unsigned int screen_depth;
87 Window root_window;
88 DWORD desktop_tid = 0;
89 int dxgrab = 0;
90 int usedga = 0;
91 int usexvidmode = 0;
92 int usexrandr = 1;
93 int use_xkb = 1;
94 int use_take_focus = 1;
95 int managed_mode = 1;
96 int client_side_with_core = 1;
97 int client_side_with_render = 1;
98 int client_side_antialias_with_core = 1;
99 int client_side_antialias_with_render = 1;
100 int using_wine_desktop = 0;
101
102 unsigned int X11DRV_server_startticks;
103
104 static BOOL synchronous;  /* run in synchronous mode? */
105 static BOOL desktop_dbl_buf = TRUE;
106 static char *desktop_geometry;
107 static XVisualInfo *desktop_vi;
108
109 static x11drv_error_callback err_callback;   /* current callback for error */
110 static Display *err_callback_display;        /* display callback is set for */
111 static void *err_callback_arg;               /* error callback argument */
112 static int err_callback_result;              /* error callback result */
113 static unsigned long err_serial;             /* serial number of first request */
114 static int (*old_error_handler)( Display *, XErrorEvent * );
115 static int use_xim = 1;
116 static char input_style[20];
117
118 #define IS_OPTION_TRUE(ch) \
119     ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
120 #define IS_OPTION_FALSE(ch) \
121     ((ch) == 'n' || (ch) == 'N' || (ch) == 'f' || (ch) == 'F' || (ch) == '0')
122
123 /***********************************************************************
124  *              ignore_error
125  *
126  * Check if the X error is one we can ignore.
127  */
128 static inline BOOL ignore_error( Display *display, XErrorEvent *event )
129 {
130     if (event->request_code == X_SetInputFocus && event->error_code == BadMatch) return TRUE;
131     return FALSE;
132 }
133
134
135 /***********************************************************************
136  *              X11DRV_expect_error
137  *
138  * Setup a callback function that will be called on an X error.  The
139  * callback must return non-zero if the error is the one it expected.
140  * This function acquires the x11 lock; X11DRV_check_error must be
141  * called in all cases to release it.
142  */
143 void X11DRV_expect_error( Display *display, x11drv_error_callback callback, void *arg )
144 {
145     wine_tsx11_lock();
146     err_callback         = callback;
147     err_callback_display = display;
148     err_callback_arg     = arg;
149     err_callback_result  = 0;
150     err_serial           = NextRequest(display);
151 }
152
153
154 /***********************************************************************
155  *              X11DRV_check_error
156  *
157  * Check if an expected X11 error occurred; return non-zero if yes.
158  * Also release the x11 lock obtained in X11DRV_expect_error.
159  * The caller is responsible for calling XSync first if necessary.
160  */
161 int X11DRV_check_error(void)
162 {
163     int ret;
164     err_callback = NULL;
165     ret = err_callback_result;
166     wine_tsx11_unlock();
167     return ret;
168 }
169
170
171 /***********************************************************************
172  *              error_handler
173  */
174 static int error_handler( Display *display, XErrorEvent *error_evt )
175 {
176     if (err_callback && display == err_callback_display &&
177         (long)(error_evt->serial - err_serial) >= 0)
178     {
179         if ((err_callback_result = err_callback( display, error_evt, err_callback_arg )))
180         {
181             TRACE( "got expected error %d req %d\n",
182                    error_evt->error_code, error_evt->request_code );
183             return 0;
184         }
185     }
186     if (ignore_error( display, error_evt ))
187     {
188         TRACE( "got ignored error %d req %d\n",
189                error_evt->error_code, error_evt->request_code );
190         return 0;
191     }
192     if (synchronous)
193     {
194         ERR( "X protocol error: serial=%ld, request_code=%d - breaking into debugger\n",
195              error_evt->serial, error_evt->request_code );
196         DebugBreak();  /* force an entry in the debugger */
197     }
198     old_error_handler( display, error_evt );
199     return 0;
200 }
201
202 /***********************************************************************
203  *              wine_tsx11_lock   (X11DRV.@)
204  */
205 void wine_tsx11_lock(void)
206 {
207     EnterCriticalSection( &X11DRV_CritSection );
208 }
209
210 /***********************************************************************
211  *              wine_tsx11_unlock   (X11DRV.@)
212  */
213 void wine_tsx11_unlock(void)
214 {
215     LeaveCriticalSection( &X11DRV_CritSection );
216 }
217
218 /***********************************************************************
219  *              get_server_startup
220  *
221  * Get the server startup time
222  * Won't be exact, but should be sufficient
223  */
224 static void get_server_startup(void)
225 {
226     struct timeval t;
227     gettimeofday( &t, NULL );
228     X11DRV_server_startticks = ((t.tv_sec * 1000) + (t.tv_usec / 1000)) - GetTickCount();
229 }
230
231
232 /***********************************************************************
233  *              get_config_key
234  *
235  * Get a config key from either the app-specific or the default config
236  */
237 inline static DWORD get_config_key( HKEY defkey, HKEY appkey, const char *name,
238                                     char *buffer, DWORD size )
239 {
240     if (appkey && !RegQueryValueExA( appkey, name, 0, NULL, buffer, &size )) return 0;
241     return RegQueryValueExA( defkey, name, 0, NULL, buffer, &size );
242 }
243
244
245 /***********************************************************************
246  *              setup_options
247  *
248  * Setup the x11drv options.
249  */
250 static void setup_options(void)
251 {
252     char buffer[MAX_PATH+16];
253     HKEY hkey, appkey = 0;
254     DWORD len;
255
256     if (RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\x11drv", 0, NULL,
257                          REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, NULL ))
258     {
259         ERR("Cannot create config registry key\n" );
260         ExitProcess(1);
261     }
262
263     /* open the app-specific key */
264
265     len = (GetModuleFileNameA( 0, buffer, MAX_PATH ));
266     if (len && len < MAX_PATH)
267     {
268         HKEY tmpkey;
269         char *p, *appname = buffer;
270         if ((p = strrchr( appname, '/' ))) appname = p + 1;
271         if ((p = strrchr( appname, '\\' ))) appname = p + 1;
272         strcat( appname, "\\x11drv" );
273         if (!RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\AppDefaults", &tmpkey ))
274         {
275             if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
276             RegCloseKey( tmpkey );
277         }
278     }
279
280     if (!get_config_key( hkey, appkey, "Desktop", buffer, sizeof(buffer) ))
281     {
282         /* Imperfect validation:  If Desktop=N, then we don't turn on
283         ** the --desktop option.  We should really validate for a correct
284         ** sizing entry */
285         if (!IS_OPTION_FALSE(buffer[0])) desktop_geometry = strdup(buffer);
286     }
287
288     if (!get_config_key( hkey, appkey, "Managed", buffer, sizeof(buffer) ))
289         managed_mode = IS_OPTION_TRUE( buffer[0] );
290
291     if (!get_config_key( hkey, appkey, "DXGrab", buffer, sizeof(buffer) ))
292         dxgrab = IS_OPTION_TRUE( buffer[0] );
293
294     if (!get_config_key( hkey, appkey, "UseDGA", buffer, sizeof(buffer) ))
295         usedga = IS_OPTION_TRUE( buffer[0] );
296
297     if (!get_config_key( hkey, appkey, "UseXVidMode", buffer, sizeof(buffer) ))
298         usexvidmode = IS_OPTION_TRUE( buffer[0] );
299
300     if (!get_config_key( hkey, appkey, "UseXRandR", buffer, sizeof(buffer) ))
301         usexrandr = IS_OPTION_TRUE( buffer[0] );
302
303     if (!get_config_key( hkey, appkey, "UseTakeFocus", buffer, sizeof(buffer) ))
304         use_take_focus = IS_OPTION_TRUE( buffer[0] );
305
306     screen_depth = 0;
307     if (!get_config_key( hkey, appkey, "ScreenDepth", buffer, sizeof(buffer) ))
308         screen_depth = atoi(buffer);
309
310     if (!get_config_key( hkey, appkey, "Synchronous", buffer, sizeof(buffer) ))
311         synchronous = IS_OPTION_TRUE( buffer[0] );
312
313     if (!get_config_key( hkey, appkey, "ClientSideWithCore", buffer, sizeof(buffer) ))
314         client_side_with_core = IS_OPTION_TRUE( buffer[0] );
315
316     if (!get_config_key( hkey, appkey, "ClientSideWithRender", buffer, sizeof(buffer) ))
317         client_side_with_render = IS_OPTION_TRUE( buffer[0] );
318
319     if (!get_config_key( hkey, appkey, "ClientSideAntiAliasWithCore", buffer, sizeof(buffer) ))
320         client_side_antialias_with_core = IS_OPTION_TRUE( buffer[0] );
321
322     if (!get_config_key( hkey, appkey, "ClientSideAntiAliasWithRender", buffer, sizeof(buffer) ))
323         client_side_antialias_with_render = IS_OPTION_TRUE( buffer[0] );
324
325     if (!get_config_key( hkey, appkey, "DesktopDoubleBuffered", buffer, sizeof(buffer) ))
326         desktop_dbl_buf = IS_OPTION_TRUE( buffer[0] );
327
328     if (!get_config_key( hkey, appkey, "UseXIM", buffer, sizeof(buffer) ))
329         use_xim = IS_OPTION_TRUE( buffer[0] );
330
331     get_config_key( hkey, appkey, "InputStyle", input_style, sizeof(input_style) );
332
333     if (appkey) RegCloseKey( appkey );
334     RegCloseKey( hkey );
335 }
336
337
338 /***********************************************************************
339  *           X11DRV process initialisation routine
340  */
341 static BOOL process_attach(void)
342 {
343     Display *display;
344
345     get_server_startup();
346     setup_options();
347
348     /* Open display */
349
350     if (!(display = XOpenDisplay( NULL ))) return FALSE;
351
352     fcntl( ConnectionNumber(display), F_SETFD, 1 ); /* set close on exec flag */
353     screen = DefaultScreenOfDisplay( display );
354     visual = DefaultVisual( display, DefaultScreen(display) );
355     root_window = DefaultRootWindow( display );
356     old_error_handler = XSetErrorHandler( error_handler );
357
358     /* Initialize screen depth */
359
360     if (screen_depth)  /* depth specified */
361     {
362         int depth_count, i;
363         int *depth_list = XListDepths(display, DefaultScreen(display), &depth_count);
364         for (i = 0; i < depth_count; i++)
365             if (depth_list[i] == screen_depth) break;
366         XFree( depth_list );
367         if (i >= depth_count)
368         {
369             WARN( "invalid depth %d, using default\n", screen_depth );
370             screen_depth = 0;
371         }
372     }
373     if (!screen_depth) screen_depth = DefaultDepthOfScreen( screen );
374
375     /* Initialize OpenGL */
376     X11DRV_OpenGL_Init(display);
377
378     /* If OpenGL is available, change the default visual, etc as necessary */
379     if (desktop_dbl_buf && (desktop_vi = X11DRV_setup_opengl_visual( display )))
380     {
381         visual       = desktop_vi->visual;
382         screen       = ScreenOfDisplay(display, desktop_vi->screen);
383         screen_depth = desktop_vi->depth;
384     }
385
386     if (synchronous) XSynchronize( display, True );
387
388     screen_width  = WidthOfScreen( screen );
389     screen_height = HeightOfScreen( screen );
390
391     X11DRV_Settings_Init();
392
393     if (desktop_geometry)
394     {
395         root_window = X11DRV_create_desktop( desktop_vi, desktop_geometry );
396         using_wine_desktop = 1;
397     }
398
399     /* initialize GDI */
400     X11DRV_GDI_Initialize( display );
401
402 #ifdef HAVE_LIBXXF86VM
403     /* initialize XVidMode */
404     X11DRV_XF86VM_Init();
405 #endif
406 #ifdef HAVE_LIBXRANDR
407     /* initialize XRandR */
408     X11DRV_XRandR_Init();
409 #endif
410 #ifdef HAVE_LIBXXF86DGA2
411     /* initialize DGA2 */
412     X11DRV_XF86DGA2_Init();
413 #endif
414
415     return TRUE;
416 }
417
418
419 /***********************************************************************
420  *           X11DRV thread termination routine
421  */
422 static void thread_detach(void)
423 {
424     struct x11drv_thread_data *data = NtCurrentTeb()->driver_data;
425
426     if (data)
427     {
428         CloseHandle( data->display_fd );
429         wine_tsx11_lock();
430         XCloseDisplay( data->display );
431         /* if (data->xim) XCloseIM( data->xim ); */ /* crashes Xlib */
432         wine_tsx11_unlock();
433         HeapFree( GetProcessHeap(), 0, data );
434     }
435 }
436
437
438 /***********************************************************************
439  *           X11DRV process termination routine
440  */
441 static void process_detach(void)
442 {
443 #ifdef HAVE_LIBXXF86DGA2
444     /* cleanup DGA2 */
445     X11DRV_XF86DGA2_Cleanup();
446 #endif
447 #ifdef HAVE_LIBXXF86VM
448     /* cleanup XVidMode */
449     X11DRV_XF86VM_Cleanup();
450 #endif
451     if(using_client_side_fonts)
452         X11DRV_XRender_Finalize();
453
454     /* cleanup GDI */
455     X11DRV_GDI_Finalize();
456
457     DeleteCriticalSection( &X11DRV_CritSection );
458 }
459
460
461 /***********************************************************************
462  *           X11DRV thread initialisation routine
463  */
464 struct x11drv_thread_data *x11drv_init_thread_data(void)
465 {
466     struct x11drv_thread_data *data;
467
468     if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) )))
469     {
470         ERR( "could not create data\n" );
471         ExitProcess(1);
472     }
473     wine_tsx11_lock();
474     if (!(data->display = XOpenDisplay(NULL)))
475     {
476         wine_tsx11_unlock();
477         MESSAGE( "x11drv: Can't open display: %s\n", XDisplayName(NULL) );
478         MESSAGE( "Please ensure that your X server is running and that $DISPLAY is set correctly.\n" );
479         ExitProcess(1);
480     }
481
482     fcntl( ConnectionNumber(data->display), F_SETFD, 1 ); /* set close on exec flag */
483
484 #ifdef HAVE_XKB
485     if (use_xkb)
486     {
487         use_xkb = XkbUseExtension( data->display, NULL, NULL );
488         if (use_xkb) XkbSetDetectableAutoRepeat( data->display, True, NULL );
489     }
490 #endif
491
492     if (synchronous) XSynchronize( data->display, True );
493     wine_tsx11_unlock();
494
495     if (use_xim && !(data->xim = X11DRV_SetupXIM( data->display, input_style )))
496         WARN("Input Method is not available\n");
497
498     if (wine_server_fd_to_handle( ConnectionNumber(data->display), GENERIC_READ | SYNCHRONIZE,
499                                   FALSE, &data->display_fd ))
500     {
501         MESSAGE( "x11drv: Can't allocate handle for display fd\n" );
502         ExitProcess(1);
503     }
504     data->process_event_count = 0;
505     data->cursor = None;
506     data->cursor_window = None;
507     data->last_focus = 0;
508     NtCurrentTeb()->driver_data = data;
509     if (desktop_tid) AttachThreadInput( GetCurrentThreadId(), desktop_tid, TRUE );
510     return data;
511 }
512
513
514 /***********************************************************************
515  *           X11DRV initialisation routine
516  */
517 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
518 {
519     BOOL ret = TRUE;
520
521     switch(reason)
522     {
523     case DLL_PROCESS_ATTACH:
524         ret = process_attach();
525         break;
526     case DLL_THREAD_DETACH:
527         thread_detach();
528         break;
529     case DLL_PROCESS_DETACH:
530         process_detach();
531         break;
532     }
533     return ret;
534 }
535
536 /***********************************************************************
537  *              GetScreenSaveActive (X11DRV.@)
538  *
539  * Returns the active status of the screen saver
540  */
541 BOOL X11DRV_GetScreenSaveActive(void)
542 {
543     int timeout, temp;
544     wine_tsx11_lock();
545     XGetScreenSaver(gdi_display, &timeout, &temp, &temp, &temp);
546     wine_tsx11_unlock();
547     return timeout != 0;
548 }
549
550 /***********************************************************************
551  *              SetScreenSaveActive (X11DRV.@)
552  *
553  * Activate/Deactivate the screen saver
554  */
555 void X11DRV_SetScreenSaveActive(BOOL bActivate)
556 {
557     int timeout, interval, prefer_blanking, allow_exposures;
558     static int last_timeout = 15 * 60;
559
560     wine_tsx11_lock();
561     XGetScreenSaver(gdi_display, &timeout, &interval, &prefer_blanking,
562                     &allow_exposures);
563     if (timeout) last_timeout = timeout;
564
565     timeout = bActivate ? last_timeout : 0;
566     XSetScreenSaver(gdi_display, timeout, interval, prefer_blanking,
567                     allow_exposures);
568     wine_tsx11_unlock();
569 }