Remove wmlib
[wmaker-crm.git] / src / main.c
blobbd59b68765b708bb5ae4a92ff79c7c03e3b2c3c0
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 "wconfig.h"
24 #ifdef HAVE_INOTIFY
25 #include <sys/inotify.h>
26 #endif
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <string.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
34 #include <fcntl.h>
36 #include <X11/Xlib.h>
37 #include <X11/Xutil.h>
39 /* Xlocale.h and locale.h are the same if X_LOCALE is undefind in wconfig.h,
40 * and if X_LOCALE is defined, X's locale emulating functions will be used.
41 * See Xlocale.h for more information.
43 #include <X11/Xlocale.h>
45 #define MAINFILE
47 #include "WindowMaker.h"
48 #include "window.h"
49 #include "funcs.h"
50 #include "menu.h"
51 #include "keybind.h"
52 #include "xmodifier.h"
53 #include "session.h"
54 #include "dialog.h"
56 #include <WINGs/WUtil.h>
58 #ifndef GLOBAL_DEFAULTS_SUBDIR
59 #define GLOBAL_DEFAULTS_SUBDIR "WindowMaker"
60 #endif
62 /****** Global Variables ******/
64 /* general info */
66 Display *dpy;
68 char *ProgName;
70 unsigned int ValidModMask = 0xff;
72 #ifdef HAVE_INOTIFY
73 int inotifyFD;
74 int inotifyWD;
75 #endif
76 /* locale to use. NULL==POSIX or C */
77 char *Locale = NULL;
79 int wScreenCount = 0;
81 WPreferences wPreferences;
83 WMPropList *wDomainName;
84 WMPropList *wAttributeDomainName;
86 WShortKey wKeyBindings[WKBD_LAST];
88 /* defaults domains */
89 WDDomain *WDWindowMaker = NULL;
90 WDDomain *WDWindowAttributes = NULL;
91 WDDomain *WDRootMenu = NULL;
93 /* XContexts */
94 XContext wWinContext;
95 XContext wAppWinContext;
96 XContext wStackContext;
97 XContext wVEdgeContext;
99 /* Atoms */
100 Atom _XA_WM_STATE;
101 Atom _XA_WM_CHANGE_STATE;
102 Atom _XA_WM_PROTOCOLS;
103 Atom _XA_WM_TAKE_FOCUS;
104 Atom _XA_WM_DELETE_WINDOW;
105 Atom _XA_WM_SAVE_YOURSELF;
106 Atom _XA_WM_CLIENT_LEADER;
107 Atom _XA_WM_COLORMAP_WINDOWS;
108 Atom _XA_WM_COLORMAP_NOTIFY;
110 Atom _XA_GNUSTEP_WM_ATTR;
111 Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
112 Atom _XA_GNUSTEP_WM_RESIZEBAR;
113 Atom _XA_GNUSTEP_TITLEBAR_STATE;
115 Atom _XA_WINDOWMAKER_MENU;
116 Atom _XA_WINDOWMAKER_WM_PROTOCOLS;
117 Atom _XA_WINDOWMAKER_STATE;
119 Atom _XA_WINDOWMAKER_WM_FUNCTION;
120 Atom _XA_WINDOWMAKER_NOTICEBOARD;
121 Atom _XA_WINDOWMAKER_COMMAND;
123 Atom _XA_WINDOWMAKER_ICON_SIZE;
124 Atom _XA_WINDOWMAKER_ICON_TILE;
126 Atom _XA_WM_IGNORE_FOCUS_EVENTS;
128 /* cursors */
129 Cursor wCursor[WCUR_LAST];
131 /* last event timestamp for XSetInputFocus */
132 Time LastTimestamp = CurrentTime;
133 /* timestamp on the last time we did XSetInputFocus() */
134 Time LastFocusChange = CurrentTime;
136 #ifdef SHAPE
137 Bool wShapeSupported;
138 int wShapeEventBase;
139 #endif
141 #ifdef KEEP_XKB_LOCK_STATUS
142 Bool wXkbSupported;
143 int wXkbEventBase;
144 #endif
146 /* special flags */
147 char WProgramSigState = 0;
148 char WProgramState = WSTATE_NORMAL;
149 char WDelayedActionSet = 0;
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 *wVisualID = NULL;
182 static int wVisualID_len = 0;
184 static int real_main(int argc, char **argv);
186 int getWVisualID(int screen)
188 if (wVisualID == NULL)
189 return -1;
190 if (screen < 0 || screen >= wVisualID_len)
191 return -1;
193 return wVisualID[screen];
196 static void setWVisualID(int screen, int val)
198 int i;
200 if (screen < 0)
201 return;
203 if (wVisualID == NULL) {
204 /* no array at all, alloc space for screen + 1 entries
205 * and init with default value */
206 wVisualID_len = screen + 1;
207 wVisualID = (int *)malloc(wVisualID_len * sizeof(int));
208 for (i = 0; i < wVisualID_len; i++) {
209 wVisualID[i] = -1;
211 } else if (screen >= wVisualID_len) {
212 /* larger screen number than previously allocated
213 so enlarge array */
214 int oldlen = wVisualID_len;
216 wVisualID_len = screen + 1;
217 wVisualID = (int *)realloc(wVisualID, wVisualID_len * sizeof(int));
218 for (i = oldlen; i < wVisualID_len; i++) {
219 wVisualID[i] = -1;
223 wVisualID[screen] = val;
227 * this function splits a given string at the comma into tokens
228 * and set the wVisualID variable to each parsed number
230 static int initWVisualID(const char *user_str)
232 char *mystr = strdup(user_str);
233 int cur_in_pos = 0;
234 int cur_out_pos = 0;
235 int cur_screen = 0;
236 int error_found = 0;
238 for (;;) {
239 /* check for delimiter */
240 if (user_str[cur_in_pos] == '\0' || user_str[cur_in_pos] == ',') {
241 int v;
243 mystr[cur_out_pos] = '\0';
245 if (sscanf(mystr, "%i", &v) != 1) {
246 error_found = 1;
247 break;
250 setWVisualID(cur_screen, v);
252 cur_screen++;
253 cur_out_pos = 0;
256 /* break in case last char has been consumed */
257 if (user_str[cur_in_pos] == '\0')
258 break;
260 /* if the current char is no delimiter put it into mystr */
261 if (user_str[cur_in_pos] != ',') {
262 mystr[cur_out_pos++] = user_str[cur_in_pos];
264 cur_in_pos++;
267 free(mystr);
269 if (cur_screen == 0||error_found != 0)
270 return 1;
272 return 0;
275 void Exit(int status)
277 if (dpy)
278 XCloseDisplay(dpy);
280 exit(status);
283 void Restart(char *manager, Bool abortOnFailure)
285 char *prog = NULL;
286 char *argv[MAX_RESTART_ARGS];
287 int i;
289 if (manager && manager[0] != 0) {
290 prog = argv[0] = strtok(manager, " ");
291 for (i = 1; i < MAX_RESTART_ARGS; i++) {
292 argv[i] = strtok(NULL, " ");
293 if (argv[i] == NULL) {
294 break;
298 if (dpy) {
299 XCloseDisplay(dpy);
300 dpy = NULL;
302 if (!prog) {
303 execvp(Arguments[0], Arguments);
304 wfatal(_("failed to restart Window Maker."));
305 } else {
306 execvp(prog, argv);
307 wsyserror(_("could not exec %s"), prog);
309 if (abortOnFailure)
310 exit(7);
313 void SetupEnvironment(WScreen * scr)
315 char *tmp, *ptr;
316 char buf[16];
318 if (multiHead) {
319 int len = strlen(DisplayName) + 64;
320 tmp = wmalloc(len);
321 snprintf(tmp, len, "DISPLAY=%s", XDisplayName(DisplayName));
322 ptr = strchr(strchr(tmp, ':'), '.');
323 if (ptr)
324 *ptr = 0;
325 snprintf(buf, sizeof(buf), ".%i", scr->screen);
326 strcat(tmp, buf);
327 putenv(tmp);
329 tmp = wmalloc(60);
330 snprintf(tmp, 60, "WRASTER_COLOR_RESOLUTION%i=%i", scr->screen,
331 scr->rcontext->attribs->colors_per_channel);
332 putenv(tmp);
335 typedef struct {
336 WScreen *scr;
337 char *command;
338 } _tuple;
340 static void shellCommandHandler(pid_t pid, unsigned char status, _tuple * data)
342 if (status == 127) {
343 char *buffer;
345 buffer = wstrconcat(_("Could not execute command: "), data->command);
347 wMessageDialog(data->scr, _("Error"), buffer, _("OK"), NULL, NULL);
348 wfree(buffer);
349 } else if (status != 127) {
351 printf("%s: %i\n", data->command, status);
355 wfree(data->command);
356 wfree(data);
359 void ExecuteShellCommand(WScreen * scr, char *command)
361 static char *shell = NULL;
362 pid_t pid;
365 * This have a problem: if the shell is tcsh (not sure about others)
366 * and ~/.tcshrc have /bin/stty erase ^H somewhere on it, the shell
367 * will block and the command will not be executed.
368 if (!shell) {
369 shell = getenv("SHELL");
370 if (!shell)
371 shell = "/bin/sh";
374 shell = "/bin/sh";
376 pid = fork();
378 if (pid == 0) {
380 SetupEnvironment(scr);
382 #ifdef HAVE_SETSID
383 setsid();
384 #endif
385 execl(shell, shell, "-c", command, NULL);
386 wsyserror("could not execute %s -c %s", shell, command);
387 Exit(-1);
388 } else if (pid < 0) {
389 wsyserror("cannot fork a new process");
390 } else {
391 _tuple *data = wmalloc(sizeof(_tuple));
393 data->scr = scr;
394 data->command = wstrdup(command);
396 wAddDeathHandler(pid, (WDeathHandler *) shellCommandHandler, data);
401 *---------------------------------------------------------------------
402 * wAbort--
403 * Do a major cleanup and exit the program
405 *----------------------------------------------------------------------
407 void wAbort(Bool dumpCore)
409 int i;
410 WScreen *scr;
412 for (i = 0; i < wScreenCount; i++) {
413 scr = wScreenWithNumber(i);
414 if (scr)
415 RestoreDesktop(scr);
417 printf(_("%s aborted.\n"), ProgName);
418 if (dumpCore)
419 abort();
420 else
421 exit(1);
424 void print_help()
426 printf(_("Usage: %s [options]\n"), ProgName);
427 puts(_("The Window Maker window manager for the X window system"));
428 puts("");
429 puts(_(" -display host:dpy display to use"));
430 #ifdef USECPP
431 puts(_(" --no-cpp disable preprocessing of configuration files"));
432 #endif
433 puts(_(" --no-dock do not open the application Dock"));
434 puts(_(" --no-clip do not open the workspace Clip"));
435 puts(_(" --no-autolaunch do not autolaunch applications"));
436 puts(_(" --dont-restore do not restore saved session"));
438 puts(_(" --locale locale locale to use"));
440 puts(_(" --create-stdcmap create the standard colormap hint in PseudoColor visuals"));
441 puts(_(" --visual-id visualid visual id of visual to use"));
442 puts(_(" --static do not update or save configurations"));
443 #ifndef HAVE_INOTIFY
444 puts(_(" --no-polling do not periodically check for configuration updates"));
445 #endif
446 puts(_(" --version print version and exit"));
447 puts(_(" --help show this message"));
450 void check_defaults()
452 char *path;
454 path = wdefaultspathfordomain("WindowMaker");
456 if (access(path, R_OK) != 0) {
457 wwarning(_("could not find user GNUstep directory (%s)."), path);
459 if (system("wmaker.inst --batch") != 0) {
460 wwarning(_("There was an error while creating GNUstep directory, please "
461 "make sure you have installed Window Maker correctly and run wmaker.inst"));
462 } else {
463 wwarning(_("%s directory created with default configuration."), path);
467 wfree(path);
470 #ifdef HAVE_INOTIFY
472 * Add watch here, used to notify if configuration
473 * files have changed, using linux kernel inotify mechanism
476 static void inotifyWatchConfig()
478 char *watchPath = NULL;
479 inotifyFD = inotify_init(); /* Initialise an inotify instance */
480 if (inotifyFD < 0) {
481 wwarning(_("could not initialise an inotify instance."
482 " Changes to the defaults database will require"
483 " a restart to take effect. Check your kernel!"));
484 } else {
485 watchPath = wstrconcat(wusergnusteppath(), "/Defaults");
486 /* Add the watch; really we are only looking for modify events
487 * but we might want more in the future so check all events for now.
488 * The individual events are checked for in event.c.
490 inotifyWD = inotify_add_watch(inotifyFD, watchPath, IN_ALL_EVENTS);
491 if (inotifyWD < 0) {
492 wwarning(_("could not add an inotify watch on path\n."
493 "%s\n"
494 "Changes to the defaults database will require"
495 " a restart to take effect."), watchPath);
496 close(inotifyFD);
499 wfree(watchPath);
501 #endif /* HAVE_INOTIFY */
503 static void execInitScript()
505 char *file, *paths;
507 paths = wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
508 paths = wstrappend(paths, ":" DEF_CONFIG_PATHS);
510 file = wfindfile(paths, DEF_INIT_SCRIPT);
511 wfree(paths);
513 if (file) {
514 if (system(file) != 0) {
515 wsyserror(_("%s:could not execute initialization script"), file);
517 wfree(file);
521 void ExecExitScript()
523 char *file, *paths;
525 paths = wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
526 paths = wstrappend(paths, ":" DEF_CONFIG_PATHS);
528 file = wfindfile(paths, DEF_EXIT_SCRIPT);
529 wfree(paths);
531 if (file) {
532 if (system(file) != 0) {
533 wsyserror(_("%s:could not execute exit script"), file);
535 wfree(file);
539 int main(int argc, char **argv)
541 int i_am_the_monitor, i, len;
542 char *str, *alt;
544 /* setup common stuff for the monitor and wmaker itself */
545 WMInitializeApplication("WindowMaker", &argc, argv);
547 memset(&wPreferences, 0, sizeof(WPreferences));
549 wPreferences.fallbackWMs = WMCreateArray(8);
550 alt = getenv("WINDOWMAKER_ALT_WM");
551 if (alt != NULL)
552 WMAddToArray(wPreferences.fallbackWMs, wstrdup(alt));
554 WMAddToArray(wPreferences.fallbackWMs, wstrdup("blackbox"));
555 WMAddToArray(wPreferences.fallbackWMs, wstrdup("metacity"));
556 WMAddToArray(wPreferences.fallbackWMs, wstrdup("fvwm"));
557 WMAddToArray(wPreferences.fallbackWMs, wstrdup("twm"));
558 WMAddToArray(wPreferences.fallbackWMs, NULL);
559 WMAddToArray(wPreferences.fallbackWMs, wstrdup("rxvt"));
560 WMAddToArray(wPreferences.fallbackWMs, wstrdup("xterm"));
562 i_am_the_monitor = 1;
564 for (i = 1; i < argc; i++) {
565 if (strncmp(argv[i], "--for-real", strlen("--for-real")) == 0) {
566 i_am_the_monitor = 0;
567 break;
568 } else if (strcmp(argv[i], "-display") == 0 || strcmp(argv[i], "--display") == 0) {
569 i++;
570 if (i >= argc) {
571 wwarning(_("too few arguments for %s"), argv[i - 1]);
572 exit(0);
574 DisplayName = argv[i];
578 DisplayName = XDisplayName(DisplayName);
579 len = strlen(DisplayName) + 64;
580 str = wmalloc(len);
581 snprintf(str, len, "DISPLAY=%s", DisplayName);
582 putenv(str);
584 if (i_am_the_monitor)
585 return MonitorLoop(argc, argv);
586 else
587 return real_main(argc, argv);
590 static int real_main(int argc, char **argv)
592 int i, restart = 0;
593 char *pos;
594 int d, s;
595 int flag;
597 setlocale(LC_ALL, "");
598 wsetabort(wAbort);
600 /* for telling WPrefs what's the name of the wmaker binary being ran */
601 setenv("WMAKER_BIN_NAME", argv[0], 1);
603 flag = 0;
604 ArgCount = argc;
605 Arguments = wmalloc(sizeof(char *) * (ArgCount + 1));
606 for (i = 0; i < argc; i++) {
607 Arguments[i] = argv[i];
609 /* add the extra option to signal that we're just restarting wmaker */
610 Arguments[argc - 1] = "--for-real=";
611 Arguments[argc] = NULL;
613 ProgName = strrchr(argv[0], '/');
614 if (!ProgName)
615 ProgName = argv[0];
616 else
617 ProgName++;
619 restart = 0;
621 if (argc > 1) {
622 for (i = 1; i < argc; i++) {
623 #ifdef USECPP
624 if (strcmp(argv[i], "-nocpp") == 0 || strcmp(argv[i], "--no-cpp") == 0) {
625 wPreferences.flags.nocpp = 1;
626 } else
627 #endif
628 if (strcmp(argv[i], "--for-real") == 0) {
629 wPreferences.flags.restarting = 0;
630 } else if (strcmp(argv[i], "--for-real=") == 0) {
631 wPreferences.flags.restarting = 1;
632 } else if (strcmp(argv[i], "--for-real-") == 0) {
633 wPreferences.flags.restarting = 2;
634 } else if (strcmp(argv[i], "-no-autolaunch") == 0
635 || strcmp(argv[i], "--no-autolaunch") == 0) {
636 wPreferences.flags.noautolaunch = 1;
637 } else if (strcmp(argv[i], "-dont-restore") == 0 || strcmp(argv[i], "--dont-restore") == 0) {
638 wPreferences.flags.norestore = 1;
639 } else if (strcmp(argv[i], "-nodock") == 0 || strcmp(argv[i], "--no-dock") == 0) {
640 wPreferences.flags.nodock = 1;
641 } else if (strcmp(argv[i], "-noclip") == 0 || strcmp(argv[i], "--no-clip") == 0) {
642 wPreferences.flags.noclip = 1;
643 } else if (strcmp(argv[i], "-version") == 0 || strcmp(argv[i], "--version") == 0) {
644 printf("Window Maker %s\n", VERSION);
645 exit(0);
646 } else if (strcmp(argv[i], "--global_defaults_path") == 0) {
647 printf("%s/%s\n", SYSCONFDIR, GLOBAL_DEFAULTS_SUBDIR);
648 exit(0);
649 } else if (strcmp(argv[i], "-locale") == 0 || strcmp(argv[i], "--locale") == 0) {
650 i++;
651 if (i >= argc) {
652 wwarning(_("too few arguments for %s"), argv[i - 1]);
653 exit(0);
655 Locale = argv[i];
656 } else if (strcmp(argv[i], "-display") == 0 || strcmp(argv[i], "--display") == 0) {
657 i++;
658 if (i >= argc) {
659 wwarning(_("too few arguments for %s"), argv[i - 1]);
660 exit(0);
662 DisplayName = argv[i];
663 } else if (strcmp(argv[i], "-visualid") == 0 || strcmp(argv[i], "--visual-id") == 0) {
664 i++;
665 if (i >= argc) {
666 wwarning(_("too few arguments for %s"), argv[i - 1]);
667 exit(0);
669 if (initWVisualID(argv[i]) != 0) {
670 wwarning(_("bad value for visualid: \"%s\""), argv[i]);
671 exit(0);
673 } else if (strcmp(argv[i], "-static") == 0 || strcmp(argv[i], "--static") == 0
674 #ifndef HAVE_INOTIFY
675 || strcmp(argv[i], "--no-polling") == 0
676 #endif
678 wPreferences.flags.noupdates = 1;
679 } else if (strcmp(argv[i], "--help") == 0) {
680 print_help();
681 exit(0);
682 } else {
683 printf(_("%s: invalid argument '%s'\n"), argv[0], argv[i]);
684 printf(_("Try '%s --help' for more information\n"), argv[0]);
685 exit(1);
690 if (!wPreferences.flags.noupdates) {
691 /* check existence of Defaults DB directory */
692 check_defaults();
695 if (Locale) {
696 setenv("LANG", Locale, 1);
697 } else {
698 Locale = getenv("LC_ALL");
699 if (!Locale) {
700 Locale = getenv("LANG");
704 setlocale(LC_ALL, "");
706 if (!Locale || strcmp(Locale, "C") == 0 || strcmp(Locale, "POSIX") == 0)
707 Locale = NULL;
708 #ifdef I18N
709 if (getenv("NLSPATH"))
710 bindtextdomain("WindowMaker", getenv("NLSPATH"));
711 else
712 bindtextdomain("WindowMaker", LOCALEDIR);
713 bind_textdomain_codeset("WindowMaker", "UTF-8");
714 textdomain("WindowMaker");
716 if (!XSupportsLocale()) {
717 wwarning(_("X server does not support locale"));
720 if (XSetLocaleModifiers("") == NULL) {
721 wwarning(_("cannot set locale modifiers"));
723 #endif
725 if (Locale) {
726 char *ptr;
728 Locale = wstrdup(Locale);
729 ptr = strchr(Locale, '.');
730 if (ptr)
731 *ptr = 0;
734 /* open display */
735 dpy = XOpenDisplay(DisplayName);
736 if (dpy == NULL) {
737 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName));
738 exit(1);
741 if (fcntl(ConnectionNumber(dpy), F_SETFD, FD_CLOEXEC) < 0) {
742 wsyserror("error setting close-on-exec flag for X connection");
743 exit(1);
747 if (getWVisualID(0) < 0) {
749 * If unspecified, use default visual instead of waiting
750 * for wrlib/context.c:bestContext() that may end up choosing
751 * the "fake" 24 bits added by the Composite extension.
752 * This is required to avoid all sort of corruptions when
753 * composite is enabled, and at a depth other than 24.
755 setWVisualID(0, (int)DefaultVisual(dpy, DefaultScreen(dpy))->visualid);
758 /* check if the user specified a complete display name (with screen).
759 * If so, only manage the specified screen */
760 if (DisplayName)
761 pos = strchr(DisplayName, ':');
762 else
763 pos = NULL;
765 if (pos && sscanf(pos, ":%i.%i", &d, &s) == 2)
766 multiHead = False;
768 DisplayName = XDisplayName(DisplayName);
769 setenv("DISPLAY", DisplayName, 1);
771 wXModifierInitialize();
772 StartUp(!multiHead);
774 if (wScreenCount == 1)
775 multiHead = False;
777 execInitScript();
778 #ifdef HAVE_INOTIFY
779 inotifyWatchConfig();
780 #endif
781 EventLoop();
782 return -1;