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>
37 /* Xlocale.h and locale.h are the same if X_LOCALE is undefind in wconfig.h,
38 * and if X_LOCALE is defined, X's locale emulating functions will be used.
39 * See Xlocale.h for more information.
41 #include <X11/Xlocale.h>
45 #include "WindowMaker.h"
50 #include "xmodifier.h"
55 #include <WINGs/WUtil.h>
57 #ifndef GLOBAL_DEFAULTS_SUBDIR
58 #define GLOBAL_DEFAULTS_SUBDIR "WindowMaker"
61 /****** Global Variables ******/
69 unsigned int ValidModMask
= 0xff;
73 /* locale to use. NULL==POSIX or C */
78 WPreferences wPreferences
;
80 WMPropList
*wDomainName
;
81 WMPropList
*wAttributeDomainName
;
83 WShortKey wKeyBindings
[WKBD_LAST
];
85 /* defaults domains */
86 WDDomain
*WDWindowMaker
= NULL
;
87 WDDomain
*WDWindowAttributes
= NULL
;
88 WDDomain
*WDRootMenu
= NULL
;
92 XContext wAppWinContext
;
93 XContext wStackContext
;
94 XContext wVEdgeContext
;
98 Atom _XA_WM_CHANGE_STATE
;
99 Atom _XA_WM_PROTOCOLS
;
100 Atom _XA_WM_TAKE_FOCUS
;
101 Atom _XA_WM_DELETE_WINDOW
;
102 Atom _XA_WM_SAVE_YOURSELF
;
103 Atom _XA_WM_CLIENT_LEADER
;
104 Atom _XA_WM_COLORMAP_WINDOWS
;
105 Atom _XA_WM_COLORMAP_NOTIFY
;
107 Atom _XA_GNUSTEP_WM_ATTR
;
108 Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW
;
109 Atom _XA_GNUSTEP_WM_RESIZEBAR
;
110 Atom _XA_GNUSTEP_TITLEBAR_STATE
;
112 Atom _XA_WINDOWMAKER_MENU
;
113 Atom _XA_WINDOWMAKER_WM_PROTOCOLS
;
114 Atom _XA_WINDOWMAKER_STATE
;
116 Atom _XA_WINDOWMAKER_WM_FUNCTION
;
117 Atom _XA_WINDOWMAKER_NOTICEBOARD
;
118 Atom _XA_WINDOWMAKER_COMMAND
;
120 Atom _XA_WINDOWMAKER_ICON_SIZE
;
121 Atom _XA_WINDOWMAKER_ICON_TILE
;
123 Atom _XA_WM_IGNORE_FOCUS_EVENTS
;
126 Cursor wCursor
[WCUR_LAST
];
128 /* last event timestamp for XSetInputFocus */
129 Time LastTimestamp
= CurrentTime
;
130 /* timestamp on the last time we did XSetInputFocus() */
131 Time LastFocusChange
= CurrentTime
;
134 Bool wShapeSupported
;
138 #ifdef KEEP_XKB_LOCK_STATUS
144 char WProgramSigState
= 0;
145 char WProgramState
= WSTATE_NORMAL
;
146 char WDelayedActionSet
= 0;
149 const char *WMNManaged
= "WMNManaged";
150 const char *WMNUnmanaged
= "WMNUnmanaged";
151 const char *WMNChangedWorkspace
= "WMNChangedWorkspace";
152 const char *WMNChangedState
= "WMNChangedState";
153 const char *WMNChangedFocus
= "WMNChangedFocus";
154 const char *WMNChangedStacking
= "WMNChangedStacking";
155 const char *WMNChangedName
= "WMNChangedName";
157 const char *WMNWorkspaceCreated
= "WMNWorkspaceCreated";
158 const char *WMNWorkspaceDestroyed
= "WMNWorkspaceDestroyed";
159 const char *WMNWorkspaceChanged
= "WMNWorkspaceChanged";
160 const char *WMNWorkspaceNameChanged
= "WMNWorkspaceNameChanged";
162 const char *WMNResetStacking
= "WMNResetStacking";
164 /******** End Global Variables *****/
166 static char *DisplayName
= NULL
;
168 static char **Arguments
;
172 extern void EventLoop();
173 extern void StartUp();
174 extern int MonitorLoop(int argc
, char **argv
);
176 static Bool multiHead
= True
;
178 static int *wVisualID
= NULL
;
179 static int wVisualID_len
= 0;
181 static int real_main(int argc
, char **argv
);
183 int getWVisualID(int screen
)
185 if (wVisualID
== NULL
)
187 if (screen
< 0 || screen
>= wVisualID_len
)
190 return wVisualID
[screen
];
193 static void setWVisualID(int screen
, int val
)
200 if (wVisualID
== NULL
) {
201 /* no array at all, alloc space for screen + 1 entries
202 * and init with default value */
203 wVisualID_len
= screen
+ 1;
204 wVisualID
= (int *)malloc(wVisualID_len
* sizeof(int));
205 for (i
= 0; i
< wVisualID_len
; i
++) {
208 } else if (screen
>= wVisualID_len
) {
209 /* larger screen number than previously allocated
211 int oldlen
= wVisualID_len
;
213 wVisualID_len
= screen
+ 1;
214 wVisualID
= (int *)realloc(wVisualID
, wVisualID_len
* sizeof(int));
215 for (i
= oldlen
; i
< wVisualID_len
; i
++) {
220 wVisualID
[screen
] = val
;
224 * this function splits a given string at the comma into tokens
225 * and set the wVisualID variable to each parsed number
227 static int initWVisualID(const char *user_str
)
229 char *mystr
= strdup(user_str
);
236 /* check for delimiter */
237 if (user_str
[cur_in_pos
] == '\0' || user_str
[cur_in_pos
] == ',') {
240 mystr
[cur_out_pos
] = '\0';
242 if (sscanf(mystr
, "%i", &v
) != 1) {
247 setWVisualID(cur_screen
, v
);
253 /* break in case last char has been consumed */
254 if (user_str
[cur_in_pos
] == '\0')
257 /* if the current char is no delimiter put it into mystr */
258 if (user_str
[cur_in_pos
] != ',') {
259 mystr
[cur_out_pos
++] = user_str
[cur_in_pos
];
266 if (cur_screen
== 0||error_found
!= 0)
272 void Exit(int status
)
280 void Restart(char *manager
, Bool abortOnFailure
)
283 char *argv
[MAX_RESTART_ARGS
];
286 if (manager
&& manager
[0] != 0) {
287 prog
= argv
[0] = strtok(manager
, " ");
288 for (i
= 1; i
< MAX_RESTART_ARGS
; i
++) {
289 argv
[i
] = strtok(NULL
, " ");
290 if (argv
[i
] == NULL
) {
300 execvp(Arguments
[0], Arguments
);
301 wfatal(_("failed to restart Window Maker."));
304 wsyserror(_("could not exec %s"), prog
);
310 void SetupEnvironment(WScreen
* scr
)
316 int len
= strlen(DisplayName
) + 64;
318 snprintf(tmp
, len
, "DISPLAY=%s", XDisplayName(DisplayName
));
319 ptr
= strchr(strchr(tmp
, ':'), '.');
322 snprintf(buf
, sizeof(buf
), ".%i", scr
->screen
);
327 snprintf(tmp
, 60, "WRASTER_COLOR_RESOLUTION%i=%i", scr
->screen
,
328 scr
->rcontext
->attribs
->colors_per_channel
);
337 static void shellCommandHandler(pid_t pid
, unsigned char status
, _tuple
* data
)
342 buffer
= wstrconcat(_("Could not execute command: "), data
->command
);
344 wMessageDialog(data
->scr
, _("Error"), buffer
, _("OK"), NULL
, NULL
);
346 } else if (status
!= 127) {
348 printf("%s: %i\n", data->command, status);
352 wfree(data
->command
);
356 void ExecuteShellCommand(WScreen
* scr
, char *command
)
358 static char *shell
= NULL
;
362 * This have a problem: if the shell is tcsh (not sure about others)
363 * and ~/.tcshrc have /bin/stty erase ^H somewhere on it, the shell
364 * will block and the command will not be executed.
366 shell = getenv("SHELL");
377 SetupEnvironment(scr
);
382 execl(shell
, shell
, "-c", command
, NULL
);
383 wsyserror("could not execute %s -c %s", shell
, command
);
385 } else if (pid
< 0) {
386 wsyserror("cannot fork a new process");
388 _tuple
*data
= wmalloc(sizeof(_tuple
));
391 data
->command
= wstrdup(command
);
393 wAddDeathHandler(pid
, (WDeathHandler
*) shellCommandHandler
, data
);
398 *---------------------------------------------------------------------
400 * Do a major cleanup and exit the program
402 *----------------------------------------------------------------------
404 void wAbort(Bool dumpCore
)
409 for (i
= 0; i
< wScreenCount
; i
++) {
410 scr
= wScreenWithNumber(i
);
414 printf(_("%s aborted.\n"), ProgName
);
423 printf(_("Usage: %s [options]\n"), ProgName
);
424 puts(_("The Window Maker window manager for the X window system"));
426 puts(_(" -display host:dpy display to use"));
428 puts(_(" --no-cpp disable preprocessing of configuration files"));
430 puts(_(" --no-dock do not open the application Dock"));
431 puts(_(" --no-clip do not open the workspace Clip"));
432 puts(_(" --no-autolaunch do not autolaunch applications"));
433 puts(_(" --dont-restore do not restore saved session"));
435 puts(_(" --locale locale locale to use"));
437 puts(_(" --create-stdcmap create the standard colormap hint in PseudoColor visuals"));
438 puts(_(" --visual-id visualid visual id of visual to use"));
439 puts(_(" --static do not update or save configurations"));
441 puts(_(" --synchronous turn on synchronous display mode"));
443 puts(_(" --version print version and exit"));
444 puts(_(" --help show this message"));
447 void check_defaults()
451 path
= wdefaultspathfordomain("WindowMaker");
453 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"), path
);
459 wwarning(_("could not find user GNUstep directory (%s)."), path
);
461 if (system("wmaker.inst --batch") != 0) {
462 wwarning(_("There was an error while creating GNUstep directory, please "
463 "make sure you have installed Window Maker correctly and run wmaker.inst"));
465 wwarning(_("%s directory created with default configuration."), path
);
474 * Add watch here, used to notify if configuration
475 * files have changed, using linux kernel inotify mechanism
478 static void inotifyWatchConfig()
480 char *watchPath
= NULL
;
481 inotifyFD
= inotify_init(); /* Initialise an inotify instance */
483 wwarning(_("could not initialise an inotify instance."
484 " Changes to the defaults database will require"
485 " a restart to take effect. Check your kernel!"));
487 watchPath
= wstrconcat(wusergnusteppath(), "/Defaults");
488 /* Add the watch; really we are only looking for modify events
489 * but we might want more in the future so check all events for now.
490 * The individual events are checked for in event.c.
492 inotifyWD
= inotify_add_watch(inotifyFD
, watchPath
, IN_ALL_EVENTS
);
494 wwarning(_("could not add an inotify watch on path\n."
496 "Changes to the defaults database will require"
497 " a restart to take effect."), watchPath
);
504 static void execInitScript()
508 paths
= wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
509 paths
= wstrappend(paths
, ":" DEF_CONFIG_PATHS
);
511 file
= wfindfile(paths
, DEF_INIT_SCRIPT
);
515 if (system(file
) != 0) {
516 wsyserror(_("%s:could not execute initialization script"), file
);
520 execl("/bin/sh", "/bin/sh", "-c", file
, NULL
);
521 wsyserror(_("%s:could not execute initialization script"), file
);
529 void ExecExitScript()
533 paths
= wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
534 paths
= wstrappend(paths
, ":" DEF_CONFIG_PATHS
);
536 file
= wfindfile(paths
, DEF_EXIT_SCRIPT
);
540 if (system(file
) != 0) {
541 wsyserror(_("%s:could not execute exit script"), file
);
545 execl("/bin/sh", "/bin/sh", "-c", file
, NULL
);
546 wsyserror(_("%s:could not execute exit script"), file
);
555 char *getFullPath(char *path
)
559 char *basep
= (char *)buffer
;
561 if (*path
!= '/' && getcwd(buffer
, 1023)) {
564 if (strncmp(path
, "../", 3) == 0) {
566 basep
= strchr(basep
, '/');
567 if (!basep
|| *path
== 0)
571 if (*path
== '/' || strncmp(path
, "./", 2) == 0) {
582 return wstrconcat(path
);
589 int main(int argc
, char **argv
)
591 int i_am_the_monitor
, i
, len
;
594 /* setup common stuff for the monitor and wmaker itself */
595 WMInitializeApplication("WindowMaker", &argc
, argv
);
597 memset(&wPreferences
, 0, sizeof(WPreferences
));
599 wPreferences
.fallbackWMs
= WMCreateArray(8);
600 alt
= getenv("WINDOWMAKER_ALT_WM");
602 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup(alt
));
604 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("blackbox"));
605 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("metacity"));
606 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("fvwm"));
607 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("twm"));
608 WMAddToArray(wPreferences
.fallbackWMs
, NULL
);
609 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("rxvt"));
610 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("xterm"));
612 i_am_the_monitor
= 1;
614 for (i
= 1; i
< argc
; i
++) {
615 if (strncmp(argv
[i
], "--for-real", strlen("--for-real")) == 0) {
616 i_am_the_monitor
= 0;
618 } else if (strcmp(argv
[i
], "-display") == 0 || strcmp(argv
[i
], "--display") == 0) {
621 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
624 DisplayName
= argv
[i
];
628 DisplayName
= XDisplayName(DisplayName
);
629 len
= strlen(DisplayName
) + 64;
631 snprintf(str
, len
, "DISPLAY=%s", DisplayName
);
634 if (i_am_the_monitor
)
635 return MonitorLoop(argc
, argv
);
637 return real_main(argc
, argv
);
640 static int real_main(int argc
, char **argv
)
649 setlocale(LC_ALL
, "");
653 /* for telling WPrefs what's the name of the wmaker binary being ran */
654 setenv("WMAKER_BIN_NAME", argv
[0], 1);
658 Arguments
= wmalloc(sizeof(char *) * (ArgCount
+ 1));
659 for (i
= 0; i
< argc
; i
++) {
660 Arguments
[i
] = argv
[i
];
662 /* add the extra option to signal that we're just restarting wmaker */
663 Arguments
[argc
- 1] = "--for-real=";
664 Arguments
[argc
] = NULL
;
666 ProgName
= strrchr(argv
[0], '/');
675 for (i
= 1; i
< argc
; i
++) {
677 if (strcmp(argv
[i
], "-nocpp") == 0 || strcmp(argv
[i
], "--no-cpp") == 0) {
678 wPreferences
.flags
.nocpp
= 1;
681 if (strcmp(argv
[i
], "--for-real") == 0) {
682 wPreferences
.flags
.restarting
= 0;
683 } else if (strcmp(argv
[i
], "--for-real=") == 0) {
684 wPreferences
.flags
.restarting
= 1;
685 } else if (strcmp(argv
[i
], "--for-real-") == 0) {
686 wPreferences
.flags
.restarting
= 2;
687 } else if (strcmp(argv
[i
], "-no-autolaunch") == 0
688 || strcmp(argv
[i
], "--no-autolaunch") == 0) {
689 wPreferences
.flags
.noautolaunch
= 1;
690 } else if (strcmp(argv
[i
], "-dont-restore") == 0 || strcmp(argv
[i
], "--dont-restore") == 0) {
691 wPreferences
.flags
.norestore
= 1;
692 } else if (strcmp(argv
[i
], "-nodock") == 0 || strcmp(argv
[i
], "--no-dock") == 0) {
693 wPreferences
.flags
.nodock
= 1;
694 } else if (strcmp(argv
[i
], "-noclip") == 0 || strcmp(argv
[i
], "--no-clip") == 0) {
695 wPreferences
.flags
.noclip
= 1;
696 } else if (strcmp(argv
[i
], "-version") == 0 || strcmp(argv
[i
], "--version") == 0) {
697 printf("Window Maker %s\n", VERSION
);
699 } else if (strcmp(argv
[i
], "--global_defaults_path") == 0) {
700 printf("%s/%s\n", SYSCONFDIR
, GLOBAL_DEFAULTS_SUBDIR
);
703 } else if (strcmp(argv
[i
], "--synchronous") == 0) {
706 } else if (strcmp(argv
[i
], "-locale") == 0 || strcmp(argv
[i
], "--locale") == 0) {
709 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
713 } else if (strcmp(argv
[i
], "-display") == 0 || strcmp(argv
[i
], "--display") == 0) {
716 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
719 DisplayName
= argv
[i
];
720 } else if (strcmp(argv
[i
], "-visualid") == 0 || strcmp(argv
[i
], "--visual-id") == 0) {
723 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
726 if (initWVisualID(argv
[i
]) != 0) {
727 wwarning(_("bad value for visualid: \"%s\""), argv
[i
]);
730 } else if (strcmp(argv
[i
], "-static") == 0 || strcmp(argv
[i
], "--static") == 0) {
731 wPreferences
.flags
.noupdates
= 1;
732 } else if (strcmp(argv
[i
], "--help") == 0) {
736 printf(_("%s: invalid argument '%s'\n"), argv
[0], argv
[i
]);
737 printf(_("Try '%s --help' for more information\n"), argv
[0]);
743 if (!wPreferences
.flags
.noupdates
) {
744 /* check existence of Defaults DB directory */
749 setenv("LANG", Locale
, 1);
751 Locale
= getenv("LC_ALL");
753 Locale
= getenv("LANG");
757 setlocale(LC_ALL
, "");
759 if (!Locale
|| strcmp(Locale
, "C") == 0 || strcmp(Locale
, "POSIX") == 0)
762 if (getenv("NLSPATH"))
763 bindtextdomain("WindowMaker", getenv("NLSPATH"));
765 bindtextdomain("WindowMaker", LOCALEDIR
);
766 bind_textdomain_codeset("WindowMaker", "UTF-8");
767 textdomain("WindowMaker");
769 if (!XSupportsLocale()) {
770 wwarning(_("X server does not support locale"));
773 if (XSetLocaleModifiers("") == NULL
) {
774 wwarning(_("cannot set locale modifiers"));
781 Locale
= wstrdup(Locale
);
782 ptr
= strchr(Locale
, '.');
788 dpy
= XOpenDisplay(DisplayName
);
790 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName
));
794 if (fcntl(ConnectionNumber(dpy
), F_SETFD
, FD_CLOEXEC
) < 0) {
795 wsyserror("error setting close-on-exec flag for X connection");
800 if (getWVisualID(0) < 0) {
802 * If unspecified, use default visual instead of waiting
803 * for wrlib/context.c:bestContext() that may end up choosing
804 * the "fake" 24 bits added by the Composite extension.
805 * This is required to avoid all sort of corruptions when
806 * composite is enabled, and at a depth other than 24.
808 setWVisualID(0, (int)DefaultVisual(dpy
, DefaultScreen(dpy
))->visualid
);
811 /* check if the user specified a complete display name (with screen).
812 * If so, only manage the specified screen */
814 pos
= strchr(DisplayName
, ':');
818 if (pos
&& sscanf(pos
, ":%i.%i", &d
, &s
) == 2)
821 DisplayName
= XDisplayName(DisplayName
);
822 setenv("DISPLAY", DisplayName
, 1);
826 XSynchronize(dpy
, True
);
829 wXModifierInitialize();
832 if (wScreenCount
== 1)
836 inotifyWatchConfig();