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"
53 #include "xmodifier.h"
59 #include <WINGs/WUtil.h>
61 #ifndef GLOBAL_DEFAULTS_SUBDIR
62 #define GLOBAL_DEFAULTS_SUBDIR "WindowMaker"
65 /****** Global Variables ******/
73 unsigned int ValidModMask
= 0xff;
79 /* locale to use. NULL==POSIX or C */
84 WPreferences wPreferences
;
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
147 char WProgramSigState
= 0;
148 char WProgramState
= WSTATE_NORMAL
;
149 char WDelayedActionSet
= 0;
152 const char *WMNManaged
= "WMNManaged";
153 const char *WMNUnmanaged
= "WMNUnmanaged";
154 const char *WMNChangedWorkspace
= "WMNChangedWorkspace";
155 const char *WMNChangedState
= "WMNChangedState";
156 const char *WMNChangedFocus
= "WMNChangedFocus";
157 const char *WMNChangedStacking
= "WMNChangedStacking";
158 const char *WMNChangedName
= "WMNChangedName";
160 const char *WMNWorkspaceCreated
= "WMNWorkspaceCreated";
161 const char *WMNWorkspaceDestroyed
= "WMNWorkspaceDestroyed";
162 const char *WMNWorkspaceChanged
= "WMNWorkspaceChanged";
163 const char *WMNWorkspaceNameChanged
= "WMNWorkspaceNameChanged";
165 const char *WMNResetStacking
= "WMNResetStacking";
167 /******** End Global Variables *****/
169 static char *DisplayName
= NULL
;
171 static char **Arguments
;
175 static Bool multiHead
= True
;
177 static int *wVisualID
= NULL
;
178 static int wVisualID_len
= 0;
180 static int real_main(int argc
, char **argv
);
182 int getWVisualID(int screen
)
184 if (wVisualID
== NULL
)
186 if (screen
< 0 || screen
>= wVisualID_len
)
189 return wVisualID
[screen
];
192 static void setWVisualID(int screen
, int val
)
199 if (wVisualID
== NULL
) {
200 /* no array at all, alloc space for screen + 1 entries
201 * and init with default value */
202 wVisualID_len
= screen
+ 1;
203 wVisualID
= (int *)malloc(wVisualID_len
* sizeof(int));
204 for (i
= 0; i
< wVisualID_len
; i
++) {
207 } else if (screen
>= wVisualID_len
) {
208 /* larger screen number than previously allocated
210 int oldlen
= wVisualID_len
;
212 wVisualID_len
= screen
+ 1;
213 wVisualID
= (int *)realloc(wVisualID
, wVisualID_len
* sizeof(int));
214 for (i
= oldlen
; i
< wVisualID_len
; i
++) {
219 wVisualID
[screen
] = val
;
223 * this function splits a given string at the comma into tokens
224 * and set the wVisualID variable to each parsed number
226 static int initWVisualID(const char *user_str
)
228 char *mystr
= strdup(user_str
);
235 /* check for delimiter */
236 if (user_str
[cur_in_pos
] == '\0' || user_str
[cur_in_pos
] == ',') {
239 mystr
[cur_out_pos
] = '\0';
241 if (sscanf(mystr
, "%i", &v
) != 1) {
246 setWVisualID(cur_screen
, v
);
252 /* break in case last char has been consumed */
253 if (user_str
[cur_in_pos
] == '\0')
256 /* if the current char is no delimiter put it into mystr */
257 if (user_str
[cur_in_pos
] != ',') {
258 mystr
[cur_out_pos
++] = user_str
[cur_in_pos
];
265 if (cur_screen
== 0||error_found
!= 0)
271 void Exit(int status
)
279 void Restart(char *manager
, Bool abortOnFailure
)
282 char *argv
[MAX_RESTART_ARGS
];
285 if (manager
&& manager
[0] != 0) {
286 prog
= argv
[0] = strtok(manager
, " ");
287 for (i
= 1; i
< MAX_RESTART_ARGS
; i
++) {
288 argv
[i
] = strtok(NULL
, " ");
289 if (argv
[i
] == NULL
) {
299 execvp(Arguments
[0], Arguments
);
300 wfatal(_("failed to restart Window Maker."));
303 werror(_("could not exec %s"), prog
);
309 void SetupEnvironment(WScreen
* scr
)
315 int len
= strlen(DisplayName
) + 64;
317 snprintf(tmp
, len
, "DISPLAY=%s", XDisplayName(DisplayName
));
318 ptr
= strchr(strchr(tmp
, ':'), '.');
321 snprintf(buf
, sizeof(buf
), ".%i", scr
->screen
);
326 snprintf(tmp
, 60, "WRASTER_COLOR_RESOLUTION%i=%i", scr
->screen
,
327 scr
->rcontext
->attribs
->colors_per_channel
);
336 static void shellCommandHandler(pid_t pid
, unsigned char status
, _tuple
* data
)
341 buffer
= wstrconcat(_("Could not execute command: "), data
->command
);
343 wMessageDialog(data
->scr
, _("Error"), buffer
, _("OK"), NULL
, NULL
);
345 } else if (status
!= 127) {
347 printf("%s: %i\n", data->command, status);
351 wfree(data
->command
);
355 void ExecuteShellCommand(WScreen
* scr
, char *command
)
357 static char *shell
= NULL
;
361 * This have a problem: if the shell is tcsh (not sure about others)
362 * and ~/.tcshrc have /bin/stty erase ^H somewhere on it, the shell
363 * will block and the command will not be executed.
365 shell = getenv("SHELL");
376 SetupEnvironment(scr
);
381 execl(shell
, shell
, "-c", command
, NULL
);
382 werror("could not execute %s -c %s", shell
, command
);
384 } else if (pid
< 0) {
385 werror("cannot fork a new process");
387 _tuple
*data
= wmalloc(sizeof(_tuple
));
390 data
->command
= wstrdup(command
);
392 wAddDeathHandler(pid
, (WDeathHandler
*) shellCommandHandler
, data
);
397 *---------------------------------------------------------------------
399 * Launch a new instance of the active window
401 *----------------------------------------------------------------------
403 Bool
RelaunchWindow(WWindow
*wwin
)
405 if (! wwin
|| ! wwin
->client_win
) {
406 werror("no window to relaunch");
413 if (! XGetCommand(dpy
, wwin
->client_win
, &argv
, &argc
) || argc
== 0 || argv
== NULL
) {
414 werror("cannot relaunch the application because no WM_COMMAND property is set");
421 SetupEnvironment(wwin
->screen_ptr
);
425 /* argv is not null-terminated */
426 char **a
= (char **) malloc(argc
+ 1);
428 werror("out of memory trying to relaunch the application");
433 for (i
= 0; i
< argc
; i
++)
439 } else if (pid
< 0) {
440 werror("cannot fork a new process");
442 XFreeStringList(argv
);
445 _tuple
*data
= wmalloc(sizeof(_tuple
));
447 data
->scr
= wwin
->screen_ptr
;
448 data
->command
= wtokenjoin(argv
, argc
);
450 /* not actually a shell command */
451 wAddDeathHandler(pid
, (WDeathHandler
*) shellCommandHandler
, data
);
453 XFreeStringList(argv
);
460 *---------------------------------------------------------------------
462 * Do a major cleanup and exit the program
464 *----------------------------------------------------------------------
466 void wAbort(Bool dumpCore
)
471 for (i
= 0; i
< wScreenCount
; i
++) {
472 scr
= wScreenWithNumber(i
);
476 printf(_("%s aborted.\n"), ProgName
);
483 static void print_help(void)
485 printf(_("Usage: %s [options]\n"), ProgName
);
486 puts(_("The Window Maker window manager for the X window system"));
488 puts(_(" -display host:dpy display to use"));
489 puts(_(" --no-dock do not open the application Dock"));
490 puts(_(" --no-clip do not open the workspace Clip"));
491 puts(_(" --no-autolaunch do not autolaunch applications"));
492 puts(_(" --dont-restore do not restore saved session"));
494 puts(_(" --locale locale locale to use"));
496 puts(_(" --visual-id visualid visual id of visual to use"));
497 puts(_(" --static do not update or save configurations"));
499 puts(_(" --no-polling do not periodically check for configuration updates"));
501 puts(_(" --version print version and exit"));
502 puts(_(" --help show this message"));
505 static void check_defaults(void)
509 path
= wdefaultspathfordomain("WindowMaker");
511 if (access(path
, R_OK
) != 0) {
512 wwarning(_("could not find user GNUstep directory (%s)."), path
);
514 if (system("wmaker.inst --batch") != 0) {
515 wwarning(_("There was an error while creating GNUstep directory, please "
516 "make sure you have installed Window Maker correctly and run wmaker.inst"));
518 wwarning(_("%s directory created with default configuration."), path
);
527 * Add watch here, used to notify if configuration
528 * files have changed, using linux kernel inotify mechanism
531 static void inotifyWatchConfig(void)
533 char *watchPath
= NULL
;
534 inotifyFD
= inotify_init(); /* Initialise an inotify instance */
536 wwarning(_("could not initialise an inotify instance."
537 " Changes to the defaults database will require"
538 " a restart to take effect. Check your kernel!"));
540 watchPath
= wstrconcat(wusergnusteppath(), "/Defaults");
541 /* Add the watch; really we are only looking for modify events
542 * but we might want more in the future so check all events for now.
543 * The individual events are checked for in event.c.
545 inotifyWD
= inotify_add_watch(inotifyFD
, watchPath
, IN_ALL_EVENTS
);
547 wwarning(_("could not add an inotify watch on path %s."
548 "Changes to the defaults database will require"
549 " a restart to take effect."), watchPath
);
555 #endif /* HAVE_INOTIFY */
557 static void execInitScript(void)
561 paths
= wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
562 paths
= wstrappend(paths
, ":" DEF_CONFIG_PATHS
);
564 file
= wfindfile(paths
, DEF_INIT_SCRIPT
);
568 if (system(file
) != 0)
569 werror(_("%s:could not execute initialization script"), file
);
575 void ExecExitScript(void)
579 paths
= wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
580 paths
= wstrappend(paths
, ":" DEF_CONFIG_PATHS
);
582 file
= wfindfile(paths
, DEF_EXIT_SCRIPT
);
586 if (system(file
) != 0)
587 werror(_("%s:could not execute exit script"), file
);
593 int main(int argc
, char **argv
)
595 int i_am_the_monitor
, i
, len
;
598 /* setup common stuff for the monitor and wmaker itself */
599 WMInitializeApplication("WindowMaker", &argc
, argv
);
601 memset(&wPreferences
, 0, sizeof(WPreferences
));
603 wPreferences
.fallbackWMs
= WMCreateArray(8);
604 alt
= getenv("WINDOWMAKER_ALT_WM");
606 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup(alt
));
608 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("blackbox"));
609 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("metacity"));
610 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("fvwm"));
611 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("twm"));
612 WMAddToArray(wPreferences
.fallbackWMs
, NULL
);
613 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("rxvt"));
614 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("xterm"));
616 i_am_the_monitor
= 1;
618 for (i
= 1; i
< argc
; i
++) {
619 if (strncmp(argv
[i
], "--for-real", strlen("--for-real")) == 0) {
620 i_am_the_monitor
= 0;
622 } else if (strcmp(argv
[i
], "-display") == 0 || strcmp(argv
[i
], "--display") == 0) {
625 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
628 DisplayName
= argv
[i
];
632 DisplayName
= XDisplayName(DisplayName
);
633 len
= strlen(DisplayName
) + 64;
635 snprintf(str
, len
, "DISPLAY=%s", DisplayName
);
638 if (i_am_the_monitor
)
639 return MonitorLoop(argc
, argv
);
641 return real_main(argc
, argv
);
644 static int real_main(int argc
, char **argv
)
650 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);
657 Arguments
= wmalloc(sizeof(char *) * (ArgCount
+ 1));
658 for (i
= 0; i
< argc
; i
++) {
659 Arguments
[i
] = argv
[i
];
661 /* add the extra option to signal that we're just restarting wmaker */
662 Arguments
[argc
- 1] = "--for-real=";
663 Arguments
[argc
] = NULL
;
665 ProgName
= strrchr(argv
[0], '/');
672 for (i
= 1; i
< argc
; i
++) {
673 if (strcmp(argv
[i
], "-nocpp") == 0 || strcmp(argv
[i
], "--no-cpp") == 0) {
674 wwarning(_("option \"%s\" is deprecated, please remove it from your script"), argv
[i
]);
675 } else if (strcmp(argv
[i
], "--for-real") == 0) {
676 wPreferences
.flags
.restarting
= 0;
677 } else if (strcmp(argv
[i
], "--for-real=") == 0) {
678 wPreferences
.flags
.restarting
= 1;
679 } else if (strcmp(argv
[i
], "--for-real-") == 0) {
680 wPreferences
.flags
.restarting
= 2;
681 } else if (strcmp(argv
[i
], "-no-autolaunch") == 0
682 || strcmp(argv
[i
], "--no-autolaunch") == 0) {
683 wPreferences
.flags
.noautolaunch
= 1;
684 } else if (strcmp(argv
[i
], "-dont-restore") == 0 || strcmp(argv
[i
], "--dont-restore") == 0) {
685 wPreferences
.flags
.norestore
= 1;
686 } else if (strcmp(argv
[i
], "-nodock") == 0 || strcmp(argv
[i
], "--no-dock") == 0) {
687 wPreferences
.flags
.nodock
= 1;
688 wPreferences
.flags
.nodrawer
= 1;
689 } else if (strcmp(argv
[i
], "-noclip") == 0 || strcmp(argv
[i
], "--no-clip") == 0) {
690 wPreferences
.flags
.noclip
= 1;
691 } else if (strcmp(argv
[i
], "-nodrawer") == 0 || strcmp(argv
[i
], "--no-drawer") == 0) {
692 wPreferences
.flags
.nodrawer
= 1;
693 } else if (strcmp(argv
[i
], "-version") == 0 || strcmp(argv
[i
], "--version") == 0) {
694 printf("Window Maker %s\n", VERSION
);
696 } else if (strcmp(argv
[i
], "--global_defaults_path") == 0) {
697 printf("%s/%s\n", SYSCONFDIR
, GLOBAL_DEFAULTS_SUBDIR
);
699 } else if (strcmp(argv
[i
], "-locale") == 0 || strcmp(argv
[i
], "--locale") == 0) {
702 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
706 } else if (strcmp(argv
[i
], "-display") == 0 || strcmp(argv
[i
], "--display") == 0) {
709 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
712 DisplayName
= argv
[i
];
713 } else if (strcmp(argv
[i
], "-visualid") == 0 || strcmp(argv
[i
], "--visual-id") == 0) {
716 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
719 if (initWVisualID(argv
[i
]) != 0) {
720 wwarning(_("bad value for visualid: \"%s\""), argv
[i
]);
723 } else if (strcmp(argv
[i
], "-static") == 0 || strcmp(argv
[i
], "--static") == 0
725 || strcmp(argv
[i
], "--no-polling") == 0
728 wPreferences
.flags
.noupdates
= 1;
729 } else if (strcmp(argv
[i
], "--help") == 0) {
733 printf(_("%s: invalid argument '%s'\n"), argv
[0], argv
[i
]);
734 printf(_("Try '%s --help' for more information\n"), argv
[0]);
740 if (!wPreferences
.flags
.noupdates
) {
741 /* check existence of Defaults DB directory */
746 setenv("LANG", Locale
, 1);
748 Locale
= getenv("LC_ALL");
750 Locale
= getenv("LANG");
754 setlocale(LC_ALL
, "");
756 if (!Locale
|| strcmp(Locale
, "C") == 0 || strcmp(Locale
, "POSIX") == 0)
759 if (getenv("NLSPATH")) {
760 bindtextdomain("WindowMaker", getenv("NLSPATH"));
761 #if defined(MENU_TEXTDOMAIN)
762 bindtextdomain(MENU_TEXTDOMAIN
, getenv("NLSPATH"));
765 bindtextdomain("WindowMaker", LOCALEDIR
);
766 #if defined(MENU_TEXTDOMAIN)
767 bindtextdomain(MENU_TEXTDOMAIN
, LOCALEDIR
);
770 bind_textdomain_codeset("WindowMaker", "UTF-8");
771 #if defined(MENU_TEXTDOMAIN)
772 bind_textdomain_codeset(MENU_TEXTDOMAIN
, "UTF-8");
774 textdomain("WindowMaker");
776 if (!XSupportsLocale()) {
777 wwarning(_("X server does not support locale"));
780 if (XSetLocaleModifiers("") == NULL
) {
781 wwarning(_("cannot set locale modifiers"));
788 Locale
= wstrdup(Locale
);
789 ptr
= strchr(Locale
, '.');
795 dpy
= XOpenDisplay(DisplayName
);
797 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName
));
801 if (fcntl(ConnectionNumber(dpy
), F_SETFD
, FD_CLOEXEC
) < 0) {
802 werror("error setting close-on-exec flag for X connection");
807 if (getWVisualID(0) < 0) {
809 * If unspecified, use default visual instead of waiting
810 * for wrlib/context.c:bestContext() that may end up choosing
811 * the "fake" 24 bits added by the Composite extension.
812 * This is required to avoid all sort of corruptions when
813 * composite is enabled, and at a depth other than 24.
815 setWVisualID(0, (int)DefaultVisual(dpy
, DefaultScreen(dpy
))->visualid
);
818 /* check if the user specified a complete display name (with screen).
819 * If so, only manage the specified screen */
821 pos
= strchr(DisplayName
, ':');
825 if (pos
&& sscanf(pos
, ":%i.%i", &d
, &s
) == 2)
828 DisplayName
= XDisplayName(DisplayName
);
829 setenv("DISPLAY", DisplayName
, 1);
831 wXModifierInitialize();
834 if (wScreenCount
== 1)
839 inotifyWatchConfig();