2 * X11DRV initialization code
4 * Copyright 1998 Patrik Stridvall
5 * Copyright 2000 Alexandre Julliard
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.
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.
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
28 #ifdef HAVE_SYS_TIME_H
29 # include <sys/time.h>
34 #include <X11/cursorfont.h>
40 #include "wine/winbase16.h"
50 #include "wine/server.h"
51 #include "wine/debug.h"
53 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
55 static void (*old_tsx11_lock)(void);
56 static void (*old_tsx11_unlock)(void);
58 static CRITICAL_SECTION X11DRV_CritSection = CRITICAL_SECTION_INIT("X11DRV_CritSection");
62 unsigned int screen_width;
63 unsigned int screen_height;
64 unsigned int screen_depth;
66 int dxgrab, usedga, usexvidmode;
67 int use_take_focus = 1;
70 unsigned int X11DRV_server_startticks;
72 static BOOL synchronous; /* run in synchronous mode? */
73 static char *desktop_geometry;
74 static XVisualInfo *desktop_vi;
76 static x11drv_error_callback err_callback; /* current callback for error */
77 static Display *err_callback_display; /* display callback is set for */
78 static void *err_callback_arg; /* error callback argument */
79 static int err_callback_result; /* error callback result */
80 static int (*old_error_handler)( Display *, XErrorEvent * );
82 #define IS_OPTION_TRUE(ch) \
83 ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
84 #define IS_OPTION_FALSE(ch) \
85 ((ch) == 'n' || (ch) == 'N' || (ch) == 'f' || (ch) == 'F' || (ch) == '0')
87 /***********************************************************************
90 * Setup a callback function that will be called on an X error. The
91 * callback must return non-zero if the error is the one it expected.
92 * This function acquires the x11 lock; X11DRV_check_error must be
93 * called in all cases to release it.
95 void X11DRV_expect_error( Display *display, x11drv_error_callback callback, void *arg )
98 XSync( display, False );
99 err_callback = callback;
100 err_callback_display = display;
101 err_callback_arg = arg;
102 err_callback_result = 0;
106 /***********************************************************************
109 * Check if an expected X11 error occurred; return non-zero if yes.
110 * Also release the x11 lock obtained in X11DRV_expect_error.
112 int X11DRV_check_error(void)
115 XSync( err_callback_display, False );
117 ret = err_callback_result;
123 /***********************************************************************
126 static int error_handler( Display *display, XErrorEvent *error_evt )
128 if (err_callback && display == err_callback_display)
130 if ((err_callback_result = err_callback( display, error_evt, err_callback_arg )))
132 TRACE( "got expected error\n" );
136 if (synchronous) DebugBreak(); /* force an entry in the debugger */
137 old_error_handler( display, error_evt );
141 /***********************************************************************
144 static void lock_tsx11(void)
146 EnterCriticalSection( &X11DRV_CritSection );
149 /***********************************************************************
152 static void unlock_tsx11(void)
154 LeaveCriticalSection( &X11DRV_CritSection );
157 /***********************************************************************
160 * Get the server startup time
161 * Won't be exact, but should be sufficient
163 static void get_server_startup(void)
166 gettimeofday( &t, NULL );
167 X11DRV_server_startticks = ((t.tv_sec * 1000) + (t.tv_usec / 1000)) - GetTickCount();
171 /***********************************************************************
174 * Get a config key from either the app-specific or the default config
176 inline static DWORD get_config_key( HKEY defkey, HKEY appkey, const char *name,
177 char *buffer, DWORD size )
179 if (appkey && !RegQueryValueExA( appkey, name, 0, NULL, buffer, &size )) return 0;
180 return RegQueryValueExA( defkey, name, 0, NULL, buffer, &size );
184 /***********************************************************************
187 * Setup the x11drv options.
189 static void setup_options(void)
191 char buffer[MAX_PATH+16];
192 HKEY hkey, appkey = 0;
195 if (RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\x11drv", 0, NULL,
196 REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, NULL ))
198 ERR("Cannot create config registry key\n" );
202 /* open the app-specific key */
204 if (GetModuleFileNameA( 0, buffer, MAX_PATH ))
207 char *p, *appname = buffer;
208 if ((p = strrchr( appname, '/' ))) appname = p + 1;
209 if ((p = strrchr( appname, '\\' ))) appname = p + 1;
210 strcat( appname, "\\x11drv" );
211 if (!RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\AppDefaults", &tmpkey ))
213 if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
214 RegCloseKey( tmpkey );
218 /* get the display name */
220 strcpy( buffer, "DISPLAY=" );
221 count = sizeof(buffer) - 8;
222 if (!RegQueryValueExA( hkey, "display", 0, NULL, buffer + 8, &count ))
224 const char *display_name = getenv( "DISPLAY" );
225 if (display_name && strcmp( buffer, display_name ))
226 MESSAGE( "x11drv: Warning: $DISPLAY variable ignored, using '%s' specified in config file\n",
228 putenv( strdup(buffer) );
231 if (!get_config_key( hkey, appkey, "Desktop", buffer, sizeof(buffer) ))
233 /* Imperfect validation: If Desktop=N, then we don't turn on
234 ** the --desktop option. We should really validate for a correct
236 if (!IS_OPTION_FALSE(buffer[0])) desktop_geometry = strdup(buffer);
239 if (!get_config_key( hkey, appkey, "Managed", buffer, sizeof(buffer) ))
240 managed_mode = IS_OPTION_TRUE( buffer[0] );
242 if (!get_config_key( hkey, appkey, "DXGrab", buffer, sizeof(buffer) ))
243 dxgrab = IS_OPTION_TRUE( buffer[0] );
245 if (!get_config_key( hkey, appkey, "UseDGA", buffer, sizeof(buffer) ))
246 usedga = IS_OPTION_TRUE( buffer[0] );
248 if (!get_config_key( hkey, appkey, "UseXVidMode", buffer, sizeof(buffer) ))
249 usexvidmode = IS_OPTION_TRUE( buffer[0] );
251 if (!get_config_key( hkey, appkey, "UseTakeFocus", buffer, sizeof(buffer) ))
252 use_take_focus = IS_OPTION_TRUE( buffer[0] );
255 if (!get_config_key( hkey, appkey, "ScreenDepth", buffer, sizeof(buffer) ))
256 screen_depth = atoi(buffer);
258 if (!get_config_key( hkey, appkey, "Synchronous", buffer, sizeof(buffer) ))
259 synchronous = IS_OPTION_TRUE( buffer[0] );
261 if (appkey) RegCloseKey( appkey );
266 /***********************************************************************
267 * setup_opengl_visual
269 * Setup the default visual used for OpenGL and Direct3D, and the desktop
270 * window (if it exists). If OpenGL isn't available, the visual is simply
271 * set to the default visual for the display
274 static void setup_opengl_visual( Display *display )
276 int err_base, evt_base;
278 /* In order to support OpenGL or D3D, we require a double-buffered
280 if (glXQueryExtension(display, &err_base, &evt_base) == True) {
281 int dblBuf[]={GLX_RGBA,GLX_DEPTH_SIZE,16,GLX_DOUBLEBUFFER,None};
284 desktop_vi = glXChooseVisual(display, DefaultScreen(display), dblBuf);
288 if (desktop_vi != NULL) {
289 visual = desktop_vi->visual;
290 screen = ScreenOfDisplay(display, desktop_vi->screen);
291 screen_depth = desktop_vi->depth;
294 #endif /* HAVE_OPENGL */
296 /***********************************************************************
297 * X11DRV process initialisation routine
299 static void process_attach(void)
303 get_server_startup();
306 /* setup TSX11 locking */
307 old_tsx11_lock = wine_tsx11_lock;
308 old_tsx11_unlock = wine_tsx11_unlock;
309 wine_tsx11_lock = lock_tsx11;
310 wine_tsx11_unlock = unlock_tsx11;
314 if (!(display = TSXOpenDisplay( NULL )))
316 MESSAGE( "x11drv: Can't open display: %s\n", XDisplayName(NULL) );
319 fcntl( ConnectionNumber(display), F_SETFD, 1 ); /* set close on exec flag */
320 screen = DefaultScreenOfDisplay( display );
321 visual = DefaultVisual( display, DefaultScreen(display) );
322 root_window = DefaultRootWindow( display );
323 old_error_handler = XSetErrorHandler( error_handler );
325 /* Initialize screen depth */
327 if (screen_depth) /* depth specified */
330 int *depth_list = TSXListDepths(display, DefaultScreen(display), &depth_count);
331 for (i = 0; i < depth_count; i++)
332 if (depth_list[i] == screen_depth) break;
333 TSXFree( depth_list );
334 if (i >= depth_count)
336 MESSAGE( "x11drv: Depth %d not supported on this screen.\n", screen_depth );
340 else screen_depth = DefaultDepthOfScreen( screen );
342 /* If OpenGL is available, change the default visual, etc as necessary */
344 setup_opengl_visual( display );
345 #endif /* HAVE_OPENGL */
347 /* tell the libX11 that we will do input method handling ourselves
348 * that keep libX11 from doing anything whith dead keys, allowing Wine
349 * to have total control over dead keys, that is this line allows
350 * them to work in Wine, even whith a libX11 including the dead key
351 * patches from Th.Quinot (http://Web.FdN.FR/~tquinot/dead-keys.en.html)
353 TSXOpenIM( display, NULL, NULL, NULL);
355 if (synchronous) XSynchronize( display, True );
357 screen_width = WidthOfScreen( screen );
358 screen_height = HeightOfScreen( screen );
360 if (desktop_geometry)
361 root_window = X11DRV_create_desktop( desktop_vi, desktop_geometry );
364 if(!X11DRV_GDI_Initialize( display ))
366 ERR( "Couldn't Initialize GDI.\n" );
370 #ifdef HAVE_LIBXXF86VM
371 /* initialize XVidMode */
372 X11DRV_XF86VM_Init();
374 #ifdef HAVE_LIBXXF86DGA2
375 /* initialize DGA2 */
376 X11DRV_XF86DGA2_Init();
380 /*X11DRV_GLX_Init();*/
383 /* load display.dll */
384 LoadLibrary16( "display" );
388 /***********************************************************************
389 * X11DRV thread termination routine
391 static void thread_detach(void)
393 struct x11drv_thread_data *data = NtCurrentTeb()->driver_data;
397 CloseHandle( data->display_fd );
399 XCloseDisplay( data->display );
401 HeapFree( GetProcessHeap(), 0, data );
406 /***********************************************************************
407 * X11DRV process termination routine
409 static void process_detach(void)
413 /*X11DRV_GLX_Cleanup();*/
415 #ifdef HAVE_LIBXXF86DGA2
417 X11DRV_XF86DGA2_Cleanup();
419 #ifdef HAVE_LIBXXF86VM
420 /* cleanup XVidMode */
421 X11DRV_XF86VM_Cleanup();
424 /* FIXME: should detach all threads */
428 X11DRV_GDI_Finalize();
430 /* restore TSX11 locking */
431 wine_tsx11_lock = old_tsx11_lock;
432 wine_tsx11_unlock = old_tsx11_unlock;
433 DeleteCriticalSection( &X11DRV_CritSection );
437 /***********************************************************************
438 * X11DRV thread initialisation routine
440 struct x11drv_thread_data *x11drv_init_thread_data(void)
442 struct x11drv_thread_data *data;
444 if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) )))
446 ERR( "could not create data\n" );
450 if (!(data->display = XOpenDisplay(NULL)))
453 MESSAGE( "x11drv: Can't open display: %s\n", XDisplayName(NULL) );
456 fcntl( ConnectionNumber(data->display), F_SETFD, 1 ); /* set close on exec flag */
457 if (synchronous) XSynchronize( data->display, True );
459 if (wine_server_fd_to_handle( ConnectionNumber(data->display), GENERIC_READ | SYNCHRONIZE,
460 FALSE, &data->display_fd ))
462 MESSAGE( "x11drv: Can't allocate handle for display fd\n" );
465 data->process_event_count = 0;
467 data->cursor_window = None;
468 data->last_focus = 0;
469 NtCurrentTeb()->driver_data = data;
474 /***********************************************************************
475 * X11DRV initialisation routine
477 BOOL WINAPI X11DRV_Init( HINSTANCE hinst, DWORD reason, LPVOID reserved )
481 case DLL_PROCESS_ATTACH:
484 case DLL_THREAD_DETACH:
487 case DLL_PROCESS_DETACH:
494 /***********************************************************************
495 * GetScreenSaveActive (X11DRV.@)
497 * Returns the active status of the screen saver
499 BOOL X11DRV_GetScreenSaveActive(void)
502 TSXGetScreenSaver(gdi_display, &timeout, &temp, &temp, &temp);
506 /***********************************************************************
507 * SetScreenSaveActive (X11DRV.@)
509 * Activate/Deactivate the screen saver
511 void X11DRV_SetScreenSaveActive(BOOL bActivate)
513 int timeout, interval, prefer_blanking, allow_exposures;
514 static int last_timeout = 15 * 60;
516 TSXGetScreenSaver(gdi_display, &timeout, &interval, &prefer_blanking,
518 if (timeout) last_timeout = timeout;
520 timeout = bActivate ? last_timeout : 0;
521 TSXSetScreenSaver(gdi_display, timeout, interval, prefer_blanking,