Release 961222
[wine/multimedia.git] / misc / main.c
blob754950e989ce7f4c5633685146bfcacec47be8b0
1 /*
2 * Main function.
4 * Copyright 1994 Alexandre Julliard
5 */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <ctype.h>
12 /* #include <locale.h> */
13 #ifdef MALLOC_DEBUGGING
14 #include <malloc.h>
15 #endif
16 #include <X11/Xlib.h>
17 #include <X11/Xresource.h>
18 #include <X11/Xutil.h>
19 #include <X11/Xlocale.h>
20 #include <X11/cursorfont.h>
21 #include "heap.h"
22 #include "message.h"
23 #include "module.h"
24 #include "msdos.h"
25 #include "windows.h"
26 #include "color.h"
27 #include "winsock.h"
28 #include "options.h"
29 #include "desktop.h"
30 #include "registers.h"
31 #include "shell.h"
32 #include "winbase.h"
33 #define DEBUG_DEFINE_VARIABLES
34 #include "stddebug.h"
35 #include "debug.h"
36 #include "xmalloc.h"
38 const char people[] = "Wine is available thanks to the work of "
39 "Bob Amstadt, Dag Asheim, Martin Ayotte, Ross Biro, Uwe Bonnes, Erik Bos, "
40 "Fons Botman, John Brezak, Andrew Bulhak, John Burton, "
41 "Niels de Carpentier, Roman Dolejsi, Frans van Dorsselaer, Paul Falstad, "
42 "Olaf Flebbe, Peter Galbavy, Ramon Garcia, Hans de Graaff, "
43 "Charles M. Hannum, John Harvey, Cameron Heide, Jochen Hoenicke, "
44 "Onno Hovers, Jeffrey Hsu, Miguel de Icaza, Jukka Iivonen, "
45 "Alexandre Julliard, Jochen Karrer, Andreas Kirschbaum, Albrecht Kleine, "
46 "Jon Konrath, Alex Korobka, Greg Kreider, Anand Kumria, Scott A. Laird, "
47 "Martin von Loewis, Kenneth MacDonald, Peter MacDonald, William Magro, "
48 "Juergen Marquardt, Marcus Meissner, Graham Menhennitt, David Metcalfe, "
49 "Steffen Moeller, Philippe De Muyter, Itai Nahshon, Michael Patra, "
50 "Jim Peterson, Robert Pouliot, Keith Reynolds, John Richardson, "
51 "Johannes Ruscheinski, Thomas Sandford, Constantine Sapuntzakis, "
52 "Daniel Schepler, Ulrich Schmid, Bernd Schmidt, Yngvi Sigurjonsson, "
53 "Rick Sladkey, William Smith, Erik Svendsen, Tristan Tarrant, "
54 "Andrew Taylor, Duncan C Thomson, Goran Thyni, Jimmy Tirtawangsa, "
55 "Jon Tombs, Linus Torvalds, Gregory Trubetskoy, Michael Veksler, "
56 "Sven Verdoolaege, Eric Warnke, Manfred Weichel, Morten Welinder, "
57 "Jan Willamowius, Carl Williams, Karl Guenter Wuensch, Eric Youngdale, "
58 "and James Youngman. ";
60 const WINE_LANGUAGE_DEF Languages[] =
62 {"En",0x0409}, /* LANG_En */
63 {"Es",0x040A}, /* LANG_Es */
64 {"De",0x0407}, /* LANG_De */
65 {"No",0x0414}, /* LANG_No */
66 {"Fr",0x0400}, /* LANG_Fr */
67 {"Fi",0x040B}, /* LANG_Fi */
68 {"Da",0x0406}, /* LANG_Da */
69 {"Cz",0x0405}, /* LANG_Cz */
70 {"Eo", 0}, /* LANG_Eo */ /* FIXME languageid */
71 {"It",0x0410}, /* LANG_It */
72 {"Ko",0x0412}, /* LANG_Ko */
73 {NULL,0}
76 WORD WINE_LanguageId = 0;
78 #define WINE_CLASS "Wine" /* Class name for resources */
80 #define WINE_APP_DEFAULTS "/usr/lib/X11/app-defaults/Wine"
82 typedef struct tagENVENTRY {
83 LPSTR Name;
84 LPSTR Value;
85 WORD wSize;
86 struct tagENVENTRY *Prev;
87 struct tagENVENTRY *Next;
88 } ENVENTRY, *LPENVENTRY;
90 LPENVENTRY lpEnvList = NULL;
92 Display *display;
93 Screen *screen;
94 Window rootWindow;
95 int screenWidth = 0, screenHeight = 0; /* Desktop window dimensions */
96 int screenDepth = 0; /* Screen depth to use */
97 int desktopX = 0, desktopY = 0; /* Desktop window position (if any) */
98 int getVersion16 = 0;
99 int getVersion32 = 0;
100 OSVERSIONINFO32A getVersionEx;
102 struct options Options =
103 { /* default options */
104 NULL, /* desktopGeometry */
105 NULL, /* programName */
106 FALSE, /* usePrivateMap */
107 FALSE, /* useFixedMap */
108 FALSE, /* synchronous */
109 FALSE, /* backing store */
110 SW_SHOWNORMAL, /* cmdShow */
111 FALSE,
112 FALSE, /* AllowReadOnly */
113 MODE_ENHANCED, /* Enhanced mode */
114 FALSE, /* IPC enabled */
115 #ifdef DEFAULT_LANG
116 DEFAULT_LANG, /* Default language */
117 #else
118 LANG_En,
119 #endif
120 FALSE, /* Managed windows */
121 FALSE /* Perfect graphics */
125 static XrmOptionDescRec optionsTable[] =
127 { "-backingstore", ".backingstore", XrmoptionNoArg, (caddr_t)"on" },
128 { "-desktop", ".desktop", XrmoptionSepArg, (caddr_t)NULL },
129 { "-depth", ".depth", XrmoptionSepArg, (caddr_t)NULL },
130 { "-display", ".display", XrmoptionSepArg, (caddr_t)NULL },
131 { "-iconic", ".iconic", XrmoptionNoArg, (caddr_t)"on" },
132 { "-ipc", ".ipc", XrmoptionNoArg, (caddr_t)"off"},
133 { "-language", ".language", XrmoptionSepArg, (caddr_t)"En" },
134 { "-name", ".name", XrmoptionSepArg, (caddr_t)NULL },
135 { "-perfect", ".perfect", XrmoptionNoArg, (caddr_t)"on" },
136 { "-privatemap", ".privatemap", XrmoptionNoArg, (caddr_t)"on" },
137 { "-fixedmap", ".fixedmap", XrmoptionNoArg, (caddr_t)"on" },
138 { "-synchronous", ".synchronous", XrmoptionNoArg, (caddr_t)"on" },
139 { "-debug", ".debug", XrmoptionNoArg, (caddr_t)"on" },
140 { "-debugmsg", ".debugmsg", XrmoptionSepArg, (caddr_t)NULL },
141 { "-dll", ".dll", XrmoptionSepArg, (caddr_t)NULL },
142 { "-allowreadonly", ".allowreadonly", XrmoptionNoArg, (caddr_t)"on" },
143 { "-mode", ".mode", XrmoptionSepArg, (caddr_t)NULL },
144 { "-managed", ".managed", XrmoptionNoArg, (caddr_t)"off"},
145 { "-winver", ".winver", XrmoptionSepArg, (caddr_t)NULL }
148 #define NB_OPTIONS (sizeof(optionsTable) / sizeof(optionsTable[0]))
150 #define USAGE \
151 "Usage: %s [options] program_name [arguments]\n" \
152 "\n" \
153 "Options:\n" \
154 " -allowreadonly Read only files may be opened in write mode\n" \
155 " -backingstore Turn on backing store\n" \
156 " -debug Enter debugger before starting application\n" \
157 " -debugmsg name Turn debugging-messages on or off\n" \
158 " -depth n Change the depth to use for multiple-depth screens\n" \
159 " -desktop geom Use a desktop window of the given geometry\n" \
160 " -display name Use the specified display\n" \
161 " -dll name Enable or disable built-in DLLs\n" \
162 " -fixedmap Use a \"standard\" color map\n" \
163 " -iconic Start as an icon\n" \
164 " -ipc Enable IPC facilities\n" \
165 " -language xx Set the language (one of En,Es,De,No,Fr,Fi,Da,Cz,Eo,It,Ko)\n" \
166 " -managed Allow the window manager to manage created windows\n" \
167 " -mode mode Start Wine in a particular mode (standard or enhanced)\n" \
168 " -name name Set the application name\n" \
169 " -perfect Favor correctness over speed for graphical operations\n" \
170 " -privatemap Use a private color map\n" \
171 " -synchronous Turn on synchronous display mode\n" \
172 " -winver Version to imitate (one of win31,win95,nt351)\n"
176 /***********************************************************************
177 * MAIN_Usage
179 #ifndef WINELIB32
180 static void MAIN_Usage( char *name )
182 fprintf( stderr, USAGE, name );
183 exit(1);
185 #endif
188 /***********************************************************************
189 * MAIN_GetProgramName
191 * Get the program name. The name is specified by (in order of precedence):
192 * - the option '-name'.
193 * - the environment variable 'WINE_NAME'.
194 * - the last component of argv[0].
196 static char *MAIN_GetProgramName( int argc, char *argv[] )
198 int i;
199 char *p;
201 for (i = 1; i < argc-1; i++)
202 if (!strcmp( argv[i], "-name" )) return argv[i+1];
203 if ((p = getenv( "WINE_NAME" )) != NULL) return p;
204 if ((p = strrchr( argv[0], '/' )) != NULL) return p+1;
205 return argv[0];
209 /***********************************************************************
210 * MAIN_GetResource
212 * Fetch the value of resource 'name' using the correct instance name.
213 * 'name' must begin with '.' or '*'
215 static int MAIN_GetResource( XrmDatabase db, char *name, XrmValue *value )
217 char *buff_instance, *buff_class;
218 char *dummy;
219 int retval;
221 buff_instance = (char *)xmalloc(strlen(Options.programName)+strlen(name)+1);
222 buff_class = (char *)xmalloc( strlen(WINE_CLASS) + strlen(name) + 1 );
224 strcpy( buff_instance, Options.programName );
225 strcat( buff_instance, name );
226 strcpy( buff_class, WINE_CLASS );
227 strcat( buff_class, name );
228 retval = XrmGetResource( db, buff_instance, buff_class, &dummy, value );
229 free( buff_instance );
230 free( buff_class );
231 return retval;
235 /***********************************************************************
236 * ParseDebugOptions
238 * Turns specific debug messages on or off, according to "options".
239 * Returns TRUE if parsing was successful
241 #ifdef DEBUG_RUNTIME
243 BOOL ParseDebugOptions(char *options)
245 int l;
246 if (strlen(options)<3)
247 return FALSE;
250 if ((*options!='+')&&(*options!='-'))
251 return FALSE;
252 if (strchr(options,','))
253 l=strchr(options,',')-options;
254 else
255 l=strlen(options);
256 if (!lstrncmpi32A(options+1,"all",l-1))
258 int i;
259 for (i=0;i<sizeof(debug_msg_enabled)/sizeof(short);i++)
260 debug_msg_enabled[i]=(*options=='+');
262 else
264 int i;
265 for (i=0;i<sizeof(debug_msg_enabled)/sizeof(short);i++)
266 if (debug_msg_name && (!lstrncmpi32A(options+1,debug_msg_name[i],l-1)))
268 debug_msg_enabled[i]=(*options=='+');
269 break;
271 if (i==sizeof(debug_msg_enabled)/sizeof(short))
272 return FALSE;
274 options+=l;
276 while((*options==',')&&(*(++options)));
277 if (*options)
278 return FALSE;
279 else
280 return TRUE;
283 #endif
286 /***********************************************************************
287 * MAIN_ParseLanguageOption
289 * Parse -language option.
291 static void MAIN_ParseLanguageOption( char *arg )
293 const WINE_LANGUAGE_DEF *p = Languages;
295 Options.language = LANG_En; /* First language */
296 for (;p->name;p++)
298 if (!lstrcmpi32A( p->name, arg ))
300 WINE_LanguageId = p->langid;
301 return;
303 Options.language++;
305 fprintf( stderr, "Invalid language specified '%s'. Supported languages are: ", arg );
306 for (p = Languages; p->name; p++) fprintf( stderr, "%s ", p->name );
307 fprintf( stderr, "\n" );
308 exit(1);
312 /***********************************************************************
313 * MAIN_ParseModeOption
315 * Parse -mode option.
317 static void MAIN_ParseModeOption( char *arg )
319 if (!lstrcmpi32A("enhanced", arg)) Options.mode = MODE_ENHANCED;
320 else if (!lstrcmpi32A("standard", arg)) Options.mode = MODE_STANDARD;
321 else
323 fprintf(stderr, "Invalid mode '%s' specified.\n", arg);
324 fprintf(stderr, "Valid modes are: 'standard', 'enhanced' (default).\n");
325 exit(1);
329 /**********************************************************************
330 * MAIN_ParseVersion
332 static void MAIN_ParseVersion( char *arg )
334 /* If you add any other options,
335 verify the values you return on the real thing */
336 if(strcmp(arg,"win31")==0)
338 getVersion16 = 0x06160A03;
339 /* FIXME: My Win32s installation failed to execute the
340 MSVC 4 test program. So check these values */
341 getVersion32 = 0x80000A03;
342 getVersionEx.dwMajorVersion=3;
343 getVersionEx.dwMinorVersion=10;
344 getVersionEx.dwBuildNumber=0;
345 getVersionEx.dwPlatformId=VER_PLATFORM_WIN32s;
346 strcpy(getVersionEx.szCSDVersion,"Win32s 1.3");
348 else if(strcmp(arg, "win95")==0)
350 getVersion16 = 0x07005F03;
351 getVersion32 = 0xC0000004;
352 getVersionEx.dwMajorVersion=4;
353 getVersionEx.dwMinorVersion=0;
354 getVersionEx.dwBuildNumber=0x40003B6;
355 getVersionEx.dwPlatformId=VER_PLATFORM_WIN32_WINDOWS;
356 strcpy(getVersionEx.szCSDVersion,"");
358 else if(strcmp(arg, "nt351")==0)
360 getVersion16 = 0x05000A03;
361 getVersion32 = 0x04213303;
362 getVersionEx.dwMajorVersion=3;
363 getVersionEx.dwMinorVersion=51;
364 getVersionEx.dwBuildNumber=0x421;
365 getVersionEx.dwPlatformId=VER_PLATFORM_WIN32_NT;
366 strcpy(getVersionEx.szCSDVersion,"Service Pack 2");
368 else fprintf(stderr, "Unknown winver system code - ignored\n");
371 /***********************************************************************
372 * MAIN_ParseOptions
374 * Parse command line options and open display.
376 static void MAIN_ParseOptions( int *argc, char *argv[] )
378 char *display_name = NULL;
379 XrmValue value;
380 XrmDatabase db = XrmGetFileDatabase(WINE_APP_DEFAULTS);
381 int i;
382 char *xrm_string;
384 Options.programName = MAIN_GetProgramName( *argc, argv );
386 /* Get display name from command line */
387 for (i = 1; i < *argc - 1; i++)
388 if (!strcmp( argv[i], "-display" ))
390 display_name = argv[i+1];
391 break;
394 #ifdef WINELIB
395 /* Need to assemble command line and pass it to WinMain */
396 #else
397 if (*argc < 2 || lstrcmpi32A(argv[1], "-h") == 0)
398 MAIN_Usage( argv[0] );
399 #endif
401 /* Open display */
403 if (display_name == NULL &&
404 MAIN_GetResource( db, ".display", &value )) display_name = value.addr;
406 if (!(display = XOpenDisplay( display_name )))
408 fprintf( stderr, "%s: Can't open display: %s\n",
409 argv[0], display_name ? display_name : "(none specified)" );
410 exit(1);
413 /* Merge file and screen databases */
414 if ((xrm_string = XResourceManagerString( display )) != NULL)
416 XrmDatabase display_db = XrmGetStringDatabase( xrm_string );
417 XrmMergeDatabases( display_db, &db );
420 /* Parse command line */
421 XrmParseCommand( &db, optionsTable, NB_OPTIONS,
422 Options.programName, argc, argv );
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, ".allowreadonly", &value ))
438 Options.allowReadOnly = TRUE;
439 if (MAIN_GetResource( db, ".ipc", &value ))
440 Options.ipc = TRUE;
441 if (MAIN_GetResource( db, ".perfect", &value ))
442 Options.perfectGraphics = TRUE;
443 if (MAIN_GetResource( db, ".depth", &value))
444 screenDepth = atoi( value.addr );
445 if (MAIN_GetResource( db, ".desktop", &value))
446 Options.desktopGeometry = value.addr;
447 if (MAIN_GetResource( db, ".language", &value))
448 MAIN_ParseLanguageOption( (char *)value.addr );
449 if (MAIN_GetResource( db, ".managed", &value))
450 Options.managed = TRUE;
451 if (MAIN_GetResource( db, ".mode", &value))
452 MAIN_ParseModeOption( (char *)value.addr );
454 #ifdef DEBUG_RUNTIME
455 if (MAIN_GetResource( db, ".debugoptions", &value))
456 ParseDebugOptions((char*)value.addr);
457 #endif
458 if (MAIN_GetResource( db, ".debugmsg", &value))
460 #ifndef DEBUG_RUNTIME
461 fprintf(stderr,"%s: Option \"-debugmsg\" not implemented.\n" \
462 " Recompile with DEBUG_RUNTIME in include/stddebug.h defined.\n",
463 argv[0]);
464 exit(1);
465 #else
466 if (ParseDebugOptions((char*)value.addr)==FALSE)
468 int i;
469 fprintf(stderr,"%s: Syntax: -debugmsg +xxx,... or -debugmsg -xxx,...\n",argv[0]);
470 fprintf(stderr,"Example: -debugmsg +all,-heap turn on all messages except heap messages\n");
471 fprintf(stderr,"Available message types:\n");
472 fprintf(stderr,"%-9s ","all");
473 for(i=0;i<sizeof(debug_msg_enabled)/sizeof(short);i++)
474 if(debug_msg_name[i])
475 fprintf(stderr,"%-9s%c",debug_msg_name[i],
476 (((i+2)%8==0)?'\n':' '));
477 fprintf(stderr,"\n\n");
478 exit(1);
480 #endif
483 if(MAIN_GetResource( db, ".dll", &value))
485 #ifndef WINELIB
486 if (!BUILTIN_ParseDLLOptions( (char*)value.addr ))
488 fprintf(stderr,"%s: Syntax: -dll +xxx,... or -dll -xxx,...\n",argv[0]);
489 fprintf(stderr,"Example: -dll -ole2 Do not use emulated OLE2.DLL\n");
490 fprintf(stderr,"Available DLLs:\n");
491 BUILTIN_PrintDLLs();
492 exit(1);
494 #else
495 fprintf(stderr,"-dll not supported in libwine\n");
496 #endif
499 if(MAIN_GetResource( db, ".winver", &value))
500 MAIN_ParseVersion( (char*)value.addr );
504 /***********************************************************************
505 * MAIN_CreateDesktop
507 static void MAIN_CreateDesktop( int argc, char *argv[] )
509 int flags;
510 unsigned int width = 640, height = 480; /* Default size = 640x480 */
511 char *name = "Wine desktop";
512 XSizeHints *size_hints;
513 XWMHints *wm_hints;
514 XClassHint *class_hints;
515 XSetWindowAttributes win_attr;
516 XTextProperty window_name;
517 Atom XA_WM_DELETE_WINDOW;
519 flags = XParseGeometry( Options.desktopGeometry,
520 &desktopX, &desktopY, &width, &height );
521 screenWidth = width;
522 screenHeight = height;
524 /* Create window */
526 win_attr.event_mask = ExposureMask | KeyPressMask | KeyReleaseMask |
527 PointerMotionMask | ButtonPressMask |
528 ButtonReleaseMask | EnterWindowMask |
529 StructureNotifyMask;
530 win_attr.cursor = XCreateFontCursor( display, XC_top_left_arrow );
532 rootWindow = XCreateWindow( display, DefaultRootWindow(display),
533 desktopX, desktopY, width, height, 0,
534 CopyFromParent, InputOutput, CopyFromParent,
535 CWEventMask | CWCursor, &win_attr );
537 /* Set window manager properties */
539 size_hints = XAllocSizeHints();
540 wm_hints = XAllocWMHints();
541 class_hints = XAllocClassHint();
542 if (!size_hints || !wm_hints || !class_hints)
544 fprintf( stderr, "Not enough memory for window manager hints.\n" );
545 exit(1);
547 size_hints->min_width = size_hints->max_width = width;
548 size_hints->min_height = size_hints->max_height = height;
549 size_hints->flags = PMinSize | PMaxSize;
550 if (flags & (XValue | YValue)) size_hints->flags |= USPosition;
551 if (flags & (WidthValue | HeightValue)) size_hints->flags |= USSize;
552 else size_hints->flags |= PSize;
554 wm_hints->flags = InputHint | StateHint;
555 wm_hints->input = True;
556 wm_hints->initial_state = NormalState;
557 class_hints->res_name = argv[0];
558 class_hints->res_class = "Wine";
560 XStringListToTextProperty( &name, 1, &window_name );
561 XSetWMProperties( display, rootWindow, &window_name, &window_name,
562 argv, argc, size_hints, wm_hints, class_hints );
563 XA_WM_DELETE_WINDOW = XInternAtom( display, "WM_DELETE_WINDOW", False );
564 XSetWMProtocols( display, rootWindow, &XA_WM_DELETE_WINDOW, 1 );
565 XFree( size_hints );
566 XFree( wm_hints );
567 XFree( class_hints );
569 /* Map window */
571 XMapWindow( display, rootWindow );
575 XKeyboardState keyboard_state;
577 /***********************************************************************
578 * MAIN_SaveSetup
580 static void MAIN_SaveSetup(void)
582 XGetKeyboardControl(display, &keyboard_state);
585 /***********************************************************************
586 * MAIN_RestoreSetup
588 static void MAIN_RestoreSetup(void)
590 XKeyboardControl keyboard_value;
592 keyboard_value.key_click_percent = keyboard_state.key_click_percent;
593 keyboard_value.bell_percent = keyboard_state.bell_percent;
594 keyboard_value.bell_pitch = keyboard_state.bell_pitch;
595 keyboard_value.bell_duration = keyboard_state.bell_duration;
596 keyboard_value.auto_repeat_mode = keyboard_state.global_auto_repeat;
598 XChangeKeyboardControl(display, KBKeyClickPercent | KBBellPercent |
599 KBBellPitch | KBBellDuration | KBAutoRepeatMode, &keyboard_value);
603 static void called_at_exit(void)
605 MAIN_RestoreSetup();
606 COLOR_Cleanup();
607 WINSOCK_Shutdown();
610 /***********************************************************************
611 * MAIN_WineInit
613 * Wine initialisation and command-line parsing
615 BOOL32 MAIN_WineInit( int *argc, char *argv[] )
617 int depth_count, i;
618 int *depth_list;
619 struct timeval tv;
621 extern int _WinMain(int argc, char **argv);
623 #ifdef MALLOC_DEBUGGING
624 char *trace;
626 mcheck(NULL);
627 if (!(trace = getenv("MALLOC_TRACE")))
629 fprintf( stderr, "MALLOC_TRACE not set. No trace generated\n" );
631 else
633 fprintf( stderr, "malloc trace goes to %s\n", trace );
634 mtrace();
636 #endif
638 setbuf(stdout,NULL);
639 setbuf(stderr,NULL);
641 setlocale(LC_CTYPE,"");
642 gettimeofday( &tv, NULL);
643 MSG_WineStartTicks = (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
645 XrmInitialize();
647 MAIN_ParseOptions( argc, argv );
649 if (Options.desktopGeometry && Options.managed)
651 fprintf( stderr, "%s: -managed and -desktop options cannot be used together\n",
652 Options.programName );
653 exit(1);
656 screen = DefaultScreenOfDisplay( display );
657 screenWidth = WidthOfScreen( screen );
658 screenHeight = HeightOfScreen( screen );
659 if (screenDepth) /* -depth option specified */
661 depth_list = XListDepths(display,DefaultScreen(display),&depth_count);
662 for (i = 0; i < depth_count; i++)
663 if (depth_list[i] == screenDepth) break;
664 XFree( depth_list );
665 if (i >= depth_count)
667 fprintf( stderr, "%s: Depth %d not supported on this screen.\n",
668 Options.programName, screenDepth );
669 exit(1);
672 else screenDepth = DefaultDepthOfScreen( screen );
673 if (Options.synchronous) XSynchronize( display, True );
674 if (Options.desktopGeometry) MAIN_CreateDesktop( *argc, argv );
675 else rootWindow = DefaultRootWindow( display );
677 MAIN_SaveSetup();
678 atexit(called_at_exit);
679 return TRUE;
683 /***********************************************************************
684 * MessageBeep (USER.104)
686 void MessageBeep(WORD i)
688 XBell(display, 100);
692 /***********************************************************************
693 * Beep (KERNEL32.11)
695 BOOL32 Beep( DWORD dwFreq, DWORD dwDur )
697 /* dwFreq and dwDur are ignored by Win95 */
698 XBell(display, 100);
699 return TRUE;
703 /***********************************************************************
704 * GetVersion16 (KERNEL.3)
706 LONG GetVersion16(void)
708 if (getVersion16) return getVersion16;
709 return MAKELONG( WINVERSION, WINDOSVER );
713 /***********************************************************************
714 * GetVersion32
716 LONG GetVersion32(void)
718 if (getVersion32) return getVersion32;
719 return MAKELONG( 4, DOSVERSION);
723 /***********************************************************************
724 * GetVersionExA
726 BOOL32 GetVersionEx32A(OSVERSIONINFO32A *v)
728 if(v->dwOSVersionInfoSize!=sizeof(OSVERSIONINFO32A))
730 fprintf(stddeb,"wrong OSVERSIONINFO size from app");
731 return FALSE;
733 if(!getVersion32)
735 /* Return something like NT 3.5 */
736 v->dwMajorVersion = 3;
737 v->dwMinorVersion = 5;
738 v->dwBuildNumber = 42;
739 v->dwPlatformId = VER_PLATFORM_WIN32_NT;
740 strcpy(v->szCSDVersion, "Wine is not an emulator");
741 return TRUE;
743 v->dwMajorVersion = getVersionEx.dwMajorVersion;
744 v->dwMinorVersion = getVersionEx.dwMinorVersion;
745 v->dwBuildNumber = getVersionEx.dwBuildNumber;
746 v->dwPlatformId = getVersionEx.dwPlatformId;
747 strcpy(v->szCSDVersion, getVersionEx.szCSDVersion);
748 return TRUE;
752 /***********************************************************************
753 * GetVersionExW
755 BOOL32 GetVersionEx32W(OSVERSIONINFO32W *v)
757 OSVERSIONINFO32A v1;
758 if(v->dwOSVersionInfoSize!=sizeof(OSVERSIONINFO32W))
760 fprintf(stddeb,"wrong OSVERSIONINFO size from app");
761 return FALSE;
763 v1.dwOSVersionInfoSize=sizeof(v1);
764 GetVersionEx32A(&v1);
765 v->dwMajorVersion = v1.dwMajorVersion;
766 v->dwMinorVersion = v1.dwMinorVersion;
767 v->dwBuildNumber = v1.dwBuildNumber;
768 v->dwPlatformId = v1.dwPlatformId;
769 lstrcpyAtoW( v->szCSDVersion, v1.szCSDVersion );
770 return TRUE;
773 /***********************************************************************
774 * GetWinFlags (KERNEL.132)
776 LONG GetWinFlags(void)
778 static const long cpuflags[5] =
779 { WF_CPU086, WF_CPU186, WF_CPU286, WF_CPU386, WF_CPU486 };
781 long result = 0;
783 /* There doesn't seem to be any Pentium flag. */
784 #ifndef WINELIB
785 long cpuflag = cpuflags[MIN (runtime_cpu (), 4)];
786 #else
787 long cpuflag = cpuflags[4];
788 #endif
790 switch(Options.mode) {
791 case MODE_STANDARD:
792 result = (WF_STANDARD | cpuflag | WF_PMODE | WF_80x87);
793 break;
795 case MODE_ENHANCED:
796 result = (WF_ENHANCED | cpuflag | WF_PMODE | WF_80x87 | WF_PAGING);
797 break;
799 default:
800 fprintf(stderr, "Unknown mode set? This shouldn't happen. Check GetWinFlags()!\n");
801 break;
803 if( getVersionEx.dwPlatformId == VER_PLATFORM_WIN32_NT )
804 result |= 0x4000; /* undocumented WF_WINNT */
805 return result;
808 /***********************************************************************
809 * SetEnvironment (GDI.132)
811 int SetEnvironment(LPCSTR lpPortName, LPCSTR lpEnviron, WORD nCount)
813 LPENVENTRY lpNewEnv;
814 LPENVENTRY lpEnv = lpEnvList;
815 dprintf_env(stddeb, "SetEnvironment('%s', '%s', %d) !\n",
816 lpPortName, lpEnviron, nCount);
817 if (lpPortName == NULL) return -1;
818 while (lpEnv != NULL) {
819 if (lpEnv->Name != NULL && strcmp(lpEnv->Name, lpPortName) == 0) {
820 if (nCount == 0 || lpEnviron == NULL) {
821 if (lpEnv->Prev != NULL) lpEnv->Prev->Next = lpEnv->Next;
822 if (lpEnv->Next != NULL) lpEnv->Next->Prev = lpEnv->Prev;
823 free(lpEnv->Value);
824 free(lpEnv->Name);
825 free(lpEnv);
826 dprintf_env(stddeb, "SetEnvironment() // entry deleted !\n");
827 return -1;
829 free(lpEnv->Value);
830 lpEnv->Value = malloc(nCount);
831 if (lpEnv->Value == NULL) {
832 dprintf_env(stddeb, "SetEnvironment() // Error allocating entry value !\n");
833 return 0;
835 memcpy(lpEnv->Value, lpEnviron, nCount);
836 lpEnv->wSize = nCount;
837 dprintf_env(stddeb, "SetEnvironment() // entry modified !\n");
838 return nCount;
840 if (lpEnv->Next == NULL) break;
841 lpEnv = lpEnv->Next;
843 if (nCount == 0 || lpEnviron == NULL) return -1;
844 dprintf_env(stddeb, "SetEnvironment() // new entry !\n");
845 lpNewEnv = malloc(sizeof(ENVENTRY));
846 if (lpNewEnv == NULL) {
847 dprintf_env(stddeb, "SetEnvironment() // Error allocating new entry !\n");
848 return 0;
850 if (lpEnvList == NULL) {
851 lpEnvList = lpNewEnv;
852 lpNewEnv->Prev = NULL;
854 else
856 lpEnv->Next = lpNewEnv;
857 lpNewEnv->Prev = lpEnv;
859 lpNewEnv->Next = NULL;
860 lpNewEnv->Name = malloc(strlen(lpPortName) + 1);
861 if (lpNewEnv->Name == NULL) {
862 dprintf_env(stddeb, "SetEnvironment() // Error allocating entry name !\n");
863 return 0;
865 strcpy(lpNewEnv->Name, lpPortName);
866 lpNewEnv->Value = malloc(nCount);
867 if (lpNewEnv->Value == NULL) {
868 dprintf_env(stddeb, "SetEnvironment() // Error allocating entry value !\n");
869 return 0;
871 memcpy(lpNewEnv->Value, lpEnviron, nCount);
872 lpNewEnv->wSize = nCount;
873 return nCount;
877 /***********************************************************************
878 * SetEnvironmentVariable32A (KERNEL32.484)
880 BOOL32 SetEnvironmentVariable32A( LPCSTR lpName, LPCSTR lpValue )
882 int rc;
884 rc = SetEnvironment(lpName, lpValue, lpValue?(strlen(lpValue)+1):0);
885 return (rc>0)?TRUE:FALSE;
889 /***********************************************************************
890 * SetEnvironmentVariable32W (KERNEL32.485)
892 BOOL32 SetEnvironmentVariable32W( LPCWSTR lpName, LPCWSTR lpValue )
894 LPSTR lpNameA = HEAP_strdupWtoA( GetProcessHeap(), 0, lpName );
895 LPSTR lpValueA = HEAP_strdupWtoA( GetProcessHeap(), 0, lpValue );
896 BOOL32 ret = SetEnvironmentVariable32A(lpNameA,lpValueA);
897 HeapFree( GetProcessHeap(), 0, lpNameA );
898 HeapFree( GetProcessHeap(), 0, lpValueA );
899 return ret;
903 /***********************************************************************
904 * GetEnvironment (GDI.134)
906 int GetEnvironment(LPSTR lpPortName, LPSTR lpEnviron, WORD nMaxSiz)
908 WORD nCount;
909 LPENVENTRY lpEnv = lpEnvList;
911 dprintf_env(stddeb, "GetEnvironment('%s', '%s', %d) !\n",
912 lpPortName, lpEnviron, nMaxSiz);
913 while (lpEnv != NULL) {
914 if (lpEnv->Name != NULL && strcmp(lpEnv->Name, lpPortName) == 0) {
915 if( lpEnviron == NULL ) return lpEnv->wSize;
916 nCount = MIN(nMaxSiz, lpEnv->wSize);
917 memcpy(lpEnviron, lpEnv->Value, nCount);
918 dprintf_env(stddeb, "GetEnvironment() // found '%s' !\n", lpEnv->Value);
919 return nCount;
921 lpEnv = lpEnv->Next;
923 dprintf_env(stddeb, "GetEnvironment() // not found !\n");
924 return 0;
927 /***********************************************************************
928 * GetEnvironmentVariable32A (KERNEL32.213)
930 DWORD GetEnvironmentVariable32A( LPSTR lpName, LPSTR lpValue, DWORD size )
932 return GetEnvironment(lpName, lpValue, size);
935 /***********************************************************************
936 * GetEnvironmentVariable32W (KERNEL32.214)
938 DWORD GetEnvironmentVariable32W( LPWSTR nameW, LPWSTR valW, DWORD size )
940 LPSTR name = HEAP_strdupWtoA( GetProcessHeap(), 0, nameW );
941 LPSTR val = valW ? HeapAlloc( GetProcessHeap(), 0, size ) : NULL;
942 DWORD res = GetEnvironment( name, val, size );
943 HeapFree( GetProcessHeap(), 0, name );
944 if (val)
946 lstrcpyAtoW( valW, val );
947 HeapFree( GetProcessHeap(), 0, val );
949 return res;
952 /***********************************************************************
953 * GetEnvironmentStrings (KERNEL32.210)
955 LPVOID GetEnvironmentStrings(void)
957 int count;
958 LPENVENTRY lpEnv;
959 char *envtable, *envptr;
961 /* Count the total number of bytes we'll need for the string
962 * table. Include the trailing nuls and the final double nul.
964 count = 1;
965 lpEnv = lpEnvList;
966 while(lpEnv != NULL)
968 if(lpEnv->Name != NULL)
970 count += strlen(lpEnv->Name) + 1;
971 count += strlen(lpEnv->Value) + 1;
973 lpEnv = lpEnv->Next;
976 envtable = malloc(count);
977 if(envtable)
979 lpEnv = lpEnvList;
980 envptr = envtable;
982 while(lpEnv != NULL)
984 if(lpEnv->Name != NULL)
986 count = sprintf(envptr, "%s=%s", lpEnv->Name, lpEnv->Value);
987 envptr += count + 1;
989 lpEnv = lpEnv->Next;
991 *envptr = '\0';
994 return envtable;
998 LPVOID GetEnvironmentStringsW(void)
1000 int count,len;
1001 LPENVENTRY lpEnv;
1002 char *envtable, *envptr;
1003 WCHAR *wenvtable;
1005 /* Count the total number of bytes we'll need for the string
1006 * table. Include the trailing nuls and the final double nul.
1008 count = 1;
1009 lpEnv = lpEnvList;
1010 while(lpEnv != NULL)
1012 if(lpEnv->Name != NULL)
1014 count += strlen(lpEnv->Name) + 1;
1015 count += strlen(lpEnv->Value) + 1;
1017 lpEnv = lpEnv->Next;
1020 len=count;
1021 envtable = malloc(count);
1022 if(envtable)
1024 lpEnv = lpEnvList;
1025 envptr = envtable;
1027 while(lpEnv != NULL)
1029 if(lpEnv->Name != NULL)
1031 count = sprintf(envptr, "%s=%s", lpEnv->Name, lpEnv->Value);
1032 envptr += count + 1;
1034 lpEnv = lpEnv->Next;
1036 *envptr = '\0';
1039 wenvtable = malloc(2*len);
1040 for(count=0;count<len;count++)
1041 wenvtable[count]=(WCHAR)envtable[count];
1042 free(envtable);
1044 return wenvtable;
1047 void FreeEnvironmentStringsA(void *e)
1049 free(e);
1052 void FreeEnvironmentStringsW(void* e)
1054 free(e);
1057 /***********************************************************************
1058 * GetTimerResolution (USER.14)
1060 LONG GetTimerResolution(void)
1062 return (1000);
1065 /***********************************************************************
1066 * SystemParametersInfo32A (USER32.539)
1068 BOOL32 SystemParametersInfo32A( UINT32 uAction, UINT32 uParam,
1069 LPVOID lpvParam, UINT32 fuWinIni )
1071 return SystemParametersInfo16(uAction,uParam,lpvParam,fuWinIni);
1075 /***********************************************************************
1076 * SystemParametersInfo16 (USER.483)
1078 BOOL16 SystemParametersInfo16( UINT16 uAction, UINT16 uParam,
1079 LPVOID lpvParam, UINT16 fuWinIni )
1081 int timeout, temp;
1082 char buffer[256];
1083 XKeyboardState keyboard_state;
1084 XKeyboardControl keyboard_value;
1087 switch (uAction)
1089 case SPI_GETBEEP:
1090 XGetKeyboardControl(display, &keyboard_state);
1091 if (keyboard_state.bell_percent == 0)
1092 *(BOOL *) lpvParam = FALSE;
1093 else
1094 *(BOOL *) lpvParam = TRUE;
1095 break;
1097 case SPI_GETBORDER:
1098 *(INT *)lpvParam = GetSystemMetrics( SM_CXFRAME );
1099 break;
1101 case SPI_GETFASTTASKSWITCH:
1102 if ( GetProfileInt32A( "windows", "CoolSwitch", 1 ) == 1 )
1103 *(BOOL *) lpvParam = TRUE;
1104 else
1105 *(BOOL *) lpvParam = FALSE;
1106 break;
1108 case SPI_GETGRIDGRANULARITY:
1109 *(INT *) lpvParam = GetProfileInt32A( "desktop",
1110 "GridGranularity",
1111 1 );
1112 break;
1114 case SPI_GETICONTITLEWRAP:
1115 *(BOOL *) lpvParam = GetProfileInt32A( "desktop",
1116 "IconTitleWrap",
1117 TRUE );
1118 break;
1120 case SPI_GETKEYBOARDDELAY:
1121 *(INT *) lpvParam = GetProfileInt32A( "keyboard",
1122 "KeyboardDelay", 1 );
1123 break;
1125 case SPI_GETKEYBOARDSPEED:
1126 *(WORD *) lpvParam = GetProfileInt32A( "keyboard",
1127 "KeyboardSpeed",
1128 30 );
1129 break;
1131 case SPI_GETMENUDROPALIGNMENT:
1132 *(BOOL *) lpvParam = GetSystemMetrics( SM_MENUDROPALIGNMENT ); /* XXX check this */
1133 break;
1135 case SPI_GETSCREENSAVEACTIVE:
1136 if ( GetProfileInt32A( "windows", "ScreenSaveActive", 1 ) == 1 )
1137 *(BOOL *) lpvParam = TRUE;
1138 else
1139 *(BOOL *) lpvParam = FALSE;
1140 break;
1142 case SPI_GETSCREENSAVETIMEOUT:
1143 /* FIXME GetProfileInt( "windows", "ScreenSaveTimeout", 300 ); */
1144 XGetScreenSaver(display, &timeout, &temp,&temp,&temp);
1145 *(INT *) lpvParam = timeout * 1000;
1146 break;
1148 case SPI_ICONHORIZONTALSPACING:
1149 /* FIXME Get/SetProfileInt */
1150 if (lpvParam == NULL)
1151 /*SetSystemMetrics( SM_CXICONSPACING, uParam )*/ ;
1152 else
1153 *(INT *) lpvParam = GetSystemMetrics( SM_CXICONSPACING );
1154 break;
1156 case SPI_ICONVERTICALSPACING:
1157 /* FIXME Get/SetProfileInt */
1158 if (lpvParam == NULL)
1159 /*SetSystemMetrics( SM_CYICONSPACING, uParam )*/ ;
1160 else
1161 *(INT *) lpvParam = GetSystemMetrics(SM_CYICONSPACING);
1162 break;
1164 case SPI_SETBEEP:
1165 if (uParam == TRUE)
1166 keyboard_value.bell_percent = -1;
1167 else
1168 keyboard_value.bell_percent = 0;
1169 XChangeKeyboardControl(display, KBBellPercent,
1170 &keyboard_value);
1171 break;
1173 case SPI_SETSCREENSAVEACTIVE:
1174 if (uParam == TRUE)
1175 XActivateScreenSaver(display);
1176 else
1177 XResetScreenSaver(display);
1178 break;
1180 case SPI_SETSCREENSAVETIMEOUT:
1181 XSetScreenSaver(display, uParam, 60, DefaultBlanking,
1182 DefaultExposures);
1183 break;
1185 case SPI_SETDESKWALLPAPER:
1186 return (SetDeskWallPaper32((LPSTR) lpvParam));
1187 break;
1189 case SPI_SETDESKPATTERN:
1190 if ((INT16)uParam == -1) {
1191 GetProfileString32A("Desktop", "Pattern",
1192 "170 85 170 85 170 85 170 85",
1193 buffer, sizeof(buffer) );
1194 return (DESKTOP_SetPattern((LPSTR) buffer));
1195 } else
1196 return (DESKTOP_SetPattern((LPSTR) lpvParam));
1197 break;
1199 case SPI_GETICONTITLELOGFONT:
1201 /* FIXME GetProfileString32A( "?", "?", "?" ) */
1202 LPLOGFONT16 lpLogFont = (LPLOGFONT16)lpvParam;
1203 lpLogFont->lfHeight = 10;
1204 lpLogFont->lfWidth = 0;
1205 lpLogFont->lfEscapement = lpLogFont->lfOrientation = 0;
1206 lpLogFont->lfWeight = FW_NORMAL;
1207 lpLogFont->lfItalic = FALSE;
1208 lpLogFont->lfStrikeOut = FALSE;
1209 lpLogFont->lfUnderline = FALSE;
1210 lpLogFont->lfCharSet = ANSI_CHARSET;
1211 lpLogFont->lfOutPrecision = OUT_DEFAULT_PRECIS;
1212 lpLogFont->lfClipPrecision = CLIP_DEFAULT_PRECIS;
1213 lpLogFont->lfPitchAndFamily = DEFAULT_PITCH | FF_SWISS;
1214 break;
1217 case SPI_LANGDRIVER:
1218 case SPI_SETBORDER:
1219 case SPI_SETDOUBLECLKHEIGHT:
1220 case SPI_SETDOUBLECLICKTIME:
1221 case SPI_SETDOUBLECLKWIDTH:
1222 case SPI_SETFASTTASKSWITCH:
1223 case SPI_SETKEYBOARDDELAY:
1224 case SPI_SETKEYBOARDSPEED:
1225 fprintf(stderr, "SystemParametersInfo: option %d ignored.\n", uAction);
1226 break;
1228 case SPI_GETWORKAREA:
1229 SetRect16( (RECT16 *)lpvParam, 0, 0,
1230 GetSystemMetrics( SM_CXSCREEN ),
1231 GetSystemMetrics( SM_CYSCREEN ) );
1232 break;
1234 default:
1235 fprintf(stderr, "SystemParametersInfo: unknown option %d.\n", uAction);
1236 break;
1238 return 1;
1241 /***********************************************************************
1242 * SystemParametersInfo32W (USER32.540)
1244 BOOL32 SystemParametersInfo32W( UINT32 uAction, UINT32 uParam,
1245 LPVOID lpvParam, UINT32 fuWinIni )
1247 char buffer[256];
1249 switch (uAction)
1251 case SPI_SETDESKWALLPAPER:
1252 if (lpvParam)
1254 lstrcpynWtoA(buffer,(LPWSTR)lpvParam,sizeof(buffer));
1255 return SetDeskWallPaper32(buffer);
1257 return SetDeskWallPaper32(NULL);
1259 case SPI_SETDESKPATTERN:
1260 if ((INT) uParam == -1)
1262 GetProfileString32A("Desktop", "Pattern",
1263 "170 85 170 85 170 85 170 85",
1264 buffer, sizeof(buffer) );
1265 return (DESKTOP_SetPattern((LPSTR) buffer));
1267 if (lpvParam)
1269 lstrcpynWtoA(buffer,(LPWSTR)lpvParam,sizeof(buffer));
1270 return DESKTOP_SetPattern(buffer);
1272 return DESKTOP_SetPattern(NULL);
1274 case SPI_GETICONTITLELOGFONT:
1276 /* FIXME GetProfileString32A( "?", "?", "?" ) */
1277 LPLOGFONT32W lpLogFont = (LPLOGFONT32W)lpvParam;
1278 lpLogFont->lfHeight = 10;
1279 lpLogFont->lfWidth = 0;
1280 lpLogFont->lfEscapement = lpLogFont->lfOrientation = 0;
1281 lpLogFont->lfWeight = FW_NORMAL;
1282 lpLogFont->lfItalic = lpLogFont->lfStrikeOut = lpLogFont->lfUnderline = FALSE;
1283 lpLogFont->lfCharSet = ANSI_CHARSET;
1284 lpLogFont->lfOutPrecision = OUT_DEFAULT_PRECIS;
1285 lpLogFont->lfClipPrecision = CLIP_DEFAULT_PRECIS;
1286 lpLogFont->lfPitchAndFamily = DEFAULT_PITCH | FF_SWISS;
1288 break;
1290 default:
1291 return SystemParametersInfo32A(uAction,uParam,lpvParam,fuWinIni);
1294 return TRUE;
1298 /***********************************************************************
1299 * COPY (GDI.250)
1301 void Copy(LPVOID lpSource, LPVOID lpDest, WORD nBytes)
1303 memcpy(lpDest, lpSource, nBytes);
1306 /***********************************************************************
1307 * SWAPMOUSEBUTTON (USER.186)
1309 BOOL SwapMouseButton(BOOL fSwap)
1311 return 0; /* don't swap */
1314 /***********************************************************************
1315 * FileCDR (KERNEL.130)
1317 void FileCDR(FARPROC16 x)
1319 printf("FileCDR(%8x)\n", (int) x);
1322 /***********************************************************************
1323 * GetWinDebugInfo (KERNEL.355)
1325 BOOL GetWinDebugInfo(WINDEBUGINFO *lpwdi, UINT flags)
1327 printf("GetWinDebugInfo(%8lx,%d) stub returning 0\n", (unsigned long)lpwdi, flags);
1328 /* 0 means not in debugging mode/version */
1329 /* Can this type of debugging be used in wine ? */
1330 /* Constants: WDI_OPTIONS WDI_FILTER WDI_ALLOCBREAK */
1331 return 0;
1334 /***********************************************************************
1335 * GetWinDebugInfo (KERNEL.355)
1337 BOOL SetWinDebugInfo(WINDEBUGINFO *lpwdi)
1339 printf("SetWinDebugInfo(%8lx) stub returning 0\n", (unsigned long)lpwdi);
1340 /* 0 means not in debugging mode/version */
1341 /* Can this type of debugging be used in wine ? */
1342 /* Constants: WDI_OPTIONS WDI_FILTER WDI_ALLOCBREAK */
1343 return 0;