Fixed file descriptor leaks.
[wine.git] / dlls / x11drv / x11drv_main.c
blobd546aa86aa5d61d5a0743048ed3c0c1362610fff
1 /*
2 * X11DRV initialization code
4 * Copyright 1998 Patrik Stridvall
5 * Copyright 2000 Alexandre Julliard
6 */
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"
15 #include "winbase.h"
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"
27 static USER_DRIVER user_driver =
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
60 static XKeyboardState keyboard_state;
62 Display *display;
63 Screen *screen;
64 Visual *visual;
65 int screen_depth;
66 Window root_window;
68 /***********************************************************************
69 * error_handler
71 static int error_handler(Display *display, XErrorEvent *error_evt)
73 DebugBreak(); /* force an entry in the debugger */
74 return 0;
78 /***********************************************************************
79 * create_desktop
81 * Create the desktop window for the --desktop mode.
83 static void create_desktop( const char *geometry )
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;
95 flags = TSXParseGeometry( geometry, &x, &y, &width, &height );
96 MONITOR_PrimaryMonitor.rect.right = width;
97 MONITOR_PrimaryMonitor.rect.bottom = height;
99 /* Create window */
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 );
107 root_window = TSXCreateWindow( display, DefaultRootWindow(display),
108 x, y, width, height, 0,
109 CopyFromParent, InputOutput, CopyFromParent,
110 CWBackPixel | CWEventMask | CWCursor, &win_attr);
112 /* Set window manager properties */
114 size_hints = TSXAllocSizeHints();
115 wm_hints = TSXAllocWMHints();
116 class_hints = TSXAllocClassHint();
117 if (!size_hints || !wm_hints || !class_hints)
119 MESSAGE("Not enough memory for window manager hints.\n" );
120 ExitProcess(1);
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;
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";
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 );
144 /* Map window */
146 TSXMapWindow( display, root_window );
150 /***********************************************************************
151 * X11DRV process initialisation routine
153 static void process_attach(void)
155 USER_Driver = &user_driver;
156 CLIPBOARD_Driver = &X11DRV_CLIPBOARD_Driver;
157 WND_Driver = &X11DRV_WND_Driver;
159 /* We need this before calling any Xlib function */
160 InitializeCriticalSection( &X11DRV_CritSection );
161 MakeCriticalSectionGlobal( &X11DRV_CritSection );
163 putenv("XKB_DISABLE="); /* Disable XKB extension if present. */
165 /* Open display */
167 if (!(display = TSXOpenDisplay( Options.display )))
169 MESSAGE( "%s: Can't open display: %s\n",
170 argv0, Options.display ? Options.display : "(none specified)" );
171 ExitProcess(1);
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 );
178 /* Initialize screen depth */
180 screen_depth = PROFILE_GetWineIniInt( "x11drv", "ScreenDepth", 0 );
181 if (screen_depth) /* depth specified */
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)
190 MESSAGE( "%s: Depth %d not supported on this screen.\n", argv0, screen_depth );
191 ExitProcess(1);
194 else screen_depth = DefaultDepthOfScreen( screen );
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)
202 TSXOpenIM(display,NULL,NULL,NULL);
204 if (Options.synchronous) XSetErrorHandler( error_handler );
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;
212 if (Options.desktopGeometry)
214 Options.managed = FALSE;
215 create_desktop( Options.desktopGeometry );
218 /* initialize GDI */
219 X11DRV_GDI_Initialize();
221 /* save keyboard setup */
222 TSXGetKeyboardControl(display, &keyboard_state);
224 /* initialize event handling */
225 X11DRV_EVENT_Init();
229 /***********************************************************************
230 * X11DRV process termination routine
232 static void process_detach(void)
234 /* restore keyboard setup */
235 XKeyboardControl keyboard_value;
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;
243 XChangeKeyboardControl(display, KBKeyClickPercent | KBBellPercent |
244 KBBellPitch | KBBellDuration | KBAutoRepeatMode, &keyboard_value);
246 /* cleanup GDI */
247 X11DRV_GDI_Finalize();
249 #if 0 /* FIXME */
251 /* close the display */
252 XCloseDisplay( display );
253 display = NULL;
255 USER_Driver = NULL;
256 CLIPBOARD_Driver = NULL;
257 WND_Driver = NULL;
258 #endif
262 /***********************************************************************
263 * X11DRV initialisation routine
265 BOOL WINAPI X11DRV_Init( HINSTANCE hinst, DWORD reason, LPVOID reserved )
267 static int process_count;
269 switch(reason)
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;
278 return TRUE;
281 /***********************************************************************
282 * X11DRV_GetScreenSaveActive
284 * Returns the active status of the screen saver
286 BOOL X11DRV_GetScreenSaveActive(void)
288 int timeout, temp;
289 TSXGetScreenSaver(display, &timeout, &temp, &temp, &temp);
290 return timeout != NULL;
293 /***********************************************************************
294 * X11DRV_SetScreenSaveActive
296 * Activate/Deactivate the screen saver
298 void X11DRV_SetScreenSaveActive(BOOL bActivate)
300 if(bActivate)
301 TSXActivateScreenSaver(display);
302 else
303 TSXResetScreenSaver(display);
306 /***********************************************************************
307 * X11DRV_GetScreenSaveTimeout
309 * Return the screen saver timeout
311 int X11DRV_GetScreenSaveTimeout(void)
313 int timeout, temp;
314 TSXGetScreenSaver(display, &timeout, &temp, &temp, &temp);
315 return timeout;
318 /***********************************************************************
319 * X11DRV_SetScreenSaveTimeout
321 * Set the screen saver timeout
323 void X11DRV_SetScreenSaveTimeout(int nTimeout)
325 TSXSetScreenSaver(display, nTimeout, 60, DefaultBlanking, DefaultExposures);
328 /***********************************************************************
329 * X11DRV_IsSingleWindow
331 BOOL X11DRV_IsSingleWindow(void)
333 return (root_window != DefaultRootWindow(display));