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,
22 #include <sys/inotify.h>
31 #include <sys/types.h>
35 #include <X11/Xutil.h>
38 /* Xlocale.h and locale.h are the same if X_LOCALE is undefind in wconfig.h,
39 * and if X_LOCALE is defined, X's locale emulating functions will be used.
40 * See Xlocale.h for more information.
42 #include <X11/Xlocale.h>
46 #include "WindowMaker.h"
51 #include "xmodifier.h"
56 #include <WINGs/WUtil.h>
58 /****** Global Variables ******/
66 unsigned int ValidModMask
= 0xff;
70 /* locale to use. NULL==POSIX or C */
75 WPreferences wPreferences
;
78 WMPropList
*wDomainName
;
79 WMPropList
*wAttributeDomainName
;
81 WShortKey wKeyBindings
[WKBD_LAST
];
83 /* defaults domains */
84 WDDomain
*WDWindowMaker
= NULL
;
85 WDDomain
*WDWindowAttributes
= NULL
;
86 WDDomain
*WDRootMenu
= NULL
;
90 XContext wAppWinContext
;
91 XContext wStackContext
;
92 XContext wVEdgeContext
;
96 Atom _XA_WM_CHANGE_STATE
;
97 Atom _XA_WM_PROTOCOLS
;
98 Atom _XA_WM_TAKE_FOCUS
;
99 Atom _XA_WM_DELETE_WINDOW
;
100 Atom _XA_WM_SAVE_YOURSELF
;
101 Atom _XA_WM_CLIENT_LEADER
;
102 Atom _XA_WM_COLORMAP_WINDOWS
;
103 Atom _XA_WM_COLORMAP_NOTIFY
;
105 Atom _XA_GNUSTEP_WM_ATTR
;
106 Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW
;
107 Atom _XA_GNUSTEP_WM_RESIZEBAR
;
108 Atom _XA_GNUSTEP_TITLEBAR_STATE
;
110 Atom _XA_WINDOWMAKER_MENU
;
111 Atom _XA_WINDOWMAKER_WM_PROTOCOLS
;
112 Atom _XA_WINDOWMAKER_STATE
;
114 Atom _XA_WINDOWMAKER_WM_FUNCTION
;
115 Atom _XA_WINDOWMAKER_NOTICEBOARD
;
116 Atom _XA_WINDOWMAKER_COMMAND
;
118 Atom _XA_WINDOWMAKER_ICON_SIZE
;
119 Atom _XA_WINDOWMAKER_ICON_TILE
;
123 Cursor wCursor
[WCUR_LAST
];
125 /* last event timestamp for XSetInputFocus */
126 Time LastTimestamp
= CurrentTime
;
127 /* timestamp on the last time we did XSetInputFocus() */
128 Time LastFocusChange
= CurrentTime
;
131 Bool wShapeSupported
;
135 #ifdef KEEP_XKB_LOCK_STATUS
141 char WProgramSigState
= 0;
142 char WProgramState
= WSTATE_NORMAL
;
143 char WDelayedActionSet
= 0;
146 /* temporary stuff */
151 const char *WMNManaged
= "WMNManaged";
152 const char *WMNUnmanaged
= "WMNUnmanaged";
153 const char *WMNChangedWorkspace
= "WMNChangedWorkspace";
154 const char *WMNChangedState
= "WMNChangedState";
155 const char *WMNChangedFocus
= "WMNChangedFocus";
156 const char *WMNChangedStacking
= "WMNChangedStacking";
157 const char *WMNChangedName
= "WMNChangedName";
159 const char *WMNWorkspaceCreated
= "WMNWorkspaceCreated";
160 const char *WMNWorkspaceDestroyed
= "WMNWorkspaceDestroyed";
161 const char *WMNWorkspaceChanged
= "WMNWorkspaceChanged";
162 const char *WMNWorkspaceNameChanged
= "WMNWorkspaceNameChanged";
164 const char *WMNResetStacking
= "WMNResetStacking";
166 /******** End Global Variables *****/
168 static char *DisplayName
= NULL
;
170 static char **Arguments
;
174 extern void EventLoop();
175 extern void StartUp();
176 extern int MonitorLoop(int argc
, char **argv
);
178 static Bool multiHead
= True
;
180 /* stdi/o for log shell */
181 static int LogStdIn
= -1, LogStdOut
= -1, LogStdErr
= -1;
184 static int real_main(int argc
, char **argv
);
190 wSessionDisconnectManager();
200 Restart(char *manager
, Bool abortOnFailure
)
203 char *argv
[MAX_RESTART_ARGS
];
206 if (manager
&& manager
[0]!=0) {
207 prog
= argv
[0] = strtok(manager
, " ");
208 for (i
=1; i
<MAX_RESTART_ARGS
; i
++) {
209 argv
[i
]=strtok(NULL
, " ");
217 wSessionDisconnectManager();
223 execvp(Arguments
[0], Arguments
);
224 wfatal(_("failed to restart Window Maker."));
227 wsyserror(_("could not exec %s"), prog
);
236 SetupEnvironment(WScreen
*scr
)
242 int len
= strlen(DisplayName
)+64;
244 snprintf(tmp
, len
, "DISPLAY=%s", XDisplayName(DisplayName
));
245 ptr
= strchr(strchr(tmp
, ':'), '.');
248 snprintf(buf
, sizeof(buf
), ".%i", scr
->screen
);
253 snprintf(tmp
, 60, "WRASTER_COLOR_RESOLUTION%i=%i", scr
->screen
,
254 scr
->rcontext
->attribs
->colors_per_channel
);
268 shellCommandHandler(pid_t pid
, unsigned char status
, _tuple
*data
)
273 buffer
= wstrconcat(_("Could not execute command: "), data
->command
);
275 wMessageDialog(data
->scr
, _("Error"), buffer
, _("OK"), NULL
, NULL
);
277 } else if (status
!= 127) {
279 printf("%s: %i\n", data->command, status);
283 wfree(data
->command
);
289 ExecuteShellCommand(WScreen
*scr
, char *command
)
291 static char *shell
= NULL
;
295 * This have a problem: if the shell is tcsh (not sure about others)
296 * and ~/.tcshrc have /bin/stty erase ^H somewhere on it, the shell
297 * will block and the command will not be executed.
299 shell = getenv("SHELL");
310 SetupEnvironment(scr
);
315 execl(shell
, shell
, "-c", command
, NULL
);
316 wsyserror("could not execute %s -c %s", shell
, command
);
318 } else if (pid
< 0) {
319 wsyserror("cannot fork a new process");
321 _tuple
*data
= wmalloc(sizeof(_tuple
));
324 data
->command
= wstrdup(command
);
326 wAddDeathHandler(pid
, (WDeathHandler
*)shellCommandHandler
, data
);
332 *---------------------------------------------------------------------------
334 * Start a shell that will receive all stdin and stdout from processes
336 *---------------------------------------------------------------------------
339 StartLogShell(WScreen
*scr
)
346 SetupEnvironment(scr
);
348 if (pipe(in_fd
) < 0) {
349 wsyserror("could not create pipe for log shell\n");
352 if (pipe(out_fd
) < 0) {
353 wsyserror("could not create pipe for log shell\n");
358 if (pipe(err_fd
) < 0) {
359 wsyserror("could not create pipe for log shell\n");
369 wsyserror("could not fork a new process for log shell\n");
371 } else if (pid
== 0) {
380 if (dup2(in_fd
[1], 0) < 0) {
381 wsyserror("could not redirect stdin for log shell\n");
384 if (dup2(out_fd
[1], 1) < 0) {
385 wsyserror("could not redirect stdout for log shell\n");
388 if (dup2(err_fd
[1], 2) < 0) {
389 wsyserror("could not redirect stderr for log shell\n");
397 execl("/bin/sh", "/bin/sh", "-c", wPreferences
.logger_shell
, NULL
);
398 wsyserror("could not execute %s\n", wPreferences
.logger_shell
);
406 LogStdOut
= out_fd
[0];
407 LogStdErr
= err_fd
[0];
413 *---------------------------------------------------------------------
415 * Do a major cleanup and exit the program
417 *----------------------------------------------------------------------
420 wAbort(Bool dumpCore
)
425 for (i
=0; i
<wScreenCount
; i
++) {
426 scr
= wScreenWithNumber(i
);
430 printf(_("%s aborted.\n"), ProgName
);
441 printf(_("Usage: %s [options]\n"), ProgName
);
442 puts(_("The Window Maker window manager for the X window system"));
444 puts(_(" -display host:dpy display to use"));
446 puts(_(" --no-cpp disable preprocessing of configuration files"));
448 puts(_(" --no-dock do not open the application Dock"));
449 puts(_(" --no-clip do not open the workspace Clip"));
450 puts(_(" --no-autolaunch do not autolaunch applications"));
451 puts(_(" --dont-restore do not restore saved session"));
453 puts(_(" --locale locale locale to use"));
455 puts(_(" --create-stdcmap create the standard colormap hint in PseudoColor visuals"));
456 puts(_(" --visual-id visualid visual id of visual to use"));
457 puts(_(" --static do not update or save configurations"));
459 puts(_(" --synchronous turn on synchronous display mode"));
461 puts(_(" --version print version and exit"));
462 puts(_(" --help show this message"));
472 path
= wdefaultspathfordomain("WindowMaker");
474 if (access(path
, R_OK
)!=0) {
476 wfatal(_("could not find user GNUstep directory (%s).\n"
477 "Make sure you have installed Window Maker correctly and run wmaker.inst"),
481 wwarning(_("could not find user GNUstep directory (%s)."), path
);
483 if (system("wmaker.inst --batch") != 0) {
484 wwarning(_("There was an error while creating GNUstep directory, please "
485 "make sure you have installed Window Maker correctly and run wmaker.inst"));
487 wwarning(_("%s directory created with default configuration."), path
);
497 * Add watch here, used to notify if configuration
498 * files have changed, using linux kernel inotify mechanism
505 inotifyFD
= inotify_init(); /* Initialise an inotify instance */
507 wwarning(_("could not initialise an inotify instance."
508 " Changes to the defaults database will require"
509 " a restart to take effect. Check your kernel!"));
511 watchPath
= wstrconcat(wusergnusteppath(), "/Defaults");
512 /* Add the watch; really we are only looking for modify events
513 * but we might want more in the future so check all events for now.
514 * The individual events are checked for in event.c.
516 inotifyWD
= inotify_add_watch (inotifyFD
, watchPath
, IN_ALL_EVENTS
);
518 wwarning(_("could not add an inotify watch on path\n."
520 "Changes to the defaults database will require"
521 " a restart to take effect."),watchPath
);
533 paths
= wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
534 paths
= wstrappend(paths
, ":"DEF_CONFIG_PATHS
);
536 file
= wfindfile(paths
, DEF_INIT_SCRIPT
);
540 if (system(file
) != 0) {
541 wsyserror(_("%s:could not execute initialization script"), file
);
545 execl("/bin/sh", "/bin/sh", "-c", file
, NULL
);
546 wsyserror(_("%s:could not execute initialization script"), file
);
560 paths
= wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
561 paths
= wstrappend(paths
, ":"DEF_CONFIG_PATHS
);
563 file
= wfindfile(paths
, DEF_EXIT_SCRIPT
);
567 if (system(file
) != 0) {
568 wsyserror(_("%s:could not execute exit script"), file
);
572 execl("/bin/sh", "/bin/sh", "-c", file
, NULL
);
573 wsyserror(_("%s:could not execute exit script"), file
);
583 getFullPath(char *path
)
587 char *basep
= (char*)buffer
;
589 if (*path
!= '/' && getcwd(buffer
, 1023)) {
592 if (strncmp(path
, "../", 3)==0) {
594 basep
= strchr(basep
, '/');
595 if (!basep
|| *path
==0)
599 if (*path
== '/' || strncmp(path
, "./",2)==0) {
612 return wstrconcat(path
);
621 main(int argc
, char **argv
)
623 int i_am_the_monitor
, i
, len
;
626 /* setup common stuff for the monitor and wmaker itself */
627 WMInitializeApplication("WindowMaker", &argc
, argv
);
629 memset(&wPreferences
, 0, sizeof(WPreferences
));
631 wPreferences
.fallbackWMs
= WMCreateArray(8);
632 alt
= getenv("WINDOWMAKER_ALT_WM");
634 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup(alt
));
636 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("blackbox"));
637 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("metacity"));
638 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("fvwm"));
639 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("twm"));
640 WMAddToArray(wPreferences
.fallbackWMs
, NULL
);
641 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("rxvt"));
642 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("xterm"));
646 for (i
= 1; i
< argc
; i
++)
648 if (strncmp(argv
[i
], "--for-real", strlen("--for-real"))==0)
653 else if (strcmp(argv
[i
], "-display")==0 || strcmp(argv
[i
], "--display")==0)
657 wwarning(_("too few arguments for %s"), argv
[i
-1]);
660 DisplayName
= argv
[i
];
664 DisplayName
= XDisplayName(DisplayName
);
665 len
= strlen(DisplayName
)+64;
667 snprintf(str
, len
, "DISPLAY=%s", DisplayName
);
670 if (i_am_the_monitor
)
671 return MonitorLoop(argc
, argv
);
673 return real_main(argc
, argv
);
678 real_main(int argc
, char **argv
)
687 setlocale(LC_ALL
, "");
691 /* for telling WPrefs what's the name of the wmaker binary being ran */
693 str
= wstrconcat("WMAKER_BIN_NAME=", argv
[0]);
698 Arguments
= wmalloc(sizeof(char*)*(ArgCount
+1));
699 for (i
= 0; i
< argc
; i
++)
701 Arguments
[i
]= argv
[i
];
703 /* add the extra option to signal that we're just restarting wmaker */
704 Arguments
[argc
-1]= "--for-real=";
705 Arguments
[argc
]= NULL
;
707 ProgName
= strrchr(argv
[0],'/');
717 for (i
=1; i
<argc
; i
++) {
719 if (strcmp(argv
[i
], "-nocpp")==0
720 || strcmp(argv
[i
], "--no-cpp")==0) {
721 wPreferences
.flags
.nocpp
=1;
724 if (strcmp(argv
[i
], "--for-real")==0) {
725 wPreferences
.flags
.restarting
= 0;
726 } else if (strcmp(argv
[i
], "--for-real=")==0) {
727 wPreferences
.flags
.restarting
= 1;
728 } else if (strcmp(argv
[i
], "--for-real-")==0) {
729 wPreferences
.flags
.restarting
= 2;
730 } else if (strcmp(argv
[i
], "-no-autolaunch")==0
731 || strcmp(argv
[i
], "--no-autolaunch")==0) {
732 wPreferences
.flags
.noautolaunch
= 1;
733 } else if (strcmp(argv
[i
], "-dont-restore")==0
734 || strcmp(argv
[i
], "--dont-restore")==0) {
735 wPreferences
.flags
.norestore
= 1;
736 } else if (strcmp(argv
[i
], "-nodock")==0
737 || strcmp(argv
[i
], "--no-dock")==0) {
738 wPreferences
.flags
.nodock
=1;
739 } else if (strcmp(argv
[i
], "-noclip")==0
740 || strcmp(argv
[i
], "--no-clip")==0) {
741 wPreferences
.flags
.noclip
=1;
742 } else if (strcmp(argv
[i
], "-version")==0
743 || strcmp(argv
[i
], "--version")==0) {
744 printf("Window Maker %s\n", VERSION
);
746 } else if (strcmp(argv
[i
], "--global_defaults_path")==0) {
747 printf("%s/WindowMaker\n", SYSCONFDIR
);
750 } else if (strcmp(argv
[i
], "--synchronous")==0) {
753 } else if (strcmp(argv
[i
], "-locale")==0
754 || strcmp(argv
[i
], "--locale")==0) {
757 wwarning(_("too few arguments for %s"), argv
[i
-1]);
761 } else if (strcmp(argv
[i
], "-display")==0
762 || strcmp(argv
[i
], "--display")==0) {
765 wwarning(_("too few arguments for %s"), argv
[i
-1]);
768 DisplayName
= argv
[i
];
769 } else if (strcmp(argv
[i
], "-visualid")==0
770 || strcmp(argv
[i
], "--visual-id")==0) {
773 wwarning(_("too few arguments for %s"), argv
[i
-1]);
776 if (sscanf(argv
[i
], "%i", &wVisualID
)!=1) {
777 wwarning(_("bad value for visualid: \"%s\""), argv
[i
]);
780 } else if (strcmp(argv
[i
], "-static")==0
781 || strcmp(argv
[i
], "--static")==0) {
783 wPreferences
.flags
.noupdates
= 1;
785 } else if (strcmp(argv
[i
], "-clientid")==0
786 || strcmp(argv
[i
], "-restore")==0) {
789 wwarning(_("too few arguments for %s"), argv
[i
-1]);
793 } else if (strcmp(argv
[i
], "--help")==0) {
797 printf(_("%s: invalid argument '%s'\n"), argv
[0], argv
[i
]);
798 printf(_("Try '%s --help' for more information\n"), argv
[0]);
804 if (!wPreferences
.flags
.noupdates
) {
805 /* check existence of Defaults DB directory */
811 /* return of wstrconcat should not be free-ed! read putenv man page */
812 putenv(wstrconcat("LANG=", Locale
));
814 Locale
= getenv("LC_ALL");
816 Locale
= getenv("LANG");
820 setlocale(LC_ALL
, "");
822 if (!Locale
|| strcmp(Locale
, "C")==0 || strcmp(Locale
, "POSIX")==0)
825 if (getenv("NLSPATH"))
826 bindtextdomain("WindowMaker", getenv("NLSPATH"));
828 bindtextdomain("WindowMaker", LOCALEDIR
);
829 bind_textdomain_codeset("WindowMaker", "UTF-8");
830 textdomain("WindowMaker");
832 if (!XSupportsLocale()) {
833 wwarning(_("X server does not support locale"));
836 if (XSetLocaleModifiers("") == NULL
) {
837 wwarning(_("cannot set locale modifiers"));
844 Locale
= wstrdup(Locale
);
845 ptr
= strchr(Locale
, '.');
851 dpy
= XOpenDisplay(DisplayName
);
853 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName
));
857 if (fcntl(ConnectionNumber(dpy
), F_SETFD
, FD_CLOEXEC
) < 0) {
858 wsyserror("error setting close-on-exec flag for X connection");
864 * If unspecified, use default visual instead of waiting
865 * for wrlib/context.c:bestContext() that may end up choosing
866 * the "fake" 24 bits added by the Composite extension.
867 * This is required to avoid all sort of corruptions when
868 * composite is enabled, and at a depth other than 24.
870 wVisualID
= (int)DefaultVisual(dpy
, DefaultScreen(dpy
))->visualid
;
872 /* check if the user specified a complete display name (with screen).
873 * If so, only manage the specified screen */
875 str
= strchr(DisplayName
, ':');
879 if (str
&& sscanf(str
, ":%i.%i", &d
, &s
)==2)
882 DisplayName
= XDisplayName(DisplayName
);
884 int len
= strlen(DisplayName
)+64;
886 snprintf(str
, len
, "DISPLAY=%s", DisplayName
);
892 XSynchronize(dpy
, True
);
895 wXModifierInitialize();
898 wSessionConnectManager(argv
, argc
);
907 inotifyWatchConfig();