Add missing '\n's to debug traces.
[wine/hacks.git] / dlls / x11drv / x11drv_main.c
bloba94f977b8f842d581034b3d3607971a7a05aa03f
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 <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #ifdef HAVE_SYS_TIME_H
30 # include <sys/time.h>
31 #endif
32 #ifdef HAVE_UNISTD_H
33 # include <unistd.h>
34 #endif
35 #include <X11/cursorfont.h>
36 #include <X11/Xlib.h>
37 #ifdef HAVE_XKB
38 #include <X11/XKBlib.h>
39 #endif
41 #define BOOL X_BOOL
42 #define BYTE X_BYTE
43 #define INT8 X_INT8
44 #define INT16 X_INT16
45 #define INT32 X_INT32
46 #include <X11/Xproto.h>
47 #undef BOOL
48 #undef BYTE
49 #undef INT8
50 #undef INT16
51 #undef INT32
53 #include "windef.h"
54 #include "winbase.h"
55 #include "wine/winbase16.h"
56 #include "winreg.h"
58 #include "user.h"
59 #include "win.h"
60 #include "x11drv.h"
61 #include "xvidmode.h"
62 #include "xrandr.h"
63 #include "dga2.h"
64 #include "wine/server.h"
65 #include "wine/debug.h"
67 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
69 static CRITICAL_SECTION X11DRV_CritSection;
70 static CRITICAL_SECTION_DEBUG critsect_debug =
72 0, 0, &X11DRV_CritSection,
73 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
74 0, 0, { 0, (DWORD)(__FILE__ ": X11DRV_CritSection") }
76 static CRITICAL_SECTION X11DRV_CritSection = { &critsect_debug, -1, 0, 0, 0, 0 };
78 Screen *screen;
79 Visual *visual;
80 unsigned int screen_width;
81 unsigned int screen_height;
82 unsigned int screen_depth;
83 Window root_window;
84 DWORD desktop_tid = 0;
85 int dxgrab, usedga, usexvidmode, usexrandr;
86 int use_xkb = 1;
87 int use_take_focus = 1;
88 int managed_mode = 1;
89 int client_side_with_core = 1;
90 int client_side_with_render = 1;
91 int client_side_antialias_with_core = 1;
92 int client_side_antialias_with_render = 1;
94 unsigned int X11DRV_server_startticks;
96 static BOOL synchronous; /* run in synchronous mode? */
97 static BOOL desktop_dbl_buf;
98 static char *desktop_geometry;
99 static XVisualInfo *desktop_vi;
101 static x11drv_error_callback err_callback; /* current callback for error */
102 static Display *err_callback_display; /* display callback is set for */
103 static void *err_callback_arg; /* error callback argument */
104 static int err_callback_result; /* error callback result */
105 static unsigned long err_serial; /* serial number of first request */
106 static int (*old_error_handler)( Display *, XErrorEvent * );
107 static int use_xim = 1;
108 static char input_style[20];
110 #define IS_OPTION_TRUE(ch) \
111 ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
112 #define IS_OPTION_FALSE(ch) \
113 ((ch) == 'n' || (ch) == 'N' || (ch) == 'f' || (ch) == 'F' || (ch) == '0')
115 /***********************************************************************
116 * ignore_error
118 * Check if the X error is one we can ignore.
120 static inline BOOL ignore_error( Display *display, XErrorEvent *event )
122 if (event->request_code == X_SetInputFocus && event->error_code == BadMatch) return TRUE;
123 return FALSE;
127 /***********************************************************************
128 * X11DRV_expect_error
130 * Setup a callback function that will be called on an X error. The
131 * callback must return non-zero if the error is the one it expected.
132 * This function acquires the x11 lock; X11DRV_check_error must be
133 * called in all cases to release it.
135 void X11DRV_expect_error( Display *display, x11drv_error_callback callback, void *arg )
137 wine_tsx11_lock();
138 err_callback = callback;
139 err_callback_display = display;
140 err_callback_arg = arg;
141 err_callback_result = 0;
142 err_serial = NextRequest(display);
146 /***********************************************************************
147 * X11DRV_check_error
149 * Check if an expected X11 error occurred; return non-zero if yes.
150 * Also release the x11 lock obtained in X11DRV_expect_error.
151 * The caller is responsible for calling XSync first if necessary.
153 int X11DRV_check_error(void)
155 int ret;
156 err_callback = NULL;
157 ret = err_callback_result;
158 wine_tsx11_unlock();
159 return ret;
163 /***********************************************************************
164 * error_handler
166 static int error_handler( Display *display, XErrorEvent *error_evt )
168 if (err_callback && display == err_callback_display &&
169 (long)(error_evt->serial - err_serial) >= 0)
171 if ((err_callback_result = err_callback( display, error_evt, err_callback_arg )))
173 TRACE( "got expected error %d req %d\n",
174 error_evt->error_code, error_evt->request_code );
175 return 0;
178 if (ignore_error( display, error_evt ))
180 TRACE( "got ignored error %d req %d\n",
181 error_evt->error_code, error_evt->request_code );
182 return 0;
184 if (synchronous) DebugBreak(); /* force an entry in the debugger */
185 old_error_handler( display, error_evt );
186 return 0;
189 /***********************************************************************
190 * wine_tsx11_lock (X11DRV.@)
192 void wine_tsx11_lock(void)
194 EnterCriticalSection( &X11DRV_CritSection );
197 /***********************************************************************
198 * wine_tsx11_unlock (X11DRV.@)
200 void wine_tsx11_unlock(void)
202 LeaveCriticalSection( &X11DRV_CritSection );
205 /***********************************************************************
206 * get_server_startup
208 * Get the server startup time
209 * Won't be exact, but should be sufficient
211 static void get_server_startup(void)
213 struct timeval t;
214 gettimeofday( &t, NULL );
215 X11DRV_server_startticks = ((t.tv_sec * 1000) + (t.tv_usec / 1000)) - GetTickCount();
219 /***********************************************************************
220 * get_config_key
222 * Get a config key from either the app-specific or the default config
224 inline static DWORD get_config_key( HKEY defkey, HKEY appkey, const char *name,
225 char *buffer, DWORD size )
227 if (appkey && !RegQueryValueExA( appkey, name, 0, NULL, buffer, &size )) return 0;
228 return RegQueryValueExA( defkey, name, 0, NULL, buffer, &size );
232 /***********************************************************************
233 * setup_options
235 * Setup the x11drv options.
237 static void setup_options(void)
239 char buffer[MAX_PATH+16];
240 HKEY hkey, appkey = 0;
242 if (RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\x11drv", 0, NULL,
243 REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, NULL ))
245 ERR("Cannot create config registry key\n" );
246 ExitProcess(1);
249 /* open the app-specific key */
251 if (GetModuleFileNameA( 0, buffer, MAX_PATH ))
253 HKEY tmpkey;
254 char *p, *appname = buffer;
255 if ((p = strrchr( appname, '/' ))) appname = p + 1;
256 if ((p = strrchr( appname, '\\' ))) appname = p + 1;
257 strcat( appname, "\\x11drv" );
258 if (!RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\AppDefaults", &tmpkey ))
260 if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
261 RegCloseKey( tmpkey );
265 if (!get_config_key( hkey, appkey, "Desktop", buffer, sizeof(buffer) ))
267 /* Imperfect validation: If Desktop=N, then we don't turn on
268 ** the --desktop option. We should really validate for a correct
269 ** sizing entry */
270 if (!IS_OPTION_FALSE(buffer[0])) desktop_geometry = strdup(buffer);
273 if (!get_config_key( hkey, appkey, "Managed", buffer, sizeof(buffer) ))
274 managed_mode = IS_OPTION_TRUE( buffer[0] );
276 if (!get_config_key( hkey, appkey, "DXGrab", buffer, sizeof(buffer) ))
277 dxgrab = IS_OPTION_TRUE( buffer[0] );
279 if (!get_config_key( hkey, appkey, "UseDGA", buffer, sizeof(buffer) ))
280 usedga = IS_OPTION_TRUE( buffer[0] );
282 if (!get_config_key( hkey, appkey, "UseXVidMode", buffer, sizeof(buffer) ))
283 usexvidmode = IS_OPTION_TRUE( buffer[0] );
285 if (!get_config_key( hkey, appkey, "UseXRandR", buffer, sizeof(buffer) ))
286 usexrandr = IS_OPTION_TRUE( buffer[0] );
288 if (!get_config_key( hkey, appkey, "UseTakeFocus", buffer, sizeof(buffer) ))
289 use_take_focus = IS_OPTION_TRUE( buffer[0] );
291 screen_depth = 0;
292 if (!get_config_key( hkey, appkey, "ScreenDepth", buffer, sizeof(buffer) ))
293 screen_depth = atoi(buffer);
295 if (!get_config_key( hkey, appkey, "Synchronous", buffer, sizeof(buffer) ))
296 synchronous = IS_OPTION_TRUE( buffer[0] );
298 if (!get_config_key( hkey, appkey, "ClientSideWithCore", buffer, sizeof(buffer) ))
299 client_side_with_core = IS_OPTION_TRUE( buffer[0] );
301 if (!get_config_key( hkey, appkey, "ClientSideWithRender", buffer, sizeof(buffer) ))
302 client_side_with_render = IS_OPTION_TRUE( buffer[0] );
304 if (!get_config_key( hkey, appkey, "ClientSideAntiAliasWithCore", buffer, sizeof(buffer) ))
305 client_side_antialias_with_core = IS_OPTION_TRUE( buffer[0] );
307 if (!get_config_key( hkey, appkey, "ClientSideAntiAliasWithRender", buffer, sizeof(buffer) ))
308 client_side_antialias_with_render = IS_OPTION_TRUE( buffer[0] );
310 if (!get_config_key( hkey, appkey, "DesktopDoubleBuffered", buffer, sizeof(buffer) ))
311 desktop_dbl_buf = IS_OPTION_TRUE( buffer[0] );
313 if (!get_config_key( hkey, appkey, "UseXIM", buffer, sizeof(buffer) ))
314 use_xim = IS_OPTION_TRUE( buffer[0] );
316 get_config_key( hkey, appkey, "InputStyle", input_style, sizeof(input_style) );
318 if (appkey) RegCloseKey( appkey );
319 RegCloseKey( hkey );
323 /***********************************************************************
324 * X11DRV process initialisation routine
326 static void process_attach(void)
328 Display *display;
330 get_server_startup();
331 setup_options();
333 /* Open display */
335 if (!(display = XOpenDisplay( NULL )))
337 MESSAGE( "x11drv: Can't open display: %s\n", XDisplayName(NULL) );
338 ExitProcess(1);
340 fcntl( ConnectionNumber(display), F_SETFD, 1 ); /* set close on exec flag */
341 screen = DefaultScreenOfDisplay( display );
342 visual = DefaultVisual( display, DefaultScreen(display) );
343 root_window = DefaultRootWindow( display );
344 old_error_handler = XSetErrorHandler( error_handler );
346 /* Initialize screen depth */
348 if (screen_depth) /* depth specified */
350 int depth_count, i;
351 int *depth_list = XListDepths(display, DefaultScreen(display), &depth_count);
352 for (i = 0; i < depth_count; i++)
353 if (depth_list[i] == screen_depth) break;
354 XFree( depth_list );
355 if (i >= depth_count)
357 MESSAGE( "x11drv: Depth %d not supported on this screen.\n", screen_depth );
358 ExitProcess(1);
361 else screen_depth = DefaultDepthOfScreen( screen );
363 /* Initialize OpenGL */
364 X11DRV_OpenGL_Init(display);
366 /* If OpenGL is available, change the default visual, etc as necessary */
367 if (desktop_dbl_buf && (desktop_vi = X11DRV_setup_opengl_visual( display )))
369 visual = desktop_vi->visual;
370 screen = ScreenOfDisplay(display, desktop_vi->screen);
371 screen_depth = desktop_vi->depth;
374 if (synchronous) XSynchronize( display, True );
376 screen_width = WidthOfScreen( screen );
377 screen_height = HeightOfScreen( screen );
379 X11DRV_Settings_Init();
381 if (desktop_geometry)
382 root_window = X11DRV_create_desktop( desktop_vi, desktop_geometry );
384 /* initialize GDI */
385 if(!X11DRV_GDI_Initialize( display ))
387 ERR( "Couldn't Initialize GDI.\n" );
388 ExitProcess(1);
391 #ifdef HAVE_LIBXXF86VM
392 /* initialize XVidMode */
393 X11DRV_XF86VM_Init();
394 #endif
395 #ifdef HAVE_LIBXRANDR
396 /* initialize XRandR */
397 X11DRV_XRandR_Init();
398 #endif
399 #ifdef HAVE_LIBXXF86DGA2
400 /* initialize DGA2 */
401 X11DRV_XF86DGA2_Init();
402 #endif
406 /***********************************************************************
407 * X11DRV thread termination routine
409 static void thread_detach(void)
411 struct x11drv_thread_data *data = NtCurrentTeb()->driver_data;
413 if (data)
415 CloseHandle( data->display_fd );
416 wine_tsx11_lock();
417 XCloseDisplay( data->display );
418 /* if (data->xim) XCloseIM( data->xim ); */ /* crashes Xlib */
419 wine_tsx11_unlock();
420 HeapFree( GetProcessHeap(), 0, data );
425 /***********************************************************************
426 * X11DRV process termination routine
428 static void process_detach(void)
430 #ifdef HAVE_LIBXXF86DGA2
431 /* cleanup DGA2 */
432 X11DRV_XF86DGA2_Cleanup();
433 #endif
434 #ifdef HAVE_LIBXXF86VM
435 /* cleanup XVidMode */
436 X11DRV_XF86VM_Cleanup();
437 #endif
438 if(using_client_side_fonts)
439 X11DRV_XRender_Finalize();
441 /* FIXME: should detach all threads */
442 thread_detach();
444 /* cleanup GDI */
445 X11DRV_GDI_Finalize();
447 DeleteCriticalSection( &X11DRV_CritSection );
451 /***********************************************************************
452 * X11DRV thread initialisation routine
454 struct x11drv_thread_data *x11drv_init_thread_data(void)
456 struct x11drv_thread_data *data;
458 if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) )))
460 ERR( "could not create data\n" );
461 ExitProcess(1);
463 wine_tsx11_lock();
464 if (!(data->display = XOpenDisplay(NULL)))
466 wine_tsx11_unlock();
467 MESSAGE( "x11drv: Can't open display: %s\n", XDisplayName(NULL) );
468 ExitProcess(1);
471 fcntl( ConnectionNumber(data->display), F_SETFD, 1 ); /* set close on exec flag */
473 #ifdef HAVE_XKB
474 if (use_xkb)
476 use_xkb = XkbUseExtension( data->display, NULL, NULL );
477 if (use_xkb) XkbSetDetectableAutoRepeat( data->display, True, NULL );
479 #endif
481 if (synchronous) XSynchronize( data->display, True );
482 wine_tsx11_unlock();
484 if (use_xim && !(data->xim = X11DRV_SetupXIM( data->display, input_style )))
485 WARN("Input Method is not available\n");
487 if (wine_server_fd_to_handle( ConnectionNumber(data->display), GENERIC_READ | SYNCHRONIZE,
488 FALSE, &data->display_fd ))
490 MESSAGE( "x11drv: Can't allocate handle for display fd\n" );
491 ExitProcess(1);
493 data->process_event_count = 0;
494 data->cursor = None;
495 data->cursor_window = None;
496 data->last_focus = 0;
497 NtCurrentTeb()->driver_data = data;
498 if (desktop_tid) AttachThreadInput( GetCurrentThreadId(), desktop_tid, TRUE );
499 return data;
503 /***********************************************************************
504 * X11DRV initialisation routine
506 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
508 switch(reason)
510 case DLL_PROCESS_ATTACH:
511 process_attach();
512 break;
513 case DLL_THREAD_DETACH:
514 thread_detach();
515 break;
516 case DLL_PROCESS_DETACH:
517 process_detach();
518 break;
520 return TRUE;
523 /***********************************************************************
524 * GetScreenSaveActive (X11DRV.@)
526 * Returns the active status of the screen saver
528 BOOL X11DRV_GetScreenSaveActive(void)
530 int timeout, temp;
531 wine_tsx11_lock();
532 XGetScreenSaver(gdi_display, &timeout, &temp, &temp, &temp);
533 wine_tsx11_unlock();
534 return timeout != 0;
537 /***********************************************************************
538 * SetScreenSaveActive (X11DRV.@)
540 * Activate/Deactivate the screen saver
542 void X11DRV_SetScreenSaveActive(BOOL bActivate)
544 int timeout, interval, prefer_blanking, allow_exposures;
545 static int last_timeout = 15 * 60;
547 wine_tsx11_lock();
548 XGetScreenSaver(gdi_display, &timeout, &interval, &prefer_blanking,
549 &allow_exposures);
550 if (timeout) last_timeout = timeout;
552 timeout = bActivate ? last_timeout : 0;
553 XSetScreenSaver(gdi_display, timeout, interval, prefer_blanking,
554 allow_exposures);
555 wine_tsx11_unlock();