Added some basic Winsock2 definitions.
[wine] / windows / x11drv / main.c
1 /*
2  * X11 main driver
3  *
4  * Copyright 1998 Patrik Stridvall
5  *
6  */
7
8 #include "config.h"
9
10 #ifndef X_DISPLAY_MISSING
11
12 #include <X11/Xlocale.h>
13 #include "ts_xlib.h"
14 #include "ts_xresource.h"
15 #include "ts_xutil.h"
16
17 #include <signal.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <unistd.h>
21
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"
37 #include "win.h"
38
39 /**********************************************************************/
40
41 void X11DRV_USER_ParseOptions(int *argc, char *argv[]);
42 void X11DRV_USER_Create(void);
43 void X11DRV_USER_SaveSetup(void);
44 void X11DRV_USER_RestoreSetup(void);
45
46 /**********************************************************************/
47
48 #define WINE_CLASS        "Wine"    /* Class name for resources */
49 #define WINE_APP_DEFAULTS "/usr/lib/X11/app-defaults/Wine"
50
51 static XrmOptionDescRec optionsTable[] =
52 {
53     { "-backingstore",  ".backingstore",    XrmoptionNoArg,  (caddr_t)"on" },
54     { "-desktop",       ".desktop",         XrmoptionSepArg, (caddr_t)NULL },
55     { "-depth",         ".depth",           XrmoptionSepArg, (caddr_t)NULL },
56     { "-display",       ".display",         XrmoptionSepArg, (caddr_t)NULL },
57     { "-iconic",        ".iconic",          XrmoptionNoArg,  (caddr_t)"on" },
58     { "-language",      ".language",        XrmoptionSepArg, (caddr_t)"En" },
59     { "-name",          ".name",            XrmoptionSepArg, (caddr_t)NULL },
60     { "-perfect",       ".perfect",         XrmoptionNoArg,  (caddr_t)"on" },
61     { "-privatemap",    ".privatemap",      XrmoptionNoArg,  (caddr_t)"on" },
62     { "-fixedmap",      ".fixedmap",        XrmoptionNoArg,  (caddr_t)"on" },
63     { "-synchronous",   ".synchronous",     XrmoptionNoArg,  (caddr_t)"on" },
64     { "-debug",         ".debug",           XrmoptionNoArg,  (caddr_t)"on" },
65     { "-debugmsg",      ".debugmsg",        XrmoptionSepArg, (caddr_t)NULL },
66     { "-dll",           ".dll",             XrmoptionSepArg, (caddr_t)NULL },
67     { "-failreadonly",  ".failreadonly",    XrmoptionNoArg,  (caddr_t)"on" },
68     { "-mode",          ".mode",            XrmoptionSepArg, (caddr_t)NULL },
69     { "-managed",       ".managed",         XrmoptionNoArg,  (caddr_t)"off"},
70     { "-winver",        ".winver",          XrmoptionSepArg, (caddr_t)NULL },
71     { "-config",        ".config",          XrmoptionSepArg, (caddr_t)NULL },
72     { "-nodga",         ".nodga",           XrmoptionNoArg,  (caddr_t)"off"},
73     { "-noxshm",        ".noxshm",          XrmoptionNoArg,  (caddr_t)"off"},
74     { "-console",       ".console",         XrmoptionSepArg, (caddr_t)NULL },
75     { "-dosver",        ".dosver",          XrmoptionSepArg, (caddr_t)NULL }
76 };
77
78 #define NB_OPTIONS  (sizeof(optionsTable) / sizeof(optionsTable[0]))
79
80 /**********************************************************************/
81
82 static XKeyboardState X11DRV_XKeyboardState;
83 Display *display;
84
85 /***********************************************************************
86  *              X11DRV_GetXScreen
87  *
88  * Return the X screen associated to the current desktop.
89  */
90 Screen *X11DRV_GetXScreen()
91 {
92   return X11DRV_MONITOR_GetXScreen(&MONITOR_PrimaryMonitor);
93 }
94
95 /***********************************************************************
96  *              X11DRV_GetXRootWindow
97  *
98  * Return the X display associated to the current desktop.
99  */
100 Window X11DRV_GetXRootWindow()
101 {
102   return X11DRV_MONITOR_GetXRootWindow(&MONITOR_PrimaryMonitor);
103 }
104
105 /***********************************************************************
106  *              X11DRV_USER_Initialize
107  */
108 BOOL X11DRV_USER_Initialize(void)
109 {
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;
117
118   /* We need this before calling any Xlib function */
119   InitializeCriticalSection( &X11DRV_CritSection );
120   MakeCriticalSectionGlobal( &X11DRV_CritSection );
121   
122   TSXrmInitialize();
123   
124   putenv("XKB_DISABLE="); /* Disable XKB extension if present. */
125
126   X11DRV_USER_ParseOptions( Options.argc, Options.argv );
127   X11DRV_USER_Create();
128   X11DRV_USER_SaveSetup();
129
130   return TRUE;
131 }
132
133 /***********************************************************************
134  *              X11DRV_USER_Finalize
135  */
136 void X11DRV_USER_Finalize(void)
137 {
138   X11DRV_USER_RestoreSetup();
139 }
140
141 /**************************************************************************
142  *              X11DRV_USER_BeginDebugging
143  */
144 void X11DRV_USER_BeginDebugging(void)
145 {
146   TSXUngrabServer(display);
147   TSXFlush(display);
148 }
149
150 /**************************************************************************
151  *              X11DRV_USER_EndDebugging
152  */
153 void X11DRV_USER_EndDebugging(void)
154 {
155 }
156
157 /***********************************************************************
158  *               X11DRV_USER_GetResource
159  *
160  * Fetch the value of resource 'name' using the correct instance name.
161  * 'name' must begin with '.' or '*'
162  */
163 static int X11DRV_USER_GetResource( XrmDatabase db, char *name, XrmValue *value )
164 {
165   char *buff_instance, *buff_class;
166   char *dummy;
167   int retval;
168   
169   buff_instance = (char *)xmalloc(strlen(Options.programName)+strlen(name)+1);
170   buff_class    = (char *)xmalloc( strlen(WINE_CLASS) + strlen(name) + 1 );
171     
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;
180 }
181
182 /***********************************************************************
183  *              X11DRV_USER_ParseOptions
184  * Parse command line options and open display.
185  */
186 void X11DRV_USER_ParseOptions(int *argc, char *argv[])
187 {
188   int i;
189   char *display_name = NULL;
190   XrmValue value;
191   XrmDatabase db = TSXrmGetFileDatabase(WINE_APP_DEFAULTS);
192   char *xrm_string;
193
194   /* Get display name from command line */
195   for (i = 1; i < *argc; i++)
196     {
197       if (!strcmp( argv[i], "-display" )) display_name = argv[i+1];
198     }
199
200   /* Open display */
201   
202   if (display_name == NULL &&
203       X11DRV_USER_GetResource( db, ".display", &value )) display_name = value.addr;
204   
205   if (!(display = TSXOpenDisplay( display_name )))
206     {
207       MESSAGE( "%s: Can't open display: %s\n",
208            argv[0], display_name ? display_name : "(none specified)" );
209       exit(1);
210     }
211   
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)
217    */
218   TSXOpenIM(display,NULL,NULL,NULL);
219   
220   /* Merge file and screen databases */
221   if ((xrm_string = TSXResourceManagerString( display )) != NULL)
222     {
223       XrmDatabase display_db = TSXrmGetStringDatabase( xrm_string );
224       TSXrmMergeDatabases( display_db, &db );
225     }
226   
227   /* Parse command line */
228   TSXrmParseCommand( &db, optionsTable, NB_OPTIONS,
229                      Options.programName, argc, argv );
230   
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     {
262 #ifndef DEBUG_RUNTIME
263       MESSAGE("%s: Option \"-debugmsg\" not implemented.\n" \
264           "    Recompile with DEBUG_RUNTIME in include/debugtools.h defined.\n",
265           argv[0]);
266       exit(1);
267 #else
268       MAIN_ParseDebugOptions((char*)value.addr);
269 #endif
270     }
271   
272   if (X11DRV_USER_GetResource( db, ".dll", &value))
273   {
274       if (Options.dllFlags)
275       {
276           /* don't overwrite previous value. Should we
277            * automatically add the ',' between multiple DLLs ?
278            */
279           MESSAGE("Only one -dll flag is allowed. Use ',' between multiple DLLs\n");
280       }
281       else Options.dllFlags = xstrdup((char *)value.addr);
282   }
283   
284   if (X11DRV_USER_GetResource( db, ".winver", &value))
285     VERSION_ParseWinVersion( (char*)value.addr );
286   if (X11DRV_USER_GetResource( db, ".dosver", &value))
287     VERSION_ParseDosVersion( (char*)value.addr );
288   if (X11DRV_USER_GetResource( db, ".config", &value))
289     Options.configFileName = xstrdup((char *)value.addr);
290   if (X11DRV_USER_GetResource( db, ".nodga", &value))
291     Options.noDGA = TRUE;
292   if (X11DRV_USER_GetResource( db, ".noxshm", &value))
293     Options.noXSHM = TRUE;
294   if (X11DRV_USER_GetResource( db, ".console", &value))
295       driver.driver_list = xstrdup((char *)value.addr);
296   else
297       driver.driver_list = CONSOLE_DEFAULT_DRIVER;
298 }
299
300 /***********************************************************************
301  *              X11DRV_USER_ErrorHandler
302  */
303 static int X11DRV_USER_ErrorHandler(Display *display, XErrorEvent *error_evt)
304 {
305     DebugBreak();  /* force an entry in the debugger */
306     return 0;
307 }
308
309 /***********************************************************************
310  *              X11DRV_USER_Create
311  */
312 void X11DRV_USER_Create()
313 {
314   if (Options.synchronous) XSetErrorHandler( X11DRV_USER_ErrorHandler );
315   
316   if (Options.desktopGeometry && Options.managed)
317     {
318 #if 0
319       MESSAGE( "%s: -managed and -desktop options cannot be used together\n",
320            Options.programName );
321       exit(1);
322 #else
323       Options.managed = FALSE;
324 #endif
325     }
326 }
327
328 /***********************************************************************
329  *           X11DRV_USER_SaveSetup
330  */
331 void X11DRV_USER_SaveSetup()
332 {
333   TSXGetKeyboardControl(display, &X11DRV_XKeyboardState);
334 }
335
336 /***********************************************************************
337  *           X11DRV_USER_RestoreSetup
338  */
339 void X11DRV_USER_RestoreSetup()
340 {
341   XKeyboardControl keyboard_value;
342   
343   keyboard_value.key_click_percent      = X11DRV_XKeyboardState.key_click_percent;
344   keyboard_value.bell_percent   = X11DRV_XKeyboardState.bell_percent;
345   keyboard_value.bell_pitch             = X11DRV_XKeyboardState.bell_pitch;
346   keyboard_value.bell_duration  = X11DRV_XKeyboardState.bell_duration;
347   keyboard_value.auto_repeat_mode       = X11DRV_XKeyboardState.global_auto_repeat;
348   
349   XChangeKeyboardControl(display, KBKeyClickPercent | KBBellPercent | 
350                          KBBellPitch | KBBellDuration | KBAutoRepeatMode, &keyboard_value);
351 }
352
353 #endif /* X_DISPLAY_MISSING */