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