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