changed indentation to use spaces only
[wmaker-crm.git] / src / main.c
blob938d2bb0cb61c6463067e7fc1a77cec5013b17d5
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 "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;
88 XContext wVEdgeContext;
90 /* Atoms */
91 Atom _XA_WM_STATE;
92 Atom _XA_WM_CHANGE_STATE;
93 Atom _XA_WM_PROTOCOLS;
94 Atom _XA_WM_TAKE_FOCUS;
95 Atom _XA_WM_DELETE_WINDOW;
96 Atom _XA_WM_SAVE_YOURSELF;
97 Atom _XA_WM_CLIENT_LEADER;
98 Atom _XA_WM_COLORMAP_WINDOWS;
99 Atom _XA_WM_COLORMAP_NOTIFY;
101 Atom _XA_GNUSTEP_WM_ATTR;
102 Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
103 Atom _XA_GNUSTEP_WM_RESIZEBAR;
104 Atom _XA_GNUSTEP_TITLEBAR_STATE;
106 Atom _XA_WINDOWMAKER_MENU;
107 Atom _XA_WINDOWMAKER_WM_PROTOCOLS;
108 Atom _XA_WINDOWMAKER_STATE;
110 Atom _XA_WINDOWMAKER_WM_FUNCTION;
111 Atom _XA_WINDOWMAKER_NOTICEBOARD;
112 Atom _XA_WINDOWMAKER_COMMAND;
114 Atom _XA_WINDOWMAKER_ICON_SIZE;
115 Atom _XA_WINDOWMAKER_ICON_TILE;
118 #ifdef OFFIX_DND
119 Atom _XA_DND_PROTOCOL;
120 Atom _XA_DND_SELECTION;
121 #endif
123 /* cursors */
124 Cursor wCursor[WCUR_LAST];
126 /* last event timestamp for XSetInputFocus */
127 Time LastTimestamp = CurrentTime;
128 /* timestamp on the last time we did XSetInputFocus() */
129 Time LastFocusChange = CurrentTime;
131 #ifdef SHAPE
132 Bool wShapeSupported;
133 int wShapeEventBase;
134 #endif
136 #ifdef KEEP_XKB_LOCK_STATUS
137 Bool wXkbSupported;
138 int wXkbEventBase;
139 #endif
141 /* special flags */
142 char WProgramSigState = 0;
143 char WProgramState = WSTATE_NORMAL;
144 char WDelayedActionSet = 0;
147 /* temporary stuff */
148 int wVisualID = -1;
152 /* notifications */
153 const char *WMNManaged = "WMNManaged";
154 const char *WMNUnmanaged = "WMNUnmanaged";
155 const char *WMNChangedWorkspace = "WMNChangedWorkspace";
156 const char *WMNChangedState = "WMNChangedState";
157 const char *WMNChangedFocus = "WMNChangedFocus";
158 const char *WMNChangedStacking = "WMNChangedStacking";
159 const char *WMNChangedName = "WMNChangedName";
161 const char *WMNWorkspaceCreated = "WMNWorkspaceCreated";
162 const char *WMNWorkspaceDestroyed = "WMNWorkspaceDestroyed";
163 const char *WMNWorkspaceChanged = "WMNWorkspaceChanged";
164 const char *WMNWorkspaceNameChanged = "WMNWorkspaceNameChanged";
166 const char *WMNResetStacking = "WMNResetStacking";
168 /******** End Global Variables *****/
170 static char *DisplayName = NULL;
172 static char **Arguments;
174 static int ArgCount;
176 extern void EventLoop();
177 extern void StartUp();
179 static Bool multiHead = True;
181 /* stdi/o for log shell */
182 static int LogStdIn = -1, LogStdOut = -1, LogStdErr = -1;
185 void
186 Exit(int status)
188 #ifdef XSMP_ENABLED
189 wSessionDisconnectManager();
190 #endif
191 if (dpy)
192 XCloseDisplay(dpy);
194 exit(status);
198 void
199 Restart(char *manager, Bool abortOnFailure)
201 char *prog=NULL;
202 char *argv[MAX_RESTART_ARGS];
203 int i;
205 if (manager && manager[0]!=0) {
206 prog = argv[0] = strtok(manager, " ");
207 for (i=1; i<MAX_RESTART_ARGS; i++) {
208 argv[i]=strtok(NULL, " ");
209 if (argv[i]==NULL) {
210 break;
214 if (dpy) {
215 #ifdef XSMP_ENABLED
216 wSessionDisconnectManager();
217 #endif
218 XCloseDisplay(dpy);
219 dpy = NULL;
221 if (!prog) {
222 execvp(Arguments[0], Arguments);
223 wfatal(_("failed to restart Window Maker."));
224 } else {
225 execvp(prog, argv);
226 wsyserror(_("could not exec %s"), prog);
228 if (abortOnFailure)
229 exit(-1);
234 void
235 SetupEnvironment(WScreen *scr)
237 char *tmp, *ptr;
238 char buf[16];
240 if (multiHead) {
241 int len = strlen(DisplayName)+64;
242 tmp = wmalloc(len);
243 snprintf(tmp, len, "DISPLAY=%s", XDisplayName(DisplayName));
244 ptr = strchr(strchr(tmp, ':'), '.');
245 if (ptr)
246 *ptr = 0;
247 snprintf(buf, sizeof(buf), ".%i", scr->screen);
248 strcat(tmp, buf);
249 putenv(tmp);
251 tmp = wmalloc(60);
252 snprintf(tmp, 60, "WRASTER_COLOR_RESOLUTION%i=%i", scr->screen,
253 scr->rcontext->attribs->colors_per_channel);
254 putenv(tmp);
260 typedef struct {
261 WScreen *scr;
262 char *command;
263 } _tuple;
266 static void
267 shellCommandHandler(pid_t pid, unsigned char status, _tuple *data)
269 if (status == 127) {
270 char *buffer;
272 buffer = wstrconcat(_("Could not execute command: "), data->command);
274 wMessageDialog(data->scr, _("Error"), buffer, _("OK"), NULL, NULL);
275 wfree(buffer);
276 } else if (status != 127) {
278 printf("%s: %i\n", data->command, status);
282 wfree(data->command);
283 wfree(data);
287 void
288 ExecuteShellCommand(WScreen *scr, char *command)
290 static char *shell = NULL;
291 pid_t pid;
294 * This have a problem: if the shell is tcsh (not sure about others)
295 * and ~/.tcshrc have /bin/stty erase ^H somewhere on it, the shell
296 * will block and the command will not be executed.
297 if (!shell) {
298 shell = getenv("SHELL");
299 if (!shell)
300 shell = "/bin/sh";
303 shell = "/bin/sh";
305 pid = fork();
307 if (pid==0) {
309 SetupEnvironment(scr);
311 #ifdef HAVE_SETSID
312 setsid();
313 #endif
314 execl(shell, shell, "-c", command, NULL);
315 wsyserror("could not execute %s -c %s", shell, command);
316 Exit(-1);
317 } else if (pid < 0) {
318 wsyserror("cannot fork a new process");
319 } else {
320 _tuple *data = wmalloc(sizeof(_tuple));
322 data->scr = scr;
323 data->command = wstrdup(command);
325 wAddDeathHandler(pid, (WDeathHandler*)shellCommandHandler, data);
331 *---------------------------------------------------------------------------
332 * StartLogShell
333 * Start a shell that will receive all stdin and stdout from processes
334 * forked by wmaker.
335 *---------------------------------------------------------------------------
337 void
338 StartLogShell(WScreen *scr)
340 int in_fd[2];
341 int out_fd[2];
342 int err_fd[2];
343 pid_t pid;
345 SetupEnvironment(scr);
347 if (pipe(in_fd) < 0) {
348 wsyserror("could not create pipe for log shell\n");
349 return;
351 if (pipe(out_fd) < 0) {
352 wsyserror("could not create pipe for log shell\n");
353 close(in_fd[0]);
354 close(in_fd[1]);
355 return;
357 if (pipe(err_fd) < 0) {
358 wsyserror("could not create pipe for log shell\n");
359 close(out_fd[0]);
360 close(out_fd[1]);
361 close(in_fd[0]);
362 close(in_fd[1]);
363 return;
366 pid = fork();
367 if (pid < 0) {
368 wsyserror("could not fork a new process for log shell\n");
369 return;
370 } else if (pid == 0) {
371 close(in_fd[0]);
372 close(out_fd[1]);
373 close(err_fd[1]);
375 close(0);
376 close(1);
377 close(2);
379 if (dup2(in_fd[1], 0) < 0) {
380 wsyserror("could not redirect stdin for log shell\n");
381 exit(1);
383 if (dup2(out_fd[1], 1) < 0) {
384 wsyserror("could not redirect stdout for log shell\n");
385 exit(1);
387 if (dup2(err_fd[1], 2) < 0) {
388 wsyserror("could not redirect stderr for log shell\n");
389 exit(1);
392 close(in_fd[1]);
393 close(out_fd[1]);
394 close(err_fd[1]);
396 execl("/bin/sh", "/bin/sh", "-c", wPreferences.logger_shell, NULL);
397 wsyserror("could not execute %s\n", wPreferences.logger_shell);
398 exit(1);
399 } else {
400 close(in_fd[1]);
401 close(out_fd[0]);
402 close(err_fd[0]);
404 LogStdIn = in_fd[1];
405 LogStdOut = out_fd[0];
406 LogStdErr = err_fd[0];
412 *---------------------------------------------------------------------
413 * wAbort--
414 * Do a major cleanup and exit the program
416 *----------------------------------------------------------------------
418 void
419 wAbort(Bool dumpCore)
421 int i;
422 WScreen *scr;
424 for (i=0; i<wScreenCount; i++) {
425 scr = wScreenWithNumber(i);
426 if (scr)
427 RestoreDesktop(scr);
429 printf(_("%s aborted.\n"), ProgName);
430 if (dumpCore)
431 abort();
432 else
433 exit(1);
437 void
438 print_help()
440 printf(_("Usage: %s [options]\n"), ProgName);
441 puts(_("The Window Maker window manager for the X window system"));
442 puts("");
443 puts(_(" -display host:dpy display to use"));
444 #ifdef USECPP
445 puts(_(" --no-cpp disable preprocessing of configuration files"));
446 #endif
447 puts(_(" --no-dock do not open the application Dock"));
448 puts(_(" --no-clip do not open the workspace Clip"));
449 puts(_(" --no-autolaunch do not autolaunch applications"));
450 puts(_(" --dont-restore do not restore saved session"));
452 puts(_(" --locale locale locale to use"));
454 puts(_(" --create-stdcmap create the standard colormap hint in PseudoColor visuals"));
455 puts(_(" --visual-id visualid visual id of visual to use"));
456 puts(_(" --static do not update or save configurations"));
457 puts(_(" --no-polling do not periodically check for configuration updates"));
458 #ifdef DEBUG
459 puts(_(" --synchronous turn on synchronous display mode"));
460 #endif
461 puts(_(" --version print version and exit"));
462 puts(_(" --help show this message"));
467 void
468 check_defaults()
470 char *path;
472 path = wdefaultspathfordomain("");
473 if (access(path, R_OK)!=0) {
474 #if 0
475 wfatal(_("could not find user GNUstep directory (%s).\n"
476 "Make sure you have installed Window Maker correctly and run wmaker.inst"),
477 path);
478 exit(1);
479 #else
480 wwarning(_("could not find user GNUstep directory (%s)."), path);
482 if (system("wmaker.inst --batch") != 0) {
483 wwarning(_("There was an error while creating GNUstep directory, please "
484 "make sure you have installed Window Maker correctly and run wmaker.inst"));
485 } else {
486 wwarning(_("%s directory created with default configuration."), path);
488 #endif
491 wfree(path);
495 static void
496 execInitScript()
498 char *file, *paths;
500 paths = wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
501 paths = wstrappend(paths, ":"DEF_CONFIG_PATHS);
503 file = wfindfile(paths, DEF_INIT_SCRIPT);
504 wfree(paths);
506 if (file) {
507 if (system(file) != 0) {
508 wsyserror(_("%s:could not execute initialization script"), file);
510 #if 0
511 if (fork()==0) {
512 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
513 wsyserror(_("%s:could not execute initialization script"), file);
514 exit(1);
516 #endif
517 wfree(file);
522 void
523 ExecExitScript()
525 char *file, *paths;
527 paths = wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
528 paths = wstrappend(paths, ":"DEF_CONFIG_PATHS);
530 file = wfindfile(paths, DEF_EXIT_SCRIPT);
531 wfree(paths);
533 if (file) {
534 if (system(file) != 0) {
535 wsyserror(_("%s:could not execute exit script"), file);
537 #if 0
538 if (fork()==0) {
539 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
540 wsyserror(_("%s:could not execute exit script"), file);
541 exit(1);
543 #endif
544 wfree(file);
548 #if 0
549 char*
550 getFullPath(char *path)
552 char buffer[1024];
553 char *tmp;
554 char *basep = (char*)buffer;
556 if (*path != '/' && getcwd(buffer, 1023)) {
558 for (;;) {
559 if (strncmp(path, "../", 3)==0) {
560 path += 3;
561 basep = strchr(basep, '/');
562 if (!basep || *path==0)
563 break;
566 if (*path == '/' || strncmp(path, "./",2)==0) {
567 tmp =
571 * path
572 * ./path
573 * ../path
574 * ../../path
578 } else {
579 return wstrconcat(path);
582 return tmp;
584 #endif
587 main(int argc, char **argv)
589 int i, restart=0;
590 char *str, *alt;
591 int d, s;
592 #ifdef DEBUG
593 Bool doSync = False;
594 #endif
595 setlocale(LC_ALL, "");
597 wsetabort(wAbort);
599 /* for telling WPrefs what's the name of the wmaker binary being ran */
601 str = wstrconcat("WMAKER_BIN_NAME=", argv[0]);
602 putenv(str);
604 ArgCount = argc;
605 Arguments = argv;
607 WMInitializeApplication("WindowMaker", &argc, argv);
610 ProgName = strrchr(argv[0],'/');
611 if (!ProgName)
612 ProgName = argv[0];
613 else
614 ProgName++;
617 restart = 0;
619 memset(&wPreferences, 0, sizeof(WPreferences));
621 wPreferences.fallbackWMs = WMCreateArray(8);
622 alt = getenv("WINDOWMAKER_ALT_WM");
623 if (alt != NULL)
624 WMAddToArray(wPreferences.fallbackWMs, wstrdup(alt));
626 WMAddToArray(wPreferences.fallbackWMs, wstrdup(FALLBACK_WINDOWMANAGER));
627 WMAddToArray(wPreferences.fallbackWMs, wstrdup("fvwm"));
628 WMAddToArray(wPreferences.fallbackWMs, wstrdup("twm"));
629 WMAddToArray(wPreferences.fallbackWMs, NULL);
630 WMAddToArray(wPreferences.fallbackWMs, wstrdup("rxvt"));
631 WMAddToArray(wPreferences.fallbackWMs, wstrdup("xterm"));
633 if (argc>1) {
634 for (i=1; i<argc; i++) {
635 #ifdef USECPP
636 if (strcmp(argv[i], "-nocpp")==0
637 || strcmp(argv[i], "--no-cpp")==0) {
638 wPreferences.flags.nocpp=1;
639 } else
640 #endif
641 if (strcmp(argv[i], "-no-autolaunch")==0
642 || strcmp(argv[i], "--no-autolaunch")==0) {
643 wPreferences.flags.noautolaunch = 1;
644 } else if (strcmp(argv[i], "-dont-restore")==0
645 || strcmp(argv[i], "--dont-restore")==0) {
646 wPreferences.flags.norestore = 1;
647 } else if (strcmp(argv[i], "-nodock")==0
648 || strcmp(argv[i], "--no-dock")==0) {
649 wPreferences.flags.nodock=1;
650 } else if (strcmp(argv[i], "-noclip")==0
651 || strcmp(argv[i], "--no-clip")==0) {
652 wPreferences.flags.noclip=1;
653 } else if (strcmp(argv[i], "-version")==0
654 || strcmp(argv[i], "--version")==0) {
655 printf("Window Maker %s\n", VERSION);
656 exit(0);
657 } else if (strcmp(argv[i], "--global_defaults_path")==0) {
658 printf("%s/WindowMaker\n", SYSCONFDIR);
659 exit(0);
660 #ifdef DEBUG
661 } else if (strcmp(argv[i], "--synchronous")==0) {
662 doSync = 1;
663 #endif
664 } else if (strcmp(argv[i], "-locale")==0
665 || strcmp(argv[i], "--locale")==0) {
666 i++;
667 if (i>=argc) {
668 wwarning(_("too few arguments for %s"), argv[i-1]);
669 exit(0);
671 Locale = argv[i];
672 } else if (strcmp(argv[i], "-display")==0
673 || strcmp(argv[i], "--display")==0) {
674 i++;
675 if (i>=argc) {
676 wwarning(_("too few arguments for %s"), argv[i-1]);
677 exit(0);
679 DisplayName = argv[i];
680 } else if (strcmp(argv[i], "-visualid")==0
681 || strcmp(argv[i], "--visual-id")==0) {
682 i++;
683 if (i>=argc) {
684 wwarning(_("too few arguments for %s"), argv[i-1]);
685 exit(0);
687 if (sscanf(argv[i], "%i", &wVisualID)!=1) {
688 wwarning(_("bad value for visualid: \"%s\""), argv[i]);
689 exit(0);
691 } else if (strcmp(argv[i], "-static")==0
692 || strcmp(argv[i], "--static")==0) {
694 wPreferences.flags.noupdates = 1;
695 } else if (strcmp(argv[i], "-nopolling")==0
696 || strcmp(argv[i], "--no-polling")==0) {
698 wPreferences.flags.nopolling = 1;
699 #ifdef XSMP_ENABLED
700 } else if (strcmp(argv[i], "-clientid")==0
701 || strcmp(argv[i], "-restore")==0) {
702 i++;
703 if (i>=argc) {
704 wwarning(_("too few arguments for %s"), argv[i-1]);
705 exit(0);
707 #endif
708 } else if (strcmp(argv[i], "--help")==0) {
709 print_help();
710 exit(0);
711 } else {
712 printf(_("%s: invalid argument '%s'\n"), argv[0], argv[i]);
713 printf(_("Try '%s --help' for more information\n"), argv[0]);
714 exit(1);
719 if (!wPreferences.flags.noupdates) {
720 /* check existence of Defaults DB directory */
721 check_defaults();
725 if (Locale) {
726 /* return of wstrconcat should not be free-ed! read putenv man page */
727 putenv(wstrconcat("LANG=", Locale));
728 } else {
729 Locale = getenv("LC_ALL");
730 if (!Locale) {
731 Locale = getenv("LANG");
735 setlocale(LC_ALL, "");
737 if (!Locale || strcmp(Locale, "C")==0 || strcmp(Locale, "POSIX")==0)
738 Locale = NULL;
739 #ifdef I18N
740 if (getenv("NLSPATH"))
741 bindtextdomain("WindowMaker", getenv("NLSPATH"));
742 else
743 bindtextdomain("WindowMaker", LOCALEDIR);
744 textdomain("WindowMaker");
746 if (!XSupportsLocale()) {
747 wwarning(_("X server does not support locale"));
750 if (XSetLocaleModifiers("") == NULL) {
751 wwarning(_("cannot set locale modifiers"));
753 #endif
755 if (Locale) {
756 char *ptr;
758 Locale = wstrdup(Locale);
759 ptr = strchr(Locale, '.');
760 if (ptr)
761 *ptr = 0;
764 /* open display */
765 dpy = XOpenDisplay(DisplayName);
766 if (dpy == NULL) {
767 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName));
768 exit(1);
771 if (fcntl(ConnectionNumber(dpy), F_SETFD, FD_CLOEXEC) < 0) {
772 wsyserror("error setting close-on-exec flag for X connection");
773 exit(1);
776 /* check if the user specified a complete display name (with screen).
777 * If so, only manage the specified screen */
778 if (DisplayName)
779 str = strchr(DisplayName, ':');
780 else
781 str = NULL;
783 if (str && sscanf(str, ":%i.%i", &d, &s)==2)
784 multiHead = False;
786 DisplayName = XDisplayName(DisplayName);
788 int len = strlen(DisplayName)+64;
789 str = wmalloc(len);
790 snprintf(str, len, "DISPLAY=%s", DisplayName);
792 putenv(str);
794 #ifdef DEBUG
795 if (doSync)
796 XSynchronize(dpy, True);
797 #endif
799 wXModifierInitialize();
801 #ifdef XSMP_ENABLED
802 wSessionConnectManager(argv, argc);
803 #endif
805 StartUp(!multiHead);
807 if (wScreenCount==1)
808 multiHead = False;
810 execInitScript();
812 EventLoop();
813 return -1;