2 * X11DRV initialization code
4 * Copyright 1998 Patrik Stridvall
5 * Copyright 2000 Alexandre Julliard
10 #ifdef NO_REENTRANT_X11
11 /* Get pointers to the static errno and h_errno variables used by Xlib. This
12 must be done before including <errno.h> makes the variables invisible. */
14 static int *perrno = &errno;
16 static int *ph_errno = &h_errno;
17 #endif /* NO_REENTRANT_X11 */
25 #include <X11/cursorfont.h>
31 #include "wine/winbase16.h"
34 #include "debugtools.h"
42 DEFAULT_DEBUG_CHANNEL(x11drv);
44 static XKeyboardState keyboard_state;
45 static void (*old_tsx11_lock)(void);
46 static void (*old_tsx11_unlock)(void);
48 static CRITICAL_SECTION X11DRV_CritSection = CRITICAL_SECTION_INIT;
53 unsigned int screen_width;
54 unsigned int screen_height;
55 unsigned int screen_depth;
58 unsigned int X11DRV_server_startticks;
60 #ifdef NO_REENTRANT_X11
61 static int* (*old_errno_location)(void);
62 static int* (*old_h_errno_location)(void);
64 /***********************************************************************
67 * Get the per-thread errno location.
69 static int *x11_errno_location(void)
71 /* Use static libc errno while running in Xlib. */
72 if (X11DRV_CritSection.OwningThread == GetCurrentThreadId()) return perrno;
73 return old_errno_location();
76 /***********************************************************************
77 * x11_h_errno_location
79 * Get the per-thread h_errno location.
81 static int *x11_h_errno_location(void)
83 /* Use static libc h_errno while running in Xlib. */
84 if (X11DRV_CritSection.OwningThread == GetCurrentThreadId()) return ph_errno;
85 return old_h_errno_location();
87 #endif /* NO_REENTRANT_X11 */
89 /***********************************************************************
92 static int error_handler(Display *display, XErrorEvent *error_evt)
94 DebugBreak(); /* force an entry in the debugger */
98 /***********************************************************************
101 static void lock_tsx11(void)
103 RtlEnterCriticalSection( &X11DRV_CritSection );
106 /***********************************************************************
109 static void unlock_tsx11(void)
111 RtlLeaveCriticalSection( &X11DRV_CritSection );
114 /***********************************************************************
117 * Get the server startup time
118 * Won't be exact, but should be sufficient
120 static void get_server_startup(void)
123 gettimeofday( &t, NULL );
124 X11DRV_server_startticks = ((t.tv_sec * 1000) + (t.tv_usec / 1000)) - GetTickCount();
128 /***********************************************************************
131 * Setup the x11drv options.
133 static void setup_options(void)
139 if (RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\x11drv", 0, NULL,
140 REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, NULL ))
142 ERR("Cannot create config registry key\n" );
146 /* --display option */
148 count = sizeof(buffer);
149 if (!RegQueryValueExA( hkey, "display", 0, &type, buffer, &count ))
153 if (strcmp( buffer, Options.display ))
154 MESSAGE( "%s: warning: --display option ignored, using '%s'\n", argv0, buffer );
156 else if ((Options.display = getenv( "DISPLAY" )))
158 if (strcmp( buffer, Options.display ))
159 MESSAGE( "%s: warning: $DISPLAY variable ignored, using '%s'\n", argv0, buffer );
161 Options.display = strdup(buffer);
165 if (!Options.display && !(Options.display = getenv( "DISPLAY" )))
167 MESSAGE( "%s: no display specified\n", argv0 );
170 RegSetValueExA( hkey, "display", 0, REG_SZ, Options.display, strlen(Options.display)+1 );
173 /* check and set --managed and --desktop options in wine config file
174 * if it was not set on command line */
176 if ((!Options.managed) && (Options.desktopGeometry == NULL))
178 count = sizeof(buffer);
179 if (!RegQueryValueExA( hkey, "managed", 0, &type, buffer, &count ))
180 Options.managed = IS_OPTION_TRUE( buffer[0] );
182 count = sizeof(buffer);
183 if (!RegQueryValueExA( hkey, "Desktop", 0, &type, buffer, &count ))
184 /* Imperfect validation: If Desktop=N, then we don't turn on
185 ** the --desktop option. We should really validate for a correct
187 if (! IS_OPTION_FALSE(buffer[0]))
188 Options.desktopGeometry = strdup(buffer);
192 RegSetValueExA( hkey, "managed", 0, REG_SZ, "y", 2 );
194 if (Options.desktopGeometry)
195 RegSetValueExA( hkey, "desktop", 0, REG_SZ, Options.desktopGeometry, strlen(Options.desktopGeometry)+1 );
201 /***********************************************************************
204 * Create the desktop window for the --desktop mode.
206 static void create_desktop( const char *geometry )
208 int x = 0, y = 0, flags;
209 unsigned int width = 640, height = 480; /* Default size = 640x480 */
210 char *name = "Wine desktop";
211 XSizeHints *size_hints;
213 XClassHint *class_hints;
214 XSetWindowAttributes win_attr;
215 XTextProperty window_name;
216 Atom XA_WM_DELETE_WINDOW;
217 /* Used to create the desktop window with a good visual */
218 XVisualInfo *vi = NULL;
221 int err_base, evt_base;
223 /* Get in wine.ini if the desktop window should have a double-buffered visual or not.
224 But first, test if OpenGL is even supported on the display ! */
225 if (glXQueryExtension(display, &err_base, &evt_base) == True) {
226 dblbuf_visual = PROFILE_GetWineIniBool( "x11drv", "DesktopDoubleBuffered", 0 );
228 int dblBuf[]={GLX_RGBA,GLX_DEPTH_SIZE,16,GLX_DOUBLEBUFFER,None};
231 vi = glXChooseVisual(display, DefaultScreen(display), dblBuf);
232 win_attr.colormap = XCreateColormap(display, RootWindow(display,vi->screen),
233 vi->visual, AllocNone);
237 #endif /* HAVE_OPENGL */
239 flags = TSXParseGeometry( geometry, &x, &y, &width, &height );
240 screen_width = width;
241 screen_height = height;
244 win_attr.background_pixel = BlackPixel(display, 0);
245 win_attr.event_mask = ExposureMask | KeyPressMask | KeyReleaseMask |
246 PointerMotionMask | ButtonPressMask |
247 ButtonReleaseMask | EnterWindowMask;
248 win_attr.cursor = TSXCreateFontCursor( display, XC_top_left_arrow );
252 screen = ScreenOfDisplay(display, vi->screen);
253 screen_depth = vi->depth;
255 root_window = TSXCreateWindow( display,
256 (vi == NULL ? DefaultRootWindow(display) : RootWindow(display, vi->screen)),
257 x, y, width, height, 0,
258 (vi == NULL ? CopyFromParent : vi->depth),
260 (vi == NULL ? CopyFromParent : vi->visual),
261 CWBackPixel | CWEventMask | CWCursor | (vi == NULL ? 0 : CWColormap),
264 /* Set window manager properties */
265 size_hints = TSXAllocSizeHints();
266 wm_hints = TSXAllocWMHints();
267 class_hints = TSXAllocClassHint();
268 if (!size_hints || !wm_hints || !class_hints)
270 MESSAGE("Not enough memory for window manager hints.\n" );
273 size_hints->min_width = size_hints->max_width = width;
274 size_hints->min_height = size_hints->max_height = height;
275 size_hints->flags = PMinSize | PMaxSize;
276 if (flags & (XValue | YValue)) size_hints->flags |= USPosition;
277 if (flags & (WidthValue | HeightValue)) size_hints->flags |= USSize;
278 else size_hints->flags |= PSize;
280 wm_hints->flags = InputHint | StateHint;
281 wm_hints->input = True;
282 wm_hints->initial_state = NormalState;
283 class_hints->res_name = (char *)argv0;
284 class_hints->res_class = "Wine";
286 TSXStringListToTextProperty( &name, 1, &window_name );
287 TSXSetWMProperties( display, root_window, &window_name, &window_name,
288 NULL, 0, size_hints, wm_hints, class_hints );
289 XA_WM_DELETE_WINDOW = TSXInternAtom( display, "WM_DELETE_WINDOW", False );
290 TSXSetWMProtocols( display, root_window, &XA_WM_DELETE_WINDOW, 1 );
291 TSXFree( size_hints );
293 TSXFree( class_hints );
296 TSXMapWindow( display, root_window );
300 /***********************************************************************
301 * X11DRV process initialisation routine
303 static void process_attach(void)
305 WND_Driver = &X11DRV_WND_Driver;
307 get_server_startup();
310 /* setup TSX11 locking */
311 #ifdef NO_REENTRANT_X11
312 old_errno_location = (void *)InterlockedExchange( (PLONG)&wine_errno_location,
313 (LONG)x11_errno_location );
314 old_h_errno_location = (void *)InterlockedExchange( (PLONG)&wine_h_errno_location,
315 (LONG)x11_h_errno_location );
316 #endif /* NO_REENTRANT_X11 */
317 old_tsx11_lock = wine_tsx11_lock;
318 old_tsx11_unlock = wine_tsx11_unlock;
319 wine_tsx11_lock = lock_tsx11;
320 wine_tsx11_unlock = unlock_tsx11;
324 if (!(display = TSXOpenDisplay( Options.display )))
326 MESSAGE( "%s: Can't open display: %s\n", argv0, Options.display );
329 fcntl( ConnectionNumber(display), F_SETFD, 1 ); /* set close on exec flag */
330 screen = DefaultScreenOfDisplay( display );
331 visual = DefaultVisual( display, DefaultScreen(display) );
332 root_window = DefaultRootWindow( display );
334 /* Initialize screen depth */
336 screen_depth = PROFILE_GetWineIniInt( "x11drv", "ScreenDepth", 0 );
337 if (screen_depth) /* depth specified */
340 int *depth_list = TSXListDepths(display, DefaultScreen(display), &depth_count);
341 for (i = 0; i < depth_count; i++)
342 if (depth_list[i] == screen_depth) break;
343 TSXFree( depth_list );
344 if (i >= depth_count)
346 MESSAGE( "%s: Depth %d not supported on this screen.\n", argv0, screen_depth );
350 else screen_depth = DefaultDepthOfScreen( screen );
352 /* tell the libX11 that we will do input method handling ourselves
353 * that keep libX11 from doing anything whith dead keys, allowing Wine
354 * to have total control over dead keys, that is this line allows
355 * them to work in Wine, even whith a libX11 including the dead key
356 * patches from Th.Quinot (http://Web.FdN.FR/~tquinot/dead-keys.en.html)
358 TSXOpenIM( display, NULL, NULL, NULL);
360 if (Options.synchronous) XSetErrorHandler( error_handler );
362 screen_width = WidthOfScreen( screen );
363 screen_height = HeightOfScreen( screen );
365 if (Options.desktopGeometry)
367 Options.managed = FALSE;
368 create_desktop( Options.desktopGeometry );
372 if(!X11DRV_GDI_Initialize())
374 MESSAGE( "%s: X11DRV Couldn't Initialize GDI.\n", argv0 );
379 /* save keyboard setup */
380 TSXGetKeyboardControl(display, &keyboard_state);
382 /* initialize event handling */
385 /* load display.dll */
386 LoadLibrary16( "display" );
390 /***********************************************************************
391 * X11DRV process termination routine
393 static void process_detach(void)
395 /* restore keyboard setup */
396 XKeyboardControl keyboard_value;
398 keyboard_value.key_click_percent = keyboard_state.key_click_percent;
399 keyboard_value.bell_percent = keyboard_state.bell_percent;
400 keyboard_value.bell_pitch = keyboard_state.bell_pitch;
401 keyboard_value.bell_duration = keyboard_state.bell_duration;
402 keyboard_value.auto_repeat_mode = keyboard_state.global_auto_repeat;
404 XChangeKeyboardControl(display, KBKeyClickPercent | KBBellPercent |
405 KBBellPitch | KBBellDuration | KBAutoRepeatMode, &keyboard_value);
408 X11DRV_GDI_Finalize();
410 /* restore TSX11 locking */
411 wine_tsx11_lock = old_tsx11_lock;
412 wine_tsx11_unlock = old_tsx11_unlock;
413 #ifdef NO_REENTRANT_X11
414 wine_errno_location = old_errno_location;
415 wine_h_errno_location = old_h_errno_location;
416 #endif /* NO_REENTRANT_X11 */
419 /* close the display */
420 XCloseDisplay( display );
428 /***********************************************************************
429 * X11DRV initialisation routine
431 BOOL WINAPI X11DRV_Init( HINSTANCE hinst, DWORD reason, LPVOID reserved )
435 case DLL_PROCESS_ATTACH:
438 case DLL_PROCESS_DETACH:
445 /***********************************************************************
446 * X11DRV_GetScreenSaveActive
448 * Returns the active status of the screen saver
450 BOOL X11DRV_GetScreenSaveActive(void)
453 TSXGetScreenSaver(display, &timeout, &temp, &temp, &temp);
457 /***********************************************************************
458 * X11DRV_SetScreenSaveActive
460 * Activate/Deactivate the screen saver
462 void X11DRV_SetScreenSaveActive(BOOL bActivate)
465 TSXActivateScreenSaver(display);
467 TSXResetScreenSaver(display);
470 /***********************************************************************
471 * X11DRV_GetScreenSaveTimeout
473 * Return the screen saver timeout
475 int X11DRV_GetScreenSaveTimeout(void)
478 TSXGetScreenSaver(display, &timeout, &temp, &temp, &temp);
482 /***********************************************************************
483 * X11DRV_SetScreenSaveTimeout
485 * Set the screen saver timeout
487 void X11DRV_SetScreenSaveTimeout(int nTimeout)
489 /* timeout is a 16bit entity (CARD16) in the protocol, so it should
490 * not get over 32767 or it will get negative. */
491 if (nTimeout>32767) nTimeout = 32767;
492 TSXSetScreenSaver(display, nTimeout, 60, DefaultBlanking, DefaultExposures);
495 /***********************************************************************
496 * X11DRV_IsSingleWindow
498 BOOL X11DRV_IsSingleWindow(void)
500 return (root_window != DefaultRootWindow(display));