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