Change to the linux kernel coding style
[wmaker-crm.git] / src / main.c
blob3f8e36c6905cf6f6f3aa0237c74a61b8888ce8f1
1 /*
2 * Window Maker window manager
3 *
4 * Copyright (c) 1997-2003 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.
10 *
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.
15 *
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.
20 */
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.
40 */
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 /* cursors */
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;
127 #ifdef SHAPE
128 Bool wShapeSupported;
129 int wShapeEventBase;
130 #endif
132 #ifdef KEEP_XKB_LOCK_STATUS
133 Bool wXkbSupported;
134 int wXkbEventBase;
135 #endif
137 /* special flags */
138 char WProgramSigState = 0;
139 char WProgramState = WSTATE_NORMAL;
140 char WDelayedActionSet = 0;
142 /* temporary stuff */
143 int wVisualID = -1;
145 /* notifications */
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;
167 static int ArgCount;
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)
179 #ifdef XSMP_ENABLED
180 wSessionDisconnectManager();
181 #endif
182 if (dpy)
183 XCloseDisplay(dpy);
185 exit(status);
188 void Restart(char *manager, Bool abortOnFailure)
190 char *prog = NULL;
191 char *argv[MAX_RESTART_ARGS];
192 int i;
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) {
199 break;
203 if (dpy) {
204 #ifdef XSMP_ENABLED
205 wSessionDisconnectManager();
206 #endif
207 XCloseDisplay(dpy);
208 dpy = NULL;
210 if (!prog) {
211 execvp(Arguments[0], Arguments);
212 wfatal(_("failed to restart Window Maker."));
213 } else {
214 execvp(prog, argv);
215 wsyserror(_("could not exec %s"), prog);
217 if (abortOnFailure)
218 exit(7);
221 void SetupEnvironment(WScreen * scr)
223 char *tmp, *ptr;
224 char buf[16];
226 if (multiHead) {
227 int len = strlen(DisplayName) + 64;
228 tmp = wmalloc(len);
229 snprintf(tmp, len, "DISPLAY=%s", XDisplayName(DisplayName));
230 ptr = strchr(strchr(tmp, ':'), '.');
231 if (ptr)
232 *ptr = 0;
233 snprintf(buf, sizeof(buf), ".%i", scr->screen);
234 strcat(tmp, buf);
235 putenv(tmp);
237 tmp = wmalloc(60);
238 snprintf(tmp, 60, "WRASTER_COLOR_RESOLUTION%i=%i", scr->screen,
239 scr->rcontext->attribs->colors_per_channel);
240 putenv(tmp);
243 typedef struct {
244 WScreen *scr;
245 char *command;
246 } _tuple;
248 static void shellCommandHandler(pid_t pid, unsigned char status, _tuple * data)
250 if (status == 127) {
251 char *buffer;
253 buffer = wstrconcat(_("Could not execute command: "), data->command);
255 wMessageDialog(data->scr, _("Error"), buffer, _("OK"), NULL, NULL);
256 wfree(buffer);
257 } else if (status != 127) {
258 /*
259 printf("%s: %i\n", data->command, status);
260 */
263 wfree(data->command);
264 wfree(data);
267 void ExecuteShellCommand(WScreen * scr, char *command)
269 static char *shell = NULL;
270 pid_t pid;
272 /*
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.
276 if (!shell) {
277 shell = getenv("SHELL");
278 if (!shell)
279 shell = "/bin/sh";
281 */
282 shell = "/bin/sh";
284 pid = fork();
286 if (pid == 0) {
288 SetupEnvironment(scr);
290 #ifdef HAVE_SETSID
291 setsid();
292 #endif
293 execl(shell, shell, "-c", command, NULL);
294 wsyserror("could not execute %s -c %s", shell, command);
295 Exit(-1);
296 } else if (pid < 0) {
297 wsyserror("cannot fork a new process");
298 } else {
299 _tuple *data = wmalloc(sizeof(_tuple));
301 data->scr = scr;
302 data->command = wstrdup(command);
304 wAddDeathHandler(pid, (WDeathHandler *) shellCommandHandler, data);
308 /*
309 *---------------------------------------------------------------------
310 * wAbort--
311 * Do a major cleanup and exit the program
313 *----------------------------------------------------------------------
314 */
315 void wAbort(Bool dumpCore)
317 int i;
318 WScreen *scr;
320 for (i = 0; i < wScreenCount; i++) {
321 scr = wScreenWithNumber(i);
322 if (scr)
323 RestoreDesktop(scr);
325 printf(_("%s aborted.\n"), ProgName);
326 if (dumpCore)
327 abort();
328 else
329 exit(1);
332 void print_help()
334 printf(_("Usage: %s [options]\n"), ProgName);
335 puts(_("The Window Maker window manager for the X window system"));
336 puts("");
337 puts(_(" -display host:dpy display to use"));
338 #ifdef USECPP
339 puts(_(" --no-cpp disable preprocessing of configuration files"));
340 #endif
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"));
351 #ifdef DEBUG
352 puts(_(" --synchronous turn on synchronous display mode"));
353 #endif
354 puts(_(" --version print version and exit"));
355 puts(_(" --help show this message"));
358 void check_defaults()
360 char *path;
362 path = wdefaultspathfordomain("WindowMaker");
364 if (access(path, R_OK) != 0) {
365 #if 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);
368 exit(1);
369 #else
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"));
375 } else {
376 wwarning(_("%s directory created with default configuration."), path);
378 #endif
381 wfree(path);
384 /*
385 * Add watch here, used to notify if configuration
386 * files have changed, using linux kernel inotify mechanism
387 */
389 static void inotifyWatchConfig()
391 char *watchPath = NULL;
392 inotifyFD = inotify_init(); /* Initialise an inotify instance */
393 if (inotifyFD < 0) {
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!"));
397 } else {
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.
402 */
403 inotifyWD = inotify_add_watch(inotifyFD, watchPath, IN_ALL_EVENTS);
404 if (inotifyWD < 0) {
405 wwarning(_("could not add an inotify watch on path\n."
406 "%s\n"
407 "Changes to the defaults database will require"
408 " a restart to take effect."), watchPath);
409 close(inotifyFD);
412 wfree(watchPath);
415 static void execInitScript()
417 char *file, *paths;
419 paths = wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
420 paths = wstrappend(paths, ":" DEF_CONFIG_PATHS);
422 file = wfindfile(paths, DEF_INIT_SCRIPT);
423 wfree(paths);
425 if (file) {
426 if (system(file) != 0) {
427 wsyserror(_("%s:could not execute initialization script"), file);
429 #if 0
430 if (fork() == 0) {
431 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
432 wsyserror(_("%s:could not execute initialization script"), file);
433 exit(1);
435 #endif
436 wfree(file);
440 void ExecExitScript()
442 char *file, *paths;
444 paths = wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
445 paths = wstrappend(paths, ":" DEF_CONFIG_PATHS);
447 file = wfindfile(paths, DEF_EXIT_SCRIPT);
448 wfree(paths);
450 if (file) {
451 if (system(file) != 0) {
452 wsyserror(_("%s:could not execute exit script"), file);
454 #if 0
455 if (fork() == 0) {
456 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
457 wsyserror(_("%s:could not execute exit script"), file);
458 exit(1);
460 #endif
461 wfree(file);
465 #if 0
466 char *getFullPath(char *path)
468 char buffer[1024];
469 char *tmp;
470 char *basep = (char *)buffer;
472 if (*path != '/' && getcwd(buffer, 1023)) {
474 for (;;) {
475 if (strncmp(path, "../", 3) == 0) {
476 path += 3;
477 basep = strchr(basep, '/');
478 if (!basep || *path == 0)
479 break;
482 if (*path == '/' || strncmp(path, "./", 2) == 0) {
483 tmp =}
485 /*
486 * path
487 * ./path
488 * ../path
489 * ../../path
490 */
492 } else {
493 return wstrconcat(path);
496 return tmp;
498 #endif
500 int main(int argc, char **argv)
502 int i_am_the_monitor, i, len;
503 char *str, *alt;
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");
512 if (alt != NULL)
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;
528 break;
529 } else if (strcmp(argv[i], "-display") == 0 || strcmp(argv[i], "--display") == 0) {
530 i++;
531 if (i >= argc) {
532 wwarning(_("too few arguments for %s"), argv[i - 1]);
533 exit(0);
535 DisplayName = argv[i];
539 DisplayName = XDisplayName(DisplayName);
540 len = strlen(DisplayName) + 64;
541 str = wmalloc(len);
542 snprintf(str, len, "DISPLAY=%s", DisplayName);
543 putenv(str);
545 if (i_am_the_monitor)
546 return MonitorLoop(argc, argv);
547 else
548 return real_main(argc, argv);
551 static int real_main(int argc, char **argv)
553 int i, restart = 0;
554 char *pos;
555 int d, s;
556 int flag;
557 #ifdef DEBUG
558 Bool doSync = False;
559 #endif
560 setlocale(LC_ALL, "");
562 wsetabort(wAbort);
564 /* for telling WPrefs what's the name of the wmaker binary being ran */
565 setenv("WMAKER_BIN_NAME", argv[0], 1);
567 flag = 0;
568 ArgCount = argc;
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], '/');
578 if (!ProgName)
579 ProgName = argv[0];
580 else
581 ProgName++;
583 restart = 0;
585 if (argc > 1) {
586 for (i = 1; i < argc; i++) {
587 #ifdef USECPP
588 if (strcmp(argv[i], "-nocpp") == 0 || strcmp(argv[i], "--no-cpp") == 0) {
589 wPreferences.flags.nocpp = 1;
590 } else
591 #endif
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);
609 exit(0);
610 } else if (strcmp(argv[i], "--global_defaults_path") == 0) {
611 printf("%s/WindowMaker\n", SYSCONFDIR);
612 exit(0);
613 #ifdef DEBUG
614 } else if (strcmp(argv[i], "--synchronous") == 0) {
615 doSync = 1;
616 #endif
617 } else if (strcmp(argv[i], "-locale") == 0 || strcmp(argv[i], "--locale") == 0) {
618 i++;
619 if (i >= argc) {
620 wwarning(_("too few arguments for %s"), argv[i - 1]);
621 exit(0);
623 Locale = argv[i];
624 } else if (strcmp(argv[i], "-display") == 0 || strcmp(argv[i], "--display") == 0) {
625 i++;
626 if (i >= argc) {
627 wwarning(_("too few arguments for %s"), argv[i - 1]);
628 exit(0);
630 DisplayName = argv[i];
631 } else if (strcmp(argv[i], "-visualid") == 0 || strcmp(argv[i], "--visual-id") == 0) {
632 i++;
633 if (i >= argc) {
634 wwarning(_("too few arguments for %s"), argv[i - 1]);
635 exit(0);
637 if (sscanf(argv[i], "%i", &wVisualID) != 1) {
638 wwarning(_("bad value for visualid: \"%s\""), argv[i]);
639 exit(0);
641 } else if (strcmp(argv[i], "-static") == 0 || strcmp(argv[i], "--static") == 0) {
643 wPreferences.flags.noupdates = 1;
644 #ifdef XSMP_ENABLED
645 } else if (strcmp(argv[i], "-clientid") == 0 || strcmp(argv[i], "-restore") == 0) {
646 i++;
647 if (i >= argc) {
648 wwarning(_("too few arguments for %s"), argv[i - 1]);
649 exit(0);
651 #endif
652 } else if (strcmp(argv[i], "--help") == 0) {
653 print_help();
654 exit(0);
655 } else {
656 printf(_("%s: invalid argument '%s'\n"), argv[0], argv[i]);
657 printf(_("Try '%s --help' for more information\n"), argv[0]);
658 exit(1);
663 if (!wPreferences.flags.noupdates) {
664 /* check existence of Defaults DB directory */
665 check_defaults();
668 if (Locale) {
669 setenv("LANG", Locale, 1);
670 } else {
671 Locale = getenv("LC_ALL");
672 if (!Locale) {
673 Locale = getenv("LANG");
677 setlocale(LC_ALL, "");
679 if (!Locale || strcmp(Locale, "C") == 0 || strcmp(Locale, "POSIX") == 0)
680 Locale = NULL;
681 #ifdef I18N
682 if (getenv("NLSPATH"))
683 bindtextdomain("WindowMaker", getenv("NLSPATH"));
684 else
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"));
696 #endif
698 if (Locale) {
699 char *ptr;
701 Locale = wstrdup(Locale);
702 ptr = strchr(Locale, '.');
703 if (ptr)
704 *ptr = 0;
707 /* open display */
708 dpy = XOpenDisplay(DisplayName);
709 if (dpy == NULL) {
710 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName));
711 exit(1);
714 if (fcntl(ConnectionNumber(dpy), F_SETFD, FD_CLOEXEC) < 0) {
715 wsyserror("error setting close-on-exec flag for X connection");
716 exit(1);
719 if (wVisualID < 0)
720 /*
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.
726 */
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 */
731 if (DisplayName)
732 pos = strchr(DisplayName, ':');
733 else
734 pos = NULL;
736 if (pos && sscanf(pos, ":%i.%i", &d, &s) == 2)
737 multiHead = False;
739 DisplayName = XDisplayName(DisplayName);
740 setenv("DISPLAY", DisplayName, 1);
742 #ifdef DEBUG
743 if (doSync)
744 XSynchronize(dpy, True);
745 #endif
747 wXModifierInitialize();
749 #ifdef XSMP_ENABLED
750 wSessionConnectManager(argv, argc);
751 #endif
753 StartUp(!multiHead);
755 if (wScreenCount == 1)
756 multiHead = False;
758 execInitScript();
759 inotifyWatchConfig();
760 EventLoop();
761 return -1;