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>
43 #include "wine/winbase16.h"
50 #include "wine/server.h"
51 #include "wine/debug.h"
53 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
55 static CRITICAL_SECTION X11DRV_CritSection;
56 static CRITICAL_SECTION_DEBUG critsect_debug =
58 0, 0, &X11DRV_CritSection,
59 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
60 0, 0, { 0, (DWORD)(__FILE__ ": X11DRV_CritSection") }
62 static CRITICAL_SECTION X11DRV_CritSection = { &critsect_debug, -1, 0, 0, 0, 0 };
66 unsigned int screen_width;
67 unsigned int screen_height;
68 unsigned int screen_depth;
70 DWORD desktop_tid = 0;
76 int use_take_focus = 1;
77 int use_primary_selection = 0;
79 int client_side_with_core = 1;
80 int client_side_with_render = 1;
81 int client_side_antialias_with_core = 1;
82 int client_side_antialias_with_render = 1;
83 int using_wine_desktop = 0;
84 DWORD thread_data_tls_index = TLS_OUT_OF_INDEXES;
86 static BOOL synchronous; /* run in synchronous mode? */
87 static BOOL desktop_dbl_buf = TRUE;
88 static char *desktop_geometry;
89 static XVisualInfo *desktop_vi;
91 static x11drv_error_callback err_callback; /* current callback for error */
92 static Display *err_callback_display; /* display callback is set for */
93 static void *err_callback_arg; /* error callback argument */
94 static int err_callback_result; /* error callback result */
95 static unsigned long err_serial; /* serial number of first request */
96 static int (*old_error_handler)( Display *, XErrorEvent * );
97 static int use_xim = 1;
98 static char input_style[20];
100 #define IS_OPTION_TRUE(ch) \
101 ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
102 #define IS_OPTION_FALSE(ch) \
103 ((ch) == 'n' || (ch) == 'N' || (ch) == 'f' || (ch) == 'F' || (ch) == '0')
105 /***********************************************************************
108 * Check if the X error is one we can ignore.
110 static inline BOOL ignore_error( Display *display, XErrorEvent *event )
112 if (event->request_code == X_SetInputFocus && event->error_code == BadMatch) return TRUE;
117 /***********************************************************************
118 * X11DRV_expect_error
120 * Setup a callback function that will be called on an X error. The
121 * callback must return non-zero if the error is the one it expected.
122 * This function acquires the x11 lock; X11DRV_check_error must be
123 * called in all cases to release it.
125 void X11DRV_expect_error( Display *display, x11drv_error_callback callback, void *arg )
128 err_callback = callback;
129 err_callback_display = display;
130 err_callback_arg = arg;
131 err_callback_result = 0;
132 err_serial = NextRequest(display);
136 /***********************************************************************
139 * Check if an expected X11 error occurred; return non-zero if yes.
140 * Also release the x11 lock obtained in X11DRV_expect_error.
141 * The caller is responsible for calling XSync first if necessary.
143 int X11DRV_check_error(void)
147 ret = err_callback_result;
153 /***********************************************************************
156 static int error_handler( Display *display, XErrorEvent *error_evt )
158 if (err_callback && display == err_callback_display &&
159 (long)(error_evt->serial - err_serial) >= 0)
161 if ((err_callback_result = err_callback( display, error_evt, err_callback_arg )))
163 TRACE( "got expected error %d req %d\n",
164 error_evt->error_code, error_evt->request_code );
168 if (ignore_error( display, error_evt ))
170 TRACE( "got ignored error %d req %d\n",
171 error_evt->error_code, error_evt->request_code );
176 ERR( "X protocol error: serial=%ld, request_code=%d - breaking into debugger\n",
177 error_evt->serial, error_evt->request_code );
178 DebugBreak(); /* force an entry in the debugger */
180 old_error_handler( display, error_evt );
184 /***********************************************************************
185 * wine_tsx11_lock (X11DRV.@)
187 void wine_tsx11_lock(void)
189 EnterCriticalSection( &X11DRV_CritSection );
192 /***********************************************************************
193 * wine_tsx11_unlock (X11DRV.@)
195 void wine_tsx11_unlock(void)
197 LeaveCriticalSection( &X11DRV_CritSection );
201 /***********************************************************************
204 * Get a config key from either the app-specific or the default config
206 inline static DWORD get_config_key( HKEY defkey, HKEY appkey, const char *name,
207 char *buffer, DWORD size )
209 if (appkey && !RegQueryValueExA( appkey, name, 0, NULL, buffer, &size )) return 0;
210 return RegQueryValueExA( defkey, name, 0, NULL, buffer, &size );
214 /***********************************************************************
217 * Setup the x11drv options.
219 static void setup_options(void)
221 char buffer[MAX_PATH+16];
222 HKEY hkey, appkey = 0;
225 /* @@ Wine registry key: HKLM\Software\Wine\Wine\Config\x11drv */
226 if (RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\x11drv", 0, NULL,
227 REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, NULL ))
229 ERR("Cannot create config registry key\n" );
233 /* open the app-specific key */
235 len = (GetModuleFileNameA( 0, buffer, MAX_PATH ));
236 if (len && len < MAX_PATH)
239 char *p, *appname = buffer;
240 if ((p = strrchr( appname, '/' ))) appname = p + 1;
241 if ((p = strrchr( appname, '\\' ))) appname = p + 1;
242 strcat( appname, "\\x11drv" );
243 /* @@ Wine registry key: HKLM\Software\Wine\Wine\Config\AppDefaults\app.exe\x11drv */
244 if (!RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\AppDefaults", &tmpkey ))
246 if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
247 RegCloseKey( tmpkey );
251 if (!get_config_key( hkey, appkey, "Desktop", buffer, sizeof(buffer) ))
253 /* Imperfect validation: If Desktop=N, then we don't turn on
254 ** the --desktop option. We should really validate for a correct
256 if (!IS_OPTION_FALSE(buffer[0])) desktop_geometry = strdup(buffer);
259 if (!get_config_key( hkey, appkey, "Managed", buffer, sizeof(buffer) ))
260 managed_mode = IS_OPTION_TRUE( buffer[0] );
262 if (!get_config_key( hkey, appkey, "DXGrab", buffer, sizeof(buffer) ))
263 dxgrab = IS_OPTION_TRUE( buffer[0] );
265 if (!get_config_key( hkey, appkey, "UseDGA", buffer, sizeof(buffer) ))
266 usedga = IS_OPTION_TRUE( buffer[0] );
268 if (!get_config_key( hkey, appkey, "UseXVidMode", buffer, sizeof(buffer) ))
269 usexvidmode = IS_OPTION_TRUE( buffer[0] );
271 if (!get_config_key( hkey, appkey, "UseXRandR", buffer, sizeof(buffer) ))
272 usexrandr = IS_OPTION_TRUE( buffer[0] );
274 if (!get_config_key( hkey, appkey, "UseTakeFocus", buffer, sizeof(buffer) ))
275 use_take_focus = IS_OPTION_TRUE( buffer[0] );
277 if (!get_config_key( hkey, appkey, "UsePrimarySelection", buffer, sizeof(buffer) ))
278 use_primary_selection = IS_OPTION_TRUE( buffer[0] );
281 if (!get_config_key( hkey, appkey, "ScreenDepth", buffer, sizeof(buffer) ))
282 screen_depth = atoi(buffer);
284 if (!get_config_key( hkey, appkey, "Synchronous", buffer, sizeof(buffer) ))
285 synchronous = IS_OPTION_TRUE( buffer[0] );
287 if (!get_config_key( hkey, appkey, "ClientSideWithCore", buffer, sizeof(buffer) ))
288 client_side_with_core = IS_OPTION_TRUE( buffer[0] );
290 if (!get_config_key( hkey, appkey, "ClientSideWithRender", buffer, sizeof(buffer) ))
291 client_side_with_render = IS_OPTION_TRUE( buffer[0] );
293 if (!get_config_key( hkey, appkey, "ClientSideAntiAliasWithCore", buffer, sizeof(buffer) ))
294 client_side_antialias_with_core = IS_OPTION_TRUE( buffer[0] );
296 if (!get_config_key( hkey, appkey, "ClientSideAntiAliasWithRender", buffer, sizeof(buffer) ))
297 client_side_antialias_with_render = IS_OPTION_TRUE( buffer[0] );
299 if (!get_config_key( hkey, appkey, "DesktopDoubleBuffered", buffer, sizeof(buffer) ))
300 desktop_dbl_buf = IS_OPTION_TRUE( buffer[0] );
302 if (!get_config_key( hkey, appkey, "UseXIM", buffer, sizeof(buffer) ))
303 use_xim = IS_OPTION_TRUE( buffer[0] );
305 get_config_key( hkey, appkey, "InputStyle", input_style, sizeof(input_style) );
307 if (appkey) RegCloseKey( appkey );
312 /***********************************************************************
313 * X11DRV process initialisation routine
315 static BOOL process_attach(void)
321 if ((thread_data_tls_index = TlsAlloc()) == TLS_OUT_OF_INDEXES) return FALSE;
325 if (!(display = XOpenDisplay( NULL ))) return FALSE;
327 fcntl( ConnectionNumber(display), F_SETFD, 1 ); /* set close on exec flag */
328 screen = DefaultScreenOfDisplay( display );
329 visual = DefaultVisual( display, DefaultScreen(display) );
330 root_window = DefaultRootWindow( display );
331 old_error_handler = XSetErrorHandler( error_handler );
333 /* Initialize screen depth */
335 if (screen_depth) /* depth specified */
338 int *depth_list = XListDepths(display, DefaultScreen(display), &depth_count);
339 for (i = 0; i < depth_count; i++)
340 if (depth_list[i] == screen_depth) break;
342 if (i >= depth_count)
344 WARN( "invalid depth %d, using default\n", screen_depth );
348 if (!screen_depth) screen_depth = DefaultDepthOfScreen( screen );
350 /* Initialize OpenGL */
351 X11DRV_OpenGL_Init(display);
353 /* If OpenGL is available, change the default visual, etc as necessary */
354 if (desktop_dbl_buf && (desktop_vi = X11DRV_setup_opengl_visual( display )))
356 visual = desktop_vi->visual;
357 screen = ScreenOfDisplay(display, desktop_vi->screen);
358 screen_depth = desktop_vi->depth;
361 if (synchronous) XSynchronize( display, True );
363 screen_width = WidthOfScreen( screen );
364 screen_height = HeightOfScreen( screen );
366 X11DRV_Settings_Init();
368 if (desktop_geometry)
370 root_window = X11DRV_create_desktop( desktop_vi, desktop_geometry );
371 using_wine_desktop = 1;
375 X11DRV_GDI_Initialize( display );
377 #ifdef HAVE_LIBXXF86VM
378 /* initialize XVidMode */
379 X11DRV_XF86VM_Init();
381 #ifdef HAVE_LIBXRANDR
382 /* initialize XRandR */
383 X11DRV_XRandR_Init();
385 #ifdef HAVE_LIBXXF86DGA2
386 /* initialize DGA2 */
387 X11DRV_XF86DGA2_Init();
390 X11DRV_InitKeyboard();
396 /***********************************************************************
397 * X11DRV thread termination routine
399 static void thread_detach(void)
401 struct x11drv_thread_data *data = TlsGetValue( thread_data_tls_index );
405 CloseHandle( data->display_fd );
407 XCloseDisplay( data->display );
408 /* if (data->xim) XCloseIM( data->xim ); */ /* crashes Xlib */
410 HeapFree( GetProcessHeap(), 0, data );
415 /***********************************************************************
416 * X11DRV process termination routine
418 static void process_detach(void)
420 #ifdef HAVE_LIBXXF86DGA2
422 X11DRV_XF86DGA2_Cleanup();
424 #ifdef HAVE_LIBXXF86VM
425 /* cleanup XVidMode */
426 X11DRV_XF86VM_Cleanup();
428 if(using_client_side_fonts)
429 X11DRV_XRender_Finalize();
432 X11DRV_GDI_Finalize();
434 DeleteCriticalSection( &X11DRV_CritSection );
435 TlsFree( thread_data_tls_index );
439 /***********************************************************************
440 * X11DRV thread initialisation routine
442 struct x11drv_thread_data *x11drv_init_thread_data(void)
444 struct x11drv_thread_data *data;
446 if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) )))
448 ERR( "could not create data\n" );
452 if (!(data->display = XOpenDisplay(NULL)))
455 MESSAGE( "x11drv: Can't open display: %s\n", XDisplayName(NULL) );
456 MESSAGE( "Please ensure that your X server is running and that $DISPLAY is set correctly.\n" );
460 fcntl( ConnectionNumber(data->display), F_SETFD, 1 ); /* set close on exec flag */
465 use_xkb = XkbUseExtension( data->display, NULL, NULL );
466 if (use_xkb) XkbSetDetectableAutoRepeat( data->display, True, NULL );
470 if (synchronous) XSynchronize( data->display, True );
473 if (use_xim && !(data->xim = X11DRV_SetupXIM( data->display, input_style )))
474 WARN("Input Method is not available\n");
476 if (wine_server_fd_to_handle( ConnectionNumber(data->display), GENERIC_READ | SYNCHRONIZE,
477 FALSE, &data->display_fd ))
479 MESSAGE( "x11drv: Can't allocate handle for display fd\n" );
482 data->process_event_count = 0;
484 data->cursor_window = None;
485 data->grab_window = None;
486 data->last_focus = 0;
487 data->selection_wnd = 0;
488 TlsSetValue( thread_data_tls_index, data );
489 if (desktop_tid) AttachThreadInput( GetCurrentThreadId(), desktop_tid, TRUE );
494 /***********************************************************************
495 * X11DRV initialisation routine
497 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
503 case DLL_PROCESS_ATTACH:
504 ret = process_attach();
506 case DLL_THREAD_DETACH:
509 case DLL_PROCESS_DETACH:
516 /***********************************************************************
517 * GetScreenSaveActive (X11DRV.@)
519 * Returns the active status of the screen saver
521 BOOL X11DRV_GetScreenSaveActive(void)
525 XGetScreenSaver(gdi_display, &timeout, &temp, &temp, &temp);
530 /***********************************************************************
531 * SetScreenSaveActive (X11DRV.@)
533 * Activate/Deactivate the screen saver
535 void X11DRV_SetScreenSaveActive(BOOL bActivate)
537 int timeout, interval, prefer_blanking, allow_exposures;
538 static int last_timeout = 15 * 60;
541 XGetScreenSaver(gdi_display, &timeout, &interval, &prefer_blanking,
543 if (timeout) last_timeout = timeout;
545 timeout = bActivate ? last_timeout : 0;
546 XSetScreenSaver(gdi_display, timeout, interval, prefer_blanking,