Release 980913
[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     /* tell the libX11 that we will do input method handling ourselves
406      * that keep libX11 from doing anything whith dead keys, allowing Wine
407      * to have total control over dead keys, that is this line allows
408      * them to work in Wine, even whith a libX11 including the dead key
409      * patches from Th.Quinot (http://Web.FdN.FR/~tquinot/dead-keys.en.html)
410      */
411     TSXOpenIM(display,NULL,NULL,NULL);
412
413       /* Merge file and screen databases */
414     if ((xrm_string = TSXResourceManagerString( display )) != NULL)
415     {
416         XrmDatabase display_db = TSXrmGetStringDatabase( xrm_string );
417         TSXrmMergeDatabases( display_db, &db );
418     }
419
420       /* Parse command line */
421     TSXrmParseCommand( &db, optionsTable, NB_OPTIONS,
422                      Options.programName, argc, argv );
423
424       /* Get all options */
425     if (MAIN_GetResource( db, ".iconic", &value ))
426         Options.cmdShow = SW_SHOWMINIMIZED;
427     if (MAIN_GetResource( db, ".privatemap", &value ))
428         Options.usePrivateMap = TRUE;
429     if (MAIN_GetResource( db, ".fixedmap", &value ))
430         Options.useFixedMap = TRUE;
431     if (MAIN_GetResource( db, ".synchronous", &value ))
432         Options.synchronous = TRUE;
433     if (MAIN_GetResource( db, ".backingstore", &value ))
434         Options.backingstore = TRUE;    
435     if (MAIN_GetResource( db, ".debug", &value ))
436         Options.debug = TRUE;
437     if (MAIN_GetResource( db, ".failreadonly", &value ))
438         Options.failReadOnly = TRUE;
439     if (MAIN_GetResource( db, ".perfect", &value ))
440         Options.perfectGraphics = TRUE;
441     if (MAIN_GetResource( db, ".depth", &value))
442         screenDepth = atoi( value.addr );
443     if (MAIN_GetResource( db, ".desktop", &value))
444         Options.desktopGeometry = value.addr;
445     if (MAIN_GetResource( db, ".language", &value))
446         MAIN_ParseLanguageOption( (char *)value.addr );
447     if (MAIN_GetResource( db, ".managed", &value))
448         Options.managed = TRUE;
449     if (MAIN_GetResource( db, ".mode", &value))
450         MAIN_ParseModeOption( (char *)value.addr );
451     if (MAIN_GetResource( db, ".debugoptions", &value))
452         MAIN_ParseDebugOptions((char*)value.addr);
453     if (MAIN_GetResource( db, ".debugmsg", &value))
454       {
455 #ifndef DEBUG_RUNTIME
456         MSG("%s: Option \"-debugmsg\" not implemented.\n" \
457           "    Recompile with DEBUG_RUNTIME in include/debugtools.h defined.\n",
458           argv[0]);
459         exit(1);
460 #else
461         if (MAIN_ParseDebugOptions((char*)value.addr)==FALSE)
462           {
463             int i;
464             MSG("%s: Syntax: -debugmsg [class]+xxx,...  or "
465                     "-debugmsg [class]-xxx,...\n",argv[0]);
466             MSG("Example: -debugmsg +all,warn-heap\n"
467                     "  turn on all messages except warning heap messages\n");
468             MSG("Special case: -debugmsg +relay=DLL:DLL.###:FuncName\n"
469                 "  turn on -debugmsg +relay only as specified\n"
470                 "Special case: -debugmsg -relay=DLL:DLL.###:FuncName\n"
471                 "  turn on -debugmsg +relay except as specified\n"
472                 "Also permitted, +snoop=..., -snoop=... as with relay.\n\n");
473
474             MSG("Available message classes:\n");
475             for(i=0;i<DEBUG_CLASS_COUNT;i++)
476               MSG( "%-9s", debug_cl_name[i]);
477             MSG("\n\n");
478
479             MSG("Available message types:\n");
480             MSG("%-9s ","all");
481             for(i=0;i<DEBUG_CHANNEL_COUNT;i++)
482               if(debug_ch_name[i])
483                 MSG("%-9s%c",debug_ch_name[i],
484                         (((i+2)%8==0)?'\n':' '));
485             MSG("\n\n");
486             exit(1);
487           }
488 #endif
489       }
490
491       if (MAIN_GetResource( db, ".dll", &value))
492       {
493           /* Hack: store option value in Options to be retrieved */
494           /* later on inside the emulator code. */
495           if (!__winelib) Options.dllFlags = xstrdup((char *)value.addr);
496           else
497           {
498               MSG("-dll not supported in Winelib\n" );
499               exit(1);
500           }
501       }
502
503       if (MAIN_GetResource( db, ".winver", &value))
504           VERSION_ParseVersion( (char*)value.addr );
505 }
506
507
508 /***********************************************************************
509  *           MAIN_CreateDesktop
510  */
511 static void MAIN_CreateDesktop( int argc, char *argv[] )
512 {
513     int x, y, flags;
514     unsigned int width = 640, height = 480;  /* Default size = 640x480 */
515     char *name = "Wine desktop";
516     XSizeHints *size_hints;
517     XWMHints   *wm_hints;
518     XClassHint *class_hints;
519     XSetWindowAttributes win_attr;
520     XTextProperty window_name;
521     Atom XA_WM_DELETE_WINDOW;
522
523     flags = TSXParseGeometry( Options.desktopGeometry, &x, &y, &width, &height );
524     screenWidth  = width;
525     screenHeight = height;
526
527       /* Create window */
528
529     win_attr.background_pixel = BlackPixel(display,0);
530     win_attr.event_mask = ExposureMask | KeyPressMask | KeyReleaseMask |
531                          PointerMotionMask | ButtonPressMask |
532                          ButtonReleaseMask | EnterWindowMask;
533     win_attr.cursor = TSXCreateFontCursor( display, XC_top_left_arrow );
534
535     rootWindow = TSXCreateWindow( display, DefaultRootWindow(display),
536                                 x, y, width, height, 0,
537                                 CopyFromParent, InputOutput, CopyFromParent,
538                                 CWBackPixel | CWEventMask | CWCursor, &win_attr );
539
540       /* Set window manager properties */
541
542     size_hints  = TSXAllocSizeHints();
543     wm_hints    = TSXAllocWMHints();
544     class_hints = TSXAllocClassHint();
545     if (!size_hints || !wm_hints || !class_hints)
546     {
547         MSG("Not enough memory for window manager hints.\n" );
548         exit(1);
549     }
550     size_hints->min_width = size_hints->max_width = width;
551     size_hints->min_height = size_hints->max_height = height;
552     size_hints->flags = PMinSize | PMaxSize;
553     if (flags & (XValue | YValue)) size_hints->flags |= USPosition;
554     if (flags & (WidthValue | HeightValue)) size_hints->flags |= USSize;
555     else size_hints->flags |= PSize;
556
557     wm_hints->flags = InputHint | StateHint;
558     wm_hints->input = True;
559     wm_hints->initial_state = NormalState;
560     class_hints->res_name = argv[0];
561     class_hints->res_class = "Wine";
562
563     TSXStringListToTextProperty( &name, 1, &window_name );
564     TSXSetWMProperties( display, rootWindow, &window_name, &window_name,
565                       argv, argc, size_hints, wm_hints, class_hints );
566     XA_WM_DELETE_WINDOW = TSXInternAtom( display, "WM_DELETE_WINDOW", False );
567     TSXSetWMProtocols( display, rootWindow, &XA_WM_DELETE_WINDOW, 1 );
568     TSXFree( size_hints );
569     TSXFree( wm_hints );
570     TSXFree( class_hints );
571
572       /* Map window */
573
574     TSXMapWindow( display, rootWindow );
575 }
576
577
578 XKeyboardState keyboard_state;
579
580 /***********************************************************************
581  *           MAIN_SaveSetup
582  */
583 static void MAIN_SaveSetup(void)
584 {
585     TSXGetKeyboardControl(display, &keyboard_state);
586 }
587
588 /***********************************************************************
589  *           MAIN_RestoreSetup
590  */
591 static void MAIN_RestoreSetup(void)
592 {
593     XKeyboardControl keyboard_value;
594
595     keyboard_value.key_click_percent    = keyboard_state.key_click_percent;
596     keyboard_value.bell_percent         = keyboard_state.bell_percent;
597     keyboard_value.bell_pitch           = keyboard_state.bell_pitch;
598     keyboard_value.bell_duration        = keyboard_state.bell_duration;
599     keyboard_value.auto_repeat_mode     = keyboard_state.global_auto_repeat;
600
601     XChangeKeyboardControl(display, KBKeyClickPercent | KBBellPercent | 
602         KBBellPitch | KBBellDuration | KBAutoRepeatMode, &keyboard_value);
603 }
604
605
606 /***********************************************************************
607  *           called_at_exit
608  */
609 static void called_at_exit(void)
610 {
611     MAIN_RestoreSetup();
612     COLOR_Cleanup();
613     WINSOCK_Shutdown();
614     /* FIXME: should check for other processes or threads */
615     DeleteCriticalSection( HEAP_SystemLock );
616 }
617
618 /***********************************************************************
619  *           MAIN_WineInit
620  *
621  * Wine initialisation and command-line parsing
622  */
623 BOOL32 MAIN_WineInit( int *argc, char *argv[] )
624 {    
625     int depth_count, i;
626     int *depth_list;
627     struct timeval tv;
628
629 #ifdef MALLOC_DEBUGGING
630     char *trace;
631
632     mcheck(NULL);
633     if (!(trace = getenv("MALLOC_TRACE")))
634     {       
635         MSG( "MALLOC_TRACE not set. No trace generated\n" );
636     }
637     else
638     {
639         MSG( "malloc trace goes to %s\n", trace );
640         mtrace();
641     }
642 #endif
643
644     setbuf(stdout,NULL);
645     setbuf(stderr,NULL);
646
647     setlocale(LC_CTYPE,"");
648     gettimeofday( &tv, NULL);
649     MSG_WineStartTicks = (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
650
651     /* We need this before calling any Xlib function */
652     InitializeCriticalSection( &X11DRV_CritSection );
653
654     TSXrmInitialize();
655
656     putenv("XKB_DISABLE="); /* Disable XKB extension if present. */
657
658     MAIN_ParseOptions( argc, argv );
659
660     if (Options.desktopGeometry && Options.managed)
661     {
662 #if 0
663         MSG( "%s: -managed and -desktop options cannot be used together\n",
664                  Options.programName );
665         exit(1);
666 #else
667         Options.managed = FALSE;
668 #endif
669     }
670
671     screen       = DefaultScreenOfDisplay( display );
672     screenWidth  = WidthOfScreen( screen );
673     screenHeight = HeightOfScreen( screen );
674     if (screenDepth)  /* -depth option specified */
675     {
676         depth_list = TSXListDepths(display,DefaultScreen(display),&depth_count);
677         for (i = 0; i < depth_count; i++)
678             if (depth_list[i] == screenDepth) break;
679         TSXFree( depth_list );
680         if (i >= depth_count)
681         {
682             MSG( "%s: Depth %d not supported on this screen.\n",
683                               Options.programName, screenDepth );
684             exit(1);
685         }
686     }
687     else screenDepth  = DefaultDepthOfScreen( screen );
688     if (Options.synchronous) TSXSynchronize( display, True );
689     if (Options.desktopGeometry) MAIN_CreateDesktop( *argc, argv );
690     else rootWindow = DefaultRootWindow( display );
691
692     MAIN_SaveSetup();
693     atexit(called_at_exit);
694     return TRUE;
695 }
696
697
698 /***********************************************************************
699  *           MessageBeep16   (USER.104)
700  */
701 void WINAPI MessageBeep16( UINT16 i )
702 {
703     MessageBeep32( i );
704 }
705
706
707 /***********************************************************************
708  *           MessageBeep32   (USER32.390)
709  */
710 BOOL32 WINAPI MessageBeep32( UINT32 i )
711 {
712     TSXBell( display, 0 );
713     return TRUE;
714 }
715
716
717 /***********************************************************************
718  *           Beep   (KERNEL32.11)
719  */
720 BOOL32 WINAPI Beep( DWORD dwFreq, DWORD dwDur )
721 {
722     /* dwFreq and dwDur are ignored by Win95 */
723     TSXBell(display, 0);
724     return TRUE;
725 }
726
727
728 /***********************************************************************
729  *      GetTimerResolution (USER.14)
730  */
731 LONG WINAPI GetTimerResolution(void)
732 {
733         return (1000);
734 }
735
736 /***********************************************************************
737  *      SystemParametersInfo32A   (USER32.540)
738  */
739 BOOL32 WINAPI SystemParametersInfo32A( UINT32 uAction, UINT32 uParam,
740                                        LPVOID lpvParam, UINT32 fuWinIni )
741 {
742         int timeout, temp;
743         XKeyboardState          keyboard_state;
744
745         switch (uAction) {
746         case SPI_GETBEEP:
747                 TSXGetKeyboardControl(display, &keyboard_state);
748                 if (keyboard_state.bell_percent == 0)
749                         *(BOOL32 *) lpvParam = FALSE;
750                 else
751                         *(BOOL32 *) lpvParam = TRUE;
752                 break;
753
754         case SPI_GETBORDER:
755                 *(INT32 *)lpvParam = GetSystemMetrics32( SM_CXFRAME );
756                 break;
757
758         case SPI_GETFASTTASKSWITCH:
759                 if ( GetProfileInt32A( "windows", "CoolSwitch", 1 ) == 1 )
760                         *(BOOL32 *) lpvParam = TRUE;
761                 else
762                         *(BOOL32 *) lpvParam = FALSE;
763                 break;
764
765         case SPI_GETDRAGFULLWINDOWS:
766           *(BOOL32 *) lpvParam = FALSE;
767                 
768         case SPI_GETGRIDGRANULARITY:
769                 *(INT32*)lpvParam=GetProfileInt32A("desktop","GridGranularity",1);
770                 break;
771
772         case SPI_GETICONTITLEWRAP:
773                 *(BOOL32*)lpvParam=GetProfileInt32A("desktop","IconTitleWrap",TRUE);
774                 break;
775
776         case SPI_GETKEYBOARDDELAY:
777                 *(INT32*)lpvParam=GetProfileInt32A("keyboard","KeyboardDelay",1);
778                 break;
779
780         case SPI_GETKEYBOARDSPEED:
781                 *(DWORD*)lpvParam=GetProfileInt32A("keyboard","KeyboardSpeed",30);
782                 break;
783
784         case SPI_GETMENUDROPALIGNMENT:
785                 *(BOOL32*)lpvParam=GetSystemMetrics32(SM_MENUDROPALIGNMENT); /* XXX check this */
786                 break;
787
788         case SPI_GETSCREENSAVEACTIVE:
789                 if ( GetProfileInt32A( "windows", "ScreenSaveActive", 1 ) == 1 )
790                         *(BOOL32*)lpvParam = TRUE;
791                 else
792                         *(BOOL32*)lpvParam = FALSE;
793                 break;
794
795         case SPI_GETSCREENSAVETIMEOUT:
796         /* FIXME GetProfileInt( "windows", "ScreenSaveTimeout", 300 ); */
797                 TSXGetScreenSaver(display, &timeout, &temp,&temp,&temp);
798                 *(INT32 *) lpvParam = timeout * 1000;
799                 break;
800
801         case SPI_ICONHORIZONTALSPACING:
802                 /* FIXME Get/SetProfileInt */
803                 if (lpvParam == NULL)
804                         /*SetSystemMetrics( SM_CXICONSPACING, uParam )*/ ;
805                 else
806                         *(INT32*)lpvParam=GetSystemMetrics32(SM_CXICONSPACING);
807                 break;
808
809         case SPI_ICONVERTICALSPACING:
810                 /* FIXME Get/SetProfileInt */
811                 if (lpvParam == NULL)
812                         /*SetSystemMetrics( SM_CYICONSPACING, uParam )*/ ;
813                 else
814                         *(INT32*)lpvParam=GetSystemMetrics32(SM_CYICONSPACING);
815                 break;
816
817         case SPI_GETICONTITLELOGFONT: {
818                 LPLOGFONT32A lpLogFont = (LPLOGFONT32A)lpvParam;
819
820                 /* from now on we always have an alias for MS Sans Serif */
821
822                 GetProfileString32A("Desktop", "IconTitleFaceName", "MS Sans Serif", 
823                         lpLogFont->lfFaceName, LF_FACESIZE );
824                 lpLogFont->lfHeight = -GetProfileInt32A("Desktop","IconTitleSize", 8);
825                 lpLogFont->lfWidth = 0;
826                 lpLogFont->lfEscapement = lpLogFont->lfOrientation = 0;
827                 lpLogFont->lfWeight = FW_NORMAL;
828                 lpLogFont->lfItalic = FALSE;
829                 lpLogFont->lfStrikeOut = FALSE;
830                 lpLogFont->lfUnderline = FALSE;
831                 lpLogFont->lfCharSet = ANSI_CHARSET;
832                 lpLogFont->lfOutPrecision = OUT_DEFAULT_PRECIS;
833                 lpLogFont->lfClipPrecision = CLIP_DEFAULT_PRECIS;
834                 lpLogFont->lfPitchAndFamily = DEFAULT_PITCH | FF_SWISS;
835                 break;
836         }
837         case SPI_GETWORKAREA:
838                 SetRect32( (RECT32 *)lpvParam, 0, 0,
839                         GetSystemMetrics32( SM_CXSCREEN ),
840                         GetSystemMetrics32( SM_CYSCREEN )
841                 );
842                 break;
843         case SPI_GETNONCLIENTMETRICS: 
844
845 #define lpnm ((LPNONCLIENTMETRICS32A)lpvParam)
846                 
847                 if( lpnm->cbSize == sizeof(NONCLIENTMETRICS32A) )
848                 {
849                     /* FIXME: initialize geometry entries */
850
851                     SystemParametersInfo32A(SPI_GETICONTITLELOGFONT, 0,
852                                                         (LPVOID)&(lpnm->lfCaptionFont),0);
853                     lpnm->lfCaptionFont.lfWeight = FW_BOLD;
854                     SystemParametersInfo32A(SPI_GETICONTITLELOGFONT, 0,
855                                                         (LPVOID)&(lpnm->lfMenuFont),0);
856                     SystemParametersInfo32A(SPI_GETICONTITLELOGFONT, 0,
857                                                         (LPVOID)&(lpnm->lfStatusFont),0);
858                     SystemParametersInfo32A(SPI_GETICONTITLELOGFONT, 0,
859                                                         (LPVOID)&(lpnm->lfMessageFont),0);
860                 }
861 #undef lpnm
862                 break;
863
864         case SPI_GETANIMATION: {
865                 LPANIMATIONINFO lpAnimInfo = (LPANIMATIONINFO)lpvParam;
866  
867                 /* Tell it "disabled" */
868                 lpAnimInfo->cbSize = sizeof(ANIMATIONINFO);
869                 uParam = sizeof(ANIMATIONINFO);
870                 lpAnimInfo->iMinAnimate = 0; /* Minimise and restore animation is disabled (nonzero == enabled) */
871                 break;
872         }
873  
874         case SPI_SETANIMATION: {
875                 LPANIMATIONINFO lpAnimInfo = (LPANIMATIONINFO)lpvParam;
876  
877                 /* Do nothing */
878                 WARN(system, "SPI_SETANIMATION ignored.\n");
879                 lpAnimInfo->cbSize = sizeof(ANIMATIONINFO);
880                 uParam = sizeof(ANIMATIONINFO);
881                 break;
882         }
883
884         case SPI_GETHIGHCONTRAST:
885         {
886                 LPHIGHCONTRASTA lpHighContrastA = (LPHIGHCONTRASTA)lpvParam;
887
888                 FIXME(system,"SPI_GETHIGHCONTRAST not fully implemented\n");
889
890                 if ( lpHighContrastA->cbSize == sizeof( HIGHCONTRASTA ) )
891                 {
892                         /* Indicate that there is no high contrast available */
893                         lpHighContrastA->dwFlags = 0;
894                         lpHighContrastA->lpszDefaultScheme = NULL;
895                 }
896                 else
897                 {
898                         return FALSE;
899                 }
900
901                 break;
902         }
903
904         default:
905                 return SystemParametersInfo16(uAction,uParam,lpvParam,fuWinIni);
906         }
907         return TRUE;
908 }
909
910
911 /***********************************************************************
912  *      SystemParametersInfo16   (USER.483)
913  */
914 BOOL16 WINAPI SystemParametersInfo16( UINT16 uAction, UINT16 uParam,
915                                       LPVOID lpvParam, UINT16 fuWinIni )
916 {
917         int timeout, temp;
918         char buffer[256];
919         XKeyboardState          keyboard_state;
920         XKeyboardControl        keyboard_value;
921
922
923         switch (uAction)
924         {
925                 case SPI_GETBEEP:
926                         TSXGetKeyboardControl(display, &keyboard_state);
927                         if (keyboard_state.bell_percent == 0)
928                                 *(BOOL16 *) lpvParam = FALSE;
929                         else
930                                 *(BOOL16 *) lpvParam = TRUE;
931                         break;
932                 
933                 case SPI_GETBORDER:
934                         *(INT16 *)lpvParam = GetSystemMetrics16( SM_CXFRAME );
935                         break;
936
937                 case SPI_GETFASTTASKSWITCH:
938                     if ( GetProfileInt32A( "windows", "CoolSwitch", 1 ) == 1 )
939                           *(BOOL16 *) lpvParam = TRUE;
940                         else
941                           *(BOOL16 *) lpvParam = FALSE;
942                         break;
943
944                 case SPI_GETGRIDGRANULARITY:
945                     *(INT16 *) lpvParam = GetProfileInt32A( "desktop", 
946                                                           "GridGranularity",
947                                                           1 );
948                     break;
949
950                 case SPI_GETICONTITLEWRAP:
951                     *(BOOL16 *) lpvParam = GetProfileInt32A( "desktop",
952                                                            "IconTitleWrap",
953                                                            TRUE );
954                     break;
955
956                 case SPI_GETKEYBOARDDELAY:
957                     *(INT16 *) lpvParam = GetProfileInt32A( "keyboard",
958                                                           "KeyboardDelay", 1 );
959                     break;
960
961                 case SPI_GETKEYBOARDSPEED:
962                     *(WORD *) lpvParam = GetProfileInt32A( "keyboard",
963                                                            "KeyboardSpeed",
964                                                            30 );
965                     break;
966
967                 case SPI_GETMENUDROPALIGNMENT:
968                         *(BOOL16 *) lpvParam = GetSystemMetrics16( SM_MENUDROPALIGNMENT ); /* XXX check this */
969                         break;
970
971                 case SPI_GETSCREENSAVEACTIVE:
972                     if ( GetProfileInt32A( "windows", "ScreenSaveActive", 1 ) == 1 )
973                         *(BOOL16 *) lpvParam = TRUE;
974                     else
975                         *(BOOL16 *) lpvParam = FALSE;
976                     break;
977
978                 case SPI_GETSCREENSAVETIMEOUT:
979                         /* FIXME GetProfileInt( "windows", "ScreenSaveTimeout", 300 ); */
980                         TSXGetScreenSaver(display, &timeout, &temp,&temp,&temp);
981                         *(INT16 *) lpvParam = timeout;
982                         break;
983
984                 case SPI_ICONHORIZONTALSPACING:
985                     /* FIXME Get/SetProfileInt */
986                         if (lpvParam == NULL)
987                             /*SetSystemMetrics( SM_CXICONSPACING, uParam )*/ ;
988                         else
989                             *(INT16 *)lpvParam = GetSystemMetrics16( SM_CXICONSPACING );
990                         break;
991
992                 case SPI_ICONVERTICALSPACING:
993                     /* FIXME Get/SetProfileInt */
994                     if (lpvParam == NULL)
995                         /*SetSystemMetrics( SM_CYICONSPACING, uParam )*/ ;
996                     else
997                         *(INT16 *)lpvParam = GetSystemMetrics16(SM_CYICONSPACING);
998                     break;
999
1000                 case SPI_SETBEEP:
1001                         if (uParam == TRUE)
1002                                 keyboard_value.bell_percent = -1;
1003                         else
1004                                 keyboard_value.bell_percent = 0;                        
1005                         TSXChangeKeyboardControl(display, KBBellPercent, 
1006                                                         &keyboard_value);
1007                         break;
1008
1009                 case SPI_SETSCREENSAVEACTIVE:
1010                         if (uParam == TRUE)
1011                                 TSXActivateScreenSaver(display);
1012                         else
1013                                 TSXResetScreenSaver(display);
1014                         break;
1015
1016                 case SPI_SETSCREENSAVETIMEOUT:
1017                         TSXSetScreenSaver(display, uParam, 60, DefaultBlanking, 
1018                                                         DefaultExposures);
1019                         break;
1020
1021                 case SPI_SETDESKWALLPAPER:
1022                         return (SetDeskWallPaper32((LPSTR) lpvParam));
1023                         break;
1024
1025                 case SPI_SETDESKPATTERN:
1026                         if ((INT16)uParam == -1) {
1027                                 GetProfileString32A("Desktop", "Pattern", 
1028                                                 "170 85 170 85 170 85 170 85", 
1029                                                 buffer, sizeof(buffer) );
1030                                 return (DESKTOP_SetPattern((LPSTR) buffer));
1031                         } else
1032                                 return (DESKTOP_SetPattern((LPSTR) lpvParam));
1033                         break;
1034
1035                 case SPI_GETICONTITLELOGFONT: 
1036                 {
1037                     LPLOGFONT16 lpLogFont = (LPLOGFONT16)lpvParam;
1038
1039                     GetProfileString32A("Desktop", "IconTitleFaceName", "MS Sans Serif", 
1040                                         lpLogFont->lfFaceName, LF_FACESIZE );
1041                     lpLogFont->lfHeight = -GetProfileInt32A("Desktop","IconTitleSize", 8);
1042                     lpLogFont->lfWidth = 0;
1043                     lpLogFont->lfEscapement = lpLogFont->lfOrientation = 0;
1044                     lpLogFont->lfWeight = FW_NORMAL;
1045                     lpLogFont->lfItalic = FALSE;
1046                     lpLogFont->lfStrikeOut = FALSE;
1047                     lpLogFont->lfUnderline = FALSE;
1048                     lpLogFont->lfCharSet = ANSI_CHARSET;
1049                     lpLogFont->lfOutPrecision = OUT_DEFAULT_PRECIS;
1050                     lpLogFont->lfClipPrecision = CLIP_DEFAULT_PRECIS;
1051                     lpLogFont->lfPitchAndFamily = DEFAULT_PITCH | FF_SWISS;
1052                     break;
1053                 }
1054                 case SPI_GETNONCLIENTMETRICS:
1055
1056 #define lpnm ((LPNONCLIENTMETRICS16)lpvParam)
1057                     if( lpnm->cbSize == sizeof(NONCLIENTMETRICS16) )
1058                     {
1059                         /* FIXME: initialize geometry entries */
1060                         SystemParametersInfo16( SPI_GETICONTITLELOGFONT, 0, 
1061                                                         (LPVOID)&(lpnm->lfCaptionFont),0);
1062                         lpnm->lfCaptionFont.lfWeight = FW_BOLD;
1063                         SystemParametersInfo16( SPI_GETICONTITLELOGFONT, 0,
1064                                                         (LPVOID)&(lpnm->lfMenuFont),0);
1065                         SystemParametersInfo16( SPI_GETICONTITLELOGFONT, 0,
1066                                                         (LPVOID)&(lpnm->lfStatusFont),0);
1067                         SystemParametersInfo16( SPI_GETICONTITLELOGFONT, 0,
1068                                                         (LPVOID)&(lpnm->lfMessageFont),0);
1069                     }
1070                     else /* winfile 95 sets sbSize to 340 */
1071                         SystemParametersInfo32A( uAction, uParam, lpvParam, fuWinIni );
1072 #undef lpnm
1073                     break;
1074
1075                 case SPI_LANGDRIVER:
1076                 case SPI_SETBORDER:
1077                 case SPI_SETDOUBLECLKHEIGHT:
1078                 case SPI_SETDOUBLECLICKTIME:
1079                 case SPI_SETDOUBLECLKWIDTH:
1080                 case SPI_SETFASTTASKSWITCH:
1081                 case SPI_SETKEYBOARDDELAY:
1082                 case SPI_SETKEYBOARDSPEED:
1083                         WARN(system, "Option %d ignored.\n", uAction);
1084                         break;
1085
1086                 case SPI_GETWORKAREA:
1087                     SetRect16( (RECT16 *)lpvParam, 0, 0,
1088                                GetSystemMetrics16( SM_CXSCREEN ),
1089                                GetSystemMetrics16( SM_CYSCREEN ) );
1090                     break;
1091
1092                 default:
1093                         WARN(system, "Unknown option %d.\n", uAction);
1094                         break;
1095         }
1096         return 1;
1097 }
1098
1099 /***********************************************************************
1100  *      SystemParametersInfo32W   (USER32.541)
1101  */
1102 BOOL32 WINAPI SystemParametersInfo32W( UINT32 uAction, UINT32 uParam,
1103                                        LPVOID lpvParam, UINT32 fuWinIni )
1104 {
1105     char buffer[256];
1106
1107     switch (uAction)
1108     {
1109     case SPI_SETDESKWALLPAPER:
1110         if (lpvParam)
1111         {
1112             lstrcpynWtoA(buffer,(LPWSTR)lpvParam,sizeof(buffer));
1113             return SetDeskWallPaper32(buffer);
1114         }
1115         return SetDeskWallPaper32(NULL);
1116
1117     case SPI_SETDESKPATTERN:
1118         if ((INT32) uParam == -1)
1119         {
1120             GetProfileString32A("Desktop", "Pattern", 
1121                                 "170 85 170 85 170 85 170 85", 
1122                                 buffer, sizeof(buffer) );
1123             return (DESKTOP_SetPattern((LPSTR) buffer));
1124         }
1125         if (lpvParam)
1126         {
1127             lstrcpynWtoA(buffer,(LPWSTR)lpvParam,sizeof(buffer));
1128             return DESKTOP_SetPattern(buffer);
1129         }
1130         return DESKTOP_SetPattern(NULL);
1131
1132     case SPI_GETICONTITLELOGFONT:
1133         {
1134             /* FIXME GetProfileString32A( "?", "?", "?" ) */
1135             LPLOGFONT32W lpLogFont = (LPLOGFONT32W)lpvParam;
1136             lpLogFont->lfHeight = 10;
1137             lpLogFont->lfWidth = 0;
1138             lpLogFont->lfEscapement = lpLogFont->lfOrientation = 0;
1139             lpLogFont->lfWeight = FW_NORMAL;
1140             lpLogFont->lfItalic = lpLogFont->lfStrikeOut = lpLogFont->lfUnderline = FALSE;
1141             lpLogFont->lfCharSet = ANSI_CHARSET;
1142             lpLogFont->lfOutPrecision = OUT_DEFAULT_PRECIS;
1143             lpLogFont->lfClipPrecision = CLIP_DEFAULT_PRECIS;
1144             lpLogFont->lfPitchAndFamily = DEFAULT_PITCH | FF_SWISS;
1145         }
1146         break;
1147     case SPI_GETNONCLIENTMETRICS: {
1148         /* FIXME: implement correctly */
1149         LPNONCLIENTMETRICS32W   lpnm=(LPNONCLIENTMETRICS32W)lpvParam;
1150
1151         SystemParametersInfo32W(SPI_GETICONTITLELOGFONT,0,(LPVOID)&(lpnm->lfCaptionFont),0);
1152         lpnm->lfCaptionFont.lfWeight = FW_BOLD;
1153         SystemParametersInfo32W(SPI_GETICONTITLELOGFONT,0,(LPVOID)&(lpnm->lfMenuFont),0);
1154         SystemParametersInfo32W(SPI_GETICONTITLELOGFONT,0,(LPVOID)&(lpnm->lfStatusFont),0);
1155         SystemParametersInfo32W(SPI_GETICONTITLELOGFONT,0,(LPVOID)&(lpnm->lfMessageFont),0);
1156         break;
1157     }
1158
1159     case SPI_GETHIGHCONTRAST:
1160     {
1161        LPHIGHCONTRASTW lpHighContrastW = (LPHIGHCONTRASTW)lpvParam;
1162
1163        FIXME(system,"SPI_GETHIGHCONTRAST not fully implemented\n");
1164
1165        if ( lpHighContrastW->cbSize == sizeof( HIGHCONTRASTW ) )
1166        {
1167           /* Indicate that there is no high contrast available */
1168           lpHighContrastW->dwFlags = 0;
1169           lpHighContrastW->lpszDefaultScheme = NULL;
1170        }
1171        else
1172        {
1173           return FALSE;
1174        }
1175
1176        break;
1177     }
1178
1179     default:
1180         return SystemParametersInfo32A(uAction,uParam,lpvParam,fuWinIni);
1181         
1182     }
1183     return TRUE;
1184 }
1185
1186
1187 /***********************************************************************
1188 *       FileCDR (KERNEL.130)
1189 */
1190 FARPROC16 WINAPI FileCDR(FARPROC16 x)
1191 {
1192         FIXME(file,"(%8x): stub\n", (int) x);
1193         return (FARPROC16)TRUE;
1194 }