fixed cosmetic bug in geom. dpy window for 8bpp
[wmaker-crm.git] / src / main.c
blobb58e85574a966288ef9c9b38ef240613dac47b06
1 /*
2 * Window Maker window manager
3 *
4 * Copyright (c) 1997, 1998 Alfredo K. Kojima
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 * USA.
22 #include "wconfig.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <fcntl.h>
32 #include <X11/Xlib.h>
33 #include <X11/Xutil.h>
36 /* Xlocale.h and locale.h are the same if X_LOCALE is undefind in wconfig.h,
37 * and if X_LOCALE is defined, X's locale emulating functions will be used.
38 * See Xlocale.h for more information.
40 #include <X11/Xlocale.h>
42 #define MAINFILE
44 #include "WindowMaker.h"
45 #include "window.h"
46 #include "funcs.h"
47 #include "menu.h"
48 #include "keybind.h"
49 #include "xmodifier.h"
50 #include "defaults.h"
51 #include "session.h"
53 #include <proplist.h>
55 /****** Global Variables ******/
57 /* general info */
59 Display *dpy;
61 char *ProgName;
63 unsigned int ValidModMask = 0xff;
65 /* locale to use. NULL==POSIX or C */
66 char *Locale=NULL;
68 int wScreenCount=0;
70 WPreferences wPreferences;
73 proplist_t wDomainName;
74 proplist_t wAttributeDomainName;
76 WShortKey wKeyBindings[WKBD_LAST];
78 /* defaults domains */
79 WDDomain *WDWindowMaker = NULL;
80 WDDomain *WDRootMenu = NULL;
81 WDDomain *WDWindowAttributes = NULL;
84 /* XContexts */
85 XContext wWinContext;
86 XContext wAppWinContext;
87 XContext wStackContext;
89 /* Atoms */
90 Atom _XA_WM_STATE;
91 Atom _XA_WM_CHANGE_STATE;
92 Atom _XA_WM_PROTOCOLS;
93 Atom _XA_WM_TAKE_FOCUS;
94 Atom _XA_WM_DELETE_WINDOW;
95 Atom _XA_WM_SAVE_YOURSELF;
96 Atom _XA_WM_CLIENT_LEADER;
97 Atom _XA_WM_COLORMAP_WINDOWS;
98 Atom _XA_WM_COLORMAP_NOTIFY;
100 Atom _XA_GNUSTEP_WM_ATTR;
101 Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
102 Atom _XA_GNUSTEP_WM_RESIZEBAR;
104 Atom _XA_WINDOWMAKER_MENU;
105 Atom _XA_WINDOWMAKER_WM_PROTOCOLS;
106 Atom _XA_WINDOWMAKER_STATE;
108 Atom _XA_WINDOWMAKER_WM_FUNCTION;
109 Atom _XA_WINDOWMAKER_NOTICEBOARD;
110 Atom _XA_WINDOWMAKER_COMMAND;
112 #ifdef OFFIX_DND
113 Atom _XA_DND_PROTOCOL;
114 Atom _XA_DND_SELECTION;
115 #endif
117 /* cursors */
118 Cursor wCursor[WCUR_LAST];
120 /* last event timestamp for XSetInputFocus */
121 Time LastTimestamp;
122 /* timestamp on the last time we did XSetInputFocus() */
123 Time LastFocusChange;
125 #ifdef SHAPE
126 Bool wShapeSupported;
127 int wShapeEventBase;
128 #endif
130 #ifdef KEEP_XKB_LOCK_STATUS
131 Bool wXkbSupported;
132 int wXkbEventBase;
133 #endif
135 /* special flags */
136 char WProgramState = WSTATE_NORMAL;
137 char WDelayedActionSet = 0;
139 /* temporary stuff */
140 int wVisualID = -1;
143 /******** End Global Variables *****/
145 static char *DisplayName = NULL;
147 static char **Arguments;
149 static int ArgCount;
151 extern void EventLoop();
152 extern void StartUp();
155 void
156 Exit(int status)
158 #ifdef XSMP_ENABLED
159 wSessionDisconnectManager();
160 #endif
161 if (dpy)
162 XCloseDisplay(dpy);
164 exit(status);
168 void
169 Restart(char *manager, Bool abortOnFailure)
171 char *prog=NULL;
172 char *argv[MAX_RESTART_ARGS];
173 int i;
175 if (manager && manager[0]!=0) {
176 prog = argv[0] = strtok(manager, " ");
177 for (i=1; i<MAX_RESTART_ARGS; i++) {
178 argv[i]=strtok(NULL, " ");
179 if (argv[i]==NULL) {
180 break;
184 if (dpy) {
185 #ifdef XSMP_ENABLED
186 wSessionDisconnectManager();
187 #endif
188 XCloseDisplay(dpy);
189 dpy = NULL;
191 if (!prog) {
192 execvp(Arguments[0], Arguments);
193 wfatal(_("failed to restart Window Maker."));
194 } else {
195 execvp(prog, argv);
196 wsyserror(_("could not exec %s"), prog);
198 if (abortOnFailure)
199 exit(-1);
204 void
205 SetupEnvironment(WScreen *scr)
207 char *tmp, *ptr;
208 char buf[16];
210 if (wScreenCount > 1) {
211 tmp = wmalloc(strlen(DisplayName)+64);
212 sprintf(tmp, "DISPLAY=%s", XDisplayName(DisplayName));
213 ptr = strchr(strchr(tmp, ':'), '.');
214 if (ptr)
215 *ptr = 0;
216 sprintf(buf, ".%i", scr->screen);
217 strcat(tmp, buf);
218 putenv(tmp);
220 tmp = wmalloc(60);
221 sprintf(tmp, "WRASTER_COLOR_RESOLUTION%i=%i", scr->screen,
222 scr->rcontext->attribs->colors_per_channel);
223 putenv(tmp);
228 *---------------------------------------------------------------------
229 * wAbort--
230 * Do a major cleanup and exit the program
232 *----------------------------------------------------------------------
234 void
235 wAbort(Bool dumpCore)
237 int i;
238 WScreen *scr;
240 for (i=0; i<wScreenCount; i++) {
241 scr = wScreenWithNumber(i);
242 if (scr)
243 RestoreDesktop(scr);
245 printf(_("%s aborted.\n"), ProgName);
246 if (dumpCore)
247 abort();
248 else
249 exit(1);
253 void
254 print_help()
256 printf(_("Usage: %s [options]\n"), ProgName);
257 puts(_("The Window Maker window manager for the X window system"));
258 puts("");
259 puts(_(" -display host:dpy display to use"));
260 #ifdef USECPP
261 puts(_(" --no-cpp disable preprocessing of configuration files"));
262 #endif
263 puts(_(" --no-dock do not open the application Dock"));
264 puts(_(" --no-clip do not open the workspace Clip"));
266 puts(_(" --locale locale locale to use"));
268 puts(_(" --visual-id visualid visual id of visual to use"));
269 puts(_(" --static do not update or save configurations"));
270 #ifdef DEBUG
271 puts(_(" --synchronous turn on synchronous display mode"));
272 #endif
273 puts(_(" --version print version and exit"));
274 puts(_(" --help show this message"));
279 void
280 check_defaults()
282 char *path;
284 path = wdefaultspathfordomain("");
285 if (access(path, R_OK)!=0) {
286 wfatal(_("could not find user GNUstep directory (%s).\n"
287 "Make sure you have installed Window Maker correctly and run wmaker.inst"),
288 path);
289 exit(1);
292 free(path);
296 static void
297 execInitScript()
299 char *file;
300 char *paths = wstrappend(wusergnusteppath(), ":"DEF_CONFIG_PATHS);
302 file = wfindfile(paths, DEF_INIT_SCRIPT);
303 free(paths);
305 if (file) {
306 if (system(file) != 0) {
307 wsyserror(_("%s:could not execute initialization script"), file);
309 #if 0
310 if (fork()==0) {
311 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
312 wsyserror(_("%s:could not execute initialization script"), file);
313 exit(1);
315 #endif
316 free(file);
321 void
322 ExecExitScript()
324 char *file;
325 char *paths = wstrappend(wusergnusteppath(), ":"DEF_CONFIG_PATHS);
327 file = wfindfile(paths, DEF_EXIT_SCRIPT);
328 free(paths);
330 if (file) {
331 if (system(file) != 0) {
332 wsyserror(_("%s:could not execute exit script"), file);
334 #if 0
335 if (fork()==0) {
336 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
337 wsyserror(_("%s:could not execute exit script"), file);
338 exit(1);
340 #endif
341 free(file);
348 main(int argc, char **argv)
350 int i, restart=0;
351 Bool multiHead = True;
352 char *str;
353 int d, s;
354 #ifdef DEBUG
355 Bool doSync = False;
356 #endif
358 wsetabort(wAbort);
360 /* for telling WPrefs what's the name of the wmaker binary being ran */
361 str = wstrappend("WMAKER_BIN_NAME=", argv[0]);
362 putenv(str);
364 ArgCount = argc;
365 Arguments = argv;
367 WMInitializeApplication("WindowMaker", &argc, argv);
370 ProgName = strrchr(argv[0],'/');
371 if (!ProgName)
372 ProgName = argv[0];
373 else
374 ProgName++;
377 restart = 0;
379 memset(&wPreferences, 0, sizeof(WPreferences));
381 if (argc>1) {
382 for (i=1; i<argc; i++) {
383 #ifdef USECPP
384 if (strcmp(argv[i], "-nocpp")==0
385 || strcmp(argv[i], "--no-cpp")==0) {
386 wPreferences.flags.nocpp=1;
387 } else
388 #endif
389 if (strcmp(argv[i], "-nodock")==0
390 || strcmp(argv[i], "--no-dock")==0) {
391 wPreferences.flags.nodock=1;
392 } else if (strcmp(argv[i], "-noclip")==0
393 || strcmp(argv[i], "--no-clip")==0) {
394 wPreferences.flags.noclip=1;
395 } else if (strcmp(argv[i], "-version")==0
396 || strcmp(argv[i], "--version")==0) {
397 printf("Window Maker %s\n", VERSION);
398 exit(0);
399 } else if (strcmp(argv[i], "--global_defaults_path")==0) {
400 puts(SYSCONFDIR);
401 exit(0);
402 #ifdef DEBUG
403 } else if (strcmp(argv[i], "--synchronous")==0) {
404 doSync = 1;
405 #endif
406 } else if (strcmp(argv[i], "-locale")==0
407 || strcmp(argv[i], "--locale")==0) {
408 i++;
409 if (i>=argc) {
410 wwarning(_("too few arguments for %s"), argv[i-1]);
411 exit(0);
413 Locale = argv[i];
414 } else if (strcmp(argv[i], "-display")==0) {
415 i++;
416 if (i>=argc) {
417 wwarning(_("too few arguments for %s"), argv[i-1]);
418 exit(0);
420 DisplayName = argv[i];
421 } else if (strcmp(argv[i], "-visualid")==0
422 || strcmp(argv[i], "--visual-id")==0) {
423 i++;
424 if (i>=argc) {
425 wwarning(_("too few arguments for %s"), argv[i-1]);
426 exit(0);
428 if (sscanf(argv[i], "%i", &wVisualID)!=1) {
429 wwarning(_("bad value for visualid: \"%s\""), argv[i]);
430 exit(0);
432 } else if (strcmp(argv[i], "-static")==0
433 || strcmp(argv[i], "--static")==0) {
435 wPreferences.flags.noupdates = 1;
436 #ifdef XSMP_ENABLED
437 } else if (strcmp(argv[i], "-clientid")==0
438 || strcmp(argv[i], "-restore")==0) {
439 i++;
440 if (i>=argc) {
441 wwarning(_("too few arguments for %s"), argv[i-1]);
442 exit(0);
444 #endif
445 } else if (strcmp(argv[i], "--help")==0) {
446 print_help();
447 exit(0);
448 } else {
449 printf(_("%s: invalid argument '%s'\n"), argv[0], argv[i]);
450 printf(_("Try '%s --help' for more information\n"), argv[0]);
451 exit(1);
456 if (!wPreferences.flags.noupdates) {
457 /* check existence of Defaults DB directory */
458 check_defaults();
461 #if 0
462 tmp = getenv("LANG");
463 if (tmp) {
464 if (setlocale(LC_ALL,"") == NULL) {
465 wwarning("cannot set locale %s", tmp);
466 wwarning("falling back to C locale");
467 setlocale(LC_ALL,"C");
468 Locale = NULL;
469 } else {
470 if (strcmp(tmp, "C")==0 || strcmp(tmp, "POSIX")==0)
471 Locale = NULL;
472 else
473 Locale = tmp;
475 } else {
476 Locale = NULL;
478 #endif
479 if (!Locale) {
480 Locale = getenv("LANG");
482 setlocale(LC_ALL, Locale);
483 if (!Locale || strcmp(Locale, "C")==0 || strcmp(Locale, "POSIX")==0)
484 Locale = NULL;
485 #ifdef I18N
486 if (getenv("NLSPATH"))
487 bindtextdomain("WindowMaker", getenv("NLSPATH"));
488 else
489 bindtextdomain("WindowMaker", LOCALEDIR);
490 textdomain("WindowMaker");
492 if (!XSupportsLocale()) {
493 wwarning(_("X server does not support locale"));
495 if (XSetLocaleModifiers("") == NULL) {
496 wwarning(_("cannot set locale modifiers"));
498 #endif
500 if (Locale) {
501 char *ptr;
503 Locale = wstrdup(Locale);
504 ptr = strchr(Locale, '.');
505 if (ptr)
506 *ptr = 0;
510 /* open display */
511 dpy = XOpenDisplay(DisplayName);
512 if (dpy == NULL) {
513 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName));
514 exit(1);
517 if (fcntl(ConnectionNumber(dpy), F_SETFD, FD_CLOEXEC) < 0) {
518 wsyserror("error setting close-on-exec flag for X connection");
519 exit(1);
522 /* check if the user specified a complete display name (with screen).
523 * If so, only manage the specified screen */
524 if (DisplayName)
525 str = strchr(DisplayName, ':');
526 else
527 str = NULL;
529 if (str && sscanf(str, "%i.%i", &d, &s)==2)
530 multiHead = False;
532 DisplayName = XDisplayName(DisplayName);
533 str = wmalloc(strlen(DisplayName)+64);
534 sprintf(str, "DISPLAY=%s", DisplayName);
535 putenv(str);
537 #ifdef DEBUG
538 if (doSync)
539 XSynchronize(dpy, True);
540 #endif
542 wXModifierInitialize();
544 #ifdef XSMP_ENABLED
545 wSessionConnectManager(argv, argc);
546 #endif
548 StartUp(!multiHead);
550 execInitScript();
552 EventLoop();
553 return -1;