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
24 #ifdef NO_REENTRANT_X11
25 /* Get pointers to the static errno and h_errno variables used by Xlib. This
26 must be done before including <errno.h> makes the variables invisible. */
28 static int *perrno = &errno;
30 static int *ph_errno = &h_errno;
31 #endif /* NO_REENTRANT_X11 */
39 #include <X11/cursorfont.h>
45 #include "wine/winbase16.h"
48 #include "wine/debug.h"
59 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
61 static void (*old_tsx11_lock)(void);
62 static void (*old_tsx11_unlock)(void);
64 static CRITICAL_SECTION X11DRV_CritSection = CRITICAL_SECTION_INIT("X11DRV_CritSection");
68 unsigned int screen_width;
69 unsigned int screen_height;
70 unsigned int screen_depth;
72 int dxgrab, usedga, usexvidmode;
74 unsigned int X11DRV_server_startticks;
76 static BOOL synchronous; /* run in synchronous mode? */
77 static char *desktop_geometry;
78 static XVisualInfo *desktop_vi;
80 #define IS_OPTION_TRUE(ch) \
81 ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
82 #define IS_OPTION_FALSE(ch) \
83 ((ch) == 'n' || (ch) == 'N' || (ch) == 'f' || (ch) == 'F' || (ch) == '0')
85 #ifdef NO_REENTRANT_X11
86 static int* (*old_errno_location)(void);
87 static int* (*old_h_errno_location)(void);
89 /***********************************************************************
92 * Get the per-thread errno location.
94 static int *x11_errno_location(void)
96 /* Use static libc errno while running in Xlib. */
97 if (X11DRV_CritSection.OwningThread == GetCurrentThreadId()) return perrno;
98 return old_errno_location();
101 /***********************************************************************
102 * x11_h_errno_location
104 * Get the per-thread h_errno location.
106 static int *x11_h_errno_location(void)
108 /* Use static libc h_errno while running in Xlib. */
109 if (X11DRV_CritSection.OwningThread == GetCurrentThreadId()) return ph_errno;
110 return old_h_errno_location();
112 #endif /* NO_REENTRANT_X11 */
114 /***********************************************************************
117 static int error_handler(Display *display, XErrorEvent *error_evt)
119 DebugBreak(); /* force an entry in the debugger */
123 /***********************************************************************
126 static void lock_tsx11(void)
128 RtlEnterCriticalSection( &X11DRV_CritSection );
131 /***********************************************************************
134 static void unlock_tsx11(void)
136 RtlLeaveCriticalSection( &X11DRV_CritSection );
139 /***********************************************************************
142 * Get the server startup time
143 * Won't be exact, but should be sufficient
145 static void get_server_startup(void)
148 gettimeofday( &t, NULL );
149 X11DRV_server_startticks = ((t.tv_sec * 1000) + (t.tv_usec / 1000)) - GetTickCount();
153 /***********************************************************************
156 * Get a config key from either the app-specific or the default config
158 inline static DWORD get_config_key( HKEY defkey, HKEY appkey, const char *name,
159 char *buffer, DWORD size )
161 if (appkey && !RegQueryValueExA( appkey, name, 0, NULL, buffer, &size )) return 0;
162 return RegQueryValueExA( defkey, name, 0, NULL, buffer, &size );
166 /***********************************************************************
169 * Setup the x11drv options.
171 static void setup_options(void)
173 char buffer[MAX_PATH+16];
174 HKEY hkey, appkey = 0;
177 if (RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\x11drv", 0, NULL,
178 REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, NULL ))
180 ERR("Cannot create config registry key\n" );
184 /* open the app-specific key */
186 if (GetModuleFileName16( GetCurrentTask(), buffer, MAX_PATH ) ||
187 GetModuleFileNameA( 0, buffer, MAX_PATH ))
190 char *p, *appname = buffer;
191 if ((p = strrchr( appname, '/' ))) appname = p + 1;
192 if ((p = strrchr( appname, '\\' ))) appname = p + 1;
193 strcat( appname, "\\x11drv" );
194 if (!RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\AppDefaults", &tmpkey ))
196 if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
197 RegCloseKey( tmpkey );
201 /* get the display name */
203 strcpy( buffer, "DISPLAY=" );
204 count = sizeof(buffer) - 8;
205 if (!RegQueryValueExA( hkey, "display", 0, NULL, buffer + 8, &count ))
207 const char *display_name = getenv( "DISPLAY" );
208 if (display_name && strcmp( buffer, display_name ))
209 MESSAGE( "x11drv: Warning: $DISPLAY variable ignored, using '%s' specified in config file\n",
211 putenv( strdup(buffer) );
214 /* check --managed option in wine config file if it was not set on command line */
216 if (!Options.managed)
218 if (!get_config_key( hkey, appkey, "Managed", buffer, sizeof(buffer) ))
219 Options.managed = IS_OPTION_TRUE( buffer[0] );
222 if (!get_config_key( hkey, appkey, "Desktop", buffer, sizeof(buffer) ))
224 /* Imperfect validation: If Desktop=N, then we don't turn on
225 ** the --desktop option. We should really validate for a correct
227 if (!IS_OPTION_FALSE(buffer[0])) desktop_geometry = strdup(buffer);
230 if (!get_config_key( hkey, appkey, "DXGrab", buffer, sizeof(buffer) ))
231 dxgrab = IS_OPTION_TRUE( buffer[0] );
233 if (!get_config_key( hkey, appkey, "UseDGA", buffer, sizeof(buffer) ))
234 usedga = IS_OPTION_TRUE( buffer[0] );
236 if (!get_config_key( hkey, appkey, "UseXVidMode", buffer, sizeof(buffer) ))
237 usexvidmode = IS_OPTION_TRUE( buffer[0] );
240 if (!get_config_key( hkey, appkey, "ScreenDepth", buffer, sizeof(buffer) ))
241 screen_depth = atoi(buffer);
243 if (!get_config_key( hkey, appkey, "Synchronous", buffer, sizeof(buffer) ))
244 synchronous = IS_OPTION_TRUE( buffer[0] );
246 if (appkey) RegCloseKey( appkey );
251 /***********************************************************************
252 * setup_opengl_visual
254 * Setup the default visual used for OpenGL and Direct3D, and the desktop
255 * window (if it exists). If OpenGL isn't available, the visual is simply
256 * set to the default visual for the display
259 static void setup_opengl_visual( Display *display )
261 int err_base, evt_base;
263 /* In order to support OpenGL or D3D, we require a double-buffered
265 if (glXQueryExtension(display, &err_base, &evt_base) == True) {
266 int dblBuf[]={GLX_RGBA,GLX_DEPTH_SIZE,16,GLX_DOUBLEBUFFER,None};
269 desktop_vi = glXChooseVisual(display, DefaultScreen(display), dblBuf);
273 if (desktop_vi != NULL) {
274 visual = desktop_vi->visual;
275 screen = ScreenOfDisplay(display, desktop_vi->screen);
276 screen_depth = desktop_vi->depth;
279 #endif /* HAVE_OPENGL */
281 /***********************************************************************
282 * X11DRV process initialisation routine
284 static void process_attach(void)
288 get_server_startup();
291 /* setup TSX11 locking */
292 #ifdef NO_REENTRANT_X11
293 old_errno_location = InterlockedExchangePointer( &wine_errno_location,
294 x11_errno_location );
295 old_h_errno_location = InterlockedExchangePointer( &wine_h_errno_location,
296 x11_h_errno_location );
297 #endif /* NO_REENTRANT_X11 */
298 old_tsx11_lock = wine_tsx11_lock;
299 old_tsx11_unlock = wine_tsx11_unlock;
300 wine_tsx11_lock = lock_tsx11;
301 wine_tsx11_unlock = unlock_tsx11;
305 if (!(display = TSXOpenDisplay( NULL )))
307 MESSAGE( "x11drv: Can't open display: %s\n", XDisplayName(NULL) );
310 fcntl( ConnectionNumber(display), F_SETFD, 1 ); /* set close on exec flag */
311 screen = DefaultScreenOfDisplay( display );
312 visual = DefaultVisual( display, DefaultScreen(display) );
313 root_window = DefaultRootWindow( display );
315 /* Initialize screen depth */
317 if (screen_depth) /* depth specified */
320 int *depth_list = TSXListDepths(display, DefaultScreen(display), &depth_count);
321 for (i = 0; i < depth_count; i++)
322 if (depth_list[i] == screen_depth) break;
323 TSXFree( depth_list );
324 if (i >= depth_count)
326 MESSAGE( "x11drv: Depth %d not supported on this screen.\n", screen_depth );
330 else screen_depth = DefaultDepthOfScreen( screen );
332 /* If OpenGL is available, change the default visual, etc as necessary */
334 setup_opengl_visual( display );
335 #endif /* HAVE_OPENGL */
337 /* tell the libX11 that we will do input method handling ourselves
338 * that keep libX11 from doing anything whith dead keys, allowing Wine
339 * to have total control over dead keys, that is this line allows
340 * them to work in Wine, even whith a libX11 including the dead key
341 * patches from Th.Quinot (http://Web.FdN.FR/~tquinot/dead-keys.en.html)
343 TSXOpenIM( display, NULL, NULL, NULL);
347 XSetErrorHandler( error_handler );
348 XSynchronize( display, True );
351 screen_width = WidthOfScreen( screen );
352 screen_height = HeightOfScreen( screen );
354 if (desktop_geometry)
356 Options.managed = FALSE;
357 root_window = X11DRV_create_desktop( desktop_vi, desktop_geometry );
361 if(!X11DRV_GDI_Initialize( display ))
363 ERR( "Couldn't Initialize GDI.\n" );
367 #ifdef HAVE_LIBXXF86VM
368 /* initialize XVidMode */
369 X11DRV_XF86VM_Init();
371 #ifdef HAVE_LIBXXF86DGA2
372 /* initialize DGA2 */
373 X11DRV_XF86DGA2_Init();
377 /*X11DRV_GLX_Init();*/
380 /* load display.dll */
381 LoadLibrary16( "display" );
385 /***********************************************************************
386 * X11DRV thread termination routine
388 static void thread_detach(void)
390 struct x11drv_thread_data *data = NtCurrentTeb()->driver_data;
394 CloseHandle( data->display_fd );
396 XCloseDisplay( data->display );
398 HeapFree( GetProcessHeap(), 0, data );
403 /***********************************************************************
404 * X11DRV process termination routine
406 static void process_detach(void)
410 /*X11DRV_GLX_Cleanup();*/
412 #ifdef HAVE_LIBXXF86DGA2
414 X11DRV_XF86DGA2_Cleanup();
416 #ifdef HAVE_LIBXXF86VM
417 /* cleanup XVidMode */
418 X11DRV_XF86VM_Cleanup();
421 /* FIXME: should detach all threads */
425 X11DRV_GDI_Finalize();
427 /* restore TSX11 locking */
428 wine_tsx11_lock = old_tsx11_lock;
429 wine_tsx11_unlock = old_tsx11_unlock;
430 #ifdef NO_REENTRANT_X11
431 wine_errno_location = old_errno_location;
432 wine_h_errno_location = old_h_errno_location;
433 #endif /* NO_REENTRANT_X11 */
434 RtlDeleteCriticalSection( &X11DRV_CritSection );
438 /***********************************************************************
439 * X11DRV thread initialisation routine
441 struct x11drv_thread_data *x11drv_init_thread_data(void)
443 struct x11drv_thread_data *data;
445 if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) )))
447 ERR( "could not create data\n" );
451 if (!(data->display = XOpenDisplay(NULL)))
454 MESSAGE( "x11drv: Can't open display: %s\n", XDisplayName(NULL) );
457 fcntl( ConnectionNumber(data->display), F_SETFD, 1 ); /* set close on exec flag */
458 if (synchronous) XSynchronize( data->display, True );
460 data->display_fd = FILE_DupUnixHandle( ConnectionNumber(data->display),
461 GENERIC_READ | SYNCHRONIZE, FALSE );
462 data->process_event_count = 0;
463 NtCurrentTeb()->driver_data = data;
468 /***********************************************************************
469 * X11DRV initialisation routine
471 BOOL WINAPI X11DRV_Init( HINSTANCE hinst, DWORD reason, LPVOID reserved )
475 case DLL_PROCESS_ATTACH:
478 case DLL_THREAD_DETACH:
481 case DLL_PROCESS_DETACH:
488 /***********************************************************************
489 * GetScreenSaveActive (X11DRV.@)
491 * Returns the active status of the screen saver
493 BOOL X11DRV_GetScreenSaveActive(void)
496 TSXGetScreenSaver(gdi_display, &timeout, &temp, &temp, &temp);
500 /***********************************************************************
501 * SetScreenSaveActive (X11DRV.@)
503 * Activate/Deactivate the screen saver
505 void X11DRV_SetScreenSaveActive(BOOL bActivate)
507 int timeout, interval, prefer_blanking, allow_exposures;
508 static int last_timeout = 15 * 60;
510 TSXGetScreenSaver(gdi_display, &timeout, &interval, &prefer_blanking,
512 if (timeout) last_timeout = timeout;
514 timeout = bActivate ? last_timeout : 0;
515 TSXSetScreenSaver(gdi_display, timeout, interval, prefer_blanking,