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"
43 DEFAULT_DEBUG_CHANNEL(x11drv
);
45 static XKeyboardState keyboard_state
;
46 static void (*old_tsx11_lock
)(void);
47 static void (*old_tsx11_unlock
)(void);
49 static CRITICAL_SECTION X11DRV_CritSection
= CRITICAL_SECTION_INIT
;
54 unsigned int screen_width
;
55 unsigned int screen_height
;
56 unsigned int screen_depth
;
59 unsigned int X11DRV_server_startticks
;
61 #ifdef NO_REENTRANT_X11
62 static int* (*old_errno_location
)(void);
63 static int* (*old_h_errno_location
)(void);
65 /***********************************************************************
68 * Get the per-thread errno location.
70 static int *x11_errno_location(void)
72 /* Use static libc errno while running in Xlib. */
73 if (X11DRV_CritSection
.OwningThread
== GetCurrentThreadId()) return perrno
;
74 return old_errno_location();
77 /***********************************************************************
78 * x11_h_errno_location
80 * Get the per-thread h_errno location.
82 static int *x11_h_errno_location(void)
84 /* Use static libc h_errno while running in Xlib. */
85 if (X11DRV_CritSection
.OwningThread
== GetCurrentThreadId()) return ph_errno
;
86 return old_h_errno_location();
88 #endif /* NO_REENTRANT_X11 */
90 /***********************************************************************
93 static int error_handler(Display
*display
, XErrorEvent
*error_evt
)
95 DebugBreak(); /* force an entry in the debugger */
99 /***********************************************************************
102 static void lock_tsx11(void)
104 RtlEnterCriticalSection( &X11DRV_CritSection
);
107 /***********************************************************************
110 static void unlock_tsx11(void)
112 RtlLeaveCriticalSection( &X11DRV_CritSection
);
115 /***********************************************************************
118 * Get the server startup time
119 * Won't be exact, but should be sufficient
121 static void get_server_startup(void)
124 gettimeofday( &t
, NULL
);
125 X11DRV_server_startticks
= ((t
.tv_sec
* 1000) + (t
.tv_usec
/ 1000)) - GetTickCount();
129 /***********************************************************************
132 * Setup the x11drv options.
134 static void setup_options(void)
140 if (RegCreateKeyExA( HKEY_LOCAL_MACHINE
, "Software\\Wine\\Wine\\Config\\x11drv", 0, NULL
,
141 REG_OPTION_VOLATILE
, KEY_ALL_ACCESS
, NULL
, &hkey
, NULL
))
143 ERR("Cannot create config registry key\n" );
147 /* --display option */
149 count
= sizeof(buffer
);
150 if (!RegQueryValueExA( hkey
, "display", 0, &type
, buffer
, &count
))
154 if (strcmp( buffer
, Options
.display
))
155 MESSAGE( "%s: warning: --display option ignored, using '%s'\n", argv0
, buffer
);
157 else if ((Options
.display
= getenv( "DISPLAY" )))
159 if (strcmp( buffer
, Options
.display
))
160 MESSAGE( "%s: warning: $DISPLAY variable ignored, using '%s'\n", argv0
, buffer
);
162 Options
.display
= strdup(buffer
);
166 if (!Options
.display
&& !(Options
.display
= getenv( "DISPLAY" )))
168 MESSAGE( "%s: no display specified\n", argv0
);
171 RegSetValueExA( hkey
, "display", 0, REG_SZ
, Options
.display
, strlen(Options
.display
)+1 );
174 /* check and set --managed and --desktop options in wine config file
175 * if it was not set on command line */
177 if ((!Options
.managed
) && (Options
.desktopGeometry
== NULL
))
179 count
= sizeof(buffer
);
180 if (!RegQueryValueExA( hkey
, "managed", 0, &type
, buffer
, &count
))
181 Options
.managed
= IS_OPTION_TRUE( buffer
[0] );
183 count
= sizeof(buffer
);
184 if (!RegQueryValueExA( hkey
, "Desktop", 0, &type
, buffer
, &count
))
185 /* Imperfect validation: If Desktop=N, then we don't turn on
186 ** the --desktop option. We should really validate for a correct
188 if (! IS_OPTION_FALSE(buffer
[0]))
189 Options
.desktopGeometry
= strdup(buffer
);
193 RegSetValueExA( hkey
, "managed", 0, REG_SZ
, "y", 2 );
195 if (Options
.desktopGeometry
)
196 RegSetValueExA( hkey
, "desktop", 0, REG_SZ
, Options
.desktopGeometry
, strlen(Options
.desktopGeometry
)+1 );
202 /***********************************************************************
205 * Create the desktop window for the --desktop mode.
207 static void create_desktop( const char *geometry
)
209 int x
= 0, y
= 0, flags
;
210 unsigned int width
= 640, height
= 480; /* Default size = 640x480 */
211 char *name
= "Wine desktop";
212 XSizeHints
*size_hints
;
214 XClassHint
*class_hints
;
215 XSetWindowAttributes win_attr
;
216 XTextProperty window_name
;
217 Atom XA_WM_DELETE_WINDOW
;
218 /* Used to create the desktop window with a good visual */
219 XVisualInfo
*vi
= NULL
;
222 int err_base
, evt_base
;
224 /* Get in wine.ini if the desktop window should have a double-buffered visual or not.
225 But first, test if OpenGL is even supported on the display ! */
226 if (glXQueryExtension(display
, &err_base
, &evt_base
) == True
) {
227 dblbuf_visual
= PROFILE_GetWineIniBool( "x11drv", "DesktopDoubleBuffered", 0 );
229 int dblBuf
[]={GLX_RGBA
,GLX_DEPTH_SIZE
,16,GLX_DOUBLEBUFFER
,None
};
232 vi
= glXChooseVisual(display
, DefaultScreen(display
), dblBuf
);
233 win_attr
.colormap
= XCreateColormap(display
, RootWindow(display
,vi
->screen
),
234 vi
->visual
, AllocNone
);
238 #endif /* HAVE_OPENGL */
240 flags
= TSXParseGeometry( geometry
, &x
, &y
, &width
, &height
);
241 screen_width
= width
;
242 screen_height
= height
;
245 win_attr
.background_pixel
= BlackPixel(display
, 0);
246 win_attr
.event_mask
= ExposureMask
| KeyPressMask
| KeyReleaseMask
|
247 PointerMotionMask
| ButtonPressMask
|
248 ButtonReleaseMask
| EnterWindowMask
;
249 win_attr
.cursor
= TSXCreateFontCursor( display
, XC_top_left_arrow
);
253 screen
= ScreenOfDisplay(display
, vi
->screen
);
254 screen_depth
= vi
->depth
;
256 root_window
= TSXCreateWindow( display
,
257 (vi
== NULL
? DefaultRootWindow(display
) : RootWindow(display
, vi
->screen
)),
258 x
, y
, width
, height
, 0,
259 (vi
== NULL
? CopyFromParent
: vi
->depth
),
261 (vi
== NULL
? CopyFromParent
: vi
->visual
),
262 CWBackPixel
| CWEventMask
| CWCursor
| (vi
== NULL
? 0 : CWColormap
),
265 /* Set window manager properties */
266 size_hints
= TSXAllocSizeHints();
267 wm_hints
= TSXAllocWMHints();
268 class_hints
= TSXAllocClassHint();
269 if (!size_hints
|| !wm_hints
|| !class_hints
)
271 MESSAGE("Not enough memory for window manager hints.\n" );
274 size_hints
->min_width
= size_hints
->max_width
= width
;
275 size_hints
->min_height
= size_hints
->max_height
= height
;
276 size_hints
->flags
= PMinSize
| PMaxSize
;
277 if (flags
& (XValue
| YValue
)) size_hints
->flags
|= USPosition
;
278 if (flags
& (WidthValue
| HeightValue
)) size_hints
->flags
|= USSize
;
279 else size_hints
->flags
|= PSize
;
281 wm_hints
->flags
= InputHint
| StateHint
;
282 wm_hints
->input
= True
;
283 wm_hints
->initial_state
= NormalState
;
284 class_hints
->res_name
= (char *)argv0
;
285 class_hints
->res_class
= "Wine";
287 TSXStringListToTextProperty( &name
, 1, &window_name
);
288 TSXSetWMProperties( display
, root_window
, &window_name
, &window_name
,
289 NULL
, 0, size_hints
, wm_hints
, class_hints
);
290 XA_WM_DELETE_WINDOW
= TSXInternAtom( display
, "WM_DELETE_WINDOW", False
);
291 TSXSetWMProtocols( display
, root_window
, &XA_WM_DELETE_WINDOW
, 1 );
292 TSXFree( size_hints
);
294 TSXFree( class_hints
);
297 TSXMapWindow( display
, root_window
);
301 /***********************************************************************
302 * X11DRV process initialisation routine
304 static void process_attach(void)
306 WND_Driver
= &X11DRV_WND_Driver
;
308 get_server_startup();
311 /* setup TSX11 locking */
312 #ifdef NO_REENTRANT_X11
313 old_errno_location
= (void *)InterlockedExchange( (PLONG
)&wine_errno_location
,
314 (LONG
)x11_errno_location
);
315 old_h_errno_location
= (void *)InterlockedExchange( (PLONG
)&wine_h_errno_location
,
316 (LONG
)x11_h_errno_location
);
317 #endif /* NO_REENTRANT_X11 */
318 old_tsx11_lock
= wine_tsx11_lock
;
319 old_tsx11_unlock
= wine_tsx11_unlock
;
320 wine_tsx11_lock
= lock_tsx11
;
321 wine_tsx11_unlock
= unlock_tsx11
;
325 if (!(display
= TSXOpenDisplay( Options
.display
)))
327 MESSAGE( "%s: Can't open display: %s\n", argv0
, Options
.display
);
330 fcntl( ConnectionNumber(display
), F_SETFD
, 1 ); /* set close on exec flag */
331 screen
= DefaultScreenOfDisplay( display
);
332 visual
= DefaultVisual( display
, DefaultScreen(display
) );
333 root_window
= DefaultRootWindow( display
);
335 /* Initialize screen depth */
337 screen_depth
= PROFILE_GetWineIniInt( "x11drv", "ScreenDepth", 0 );
338 if (screen_depth
) /* depth specified */
341 int *depth_list
= TSXListDepths(display
, DefaultScreen(display
), &depth_count
);
342 for (i
= 0; i
< depth_count
; i
++)
343 if (depth_list
[i
] == screen_depth
) break;
344 TSXFree( depth_list
);
345 if (i
>= depth_count
)
347 MESSAGE( "%s: Depth %d not supported on this screen.\n", argv0
, screen_depth
);
351 else screen_depth
= DefaultDepthOfScreen( screen
);
353 /* tell the libX11 that we will do input method handling ourselves
354 * that keep libX11 from doing anything whith dead keys, allowing Wine
355 * to have total control over dead keys, that is this line allows
356 * them to work in Wine, even whith a libX11 including the dead key
357 * patches from Th.Quinot (http://Web.FdN.FR/~tquinot/dead-keys.en.html)
359 TSXOpenIM( display
, NULL
, NULL
, NULL
);
361 if (Options
.synchronous
) XSetErrorHandler( error_handler
);
363 screen_width
= WidthOfScreen( screen
);
364 screen_height
= HeightOfScreen( screen
);
366 if (Options
.desktopGeometry
)
368 Options
.managed
= FALSE
;
369 create_desktop( Options
.desktopGeometry
);
373 if(!X11DRV_GDI_Initialize())
375 MESSAGE( "%s: X11DRV Couldn't Initialize GDI.\n", argv0
);
380 /* save keyboard setup */
381 TSXGetKeyboardControl(display
, &keyboard_state
);
383 /* initialize event handling */
386 #ifdef HAVE_LIBXXF86VM
387 /* initialize XVidMode */
388 X11DRV_XF86VM_Init();
391 /* load display.dll */
392 LoadLibrary16( "display" );
396 /***********************************************************************
397 * X11DRV process termination routine
399 static void process_detach(void)
401 /* restore keyboard setup */
402 XKeyboardControl keyboard_value
;
404 keyboard_value
.key_click_percent
= keyboard_state
.key_click_percent
;
405 keyboard_value
.bell_percent
= keyboard_state
.bell_percent
;
406 keyboard_value
.bell_pitch
= keyboard_state
.bell_pitch
;
407 keyboard_value
.bell_duration
= keyboard_state
.bell_duration
;
408 keyboard_value
.auto_repeat_mode
= keyboard_state
.global_auto_repeat
;
410 XChangeKeyboardControl(display
, KBKeyClickPercent
| KBBellPercent
|
411 KBBellPitch
| KBBellDuration
| KBAutoRepeatMode
, &keyboard_value
);
413 #ifdef HAVE_LIBXXF86VM
414 /* cleanup XVidMode */
415 X11DRV_XF86VM_Cleanup();
419 X11DRV_GDI_Finalize();
421 /* restore TSX11 locking */
422 wine_tsx11_lock
= old_tsx11_lock
;
423 wine_tsx11_unlock
= old_tsx11_unlock
;
424 #ifdef NO_REENTRANT_X11
425 wine_errno_location
= old_errno_location
;
426 wine_h_errno_location
= old_h_errno_location
;
427 #endif /* NO_REENTRANT_X11 */
430 /* close the display */
431 XCloseDisplay( display
);
439 /***********************************************************************
440 * X11DRV initialisation routine
442 BOOL WINAPI
X11DRV_Init( HINSTANCE hinst
, DWORD reason
, LPVOID reserved
)
446 case DLL_PROCESS_ATTACH
:
449 case DLL_PROCESS_DETACH
:
456 /***********************************************************************
457 * X11DRV_GetScreenSaveActive
459 * Returns the active status of the screen saver
461 BOOL
X11DRV_GetScreenSaveActive(void)
464 TSXGetScreenSaver(display
, &timeout
, &temp
, &temp
, &temp
);
468 /***********************************************************************
469 * X11DRV_SetScreenSaveActive
471 * Activate/Deactivate the screen saver
473 void X11DRV_SetScreenSaveActive(BOOL bActivate
)
476 TSXActivateScreenSaver(display
);
478 TSXResetScreenSaver(display
);
481 /***********************************************************************
482 * X11DRV_GetScreenSaveTimeout
484 * Return the screen saver timeout
486 int X11DRV_GetScreenSaveTimeout(void)
489 TSXGetScreenSaver(display
, &timeout
, &temp
, &temp
, &temp
);
493 /***********************************************************************
494 * X11DRV_SetScreenSaveTimeout
496 * Set the screen saver timeout
498 void X11DRV_SetScreenSaveTimeout(int nTimeout
)
500 /* timeout is a 16bit entity (CARD16) in the protocol, so it should
501 * not get over 32767 or it will get negative. */
502 if (nTimeout
>32767) nTimeout
= 32767;
503 TSXSetScreenSaver(display
, nTimeout
, 60, DefaultBlanking
, DefaultExposures
);
506 /***********************************************************************
507 * X11DRV_IsSingleWindow
509 BOOL
X11DRV_IsSingleWindow(void)
511 return (root_window
!= DefaultRootWindow(display
));