If using the default values, also set dwType to REG_SZ as our default
[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 "ts_xlib.h"
37 #ifdef HAVE_XKB
38 #include <X11/XKBlib.h>
39 #endif
40
41 #include "windef.h"
42 #include "winbase.h"
43 #include "wine/winbase16.h"
44 #include "winreg.h"
45
46 #include "gdi.h"
47 #include "user.h"
48 #include "win.h"
49 #include "x11drv.h"
50 #include "xvidmode.h"
51 #include "xrandr.h"
52 #include "dga2.h"
53 #include "wine/server.h"
54 #include "wine/debug.h"
55
56 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
57
58 static CRITICAL_SECTION X11DRV_CritSection;
59 static CRITICAL_SECTION_DEBUG critsect_debug =
60 {
61     0, 0, &X11DRV_CritSection,
62     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
63       0, 0, { 0, (DWORD)(__FILE__ ": X11DRV_CritSection") }
64 };
65 static CRITICAL_SECTION X11DRV_CritSection = { &critsect_debug, -1, 0, 0, 0, 0 };
66
67 Screen *screen;
68 Visual *visual;
69 unsigned int screen_width;
70 unsigned int screen_height;
71 unsigned int screen_depth;
72 Window root_window;
73 DWORD desktop_tid = 0;
74 int dxgrab, usedga, usexvidmode, usexrandr;
75 int use_xkb = 1;
76 int use_take_focus = 1;
77 int managed_mode = 1;
78 int client_side_with_core = 1;
79 int client_side_with_render = 1;
80 int client_side_antialias_with_core = 1;
81 int client_side_antialias_with_render = 1;
82
83 unsigned int X11DRV_server_startticks;
84
85 static BOOL synchronous;  /* run in synchronous mode? */
86 static BOOL desktop_dbl_buf;
87 static char *desktop_geometry;
88 static XVisualInfo *desktop_vi;
89
90 static x11drv_error_callback err_callback;   /* current callback for error */
91 static Display *err_callback_display;        /* display callback is set for */
92 static void *err_callback_arg;               /* error callback argument */
93 static int err_callback_result;              /* error callback result */
94 static unsigned long err_serial;             /* serial number of first request */
95 static int (*old_error_handler)( Display *, XErrorEvent * );
96
97 #define IS_OPTION_TRUE(ch) \
98     ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
99 #define IS_OPTION_FALSE(ch) \
100     ((ch) == 'n' || (ch) == 'N' || (ch) == 'f' || (ch) == 'F' || (ch) == '0')
101
102 /***********************************************************************
103  *              X11DRV_expect_error
104  *
105  * Setup a callback function that will be called on an X error.  The
106  * callback must return non-zero if the error is the one it expected.
107  * This function acquires the x11 lock; X11DRV_check_error must be
108  * called in all cases to release it.
109  */
110 void X11DRV_expect_error( Display *display, x11drv_error_callback callback, void *arg )
111 {
112     wine_tsx11_lock();
113     err_callback         = callback;
114     err_callback_display = display;
115     err_callback_arg     = arg;
116     err_callback_result  = 0;
117     err_serial           = NextRequest(display);
118 }
119
120
121 /***********************************************************************
122  *              X11DRV_check_error
123  *
124  * Check if an expected X11 error occurred; return non-zero if yes.
125  * Also release the x11 lock obtained in X11DRV_expect_error.
126  * The caller is responsible for calling XSync first if necessary.
127  */
128 int X11DRV_check_error(void)
129 {
130     int ret;
131     err_callback = NULL;
132     ret = err_callback_result;
133     wine_tsx11_unlock();
134     return ret;
135 }
136
137
138 /***********************************************************************
139  *              error_handler
140  */
141 static int error_handler( Display *display, XErrorEvent *error_evt )
142 {
143     if (err_callback && display == err_callback_display &&
144         (long)(error_evt->serial - err_serial) >= 0)
145     {
146         if ((err_callback_result = err_callback( display, error_evt, err_callback_arg )))
147         {
148             TRACE( "got expected error\n" );
149             return 0;
150         }
151     }
152     if (synchronous) DebugBreak();  /* force an entry in the debugger */
153     old_error_handler( display, error_evt );
154     return 0;
155 }
156
157 /***********************************************************************
158  *              wine_tsx11_lock   (X11DRV.@)
159  */
160 void wine_tsx11_lock(void)
161 {
162     EnterCriticalSection( &X11DRV_CritSection );
163 }
164
165 /***********************************************************************
166  *              wine_tsx11_unlock   (X11DRV.@)
167  */
168 void wine_tsx11_unlock(void)
169 {
170     LeaveCriticalSection( &X11DRV_CritSection );
171 }
172
173 /***********************************************************************
174  *              get_server_startup
175  *
176  * Get the server startup time
177  * Won't be exact, but should be sufficient
178  */
179 static void get_server_startup(void)
180 {
181     struct timeval t;
182     gettimeofday( &t, NULL );
183     X11DRV_server_startticks = ((t.tv_sec * 1000) + (t.tv_usec / 1000)) - GetTickCount();
184 }
185
186
187 /***********************************************************************
188  *              get_config_key
189  *
190  * Get a config key from either the app-specific or the default config
191  */
192 inline static DWORD get_config_key( HKEY defkey, HKEY appkey, const char *name,
193                                     char *buffer, DWORD size )
194 {
195     if (appkey && !RegQueryValueExA( appkey, name, 0, NULL, buffer, &size )) return 0;
196     return RegQueryValueExA( defkey, name, 0, NULL, buffer, &size );
197 }
198
199
200 /***********************************************************************
201  *              setup_options
202  *
203  * Setup the x11drv options.
204  */
205 static void setup_options(void)
206 {
207     char buffer[MAX_PATH+16];
208     HKEY hkey, appkey = 0;
209     DWORD count;
210
211     if (RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\x11drv", 0, NULL,
212                          REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, NULL ))
213     {
214         ERR("Cannot create config registry key\n" );
215         ExitProcess(1);
216     }
217
218     /* open the app-specific key */
219
220     if (GetModuleFileNameA( 0, buffer, MAX_PATH ))
221     {
222         HKEY tmpkey;
223         char *p, *appname = buffer;
224         if ((p = strrchr( appname, '/' ))) appname = p + 1;
225         if ((p = strrchr( appname, '\\' ))) appname = p + 1;
226         strcat( appname, "\\x11drv" );
227         if (!RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\AppDefaults", &tmpkey ))
228         {
229             if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
230             RegCloseKey( tmpkey );
231         }
232     }
233
234     /* get the display name */
235
236     strcpy( buffer, "DISPLAY=" );
237     count = sizeof(buffer) - 8;
238     if (!RegQueryValueExA( hkey, "display", 0, NULL, buffer + 8, &count ))
239     {
240         const char *display_name = getenv( "DISPLAY" );
241         if (display_name && strcmp( buffer, display_name ))
242             MESSAGE( "x11drv: Warning: $DISPLAY variable ignored, using '%s' specified in config file\n",
243                      buffer + 8 );
244         putenv( strdup(buffer) );
245     }
246
247     if (!get_config_key( hkey, appkey, "Desktop", buffer, sizeof(buffer) ))
248     {
249         /* Imperfect validation:  If Desktop=N, then we don't turn on
250         ** the --desktop option.  We should really validate for a correct
251         ** sizing entry */
252         if (!IS_OPTION_FALSE(buffer[0])) desktop_geometry = strdup(buffer);
253     }
254
255     if (!get_config_key( hkey, appkey, "Managed", buffer, sizeof(buffer) ))
256         managed_mode = IS_OPTION_TRUE( buffer[0] );
257
258     if (!get_config_key( hkey, appkey, "DXGrab", buffer, sizeof(buffer) ))
259         dxgrab = IS_OPTION_TRUE( buffer[0] );
260
261     if (!get_config_key( hkey, appkey, "UseDGA", buffer, sizeof(buffer) ))
262         usedga = IS_OPTION_TRUE( buffer[0] );
263
264     if (!get_config_key( hkey, appkey, "UseXVidMode", buffer, sizeof(buffer) ))
265         usexvidmode = IS_OPTION_TRUE( buffer[0] );
266
267     if (!get_config_key( hkey, appkey, "UseXRandR", buffer, sizeof(buffer) ))
268         usexrandr = IS_OPTION_TRUE( buffer[0] );
269
270     if (!get_config_key( hkey, appkey, "UseTakeFocus", buffer, sizeof(buffer) ))
271         use_take_focus = IS_OPTION_TRUE( buffer[0] );
272
273     screen_depth = 0;
274     if (!get_config_key( hkey, appkey, "ScreenDepth", buffer, sizeof(buffer) ))
275         screen_depth = atoi(buffer);
276
277     if (!get_config_key( hkey, appkey, "Synchronous", buffer, sizeof(buffer) ))
278         synchronous = IS_OPTION_TRUE( buffer[0] );
279
280     if (!get_config_key( hkey, appkey, "ClientSideWithCore", buffer, sizeof(buffer) ))
281         client_side_with_core = IS_OPTION_TRUE( buffer[0] );
282
283     if (!get_config_key( hkey, appkey, "ClientSideWithRender", buffer, sizeof(buffer) ))
284         client_side_with_render = IS_OPTION_TRUE( buffer[0] );
285
286     if (!get_config_key( hkey, appkey, "ClientSideAntiAliasWithCore", buffer, sizeof(buffer) ))
287         client_side_antialias_with_core = IS_OPTION_TRUE( buffer[0] );
288
289     if (!get_config_key( hkey, appkey, "ClientSideAntiAliasWithRender", buffer, sizeof(buffer) ))
290         client_side_antialias_with_render = IS_OPTION_TRUE( buffer[0] );
291
292     if (!get_config_key( hkey, appkey, "DesktopDoubleBuffered", buffer, sizeof(buffer) ))
293         desktop_dbl_buf = IS_OPTION_TRUE( buffer[0] );
294
295     if (appkey) RegCloseKey( appkey );
296     RegCloseKey( hkey );
297 }
298
299
300 /***********************************************************************
301  *           X11DRV process initialisation routine
302  */
303 static void process_attach(void)
304 {
305     Display *display;
306
307     get_server_startup();
308     setup_options();
309
310     /* Open display */
311
312     if (!(display = TSXOpenDisplay( NULL )))
313     {
314         MESSAGE( "x11drv: Can't open display: %s\n", XDisplayName(NULL) );
315         ExitProcess(1);
316     }
317     fcntl( ConnectionNumber(display), F_SETFD, 1 ); /* set close on exec flag */
318     screen = DefaultScreenOfDisplay( display );
319     visual = DefaultVisual( display, DefaultScreen(display) );
320     root_window = DefaultRootWindow( display );
321     old_error_handler = XSetErrorHandler( error_handler );
322
323     /* Initialize screen depth */
324
325     if (screen_depth)  /* depth specified */
326     {
327         int depth_count, i;
328         int *depth_list = TSXListDepths(display, DefaultScreen(display), &depth_count);
329         for (i = 0; i < depth_count; i++)
330             if (depth_list[i] == screen_depth) break;
331         TSXFree( depth_list );
332         if (i >= depth_count)
333         {
334             MESSAGE( "x11drv: Depth %d not supported on this screen.\n", screen_depth );
335             ExitProcess(1);
336         }
337     }
338     else screen_depth = DefaultDepthOfScreen( screen );
339
340     /* check for Xkb extension */
341 #ifdef HAVE_XKB
342     if (use_xkb)
343     {
344         int xkb_opcode, xkb_event, xkb_error;
345         int xkb_major = XkbMajorVersion, xkb_minor = XkbMinorVersion;
346
347         use_xkb = XkbQueryExtension(display, &xkb_opcode, &xkb_event, &xkb_error,
348                                     &xkb_major, &xkb_minor);
349         if (use_xkb) /* we have XKB, approximate Windows behaviour */
350             XkbSetDetectableAutoRepeat(display, True, NULL);
351     }
352 #endif
353
354     /* Initialize OpenGL */
355     X11DRV_OpenGL_Init(display);
356
357     /* If OpenGL is available, change the default visual, etc as necessary */
358     if (desktop_dbl_buf && (desktop_vi = X11DRV_setup_opengl_visual( display )))
359     {
360         visual       = desktop_vi->visual;
361         screen       = ScreenOfDisplay(display, desktop_vi->screen);
362         screen_depth = desktop_vi->depth;
363     }
364
365     if (synchronous) XSynchronize( display, True );
366
367     screen_width  = WidthOfScreen( screen );
368     screen_height = HeightOfScreen( screen );
369
370     X11DRV_Settings_Init();
371
372     if (desktop_geometry)
373         root_window = X11DRV_create_desktop( desktop_vi, desktop_geometry );
374
375     /* initialize GDI */
376     if(!X11DRV_GDI_Initialize( display ))
377     {
378         ERR( "Couldn't Initialize GDI.\n" );
379         ExitProcess(1);
380     }
381
382     /* Initialize clipboard */
383     if (!X11DRV_InitClipboard( display ))
384     {
385         ERR( "Couldn't Initialize clipboard.\n" );
386         ExitProcess(1);
387     }
388
389 #ifdef HAVE_LIBXXF86VM
390     /* initialize XVidMode */
391     X11DRV_XF86VM_Init();
392 #endif
393 #ifdef HAVE_LIBXRANDR
394     /* initialize XRandR */
395     X11DRV_XRandR_Init();
396 #endif
397 #ifdef HAVE_LIBXXF86DGA2
398     /* initialize DGA2 */
399     X11DRV_XF86DGA2_Init();
400 #endif
401 }
402
403
404 /***********************************************************************
405  *           X11DRV thread termination routine
406  */
407 static void thread_detach(void)
408 {
409     struct x11drv_thread_data *data = NtCurrentTeb()->driver_data;
410
411     if (data)
412     {
413         CloseHandle( data->display_fd );
414         wine_tsx11_lock();
415         XCloseDisplay( data->display );
416         /* if (data->xim) XCloseIM( data->xim ); */ /* crashes Xlib */
417         wine_tsx11_unlock();
418         HeapFree( GetProcessHeap(), 0, data );
419     }
420 }
421
422
423 /***********************************************************************
424  *           X11DRV process termination routine
425  */
426 static void process_detach(void)
427 {
428 #ifdef HAVE_LIBXXF86DGA2
429     /* cleanup DGA2 */
430     X11DRV_XF86DGA2_Cleanup();
431 #endif
432 #ifdef HAVE_LIBXXF86VM
433     /* cleanup XVidMode */
434     X11DRV_XF86VM_Cleanup();
435 #endif
436     if(using_client_side_fonts)
437         X11DRV_XRender_Finalize();
438
439     /* FIXME: should detach all threads */
440     thread_detach();
441
442     /* cleanup GDI */
443     X11DRV_GDI_Finalize();
444
445     DeleteCriticalSection( &X11DRV_CritSection );
446 }
447
448
449 /***********************************************************************
450  *           X11DRV thread initialisation routine
451  */
452 struct x11drv_thread_data *x11drv_init_thread_data(void)
453 {
454     struct x11drv_thread_data *data;
455
456     if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) )))
457     {
458         ERR( "could not create data\n" );
459         ExitProcess(1);
460     }
461     wine_tsx11_lock();
462     if (!(data->display = XOpenDisplay(NULL)))
463     {
464         wine_tsx11_unlock();
465         MESSAGE( "x11drv: Can't open display: %s\n", XDisplayName(NULL) );
466         ExitProcess(1);
467     }
468     fcntl( ConnectionNumber(data->display), F_SETFD, 1 ); /* set close on exec flag */
469
470     if ((data->xim = XOpenIM( data->display, NULL, NULL, NULL )))
471     {
472         TRACE("X display of IM = %p\n", XDisplayOfIM(data->xim));
473         TRACE("Using %s locale of Input Method\n", XLocaleOfIM(data->xim));
474     }
475     else
476         WARN("Can't open input method\n");
477
478 #ifdef HAVE_XKB
479     if (use_xkb) XkbSetDetectableAutoRepeat( data->display, True, NULL );
480 #endif
481
482     if (synchronous) XSynchronize( data->display, True );
483     wine_tsx11_unlock();
484     if (wine_server_fd_to_handle( ConnectionNumber(data->display), GENERIC_READ | SYNCHRONIZE,
485                                   FALSE, &data->display_fd ))
486     {
487         MESSAGE( "x11drv: Can't allocate handle for display fd\n" );
488         ExitProcess(1);
489     }
490     data->process_event_count = 0;
491     data->cursor = None;
492     data->cursor_window = None;
493     data->last_focus = 0;
494     NtCurrentTeb()->driver_data = data;
495     if (desktop_tid) AttachThreadInput( GetCurrentThreadId(), desktop_tid, TRUE );
496     return data;
497 }
498
499
500 /***********************************************************************
501  *           X11DRV initialisation routine
502  */
503 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
504 {
505     switch(reason)
506     {
507     case DLL_PROCESS_ATTACH:
508         process_attach();
509         break;
510     case DLL_THREAD_DETACH:
511         thread_detach();
512         break;
513     case DLL_PROCESS_DETACH:
514         process_detach();
515         break;
516     }
517     return TRUE;
518 }
519
520 /***********************************************************************
521  *              GetScreenSaveActive (X11DRV.@)
522  *
523  * Returns the active status of the screen saver
524  */
525 BOOL X11DRV_GetScreenSaveActive(void)
526 {
527     int timeout, temp;
528     TSXGetScreenSaver(gdi_display, &timeout, &temp, &temp, &temp);
529     return timeout != 0;
530 }
531
532 /***********************************************************************
533  *              SetScreenSaveActive (X11DRV.@)
534  *
535  * Activate/Deactivate the screen saver
536  */
537 void X11DRV_SetScreenSaveActive(BOOL bActivate)
538 {
539     int timeout, interval, prefer_blanking, allow_exposures;
540     static int last_timeout = 15 * 60;
541
542     TSXGetScreenSaver(gdi_display, &timeout, &interval, &prefer_blanking,
543                       &allow_exposures);
544     if (timeout) last_timeout = timeout;
545
546     timeout = bActivate ? last_timeout : 0;
547     TSXSetScreenSaver(gdi_display, timeout, interval, prefer_blanking,
548                       allow_exposures);
549 }