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
29 #ifdef HAVE_SYS_TIME_H
30 # include <sys/time.h>
35 #include <X11/cursorfont.h>
38 #include <X11/XKBlib.h>
47 #define LONG64 X_LONG64
48 #include <X11/Xproto.h>
59 #include "wine/winbase16.h"
66 #include "wine/server.h"
67 #include "wine/debug.h"
69 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
71 static CRITICAL_SECTION X11DRV_CritSection;
72 static CRITICAL_SECTION_DEBUG critsect_debug =
74 0, 0, &X11DRV_CritSection,
75 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
76 0, 0, { 0, (DWORD)(__FILE__ ": X11DRV_CritSection") }
78 static CRITICAL_SECTION X11DRV_CritSection = { &critsect_debug, -1, 0, 0, 0, 0 };
82 unsigned int screen_width;
83 unsigned int screen_height;
84 unsigned int screen_depth;
86 DWORD desktop_tid = 0;
92 int use_take_focus = 1;
94 int client_side_with_core = 1;
95 int client_side_with_render = 1;
96 int client_side_antialias_with_core = 1;
97 int client_side_antialias_with_render = 1;
98 int using_wine_desktop = 0;
100 static BOOL synchronous; /* run in synchronous mode? */
101 static BOOL desktop_dbl_buf = TRUE;
102 static char *desktop_geometry;
103 static XVisualInfo *desktop_vi;
105 static x11drv_error_callback err_callback; /* current callback for error */
106 static Display *err_callback_display; /* display callback is set for */
107 static void *err_callback_arg; /* error callback argument */
108 static int err_callback_result; /* error callback result */
109 static unsigned long err_serial; /* serial number of first request */
110 static int (*old_error_handler)( Display *, XErrorEvent * );
111 static int use_xim = 1;
112 static char input_style[20];
114 #define IS_OPTION_TRUE(ch) \
115 ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
116 #define IS_OPTION_FALSE(ch) \
117 ((ch) == 'n' || (ch) == 'N' || (ch) == 'f' || (ch) == 'F' || (ch) == '0')
119 /***********************************************************************
122 * Check if the X error is one we can ignore.
124 static inline BOOL ignore_error( Display *display, XErrorEvent *event )
126 if (event->request_code == X_SetInputFocus && event->error_code == BadMatch) return TRUE;
131 /***********************************************************************
132 * X11DRV_expect_error
134 * Setup a callback function that will be called on an X error. The
135 * callback must return non-zero if the error is the one it expected.
136 * This function acquires the x11 lock; X11DRV_check_error must be
137 * called in all cases to release it.
139 void X11DRV_expect_error( Display *display, x11drv_error_callback callback, void *arg )
142 err_callback = callback;
143 err_callback_display = display;
144 err_callback_arg = arg;
145 err_callback_result = 0;
146 err_serial = NextRequest(display);
150 /***********************************************************************
153 * Check if an expected X11 error occurred; return non-zero if yes.
154 * Also release the x11 lock obtained in X11DRV_expect_error.
155 * The caller is responsible for calling XSync first if necessary.
157 int X11DRV_check_error(void)
161 ret = err_callback_result;
167 /***********************************************************************
170 static int error_handler( Display *display, XErrorEvent *error_evt )
172 if (err_callback && display == err_callback_display &&
173 (long)(error_evt->serial - err_serial) >= 0)
175 if ((err_callback_result = err_callback( display, error_evt, err_callback_arg )))
177 TRACE( "got expected error %d req %d\n",
178 error_evt->error_code, error_evt->request_code );
182 if (ignore_error( display, error_evt ))
184 TRACE( "got ignored error %d req %d\n",
185 error_evt->error_code, error_evt->request_code );
190 ERR( "X protocol error: serial=%ld, request_code=%d - breaking into debugger\n",
191 error_evt->serial, error_evt->request_code );
192 DebugBreak(); /* force an entry in the debugger */
194 old_error_handler( display, error_evt );
198 /***********************************************************************
199 * wine_tsx11_lock (X11DRV.@)
201 void wine_tsx11_lock(void)
203 EnterCriticalSection( &X11DRV_CritSection );
206 /***********************************************************************
207 * wine_tsx11_unlock (X11DRV.@)
209 void wine_tsx11_unlock(void)
211 LeaveCriticalSection( &X11DRV_CritSection );
215 /***********************************************************************
218 * Get a config key from either the app-specific or the default config
220 inline static DWORD get_config_key( HKEY defkey, HKEY appkey, const char *name,
221 char *buffer, DWORD size )
223 if (appkey && !RegQueryValueExA( appkey, name, 0, NULL, buffer, &size )) return 0;
224 return RegQueryValueExA( defkey, name, 0, NULL, buffer, &size );
228 /***********************************************************************
231 * Setup the x11drv options.
233 static void setup_options(void)
235 char buffer[MAX_PATH+16];
236 HKEY hkey, appkey = 0;
239 if (RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\x11drv", 0, NULL,
240 REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, NULL ))
242 ERR("Cannot create config registry key\n" );
246 /* open the app-specific key */
248 len = (GetModuleFileNameA( 0, buffer, MAX_PATH ));
249 if (len && len < MAX_PATH)
252 char *p, *appname = buffer;
253 if ((p = strrchr( appname, '/' ))) appname = p + 1;
254 if ((p = strrchr( appname, '\\' ))) appname = p + 1;
255 strcat( appname, "\\x11drv" );
256 if (!RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\AppDefaults", &tmpkey ))
258 if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
259 RegCloseKey( tmpkey );
263 if (!get_config_key( hkey, appkey, "Desktop", buffer, sizeof(buffer) ))
265 /* Imperfect validation: If Desktop=N, then we don't turn on
266 ** the --desktop option. We should really validate for a correct
268 if (!IS_OPTION_FALSE(buffer[0])) desktop_geometry = strdup(buffer);
271 if (!get_config_key( hkey, appkey, "Managed", buffer, sizeof(buffer) ))
272 managed_mode = IS_OPTION_TRUE( buffer[0] );
274 if (!get_config_key( hkey, appkey, "DXGrab", buffer, sizeof(buffer) ))
275 dxgrab = IS_OPTION_TRUE( buffer[0] );
277 if (!get_config_key( hkey, appkey, "UseDGA", buffer, sizeof(buffer) ))
278 usedga = IS_OPTION_TRUE( buffer[0] );
280 if (!get_config_key( hkey, appkey, "UseXVidMode", buffer, sizeof(buffer) ))
281 usexvidmode = IS_OPTION_TRUE( buffer[0] );
283 if (!get_config_key( hkey, appkey, "UseXRandR", buffer, sizeof(buffer) ))
284 usexrandr = IS_OPTION_TRUE( buffer[0] );
286 if (!get_config_key( hkey, appkey, "UseTakeFocus", buffer, sizeof(buffer) ))
287 use_take_focus = IS_OPTION_TRUE( buffer[0] );
290 if (!get_config_key( hkey, appkey, "ScreenDepth", buffer, sizeof(buffer) ))
291 screen_depth = atoi(buffer);
293 if (!get_config_key( hkey, appkey, "Synchronous", buffer, sizeof(buffer) ))
294 synchronous = IS_OPTION_TRUE( buffer[0] );
296 if (!get_config_key( hkey, appkey, "ClientSideWithCore", buffer, sizeof(buffer) ))
297 client_side_with_core = IS_OPTION_TRUE( buffer[0] );
299 if (!get_config_key( hkey, appkey, "ClientSideWithRender", buffer, sizeof(buffer) ))
300 client_side_with_render = IS_OPTION_TRUE( buffer[0] );
302 if (!get_config_key( hkey, appkey, "ClientSideAntiAliasWithCore", buffer, sizeof(buffer) ))
303 client_side_antialias_with_core = IS_OPTION_TRUE( buffer[0] );
305 if (!get_config_key( hkey, appkey, "ClientSideAntiAliasWithRender", buffer, sizeof(buffer) ))
306 client_side_antialias_with_render = IS_OPTION_TRUE( buffer[0] );
308 if (!get_config_key( hkey, appkey, "DesktopDoubleBuffered", buffer, sizeof(buffer) ))
309 desktop_dbl_buf = IS_OPTION_TRUE( buffer[0] );
311 if (!get_config_key( hkey, appkey, "UseXIM", buffer, sizeof(buffer) ))
312 use_xim = IS_OPTION_TRUE( buffer[0] );
314 get_config_key( hkey, appkey, "InputStyle", input_style, sizeof(input_style) );
316 if (appkey) RegCloseKey( appkey );
321 /***********************************************************************
322 * X11DRV process initialisation routine
324 static BOOL process_attach(void)
332 if (!(display = XOpenDisplay( NULL ))) return FALSE;
334 fcntl( ConnectionNumber(display), F_SETFD, 1 ); /* set close on exec flag */
335 screen = DefaultScreenOfDisplay( display );
336 visual = DefaultVisual( display, DefaultScreen(display) );
337 root_window = DefaultRootWindow( display );
338 old_error_handler = XSetErrorHandler( error_handler );
340 /* Initialize screen depth */
342 if (screen_depth) /* depth specified */
345 int *depth_list = XListDepths(display, DefaultScreen(display), &depth_count);
346 for (i = 0; i < depth_count; i++)
347 if (depth_list[i] == screen_depth) break;
349 if (i >= depth_count)
351 WARN( "invalid depth %d, using default\n", screen_depth );
355 if (!screen_depth) screen_depth = DefaultDepthOfScreen( screen );
357 /* Initialize OpenGL */
358 X11DRV_OpenGL_Init(display);
360 /* If OpenGL is available, change the default visual, etc as necessary */
361 if (desktop_dbl_buf && (desktop_vi = X11DRV_setup_opengl_visual( display )))
363 visual = desktop_vi->visual;
364 screen = ScreenOfDisplay(display, desktop_vi->screen);
365 screen_depth = desktop_vi->depth;
368 if (synchronous) XSynchronize( display, True );
370 screen_width = WidthOfScreen( screen );
371 screen_height = HeightOfScreen( screen );
373 X11DRV_Settings_Init();
375 if (desktop_geometry)
377 root_window = X11DRV_create_desktop( desktop_vi, desktop_geometry );
378 using_wine_desktop = 1;
382 X11DRV_GDI_Initialize( display );
384 #ifdef HAVE_LIBXXF86VM
385 /* initialize XVidMode */
386 X11DRV_XF86VM_Init();
388 #ifdef HAVE_LIBXRANDR
389 /* initialize XRandR */
390 X11DRV_XRandR_Init();
392 #ifdef HAVE_LIBXXF86DGA2
393 /* initialize DGA2 */
394 X11DRV_XF86DGA2_Init();
401 /***********************************************************************
402 * X11DRV thread termination routine
404 static void thread_detach(void)
406 struct x11drv_thread_data *data = NtCurrentTeb()->driver_data;
410 CloseHandle( data->display_fd );
412 XCloseDisplay( data->display );
413 /* if (data->xim) XCloseIM( data->xim ); */ /* crashes Xlib */
415 HeapFree( GetProcessHeap(), 0, data );
420 /***********************************************************************
421 * X11DRV process termination routine
423 static void process_detach(void)
425 #ifdef HAVE_LIBXXF86DGA2
427 X11DRV_XF86DGA2_Cleanup();
429 #ifdef HAVE_LIBXXF86VM
430 /* cleanup XVidMode */
431 X11DRV_XF86VM_Cleanup();
433 if(using_client_side_fonts)
434 X11DRV_XRender_Finalize();
437 X11DRV_GDI_Finalize();
439 DeleteCriticalSection( &X11DRV_CritSection );
443 /***********************************************************************
444 * X11DRV thread initialisation routine
446 struct x11drv_thread_data *x11drv_init_thread_data(void)
448 struct x11drv_thread_data *data;
450 if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) )))
452 ERR( "could not create data\n" );
456 if (!(data->display = XOpenDisplay(NULL)))
459 MESSAGE( "x11drv: Can't open display: %s\n", XDisplayName(NULL) );
460 MESSAGE( "Please ensure that your X server is running and that $DISPLAY is set correctly.\n" );
464 fcntl( ConnectionNumber(data->display), F_SETFD, 1 ); /* set close on exec flag */
469 use_xkb = XkbUseExtension( data->display, NULL, NULL );
470 if (use_xkb) XkbSetDetectableAutoRepeat( data->display, True, NULL );
474 if (synchronous) XSynchronize( data->display, True );
477 if (use_xim && !(data->xim = X11DRV_SetupXIM( data->display, input_style )))
478 WARN("Input Method is not available\n");
480 if (wine_server_fd_to_handle( ConnectionNumber(data->display), GENERIC_READ | SYNCHRONIZE,
481 FALSE, &data->display_fd ))
483 MESSAGE( "x11drv: Can't allocate handle for display fd\n" );
486 data->process_event_count = 0;
488 data->cursor_window = None;
489 data->last_focus = 0;
490 NtCurrentTeb()->driver_data = data;
491 if (desktop_tid) AttachThreadInput( GetCurrentThreadId(), desktop_tid, TRUE );
496 /***********************************************************************
497 * X11DRV initialisation routine
499 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
505 case DLL_PROCESS_ATTACH:
506 ret = process_attach();
508 case DLL_THREAD_DETACH:
511 case DLL_PROCESS_DETACH:
518 /***********************************************************************
519 * GetScreenSaveActive (X11DRV.@)
521 * Returns the active status of the screen saver
523 BOOL X11DRV_GetScreenSaveActive(void)
527 XGetScreenSaver(gdi_display, &timeout, &temp, &temp, &temp);
532 /***********************************************************************
533 * SetScreenSaveActive (X11DRV.@)
535 * Activate/Deactivate the screen saver
537 void X11DRV_SetScreenSaveActive(BOOL bActivate)
539 int timeout, interval, prefer_blanking, allow_exposures;
540 static int last_timeout = 15 * 60;
543 XGetScreenSaver(gdi_display, &timeout, &interval, &prefer_blanking,
545 if (timeout) last_timeout = timeout;
547 timeout = bActivate ? last_timeout : 0;
548 XSetScreenSaver(gdi_display, timeout, interval, prefer_blanking,