Remove unused StartLogShell function.
[wmaker-crm.git] / src / main.c
blobd61fda2e027ccff2f508d7ddbcf969df3528a0ff
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>
38 /* Xlocale.h and locale.h are the same if X_LOCALE is undefind in wconfig.h,
39 * and if X_LOCALE is defined, X's locale emulating functions will be used.
40 * See Xlocale.h for more information.
42 #include <X11/Xlocale.h>
44 #define MAINFILE
46 #include "WindowMaker.h"
47 #include "window.h"
48 #include "funcs.h"
49 #include "menu.h"
50 #include "keybind.h"
51 #include "xmodifier.h"
52 #include "defaults.h"
53 #include "session.h"
54 #include "dialog.h"
56 #include <WINGs/WUtil.h>
58 /****** Global Variables ******/
60 /* general info */
62 Display *dpy;
64 char *ProgName;
66 unsigned int ValidModMask = 0xff;
68 int inotifyFD;
69 int inotifyWD;
70 /* locale to use. NULL==POSIX or C */
71 char *Locale=NULL;
73 int wScreenCount=0;
75 WPreferences wPreferences;
78 WMPropList *wDomainName;
79 WMPropList *wAttributeDomainName;
81 WShortKey wKeyBindings[WKBD_LAST];
83 /* defaults domains */
84 WDDomain *WDWindowMaker = NULL;
85 WDDomain *WDWindowAttributes = NULL;
86 WDDomain *WDRootMenu = NULL;
88 /* XContexts */
89 XContext wWinContext;
90 XContext wAppWinContext;
91 XContext wStackContext;
92 XContext wVEdgeContext;
94 /* Atoms */
95 Atom _XA_WM_STATE;
96 Atom _XA_WM_CHANGE_STATE;
97 Atom _XA_WM_PROTOCOLS;
98 Atom _XA_WM_TAKE_FOCUS;
99 Atom _XA_WM_DELETE_WINDOW;
100 Atom _XA_WM_SAVE_YOURSELF;
101 Atom _XA_WM_CLIENT_LEADER;
102 Atom _XA_WM_COLORMAP_WINDOWS;
103 Atom _XA_WM_COLORMAP_NOTIFY;
105 Atom _XA_GNUSTEP_WM_ATTR;
106 Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
107 Atom _XA_GNUSTEP_WM_RESIZEBAR;
108 Atom _XA_GNUSTEP_TITLEBAR_STATE;
110 Atom _XA_WINDOWMAKER_MENU;
111 Atom _XA_WINDOWMAKER_WM_PROTOCOLS;
112 Atom _XA_WINDOWMAKER_STATE;
114 Atom _XA_WINDOWMAKER_WM_FUNCTION;
115 Atom _XA_WINDOWMAKER_NOTICEBOARD;
116 Atom _XA_WINDOWMAKER_COMMAND;
118 Atom _XA_WINDOWMAKER_ICON_SIZE;
119 Atom _XA_WINDOWMAKER_ICON_TILE;
122 /* cursors */
123 Cursor wCursor[WCUR_LAST];
125 /* last event timestamp for XSetInputFocus */
126 Time LastTimestamp = CurrentTime;
127 /* timestamp on the last time we did XSetInputFocus() */
128 Time LastFocusChange = CurrentTime;
130 #ifdef SHAPE
131 Bool wShapeSupported;
132 int wShapeEventBase;
133 #endif
135 #ifdef KEEP_XKB_LOCK_STATUS
136 Bool wXkbSupported;
137 int wXkbEventBase;
138 #endif
140 /* special flags */
141 char WProgramSigState = 0;
142 char WProgramState = WSTATE_NORMAL;
143 char WDelayedActionSet = 0;
146 /* temporary stuff */
147 int wVisualID = -1;
150 /* notifications */
151 const char *WMNManaged = "WMNManaged";
152 const char *WMNUnmanaged = "WMNUnmanaged";
153 const char *WMNChangedWorkspace = "WMNChangedWorkspace";
154 const char *WMNChangedState = "WMNChangedState";
155 const char *WMNChangedFocus = "WMNChangedFocus";
156 const char *WMNChangedStacking = "WMNChangedStacking";
157 const char *WMNChangedName = "WMNChangedName";
159 const char *WMNWorkspaceCreated = "WMNWorkspaceCreated";
160 const char *WMNWorkspaceDestroyed = "WMNWorkspaceDestroyed";
161 const char *WMNWorkspaceChanged = "WMNWorkspaceChanged";
162 const char *WMNWorkspaceNameChanged = "WMNWorkspaceNameChanged";
164 const char *WMNResetStacking = "WMNResetStacking";
166 /******** End Global Variables *****/
168 static char *DisplayName = NULL;
170 static char **Arguments;
172 static int ArgCount;
174 extern void EventLoop();
175 extern void StartUp();
176 extern int MonitorLoop(int argc, char **argv);
178 static Bool multiHead = True;
180 /* stdi/o for log shell */
181 static int LogStdIn = -1, LogStdOut = -1, LogStdErr = -1;
184 static int real_main(int argc, char **argv);
186 void
187 Exit(int status)
189 #ifdef XSMP_ENABLED
190 wSessionDisconnectManager();
191 #endif
192 if (dpy)
193 XCloseDisplay(dpy);
195 exit(status);
199 void
200 Restart(char *manager, Bool abortOnFailure)
202 char *prog=NULL;
203 char *argv[MAX_RESTART_ARGS];
204 int i;
206 if (manager && manager[0]!=0) {
207 prog = argv[0] = strtok(manager, " ");
208 for (i=1; i<MAX_RESTART_ARGS; i++) {
209 argv[i]=strtok(NULL, " ");
210 if (argv[i]==NULL) {
211 break;
215 if (dpy) {
216 #ifdef XSMP_ENABLED
217 wSessionDisconnectManager();
218 #endif
219 XCloseDisplay(dpy);
220 dpy = NULL;
222 if (!prog) {
223 execvp(Arguments[0], Arguments);
224 wfatal(_("failed to restart Window Maker."));
225 } else {
226 execvp(prog, argv);
227 wsyserror(_("could not exec %s"), prog);
229 if (abortOnFailure)
230 exit(7);
235 void
236 SetupEnvironment(WScreen *scr)
238 char *tmp, *ptr;
239 char buf[16];
241 if (multiHead) {
242 int len = strlen(DisplayName)+64;
243 tmp = wmalloc(len);
244 snprintf(tmp, len, "DISPLAY=%s", XDisplayName(DisplayName));
245 ptr = strchr(strchr(tmp, ':'), '.');
246 if (ptr)
247 *ptr = 0;
248 snprintf(buf, sizeof(buf), ".%i", scr->screen);
249 strcat(tmp, buf);
250 putenv(tmp);
252 tmp = wmalloc(60);
253 snprintf(tmp, 60, "WRASTER_COLOR_RESOLUTION%i=%i", scr->screen,
254 scr->rcontext->attribs->colors_per_channel);
255 putenv(tmp);
261 typedef struct {
262 WScreen *scr;
263 char *command;
264 } _tuple;
267 static void
268 shellCommandHandler(pid_t pid, unsigned char status, _tuple *data)
270 if (status == 127) {
271 char *buffer;
273 buffer = wstrconcat(_("Could not execute command: "), data->command);
275 wMessageDialog(data->scr, _("Error"), buffer, _("OK"), NULL, NULL);
276 wfree(buffer);
277 } else if (status != 127) {
279 printf("%s: %i\n", data->command, status);
283 wfree(data->command);
284 wfree(data);
288 void
289 ExecuteShellCommand(WScreen *scr, char *command)
291 static char *shell = NULL;
292 pid_t pid;
295 * This have a problem: if the shell is tcsh (not sure about others)
296 * and ~/.tcshrc have /bin/stty erase ^H somewhere on it, the shell
297 * will block and the command will not be executed.
298 if (!shell) {
299 shell = getenv("SHELL");
300 if (!shell)
301 shell = "/bin/sh";
304 shell = "/bin/sh";
306 pid = fork();
308 if (pid==0) {
310 SetupEnvironment(scr);
312 #ifdef HAVE_SETSID
313 setsid();
314 #endif
315 execl(shell, shell, "-c", command, NULL);
316 wsyserror("could not execute %s -c %s", shell, command);
317 Exit(-1);
318 } else if (pid < 0) {
319 wsyserror("cannot fork a new process");
320 } else {
321 _tuple *data = wmalloc(sizeof(_tuple));
323 data->scr = scr;
324 data->command = wstrdup(command);
326 wAddDeathHandler(pid, (WDeathHandler*)shellCommandHandler, data);
332 *---------------------------------------------------------------------
333 * wAbort--
334 * Do a major cleanup and exit the program
336 *----------------------------------------------------------------------
338 void
339 wAbort(Bool dumpCore)
341 int i;
342 WScreen *scr;
344 for (i=0; i<wScreenCount; i++) {
345 scr = wScreenWithNumber(i);
346 if (scr)
347 RestoreDesktop(scr);
349 printf(_("%s aborted.\n"), ProgName);
350 if (dumpCore)
351 abort();
352 else
353 exit(1);
357 void
358 print_help()
360 printf(_("Usage: %s [options]\n"), ProgName);
361 puts(_("The Window Maker window manager for the X window system"));
362 puts("");
363 puts(_(" -display host:dpy display to use"));
364 #ifdef USECPP
365 puts(_(" --no-cpp disable preprocessing of configuration files"));
366 #endif
367 puts(_(" --no-dock do not open the application Dock"));
368 puts(_(" --no-clip do not open the workspace Clip"));
369 puts(_(" --no-autolaunch do not autolaunch applications"));
370 puts(_(" --dont-restore do not restore saved session"));
372 puts(_(" --locale locale locale to use"));
374 puts(_(" --create-stdcmap create the standard colormap hint in PseudoColor visuals"));
375 puts(_(" --visual-id visualid visual id of visual to use"));
376 puts(_(" --static do not update or save configurations"));
377 #ifdef DEBUG
378 puts(_(" --synchronous turn on synchronous display mode"));
379 #endif
380 puts(_(" --version print version and exit"));
381 puts(_(" --help show this message"));
386 void
387 check_defaults()
389 char *path;
391 path = wdefaultspathfordomain("WindowMaker");
393 if (access(path, R_OK)!=0) {
394 #if 0
395 wfatal(_("could not find user GNUstep directory (%s).\n"
396 "Make sure you have installed Window Maker correctly and run wmaker.inst"),
397 path);
398 exit(1);
399 #else
400 wwarning(_("could not find user GNUstep directory (%s)."), path);
402 if (system("wmaker.inst --batch") != 0) {
403 wwarning(_("There was an error while creating GNUstep directory, please "
404 "make sure you have installed Window Maker correctly and run wmaker.inst"));
405 } else {
406 wwarning(_("%s directory created with default configuration."), path);
408 #endif
411 wfree(path);
416 * Add watch here, used to notify if configuration
417 * files have changed, using linux kernel inotify mechanism
420 static void
421 inotifyWatchConfig()
423 char *watchPath = NULL;
424 inotifyFD = inotify_init(); /* Initialise an inotify instance */
425 if (inotifyFD < 0) {
426 wwarning(_("could not initialise an inotify instance."
427 " Changes to the defaults database will require"
428 " a restart to take effect. Check your kernel!"));
429 } else {
430 watchPath = wstrconcat(wusergnusteppath(), "/Defaults");
431 /* Add the watch; really we are only looking for modify events
432 * but we might want more in the future so check all events for now.
433 * The individual events are checked for in event.c.
435 inotifyWD = inotify_add_watch (inotifyFD, watchPath, IN_ALL_EVENTS);
436 if (inotifyWD < 0) {
437 wwarning(_("could not add an inotify watch on path\n."
438 "%s\n"
439 "Changes to the defaults database will require"
440 " a restart to take effect."),watchPath);
441 close (inotifyFD);
444 wfree(watchPath);
447 static void
448 execInitScript()
450 char *file, *paths;
452 paths = wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
453 paths = wstrappend(paths, ":"DEF_CONFIG_PATHS);
455 file = wfindfile(paths, DEF_INIT_SCRIPT);
456 wfree(paths);
458 if (file) {
459 if (system(file) != 0) {
460 wsyserror(_("%s:could not execute initialization script"), file);
462 #if 0
463 if (fork()==0) {
464 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
465 wsyserror(_("%s:could not execute initialization script"), file);
466 exit(1);
468 #endif
469 wfree(file);
474 void
475 ExecExitScript()
477 char *file, *paths;
479 paths = wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
480 paths = wstrappend(paths, ":"DEF_CONFIG_PATHS);
482 file = wfindfile(paths, DEF_EXIT_SCRIPT);
483 wfree(paths);
485 if (file) {
486 if (system(file) != 0) {
487 wsyserror(_("%s:could not execute exit script"), file);
489 #if 0
490 if (fork()==0) {
491 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
492 wsyserror(_("%s:could not execute exit script"), file);
493 exit(1);
495 #endif
496 wfree(file);
500 #if 0
501 char*
502 getFullPath(char *path)
504 char buffer[1024];
505 char *tmp;
506 char *basep = (char*)buffer;
508 if (*path != '/' && getcwd(buffer, 1023)) {
510 for (;;) {
511 if (strncmp(path, "../", 3)==0) {
512 path += 3;
513 basep = strchr(basep, '/');
514 if (!basep || *path==0)
515 break;
518 if (*path == '/' || strncmp(path, "./",2)==0) {
519 tmp =
523 * path
524 * ./path
525 * ../path
526 * ../../path
530 } else {
531 return wstrconcat(path);
534 return tmp;
536 #endif
540 main(int argc, char **argv)
542 int i_am_the_monitor, i, len;
543 char *str, *alt;
545 /* setup common stuff for the monitor and wmaker itself */
546 WMInitializeApplication("WindowMaker", &argc, argv);
548 memset(&wPreferences, 0, sizeof(WPreferences));
550 wPreferences.fallbackWMs = WMCreateArray(8);
551 alt = getenv("WINDOWMAKER_ALT_WM");
552 if (alt != NULL)
553 WMAddToArray(wPreferences.fallbackWMs, wstrdup(alt));
555 WMAddToArray(wPreferences.fallbackWMs, wstrdup("blackbox"));
556 WMAddToArray(wPreferences.fallbackWMs, wstrdup("metacity"));
557 WMAddToArray(wPreferences.fallbackWMs, wstrdup("fvwm"));
558 WMAddToArray(wPreferences.fallbackWMs, wstrdup("twm"));
559 WMAddToArray(wPreferences.fallbackWMs, NULL);
560 WMAddToArray(wPreferences.fallbackWMs, wstrdup("rxvt"));
561 WMAddToArray(wPreferences.fallbackWMs, wstrdup("xterm"));
563 i_am_the_monitor= 1;
565 for (i= 1; i < argc; i++)
567 if (strncmp(argv[i], "--for-real", strlen("--for-real"))==0)
569 i_am_the_monitor= 0;
570 break;
572 else if (strcmp(argv[i], "-display")==0 || strcmp(argv[i], "--display")==0)
574 i++;
575 if (i>=argc) {
576 wwarning(_("too few arguments for %s"), argv[i-1]);
577 exit(0);
579 DisplayName = argv[i];
583 DisplayName = XDisplayName(DisplayName);
584 len = strlen(DisplayName)+64;
585 str = wmalloc(len);
586 snprintf(str, len, "DISPLAY=%s", DisplayName);
587 putenv(str);
589 if (i_am_the_monitor)
590 return MonitorLoop(argc, argv);
591 else
592 return real_main(argc, argv);
596 static int
597 real_main(int argc, char **argv)
599 int i, restart=0;
600 char *str;
601 int d, s;
602 int flag;
603 #ifdef DEBUG
604 Bool doSync = False;
605 #endif
606 setlocale(LC_ALL, "");
608 wsetabort(wAbort);
610 /* for telling WPrefs what's the name of the wmaker binary being ran */
612 str = wstrconcat("WMAKER_BIN_NAME=", argv[0]);
613 putenv(str);
615 flag= 0;
616 ArgCount = argc;
617 Arguments = wmalloc(sizeof(char*)*(ArgCount+1));
618 for (i= 0; i < argc; i++)
620 Arguments[i]= argv[i];
622 /* add the extra option to signal that we're just restarting wmaker */
623 Arguments[argc-1]= "--for-real=";
624 Arguments[argc]= NULL;
626 ProgName = strrchr(argv[0],'/');
627 if (!ProgName)
628 ProgName = argv[0];
629 else
630 ProgName++;
633 restart = 0;
635 if (argc>1) {
636 for (i=1; i<argc; i++) {
637 #ifdef USECPP
638 if (strcmp(argv[i], "-nocpp")==0
639 || strcmp(argv[i], "--no-cpp")==0) {
640 wPreferences.flags.nocpp=1;
641 } else
642 #endif
643 if (strcmp(argv[i], "--for-real")==0) {
644 wPreferences.flags.restarting = 0;
645 } else if (strcmp(argv[i], "--for-real=")==0) {
646 wPreferences.flags.restarting = 1;
647 } else if (strcmp(argv[i], "--for-real-")==0) {
648 wPreferences.flags.restarting = 2;
649 } else if (strcmp(argv[i], "-no-autolaunch")==0
650 || strcmp(argv[i], "--no-autolaunch")==0) {
651 wPreferences.flags.noautolaunch = 1;
652 } else if (strcmp(argv[i], "-dont-restore")==0
653 || strcmp(argv[i], "--dont-restore")==0) {
654 wPreferences.flags.norestore = 1;
655 } else if (strcmp(argv[i], "-nodock")==0
656 || strcmp(argv[i], "--no-dock")==0) {
657 wPreferences.flags.nodock=1;
658 } else if (strcmp(argv[i], "-noclip")==0
659 || strcmp(argv[i], "--no-clip")==0) {
660 wPreferences.flags.noclip=1;
661 } else if (strcmp(argv[i], "-version")==0
662 || strcmp(argv[i], "--version")==0) {
663 printf("Window Maker %s\n", VERSION);
664 exit(0);
665 } else if (strcmp(argv[i], "--global_defaults_path")==0) {
666 printf("%s/WindowMaker\n", SYSCONFDIR);
667 exit(0);
668 #ifdef DEBUG
669 } else if (strcmp(argv[i], "--synchronous")==0) {
670 doSync = 1;
671 #endif
672 } else if (strcmp(argv[i], "-locale")==0
673 || strcmp(argv[i], "--locale")==0) {
674 i++;
675 if (i>=argc) {
676 wwarning(_("too few arguments for %s"), argv[i-1]);
677 exit(0);
679 Locale = argv[i];
680 } else if (strcmp(argv[i], "-display")==0
681 || strcmp(argv[i], "--display")==0) {
682 i++;
683 if (i>=argc) {
684 wwarning(_("too few arguments for %s"), argv[i-1]);
685 exit(0);
687 DisplayName = argv[i];
688 } else if (strcmp(argv[i], "-visualid")==0
689 || strcmp(argv[i], "--visual-id")==0) {
690 i++;
691 if (i>=argc) {
692 wwarning(_("too few arguments for %s"), argv[i-1]);
693 exit(0);
695 if (sscanf(argv[i], "%i", &wVisualID)!=1) {
696 wwarning(_("bad value for visualid: \"%s\""), argv[i]);
697 exit(0);
699 } else if (strcmp(argv[i], "-static")==0
700 || strcmp(argv[i], "--static")==0) {
702 wPreferences.flags.noupdates = 1;
703 #ifdef XSMP_ENABLED
704 } else if (strcmp(argv[i], "-clientid")==0
705 || strcmp(argv[i], "-restore")==0) {
706 i++;
707 if (i>=argc) {
708 wwarning(_("too few arguments for %s"), argv[i-1]);
709 exit(0);
711 #endif
712 } else if (strcmp(argv[i], "--help")==0) {
713 print_help();
714 exit(0);
715 } else {
716 printf(_("%s: invalid argument '%s'\n"), argv[0], argv[i]);
717 printf(_("Try '%s --help' for more information\n"), argv[0]);
718 exit(1);
723 if (!wPreferences.flags.noupdates) {
724 /* check existence of Defaults DB directory */
725 check_defaults();
729 if (Locale) {
730 /* return of wstrconcat should not be free-ed! read putenv man page */
731 putenv(wstrconcat("LANG=", Locale));
732 } else {
733 Locale = getenv("LC_ALL");
734 if (!Locale) {
735 Locale = getenv("LANG");
739 setlocale(LC_ALL, "");
741 if (!Locale || strcmp(Locale, "C")==0 || strcmp(Locale, "POSIX")==0)
742 Locale = NULL;
743 #ifdef I18N
744 if (getenv("NLSPATH"))
745 bindtextdomain("WindowMaker", getenv("NLSPATH"));
746 else
747 bindtextdomain("WindowMaker", LOCALEDIR);
748 bind_textdomain_codeset("WindowMaker", "UTF-8");
749 textdomain("WindowMaker");
751 if (!XSupportsLocale()) {
752 wwarning(_("X server does not support locale"));
755 if (XSetLocaleModifiers("") == NULL) {
756 wwarning(_("cannot set locale modifiers"));
758 #endif
760 if (Locale) {
761 char *ptr;
763 Locale = wstrdup(Locale);
764 ptr = strchr(Locale, '.');
765 if (ptr)
766 *ptr = 0;
769 /* open display */
770 dpy = XOpenDisplay(DisplayName);
771 if (dpy == NULL) {
772 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName));
773 exit(1);
776 if (fcntl(ConnectionNumber(dpy), F_SETFD, FD_CLOEXEC) < 0) {
777 wsyserror("error setting close-on-exec flag for X connection");
778 exit(1);
781 if (wVisualID < 0)
783 * If unspecified, use default visual instead of waiting
784 * for wrlib/context.c:bestContext() that may end up choosing
785 * the "fake" 24 bits added by the Composite extension.
786 * This is required to avoid all sort of corruptions when
787 * composite is enabled, and at a depth other than 24.
789 wVisualID = (int)DefaultVisual(dpy, DefaultScreen(dpy))->visualid;
791 /* check if the user specified a complete display name (with screen).
792 * If so, only manage the specified screen */
793 if (DisplayName)
794 str = strchr(DisplayName, ':');
795 else
796 str = NULL;
798 if (str && sscanf(str, ":%i.%i", &d, &s)==2)
799 multiHead = False;
801 DisplayName = XDisplayName(DisplayName);
803 int len = strlen(DisplayName)+64;
804 str = wmalloc(len);
805 snprintf(str, len, "DISPLAY=%s", DisplayName);
807 putenv(str);
809 #ifdef DEBUG
810 if (doSync)
811 XSynchronize(dpy, True);
812 #endif
814 wXModifierInitialize();
816 #ifdef XSMP_ENABLED
817 wSessionConnectManager(argv, argc);
818 #endif
820 StartUp(!multiHead);
822 if (wScreenCount==1)
823 multiHead = False;
825 execInitScript();
826 inotifyWatchConfig();
827 EventLoop();
828 return -1;