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