Add GLOBAL_DEFAULTS_SUBDIR and fix a syntax error in wmlib/Makefile.am
[wmaker-crm.git] / src / main.c
blob2ad68f86c8a3125891c61a7d9edc0109c2273423
1 /*
2 * Window Maker window manager
4 * Copyright (c) 1997-2003 Alfredo K. Kojima
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 <sys/inotify.h>
24 #include "wconfig.h"
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #include <fcntl.h>
34 #include <X11/Xlib.h>
35 #include <X11/Xutil.h>
37 /* Xlocale.h and locale.h are the same if X_LOCALE is undefind in wconfig.h,
38 * and if X_LOCALE is defined, X's locale emulating functions will be used.
39 * See Xlocale.h for more information.
41 #include <X11/Xlocale.h>
43 #define MAINFILE
45 #include "WindowMaker.h"
46 #include "window.h"
47 #include "funcs.h"
48 #include "menu.h"
49 #include "keybind.h"
50 #include "xmodifier.h"
51 #include "defaults.h"
52 #include "session.h"
53 #include "dialog.h"
55 #include <WINGs/WUtil.h>
57 #ifndef GLOBAL_DEFAULTS_SUBDIR
58 #define GLOBAL_DEFAULTS_SUBDIR "WindowMaker"
59 #endif
61 /****** Global Variables ******/
63 /* general info */
65 Display *dpy;
67 char *ProgName;
69 unsigned int ValidModMask = 0xff;
71 int inotifyFD;
72 int inotifyWD;
73 /* locale to use. NULL==POSIX or C */
74 char *Locale = NULL;
76 int wScreenCount = 0;
78 WPreferences wPreferences;
80 WMPropList *wDomainName;
81 WMPropList *wAttributeDomainName;
83 WShortKey wKeyBindings[WKBD_LAST];
85 /* defaults domains */
86 WDDomain *WDWindowMaker = NULL;
87 WDDomain *WDWindowAttributes = NULL;
88 WDDomain *WDRootMenu = NULL;
90 /* XContexts */
91 XContext wWinContext;
92 XContext wAppWinContext;
93 XContext wStackContext;
94 XContext wVEdgeContext;
96 /* Atoms */
97 Atom _XA_WM_STATE;
98 Atom _XA_WM_CHANGE_STATE;
99 Atom _XA_WM_PROTOCOLS;
100 Atom _XA_WM_TAKE_FOCUS;
101 Atom _XA_WM_DELETE_WINDOW;
102 Atom _XA_WM_SAVE_YOURSELF;
103 Atom _XA_WM_CLIENT_LEADER;
104 Atom _XA_WM_COLORMAP_WINDOWS;
105 Atom _XA_WM_COLORMAP_NOTIFY;
107 Atom _XA_GNUSTEP_WM_ATTR;
108 Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
109 Atom _XA_GNUSTEP_WM_RESIZEBAR;
110 Atom _XA_GNUSTEP_TITLEBAR_STATE;
112 Atom _XA_WINDOWMAKER_MENU;
113 Atom _XA_WINDOWMAKER_WM_PROTOCOLS;
114 Atom _XA_WINDOWMAKER_STATE;
116 Atom _XA_WINDOWMAKER_WM_FUNCTION;
117 Atom _XA_WINDOWMAKER_NOTICEBOARD;
118 Atom _XA_WINDOWMAKER_COMMAND;
120 Atom _XA_WINDOWMAKER_ICON_SIZE;
121 Atom _XA_WINDOWMAKER_ICON_TILE;
123 Atom _XA_WM_IGNORE_FOCUS_EVENTS;
125 /* cursors */
126 Cursor wCursor[WCUR_LAST];
128 /* last event timestamp for XSetInputFocus */
129 Time LastTimestamp = CurrentTime;
130 /* timestamp on the last time we did XSetInputFocus() */
131 Time LastFocusChange = CurrentTime;
133 #ifdef SHAPE
134 Bool wShapeSupported;
135 int wShapeEventBase;
136 #endif
138 #ifdef KEEP_XKB_LOCK_STATUS
139 Bool wXkbSupported;
140 int wXkbEventBase;
141 #endif
143 /* special flags */
144 char WProgramSigState = 0;
145 char WProgramState = WSTATE_NORMAL;
146 char WDelayedActionSet = 0;
148 /* temporary stuff */
149 int wVisualID = -1;
151 /* notifications */
152 const char *WMNManaged = "WMNManaged";
153 const char *WMNUnmanaged = "WMNUnmanaged";
154 const char *WMNChangedWorkspace = "WMNChangedWorkspace";
155 const char *WMNChangedState = "WMNChangedState";
156 const char *WMNChangedFocus = "WMNChangedFocus";
157 const char *WMNChangedStacking = "WMNChangedStacking";
158 const char *WMNChangedName = "WMNChangedName";
160 const char *WMNWorkspaceCreated = "WMNWorkspaceCreated";
161 const char *WMNWorkspaceDestroyed = "WMNWorkspaceDestroyed";
162 const char *WMNWorkspaceChanged = "WMNWorkspaceChanged";
163 const char *WMNWorkspaceNameChanged = "WMNWorkspaceNameChanged";
165 const char *WMNResetStacking = "WMNResetStacking";
167 /******** End Global Variables *****/
169 static char *DisplayName = NULL;
171 static char **Arguments;
173 static int ArgCount;
175 extern void EventLoop();
176 extern void StartUp();
177 extern int MonitorLoop(int argc, char **argv);
179 static Bool multiHead = True;
181 static int real_main(int argc, char **argv);
183 void Exit(int status)
185 #ifdef XSMP_ENABLED
186 wSessionDisconnectManager();
187 #endif
188 if (dpy)
189 XCloseDisplay(dpy);
191 exit(status);
194 void Restart(char *manager, Bool abortOnFailure)
196 char *prog = NULL;
197 char *argv[MAX_RESTART_ARGS];
198 int i;
200 if (manager && manager[0] != 0) {
201 prog = argv[0] = strtok(manager, " ");
202 for (i = 1; i < MAX_RESTART_ARGS; i++) {
203 argv[i] = strtok(NULL, " ");
204 if (argv[i] == NULL) {
205 break;
209 if (dpy) {
210 #ifdef XSMP_ENABLED
211 wSessionDisconnectManager();
212 #endif
213 XCloseDisplay(dpy);
214 dpy = NULL;
216 if (!prog) {
217 execvp(Arguments[0], Arguments);
218 wfatal(_("failed to restart Window Maker."));
219 } else {
220 execvp(prog, argv);
221 wsyserror(_("could not exec %s"), prog);
223 if (abortOnFailure)
224 exit(7);
227 void SetupEnvironment(WScreen * scr)
229 char *tmp, *ptr;
230 char buf[16];
232 if (multiHead) {
233 int len = strlen(DisplayName) + 64;
234 tmp = wmalloc(len);
235 snprintf(tmp, len, "DISPLAY=%s", XDisplayName(DisplayName));
236 ptr = strchr(strchr(tmp, ':'), '.');
237 if (ptr)
238 *ptr = 0;
239 snprintf(buf, sizeof(buf), ".%i", scr->screen);
240 strcat(tmp, buf);
241 putenv(tmp);
243 tmp = wmalloc(60);
244 snprintf(tmp, 60, "WRASTER_COLOR_RESOLUTION%i=%i", scr->screen,
245 scr->rcontext->attribs->colors_per_channel);
246 putenv(tmp);
249 typedef struct {
250 WScreen *scr;
251 char *command;
252 } _tuple;
254 static void shellCommandHandler(pid_t pid, unsigned char status, _tuple * data)
256 if (status == 127) {
257 char *buffer;
259 buffer = wstrconcat(_("Could not execute command: "), data->command);
261 wMessageDialog(data->scr, _("Error"), buffer, _("OK"), NULL, NULL);
262 wfree(buffer);
263 } else if (status != 127) {
265 printf("%s: %i\n", data->command, status);
269 wfree(data->command);
270 wfree(data);
273 void ExecuteShellCommand(WScreen * scr, char *command)
275 static char *shell = NULL;
276 pid_t pid;
279 * This have a problem: if the shell is tcsh (not sure about others)
280 * and ~/.tcshrc have /bin/stty erase ^H somewhere on it, the shell
281 * will block and the command will not be executed.
282 if (!shell) {
283 shell = getenv("SHELL");
284 if (!shell)
285 shell = "/bin/sh";
288 shell = "/bin/sh";
290 pid = fork();
292 if (pid == 0) {
294 SetupEnvironment(scr);
296 #ifdef HAVE_SETSID
297 setsid();
298 #endif
299 execl(shell, shell, "-c", command, NULL);
300 wsyserror("could not execute %s -c %s", shell, command);
301 Exit(-1);
302 } else if (pid < 0) {
303 wsyserror("cannot fork a new process");
304 } else {
305 _tuple *data = wmalloc(sizeof(_tuple));
307 data->scr = scr;
308 data->command = wstrdup(command);
310 wAddDeathHandler(pid, (WDeathHandler *) shellCommandHandler, data);
315 *---------------------------------------------------------------------
316 * wAbort--
317 * Do a major cleanup and exit the program
319 *----------------------------------------------------------------------
321 void wAbort(Bool dumpCore)
323 int i;
324 WScreen *scr;
326 for (i = 0; i < wScreenCount; i++) {
327 scr = wScreenWithNumber(i);
328 if (scr)
329 RestoreDesktop(scr);
331 printf(_("%s aborted.\n"), ProgName);
332 if (dumpCore)
333 abort();
334 else
335 exit(1);
338 void print_help()
340 printf(_("Usage: %s [options]\n"), ProgName);
341 puts(_("The Window Maker window manager for the X window system"));
342 puts("");
343 puts(_(" -display host:dpy display to use"));
344 #ifdef USECPP
345 puts(_(" --no-cpp disable preprocessing of configuration files"));
346 #endif
347 puts(_(" --no-dock do not open the application Dock"));
348 puts(_(" --no-clip do not open the workspace Clip"));
349 puts(_(" --no-autolaunch do not autolaunch applications"));
350 puts(_(" --dont-restore do not restore saved session"));
352 puts(_(" --locale locale locale to use"));
354 puts(_(" --create-stdcmap create the standard colormap hint in PseudoColor visuals"));
355 puts(_(" --visual-id visualid visual id of visual to use"));
356 puts(_(" --static do not update or save configurations"));
357 #ifdef DEBUG
358 puts(_(" --synchronous turn on synchronous display mode"));
359 #endif
360 puts(_(" --version print version and exit"));
361 puts(_(" --help show this message"));
364 void check_defaults()
366 char *path;
368 path = wdefaultspathfordomain("WindowMaker");
370 if (access(path, R_OK) != 0) {
371 #if 0
372 wfatal(_("could not find user GNUstep directory (%s).\n"
373 "Make sure you have installed Window Maker correctly and run wmaker.inst"), path);
374 exit(1);
375 #else
376 wwarning(_("could not find user GNUstep directory (%s)."), path);
378 if (system("wmaker.inst --batch") != 0) {
379 wwarning(_("There was an error while creating GNUstep directory, please "
380 "make sure you have installed Window Maker correctly and run wmaker.inst"));
381 } else {
382 wwarning(_("%s directory created with default configuration."), path);
384 #endif
387 wfree(path);
391 * Add watch here, used to notify if configuration
392 * files have changed, using linux kernel inotify mechanism
395 static void inotifyWatchConfig()
397 char *watchPath = NULL;
398 inotifyFD = inotify_init(); /* Initialise an inotify instance */
399 if (inotifyFD < 0) {
400 wwarning(_("could not initialise an inotify instance."
401 " Changes to the defaults database will require"
402 " a restart to take effect. Check your kernel!"));
403 } else {
404 watchPath = wstrconcat(wusergnusteppath(), "/Defaults");
405 /* Add the watch; really we are only looking for modify events
406 * but we might want more in the future so check all events for now.
407 * The individual events are checked for in event.c.
409 inotifyWD = inotify_add_watch(inotifyFD, watchPath, IN_ALL_EVENTS);
410 if (inotifyWD < 0) {
411 wwarning(_("could not add an inotify watch on path\n."
412 "%s\n"
413 "Changes to the defaults database will require"
414 " a restart to take effect."), watchPath);
415 close(inotifyFD);
418 wfree(watchPath);
421 static void execInitScript()
423 char *file, *paths;
425 paths = wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
426 paths = wstrappend(paths, ":" DEF_CONFIG_PATHS);
428 file = wfindfile(paths, DEF_INIT_SCRIPT);
429 wfree(paths);
431 if (file) {
432 if (system(file) != 0) {
433 wsyserror(_("%s:could not execute initialization script"), file);
435 #if 0
436 if (fork() == 0) {
437 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
438 wsyserror(_("%s:could not execute initialization script"), file);
439 exit(1);
441 #endif
442 wfree(file);
446 void ExecExitScript()
448 char *file, *paths;
450 paths = wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
451 paths = wstrappend(paths, ":" DEF_CONFIG_PATHS);
453 file = wfindfile(paths, DEF_EXIT_SCRIPT);
454 wfree(paths);
456 if (file) {
457 if (system(file) != 0) {
458 wsyserror(_("%s:could not execute exit script"), file);
460 #if 0
461 if (fork() == 0) {
462 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
463 wsyserror(_("%s:could not execute exit script"), file);
464 exit(1);
466 #endif
467 wfree(file);
471 #if 0
472 char *getFullPath(char *path)
474 char buffer[1024];
475 char *tmp;
476 char *basep = (char *)buffer;
478 if (*path != '/' && getcwd(buffer, 1023)) {
480 for (;;) {
481 if (strncmp(path, "../", 3) == 0) {
482 path += 3;
483 basep = strchr(basep, '/');
484 if (!basep || *path == 0)
485 break;
488 if (*path == '/' || strncmp(path, "./", 2) == 0) {
489 tmp =}
492 * path
493 * ./path
494 * ../path
495 * ../../path
498 } else {
499 return wstrconcat(path);
502 return tmp;
504 #endif
506 int main(int argc, char **argv)
508 int i_am_the_monitor, i, len;
509 char *str, *alt;
511 /* setup common stuff for the monitor and wmaker itself */
512 WMInitializeApplication("WindowMaker", &argc, argv);
514 memset(&wPreferences, 0, sizeof(WPreferences));
516 wPreferences.fallbackWMs = WMCreateArray(8);
517 alt = getenv("WINDOWMAKER_ALT_WM");
518 if (alt != NULL)
519 WMAddToArray(wPreferences.fallbackWMs, wstrdup(alt));
521 WMAddToArray(wPreferences.fallbackWMs, wstrdup("blackbox"));
522 WMAddToArray(wPreferences.fallbackWMs, wstrdup("metacity"));
523 WMAddToArray(wPreferences.fallbackWMs, wstrdup("fvwm"));
524 WMAddToArray(wPreferences.fallbackWMs, wstrdup("twm"));
525 WMAddToArray(wPreferences.fallbackWMs, NULL);
526 WMAddToArray(wPreferences.fallbackWMs, wstrdup("rxvt"));
527 WMAddToArray(wPreferences.fallbackWMs, wstrdup("xterm"));
529 i_am_the_monitor = 1;
531 for (i = 1; i < argc; i++) {
532 if (strncmp(argv[i], "--for-real", strlen("--for-real")) == 0) {
533 i_am_the_monitor = 0;
534 break;
535 } else if (strcmp(argv[i], "-display") == 0 || strcmp(argv[i], "--display") == 0) {
536 i++;
537 if (i >= argc) {
538 wwarning(_("too few arguments for %s"), argv[i - 1]);
539 exit(0);
541 DisplayName = argv[i];
545 DisplayName = XDisplayName(DisplayName);
546 len = strlen(DisplayName) + 64;
547 str = wmalloc(len);
548 snprintf(str, len, "DISPLAY=%s", DisplayName);
549 putenv(str);
551 if (i_am_the_monitor)
552 return MonitorLoop(argc, argv);
553 else
554 return real_main(argc, argv);
557 static int real_main(int argc, char **argv)
559 int i, restart = 0;
560 char *pos;
561 int d, s;
562 int flag;
563 #ifdef DEBUG
564 Bool doSync = False;
565 #endif
566 setlocale(LC_ALL, "");
568 wsetabort(wAbort);
570 /* for telling WPrefs what's the name of the wmaker binary being ran */
571 setenv("WMAKER_BIN_NAME", argv[0], 1);
573 flag = 0;
574 ArgCount = argc;
575 Arguments = wmalloc(sizeof(char *) * (ArgCount + 1));
576 for (i = 0; i < argc; i++) {
577 Arguments[i] = argv[i];
579 /* add the extra option to signal that we're just restarting wmaker */
580 Arguments[argc - 1] = "--for-real=";
581 Arguments[argc] = NULL;
583 ProgName = strrchr(argv[0], '/');
584 if (!ProgName)
585 ProgName = argv[0];
586 else
587 ProgName++;
589 restart = 0;
591 if (argc > 1) {
592 for (i = 1; i < argc; i++) {
593 #ifdef USECPP
594 if (strcmp(argv[i], "-nocpp") == 0 || strcmp(argv[i], "--no-cpp") == 0) {
595 wPreferences.flags.nocpp = 1;
596 } else
597 #endif
598 if (strcmp(argv[i], "--for-real") == 0) {
599 wPreferences.flags.restarting = 0;
600 } else if (strcmp(argv[i], "--for-real=") == 0) {
601 wPreferences.flags.restarting = 1;
602 } else if (strcmp(argv[i], "--for-real-") == 0) {
603 wPreferences.flags.restarting = 2;
604 } else if (strcmp(argv[i], "-no-autolaunch") == 0
605 || strcmp(argv[i], "--no-autolaunch") == 0) {
606 wPreferences.flags.noautolaunch = 1;
607 } else if (strcmp(argv[i], "-dont-restore") == 0 || strcmp(argv[i], "--dont-restore") == 0) {
608 wPreferences.flags.norestore = 1;
609 } else if (strcmp(argv[i], "-nodock") == 0 || strcmp(argv[i], "--no-dock") == 0) {
610 wPreferences.flags.nodock = 1;
611 } else if (strcmp(argv[i], "-noclip") == 0 || strcmp(argv[i], "--no-clip") == 0) {
612 wPreferences.flags.noclip = 1;
613 } else if (strcmp(argv[i], "-version") == 0 || strcmp(argv[i], "--version") == 0) {
614 printf("Window Maker %s\n", VERSION);
615 exit(0);
616 } else if (strcmp(argv[i], "--global_defaults_path") == 0) {
617 printf("%s/%s\n", SYSCONFDIR, GLOBAL_DEFAULTS_SUBDIR);
618 exit(0);
619 #ifdef DEBUG
620 } else if (strcmp(argv[i], "--synchronous") == 0) {
621 doSync = 1;
622 #endif
623 } else if (strcmp(argv[i], "-locale") == 0 || strcmp(argv[i], "--locale") == 0) {
624 i++;
625 if (i >= argc) {
626 wwarning(_("too few arguments for %s"), argv[i - 1]);
627 exit(0);
629 Locale = argv[i];
630 } else if (strcmp(argv[i], "-display") == 0 || strcmp(argv[i], "--display") == 0) {
631 i++;
632 if (i >= argc) {
633 wwarning(_("too few arguments for %s"), argv[i - 1]);
634 exit(0);
636 DisplayName = argv[i];
637 } else if (strcmp(argv[i], "-visualid") == 0 || strcmp(argv[i], "--visual-id") == 0) {
638 i++;
639 if (i >= argc) {
640 wwarning(_("too few arguments for %s"), argv[i - 1]);
641 exit(0);
643 if (sscanf(argv[i], "%i", &wVisualID) != 1) {
644 wwarning(_("bad value for visualid: \"%s\""), argv[i]);
645 exit(0);
647 } else if (strcmp(argv[i], "-static") == 0 || strcmp(argv[i], "--static") == 0) {
649 wPreferences.flags.noupdates = 1;
650 #ifdef XSMP_ENABLED
651 } else if (strcmp(argv[i], "-clientid") == 0 || strcmp(argv[i], "-restore") == 0) {
652 i++;
653 if (i >= argc) {
654 wwarning(_("too few arguments for %s"), argv[i - 1]);
655 exit(0);
657 #endif
658 } else if (strcmp(argv[i], "--help") == 0) {
659 print_help();
660 exit(0);
661 } else {
662 printf(_("%s: invalid argument '%s'\n"), argv[0], argv[i]);
663 printf(_("Try '%s --help' for more information\n"), argv[0]);
664 exit(1);
669 if (!wPreferences.flags.noupdates) {
670 /* check existence of Defaults DB directory */
671 check_defaults();
674 if (Locale) {
675 setenv("LANG", Locale, 1);
676 } else {
677 Locale = getenv("LC_ALL");
678 if (!Locale) {
679 Locale = getenv("LANG");
683 setlocale(LC_ALL, "");
685 if (!Locale || strcmp(Locale, "C") == 0 || strcmp(Locale, "POSIX") == 0)
686 Locale = NULL;
687 #ifdef I18N
688 if (getenv("NLSPATH"))
689 bindtextdomain("WindowMaker", getenv("NLSPATH"));
690 else
691 bindtextdomain("WindowMaker", LOCALEDIR);
692 bind_textdomain_codeset("WindowMaker", "UTF-8");
693 textdomain("WindowMaker");
695 if (!XSupportsLocale()) {
696 wwarning(_("X server does not support locale"));
699 if (XSetLocaleModifiers("") == NULL) {
700 wwarning(_("cannot set locale modifiers"));
702 #endif
704 if (Locale) {
705 char *ptr;
707 Locale = wstrdup(Locale);
708 ptr = strchr(Locale, '.');
709 if (ptr)
710 *ptr = 0;
713 /* open display */
714 dpy = XOpenDisplay(DisplayName);
715 if (dpy == NULL) {
716 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName));
717 exit(1);
720 if (fcntl(ConnectionNumber(dpy), F_SETFD, FD_CLOEXEC) < 0) {
721 wsyserror("error setting close-on-exec flag for X connection");
722 exit(1);
725 if (wVisualID < 0)
727 * If unspecified, use default visual instead of waiting
728 * for wrlib/context.c:bestContext() that may end up choosing
729 * the "fake" 24 bits added by the Composite extension.
730 * This is required to avoid all sort of corruptions when
731 * composite is enabled, and at a depth other than 24.
733 wVisualID = (int)DefaultVisual(dpy, DefaultScreen(dpy))->visualid;
735 /* check if the user specified a complete display name (with screen).
736 * If so, only manage the specified screen */
737 if (DisplayName)
738 pos = strchr(DisplayName, ':');
739 else
740 pos = NULL;
742 if (pos && sscanf(pos, ":%i.%i", &d, &s) == 2)
743 multiHead = False;
745 DisplayName = XDisplayName(DisplayName);
746 setenv("DISPLAY", DisplayName, 1);
748 #ifdef DEBUG
749 if (doSync)
750 XSynchronize(dpy, True);
751 #endif
753 wXModifierInitialize();
755 #ifdef XSMP_ENABLED
756 wSessionConnectManager(argv, argc);
757 #endif
759 StartUp(!multiHead);
761 if (wScreenCount == 1)
762 multiHead = False;
764 execInitScript();
765 inotifyWatchConfig();
766 EventLoop();
767 return -1;