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
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
22 #include <sys/inotify.h>
31 #include <sys/types.h>
35 #include <X11/Xutil.h>
37 /* Xlocale.h and locale.h are the same if X_LOCALE is undefind in wconfig.h,
38 * and if X_LOCALE is defined, X's locale emulating functions will be used.
39 * See Xlocale.h for more information.
41 #include <X11/Xlocale.h>
45 #include "WindowMaker.h"
50 #include "xmodifier.h"
55 #include <WINGs/WUtil.h>
57 /****** Global Variables ******/
65 unsigned int ValidModMask
= 0xff;
69 /* locale to use. NULL==POSIX or C */
74 WPreferences wPreferences
;
76 WMPropList
*wDomainName
;
77 WMPropList
*wAttributeDomainName
;
79 WShortKey wKeyBindings
[WKBD_LAST
];
81 /* defaults domains */
82 WDDomain
*WDWindowMaker
= NULL
;
83 WDDomain
*WDWindowAttributes
= NULL
;
84 WDDomain
*WDRootMenu
= NULL
;
88 XContext wAppWinContext
;
89 XContext wStackContext
;
90 XContext wVEdgeContext
;
94 Atom _XA_WM_CHANGE_STATE
;
95 Atom _XA_WM_PROTOCOLS
;
96 Atom _XA_WM_TAKE_FOCUS
;
97 Atom _XA_WM_DELETE_WINDOW
;
98 Atom _XA_WM_SAVE_YOURSELF
;
99 Atom _XA_WM_CLIENT_LEADER
;
100 Atom _XA_WM_COLORMAP_WINDOWS
;
101 Atom _XA_WM_COLORMAP_NOTIFY
;
103 Atom _XA_GNUSTEP_WM_ATTR
;
104 Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW
;
105 Atom _XA_GNUSTEP_WM_RESIZEBAR
;
106 Atom _XA_GNUSTEP_TITLEBAR_STATE
;
108 Atom _XA_WINDOWMAKER_MENU
;
109 Atom _XA_WINDOWMAKER_WM_PROTOCOLS
;
110 Atom _XA_WINDOWMAKER_STATE
;
112 Atom _XA_WINDOWMAKER_WM_FUNCTION
;
113 Atom _XA_WINDOWMAKER_NOTICEBOARD
;
114 Atom _XA_WINDOWMAKER_COMMAND
;
116 Atom _XA_WINDOWMAKER_ICON_SIZE
;
117 Atom _XA_WINDOWMAKER_ICON_TILE
;
119 Atom _XA_WM_IGNORE_FOCUS_EVENTS
;
122 Cursor wCursor
[WCUR_LAST
];
124 /* last event timestamp for XSetInputFocus */
125 Time LastTimestamp
= CurrentTime
;
126 /* timestamp on the last time we did XSetInputFocus() */
127 Time LastFocusChange
= CurrentTime
;
130 Bool wShapeSupported
;
134 #ifdef KEEP_XKB_LOCK_STATUS
140 char WProgramSigState
= 0;
141 char WProgramState
= WSTATE_NORMAL
;
142 char WDelayedActionSet
= 0;
144 /* temporary stuff */
148 const char *WMNManaged
= "WMNManaged";
149 const char *WMNUnmanaged
= "WMNUnmanaged";
150 const char *WMNChangedWorkspace
= "WMNChangedWorkspace";
151 const char *WMNChangedState
= "WMNChangedState";
152 const char *WMNChangedFocus
= "WMNChangedFocus";
153 const char *WMNChangedStacking
= "WMNChangedStacking";
154 const char *WMNChangedName
= "WMNChangedName";
156 const char *WMNWorkspaceCreated
= "WMNWorkspaceCreated";
157 const char *WMNWorkspaceDestroyed
= "WMNWorkspaceDestroyed";
158 const char *WMNWorkspaceChanged
= "WMNWorkspaceChanged";
159 const char *WMNWorkspaceNameChanged
= "WMNWorkspaceNameChanged";
161 const char *WMNResetStacking
= "WMNResetStacking";
163 /******** End Global Variables *****/
165 static char *DisplayName
= NULL
;
167 static char **Arguments
;
171 extern void EventLoop();
172 extern void StartUp();
173 extern int MonitorLoop(int argc
, char **argv
);
175 static Bool multiHead
= True
;
177 static int real_main(int argc
, char **argv
);
179 void Exit(int status
)
182 wSessionDisconnectManager();
190 void Restart(char *manager
, Bool abortOnFailure
)
193 char *argv
[MAX_RESTART_ARGS
];
196 if (manager
&& manager
[0] != 0) {
197 prog
= argv
[0] = strtok(manager
, " ");
198 for (i
= 1; i
< MAX_RESTART_ARGS
; i
++) {
199 argv
[i
] = strtok(NULL
, " ");
200 if (argv
[i
] == NULL
) {
207 wSessionDisconnectManager();
213 execvp(Arguments
[0], Arguments
);
214 wfatal(_("failed to restart Window Maker."));
217 wsyserror(_("could not exec %s"), prog
);
223 void SetupEnvironment(WScreen
* scr
)
229 int len
= strlen(DisplayName
) + 64;
231 snprintf(tmp
, len
, "DISPLAY=%s", XDisplayName(DisplayName
));
232 ptr
= strchr(strchr(tmp
, ':'), '.');
235 snprintf(buf
, sizeof(buf
), ".%i", scr
->screen
);
240 snprintf(tmp
, 60, "WRASTER_COLOR_RESOLUTION%i=%i", scr
->screen
,
241 scr
->rcontext
->attribs
->colors_per_channel
);
250 static void shellCommandHandler(pid_t pid
, unsigned char status
, _tuple
* data
)
255 buffer
= wstrconcat(_("Could not execute command: "), data
->command
);
257 wMessageDialog(data
->scr
, _("Error"), buffer
, _("OK"), NULL
, NULL
);
259 } else if (status
!= 127) {
261 printf("%s: %i\n", data->command, status);
265 wfree(data
->command
);
269 void ExecuteShellCommand(WScreen
* scr
, char *command
)
271 static char *shell
= NULL
;
275 * This have a problem: if the shell is tcsh (not sure about others)
276 * and ~/.tcshrc have /bin/stty erase ^H somewhere on it, the shell
277 * will block and the command will not be executed.
279 shell = getenv("SHELL");
290 SetupEnvironment(scr
);
295 execl(shell
, shell
, "-c", command
, NULL
);
296 wsyserror("could not execute %s -c %s", shell
, command
);
298 } else if (pid
< 0) {
299 wsyserror("cannot fork a new process");
301 _tuple
*data
= wmalloc(sizeof(_tuple
));
304 data
->command
= wstrdup(command
);
306 wAddDeathHandler(pid
, (WDeathHandler
*) shellCommandHandler
, data
);
311 *---------------------------------------------------------------------
313 * Do a major cleanup and exit the program
315 *----------------------------------------------------------------------
317 void wAbort(Bool dumpCore
)
322 for (i
= 0; i
< wScreenCount
; i
++) {
323 scr
= wScreenWithNumber(i
);
327 printf(_("%s aborted.\n"), ProgName
);
336 printf(_("Usage: %s [options]\n"), ProgName
);
337 puts(_("The Window Maker window manager for the X window system"));
339 puts(_(" -display host:dpy display to use"));
341 puts(_(" --no-cpp disable preprocessing of configuration files"));
343 puts(_(" --no-dock do not open the application Dock"));
344 puts(_(" --no-clip do not open the workspace Clip"));
345 puts(_(" --no-autolaunch do not autolaunch applications"));
346 puts(_(" --dont-restore do not restore saved session"));
348 puts(_(" --locale locale locale to use"));
350 puts(_(" --create-stdcmap create the standard colormap hint in PseudoColor visuals"));
351 puts(_(" --visual-id visualid visual id of visual to use"));
352 puts(_(" --static do not update or save configurations"));
354 puts(_(" --synchronous turn on synchronous display mode"));
356 puts(_(" --version print version and exit"));
357 puts(_(" --help show this message"));
360 void check_defaults()
364 path
= wdefaultspathfordomain("WindowMaker");
366 if (access(path
, R_OK
) != 0) {
368 wfatal(_("could not find user GNUstep directory (%s).\n"
369 "Make sure you have installed Window Maker correctly and run wmaker.inst"), path
);
372 wwarning(_("could not find user GNUstep directory (%s)."), path
);
374 if (system("wmaker.inst --batch") != 0) {
375 wwarning(_("There was an error while creating GNUstep directory, please "
376 "make sure you have installed Window Maker correctly and run wmaker.inst"));
378 wwarning(_("%s directory created with default configuration."), path
);
387 * Add watch here, used to notify if configuration
388 * files have changed, using linux kernel inotify mechanism
391 static void inotifyWatchConfig()
393 char *watchPath
= NULL
;
394 inotifyFD
= inotify_init(); /* Initialise an inotify instance */
396 wwarning(_("could not initialise an inotify instance."
397 " Changes to the defaults database will require"
398 " a restart to take effect. Check your kernel!"));
400 watchPath
= wstrconcat(wusergnusteppath(), "/Defaults");
401 /* Add the watch; really we are only looking for modify events
402 * but we might want more in the future so check all events for now.
403 * The individual events are checked for in event.c.
405 inotifyWD
= inotify_add_watch(inotifyFD
, watchPath
, IN_ALL_EVENTS
);
407 wwarning(_("could not add an inotify watch on path\n."
409 "Changes to the defaults database will require"
410 " a restart to take effect."), watchPath
);
417 static void execInitScript()
421 paths
= wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
422 paths
= wstrappend(paths
, ":" DEF_CONFIG_PATHS
);
424 file
= wfindfile(paths
, DEF_INIT_SCRIPT
);
428 if (system(file
) != 0) {
429 wsyserror(_("%s:could not execute initialization script"), file
);
433 execl("/bin/sh", "/bin/sh", "-c", file
, NULL
);
434 wsyserror(_("%s:could not execute initialization script"), file
);
442 void ExecExitScript()
446 paths
= wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
447 paths
= wstrappend(paths
, ":" DEF_CONFIG_PATHS
);
449 file
= wfindfile(paths
, DEF_EXIT_SCRIPT
);
453 if (system(file
) != 0) {
454 wsyserror(_("%s:could not execute exit script"), file
);
458 execl("/bin/sh", "/bin/sh", "-c", file
, NULL
);
459 wsyserror(_("%s:could not execute exit script"), file
);
468 char *getFullPath(char *path
)
472 char *basep
= (char *)buffer
;
474 if (*path
!= '/' && getcwd(buffer
, 1023)) {
477 if (strncmp(path
, "../", 3) == 0) {
479 basep
= strchr(basep
, '/');
480 if (!basep
|| *path
== 0)
484 if (*path
== '/' || strncmp(path
, "./", 2) == 0) {
495 return wstrconcat(path
);
502 int main(int argc
, char **argv
)
504 int i_am_the_monitor
, i
, len
;
507 /* setup common stuff for the monitor and wmaker itself */
508 WMInitializeApplication("WindowMaker", &argc
, argv
);
510 memset(&wPreferences
, 0, sizeof(WPreferences
));
512 wPreferences
.fallbackWMs
= WMCreateArray(8);
513 alt
= getenv("WINDOWMAKER_ALT_WM");
515 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup(alt
));
517 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("blackbox"));
518 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("metacity"));
519 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("fvwm"));
520 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("twm"));
521 WMAddToArray(wPreferences
.fallbackWMs
, NULL
);
522 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("rxvt"));
523 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("xterm"));
525 i_am_the_monitor
= 1;
527 for (i
= 1; i
< argc
; i
++) {
528 if (strncmp(argv
[i
], "--for-real", strlen("--for-real")) == 0) {
529 i_am_the_monitor
= 0;
531 } else if (strcmp(argv
[i
], "-display") == 0 || strcmp(argv
[i
], "--display") == 0) {
534 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
537 DisplayName
= argv
[i
];
541 DisplayName
= XDisplayName(DisplayName
);
542 len
= strlen(DisplayName
) + 64;
544 snprintf(str
, len
, "DISPLAY=%s", DisplayName
);
547 if (i_am_the_monitor
)
548 return MonitorLoop(argc
, argv
);
550 return real_main(argc
, argv
);
553 static int real_main(int argc
, char **argv
)
562 setlocale(LC_ALL
, "");
566 /* for telling WPrefs what's the name of the wmaker binary being ran */
567 setenv("WMAKER_BIN_NAME", argv
[0], 1);
571 Arguments
= wmalloc(sizeof(char *) * (ArgCount
+ 1));
572 for (i
= 0; i
< argc
; i
++) {
573 Arguments
[i
] = argv
[i
];
575 /* add the extra option to signal that we're just restarting wmaker */
576 Arguments
[argc
- 1] = "--for-real=";
577 Arguments
[argc
] = NULL
;
579 ProgName
= strrchr(argv
[0], '/');
588 for (i
= 1; i
< argc
; i
++) {
590 if (strcmp(argv
[i
], "-nocpp") == 0 || strcmp(argv
[i
], "--no-cpp") == 0) {
591 wPreferences
.flags
.nocpp
= 1;
594 if (strcmp(argv
[i
], "--for-real") == 0) {
595 wPreferences
.flags
.restarting
= 0;
596 } else if (strcmp(argv
[i
], "--for-real=") == 0) {
597 wPreferences
.flags
.restarting
= 1;
598 } else if (strcmp(argv
[i
], "--for-real-") == 0) {
599 wPreferences
.flags
.restarting
= 2;
600 } else if (strcmp(argv
[i
], "-no-autolaunch") == 0
601 || strcmp(argv
[i
], "--no-autolaunch") == 0) {
602 wPreferences
.flags
.noautolaunch
= 1;
603 } else if (strcmp(argv
[i
], "-dont-restore") == 0 || strcmp(argv
[i
], "--dont-restore") == 0) {
604 wPreferences
.flags
.norestore
= 1;
605 } else if (strcmp(argv
[i
], "-nodock") == 0 || strcmp(argv
[i
], "--no-dock") == 0) {
606 wPreferences
.flags
.nodock
= 1;
607 } else if (strcmp(argv
[i
], "-noclip") == 0 || strcmp(argv
[i
], "--no-clip") == 0) {
608 wPreferences
.flags
.noclip
= 1;
609 } else if (strcmp(argv
[i
], "-version") == 0 || strcmp(argv
[i
], "--version") == 0) {
610 printf("Window Maker %s\n", VERSION
);
612 } else if (strcmp(argv
[i
], "--global_defaults_path") == 0) {
613 printf("%s/WindowMaker\n", SYSCONFDIR
);
616 } else if (strcmp(argv
[i
], "--synchronous") == 0) {
619 } else if (strcmp(argv
[i
], "-locale") == 0 || strcmp(argv
[i
], "--locale") == 0) {
622 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
626 } else if (strcmp(argv
[i
], "-display") == 0 || strcmp(argv
[i
], "--display") == 0) {
629 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
632 DisplayName
= argv
[i
];
633 } else if (strcmp(argv
[i
], "-visualid") == 0 || strcmp(argv
[i
], "--visual-id") == 0) {
636 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
639 if (sscanf(argv
[i
], "%i", &wVisualID
) != 1) {
640 wwarning(_("bad value for visualid: \"%s\""), argv
[i
]);
643 } else if (strcmp(argv
[i
], "-static") == 0 || strcmp(argv
[i
], "--static") == 0) {
645 wPreferences
.flags
.noupdates
= 1;
647 } else if (strcmp(argv
[i
], "-clientid") == 0 || strcmp(argv
[i
], "-restore") == 0) {
650 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
654 } else if (strcmp(argv
[i
], "--help") == 0) {
658 printf(_("%s: invalid argument '%s'\n"), argv
[0], argv
[i
]);
659 printf(_("Try '%s --help' for more information\n"), argv
[0]);
665 if (!wPreferences
.flags
.noupdates
) {
666 /* check existence of Defaults DB directory */
671 setenv("LANG", Locale
, 1);
673 Locale
= getenv("LC_ALL");
675 Locale
= getenv("LANG");
679 setlocale(LC_ALL
, "");
681 if (!Locale
|| strcmp(Locale
, "C") == 0 || strcmp(Locale
, "POSIX") == 0)
684 if (getenv("NLSPATH"))
685 bindtextdomain("WindowMaker", getenv("NLSPATH"));
687 bindtextdomain("WindowMaker", LOCALEDIR
);
688 bind_textdomain_codeset("WindowMaker", "UTF-8");
689 textdomain("WindowMaker");
691 if (!XSupportsLocale()) {
692 wwarning(_("X server does not support locale"));
695 if (XSetLocaleModifiers("") == NULL
) {
696 wwarning(_("cannot set locale modifiers"));
703 Locale
= wstrdup(Locale
);
704 ptr
= strchr(Locale
, '.');
710 dpy
= XOpenDisplay(DisplayName
);
712 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName
));
716 if (fcntl(ConnectionNumber(dpy
), F_SETFD
, FD_CLOEXEC
) < 0) {
717 wsyserror("error setting close-on-exec flag for X connection");
723 * If unspecified, use default visual instead of waiting
724 * for wrlib/context.c:bestContext() that may end up choosing
725 * the "fake" 24 bits added by the Composite extension.
726 * This is required to avoid all sort of corruptions when
727 * composite is enabled, and at a depth other than 24.
729 wVisualID
= (int)DefaultVisual(dpy
, DefaultScreen(dpy
))->visualid
;
731 /* check if the user specified a complete display name (with screen).
732 * If so, only manage the specified screen */
734 pos
= strchr(DisplayName
, ':');
738 if (pos
&& sscanf(pos
, ":%i.%i", &d
, &s
) == 2)
741 DisplayName
= XDisplayName(DisplayName
);
742 setenv("DISPLAY", DisplayName
, 1);
746 XSynchronize(dpy
, True
);
749 wXModifierInitialize();
752 wSessionConnectManager(argv
, argc
);
757 if (wScreenCount
== 1)
761 inotifyWatchConfig();