2 * X11DRV initialization code
4 * Copyright 1998 Patrik Stridvall
5 * Copyright 2000 Alexandre Julliard
16 #include <X11/cursorfont.h>
22 #include "wine/winbase16.h"
26 #include "debugtools.h"
34 DEFAULT_DEBUG_CHANNEL(x11drv);
36 static XKeyboardState keyboard_state;
37 static void (*old_tsx11_lock)(void);
38 static void (*old_tsx11_unlock)(void);
43 unsigned int screen_width;
44 unsigned int screen_height;
45 unsigned int screen_depth;
48 unsigned int X11DRV_server_startticks;
50 /***********************************************************************
53 static int error_handler(Display *display, XErrorEvent *error_evt)
55 DebugBreak(); /* force an entry in the debugger */
59 /***********************************************************************
62 static void lock_tsx11(void)
64 RtlEnterCriticalSection( &X11DRV_CritSection );
67 /***********************************************************************
70 static void unlock_tsx11(void)
72 RtlLeaveCriticalSection( &X11DRV_CritSection );
75 /***********************************************************************
78 * Get the server startup time
79 * Won't be exact, but should be sufficient
81 static void get_server_startup(void)
84 gettimeofday( &t, NULL );
85 X11DRV_server_startticks = ((t.tv_sec * 1000) + (t.tv_usec / 1000)) - GetTickCount();
89 /***********************************************************************
92 * Setup the x11drv options.
94 static void setup_options(void)
100 if (RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\x11drv", 0, NULL,
101 REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, NULL ))
103 ERR("Cannot create config registry key\n" );
107 /* --display option */
109 count = sizeof(buffer);
110 if (!RegQueryValueExA( hkey, "display", 0, &type, buffer, &count ))
114 if (strcmp( buffer, Options.display ))
115 MESSAGE( "%s: warning: --display option ignored, using '%s'\n", argv0, buffer );
117 else if ((Options.display = getenv( "DISPLAY" )))
119 if (strcmp( buffer, Options.display ))
120 MESSAGE( "%s: warning: $DISPLAY variable ignored, using '%s'\n", argv0, buffer );
122 Options.display = strdup(buffer);
126 if (!Options.display && !(Options.display = getenv( "DISPLAY" )))
128 MESSAGE( "%s: no display specified\n", argv0 );
131 RegSetValueExA( hkey, "display", 0, REG_SZ, Options.display, strlen(Options.display)+1 );
134 /* check and set --managed and --desktop options in wine config file
135 * if it was not set on command line */
137 if ((!Options.managed) && (Options.desktopGeometry == NULL))
139 count = sizeof(buffer);
140 if (!RegQueryValueExA( hkey, "managed", 0, &type, buffer, &count ))
141 Options.managed = IS_OPTION_TRUE( buffer[0] );
143 count = sizeof(buffer);
144 if (!RegQueryValueExA( hkey, "Desktop", 0, &type, buffer, &count ))
145 /* Imperfect validation: If Desktop=N, then we don't turn on
146 ** the --desktop option. We should really validate for a correct
148 if (! IS_OPTION_FALSE(buffer[0]))
149 Options.desktopGeometry = strdup(buffer);
153 RegSetValueExA( hkey, "managed", 0, REG_SZ, "y", 2 );
155 if (Options.desktopGeometry)
156 RegSetValueExA( hkey, "desktop", 0, REG_SZ, Options.desktopGeometry, strlen(Options.desktopGeometry)+1 );
162 /***********************************************************************
165 * Create the desktop window for the --desktop mode.
167 static void create_desktop( const char *geometry )
169 int x = 0, y = 0, flags;
170 unsigned int width = 640, height = 480; /* Default size = 640x480 */
171 char *name = "Wine desktop";
172 XSizeHints *size_hints;
174 XClassHint *class_hints;
175 XSetWindowAttributes win_attr;
176 XTextProperty window_name;
177 Atom XA_WM_DELETE_WINDOW;
178 /* Used to create the desktop window with a good visual */
179 XVisualInfo *vi = NULL;
182 int err_base, evt_base;
184 /* Get in wine.ini if the desktop window should have a double-buffered visual or not.
185 But first, test if OpenGL is even supported on the display ! */
186 if (glXQueryExtension(display, &err_base, &evt_base) == True) {
187 dblbuf_visual = PROFILE_GetWineIniBool( "x11drv", "DesktopDoubleBuffered", 0 );
189 int dblBuf[]={GLX_RGBA,GLX_DEPTH_SIZE,16,GLX_DOUBLEBUFFER,None};
192 vi = glXChooseVisual(display, DefaultScreen(display), dblBuf);
193 win_attr.colormap = XCreateColormap(display, RootWindow(display,vi->screen),
194 vi->visual, AllocNone);
198 #endif /* HAVE_OPENGL */
200 flags = TSXParseGeometry( geometry, &x, &y, &width, &height );
201 screen_width = width;
202 screen_height = height;
205 win_attr.background_pixel = BlackPixel(display, 0);
206 win_attr.event_mask = ExposureMask | KeyPressMask | KeyReleaseMask |
207 PointerMotionMask | ButtonPressMask |
208 ButtonReleaseMask | EnterWindowMask;
209 win_attr.cursor = TSXCreateFontCursor( display, XC_top_left_arrow );
213 screen = ScreenOfDisplay(display, vi->screen);
214 screen_depth = vi->depth;
216 root_window = TSXCreateWindow( display,
217 (vi == NULL ? DefaultRootWindow(display) : RootWindow(display, vi->screen)),
218 x, y, width, height, 0,
219 (vi == NULL ? CopyFromParent : vi->depth),
221 (vi == NULL ? CopyFromParent : vi->visual),
222 CWBackPixel | CWEventMask | CWCursor | (vi == NULL ? 0 : CWColormap),
225 /* Set window manager properties */
226 size_hints = TSXAllocSizeHints();
227 wm_hints = TSXAllocWMHints();
228 class_hints = TSXAllocClassHint();
229 if (!size_hints || !wm_hints || !class_hints)
231 MESSAGE("Not enough memory for window manager hints.\n" );
234 size_hints->min_width = size_hints->max_width = width;
235 size_hints->min_height = size_hints->max_height = height;
236 size_hints->flags = PMinSize | PMaxSize;
237 if (flags & (XValue | YValue)) size_hints->flags |= USPosition;
238 if (flags & (WidthValue | HeightValue)) size_hints->flags |= USSize;
239 else size_hints->flags |= PSize;
241 wm_hints->flags = InputHint | StateHint;
242 wm_hints->input = True;
243 wm_hints->initial_state = NormalState;
244 class_hints->res_name = (char *)argv0;
245 class_hints->res_class = "Wine";
247 TSXStringListToTextProperty( &name, 1, &window_name );
248 TSXSetWMProperties( display, root_window, &window_name, &window_name,
249 NULL, 0, size_hints, wm_hints, class_hints );
250 XA_WM_DELETE_WINDOW = TSXInternAtom( display, "WM_DELETE_WINDOW", False );
251 TSXSetWMProtocols( display, root_window, &XA_WM_DELETE_WINDOW, 1 );
252 TSXFree( size_hints );
254 TSXFree( class_hints );
257 TSXMapWindow( display, root_window );
260 /* Created so that XOpenIM can be called using the 'large stack' */
261 static void XOpenIM_large_stack(void)
263 TSXOpenIM(display,NULL,NULL,NULL);
266 /***********************************************************************
267 * X11DRV process initialisation routine
269 static void process_attach(void)
271 WND_Driver = &X11DRV_WND_Driver;
273 get_server_startup();
276 /* setup TSX11 locking */
277 old_tsx11_lock = wine_tsx11_lock;
278 old_tsx11_unlock = wine_tsx11_unlock;
279 wine_tsx11_lock = lock_tsx11;
280 wine_tsx11_unlock = unlock_tsx11;
284 if (!(display = TSXOpenDisplay( Options.display )))
286 MESSAGE( "%s: Can't open display: %s\n", argv0, Options.display );
289 fcntl( ConnectionNumber(display), F_SETFD, 1 ); /* set close on exec flag */
290 screen = DefaultScreenOfDisplay( display );
291 visual = DefaultVisual( display, DefaultScreen(display) );
292 root_window = DefaultRootWindow( display );
294 /* Initialize screen depth */
296 screen_depth = PROFILE_GetWineIniInt( "x11drv", "ScreenDepth", 0 );
297 if (screen_depth) /* depth specified */
300 int *depth_list = TSXListDepths(display, DefaultScreen(display), &depth_count);
301 for (i = 0; i < depth_count; i++)
302 if (depth_list[i] == screen_depth) break;
303 TSXFree( depth_list );
304 if (i >= depth_count)
306 MESSAGE( "%s: Depth %d not supported on this screen.\n", argv0, screen_depth );
310 else screen_depth = DefaultDepthOfScreen( screen );
312 /* tell the libX11 that we will do input method handling ourselves
313 * that keep libX11 from doing anything whith dead keys, allowing Wine
314 * to have total control over dead keys, that is this line allows
315 * them to work in Wine, even whith a libX11 including the dead key
316 * patches from Th.Quinot (http://Web.FdN.FR/~tquinot/dead-keys.en.html)
318 CALL_LARGE_STACK( XOpenIM_large_stack, NULL );
320 if (Options.synchronous) XSetErrorHandler( error_handler );
322 screen_width = WidthOfScreen( screen );
323 screen_height = HeightOfScreen( screen );
325 if (Options.desktopGeometry)
327 Options.managed = FALSE;
328 create_desktop( Options.desktopGeometry );
332 X11DRV_GDI_Initialize();
334 /* save keyboard setup */
335 TSXGetKeyboardControl(display, &keyboard_state);
337 /* initialize event handling */
340 /* load display.dll */
341 LoadLibrary16( "display" );
345 /***********************************************************************
346 * X11DRV process termination routine
348 static void process_detach(void)
350 /* restore keyboard setup */
351 XKeyboardControl keyboard_value;
353 keyboard_value.key_click_percent = keyboard_state.key_click_percent;
354 keyboard_value.bell_percent = keyboard_state.bell_percent;
355 keyboard_value.bell_pitch = keyboard_state.bell_pitch;
356 keyboard_value.bell_duration = keyboard_state.bell_duration;
357 keyboard_value.auto_repeat_mode = keyboard_state.global_auto_repeat;
359 XChangeKeyboardControl(display, KBKeyClickPercent | KBBellPercent |
360 KBBellPitch | KBBellDuration | KBAutoRepeatMode, &keyboard_value);
363 X11DRV_GDI_Finalize();
365 /* restore TSX11 locking */
366 wine_tsx11_lock = old_tsx11_lock;
367 wine_tsx11_unlock = old_tsx11_unlock;
370 /* close the display */
371 XCloseDisplay( display );
379 /***********************************************************************
380 * X11DRV initialisation routine
382 BOOL WINAPI X11DRV_Init( HINSTANCE hinst, DWORD reason, LPVOID reserved )
384 static int process_count;
388 case DLL_PROCESS_ATTACH:
389 if (!process_count++) process_attach();
391 case DLL_PROCESS_DETACH:
392 if (!--process_count) process_detach();
398 /***********************************************************************
399 * X11DRV_GetScreenSaveActive
401 * Returns the active status of the screen saver
403 BOOL X11DRV_GetScreenSaveActive(void)
406 TSXGetScreenSaver(display, &timeout, &temp, &temp, &temp);
410 /***********************************************************************
411 * X11DRV_SetScreenSaveActive
413 * Activate/Deactivate the screen saver
415 void X11DRV_SetScreenSaveActive(BOOL bActivate)
418 TSXActivateScreenSaver(display);
420 TSXResetScreenSaver(display);
423 /***********************************************************************
424 * X11DRV_GetScreenSaveTimeout
426 * Return the screen saver timeout
428 int X11DRV_GetScreenSaveTimeout(void)
431 TSXGetScreenSaver(display, &timeout, &temp, &temp, &temp);
435 /***********************************************************************
436 * X11DRV_SetScreenSaveTimeout
438 * Set the screen saver timeout
440 void X11DRV_SetScreenSaveTimeout(int nTimeout)
442 /* timeout is a 16bit entity (CARD16) in the protocol, so it should
443 * not get over 32767 or it will get negative. */
444 if (nTimeout>32767) nTimeout = 32767;
445 TSXSetScreenSaver(display, nTimeout, 60, DefaultBlanking, DefaultExposures);
448 /***********************************************************************
449 * X11DRV_IsSingleWindow
451 BOOL X11DRV_IsSingleWindow(void)
453 return (root_window != DefaultRootWindow(display));