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 Bool wShapeSupported
;
133 #ifdef KEEP_XKB_LOCK_STATUS
139 char WDelayedActionSet
= 0;
142 const char WMNManaged
[] = "WMNManaged";
143 const char WMNUnmanaged
[] = "WMNUnmanaged";
144 const char WMNChangedWorkspace
[] = "WMNChangedWorkspace";
145 const char WMNChangedState
[] = "WMNChangedState";
146 const char WMNChangedFocus
[] = "WMNChangedFocus";
147 const char WMNChangedStacking
[] = "WMNChangedStacking";
148 const char WMNChangedName
[] = "WMNChangedName";
150 const char WMNWorkspaceCreated
[] = "WMNWorkspaceCreated";
151 const char WMNWorkspaceDestroyed
[] = "WMNWorkspaceDestroyed";
152 const char WMNWorkspaceChanged
[] = "WMNWorkspaceChanged";
153 const char WMNWorkspaceNameChanged
[] = "WMNWorkspaceNameChanged";
155 const char WMNResetStacking
[] = "WMNResetStacking";
157 /******** End Global Variables *****/
159 static char *DisplayName
= NULL
;
161 static char **Arguments
;
165 static Bool multiHead
= True
;
167 static int *wVisualID
= NULL
;
168 static int wVisualID_len
= 0;
170 static int real_main(int argc
, char **argv
);
172 int getWVisualID(int screen
)
174 if (wVisualID
== NULL
)
176 if (screen
< 0 || screen
>= wVisualID_len
)
179 return wVisualID
[screen
];
182 static void setWVisualID(int screen
, int val
)
189 if (wVisualID
== NULL
) {
190 /* no array at all, alloc space for screen + 1 entries
191 * and init with default value */
192 wVisualID_len
= screen
+ 1;
193 wVisualID
= (int *)malloc(wVisualID_len
* sizeof(int));
194 for (i
= 0; i
< wVisualID_len
; i
++) {
197 } else if (screen
>= wVisualID_len
) {
198 /* larger screen number than previously allocated
200 int oldlen
= wVisualID_len
;
202 wVisualID_len
= screen
+ 1;
203 wVisualID
= (int *)realloc(wVisualID
, wVisualID_len
* sizeof(int));
204 for (i
= oldlen
; i
< wVisualID_len
; i
++) {
209 wVisualID
[screen
] = val
;
213 * this function splits a given string at the comma into tokens
214 * and set the wVisualID variable to each parsed number
216 static int initWVisualID(const char *user_str
)
218 char *mystr
= strdup(user_str
);
225 /* check for delimiter */
226 if (user_str
[cur_in_pos
] == '\0' || user_str
[cur_in_pos
] == ',') {
229 mystr
[cur_out_pos
] = '\0';
231 if (sscanf(mystr
, "%i", &v
) != 1) {
236 setWVisualID(cur_screen
, v
);
242 /* break in case last char has been consumed */
243 if (user_str
[cur_in_pos
] == '\0')
246 /* if the current char is no delimiter put it into mystr */
247 if (user_str
[cur_in_pos
] != ',') {
248 mystr
[cur_out_pos
++] = user_str
[cur_in_pos
];
255 if (cur_screen
== 0||error_found
!= 0)
261 noreturn
void Exit(int status
)
269 void Restart(char *manager
, Bool abortOnFailure
)
272 char *argv
[MAX_RESTART_ARGS
];
275 if (manager
&& manager
[0] != 0) {
276 prog
= argv
[0] = strtok(manager
, " ");
277 for (i
= 1; i
< MAX_RESTART_ARGS
; i
++) {
278 argv
[i
] = strtok(NULL
, " ");
279 if (argv
[i
] == NULL
) {
289 execvp(Arguments
[0], Arguments
);
290 wfatal(_("failed to restart Window Maker."));
293 werror(_("could not exec %s"), prog
);
299 void SetupEnvironment(WScreen
* scr
)
305 int len
= strlen(DisplayName
) + 64;
307 snprintf(tmp
, len
, "DISPLAY=%s", XDisplayName(DisplayName
));
308 ptr
= strchr(strchr(tmp
, ':'), '.');
311 snprintf(buf
, sizeof(buf
), ".%i", scr
->screen
);
316 snprintf(tmp
, 60, "WRASTER_COLOR_RESOLUTION%i=%i", scr
->screen
,
317 scr
->rcontext
->attribs
->colors_per_channel
);
326 static void shellCommandHandler(pid_t pid
, unsigned char status
, _tuple
* data
)
331 buffer
= wstrconcat(_("Could not execute command: "), data
->command
);
333 wMessageDialog(data
->scr
, _("Error"), buffer
, _("OK"), NULL
, NULL
);
335 } else if (status
!= 127) {
337 printf("%s: %i\n", data->command, status);
341 wfree(data
->command
);
345 void ExecuteShellCommand(WScreen
*scr
, const char *command
)
347 static char *shell
= NULL
;
351 * This have a problem: if the shell is tcsh (not sure about others)
352 * and ~/.tcshrc have /bin/stty erase ^H somewhere on it, the shell
353 * will block and the command will not be executed.
355 shell = getenv("SHELL");
366 SetupEnvironment(scr
);
371 execl(shell
, shell
, "-c", command
, NULL
);
372 werror("could not execute %s -c %s", shell
, command
);
374 } else if (pid
< 0) {
375 werror("cannot fork a new process");
377 _tuple
*data
= wmalloc(sizeof(_tuple
));
380 data
->command
= wstrdup(command
);
382 wAddDeathHandler(pid
, (WDeathHandler
*) shellCommandHandler
, data
);
387 *---------------------------------------------------------------------
389 * Launch a new instance of the active window
391 *----------------------------------------------------------------------
393 Bool
RelaunchWindow(WWindow
*wwin
)
395 if (! wwin
|| ! wwin
->client_win
) {
396 werror("no window to relaunch");
403 if (! XGetCommand(dpy
, wwin
->client_win
, &argv
, &argc
) || argc
== 0 || argv
== NULL
) {
404 werror("cannot relaunch the application because no WM_COMMAND property is set");
411 SetupEnvironment(wwin
->screen_ptr
);
415 /* argv is not null-terminated */
416 char **a
= (char **) malloc(argc
+ 1);
418 werror("out of memory trying to relaunch the application");
423 for (i
= 0; i
< argc
; i
++)
429 } else if (pid
< 0) {
430 werror("cannot fork a new process");
432 XFreeStringList(argv
);
435 _tuple
*data
= wmalloc(sizeof(_tuple
));
437 data
->scr
= wwin
->screen_ptr
;
438 data
->command
= wtokenjoin(argv
, argc
);
440 /* not actually a shell command */
441 wAddDeathHandler(pid
, (WDeathHandler
*) shellCommandHandler
, data
);
443 XFreeStringList(argv
);
450 *---------------------------------------------------------------------
452 * Do a major cleanup and exit the program
454 *----------------------------------------------------------------------
456 noreturn
void wAbort(Bool dumpCore
)
461 for (i
= 0; i
< wScreenCount
; i
++) {
462 scr
= wScreenWithNumber(i
);
466 printf(_("%s aborted.\n"), ProgName
);
473 static void print_help(void)
475 printf(_("Usage: %s [options]\n"), ProgName
);
476 puts(_("The Window Maker window manager for the X window system"));
478 puts(_(" -display host:dpy display to use"));
479 puts(_(" --no-dock do not open the application Dock"));
480 puts(_(" --no-clip do not open the workspace Clip"));
481 puts(_(" --no-autolaunch do not autolaunch applications"));
482 puts(_(" --dont-restore do not restore saved session"));
484 puts(_(" --locale locale locale to use"));
486 puts(_(" --visual-id visualid visual id of visual to use"));
487 puts(_(" --static do not update or save configurations"));
489 puts(_(" --no-polling do not periodically check for configuration updates"));
491 puts(_(" --version print version and exit"));
492 puts(_(" --help show this message"));
495 static void check_defaults(void)
499 path
= wdefaultspathfordomain("WindowMaker");
501 if (access(path
, R_OK
) != 0) {
502 wwarning(_("could not find user GNUstep directory (%s)."), path
);
504 if (system("wmaker.inst --batch") != 0) {
505 wwarning(_("There was an error while creating GNUstep directory, please "
506 "make sure you have installed Window Maker correctly and run wmaker.inst"));
508 wwarning(_("%s directory created with default configuration."), path
);
517 * Add watch here, used to notify if configuration
518 * files have changed, using linux kernel inotify mechanism
521 static void inotifyWatchConfig(void)
523 char *watchPath
= NULL
;
524 inotifyFD
= inotify_init(); /* Initialise an inotify instance */
526 wwarning(_("could not initialise an inotify instance."
527 " Changes to the defaults database will require"
528 " a restart to take effect. Check your kernel!"));
530 watchPath
= wstrconcat(wusergnusteppath(), "/Defaults");
531 /* Add the watch; really we are only looking for modify events
532 * but we might want more in the future so check all events for now.
533 * The individual events are checked for in event.c.
535 inotifyWD
= inotify_add_watch(inotifyFD
, watchPath
, IN_ALL_EVENTS
);
537 wwarning(_("could not add an inotify watch on path %s."
538 "Changes to the defaults database will require"
539 " a restart to take effect."), watchPath
);
545 #endif /* HAVE_INOTIFY */
547 static void execInitScript(void)
551 paths
= wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
552 paths
= wstrappend(paths
, ":" DEF_CONFIG_PATHS
);
554 file
= wfindfile(paths
, DEF_INIT_SCRIPT
);
558 if (system(file
) != 0)
559 werror(_("%s:could not execute initialization script"), file
);
565 void ExecExitScript(void)
569 paths
= wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
570 paths
= wstrappend(paths
, ":" DEF_CONFIG_PATHS
);
572 file
= wfindfile(paths
, DEF_EXIT_SCRIPT
);
576 if (system(file
) != 0)
577 werror(_("%s:could not execute exit script"), file
);
583 int main(int argc
, char **argv
)
585 int i_am_the_monitor
, i
, len
;
588 memset(&w_global
, 0, sizeof(w_global
));
589 w_global
.program
.state
= WSTATE_NORMAL
;
590 w_global
.program
.signal_state
= WSTATE_NORMAL
;
591 w_global
.timestamp
.last_event
= CurrentTime
;
592 w_global
.timestamp
.focus_change
= CurrentTime
;
594 /* setup common stuff for the monitor and wmaker itself */
595 WMInitializeApplication("WindowMaker", &argc
, argv
);
597 memset(&wPreferences
, 0, sizeof(wPreferences
));
599 wPreferences
.fallbackWMs
= WMCreateArray(8);
600 alt
= getenv("WINDOWMAKER_ALT_WM");
602 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup(alt
));
604 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("blackbox"));
605 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("metacity"));
606 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("fvwm"));
607 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("twm"));
608 WMAddToArray(wPreferences
.fallbackWMs
, NULL
);
609 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("rxvt"));
610 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("xterm"));
612 i_am_the_monitor
= 1;
614 for (i
= 1; i
< argc
; i
++) {
615 if (strncmp(argv
[i
], "--for-real", strlen("--for-real")) == 0) {
616 i_am_the_monitor
= 0;
618 } else if (strcmp(argv
[i
], "-display") == 0 || strcmp(argv
[i
], "--display") == 0) {
621 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
624 DisplayName
= argv
[i
];
628 DisplayName
= XDisplayName(DisplayName
);
629 len
= strlen(DisplayName
) + 64;
631 snprintf(str
, len
, "DISPLAY=%s", DisplayName
);
634 if (i_am_the_monitor
)
635 return MonitorLoop(argc
, argv
);
637 return real_main(argc
, argv
);
640 static int real_main(int argc
, char **argv
)
646 setlocale(LC_ALL
, "");
649 /* for telling WPrefs what's the name of the wmaker binary being ran */
650 setenv("WMAKER_BIN_NAME", argv
[0], 1);
653 Arguments
= wmalloc(sizeof(char *) * (ArgCount
+ 1));
654 for (i
= 0; i
< argc
; i
++) {
655 Arguments
[i
] = argv
[i
];
657 /* add the extra option to signal that we're just restarting wmaker */
658 Arguments
[argc
- 1] = "--for-real=";
659 Arguments
[argc
] = NULL
;
661 ProgName
= strrchr(argv
[0], '/');
668 for (i
= 1; i
< argc
; i
++) {
669 if (strcmp(argv
[i
], "-nocpp") == 0 || strcmp(argv
[i
], "--no-cpp") == 0) {
670 wwarning(_("option \"%s\" is deprecated, please remove it from your script"), argv
[i
]);
671 } else if (strcmp(argv
[i
], "--for-real") == 0) {
672 wPreferences
.flags
.restarting
= 0;
673 } else if (strcmp(argv
[i
], "--for-real=") == 0) {
674 wPreferences
.flags
.restarting
= 1;
675 } else if (strcmp(argv
[i
], "--for-real-") == 0) {
676 wPreferences
.flags
.restarting
= 2;
677 } else if (strcmp(argv
[i
], "-no-autolaunch") == 0
678 || strcmp(argv
[i
], "--no-autolaunch") == 0) {
679 wPreferences
.flags
.noautolaunch
= 1;
680 } else if (strcmp(argv
[i
], "-dont-restore") == 0 || strcmp(argv
[i
], "--dont-restore") == 0) {
681 wPreferences
.flags
.norestore
= 1;
682 } else if (strcmp(argv
[i
], "-nodock") == 0 || strcmp(argv
[i
], "--no-dock") == 0) {
683 wPreferences
.flags
.nodock
= 1;
684 wPreferences
.flags
.nodrawer
= 1;
685 } else if (strcmp(argv
[i
], "-noclip") == 0 || strcmp(argv
[i
], "--no-clip") == 0) {
686 wPreferences
.flags
.noclip
= 1;
687 } else if (strcmp(argv
[i
], "-nodrawer") == 0 || strcmp(argv
[i
], "--no-drawer") == 0) {
688 wPreferences
.flags
.nodrawer
= 1;
689 } else if (strcmp(argv
[i
], "-version") == 0 || strcmp(argv
[i
], "--version") == 0) {
690 printf("Window Maker %s\n", VERSION
);
692 } else if (strcmp(argv
[i
], "--global_defaults_path") == 0) {
693 printf("%s/%s\n", SYSCONFDIR
, GLOBAL_DEFAULTS_SUBDIR
);
695 } else if (strcmp(argv
[i
], "-locale") == 0 || strcmp(argv
[i
], "--locale") == 0) {
698 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
701 w_global
.locale
= argv
[i
];
702 } else if (strcmp(argv
[i
], "-display") == 0 || strcmp(argv
[i
], "--display") == 0) {
705 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
708 DisplayName
= argv
[i
];
709 } else if (strcmp(argv
[i
], "-visualid") == 0 || strcmp(argv
[i
], "--visual-id") == 0) {
712 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
715 if (initWVisualID(argv
[i
]) != 0) {
716 wwarning(_("bad value for visualid: \"%s\""), argv
[i
]);
719 } else if (strcmp(argv
[i
], "-static") == 0 || strcmp(argv
[i
], "--static") == 0
721 || strcmp(argv
[i
], "--no-polling") == 0
724 wPreferences
.flags
.noupdates
= 1;
725 } else if (strcmp(argv
[i
], "--help") == 0) {
729 printf(_("%s: invalid argument '%s'\n"), argv
[0], argv
[i
]);
730 printf(_("Try '%s --help' for more information\n"), argv
[0]);
736 if (!wPreferences
.flags
.noupdates
) {
737 /* check existence of Defaults DB directory */
741 if (w_global
.locale
) {
742 setenv("LANG", w_global
.locale
, 1);
744 w_global
.locale
= getenv("LC_ALL");
745 if (!w_global
.locale
) {
746 w_global
.locale
= getenv("LANG");
750 setlocale(LC_ALL
, "");
752 if (!w_global
.locale
|| strcmp(w_global
.locale
, "C") == 0 || strcmp(w_global
.locale
, "POSIX") == 0)
753 w_global
.locale
= NULL
;
755 if (getenv("NLSPATH")) {
756 bindtextdomain("WindowMaker", getenv("NLSPATH"));
757 #if defined(MENU_TEXTDOMAIN)
758 bindtextdomain(MENU_TEXTDOMAIN
, getenv("NLSPATH"));
761 bindtextdomain("WindowMaker", LOCALEDIR
);
762 #if defined(MENU_TEXTDOMAIN)
763 bindtextdomain(MENU_TEXTDOMAIN
, LOCALEDIR
);
766 bind_textdomain_codeset("WindowMaker", "UTF-8");
767 #if defined(MENU_TEXTDOMAIN)
768 bind_textdomain_codeset(MENU_TEXTDOMAIN
, "UTF-8");
770 textdomain("WindowMaker");
772 if (!XSupportsLocale()) {
773 wwarning(_("X server does not support locale"));
776 if (XSetLocaleModifiers("") == NULL
) {
777 wwarning(_("cannot set locale modifiers"));
781 if (w_global
.locale
) {
784 w_global
.locale
= wstrdup(w_global
.locale
);
785 ptr
= strchr(w_global
.locale
, '.');
791 dpy
= XOpenDisplay(DisplayName
);
793 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName
));
797 if (fcntl(ConnectionNumber(dpy
), F_SETFD
, FD_CLOEXEC
) < 0) {
798 werror("error setting close-on-exec flag for X connection");
803 if (getWVisualID(0) < 0) {
805 * If unspecified, use default visual instead of waiting
806 * for wrlib/context.c:bestContext() that may end up choosing
807 * the "fake" 24 bits added by the Composite extension.
808 * This is required to avoid all sort of corruptions when
809 * composite is enabled, and at a depth other than 24.
811 setWVisualID(0, (int)DefaultVisual(dpy
, DefaultScreen(dpy
))->visualid
);
814 /* check if the user specified a complete display name (with screen).
815 * If so, only manage the specified screen */
817 pos
= strchr(DisplayName
, ':');
821 if (pos
&& sscanf(pos
, ":%i.%i", &d
, &s
) == 2)
824 DisplayName
= XDisplayName(DisplayName
);
825 setenv("DISPLAY", DisplayName
, 1);
827 wXModifierInitialize();
830 if (wScreenCount
== 1)
835 inotifyWatchConfig();