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 along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <sys/inotify.h>
32 #include <sys/types.h>
36 #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"
55 #include <WINGs/WUtil.h>
57 #ifndef GLOBAL_DEFAULTS_SUBDIR
58 #define GLOBAL_DEFAULTS_SUBDIR "WindowMaker"
61 /****** Global Variables ******/
69 unsigned int ValidModMask
= 0xff;
75 /* locale to use. NULL==POSIX or C */
80 WPreferences wPreferences
;
82 WMPropList
*wDomainName
;
83 WMPropList
*wAttributeDomainName
;
85 WShortKey wKeyBindings
[WKBD_LAST
];
87 /* defaults domains */
88 WDDomain
*WDWindowMaker
= NULL
;
89 WDDomain
*WDWindowAttributes
= NULL
;
90 WDDomain
*WDRootMenu
= NULL
;
94 XContext wAppWinContext
;
95 XContext wStackContext
;
96 XContext wVEdgeContext
;
100 Atom _XA_WM_CHANGE_STATE
;
101 Atom _XA_WM_PROTOCOLS
;
102 Atom _XA_WM_TAKE_FOCUS
;
103 Atom _XA_WM_DELETE_WINDOW
;
104 Atom _XA_WM_SAVE_YOURSELF
;
105 Atom _XA_WM_CLIENT_LEADER
;
106 Atom _XA_WM_COLORMAP_WINDOWS
;
107 Atom _XA_WM_COLORMAP_NOTIFY
;
109 Atom _XA_GNUSTEP_WM_ATTR
;
110 Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW
;
111 Atom _XA_GNUSTEP_WM_RESIZEBAR
;
112 Atom _XA_GNUSTEP_TITLEBAR_STATE
;
114 Atom _XA_WINDOWMAKER_MENU
;
115 Atom _XA_WINDOWMAKER_WM_PROTOCOLS
;
116 Atom _XA_WINDOWMAKER_STATE
;
118 Atom _XA_WINDOWMAKER_WM_FUNCTION
;
119 Atom _XA_WINDOWMAKER_NOTICEBOARD
;
120 Atom _XA_WINDOWMAKER_COMMAND
;
122 Atom _XA_WINDOWMAKER_ICON_SIZE
;
123 Atom _XA_WINDOWMAKER_ICON_TILE
;
125 Atom _XA_WM_IGNORE_FOCUS_EVENTS
;
128 Cursor wCursor
[WCUR_LAST
];
130 /* last event timestamp for XSetInputFocus */
131 Time LastTimestamp
= CurrentTime
;
132 /* timestamp on the last time we did XSetInputFocus() */
133 Time LastFocusChange
= CurrentTime
;
136 Bool wShapeSupported
;
140 #ifdef KEEP_XKB_LOCK_STATUS
147 int randr_event_base
;
151 char WProgramSigState
= 0;
152 char WProgramState
= WSTATE_NORMAL
;
153 char WDelayedActionSet
= 0;
156 const char *WMNManaged
= "WMNManaged";
157 const char *WMNUnmanaged
= "WMNUnmanaged";
158 const char *WMNChangedWorkspace
= "WMNChangedWorkspace";
159 const char *WMNChangedState
= "WMNChangedState";
160 const char *WMNChangedFocus
= "WMNChangedFocus";
161 const char *WMNChangedStacking
= "WMNChangedStacking";
162 const char *WMNChangedName
= "WMNChangedName";
164 const char *WMNWorkspaceCreated
= "WMNWorkspaceCreated";
165 const char *WMNWorkspaceDestroyed
= "WMNWorkspaceDestroyed";
166 const char *WMNWorkspaceChanged
= "WMNWorkspaceChanged";
167 const char *WMNWorkspaceNameChanged
= "WMNWorkspaceNameChanged";
169 const char *WMNResetStacking
= "WMNResetStacking";
171 /******** End Global Variables *****/
173 static char *DisplayName
= NULL
;
175 static char **Arguments
;
179 extern void EventLoop(void);
180 extern void StartUp(Bool defaultScreenOnly
);
181 extern int MonitorLoop(int argc
, char **argv
);
183 static Bool multiHead
= True
;
185 static int *wVisualID
= NULL
;
186 static int wVisualID_len
= 0;
188 static int real_main(int argc
, char **argv
);
190 int getWVisualID(int screen
)
192 if (wVisualID
== NULL
)
194 if (screen
< 0 || screen
>= wVisualID_len
)
197 return wVisualID
[screen
];
200 static void setWVisualID(int screen
, int val
)
207 if (wVisualID
== NULL
) {
208 /* no array at all, alloc space for screen + 1 entries
209 * and init with default value */
210 wVisualID_len
= screen
+ 1;
211 wVisualID
= (int *)malloc(wVisualID_len
* sizeof(int));
212 for (i
= 0; i
< wVisualID_len
; i
++) {
215 } else if (screen
>= wVisualID_len
) {
216 /* larger screen number than previously allocated
218 int oldlen
= wVisualID_len
;
220 wVisualID_len
= screen
+ 1;
221 wVisualID
= (int *)realloc(wVisualID
, wVisualID_len
* sizeof(int));
222 for (i
= oldlen
; i
< wVisualID_len
; i
++) {
227 wVisualID
[screen
] = val
;
231 * this function splits a given string at the comma into tokens
232 * and set the wVisualID variable to each parsed number
234 static int initWVisualID(const char *user_str
)
236 char *mystr
= strdup(user_str
);
243 /* check for delimiter */
244 if (user_str
[cur_in_pos
] == '\0' || user_str
[cur_in_pos
] == ',') {
247 mystr
[cur_out_pos
] = '\0';
249 if (sscanf(mystr
, "%i", &v
) != 1) {
254 setWVisualID(cur_screen
, v
);
260 /* break in case last char has been consumed */
261 if (user_str
[cur_in_pos
] == '\0')
264 /* if the current char is no delimiter put it into mystr */
265 if (user_str
[cur_in_pos
] != ',') {
266 mystr
[cur_out_pos
++] = user_str
[cur_in_pos
];
273 if (cur_screen
== 0||error_found
!= 0)
279 void Exit(int status
)
287 void Restart(char *manager
, Bool abortOnFailure
)
290 char *argv
[MAX_RESTART_ARGS
];
293 if (manager
&& manager
[0] != 0) {
294 prog
= argv
[0] = strtok(manager
, " ");
295 for (i
= 1; i
< MAX_RESTART_ARGS
; i
++) {
296 argv
[i
] = strtok(NULL
, " ");
297 if (argv
[i
] == NULL
) {
307 execvp(Arguments
[0], Arguments
);
308 wfatal(_("failed to restart Window Maker."));
311 werror(_("could not exec %s"), prog
);
317 void SetupEnvironment(WScreen
* scr
)
323 int len
= strlen(DisplayName
) + 64;
325 snprintf(tmp
, len
, "DISPLAY=%s", XDisplayName(DisplayName
));
326 ptr
= strchr(strchr(tmp
, ':'), '.');
329 snprintf(buf
, sizeof(buf
), ".%i", scr
->screen
);
334 snprintf(tmp
, 60, "WRASTER_COLOR_RESOLUTION%i=%i", scr
->screen
,
335 scr
->rcontext
->attribs
->colors_per_channel
);
344 static void shellCommandHandler(pid_t pid
, unsigned char status
, _tuple
* data
)
349 buffer
= wstrconcat(_("Could not execute command: "), data
->command
);
351 wMessageDialog(data
->scr
, _("Error"), buffer
, _("OK"), NULL
, NULL
);
353 } else if (status
!= 127) {
355 printf("%s: %i\n", data->command, status);
359 wfree(data
->command
);
363 void ExecuteShellCommand(WScreen
* scr
, char *command
)
365 static char *shell
= NULL
;
369 * This have a problem: if the shell is tcsh (not sure about others)
370 * and ~/.tcshrc have /bin/stty erase ^H somewhere on it, the shell
371 * will block and the command will not be executed.
373 shell = getenv("SHELL");
384 SetupEnvironment(scr
);
389 execl(shell
, shell
, "-c", command
, NULL
);
390 werror("could not execute %s -c %s", shell
, command
);
392 } else if (pid
< 0) {
393 werror("cannot fork a new process");
395 _tuple
*data
= wmalloc(sizeof(_tuple
));
398 data
->command
= wstrdup(command
);
400 wAddDeathHandler(pid
, (WDeathHandler
*) shellCommandHandler
, data
);
405 *---------------------------------------------------------------------
407 * Do a major cleanup and exit the program
409 *----------------------------------------------------------------------
411 void wAbort(Bool dumpCore
)
416 for (i
= 0; i
< wScreenCount
; i
++) {
417 scr
= wScreenWithNumber(i
);
421 printf(_("%s aborted.\n"), ProgName
);
430 printf(_("Usage: %s [options]\n"), ProgName
);
431 puts(_("The Window Maker window manager for the X window system"));
433 puts(_(" -display host:dpy display to use"));
435 puts(_(" --no-cpp disable preprocessing of configuration files"));
437 puts(_(" --no-dock do not open the application Dock"));
438 puts(_(" --no-clip do not open the workspace Clip"));
439 puts(_(" --no-autolaunch do not autolaunch applications"));
440 puts(_(" --dont-restore do not restore saved session"));
442 puts(_(" --locale locale locale to use"));
444 puts(_(" --create-stdcmap create the standard colormap hint in PseudoColor visuals"));
445 puts(_(" --visual-id visualid visual id of visual to use"));
446 puts(_(" --static do not update or save configurations"));
448 puts(_(" --no-polling do not periodically check for configuration updates"));
450 puts(_(" --version print version and exit"));
451 puts(_(" --help show this message"));
454 void check_defaults()
458 path
= wdefaultspathfordomain("WindowMaker");
460 if (access(path
, R_OK
) != 0) {
461 wwarning(_("could not find user GNUstep directory (%s)."), path
);
463 if (system("wmaker.inst --batch") != 0) {
464 wwarning(_("There was an error while creating GNUstep directory, please "
465 "make sure you have installed Window Maker correctly and run wmaker.inst"));
467 wwarning(_("%s directory created with default configuration."), path
);
476 * Add watch here, used to notify if configuration
477 * files have changed, using linux kernel inotify mechanism
480 static void inotifyWatchConfig()
482 char *watchPath
= NULL
;
483 inotifyFD
= inotify_init(); /* Initialise an inotify instance */
485 wwarning(_("could not initialise an inotify instance."
486 " Changes to the defaults database will require"
487 " a restart to take effect. Check your kernel!"));
489 watchPath
= wstrconcat(wusergnusteppath(), "/Defaults");
490 /* Add the watch; really we are only looking for modify events
491 * but we might want more in the future so check all events for now.
492 * The individual events are checked for in event.c.
494 inotifyWD
= inotify_add_watch(inotifyFD
, watchPath
, IN_ALL_EVENTS
);
496 wwarning(_("could not add an inotify watch on path %s."
497 "Changes to the defaults database will require"
498 " a restart to take effect."), watchPath
);
504 #endif /* HAVE_INOTIFY */
506 static void execInitScript()
510 paths
= wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
511 paths
= wstrappend(paths
, ":" DEF_CONFIG_PATHS
);
513 file
= wfindfile(paths
, DEF_INIT_SCRIPT
);
517 if (system(file
) != 0) {
518 werror(_("%s:could not execute initialization script"), file
);
524 void ExecExitScript()
528 paths
= wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
529 paths
= wstrappend(paths
, ":" DEF_CONFIG_PATHS
);
531 file
= wfindfile(paths
, DEF_EXIT_SCRIPT
);
535 if (system(file
) != 0) {
536 werror(_("%s:could not execute exit script"), file
);
542 int main(int argc
, char **argv
)
544 int i_am_the_monitor
, i
, len
;
547 /* setup common stuff for the monitor and wmaker itself */
548 WMInitializeApplication("WindowMaker", &argc
, argv
);
550 memset(&wPreferences
, 0, sizeof(WPreferences
));
552 wPreferences
.fallbackWMs
= WMCreateArray(8);
553 alt
= getenv("WINDOWMAKER_ALT_WM");
555 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup(alt
));
557 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("blackbox"));
558 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("metacity"));
559 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("fvwm"));
560 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("twm"));
561 WMAddToArray(wPreferences
.fallbackWMs
, NULL
);
562 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("rxvt"));
563 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("xterm"));
565 i_am_the_monitor
= 1;
567 for (i
= 1; i
< argc
; i
++) {
568 if (strncmp(argv
[i
], "--for-real", strlen("--for-real")) == 0) {
569 i_am_the_monitor
= 0;
571 } else if (strcmp(argv
[i
], "-display") == 0 || strcmp(argv
[i
], "--display") == 0) {
574 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
577 DisplayName
= argv
[i
];
581 DisplayName
= XDisplayName(DisplayName
);
582 len
= strlen(DisplayName
) + 64;
584 snprintf(str
, len
, "DISPLAY=%s", DisplayName
);
587 if (i_am_the_monitor
)
588 return MonitorLoop(argc
, argv
);
590 return real_main(argc
, argv
);
593 static int real_main(int argc
, char **argv
)
599 setlocale(LC_ALL
, "");
602 /* for telling WPrefs what's the name of the wmaker binary being ran */
603 setenv("WMAKER_BIN_NAME", argv
[0], 1);
606 Arguments
= wmalloc(sizeof(char *) * (ArgCount
+ 1));
607 for (i
= 0; i
< argc
; i
++) {
608 Arguments
[i
] = argv
[i
];
610 /* add the extra option to signal that we're just restarting wmaker */
611 Arguments
[argc
- 1] = "--for-real=";
612 Arguments
[argc
] = NULL
;
614 ProgName
= strrchr(argv
[0], '/');
621 for (i
= 1; i
< argc
; i
++) {
623 if (strcmp(argv
[i
], "-nocpp") == 0 || strcmp(argv
[i
], "--no-cpp") == 0) {
624 wPreferences
.flags
.nocpp
= 1;
627 if (strcmp(argv
[i
], "--for-real") == 0) {
628 wPreferences
.flags
.restarting
= 0;
629 } else if (strcmp(argv
[i
], "--for-real=") == 0) {
630 wPreferences
.flags
.restarting
= 1;
631 } else if (strcmp(argv
[i
], "--for-real-") == 0) {
632 wPreferences
.flags
.restarting
= 2;
633 } else if (strcmp(argv
[i
], "-no-autolaunch") == 0
634 || strcmp(argv
[i
], "--no-autolaunch") == 0) {
635 wPreferences
.flags
.noautolaunch
= 1;
636 } else if (strcmp(argv
[i
], "-dont-restore") == 0 || strcmp(argv
[i
], "--dont-restore") == 0) {
637 wPreferences
.flags
.norestore
= 1;
638 } else if (strcmp(argv
[i
], "-nodock") == 0 || strcmp(argv
[i
], "--no-dock") == 0) {
639 wPreferences
.flags
.nodock
= 1;
640 } else if (strcmp(argv
[i
], "-noclip") == 0 || strcmp(argv
[i
], "--no-clip") == 0) {
641 wPreferences
.flags
.noclip
= 1;
642 } else if (strcmp(argv
[i
], "-version") == 0 || strcmp(argv
[i
], "--version") == 0) {
643 printf("Window Maker %s\n", VERSION
);
645 } else if (strcmp(argv
[i
], "--global_defaults_path") == 0) {
646 printf("%s/%s\n", SYSCONFDIR
, GLOBAL_DEFAULTS_SUBDIR
);
648 } else if (strcmp(argv
[i
], "-locale") == 0 || strcmp(argv
[i
], "--locale") == 0) {
651 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
655 } else if (strcmp(argv
[i
], "-display") == 0 || strcmp(argv
[i
], "--display") == 0) {
658 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
661 DisplayName
= argv
[i
];
662 } else if (strcmp(argv
[i
], "-visualid") == 0 || strcmp(argv
[i
], "--visual-id") == 0) {
665 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
668 if (initWVisualID(argv
[i
]) != 0) {
669 wwarning(_("bad value for visualid: \"%s\""), argv
[i
]);
672 } else if (strcmp(argv
[i
], "-static") == 0 || strcmp(argv
[i
], "--static") == 0
674 || strcmp(argv
[i
], "--no-polling") == 0
677 wPreferences
.flags
.noupdates
= 1;
678 } else if (strcmp(argv
[i
], "--help") == 0) {
682 printf(_("%s: invalid argument '%s'\n"), argv
[0], argv
[i
]);
683 printf(_("Try '%s --help' for more information\n"), argv
[0]);
689 if (!wPreferences
.flags
.noupdates
) {
690 /* check existence of Defaults DB directory */
695 setenv("LANG", Locale
, 1);
697 Locale
= getenv("LC_ALL");
699 Locale
= getenv("LANG");
703 setlocale(LC_ALL
, "");
705 if (!Locale
|| strcmp(Locale
, "C") == 0 || strcmp(Locale
, "POSIX") == 0)
708 if (getenv("NLSPATH")) {
709 bindtextdomain("WindowMaker", getenv("NLSPATH"));
710 #if defined(MENU_TEXTDOMAIN)
711 bindtextdomain(MENU_TEXTDOMAIN
, getenv("NLSPATH"));
714 bindtextdomain("WindowMaker", LOCALEDIR
);
715 #if defined(MENU_TEXTDOMAIN)
716 bindtextdomain(MENU_TEXTDOMAIN
, LOCALEDIR
);
719 bind_textdomain_codeset("WindowMaker", "UTF-8");
720 #if defined(MENU_TEXTDOMAIN)
721 bind_textdomain_codeset(MENU_TEXTDOMAIN
, "UTF-8");
723 textdomain("WindowMaker");
725 if (!XSupportsLocale()) {
726 wwarning(_("X server does not support locale"));
729 if (XSetLocaleModifiers("") == NULL
) {
730 wwarning(_("cannot set locale modifiers"));
737 Locale
= wstrdup(Locale
);
738 ptr
= strchr(Locale
, '.');
744 dpy
= XOpenDisplay(DisplayName
);
746 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName
));
750 if (fcntl(ConnectionNumber(dpy
), F_SETFD
, FD_CLOEXEC
) < 0) {
751 werror("error setting close-on-exec flag for X connection");
756 if (getWVisualID(0) < 0) {
758 * If unspecified, use default visual instead of waiting
759 * for wrlib/context.c:bestContext() that may end up choosing
760 * the "fake" 24 bits added by the Composite extension.
761 * This is required to avoid all sort of corruptions when
762 * composite is enabled, and at a depth other than 24.
764 setWVisualID(0, (int)DefaultVisual(dpy
, DefaultScreen(dpy
))->visualid
);
767 /* check if the user specified a complete display name (with screen).
768 * If so, only manage the specified screen */
770 pos
= strchr(DisplayName
, ':');
774 if (pos
&& sscanf(pos
, ":%i.%i", &d
, &s
) == 2)
777 DisplayName
= XDisplayName(DisplayName
);
778 setenv("DISPLAY", DisplayName
, 1);
780 wXModifierInitialize();
783 if (wScreenCount
== 1)
788 inotifyWatchConfig();