fixed bugs, re-enabled circulate raise
[wmaker-crm.git] / src / main.c
blob06f14bb7466544bbadc9b8552d3e802dfe9bd91b
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 /* cursors */
119 Cursor wCursor[WCUR_LAST];
121 /* last event timestamp for XSetInputFocus */
122 Time LastTimestamp = CurrentTime;
123 /* timestamp on the last time we did XSetInputFocus() */
124 Time LastFocusChange = CurrentTime;
126 #ifdef SHAPE
127 Bool wShapeSupported;
128 int wShapeEventBase;
129 #endif
131 #ifdef KEEP_XKB_LOCK_STATUS
132 Bool wXkbSupported;
133 int wXkbEventBase;
134 #endif
136 /* special flags */
137 char WProgramSigState = 0;
138 char WProgramState = WSTATE_NORMAL;
139 char WDelayedActionSet = 0;
142 /* temporary stuff */
143 int wVisualID = -1;
146 /* notifications */
147 const char *WMNManaged = "WMNManaged";
148 const char *WMNUnmanaged = "WMNUnmanaged";
149 const char *WMNChangedWorkspace = "WMNChangedWorkspace";
150 const char *WMNChangedState = "WMNChangedState";
151 const char *WMNChangedFocus = "WMNChangedFocus";
152 const char *WMNChangedStacking = "WMNChangedStacking";
153 const char *WMNChangedName = "WMNChangedName";
155 const char *WMNWorkspaceCreated = "WMNWorkspaceCreated";
156 const char *WMNWorkspaceDestroyed = "WMNWorkspaceDestroyed";
157 const char *WMNWorkspaceChanged = "WMNWorkspaceChanged";
158 const char *WMNWorkspaceNameChanged = "WMNWorkspaceNameChanged";
160 const char *WMNResetStacking = "WMNResetStacking";
162 /******** End Global Variables *****/
164 static char *DisplayName = NULL;
166 static char **Arguments;
168 static int ArgCount;
170 extern void EventLoop();
171 extern void StartUp();
172 extern int MonitorLoop(int argc, char **argv);
174 static Bool multiHead = True;
176 /* stdi/o for log shell */
177 static int LogStdIn = -1, LogStdOut = -1, LogStdErr = -1;
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 * StartLogShell
330 * Start a shell that will receive all stdin and stdout from processes
331 * forked by wmaker.
332 *---------------------------------------------------------------------------
334 void
335 StartLogShell(WScreen *scr)
337 int in_fd[2];
338 int out_fd[2];
339 int err_fd[2];
340 pid_t pid;
342 SetupEnvironment(scr);
344 if (pipe(in_fd) < 0) {
345 wsyserror("could not create pipe for log shell\n");
346 return;
348 if (pipe(out_fd) < 0) {
349 wsyserror("could not create pipe for log shell\n");
350 close(in_fd[0]);
351 close(in_fd[1]);
352 return;
354 if (pipe(err_fd) < 0) {
355 wsyserror("could not create pipe for log shell\n");
356 close(out_fd[0]);
357 close(out_fd[1]);
358 close(in_fd[0]);
359 close(in_fd[1]);
360 return;
363 pid = fork();
364 if (pid < 0) {
365 wsyserror("could not fork a new process for log shell\n");
366 return;
367 } else if (pid == 0) {
368 close(in_fd[0]);
369 close(out_fd[1]);
370 close(err_fd[1]);
372 close(0);
373 close(1);
374 close(2);
376 if (dup2(in_fd[1], 0) < 0) {
377 wsyserror("could not redirect stdin for log shell\n");
378 exit(1);
380 if (dup2(out_fd[1], 1) < 0) {
381 wsyserror("could not redirect stdout for log shell\n");
382 exit(1);
384 if (dup2(err_fd[1], 2) < 0) {
385 wsyserror("could not redirect stderr for log shell\n");
386 exit(1);
389 close(in_fd[1]);
390 close(out_fd[1]);
391 close(err_fd[1]);
393 execl("/bin/sh", "/bin/sh", "-c", wPreferences.logger_shell, NULL);
394 wsyserror("could not execute %s\n", wPreferences.logger_shell);
395 exit(1);
396 } else {
397 close(in_fd[1]);
398 close(out_fd[0]);
399 close(err_fd[0]);
401 LogStdIn = in_fd[1];
402 LogStdOut = out_fd[0];
403 LogStdErr = err_fd[0];
409 *---------------------------------------------------------------------
410 * wAbort--
411 * Do a major cleanup and exit the program
413 *----------------------------------------------------------------------
415 void
416 wAbort(Bool dumpCore)
418 int i;
419 WScreen *scr;
421 for (i=0; i<wScreenCount; i++) {
422 scr = wScreenWithNumber(i);
423 if (scr)
424 RestoreDesktop(scr);
426 printf(_("%s aborted.\n"), ProgName);
427 if (dumpCore)
428 abort();
429 else
430 exit(1);
434 void
435 print_help()
437 printf(_("Usage: %s [options]\n"), ProgName);
438 puts(_("The Window Maker window manager for the X window system"));
439 puts("");
440 puts(_(" -display host:dpy display to use"));
441 #ifdef USECPP
442 puts(_(" --no-cpp disable preprocessing of configuration files"));
443 #endif
444 puts(_(" --no-dock do not open the application Dock"));
445 puts(_(" --no-clip do not open the workspace Clip"));
446 puts(_(" --no-autolaunch do not autolaunch applications"));
447 puts(_(" --dont-restore do not restore saved session"));
449 puts(_(" --locale locale locale to use"));
451 puts(_(" --create-stdcmap create the standard colormap hint in PseudoColor visuals"));
452 puts(_(" --visual-id visualid visual id of visual to use"));
453 puts(_(" --static do not update or save configurations"));
454 puts(_(" --no-polling do not periodically check for configuration updates"));
455 #ifdef DEBUG
456 puts(_(" --synchronous turn on synchronous display mode"));
457 #endif
458 puts(_(" --version print version and exit"));
459 puts(_(" --help show this message"));
464 void
465 check_defaults()
467 char *path;
469 path = wdefaultspathfordomain("WindowMaker");
471 if (access(path, R_OK)!=0) {
472 #if 0
473 wfatal(_("could not find user GNUstep directory (%s).\n"
474 "Make sure you have installed Window Maker correctly and run wmaker.inst"),
475 path);
476 exit(1);
477 #else
478 wwarning(_("could not find user GNUstep directory (%s)."), path);
480 if (system("wmaker.inst --batch") != 0) {
481 wwarning(_("There was an error while creating GNUstep directory, please "
482 "make sure you have installed Window Maker correctly and run wmaker.inst"));
483 } else {
484 wwarning(_("%s directory created with default configuration."), path);
486 #endif
489 wfree(path);
493 static void
494 execInitScript()
496 char *file, *paths;
498 paths = wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
499 paths = wstrappend(paths, ":"DEF_CONFIG_PATHS);
501 file = wfindfile(paths, DEF_INIT_SCRIPT);
502 wfree(paths);
504 if (file) {
505 if (system(file) != 0) {
506 wsyserror(_("%s:could not execute initialization script"), file);
508 #if 0
509 if (fork()==0) {
510 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
511 wsyserror(_("%s:could not execute initialization script"), file);
512 exit(1);
514 #endif
515 wfree(file);
520 void
521 ExecExitScript()
523 char *file, *paths;
525 paths = wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
526 paths = wstrappend(paths, ":"DEF_CONFIG_PATHS);
528 file = wfindfile(paths, DEF_EXIT_SCRIPT);
529 wfree(paths);
531 if (file) {
532 if (system(file) != 0) {
533 wsyserror(_("%s:could not execute exit script"), file);
535 #if 0
536 if (fork()==0) {
537 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
538 wsyserror(_("%s:could not execute exit script"), file);
539 exit(1);
541 #endif
542 wfree(file);
546 #if 0
547 char*
548 getFullPath(char *path)
550 char buffer[1024];
551 char *tmp;
552 char *basep = (char*)buffer;
554 if (*path != '/' && getcwd(buffer, 1023)) {
556 for (;;) {
557 if (strncmp(path, "../", 3)==0) {
558 path += 3;
559 basep = strchr(basep, '/');
560 if (!basep || *path==0)
561 break;
564 if (*path == '/' || strncmp(path, "./",2)==0) {
565 tmp =
569 * path
570 * ./path
571 * ../path
572 * ../../path
576 } else {
577 return wstrconcat(path);
580 return tmp;
582 #endif
587 main(int argc, char **argv)
589 int i;
590 int i_am_the_monitor= 1;
592 for (i= 1; i < argc; i++)
594 if (strncmp(argv[i], "--for-real", strlen("--for-real"))==0)
596 i_am_the_monitor= 0;
597 break;
601 if (i_am_the_monitor)
602 return MonitorLoop(argc, argv);
603 else
604 return real_main(argc, argv);
608 static int
609 real_main(int argc, char **argv)
611 int i, restart=0;
612 char *str, *alt;
613 int d, s;
614 int flag;
615 #ifdef DEBUG
616 Bool doSync = False;
617 #endif
618 setlocale(LC_ALL, "");
620 wsetabort(wAbort);
622 /* for telling WPrefs what's the name of the wmaker binary being ran */
624 str = wstrconcat("WMAKER_BIN_NAME=", argv[0]);
625 putenv(str);
627 flag= 0;
628 ArgCount = argc;
629 Arguments = wmalloc(sizeof(char*)*(ArgCount+1));
630 for (i= 0; i < argc; i++)
632 Arguments[i]= argv[i];
634 /* add the extra option to signal that we're just restarting wmaker */
635 Arguments[argc-1]= "--for-real=";
636 Arguments[argc]= NULL;
638 WMInitializeApplication("WindowMaker", &argc, argv);
641 ProgName = strrchr(argv[0],'/');
642 if (!ProgName)
643 ProgName = argv[0];
644 else
645 ProgName++;
648 restart = 0;
650 memset(&wPreferences, 0, sizeof(WPreferences));
652 wPreferences.fallbackWMs = WMCreateArray(8);
653 alt = getenv("WINDOWMAKER_ALT_WM");
654 if (alt != NULL)
655 WMAddToArray(wPreferences.fallbackWMs, wstrdup(alt));
657 WMAddToArray(wPreferences.fallbackWMs, wstrdup(FALLBACK_WINDOWMANAGER));
658 WMAddToArray(wPreferences.fallbackWMs, wstrdup("metacity"));
659 WMAddToArray(wPreferences.fallbackWMs, wstrdup("fvwm"));
660 WMAddToArray(wPreferences.fallbackWMs, wstrdup("twm"));
661 WMAddToArray(wPreferences.fallbackWMs, NULL);
662 WMAddToArray(wPreferences.fallbackWMs, wstrdup("rxvt"));
663 WMAddToArray(wPreferences.fallbackWMs, wstrdup("xterm"));
665 if (argc>1) {
666 for (i=1; i<argc; i++) {
667 #ifdef USECPP
668 if (strcmp(argv[i], "-nocpp")==0
669 || strcmp(argv[i], "--no-cpp")==0) {
670 wPreferences.flags.nocpp=1;
671 } else
672 #endif
673 if (strcmp(argv[i], "--for-real")==0) {
674 wPreferences.flags.restarting = 0;
675 } else if (strcmp(argv[i], "--for-real=")==0) {
676 wPreferences.flags.restarting = 1;
677 } else if (strcmp(argv[i], "--for-real-")==0) {
678 wPreferences.flags.restarting = 2;
679 } else if (strcmp(argv[i], "-no-autolaunch")==0
680 || strcmp(argv[i], "--no-autolaunch")==0) {
681 wPreferences.flags.noautolaunch = 1;
682 } else if (strcmp(argv[i], "-dont-restore")==0
683 || strcmp(argv[i], "--dont-restore")==0) {
684 wPreferences.flags.norestore = 1;
685 } else if (strcmp(argv[i], "-nodock")==0
686 || strcmp(argv[i], "--no-dock")==0) {
687 wPreferences.flags.nodock=1;
688 } else if (strcmp(argv[i], "-noclip")==0
689 || strcmp(argv[i], "--no-clip")==0) {
690 wPreferences.flags.noclip=1;
691 } else if (strcmp(argv[i], "-version")==0
692 || strcmp(argv[i], "--version")==0) {
693 printf("Window Maker %s\n", VERSION);
694 exit(0);
695 } else if (strcmp(argv[i], "--global_defaults_path")==0) {
696 printf("%s/WindowMaker\n", SYSCONFDIR);
697 exit(0);
698 #ifdef DEBUG
699 } else if (strcmp(argv[i], "--synchronous")==0) {
700 doSync = 1;
701 #endif
702 } else if (strcmp(argv[i], "-locale")==0
703 || strcmp(argv[i], "--locale")==0) {
704 i++;
705 if (i>=argc) {
706 wwarning(_("too few arguments for %s"), argv[i-1]);
707 exit(0);
709 Locale = argv[i];
710 } else if (strcmp(argv[i], "-display")==0
711 || strcmp(argv[i], "--display")==0) {
712 i++;
713 if (i>=argc) {
714 wwarning(_("too few arguments for %s"), argv[i-1]);
715 exit(0);
717 DisplayName = argv[i];
718 } else if (strcmp(argv[i], "-visualid")==0
719 || strcmp(argv[i], "--visual-id")==0) {
720 i++;
721 if (i>=argc) {
722 wwarning(_("too few arguments for %s"), argv[i-1]);
723 exit(0);
725 if (sscanf(argv[i], "%i", &wVisualID)!=1) {
726 wwarning(_("bad value for visualid: \"%s\""), argv[i]);
727 exit(0);
729 } else if (strcmp(argv[i], "-static")==0
730 || strcmp(argv[i], "--static")==0) {
732 wPreferences.flags.noupdates = 1;
733 } else if (strcmp(argv[i], "-nopolling")==0
734 || strcmp(argv[i], "--no-polling")==0) {
736 wPreferences.flags.nopolling = 1;
737 #ifdef XSMP_ENABLED
738 } else if (strcmp(argv[i], "-clientid")==0
739 || strcmp(argv[i], "-restore")==0) {
740 i++;
741 if (i>=argc) {
742 wwarning(_("too few arguments for %s"), argv[i-1]);
743 exit(0);
745 #endif
746 } else if (strcmp(argv[i], "--help")==0) {
747 print_help();
748 exit(0);
749 } else {
750 printf(_("%s: invalid argument '%s'\n"), argv[0], argv[i]);
751 printf(_("Try '%s --help' for more information\n"), argv[0]);
752 exit(1);
757 if (!wPreferences.flags.noupdates) {
758 /* check existence of Defaults DB directory */
759 check_defaults();
763 if (Locale) {
764 /* return of wstrconcat should not be free-ed! read putenv man page */
765 putenv(wstrconcat("LANG=", Locale));
766 } else {
767 Locale = getenv("LC_ALL");
768 if (!Locale) {
769 Locale = getenv("LANG");
773 setlocale(LC_ALL, "");
775 if (!Locale || strcmp(Locale, "C")==0 || strcmp(Locale, "POSIX")==0)
776 Locale = NULL;
777 #ifdef I18N
778 if (getenv("NLSPATH"))
779 bindtextdomain("WindowMaker", getenv("NLSPATH"));
780 else
781 bindtextdomain("WindowMaker", LOCALEDIR);
782 bind_textdomain_codeset("WindowMaker", "UTF-8");
783 textdomain("WindowMaker");
785 if (!XSupportsLocale()) {
786 wwarning(_("X server does not support locale"));
789 if (XSetLocaleModifiers("") == NULL) {
790 wwarning(_("cannot set locale modifiers"));
792 #endif
794 if (Locale) {
795 char *ptr;
797 Locale = wstrdup(Locale);
798 ptr = strchr(Locale, '.');
799 if (ptr)
800 *ptr = 0;
803 /* open display */
804 dpy = XOpenDisplay(DisplayName);
805 if (dpy == NULL) {
806 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName));
807 exit(1);
810 if (fcntl(ConnectionNumber(dpy), F_SETFD, FD_CLOEXEC) < 0) {
811 wsyserror("error setting close-on-exec flag for X connection");
812 exit(1);
815 /* check if the user specified a complete display name (with screen).
816 * If so, only manage the specified screen */
817 if (DisplayName)
818 str = strchr(DisplayName, ':');
819 else
820 str = NULL;
822 if (str && sscanf(str, ":%i.%i", &d, &s)==2)
823 multiHead = False;
825 DisplayName = XDisplayName(DisplayName);
827 int len = strlen(DisplayName)+64;
828 str = wmalloc(len);
829 snprintf(str, len, "DISPLAY=%s", DisplayName);
831 putenv(str);
833 #ifdef DEBUG
834 if (doSync)
835 XSynchronize(dpy, True);
836 #endif
838 wXModifierInitialize();
840 #ifdef XSMP_ENABLED
841 wSessionConnectManager(argv, argc);
842 #endif
844 StartUp(!multiHead);
846 if (wScreenCount==1)
847 multiHead = False;
849 execInitScript();
851 EventLoop();
852 return -1;