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
30 #include <X11/cursorfont.h>
36 #include "wine/winbase16.h"
46 #include "wine/server.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
51 static void (*old_tsx11_lock)(void);
52 static void (*old_tsx11_unlock)(void);
54 static CRITICAL_SECTION X11DRV_CritSection = CRITICAL_SECTION_INIT("X11DRV_CritSection");
58 unsigned int screen_width;
59 unsigned int screen_height;
60 unsigned int screen_depth;
62 int dxgrab, usedga, usexvidmode;
65 unsigned int X11DRV_server_startticks;
67 static BOOL synchronous; /* run in synchronous mode? */
68 static char *desktop_geometry;
69 static XVisualInfo *desktop_vi;
71 static x11drv_error_callback err_callback; /* current callback for error */
72 static Display *err_callback_display; /* display callback is set for */
73 static void *err_callback_arg; /* error callback argument */
74 static int err_callback_result; /* error callback result */
75 static int (*old_error_handler)( Display *, XErrorEvent * );
77 #define IS_OPTION_TRUE(ch) \
78 ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
79 #define IS_OPTION_FALSE(ch) \
80 ((ch) == 'n' || (ch) == 'N' || (ch) == 'f' || (ch) == 'F' || (ch) == '0')
82 /***********************************************************************
85 * Setup a callback function that will be called on an X error. The
86 * callback must return non-zero if the error is the one it expected.
87 * This function acquires the x11 lock; X11DRV_check_error must be
88 * called in all cases to release it.
90 void X11DRV_expect_error( Display *display, x11drv_error_callback callback, void *arg )
93 XSync( display, False );
94 err_callback = callback;
95 err_callback_display = display;
96 err_callback_arg = arg;
97 err_callback_result = 0;
101 /***********************************************************************
104 * Check if an expected X11 error occurred; return non-zero if yes.
105 * Also release the x11 lock obtained in X11DRV_expect_error.
107 int X11DRV_check_error(void)
110 XSync( err_callback_display, False );
112 ret = err_callback_result;
118 /***********************************************************************
121 static int error_handler( Display *display, XErrorEvent *error_evt )
123 if (err_callback && display == err_callback_display)
125 if ((err_callback_result = err_callback( display, error_evt, err_callback_arg )))
127 TRACE( "got expected error\n" );
131 if (synchronous) DebugBreak(); /* force an entry in the debugger */
132 old_error_handler( display, error_evt );
136 /***********************************************************************
139 static void lock_tsx11(void)
141 EnterCriticalSection( &X11DRV_CritSection );
144 /***********************************************************************
147 static void unlock_tsx11(void)
149 LeaveCriticalSection( &X11DRV_CritSection );
152 /***********************************************************************
155 * Get the server startup time
156 * Won't be exact, but should be sufficient
158 static void get_server_startup(void)
161 gettimeofday( &t, NULL );
162 X11DRV_server_startticks = ((t.tv_sec * 1000) + (t.tv_usec / 1000)) - GetTickCount();
166 /***********************************************************************
169 * Get a config key from either the app-specific or the default config
171 inline static DWORD get_config_key( HKEY defkey, HKEY appkey, const char *name,
172 char *buffer, DWORD size )
174 if (appkey && !RegQueryValueExA( appkey, name, 0, NULL, buffer, &size )) return 0;
175 return RegQueryValueExA( defkey, name, 0, NULL, buffer, &size );
179 /***********************************************************************
182 * Setup the x11drv options.
184 static void setup_options(void)
186 char buffer[MAX_PATH+16];
187 HKEY hkey, appkey = 0;
190 if (RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\x11drv", 0, NULL,
191 REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, NULL ))
193 ERR("Cannot create config registry key\n" );
197 /* open the app-specific key */
199 if (GetModuleFileNameA( 0, buffer, MAX_PATH ))
202 char *p, *appname = buffer;
203 if ((p = strrchr( appname, '/' ))) appname = p + 1;
204 if ((p = strrchr( appname, '\\' ))) appname = p + 1;
205 strcat( appname, "\\x11drv" );
206 if (!RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\AppDefaults", &tmpkey ))
208 if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
209 RegCloseKey( tmpkey );
213 /* get the display name */
215 strcpy( buffer, "DISPLAY=" );
216 count = sizeof(buffer) - 8;
217 if (!RegQueryValueExA( hkey, "display", 0, NULL, buffer + 8, &count ))
219 const char *display_name = getenv( "DISPLAY" );
220 if (display_name && strcmp( buffer, display_name ))
221 MESSAGE( "x11drv: Warning: $DISPLAY variable ignored, using '%s' specified in config file\n",
223 putenv( strdup(buffer) );
226 if (!get_config_key( hkey, appkey, "Desktop", buffer, sizeof(buffer) ))
228 /* Imperfect validation: If Desktop=N, then we don't turn on
229 ** the --desktop option. We should really validate for a correct
231 if (!IS_OPTION_FALSE(buffer[0])) desktop_geometry = strdup(buffer);
234 if (!get_config_key( hkey, appkey, "Managed", buffer, sizeof(buffer) ))
235 managed_mode = IS_OPTION_TRUE( buffer[0] );
237 if (!get_config_key( hkey, appkey, "DXGrab", buffer, sizeof(buffer) ))
238 dxgrab = IS_OPTION_TRUE( buffer[0] );
240 if (!get_config_key( hkey, appkey, "UseDGA", buffer, sizeof(buffer) ))
241 usedga = IS_OPTION_TRUE( buffer[0] );
243 if (!get_config_key( hkey, appkey, "UseXVidMode", buffer, sizeof(buffer) ))
244 usexvidmode = IS_OPTION_TRUE( buffer[0] );
247 if (!get_config_key( hkey, appkey, "ScreenDepth", buffer, sizeof(buffer) ))
248 screen_depth = atoi(buffer);
250 if (!get_config_key( hkey, appkey, "Synchronous", buffer, sizeof(buffer) ))
251 synchronous = IS_OPTION_TRUE( buffer[0] );
253 if (appkey) RegCloseKey( appkey );
258 /***********************************************************************
259 * setup_opengl_visual
261 * Setup the default visual used for OpenGL and Direct3D, and the desktop
262 * window (if it exists). If OpenGL isn't available, the visual is simply
263 * set to the default visual for the display
266 static void setup_opengl_visual( Display *display )
268 int err_base, evt_base;
270 /* In order to support OpenGL or D3D, we require a double-buffered
272 if (glXQueryExtension(display, &err_base, &evt_base) == True) {
273 int dblBuf[]={GLX_RGBA,GLX_DEPTH_SIZE,16,GLX_DOUBLEBUFFER,None};
276 desktop_vi = glXChooseVisual(display, DefaultScreen(display), dblBuf);
280 if (desktop_vi != NULL) {
281 visual = desktop_vi->visual;
282 screen = ScreenOfDisplay(display, desktop_vi->screen);
283 screen_depth = desktop_vi->depth;
286 #endif /* HAVE_OPENGL */
288 /***********************************************************************
289 * X11DRV process initialisation routine
291 static void process_attach(void)
295 get_server_startup();
298 /* setup TSX11 locking */
299 old_tsx11_lock = wine_tsx11_lock;
300 old_tsx11_unlock = wine_tsx11_unlock;
301 wine_tsx11_lock = lock_tsx11;
302 wine_tsx11_unlock = unlock_tsx11;
306 if (!(display = TSXOpenDisplay( NULL )))
308 MESSAGE( "x11drv: Can't open display: %s\n", XDisplayName(NULL) );
311 fcntl( ConnectionNumber(display), F_SETFD, 1 ); /* set close on exec flag */
312 screen = DefaultScreenOfDisplay( display );
313 visual = DefaultVisual( display, DefaultScreen(display) );
314 root_window = DefaultRootWindow( display );
315 old_error_handler = XSetErrorHandler( error_handler );
317 /* Initialize screen depth */
319 if (screen_depth) /* depth specified */
322 int *depth_list = TSXListDepths(display, DefaultScreen(display), &depth_count);
323 for (i = 0; i < depth_count; i++)
324 if (depth_list[i] == screen_depth) break;
325 TSXFree( depth_list );
326 if (i >= depth_count)
328 MESSAGE( "x11drv: Depth %d not supported on this screen.\n", screen_depth );
332 else screen_depth = DefaultDepthOfScreen( screen );
334 /* If OpenGL is available, change the default visual, etc as necessary */
336 setup_opengl_visual( display );
337 #endif /* HAVE_OPENGL */
339 /* tell the libX11 that we will do input method handling ourselves
340 * that keep libX11 from doing anything whith dead keys, allowing Wine
341 * to have total control over dead keys, that is this line allows
342 * them to work in Wine, even whith a libX11 including the dead key
343 * patches from Th.Quinot (http://Web.FdN.FR/~tquinot/dead-keys.en.html)
345 TSXOpenIM( display, NULL, NULL, NULL);
347 if (synchronous) XSynchronize( display, True );
349 screen_width = WidthOfScreen( screen );
350 screen_height = HeightOfScreen( screen );
352 if (desktop_geometry)
353 root_window = X11DRV_create_desktop( desktop_vi, desktop_geometry );
356 if(!X11DRV_GDI_Initialize( display ))
358 ERR( "Couldn't Initialize GDI.\n" );
362 #ifdef HAVE_LIBXXF86VM
363 /* initialize XVidMode */
364 X11DRV_XF86VM_Init();
366 #ifdef HAVE_LIBXXF86DGA2
367 /* initialize DGA2 */
368 X11DRV_XF86DGA2_Init();
372 /*X11DRV_GLX_Init();*/
375 /* load display.dll */
376 LoadLibrary16( "display" );
380 /***********************************************************************
381 * X11DRV thread termination routine
383 static void thread_detach(void)
385 struct x11drv_thread_data *data = NtCurrentTeb()->driver_data;
389 CloseHandle( data->display_fd );
391 XCloseDisplay( data->display );
393 HeapFree( GetProcessHeap(), 0, data );
398 /***********************************************************************
399 * X11DRV process termination routine
401 static void process_detach(void)
405 /*X11DRV_GLX_Cleanup();*/
407 #ifdef HAVE_LIBXXF86DGA2
409 X11DRV_XF86DGA2_Cleanup();
411 #ifdef HAVE_LIBXXF86VM
412 /* cleanup XVidMode */
413 X11DRV_XF86VM_Cleanup();
416 /* FIXME: should detach all threads */
420 X11DRV_GDI_Finalize();
422 /* restore TSX11 locking */
423 wine_tsx11_lock = old_tsx11_lock;
424 wine_tsx11_unlock = old_tsx11_unlock;
425 DeleteCriticalSection( &X11DRV_CritSection );
429 /***********************************************************************
430 * X11DRV thread initialisation routine
432 struct x11drv_thread_data *x11drv_init_thread_data(void)
434 struct x11drv_thread_data *data;
436 if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) )))
438 ERR( "could not create data\n" );
442 if (!(data->display = XOpenDisplay(NULL)))
445 MESSAGE( "x11drv: Can't open display: %s\n", XDisplayName(NULL) );
448 fcntl( ConnectionNumber(data->display), F_SETFD, 1 ); /* set close on exec flag */
449 if (synchronous) XSynchronize( data->display, True );
451 if (wine_server_fd_to_handle( ConnectionNumber(data->display), GENERIC_READ | SYNCHRONIZE,
452 FALSE, &data->display_fd ))
454 MESSAGE( "x11drv: Can't allocate handle for display fd\n" );
457 data->process_event_count = 0;
459 data->cursor_window = None;
460 NtCurrentTeb()->driver_data = data;
465 /***********************************************************************
466 * X11DRV initialisation routine
468 BOOL WINAPI X11DRV_Init( HINSTANCE hinst, DWORD reason, LPVOID reserved )
472 case DLL_PROCESS_ATTACH:
475 case DLL_THREAD_DETACH:
478 case DLL_PROCESS_DETACH:
485 /***********************************************************************
486 * GetScreenSaveActive (X11DRV.@)
488 * Returns the active status of the screen saver
490 BOOL X11DRV_GetScreenSaveActive(void)
493 TSXGetScreenSaver(gdi_display, &timeout, &temp, &temp, &temp);
497 /***********************************************************************
498 * SetScreenSaveActive (X11DRV.@)
500 * Activate/Deactivate the screen saver
502 void X11DRV_SetScreenSaveActive(BOOL bActivate)
504 int timeout, interval, prefer_blanking, allow_exposures;
505 static int last_timeout = 15 * 60;
507 TSXGetScreenSaver(gdi_display, &timeout, &interval, &prefer_blanking,
509 if (timeout) last_timeout = timeout;
511 timeout = bActivate ? last_timeout : 0;
512 TSXSetScreenSaver(gdi_display, timeout, interval, prefer_blanking,