fixed some signal handling bugs
[wmaker-crm.git] / src / main.c
blobf2861609276638e6344867f890113650b71dc1a7
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 tmp = wmalloc(strlen(DisplayName)+64);
224 sprintf(tmp, "DISPLAY=%s", XDisplayName(DisplayName));
225 ptr = strchr(strchr(tmp, ':'), '.');
226 if (ptr)
227 *ptr = 0;
228 sprintf(buf, ".%i", scr->screen);
229 strcat(tmp, buf);
230 putenv(tmp);
232 tmp = wmalloc(60);
233 sprintf(tmp, "WRASTER_COLOR_RESOLUTION%i=%i", scr->screen,
234 scr->rcontext->attribs->colors_per_channel);
235 putenv(tmp);
241 typedef struct {
242 WScreen *scr;
243 char *command;
244 } _tuple;
247 static void
248 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) {
259 printf("%s: %i\n", data->command, status);
263 wfree(data->command);
264 wfree(data);
268 void
269 ExecuteShellCommand(WScreen *scr, char *command)
271 static char *shell = NULL;
272 pid_t pid;
275 * This have a problem: if the shell is tcsh (not sure about others)
276 * and ~/.tcshrc have /bin/stty erase ^H somewhere on it, the shell
277 * will block and the command will not be executed.
278 if (!shell) {
279 shell = getenv("SHELL");
280 if (!shell)
281 shell = "/bin/sh";
284 shell = "/bin/sh";
286 pid = fork();
288 if (pid==0) {
290 SetupEnvironment(scr);
292 #ifdef HAVE_SETPGID
293 setpgid(0, 0);
294 #endif
295 execl(shell, shell, "-c", command, NULL);
296 wsyserror("could not execute %s -c %s", shell, command);
297 Exit(-1);
298 } else if (pid < 0) {
299 wsyserror("cannot fork a new process");
300 } else {
301 _tuple *data = wmalloc(sizeof(_tuple));
303 data->scr = scr;
304 data->command = wstrdup(command);
306 wAddDeathHandler(pid, (WDeathHandler*)shellCommandHandler, data);
312 *---------------------------------------------------------------------------
313 * StartLogShell
314 * Start a shell that will receive all stdin and stdout from processes
315 * forked by wmaker.
316 *---------------------------------------------------------------------------
318 void
319 StartLogShell(WScreen *scr)
321 int in_fd[2];
322 int out_fd[2];
323 int err_fd[2];
324 pid_t pid;
326 SetupEnvironment(scr);
328 if (pipe(in_fd) < 0) {
329 wsyserror("could not create pipe for log shell\n");
330 return;
332 if (pipe(out_fd) < 0) {
333 wsyserror("could not create pipe for log shell\n");
334 close(in_fd[0]);
335 close(in_fd[1]);
336 return;
338 if (pipe(err_fd) < 0) {
339 wsyserror("could not create pipe for log shell\n");
340 close(out_fd[0]);
341 close(out_fd[1]);
342 close(in_fd[0]);
343 close(in_fd[1]);
344 return;
347 pid = fork();
348 if (pid < 0) {
349 wsyserror("could not fork a new process for log shell\n");
350 return;
351 } else if (pid == 0) {
352 close(in_fd[0]);
353 close(out_fd[1]);
354 close(err_fd[1]);
356 close(0);
357 close(1);
358 close(2);
360 if (dup2(in_fd[1], 0) < 0) {
361 wsyserror("could not redirect stdin for log shell\n");
362 exit(1);
364 if (dup2(out_fd[1], 1) < 0) {
365 wsyserror("could not redirect stdout for log shell\n");
366 exit(1);
368 if (dup2(err_fd[1], 2) < 0) {
369 wsyserror("could not redirect stderr for log shell\n");
370 exit(1);
373 close(in_fd[1]);
374 close(out_fd[1]);
375 close(err_fd[1]);
377 execl("/bin/sh", "/bin/sh", "-c", wPreferences.logger_shell, NULL);
378 wsyserror("could not execute %s\n", wPreferences.logger_shell);
379 exit(1);
380 } else {
381 close(in_fd[1]);
382 close(out_fd[0]);
383 close(err_fd[0]);
385 LogStdIn = in_fd[1];
386 LogStdOut = out_fd[0];
387 LogStdErr = err_fd[0];
393 *---------------------------------------------------------------------
394 * wAbort--
395 * Do a major cleanup and exit the program
397 *----------------------------------------------------------------------
399 void
400 wAbort(Bool dumpCore)
402 int i;
403 WScreen *scr;
405 for (i=0; i<wScreenCount; i++) {
406 scr = wScreenWithNumber(i);
407 if (scr)
408 RestoreDesktop(scr);
410 printf(_("%s aborted.\n"), ProgName);
411 if (dumpCore)
412 abort();
413 else
414 exit(1);
418 void
419 print_help()
421 printf(_("Usage: %s [options]\n"), ProgName);
422 puts(_("The Window Maker window manager for the X window system"));
423 puts("");
424 puts(_(" -display host:dpy display to use"));
425 #ifdef USECPP
426 puts(_(" --no-cpp disable preprocessing of configuration files"));
427 #endif
428 puts(_(" --no-dock do not open the application Dock"));
429 puts(_(" --no-clip do not open the workspace Clip"));
430 puts(_(" --no-autolaunch do not autolaunch applications"));
431 puts(_(" --dont-restore do not restore saved session"));
433 puts(_(" --locale locale locale to use"));
435 puts(_(" --create-stdcmap create the standard colormap hint in PseudoColor visuals"));
436 puts(_(" --visual-id visualid visual id of visual to use"));
437 puts(_(" --static do not update or save configurations"));
438 puts(_(" --no-polling do not periodically check for configuration updates"));
439 #ifdef DEBUG
440 puts(_(" --synchronous turn on synchronous display mode"));
441 #endif
442 puts(_(" --version print version and exit"));
443 puts(_(" --help show this message"));
448 void
449 check_defaults()
451 char *path;
453 path = wdefaultspathfordomain("");
454 if (access(path, R_OK)!=0) {
455 wfatal(_("could not find user GNUstep directory (%s).\n"
456 "Make sure you have installed Window Maker correctly and run wmaker.inst"),
457 path);
458 exit(1);
461 wfree(path);
465 static void
466 execInitScript()
468 char *file;
469 char *paths = wstrconcat(wusergnusteppath(), ":"DEF_CONFIG_PATHS);
471 file = wfindfile(paths, DEF_INIT_SCRIPT);
472 wfree(paths);
474 if (file) {
475 if (system(file) != 0) {
476 wsyserror(_("%s:could not execute initialization script"), file);
478 #if 0
479 if (fork()==0) {
480 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
481 wsyserror(_("%s:could not execute initialization script"), file);
482 exit(1);
484 #endif
485 wfree(file);
490 void
491 ExecExitScript()
493 char *file;
494 char *paths = wstrconcat(wusergnusteppath(), ":"DEF_CONFIG_PATHS);
496 file = wfindfile(paths, DEF_EXIT_SCRIPT);
497 wfree(paths);
499 if (file) {
500 if (system(file) != 0) {
501 wsyserror(_("%s:could not execute exit script"), file);
503 #if 0
504 if (fork()==0) {
505 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
506 wsyserror(_("%s:could not execute exit script"), file);
507 exit(1);
509 #endif
510 wfree(file);
514 #if 0
515 char*
516 getFullPath(char *path)
518 char buffer[1024];
519 char *tmp;
520 char *basep = (char*)buffer;
522 if (*path != '/' && getcwd(buffer, 1023)) {
524 for (;;) {
525 if (strncmp(path, "../", 3)==0) {
526 path += 3;
527 basep = strchr(basep, '/');
528 if (!basep || *path==0)
529 break;
532 if (*path == '/' || strncmp(path, "./",2)==0) {
533 tmp =
537 * path
538 * ./path
539 * ../path
540 * ../../path
544 } else {
545 return wstrconcat(path);
548 return tmp;
550 #endif
553 main(int argc, char **argv)
555 int i, restart=0;
556 char *str;
557 int d, s;
558 #ifdef DEBUG
559 Bool doSync = False;
560 #endif
561 setlocale(LC_ALL, "");
563 wsetabort(wAbort);
565 /* for telling WPrefs what's the name of the wmaker binary being ran */
567 str = wstrconcat("WMAKER_BIN_NAME=", argv[0]);
568 putenv(str);
570 ArgCount = argc;
571 Arguments = argv;
573 WMInitializeApplication("WindowMaker", &argc, argv);
576 ProgName = strrchr(argv[0],'/');
577 if (!ProgName)
578 ProgName = argv[0];
579 else
580 ProgName++;
583 restart = 0;
585 memset(&wPreferences, 0, sizeof(WPreferences));
587 if (argc>1) {
588 for (i=1; i<argc; i++) {
589 #ifdef USECPP
590 if (strcmp(argv[i], "-nocpp")==0
591 || strcmp(argv[i], "--no-cpp")==0) {
592 wPreferences.flags.nocpp=1;
593 } else
594 #endif
595 if (strcmp(argv[i], "-no-autolaunch")==0
596 || strcmp(argv[i], "--no-autolaunch")==0) {
597 wPreferences.flags.noautolaunch = 1;
598 } else if (strcmp(argv[i], "-dont-restore")==0
599 || strcmp(argv[i], "--dont-restore")==0) {
600 wPreferences.flags.norestore = 1;
601 } else if (strcmp(argv[i], "-nodock")==0
602 || strcmp(argv[i], "--no-dock")==0) {
603 wPreferences.flags.nodock=1;
604 } else if (strcmp(argv[i], "-noclip")==0
605 || strcmp(argv[i], "--no-clip")==0) {
606 wPreferences.flags.noclip=1;
607 } else if (strcmp(argv[i], "-version")==0
608 || strcmp(argv[i], "--version")==0) {
609 printf("Window Maker %s\n", VERSION);
610 exit(0);
611 } else if (strcmp(argv[i], "--global_defaults_path")==0) {
612 printf("%s/WindowMaker\n", SYSCONFDIR);
613 exit(0);
614 #ifdef DEBUG
615 } else if (strcmp(argv[i], "--synchronous")==0) {
616 doSync = 1;
617 #endif
618 } else if (strcmp(argv[i], "-locale")==0
619 || strcmp(argv[i], "--locale")==0) {
620 i++;
621 if (i>=argc) {
622 wwarning(_("too few arguments for %s"), argv[i-1]);
623 exit(0);
625 Locale = argv[i];
626 } else if (strcmp(argv[i], "-display")==0
627 || strcmp(argv[i], "--display")==0) {
628 i++;
629 if (i>=argc) {
630 wwarning(_("too few arguments for %s"), argv[i-1]);
631 exit(0);
633 DisplayName = argv[i];
634 } else if (strcmp(argv[i], "-visualid")==0
635 || strcmp(argv[i], "--visual-id")==0) {
636 i++;
637 if (i>=argc) {
638 wwarning(_("too few arguments for %s"), argv[i-1]);
639 exit(0);
641 if (sscanf(argv[i], "%i", &wVisualID)!=1) {
642 wwarning(_("bad value for visualid: \"%s\""), argv[i]);
643 exit(0);
645 } else if (strcmp(argv[i], "-static")==0
646 || strcmp(argv[i], "--static")==0) {
648 wPreferences.flags.noupdates = 1;
649 } else if (strcmp(argv[i], "-nopolling")==0
650 || strcmp(argv[i], "--no-polling")==0) {
652 wPreferences.flags.nopolling = 1;
653 #ifdef XSMP_ENABLED
654 } else if (strcmp(argv[i], "-clientid")==0
655 || strcmp(argv[i], "-restore")==0) {
656 i++;
657 if (i>=argc) {
658 wwarning(_("too few arguments for %s"), argv[i-1]);
659 exit(0);
661 #endif
662 } else if (strcmp(argv[i], "--help")==0) {
663 print_help();
664 exit(0);
665 } else {
666 printf(_("%s: invalid argument '%s'\n"), argv[0], argv[i]);
667 printf(_("Try '%s --help' for more information\n"), argv[0]);
668 exit(1);
673 if (!wPreferences.flags.noupdates) {
674 /* check existence of Defaults DB directory */
675 check_defaults();
679 if (!Locale) {
680 Locale = getenv("LC_ALL");
682 if (!Locale) {
683 Locale = getenv("LANG");
686 setlocale(LC_ALL, "");
688 if (!Locale || strcmp(Locale, "C")==0 || strcmp(Locale, "POSIX")==0)
689 Locale = NULL;
690 #ifdef I18N
691 if (getenv("NLSPATH"))
692 bindtextdomain("WindowMaker", getenv("NLSPATH"));
693 else
694 bindtextdomain("WindowMaker", LOCALEDIR);
695 textdomain("WindowMaker");
697 if (!XSupportsLocale()) {
698 wwarning(_("X server does not support locale"));
701 if (XSetLocaleModifiers("") == NULL) {
702 wwarning(_("cannot set locale modifiers"));
704 #endif
706 if (Locale) {
707 char *ptr;
709 Locale = wstrdup(Locale);
710 ptr = strchr(Locale, '.');
711 if (ptr)
712 *ptr = 0;
715 /* open display */
716 dpy = XOpenDisplay(DisplayName);
717 if (dpy == NULL) {
718 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName));
719 exit(1);
722 if (fcntl(ConnectionNumber(dpy), F_SETFD, FD_CLOEXEC) < 0) {
723 wsyserror("error setting close-on-exec flag for X connection");
724 exit(1);
727 /* check if the user specified a complete display name (with screen).
728 * If so, only manage the specified screen */
729 if (DisplayName)
730 str = strchr(DisplayName, ':');
731 else
732 str = NULL;
734 if (str && sscanf(str, "%i.%i", &d, &s)==2)
735 multiHead = False;
737 DisplayName = XDisplayName(DisplayName);
738 str = wmalloc(strlen(DisplayName)+64);
739 sprintf(str, "DISPLAY=%s", DisplayName);
740 putenv(str);
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();
760 EventLoop();
761 return -1;