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