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"
60 #include <WINGs/WUtil.h>
62 #ifndef GLOBAL_DEFAULTS_SUBDIR
63 #define GLOBAL_DEFAULTS_SUBDIR "WindowMaker"
66 /****** Global Variables ******/
74 unsigned int ValidModMask
= 0xff;
80 /* locale to use. NULL==POSIX or C */
85 struct WPreferences wPreferences
;
87 WShortKey wKeyBindings
[WKBD_LAST
];
89 /* defaults domains */
90 WDDomain
*WDWindowMaker
= NULL
;
91 WDDomain
*WDWindowAttributes
= NULL
;
92 WDDomain
*WDRootMenu
= NULL
;
96 XContext wAppWinContext
;
97 XContext wStackContext
;
98 XContext wVEdgeContext
;
102 Atom _XA_WM_CHANGE_STATE
;
103 Atom _XA_WM_PROTOCOLS
;
104 Atom _XA_WM_TAKE_FOCUS
;
105 Atom _XA_WM_DELETE_WINDOW
;
106 Atom _XA_WM_SAVE_YOURSELF
;
107 Atom _XA_WM_CLIENT_LEADER
;
108 Atom _XA_WM_COLORMAP_WINDOWS
;
109 Atom _XA_WM_COLORMAP_NOTIFY
;
111 Atom _XA_GNUSTEP_WM_ATTR
;
112 Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW
;
113 Atom _XA_GNUSTEP_WM_RESIZEBAR
;
114 Atom _XA_GNUSTEP_TITLEBAR_STATE
;
116 Atom _XA_WINDOWMAKER_MENU
;
117 Atom _XA_WINDOWMAKER_WM_PROTOCOLS
;
118 Atom _XA_WINDOWMAKER_STATE
;
120 Atom _XA_WINDOWMAKER_WM_FUNCTION
;
121 Atom _XA_WINDOWMAKER_NOTICEBOARD
;
122 Atom _XA_WINDOWMAKER_COMMAND
;
124 Atom _XA_WINDOWMAKER_ICON_SIZE
;
125 Atom _XA_WINDOWMAKER_ICON_TILE
;
127 Atom _XA_WM_IGNORE_FOCUS_EVENTS
;
130 Cursor wCursor
[WCUR_LAST
];
132 /* last event timestamp for XSetInputFocus */
133 Time LastTimestamp
= CurrentTime
;
134 /* timestamp on the last time we did XSetInputFocus() */
135 Time LastFocusChange
= CurrentTime
;
138 Bool wShapeSupported
;
142 #ifdef KEEP_XKB_LOCK_STATUS
148 char WProgramSigState
= 0;
149 char WProgramState
= WSTATE_NORMAL
;
150 char WDelayedActionSet
= 0;
153 const char WMNManaged
[] = "WMNManaged";
154 const char WMNUnmanaged
[] = "WMNUnmanaged";
155 const char WMNChangedWorkspace
[] = "WMNChangedWorkspace";
156 const char WMNChangedState
[] = "WMNChangedState";
157 const char WMNChangedFocus
[] = "WMNChangedFocus";
158 const char WMNChangedStacking
[] = "WMNChangedStacking";
159 const char WMNChangedName
[] = "WMNChangedName";
161 const char WMNWorkspaceCreated
[] = "WMNWorkspaceCreated";
162 const char WMNWorkspaceDestroyed
[] = "WMNWorkspaceDestroyed";
163 const char WMNWorkspaceChanged
[] = "WMNWorkspaceChanged";
164 const char WMNWorkspaceNameChanged
[] = "WMNWorkspaceNameChanged";
166 const char WMNResetStacking
[] = "WMNResetStacking";
168 /******** End Global Variables *****/
170 static char *DisplayName
= NULL
;
172 static char **Arguments
;
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 noreturn
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 werror(_("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
, const 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 werror("could not execute %s -c %s", shell
, command
);
385 } else if (pid
< 0) {
386 werror("cannot fork a new process");
388 _tuple
*data
= wmalloc(sizeof(_tuple
));
391 data
->command
= wstrdup(command
);
393 wAddDeathHandler(pid
, (WDeathHandler
*) shellCommandHandler
, data
);
398 *---------------------------------------------------------------------
400 * Launch a new instance of the active window
402 *----------------------------------------------------------------------
404 Bool
RelaunchWindow(WWindow
*wwin
)
406 if (! wwin
|| ! wwin
->client_win
) {
407 werror("no window to relaunch");
414 if (! XGetCommand(dpy
, wwin
->client_win
, &argv
, &argc
) || argc
== 0 || argv
== NULL
) {
415 werror("cannot relaunch the application because no WM_COMMAND property is set");
422 SetupEnvironment(wwin
->screen_ptr
);
426 /* argv is not null-terminated */
427 char **a
= (char **) malloc(argc
+ 1);
429 werror("out of memory trying to relaunch the application");
434 for (i
= 0; i
< argc
; i
++)
440 } else if (pid
< 0) {
441 werror("cannot fork a new process");
443 XFreeStringList(argv
);
446 _tuple
*data
= wmalloc(sizeof(_tuple
));
448 data
->scr
= wwin
->screen_ptr
;
449 data
->command
= wtokenjoin(argv
, argc
);
451 /* not actually a shell command */
452 wAddDeathHandler(pid
, (WDeathHandler
*) shellCommandHandler
, data
);
454 XFreeStringList(argv
);
461 *---------------------------------------------------------------------
463 * Do a major cleanup and exit the program
465 *----------------------------------------------------------------------
467 noreturn
void wAbort(Bool dumpCore
)
472 for (i
= 0; i
< wScreenCount
; i
++) {
473 scr
= wScreenWithNumber(i
);
477 printf(_("%s aborted.\n"), ProgName
);
484 static void print_help(void)
486 printf(_("Usage: %s [options]\n"), ProgName
);
487 puts(_("The Window Maker window manager for the X window system"));
489 puts(_(" -display host:dpy display to use"));
490 puts(_(" --no-dock do not open the application Dock"));
491 puts(_(" --no-clip do not open the workspace Clip"));
492 puts(_(" --no-autolaunch do not autolaunch applications"));
493 puts(_(" --dont-restore do not restore saved session"));
495 puts(_(" --locale locale locale to use"));
497 puts(_(" --visual-id visualid visual id of visual to use"));
498 puts(_(" --static do not update or save configurations"));
500 puts(_(" --no-polling do not periodically check for configuration updates"));
502 puts(_(" --version print version and exit"));
503 puts(_(" --help show this message"));
506 static void check_defaults(void)
510 path
= wdefaultspathfordomain("WindowMaker");
512 if (access(path
, R_OK
) != 0) {
513 wwarning(_("could not find user GNUstep directory (%s)."), path
);
515 if (system("wmaker.inst --batch") != 0) {
516 wwarning(_("There was an error while creating GNUstep directory, please "
517 "make sure you have installed Window Maker correctly and run wmaker.inst"));
519 wwarning(_("%s directory created with default configuration."), path
);
528 * Add watch here, used to notify if configuration
529 * files have changed, using linux kernel inotify mechanism
532 static void inotifyWatchConfig(void)
534 char *watchPath
= NULL
;
535 inotifyFD
= inotify_init(); /* Initialise an inotify instance */
537 wwarning(_("could not initialise an inotify instance."
538 " Changes to the defaults database will require"
539 " a restart to take effect. Check your kernel!"));
541 watchPath
= wstrconcat(wusergnusteppath(), "/Defaults");
542 /* Add the watch; really we are only looking for modify events
543 * but we might want more in the future so check all events for now.
544 * The individual events are checked for in event.c.
546 inotifyWD
= inotify_add_watch(inotifyFD
, watchPath
, IN_ALL_EVENTS
);
548 wwarning(_("could not add an inotify watch on path %s."
549 "Changes to the defaults database will require"
550 " a restart to take effect."), watchPath
);
556 #endif /* HAVE_INOTIFY */
558 static void execInitScript(void)
562 paths
= wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
563 paths
= wstrappend(paths
, ":" DEF_CONFIG_PATHS
);
565 file
= wfindfile(paths
, DEF_INIT_SCRIPT
);
569 if (system(file
) != 0)
570 werror(_("%s:could not execute initialization script"), file
);
576 void ExecExitScript(void)
580 paths
= wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
581 paths
= wstrappend(paths
, ":" DEF_CONFIG_PATHS
);
583 file
= wfindfile(paths
, DEF_EXIT_SCRIPT
);
587 if (system(file
) != 0)
588 werror(_("%s:could not execute exit script"), file
);
594 int main(int argc
, char **argv
)
596 int i_am_the_monitor
, i
, len
;
599 /* setup common stuff for the monitor and wmaker itself */
600 WMInitializeApplication("WindowMaker", &argc
, argv
);
602 memset(&wPreferences
, 0, sizeof(wPreferences
));
604 wPreferences
.fallbackWMs
= WMCreateArray(8);
605 alt
= getenv("WINDOWMAKER_ALT_WM");
607 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup(alt
));
609 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("blackbox"));
610 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("metacity"));
611 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("fvwm"));
612 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("twm"));
613 WMAddToArray(wPreferences
.fallbackWMs
, NULL
);
614 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("rxvt"));
615 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("xterm"));
617 i_am_the_monitor
= 1;
619 for (i
= 1; i
< argc
; i
++) {
620 if (strncmp(argv
[i
], "--for-real", strlen("--for-real")) == 0) {
621 i_am_the_monitor
= 0;
623 } else if (strcmp(argv
[i
], "-display") == 0 || strcmp(argv
[i
], "--display") == 0) {
626 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
629 DisplayName
= argv
[i
];
633 DisplayName
= XDisplayName(DisplayName
);
634 len
= strlen(DisplayName
) + 64;
636 snprintf(str
, len
, "DISPLAY=%s", DisplayName
);
639 if (i_am_the_monitor
)
640 return MonitorLoop(argc
, argv
);
642 return real_main(argc
, argv
);
645 static int real_main(int argc
, char **argv
)
651 setlocale(LC_ALL
, "");
654 /* for telling WPrefs what's the name of the wmaker binary being ran */
655 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], '/');
673 for (i
= 1; i
< argc
; i
++) {
674 if (strcmp(argv
[i
], "-nocpp") == 0 || strcmp(argv
[i
], "--no-cpp") == 0) {
675 wwarning(_("option \"%s\" is deprecated, please remove it from your script"), argv
[i
]);
676 } else if (strcmp(argv
[i
], "--for-real") == 0) {
677 wPreferences
.flags
.restarting
= 0;
678 } else if (strcmp(argv
[i
], "--for-real=") == 0) {
679 wPreferences
.flags
.restarting
= 1;
680 } else if (strcmp(argv
[i
], "--for-real-") == 0) {
681 wPreferences
.flags
.restarting
= 2;
682 } else if (strcmp(argv
[i
], "-no-autolaunch") == 0
683 || strcmp(argv
[i
], "--no-autolaunch") == 0) {
684 wPreferences
.flags
.noautolaunch
= 1;
685 } else if (strcmp(argv
[i
], "-dont-restore") == 0 || strcmp(argv
[i
], "--dont-restore") == 0) {
686 wPreferences
.flags
.norestore
= 1;
687 } else if (strcmp(argv
[i
], "-nodock") == 0 || strcmp(argv
[i
], "--no-dock") == 0) {
688 wPreferences
.flags
.nodock
= 1;
689 wPreferences
.flags
.nodrawer
= 1;
690 } else if (strcmp(argv
[i
], "-noclip") == 0 || strcmp(argv
[i
], "--no-clip") == 0) {
691 wPreferences
.flags
.noclip
= 1;
692 } else if (strcmp(argv
[i
], "-nodrawer") == 0 || strcmp(argv
[i
], "--no-drawer") == 0) {
693 wPreferences
.flags
.nodrawer
= 1;
694 } else if (strcmp(argv
[i
], "-version") == 0 || strcmp(argv
[i
], "--version") == 0) {
695 printf("Window Maker %s\n", VERSION
);
697 } else if (strcmp(argv
[i
], "--global_defaults_path") == 0) {
698 printf("%s/%s\n", SYSCONFDIR
, GLOBAL_DEFAULTS_SUBDIR
);
700 } else if (strcmp(argv
[i
], "-locale") == 0 || strcmp(argv
[i
], "--locale") == 0) {
703 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
707 } else if (strcmp(argv
[i
], "-display") == 0 || strcmp(argv
[i
], "--display") == 0) {
710 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
713 DisplayName
= argv
[i
];
714 } else if (strcmp(argv
[i
], "-visualid") == 0 || strcmp(argv
[i
], "--visual-id") == 0) {
717 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
720 if (initWVisualID(argv
[i
]) != 0) {
721 wwarning(_("bad value for visualid: \"%s\""), argv
[i
]);
724 } else if (strcmp(argv
[i
], "-static") == 0 || strcmp(argv
[i
], "--static") == 0
726 || strcmp(argv
[i
], "--no-polling") == 0
729 wPreferences
.flags
.noupdates
= 1;
730 } else if (strcmp(argv
[i
], "--help") == 0) {
734 printf(_("%s: invalid argument '%s'\n"), argv
[0], argv
[i
]);
735 printf(_("Try '%s --help' for more information\n"), argv
[0]);
741 if (!wPreferences
.flags
.noupdates
) {
742 /* check existence of Defaults DB directory */
747 setenv("LANG", Locale
, 1);
749 Locale
= getenv("LC_ALL");
751 Locale
= getenv("LANG");
755 setlocale(LC_ALL
, "");
757 if (!Locale
|| strcmp(Locale
, "C") == 0 || strcmp(Locale
, "POSIX") == 0)
760 if (getenv("NLSPATH")) {
761 bindtextdomain("WindowMaker", getenv("NLSPATH"));
762 #if defined(MENU_TEXTDOMAIN)
763 bindtextdomain(MENU_TEXTDOMAIN
, getenv("NLSPATH"));
766 bindtextdomain("WindowMaker", LOCALEDIR
);
767 #if defined(MENU_TEXTDOMAIN)
768 bindtextdomain(MENU_TEXTDOMAIN
, LOCALEDIR
);
771 bind_textdomain_codeset("WindowMaker", "UTF-8");
772 #if defined(MENU_TEXTDOMAIN)
773 bind_textdomain_codeset(MENU_TEXTDOMAIN
, "UTF-8");
775 textdomain("WindowMaker");
777 if (!XSupportsLocale()) {
778 wwarning(_("X server does not support locale"));
781 if (XSetLocaleModifiers("") == NULL
) {
782 wwarning(_("cannot set locale modifiers"));
789 Locale
= wstrdup(Locale
);
790 ptr
= strchr(Locale
, '.');
796 dpy
= XOpenDisplay(DisplayName
);
798 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName
));
802 if (fcntl(ConnectionNumber(dpy
), F_SETFD
, FD_CLOEXEC
) < 0) {
803 werror("error setting close-on-exec flag for X connection");
808 if (getWVisualID(0) < 0) {
810 * If unspecified, use default visual instead of waiting
811 * for wrlib/context.c:bestContext() that may end up choosing
812 * the "fake" 24 bits added by the Composite extension.
813 * This is required to avoid all sort of corruptions when
814 * composite is enabled, and at a depth other than 24.
816 setWVisualID(0, (int)DefaultVisual(dpy
, DefaultScreen(dpy
))->visualid
);
819 /* check if the user specified a complete display name (with screen).
820 * If so, only manage the specified screen */
822 pos
= strchr(DisplayName
, ':');
826 if (pos
&& sscanf(pos
, ":%i.%i", &d
, &s
) == 2)
829 DisplayName
= XDisplayName(DisplayName
);
830 setenv("DISPLAY", DisplayName
, 1);
832 wXModifierInitialize();
835 if (wScreenCount
== 1)
840 inotifyWatchConfig();