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"
57 #include <WINGs/WUtil.h>
59 #ifndef GLOBAL_DEFAULTS_SUBDIR
60 #define GLOBAL_DEFAULTS_SUBDIR "WindowMaker"
63 /****** Global Variables ******/
71 unsigned int ValidModMask
= 0xff;
77 /* locale to use. NULL==POSIX or C */
82 WPreferences wPreferences
;
84 WShortKey wKeyBindings
[WKBD_LAST
];
86 /* defaults domains */
87 WDDomain
*WDWindowMaker
= NULL
;
88 WDDomain
*WDWindowAttributes
= NULL
;
89 WDDomain
*WDRootMenu
= NULL
;
93 XContext wAppWinContext
;
94 XContext wStackContext
;
95 XContext wVEdgeContext
;
99 Atom _XA_WM_CHANGE_STATE
;
100 Atom _XA_WM_PROTOCOLS
;
101 Atom _XA_WM_TAKE_FOCUS
;
102 Atom _XA_WM_DELETE_WINDOW
;
103 Atom _XA_WM_SAVE_YOURSELF
;
104 Atom _XA_WM_CLIENT_LEADER
;
105 Atom _XA_WM_COLORMAP_WINDOWS
;
106 Atom _XA_WM_COLORMAP_NOTIFY
;
108 Atom _XA_GNUSTEP_WM_ATTR
;
109 Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW
;
110 Atom _XA_GNUSTEP_WM_RESIZEBAR
;
111 Atom _XA_GNUSTEP_TITLEBAR_STATE
;
113 Atom _XA_WINDOWMAKER_MENU
;
114 Atom _XA_WINDOWMAKER_WM_PROTOCOLS
;
115 Atom _XA_WINDOWMAKER_STATE
;
117 Atom _XA_WINDOWMAKER_WM_FUNCTION
;
118 Atom _XA_WINDOWMAKER_NOTICEBOARD
;
119 Atom _XA_WINDOWMAKER_COMMAND
;
121 Atom _XA_WINDOWMAKER_ICON_SIZE
;
122 Atom _XA_WINDOWMAKER_ICON_TILE
;
124 Atom _XA_WM_IGNORE_FOCUS_EVENTS
;
127 Cursor wCursor
[WCUR_LAST
];
129 /* last event timestamp for XSetInputFocus */
130 Time LastTimestamp
= CurrentTime
;
131 /* timestamp on the last time we did XSetInputFocus() */
132 Time LastFocusChange
= CurrentTime
;
135 Bool wShapeSupported
;
139 #ifdef KEEP_XKB_LOCK_STATUS
145 char WProgramSigState
= 0;
146 char WProgramState
= WSTATE_NORMAL
;
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 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
, 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 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 /* setup common stuff for the monitor and wmaker itself */
597 WMInitializeApplication("WindowMaker", &argc
, argv
);
599 memset(&wPreferences
, 0, sizeof(WPreferences
));
601 wPreferences
.fallbackWMs
= WMCreateArray(8);
602 alt
= getenv("WINDOWMAKER_ALT_WM");
604 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup(alt
));
606 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("blackbox"));
607 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("metacity"));
608 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("fvwm"));
609 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("twm"));
610 WMAddToArray(wPreferences
.fallbackWMs
, NULL
);
611 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("rxvt"));
612 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("xterm"));
614 i_am_the_monitor
= 1;
616 for (i
= 1; i
< argc
; i
++) {
617 if (strncmp(argv
[i
], "--for-real", strlen("--for-real")) == 0) {
618 i_am_the_monitor
= 0;
620 } else if (strcmp(argv
[i
], "-display") == 0 || strcmp(argv
[i
], "--display") == 0) {
623 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
626 DisplayName
= argv
[i
];
630 DisplayName
= XDisplayName(DisplayName
);
631 len
= strlen(DisplayName
) + 64;
633 snprintf(str
, len
, "DISPLAY=%s", DisplayName
);
636 if (i_am_the_monitor
)
637 return MonitorLoop(argc
, argv
);
639 return real_main(argc
, argv
);
642 static int real_main(int argc
, char **argv
)
648 setlocale(LC_ALL
, "");
651 /* for telling WPrefs what's the name of the wmaker binary being ran */
652 setenv("WMAKER_BIN_NAME", argv
[0], 1);
655 Arguments
= wmalloc(sizeof(char *) * (ArgCount
+ 1));
656 for (i
= 0; i
< argc
; i
++) {
657 Arguments
[i
] = argv
[i
];
659 /* add the extra option to signal that we're just restarting wmaker */
660 Arguments
[argc
- 1] = "--for-real=";
661 Arguments
[argc
] = NULL
;
663 ProgName
= strrchr(argv
[0], '/');
670 for (i
= 1; i
< argc
; i
++) {
671 if (strcmp(argv
[i
], "-nocpp") == 0 || strcmp(argv
[i
], "--no-cpp") == 0) {
672 wwarning(_("option \"%s\" is deprecated, please remove it from your script"), argv
[i
]);
673 } else if (strcmp(argv
[i
], "--for-real") == 0) {
674 wPreferences
.flags
.restarting
= 0;
675 } else if (strcmp(argv
[i
], "--for-real=") == 0) {
676 wPreferences
.flags
.restarting
= 1;
677 } else if (strcmp(argv
[i
], "--for-real-") == 0) {
678 wPreferences
.flags
.restarting
= 2;
679 } else if (strcmp(argv
[i
], "-no-autolaunch") == 0
680 || strcmp(argv
[i
], "--no-autolaunch") == 0) {
681 wPreferences
.flags
.noautolaunch
= 1;
682 } else if (strcmp(argv
[i
], "-dont-restore") == 0 || strcmp(argv
[i
], "--dont-restore") == 0) {
683 wPreferences
.flags
.norestore
= 1;
684 } else if (strcmp(argv
[i
], "-nodock") == 0 || strcmp(argv
[i
], "--no-dock") == 0) {
685 wPreferences
.flags
.nodock
= 1;
686 wPreferences
.flags
.nodrawer
= 1;
687 } else if (strcmp(argv
[i
], "-noclip") == 0 || strcmp(argv
[i
], "--no-clip") == 0) {
688 wPreferences
.flags
.noclip
= 1;
689 } else if (strcmp(argv
[i
], "-nodrawer") == 0 || strcmp(argv
[i
], "--no-drawer") == 0) {
690 wPreferences
.flags
.nodrawer
= 1;
691 } else if (strcmp(argv
[i
], "-version") == 0 || strcmp(argv
[i
], "--version") == 0) {
692 printf("Window Maker %s\n", VERSION
);
694 } else if (strcmp(argv
[i
], "--global_defaults_path") == 0) {
695 printf("%s/%s\n", SYSCONFDIR
, GLOBAL_DEFAULTS_SUBDIR
);
697 } else if (strcmp(argv
[i
], "-locale") == 0 || strcmp(argv
[i
], "--locale") == 0) {
700 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
704 } else if (strcmp(argv
[i
], "-display") == 0 || strcmp(argv
[i
], "--display") == 0) {
707 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
710 DisplayName
= argv
[i
];
711 } else if (strcmp(argv
[i
], "-visualid") == 0 || strcmp(argv
[i
], "--visual-id") == 0) {
714 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
717 if (initWVisualID(argv
[i
]) != 0) {
718 wwarning(_("bad value for visualid: \"%s\""), argv
[i
]);
721 } else if (strcmp(argv
[i
], "-static") == 0 || strcmp(argv
[i
], "--static") == 0
723 || strcmp(argv
[i
], "--no-polling") == 0
726 wPreferences
.flags
.noupdates
= 1;
727 } else if (strcmp(argv
[i
], "--help") == 0) {
731 printf(_("%s: invalid argument '%s'\n"), argv
[0], argv
[i
]);
732 printf(_("Try '%s --help' for more information\n"), argv
[0]);
738 if (!wPreferences
.flags
.noupdates
) {
739 /* check existence of Defaults DB directory */
744 setenv("LANG", Locale
, 1);
746 Locale
= getenv("LC_ALL");
748 Locale
= getenv("LANG");
752 setlocale(LC_ALL
, "");
754 if (!Locale
|| strcmp(Locale
, "C") == 0 || strcmp(Locale
, "POSIX") == 0)
757 if (getenv("NLSPATH")) {
758 bindtextdomain("WindowMaker", getenv("NLSPATH"));
759 #if defined(MENU_TEXTDOMAIN)
760 bindtextdomain(MENU_TEXTDOMAIN
, getenv("NLSPATH"));
763 bindtextdomain("WindowMaker", LOCALEDIR
);
764 #if defined(MENU_TEXTDOMAIN)
765 bindtextdomain(MENU_TEXTDOMAIN
, LOCALEDIR
);
768 bind_textdomain_codeset("WindowMaker", "UTF-8");
769 #if defined(MENU_TEXTDOMAIN)
770 bind_textdomain_codeset(MENU_TEXTDOMAIN
, "UTF-8");
772 textdomain("WindowMaker");
774 if (!XSupportsLocale()) {
775 wwarning(_("X server does not support locale"));
778 if (XSetLocaleModifiers("") == NULL
) {
779 wwarning(_("cannot set locale modifiers"));
786 Locale
= wstrdup(Locale
);
787 ptr
= strchr(Locale
, '.');
793 dpy
= XOpenDisplay(DisplayName
);
795 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName
));
799 if (fcntl(ConnectionNumber(dpy
), F_SETFD
, FD_CLOEXEC
) < 0) {
800 werror("error setting close-on-exec flag for X connection");
805 if (getWVisualID(0) < 0) {
807 * If unspecified, use default visual instead of waiting
808 * for wrlib/context.c:bestContext() that may end up choosing
809 * the "fake" 24 bits added by the Composite extension.
810 * This is required to avoid all sort of corruptions when
811 * composite is enabled, and at a depth other than 24.
813 setWVisualID(0, (int)DefaultVisual(dpy
, DefaultScreen(dpy
))->visualid
);
816 /* check if the user specified a complete display name (with screen).
817 * If so, only manage the specified screen */
819 pos
= strchr(DisplayName
, ':');
823 if (pos
&& sscanf(pos
, ":%i.%i", &d
, &s
) == 2)
826 DisplayName
= XDisplayName(DisplayName
);
827 setenv("DISPLAY", DisplayName
, 1);
829 wXModifierInitialize();
832 if (wScreenCount
== 1)
837 inotifyWatchConfig();