Jean-Claude Batista
[wine/multimedia.git] / dlls / x11drv / x11drv_main.c
blobad5c74a9acb91ef4aa28a421b69f067c70290694
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 "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"
28 static USER_DRIVER user_driver =
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
61 static XKeyboardState keyboard_state;
63 Display *display;
64 Screen *screen;
65 Visual *visual;
66 int screen_depth;
67 Window root_window;
69 /***********************************************************************
70 * error_handler
72 static int error_handler(Display *display, XErrorEvent *error_evt)
74 DebugBreak(); /* force an entry in the debugger */
75 return 0;
79 /***********************************************************************
80 * create_desktop
82 * Create the desktop window for the --desktop mode.
84 static void create_desktop( const char *geometry )
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;
96 flags = TSXParseGeometry( geometry, &x, &y, &width, &height );
97 MONITOR_PrimaryMonitor.rect.right = width;
98 MONITOR_PrimaryMonitor.rect.bottom = height;
100 /* Create window */
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 );
108 root_window = TSXCreateWindow( display, DefaultRootWindow(display),
109 x, y, width, height, 0,
110 CopyFromParent, InputOutput, CopyFromParent,
111 CWBackPixel | CWEventMask | CWCursor, &win_attr);
113 /* Set window manager properties */
115 size_hints = TSXAllocSizeHints();
116 wm_hints = TSXAllocWMHints();
117 class_hints = TSXAllocClassHint();
118 if (!size_hints || !wm_hints || !class_hints)
120 MESSAGE("Not enough memory for window manager hints.\n" );
121 ExitProcess(1);
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;
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";
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 );
145 /* Map window */
147 TSXMapWindow( display, root_window );
150 /* Created so that XOpenIM can be called using the 'large stack' */
151 static void XOpenIM_large_stack(void)
153 TSXOpenIM(display,NULL,NULL,NULL);
156 /***********************************************************************
157 * X11DRV process initialisation routine
159 static void process_attach(void)
161 USER_Driver = &user_driver;
162 CLIPBOARD_Driver = &X11DRV_CLIPBOARD_Driver;
163 WND_Driver = &X11DRV_WND_Driver;
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 CALL_LARGE_STACK( XOpenIM_large_stack, 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 != 0;
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));