Yet another hack to try to make this file compile in all
[wine/gsoc_dplay.git] / dlls / x11drv / x11drv_main.c
blob5e4df9d3b7ee0f27683ad0ef2cc620715c6c2a0c
1 /*
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
22 #include "config.h"
24 #include <fcntl.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #ifdef HAVE_SYS_TIME_H
29 # include <sys/time.h>
30 #endif
31 #ifdef HAVE_UNISTD_H
32 # include <unistd.h>
33 #endif
34 #include <X11/cursorfont.h>
35 #include "ts_xlib.h"
36 #ifdef HAVE_XKB
37 #include <X11/XKBlib.h>
38 #endif
40 #include "winbase.h"
41 #include "wine/winbase16.h"
42 #include "winreg.h"
44 #include "gdi.h"
45 #include "user.h"
46 #include "win.h"
47 #include "x11drv.h"
48 #include "xvidmode.h"
49 #include "dga2.h"
50 #include "wine/server.h"
51 #include "wine/debug.h"
53 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
55 static CRITICAL_SECTION X11DRV_CritSection = CRITICAL_SECTION_INIT("X11DRV_CritSection");
57 Screen *screen;
58 Visual *visual;
59 unsigned int screen_width;
60 unsigned int screen_height;
61 unsigned int screen_depth;
62 Window root_window;
63 DWORD desktop_tid = 0;
64 int dxgrab, usedga, usexvidmode;
65 int use_xkb = 1;
66 int use_take_focus = 1;
67 int managed_mode = 1;
68 int client_side_with_core = 1;
69 int client_side_with_render = 1;
70 int client_side_antialias_with_core = 1;
71 int client_side_antialias_with_render = 1;
73 unsigned int X11DRV_server_startticks;
75 static BOOL synchronous; /* run in synchronous mode? */
76 static BOOL desktop_dbl_buf;
77 static char *desktop_geometry;
78 static XVisualInfo *desktop_vi;
80 static x11drv_error_callback err_callback; /* current callback for error */
81 static Display *err_callback_display; /* display callback is set for */
82 static void *err_callback_arg; /* error callback argument */
83 static int err_callback_result; /* error callback result */
84 static int (*old_error_handler)( Display *, XErrorEvent * );
86 #define IS_OPTION_TRUE(ch) \
87 ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
88 #define IS_OPTION_FALSE(ch) \
89 ((ch) == 'n' || (ch) == 'N' || (ch) == 'f' || (ch) == 'F' || (ch) == '0')
91 /***********************************************************************
92 * X11DRV_expect_error
94 * Setup a callback function that will be called on an X error. The
95 * callback must return non-zero if the error is the one it expected.
96 * This function acquires the x11 lock; X11DRV_check_error must be
97 * called in all cases to release it.
99 void X11DRV_expect_error( Display *display, x11drv_error_callback callback, void *arg )
101 wine_tsx11_lock();
102 XSync( display, False );
103 err_callback = callback;
104 err_callback_display = display;
105 err_callback_arg = arg;
106 err_callback_result = 0;
110 /***********************************************************************
111 * X11DRV_check_error
113 * Check if an expected X11 error occurred; return non-zero if yes.
114 * Also release the x11 lock obtained in X11DRV_expect_error.
116 int X11DRV_check_error(void)
118 int ret;
119 XSync( err_callback_display, False );
120 err_callback = NULL;
121 ret = err_callback_result;
122 wine_tsx11_unlock();
123 return ret;
127 /***********************************************************************
128 * error_handler
130 static int error_handler( Display *display, XErrorEvent *error_evt )
132 if (err_callback && display == err_callback_display)
134 if ((err_callback_result = err_callback( display, error_evt, err_callback_arg )))
136 TRACE( "got expected error\n" );
137 return 0;
140 if (synchronous) DebugBreak(); /* force an entry in the debugger */
141 old_error_handler( display, error_evt );
142 return 0;
145 /***********************************************************************
146 * wine_tsx11_lock (X11DRV.@)
148 void wine_tsx11_lock(void)
150 EnterCriticalSection( &X11DRV_CritSection );
153 /***********************************************************************
154 * wine_tsx11_unlock (X11DRV.@)
156 void wine_tsx11_unlock(void)
158 LeaveCriticalSection( &X11DRV_CritSection );
161 /***********************************************************************
162 * get_server_startup
164 * Get the server startup time
165 * Won't be exact, but should be sufficient
167 static void get_server_startup(void)
169 struct timeval t;
170 gettimeofday( &t, NULL );
171 X11DRV_server_startticks = ((t.tv_sec * 1000) + (t.tv_usec / 1000)) - GetTickCount();
175 /***********************************************************************
176 * get_config_key
178 * Get a config key from either the app-specific or the default config
180 inline static DWORD get_config_key( HKEY defkey, HKEY appkey, const char *name,
181 char *buffer, DWORD size )
183 if (appkey && !RegQueryValueExA( appkey, name, 0, NULL, buffer, &size )) return 0;
184 return RegQueryValueExA( defkey, name, 0, NULL, buffer, &size );
188 /***********************************************************************
189 * setup_options
191 * Setup the x11drv options.
193 static void setup_options(void)
195 char buffer[MAX_PATH+16];
196 HKEY hkey, appkey = 0;
197 DWORD count;
199 if (RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\x11drv", 0, NULL,
200 REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, NULL ))
202 ERR("Cannot create config registry key\n" );
203 ExitProcess(1);
206 /* open the app-specific key */
208 if (GetModuleFileNameA( 0, buffer, MAX_PATH ))
210 HKEY tmpkey;
211 char *p, *appname = buffer;
212 if ((p = strrchr( appname, '/' ))) appname = p + 1;
213 if ((p = strrchr( appname, '\\' ))) appname = p + 1;
214 strcat( appname, "\\x11drv" );
215 if (!RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\AppDefaults", &tmpkey ))
217 if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
218 RegCloseKey( tmpkey );
222 /* get the display name */
224 strcpy( buffer, "DISPLAY=" );
225 count = sizeof(buffer) - 8;
226 if (!RegQueryValueExA( hkey, "display", 0, NULL, buffer + 8, &count ))
228 const char *display_name = getenv( "DISPLAY" );
229 if (display_name && strcmp( buffer, display_name ))
230 MESSAGE( "x11drv: Warning: $DISPLAY variable ignored, using '%s' specified in config file\n",
231 buffer + 8 );
232 putenv( strdup(buffer) );
235 if (!get_config_key( hkey, appkey, "Desktop", buffer, sizeof(buffer) ))
237 /* Imperfect validation: If Desktop=N, then we don't turn on
238 ** the --desktop option. We should really validate for a correct
239 ** sizing entry */
240 if (!IS_OPTION_FALSE(buffer[0])) desktop_geometry = strdup(buffer);
243 if (!get_config_key( hkey, appkey, "Managed", buffer, sizeof(buffer) ))
244 managed_mode = IS_OPTION_TRUE( buffer[0] );
246 if (!get_config_key( hkey, appkey, "DXGrab", buffer, sizeof(buffer) ))
247 dxgrab = IS_OPTION_TRUE( buffer[0] );
249 if (!get_config_key( hkey, appkey, "UseDGA", buffer, sizeof(buffer) ))
250 usedga = IS_OPTION_TRUE( buffer[0] );
252 if (!get_config_key( hkey, appkey, "UseXVidMode", buffer, sizeof(buffer) ))
253 usexvidmode = IS_OPTION_TRUE( buffer[0] );
255 if (!get_config_key( hkey, appkey, "UseTakeFocus", buffer, sizeof(buffer) ))
256 use_take_focus = IS_OPTION_TRUE( buffer[0] );
258 screen_depth = 0;
259 if (!get_config_key( hkey, appkey, "ScreenDepth", buffer, sizeof(buffer) ))
260 screen_depth = atoi(buffer);
262 if (!get_config_key( hkey, appkey, "Synchronous", buffer, sizeof(buffer) ))
263 synchronous = IS_OPTION_TRUE( buffer[0] );
265 if (!get_config_key( hkey, appkey, "ClientSideWithCore", buffer, sizeof(buffer) ))
266 client_side_with_core = IS_OPTION_TRUE( buffer[0] );
268 if (!get_config_key( hkey, appkey, "ClientSideWithRender", buffer, sizeof(buffer) ))
269 client_side_with_render = IS_OPTION_TRUE( buffer[0] );
271 if (!get_config_key( hkey, appkey, "ClientSideAntiAliasWithCore", buffer, sizeof(buffer) ))
272 client_side_antialias_with_core = IS_OPTION_TRUE( buffer[0] );
274 if (!get_config_key( hkey, appkey, "ClientSideAntiAliasWithRender", buffer, sizeof(buffer) ))
275 client_side_antialias_with_render = IS_OPTION_TRUE( buffer[0] );
277 if (!get_config_key( hkey, appkey, "DesktopDoubleBuffered", buffer, sizeof(buffer) ))
278 desktop_dbl_buf = IS_OPTION_TRUE( buffer[0] );
280 if (appkey) RegCloseKey( appkey );
281 RegCloseKey( hkey );
285 /***********************************************************************
286 * X11DRV process initialisation routine
288 static void process_attach(void)
290 Display *display;
292 get_server_startup();
293 setup_options();
295 /* Open display */
297 if (!(display = TSXOpenDisplay( NULL )))
299 MESSAGE( "x11drv: Can't open display: %s\n", XDisplayName(NULL) );
300 ExitProcess(1);
302 fcntl( ConnectionNumber(display), F_SETFD, 1 ); /* set close on exec flag */
303 screen = DefaultScreenOfDisplay( display );
304 visual = DefaultVisual( display, DefaultScreen(display) );
305 root_window = DefaultRootWindow( display );
306 old_error_handler = XSetErrorHandler( error_handler );
308 /* Initialize screen depth */
310 if (screen_depth) /* depth specified */
312 int depth_count, i;
313 int *depth_list = TSXListDepths(display, DefaultScreen(display), &depth_count);
314 for (i = 0; i < depth_count; i++)
315 if (depth_list[i] == screen_depth) break;
316 TSXFree( depth_list );
317 if (i >= depth_count)
319 MESSAGE( "x11drv: Depth %d not supported on this screen.\n", screen_depth );
320 ExitProcess(1);
323 else screen_depth = DefaultDepthOfScreen( screen );
325 /* check for Xkb extension */
326 #ifdef HAVE_XKB
327 if (use_xkb)
329 int xkb_opcode, xkb_event, xkb_error;
330 int xkb_major = XkbMajorVersion, xkb_minor = XkbMinorVersion;
332 use_xkb = XkbQueryExtension(display, &xkb_opcode, &xkb_event, &xkb_error,
333 &xkb_major, &xkb_minor);
334 if (use_xkb) /* we have XKB, approximate Windows behaviour */
335 XkbSetDetectableAutoRepeat(display, True, NULL);
337 #endif
339 /* Initialize OpenGL */
340 X11DRV_OpenGL_Init(display);
342 /* If OpenGL is available, change the default visual, etc as necessary */
343 if (desktop_dbl_buf && (desktop_vi = X11DRV_setup_opengl_visual( display )))
345 visual = desktop_vi->visual;
346 screen = ScreenOfDisplay(display, desktop_vi->screen);
347 screen_depth = desktop_vi->depth;
350 if (synchronous) XSynchronize( display, True );
352 screen_width = WidthOfScreen( screen );
353 screen_height = HeightOfScreen( screen );
355 if (desktop_geometry)
356 root_window = X11DRV_create_desktop( desktop_vi, desktop_geometry );
358 /* initialize GDI */
359 if(!X11DRV_GDI_Initialize( display ))
361 ERR( "Couldn't Initialize GDI.\n" );
362 ExitProcess(1);
365 #ifdef HAVE_LIBXXF86VM
366 /* initialize XVidMode */
367 X11DRV_XF86VM_Init();
368 #endif
369 #ifdef HAVE_LIBXXF86DGA2
370 /* initialize DGA2 */
371 X11DRV_XF86DGA2_Init();
372 #endif
374 /* load display.dll */
375 LoadLibrary16( "display" );
379 /***********************************************************************
380 * X11DRV thread termination routine
382 static void thread_detach(void)
384 struct x11drv_thread_data *data = NtCurrentTeb()->driver_data;
386 if (data)
388 CloseHandle( data->display_fd );
389 wine_tsx11_lock();
390 XCloseDisplay( data->display );
391 /* if (data->xim) XCloseIM( data->xim ); */ /* crashes Xlib */
392 wine_tsx11_unlock();
393 HeapFree( GetProcessHeap(), 0, data );
398 /***********************************************************************
399 * X11DRV process termination routine
401 static void process_detach(void)
403 #ifdef HAVE_LIBXXF86DGA2
404 /* cleanup DGA2 */
405 X11DRV_XF86DGA2_Cleanup();
406 #endif
407 #ifdef HAVE_LIBXXF86VM
408 /* cleanup XVidMode */
409 X11DRV_XF86VM_Cleanup();
410 #endif
411 if(using_client_side_fonts)
412 X11DRV_XRender_Finalize();
414 /* FIXME: should detach all threads */
415 thread_detach();
417 /* cleanup GDI */
418 X11DRV_GDI_Finalize();
420 DeleteCriticalSection( &X11DRV_CritSection );
424 /***********************************************************************
425 * X11DRV thread initialisation routine
427 struct x11drv_thread_data *x11drv_init_thread_data(void)
429 struct x11drv_thread_data *data;
431 if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) )))
433 ERR( "could not create data\n" );
434 ExitProcess(1);
436 wine_tsx11_lock();
437 if (!(data->display = XOpenDisplay(NULL)))
439 wine_tsx11_unlock();
440 MESSAGE( "x11drv: Can't open display: %s\n", XDisplayName(NULL) );
441 ExitProcess(1);
443 fcntl( ConnectionNumber(data->display), F_SETFD, 1 ); /* set close on exec flag */
445 if (!(data->xim = XOpenIM( data->display, NULL, NULL, NULL )))
446 WARN("Can't open input method\n");
448 #ifdef HAVE_XKB
449 if (use_xkb) XkbSetDetectableAutoRepeat( data->display, True, NULL );
450 #endif
452 if (synchronous) XSynchronize( data->display, True );
453 wine_tsx11_unlock();
454 if (wine_server_fd_to_handle( ConnectionNumber(data->display), GENERIC_READ | SYNCHRONIZE,
455 FALSE, &data->display_fd ))
457 MESSAGE( "x11drv: Can't allocate handle for display fd\n" );
458 ExitProcess(1);
460 data->process_event_count = 0;
461 data->cursor = None;
462 data->cursor_window = None;
463 data->last_focus = 0;
464 NtCurrentTeb()->driver_data = data;
465 if (desktop_tid) AttachThreadInput( GetCurrentThreadId(), desktop_tid, TRUE );
466 return data;
470 /***********************************************************************
471 * X11DRV initialisation routine
473 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
475 switch(reason)
477 case DLL_PROCESS_ATTACH:
478 process_attach();
479 break;
480 case DLL_THREAD_DETACH:
481 thread_detach();
482 break;
483 case DLL_PROCESS_DETACH:
484 process_detach();
485 break;
487 return TRUE;
490 /***********************************************************************
491 * GetScreenSaveActive (X11DRV.@)
493 * Returns the active status of the screen saver
495 BOOL X11DRV_GetScreenSaveActive(void)
497 int timeout, temp;
498 TSXGetScreenSaver(gdi_display, &timeout, &temp, &temp, &temp);
499 return timeout != 0;
502 /***********************************************************************
503 * SetScreenSaveActive (X11DRV.@)
505 * Activate/Deactivate the screen saver
507 void X11DRV_SetScreenSaveActive(BOOL bActivate)
509 int timeout, interval, prefer_blanking, allow_exposures;
510 static int last_timeout = 15 * 60;
512 TSXGetScreenSaver(gdi_display, &timeout, &interval, &prefer_blanking,
513 &allow_exposures);
514 if (timeout) last_timeout = timeout;
516 timeout = bActivate ? last_timeout : 0;
517 TSXSetScreenSaver(gdi_display, timeout, interval, prefer_blanking,
518 allow_exposures);