Make UseXrandr default to true in the no config file case.
[wine/wine-kai.git] / dlls / x11drv / x11drv_main.c
blob86cf3eedd6495ae5548bb7993226d453369a2cdf
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 = 0;
86 int usedga = 0;
87 int usexvidmode = 0;
88 int usexrandr = 1;
89 int use_xkb = 1;
90 int use_take_focus = 1;
91 int managed_mode = 1;
92 int client_side_with_core = 1;
93 int client_side_with_render = 1;
94 int client_side_antialias_with_core = 1;
95 int client_side_antialias_with_render = 1;
96 int using_wine_desktop = 0;
98 unsigned int X11DRV_server_startticks;
100 static BOOL synchronous; /* run in synchronous mode? */
101 static BOOL desktop_dbl_buf;
102 static char *desktop_geometry;
103 static XVisualInfo *desktop_vi;
105 static x11drv_error_callback err_callback; /* current callback for error */
106 static Display *err_callback_display; /* display callback is set for */
107 static void *err_callback_arg; /* error callback argument */
108 static int err_callback_result; /* error callback result */
109 static unsigned long err_serial; /* serial number of first request */
110 static int (*old_error_handler)( Display *, XErrorEvent * );
111 static int use_xim = 1;
112 static char input_style[20];
114 #define IS_OPTION_TRUE(ch) \
115 ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
116 #define IS_OPTION_FALSE(ch) \
117 ((ch) == 'n' || (ch) == 'N' || (ch) == 'f' || (ch) == 'F' || (ch) == '0')
119 /***********************************************************************
120 * ignore_error
122 * Check if the X error is one we can ignore.
124 static inline BOOL ignore_error( Display *display, XErrorEvent *event )
126 if (event->request_code == X_SetInputFocus && event->error_code == BadMatch) return TRUE;
127 return FALSE;
131 /***********************************************************************
132 * X11DRV_expect_error
134 * Setup a callback function that will be called on an X error. The
135 * callback must return non-zero if the error is the one it expected.
136 * This function acquires the x11 lock; X11DRV_check_error must be
137 * called in all cases to release it.
139 void X11DRV_expect_error( Display *display, x11drv_error_callback callback, void *arg )
141 wine_tsx11_lock();
142 err_callback = callback;
143 err_callback_display = display;
144 err_callback_arg = arg;
145 err_callback_result = 0;
146 err_serial = NextRequest(display);
150 /***********************************************************************
151 * X11DRV_check_error
153 * Check if an expected X11 error occurred; return non-zero if yes.
154 * Also release the x11 lock obtained in X11DRV_expect_error.
155 * The caller is responsible for calling XSync first if necessary.
157 int X11DRV_check_error(void)
159 int ret;
160 err_callback = NULL;
161 ret = err_callback_result;
162 wine_tsx11_unlock();
163 return ret;
167 /***********************************************************************
168 * error_handler
170 static int error_handler( Display *display, XErrorEvent *error_evt )
172 if (err_callback && display == err_callback_display &&
173 (long)(error_evt->serial - err_serial) >= 0)
175 if ((err_callback_result = err_callback( display, error_evt, err_callback_arg )))
177 TRACE( "got expected error %d req %d\n",
178 error_evt->error_code, error_evt->request_code );
179 return 0;
182 if (ignore_error( display, error_evt ))
184 TRACE( "got ignored error %d req %d\n",
185 error_evt->error_code, error_evt->request_code );
186 return 0;
188 if (synchronous) DebugBreak(); /* force an entry in the debugger */
189 old_error_handler( display, error_evt );
190 return 0;
193 /***********************************************************************
194 * wine_tsx11_lock (X11DRV.@)
196 void wine_tsx11_lock(void)
198 EnterCriticalSection( &X11DRV_CritSection );
201 /***********************************************************************
202 * wine_tsx11_unlock (X11DRV.@)
204 void wine_tsx11_unlock(void)
206 LeaveCriticalSection( &X11DRV_CritSection );
209 /***********************************************************************
210 * get_server_startup
212 * Get the server startup time
213 * Won't be exact, but should be sufficient
215 static void get_server_startup(void)
217 struct timeval t;
218 gettimeofday( &t, NULL );
219 X11DRV_server_startticks = ((t.tv_sec * 1000) + (t.tv_usec / 1000)) - GetTickCount();
223 /***********************************************************************
224 * get_config_key
226 * Get a config key from either the app-specific or the default config
228 inline static DWORD get_config_key( HKEY defkey, HKEY appkey, const char *name,
229 char *buffer, DWORD size )
231 if (appkey && !RegQueryValueExA( appkey, name, 0, NULL, buffer, &size )) return 0;
232 return RegQueryValueExA( defkey, name, 0, NULL, buffer, &size );
236 /***********************************************************************
237 * setup_options
239 * Setup the x11drv options.
241 static void setup_options(void)
243 char buffer[MAX_PATH+16];
244 HKEY hkey, appkey = 0;
245 DWORD len;
247 if (RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\x11drv", 0, NULL,
248 REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, NULL ))
250 ERR("Cannot create config registry key\n" );
251 ExitProcess(1);
254 /* open the app-specific key */
256 len = (GetModuleFileNameA( 0, buffer, MAX_PATH ));
257 if (len && len < MAX_PATH)
259 HKEY tmpkey;
260 char *p, *appname = buffer;
261 if ((p = strrchr( appname, '/' ))) appname = p + 1;
262 if ((p = strrchr( appname, '\\' ))) appname = p + 1;
263 strcat( appname, "\\x11drv" );
264 if (!RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\AppDefaults", &tmpkey ))
266 if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
267 RegCloseKey( tmpkey );
271 if (!get_config_key( hkey, appkey, "Desktop", buffer, sizeof(buffer) ))
273 /* Imperfect validation: If Desktop=N, then we don't turn on
274 ** the --desktop option. We should really validate for a correct
275 ** sizing entry */
276 if (!IS_OPTION_FALSE(buffer[0])) desktop_geometry = strdup(buffer);
279 if (!get_config_key( hkey, appkey, "Managed", buffer, sizeof(buffer) ))
280 managed_mode = IS_OPTION_TRUE( buffer[0] );
282 if (!get_config_key( hkey, appkey, "DXGrab", buffer, sizeof(buffer) ))
283 dxgrab = IS_OPTION_TRUE( buffer[0] );
285 if (!get_config_key( hkey, appkey, "UseDGA", buffer, sizeof(buffer) ))
286 usedga = IS_OPTION_TRUE( buffer[0] );
288 if (!get_config_key( hkey, appkey, "UseXVidMode", buffer, sizeof(buffer) ))
289 usexvidmode = IS_OPTION_TRUE( buffer[0] );
291 if (!get_config_key( hkey, appkey, "UseXRandR", buffer, sizeof(buffer) ))
292 usexrandr = IS_OPTION_TRUE( buffer[0] );
294 if (!get_config_key( hkey, appkey, "UseTakeFocus", buffer, sizeof(buffer) ))
295 use_take_focus = IS_OPTION_TRUE( buffer[0] );
297 screen_depth = 0;
298 if (!get_config_key( hkey, appkey, "ScreenDepth", buffer, sizeof(buffer) ))
299 screen_depth = atoi(buffer);
301 if (!get_config_key( hkey, appkey, "Synchronous", buffer, sizeof(buffer) ))
302 synchronous = IS_OPTION_TRUE( buffer[0] );
304 if (!get_config_key( hkey, appkey, "ClientSideWithCore", buffer, sizeof(buffer) ))
305 client_side_with_core = IS_OPTION_TRUE( buffer[0] );
307 if (!get_config_key( hkey, appkey, "ClientSideWithRender", buffer, sizeof(buffer) ))
308 client_side_with_render = IS_OPTION_TRUE( buffer[0] );
310 if (!get_config_key( hkey, appkey, "ClientSideAntiAliasWithCore", buffer, sizeof(buffer) ))
311 client_side_antialias_with_core = IS_OPTION_TRUE( buffer[0] );
313 if (!get_config_key( hkey, appkey, "ClientSideAntiAliasWithRender", buffer, sizeof(buffer) ))
314 client_side_antialias_with_render = IS_OPTION_TRUE( buffer[0] );
316 if (!get_config_key( hkey, appkey, "DesktopDoubleBuffered", buffer, sizeof(buffer) ))
317 desktop_dbl_buf = IS_OPTION_TRUE( buffer[0] );
319 if (!get_config_key( hkey, appkey, "UseXIM", buffer, sizeof(buffer) ))
320 use_xim = IS_OPTION_TRUE( buffer[0] );
322 get_config_key( hkey, appkey, "InputStyle", input_style, sizeof(input_style) );
324 if (appkey) RegCloseKey( appkey );
325 RegCloseKey( hkey );
329 /***********************************************************************
330 * X11DRV process initialisation routine
332 static void process_attach(void)
334 Display *display;
336 get_server_startup();
337 setup_options();
339 /* Open display */
341 if (!(display = XOpenDisplay( NULL )))
343 MESSAGE( "x11drv: Can't open display: %s\n", XDisplayName(NULL) );
344 MESSAGE( "Please ensure that your X server is running and that $DISPLAY is set correctly.\n" );
345 ExitProcess(1);
347 fcntl( ConnectionNumber(display), F_SETFD, 1 ); /* set close on exec flag */
348 screen = DefaultScreenOfDisplay( display );
349 visual = DefaultVisual( display, DefaultScreen(display) );
350 root_window = DefaultRootWindow( display );
351 old_error_handler = XSetErrorHandler( error_handler );
353 /* Initialize screen depth */
355 if (screen_depth) /* depth specified */
357 int depth_count, i;
358 int *depth_list = XListDepths(display, DefaultScreen(display), &depth_count);
359 for (i = 0; i < depth_count; i++)
360 if (depth_list[i] == screen_depth) break;
361 XFree( depth_list );
362 if (i >= depth_count)
364 MESSAGE( "x11drv: Depth %d not supported on this screen.\n", screen_depth );
365 ExitProcess(1);
368 else screen_depth = DefaultDepthOfScreen( screen );
370 /* Initialize OpenGL */
371 X11DRV_OpenGL_Init(display);
373 /* If OpenGL is available, change the default visual, etc as necessary */
374 if (desktop_dbl_buf && (desktop_vi = X11DRV_setup_opengl_visual( display )))
376 visual = desktop_vi->visual;
377 screen = ScreenOfDisplay(display, desktop_vi->screen);
378 screen_depth = desktop_vi->depth;
381 if (synchronous) XSynchronize( display, True );
383 screen_width = WidthOfScreen( screen );
384 screen_height = HeightOfScreen( screen );
386 X11DRV_Settings_Init();
388 if (desktop_geometry)
390 root_window = X11DRV_create_desktop( desktop_vi, desktop_geometry );
391 using_wine_desktop = 1;
394 /* initialize GDI */
395 if(!X11DRV_GDI_Initialize( display ))
397 ERR( "Couldn't Initialize GDI.\n" );
398 ExitProcess(1);
401 #ifdef HAVE_LIBXXF86VM
402 /* initialize XVidMode */
403 X11DRV_XF86VM_Init();
404 #endif
405 #ifdef HAVE_LIBXRANDR
406 /* initialize XRandR */
407 X11DRV_XRandR_Init();
408 #endif
409 #ifdef HAVE_LIBXXF86DGA2
410 /* initialize DGA2 */
411 X11DRV_XF86DGA2_Init();
412 #endif
416 /***********************************************************************
417 * X11DRV thread termination routine
419 static void thread_detach(void)
421 struct x11drv_thread_data *data = NtCurrentTeb()->driver_data;
423 if (data)
425 CloseHandle( data->display_fd );
426 wine_tsx11_lock();
427 XCloseDisplay( data->display );
428 /* if (data->xim) XCloseIM( data->xim ); */ /* crashes Xlib */
429 wine_tsx11_unlock();
430 HeapFree( GetProcessHeap(), 0, data );
435 /***********************************************************************
436 * X11DRV process termination routine
438 static void process_detach(void)
440 #ifdef HAVE_LIBXXF86DGA2
441 /* cleanup DGA2 */
442 X11DRV_XF86DGA2_Cleanup();
443 #endif
444 #ifdef HAVE_LIBXXF86VM
445 /* cleanup XVidMode */
446 X11DRV_XF86VM_Cleanup();
447 #endif
448 if(using_client_side_fonts)
449 X11DRV_XRender_Finalize();
451 /* cleanup GDI */
452 X11DRV_GDI_Finalize();
454 DeleteCriticalSection( &X11DRV_CritSection );
458 /***********************************************************************
459 * X11DRV thread initialisation routine
461 struct x11drv_thread_data *x11drv_init_thread_data(void)
463 struct x11drv_thread_data *data;
465 if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) )))
467 ERR( "could not create data\n" );
468 ExitProcess(1);
470 wine_tsx11_lock();
471 if (!(data->display = XOpenDisplay(NULL)))
473 wine_tsx11_unlock();
474 MESSAGE( "x11drv: Can't open display: %s\n", XDisplayName(NULL) );
475 MESSAGE( "Please ensure that your X server is running and that $DISPLAY is set correctly.\n" );
476 ExitProcess(1);
479 fcntl( ConnectionNumber(data->display), F_SETFD, 1 ); /* set close on exec flag */
481 #ifdef HAVE_XKB
482 if (use_xkb)
484 use_xkb = XkbUseExtension( data->display, NULL, NULL );
485 if (use_xkb) XkbSetDetectableAutoRepeat( data->display, True, NULL );
487 #endif
489 if (synchronous) XSynchronize( data->display, True );
490 wine_tsx11_unlock();
492 if (use_xim && !(data->xim = X11DRV_SetupXIM( data->display, input_style )))
493 WARN("Input Method is not available\n");
495 if (wine_server_fd_to_handle( ConnectionNumber(data->display), GENERIC_READ | SYNCHRONIZE,
496 FALSE, &data->display_fd ))
498 MESSAGE( "x11drv: Can't allocate handle for display fd\n" );
499 ExitProcess(1);
501 data->process_event_count = 0;
502 data->cursor = None;
503 data->cursor_window = None;
504 data->last_focus = 0;
505 NtCurrentTeb()->driver_data = data;
506 if (desktop_tid) AttachThreadInput( GetCurrentThreadId(), desktop_tid, TRUE );
507 return data;
511 /***********************************************************************
512 * X11DRV initialisation routine
514 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
516 switch(reason)
518 case DLL_PROCESS_ATTACH:
519 process_attach();
520 break;
521 case DLL_THREAD_DETACH:
522 thread_detach();
523 break;
524 case DLL_PROCESS_DETACH:
525 process_detach();
526 break;
528 return TRUE;
531 /***********************************************************************
532 * GetScreenSaveActive (X11DRV.@)
534 * Returns the active status of the screen saver
536 BOOL X11DRV_GetScreenSaveActive(void)
538 int timeout, temp;
539 wine_tsx11_lock();
540 XGetScreenSaver(gdi_display, &timeout, &temp, &temp, &temp);
541 wine_tsx11_unlock();
542 return timeout != 0;
545 /***********************************************************************
546 * SetScreenSaveActive (X11DRV.@)
548 * Activate/Deactivate the screen saver
550 void X11DRV_SetScreenSaveActive(BOOL bActivate)
552 int timeout, interval, prefer_blanking, allow_exposures;
553 static int last_timeout = 15 * 60;
555 wine_tsx11_lock();
556 XGetScreenSaver(gdi_display, &timeout, &interval, &prefer_blanking,
557 &allow_exposures);
558 if (timeout) last_timeout = timeout;
560 timeout = bActivate ? last_timeout : 0;
561 XSetScreenSaver(gdi_display, timeout, interval, prefer_blanking,
562 allow_exposures);
563 wine_tsx11_unlock();