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"
68 #include "wine/server.h"
69 #include "wine/debug.h"
71 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
73 static CRITICAL_SECTION X11DRV_CritSection;
74 static CRITICAL_SECTION_DEBUG critsect_debug =
76 0, 0, &X11DRV_CritSection,
77 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
78 0, 0, { 0, (DWORD)(__FILE__ ": X11DRV_CritSection") }
80 static CRITICAL_SECTION X11DRV_CritSection = { &critsect_debug, -1, 0, 0, 0, 0 };
84 unsigned int screen_width;
85 unsigned int screen_height;
86 unsigned int screen_depth;
88 DWORD desktop_tid = 0;
94 int use_take_focus = 1;
96 int client_side_with_core = 1;
97 int client_side_with_render = 1;
98 int client_side_antialias_with_core = 1;
99 int client_side_antialias_with_render = 1;
100 int using_wine_desktop = 0;
102 unsigned int X11DRV_server_startticks;
104 static BOOL synchronous; /* run in synchronous mode? */
105 static BOOL desktop_dbl_buf = TRUE;
106 static char *desktop_geometry;
107 static XVisualInfo *desktop_vi;
109 static x11drv_error_callback err_callback; /* current callback for error */
110 static Display *err_callback_display; /* display callback is set for */
111 static void *err_callback_arg; /* error callback argument */
112 static int err_callback_result; /* error callback result */
113 static unsigned long err_serial; /* serial number of first request */
114 static int (*old_error_handler)( Display *, XErrorEvent * );
115 static int use_xim = 1;
116 static char input_style[20];
118 #define IS_OPTION_TRUE(ch) \
119 ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
120 #define IS_OPTION_FALSE(ch) \
121 ((ch) == 'n' || (ch) == 'N' || (ch) == 'f' || (ch) == 'F' || (ch) == '0')
123 /***********************************************************************
126 * Check if the X error is one we can ignore.
128 static inline BOOL ignore_error( Display *display, XErrorEvent *event )
130 if (event->request_code == X_SetInputFocus && event->error_code == BadMatch) return TRUE;
135 /***********************************************************************
136 * X11DRV_expect_error
138 * Setup a callback function that will be called on an X error. The
139 * callback must return non-zero if the error is the one it expected.
140 * This function acquires the x11 lock; X11DRV_check_error must be
141 * called in all cases to release it.
143 void X11DRV_expect_error( Display *display, x11drv_error_callback callback, void *arg )
146 err_callback = callback;
147 err_callback_display = display;
148 err_callback_arg = arg;
149 err_callback_result = 0;
150 err_serial = NextRequest(display);
154 /***********************************************************************
157 * Check if an expected X11 error occurred; return non-zero if yes.
158 * Also release the x11 lock obtained in X11DRV_expect_error.
159 * The caller is responsible for calling XSync first if necessary.
161 int X11DRV_check_error(void)
165 ret = err_callback_result;
171 /***********************************************************************
174 static int error_handler( Display *display, XErrorEvent *error_evt )
176 if (err_callback && display == err_callback_display &&
177 (long)(error_evt->serial - err_serial) >= 0)
179 if ((err_callback_result = err_callback( display, error_evt, err_callback_arg )))
181 TRACE( "got expected error %d req %d\n",
182 error_evt->error_code, error_evt->request_code );
186 if (ignore_error( display, error_evt ))
188 TRACE( "got ignored error %d req %d\n",
189 error_evt->error_code, error_evt->request_code );
194 ERR( "X protocol error: serial=%ld, request_code=%d - breaking into debugger\n",
195 error_evt->serial, error_evt->request_code );
196 DebugBreak(); /* force an entry in the debugger */
198 old_error_handler( display, error_evt );
202 /***********************************************************************
203 * wine_tsx11_lock (X11DRV.@)
205 void wine_tsx11_lock(void)
207 EnterCriticalSection( &X11DRV_CritSection );
210 /***********************************************************************
211 * wine_tsx11_unlock (X11DRV.@)
213 void wine_tsx11_unlock(void)
215 LeaveCriticalSection( &X11DRV_CritSection );
218 /***********************************************************************
221 * Get the server startup time
222 * Won't be exact, but should be sufficient
224 static void get_server_startup(void)
227 gettimeofday( &t, NULL );
228 X11DRV_server_startticks = ((t.tv_sec * 1000) + (t.tv_usec / 1000)) - GetTickCount();
232 /***********************************************************************
235 * Get a config key from either the app-specific or the default config
237 inline static DWORD get_config_key( HKEY defkey, HKEY appkey, const char *name,
238 char *buffer, DWORD size )
240 if (appkey && !RegQueryValueExA( appkey, name, 0, NULL, buffer, &size )) return 0;
241 return RegQueryValueExA( defkey, name, 0, NULL, buffer, &size );
245 /***********************************************************************
248 * Setup the x11drv options.
250 static void setup_options(void)
252 char buffer[MAX_PATH+16];
253 HKEY hkey, appkey = 0;
256 if (RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\x11drv", 0, NULL,
257 REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, NULL ))
259 ERR("Cannot create config registry key\n" );
263 /* open the app-specific key */
265 len = (GetModuleFileNameA( 0, buffer, MAX_PATH ));
266 if (len && len < MAX_PATH)
269 char *p, *appname = buffer;
270 if ((p = strrchr( appname, '/' ))) appname = p + 1;
271 if ((p = strrchr( appname, '\\' ))) appname = p + 1;
272 strcat( appname, "\\x11drv" );
273 if (!RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\AppDefaults", &tmpkey ))
275 if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
276 RegCloseKey( tmpkey );
280 if (!get_config_key( hkey, appkey, "Desktop", buffer, sizeof(buffer) ))
282 /* Imperfect validation: If Desktop=N, then we don't turn on
283 ** the --desktop option. We should really validate for a correct
285 if (!IS_OPTION_FALSE(buffer[0])) desktop_geometry = strdup(buffer);
288 if (!get_config_key( hkey, appkey, "Managed", buffer, sizeof(buffer) ))
289 managed_mode = IS_OPTION_TRUE( buffer[0] );
291 if (!get_config_key( hkey, appkey, "DXGrab", buffer, sizeof(buffer) ))
292 dxgrab = IS_OPTION_TRUE( buffer[0] );
294 if (!get_config_key( hkey, appkey, "UseDGA", buffer, sizeof(buffer) ))
295 usedga = IS_OPTION_TRUE( buffer[0] );
297 if (!get_config_key( hkey, appkey, "UseXVidMode", buffer, sizeof(buffer) ))
298 usexvidmode = IS_OPTION_TRUE( buffer[0] );
300 if (!get_config_key( hkey, appkey, "UseXRandR", buffer, sizeof(buffer) ))
301 usexrandr = IS_OPTION_TRUE( buffer[0] );
303 if (!get_config_key( hkey, appkey, "UseTakeFocus", buffer, sizeof(buffer) ))
304 use_take_focus = IS_OPTION_TRUE( buffer[0] );
307 if (!get_config_key( hkey, appkey, "ScreenDepth", buffer, sizeof(buffer) ))
308 screen_depth = atoi(buffer);
310 if (!get_config_key( hkey, appkey, "Synchronous", buffer, sizeof(buffer) ))
311 synchronous = IS_OPTION_TRUE( buffer[0] );
313 if (!get_config_key( hkey, appkey, "ClientSideWithCore", buffer, sizeof(buffer) ))
314 client_side_with_core = IS_OPTION_TRUE( buffer[0] );
316 if (!get_config_key( hkey, appkey, "ClientSideWithRender", buffer, sizeof(buffer) ))
317 client_side_with_render = IS_OPTION_TRUE( buffer[0] );
319 if (!get_config_key( hkey, appkey, "ClientSideAntiAliasWithCore", buffer, sizeof(buffer) ))
320 client_side_antialias_with_core = IS_OPTION_TRUE( buffer[0] );
322 if (!get_config_key( hkey, appkey, "ClientSideAntiAliasWithRender", buffer, sizeof(buffer) ))
323 client_side_antialias_with_render = IS_OPTION_TRUE( buffer[0] );
325 if (!get_config_key( hkey, appkey, "DesktopDoubleBuffered", buffer, sizeof(buffer) ))
326 desktop_dbl_buf = IS_OPTION_TRUE( buffer[0] );
328 if (!get_config_key( hkey, appkey, "UseXIM", buffer, sizeof(buffer) ))
329 use_xim = IS_OPTION_TRUE( buffer[0] );
331 get_config_key( hkey, appkey, "InputStyle", input_style, sizeof(input_style) );
333 if (appkey) RegCloseKey( appkey );
338 /***********************************************************************
339 * X11DRV process initialisation routine
341 static BOOL process_attach(void)
345 get_server_startup();
350 if (!(display = XOpenDisplay( NULL ))) return FALSE;
352 fcntl( ConnectionNumber(display), F_SETFD, 1 ); /* set close on exec flag */
353 screen = DefaultScreenOfDisplay( display );
354 visual = DefaultVisual( display, DefaultScreen(display) );
355 root_window = DefaultRootWindow( display );
356 old_error_handler = XSetErrorHandler( error_handler );
358 /* Initialize screen depth */
360 if (screen_depth) /* depth specified */
363 int *depth_list = XListDepths(display, DefaultScreen(display), &depth_count);
364 for (i = 0; i < depth_count; i++)
365 if (depth_list[i] == screen_depth) break;
367 if (i >= depth_count)
369 WARN( "invalid depth %d, using default\n", screen_depth );
373 if (!screen_depth) screen_depth = DefaultDepthOfScreen( screen );
375 /* Initialize OpenGL */
376 X11DRV_OpenGL_Init(display);
378 /* If OpenGL is available, change the default visual, etc as necessary */
379 if (desktop_dbl_buf && (desktop_vi = X11DRV_setup_opengl_visual( display )))
381 visual = desktop_vi->visual;
382 screen = ScreenOfDisplay(display, desktop_vi->screen);
383 screen_depth = desktop_vi->depth;
386 if (synchronous) XSynchronize( display, True );
388 screen_width = WidthOfScreen( screen );
389 screen_height = HeightOfScreen( screen );
391 X11DRV_Settings_Init();
393 if (desktop_geometry)
395 root_window = X11DRV_create_desktop( desktop_vi, desktop_geometry );
396 using_wine_desktop = 1;
400 X11DRV_GDI_Initialize( display );
402 #ifdef HAVE_LIBXXF86VM
403 /* initialize XVidMode */
404 X11DRV_XF86VM_Init();
406 #ifdef HAVE_LIBXRANDR
407 /* initialize XRandR */
408 X11DRV_XRandR_Init();
410 #ifdef HAVE_LIBXXF86DGA2
411 /* initialize DGA2 */
412 X11DRV_XF86DGA2_Init();
419 /***********************************************************************
420 * X11DRV thread termination routine
422 static void thread_detach(void)
424 struct x11drv_thread_data *data = NtCurrentTeb()->driver_data;
428 CloseHandle( data->display_fd );
430 XCloseDisplay( data->display );
431 /* if (data->xim) XCloseIM( data->xim ); */ /* crashes Xlib */
433 HeapFree( GetProcessHeap(), 0, data );
438 /***********************************************************************
439 * X11DRV process termination routine
441 static void process_detach(void)
443 #ifdef HAVE_LIBXXF86DGA2
445 X11DRV_XF86DGA2_Cleanup();
447 #ifdef HAVE_LIBXXF86VM
448 /* cleanup XVidMode */
449 X11DRV_XF86VM_Cleanup();
451 if(using_client_side_fonts)
452 X11DRV_XRender_Finalize();
455 X11DRV_GDI_Finalize();
457 DeleteCriticalSection( &X11DRV_CritSection );
461 /***********************************************************************
462 * X11DRV thread initialisation routine
464 struct x11drv_thread_data *x11drv_init_thread_data(void)
466 struct x11drv_thread_data *data;
468 if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) )))
470 ERR( "could not create data\n" );
474 if (!(data->display = XOpenDisplay(NULL)))
477 MESSAGE( "x11drv: Can't open display: %s\n", XDisplayName(NULL) );
478 MESSAGE( "Please ensure that your X server is running and that $DISPLAY is set correctly.\n" );
482 fcntl( ConnectionNumber(data->display), F_SETFD, 1 ); /* set close on exec flag */
487 use_xkb = XkbUseExtension( data->display, NULL, NULL );
488 if (use_xkb) XkbSetDetectableAutoRepeat( data->display, True, NULL );
492 if (synchronous) XSynchronize( data->display, True );
495 if (use_xim && !(data->xim = X11DRV_SetupXIM( data->display, input_style )))
496 WARN("Input Method is not available\n");
498 if (wine_server_fd_to_handle( ConnectionNumber(data->display), GENERIC_READ | SYNCHRONIZE,
499 FALSE, &data->display_fd ))
501 MESSAGE( "x11drv: Can't allocate handle for display fd\n" );
504 data->process_event_count = 0;
506 data->cursor_window = None;
507 data->last_focus = 0;
508 NtCurrentTeb()->driver_data = data;
509 if (desktop_tid) AttachThreadInput( GetCurrentThreadId(), desktop_tid, TRUE );
514 /***********************************************************************
515 * X11DRV initialisation routine
517 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
523 case DLL_PROCESS_ATTACH:
524 ret = process_attach();
526 case DLL_THREAD_DETACH:
529 case DLL_PROCESS_DETACH:
536 /***********************************************************************
537 * GetScreenSaveActive (X11DRV.@)
539 * Returns the active status of the screen saver
541 BOOL X11DRV_GetScreenSaveActive(void)
545 XGetScreenSaver(gdi_display, &timeout, &temp, &temp, &temp);
550 /***********************************************************************
551 * SetScreenSaveActive (X11DRV.@)
553 * Activate/Deactivate the screen saver
555 void X11DRV_SetScreenSaveActive(BOOL bActivate)
557 int timeout, interval, prefer_blanking, allow_exposures;
558 static int last_timeout = 15 * 60;
561 XGetScreenSaver(gdi_display, &timeout, &interval, &prefer_blanking,
563 if (timeout) last_timeout = timeout;
565 timeout = bActivate ? last_timeout : 0;
566 XSetScreenSaver(gdi_display, timeout, interval, prefer_blanking,