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