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,
22 #include <sys/inotify.h>
31 #include <sys/types.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>
45 #include "WindowMaker.h"
50 #include "xmodifier.h"
55 #include <WINGs/WUtil.h>
57 /****** Global Variables ******/
65 unsigned int ValidModMask = 0xff;
69 /* locale to use. NULL==POSIX or C */
74 WPreferences wPreferences;
76 WMPropList *wDomainName;
77 WMPropList *wAttributeDomainName;
79 WShortKey wKeyBindings[WKBD_LAST];
81 /* defaults domains */
82 WDDomain *WDWindowMaker = NULL;
83 WDDomain *WDWindowAttributes = NULL;
84 WDDomain *WDRootMenu = NULL;
88 XContext wAppWinContext;
89 XContext wStackContext;
90 XContext wVEdgeContext;
94 Atom _XA_WM_CHANGE_STATE;
95 Atom _XA_WM_PROTOCOLS;
96 Atom _XA_WM_TAKE_FOCUS;
97 Atom _XA_WM_DELETE_WINDOW;
98 Atom _XA_WM_SAVE_YOURSELF;
99 Atom _XA_WM_CLIENT_LEADER;
100 Atom _XA_WM_COLORMAP_WINDOWS;
101 Atom _XA_WM_COLORMAP_NOTIFY;
103 Atom _XA_GNUSTEP_WM_ATTR;
104 Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
105 Atom _XA_GNUSTEP_WM_RESIZEBAR;
106 Atom _XA_GNUSTEP_TITLEBAR_STATE;
108 Atom _XA_WINDOWMAKER_MENU;
109 Atom _XA_WINDOWMAKER_WM_PROTOCOLS;
110 Atom _XA_WINDOWMAKER_STATE;
112 Atom _XA_WINDOWMAKER_WM_FUNCTION;
113 Atom _XA_WINDOWMAKER_NOTICEBOARD;
114 Atom _XA_WINDOWMAKER_COMMAND;
116 Atom _XA_WINDOWMAKER_ICON_SIZE;
117 Atom _XA_WINDOWMAKER_ICON_TILE;
120 Cursor wCursor[WCUR_LAST];
122 /* last event timestamp for XSetInputFocus */
123 Time LastTimestamp = CurrentTime;
124 /* timestamp on the last time we did XSetInputFocus() */
125 Time LastFocusChange = CurrentTime;
128 Bool wShapeSupported;
132 #ifdef KEEP_XKB_LOCK_STATUS
138 char WProgramSigState = 0;
139 char WProgramState = WSTATE_NORMAL;
140 char WDelayedActionSet = 0;
142 /* temporary stuff */
146 const char *WMNManaged = "WMNManaged";
147 const char *WMNUnmanaged = "WMNUnmanaged";
148 const char *WMNChangedWorkspace = "WMNChangedWorkspace";
149 const char *WMNChangedState = "WMNChangedState";
150 const char *WMNChangedFocus = "WMNChangedFocus";
151 const char *WMNChangedStacking = "WMNChangedStacking";
152 const char *WMNChangedName = "WMNChangedName";
154 const char *WMNWorkspaceCreated = "WMNWorkspaceCreated";
155 const char *WMNWorkspaceDestroyed = "WMNWorkspaceDestroyed";
156 const char *WMNWorkspaceChanged = "WMNWorkspaceChanged";
157 const char *WMNWorkspaceNameChanged = "WMNWorkspaceNameChanged";
159 const char *WMNResetStacking = "WMNResetStacking";
161 /******** End Global Variables *****/
163 static char *DisplayName = NULL;
165 static char **Arguments;
169 extern void EventLoop();
170 extern void StartUp();
171 extern int MonitorLoop(int argc, char **argv);
173 static Bool multiHead = True;
175 static int real_main(int argc, char **argv);
177 void Exit(int status)
180 wSessionDisconnectManager();
188 void Restart(char *manager, Bool abortOnFailure)
191 char *argv[MAX_RESTART_ARGS];
194 if (manager && manager[0] != 0) {
195 prog = argv[0] = strtok(manager, " ");
196 for (i = 1; i < MAX_RESTART_ARGS; i++) {
197 argv[i] = strtok(NULL, " ");
198 if (argv[i] == NULL) {
205 wSessionDisconnectManager();
211 execvp(Arguments[0], Arguments);
212 wfatal(_("failed to restart Window Maker."));
215 wsyserror(_("could not exec %s"), prog);
221 void SetupEnvironment(WScreen * scr)
227 int len = strlen(DisplayName) + 64;
229 snprintf(tmp, len, "DISPLAY=%s", XDisplayName(DisplayName));
230 ptr = strchr(strchr(tmp, ':'), '.');
233 snprintf(buf, sizeof(buf), ".%i", scr->screen);
238 snprintf(tmp, 60, "WRASTER_COLOR_RESOLUTION%i=%i", scr->screen,
239 scr->rcontext->attribs->colors_per_channel);
248 static void shellCommandHandler(pid_t pid, unsigned char status, _tuple * data)
253 buffer = wstrconcat(_("Could not execute command: "), data->command);
255 wMessageDialog(data->scr, _("Error"), buffer, _("OK"), NULL, NULL);
257 } else if (status != 127) {
259 printf("%s: %i\n", data->command, status);
263 wfree(data->command);
267 void ExecuteShellCommand(WScreen * scr, char *command)
269 static char *shell = NULL;
273 * This have a problem: if the shell is tcsh (not sure about others)
274 * and ~/.tcshrc have /bin/stty erase ^H somewhere on it, the shell
275 * will block and the command will not be executed.
277 shell = getenv("SHELL");
288 SetupEnvironment(scr);
293 execl(shell, shell, "-c", command, NULL);
294 wsyserror("could not execute %s -c %s", shell, command);
296 } else if (pid < 0) {
297 wsyserror("cannot fork a new process");
299 _tuple *data = wmalloc(sizeof(_tuple));
302 data->command = wstrdup(command);
304 wAddDeathHandler(pid, (WDeathHandler *) shellCommandHandler, data);
309 *---------------------------------------------------------------------
311 * Do a major cleanup and exit the program
313 *----------------------------------------------------------------------
315 void wAbort(Bool dumpCore)
320 for (i = 0; i < wScreenCount; i++) {
321 scr = wScreenWithNumber(i);
325 printf(_("%s aborted.\n"), ProgName);
334 printf(_("Usage: %s [options]\n"), ProgName);
335 puts(_("The Window Maker window manager for the X window system"));
337 puts(_(" -display host:dpy display to use"));
339 puts(_(" --no-cpp disable preprocessing of configuration files"));
341 puts(_(" --no-dock do not open the application Dock"));
342 puts(_(" --no-clip do not open the workspace Clip"));
343 puts(_(" --no-autolaunch do not autolaunch applications"));
344 puts(_(" --dont-restore do not restore saved session"));
346 puts(_(" --locale locale locale to use"));
348 puts(_(" --create-stdcmap create the standard colormap hint in PseudoColor visuals"));
349 puts(_(" --visual-id visualid visual id of visual to use"));
350 puts(_(" --static do not update or save configurations"));
352 puts(_(" --synchronous turn on synchronous display mode"));
354 puts(_(" --version print version and exit"));
355 puts(_(" --help show this message"));
358 void check_defaults()
362 path = wdefaultspathfordomain("WindowMaker");
364 if (access(path, R_OK) != 0) {
366 wfatal(_("could not find user GNUstep directory (%s).\n"
367 "Make sure you have installed Window Maker correctly and run wmaker.inst"), path);
370 wwarning(_("could not find user GNUstep directory (%s)."), path);
372 if (system("wmaker.inst --batch") != 0) {
373 wwarning(_("There was an error while creating GNUstep directory, please "
374 "make sure you have installed Window Maker correctly and run wmaker.inst"));
376 wwarning(_("%s directory created with default configuration."), path);
385 * Add watch here, used to notify if configuration
386 * files have changed, using linux kernel inotify mechanism
389 static void inotifyWatchConfig()
391 char *watchPath = NULL;
392 inotifyFD = inotify_init(); /* Initialise an inotify instance */
394 wwarning(_("could not initialise an inotify instance."
395 " Changes to the defaults database will require"
396 " a restart to take effect. Check your kernel!"));
398 watchPath = wstrconcat(wusergnusteppath(), "/Defaults");
399 /* Add the watch; really we are only looking for modify events
400 * but we might want more in the future so check all events for now.
401 * The individual events are checked for in event.c.
403 inotifyWD = inotify_add_watch(inotifyFD, watchPath, IN_ALL_EVENTS);
405 wwarning(_("could not add an inotify watch on path\n."
407 "Changes to the defaults database will require"
408 " a restart to take effect."), watchPath);
415 static void execInitScript()
419 paths = wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
420 paths = wstrappend(paths, ":" DEF_CONFIG_PATHS);
422 file = wfindfile(paths, DEF_INIT_SCRIPT);
426 if (system(file) != 0) {
427 wsyserror(_("%s:could not execute initialization script"), file);
431 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
432 wsyserror(_("%s:could not execute initialization script"), file);
440 void ExecExitScript()
444 paths = wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
445 paths = wstrappend(paths, ":" DEF_CONFIG_PATHS);
447 file = wfindfile(paths, DEF_EXIT_SCRIPT);
451 if (system(file) != 0) {
452 wsyserror(_("%s:could not execute exit script"), file);
456 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
457 wsyserror(_("%s:could not execute exit script"), file);
466 char *getFullPath(char *path)
470 char *basep = (char *)buffer;
472 if (*path != '/' && getcwd(buffer, 1023)) {
475 if (strncmp(path, "../", 3) == 0) {
477 basep = strchr(basep, '/');
478 if (!basep || *path == 0)
482 if (*path == '/' || strncmp(path, "./", 2) == 0) {
493 return wstrconcat(path);
500 int main(int argc, char **argv)
502 int i_am_the_monitor, i, len;
505 /* setup common stuff for the monitor and wmaker itself */
506 WMInitializeApplication("WindowMaker", &argc, argv);
508 memset(&wPreferences, 0, sizeof(WPreferences));
510 wPreferences.fallbackWMs = WMCreateArray(8);
511 alt = getenv("WINDOWMAKER_ALT_WM");
513 WMAddToArray(wPreferences.fallbackWMs, wstrdup(alt));
515 WMAddToArray(wPreferences.fallbackWMs, wstrdup("blackbox"));
516 WMAddToArray(wPreferences.fallbackWMs, wstrdup("metacity"));
517 WMAddToArray(wPreferences.fallbackWMs, wstrdup("fvwm"));
518 WMAddToArray(wPreferences.fallbackWMs, wstrdup("twm"));
519 WMAddToArray(wPreferences.fallbackWMs, NULL);
520 WMAddToArray(wPreferences.fallbackWMs, wstrdup("rxvt"));
521 WMAddToArray(wPreferences.fallbackWMs, wstrdup("xterm"));
523 i_am_the_monitor = 1;
525 for (i = 1; i < argc; i++) {
526 if (strncmp(argv[i], "--for-real", strlen("--for-real")) == 0) {
527 i_am_the_monitor = 0;
529 } else if (strcmp(argv[i], "-display") == 0 || strcmp(argv[i], "--display") == 0) {
532 wwarning(_("too few arguments for %s"), argv[i - 1]);
535 DisplayName = argv[i];
539 DisplayName = XDisplayName(DisplayName);
540 len = strlen(DisplayName) + 64;
542 snprintf(str, len, "DISPLAY=%s", DisplayName);
545 if (i_am_the_monitor)
546 return MonitorLoop(argc, argv);
548 return real_main(argc, argv);
551 static int real_main(int argc, char **argv)
560 setlocale(LC_ALL, "");
564 /* for telling WPrefs what's the name of the wmaker binary being ran */
565 setenv("WMAKER_BIN_NAME", argv[0], 1);
569 Arguments = wmalloc(sizeof(char *) * (ArgCount + 1));
570 for (i = 0; i < argc; i++) {
571 Arguments[i] = argv[i];
573 /* add the extra option to signal that we're just restarting wmaker */
574 Arguments[argc - 1] = "--for-real=";
575 Arguments[argc] = NULL;
577 ProgName = strrchr(argv[0], '/');
586 for (i = 1; i < argc; i++) {
588 if (strcmp(argv[i], "-nocpp") == 0 || strcmp(argv[i], "--no-cpp") == 0) {
589 wPreferences.flags.nocpp = 1;
592 if (strcmp(argv[i], "--for-real") == 0) {
593 wPreferences.flags.restarting = 0;
594 } else if (strcmp(argv[i], "--for-real=") == 0) {
595 wPreferences.flags.restarting = 1;
596 } else if (strcmp(argv[i], "--for-real-") == 0) {
597 wPreferences.flags.restarting = 2;
598 } else if (strcmp(argv[i], "-no-autolaunch") == 0
599 || strcmp(argv[i], "--no-autolaunch") == 0) {
600 wPreferences.flags.noautolaunch = 1;
601 } else if (strcmp(argv[i], "-dont-restore") == 0 || strcmp(argv[i], "--dont-restore") == 0) {
602 wPreferences.flags.norestore = 1;
603 } else if (strcmp(argv[i], "-nodock") == 0 || strcmp(argv[i], "--no-dock") == 0) {
604 wPreferences.flags.nodock = 1;
605 } else if (strcmp(argv[i], "-noclip") == 0 || strcmp(argv[i], "--no-clip") == 0) {
606 wPreferences.flags.noclip = 1;
607 } else if (strcmp(argv[i], "-version") == 0 || strcmp(argv[i], "--version") == 0) {
608 printf("Window Maker %s\n", VERSION);
610 } else if (strcmp(argv[i], "--global_defaults_path") == 0) {
611 printf("%s/WindowMaker\n", SYSCONFDIR);
614 } else if (strcmp(argv[i], "--synchronous") == 0) {
617 } else if (strcmp(argv[i], "-locale") == 0 || strcmp(argv[i], "--locale") == 0) {
620 wwarning(_("too few arguments for %s"), argv[i - 1]);
624 } else if (strcmp(argv[i], "-display") == 0 || strcmp(argv[i], "--display") == 0) {
627 wwarning(_("too few arguments for %s"), argv[i - 1]);
630 DisplayName = argv[i];
631 } else if (strcmp(argv[i], "-visualid") == 0 || strcmp(argv[i], "--visual-id") == 0) {
634 wwarning(_("too few arguments for %s"), argv[i - 1]);
637 if (sscanf(argv[i], "%i", &wVisualID) != 1) {
638 wwarning(_("bad value for visualid: \"%s\""), argv[i]);
641 } else if (strcmp(argv[i], "-static") == 0 || strcmp(argv[i], "--static") == 0) {
643 wPreferences.flags.noupdates = 1;
645 } else if (strcmp(argv[i], "-clientid") == 0 || strcmp(argv[i], "-restore") == 0) {
648 wwarning(_("too few arguments for %s"), argv[i - 1]);
652 } else if (strcmp(argv[i], "--help") == 0) {
656 printf(_("%s: invalid argument '%s'\n"), argv[0], argv[i]);
657 printf(_("Try '%s --help' for more information\n"), argv[0]);
663 if (!wPreferences.flags.noupdates) {
664 /* check existence of Defaults DB directory */
669 setenv("LANG", Locale, 1);
671 Locale = getenv("LC_ALL");
673 Locale = getenv("LANG");
677 setlocale(LC_ALL, "");
679 if (!Locale || strcmp(Locale, "C") == 0 || strcmp(Locale, "POSIX") == 0)
682 if (getenv("NLSPATH"))
683 bindtextdomain("WindowMaker", getenv("NLSPATH"));
685 bindtextdomain("WindowMaker", LOCALEDIR);
686 bind_textdomain_codeset("WindowMaker", "UTF-8");
687 textdomain("WindowMaker");
689 if (!XSupportsLocale()) {
690 wwarning(_("X server does not support locale"));
693 if (XSetLocaleModifiers("") == NULL) {
694 wwarning(_("cannot set locale modifiers"));
701 Locale = wstrdup(Locale);
702 ptr = strchr(Locale, '.');
708 dpy = XOpenDisplay(DisplayName);
710 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName));
714 if (fcntl(ConnectionNumber(dpy), F_SETFD, FD_CLOEXEC) < 0) {
715 wsyserror("error setting close-on-exec flag for X connection");
721 * If unspecified, use default visual instead of waiting
722 * for wrlib/context.c:bestContext() that may end up choosing
723 * the "fake" 24 bits added by the Composite extension.
724 * This is required to avoid all sort of corruptions when
725 * composite is enabled, and at a depth other than 24.
727 wVisualID = (int)DefaultVisual(dpy, DefaultScreen(dpy))->visualid;
729 /* check if the user specified a complete display name (with screen).
730 * If so, only manage the specified screen */
732 pos = strchr(DisplayName, ':');
736 if (pos && sscanf(pos, ":%i.%i", &d, &s) == 2)
739 DisplayName = XDisplayName(DisplayName);
740 setenv("DISPLAY", DisplayName, 1);
744 XSynchronize(dpy, True);
747 wXModifierInitialize();
750 wSessionConnectManager(argv, argc);
755 if (wScreenCount == 1)
759 inotifyWatchConfig();