Release 980628
[wine] / misc / main.c
1 /*
2  * Main function.
3  *
4  * Copyright 1994 Alexandre Julliard
5  */
6
7 #include <stdlib.h>
8 #include <string.h>
9 #include <unistd.h>
10 #include <ctype.h>
11 /* #include <locale.h> */
12 #ifdef MALLOC_DEBUGGING
13 #include <malloc.h>
14 #endif
15 #include "ts_xlib.h"
16 #include "ts_xresource.h"
17 #include "ts_xutil.h"
18 #include <X11/Xlocale.h>
19 #include <X11/cursorfont.h>
20 #include "winsock.h"
21 #include "heap.h"
22 #include "message.h"
23 #include "msdos.h"
24 #include "windows.h"
25 #include "color.h"
26 #include "options.h"
27 #include "desktop.h"
28 #include "process.h"
29 #include "shell.h"
30 #include "winbase.h"
31 #include "debug.h"
32 #include "debugdefs.h"
33 #include "xmalloc.h"
34 #include "version.h"
35
36 const WINE_LANGUAGE_DEF Languages[] =
37 {
38     {"En",0x0409},      /* LANG_En */
39     {"Es",0x040A},      /* LANG_Es */
40     {"De",0x0407},      /* LANG_De */
41     {"No",0x0414},      /* LANG_No */
42     {"Fr",0x0400},      /* LANG_Fr */
43     {"Fi",0x040B},      /* LANG_Fi */
44     {"Da",0x0406},      /* LANG_Da */
45     {"Cz",0x0405},      /* LANG_Cz */
46     {"Eo",0x0425},      /* LANG_Eo */
47     {"It",0x0410},      /* LANG_It */
48     {"Ko",0x0412},      /* LANG_Ko */
49     {"Hu",0x0436},      /* LANG_Hu */
50     {"Pl",0x0415},      /* LANG_Pl */
51     {"Po",0x0416},      /* LANG_Po */
52     {"Sw",0x0417},      /* LANG_Sw */
53     {"Ca",0x0426},      /* LANG_Ca */
54     {NULL,0}
55 };
56
57 WORD WINE_LanguageId = 0;
58
59 #define WINE_CLASS    "Wine"    /* Class name for resources */
60
61 #define WINE_APP_DEFAULTS "/usr/lib/X11/app-defaults/Wine"
62
63 Display *display;
64 Screen *screen;
65 Window rootWindow;
66 int screenWidth = 0, screenHeight = 0;  /* Desktop window dimensions */
67 int screenDepth = 0;  /* Screen depth to use */
68
69 struct options Options =
70 {  /* default options */
71     NULL,           /* desktopGeometry */
72     NULL,           /* programName */
73     NULL,           /* argv0 */
74     NULL,           /* dllFlags */
75     FALSE,          /* usePrivateMap */
76     FALSE,          /* useFixedMap */
77     FALSE,          /* synchronous */
78     FALSE,          /* backing store */
79     SW_SHOWNORMAL,  /* cmdShow */
80     FALSE,
81     FALSE,          /* failReadOnly */
82     MODE_ENHANCED,  /* Enhanced mode */
83 #ifdef DEFAULT_LANG
84     DEFAULT_LANG,   /* Default language */
85 #else
86     LANG_En,
87 #endif
88     FALSE,          /* Managed windows */
89     FALSE           /* Perfect graphics */
90 };
91
92
93 static XrmOptionDescRec optionsTable[] =
94 {
95     { "-backingstore",  ".backingstore",    XrmoptionNoArg,  (caddr_t)"on" },
96     { "-desktop",       ".desktop",         XrmoptionSepArg, (caddr_t)NULL },
97     { "-depth",         ".depth",           XrmoptionSepArg, (caddr_t)NULL },
98     { "-display",       ".display",         XrmoptionSepArg, (caddr_t)NULL },
99     { "-iconic",        ".iconic",          XrmoptionNoArg,  (caddr_t)"on" },
100     { "-language",      ".language",        XrmoptionSepArg, (caddr_t)"En" },
101     { "-name",          ".name",            XrmoptionSepArg, (caddr_t)NULL },
102     { "-perfect",       ".perfect",         XrmoptionNoArg,  (caddr_t)"on" },
103     { "-privatemap",    ".privatemap",      XrmoptionNoArg,  (caddr_t)"on" },
104     { "-fixedmap",      ".fixedmap",        XrmoptionNoArg,  (caddr_t)"on" },
105     { "-synchronous",   ".synchronous",     XrmoptionNoArg,  (caddr_t)"on" },
106     { "-debug",         ".debug",           XrmoptionNoArg,  (caddr_t)"on" },
107     { "-debugmsg",      ".debugmsg",        XrmoptionSepArg, (caddr_t)NULL },
108     { "-dll",           ".dll",             XrmoptionSepArg, (caddr_t)NULL },
109     { "-failreadonly",  ".failreadonly",    XrmoptionNoArg,  (caddr_t)"on" },
110     { "-mode",          ".mode",            XrmoptionSepArg, (caddr_t)NULL },
111     { "-managed",       ".managed",         XrmoptionNoArg,  (caddr_t)"off"},
112     { "-winver",        ".winver",          XrmoptionSepArg, (caddr_t)NULL }
113 };
114
115 #define NB_OPTIONS  (sizeof(optionsTable) / sizeof(optionsTable[0]))
116
117 #define USAGE \
118   "%s\n" \
119   "Usage:  %s [options] \"program_name [arguments]\"\n" \
120   "\n" \
121   "Options:\n" \
122   "    -backingstore   Turn on backing store\n" \
123   "    -debug          Enter debugger before starting application\n" \
124   "    -debugmsg name  Turn debugging-messages on or off\n" \
125   "    -depth n        Change the depth to use for multiple-depth screens\n" \
126   "    -desktop geom   Use a desktop window of the given geometry\n" \
127   "    -display name   Use the specified display\n" \
128   "    -dll name       Enable or disable built-in DLLs\n" \
129   "    -failreadonly   Read only files may not be opened in write mode\n" \
130   "    -fixedmap       Use a \"standard\" color map\n" \
131   "    -help           Show this help message\n" \
132   "    -iconic         Start as an icon\n" \
133   "    -language xx    Set the language (one of En,Es,De,No,Fr,Fi,Da,Cz,Eo,It,Ko,\n                    Hu,Pl,Po,Sw,Ca)\n" \
134   "    -managed        Allow the window manager to manage created windows\n" \
135   "    -mode mode      Start Wine in a particular mode (standard or enhanced)\n" \
136   "    -name name      Set the application name\n" \
137   "    -perfect        Favor correctness over speed for graphical operations\n" \
138   "    -privatemap     Use a private color map\n" \
139   "    -synchronous    Turn on synchronous display mode\n" \
140   "    -version        Display the Wine version\n" \
141   "    -winver         Version to imitate (one of win31,win95,nt351,nt40)\n"
142
143
144
145 /***********************************************************************
146  *           MAIN_Usage
147  */
148 void MAIN_Usage( char *name )
149 {
150     MSG( USAGE, WINE_RELEASE_INFO, name );
151     exit(1);
152 }
153
154
155 /***********************************************************************
156  *           MAIN_GetProgramName
157  *
158  * Get the program name. The name is specified by (in order of precedence):
159  * - the option '-name'.
160  * - the environment variable 'WINE_NAME'.
161  * - the last component of argv[0].
162  */
163 static char *MAIN_GetProgramName( int argc, char *argv[] )
164 {
165     int i;
166     char *p;
167
168     for (i = 1; i < argc-1; i++)
169         if (!strcmp( argv[i], "-name" )) return argv[i+1];
170     if ((p = getenv( "WINE_NAME" )) != NULL) return p;
171     if ((p = strrchr( argv[0], '/' )) != NULL) return p+1;
172     return argv[0];
173 }
174
175
176 /***********************************************************************
177  *           MAIN_GetResource
178  *
179  * Fetch the value of resource 'name' using the correct instance name.
180  * 'name' must begin with '.' or '*'
181  */
182 static int MAIN_GetResource( XrmDatabase db, char *name, XrmValue *value )
183 {
184     char *buff_instance, *buff_class;
185     char *dummy;
186     int retval;
187
188     buff_instance = (char *)xmalloc(strlen(Options.programName)+strlen(name)+1);
189     buff_class    = (char *)xmalloc( strlen(WINE_CLASS) + strlen(name) + 1 );
190
191     strcpy( buff_instance, Options.programName );
192     strcat( buff_instance, name );
193     strcpy( buff_class, WINE_CLASS );
194     strcat( buff_class, name );
195     retval = TSXrmGetResource( db, buff_instance, buff_class, &dummy, value );
196     free( buff_instance );
197     free( buff_class );
198     return retval;
199 }
200
201
202 /***********************************************************************
203  *          MAIN_ParseDebugOptions
204  *
205  *  Turns specific debug messages on or off, according to "options".
206  *  
207  *  RETURNS
208  *    TRUE if parsing was successful
209  */
210 static BOOL32 MAIN_ParseDebugOptions(char *options)
211 {
212   /* defined in relay32/relay386.c */
213   extern char **debug_relay_includelist;
214   extern char **debug_relay_excludelist;
215   /* defined in relay32/snoop.c */
216   extern char **debug_snoop_includelist;
217   extern char **debug_snoop_excludelist;
218
219   int l, cls;
220   if (strlen(options)<3)
221     return FALSE;
222   do
223   {
224     if ((*options!='+')&&(*options!='-')){
225       int j;
226
227       for(j=0; j<DEBUG_CLASS_COUNT; j++)
228         if(!lstrncmpi32A(options, debug_cl_name[j], strlen(debug_cl_name[j])))
229           break;
230       if(j==DEBUG_CLASS_COUNT)
231         return FALSE;
232       options += strlen(debug_cl_name[j]);
233       if ((*options!='+')&&(*options!='-'))
234         return FALSE;
235       cls = j;
236     }
237     else
238       cls = -1; /* all classes */
239
240     if (strchr(options,','))
241       l=strchr(options,',')-options;
242     else
243       l=strlen(options);
244
245     if (!lstrncmpi32A(options+1,"all",l-1))
246       {
247         int i, j;
248         for (i=0; i<DEBUG_CHANNEL_COUNT; i++)
249           for(j=0; j<DEBUG_CLASS_COUNT; j++)
250             if(cls == -1 || cls == j)
251               debug_msg_enabled[i][j]=(*options=='+');
252       }
253     else if (!lstrncmpi32A(options+1, "relay=", 6) ||
254              !lstrncmpi32A(options+1, "snoop=", 6))
255       {
256         int i, j;
257         char *s, *s2, ***output, c;
258
259         for (i=0; i<DEBUG_CHANNEL_COUNT; i++)
260           if (debug_ch_name && (!lstrncmpi32A(debug_ch_name[i],options+1,5))){
261             for(j=0; j<DEBUG_CLASS_COUNT; j++)
262               if(cls == -1 || cls == j)
263                 debug_msg_enabled[i][j]=TRUE;
264             break;
265           }
266         /* should never happen, maybe assert(i!=DEBUG_CHANNEL_COUNT)? */
267         if (i==DEBUG_CHANNEL_COUNT)
268           return FALSE;
269         output = (*options == '+') ?
270                         ((*(options+1) == 'r') ?
271                                 &debug_relay_includelist :
272                                 &debug_snoop_includelist) :
273                         ((*(options+1) == 'r') ?
274                                 &debug_relay_excludelist :
275                                 &debug_snoop_excludelist);
276         s = options + 7;
277         i = 1;
278         while((s = strchr(s, ':'))) i++, s++;
279         *output = malloc(sizeof(char **) * i + 1);
280         i = 0;
281         s = options + 7;
282         while((s2 = strchr(s, ':'))) {
283           c = *s2;
284           *s2 = '\0';
285           *((*output)+i) = strdup(s);
286           *s2 = c;
287           s = s2 + 1;
288           i++;
289         }
290         c = *(options + l);
291         *(options + l) = '\0';
292         *((*output)+i) = strdup(s);
293         *(options + l) = c;
294         *((*output)+i+1) = NULL;
295       }
296     else
297       {
298         int i, j;
299         for (i=0; i<DEBUG_CHANNEL_COUNT; i++)
300           if (debug_ch_name && (!lstrncmpi32A(options+1,debug_ch_name[i],l-1))){
301             for(j=0; j<DEBUG_CLASS_COUNT; j++)
302               if(cls == -1 || cls == j)
303                 debug_msg_enabled[i][j]=(*options=='+');
304             break;
305           }
306         if (i==DEBUG_CHANNEL_COUNT)
307           return FALSE;
308       }
309     options+=l;
310   }
311   while((*options==',')&&(*(++options)));
312   if (*options)
313     return FALSE;
314   else
315     return TRUE;
316 }
317
318 /***********************************************************************
319  *           MAIN_ParseLanguageOption
320  *
321  * Parse -language option.
322  */
323 static void MAIN_ParseLanguageOption( char *arg )
324 {
325     const WINE_LANGUAGE_DEF *p = Languages;
326
327     Options.language = LANG_En;  /* First language */
328     for (;p->name;p++)
329     {
330         if (!lstrcmpi32A( p->name, arg ))
331         {
332             WINE_LanguageId = p->langid;
333             return;
334         }
335         Options.language++;
336     }
337     MSG( "Invalid language specified '%s'. Supported languages are: ", arg );
338     for (p = Languages; p->name; p++) MSG( "%s ", p->name );
339     MSG( "\n" );
340     exit(1);
341 }
342
343
344 /***********************************************************************
345  *           MAIN_ParseModeOption
346  *
347  * Parse -mode option.
348  */
349 static void MAIN_ParseModeOption( char *arg )
350 {
351     if (!lstrcmpi32A("enhanced", arg)) Options.mode = MODE_ENHANCED;
352     else if (!lstrcmpi32A("standard", arg)) Options.mode = MODE_STANDARD;
353     else
354     {
355         MSG( "Invalid mode '%s' specified.\n", arg);
356         MSG( "Valid modes are: 'standard', 'enhanced' (default).\n");
357         exit(1);
358     }
359 }
360
361 /***********************************************************************
362  *           MAIN_ParseOptions
363  *
364  * Parse command line options and open display.
365  */
366 static void MAIN_ParseOptions( int *argc, char *argv[] )
367 {
368     char *display_name = NULL;
369     XrmValue value;
370     XrmDatabase db = TSXrmGetFileDatabase(WINE_APP_DEFAULTS);
371     int i;
372     char *xrm_string;
373
374     Options.programName = MAIN_GetProgramName( *argc, argv );
375     Options.argv0 = argv[0];
376
377       /* Get display name from command line */
378     for (i = 1; i < *argc; i++)
379     {
380         if (!strcmp( argv[i], "-display" )) display_name = argv[i+1];
381         if (!strcmp( argv[i], "-v" ) || !strcmp( argv[i], "-version" ))
382         {
383             MSG( "%s\n", WINE_RELEASE_INFO );
384             exit(0);
385         }
386         if (!strcmp( argv[i], "-h" ) || !strcmp( argv[i], "-help" ))
387         {
388             MAIN_Usage(argv[0]);
389             exit(0);
390         }
391     }
392
393       /* Open display */
394
395     if (display_name == NULL &&
396         MAIN_GetResource( db, ".display", &value )) display_name = value.addr;
397
398     if (!(display = TSXOpenDisplay( display_name )))
399     {
400         MSG( "%s: Can't open display: %s\n",
401                  argv[0], display_name ? display_name : "(none specified)" );
402         exit(1);
403     }
404
405       /* Merge file and screen databases */
406     if ((xrm_string = TSXResourceManagerString( display )) != NULL)
407     {
408         XrmDatabase display_db = TSXrmGetStringDatabase( xrm_string );
409         TSXrmMergeDatabases( display_db, &db );
410     }
411
412       /* Parse command line */
413     TSXrmParseCommand( &db, optionsTable, NB_OPTIONS,
414                      Options.programName, argc, argv );
415
416       /* Get all options */
417     if (MAIN_GetResource( db, ".iconic", &value ))
418         Options.cmdShow = SW_SHOWMINIMIZED;
419     if (MAIN_GetResource( db, ".privatemap", &value ))
420         Options.usePrivateMap = TRUE;
421     if (MAIN_GetResource( db, ".fixedmap", &value ))
422         Options.useFixedMap = TRUE;
423     if (MAIN_GetResource( db, ".synchronous", &value ))
424         Options.synchronous = TRUE;
425     if (MAIN_GetResource( db, ".backingstore", &value ))
426         Options.backingstore = TRUE;    
427     if (MAIN_GetResource( db, ".debug", &value ))
428         Options.debug = TRUE;
429     if (MAIN_GetResource( db, ".failreadonly", &value ))
430         Options.failReadOnly = TRUE;
431     if (MAIN_GetResource( db, ".perfect", &value ))
432         Options.perfectGraphics = TRUE;
433     if (MAIN_GetResource( db, ".depth", &value))
434         screenDepth = atoi( value.addr );
435     if (MAIN_GetResource( db, ".desktop", &value))
436         Options.desktopGeometry = value.addr;
437     if (MAIN_GetResource( db, ".language", &value))
438         MAIN_ParseLanguageOption( (char *)value.addr );
439     if (MAIN_GetResource( db, ".managed", &value))
440         Options.managed = TRUE;
441     if (MAIN_GetResource( db, ".mode", &value))
442         MAIN_ParseModeOption( (char *)value.addr );
443     if (MAIN_GetResource( db, ".debugoptions", &value))
444         MAIN_ParseDebugOptions((char*)value.addr);
445     if (MAIN_GetResource( db, ".debugmsg", &value))
446       {
447 #ifndef DEBUG_RUNTIME
448         MSG("%s: Option \"-debugmsg\" not implemented.\n" \
449           "    Recompile with DEBUG_RUNTIME in include/debugtools.h defined.\n",
450           argv[0]);
451         exit(1);
452 #else
453         if (MAIN_ParseDebugOptions((char*)value.addr)==FALSE)
454           {
455             int i;
456             MSG("%s: Syntax: -debugmsg [class]+xxx,...  or "
457                     "-debugmsg [class]-xxx,...\n",argv[0]);
458             MSG("Example: -debugmsg +all,warn-heap\n"
459                     "  turn on all messages except warning heap messages\n");
460             MSG("Special case: -debugmsg +relay=DLL:DLL.###:FuncName\n"
461                 "  turn on -debugmsg +relay only as specified\n"
462                 "Special case: -debugmsg -relay=DLL:DLL.###:FuncName\n"
463                 "  turn on -debugmsg +relay except as specified\n"
464                 "Also permitted, +snoop=..., -snoop=... as with relay.\n\n");
465
466             MSG("Available message classes:\n");
467             for(i=0;i<DEBUG_CLASS_COUNT;i++)
468               MSG( "%-9s", debug_cl_name[i]);
469             MSG("\n\n");
470
471             MSG("Available message types:\n");
472             MSG("%-9s ","all");
473             for(i=0;i<DEBUG_CHANNEL_COUNT;i++)
474               if(debug_ch_name[i])
475                 MSG("%-9s%c",debug_ch_name[i],
476                         (((i+2)%8==0)?'\n':' '));
477             MSG("\n\n");
478             exit(1);
479           }
480 #endif
481       }
482
483       if (MAIN_GetResource( db, ".dll", &value))
484       {
485           /* Hack: store option value in Options to be retrieved */
486           /* later on inside the emulator code. */
487           if (!__winelib) Options.dllFlags = xstrdup((char *)value.addr);
488           else
489           {
490               MSG("-dll not supported in Winelib\n" );
491               exit(1);
492           }
493       }
494
495       if (MAIN_GetResource( db, ".winver", &value))
496           VERSION_ParseVersion( (char*)value.addr );
497 }
498
499
500 /***********************************************************************
501  *           MAIN_CreateDesktop
502  */
503 static void MAIN_CreateDesktop( int argc, char *argv[] )
504 {
505     int x, y, flags;
506     unsigned int width = 640, height = 480;  /* Default size = 640x480 */
507     char *name = "Wine desktop";
508     XSizeHints *size_hints;
509     XWMHints   *wm_hints;
510     XClassHint *class_hints;
511     XSetWindowAttributes win_attr;
512     XTextProperty window_name;
513     Atom XA_WM_DELETE_WINDOW;
514
515     flags = TSXParseGeometry( Options.desktopGeometry, &x, &y, &width, &height );
516     screenWidth  = width;
517     screenHeight = height;
518
519       /* Create window */
520
521     win_attr.event_mask = ExposureMask | KeyPressMask | KeyReleaseMask |
522                          PointerMotionMask | ButtonPressMask |
523                          ButtonReleaseMask | EnterWindowMask;
524     win_attr.cursor = TSXCreateFontCursor( display, XC_top_left_arrow );
525
526     rootWindow = TSXCreateWindow( display, DefaultRootWindow(display),
527                                 x, y, width, height, 0,
528                                 CopyFromParent, InputOutput, CopyFromParent,
529                                 CWEventMask | CWCursor, &win_attr );
530
531       /* Set window manager properties */
532
533     size_hints  = TSXAllocSizeHints();
534     wm_hints    = TSXAllocWMHints();
535     class_hints = TSXAllocClassHint();
536     if (!size_hints || !wm_hints || !class_hints)
537     {
538         MSG("Not enough memory for window manager hints.\n" );
539         exit(1);
540     }
541     size_hints->min_width = size_hints->max_width = width;
542     size_hints->min_height = size_hints->max_height = height;
543     size_hints->flags = PMinSize | PMaxSize;
544     if (flags & (XValue | YValue)) size_hints->flags |= USPosition;
545     if (flags & (WidthValue | HeightValue)) size_hints->flags |= USSize;
546     else size_hints->flags |= PSize;
547
548     wm_hints->flags = InputHint | StateHint;
549     wm_hints->input = True;
550     wm_hints->initial_state = NormalState;
551     class_hints->res_name = argv[0];
552     class_hints->res_class = "Wine";
553
554     TSXStringListToTextProperty( &name, 1, &window_name );
555     TSXSetWMProperties( display, rootWindow, &window_name, &window_name,
556                       argv, argc, size_hints, wm_hints, class_hints );
557     XA_WM_DELETE_WINDOW = TSXInternAtom( display, "WM_DELETE_WINDOW", False );
558     TSXSetWMProtocols( display, rootWindow, &XA_WM_DELETE_WINDOW, 1 );
559     TSXFree( size_hints );
560     TSXFree( wm_hints );
561     TSXFree( class_hints );
562
563       /* Map window */
564
565     TSXMapWindow( display, rootWindow );
566 }
567
568
569 XKeyboardState keyboard_state;
570
571 /***********************************************************************
572  *           MAIN_SaveSetup
573  */
574 static void MAIN_SaveSetup(void)
575 {
576     TSXGetKeyboardControl(display, &keyboard_state);
577 }
578
579 /***********************************************************************
580  *           MAIN_RestoreSetup
581  */
582 static void MAIN_RestoreSetup(void)
583 {
584     XKeyboardControl keyboard_value;
585
586     keyboard_value.key_click_percent    = keyboard_state.key_click_percent;
587     keyboard_value.bell_percent         = keyboard_state.bell_percent;
588     keyboard_value.bell_pitch           = keyboard_state.bell_pitch;
589     keyboard_value.bell_duration        = keyboard_state.bell_duration;
590     keyboard_value.auto_repeat_mode     = keyboard_state.global_auto_repeat;
591
592     XChangeKeyboardControl(display, KBKeyClickPercent | KBBellPercent | 
593         KBBellPitch | KBBellDuration | KBAutoRepeatMode, &keyboard_value);
594 }
595
596
597 /***********************************************************************
598  *           called_at_exit
599  */
600 static void called_at_exit(void)
601 {
602     MAIN_RestoreSetup();
603     COLOR_Cleanup();
604     WINSOCK_Shutdown();
605     /* FIXME: should check for other processes or threads */
606     DeleteCriticalSection( HEAP_SystemLock );
607 }
608
609 /***********************************************************************
610  *           MAIN_WineInit
611  *
612  * Wine initialisation and command-line parsing
613  */
614 BOOL32 MAIN_WineInit( int *argc, char *argv[] )
615 {    
616     int depth_count, i;
617     int *depth_list;
618     struct timeval tv;
619
620 #ifdef MALLOC_DEBUGGING
621     char *trace;
622
623     mcheck(NULL);
624     if (!(trace = getenv("MALLOC_TRACE")))
625     {       
626         MSG( "MALLOC_TRACE not set. No trace generated\n" );
627     }
628     else
629     {
630         MSG( "malloc trace goes to %s\n", trace );
631         mtrace();
632     }
633 #endif
634
635     setbuf(stdout,NULL);
636     setbuf(stderr,NULL);
637
638     setlocale(LC_CTYPE,"");
639     gettimeofday( &tv, NULL);
640     MSG_WineStartTicks = (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
641
642     /* We need this before calling any Xlib function */
643     InitializeCriticalSection( &X11DRV_CritSection );
644
645     TSXrmInitialize();
646
647     putenv("XKB_DISABLE="); /* Disable XKB extension if present. */
648
649     MAIN_ParseOptions( argc, argv );
650
651     if (Options.desktopGeometry && Options.managed)
652     {
653 #if 0
654         MSG( "%s: -managed and -desktop options cannot be used together\n",
655                  Options.programName );
656         exit(1);
657 #else
658         Options.managed = FALSE;
659 #endif
660     }
661
662     screen       = DefaultScreenOfDisplay( display );
663     screenWidth  = WidthOfScreen( screen );
664     screenHeight = HeightOfScreen( screen );
665     if (screenDepth)  /* -depth option specified */
666     {
667         depth_list = TSXListDepths(display,DefaultScreen(display),&depth_count);
668         for (i = 0; i < depth_count; i++)
669             if (depth_list[i] == screenDepth) break;
670         TSXFree( depth_list );
671         if (i >= depth_count)
672         {
673             MSG( "%s: Depth %d not supported on this screen.\n",
674                               Options.programName, screenDepth );
675             exit(1);
676         }
677     }
678     else screenDepth  = DefaultDepthOfScreen( screen );
679     if (Options.synchronous) TSXSynchronize( display, True );
680     if (Options.desktopGeometry) MAIN_CreateDesktop( *argc, argv );
681     else rootWindow = DefaultRootWindow( display );
682
683     MAIN_SaveSetup();
684     atexit(called_at_exit);
685     return TRUE;
686 }
687
688
689 /***********************************************************************
690  *           MessageBeep16   (USER.104)
691  */
692 void WINAPI MessageBeep16( UINT16 i )
693 {
694     MessageBeep32( i );
695 }
696
697
698 /***********************************************************************
699  *           MessageBeep32   (USER32.390)
700  */
701 BOOL32 WINAPI MessageBeep32( UINT32 i )
702 {
703     TSXBell( display, 0 );
704     return TRUE;
705 }
706
707
708 /***********************************************************************
709  *           Beep   (KERNEL32.11)
710  */
711 BOOL32 WINAPI Beep( DWORD dwFreq, DWORD dwDur )
712 {
713     /* dwFreq and dwDur are ignored by Win95 */
714     TSXBell(display, 0);
715     return TRUE;
716 }
717
718
719 /***********************************************************************
720  *      GetTimerResolution (USER.14)
721  */
722 LONG WINAPI GetTimerResolution(void)
723 {
724         return (1000);
725 }
726
727 /***********************************************************************
728  *      SystemParametersInfo32A   (USER32.540)
729  */
730 BOOL32 WINAPI SystemParametersInfo32A( UINT32 uAction, UINT32 uParam,
731                                        LPVOID lpvParam, UINT32 fuWinIni )
732 {
733         int timeout, temp;
734         XKeyboardState          keyboard_state;
735
736         switch (uAction) {
737         case SPI_GETBEEP:
738                 TSXGetKeyboardControl(display, &keyboard_state);
739                 if (keyboard_state.bell_percent == 0)
740                         *(BOOL32 *) lpvParam = FALSE;
741                 else
742                         *(BOOL32 *) lpvParam = TRUE;
743                 break;
744
745         case SPI_GETBORDER:
746                 *(INT32 *)lpvParam = GetSystemMetrics32( SM_CXFRAME );
747                 break;
748
749         case SPI_GETFASTTASKSWITCH:
750                 if ( GetProfileInt32A( "windows", "CoolSwitch", 1 ) == 1 )
751                         *(BOOL32 *) lpvParam = TRUE;
752                 else
753                         *(BOOL32 *) lpvParam = FALSE;
754                 break;
755
756         case SPI_GETDRAGFULLWINDOWS:
757           *(BOOL32 *) lpvParam = FALSE;
758                 
759         case SPI_GETGRIDGRANULARITY:
760                 *(INT32*)lpvParam=GetProfileInt32A("desktop","GridGranularity",1);
761                 break;
762
763         case SPI_GETICONTITLEWRAP:
764                 *(BOOL32*)lpvParam=GetProfileInt32A("desktop","IconTitleWrap",TRUE);
765                 break;
766
767         case SPI_GETKEYBOARDDELAY:
768                 *(INT32*)lpvParam=GetProfileInt32A("keyboard","KeyboardDelay",1);
769                 break;
770
771         case SPI_GETKEYBOARDSPEED:
772                 *(DWORD*)lpvParam=GetProfileInt32A("keyboard","KeyboardSpeed",30);
773                 break;
774
775         case SPI_GETMENUDROPALIGNMENT:
776                 *(BOOL32*)lpvParam=GetSystemMetrics32(SM_MENUDROPALIGNMENT); /* XXX check this */
777                 break;
778
779         case SPI_GETSCREENSAVEACTIVE:
780                 if ( GetProfileInt32A( "windows", "ScreenSaveActive", 1 ) == 1 )
781                         *(BOOL32*)lpvParam = TRUE;
782                 else
783                         *(BOOL32*)lpvParam = FALSE;
784                 break;
785
786         case SPI_GETSCREENSAVETIMEOUT:
787         /* FIXME GetProfileInt( "windows", "ScreenSaveTimeout", 300 ); */
788                 TSXGetScreenSaver(display, &timeout, &temp,&temp,&temp);
789                 *(INT32 *) lpvParam = timeout * 1000;
790                 break;
791
792         case SPI_ICONHORIZONTALSPACING:
793                 /* FIXME Get/SetProfileInt */
794                 if (lpvParam == NULL)
795                         /*SetSystemMetrics( SM_CXICONSPACING, uParam )*/ ;
796                 else
797                         *(INT32*)lpvParam=GetSystemMetrics32(SM_CXICONSPACING);
798                 break;
799
800         case SPI_ICONVERTICALSPACING:
801                 /* FIXME Get/SetProfileInt */
802                 if (lpvParam == NULL)
803                         /*SetSystemMetrics( SM_CYICONSPACING, uParam )*/ ;
804                 else
805                         *(INT32*)lpvParam=GetSystemMetrics32(SM_CYICONSPACING);
806                 break;
807
808         case SPI_GETICONTITLELOGFONT: {
809                 LPLOGFONT32A lpLogFont = (LPLOGFONT32A)lpvParam;
810
811                 /* from now on we always have an alias for MS Sans Serif */
812
813                 GetProfileString32A("Desktop", "IconTitleFaceName", "MS Sans Serif", 
814                         lpLogFont->lfFaceName, LF_FACESIZE );
815                 lpLogFont->lfHeight = -GetProfileInt32A("Desktop","IconTitleSize", 8);
816                 lpLogFont->lfWidth = 0;
817                 lpLogFont->lfEscapement = lpLogFont->lfOrientation = 0;
818                 lpLogFont->lfWeight = FW_NORMAL;
819                 lpLogFont->lfItalic = FALSE;
820                 lpLogFont->lfStrikeOut = FALSE;
821                 lpLogFont->lfUnderline = FALSE;
822                 lpLogFont->lfCharSet = ANSI_CHARSET;
823                 lpLogFont->lfOutPrecision = OUT_DEFAULT_PRECIS;
824                 lpLogFont->lfClipPrecision = CLIP_DEFAULT_PRECIS;
825                 lpLogFont->lfPitchAndFamily = DEFAULT_PITCH | FF_SWISS;
826                 break;
827         }
828         case SPI_GETWORKAREA:
829                 SetRect32( (RECT32 *)lpvParam, 0, 0,
830                         GetSystemMetrics32( SM_CXSCREEN ),
831                         GetSystemMetrics32( SM_CYSCREEN )
832                 );
833                 break;
834         case SPI_GETNONCLIENTMETRICS: 
835
836 #define lpnm ((LPNONCLIENTMETRICS32A)lpvParam)
837                 
838                 if( lpnm->cbSize == sizeof(NONCLIENTMETRICS32A) )
839                 {
840                     /* FIXME: initialize geometry entries */
841
842                     SystemParametersInfo32A(SPI_GETICONTITLELOGFONT, 0,
843                                                         (LPVOID)&(lpnm->lfCaptionFont),0);
844                     lpnm->lfCaptionFont.lfWeight = FW_BOLD;
845                     SystemParametersInfo32A(SPI_GETICONTITLELOGFONT, 0,
846                                                         (LPVOID)&(lpnm->lfMenuFont),0);
847                     SystemParametersInfo32A(SPI_GETICONTITLELOGFONT, 0,
848                                                         (LPVOID)&(lpnm->lfStatusFont),0);
849                     SystemParametersInfo32A(SPI_GETICONTITLELOGFONT, 0,
850                                                         (LPVOID)&(lpnm->lfMessageFont),0);
851                 }
852 #undef lpnm
853                 break;
854
855         case SPI_GETANIMATION: {
856                 LPANIMATIONINFO lpAnimInfo = (LPANIMATIONINFO)lpvParam;
857  
858                 /* Tell it "disabled" */
859                 lpAnimInfo->cbSize = sizeof(ANIMATIONINFO);
860                 uParam = sizeof(ANIMATIONINFO);
861                 lpAnimInfo->iMinAnimate = 0; /* Minimise and restore animation is disabled (nonzero == enabled) */
862                 break;
863         }
864  
865         case SPI_SETANIMATION: {
866                 LPANIMATIONINFO lpAnimInfo = (LPANIMATIONINFO)lpvParam;
867  
868                 /* Do nothing */
869                 WARN(system, "SPI_SETANIMATION ignored.\n");
870                 lpAnimInfo->cbSize = sizeof(ANIMATIONINFO);
871                 uParam = sizeof(ANIMATIONINFO);
872                 break;
873         }
874
875         case SPI_GETHIGHCONTRAST:
876         {
877                 LPHIGHCONTRASTA lpHighContrastA = (LPHIGHCONTRASTA)lpvParam;
878
879                 FIXME(system,"SPI_GETHIGHCONTRAST not fully implemented\n");
880
881                 if ( lpHighContrastA->cbSize == sizeof( HIGHCONTRASTA ) )
882                 {
883                         /* Indicate that there is no high contrast available */
884                         lpHighContrastA->dwFlags = 0;
885                         lpHighContrastA->lpszDefaultScheme = NULL;
886                 }
887                 else
888                 {
889                         return FALSE;
890                 }
891
892                 break;
893         }
894
895         default:
896                 return SystemParametersInfo16(uAction,uParam,lpvParam,fuWinIni);
897         }
898         return TRUE;
899 }
900
901
902 /***********************************************************************
903  *      SystemParametersInfo16   (USER.483)
904  */
905 BOOL16 WINAPI SystemParametersInfo16( UINT16 uAction, UINT16 uParam,
906                                       LPVOID lpvParam, UINT16 fuWinIni )
907 {
908         int timeout, temp;
909         char buffer[256];
910         XKeyboardState          keyboard_state;
911         XKeyboardControl        keyboard_value;
912
913
914         switch (uAction)
915         {
916                 case SPI_GETBEEP:
917                         TSXGetKeyboardControl(display, &keyboard_state);
918                         if (keyboard_state.bell_percent == 0)
919                                 *(BOOL16 *) lpvParam = FALSE;
920                         else
921                                 *(BOOL16 *) lpvParam = TRUE;
922                         break;
923                 
924                 case SPI_GETBORDER:
925                         *(INT16 *)lpvParam = GetSystemMetrics16( SM_CXFRAME );
926                         break;
927
928                 case SPI_GETFASTTASKSWITCH:
929                     if ( GetProfileInt32A( "windows", "CoolSwitch", 1 ) == 1 )
930                           *(BOOL16 *) lpvParam = TRUE;
931                         else
932                           *(BOOL16 *) lpvParam = FALSE;
933                         break;
934
935                 case SPI_GETGRIDGRANULARITY:
936                     *(INT16 *) lpvParam = GetProfileInt32A( "desktop", 
937                                                           "GridGranularity",
938                                                           1 );
939                     break;
940
941                 case SPI_GETICONTITLEWRAP:
942                     *(BOOL16 *) lpvParam = GetProfileInt32A( "desktop",
943                                                            "IconTitleWrap",
944                                                            TRUE );
945                     break;
946
947                 case SPI_GETKEYBOARDDELAY:
948                     *(INT16 *) lpvParam = GetProfileInt32A( "keyboard",
949                                                           "KeyboardDelay", 1 );
950                     break;
951
952                 case SPI_GETKEYBOARDSPEED:
953                     *(WORD *) lpvParam = GetProfileInt32A( "keyboard",
954                                                            "KeyboardSpeed",
955                                                            30 );
956                     break;
957
958                 case SPI_GETMENUDROPALIGNMENT:
959                         *(BOOL16 *) lpvParam = GetSystemMetrics16( SM_MENUDROPALIGNMENT ); /* XXX check this */
960                         break;
961
962                 case SPI_GETSCREENSAVEACTIVE:
963                     if ( GetProfileInt32A( "windows", "ScreenSaveActive", 1 ) == 1 )
964                         *(BOOL16 *) lpvParam = TRUE;
965                     else
966                         *(BOOL16 *) lpvParam = FALSE;
967                     break;
968
969                 case SPI_GETSCREENSAVETIMEOUT:
970                         /* FIXME GetProfileInt( "windows", "ScreenSaveTimeout", 300 ); */
971                         TSXGetScreenSaver(display, &timeout, &temp,&temp,&temp);
972                         *(INT16 *) lpvParam = timeout * 1000;
973                         break;
974
975                 case SPI_ICONHORIZONTALSPACING:
976                     /* FIXME Get/SetProfileInt */
977                         if (lpvParam == NULL)
978                             /*SetSystemMetrics( SM_CXICONSPACING, uParam )*/ ;
979                         else
980                             *(INT16 *)lpvParam = GetSystemMetrics16( SM_CXICONSPACING );
981                         break;
982
983                 case SPI_ICONVERTICALSPACING:
984                     /* FIXME Get/SetProfileInt */
985                     if (lpvParam == NULL)
986                         /*SetSystemMetrics( SM_CYICONSPACING, uParam )*/ ;
987                     else
988                         *(INT16 *)lpvParam = GetSystemMetrics16(SM_CYICONSPACING);
989                     break;
990
991                 case SPI_SETBEEP:
992                         if (uParam == TRUE)
993                                 keyboard_value.bell_percent = -1;
994                         else
995                                 keyboard_value.bell_percent = 0;                        
996                         TSXChangeKeyboardControl(display, KBBellPercent, 
997                                                         &keyboard_value);
998                         break;
999
1000                 case SPI_SETSCREENSAVEACTIVE:
1001                         if (uParam == TRUE)
1002                                 TSXActivateScreenSaver(display);
1003                         else
1004                                 TSXResetScreenSaver(display);
1005                         break;
1006
1007                 case SPI_SETSCREENSAVETIMEOUT:
1008                         TSXSetScreenSaver(display, uParam, 60, DefaultBlanking, 
1009                                                         DefaultExposures);
1010                         break;
1011
1012                 case SPI_SETDESKWALLPAPER:
1013                         return (SetDeskWallPaper32((LPSTR) lpvParam));
1014                         break;
1015
1016                 case SPI_SETDESKPATTERN:
1017                         if ((INT16)uParam == -1) {
1018                                 GetProfileString32A("Desktop", "Pattern", 
1019                                                 "170 85 170 85 170 85 170 85", 
1020                                                 buffer, sizeof(buffer) );
1021                                 return (DESKTOP_SetPattern((LPSTR) buffer));
1022                         } else
1023                                 return (DESKTOP_SetPattern((LPSTR) lpvParam));
1024                         break;
1025
1026                 case SPI_GETICONTITLELOGFONT: 
1027                 {
1028                     LPLOGFONT16 lpLogFont = (LPLOGFONT16)lpvParam;
1029
1030                     GetProfileString32A("Desktop", "IconTitleFaceName", "MS Sans Serif", 
1031                                         lpLogFont->lfFaceName, LF_FACESIZE );
1032                     lpLogFont->lfHeight = -GetProfileInt32A("Desktop","IconTitleSize", 8);
1033                     lpLogFont->lfWidth = 0;
1034                     lpLogFont->lfEscapement = lpLogFont->lfOrientation = 0;
1035                     lpLogFont->lfWeight = FW_NORMAL;
1036                     lpLogFont->lfItalic = FALSE;
1037                     lpLogFont->lfStrikeOut = FALSE;
1038                     lpLogFont->lfUnderline = FALSE;
1039                     lpLogFont->lfCharSet = ANSI_CHARSET;
1040                     lpLogFont->lfOutPrecision = OUT_DEFAULT_PRECIS;
1041                     lpLogFont->lfClipPrecision = CLIP_DEFAULT_PRECIS;
1042                     lpLogFont->lfPitchAndFamily = DEFAULT_PITCH | FF_SWISS;
1043                     break;
1044                 }
1045                 case SPI_GETNONCLIENTMETRICS:
1046
1047 #define lpnm ((LPNONCLIENTMETRICS16)lpvParam)
1048                     if( lpnm->cbSize == sizeof(NONCLIENTMETRICS16) )
1049                     {
1050                         /* FIXME: initialize geometry entries */
1051                         SystemParametersInfo16( SPI_GETICONTITLELOGFONT, 0, 
1052                                                         (LPVOID)&(lpnm->lfCaptionFont),0);
1053                         lpnm->lfCaptionFont.lfWeight = FW_BOLD;
1054                         SystemParametersInfo16( SPI_GETICONTITLELOGFONT, 0,
1055                                                         (LPVOID)&(lpnm->lfMenuFont),0);
1056                         SystemParametersInfo16( SPI_GETICONTITLELOGFONT, 0,
1057                                                         (LPVOID)&(lpnm->lfStatusFont),0);
1058                         SystemParametersInfo16( SPI_GETICONTITLELOGFONT, 0,
1059                                                         (LPVOID)&(lpnm->lfMessageFont),0);
1060                     }
1061                     else /* winfile 95 sets sbSize to 340 */
1062                         SystemParametersInfo32A( uAction, uParam, lpvParam, fuWinIni );
1063 #undef lpnm
1064                     break;
1065
1066                 case SPI_LANGDRIVER:
1067                 case SPI_SETBORDER:
1068                 case SPI_SETDOUBLECLKHEIGHT:
1069                 case SPI_SETDOUBLECLICKTIME:
1070                 case SPI_SETDOUBLECLKWIDTH:
1071                 case SPI_SETFASTTASKSWITCH:
1072                 case SPI_SETKEYBOARDDELAY:
1073                 case SPI_SETKEYBOARDSPEED:
1074                         WARN(system, "Option %d ignored.\n", uAction);
1075                         break;
1076
1077                 case SPI_GETWORKAREA:
1078                     SetRect16( (RECT16 *)lpvParam, 0, 0,
1079                                GetSystemMetrics16( SM_CXSCREEN ),
1080                                GetSystemMetrics16( SM_CYSCREEN ) );
1081                     break;
1082
1083                 default:
1084                         WARN(system, "Unknown option %d.\n", uAction);
1085                         break;
1086         }
1087         return 1;
1088 }
1089
1090 /***********************************************************************
1091  *      SystemParametersInfo32W   (USER32.541)
1092  */
1093 BOOL32 WINAPI SystemParametersInfo32W( UINT32 uAction, UINT32 uParam,
1094                                        LPVOID lpvParam, UINT32 fuWinIni )
1095 {
1096     char buffer[256];
1097
1098     switch (uAction)
1099     {
1100     case SPI_SETDESKWALLPAPER:
1101         if (lpvParam)
1102         {
1103             lstrcpynWtoA(buffer,(LPWSTR)lpvParam,sizeof(buffer));
1104             return SetDeskWallPaper32(buffer);
1105         }
1106         return SetDeskWallPaper32(NULL);
1107
1108     case SPI_SETDESKPATTERN:
1109         if ((INT32) uParam == -1)
1110         {
1111             GetProfileString32A("Desktop", "Pattern", 
1112                                 "170 85 170 85 170 85 170 85", 
1113                                 buffer, sizeof(buffer) );
1114             return (DESKTOP_SetPattern((LPSTR) buffer));
1115         }
1116         if (lpvParam)
1117         {
1118             lstrcpynWtoA(buffer,(LPWSTR)lpvParam,sizeof(buffer));
1119             return DESKTOP_SetPattern(buffer);
1120         }
1121         return DESKTOP_SetPattern(NULL);
1122
1123     case SPI_GETICONTITLELOGFONT:
1124         {
1125             /* FIXME GetProfileString32A( "?", "?", "?" ) */
1126             LPLOGFONT32W lpLogFont = (LPLOGFONT32W)lpvParam;
1127             lpLogFont->lfHeight = 10;
1128             lpLogFont->lfWidth = 0;
1129             lpLogFont->lfEscapement = lpLogFont->lfOrientation = 0;
1130             lpLogFont->lfWeight = FW_NORMAL;
1131             lpLogFont->lfItalic = lpLogFont->lfStrikeOut = lpLogFont->lfUnderline = FALSE;
1132             lpLogFont->lfCharSet = ANSI_CHARSET;
1133             lpLogFont->lfOutPrecision = OUT_DEFAULT_PRECIS;
1134             lpLogFont->lfClipPrecision = CLIP_DEFAULT_PRECIS;
1135             lpLogFont->lfPitchAndFamily = DEFAULT_PITCH | FF_SWISS;
1136         }
1137         break;
1138     case SPI_GETNONCLIENTMETRICS: {
1139         /* FIXME: implement correctly */
1140         LPNONCLIENTMETRICS32W   lpnm=(LPNONCLIENTMETRICS32W)lpvParam;
1141
1142         SystemParametersInfo32W(SPI_GETICONTITLELOGFONT,0,(LPVOID)&(lpnm->lfCaptionFont),0);
1143         lpnm->lfCaptionFont.lfWeight = FW_BOLD;
1144         SystemParametersInfo32W(SPI_GETICONTITLELOGFONT,0,(LPVOID)&(lpnm->lfMenuFont),0);
1145         SystemParametersInfo32W(SPI_GETICONTITLELOGFONT,0,(LPVOID)&(lpnm->lfStatusFont),0);
1146         SystemParametersInfo32W(SPI_GETICONTITLELOGFONT,0,(LPVOID)&(lpnm->lfMessageFont),0);
1147         break;
1148     }
1149
1150     case SPI_GETHIGHCONTRAST:
1151     {
1152        LPHIGHCONTRASTA lpHighContrastW = (LPHIGHCONTRASTW)lpvParam;
1153
1154        FIXME(system,"SPI_GETHIGHCONTRAST not fully implemented\n");
1155
1156        if ( lpHighContrastW->cbSize == sizeof( HIGHCONTRASTW ) )
1157        {
1158           /* Indicate that there is no high contrast available */
1159           lpHighContrastW->dwFlags = 0;
1160           lpHighContrastW->lpszDefaultScheme = NULL;
1161        }
1162        else
1163        {
1164           return FALSE;
1165        }
1166
1167        break;
1168     }
1169
1170     default:
1171         return SystemParametersInfo32A(uAction,uParam,lpvParam,fuWinIni);
1172         
1173     }
1174     return TRUE;
1175 }
1176
1177
1178 /***********************************************************************
1179 *       FileCDR (KERNEL.130)
1180 */
1181 void WINAPI FileCDR(FARPROC16 x)
1182 {
1183         FIXME(file,"(%8x): stub\n", (int) x);
1184 }