Release 940405
[wine/gsoc-2012-control.git] / misc / main.c
blobc98b72381a3de7dcd205622fdd9c3398560a8e67
1 /*
2 * Main function.
4 * Copyright 1994 Alexandre Julliard
5 */
7 static char Copyright[] = "Copyright Alexandre Julliard, 1994";
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <X11/Xlib.h>
13 #include <X11/Xresource.h>
14 #include <X11/Xutil.h>
15 #include <X11/cursorfont.h>
16 #include "msdos.h"
17 #include "windows.h"
18 #include "options.h"
19 #include "prototypes.h"
21 #define WINE_CLASS "Wine" /* Class name for resources */
23 Display * XT_display; /* To be removed */
24 Screen * XT_screen; /* To be removed */
26 Display *display;
27 Screen *screen;
28 Window rootWindow;
29 int screenWidth = 0, screenHeight = 0; /* Desktop window dimensions */
30 int screenDepth = 0; /* Screen depth to use */
31 int desktopX = 0, desktopY = 0; /* Desktop window position (if any) */
33 char *ProgramName; /* Used by resource.c with WINELIB */
35 struct options Options =
36 { /* default options */
37 NULL, /* spyFilename */
38 NULL, /* desktopGeometry */
39 NULL, /* programName */
40 FALSE, /* usePrivateMap */
41 FALSE, /* synchronous */
42 SW_SHOWNORMAL, /* cmdShow */
43 FALSE
47 static XrmOptionDescRec optionsTable[] =
49 { "-desktop", ".desktop", XrmoptionSepArg, (caddr_t)NULL },
50 { "-depth", ".depth", XrmoptionSepArg, (caddr_t)NULL },
51 { "-display", ".display", XrmoptionSepArg, (caddr_t)NULL },
52 { "-iconic", ".iconic", XrmoptionNoArg, (caddr_t)"on" },
53 { "-name", ".name", XrmoptionSepArg, (caddr_t)NULL },
54 { "-privatemap", ".privatemap", XrmoptionNoArg, (caddr_t)"on" },
55 { "-synchronous", ".synchronous", XrmoptionNoArg, (caddr_t)"on" },
56 { "-spy", ".spy", XrmoptionSepArg, (caddr_t)NULL },
57 { "-relaydbg", ".relaydbg", XrmoptionNoArg, (caddr_t)"on" }
60 #define NB_OPTIONS (sizeof(optionsTable) / sizeof(optionsTable[0]))
62 #define USAGE \
63 "Usage: %s [options] program_name [arguments]\n" \
64 "\n" \
65 "Options:\n" \
66 " -depth n Change the depth to use for multiple-depth screens\n" \
67 " -desktop geom Use a desktop window of the given geometry\n" \
68 " -display name Use the specified display\n" \
69 " -iconic Start as an icon\n" \
70 " -name name Set the application name\n" \
71 " -privatemap Use a private color map\n" \
72 " -synchronous Turn on synchronous display mode\n" \
73 " -spy file Turn on message spying to the specified file\n" \
74 " -relaydbg Display call relay information\n"
77 /***********************************************************************
78 * MAIN_Usage
80 static void MAIN_Usage( char *name )
82 fprintf( stderr, USAGE, name );
83 exit(1);
87 /***********************************************************************
88 * MAIN_GetProgramName
90 * Get the program name. The name is specified by (in order of precedence):
91 * - the option '-name'.
92 * - the environment variable 'WINE_NAME'.
93 * - the last component of argv[0].
95 static char *MAIN_GetProgramName( int argc, char *argv[] )
97 int i;
98 char *p;
100 for (i = 1; i < argc-1; i++)
101 if (!strcmp( argv[i], "-name" )) return argv[i+1];
102 if ((p = getenv( "WINE_NAME" )) != NULL) return p;
103 if ((p = strrchr( argv[0], '/' )) != NULL) return p+1;
104 return argv[0];
108 /***********************************************************************
109 * MAIN_GetResource
111 * Fetch the value of resource 'name' using the correct instance name.
112 * 'name' must begin with '.' or '*'
114 static int MAIN_GetResource( XrmDatabase db, char *name, XrmValue *value )
116 char *buff_instance, *buff_class;
117 char *dummy;
118 int retval;
120 buff_instance = (char *)malloc(strlen(Options.programName)+strlen(name)+1);
121 buff_class = (char *)malloc( strlen(WINE_CLASS) + strlen(name) + 1 );
123 strcpy( buff_instance, Options.programName );
124 strcat( buff_instance, name );
125 strcpy( buff_class, WINE_CLASS );
126 strcat( buff_class, name );
127 retval = XrmGetResource( db, buff_instance, buff_class, &dummy, value );
128 free( buff_instance );
129 free( buff_class );
130 return retval;
134 /***********************************************************************
135 * MAIN_ParseOptions
137 * Parse command line options and open display.
139 static void MAIN_ParseOptions( int *argc, char *argv[] )
141 char *display_name;
142 XrmValue value;
143 XrmDatabase db = NULL;
145 /* Parse command line */
147 Options.programName = MAIN_GetProgramName( *argc, argv );
148 XrmParseCommand( &db, optionsTable, NB_OPTIONS,
149 Options.programName, argc, argv );
150 #ifdef WINELIB
151 /* Need to assemble command line and pass it to WinMain */
152 #else
153 if (*argc < 2 || strcasecmp(argv[1], "-h") == 0)
154 MAIN_Usage( argv[0] );
155 #endif
157 /* Open display */
159 if (MAIN_GetResource( db, ".display", &value )) display_name = value.addr;
160 else display_name = NULL;
162 if (!(display = XOpenDisplay( display_name )))
164 fprintf( stderr, "%s: Can't open display: %s\n",
165 argv[0], display_name ? display_name : "" );
166 exit(1);
169 /* Get all options */
171 if (MAIN_GetResource( db, ".iconic", &value ))
172 Options.cmdShow = SW_SHOWMINIMIZED;
173 if (MAIN_GetResource( db, ".privatemap", &value ))
174 Options.usePrivateMap = TRUE;
175 if (MAIN_GetResource( db, ".synchronous", &value ))
176 Options.synchronous = TRUE;
177 if (MAIN_GetResource( db, ".relaydbg", &value ))
178 Options.relay_debug = TRUE;
179 if (MAIN_GetResource( db, ".spy", &value))
180 Options.spyFilename = value.addr;
181 if (MAIN_GetResource( db, ".depth", &value))
182 screenDepth = atoi( value.addr );
183 if (MAIN_GetResource( db, ".desktop", &value))
184 Options.desktopGeometry = value.addr;
188 /***********************************************************************
189 * MAIN_CreateDesktop
191 static void MAIN_CreateDesktop( int argc, char *argv[] )
193 int flags;
194 unsigned int width = 640, height = 480; /* Default size = 640x480 */
195 char *name = "Wine desktop";
196 XSizeHints size_hints;
197 XWMHints wm_hints;
198 XClassHint class_hints;
199 XSetWindowAttributes win_attr;
200 XTextProperty window_name;
202 flags = XParseGeometry( Options.desktopGeometry,
203 &desktopX, &desktopY, &width, &height );
204 screenWidth = width;
205 screenHeight = height;
207 /* Create window */
209 win_attr.event_mask = ExposureMask | KeyPressMask | KeyReleaseMask |
210 PointerMotionMask | ButtonPressMask |
211 ButtonReleaseMask | EnterWindowMask |
212 StructureNotifyMask;
213 win_attr.cursor = XCreateFontCursor( display, XC_top_left_arrow );
215 rootWindow = XCreateWindow( display, DefaultRootWindow(display),
216 desktopX, desktopY, width, height, 0,
217 CopyFromParent, InputOutput, CopyFromParent,
218 CWEventMask | CWCursor, &win_attr );
220 /* Set window manager properties */
222 size_hints.min_width = size_hints.max_width = width;
223 size_hints.min_height = size_hints.max_height = height;
224 size_hints.flags = PMinSize | PMaxSize;
225 if (flags & (XValue | YValue)) size_hints.flags |= USPosition;
226 if (flags & (WidthValue | HeightValue)) size_hints.flags |= USSize;
227 else size_hints.flags |= PSize;
229 wm_hints.flags = InputHint | StateHint;
230 wm_hints.input = True;
231 wm_hints.initial_state = NormalState;
232 class_hints.res_name = argv[0];
233 class_hints.res_class = "Wine";
235 XStringListToTextProperty( &name, 1, &window_name );
236 XSetWMProperties( display, rootWindow, &window_name, &window_name,
237 argv, argc, &size_hints, &wm_hints, &class_hints );
239 /* Map window */
241 XMapWindow( display, rootWindow );
245 XKeyboardState keyboard_state;
247 /***********************************************************************
248 * MAIN_SaveSetup
250 static void MAIN_SaveSetup(void)
252 XGetKeyboardControl(display, &keyboard_state);
255 /***********************************************************************
256 * MAIN_RestoreSetup
258 static void MAIN_RestoreSetup(void)
260 XKeyboardControl keyboard_value;
262 keyboard_value.key_click_percent = keyboard_state.key_click_percent;
263 keyboard_value.bell_percent = keyboard_state.bell_percent;
264 keyboard_value.bell_pitch = keyboard_state.bell_pitch;
265 keyboard_value.bell_duration = keyboard_state.bell_duration;
266 keyboard_value.auto_repeat_mode = keyboard_state.global_auto_repeat;
268 XChangeKeyboardControl(display, KBKeyClickPercent | KBBellPercent |
269 KBBellPitch | KBBellDuration | KBAutoRepeatMode, &keyboard_value);
272 static void called_at_exit(void)
274 Comm_DeInit();
275 sync_profiles();
276 MAIN_RestoreSetup();
279 /***********************************************************************
280 * main
282 int main( int argc, char *argv[] )
284 int ret_val;
285 int depth_count, i;
286 int *depth_list;
288 XrmInitialize();
290 MAIN_ParseOptions( &argc, argv );
292 screen = DefaultScreenOfDisplay( display );
293 screenWidth = WidthOfScreen( screen );
294 screenHeight = HeightOfScreen( screen );
295 XT_display = display;
296 XT_screen = screen;
297 if (screenDepth) /* -depth option specified */
299 depth_list = XListDepths(display,DefaultScreen(display),&depth_count);
300 for (i = 0; i < depth_count; i++)
301 if (depth_list[i] == screenDepth) break;
302 XFree( depth_list );
303 if (i >= depth_count)
305 fprintf( stderr, "%s: Depth %d not supported on this screen.\n",
306 Options.programName, screenDepth );
307 exit(1);
310 else screenDepth = DefaultDepthOfScreen( screen );
311 if (Options.synchronous) XSynchronize( display, True );
312 if (Options.desktopGeometry) MAIN_CreateDesktop( argc, argv );
313 else rootWindow = DefaultRootWindow( display );
315 ProgramName = argv [0];
316 MAIN_SaveSetup();
317 DOS_InitFS();
318 Comm_Init();
320 #ifndef sun
321 atexit(called_at_exit);
322 #endif
324 ret_val = _WinMain( argc, argv );
326 #ifdef sunos
327 called_at_exit();
328 #endif
330 return ret_val;
333 /***********************************************************************
334 * MessageBeep (USER.104)
336 void MessageBeep(WORD i)
338 XBell(display, 100);
341 /***********************************************************************
342 * GetVersion (KERNEL.3)
344 LONG GetVersion(void)
346 return( 0x03300a03 ); /* dos 3.30 & win 3.10 */
349 /***********************************************************************
350 * GetWinFlags (KERNEL.132)
352 LONG GetWinFlags(void)
354 return (WF_STANDARD | WF_CPU286 | WF_PMODE | WF_80x87);
357 /***********************************************************************
358 * GetTimerResolution (USER.14)
360 LONG GetTimerResolution(void)
362 return (1000);
365 /***********************************************************************
366 * SystemParametersInfo (USER.483)
368 BOOL SystemParametersInfo (UINT uAction, UINT uParam, void FAR *lpvParam, UINT fuWinIni)
370 int timeout, temp;
371 char buffer[256];
372 XKeyboardState keyboard_state;
373 XKeyboardControl keyboard_value;
376 fprintf(stderr, "SystemParametersInfo: action %d, param %x, flag %x\n",
377 uAction, uParam, fuWinIni);
379 switch (uAction) {
380 case SPI_GETBEEP:
381 XGetKeyboardControl(display, &keyboard_state);
382 if (keyboard_state.bell_percent == 0)
383 *(BOOL *) lpvParam = FALSE;
384 else
385 *(BOOL *) lpvParam = TRUE;
386 break;
388 case SPI_GETBORDER:
389 *(INT *) lpvParam = 1;
390 break;
392 case SPI_GETFASTTASKSWITCH:
393 *(BOOL *) lpvParam = FALSE;
394 break;
396 case SPI_GETGRIDGRANULARITY:
397 *(INT *) lpvParam = 1;
398 break;
400 case SPI_GETICONTITLEWRAP:
401 *(BOOL *) lpvParam = FALSE;
402 break;
404 case SPI_GETKEYBOARDDELAY:
405 *(INT *) lpvParam = 1;
406 break;
408 case SPI_GETKEYBOARDSPEED:
409 *(WORD *) lpvParam = 30;
410 break;
412 case SPI_GETMENUDROPALIGNMENT:
413 *(BOOL *) lpvParam = FALSE;
414 break;
416 case SPI_GETSCREENSAVEACTIVE:
417 *(BOOL *) lpvParam = FALSE;
418 break;
420 case SPI_GETSCREENSAVETIMEOUT:
421 XGetScreenSaver(display, &timeout, &temp,&temp,&temp);
422 *(INT *) lpvParam = timeout * 1000;
423 break;
425 case SPI_ICONHORIZONTALSPACING:
426 if (lpvParam == NULL)
427 fprintf(stderr, "SystemParametersInfo: Horizontal icon spacing set to %d\n.", uParam);
428 else
429 *(INT *) lpvParam = 50;
430 break;
432 case SPI_ICONVERTICALSPACING:
433 if (lpvParam == NULL)
434 fprintf(stderr, "SystemParametersInfo: Vertical icon spacing set to %d\n.", uParam);
435 else
436 *(INT *) lpvParam = 50;
437 break;
439 case SPI_SETBEEP:
440 if (uParam == TRUE)
441 keyboard_value.bell_percent = -1;
442 else
443 keyboard_value.bell_percent = 0;
444 XChangeKeyboardControl(display, KBBellPercent,
445 &keyboard_value);
446 break;
448 case SPI_SETSCREENSAVEACTIVE:
449 if (uParam == TRUE)
450 XActivateScreenSaver(display);
451 else
452 XResetScreenSaver(display);
453 break;
455 case SPI_SETSCREENSAVETIMEOUT:
456 XSetScreenSaver(display, uParam, 60, DefaultBlanking,
457 DefaultExposures);
458 break;
460 case SPI_SETDESKWALLPAPER:
461 return (SetDeskWallPaper((LPSTR) lpvParam));
462 break;
464 case SPI_SETDESKPATTERN:
465 if ((INT) uParam == -1) {
466 GetProfileString("Desktop", "Pattern",
467 "170 85 170 85 170 85 170 85",
468 buffer, sizeof(buffer) );
469 return (DESKTOP_SetPattern((LPSTR) buffer));
470 } else
471 return (DESKTOP_SetPattern((LPSTR) lpvParam));
472 break;
474 case SPI_LANGDRIVER:
475 case SPI_SETBORDER:
476 case SPI_SETDOUBLECLKHEIGHT:
477 case SPI_SETDOUBLECLICKTIME:
478 case SPI_SETDOUBLECLKWIDTH:
479 case SPI_SETFASTTASKSWITCH:
480 case SPI_SETKEYBOARDDELAY:
481 case SPI_SETKEYBOARDSPEED:
482 fprintf(stderr, "SystemParametersInfo: option %d ignored.\n", uParam);
483 break;
485 default:
486 fprintf(stderr, "SystemParametersInfo: unknown option %d.\n", uParam);
487 break;
489 return 1;
492 /***********************************************************************
493 * HMEMCPY (KERNEL.348)
495 void hmemcpy(void FAR *hpvDest, const void FAR *hpvSource, long cbCopy)
497 size_t copysize;
499 while (cbCopy)
501 copysize = cbCopy < 30000 ? cbCopy : 30000;
503 memcpy(hpvDest, hpvSource, copysize);
504 hpvDest += copysize;
505 hpvSource += copysize;
506 cbCopy -= copysize;
510 /***********************************************************************
511 * COPY (GDI.250)
513 void Copy(LPVOID lpSource, LPVOID lpDest, WORD nBytes)
515 memcpy(lpDest, lpSource, nBytes);