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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/port.h"
30 #ifdef HAVE_SYS_TIME_H
31 # include <sys/time.h>
36 #include <X11/cursorfont.h>
39 #include <X11/XKBlib.h>
41 #ifdef HAVE_X11_EXTENSIONS_XRENDER_H
42 #include <X11/extensions/Xrender.h>
50 #include "xcomposite.h"
51 #include "wine/server.h"
52 #include "wine/debug.h"
53 #include "wine/library.h"
55 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
56 WINE_DECLARE_DEBUG_CHANNEL(synchronous);
57 WINE_DECLARE_DEBUG_CHANNEL(winediag);
59 XVisualInfo default_visual = { 0 };
60 Colormap default_colormap = None;
61 XPixmapFormatValues **pixmap_formats;
62 unsigned int screen_width;
63 unsigned int screen_height;
64 unsigned int screen_bpp;
65 RECT virtual_screen_rect;
69 int usexcomposite = 1;
71 int use_take_focus = 1;
72 int use_primary_selection = 0;
73 int use_system_cursors = 1;
76 int grab_fullscreen = 0;
78 int decorated_mode = 1;
79 int private_color_map = 0;
80 int primary_monitor = 0;
81 int client_side_graphics = 1;
82 int client_side_with_render = 1;
83 int client_side_antialias_with_core = 1;
84 int client_side_antialias_with_render = 1;
85 int copy_default_colors = 128;
86 int alloc_system_colors = 256;
87 DWORD thread_data_tls_index = TLS_OUT_OF_INDEXES;
88 int xrender_error_base = 0;
89 HMODULE x11drv_module = 0;
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 Atom X11DRV_Atoms[NB_XATOMS - FIRST_XATOM];
107 static const char * const atom_names[NB_XATOMS - FIRST_XATOM] =
131 "_NET_STARTUP_INFO_BEGIN",
134 "_NET_SYSTEM_TRAY_OPCODE",
135 "_NET_SYSTEM_TRAY_S0",
137 "_NET_WM_MOVERESIZE",
142 "_NET_WM_STATE_ABOVE",
143 "_NET_WM_STATE_FULLSCREEN",
144 "_NET_WM_STATE_MAXIMIZED_HORZ",
145 "_NET_WM_STATE_MAXIMIZED_VERT",
146 "_NET_WM_STATE_SKIP_PAGER",
147 "_NET_WM_STATE_SKIP_TASKBAR",
149 "_NET_WM_USER_TIME_WINDOW",
150 "_NET_WM_WINDOW_OPACITY",
151 "_NET_WM_WINDOW_TYPE",
152 "_NET_WM_WINDOW_TYPE_DIALOG",
153 "_NET_WM_WINDOW_TYPE_NORMAL",
154 "_NET_WM_WINDOW_TYPE_UTILITY",
179 "WCF_DSPENHMETAFILE",
180 "WCF_DSPMETAFILEPICT",
205 /***********************************************************************
208 * Check if the X error is one we can ignore.
210 static inline BOOL ignore_error( Display *display, XErrorEvent *event )
212 if ((event->request_code == X_SetInputFocus || event->request_code == X_ChangeWindowAttributes) &&
213 (event->error_code == BadMatch || event->error_code == BadWindow)) return TRUE;
215 /* ignore a number of errors on gdi display caused by creating/destroying windows */
216 if (display == gdi_display)
218 if (event->error_code == BadDrawable ||
219 event->error_code == BadGC ||
220 event->error_code == BadWindow)
222 #ifdef HAVE_X11_EXTENSIONS_XRENDER_H
223 if (xrender_error_base) /* check for XRender errors */
225 if (event->error_code == xrender_error_base + BadPicture) return TRUE;
233 /***********************************************************************
234 * X11DRV_expect_error
236 * Setup a callback function that will be called on an X error. The
237 * callback must return non-zero if the error is the one it expected.
239 void X11DRV_expect_error( Display *display, x11drv_error_callback callback, void *arg )
241 err_callback = callback;
242 err_callback_display = display;
243 err_callback_arg = arg;
244 err_callback_result = 0;
245 err_serial = NextRequest(display);
249 /***********************************************************************
252 * Check if an expected X11 error occurred; return non-zero if yes.
253 * The caller is responsible for calling XSync first if necessary.
255 int X11DRV_check_error(void)
258 return err_callback_result;
262 /***********************************************************************
265 static int error_handler( Display *display, XErrorEvent *error_evt )
267 if (err_callback && display == err_callback_display &&
268 (long)(error_evt->serial - err_serial) >= 0)
270 if ((err_callback_result = err_callback( display, error_evt, err_callback_arg )))
272 TRACE( "got expected error %d req %d\n",
273 error_evt->error_code, error_evt->request_code );
277 if (ignore_error( display, error_evt ))
279 TRACE( "got ignored error %d req %d\n",
280 error_evt->error_code, error_evt->request_code );
283 if (TRACE_ON(synchronous))
285 ERR( "X protocol error: serial=%ld, request_code=%d - breaking into debugger\n",
286 error_evt->serial, error_evt->request_code );
287 DebugBreak(); /* force an entry in the debugger */
289 old_error_handler( display, error_evt );
293 /***********************************************************************
294 * init_pixmap_formats
296 static void init_pixmap_formats( Display *display )
298 int i, count, max = 32;
299 XPixmapFormatValues *formats = XListPixmapFormats( display, &count );
301 for (i = 0; i < count; i++)
303 TRACE( "depth %u, bpp %u, pad %u\n",
304 formats[i].depth, formats[i].bits_per_pixel, formats[i].scanline_pad );
305 if (formats[i].depth > max) max = formats[i].depth;
307 pixmap_formats = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pixmap_formats) * (max + 1) );
308 for (i = 0; i < count; i++) pixmap_formats[formats[i].depth] = &formats[i];
312 /***********************************************************************
315 * Get a config key from either the app-specific or the default config
317 static inline DWORD get_config_key( HKEY defkey, HKEY appkey, const char *name,
318 char *buffer, DWORD size )
320 if (appkey && !RegQueryValueExA( appkey, name, 0, NULL, (LPBYTE)buffer, &size )) return 0;
321 if (defkey && !RegQueryValueExA( defkey, name, 0, NULL, (LPBYTE)buffer, &size )) return 0;
322 return ERROR_FILE_NOT_FOUND;
326 /***********************************************************************
329 * Setup the x11drv options.
331 static void setup_options(void)
333 char buffer[MAX_PATH+16];
334 HKEY hkey, appkey = 0;
337 /* @@ Wine registry key: HKCU\Software\Wine\X11 Driver */
338 if (RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\X11 Driver", &hkey )) hkey = 0;
340 /* open the app-specific key */
342 len = (GetModuleFileNameA( 0, buffer, MAX_PATH ));
343 if (len && len < MAX_PATH)
346 char *p, *appname = buffer;
347 if ((p = strrchr( appname, '/' ))) appname = p + 1;
348 if ((p = strrchr( appname, '\\' ))) appname = p + 1;
349 strcat( appname, "\\X11 Driver" );
350 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\X11 Driver */
351 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey ))
353 if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
354 RegCloseKey( tmpkey );
358 if (!get_config_key( hkey, appkey, "Managed", buffer, sizeof(buffer) ))
359 managed_mode = IS_OPTION_TRUE( buffer[0] );
361 if (!get_config_key( hkey, appkey, "Decorated", buffer, sizeof(buffer) ))
362 decorated_mode = IS_OPTION_TRUE( buffer[0] );
364 if (!get_config_key( hkey, appkey, "UseXVidMode", buffer, sizeof(buffer) ))
365 usexvidmode = IS_OPTION_TRUE( buffer[0] );
367 if (!get_config_key( hkey, appkey, "UseXRandR", buffer, sizeof(buffer) ))
368 usexrandr = IS_OPTION_TRUE( buffer[0] );
370 if (!get_config_key( hkey, appkey, "UseTakeFocus", buffer, sizeof(buffer) ))
371 use_take_focus = IS_OPTION_TRUE( buffer[0] );
373 if (!get_config_key( hkey, appkey, "UsePrimarySelection", buffer, sizeof(buffer) ))
374 use_primary_selection = IS_OPTION_TRUE( buffer[0] );
376 if (!get_config_key( hkey, appkey, "UseSystemCursors", buffer, sizeof(buffer) ))
377 use_system_cursors = IS_OPTION_TRUE( buffer[0] );
379 if (!get_config_key( hkey, appkey, "ShowSystray", buffer, sizeof(buffer) ))
380 show_systray = IS_OPTION_TRUE( buffer[0] );
382 if (!get_config_key( hkey, appkey, "GrabPointer", buffer, sizeof(buffer) ))
383 grab_pointer = IS_OPTION_TRUE( buffer[0] );
385 if (!get_config_key( hkey, appkey, "GrabFullscreen", buffer, sizeof(buffer) ))
386 grab_fullscreen = IS_OPTION_TRUE( buffer[0] );
388 if (!get_config_key( hkey, appkey, "ScreenDepth", buffer, sizeof(buffer) ))
389 default_visual.depth = atoi(buffer);
391 if (!get_config_key( hkey, appkey, "ClientSideGraphics", buffer, sizeof(buffer) ))
392 client_side_graphics = IS_OPTION_TRUE( buffer[0] );
394 if (!get_config_key( hkey, appkey, "ClientSideWithRender", buffer, sizeof(buffer) ))
395 client_side_with_render = IS_OPTION_TRUE( buffer[0] );
397 if (!get_config_key( hkey, appkey, "ClientSideAntiAliasWithCore", buffer, sizeof(buffer) ))
398 client_side_antialias_with_core = IS_OPTION_TRUE( buffer[0] );
400 if (!get_config_key( hkey, appkey, "ClientSideAntiAliasWithRender", buffer, sizeof(buffer) ))
401 client_side_antialias_with_render = IS_OPTION_TRUE( buffer[0] );
403 if (!get_config_key( hkey, appkey, "UseXIM", buffer, sizeof(buffer) ))
404 use_xim = IS_OPTION_TRUE( buffer[0] );
406 if (!get_config_key( hkey, appkey, "PrivateColorMap", buffer, sizeof(buffer) ))
407 private_color_map = IS_OPTION_TRUE( buffer[0] );
409 if (!get_config_key( hkey, appkey, "PrimaryMonitor", buffer, sizeof(buffer) ))
410 primary_monitor = atoi( buffer );
412 if (!get_config_key( hkey, appkey, "CopyDefaultColors", buffer, sizeof(buffer) ))
413 copy_default_colors = atoi(buffer);
415 if (!get_config_key( hkey, appkey, "AllocSystemColors", buffer, sizeof(buffer) ))
416 alloc_system_colors = atoi(buffer);
418 get_config_key( hkey, appkey, "InputStyle", input_style, sizeof(input_style) );
420 if (appkey) RegCloseKey( appkey );
421 if (hkey) RegCloseKey( hkey );
424 #ifdef SONAME_LIBXCOMPOSITE
426 #define MAKE_FUNCPTR(f) typeof(f) * p##f;
427 MAKE_FUNCPTR(XCompositeQueryExtension)
428 MAKE_FUNCPTR(XCompositeQueryVersion)
429 MAKE_FUNCPTR(XCompositeVersion)
430 MAKE_FUNCPTR(XCompositeRedirectWindow)
431 MAKE_FUNCPTR(XCompositeRedirectSubwindows)
432 MAKE_FUNCPTR(XCompositeUnredirectWindow)
433 MAKE_FUNCPTR(XCompositeUnredirectSubwindows)
434 MAKE_FUNCPTR(XCompositeCreateRegionFromBorderClip)
435 MAKE_FUNCPTR(XCompositeNameWindowPixmap)
438 static int xcomp_event_base;
439 static int xcomp_error_base;
441 static void X11DRV_XComposite_Init(void)
443 void *xcomposite_handle = wine_dlopen(SONAME_LIBXCOMPOSITE, RTLD_NOW, NULL, 0);
444 if (!xcomposite_handle)
446 TRACE("Unable to open %s, XComposite disabled\n", SONAME_LIBXCOMPOSITE);
451 #define LOAD_FUNCPTR(f) \
452 if((p##f = wine_dlsym(xcomposite_handle, #f, NULL, 0)) == NULL) \
454 LOAD_FUNCPTR(XCompositeQueryExtension)
455 LOAD_FUNCPTR(XCompositeQueryVersion)
456 LOAD_FUNCPTR(XCompositeVersion)
457 LOAD_FUNCPTR(XCompositeRedirectWindow)
458 LOAD_FUNCPTR(XCompositeRedirectSubwindows)
459 LOAD_FUNCPTR(XCompositeUnredirectWindow)
460 LOAD_FUNCPTR(XCompositeUnredirectSubwindows)
461 LOAD_FUNCPTR(XCompositeCreateRegionFromBorderClip)
462 LOAD_FUNCPTR(XCompositeNameWindowPixmap)
465 if(!pXCompositeQueryExtension(gdi_display, &xcomp_event_base,
466 &xcomp_error_base)) {
467 TRACE("XComposite extension could not be queried; disabled\n");
468 wine_dlclose(xcomposite_handle, NULL, 0);
469 xcomposite_handle = NULL;
473 TRACE("XComposite is up and running error_base = %d\n", xcomp_error_base);
477 TRACE("Unable to load function pointers from %s, XComposite disabled\n", SONAME_LIBXCOMPOSITE);
478 wine_dlclose(xcomposite_handle, NULL, 0);
479 xcomposite_handle = NULL;
482 #endif /* defined(SONAME_LIBXCOMPOSITE) */
484 static void init_visuals( Display *display, int screen )
489 default_visual.screen = screen;
490 if (default_visual.depth) /* depth specified */
492 info = XGetVisualInfo( display, VisualScreenMask | VisualDepthMask, &default_visual, &count );
495 default_visual = *info;
498 else WARN( "no visual found for depth %d\n", default_visual.depth );
501 if (!default_visual.visual)
503 default_visual.depth = DefaultDepth( display, screen );
504 default_visual.visual = DefaultVisual( display, screen );
505 default_visual.visualid = default_visual.visual->visualid;
506 default_visual.class = default_visual.visual->class;
507 default_visual.red_mask = default_visual.visual->red_mask;
508 default_visual.green_mask = default_visual.visual->green_mask;
509 default_visual.blue_mask = default_visual.visual->blue_mask;
510 default_visual.colormap_size = default_visual.visual->map_entries;
511 default_visual.bits_per_rgb = default_visual.visual->bits_per_rgb;
513 default_colormap = XCreateColormap( gdi_display, root_window, default_visual.visual, AllocNone );
514 TRACE( "default visual %lx class %u\n", default_visual.visualid, default_visual.class );
517 /***********************************************************************
518 * X11DRV process initialisation routine
520 static BOOL process_attach(void)
524 void *libx11 = wine_dlopen( SONAME_LIBX11, RTLD_NOW|RTLD_GLOBAL, error, sizeof(error) );
528 ERR( "failed to load %s: %s\n", SONAME_LIBX11, error );
531 pXGetEventData = wine_dlsym( libx11, "XGetEventData", NULL, 0 );
532 pXFreeEventData = wine_dlsym( libx11, "XFreeEventData", NULL, 0 );
533 #ifdef SONAME_LIBXEXT
534 wine_dlopen( SONAME_LIBXEXT, RTLD_NOW|RTLD_GLOBAL, NULL, 0 );
539 if ((thread_data_tls_index = TlsAlloc()) == TLS_OUT_OF_INDEXES) return FALSE;
543 if (!XInitThreads()) ERR( "XInitThreads failed, trouble ahead\n" );
544 if (!(display = XOpenDisplay( NULL ))) return FALSE;
546 fcntl( ConnectionNumber(display), F_SETFD, 1 ); /* set close on exec flag */
547 root_window = DefaultRootWindow( display );
548 gdi_display = display;
549 old_error_handler = XSetErrorHandler( error_handler );
551 init_pixmap_formats( display );
552 init_visuals( display, DefaultScreen( display ));
553 screen_bpp = pixmap_formats[default_visual.depth]->bits_per_pixel;
555 XInternAtoms( display, (char **)atom_names, NB_XATOMS - FIRST_XATOM, False, X11DRV_Atoms );
557 winContext = XUniqueContext();
558 win_data_context = XUniqueContext();
559 cursor_context = XUniqueContext();
561 if (TRACE_ON(synchronous)) XSynchronize( display, True );
563 xinerama_init( DisplayWidth( display, default_visual.screen ),
564 DisplayHeight( display, default_visual.screen ));
565 X11DRV_Settings_Init();
567 /* initialize XVidMode */
568 X11DRV_XF86VM_Init();
569 /* initialize XRandR */
570 X11DRV_XRandR_Init();
571 #ifdef SONAME_LIBXCOMPOSITE
572 X11DRV_XComposite_Init();
574 X11DRV_XInput2_Init();
577 if (use_xkb) use_xkb = XkbUseExtension( gdi_display, NULL, NULL );
579 X11DRV_InitKeyboard( gdi_display );
580 X11DRV_InitClipboard();
581 if (use_xim) use_xim = X11DRV_InitXIM( input_style );
587 /***********************************************************************
588 * X11DRV thread termination routine
590 static void thread_detach(void)
592 struct x11drv_thread_data *data = TlsGetValue( thread_data_tls_index );
596 X11DRV_ResetSelectionOwner();
597 if (data->xim) XCloseIM( data->xim );
598 if (data->font_set) XFreeFontSet( data->display, data->font_set );
599 XCloseDisplay( data->display );
600 HeapFree( GetProcessHeap(), 0, data );
605 /* store the display fd into the message queue */
606 static void set_queue_display_fd( Display *display )
611 if (wine_server_fd_to_handle( ConnectionNumber(display), GENERIC_READ | SYNCHRONIZE, 0, &handle ))
613 MESSAGE( "x11drv: Can't allocate handle for display fd\n" );
616 SERVER_START_REQ( set_queue_fd )
618 req->handle = wine_server_obj_handle( handle );
619 ret = wine_server_call( req );
624 MESSAGE( "x11drv: Can't store handle for display fd\n" );
627 CloseHandle( handle );
631 /***********************************************************************
632 * X11DRV thread initialisation routine
634 struct x11drv_thread_data *x11drv_init_thread_data(void)
636 struct x11drv_thread_data *data = x11drv_thread_data();
638 if (data) return data;
640 if (!(data = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data) )))
642 ERR( "could not create data\n" );
645 if (!(data->display = XOpenDisplay(NULL)))
647 ERR_(winediag)( "x11drv: Can't open display: %s. Please ensure that your X server is running and that $DISPLAY is set correctly.\n", XDisplayName(NULL));
651 fcntl( ConnectionNumber(data->display), F_SETFD, 1 ); /* set close on exec flag */
654 if (use_xkb && XkbUseExtension( data->display, NULL, NULL ))
655 XkbSetDetectableAutoRepeat( data->display, True, NULL );
658 if (TRACE_ON(synchronous)) XSynchronize( data->display, True );
660 set_queue_display_fd( data->display );
661 TlsSetValue( thread_data_tls_index, data );
663 if (use_xim) X11DRV_SetupXIM();
669 /***********************************************************************
670 * X11DRV initialisation routine
672 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
678 case DLL_PROCESS_ATTACH:
679 x11drv_module = hinst;
680 ret = process_attach();
682 case DLL_THREAD_DETACH:
689 /***********************************************************************
690 * GetScreenSaveActive (X11DRV.@)
692 * Returns the active status of the screen saver
694 BOOL CDECL X11DRV_GetScreenSaveActive(void)
697 XGetScreenSaver(gdi_display, &timeout, &temp, &temp, &temp);
701 /***********************************************************************
702 * SetScreenSaveActive (X11DRV.@)
704 * Activate/Deactivate the screen saver
706 void CDECL X11DRV_SetScreenSaveActive(BOOL bActivate)
708 int timeout, interval, prefer_blanking, allow_exposures;
709 static int last_timeout = 15 * 60;
711 XLockDisplay( gdi_display );
712 XGetScreenSaver(gdi_display, &timeout, &interval, &prefer_blanking,
714 if (timeout) last_timeout = timeout;
716 timeout = bActivate ? last_timeout : 0;
717 XSetScreenSaver(gdi_display, timeout, interval, prefer_blanking,
719 XUnlockDisplay( gdi_display );