Cast time_t to long for printing.
[wine] / dlls / x11drv / x11drv_main.c
1 /*
2  * X11DRV initialization code
3  *
4  * Copyright 1998 Patrik Stridvall
5  * Copyright 2000 Alexandre Julliard
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "config.h"
23
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 "ts_xlib.h"
37 #ifdef HAVE_XKB
38 #include <X11/XKBlib.h>
39 #endif
40
41 #include "windef.h"
42 #include "winbase.h"
43 #include "wine/winbase16.h"
44 #include "winreg.h"
45
46 #include "gdi.h"
47 #include "user.h"
48 #include "win.h"
49 #include "x11drv.h"
50 #include "xvidmode.h"
51 #include "dga2.h"
52 #include "wine/server.h"
53 #include "wine/debug.h"
54
55 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
56
57 static CRITICAL_SECTION X11DRV_CritSection;
58 static CRITICAL_SECTION_DEBUG critsect_debug =
59 {
60     0, 0, &X11DRV_CritSection,
61     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
62       0, 0, { 0, (DWORD)(__FILE__ ": X11DRV_CritSection") }
63 };
64 static CRITICAL_SECTION X11DRV_CritSection = { &critsect_debug, -1, 0, 0, 0, 0 };
65
66 Screen *screen;
67 Visual *visual;
68 unsigned int screen_width;
69 unsigned int screen_height;
70 unsigned int screen_depth;
71 Window root_window;
72 DWORD desktop_tid = 0;
73 int dxgrab, usedga, usexvidmode;
74 int use_xkb = 1;
75 int use_take_focus = 1;
76 int managed_mode = 1;
77 int client_side_with_core = 1;
78 int client_side_with_render = 1;
79 int client_side_antialias_with_core = 1;
80 int client_side_antialias_with_render = 1;
81
82 unsigned int X11DRV_server_startticks;
83
84 static BOOL synchronous;  /* run in synchronous mode? */
85 static BOOL desktop_dbl_buf;
86 static char *desktop_geometry;
87 static XVisualInfo *desktop_vi;
88
89 static x11drv_error_callback err_callback;   /* current callback for error */
90 static Display *err_callback_display;        /* display callback is set for */
91 static void *err_callback_arg;               /* error callback argument */
92 static int err_callback_result;              /* error callback result */
93 static int (*old_error_handler)( Display *, XErrorEvent * );
94
95 #define IS_OPTION_TRUE(ch) \
96     ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
97 #define IS_OPTION_FALSE(ch) \
98     ((ch) == 'n' || (ch) == 'N' || (ch) == 'f' || (ch) == 'F' || (ch) == '0')
99
100 /***********************************************************************
101  *              X11DRV_expect_error
102  *
103  * Setup a callback function that will be called on an X error.  The
104  * callback must return non-zero if the error is the one it expected.
105  * This function acquires the x11 lock; X11DRV_check_error must be
106  * called in all cases to release it.
107  */
108 void X11DRV_expect_error( Display *display, x11drv_error_callback callback, void *arg )
109 {
110     wine_tsx11_lock();
111     XSync( display, False );
112     err_callback         = callback;
113     err_callback_display = display;
114     err_callback_arg     = arg;
115     err_callback_result  = 0;
116 }
117
118
119 /***********************************************************************
120  *              X11DRV_check_error
121  *
122  * Check if an expected X11 error occurred; return non-zero if yes.
123  * Also release the x11 lock obtained in X11DRV_expect_error.
124  */
125 int X11DRV_check_error(void)
126 {
127     int ret;
128     XSync( err_callback_display, False );
129     err_callback = NULL;
130     ret = err_callback_result;
131     wine_tsx11_unlock();
132     return ret;
133 }
134
135
136 /***********************************************************************
137  *              error_handler
138  */
139 static int error_handler( Display *display, XErrorEvent *error_evt )
140 {
141     if (err_callback && display == err_callback_display)
142     {
143         if ((err_callback_result = err_callback( display, error_evt, err_callback_arg )))
144         {
145             TRACE( "got expected error\n" );
146             return 0;
147         }
148     }
149     if (synchronous) DebugBreak();  /* force an entry in the debugger */
150     old_error_handler( display, error_evt );
151     return 0;
152 }
153
154 /***********************************************************************
155  *              wine_tsx11_lock   (X11DRV.@)
156  */
157 void wine_tsx11_lock(void)
158 {
159     EnterCriticalSection( &X11DRV_CritSection );
160 }
161
162 /***********************************************************************
163  *              wine_tsx11_unlock   (X11DRV.@)
164  */
165 void wine_tsx11_unlock(void)
166 {
167     LeaveCriticalSection( &X11DRV_CritSection );
168 }
169
170 /***********************************************************************
171  *              get_server_startup
172  *
173  * Get the server startup time
174  * Won't be exact, but should be sufficient
175  */
176 static void get_server_startup(void)
177 {
178     struct timeval t;
179     gettimeofday( &t, NULL );
180     X11DRV_server_startticks = ((t.tv_sec * 1000) + (t.tv_usec / 1000)) - GetTickCount();
181 }
182
183
184 /***********************************************************************
185  *              get_config_key
186  *
187  * Get a config key from either the app-specific or the default config
188  */
189 inline static DWORD get_config_key( HKEY defkey, HKEY appkey, const char *name,
190                                     char *buffer, DWORD size )
191 {
192     if (appkey && !RegQueryValueExA( appkey, name, 0, NULL, buffer, &size )) return 0;
193     return RegQueryValueExA( defkey, name, 0, NULL, buffer, &size );
194 }
195
196
197 /***********************************************************************
198  *              setup_options
199  *
200  * Setup the x11drv options.
201  */
202 static void setup_options(void)
203 {
204     char buffer[MAX_PATH+16];
205     HKEY hkey, appkey = 0;
206     DWORD count;
207
208     if (RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\x11drv", 0, NULL,
209                          REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, NULL ))
210     {
211         ERR("Cannot create config registry key\n" );
212         ExitProcess(1);
213     }
214
215     /* open the app-specific key */
216
217     if (GetModuleFileNameA( 0, buffer, MAX_PATH ))
218     {
219         HKEY tmpkey;
220         char *p, *appname = buffer;
221         if ((p = strrchr( appname, '/' ))) appname = p + 1;
222         if ((p = strrchr( appname, '\\' ))) appname = p + 1;
223         strcat( appname, "\\x11drv" );
224         if (!RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\AppDefaults", &tmpkey ))
225         {
226             if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
227             RegCloseKey( tmpkey );
228         }
229     }
230
231     /* get the display name */
232
233     strcpy( buffer, "DISPLAY=" );
234     count = sizeof(buffer) - 8;
235     if (!RegQueryValueExA( hkey, "display", 0, NULL, buffer + 8, &count ))
236     {
237         const char *display_name = getenv( "DISPLAY" );
238         if (display_name && strcmp( buffer, display_name ))
239             MESSAGE( "x11drv: Warning: $DISPLAY variable ignored, using '%s' specified in config file\n",
240                      buffer + 8 );
241         putenv( strdup(buffer) );
242     }
243
244     if (!get_config_key( hkey, appkey, "Desktop", buffer, sizeof(buffer) ))
245     {
246         /* Imperfect validation:  If Desktop=N, then we don't turn on
247         ** the --desktop option.  We should really validate for a correct
248         ** sizing entry */
249         if (!IS_OPTION_FALSE(buffer[0])) desktop_geometry = strdup(buffer);
250     }
251
252     if (!get_config_key( hkey, appkey, "Managed", buffer, sizeof(buffer) ))
253         managed_mode = IS_OPTION_TRUE( buffer[0] );
254
255     if (!get_config_key( hkey, appkey, "DXGrab", buffer, sizeof(buffer) ))
256         dxgrab = IS_OPTION_TRUE( buffer[0] );
257
258     if (!get_config_key( hkey, appkey, "UseDGA", buffer, sizeof(buffer) ))
259         usedga = IS_OPTION_TRUE( buffer[0] );
260
261     if (!get_config_key( hkey, appkey, "UseXVidMode", buffer, sizeof(buffer) ))
262         usexvidmode = IS_OPTION_TRUE( buffer[0] );
263
264     if (!get_config_key( hkey, appkey, "UseTakeFocus", buffer, sizeof(buffer) ))
265         use_take_focus = IS_OPTION_TRUE( buffer[0] );
266
267     screen_depth = 0;
268     if (!get_config_key( hkey, appkey, "ScreenDepth", buffer, sizeof(buffer) ))
269         screen_depth = atoi(buffer);
270
271     if (!get_config_key( hkey, appkey, "Synchronous", buffer, sizeof(buffer) ))
272         synchronous = IS_OPTION_TRUE( buffer[0] );
273
274     if (!get_config_key( hkey, appkey, "ClientSideWithCore", buffer, sizeof(buffer) ))
275         client_side_with_core = IS_OPTION_TRUE( buffer[0] );
276
277     if (!get_config_key( hkey, appkey, "ClientSideWithRender", buffer, sizeof(buffer) ))
278         client_side_with_render = IS_OPTION_TRUE( buffer[0] );
279
280     if (!get_config_key( hkey, appkey, "ClientSideAntiAliasWithCore", buffer, sizeof(buffer) ))
281         client_side_antialias_with_core = IS_OPTION_TRUE( buffer[0] );
282
283     if (!get_config_key( hkey, appkey, "ClientSideAntiAliasWithRender", buffer, sizeof(buffer) ))
284         client_side_antialias_with_render = IS_OPTION_TRUE( buffer[0] );
285
286     if (!get_config_key( hkey, appkey, "DesktopDoubleBuffered", buffer, sizeof(buffer) ))
287         desktop_dbl_buf = IS_OPTION_TRUE( buffer[0] );
288
289     if (appkey) RegCloseKey( appkey );
290     RegCloseKey( hkey );
291 }
292
293
294 /***********************************************************************
295  *           X11DRV process initialisation routine
296  */
297 static void process_attach(void)
298 {
299     Display *display;
300
301     get_server_startup();
302     setup_options();
303
304     /* Open display */
305
306     if (!(display = TSXOpenDisplay( NULL )))
307     {
308         MESSAGE( "x11drv: Can't open display: %s\n", XDisplayName(NULL) );
309         ExitProcess(1);
310     }
311     fcntl( ConnectionNumber(display), F_SETFD, 1 ); /* set close on exec flag */
312     screen = DefaultScreenOfDisplay( display );
313     visual = DefaultVisual( display, DefaultScreen(display) );
314     root_window = DefaultRootWindow( display );
315     old_error_handler = XSetErrorHandler( error_handler );
316
317     /* Initialize screen depth */
318
319     if (screen_depth)  /* depth specified */
320     {
321         int depth_count, i;
322         int *depth_list = TSXListDepths(display, DefaultScreen(display), &depth_count);
323         for (i = 0; i < depth_count; i++)
324             if (depth_list[i] == screen_depth) break;
325         TSXFree( depth_list );
326         if (i >= depth_count)
327         {
328             MESSAGE( "x11drv: Depth %d not supported on this screen.\n", screen_depth );
329             ExitProcess(1);
330         }
331     }
332     else screen_depth = DefaultDepthOfScreen( screen );
333
334     /* check for Xkb extension */
335 #ifdef HAVE_XKB
336     if (use_xkb)
337     {
338         int xkb_opcode, xkb_event, xkb_error;
339         int xkb_major = XkbMajorVersion, xkb_minor = XkbMinorVersion;
340
341         use_xkb = XkbQueryExtension(display, &xkb_opcode, &xkb_event, &xkb_error,
342                                     &xkb_major, &xkb_minor);
343         if (use_xkb) /* we have XKB, approximate Windows behaviour */
344             XkbSetDetectableAutoRepeat(display, True, NULL);
345     }
346 #endif
347
348     /* Initialize OpenGL */
349     X11DRV_OpenGL_Init(display);
350
351     /* If OpenGL is available, change the default visual, etc as necessary */
352     if (desktop_dbl_buf && (desktop_vi = X11DRV_setup_opengl_visual( display )))
353     {
354         visual       = desktop_vi->visual;
355         screen       = ScreenOfDisplay(display, desktop_vi->screen);
356         screen_depth = desktop_vi->depth;
357     }
358
359     if (synchronous) XSynchronize( display, True );
360
361     screen_width  = WidthOfScreen( screen );
362     screen_height = HeightOfScreen( screen );
363
364     if (desktop_geometry)
365         root_window = X11DRV_create_desktop( desktop_vi, desktop_geometry );
366
367     /* initialize GDI */
368     if(!X11DRV_GDI_Initialize( display ))
369     {
370         ERR( "Couldn't Initialize GDI.\n" );
371         ExitProcess(1);
372     }
373
374     /* Initialize clipboard */
375     if (!X11DRV_InitClipboard( display ))
376     {
377         ERR( "Couldn't Initialize clipboard.\n" );
378         ExitProcess(1);
379     }
380
381 #ifdef HAVE_LIBXXF86VM
382     /* initialize XVidMode */
383     X11DRV_XF86VM_Init();
384 #endif
385 #ifdef HAVE_LIBXXF86DGA2
386     /* initialize DGA2 */
387     X11DRV_XF86DGA2_Init();
388 #endif
389 }
390
391
392 /***********************************************************************
393  *           X11DRV thread termination routine
394  */
395 static void thread_detach(void)
396 {
397     struct x11drv_thread_data *data = NtCurrentTeb()->driver_data;
398
399     if (data)
400     {
401         CloseHandle( data->display_fd );
402         wine_tsx11_lock();
403         XCloseDisplay( data->display );
404         /* if (data->xim) XCloseIM( data->xim ); */ /* crashes Xlib */
405         wine_tsx11_unlock();
406         HeapFree( GetProcessHeap(), 0, data );
407     }
408 }
409
410
411 /***********************************************************************
412  *           X11DRV process termination routine
413  */
414 static void process_detach(void)
415 {
416 #ifdef HAVE_LIBXXF86DGA2
417     /* cleanup DGA2 */
418     X11DRV_XF86DGA2_Cleanup();
419 #endif
420 #ifdef HAVE_LIBXXF86VM
421     /* cleanup XVidMode */
422     X11DRV_XF86VM_Cleanup();
423 #endif
424     if(using_client_side_fonts)
425         X11DRV_XRender_Finalize();
426
427     /* FIXME: should detach all threads */
428     thread_detach();
429
430     /* cleanup GDI */
431     X11DRV_GDI_Finalize();
432
433     DeleteCriticalSection( &X11DRV_CritSection );
434 }
435
436
437 /***********************************************************************
438  *           X11DRV thread initialisation routine
439  */
440 struct x11drv_thread_data *x11drv_init_thread_data(void)
441 {
442     struct x11drv_thread_data *data;
443
444     if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) )))
445     {
446         ERR( "could not create data\n" );
447         ExitProcess(1);
448     }
449     wine_tsx11_lock();
450     if (!(data->display = XOpenDisplay(NULL)))
451     {
452         wine_tsx11_unlock();
453         MESSAGE( "x11drv: Can't open display: %s\n", XDisplayName(NULL) );
454         ExitProcess(1);
455     }
456     fcntl( ConnectionNumber(data->display), F_SETFD, 1 ); /* set close on exec flag */
457
458     if ((data->xim = XOpenIM( data->display, NULL, NULL, NULL )))
459     {
460         TRACE("X display of IM = %p\n", XDisplayOfIM(data->xim));
461         TRACE("Using %s locale of Input Method\n", XLocaleOfIM(data->xim));
462     }
463     else
464         WARN("Can't open input method\n");
465
466 #ifdef HAVE_XKB
467     if (use_xkb) XkbSetDetectableAutoRepeat( data->display, True, NULL );
468 #endif
469
470     if (synchronous) XSynchronize( data->display, True );
471     wine_tsx11_unlock();
472     if (wine_server_fd_to_handle( ConnectionNumber(data->display), GENERIC_READ | SYNCHRONIZE,
473                                   FALSE, &data->display_fd ))
474     {
475         MESSAGE( "x11drv: Can't allocate handle for display fd\n" );
476         ExitProcess(1);
477     }
478     data->process_event_count = 0;
479     data->cursor = None;
480     data->cursor_window = None;
481     data->last_focus = 0;
482     NtCurrentTeb()->driver_data = data;
483     if (desktop_tid) AttachThreadInput( GetCurrentThreadId(), desktop_tid, TRUE );
484     return data;
485 }
486
487
488 /***********************************************************************
489  *           X11DRV initialisation routine
490  */
491 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
492 {
493     switch(reason)
494     {
495     case DLL_PROCESS_ATTACH:
496         process_attach();
497         break;
498     case DLL_THREAD_DETACH:
499         thread_detach();
500         break;
501     case DLL_PROCESS_DETACH:
502         process_detach();
503         break;
504     }
505     return TRUE;
506 }
507
508 /***********************************************************************
509  *              GetScreenSaveActive (X11DRV.@)
510  *
511  * Returns the active status of the screen saver
512  */
513 BOOL X11DRV_GetScreenSaveActive(void)
514 {
515     int timeout, temp;
516     TSXGetScreenSaver(gdi_display, &timeout, &temp, &temp, &temp);
517     return timeout != 0;
518 }
519
520 /***********************************************************************
521  *              SetScreenSaveActive (X11DRV.@)
522  *
523  * Activate/Deactivate the screen saver
524  */
525 void X11DRV_SetScreenSaveActive(BOOL bActivate)
526 {
527     int timeout, interval, prefer_blanking, allow_exposures;
528     static int last_timeout = 15 * 60;
529
530     TSXGetScreenSaver(gdi_display, &timeout, &interval, &prefer_blanking,
531                       &allow_exposures);
532     if (timeout) last_timeout = timeout;
533
534     timeout = bActivate ? last_timeout : 0;
535     TSXSetScreenSaver(gdi_display, timeout, interval, prefer_blanking,
536                       allow_exposures);
537 }