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,
25 #include <sys/inotify.h>
33 #include <sys/types.h>
37 #include <X11/Xutil.h>
39 /* Xlocale.h and locale.h are the same if X_LOCALE is undefind in wconfig.h,
40 * and if X_LOCALE is defined, X's locale emulating functions will be used.
41 * See Xlocale.h for more information.
43 #include <X11/Xlocale.h>
47 #include "WindowMaker.h"
52 #include "xmodifier.h"
56 #include <WINGs/WUtil.h>
58 #ifndef GLOBAL_DEFAULTS_SUBDIR
59 #define GLOBAL_DEFAULTS_SUBDIR "WindowMaker"
62 /****** Global Variables ******/
70 unsigned int ValidModMask
= 0xff;
76 /* locale to use. NULL==POSIX or C */
81 WPreferences wPreferences
;
83 WMPropList
*wDomainName
;
84 WMPropList
*wAttributeDomainName
;
86 WShortKey wKeyBindings
[WKBD_LAST
];
88 /* defaults domains */
89 WDDomain
*WDWindowMaker
= NULL
;
90 WDDomain
*WDWindowAttributes
= NULL
;
91 WDDomain
*WDRootMenu
= NULL
;
95 XContext wAppWinContext
;
96 XContext wStackContext
;
97 XContext wVEdgeContext
;
101 Atom _XA_WM_CHANGE_STATE
;
102 Atom _XA_WM_PROTOCOLS
;
103 Atom _XA_WM_TAKE_FOCUS
;
104 Atom _XA_WM_DELETE_WINDOW
;
105 Atom _XA_WM_SAVE_YOURSELF
;
106 Atom _XA_WM_CLIENT_LEADER
;
107 Atom _XA_WM_COLORMAP_WINDOWS
;
108 Atom _XA_WM_COLORMAP_NOTIFY
;
110 Atom _XA_GNUSTEP_WM_ATTR
;
111 Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW
;
112 Atom _XA_GNUSTEP_WM_RESIZEBAR
;
113 Atom _XA_GNUSTEP_TITLEBAR_STATE
;
115 Atom _XA_WINDOWMAKER_MENU
;
116 Atom _XA_WINDOWMAKER_WM_PROTOCOLS
;
117 Atom _XA_WINDOWMAKER_STATE
;
119 Atom _XA_WINDOWMAKER_WM_FUNCTION
;
120 Atom _XA_WINDOWMAKER_NOTICEBOARD
;
121 Atom _XA_WINDOWMAKER_COMMAND
;
123 Atom _XA_WINDOWMAKER_ICON_SIZE
;
124 Atom _XA_WINDOWMAKER_ICON_TILE
;
126 Atom _XA_WM_IGNORE_FOCUS_EVENTS
;
129 Cursor wCursor
[WCUR_LAST
];
131 /* last event timestamp for XSetInputFocus */
132 Time LastTimestamp
= CurrentTime
;
133 /* timestamp on the last time we did XSetInputFocus() */
134 Time LastFocusChange
= CurrentTime
;
137 Bool wShapeSupported
;
141 #ifdef KEEP_XKB_LOCK_STATUS
148 int randr_event_base
;
152 char WProgramSigState
= 0;
153 char WProgramState
= WSTATE_NORMAL
;
154 char WDelayedActionSet
= 0;
157 const char *WMNManaged
= "WMNManaged";
158 const char *WMNUnmanaged
= "WMNUnmanaged";
159 const char *WMNChangedWorkspace
= "WMNChangedWorkspace";
160 const char *WMNChangedState
= "WMNChangedState";
161 const char *WMNChangedFocus
= "WMNChangedFocus";
162 const char *WMNChangedStacking
= "WMNChangedStacking";
163 const char *WMNChangedName
= "WMNChangedName";
165 const char *WMNWorkspaceCreated
= "WMNWorkspaceCreated";
166 const char *WMNWorkspaceDestroyed
= "WMNWorkspaceDestroyed";
167 const char *WMNWorkspaceChanged
= "WMNWorkspaceChanged";
168 const char *WMNWorkspaceNameChanged
= "WMNWorkspaceNameChanged";
170 const char *WMNResetStacking
= "WMNResetStacking";
172 /******** End Global Variables *****/
174 static char *DisplayName
= NULL
;
176 static char **Arguments
;
180 extern void EventLoop(void);
181 extern void StartUp(Bool defaultScreenOnly
);
182 extern int MonitorLoop(int argc
, char **argv
);
184 static Bool multiHead
= True
;
186 static int *wVisualID
= NULL
;
187 static int wVisualID_len
= 0;
189 static int real_main(int argc
, char **argv
);
191 int getWVisualID(int screen
)
193 if (wVisualID
== NULL
)
195 if (screen
< 0 || screen
>= wVisualID_len
)
198 return wVisualID
[screen
];
201 static void setWVisualID(int screen
, int val
)
208 if (wVisualID
== NULL
) {
209 /* no array at all, alloc space for screen + 1 entries
210 * and init with default value */
211 wVisualID_len
= screen
+ 1;
212 wVisualID
= (int *)malloc(wVisualID_len
* sizeof(int));
213 for (i
= 0; i
< wVisualID_len
; i
++) {
216 } else if (screen
>= wVisualID_len
) {
217 /* larger screen number than previously allocated
219 int oldlen
= wVisualID_len
;
221 wVisualID_len
= screen
+ 1;
222 wVisualID
= (int *)realloc(wVisualID
, wVisualID_len
* sizeof(int));
223 for (i
= oldlen
; i
< wVisualID_len
; i
++) {
228 wVisualID
[screen
] = val
;
232 * this function splits a given string at the comma into tokens
233 * and set the wVisualID variable to each parsed number
235 static int initWVisualID(const char *user_str
)
237 char *mystr
= strdup(user_str
);
244 /* check for delimiter */
245 if (user_str
[cur_in_pos
] == '\0' || user_str
[cur_in_pos
] == ',') {
248 mystr
[cur_out_pos
] = '\0';
250 if (sscanf(mystr
, "%i", &v
) != 1) {
255 setWVisualID(cur_screen
, v
);
261 /* break in case last char has been consumed */
262 if (user_str
[cur_in_pos
] == '\0')
265 /* if the current char is no delimiter put it into mystr */
266 if (user_str
[cur_in_pos
] != ',') {
267 mystr
[cur_out_pos
++] = user_str
[cur_in_pos
];
274 if (cur_screen
== 0||error_found
!= 0)
280 void Exit(int status
)
288 void Restart(char *manager
, Bool abortOnFailure
)
291 char *argv
[MAX_RESTART_ARGS
];
294 if (manager
&& manager
[0] != 0) {
295 prog
= argv
[0] = strtok(manager
, " ");
296 for (i
= 1; i
< MAX_RESTART_ARGS
; i
++) {
297 argv
[i
] = strtok(NULL
, " ");
298 if (argv
[i
] == NULL
) {
308 execvp(Arguments
[0], Arguments
);
309 wfatal(_("failed to restart Window Maker."));
312 werror(_("could not exec %s"), prog
);
318 void SetupEnvironment(WScreen
* scr
)
324 int len
= strlen(DisplayName
) + 64;
326 snprintf(tmp
, len
, "DISPLAY=%s", XDisplayName(DisplayName
));
327 ptr
= strchr(strchr(tmp
, ':'), '.');
330 snprintf(buf
, sizeof(buf
), ".%i", scr
->screen
);
335 snprintf(tmp
, 60, "WRASTER_COLOR_RESOLUTION%i=%i", scr
->screen
,
336 scr
->rcontext
->attribs
->colors_per_channel
);
345 static void shellCommandHandler(pid_t pid
, unsigned char status
, _tuple
* data
)
350 buffer
= wstrconcat(_("Could not execute command: "), data
->command
);
352 wMessageDialog(data
->scr
, _("Error"), buffer
, _("OK"), NULL
, NULL
);
354 } else if (status
!= 127) {
356 printf("%s: %i\n", data->command, status);
360 wfree(data
->command
);
364 void ExecuteShellCommand(WScreen
* scr
, char *command
)
366 static char *shell
= NULL
;
370 * This have a problem: if the shell is tcsh (not sure about others)
371 * and ~/.tcshrc have /bin/stty erase ^H somewhere on it, the shell
372 * will block and the command will not be executed.
374 shell = getenv("SHELL");
385 SetupEnvironment(scr
);
390 execl(shell
, shell
, "-c", command
, NULL
);
391 werror("could not execute %s -c %s", shell
, command
);
393 } else if (pid
< 0) {
394 werror("cannot fork a new process");
396 _tuple
*data
= wmalloc(sizeof(_tuple
));
399 data
->command
= wstrdup(command
);
401 wAddDeathHandler(pid
, (WDeathHandler
*) shellCommandHandler
, data
);
406 *---------------------------------------------------------------------
408 * Do a major cleanup and exit the program
410 *----------------------------------------------------------------------
412 void wAbort(Bool dumpCore
)
417 for (i
= 0; i
< wScreenCount
; i
++) {
418 scr
= wScreenWithNumber(i
);
422 printf(_("%s aborted.\n"), ProgName
);
431 printf(_("Usage: %s [options]\n"), ProgName
);
432 puts(_("The Window Maker window manager for the X window system"));
434 puts(_(" -display host:dpy display to use"));
436 puts(_(" --no-cpp disable preprocessing of configuration files"));
438 puts(_(" --no-dock do not open the application Dock"));
439 puts(_(" --no-clip do not open the workspace Clip"));
440 puts(_(" --no-autolaunch do not autolaunch applications"));
441 puts(_(" --dont-restore do not restore saved session"));
443 puts(_(" --locale locale locale to use"));
445 puts(_(" --create-stdcmap create the standard colormap hint in PseudoColor visuals"));
446 puts(_(" --visual-id visualid visual id of visual to use"));
447 puts(_(" --static do not update or save configurations"));
449 puts(_(" --no-polling do not periodically check for configuration updates"));
451 puts(_(" --version print version and exit"));
452 puts(_(" --help show this message"));
455 void check_defaults()
459 path
= wdefaultspathfordomain("WindowMaker");
461 if (access(path
, R_OK
) != 0) {
462 wwarning(_("could not find user GNUstep directory (%s)."), path
);
464 if (system("wmaker.inst --batch") != 0) {
465 wwarning(_("There was an error while creating GNUstep directory, please "
466 "make sure you have installed Window Maker correctly and run wmaker.inst"));
468 wwarning(_("%s directory created with default configuration."), path
);
477 * Add watch here, used to notify if configuration
478 * files have changed, using linux kernel inotify mechanism
481 static void inotifyWatchConfig()
483 char *watchPath
= NULL
;
484 inotifyFD
= inotify_init(); /* Initialise an inotify instance */
486 wwarning(_("could not initialise an inotify instance."
487 " Changes to the defaults database will require"
488 " a restart to take effect. Check your kernel!"));
490 watchPath
= wstrconcat(wusergnusteppath(), "/Defaults");
491 /* Add the watch; really we are only looking for modify events
492 * but we might want more in the future so check all events for now.
493 * The individual events are checked for in event.c.
495 inotifyWD
= inotify_add_watch(inotifyFD
, watchPath
, IN_ALL_EVENTS
);
497 wwarning(_("could not add an inotify watch on path %s."
498 "Changes to the defaults database will require"
499 " a restart to take effect."), watchPath
);
505 #endif /* HAVE_INOTIFY */
507 static void execInitScript()
511 paths
= wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
512 paths
= wstrappend(paths
, ":" DEF_CONFIG_PATHS
);
514 file
= wfindfile(paths
, DEF_INIT_SCRIPT
);
518 if (system(file
) != 0) {
519 werror(_("%s:could not execute initialization script"), file
);
525 void ExecExitScript()
529 paths
= wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
530 paths
= wstrappend(paths
, ":" DEF_CONFIG_PATHS
);
532 file
= wfindfile(paths
, DEF_EXIT_SCRIPT
);
536 if (system(file
) != 0) {
537 werror(_("%s:could not execute exit script"), file
);
543 int main(int argc
, char **argv
)
545 int i_am_the_monitor
, i
, len
;
548 /* setup common stuff for the monitor and wmaker itself */
549 WMInitializeApplication("WindowMaker", &argc
, argv
);
551 memset(&wPreferences
, 0, sizeof(WPreferences
));
553 wPreferences
.fallbackWMs
= WMCreateArray(8);
554 alt
= getenv("WINDOWMAKER_ALT_WM");
556 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup(alt
));
558 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("blackbox"));
559 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("metacity"));
560 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("fvwm"));
561 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("twm"));
562 WMAddToArray(wPreferences
.fallbackWMs
, NULL
);
563 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("rxvt"));
564 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("xterm"));
566 i_am_the_monitor
= 1;
568 for (i
= 1; i
< argc
; i
++) {
569 if (strncmp(argv
[i
], "--for-real", strlen("--for-real")) == 0) {
570 i_am_the_monitor
= 0;
572 } else if (strcmp(argv
[i
], "-display") == 0 || strcmp(argv
[i
], "--display") == 0) {
575 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
578 DisplayName
= argv
[i
];
582 DisplayName
= XDisplayName(DisplayName
);
583 len
= strlen(DisplayName
) + 64;
585 snprintf(str
, len
, "DISPLAY=%s", DisplayName
);
588 if (i_am_the_monitor
)
589 return MonitorLoop(argc
, argv
);
591 return real_main(argc
, argv
);
594 static int real_main(int argc
, char **argv
)
601 setlocale(LC_ALL
, "");
604 /* for telling WPrefs what's the name of the wmaker binary being ran */
605 setenv("WMAKER_BIN_NAME", argv
[0], 1);
609 Arguments
= wmalloc(sizeof(char *) * (ArgCount
+ 1));
610 for (i
= 0; i
< argc
; i
++) {
611 Arguments
[i
] = argv
[i
];
613 /* add the extra option to signal that we're just restarting wmaker */
614 Arguments
[argc
- 1] = "--for-real=";
615 Arguments
[argc
] = NULL
;
617 ProgName
= strrchr(argv
[0], '/');
626 for (i
= 1; i
< argc
; i
++) {
628 if (strcmp(argv
[i
], "-nocpp") == 0 || strcmp(argv
[i
], "--no-cpp") == 0) {
629 wPreferences
.flags
.nocpp
= 1;
632 if (strcmp(argv
[i
], "--for-real") == 0) {
633 wPreferences
.flags
.restarting
= 0;
634 } else if (strcmp(argv
[i
], "--for-real=") == 0) {
635 wPreferences
.flags
.restarting
= 1;
636 } else if (strcmp(argv
[i
], "--for-real-") == 0) {
637 wPreferences
.flags
.restarting
= 2;
638 } else if (strcmp(argv
[i
], "-no-autolaunch") == 0
639 || strcmp(argv
[i
], "--no-autolaunch") == 0) {
640 wPreferences
.flags
.noautolaunch
= 1;
641 } else if (strcmp(argv
[i
], "-dont-restore") == 0 || strcmp(argv
[i
], "--dont-restore") == 0) {
642 wPreferences
.flags
.norestore
= 1;
643 } else if (strcmp(argv
[i
], "-nodock") == 0 || strcmp(argv
[i
], "--no-dock") == 0) {
644 wPreferences
.flags
.nodock
= 1;
645 } else if (strcmp(argv
[i
], "-noclip") == 0 || strcmp(argv
[i
], "--no-clip") == 0) {
646 wPreferences
.flags
.noclip
= 1;
647 } else if (strcmp(argv
[i
], "-version") == 0 || strcmp(argv
[i
], "--version") == 0) {
648 printf("Window Maker %s\n", VERSION
);
650 } else if (strcmp(argv
[i
], "--global_defaults_path") == 0) {
651 printf("%s/%s\n", SYSCONFDIR
, GLOBAL_DEFAULTS_SUBDIR
);
653 } else if (strcmp(argv
[i
], "-locale") == 0 || strcmp(argv
[i
], "--locale") == 0) {
656 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
660 } else if (strcmp(argv
[i
], "-display") == 0 || strcmp(argv
[i
], "--display") == 0) {
663 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
666 DisplayName
= argv
[i
];
667 } else if (strcmp(argv
[i
], "-visualid") == 0 || strcmp(argv
[i
], "--visual-id") == 0) {
670 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
673 if (initWVisualID(argv
[i
]) != 0) {
674 wwarning(_("bad value for visualid: \"%s\""), argv
[i
]);
677 } else if (strcmp(argv
[i
], "-static") == 0 || strcmp(argv
[i
], "--static") == 0
679 || strcmp(argv
[i
], "--no-polling") == 0
682 wPreferences
.flags
.noupdates
= 1;
683 } else if (strcmp(argv
[i
], "--help") == 0) {
687 printf(_("%s: invalid argument '%s'\n"), argv
[0], argv
[i
]);
688 printf(_("Try '%s --help' for more information\n"), argv
[0]);
694 if (!wPreferences
.flags
.noupdates
) {
695 /* check existence of Defaults DB directory */
700 setenv("LANG", Locale
, 1);
702 Locale
= getenv("LC_ALL");
704 Locale
= getenv("LANG");
708 setlocale(LC_ALL
, "");
710 if (!Locale
|| strcmp(Locale
, "C") == 0 || strcmp(Locale
, "POSIX") == 0)
713 if (getenv("NLSPATH")) {
714 bindtextdomain("WindowMaker", getenv("NLSPATH"));
715 #if defined(MENU_TEXTDOMAIN)
716 bindtextdomain(MENU_TEXTDOMAIN
, getenv("NLSPATH"));
719 bindtextdomain("WindowMaker", LOCALEDIR
);
720 #if defined(MENU_TEXTDOMAIN)
721 bindtextdomain(MENU_TEXTDOMAIN
, LOCALEDIR
);
724 bind_textdomain_codeset("WindowMaker", "UTF-8");
725 #if defined(MENU_TEXTDOMAIN)
726 bind_textdomain_codeset(MENU_TEXTDOMAIN
, "UTF-8");
728 textdomain("WindowMaker");
730 if (!XSupportsLocale()) {
731 wwarning(_("X server does not support locale"));
734 if (XSetLocaleModifiers("") == NULL
) {
735 wwarning(_("cannot set locale modifiers"));
742 Locale
= wstrdup(Locale
);
743 ptr
= strchr(Locale
, '.');
749 dpy
= XOpenDisplay(DisplayName
);
751 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName
));
755 if (fcntl(ConnectionNumber(dpy
), F_SETFD
, FD_CLOEXEC
) < 0) {
756 werror("error setting close-on-exec flag for X connection");
761 if (getWVisualID(0) < 0) {
763 * If unspecified, use default visual instead of waiting
764 * for wrlib/context.c:bestContext() that may end up choosing
765 * the "fake" 24 bits added by the Composite extension.
766 * This is required to avoid all sort of corruptions when
767 * composite is enabled, and at a depth other than 24.
769 setWVisualID(0, (int)DefaultVisual(dpy
, DefaultScreen(dpy
))->visualid
);
772 /* check if the user specified a complete display name (with screen).
773 * If so, only manage the specified screen */
775 pos
= strchr(DisplayName
, ':');
779 if (pos
&& sscanf(pos
, ":%i.%i", &d
, &s
) == 2)
782 DisplayName
= XDisplayName(DisplayName
);
783 setenv("DISPLAY", DisplayName
, 1);
785 wXModifierInitialize();
788 if (wScreenCount
== 1)
793 inotifyWatchConfig();