- Fixed a bug that crashed wmaker when selecting the "Start alternate window
[wmaker-crm.git] / src / main.c
blob8b408bacf7b20c0e567f0036edcba425b779ce3b
1 /*
2 * Window Maker window manager
3 *
4 * Copyright (c) 1997-2002 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 <WINGs/WUtil.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 WMPropList *wDomainName;
75 WMPropList *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;
151 /* notifications */
152 const char *WMNManaged = "WMNManaged";
153 const char *WMNUnmanaged = "WMNUnmanaged";
154 const char *WMNChangedWorkspace = "WMNChangedWorkspace";
155 const char *WMNChangedState = "WMNChangedState";
156 const char *WMNChangedFocus = "WMNChangedFocus";
157 const char *WMNChangedStacking = "WMNChangedStacking";
158 const char *WMNChangedName = "WMNChangedName";
160 const char *WMNWorkspaceCreated = "WMNWorkspaceCreated";
161 const char *WMNWorkspaceDestroyed = "WMNWorkspaceDestroyed";
162 const char *WMNWorkspaceChanged = "WMNWorkspaceChanged";
163 const char *WMNWorkspaceNameChanged = "WMNWorkspaceNameChanged";
165 const char *WMNResetStacking = "WMNResetStacking";
167 /******** End Global Variables *****/
169 static char *DisplayName = NULL;
171 static char **Arguments;
173 static int ArgCount;
175 extern void EventLoop();
176 extern void StartUp();
178 static Bool multiHead = True;
180 /* stdi/o for log shell */
181 static int LogStdIn = -1, LogStdOut = -1, LogStdErr = -1;
184 void
185 Exit(int status)
187 #ifdef XSMP_ENABLED
188 wSessionDisconnectManager();
189 #endif
190 if (dpy)
191 XCloseDisplay(dpy);
193 exit(status);
197 void
198 Restart(char *manager, Bool abortOnFailure)
200 char *prog=NULL;
201 char *argv[MAX_RESTART_ARGS];
202 int i;
204 if (manager && manager[0]!=0) {
205 prog = argv[0] = strtok(manager, " ");
206 for (i=1; i<MAX_RESTART_ARGS; i++) {
207 argv[i]=strtok(NULL, " ");
208 if (argv[i]==NULL) {
209 break;
213 if (dpy) {
214 #ifdef XSMP_ENABLED
215 wSessionDisconnectManager();
216 #endif
217 XCloseDisplay(dpy);
218 dpy = NULL;
220 if (!prog) {
221 execvp(Arguments[0], Arguments);
222 wfatal(_("failed to restart Window Maker."));
223 } else {
224 execvp(prog, argv);
225 wsyserror(_("could not exec %s"), prog);
227 if (abortOnFailure)
228 exit(-1);
233 void
234 SetupEnvironment(WScreen *scr)
236 char *tmp, *ptr;
237 char buf[16];
239 if (multiHead) {
240 int len = strlen(DisplayName)+64;
241 tmp = wmalloc(len);
242 snprintf(tmp, len, "DISPLAY=%s", XDisplayName(DisplayName));
243 ptr = strchr(strchr(tmp, ':'), '.');
244 if (ptr)
245 *ptr = 0;
246 snprintf(buf, sizeof(buf), ".%i", scr->screen);
247 strcat(tmp, buf);
248 putenv(tmp);
250 tmp = wmalloc(60);
251 snprintf(tmp, 60, "WRASTER_COLOR_RESOLUTION%i=%i", scr->screen,
252 scr->rcontext->attribs->colors_per_channel);
253 putenv(tmp);
259 typedef struct {
260 WScreen *scr;
261 char *command;
262 } _tuple;
265 static void
266 shellCommandHandler(pid_t pid, unsigned char status, _tuple *data)
268 if (status == 127) {
269 char *buffer;
271 buffer = wstrconcat(_("Could not execute command: "), data->command);
273 wMessageDialog(data->scr, _("Error"), buffer, _("OK"), NULL, NULL);
274 wfree(buffer);
275 } else if (status != 127) {
277 printf("%s: %i\n", data->command, status);
281 wfree(data->command);
282 wfree(data);
286 void
287 ExecuteShellCommand(WScreen *scr, char *command)
289 static char *shell = NULL;
290 pid_t pid;
293 * This have a problem: if the shell is tcsh (not sure about others)
294 * and ~/.tcshrc have /bin/stty erase ^H somewhere on it, the shell
295 * will block and the command will not be executed.
296 if (!shell) {
297 shell = getenv("SHELL");
298 if (!shell)
299 shell = "/bin/sh";
302 shell = "/bin/sh";
304 pid = fork();
306 if (pid==0) {
308 SetupEnvironment(scr);
310 #ifdef HAVE_SETPGID
311 setpgid(0, 0);
312 #endif
313 execl(shell, shell, "-c", command, NULL);
314 wsyserror("could not execute %s -c %s", shell, command);
315 Exit(-1);
316 } else if (pid < 0) {
317 wsyserror("cannot fork a new process");
318 } else {
319 _tuple *data = wmalloc(sizeof(_tuple));
321 data->scr = scr;
322 data->command = wstrdup(command);
324 wAddDeathHandler(pid, (WDeathHandler*)shellCommandHandler, data);
330 *---------------------------------------------------------------------------
331 * StartLogShell
332 * Start a shell that will receive all stdin and stdout from processes
333 * forked by wmaker.
334 *---------------------------------------------------------------------------
336 void
337 StartLogShell(WScreen *scr)
339 int in_fd[2];
340 int out_fd[2];
341 int err_fd[2];
342 pid_t pid;
344 SetupEnvironment(scr);
346 if (pipe(in_fd) < 0) {
347 wsyserror("could not create pipe for log shell\n");
348 return;
350 if (pipe(out_fd) < 0) {
351 wsyserror("could not create pipe for log shell\n");
352 close(in_fd[0]);
353 close(in_fd[1]);
354 return;
356 if (pipe(err_fd) < 0) {
357 wsyserror("could not create pipe for log shell\n");
358 close(out_fd[0]);
359 close(out_fd[1]);
360 close(in_fd[0]);
361 close(in_fd[1]);
362 return;
365 pid = fork();
366 if (pid < 0) {
367 wsyserror("could not fork a new process for log shell\n");
368 return;
369 } else if (pid == 0) {
370 close(in_fd[0]);
371 close(out_fd[1]);
372 close(err_fd[1]);
374 close(0);
375 close(1);
376 close(2);
378 if (dup2(in_fd[1], 0) < 0) {
379 wsyserror("could not redirect stdin for log shell\n");
380 exit(1);
382 if (dup2(out_fd[1], 1) < 0) {
383 wsyserror("could not redirect stdout for log shell\n");
384 exit(1);
386 if (dup2(err_fd[1], 2) < 0) {
387 wsyserror("could not redirect stderr for log shell\n");
388 exit(1);
391 close(in_fd[1]);
392 close(out_fd[1]);
393 close(err_fd[1]);
395 execl("/bin/sh", "/bin/sh", "-c", wPreferences.logger_shell, NULL);
396 wsyserror("could not execute %s\n", wPreferences.logger_shell);
397 exit(1);
398 } else {
399 close(in_fd[1]);
400 close(out_fd[0]);
401 close(err_fd[0]);
403 LogStdIn = in_fd[1];
404 LogStdOut = out_fd[0];
405 LogStdErr = err_fd[0];
411 *---------------------------------------------------------------------
412 * wAbort--
413 * Do a major cleanup and exit the program
415 *----------------------------------------------------------------------
417 void
418 wAbort(Bool dumpCore)
420 int i;
421 WScreen *scr;
423 for (i=0; i<wScreenCount; i++) {
424 scr = wScreenWithNumber(i);
425 if (scr)
426 RestoreDesktop(scr);
428 printf(_("%s aborted.\n"), ProgName);
429 if (dumpCore)
430 abort();
431 else
432 exit(1);
436 void
437 print_help()
439 printf(_("Usage: %s [options]\n"), ProgName);
440 puts(_("The Window Maker window manager for the X window system"));
441 puts("");
442 puts(_(" -display host:dpy display to use"));
443 #ifdef USECPP
444 puts(_(" --no-cpp disable preprocessing of configuration files"));
445 #endif
446 puts(_(" --no-dock do not open the application Dock"));
447 puts(_(" --no-clip do not open the workspace Clip"));
448 puts(_(" --no-autolaunch do not autolaunch applications"));
449 puts(_(" --dont-restore do not restore saved session"));
451 puts(_(" --locale locale locale to use"));
453 puts(_(" --create-stdcmap create the standard colormap hint in PseudoColor visuals"));
454 puts(_(" --visual-id visualid visual id of visual to use"));
455 puts(_(" --static do not update or save configurations"));
456 puts(_(" --no-polling do not periodically check for configuration updates"));
457 #ifdef DEBUG
458 puts(_(" --synchronous turn on synchronous display mode"));
459 #endif
460 puts(_(" --version print version and exit"));
461 puts(_(" --help show this message"));
466 void
467 check_defaults()
469 char *path;
471 path = wdefaultspathfordomain("");
472 if (access(path, R_OK)!=0) {
473 #if 0
474 wfatal(_("could not find user GNUstep directory (%s).\n"
475 "Make sure you have installed Window Maker correctly and run wmaker.inst"),
476 path);
477 exit(1);
478 #else
479 wwarning(_("could not find user GNUstep directory (%s)."), path);
481 if (system("wmaker.inst --batch") != 0) {
482 wwarning(_("There was an error while creating GNUstep directory, please "
483 "make sure you have installed Window Maker correctly and run wmaker.inst"));
484 } else {
485 wwarning(_("%s directory created with default configuration."), path);
487 #endif
490 wfree(path);
494 static void
495 execInitScript()
497 char *file;
498 char *paths = wstrconcat(wusergnusteppath(), ":"DEF_CONFIG_PATHS);
500 file = wfindfile(paths, DEF_INIT_SCRIPT);
501 wfree(paths);
503 if (file) {
504 if (system(file) != 0) {
505 wsyserror(_("%s:could not execute initialization script"), file);
507 #if 0
508 if (fork()==0) {
509 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
510 wsyserror(_("%s:could not execute initialization script"), file);
511 exit(1);
513 #endif
514 wfree(file);
519 void
520 ExecExitScript()
522 char *file;
523 char *paths = wstrconcat(wusergnusteppath(), ":"DEF_CONFIG_PATHS);
525 file = wfindfile(paths, DEF_EXIT_SCRIPT);
526 wfree(paths);
528 if (file) {
529 if (system(file) != 0) {
530 wsyserror(_("%s:could not execute exit script"), file);
532 #if 0
533 if (fork()==0) {
534 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
535 wsyserror(_("%s:could not execute exit script"), file);
536 exit(1);
538 #endif
539 wfree(file);
543 #if 0
544 char*
545 getFullPath(char *path)
547 char buffer[1024];
548 char *tmp;
549 char *basep = (char*)buffer;
551 if (*path != '/' && getcwd(buffer, 1023)) {
553 for (;;) {
554 if (strncmp(path, "../", 3)==0) {
555 path += 3;
556 basep = strchr(basep, '/');
557 if (!basep || *path==0)
558 break;
561 if (*path == '/' || strncmp(path, "./",2)==0) {
562 tmp =
566 * path
567 * ./path
568 * ../path
569 * ../../path
573 } else {
574 return wstrconcat(path);
577 return tmp;
579 #endif
582 main(int argc, char **argv)
584 int i, restart=0;
585 char *str, *alt;
586 int d, s;
587 #ifdef DEBUG
588 Bool doSync = False;
589 #endif
590 setlocale(LC_ALL, "");
592 wsetabort(wAbort);
594 /* for telling WPrefs what's the name of the wmaker binary being ran */
596 str = wstrconcat("WMAKER_BIN_NAME=", argv[0]);
597 putenv(str);
599 ArgCount = argc;
600 Arguments = argv;
602 WMInitializeApplication("WindowMaker", &argc, argv);
605 ProgName = strrchr(argv[0],'/');
606 if (!ProgName)
607 ProgName = argv[0];
608 else
609 ProgName++;
612 restart = 0;
614 memset(&wPreferences, 0, sizeof(WPreferences));
616 wPreferences.fallbackWMs = WMCreateArray(8);
617 alt = getenv("WINDOWMAKER_ALT_WM");
618 if (alt != NULL)
619 WMAddToArray(wPreferences.fallbackWMs, wstrdup(alt));
621 WMAddToArray(wPreferences.fallbackWMs, wstrdup(FALLBACK_WINDOWMANAGER));
622 WMAddToArray(wPreferences.fallbackWMs, wstrdup("fvwm"));
623 WMAddToArray(wPreferences.fallbackWMs, wstrdup("twm"));
624 WMAddToArray(wPreferences.fallbackWMs, NULL);
625 WMAddToArray(wPreferences.fallbackWMs, wstrdup("rxvt"));
626 WMAddToArray(wPreferences.fallbackWMs, wstrdup("xterm"));
628 if (argc>1) {
629 for (i=1; i<argc; i++) {
630 #ifdef USECPP
631 if (strcmp(argv[i], "-nocpp")==0
632 || strcmp(argv[i], "--no-cpp")==0) {
633 wPreferences.flags.nocpp=1;
634 } else
635 #endif
636 if (strcmp(argv[i], "-no-autolaunch")==0
637 || strcmp(argv[i], "--no-autolaunch")==0) {
638 wPreferences.flags.noautolaunch = 1;
639 } else if (strcmp(argv[i], "-dont-restore")==0
640 || strcmp(argv[i], "--dont-restore")==0) {
641 wPreferences.flags.norestore = 1;
642 } else if (strcmp(argv[i], "-nodock")==0
643 || strcmp(argv[i], "--no-dock")==0) {
644 wPreferences.flags.nodock=1;
645 } else if (strcmp(argv[i], "-noclip")==0
646 || strcmp(argv[i], "--no-clip")==0) {
647 wPreferences.flags.noclip=1;
648 } else if (strcmp(argv[i], "-version")==0
649 || strcmp(argv[i], "--version")==0) {
650 printf("Window Maker %s\n", VERSION);
651 exit(0);
652 } else if (strcmp(argv[i], "--global_defaults_path")==0) {
653 printf("%s/WindowMaker\n", SYSCONFDIR);
654 exit(0);
655 #ifdef DEBUG
656 } else if (strcmp(argv[i], "--synchronous")==0) {
657 doSync = 1;
658 #endif
659 } else if (strcmp(argv[i], "-locale")==0
660 || strcmp(argv[i], "--locale")==0) {
661 i++;
662 if (i>=argc) {
663 wwarning(_("too few arguments for %s"), argv[i-1]);
664 exit(0);
666 Locale = argv[i];
667 } else if (strcmp(argv[i], "-display")==0
668 || strcmp(argv[i], "--display")==0) {
669 i++;
670 if (i>=argc) {
671 wwarning(_("too few arguments for %s"), argv[i-1]);
672 exit(0);
674 DisplayName = argv[i];
675 } else if (strcmp(argv[i], "-visualid")==0
676 || strcmp(argv[i], "--visual-id")==0) {
677 i++;
678 if (i>=argc) {
679 wwarning(_("too few arguments for %s"), argv[i-1]);
680 exit(0);
682 if (sscanf(argv[i], "%i", &wVisualID)!=1) {
683 wwarning(_("bad value for visualid: \"%s\""), argv[i]);
684 exit(0);
686 } else if (strcmp(argv[i], "-static")==0
687 || strcmp(argv[i], "--static")==0) {
689 wPreferences.flags.noupdates = 1;
690 } else if (strcmp(argv[i], "-nopolling")==0
691 || strcmp(argv[i], "--no-polling")==0) {
693 wPreferences.flags.nopolling = 1;
694 #ifdef XSMP_ENABLED
695 } else if (strcmp(argv[i], "-clientid")==0
696 || strcmp(argv[i], "-restore")==0) {
697 i++;
698 if (i>=argc) {
699 wwarning(_("too few arguments for %s"), argv[i-1]);
700 exit(0);
702 #endif
703 } else if (strcmp(argv[i], "--help")==0) {
704 print_help();
705 exit(0);
706 } else {
707 printf(_("%s: invalid argument '%s'\n"), argv[0], argv[i]);
708 printf(_("Try '%s --help' for more information\n"), argv[0]);
709 exit(1);
714 if (!wPreferences.flags.noupdates) {
715 /* check existence of Defaults DB directory */
716 check_defaults();
720 if (!Locale) {
721 Locale = getenv("LC_ALL");
723 if (!Locale) {
724 Locale = getenv("LANG");
727 setlocale(LC_ALL, "");
729 if (!Locale || strcmp(Locale, "C")==0 || strcmp(Locale, "POSIX")==0)
730 Locale = NULL;
731 #ifdef I18N
732 if (getenv("NLSPATH"))
733 bindtextdomain("WindowMaker", getenv("NLSPATH"));
734 else
735 bindtextdomain("WindowMaker", LOCALEDIR);
736 textdomain("WindowMaker");
738 if (!XSupportsLocale()) {
739 wwarning(_("X server does not support locale"));
742 if (XSetLocaleModifiers("") == NULL) {
743 wwarning(_("cannot set locale modifiers"));
745 #endif
747 if (Locale) {
748 char *ptr;
750 Locale = wstrdup(Locale);
751 ptr = strchr(Locale, '.');
752 if (ptr)
753 *ptr = 0;
756 /* open display */
757 dpy = XOpenDisplay(DisplayName);
758 if (dpy == NULL) {
759 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName));
760 exit(1);
763 if (fcntl(ConnectionNumber(dpy), F_SETFD, FD_CLOEXEC) < 0) {
764 wsyserror("error setting close-on-exec flag for X connection");
765 exit(1);
768 /* check if the user specified a complete display name (with screen).
769 * If so, only manage the specified screen */
770 if (DisplayName)
771 str = strchr(DisplayName, ':');
772 else
773 str = NULL;
775 if (str && sscanf(str, ":%i.%i", &d, &s)==2)
776 multiHead = False;
778 DisplayName = XDisplayName(DisplayName);
780 int len = strlen(DisplayName)+64;
781 str = wmalloc(len);
782 snprintf(str, len, "DISPLAY=%s", DisplayName);
784 putenv(str);
786 #ifdef DEBUG
787 if (doSync)
788 XSynchronize(dpy, True);
789 #endif
791 wXModifierInitialize();
793 #ifdef XSMP_ENABLED
794 wSessionConnectManager(argv, argc);
795 #endif
797 StartUp(!multiHead);
799 if (wScreenCount==1)
800 multiHead = False;
802 execInitScript();
804 EventLoop();
805 return -1;