real_main: Use setenv() instead of putenv() in remaining places
[wmaker-crm.git] / src / main.c
blobd0e7598bcdfb0f4f4f79b8c27d571d1c0d95e3d1
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 static int real_main(int argc, char **argv);
182 void
183 Exit(int status)
185 #ifdef XSMP_ENABLED
186 wSessionDisconnectManager();
187 #endif
188 if (dpy)
189 XCloseDisplay(dpy);
191 exit(status);
195 void
196 Restart(char *manager, Bool abortOnFailure)
198 char *prog=NULL;
199 char *argv[MAX_RESTART_ARGS];
200 int i;
202 if (manager && manager[0]!=0) {
203 prog = argv[0] = strtok(manager, " ");
204 for (i=1; i<MAX_RESTART_ARGS; i++) {
205 argv[i]=strtok(NULL, " ");
206 if (argv[i]==NULL) {
207 break;
211 if (dpy) {
212 #ifdef XSMP_ENABLED
213 wSessionDisconnectManager();
214 #endif
215 XCloseDisplay(dpy);
216 dpy = NULL;
218 if (!prog) {
219 execvp(Arguments[0], Arguments);
220 wfatal(_("failed to restart Window Maker."));
221 } else {
222 execvp(prog, argv);
223 wsyserror(_("could not exec %s"), prog);
225 if (abortOnFailure)
226 exit(7);
231 void
232 SetupEnvironment(WScreen *scr)
234 char *tmp, *ptr;
235 char buf[16];
237 if (multiHead) {
238 int len = strlen(DisplayName)+64;
239 tmp = wmalloc(len);
240 snprintf(tmp, len, "DISPLAY=%s", XDisplayName(DisplayName));
241 ptr = strchr(strchr(tmp, ':'), '.');
242 if (ptr)
243 *ptr = 0;
244 snprintf(buf, sizeof(buf), ".%i", scr->screen);
245 strcat(tmp, buf);
246 putenv(tmp);
248 tmp = wmalloc(60);
249 snprintf(tmp, 60, "WRASTER_COLOR_RESOLUTION%i=%i", scr->screen,
250 scr->rcontext->attribs->colors_per_channel);
251 putenv(tmp);
257 typedef struct {
258 WScreen *scr;
259 char *command;
260 } _tuple;
263 static void
264 shellCommandHandler(pid_t pid, unsigned char status, _tuple *data)
266 if (status == 127) {
267 char *buffer;
269 buffer = wstrconcat(_("Could not execute command: "), data->command);
271 wMessageDialog(data->scr, _("Error"), buffer, _("OK"), NULL, NULL);
272 wfree(buffer);
273 } else if (status != 127) {
275 printf("%s: %i\n", data->command, status);
279 wfree(data->command);
280 wfree(data);
284 void
285 ExecuteShellCommand(WScreen *scr, char *command)
287 static char *shell = NULL;
288 pid_t pid;
291 * This have a problem: if the shell is tcsh (not sure about others)
292 * and ~/.tcshrc have /bin/stty erase ^H somewhere on it, the shell
293 * will block and the command will not be executed.
294 if (!shell) {
295 shell = getenv("SHELL");
296 if (!shell)
297 shell = "/bin/sh";
300 shell = "/bin/sh";
302 pid = fork();
304 if (pid==0) {
306 SetupEnvironment(scr);
308 #ifdef HAVE_SETSID
309 setsid();
310 #endif
311 execl(shell, shell, "-c", command, NULL);
312 wsyserror("could not execute %s -c %s", shell, command);
313 Exit(-1);
314 } else if (pid < 0) {
315 wsyserror("cannot fork a new process");
316 } else {
317 _tuple *data = wmalloc(sizeof(_tuple));
319 data->scr = scr;
320 data->command = wstrdup(command);
322 wAddDeathHandler(pid, (WDeathHandler*)shellCommandHandler, data);
328 *---------------------------------------------------------------------
329 * wAbort--
330 * Do a major cleanup and exit the program
332 *----------------------------------------------------------------------
334 void
335 wAbort(Bool dumpCore)
337 int i;
338 WScreen *scr;
340 for (i=0; i<wScreenCount; i++) {
341 scr = wScreenWithNumber(i);
342 if (scr)
343 RestoreDesktop(scr);
345 printf(_("%s aborted.\n"), ProgName);
346 if (dumpCore)
347 abort();
348 else
349 exit(1);
353 void
354 print_help()
356 printf(_("Usage: %s [options]\n"), ProgName);
357 puts(_("The Window Maker window manager for the X window system"));
358 puts("");
359 puts(_(" -display host:dpy display to use"));
360 #ifdef USECPP
361 puts(_(" --no-cpp disable preprocessing of configuration files"));
362 #endif
363 puts(_(" --no-dock do not open the application Dock"));
364 puts(_(" --no-clip do not open the workspace Clip"));
365 puts(_(" --no-autolaunch do not autolaunch applications"));
366 puts(_(" --dont-restore do not restore saved session"));
368 puts(_(" --locale locale locale to use"));
370 puts(_(" --create-stdcmap create the standard colormap hint in PseudoColor visuals"));
371 puts(_(" --visual-id visualid visual id of visual to use"));
372 puts(_(" --static do not update or save configurations"));
373 #ifdef DEBUG
374 puts(_(" --synchronous turn on synchronous display mode"));
375 #endif
376 puts(_(" --version print version and exit"));
377 puts(_(" --help show this message"));
382 void
383 check_defaults()
385 char *path;
387 path = wdefaultspathfordomain("WindowMaker");
389 if (access(path, R_OK)!=0) {
390 #if 0
391 wfatal(_("could not find user GNUstep directory (%s).\n"
392 "Make sure you have installed Window Maker correctly and run wmaker.inst"),
393 path);
394 exit(1);
395 #else
396 wwarning(_("could not find user GNUstep directory (%s)."), path);
398 if (system("wmaker.inst --batch") != 0) {
399 wwarning(_("There was an error while creating GNUstep directory, please "
400 "make sure you have installed Window Maker correctly and run wmaker.inst"));
401 } else {
402 wwarning(_("%s directory created with default configuration."), path);
404 #endif
407 wfree(path);
412 * Add watch here, used to notify if configuration
413 * files have changed, using linux kernel inotify mechanism
416 static void
417 inotifyWatchConfig()
419 char *watchPath = NULL;
420 inotifyFD = inotify_init(); /* Initialise an inotify instance */
421 if (inotifyFD < 0) {
422 wwarning(_("could not initialise an inotify instance."
423 " Changes to the defaults database will require"
424 " a restart to take effect. Check your kernel!"));
425 } else {
426 watchPath = wstrconcat(wusergnusteppath(), "/Defaults");
427 /* Add the watch; really we are only looking for modify events
428 * but we might want more in the future so check all events for now.
429 * The individual events are checked for in event.c.
431 inotifyWD = inotify_add_watch (inotifyFD, watchPath, IN_ALL_EVENTS);
432 if (inotifyWD < 0) {
433 wwarning(_("could not add an inotify watch on path\n."
434 "%s\n"
435 "Changes to the defaults database will require"
436 " a restart to take effect."),watchPath);
437 close (inotifyFD);
440 wfree(watchPath);
443 static void
444 execInitScript()
446 char *file, *paths;
448 paths = wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
449 paths = wstrappend(paths, ":"DEF_CONFIG_PATHS);
451 file = wfindfile(paths, DEF_INIT_SCRIPT);
452 wfree(paths);
454 if (file) {
455 if (system(file) != 0) {
456 wsyserror(_("%s:could not execute initialization script"), file);
458 #if 0
459 if (fork()==0) {
460 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
461 wsyserror(_("%s:could not execute initialization script"), file);
462 exit(1);
464 #endif
465 wfree(file);
470 void
471 ExecExitScript()
473 char *file, *paths;
475 paths = wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
476 paths = wstrappend(paths, ":"DEF_CONFIG_PATHS);
478 file = wfindfile(paths, DEF_EXIT_SCRIPT);
479 wfree(paths);
481 if (file) {
482 if (system(file) != 0) {
483 wsyserror(_("%s:could not execute exit script"), file);
485 #if 0
486 if (fork()==0) {
487 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
488 wsyserror(_("%s:could not execute exit script"), file);
489 exit(1);
491 #endif
492 wfree(file);
496 #if 0
497 char*
498 getFullPath(char *path)
500 char buffer[1024];
501 char *tmp;
502 char *basep = (char*)buffer;
504 if (*path != '/' && getcwd(buffer, 1023)) {
506 for (;;) {
507 if (strncmp(path, "../", 3)==0) {
508 path += 3;
509 basep = strchr(basep, '/');
510 if (!basep || *path==0)
511 break;
514 if (*path == '/' || strncmp(path, "./",2)==0) {
515 tmp =
519 * path
520 * ./path
521 * ../path
522 * ../../path
526 } else {
527 return wstrconcat(path);
530 return tmp;
532 #endif
536 main(int argc, char **argv)
538 int i_am_the_monitor, i, len;
539 char *str, *alt;
541 /* setup common stuff for the monitor and wmaker itself */
542 WMInitializeApplication("WindowMaker", &argc, argv);
544 memset(&wPreferences, 0, sizeof(WPreferences));
546 wPreferences.fallbackWMs = WMCreateArray(8);
547 alt = getenv("WINDOWMAKER_ALT_WM");
548 if (alt != NULL)
549 WMAddToArray(wPreferences.fallbackWMs, wstrdup(alt));
551 WMAddToArray(wPreferences.fallbackWMs, wstrdup("blackbox"));
552 WMAddToArray(wPreferences.fallbackWMs, wstrdup("metacity"));
553 WMAddToArray(wPreferences.fallbackWMs, wstrdup("fvwm"));
554 WMAddToArray(wPreferences.fallbackWMs, wstrdup("twm"));
555 WMAddToArray(wPreferences.fallbackWMs, NULL);
556 WMAddToArray(wPreferences.fallbackWMs, wstrdup("rxvt"));
557 WMAddToArray(wPreferences.fallbackWMs, wstrdup("xterm"));
559 i_am_the_monitor= 1;
561 for (i= 1; i < argc; i++)
563 if (strncmp(argv[i], "--for-real", strlen("--for-real"))==0)
565 i_am_the_monitor= 0;
566 break;
568 else if (strcmp(argv[i], "-display")==0 || strcmp(argv[i], "--display")==0)
570 i++;
571 if (i>=argc) {
572 wwarning(_("too few arguments for %s"), argv[i-1]);
573 exit(0);
575 DisplayName = argv[i];
579 DisplayName = XDisplayName(DisplayName);
580 len = strlen(DisplayName)+64;
581 str = wmalloc(len);
582 snprintf(str, len, "DISPLAY=%s", DisplayName);
583 putenv(str);
585 if (i_am_the_monitor)
586 return MonitorLoop(argc, argv);
587 else
588 return real_main(argc, argv);
592 static int
593 real_main(int argc, char **argv)
595 int i, restart=0;
596 char *pos;
597 int d, s;
598 int flag;
599 #ifdef DEBUG
600 Bool doSync = False;
601 #endif
602 setlocale(LC_ALL, "");
604 wsetabort(wAbort);
606 /* for telling WPrefs what's the name of the wmaker binary being ran */
607 setenv("WMAKER_BIN_NAME", argv[0], 1);
609 flag= 0;
610 ArgCount = argc;
611 Arguments = wmalloc(sizeof(char*)*(ArgCount+1));
612 for (i= 0; i < argc; i++)
614 Arguments[i]= argv[i];
616 /* add the extra option to signal that we're just restarting wmaker */
617 Arguments[argc-1]= "--for-real=";
618 Arguments[argc]= NULL;
620 ProgName = strrchr(argv[0],'/');
621 if (!ProgName)
622 ProgName = argv[0];
623 else
624 ProgName++;
627 restart = 0;
629 if (argc>1) {
630 for (i=1; i<argc; i++) {
631 #ifdef USECPP
632 if (strcmp(argv[i], "-nocpp")==0
633 || strcmp(argv[i], "--no-cpp")==0) {
634 wPreferences.flags.nocpp=1;
635 } else
636 #endif
637 if (strcmp(argv[i], "--for-real")==0) {
638 wPreferences.flags.restarting = 0;
639 } else if (strcmp(argv[i], "--for-real=")==0) {
640 wPreferences.flags.restarting = 1;
641 } else if (strcmp(argv[i], "--for-real-")==0) {
642 wPreferences.flags.restarting = 2;
643 } else if (strcmp(argv[i], "-no-autolaunch")==0
644 || strcmp(argv[i], "--no-autolaunch")==0) {
645 wPreferences.flags.noautolaunch = 1;
646 } else if (strcmp(argv[i], "-dont-restore")==0
647 || strcmp(argv[i], "--dont-restore")==0) {
648 wPreferences.flags.norestore = 1;
649 } else if (strcmp(argv[i], "-nodock")==0
650 || strcmp(argv[i], "--no-dock")==0) {
651 wPreferences.flags.nodock=1;
652 } else if (strcmp(argv[i], "-noclip")==0
653 || strcmp(argv[i], "--no-clip")==0) {
654 wPreferences.flags.noclip=1;
655 } else if (strcmp(argv[i], "-version")==0
656 || strcmp(argv[i], "--version")==0) {
657 printf("Window Maker %s\n", VERSION);
658 exit(0);
659 } else if (strcmp(argv[i], "--global_defaults_path")==0) {
660 printf("%s/WindowMaker\n", SYSCONFDIR);
661 exit(0);
662 #ifdef DEBUG
663 } else if (strcmp(argv[i], "--synchronous")==0) {
664 doSync = 1;
665 #endif
666 } else if (strcmp(argv[i], "-locale")==0
667 || strcmp(argv[i], "--locale")==0) {
668 i++;
669 if (i>=argc) {
670 wwarning(_("too few arguments for %s"), argv[i-1]);
671 exit(0);
673 Locale = argv[i];
674 } else if (strcmp(argv[i], "-display")==0
675 || strcmp(argv[i], "--display")==0) {
676 i++;
677 if (i>=argc) {
678 wwarning(_("too few arguments for %s"), argv[i-1]);
679 exit(0);
681 DisplayName = argv[i];
682 } else if (strcmp(argv[i], "-visualid")==0
683 || strcmp(argv[i], "--visual-id")==0) {
684 i++;
685 if (i>=argc) {
686 wwarning(_("too few arguments for %s"), argv[i-1]);
687 exit(0);
689 if (sscanf(argv[i], "%i", &wVisualID)!=1) {
690 wwarning(_("bad value for visualid: \"%s\""), argv[i]);
691 exit(0);
693 } else if (strcmp(argv[i], "-static")==0
694 || strcmp(argv[i], "--static")==0) {
696 wPreferences.flags.noupdates = 1;
697 #ifdef XSMP_ENABLED
698 } else if (strcmp(argv[i], "-clientid")==0
699 || strcmp(argv[i], "-restore")==0) {
700 i++;
701 if (i>=argc) {
702 wwarning(_("too few arguments for %s"), argv[i-1]);
703 exit(0);
705 #endif
706 } else if (strcmp(argv[i], "--help")==0) {
707 print_help();
708 exit(0);
709 } else {
710 printf(_("%s: invalid argument '%s'\n"), argv[0], argv[i]);
711 printf(_("Try '%s --help' for more information\n"), argv[0]);
712 exit(1);
717 if (!wPreferences.flags.noupdates) {
718 /* check existence of Defaults DB directory */
719 check_defaults();
723 if (Locale) {
724 setenv("LANG", Locale, 1);
725 } else {
726 Locale = getenv("LC_ALL");
727 if (!Locale) {
728 Locale = getenv("LANG");
732 setlocale(LC_ALL, "");
734 if (!Locale || strcmp(Locale, "C")==0 || strcmp(Locale, "POSIX")==0)
735 Locale = NULL;
736 #ifdef I18N
737 if (getenv("NLSPATH"))
738 bindtextdomain("WindowMaker", getenv("NLSPATH"));
739 else
740 bindtextdomain("WindowMaker", LOCALEDIR);
741 bind_textdomain_codeset("WindowMaker", "UTF-8");
742 textdomain("WindowMaker");
744 if (!XSupportsLocale()) {
745 wwarning(_("X server does not support locale"));
748 if (XSetLocaleModifiers("") == NULL) {
749 wwarning(_("cannot set locale modifiers"));
751 #endif
753 if (Locale) {
754 char *ptr;
756 Locale = wstrdup(Locale);
757 ptr = strchr(Locale, '.');
758 if (ptr)
759 *ptr = 0;
762 /* open display */
763 dpy = XOpenDisplay(DisplayName);
764 if (dpy == NULL) {
765 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName));
766 exit(1);
769 if (fcntl(ConnectionNumber(dpy), F_SETFD, FD_CLOEXEC) < 0) {
770 wsyserror("error setting close-on-exec flag for X connection");
771 exit(1);
774 if (wVisualID < 0)
776 * If unspecified, use default visual instead of waiting
777 * for wrlib/context.c:bestContext() that may end up choosing
778 * the "fake" 24 bits added by the Composite extension.
779 * This is required to avoid all sort of corruptions when
780 * composite is enabled, and at a depth other than 24.
782 wVisualID = (int)DefaultVisual(dpy, DefaultScreen(dpy))->visualid;
784 /* check if the user specified a complete display name (with screen).
785 * If so, only manage the specified screen */
786 if (DisplayName)
787 pos = strchr(DisplayName, ':');
788 else
789 pos = NULL;
791 if (pos && sscanf(pos, ":%i.%i", &d, &s)==2)
792 multiHead = False;
794 DisplayName = XDisplayName(DisplayName);
795 setenv("DISPLAY", DisplayName, 1);
797 #ifdef DEBUG
798 if (doSync)
799 XSynchronize(dpy, True);
800 #endif
802 wXModifierInitialize();
804 #ifdef XSMP_ENABLED
805 wSessionConnectManager(argv, argc);
806 #endif
808 StartUp(!multiHead);
810 if (wScreenCount==1)
811 multiHead = False;
813 execInitScript();
814 inotifyWatchConfig();
815 EventLoop();
816 return -1;