fixed crash bug on startup
[wmaker-crm.git] / src / main.c
blob28dc4a111c653713791dd4391a624cf3eb24fc80
1 /*
2 * Window Maker window manager
3 *
4 * Copyright (c) 1997, 1998 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.
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 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <fcntl.h>
32 #include <X11/Xlib.h>
33 #include <X11/Xutil.h>
36 /* Xlocale.h and locale.h are the same if X_LOCALE is undefind in wconfig.h,
37 * and if X_LOCALE is defined, X's locale emulating functions will be used.
38 * See Xlocale.h for more information.
40 #include <X11/Xlocale.h>
42 #define MAINFILE
44 #include "WindowMaker.h"
45 #include "window.h"
46 #include "funcs.h"
47 #include "menu.h"
48 #include "keybind.h"
49 #include "xmodifier.h"
50 #include "defaults.h"
51 #include "session.h"
52 #include "dialog.h"
54 #include <proplist.h>
56 /****** Global Variables ******/
58 /* general info */
60 Display *dpy;
62 char *ProgName;
64 unsigned int ValidModMask = 0xff;
66 /* locale to use. NULL==POSIX or C */
67 char *Locale=NULL;
69 int wScreenCount=0;
71 WPreferences wPreferences;
74 proplist_t wDomainName;
75 proplist_t wAttributeDomainName;
77 WShortKey wKeyBindings[WKBD_LAST];
79 /* defaults domains */
80 WDDomain *WDWindowMaker = NULL;
81 WDDomain *WDWindowAttributes = NULL;
82 WDDomain *WDRootMenu = NULL;
84 /* XContexts */
85 XContext wWinContext;
86 XContext wAppWinContext;
87 XContext wStackContext;
89 /* Atoms */
90 Atom _XA_WM_STATE;
91 Atom _XA_WM_CHANGE_STATE;
92 Atom _XA_WM_PROTOCOLS;
93 Atom _XA_WM_TAKE_FOCUS;
94 Atom _XA_WM_DELETE_WINDOW;
95 Atom _XA_WM_SAVE_YOURSELF;
96 Atom _XA_WM_CLIENT_LEADER;
97 Atom _XA_WM_COLORMAP_WINDOWS;
98 Atom _XA_WM_COLORMAP_NOTIFY;
100 Atom _XA_GNUSTEP_WM_ATTR;
101 Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
102 Atom _XA_GNUSTEP_WM_RESIZEBAR;
103 Atom _XA_GNUSTEP_TITLEBAR_STATE;
105 Atom _XA_WINDOWMAKER_MENU;
106 Atom _XA_WINDOWMAKER_WM_PROTOCOLS;
107 Atom _XA_WINDOWMAKER_STATE;
109 Atom _XA_WINDOWMAKER_WM_FUNCTION;
110 Atom _XA_WINDOWMAKER_NOTICEBOARD;
111 Atom _XA_WINDOWMAKER_COMMAND;
113 Atom _XA_WINDOWMAKER_ICON_SIZE;
114 Atom _XA_WINDOWMAKER_ICON_TILE;
117 #ifdef OFFIX_DND
118 Atom _XA_DND_PROTOCOL;
119 Atom _XA_DND_SELECTION;
120 #endif
122 /* cursors */
123 Cursor wCursor[WCUR_LAST];
125 /* last event timestamp for XSetInputFocus */
126 Time LastTimestamp;
127 /* timestamp on the last time we did XSetInputFocus() */
128 Time LastFocusChange;
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 /******** End Global Variables *****/
152 static char *DisplayName = NULL;
154 static char **Arguments;
156 static int ArgCount;
158 extern void EventLoop();
159 extern void StartUp();
161 static Bool multiHead = True;
163 /* stdi/o for log shell */
164 static int LogStdIn = -1, LogStdOut = -1, LogStdErr = -1;
167 void
168 Exit(int status)
170 #ifdef XSMP_ENABLED
171 wSessionDisconnectManager();
172 #endif
173 if (dpy)
174 XCloseDisplay(dpy);
176 exit(status);
180 void
181 Restart(char *manager, Bool abortOnFailure)
183 char *prog=NULL;
184 char *argv[MAX_RESTART_ARGS];
185 int i;
187 if (manager && manager[0]!=0) {
188 prog = argv[0] = strtok(manager, " ");
189 for (i=1; i<MAX_RESTART_ARGS; i++) {
190 argv[i]=strtok(NULL, " ");
191 if (argv[i]==NULL) {
192 break;
196 if (dpy) {
197 #ifdef XSMP_ENABLED
198 wSessionDisconnectManager();
199 #endif
200 XCloseDisplay(dpy);
201 dpy = NULL;
203 if (!prog) {
204 execvp(Arguments[0], Arguments);
205 wfatal(_("failed to restart Window Maker."));
206 } else {
207 execvp(prog, argv);
208 wsyserror(_("could not exec %s"), prog);
210 if (abortOnFailure)
211 exit(-1);
216 void
217 SetupEnvironment(WScreen *scr)
219 char *tmp, *ptr;
220 char buf[16];
222 if (multiHead) {
223 int len = strlen(DisplayName)+64;
224 tmp = wmalloc(len);
225 snprintf(tmp, len, "DISPLAY=%s", XDisplayName(DisplayName));
226 ptr = strchr(strchr(tmp, ':'), '.');
227 if (ptr)
228 *ptr = 0;
229 snprintf(buf, sizeof(buf), ".%i", scr->screen);
230 strcat(tmp, buf);
231 putenv(tmp);
233 tmp = wmalloc(60);
234 snprintf(tmp, 60, "WRASTER_COLOR_RESOLUTION%i=%i", scr->screen,
235 scr->rcontext->attribs->colors_per_channel);
236 putenv(tmp);
242 typedef struct {
243 WScreen *scr;
244 char *command;
245 } _tuple;
248 static void
249 shellCommandHandler(pid_t pid, unsigned char status, _tuple *data)
251 if (status == 127) {
252 char *buffer;
254 buffer = wstrconcat(_("Could not execute command: "), data->command);
256 wMessageDialog(data->scr, _("Error"), buffer, _("OK"), NULL, NULL);
257 wfree(buffer);
258 } else if (status != 127) {
260 printf("%s: %i\n", data->command, status);
264 wfree(data->command);
265 wfree(data);
269 void
270 ExecuteShellCommand(WScreen *scr, char *command)
272 static char *shell = NULL;
273 pid_t pid;
276 * This have a problem: if the shell is tcsh (not sure about others)
277 * and ~/.tcshrc have /bin/stty erase ^H somewhere on it, the shell
278 * will block and the command will not be executed.
279 if (!shell) {
280 shell = getenv("SHELL");
281 if (!shell)
282 shell = "/bin/sh";
285 shell = "/bin/sh";
287 pid = fork();
289 if (pid==0) {
291 SetupEnvironment(scr);
293 #ifdef HAVE_SETPGID
294 setpgid(0, 0);
295 #endif
296 execl(shell, shell, "-c", command, NULL);
297 wsyserror("could not execute %s -c %s", shell, command);
298 Exit(-1);
299 } else if (pid < 0) {
300 wsyserror("cannot fork a new process");
301 } else {
302 _tuple *data = wmalloc(sizeof(_tuple));
304 data->scr = scr;
305 data->command = wstrdup(command);
307 wAddDeathHandler(pid, (WDeathHandler*)shellCommandHandler, data);
313 *---------------------------------------------------------------------------
314 * StartLogShell
315 * Start a shell that will receive all stdin and stdout from processes
316 * forked by wmaker.
317 *---------------------------------------------------------------------------
319 void
320 StartLogShell(WScreen *scr)
322 int in_fd[2];
323 int out_fd[2];
324 int err_fd[2];
325 pid_t pid;
327 SetupEnvironment(scr);
329 if (pipe(in_fd) < 0) {
330 wsyserror("could not create pipe for log shell\n");
331 return;
333 if (pipe(out_fd) < 0) {
334 wsyserror("could not create pipe for log shell\n");
335 close(in_fd[0]);
336 close(in_fd[1]);
337 return;
339 if (pipe(err_fd) < 0) {
340 wsyserror("could not create pipe for log shell\n");
341 close(out_fd[0]);
342 close(out_fd[1]);
343 close(in_fd[0]);
344 close(in_fd[1]);
345 return;
348 pid = fork();
349 if (pid < 0) {
350 wsyserror("could not fork a new process for log shell\n");
351 return;
352 } else if (pid == 0) {
353 close(in_fd[0]);
354 close(out_fd[1]);
355 close(err_fd[1]);
357 close(0);
358 close(1);
359 close(2);
361 if (dup2(in_fd[1], 0) < 0) {
362 wsyserror("could not redirect stdin for log shell\n");
363 exit(1);
365 if (dup2(out_fd[1], 1) < 0) {
366 wsyserror("could not redirect stdout for log shell\n");
367 exit(1);
369 if (dup2(err_fd[1], 2) < 0) {
370 wsyserror("could not redirect stderr for log shell\n");
371 exit(1);
374 close(in_fd[1]);
375 close(out_fd[1]);
376 close(err_fd[1]);
378 execl("/bin/sh", "/bin/sh", "-c", wPreferences.logger_shell, NULL);
379 wsyserror("could not execute %s\n", wPreferences.logger_shell);
380 exit(1);
381 } else {
382 close(in_fd[1]);
383 close(out_fd[0]);
384 close(err_fd[0]);
386 LogStdIn = in_fd[1];
387 LogStdOut = out_fd[0];
388 LogStdErr = err_fd[0];
394 *---------------------------------------------------------------------
395 * wAbort--
396 * Do a major cleanup and exit the program
398 *----------------------------------------------------------------------
400 void
401 wAbort(Bool dumpCore)
403 int i;
404 WScreen *scr;
406 for (i=0; i<wScreenCount; i++) {
407 scr = wScreenWithNumber(i);
408 if (scr)
409 RestoreDesktop(scr);
411 printf(_("%s aborted.\n"), ProgName);
412 if (dumpCore)
413 abort();
414 else
415 exit(1);
419 void
420 print_help()
422 printf(_("Usage: %s [options]\n"), ProgName);
423 puts(_("The Window Maker window manager for the X window system"));
424 puts("");
425 puts(_(" -display host:dpy display to use"));
426 #ifdef USECPP
427 puts(_(" --no-cpp disable preprocessing of configuration files"));
428 #endif
429 puts(_(" --no-dock do not open the application Dock"));
430 puts(_(" --no-clip do not open the workspace Clip"));
431 puts(_(" --no-autolaunch do not autolaunch applications"));
432 puts(_(" --dont-restore do not restore saved session"));
434 puts(_(" --locale locale locale to use"));
436 puts(_(" --create-stdcmap create the standard colormap hint in PseudoColor visuals"));
437 puts(_(" --visual-id visualid visual id of visual to use"));
438 puts(_(" --static do not update or save configurations"));
439 puts(_(" --no-polling do not periodically check for configuration updates"));
440 #ifdef DEBUG
441 puts(_(" --synchronous turn on synchronous display mode"));
442 #endif
443 puts(_(" --version print version and exit"));
444 puts(_(" --help show this message"));
449 void
450 check_defaults()
452 char *path;
454 path = wdefaultspathfordomain("");
455 if (access(path, R_OK)!=0) {
456 #if 0
457 wfatal(_("could not find user GNUstep directory (%s).\n"
458 "Make sure you have installed Window Maker correctly and run wmaker.inst"),
459 path);
460 exit(1);
461 #else
462 wwarning(_("could not find user GNUstep directory (%s)."), path);
464 if (system("wmaker.inst --batch") != 0) {
465 wwarning(_("There was an error while creating GNUstep directory, please "
466 "make sure you have installed Window Maker correctly and run wmaker.inst"));
467 } else {
468 wwarning(_("%s directory created with default configuration."), path);
470 #endif
473 wfree(path);
477 static void
478 execInitScript()
480 char *file;
481 char *paths = wstrconcat(wusergnusteppath(), ":"DEF_CONFIG_PATHS);
483 file = wfindfile(paths, DEF_INIT_SCRIPT);
484 wfree(paths);
486 if (file) {
487 if (system(file) != 0) {
488 wsyserror(_("%s:could not execute initialization script"), file);
490 #if 0
491 if (fork()==0) {
492 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
493 wsyserror(_("%s:could not execute initialization script"), file);
494 exit(1);
496 #endif
497 wfree(file);
502 void
503 ExecExitScript()
505 char *file;
506 char *paths = wstrconcat(wusergnusteppath(), ":"DEF_CONFIG_PATHS);
508 file = wfindfile(paths, DEF_EXIT_SCRIPT);
509 wfree(paths);
511 if (file) {
512 if (system(file) != 0) {
513 wsyserror(_("%s:could not execute exit script"), file);
515 #if 0
516 if (fork()==0) {
517 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
518 wsyserror(_("%s:could not execute exit script"), file);
519 exit(1);
521 #endif
522 wfree(file);
526 #if 0
527 char*
528 getFullPath(char *path)
530 char buffer[1024];
531 char *tmp;
532 char *basep = (char*)buffer;
534 if (*path != '/' && getcwd(buffer, 1023)) {
536 for (;;) {
537 if (strncmp(path, "../", 3)==0) {
538 path += 3;
539 basep = strchr(basep, '/');
540 if (!basep || *path==0)
541 break;
544 if (*path == '/' || strncmp(path, "./",2)==0) {
545 tmp =
549 * path
550 * ./path
551 * ../path
552 * ../../path
556 } else {
557 return wstrconcat(path);
560 return tmp;
562 #endif
565 main(int argc, char **argv)
567 int i, restart=0;
568 char *str;
569 int d, s;
570 #ifdef DEBUG
571 Bool doSync = False;
572 #endif
573 setlocale(LC_ALL, "");
575 wsetabort(wAbort);
577 /* for telling WPrefs what's the name of the wmaker binary being ran */
579 str = wstrconcat("WMAKER_BIN_NAME=", argv[0]);
580 putenv(str);
582 ArgCount = argc;
583 Arguments = argv;
585 WMInitializeApplication("WindowMaker", &argc, argv);
588 ProgName = strrchr(argv[0],'/');
589 if (!ProgName)
590 ProgName = argv[0];
591 else
592 ProgName++;
595 restart = 0;
597 memset(&wPreferences, 0, sizeof(WPreferences));
599 if (argc>1) {
600 for (i=1; i<argc; i++) {
601 #ifdef USECPP
602 if (strcmp(argv[i], "-nocpp")==0
603 || strcmp(argv[i], "--no-cpp")==0) {
604 wPreferences.flags.nocpp=1;
605 } else
606 #endif
607 if (strcmp(argv[i], "-no-autolaunch")==0
608 || strcmp(argv[i], "--no-autolaunch")==0) {
609 wPreferences.flags.noautolaunch = 1;
610 } else if (strcmp(argv[i], "-dont-restore")==0
611 || strcmp(argv[i], "--dont-restore")==0) {
612 wPreferences.flags.norestore = 1;
613 } else if (strcmp(argv[i], "-nodock")==0
614 || strcmp(argv[i], "--no-dock")==0) {
615 wPreferences.flags.nodock=1;
616 } else if (strcmp(argv[i], "-noclip")==0
617 || strcmp(argv[i], "--no-clip")==0) {
618 wPreferences.flags.noclip=1;
619 } else if (strcmp(argv[i], "-version")==0
620 || strcmp(argv[i], "--version")==0) {
621 printf("Window Maker %s\n", VERSION);
622 exit(0);
623 } else if (strcmp(argv[i], "--global_defaults_path")==0) {
624 printf("%s/WindowMaker\n", SYSCONFDIR);
625 exit(0);
626 #ifdef DEBUG
627 } else if (strcmp(argv[i], "--synchronous")==0) {
628 doSync = 1;
629 #endif
630 } else if (strcmp(argv[i], "-locale")==0
631 || strcmp(argv[i], "--locale")==0) {
632 i++;
633 if (i>=argc) {
634 wwarning(_("too few arguments for %s"), argv[i-1]);
635 exit(0);
637 Locale = argv[i];
638 } else if (strcmp(argv[i], "-display")==0
639 || strcmp(argv[i], "--display")==0) {
640 i++;
641 if (i>=argc) {
642 wwarning(_("too few arguments for %s"), argv[i-1]);
643 exit(0);
645 DisplayName = argv[i];
646 } else if (strcmp(argv[i], "-visualid")==0
647 || strcmp(argv[i], "--visual-id")==0) {
648 i++;
649 if (i>=argc) {
650 wwarning(_("too few arguments for %s"), argv[i-1]);
651 exit(0);
653 if (sscanf(argv[i], "%i", &wVisualID)!=1) {
654 wwarning(_("bad value for visualid: \"%s\""), argv[i]);
655 exit(0);
657 } else if (strcmp(argv[i], "-static")==0
658 || strcmp(argv[i], "--static")==0) {
660 wPreferences.flags.noupdates = 1;
661 } else if (strcmp(argv[i], "-nopolling")==0
662 || strcmp(argv[i], "--no-polling")==0) {
664 wPreferences.flags.nopolling = 1;
665 #ifdef XSMP_ENABLED
666 } else if (strcmp(argv[i], "-clientid")==0
667 || strcmp(argv[i], "-restore")==0) {
668 i++;
669 if (i>=argc) {
670 wwarning(_("too few arguments for %s"), argv[i-1]);
671 exit(0);
673 #endif
674 } else if (strcmp(argv[i], "--help")==0) {
675 print_help();
676 exit(0);
677 } else {
678 printf(_("%s: invalid argument '%s'\n"), argv[0], argv[i]);
679 printf(_("Try '%s --help' for more information\n"), argv[0]);
680 exit(1);
685 if (!wPreferences.flags.noupdates) {
686 /* check existence of Defaults DB directory */
687 check_defaults();
691 if (!Locale) {
692 Locale = getenv("LC_ALL");
694 if (!Locale) {
695 Locale = getenv("LANG");
698 setlocale(LC_ALL, "");
700 if (!Locale || strcmp(Locale, "C")==0 || strcmp(Locale, "POSIX")==0)
701 Locale = NULL;
702 #ifdef I18N
703 if (getenv("NLSPATH"))
704 bindtextdomain("WindowMaker", getenv("NLSPATH"));
705 else
706 bindtextdomain("WindowMaker", LOCALEDIR);
707 textdomain("WindowMaker");
709 if (!XSupportsLocale()) {
710 wwarning(_("X server does not support locale"));
713 if (XSetLocaleModifiers("") == NULL) {
714 wwarning(_("cannot set locale modifiers"));
716 #endif
718 if (Locale) {
719 char *ptr;
721 Locale = wstrdup(Locale);
722 ptr = strchr(Locale, '.');
723 if (ptr)
724 *ptr = 0;
727 /* open display */
728 dpy = XOpenDisplay(DisplayName);
729 if (dpy == NULL) {
730 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName));
731 exit(1);
734 if (fcntl(ConnectionNumber(dpy), F_SETFD, FD_CLOEXEC) < 0) {
735 wsyserror("error setting close-on-exec flag for X connection");
736 exit(1);
739 /* check if the user specified a complete display name (with screen).
740 * If so, only manage the specified screen */
741 if (DisplayName)
742 str = strchr(DisplayName, ':');
743 else
744 str = NULL;
746 if (str && sscanf(str, ":%i.%i", &d, &s)==2)
747 multiHead = False;
749 DisplayName = XDisplayName(DisplayName);
751 int len = strlen(DisplayName)+64;
752 str = wmalloc(len);
753 snprintf(str, len, "DISPLAY=%s", DisplayName);
755 putenv(str);
757 #ifdef DEBUG
758 if (doSync)
759 XSynchronize(dpy, True);
760 #endif
762 wXModifierInitialize();
764 #ifdef XSMP_ENABLED
765 wSessionConnectManager(argv, argc);
766 #endif
768 StartUp(!multiHead);
770 if (wScreenCount==1)
771 multiHead = False;
773 execInitScript();
775 EventLoop();
776 return -1;