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"
55 #include <WINGs/WUtil.h>
57 #ifndef GLOBAL_DEFAULTS_SUBDIR
58 #define GLOBAL_DEFAULTS_SUBDIR "WindowMaker"
61 /****** Global Variables ******/
69 unsigned int ValidModMask
= 0xff;
75 /* locale to use. NULL==POSIX or C */
80 WPreferences wPreferences
;
82 WShortKey wKeyBindings
[WKBD_LAST
];
84 /* defaults domains */
85 WDDomain
*WDWindowMaker
= NULL
;
86 WDDomain
*WDWindowAttributes
= NULL
;
87 WDDomain
*WDRootMenu
= NULL
;
91 XContext wAppWinContext
;
92 XContext wStackContext
;
93 XContext wVEdgeContext
;
97 Atom _XA_WM_CHANGE_STATE
;
98 Atom _XA_WM_PROTOCOLS
;
99 Atom _XA_WM_TAKE_FOCUS
;
100 Atom _XA_WM_DELETE_WINDOW
;
101 Atom _XA_WM_SAVE_YOURSELF
;
102 Atom _XA_WM_CLIENT_LEADER
;
103 Atom _XA_WM_COLORMAP_WINDOWS
;
104 Atom _XA_WM_COLORMAP_NOTIFY
;
106 Atom _XA_GNUSTEP_WM_ATTR
;
107 Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW
;
108 Atom _XA_GNUSTEP_WM_RESIZEBAR
;
109 Atom _XA_GNUSTEP_TITLEBAR_STATE
;
111 Atom _XA_WINDOWMAKER_MENU
;
112 Atom _XA_WINDOWMAKER_WM_PROTOCOLS
;
113 Atom _XA_WINDOWMAKER_STATE
;
115 Atom _XA_WINDOWMAKER_WM_FUNCTION
;
116 Atom _XA_WINDOWMAKER_NOTICEBOARD
;
117 Atom _XA_WINDOWMAKER_COMMAND
;
119 Atom _XA_WINDOWMAKER_ICON_SIZE
;
120 Atom _XA_WINDOWMAKER_ICON_TILE
;
122 Atom _XA_WM_IGNORE_FOCUS_EVENTS
;
125 Cursor wCursor
[WCUR_LAST
];
127 /* last event timestamp for XSetInputFocus */
128 Time LastTimestamp
= CurrentTime
;
129 /* timestamp on the last time we did XSetInputFocus() */
130 Time LastFocusChange
= CurrentTime
;
133 Bool wShapeSupported
;
137 #ifdef KEEP_XKB_LOCK_STATUS
144 int randr_event_base
;
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 extern void EventLoop(void);
177 extern void StartUp(Bool defaultScreenOnly
);
178 extern int MonitorLoop(int argc
, char **argv
);
180 static Bool multiHead
= True
;
182 static int *wVisualID
= NULL
;
183 static int wVisualID_len
= 0;
185 static int real_main(int argc
, char **argv
);
187 int getWVisualID(int screen
)
189 if (wVisualID
== NULL
)
191 if (screen
< 0 || screen
>= wVisualID_len
)
194 return wVisualID
[screen
];
197 static void setWVisualID(int screen
, int val
)
204 if (wVisualID
== NULL
) {
205 /* no array at all, alloc space for screen + 1 entries
206 * and init with default value */
207 wVisualID_len
= screen
+ 1;
208 wVisualID
= (int *)malloc(wVisualID_len
* sizeof(int));
209 for (i
= 0; i
< wVisualID_len
; i
++) {
212 } else if (screen
>= wVisualID_len
) {
213 /* larger screen number than previously allocated
215 int oldlen
= wVisualID_len
;
217 wVisualID_len
= screen
+ 1;
218 wVisualID
= (int *)realloc(wVisualID
, wVisualID_len
* sizeof(int));
219 for (i
= oldlen
; i
< wVisualID_len
; i
++) {
224 wVisualID
[screen
] = val
;
228 * this function splits a given string at the comma into tokens
229 * and set the wVisualID variable to each parsed number
231 static int initWVisualID(const char *user_str
)
233 char *mystr
= strdup(user_str
);
240 /* check for delimiter */
241 if (user_str
[cur_in_pos
] == '\0' || user_str
[cur_in_pos
] == ',') {
244 mystr
[cur_out_pos
] = '\0';
246 if (sscanf(mystr
, "%i", &v
) != 1) {
251 setWVisualID(cur_screen
, v
);
257 /* break in case last char has been consumed */
258 if (user_str
[cur_in_pos
] == '\0')
261 /* if the current char is no delimiter put it into mystr */
262 if (user_str
[cur_in_pos
] != ',') {
263 mystr
[cur_out_pos
++] = user_str
[cur_in_pos
];
270 if (cur_screen
== 0||error_found
!= 0)
276 void Exit(int status
)
284 void Restart(char *manager
, Bool abortOnFailure
)
287 char *argv
[MAX_RESTART_ARGS
];
290 if (manager
&& manager
[0] != 0) {
291 prog
= argv
[0] = strtok(manager
, " ");
292 for (i
= 1; i
< MAX_RESTART_ARGS
; i
++) {
293 argv
[i
] = strtok(NULL
, " ");
294 if (argv
[i
] == NULL
) {
304 execvp(Arguments
[0], Arguments
);
305 wfatal(_("failed to restart Window Maker."));
308 werror(_("could not exec %s"), prog
);
314 void SetupEnvironment(WScreen
* scr
)
320 int len
= strlen(DisplayName
) + 64;
322 snprintf(tmp
, len
, "DISPLAY=%s", XDisplayName(DisplayName
));
323 ptr
= strchr(strchr(tmp
, ':'), '.');
326 snprintf(buf
, sizeof(buf
), ".%i", scr
->screen
);
331 snprintf(tmp
, 60, "WRASTER_COLOR_RESOLUTION%i=%i", scr
->screen
,
332 scr
->rcontext
->attribs
->colors_per_channel
);
341 static void shellCommandHandler(pid_t pid
, unsigned char status
, _tuple
* data
)
346 buffer
= wstrconcat(_("Could not execute command: "), data
->command
);
348 wMessageDialog(data
->scr
, _("Error"), buffer
, _("OK"), NULL
, NULL
);
350 } else if (status
!= 127) {
352 printf("%s: %i\n", data->command, status);
356 wfree(data
->command
);
360 void ExecuteShellCommand(WScreen
* scr
, char *command
)
362 static char *shell
= NULL
;
366 * This have a problem: if the shell is tcsh (not sure about others)
367 * and ~/.tcshrc have /bin/stty erase ^H somewhere on it, the shell
368 * will block and the command will not be executed.
370 shell = getenv("SHELL");
381 SetupEnvironment(scr
);
386 execl(shell
, shell
, "-c", command
, NULL
);
387 werror("could not execute %s -c %s", shell
, command
);
389 } else if (pid
< 0) {
390 werror("cannot fork a new process");
392 _tuple
*data
= wmalloc(sizeof(_tuple
));
395 data
->command
= wstrdup(command
);
397 wAddDeathHandler(pid
, (WDeathHandler
*) shellCommandHandler
, data
);
402 *---------------------------------------------------------------------
404 * Do a major cleanup and exit the program
406 *----------------------------------------------------------------------
408 void wAbort(Bool dumpCore
)
413 for (i
= 0; i
< wScreenCount
; i
++) {
414 scr
= wScreenWithNumber(i
);
418 printf(_("%s aborted.\n"), ProgName
);
427 printf(_("Usage: %s [options]\n"), ProgName
);
428 puts(_("The Window Maker window manager for the X window system"));
430 puts(_(" -display host:dpy display to use"));
432 puts(_(" --no-cpp disable preprocessing of configuration files"));
434 puts(_(" --no-dock do not open the application Dock"));
435 puts(_(" --no-clip do not open the workspace Clip"));
436 puts(_(" --no-autolaunch do not autolaunch applications"));
437 puts(_(" --dont-restore do not restore saved session"));
439 puts(_(" --locale locale locale to use"));
441 puts(_(" --create-stdcmap create the standard colormap hint in PseudoColor visuals"));
442 puts(_(" --visual-id visualid visual id of visual to use"));
443 puts(_(" --static do not update or save configurations"));
445 puts(_(" --no-polling do not periodically check for configuration updates"));
447 puts(_(" --version print version and exit"));
448 puts(_(" --help show this message"));
451 void check_defaults()
455 path
= wdefaultspathfordomain("WindowMaker");
457 if (access(path
, R_OK
) != 0) {
458 wwarning(_("could not find user GNUstep directory (%s)."), path
);
460 if (system("wmaker.inst --batch") != 0) {
461 wwarning(_("There was an error while creating GNUstep directory, please "
462 "make sure you have installed Window Maker correctly and run wmaker.inst"));
464 wwarning(_("%s directory created with default configuration."), path
);
473 * Add watch here, used to notify if configuration
474 * files have changed, using linux kernel inotify mechanism
477 static void inotifyWatchConfig()
479 char *watchPath
= NULL
;
480 inotifyFD
= inotify_init(); /* Initialise an inotify instance */
482 wwarning(_("could not initialise an inotify instance."
483 " Changes to the defaults database will require"
484 " a restart to take effect. Check your kernel!"));
486 watchPath
= wstrconcat(wusergnusteppath(), "/Defaults");
487 /* Add the watch; really we are only looking for modify events
488 * but we might want more in the future so check all events for now.
489 * The individual events are checked for in event.c.
491 inotifyWD
= inotify_add_watch(inotifyFD
, watchPath
, IN_ALL_EVENTS
);
493 wwarning(_("could not add an inotify watch on path %s."
494 "Changes to the defaults database will require"
495 " a restart to take effect."), watchPath
);
501 #endif /* HAVE_INOTIFY */
503 static void execInitScript()
507 paths
= wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
508 paths
= wstrappend(paths
, ":" DEF_CONFIG_PATHS
);
510 file
= wfindfile(paths
, DEF_INIT_SCRIPT
);
514 if (system(file
) != 0) {
515 werror(_("%s:could not execute initialization script"), file
);
521 void ExecExitScript()
525 paths
= wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
526 paths
= wstrappend(paths
, ":" DEF_CONFIG_PATHS
);
528 file
= wfindfile(paths
, DEF_EXIT_SCRIPT
);
532 if (system(file
) != 0) {
533 werror(_("%s:could not execute exit script"), file
);
539 int main(int argc
, char **argv
)
541 int i_am_the_monitor
, i
, len
;
544 /* setup common stuff for the monitor and wmaker itself */
545 WMInitializeApplication("WindowMaker", &argc
, argv
);
547 memset(&wPreferences
, 0, sizeof(WPreferences
));
549 wPreferences
.fallbackWMs
= WMCreateArray(8);
550 alt
= getenv("WINDOWMAKER_ALT_WM");
552 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup(alt
));
554 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("blackbox"));
555 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("metacity"));
556 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("fvwm"));
557 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("twm"));
558 WMAddToArray(wPreferences
.fallbackWMs
, NULL
);
559 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("rxvt"));
560 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("xterm"));
562 i_am_the_monitor
= 1;
564 for (i
= 1; i
< argc
; i
++) {
565 if (strncmp(argv
[i
], "--for-real", strlen("--for-real")) == 0) {
566 i_am_the_monitor
= 0;
568 } else if (strcmp(argv
[i
], "-display") == 0 || strcmp(argv
[i
], "--display") == 0) {
571 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
574 DisplayName
= argv
[i
];
578 DisplayName
= XDisplayName(DisplayName
);
579 len
= strlen(DisplayName
) + 64;
581 snprintf(str
, len
, "DISPLAY=%s", DisplayName
);
584 if (i_am_the_monitor
)
585 return MonitorLoop(argc
, argv
);
587 return real_main(argc
, argv
);
590 static int real_main(int argc
, char **argv
)
596 setlocale(LC_ALL
, "");
599 /* for telling WPrefs what's the name of the wmaker binary being ran */
600 setenv("WMAKER_BIN_NAME", argv
[0], 1);
603 Arguments
= wmalloc(sizeof(char *) * (ArgCount
+ 1));
604 for (i
= 0; i
< argc
; i
++) {
605 Arguments
[i
] = argv
[i
];
607 /* add the extra option to signal that we're just restarting wmaker */
608 Arguments
[argc
- 1] = "--for-real=";
609 Arguments
[argc
] = NULL
;
611 ProgName
= strrchr(argv
[0], '/');
618 for (i
= 1; i
< argc
; i
++) {
620 if (strcmp(argv
[i
], "-nocpp") == 0 || strcmp(argv
[i
], "--no-cpp") == 0) {
621 wPreferences
.flags
.nocpp
= 1;
624 if (strcmp(argv
[i
], "--for-real") == 0) {
625 wPreferences
.flags
.restarting
= 0;
626 } else if (strcmp(argv
[i
], "--for-real=") == 0) {
627 wPreferences
.flags
.restarting
= 1;
628 } else if (strcmp(argv
[i
], "--for-real-") == 0) {
629 wPreferences
.flags
.restarting
= 2;
630 } else if (strcmp(argv
[i
], "-no-autolaunch") == 0
631 || strcmp(argv
[i
], "--no-autolaunch") == 0) {
632 wPreferences
.flags
.noautolaunch
= 1;
633 } else if (strcmp(argv
[i
], "-dont-restore") == 0 || strcmp(argv
[i
], "--dont-restore") == 0) {
634 wPreferences
.flags
.norestore
= 1;
635 } else if (strcmp(argv
[i
], "-nodock") == 0 || strcmp(argv
[i
], "--no-dock") == 0) {
636 wPreferences
.flags
.nodock
= 1;
637 } else if (strcmp(argv
[i
], "-noclip") == 0 || strcmp(argv
[i
], "--no-clip") == 0) {
638 wPreferences
.flags
.noclip
= 1;
639 } else if (strcmp(argv
[i
], "-version") == 0 || strcmp(argv
[i
], "--version") == 0) {
640 printf("Window Maker %s\n", VERSION
);
642 } else if (strcmp(argv
[i
], "--global_defaults_path") == 0) {
643 printf("%s/%s\n", SYSCONFDIR
, GLOBAL_DEFAULTS_SUBDIR
);
645 } else if (strcmp(argv
[i
], "-locale") == 0 || strcmp(argv
[i
], "--locale") == 0) {
648 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
652 } else if (strcmp(argv
[i
], "-display") == 0 || strcmp(argv
[i
], "--display") == 0) {
655 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
658 DisplayName
= argv
[i
];
659 } else if (strcmp(argv
[i
], "-visualid") == 0 || strcmp(argv
[i
], "--visual-id") == 0) {
662 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
665 if (initWVisualID(argv
[i
]) != 0) {
666 wwarning(_("bad value for visualid: \"%s\""), argv
[i
]);
669 } else if (strcmp(argv
[i
], "-static") == 0 || strcmp(argv
[i
], "--static") == 0
671 || strcmp(argv
[i
], "--no-polling") == 0
674 wPreferences
.flags
.noupdates
= 1;
675 } else if (strcmp(argv
[i
], "--help") == 0) {
679 printf(_("%s: invalid argument '%s'\n"), argv
[0], argv
[i
]);
680 printf(_("Try '%s --help' for more information\n"), argv
[0]);
686 if (!wPreferences
.flags
.noupdates
) {
687 /* check existence of Defaults DB directory */
692 setenv("LANG", Locale
, 1);
694 Locale
= getenv("LC_ALL");
696 Locale
= getenv("LANG");
700 setlocale(LC_ALL
, "");
702 if (!Locale
|| strcmp(Locale
, "C") == 0 || strcmp(Locale
, "POSIX") == 0)
705 if (getenv("NLSPATH")) {
706 bindtextdomain("WindowMaker", getenv("NLSPATH"));
707 #if defined(MENU_TEXTDOMAIN)
708 bindtextdomain(MENU_TEXTDOMAIN
, getenv("NLSPATH"));
711 bindtextdomain("WindowMaker", LOCALEDIR
);
712 #if defined(MENU_TEXTDOMAIN)
713 bindtextdomain(MENU_TEXTDOMAIN
, LOCALEDIR
);
716 bind_textdomain_codeset("WindowMaker", "UTF-8");
717 #if defined(MENU_TEXTDOMAIN)
718 bind_textdomain_codeset(MENU_TEXTDOMAIN
, "UTF-8");
720 textdomain("WindowMaker");
722 if (!XSupportsLocale()) {
723 wwarning(_("X server does not support locale"));
726 if (XSetLocaleModifiers("") == NULL
) {
727 wwarning(_("cannot set locale modifiers"));
734 Locale
= wstrdup(Locale
);
735 ptr
= strchr(Locale
, '.');
741 dpy
= XOpenDisplay(DisplayName
);
743 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName
));
747 if (fcntl(ConnectionNumber(dpy
), F_SETFD
, FD_CLOEXEC
) < 0) {
748 werror("error setting close-on-exec flag for X connection");
753 if (getWVisualID(0) < 0) {
755 * If unspecified, use default visual instead of waiting
756 * for wrlib/context.c:bestContext() that may end up choosing
757 * the "fake" 24 bits added by the Composite extension.
758 * This is required to avoid all sort of corruptions when
759 * composite is enabled, and at a depth other than 24.
761 setWVisualID(0, (int)DefaultVisual(dpy
, DefaultScreen(dpy
))->visualid
);
764 /* check if the user specified a complete display name (with screen).
765 * If so, only manage the specified screen */
767 pos
= strchr(DisplayName
, ':');
771 if (pos
&& sscanf(pos
, ":%i.%i", &d
, &s
) == 2)
774 DisplayName
= XDisplayName(DisplayName
);
775 setenv("DISPLAY", DisplayName
, 1);
777 wXModifierInitialize();
780 if (wScreenCount
== 1)
785 inotifyWatchConfig();