Removed some unnecessary includes.
[wine] / dlls / x11drv / x11drv_main.c
1 /*
2  * X11DRV initialization code
3  *
4  * Copyright 1998 Patrik Stridvall
5  * Copyright 2000 Alexandre Julliard
6  */
7
8 #include "config.h"
9
10 #include <fcntl.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <sys/time.h>
15 #include <unistd.h>
16 #include <X11/cursorfont.h>
17 #include "ts_xlib.h"
18 #include "ts_xutil.h"
19 #include "ts_shape.h"
20
21 #include "winbase.h"
22 #include "wine/winbase16.h"
23 #include "winreg.h"
24
25 #include "callback.h"
26 #include "debugtools.h"
27 #include "gdi.h"
28 #include "options.h"
29 #include "user.h"
30 #include "win.h"
31 #include "wine_gl.h"
32 #include "x11drv.h"
33
34 DEFAULT_DEBUG_CHANNEL(x11drv);
35
36 static XKeyboardState keyboard_state;
37
38 Display *display;
39 Screen *screen;
40 Visual *visual;
41 unsigned int screen_width;
42 unsigned int screen_height;
43 unsigned int screen_depth;
44 Window root_window;
45
46 unsigned int X11DRV_server_startticks;
47
48 /***********************************************************************
49  *              error_handler
50  */
51 static int error_handler(Display *display, XErrorEvent *error_evt)
52 {
53     DebugBreak();  /* force an entry in the debugger */
54     return 0;
55 }
56
57
58 /***********************************************************************
59  *              get_server_startup
60  *
61  * Get the server startup time 
62  * Won't be exact, but should be sufficient
63  */
64 static void get_server_startup(void)
65 {
66     struct timeval t;
67     gettimeofday( &t, NULL );
68     X11DRV_server_startticks = ((t.tv_sec * 1000) + (t.tv_usec / 1000)) - GetTickCount();
69 }
70
71
72 /***********************************************************************
73  *              setup_options
74  *
75  * Setup the x11drv options.
76  */
77 static void setup_options(void)
78 {
79     char buffer[256];
80     HKEY hkey;
81     DWORD type, count;
82
83     if (RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\x11drv", 0, NULL,
84                          REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, NULL ))
85     {
86         ERR("Cannot create config registry key\n" );
87         ExitProcess(1);
88     }
89
90     /* --display option */
91
92     count = sizeof(buffer);
93     if (!RegQueryValueExA( hkey, "display", 0, &type, buffer, &count ))
94     {
95         if (Options.display)
96         {
97             if (strcmp( buffer, Options.display ))
98                 MESSAGE( "%s: warning: --display option ignored, using '%s'\n", argv0, buffer );
99         }
100         else if ((Options.display = getenv( "DISPLAY" )))
101         {
102             if (strcmp( buffer, Options.display ))
103                 MESSAGE( "%s: warning: $DISPLAY variable ignored, using '%s'\n", argv0, buffer );
104         }
105         Options.display = strdup(buffer);
106     }
107     else
108     {
109         if (!Options.display && !(Options.display = getenv( "DISPLAY" )))
110         {
111             MESSAGE( "%s: no display specified\n", argv0 );
112             ExitProcess(1);
113         }
114         RegSetValueExA( hkey, "display", 0, REG_SZ, Options.display, strlen(Options.display)+1 );
115     }
116
117     /* check and set --managed and --desktop options in wine config file
118      * if it was not set on command line */
119
120     if ((!Options.managed) && (Options.desktopGeometry == NULL))
121     {
122         count = sizeof(buffer);
123         if (!RegQueryValueExA( hkey, "managed", 0, &type, buffer, &count ))
124             Options.managed = IS_OPTION_TRUE( buffer[0] );
125
126         count = sizeof(buffer);
127         if (!RegQueryValueExA( hkey, "Desktop", 0, &type, buffer, &count ))
128             /* Imperfect validation:  If Desktop=N, then we don't turn on
129             ** the --desktop option.  We should really validate for a correct
130             ** sizing entry */
131             if (! IS_OPTION_FALSE(buffer[0]))
132                 Options.desktopGeometry = strdup(buffer);
133     }
134
135     if (Options.managed)
136         RegSetValueExA( hkey, "managed", 0, REG_SZ, "y", 2 );
137
138     if (Options.desktopGeometry)
139         RegSetValueExA( hkey, "desktop", 0, REG_SZ, Options.desktopGeometry, strlen(Options.desktopGeometry)+1 );
140     
141     RegCloseKey( hkey );
142 }
143
144    
145 /***********************************************************************
146  *              create_desktop
147  *
148  * Create the desktop window for the --desktop mode.
149  */
150 static void create_desktop( const char *geometry )
151 {
152     int x = 0, y = 0, flags;
153     unsigned int width = 640, height = 480;  /* Default size = 640x480 */
154     char *name = "Wine desktop";
155     XSizeHints *size_hints;
156     XWMHints   *wm_hints;
157     XClassHint *class_hints;
158     XSetWindowAttributes win_attr;
159     XTextProperty window_name;
160     Atom XA_WM_DELETE_WINDOW;
161     /* Used to create the desktop window with a good visual */
162     XVisualInfo *vi = NULL;
163 #ifdef HAVE_OPENGL
164     BOOL dblbuf_visual;
165     int err_base, evt_base;
166     
167     /* Get in wine.ini if the desktop window should have a double-buffered visual or not.
168        But first, test if OpenGL is even supported on the display ! */
169     if (glXQueryExtension(display, &err_base, &evt_base) == True) {
170       dblbuf_visual = PROFILE_GetWineIniBool( "x11drv", "DesktopDoubleBuffered", 0 );
171       if (dblbuf_visual)  {
172         int dblBuf[]={GLX_RGBA,GLX_DEPTH_SIZE,16,GLX_DOUBLEBUFFER,None};
173         
174         ENTER_GL();
175         vi = glXChooseVisual(display, DefaultScreen(display), dblBuf);
176         win_attr.colormap = XCreateColormap(display, RootWindow(display,vi->screen),
177                                             vi->visual, AllocNone);
178         LEAVE_GL();
179       }
180     }
181 #endif /* HAVE_OPENGL */    
182
183     flags = TSXParseGeometry( geometry, &x, &y, &width, &height );
184     screen_width  = width;
185     screen_height = height;
186
187     /* Create window */
188     win_attr.background_pixel = BlackPixel(display, 0);
189     win_attr.event_mask = ExposureMask | KeyPressMask | KeyReleaseMask |
190                           PointerMotionMask | ButtonPressMask |
191                           ButtonReleaseMask | EnterWindowMask;
192     win_attr.cursor = TSXCreateFontCursor( display, XC_top_left_arrow );
193
194     if (vi != NULL) {
195       visual       = vi->visual;
196       screen       = ScreenOfDisplay(display, vi->screen);
197       screen_depth = vi->depth;
198     }
199     root_window = TSXCreateWindow( display,
200                                    (vi == NULL ? DefaultRootWindow(display) : RootWindow(display, vi->screen)),
201                                    x, y, width, height, 0,
202                                    (vi == NULL ? CopyFromParent : vi->depth),
203                                    InputOutput,
204                                    (vi == NULL ? CopyFromParent : vi->visual),
205                                    CWBackPixel | CWEventMask | CWCursor | (vi == NULL ? 0 : CWColormap),
206                                    &win_attr );
207   
208     /* Set window manager properties */
209     size_hints  = TSXAllocSizeHints();
210     wm_hints    = TSXAllocWMHints();
211     class_hints = TSXAllocClassHint();
212     if (!size_hints || !wm_hints || !class_hints)
213     {
214         MESSAGE("Not enough memory for window manager hints.\n" );
215         ExitProcess(1);
216     }
217     size_hints->min_width = size_hints->max_width = width;
218     size_hints->min_height = size_hints->max_height = height;
219     size_hints->flags = PMinSize | PMaxSize;
220     if (flags & (XValue | YValue)) size_hints->flags |= USPosition;
221     if (flags & (WidthValue | HeightValue)) size_hints->flags |= USSize;
222     else size_hints->flags |= PSize;
223
224     wm_hints->flags = InputHint | StateHint;
225     wm_hints->input = True;
226     wm_hints->initial_state = NormalState;
227     class_hints->res_name = (char *)argv0;
228     class_hints->res_class = "Wine";
229
230     TSXStringListToTextProperty( &name, 1, &window_name );
231     TSXSetWMProperties( display, root_window, &window_name, &window_name,
232                         NULL, 0, size_hints, wm_hints, class_hints );
233     XA_WM_DELETE_WINDOW = TSXInternAtom( display, "WM_DELETE_WINDOW", False );
234     TSXSetWMProtocols( display, root_window, &XA_WM_DELETE_WINDOW, 1 );
235     TSXFree( size_hints );
236     TSXFree( wm_hints );
237     TSXFree( class_hints );
238
239     /* Map window */
240     TSXMapWindow( display, root_window );
241 }
242
243 /* Created so that XOpenIM can be called using the 'large stack' */
244 static void XOpenIM_large_stack(void)
245 {
246   TSXOpenIM(display,NULL,NULL,NULL);
247 }
248
249 /***********************************************************************
250  *           X11DRV process initialisation routine
251  */
252 static void process_attach(void)
253 {
254     WND_Driver       = &X11DRV_WND_Driver;
255
256     get_server_startup();
257     setup_options();
258
259     /* Open display */
260
261     if (!(display = TSXOpenDisplay( Options.display )))
262     {
263         MESSAGE( "%s: Can't open display: %s\n", argv0, Options.display );
264         ExitProcess(1);
265     }
266     fcntl( ConnectionNumber(display), F_SETFD, 1 ); /* set close on exec flag */
267     screen = DefaultScreenOfDisplay( display );
268     visual = DefaultVisual( display, DefaultScreen(display) );
269     root_window = DefaultRootWindow( display );
270
271     /* Initialize screen depth */
272
273     screen_depth = PROFILE_GetWineIniInt( "x11drv", "ScreenDepth", 0 );
274     if (screen_depth)  /* depth specified */
275     {
276         int depth_count, i;
277         int *depth_list = TSXListDepths(display, DefaultScreen(display), &depth_count);
278         for (i = 0; i < depth_count; i++)
279             if (depth_list[i] == screen_depth) break;
280         TSXFree( depth_list );
281         if (i >= depth_count)
282         {
283             MESSAGE( "%s: Depth %d not supported on this screen.\n", argv0, screen_depth );
284             ExitProcess(1);
285         }
286     }
287     else screen_depth = DefaultDepthOfScreen( screen );
288
289     /* tell the libX11 that we will do input method handling ourselves
290      * that keep libX11 from doing anything whith dead keys, allowing Wine
291      * to have total control over dead keys, that is this line allows
292      * them to work in Wine, even whith a libX11 including the dead key
293      * patches from Th.Quinot (http://Web.FdN.FR/~tquinot/dead-keys.en.html)
294      */
295     CALL_LARGE_STACK( XOpenIM_large_stack, NULL );
296
297     if (Options.synchronous) XSetErrorHandler( error_handler );
298
299     screen_width  = WidthOfScreen( screen );
300     screen_height = HeightOfScreen( screen );
301
302     if (Options.desktopGeometry)
303     {
304         Options.managed = FALSE;
305         create_desktop( Options.desktopGeometry );
306     }
307
308     /* initialize GDI */
309     X11DRV_GDI_Initialize();
310
311     /* save keyboard setup */
312     TSXGetKeyboardControl(display, &keyboard_state);
313
314     /* initialize event handling */
315     X11DRV_EVENT_Init();
316
317     /* load display.dll */
318     LoadLibrary16( "display" );
319 }
320
321
322 /***********************************************************************
323  *           X11DRV process termination routine
324  */
325 static void process_detach(void)
326 {
327     /* restore keyboard setup */
328     XKeyboardControl keyboard_value;
329   
330     keyboard_value.key_click_percent = keyboard_state.key_click_percent;
331     keyboard_value.bell_percent      = keyboard_state.bell_percent;
332     keyboard_value.bell_pitch        = keyboard_state.bell_pitch;
333     keyboard_value.bell_duration     = keyboard_state.bell_duration;
334     keyboard_value.auto_repeat_mode  = keyboard_state.global_auto_repeat;
335   
336     XChangeKeyboardControl(display, KBKeyClickPercent | KBBellPercent | 
337                            KBBellPitch | KBBellDuration | KBAutoRepeatMode, &keyboard_value);
338
339     /* cleanup GDI */
340     X11DRV_GDI_Finalize();
341
342 #if 0  /* FIXME */
343
344     /* close the display */
345     XCloseDisplay( display );
346     display = NULL;
347
348     WND_Driver       = NULL;
349 #endif
350 }
351
352
353 /***********************************************************************
354  *           X11DRV initialisation routine
355  */
356 BOOL WINAPI X11DRV_Init( HINSTANCE hinst, DWORD reason, LPVOID reserved )
357 {
358     static int process_count;
359
360     switch(reason)
361     {
362     case DLL_PROCESS_ATTACH:
363         if (!process_count++) process_attach();
364         break;
365     case DLL_PROCESS_DETACH:
366         if (!--process_count) process_detach();
367         break;
368     }
369     return TRUE;
370 }
371
372 /***********************************************************************
373  *              X11DRV_GetScreenSaveActive
374  *
375  * Returns the active status of the screen saver
376  */
377 BOOL X11DRV_GetScreenSaveActive(void)
378 {
379     int timeout, temp;
380     TSXGetScreenSaver(display, &timeout, &temp, &temp, &temp);
381     return timeout != 0;
382 }
383
384 /***********************************************************************
385  *              X11DRV_SetScreenSaveActive
386  *
387  * Activate/Deactivate the screen saver
388  */
389 void X11DRV_SetScreenSaveActive(BOOL bActivate)
390 {
391     if(bActivate)
392         TSXActivateScreenSaver(display);
393     else
394         TSXResetScreenSaver(display);
395 }
396
397 /***********************************************************************
398  *              X11DRV_GetScreenSaveTimeout
399  *
400  * Return the screen saver timeout
401  */
402 int X11DRV_GetScreenSaveTimeout(void)
403 {
404     int timeout, temp;
405     TSXGetScreenSaver(display, &timeout, &temp, &temp, &temp);
406     return timeout;
407 }
408
409 /***********************************************************************
410  *              X11DRV_SetScreenSaveTimeout
411  *
412  * Set the screen saver timeout
413  */
414 void X11DRV_SetScreenSaveTimeout(int nTimeout)
415 {
416     /* timeout is a 16bit entity (CARD16) in the protocol, so it should
417      * not get over 32767 or it will get negative. */
418     if (nTimeout>32767) nTimeout = 32767;
419     TSXSetScreenSaver(display, nTimeout, 60, DefaultBlanking, DefaultExposures);
420 }
421
422 /***********************************************************************
423  *              X11DRV_IsSingleWindow
424  */
425 BOOL X11DRV_IsSingleWindow(void)
426 {
427     return (root_window != DefaultRootWindow(display));
428 }