Fixed some issues found by winapi_check.
[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 <X11/cursorfont.h>
14 #include "ts_xlib.h"
15 #include "ts_xutil.h"
16
17 #include "winbase.h"
18 #include "wine/winbase16.h"
19 #include "winreg.h"
20
21 #include "callback.h"
22 #include "clipboard.h"
23 #include "debugtools.h"
24 #include "gdi.h"
25 #include "monitor.h"
26 #include "options.h"
27 #include "user.h"
28 #include "win.h"
29 #include "wine_gl.h"
30 #include "x11drv.h"
31
32 DEFAULT_DEBUG_CHANNEL(x11drv);
33
34 static USER_DRIVER user_driver =
35 {
36     /* event functions */
37     X11DRV_EVENT_Synchronize,
38     X11DRV_EVENT_CheckFocus,
39     X11DRV_EVENT_UserRepaintDisable,
40     /* keyboard functions */
41     X11DRV_KEYBOARD_Init,
42     X11DRV_KEYBOARD_VkKeyScan,
43     X11DRV_KEYBOARD_MapVirtualKey,
44     X11DRV_KEYBOARD_GetKeyNameText,
45     X11DRV_KEYBOARD_ToAscii,
46     X11DRV_KEYBOARD_GetBeepActive,
47     X11DRV_KEYBOARD_SetBeepActive,
48     X11DRV_KEYBOARD_Beep,
49     X11DRV_KEYBOARD_GetDIState,
50     X11DRV_KEYBOARD_GetDIData,
51     X11DRV_KEYBOARD_GetKeyboardConfig,
52     X11DRV_KEYBOARD_SetKeyboardConfig,
53     /* mouse functions */
54     X11DRV_MOUSE_Init,
55     X11DRV_MOUSE_SetCursor,
56     X11DRV_MOUSE_MoveCursor,
57     /* screen saver functions */
58     X11DRV_GetScreenSaveActive,
59     X11DRV_SetScreenSaveActive,
60     X11DRV_GetScreenSaveTimeout,
61     X11DRV_SetScreenSaveTimeout,
62     /* windowing functions */
63     X11DRV_IsSingleWindow
64 };
65
66 static XKeyboardState keyboard_state;
67
68 Display *display;
69 Screen *screen;
70 Visual *visual;
71 int screen_depth;
72 Window root_window;
73
74 /***********************************************************************
75  *              error_handler
76  */
77 static int error_handler(Display *display, XErrorEvent *error_evt)
78 {
79     DebugBreak();  /* force an entry in the debugger */
80     return 0;
81 }
82
83
84 /***********************************************************************
85  *              setup_options
86  *
87  * Setup the x11drv options.
88  */
89 static void setup_options(void)
90 {
91     char buffer[256];
92     HKEY hkey;
93     DWORD type, count;
94
95     if (RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\x11drv", 0, NULL,
96                          REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, NULL ))
97     {
98         ERR("Cannot create config registry key\n" );
99         ExitProcess(1);
100     }
101
102     /* --display option */
103
104     count = sizeof(buffer);
105     if (!RegQueryValueExA( hkey, "display", 0, &type, buffer, &count ))
106     {
107         if (Options.display)
108         {
109             if (strcmp( buffer, Options.display ))
110                 MESSAGE( "%s: warning: --display option ignored, using '%s'\n", argv0, buffer );
111         }
112         else if ((Options.display = getenv( "DISPLAY" )))
113         {
114             if (strcmp( buffer, Options.display ))
115                 MESSAGE( "%s: warning: $DISPLAY variable ignored, using '%s'\n", argv0, buffer );
116         }
117         Options.display = strdup(buffer);
118     }
119     else
120     {
121         if (!Options.display && !(Options.display = getenv( "DISPLAY" )))
122         {
123             MESSAGE( "%s: no display specified\n", argv0 );
124             ExitProcess(1);
125         }
126         RegSetValueExA( hkey, "display", 0, REG_SZ, Options.display, strlen(Options.display)+1 );
127     }
128
129     /* --managed option */
130
131     if (!Options.managed)
132     {
133         count = sizeof(buffer);
134         if (!RegQueryValueExA( hkey, "managed", 0, &type, buffer, &count ))
135             Options.managed = IS_OPTION_TRUE( buffer[0] );
136     }
137     else RegSetValueExA( hkey, "managed", 0, REG_SZ, "y", 2 );
138
139     RegCloseKey( hkey );
140 }
141
142    
143 /***********************************************************************
144  *              create_desktop
145  *
146  * Create the desktop window for the --desktop mode.
147  */
148 static void create_desktop( const char *geometry )
149 {
150     int x = 0, y = 0, flags;
151     unsigned int width = 640, height = 480;  /* Default size = 640x480 */
152     char *name = "Wine desktop";
153     XSizeHints *size_hints;
154     XWMHints   *wm_hints;
155     XClassHint *class_hints;
156     XSetWindowAttributes win_attr;
157     XTextProperty window_name;
158     Atom XA_WM_DELETE_WINDOW;
159     /* Used to create the desktop window with a good visual */
160     XVisualInfo *vi = NULL;
161 #ifdef HAVE_OPENGL
162     BOOL dblbuf_visual;
163
164     /* Get in wine.ini if the desktop window should have a double-buffered visual or not */
165     dblbuf_visual = PROFILE_GetWineIniBool( "x11drv", "DesktopDoubleBuffered", 0 );
166     if (dblbuf_visual)  {
167       int dblBuf[]={GLX_RGBA,GLX_DEPTH_SIZE,16,GLX_DOUBLEBUFFER,None};
168       
169       ENTER_GL();
170       vi = glXChooseVisual(display, DefaultScreen(display), dblBuf);
171       win_attr.colormap = XCreateColormap(display, RootWindow(display,vi->screen),
172                                          vi->visual, AllocNone);
173       LEAVE_GL();
174     }
175 #endif /* HAVE_OPENGL */    
176
177     flags = TSXParseGeometry( geometry, &x, &y, &width, &height );
178     MONITOR_PrimaryMonitor.rect.right  = width;
179     MONITOR_PrimaryMonitor.rect.bottom = height;
180
181     /* Create window */
182     win_attr.background_pixel = BlackPixel(display, 0);
183     win_attr.event_mask = ExposureMask | KeyPressMask | KeyReleaseMask |
184                           PointerMotionMask | ButtonPressMask |
185                           ButtonReleaseMask | EnterWindowMask;
186     win_attr.cursor = TSXCreateFontCursor( display, XC_top_left_arrow );
187
188     if (vi != NULL) {
189       visual       = vi->visual;
190       screen       = ScreenOfDisplay(display, vi->screen);
191       screen_depth = vi->depth;
192     }
193     root_window = TSXCreateWindow( display,
194                                    (vi == NULL ? DefaultRootWindow(display) : RootWindow(display, vi->screen)),
195                                    x, y, width, height, 0,
196                                    (vi == NULL ? CopyFromParent : vi->depth),
197                                    InputOutput,
198                                    (vi == NULL ? CopyFromParent : vi->visual),
199                                    CWBackPixel | CWEventMask | CWCursor | (vi == NULL ? 0 : CWColormap),
200                                    &win_attr );
201   
202     /* Set window manager properties */
203     size_hints  = TSXAllocSizeHints();
204     wm_hints    = TSXAllocWMHints();
205     class_hints = TSXAllocClassHint();
206     if (!size_hints || !wm_hints || !class_hints)
207     {
208         MESSAGE("Not enough memory for window manager hints.\n" );
209         ExitProcess(1);
210     }
211     size_hints->min_width = size_hints->max_width = width;
212     size_hints->min_height = size_hints->max_height = height;
213     size_hints->flags = PMinSize | PMaxSize;
214     if (flags & (XValue | YValue)) size_hints->flags |= USPosition;
215     if (flags & (WidthValue | HeightValue)) size_hints->flags |= USSize;
216     else size_hints->flags |= PSize;
217
218     wm_hints->flags = InputHint | StateHint;
219     wm_hints->input = True;
220     wm_hints->initial_state = NormalState;
221     class_hints->res_name = (char *)argv0;
222     class_hints->res_class = "Wine";
223
224     TSXStringListToTextProperty( &name, 1, &window_name );
225     TSXSetWMProperties( display, root_window, &window_name, &window_name,
226                         NULL, 0, size_hints, wm_hints, class_hints );
227     XA_WM_DELETE_WINDOW = TSXInternAtom( display, "WM_DELETE_WINDOW", False );
228     TSXSetWMProtocols( display, root_window, &XA_WM_DELETE_WINDOW, 1 );
229     TSXFree( size_hints );
230     TSXFree( wm_hints );
231     TSXFree( class_hints );
232
233     /* Map window */
234     TSXMapWindow( display, root_window );
235 }
236
237 /* Created so that XOpenIM can be called using the 'large stack' */
238 static void XOpenIM_large_stack(void)
239 {
240   TSXOpenIM(display,NULL,NULL,NULL);
241 }
242
243 /***********************************************************************
244  *           X11DRV process initialisation routine
245  */
246 static void process_attach(void)
247 {
248     USER_Driver      = &user_driver;
249     CLIPBOARD_Driver = &X11DRV_CLIPBOARD_Driver;
250     WND_Driver       = &X11DRV_WND_Driver;
251
252     setup_options();
253
254     /* Open display */
255
256     if (!(display = TSXOpenDisplay( Options.display )))
257     {
258         MESSAGE( "%s: Can't open display: %s\n", argv0, Options.display );
259         ExitProcess(1);
260     }
261     fcntl( ConnectionNumber(display), F_SETFD, 1 ); /* set close on exec flag */
262     screen = DefaultScreenOfDisplay( display );
263     visual = DefaultVisual( display, DefaultScreen(display) );
264     root_window = DefaultRootWindow( display );
265
266     /* Initialize screen depth */
267
268     screen_depth = PROFILE_GetWineIniInt( "x11drv", "ScreenDepth", 0 );
269     if (screen_depth)  /* depth specified */
270     {
271         int depth_count, i;
272         int *depth_list = TSXListDepths(display, DefaultScreen(display), &depth_count);
273         for (i = 0; i < depth_count; i++)
274             if (depth_list[i] == screen_depth) break;
275         TSXFree( depth_list );
276         if (i >= depth_count)
277         {
278             MESSAGE( "%s: Depth %d not supported on this screen.\n", argv0, screen_depth );
279             ExitProcess(1);
280         }
281     }
282     else screen_depth = DefaultDepthOfScreen( screen );
283
284     /* tell the libX11 that we will do input method handling ourselves
285      * that keep libX11 from doing anything whith dead keys, allowing Wine
286      * to have total control over dead keys, that is this line allows
287      * them to work in Wine, even whith a libX11 including the dead key
288      * patches from Th.Quinot (http://Web.FdN.FR/~tquinot/dead-keys.en.html)
289      */
290     CALL_LARGE_STACK( XOpenIM_large_stack, NULL );
291
292     if (Options.synchronous) XSetErrorHandler( error_handler );
293
294     MONITOR_PrimaryMonitor.rect.left   = 0;
295     MONITOR_PrimaryMonitor.rect.top    = 0;
296     MONITOR_PrimaryMonitor.rect.right  = WidthOfScreen( screen );
297     MONITOR_PrimaryMonitor.rect.bottom = HeightOfScreen( screen );
298     MONITOR_PrimaryMonitor.depth       = screen_depth;
299
300     if (Options.desktopGeometry)
301     {
302         Options.managed = FALSE;
303         create_desktop( Options.desktopGeometry );
304     }
305
306     /* initialize GDI */
307     X11DRV_GDI_Initialize();
308
309     /* save keyboard setup */
310     TSXGetKeyboardControl(display, &keyboard_state);
311
312     /* initialize event handling */
313     X11DRV_EVENT_Init();
314
315     /* load display.dll */
316     LoadLibrary16( "display" );
317 }
318
319
320 /***********************************************************************
321  *           X11DRV process termination routine
322  */
323 static void process_detach(void)
324 {
325     /* restore keyboard setup */
326     XKeyboardControl keyboard_value;
327   
328     keyboard_value.key_click_percent = keyboard_state.key_click_percent;
329     keyboard_value.bell_percent      = keyboard_state.bell_percent;
330     keyboard_value.bell_pitch        = keyboard_state.bell_pitch;
331     keyboard_value.bell_duration     = keyboard_state.bell_duration;
332     keyboard_value.auto_repeat_mode  = keyboard_state.global_auto_repeat;
333   
334     XChangeKeyboardControl(display, KBKeyClickPercent | KBBellPercent | 
335                            KBBellPitch | KBBellDuration | KBAutoRepeatMode, &keyboard_value);
336
337     /* cleanup GDI */
338     X11DRV_GDI_Finalize();
339
340 #if 0  /* FIXME */
341
342     /* close the display */
343     XCloseDisplay( display );
344     display = NULL;
345
346     USER_Driver      = NULL;
347     CLIPBOARD_Driver = NULL;
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 }