Optimized debugging API to reduce code size.
[wine/wine64.git] / windows / x11drv / main.c
blobbbc752c18be57cba10133e1648fe0b19bed942df
1 /*
2 * X11 main driver
4 * Copyright 1998 Patrik Stridvall
6 */
8 #include "config.h"
10 #ifndef X_DISPLAY_MISSING
12 #include <X11/Xlocale.h>
13 #include "ts_xlib.h"
14 #include "ts_xresource.h"
15 #include "ts_xutil.h"
17 #include <signal.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <unistd.h>
22 #include "clipboard.h"
23 #include "console.h"
24 #include "debugtools.h"
25 #include "desktop.h"
26 #include "keyboard.h"
27 #include "main.h"
28 #include "message.h"
29 #include "monitor.h"
30 #include "mouse.h"
31 #include "options.h"
32 #include "win.h"
33 #include "windef.h"
34 #include "x11drv.h"
35 #include "xmalloc.h"
36 #include "version.h"
38 /**********************************************************************/
40 void X11DRV_USER_ParseOptions(int *argc, char *argv[]);
41 void X11DRV_USER_Create(void);
42 void X11DRV_USER_SaveSetup(void);
43 void X11DRV_USER_RestoreSetup(void);
45 /**********************************************************************/
47 #define WINE_CLASS "Wine" /* Class name for resources */
48 #define WINE_APP_DEFAULTS "/usr/lib/X11/app-defaults/Wine"
50 static XrmOptionDescRec optionsTable[] =
52 { "-backingstore", ".backingstore", XrmoptionNoArg, (caddr_t)"on" },
53 { "-desktop", ".desktop", XrmoptionSepArg, (caddr_t)NULL },
54 { "-depth", ".depth", XrmoptionSepArg, (caddr_t)NULL },
55 { "-display", ".display", XrmoptionSepArg, (caddr_t)NULL },
56 { "-iconic", ".iconic", XrmoptionNoArg, (caddr_t)"on" },
57 { "-language", ".language", XrmoptionSepArg, (caddr_t)"En" },
58 { "-name", ".name", XrmoptionSepArg, (caddr_t)NULL },
59 { "-perfect", ".perfect", XrmoptionNoArg, (caddr_t)"on" },
60 { "-privatemap", ".privatemap", XrmoptionNoArg, (caddr_t)"on" },
61 { "-fixedmap", ".fixedmap", XrmoptionNoArg, (caddr_t)"on" },
62 { "-synchronous", ".synchronous", XrmoptionNoArg, (caddr_t)"on" },
63 { "-debug", ".debug", XrmoptionNoArg, (caddr_t)"on" },
64 { "-debugmsg", ".debugmsg", XrmoptionSepArg, (caddr_t)NULL },
65 { "-dll", ".dll", XrmoptionSepArg, (caddr_t)NULL },
66 { "-failreadonly", ".failreadonly", XrmoptionNoArg, (caddr_t)"on" },
67 { "-mode", ".mode", XrmoptionSepArg, (caddr_t)NULL },
68 { "-managed", ".managed", XrmoptionNoArg, (caddr_t)"off"},
69 { "-winver", ".winver", XrmoptionSepArg, (caddr_t)NULL },
70 { "-config", ".config", XrmoptionSepArg, (caddr_t)NULL },
71 { "-nodga", ".nodga", XrmoptionNoArg, (caddr_t)"off"},
72 { "-noxshm", ".noxshm", XrmoptionNoArg, (caddr_t)"off"},
73 { "-dxgrab", ".dxgrab", XrmoptionNoArg, (caddr_t)"on" },
74 { "-console", ".console", XrmoptionSepArg, (caddr_t)NULL },
75 { "-dosver", ".dosver", XrmoptionSepArg, (caddr_t)NULL }
78 #define NB_OPTIONS (sizeof(optionsTable) / sizeof(optionsTable[0]))
80 /**********************************************************************/
82 static XKeyboardState X11DRV_XKeyboardState;
83 Display *display;
85 /***********************************************************************
86 * X11DRV_GetXScreen
88 * Return the X screen associated to the current desktop.
90 Screen *X11DRV_GetXScreen()
92 return X11DRV_MONITOR_GetXScreen(&MONITOR_PrimaryMonitor);
95 /***********************************************************************
96 * X11DRV_GetXRootWindow
98 * Return the X display associated to the current desktop.
100 Window X11DRV_GetXRootWindow()
102 return X11DRV_MONITOR_GetXRootWindow(&MONITOR_PrimaryMonitor);
105 /***********************************************************************
106 * X11DRV_USER_Initialize
108 BOOL X11DRV_USER_Initialize(void)
110 CLIPBOARD_Driver = &X11DRV_CLIPBOARD_Driver;
111 DESKTOP_Driver = &X11DRV_DESKTOP_Driver;
112 EVENT_Driver = &X11DRV_EVENT_Driver;
113 KEYBOARD_Driver = &X11DRV_KEYBOARD_Driver;
114 MONITOR_Driver = &X11DRV_MONITOR_Driver;
115 MOUSE_Driver = &X11DRV_MOUSE_Driver;
116 WND_Driver = &X11DRV_WND_Driver;
118 /* We need this before calling any Xlib function */
119 InitializeCriticalSection( &X11DRV_CritSection );
120 MakeCriticalSectionGlobal( &X11DRV_CritSection );
122 TSXrmInitialize();
124 putenv("XKB_DISABLE="); /* Disable XKB extension if present. */
126 X11DRV_USER_ParseOptions( Options.argc, Options.argv );
127 X11DRV_USER_Create();
128 X11DRV_USER_SaveSetup();
130 return TRUE;
133 /***********************************************************************
134 * X11DRV_USER_Finalize
136 void X11DRV_USER_Finalize(void)
138 X11DRV_USER_RestoreSetup();
141 /**************************************************************************
142 * X11DRV_USER_BeginDebugging
144 void X11DRV_USER_BeginDebugging(void)
146 TSXUngrabServer(display);
147 TSXFlush(display);
150 /**************************************************************************
151 * X11DRV_USER_EndDebugging
153 void X11DRV_USER_EndDebugging(void)
157 /***********************************************************************
158 * X11DRV_USER_GetResource
160 * Fetch the value of resource 'name' using the correct instance name.
161 * 'name' must begin with '.' or '*'
163 static int X11DRV_USER_GetResource( XrmDatabase db, char *name, XrmValue *value )
165 char *buff_instance, *buff_class;
166 char *dummy;
167 int retval;
169 buff_instance = (char *)xmalloc(strlen(Options.programName)+strlen(name)+1);
170 buff_class = (char *)xmalloc( strlen(WINE_CLASS) + strlen(name) + 1 );
172 strcpy( buff_instance, Options.programName );
173 strcat( buff_instance, name );
174 strcpy( buff_class, WINE_CLASS );
175 strcat( buff_class, name );
176 retval = TSXrmGetResource( db, buff_instance, buff_class, &dummy, value );
177 free( buff_instance );
178 free( buff_class );
179 return retval;
182 /***********************************************************************
183 * X11DRV_USER_ParseOptions
184 * Parse command line options and open display.
186 void X11DRV_USER_ParseOptions(int *argc, char *argv[])
188 int i;
189 char *display_name = NULL;
190 XrmValue value;
191 XrmDatabase db = TSXrmGetFileDatabase(WINE_APP_DEFAULTS);
192 char *xrm_string;
194 /* Get display name from command line */
195 for (i = 1; i < *argc; i++)
197 if (!strcmp( argv[i], "-display" )) display_name = argv[i+1];
200 /* Open display */
202 if (display_name == NULL &&
203 X11DRV_USER_GetResource( db, ".display", &value )) display_name = value.addr;
205 if (!(display = TSXOpenDisplay( display_name )))
207 MESSAGE( "%s: Can't open display: %s\n",
208 argv[0], display_name ? display_name : "(none specified)" );
209 exit(1);
212 /* tell the libX11 that we will do input method handling ourselves
213 * that keep libX11 from doing anything whith dead keys, allowing Wine
214 * to have total control over dead keys, that is this line allows
215 * them to work in Wine, even whith a libX11 including the dead key
216 * patches from Th.Quinot (http://Web.FdN.FR/~tquinot/dead-keys.en.html)
218 TSXOpenIM(display,NULL,NULL,NULL);
220 /* Merge file and screen databases */
221 if ((xrm_string = TSXResourceManagerString( display )) != NULL)
223 XrmDatabase display_db = TSXrmGetStringDatabase( xrm_string );
224 TSXrmMergeDatabases( display_db, &db );
227 /* Parse command line */
228 TSXrmParseCommand( &db, optionsTable, NB_OPTIONS,
229 Options.programName, argc, argv );
231 /* Get all options */
232 if (X11DRV_USER_GetResource( db, ".iconic", &value ))
233 Options.cmdShow = SW_SHOWMINIMIZED;
234 if (X11DRV_USER_GetResource( db, ".privatemap", &value ))
235 Options.usePrivateMap = TRUE;
236 if (X11DRV_USER_GetResource( db, ".fixedmap", &value ))
237 Options.useFixedMap = TRUE;
238 if (X11DRV_USER_GetResource( db, ".synchronous", &value ))
239 Options.synchronous = TRUE;
240 if (X11DRV_USER_GetResource( db, ".backingstore", &value ))
241 Options.backingstore = TRUE;
242 if (X11DRV_USER_GetResource( db, ".debug", &value ))
243 Options.debug = TRUE;
244 if (X11DRV_USER_GetResource( db, ".failreadonly", &value ))
245 Options.failReadOnly = TRUE;
246 if (X11DRV_USER_GetResource( db, ".perfect", &value ))
247 Options.perfectGraphics = TRUE;
248 if (X11DRV_USER_GetResource( db, ".depth", &value))
249 Options.screenDepth = atoi( value.addr );
250 if (X11DRV_USER_GetResource( db, ".desktop", &value))
251 Options.desktopGeometry = value.addr;
252 if (X11DRV_USER_GetResource( db, ".language", &value))
253 MAIN_ParseLanguageOption( (char *)value.addr );
254 if (X11DRV_USER_GetResource( db, ".managed", &value))
255 Options.managed = TRUE;
256 if (X11DRV_USER_GetResource( db, ".mode", &value))
257 MAIN_ParseModeOption( (char *)value.addr );
258 if (X11DRV_USER_GetResource( db, ".debugoptions", &value))
259 MAIN_ParseDebugOptions((char*)value.addr);
260 if (X11DRV_USER_GetResource( db, ".debugmsg", &value))
261 MAIN_ParseDebugOptions((char*)value.addr);
263 if (X11DRV_USER_GetResource( db, ".dll", &value))
265 if (Options.dllFlags)
267 /* don't overwrite previous value. Should we
268 * automatically add the ',' between multiple DLLs ?
270 MESSAGE("Only one -dll flag is allowed. Use ',' between multiple DLLs\n");
272 else Options.dllFlags = xstrdup((char *)value.addr);
275 if (X11DRV_USER_GetResource( db, ".winver", &value))
276 VERSION_ParseWinVersion( (char*)value.addr );
277 if (X11DRV_USER_GetResource( db, ".dosver", &value))
278 VERSION_ParseDosVersion( (char*)value.addr );
279 if (X11DRV_USER_GetResource( db, ".config", &value))
280 Options.configFileName = xstrdup((char *)value.addr);
281 if (X11DRV_USER_GetResource( db, ".nodga", &value))
282 Options.noDGA = TRUE;
283 if (X11DRV_USER_GetResource( db, ".noxshm", &value))
284 Options.noXSHM = TRUE;
285 if (X11DRV_USER_GetResource( db, ".dxgrab", &value))
286 Options.DXGrab = TRUE;
287 if (X11DRV_USER_GetResource( db, ".console", &value))
288 driver.driver_list = xstrdup((char *)value.addr);
289 else
290 driver.driver_list = CONSOLE_DEFAULT_DRIVER;
293 /***********************************************************************
294 * X11DRV_USER_ErrorHandler
296 static int X11DRV_USER_ErrorHandler(Display *display, XErrorEvent *error_evt)
298 DebugBreak(); /* force an entry in the debugger */
299 return 0;
302 /***********************************************************************
303 * X11DRV_USER_Create
305 void X11DRV_USER_Create()
307 if (Options.synchronous) XSetErrorHandler( X11DRV_USER_ErrorHandler );
309 if (Options.desktopGeometry && Options.managed)
311 #if 0
312 MESSAGE( "%s: -managed and -desktop options cannot be used together\n",
313 Options.programName );
314 exit(1);
315 #else
316 Options.managed = FALSE;
317 #endif
321 /***********************************************************************
322 * X11DRV_USER_SaveSetup
324 void X11DRV_USER_SaveSetup()
326 TSXGetKeyboardControl(display, &X11DRV_XKeyboardState);
329 /***********************************************************************
330 * X11DRV_USER_RestoreSetup
332 void X11DRV_USER_RestoreSetup()
334 XKeyboardControl keyboard_value;
336 keyboard_value.key_click_percent = X11DRV_XKeyboardState.key_click_percent;
337 keyboard_value.bell_percent = X11DRV_XKeyboardState.bell_percent;
338 keyboard_value.bell_pitch = X11DRV_XKeyboardState.bell_pitch;
339 keyboard_value.bell_duration = X11DRV_XKeyboardState.bell_duration;
340 keyboard_value.auto_repeat_mode = X11DRV_XKeyboardState.global_auto_repeat;
342 XChangeKeyboardControl(display, KBKeyClickPercent | KBBellPercent |
343 KBBellPitch | KBBellDuration | KBAutoRepeatMode, &keyboard_value);
346 #endif /* X_DISPLAY_MISSING */