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 ******/
67 struct wmaker_global_variables w_global
;
75 unsigned int ValidModMask
= 0xff;
84 struct 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 WDelayedActionSet
= 0;
150 const char WMNManaged
[] = "WMNManaged";
151 const char WMNUnmanaged
[] = "WMNUnmanaged";
152 const char WMNChangedWorkspace
[] = "WMNChangedWorkspace";
153 const char WMNChangedState
[] = "WMNChangedState";
154 const char WMNChangedFocus
[] = "WMNChangedFocus";
155 const char WMNChangedStacking
[] = "WMNChangedStacking";
156 const char WMNChangedName
[] = "WMNChangedName";
158 const char WMNWorkspaceCreated
[] = "WMNWorkspaceCreated";
159 const char WMNWorkspaceDestroyed
[] = "WMNWorkspaceDestroyed";
160 const char WMNWorkspaceChanged
[] = "WMNWorkspaceChanged";
161 const char WMNWorkspaceNameChanged
[] = "WMNWorkspaceNameChanged";
163 const char WMNResetStacking
[] = "WMNResetStacking";
165 /******** End Global Variables *****/
167 static char *DisplayName
= NULL
;
169 static char **Arguments
;
173 static Bool multiHead
= True
;
175 static int *wVisualID
= NULL
;
176 static int wVisualID_len
= 0;
178 static int real_main(int argc
, char **argv
);
180 int getWVisualID(int screen
)
182 if (wVisualID
== NULL
)
184 if (screen
< 0 || screen
>= wVisualID_len
)
187 return wVisualID
[screen
];
190 static void setWVisualID(int screen
, int val
)
197 if (wVisualID
== NULL
) {
198 /* no array at all, alloc space for screen + 1 entries
199 * and init with default value */
200 wVisualID_len
= screen
+ 1;
201 wVisualID
= (int *)malloc(wVisualID_len
* sizeof(int));
202 for (i
= 0; i
< wVisualID_len
; i
++) {
205 } else if (screen
>= wVisualID_len
) {
206 /* larger screen number than previously allocated
208 int oldlen
= wVisualID_len
;
210 wVisualID_len
= screen
+ 1;
211 wVisualID
= (int *)realloc(wVisualID
, wVisualID_len
* sizeof(int));
212 for (i
= oldlen
; i
< wVisualID_len
; i
++) {
217 wVisualID
[screen
] = val
;
221 * this function splits a given string at the comma into tokens
222 * and set the wVisualID variable to each parsed number
224 static int initWVisualID(const char *user_str
)
226 char *mystr
= strdup(user_str
);
233 /* check for delimiter */
234 if (user_str
[cur_in_pos
] == '\0' || user_str
[cur_in_pos
] == ',') {
237 mystr
[cur_out_pos
] = '\0';
239 if (sscanf(mystr
, "%i", &v
) != 1) {
244 setWVisualID(cur_screen
, v
);
250 /* break in case last char has been consumed */
251 if (user_str
[cur_in_pos
] == '\0')
254 /* if the current char is no delimiter put it into mystr */
255 if (user_str
[cur_in_pos
] != ',') {
256 mystr
[cur_out_pos
++] = user_str
[cur_in_pos
];
263 if (cur_screen
== 0||error_found
!= 0)
269 noreturn
void Exit(int status
)
277 void Restart(char *manager
, Bool abortOnFailure
)
280 char *argv
[MAX_RESTART_ARGS
];
283 if (manager
&& manager
[0] != 0) {
284 prog
= argv
[0] = strtok(manager
, " ");
285 for (i
= 1; i
< MAX_RESTART_ARGS
; i
++) {
286 argv
[i
] = strtok(NULL
, " ");
287 if (argv
[i
] == NULL
) {
297 execvp(Arguments
[0], Arguments
);
298 wfatal(_("failed to restart Window Maker."));
301 werror(_("could not exec %s"), prog
);
307 void SetupEnvironment(WScreen
* scr
)
313 int len
= strlen(DisplayName
) + 64;
315 snprintf(tmp
, len
, "DISPLAY=%s", XDisplayName(DisplayName
));
316 ptr
= strchr(strchr(tmp
, ':'), '.');
319 snprintf(buf
, sizeof(buf
), ".%i", scr
->screen
);
324 snprintf(tmp
, 60, "WRASTER_COLOR_RESOLUTION%i=%i", scr
->screen
,
325 scr
->rcontext
->attribs
->colors_per_channel
);
334 static void shellCommandHandler(pid_t pid
, unsigned char status
, _tuple
* data
)
339 buffer
= wstrconcat(_("Could not execute command: "), data
->command
);
341 wMessageDialog(data
->scr
, _("Error"), buffer
, _("OK"), NULL
, NULL
);
343 } else if (status
!= 127) {
345 printf("%s: %i\n", data->command, status);
349 wfree(data
->command
);
353 void ExecuteShellCommand(WScreen
*scr
, const char *command
)
355 static char *shell
= NULL
;
359 * This have a problem: if the shell is tcsh (not sure about others)
360 * and ~/.tcshrc have /bin/stty erase ^H somewhere on it, the shell
361 * will block and the command will not be executed.
363 shell = getenv("SHELL");
374 SetupEnvironment(scr
);
379 execl(shell
, shell
, "-c", command
, NULL
);
380 werror("could not execute %s -c %s", shell
, command
);
382 } else if (pid
< 0) {
383 werror("cannot fork a new process");
385 _tuple
*data
= wmalloc(sizeof(_tuple
));
388 data
->command
= wstrdup(command
);
390 wAddDeathHandler(pid
, (WDeathHandler
*) shellCommandHandler
, data
);
395 *---------------------------------------------------------------------
397 * Launch a new instance of the active window
399 *----------------------------------------------------------------------
401 Bool
RelaunchWindow(WWindow
*wwin
)
403 if (! wwin
|| ! wwin
->client_win
) {
404 werror("no window to relaunch");
411 if (! XGetCommand(dpy
, wwin
->client_win
, &argv
, &argc
) || argc
== 0 || argv
== NULL
) {
412 werror("cannot relaunch the application because no WM_COMMAND property is set");
419 SetupEnvironment(wwin
->screen_ptr
);
423 /* argv is not null-terminated */
424 char **a
= (char **) malloc(argc
+ 1);
426 werror("out of memory trying to relaunch the application");
431 for (i
= 0; i
< argc
; i
++)
437 } else if (pid
< 0) {
438 werror("cannot fork a new process");
440 XFreeStringList(argv
);
443 _tuple
*data
= wmalloc(sizeof(_tuple
));
445 data
->scr
= wwin
->screen_ptr
;
446 data
->command
= wtokenjoin(argv
, argc
);
448 /* not actually a shell command */
449 wAddDeathHandler(pid
, (WDeathHandler
*) shellCommandHandler
, data
);
451 XFreeStringList(argv
);
458 *---------------------------------------------------------------------
460 * Do a major cleanup and exit the program
462 *----------------------------------------------------------------------
464 noreturn
void wAbort(Bool dumpCore
)
469 for (i
= 0; i
< wScreenCount
; i
++) {
470 scr
= wScreenWithNumber(i
);
474 printf(_("%s aborted.\n"), ProgName
);
481 static void print_help(void)
483 printf(_("Usage: %s [options]\n"), ProgName
);
484 puts(_("The Window Maker window manager for the X window system"));
486 puts(_(" -display host:dpy display to use"));
487 puts(_(" --no-dock do not open the application Dock"));
488 puts(_(" --no-clip do not open the workspace Clip"));
489 puts(_(" --no-autolaunch do not autolaunch applications"));
490 puts(_(" --dont-restore do not restore saved session"));
492 puts(_(" --locale locale locale to use"));
494 puts(_(" --visual-id visualid visual id of visual to use"));
495 puts(_(" --static do not update or save configurations"));
497 puts(_(" --no-polling do not periodically check for configuration updates"));
499 puts(_(" --version print version and exit"));
500 puts(_(" --help show this message"));
503 static void check_defaults(void)
507 path
= wdefaultspathfordomain("WindowMaker");
509 if (access(path
, R_OK
) != 0) {
510 wwarning(_("could not find user GNUstep directory (%s)."), path
);
512 if (system("wmaker.inst --batch") != 0) {
513 wwarning(_("There was an error while creating GNUstep directory, please "
514 "make sure you have installed Window Maker correctly and run wmaker.inst"));
516 wwarning(_("%s directory created with default configuration."), path
);
525 * Add watch here, used to notify if configuration
526 * files have changed, using linux kernel inotify mechanism
529 static void inotifyWatchConfig(void)
531 char *watchPath
= NULL
;
532 inotifyFD
= inotify_init(); /* Initialise an inotify instance */
534 wwarning(_("could not initialise an inotify instance."
535 " Changes to the defaults database will require"
536 " a restart to take effect. Check your kernel!"));
538 watchPath
= wstrconcat(wusergnusteppath(), "/Defaults");
539 /* Add the watch; really we are only looking for modify events
540 * but we might want more in the future so check all events for now.
541 * The individual events are checked for in event.c.
543 inotifyWD
= inotify_add_watch(inotifyFD
, watchPath
, IN_ALL_EVENTS
);
545 wwarning(_("could not add an inotify watch on path %s."
546 "Changes to the defaults database will require"
547 " a restart to take effect."), watchPath
);
553 #endif /* HAVE_INOTIFY */
555 static void execInitScript(void)
559 paths
= wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
560 paths
= wstrappend(paths
, ":" DEF_CONFIG_PATHS
);
562 file
= wfindfile(paths
, DEF_INIT_SCRIPT
);
566 if (system(file
) != 0)
567 werror(_("%s:could not execute initialization script"), file
);
573 void ExecExitScript(void)
577 paths
= wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
578 paths
= wstrappend(paths
, ":" DEF_CONFIG_PATHS
);
580 file
= wfindfile(paths
, DEF_EXIT_SCRIPT
);
584 if (system(file
) != 0)
585 werror(_("%s:could not execute exit script"), file
);
591 int main(int argc
, char **argv
)
593 int i_am_the_monitor
, i
, len
;
596 memset(&w_global
, 0, sizeof(w_global
));
597 w_global
.program
.state
= WSTATE_NORMAL
;
598 w_global
.program
.signal_state
= WSTATE_NORMAL
;
600 /* setup common stuff for the monitor and wmaker itself */
601 WMInitializeApplication("WindowMaker", &argc
, argv
);
603 memset(&wPreferences
, 0, sizeof(wPreferences
));
605 wPreferences
.fallbackWMs
= WMCreateArray(8);
606 alt
= getenv("WINDOWMAKER_ALT_WM");
608 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup(alt
));
610 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("blackbox"));
611 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("metacity"));
612 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("fvwm"));
613 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("twm"));
614 WMAddToArray(wPreferences
.fallbackWMs
, NULL
);
615 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("rxvt"));
616 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("xterm"));
618 i_am_the_monitor
= 1;
620 for (i
= 1; i
< argc
; i
++) {
621 if (strncmp(argv
[i
], "--for-real", strlen("--for-real")) == 0) {
622 i_am_the_monitor
= 0;
624 } else if (strcmp(argv
[i
], "-display") == 0 || strcmp(argv
[i
], "--display") == 0) {
627 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
630 DisplayName
= argv
[i
];
634 DisplayName
= XDisplayName(DisplayName
);
635 len
= strlen(DisplayName
) + 64;
637 snprintf(str
, len
, "DISPLAY=%s", DisplayName
);
640 if (i_am_the_monitor
)
641 return MonitorLoop(argc
, argv
);
643 return real_main(argc
, argv
);
646 static int real_main(int argc
, char **argv
)
652 setlocale(LC_ALL
, "");
655 /* for telling WPrefs what's the name of the wmaker binary being ran */
656 setenv("WMAKER_BIN_NAME", argv
[0], 1);
659 Arguments
= wmalloc(sizeof(char *) * (ArgCount
+ 1));
660 for (i
= 0; i
< argc
; i
++) {
661 Arguments
[i
] = argv
[i
];
663 /* add the extra option to signal that we're just restarting wmaker */
664 Arguments
[argc
- 1] = "--for-real=";
665 Arguments
[argc
] = NULL
;
667 ProgName
= strrchr(argv
[0], '/');
674 for (i
= 1; i
< argc
; i
++) {
675 if (strcmp(argv
[i
], "-nocpp") == 0 || strcmp(argv
[i
], "--no-cpp") == 0) {
676 wwarning(_("option \"%s\" is deprecated, please remove it from your script"), argv
[i
]);
677 } else if (strcmp(argv
[i
], "--for-real") == 0) {
678 wPreferences
.flags
.restarting
= 0;
679 } else if (strcmp(argv
[i
], "--for-real=") == 0) {
680 wPreferences
.flags
.restarting
= 1;
681 } else if (strcmp(argv
[i
], "--for-real-") == 0) {
682 wPreferences
.flags
.restarting
= 2;
683 } else if (strcmp(argv
[i
], "-no-autolaunch") == 0
684 || strcmp(argv
[i
], "--no-autolaunch") == 0) {
685 wPreferences
.flags
.noautolaunch
= 1;
686 } else if (strcmp(argv
[i
], "-dont-restore") == 0 || strcmp(argv
[i
], "--dont-restore") == 0) {
687 wPreferences
.flags
.norestore
= 1;
688 } else if (strcmp(argv
[i
], "-nodock") == 0 || strcmp(argv
[i
], "--no-dock") == 0) {
689 wPreferences
.flags
.nodock
= 1;
690 wPreferences
.flags
.nodrawer
= 1;
691 } else if (strcmp(argv
[i
], "-noclip") == 0 || strcmp(argv
[i
], "--no-clip") == 0) {
692 wPreferences
.flags
.noclip
= 1;
693 } else if (strcmp(argv
[i
], "-nodrawer") == 0 || strcmp(argv
[i
], "--no-drawer") == 0) {
694 wPreferences
.flags
.nodrawer
= 1;
695 } else if (strcmp(argv
[i
], "-version") == 0 || strcmp(argv
[i
], "--version") == 0) {
696 printf("Window Maker %s\n", VERSION
);
698 } else if (strcmp(argv
[i
], "--global_defaults_path") == 0) {
699 printf("%s/%s\n", SYSCONFDIR
, GLOBAL_DEFAULTS_SUBDIR
);
701 } else if (strcmp(argv
[i
], "-locale") == 0 || strcmp(argv
[i
], "--locale") == 0) {
704 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
707 w_global
.locale
= argv
[i
];
708 } else if (strcmp(argv
[i
], "-display") == 0 || strcmp(argv
[i
], "--display") == 0) {
711 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
714 DisplayName
= argv
[i
];
715 } else if (strcmp(argv
[i
], "-visualid") == 0 || strcmp(argv
[i
], "--visual-id") == 0) {
718 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
721 if (initWVisualID(argv
[i
]) != 0) {
722 wwarning(_("bad value for visualid: \"%s\""), argv
[i
]);
725 } else if (strcmp(argv
[i
], "-static") == 0 || strcmp(argv
[i
], "--static") == 0
727 || strcmp(argv
[i
], "--no-polling") == 0
730 wPreferences
.flags
.noupdates
= 1;
731 } else if (strcmp(argv
[i
], "--help") == 0) {
735 printf(_("%s: invalid argument '%s'\n"), argv
[0], argv
[i
]);
736 printf(_("Try '%s --help' for more information\n"), argv
[0]);
742 if (!wPreferences
.flags
.noupdates
) {
743 /* check existence of Defaults DB directory */
747 if (w_global
.locale
) {
748 setenv("LANG", w_global
.locale
, 1);
750 w_global
.locale
= getenv("LC_ALL");
751 if (!w_global
.locale
) {
752 w_global
.locale
= getenv("LANG");
756 setlocale(LC_ALL
, "");
758 if (!w_global
.locale
|| strcmp(w_global
.locale
, "C") == 0 || strcmp(w_global
.locale
, "POSIX") == 0)
759 w_global
.locale
= NULL
;
761 if (getenv("NLSPATH")) {
762 bindtextdomain("WindowMaker", getenv("NLSPATH"));
763 #if defined(MENU_TEXTDOMAIN)
764 bindtextdomain(MENU_TEXTDOMAIN
, getenv("NLSPATH"));
767 bindtextdomain("WindowMaker", LOCALEDIR
);
768 #if defined(MENU_TEXTDOMAIN)
769 bindtextdomain(MENU_TEXTDOMAIN
, LOCALEDIR
);
772 bind_textdomain_codeset("WindowMaker", "UTF-8");
773 #if defined(MENU_TEXTDOMAIN)
774 bind_textdomain_codeset(MENU_TEXTDOMAIN
, "UTF-8");
776 textdomain("WindowMaker");
778 if (!XSupportsLocale()) {
779 wwarning(_("X server does not support locale"));
782 if (XSetLocaleModifiers("") == NULL
) {
783 wwarning(_("cannot set locale modifiers"));
787 if (w_global
.locale
) {
790 w_global
.locale
= wstrdup(w_global
.locale
);
791 ptr
= strchr(w_global
.locale
, '.');
797 dpy
= XOpenDisplay(DisplayName
);
799 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName
));
803 if (fcntl(ConnectionNumber(dpy
), F_SETFD
, FD_CLOEXEC
) < 0) {
804 werror("error setting close-on-exec flag for X connection");
809 if (getWVisualID(0) < 0) {
811 * If unspecified, use default visual instead of waiting
812 * for wrlib/context.c:bestContext() that may end up choosing
813 * the "fake" 24 bits added by the Composite extension.
814 * This is required to avoid all sort of corruptions when
815 * composite is enabled, and at a depth other than 24.
817 setWVisualID(0, (int)DefaultVisual(dpy
, DefaultScreen(dpy
))->visualid
);
820 /* check if the user specified a complete display name (with screen).
821 * If so, only manage the specified screen */
823 pos
= strchr(DisplayName
, ':');
827 if (pos
&& sscanf(pos
, ":%i.%i", &d
, &s
) == 2)
830 DisplayName
= XDisplayName(DisplayName
);
831 setenv("DISPLAY", DisplayName
, 1);
833 wXModifierInitialize();
836 if (wScreenCount
== 1)
841 inotifyWatchConfig();