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,
29 #include <sys/types.h>
33 #include <X11/Xutil.h>
36 /* Xlocale.h and locale.h are the same if X_LOCALE is undefind in wconfig.h,
37 * and if X_LOCALE is defined, X's locale emulating functions will be used.
38 * See Xlocale.h for more information.
40 #include <X11/Xlocale.h>
44 #include "WindowMaker.h"
49 #include "xmodifier.h"
54 #include <WINGs/WUtil.h>
56 /****** Global Variables ******/
64 unsigned int ValidModMask
= 0xff;
66 /* locale to use. NULL==POSIX or C */
71 WPreferences wPreferences
;
74 WMPropList
*wDomainName
;
75 WMPropList
*wAttributeDomainName
;
77 WShortKey wKeyBindings
[WKBD_LAST
];
79 /* defaults domains */
80 WDDomain
*WDWindowMaker
= NULL
;
81 WDDomain
*WDWindowAttributes
= NULL
;
82 WDDomain
*WDRootMenu
= NULL
;
86 XContext wAppWinContext
;
87 XContext wStackContext
;
88 XContext wVEdgeContext
;
92 Atom _XA_WM_CHANGE_STATE
;
93 Atom _XA_WM_PROTOCOLS
;
94 Atom _XA_WM_TAKE_FOCUS
;
95 Atom _XA_WM_DELETE_WINDOW
;
96 Atom _XA_WM_SAVE_YOURSELF
;
97 Atom _XA_WM_CLIENT_LEADER
;
98 Atom _XA_WM_COLORMAP_WINDOWS
;
99 Atom _XA_WM_COLORMAP_NOTIFY
;
101 Atom _XA_GNUSTEP_WM_ATTR
;
102 Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW
;
103 Atom _XA_GNUSTEP_WM_RESIZEBAR
;
104 Atom _XA_GNUSTEP_TITLEBAR_STATE
;
106 Atom _XA_WINDOWMAKER_MENU
;
107 Atom _XA_WINDOWMAKER_WM_PROTOCOLS
;
108 Atom _XA_WINDOWMAKER_STATE
;
110 Atom _XA_WINDOWMAKER_WM_FUNCTION
;
111 Atom _XA_WINDOWMAKER_NOTICEBOARD
;
112 Atom _XA_WINDOWMAKER_COMMAND
;
114 Atom _XA_WINDOWMAKER_ICON_SIZE
;
115 Atom _XA_WINDOWMAKER_ICON_TILE
;
119 Cursor wCursor
[WCUR_LAST
];
121 /* last event timestamp for XSetInputFocus */
122 Time LastTimestamp
= CurrentTime
;
123 /* timestamp on the last time we did XSetInputFocus() */
124 Time LastFocusChange
= CurrentTime
;
127 Bool wShapeSupported
;
131 #ifdef KEEP_XKB_LOCK_STATUS
137 char WProgramSigState
= 0;
138 char WProgramState
= WSTATE_NORMAL
;
139 char WDelayedActionSet
= 0;
142 /* temporary stuff */
147 const char *WMNManaged
= "WMNManaged";
148 const char *WMNUnmanaged
= "WMNUnmanaged";
149 const char *WMNChangedWorkspace
= "WMNChangedWorkspace";
150 const char *WMNChangedState
= "WMNChangedState";
151 const char *WMNChangedFocus
= "WMNChangedFocus";
152 const char *WMNChangedStacking
= "WMNChangedStacking";
153 const char *WMNChangedName
= "WMNChangedName";
155 const char *WMNWorkspaceCreated
= "WMNWorkspaceCreated";
156 const char *WMNWorkspaceDestroyed
= "WMNWorkspaceDestroyed";
157 const char *WMNWorkspaceChanged
= "WMNWorkspaceChanged";
158 const char *WMNWorkspaceNameChanged
= "WMNWorkspaceNameChanged";
160 const char *WMNResetStacking
= "WMNResetStacking";
162 /******** End Global Variables *****/
164 static char *DisplayName
= NULL
;
166 static char **Arguments
;
170 extern void EventLoop();
171 extern void StartUp();
172 extern int MonitorLoop(int argc
, char **argv
);
174 static Bool multiHead
= True
;
176 /* stdi/o for log shell */
177 static int LogStdIn
= -1, LogStdOut
= -1, LogStdErr
= -1;
180 static int real_main(int argc
, char **argv
);
186 wSessionDisconnectManager();
196 Restart(char *manager
, Bool abortOnFailure
)
199 char *argv
[MAX_RESTART_ARGS
];
202 if (manager
&& manager
[0]!=0) {
203 prog
= argv
[0] = strtok(manager
, " ");
204 for (i
=1; i
<MAX_RESTART_ARGS
; i
++) {
205 argv
[i
]=strtok(NULL
, " ");
213 wSessionDisconnectManager();
219 execvp(Arguments
[0], Arguments
);
220 wfatal(_("failed to restart Window Maker."));
223 wsyserror(_("could not exec %s"), prog
);
232 SetupEnvironment(WScreen
*scr
)
238 int len
= strlen(DisplayName
)+64;
240 snprintf(tmp
, len
, "DISPLAY=%s", XDisplayName(DisplayName
));
241 ptr
= strchr(strchr(tmp
, ':'), '.');
244 snprintf(buf
, sizeof(buf
), ".%i", scr
->screen
);
249 snprintf(tmp
, 60, "WRASTER_COLOR_RESOLUTION%i=%i", scr
->screen
,
250 scr
->rcontext
->attribs
->colors_per_channel
);
264 shellCommandHandler(pid_t pid
, unsigned char status
, _tuple
*data
)
269 buffer
= wstrconcat(_("Could not execute command: "), data
->command
);
271 wMessageDialog(data
->scr
, _("Error"), buffer
, _("OK"), NULL
, NULL
);
273 } else if (status
!= 127) {
275 printf("%s: %i\n", data->command, status);
279 wfree(data
->command
);
285 ExecuteShellCommand(WScreen
*scr
, char *command
)
287 static char *shell
= NULL
;
291 * This have a problem: if the shell is tcsh (not sure about others)
292 * and ~/.tcshrc have /bin/stty erase ^H somewhere on it, the shell
293 * will block and the command will not be executed.
295 shell = getenv("SHELL");
306 SetupEnvironment(scr
);
311 execl(shell
, shell
, "-c", command
, NULL
);
312 wsyserror("could not execute %s -c %s", shell
, command
);
314 } else if (pid
< 0) {
315 wsyserror("cannot fork a new process");
317 _tuple
*data
= wmalloc(sizeof(_tuple
));
320 data
->command
= wstrdup(command
);
322 wAddDeathHandler(pid
, (WDeathHandler
*)shellCommandHandler
, data
);
328 *---------------------------------------------------------------------------
330 * Start a shell that will receive all stdin and stdout from processes
332 *---------------------------------------------------------------------------
335 StartLogShell(WScreen
*scr
)
342 SetupEnvironment(scr
);
344 if (pipe(in_fd
) < 0) {
345 wsyserror("could not create pipe for log shell\n");
348 if (pipe(out_fd
) < 0) {
349 wsyserror("could not create pipe for log shell\n");
354 if (pipe(err_fd
) < 0) {
355 wsyserror("could not create pipe for log shell\n");
365 wsyserror("could not fork a new process for log shell\n");
367 } else if (pid
== 0) {
376 if (dup2(in_fd
[1], 0) < 0) {
377 wsyserror("could not redirect stdin for log shell\n");
380 if (dup2(out_fd
[1], 1) < 0) {
381 wsyserror("could not redirect stdout for log shell\n");
384 if (dup2(err_fd
[1], 2) < 0) {
385 wsyserror("could not redirect stderr for log shell\n");
393 execl("/bin/sh", "/bin/sh", "-c", wPreferences
.logger_shell
, NULL
);
394 wsyserror("could not execute %s\n", wPreferences
.logger_shell
);
402 LogStdOut
= out_fd
[0];
403 LogStdErr
= err_fd
[0];
409 *---------------------------------------------------------------------
411 * Do a major cleanup and exit the program
413 *----------------------------------------------------------------------
416 wAbort(Bool dumpCore
)
421 for (i
=0; i
<wScreenCount
; i
++) {
422 scr
= wScreenWithNumber(i
);
426 printf(_("%s aborted.\n"), ProgName
);
437 printf(_("Usage: %s [options]\n"), ProgName
);
438 puts(_("The Window Maker window manager for the X window system"));
440 puts(_(" -display host:dpy display to use"));
442 puts(_(" --no-cpp disable preprocessing of configuration files"));
444 puts(_(" --no-dock do not open the application Dock"));
445 puts(_(" --no-clip do not open the workspace Clip"));
446 puts(_(" --no-autolaunch do not autolaunch applications"));
447 puts(_(" --dont-restore do not restore saved session"));
449 puts(_(" --locale locale locale to use"));
451 puts(_(" --create-stdcmap create the standard colormap hint in PseudoColor visuals"));
452 puts(_(" --visual-id visualid visual id of visual to use"));
453 puts(_(" --static do not update or save configurations"));
454 puts(_(" --no-polling do not periodically check for configuration updates"));
456 puts(_(" --synchronous turn on synchronous display mode"));
458 puts(_(" --version print version and exit"));
459 puts(_(" --help show this message"));
469 path
= wdefaultspathfordomain("WindowMaker");
471 if (access(path
, R_OK
)!=0) {
473 wfatal(_("could not find user GNUstep directory (%s).\n"
474 "Make sure you have installed Window Maker correctly and run wmaker.inst"),
478 wwarning(_("could not find user GNUstep directory (%s)."), path
);
480 if (system("wmaker.inst --batch") != 0) {
481 wwarning(_("There was an error while creating GNUstep directory, please "
482 "make sure you have installed Window Maker correctly and run wmaker.inst"));
484 wwarning(_("%s directory created with default configuration."), path
);
498 paths
= wstrconcat(wusergnusteppath(), "/Library/WindowMaker");
499 paths
= wstrappend(paths
, ":"DEF_CONFIG_PATHS
);
501 file
= wfindfile(paths
, DEF_INIT_SCRIPT
);
505 if (system(file
) != 0) {
506 wsyserror(_("%s:could not execute initialization script"), file
);
510 execl("/bin/sh", "/bin/sh", "-c", file
, NULL
);
511 wsyserror(_("%s:could not execute initialization script"), file
);
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 wsyserror(_("%s:could not execute exit script"), file
);
537 execl("/bin/sh", "/bin/sh", "-c", file
, NULL
);
538 wsyserror(_("%s:could not execute exit script"), file
);
548 getFullPath(char *path
)
552 char *basep
= (char*)buffer
;
554 if (*path
!= '/' && getcwd(buffer
, 1023)) {
557 if (strncmp(path
, "../", 3)==0) {
559 basep
= strchr(basep
, '/');
560 if (!basep
|| *path
==0)
564 if (*path
== '/' || strncmp(path
, "./",2)==0) {
577 return wstrconcat(path
);
586 main(int argc
, char **argv
)
588 int i_am_the_monitor
, i
, len
;
591 /* setup common stuff for the monitor and wmaker itself */
592 WMInitializeApplication("WindowMaker", &argc
, argv
);
594 memset(&wPreferences
, 0, sizeof(WPreferences
));
596 wPreferences
.fallbackWMs
= WMCreateArray(8);
597 alt
= getenv("WINDOWMAKER_ALT_WM");
599 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup(alt
));
601 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("blackbox"));
602 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("metacity"));
603 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("fvwm"));
604 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("twm"));
605 WMAddToArray(wPreferences
.fallbackWMs
, NULL
);
606 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("rxvt"));
607 WMAddToArray(wPreferences
.fallbackWMs
, wstrdup("xterm"));
611 for (i
= 1; i
< argc
; i
++)
613 if (strncmp(argv
[i
], "--for-real", strlen("--for-real"))==0)
618 else if (strcmp(argv
[i
], "-display")==0 || strcmp(argv
[i
], "--display")==0)
622 wwarning(_("too few arguments for %s"), argv
[i
-1]);
625 DisplayName
= argv
[i
];
629 DisplayName
= XDisplayName(DisplayName
);
630 len
= strlen(DisplayName
)+64;
632 snprintf(str
, len
, "DISPLAY=%s", DisplayName
);
635 if (i_am_the_monitor
)
636 return MonitorLoop(argc
, argv
);
638 return real_main(argc
, argv
);
643 real_main(int argc
, char **argv
)
652 setlocale(LC_ALL
, "");
656 /* for telling WPrefs what's the name of the wmaker binary being ran */
658 str
= wstrconcat("WMAKER_BIN_NAME=", argv
[0]);
663 Arguments
= wmalloc(sizeof(char*)*(ArgCount
+1));
664 for (i
= 0; i
< argc
; i
++)
666 Arguments
[i
]= argv
[i
];
668 /* add the extra option to signal that we're just restarting wmaker */
669 Arguments
[argc
-1]= "--for-real=";
670 Arguments
[argc
]= NULL
;
672 ProgName
= strrchr(argv
[0],'/');
682 for (i
=1; i
<argc
; i
++) {
684 if (strcmp(argv
[i
], "-nocpp")==0
685 || strcmp(argv
[i
], "--no-cpp")==0) {
686 wPreferences
.flags
.nocpp
=1;
689 if (strcmp(argv
[i
], "--for-real")==0) {
690 wPreferences
.flags
.restarting
= 0;
691 } else if (strcmp(argv
[i
], "--for-real=")==0) {
692 wPreferences
.flags
.restarting
= 1;
693 } else if (strcmp(argv
[i
], "--for-real-")==0) {
694 wPreferences
.flags
.restarting
= 2;
695 } else if (strcmp(argv
[i
], "-no-autolaunch")==0
696 || strcmp(argv
[i
], "--no-autolaunch")==0) {
697 wPreferences
.flags
.noautolaunch
= 1;
698 } else if (strcmp(argv
[i
], "-dont-restore")==0
699 || strcmp(argv
[i
], "--dont-restore")==0) {
700 wPreferences
.flags
.norestore
= 1;
701 } else if (strcmp(argv
[i
], "-nodock")==0
702 || strcmp(argv
[i
], "--no-dock")==0) {
703 wPreferences
.flags
.nodock
=1;
704 } else if (strcmp(argv
[i
], "-noclip")==0
705 || strcmp(argv
[i
], "--no-clip")==0) {
706 wPreferences
.flags
.noclip
=1;
707 } else if (strcmp(argv
[i
], "-version")==0
708 || strcmp(argv
[i
], "--version")==0) {
709 printf("Window Maker %s\n", VERSION
);
711 } else if (strcmp(argv
[i
], "--global_defaults_path")==0) {
712 printf("%s/WindowMaker\n", SYSCONFDIR
);
715 } else if (strcmp(argv
[i
], "--synchronous")==0) {
718 } else if (strcmp(argv
[i
], "-locale")==0
719 || strcmp(argv
[i
], "--locale")==0) {
722 wwarning(_("too few arguments for %s"), argv
[i
-1]);
726 } else if (strcmp(argv
[i
], "-display")==0
727 || strcmp(argv
[i
], "--display")==0) {
730 wwarning(_("too few arguments for %s"), argv
[i
-1]);
733 DisplayName
= argv
[i
];
734 } else if (strcmp(argv
[i
], "-visualid")==0
735 || strcmp(argv
[i
], "--visual-id")==0) {
738 wwarning(_("too few arguments for %s"), argv
[i
-1]);
741 if (sscanf(argv
[i
], "%i", &wVisualID
)!=1) {
742 wwarning(_("bad value for visualid: \"%s\""), argv
[i
]);
745 } else if (strcmp(argv
[i
], "-static")==0
746 || strcmp(argv
[i
], "--static")==0) {
748 wPreferences
.flags
.noupdates
= 1;
749 } else if (strcmp(argv
[i
], "-nopolling")==0
750 || strcmp(argv
[i
], "--no-polling")==0) {
752 wPreferences
.flags
.nopolling
= 1;
754 } else if (strcmp(argv
[i
], "-clientid")==0
755 || strcmp(argv
[i
], "-restore")==0) {
758 wwarning(_("too few arguments for %s"), argv
[i
-1]);
762 } else if (strcmp(argv
[i
], "--help")==0) {
766 printf(_("%s: invalid argument '%s'\n"), argv
[0], argv
[i
]);
767 printf(_("Try '%s --help' for more information\n"), argv
[0]);
773 if (!wPreferences
.flags
.noupdates
) {
774 /* check existence of Defaults DB directory */
780 /* return of wstrconcat should not be free-ed! read putenv man page */
781 putenv(wstrconcat("LANG=", Locale
));
783 Locale
= getenv("LC_ALL");
785 Locale
= getenv("LANG");
789 setlocale(LC_ALL
, "");
791 if (!Locale
|| strcmp(Locale
, "C")==0 || strcmp(Locale
, "POSIX")==0)
794 if (getenv("NLSPATH"))
795 bindtextdomain("WindowMaker", getenv("NLSPATH"));
797 bindtextdomain("WindowMaker", LOCALEDIR
);
798 bind_textdomain_codeset("WindowMaker", "UTF-8");
799 textdomain("WindowMaker");
801 if (!XSupportsLocale()) {
802 wwarning(_("X server does not support locale"));
805 if (XSetLocaleModifiers("") == NULL
) {
806 wwarning(_("cannot set locale modifiers"));
813 Locale
= wstrdup(Locale
);
814 ptr
= strchr(Locale
, '.');
820 dpy
= XOpenDisplay(DisplayName
);
822 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName
));
826 if (fcntl(ConnectionNumber(dpy
), F_SETFD
, FD_CLOEXEC
) < 0) {
827 wsyserror("error setting close-on-exec flag for X connection");
831 /* check if the user specified a complete display name (with screen).
832 * If so, only manage the specified screen */
834 str
= strchr(DisplayName
, ':');
838 if (str
&& sscanf(str
, ":%i.%i", &d
, &s
)==2)
841 DisplayName
= XDisplayName(DisplayName
);
843 int len
= strlen(DisplayName
)+64;
845 snprintf(str
, len
, "DISPLAY=%s", DisplayName
);
851 XSynchronize(dpy
, True
);
854 wXModifierInitialize();
857 wSessionConnectManager(argv
, argc
);