Fix focus issues with the switch panel and auto-focus
[wmaker-crm.git] / src / main.c
blobfb7228b312f552c8101ea6608de4dad9fd2bc11d
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 /****** Global Variables ******/
59 /* general info */
61 Display *dpy;
63 char *ProgName;
65 unsigned int ValidModMask = 0xff;
67 int inotifyFD;
68 int inotifyWD;
69 /* locale to use. NULL==POSIX or C */
70 char *Locale = NULL;
72 int wScreenCount = 0;
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;
86 /* XContexts */
87 XContext wWinContext;
88 XContext wAppWinContext;
89 XContext wStackContext;
90 XContext wVEdgeContext;
92 /* Atoms */
93 Atom _XA_WM_STATE;
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;
119 Atom _XA_WM_IGNORE_FOCUS_EVENTS;
121 /* cursors */
122 Cursor wCursor[WCUR_LAST];
124 /* last event timestamp for XSetInputFocus */
125 Time LastTimestamp = CurrentTime;
126 /* timestamp on the last time we did XSetInputFocus() */
127 Time LastFocusChange = CurrentTime;
129 #ifdef SHAPE
130 Bool wShapeSupported;
131 int wShapeEventBase;
132 #endif
134 #ifdef KEEP_XKB_LOCK_STATUS
135 Bool wXkbSupported;
136 int wXkbEventBase;
137 #endif
139 /* special flags */
140 char WProgramSigState = 0;
141 char WProgramState = WSTATE_NORMAL;
142 char WDelayedActionSet = 0;
144 /* temporary stuff */
145 int wVisualID = -1;
147 /* notifications */
148 const char *WMNManaged = "WMNManaged";
149 const char *WMNUnmanaged = "WMNUnmanaged";
150 const char *WMNChangedWorkspace = "WMNChangedWorkspace";
151 const char *WMNChangedState = "WMNChangedState";
152 const char *WMNChangedFocus = "WMNChangedFocus";
153 const char *WMNChangedStacking = "WMNChangedStacking";
154 const char *WMNChangedName = "WMNChangedName";
156 const char *WMNWorkspaceCreated = "WMNWorkspaceCreated";
157 const char *WMNWorkspaceDestroyed = "WMNWorkspaceDestroyed";
158 const char *WMNWorkspaceChanged = "WMNWorkspaceChanged";
159 const char *WMNWorkspaceNameChanged = "WMNWorkspaceNameChanged";
161 const char *WMNResetStacking = "WMNResetStacking";
163 /******** End Global Variables *****/
165 static char *DisplayName = NULL;
167 static char **Arguments;
169 static int ArgCount;
171 extern void EventLoop();
172 extern void StartUp();
173 extern int MonitorLoop(int argc, char **argv);
175 static Bool multiHead = True;
177 static int real_main(int argc, char **argv);
179 void Exit(int status)
181 #ifdef XSMP_ENABLED
182 wSessionDisconnectManager();
183 #endif
184 if (dpy)
185 XCloseDisplay(dpy);
187 exit(status);
190 void Restart(char *manager, Bool abortOnFailure)
192 char *prog = NULL;
193 char *argv[MAX_RESTART_ARGS];
194 int i;
196 if (manager && manager[0] != 0) {
197 prog = argv[0] = strtok(manager, " ");
198 for (i = 1; i < MAX_RESTART_ARGS; i++) {
199 argv[i] = strtok(NULL, " ");
200 if (argv[i] == NULL) {
201 break;
205 if (dpy) {
206 #ifdef XSMP_ENABLED
207 wSessionDisconnectManager();
208 #endif
209 XCloseDisplay(dpy);
210 dpy = NULL;
212 if (!prog) {
213 execvp(Arguments[0], Arguments);
214 wfatal(_("failed to restart Window Maker."));
215 } else {
216 execvp(prog, argv);
217 wsyserror(_("could not exec %s"), prog);
219 if (abortOnFailure)
220 exit(7);
223 void SetupEnvironment(WScreen * scr)
225 char *tmp, *ptr;
226 char buf[16];
228 if (multiHead) {
229 int len = strlen(DisplayName) + 64;
230 tmp = wmalloc(len);
231 snprintf(tmp, len, "DISPLAY=%s", XDisplayName(DisplayName));
232 ptr = strchr(strchr(tmp, ':'), '.');
233 if (ptr)
234 *ptr = 0;
235 snprintf(buf, sizeof(buf), ".%i", scr->screen);
236 strcat(tmp, buf);
237 putenv(tmp);
239 tmp = wmalloc(60);
240 snprintf(tmp, 60, "WRASTER_COLOR_RESOLUTION%i=%i", scr->screen,
241 scr->rcontext->attribs->colors_per_channel);
242 putenv(tmp);
245 typedef struct {
246 WScreen *scr;
247 char *command;
248 } _tuple;
250 static void shellCommandHandler(pid_t pid, unsigned char status, _tuple * data)
252 if (status == 127) {
253 char *buffer;
255 buffer = wstrconcat(_("Could not execute command: "), data->command);
257 wMessageDialog(data->scr, _("Error"), buffer, _("OK"), NULL, NULL);
258 wfree(buffer);
259 } else if (status != 127) {
261 printf("%s: %i\n", data->command, status);
265 wfree(data->command);
266 wfree(data);
269 void ExecuteShellCommand(WScreen * scr, char *command)
271 static char *shell = NULL;
272 pid_t pid;
275 * This have a problem: if the shell is tcsh (not sure about others)
276 * and ~/.tcshrc have /bin/stty erase ^H somewhere on it, the shell
277 * will block and the command will not be executed.
278 if (!shell) {
279 shell = getenv("SHELL");
280 if (!shell)
281 shell = "/bin/sh";
284 shell = "/bin/sh";
286 pid = fork();
288 if (pid == 0) {
290 SetupEnvironment(scr);
292 #ifdef HAVE_SETSID
293 setsid();
294 #endif
295 execl(shell, shell, "-c", command, NULL);
296 wsyserror("could not execute %s -c %s", shell, command);
297 Exit(-1);
298 } else if (pid < 0) {
299 wsyserror("cannot fork a new process");
300 } else {
301 _tuple *data = wmalloc(sizeof(_tuple));
303 data->scr = scr;
304 data->command = wstrdup(command);
306 wAddDeathHandler(pid, (WDeathHandler *) shellCommandHandler, data);
311 *---------------------------------------------------------------------
312 * wAbort--
313 * Do a major cleanup and exit the program
315 *----------------------------------------------------------------------
317 void wAbort(Bool dumpCore)
319 int i;
320 WScreen *scr;
322 for (i = 0; i < wScreenCount; i++) {
323 scr = wScreenWithNumber(i);
324 if (scr)
325 RestoreDesktop(scr);
327 printf(_("%s aborted.\n"), ProgName);
328 if (dumpCore)
329 abort();
330 else
331 exit(1);
334 void print_help()
336 printf(_("Usage: %s [options]\n"), ProgName);
337 puts(_("The Window Maker window manager for the X window system"));
338 puts("");
339 puts(_(" -display host:dpy display to use"));
340 #ifdef USECPP
341 puts(_(" --no-cpp disable preprocessing of configuration files"));
342 #endif
343 puts(_(" --no-dock do not open the application Dock"));
344 puts(_(" --no-clip do not open the workspace Clip"));
345 puts(_(" --no-autolaunch do not autolaunch applications"));
346 puts(_(" --dont-restore do not restore saved session"));
348 puts(_(" --locale locale locale to use"));
350 puts(_(" --create-stdcmap create the standard colormap hint in PseudoColor visuals"));
351 puts(_(" --visual-id visualid visual id of visual to use"));
352 puts(_(" --static do not update or save configurations"));
353 #ifdef DEBUG
354 puts(_(" --synchronous turn on synchronous display mode"));
355 #endif
356 puts(_(" --version print version and exit"));
357 puts(_(" --help show this message"));
360 void check_defaults()
362 char *path;
364 path = wdefaultspathfordomain("WindowMaker");
366 if (access(path, R_OK) != 0) {
367 #if 0
368 wfatal(_("could not find user GNUstep directory (%s).\n"
369 "Make sure you have installed Window Maker correctly and run wmaker.inst"), path);
370 exit(1);
371 #else
372 wwarning(_("could not find user GNUstep directory (%s)."), path);
374 if (system("wmaker.inst --batch") != 0) {
375 wwarning(_("There was an error while creating GNUstep directory, please "
376 "make sure you have installed Window Maker correctly and run wmaker.inst"));
377 } else {
378 wwarning(_("%s directory created with default configuration."), path);
380 #endif
383 wfree(path);
387 * Add watch here, used to notify if configuration
388 * files have changed, using linux kernel inotify mechanism
391 static void inotifyWatchConfig()
393 char *watchPath = NULL;
394 inotifyFD = inotify_init(); /* Initialise an inotify instance */
395 if (inotifyFD < 0) {
396 wwarning(_("could not initialise an inotify instance."
397 " Changes to the defaults database will require"
398 " a restart to take effect. Check your kernel!"));
399 } else {
400 watchPath = wstrconcat(wusergnusteppath(), "/Defaults");
401 /* Add the watch; really we are only looking for modify events
402 * but we might want more in the future so check all events for now.
403 * The individual events are checked for in event.c.
405 inotifyWD = inotify_add_watch(inotifyFD, watchPath, IN_ALL_EVENTS);
406 if (inotifyWD < 0) {
407 wwarning(_("could not add an inotify watch on path\n."
408 "%s\n"
409 "Changes to the defaults database will require"
410 " a restart to take effect."), watchPath);
411 close(inotifyFD);
414 wfree(watchPath);
417 static void execInitScript()
419 char *file, *paths;
421 paths = wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
422 paths = wstrappend(paths, ":" DEF_CONFIG_PATHS);
424 file = wfindfile(paths, DEF_INIT_SCRIPT);
425 wfree(paths);
427 if (file) {
428 if (system(file) != 0) {
429 wsyserror(_("%s:could not execute initialization script"), file);
431 #if 0
432 if (fork() == 0) {
433 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
434 wsyserror(_("%s:could not execute initialization script"), file);
435 exit(1);
437 #endif
438 wfree(file);
442 void ExecExitScript()
444 char *file, *paths;
446 paths = wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
447 paths = wstrappend(paths, ":" DEF_CONFIG_PATHS);
449 file = wfindfile(paths, DEF_EXIT_SCRIPT);
450 wfree(paths);
452 if (file) {
453 if (system(file) != 0) {
454 wsyserror(_("%s:could not execute exit script"), file);
456 #if 0
457 if (fork() == 0) {
458 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
459 wsyserror(_("%s:could not execute exit script"), file);
460 exit(1);
462 #endif
463 wfree(file);
467 #if 0
468 char *getFullPath(char *path)
470 char buffer[1024];
471 char *tmp;
472 char *basep = (char *)buffer;
474 if (*path != '/' && getcwd(buffer, 1023)) {
476 for (;;) {
477 if (strncmp(path, "../", 3) == 0) {
478 path += 3;
479 basep = strchr(basep, '/');
480 if (!basep || *path == 0)
481 break;
484 if (*path == '/' || strncmp(path, "./", 2) == 0) {
485 tmp =}
488 * path
489 * ./path
490 * ../path
491 * ../../path
494 } else {
495 return wstrconcat(path);
498 return tmp;
500 #endif
502 int main(int argc, char **argv)
504 int i_am_the_monitor, i, len;
505 char *str, *alt;
507 /* setup common stuff for the monitor and wmaker itself */
508 WMInitializeApplication("WindowMaker", &argc, argv);
510 memset(&wPreferences, 0, sizeof(WPreferences));
512 wPreferences.fallbackWMs = WMCreateArray(8);
513 alt = getenv("WINDOWMAKER_ALT_WM");
514 if (alt != NULL)
515 WMAddToArray(wPreferences.fallbackWMs, wstrdup(alt));
517 WMAddToArray(wPreferences.fallbackWMs, wstrdup("blackbox"));
518 WMAddToArray(wPreferences.fallbackWMs, wstrdup("metacity"));
519 WMAddToArray(wPreferences.fallbackWMs, wstrdup("fvwm"));
520 WMAddToArray(wPreferences.fallbackWMs, wstrdup("twm"));
521 WMAddToArray(wPreferences.fallbackWMs, NULL);
522 WMAddToArray(wPreferences.fallbackWMs, wstrdup("rxvt"));
523 WMAddToArray(wPreferences.fallbackWMs, wstrdup("xterm"));
525 i_am_the_monitor = 1;
527 for (i = 1; i < argc; i++) {
528 if (strncmp(argv[i], "--for-real", strlen("--for-real")) == 0) {
529 i_am_the_monitor = 0;
530 break;
531 } else if (strcmp(argv[i], "-display") == 0 || strcmp(argv[i], "--display") == 0) {
532 i++;
533 if (i >= argc) {
534 wwarning(_("too few arguments for %s"), argv[i - 1]);
535 exit(0);
537 DisplayName = argv[i];
541 DisplayName = XDisplayName(DisplayName);
542 len = strlen(DisplayName) + 64;
543 str = wmalloc(len);
544 snprintf(str, len, "DISPLAY=%s", DisplayName);
545 putenv(str);
547 if (i_am_the_monitor)
548 return MonitorLoop(argc, argv);
549 else
550 return real_main(argc, argv);
553 static int real_main(int argc, char **argv)
555 int i, restart = 0;
556 char *pos;
557 int d, s;
558 int flag;
559 #ifdef DEBUG
560 Bool doSync = False;
561 #endif
562 setlocale(LC_ALL, "");
564 wsetabort(wAbort);
566 /* for telling WPrefs what's the name of the wmaker binary being ran */
567 setenv("WMAKER_BIN_NAME", argv[0], 1);
569 flag = 0;
570 ArgCount = argc;
571 Arguments = wmalloc(sizeof(char *) * (ArgCount + 1));
572 for (i = 0; i < argc; i++) {
573 Arguments[i] = argv[i];
575 /* add the extra option to signal that we're just restarting wmaker */
576 Arguments[argc - 1] = "--for-real=";
577 Arguments[argc] = NULL;
579 ProgName = strrchr(argv[0], '/');
580 if (!ProgName)
581 ProgName = argv[0];
582 else
583 ProgName++;
585 restart = 0;
587 if (argc > 1) {
588 for (i = 1; i < argc; i++) {
589 #ifdef USECPP
590 if (strcmp(argv[i], "-nocpp") == 0 || strcmp(argv[i], "--no-cpp") == 0) {
591 wPreferences.flags.nocpp = 1;
592 } else
593 #endif
594 if (strcmp(argv[i], "--for-real") == 0) {
595 wPreferences.flags.restarting = 0;
596 } else if (strcmp(argv[i], "--for-real=") == 0) {
597 wPreferences.flags.restarting = 1;
598 } else if (strcmp(argv[i], "--for-real-") == 0) {
599 wPreferences.flags.restarting = 2;
600 } else if (strcmp(argv[i], "-no-autolaunch") == 0
601 || strcmp(argv[i], "--no-autolaunch") == 0) {
602 wPreferences.flags.noautolaunch = 1;
603 } else if (strcmp(argv[i], "-dont-restore") == 0 || strcmp(argv[i], "--dont-restore") == 0) {
604 wPreferences.flags.norestore = 1;
605 } else if (strcmp(argv[i], "-nodock") == 0 || strcmp(argv[i], "--no-dock") == 0) {
606 wPreferences.flags.nodock = 1;
607 } else if (strcmp(argv[i], "-noclip") == 0 || strcmp(argv[i], "--no-clip") == 0) {
608 wPreferences.flags.noclip = 1;
609 } else if (strcmp(argv[i], "-version") == 0 || strcmp(argv[i], "--version") == 0) {
610 printf("Window Maker %s\n", VERSION);
611 exit(0);
612 } else if (strcmp(argv[i], "--global_defaults_path") == 0) {
613 printf("%s/WindowMaker\n", SYSCONFDIR);
614 exit(0);
615 #ifdef DEBUG
616 } else if (strcmp(argv[i], "--synchronous") == 0) {
617 doSync = 1;
618 #endif
619 } else if (strcmp(argv[i], "-locale") == 0 || strcmp(argv[i], "--locale") == 0) {
620 i++;
621 if (i >= argc) {
622 wwarning(_("too few arguments for %s"), argv[i - 1]);
623 exit(0);
625 Locale = argv[i];
626 } else if (strcmp(argv[i], "-display") == 0 || strcmp(argv[i], "--display") == 0) {
627 i++;
628 if (i >= argc) {
629 wwarning(_("too few arguments for %s"), argv[i - 1]);
630 exit(0);
632 DisplayName = argv[i];
633 } else if (strcmp(argv[i], "-visualid") == 0 || strcmp(argv[i], "--visual-id") == 0) {
634 i++;
635 if (i >= argc) {
636 wwarning(_("too few arguments for %s"), argv[i - 1]);
637 exit(0);
639 if (sscanf(argv[i], "%i", &wVisualID) != 1) {
640 wwarning(_("bad value for visualid: \"%s\""), argv[i]);
641 exit(0);
643 } else if (strcmp(argv[i], "-static") == 0 || strcmp(argv[i], "--static") == 0) {
645 wPreferences.flags.noupdates = 1;
646 #ifdef XSMP_ENABLED
647 } else if (strcmp(argv[i], "-clientid") == 0 || strcmp(argv[i], "-restore") == 0) {
648 i++;
649 if (i >= argc) {
650 wwarning(_("too few arguments for %s"), argv[i - 1]);
651 exit(0);
653 #endif
654 } else if (strcmp(argv[i], "--help") == 0) {
655 print_help();
656 exit(0);
657 } else {
658 printf(_("%s: invalid argument '%s'\n"), argv[0], argv[i]);
659 printf(_("Try '%s --help' for more information\n"), argv[0]);
660 exit(1);
665 if (!wPreferences.flags.noupdates) {
666 /* check existence of Defaults DB directory */
667 check_defaults();
670 if (Locale) {
671 setenv("LANG", Locale, 1);
672 } else {
673 Locale = getenv("LC_ALL");
674 if (!Locale) {
675 Locale = getenv("LANG");
679 setlocale(LC_ALL, "");
681 if (!Locale || strcmp(Locale, "C") == 0 || strcmp(Locale, "POSIX") == 0)
682 Locale = NULL;
683 #ifdef I18N
684 if (getenv("NLSPATH"))
685 bindtextdomain("WindowMaker", getenv("NLSPATH"));
686 else
687 bindtextdomain("WindowMaker", LOCALEDIR);
688 bind_textdomain_codeset("WindowMaker", "UTF-8");
689 textdomain("WindowMaker");
691 if (!XSupportsLocale()) {
692 wwarning(_("X server does not support locale"));
695 if (XSetLocaleModifiers("") == NULL) {
696 wwarning(_("cannot set locale modifiers"));
698 #endif
700 if (Locale) {
701 char *ptr;
703 Locale = wstrdup(Locale);
704 ptr = strchr(Locale, '.');
705 if (ptr)
706 *ptr = 0;
709 /* open display */
710 dpy = XOpenDisplay(DisplayName);
711 if (dpy == NULL) {
712 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName));
713 exit(1);
716 if (fcntl(ConnectionNumber(dpy), F_SETFD, FD_CLOEXEC) < 0) {
717 wsyserror("error setting close-on-exec flag for X connection");
718 exit(1);
721 if (wVisualID < 0)
723 * If unspecified, use default visual instead of waiting
724 * for wrlib/context.c:bestContext() that may end up choosing
725 * the "fake" 24 bits added by the Composite extension.
726 * This is required to avoid all sort of corruptions when
727 * composite is enabled, and at a depth other than 24.
729 wVisualID = (int)DefaultVisual(dpy, DefaultScreen(dpy))->visualid;
731 /* check if the user specified a complete display name (with screen).
732 * If so, only manage the specified screen */
733 if (DisplayName)
734 pos = strchr(DisplayName, ':');
735 else
736 pos = NULL;
738 if (pos && sscanf(pos, ":%i.%i", &d, &s) == 2)
739 multiHead = False;
741 DisplayName = XDisplayName(DisplayName);
742 setenv("DISPLAY", DisplayName, 1);
744 #ifdef DEBUG
745 if (doSync)
746 XSynchronize(dpy, True);
747 #endif
749 wXModifierInitialize();
751 #ifdef XSMP_ENABLED
752 wSessionConnectManager(argv, argc);
753 #endif
755 StartUp(!multiHead);
757 if (wScreenCount == 1)
758 multiHead = False;
760 execInitScript();
761 inotifyWatchConfig();
762 EventLoop();
763 return -1;