Moved the audio driver configuration to HKCU\Software\Wine\Drivers and
[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 #include "windef.h"
42 #include "winbase.h"
43 #include "wine/winbase16.h"
44 #include "winreg.h"
45
46 #include "x11drv.h"
47 #include "xvidmode.h"
48 #include "xrandr.h"
49 #include "dga2.h"
50 #include "wine/server.h"
51 #include "wine/debug.h"
52
53 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
54
55 static CRITICAL_SECTION X11DRV_CritSection;
56 static CRITICAL_SECTION_DEBUG critsect_debug =
57 {
58     0, 0, &X11DRV_CritSection,
59     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
60       0, 0, { 0, (DWORD)(__FILE__ ": X11DRV_CritSection") }
61 };
62 static CRITICAL_SECTION X11DRV_CritSection = { &critsect_debug, -1, 0, 0, 0, 0 };
63
64 Screen *screen;
65 Visual *visual;
66 unsigned int screen_width;
67 unsigned int screen_height;
68 unsigned int screen_depth;
69 Window root_window;
70 DWORD desktop_tid = 0;
71 int dxgrab = 0;
72 int usedga = 0;
73 int usexvidmode = 0;
74 int usexrandr = 1;
75 int use_xkb = 1;
76 int use_take_focus = 1;
77 int use_primary_selection = 0;
78 int managed_mode = 1;
79 int private_color_map = 0;
80 int client_side_with_core = 1;
81 int client_side_with_render = 1;
82 int client_side_antialias_with_core = 1;
83 int client_side_antialias_with_render = 1;
84 int using_wine_desktop = 0;
85 int copy_default_colors = 128;
86 int alloc_system_colors = 256;
87 DWORD thread_data_tls_index = TLS_OUT_OF_INDEXES;
88
89 static BOOL synchronous;  /* run in synchronous mode? */
90 static BOOL desktop_dbl_buf = TRUE;
91 static char *desktop_geometry;
92 static XVisualInfo *desktop_vi;
93
94 static x11drv_error_callback err_callback;   /* current callback for error */
95 static Display *err_callback_display;        /* display callback is set for */
96 static void *err_callback_arg;               /* error callback argument */
97 static int err_callback_result;              /* error callback result */
98 static unsigned long err_serial;             /* serial number of first request */
99 static int (*old_error_handler)( Display *, XErrorEvent * );
100 static int use_xim = 1;
101 static char input_style[20];
102
103 #define IS_OPTION_TRUE(ch) \
104     ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
105 #define IS_OPTION_FALSE(ch) \
106     ((ch) == 'n' || (ch) == 'N' || (ch) == 'f' || (ch) == 'F' || (ch) == '0')
107
108 /***********************************************************************
109  *              ignore_error
110  *
111  * Check if the X error is one we can ignore.
112  */
113 static inline BOOL ignore_error( Display *display, XErrorEvent *event )
114 {
115     if (event->request_code == X_SetInputFocus && event->error_code == BadMatch) return TRUE;
116     return FALSE;
117 }
118
119
120 /***********************************************************************
121  *              X11DRV_expect_error
122  *
123  * Setup a callback function that will be called on an X error.  The
124  * callback must return non-zero if the error is the one it expected.
125  * This function acquires the x11 lock; X11DRV_check_error must be
126  * called in all cases to release it.
127  */
128 void X11DRV_expect_error( Display *display, x11drv_error_callback callback, void *arg )
129 {
130     wine_tsx11_lock();
131     err_callback         = callback;
132     err_callback_display = display;
133     err_callback_arg     = arg;
134     err_callback_result  = 0;
135     err_serial           = NextRequest(display);
136 }
137
138
139 /***********************************************************************
140  *              X11DRV_check_error
141  *
142  * Check if an expected X11 error occurred; return non-zero if yes.
143  * Also release the x11 lock obtained in X11DRV_expect_error.
144  * The caller is responsible for calling XSync first if necessary.
145  */
146 int X11DRV_check_error(void)
147 {
148     int ret;
149     err_callback = NULL;
150     ret = err_callback_result;
151     wine_tsx11_unlock();
152     return ret;
153 }
154
155
156 /***********************************************************************
157  *              error_handler
158  */
159 static int error_handler( Display *display, XErrorEvent *error_evt )
160 {
161     if (err_callback && display == err_callback_display &&
162         (long)(error_evt->serial - err_serial) >= 0)
163     {
164         if ((err_callback_result = err_callback( display, error_evt, err_callback_arg )))
165         {
166             TRACE( "got expected error %d req %d\n",
167                    error_evt->error_code, error_evt->request_code );
168             return 0;
169         }
170     }
171     if (ignore_error( display, error_evt ))
172     {
173         TRACE( "got ignored error %d req %d\n",
174                error_evt->error_code, error_evt->request_code );
175         return 0;
176     }
177     if (synchronous)
178     {
179         ERR( "X protocol error: serial=%ld, request_code=%d - breaking into debugger\n",
180              error_evt->serial, error_evt->request_code );
181         DebugBreak();  /* force an entry in the debugger */
182     }
183     old_error_handler( display, error_evt );
184     return 0;
185 }
186
187 /***********************************************************************
188  *              wine_tsx11_lock   (X11DRV.@)
189  */
190 void wine_tsx11_lock(void)
191 {
192     EnterCriticalSection( &X11DRV_CritSection );
193 }
194
195 /***********************************************************************
196  *              wine_tsx11_unlock   (X11DRV.@)
197  */
198 void wine_tsx11_unlock(void)
199 {
200     LeaveCriticalSection( &X11DRV_CritSection );
201 }
202
203
204 /***********************************************************************
205  *              get_config_key
206  *
207  * Get a config key from either the app-specific or the default config
208  */
209 inline static DWORD get_config_key( HKEY defkey, HKEY appkey, const char *name,
210                                     char *buffer, DWORD size )
211 {
212     if (appkey && !RegQueryValueExA( appkey, name, 0, NULL, buffer, &size )) return 0;
213     if (defkey && !RegQueryValueExA( defkey, name, 0, NULL, buffer, &size )) return 0;
214     return ERROR_FILE_NOT_FOUND;
215 }
216
217
218 /***********************************************************************
219  *              setup_options
220  *
221  * Setup the x11drv options.
222  */
223 static void setup_options(void)
224 {
225     char buffer[MAX_PATH+16];
226     HKEY hkey, appkey = 0;
227     DWORD len;
228
229     /* @@ Wine registry key: HKCU\Software\Wine\X11 Driver */
230     if (RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\X11 Driver", &hkey )) hkey = 0;
231
232     /* open the app-specific key */
233
234     len = (GetModuleFileNameA( 0, buffer, MAX_PATH ));
235     if (len && len < MAX_PATH)
236     {
237         HKEY tmpkey;
238         char *p, *appname = buffer;
239         if ((p = strrchr( appname, '/' ))) appname = p + 1;
240         if ((p = strrchr( appname, '\\' ))) appname = p + 1;
241         strcat( appname, "\\X11 Driver" );
242         /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\X11 Driver */
243         if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey ))
244         {
245             if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
246             RegCloseKey( tmpkey );
247         }
248     }
249
250     if (!get_config_key( hkey, appkey, "Desktop", buffer, sizeof(buffer) ))
251     {
252         /* Imperfect validation:  If Desktop=N, then we don't turn on
253         ** the --desktop option.  We should really validate for a correct
254         ** sizing entry */
255         if (!IS_OPTION_FALSE(buffer[0])) desktop_geometry = strdup(buffer);
256     }
257
258     if (!get_config_key( hkey, appkey, "Managed", buffer, sizeof(buffer) ))
259         managed_mode = IS_OPTION_TRUE( buffer[0] );
260
261     if (!get_config_key( hkey, appkey, "DXGrab", buffer, sizeof(buffer) ))
262         dxgrab = IS_OPTION_TRUE( buffer[0] );
263
264     if (!get_config_key( hkey, appkey, "UseDGA", buffer, sizeof(buffer) ))
265         usedga = IS_OPTION_TRUE( buffer[0] );
266
267     if (!get_config_key( hkey, appkey, "UseXVidMode", buffer, sizeof(buffer) ))
268         usexvidmode = IS_OPTION_TRUE( buffer[0] );
269
270     if (!get_config_key( hkey, appkey, "UseXRandR", buffer, sizeof(buffer) ))
271         usexrandr = IS_OPTION_TRUE( buffer[0] );
272
273     if (!get_config_key( hkey, appkey, "UseTakeFocus", buffer, sizeof(buffer) ))
274         use_take_focus = IS_OPTION_TRUE( buffer[0] );
275
276     if (!get_config_key( hkey, appkey, "UsePrimarySelection", buffer, sizeof(buffer) ))
277         use_primary_selection = IS_OPTION_TRUE( buffer[0] );
278
279     screen_depth = 0;
280     if (!get_config_key( hkey, appkey, "ScreenDepth", buffer, sizeof(buffer) ))
281         screen_depth = atoi(buffer);
282
283     if (!get_config_key( hkey, appkey, "Synchronous", buffer, sizeof(buffer) ))
284         synchronous = IS_OPTION_TRUE( buffer[0] );
285
286     if (!get_config_key( hkey, appkey, "ClientSideWithCore", buffer, sizeof(buffer) ))
287         client_side_with_core = IS_OPTION_TRUE( buffer[0] );
288
289     if (!get_config_key( hkey, appkey, "ClientSideWithRender", buffer, sizeof(buffer) ))
290         client_side_with_render = IS_OPTION_TRUE( buffer[0] );
291
292     if (!get_config_key( hkey, appkey, "ClientSideAntiAliasWithCore", buffer, sizeof(buffer) ))
293         client_side_antialias_with_core = IS_OPTION_TRUE( buffer[0] );
294
295     if (!get_config_key( hkey, appkey, "ClientSideAntiAliasWithRender", buffer, sizeof(buffer) ))
296         client_side_antialias_with_render = IS_OPTION_TRUE( buffer[0] );
297
298     if (!get_config_key( hkey, appkey, "DesktopDoubleBuffered", buffer, sizeof(buffer) ))
299         desktop_dbl_buf = IS_OPTION_TRUE( buffer[0] );
300
301     if (!get_config_key( hkey, appkey, "UseXIM", buffer, sizeof(buffer) ))
302         use_xim = IS_OPTION_TRUE( buffer[0] );
303
304     if (!get_config_key( hkey, appkey, "PrivateColorMap", buffer, sizeof(buffer) ))
305         private_color_map = IS_OPTION_TRUE( buffer[0] );
306
307     if (!get_config_key( hkey, appkey, "CopyDefaultColors", buffer, sizeof(buffer) ))
308         copy_default_colors = atoi(buffer);
309
310     if (!get_config_key( hkey, appkey, "AllocSystemColors", buffer, sizeof(buffer) ))
311         alloc_system_colors = atoi(buffer);
312
313     get_config_key( hkey, appkey, "InputStyle", input_style, sizeof(input_style) );
314
315     if (appkey) RegCloseKey( appkey );
316     if (hkey) RegCloseKey( hkey );
317 }
318
319
320 /***********************************************************************
321  *           X11DRV process initialisation routine
322  */
323 static BOOL process_attach(void)
324 {
325     Display *display;
326
327     setup_options();
328
329     if ((thread_data_tls_index = TlsAlloc()) == TLS_OUT_OF_INDEXES) return FALSE;
330
331     /* Open display */
332
333     if (!(display = XOpenDisplay( NULL ))) return FALSE;
334
335     fcntl( ConnectionNumber(display), F_SETFD, 1 ); /* set close on exec flag */
336     screen = DefaultScreenOfDisplay( display );
337     visual = DefaultVisual( display, DefaultScreen(display) );
338     root_window = DefaultRootWindow( display );
339     old_error_handler = XSetErrorHandler( error_handler );
340
341     /* Initialize screen depth */
342
343     if (screen_depth)  /* depth specified */
344     {
345         int depth_count, i;
346         int *depth_list = XListDepths(display, DefaultScreen(display), &depth_count);
347         for (i = 0; i < depth_count; i++)
348             if (depth_list[i] == screen_depth) break;
349         XFree( depth_list );
350         if (i >= depth_count)
351         {
352             WARN( "invalid depth %d, using default\n", screen_depth );
353             screen_depth = 0;
354         }
355     }
356     if (!screen_depth) screen_depth = DefaultDepthOfScreen( screen );
357
358     /* Initialize OpenGL */
359     X11DRV_OpenGL_Init(display);
360
361     /* If OpenGL is available, change the default visual, etc as necessary */
362     if (desktop_dbl_buf && (desktop_vi = X11DRV_setup_opengl_visual( display )))
363     {
364         visual       = desktop_vi->visual;
365         screen       = ScreenOfDisplay(display, desktop_vi->screen);
366         screen_depth = desktop_vi->depth;
367     }
368
369     if (synchronous) XSynchronize( display, True );
370
371     screen_width  = WidthOfScreen( screen );
372     screen_height = HeightOfScreen( screen );
373
374     X11DRV_Settings_Init();
375
376     if (desktop_geometry)
377     {
378         root_window = X11DRV_create_desktop( desktop_vi, desktop_geometry );
379         using_wine_desktop = 1;
380     }
381
382     /* initialize GDI */
383     X11DRV_GDI_Initialize( display );
384
385 #ifdef HAVE_LIBXXF86VM
386     /* initialize XVidMode */
387     X11DRV_XF86VM_Init();
388 #endif
389 #ifdef HAVE_LIBXRANDR
390     /* initialize XRandR */
391     X11DRV_XRandR_Init();
392 #endif
393 #ifdef HAVE_LIBXXF86DGA2
394     /* initialize DGA2 */
395     X11DRV_XF86DGA2_Init();
396 #endif
397
398     X11DRV_InitKeyboard();
399
400     return TRUE;
401 }
402
403
404 /***********************************************************************
405  *           X11DRV thread termination routine
406  */
407 static void thread_detach(void)
408 {
409     struct x11drv_thread_data *data = TlsGetValue( thread_data_tls_index );
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     /* cleanup GDI */
440     X11DRV_GDI_Finalize();
441
442     DeleteCriticalSection( &X11DRV_CritSection );
443     TlsFree( thread_data_tls_index );
444 }
445
446
447 /***********************************************************************
448  *           X11DRV thread initialisation routine
449  */
450 struct x11drv_thread_data *x11drv_init_thread_data(void)
451 {
452     struct x11drv_thread_data *data;
453
454     if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) )))
455     {
456         ERR( "could not create data\n" );
457         ExitProcess(1);
458     }
459     wine_tsx11_lock();
460     if (!(data->display = XOpenDisplay(NULL)))
461     {
462         wine_tsx11_unlock();
463         MESSAGE( "x11drv: Can't open display: %s\n", XDisplayName(NULL) );
464         MESSAGE( "Please ensure that your X server is running and that $DISPLAY is set correctly.\n" );
465         ExitProcess(1);
466     }
467
468     fcntl( ConnectionNumber(data->display), F_SETFD, 1 ); /* set close on exec flag */
469
470 #ifdef HAVE_XKB
471     if (use_xkb)
472     {
473         use_xkb = XkbUseExtension( data->display, NULL, NULL );
474         if (use_xkb) XkbSetDetectableAutoRepeat( data->display, True, NULL );
475     }
476 #endif
477
478     if (synchronous) XSynchronize( data->display, True );
479     wine_tsx11_unlock();
480
481     if (use_xim && !(data->xim = X11DRV_SetupXIM( data->display, input_style )))
482         WARN("Input Method is not available\n");
483
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->grab_window = None;
494     data->last_focus = 0;
495     data->selection_wnd = 0;
496     TlsSetValue( thread_data_tls_index, data );
497     if (desktop_tid) AttachThreadInput( GetCurrentThreadId(), desktop_tid, TRUE );
498     return data;
499 }
500
501
502 /***********************************************************************
503  *           X11DRV initialisation routine
504  */
505 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
506 {
507     BOOL ret = TRUE;
508
509     switch(reason)
510     {
511     case DLL_PROCESS_ATTACH:
512         ret = process_attach();
513         break;
514     case DLL_THREAD_DETACH:
515         thread_detach();
516         break;
517     case DLL_PROCESS_DETACH:
518         process_detach();
519         break;
520     }
521     return ret;
522 }
523
524 /***********************************************************************
525  *              GetScreenSaveActive (X11DRV.@)
526  *
527  * Returns the active status of the screen saver
528  */
529 BOOL X11DRV_GetScreenSaveActive(void)
530 {
531     int timeout, temp;
532     wine_tsx11_lock();
533     XGetScreenSaver(gdi_display, &timeout, &temp, &temp, &temp);
534     wine_tsx11_unlock();
535     return timeout != 0;
536 }
537
538 /***********************************************************************
539  *              SetScreenSaveActive (X11DRV.@)
540  *
541  * Activate/Deactivate the screen saver
542  */
543 void X11DRV_SetScreenSaveActive(BOOL bActivate)
544 {
545     int timeout, interval, prefer_blanking, allow_exposures;
546     static int last_timeout = 15 * 60;
547
548     wine_tsx11_lock();
549     XGetScreenSaver(gdi_display, &timeout, &interval, &prefer_blanking,
550                     &allow_exposures);
551     if (timeout) last_timeout = timeout;
552
553     timeout = bActivate ? last_timeout : 0;
554     XSetScreenSaver(gdi_display, timeout, interval, prefer_blanking,
555                     allow_exposures);
556     wine_tsx11_unlock();
557 }