multiview and vertical splitview
[wmaker-crm.git] / src / main.c
blob049a0f9990254e564fbfcbe5e8beabc75654999d
1 /*
2 * Window Maker window manager
3 *
4 * Copyright (c) 1997, 1998 Alfredo K. Kojima
5 *
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,
19 * USA.
22 #include "wconfig.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <fcntl.h>
32 #include <X11/Xlib.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>
42 #define MAINFILE
44 #include "WindowMaker.h"
45 #include "window.h"
46 #include "funcs.h"
47 #include "menu.h"
48 #include "keybind.h"
49 #include "xmodifier.h"
50 #include "defaults.h"
51 #include "session.h"
53 #include <proplist.h>
55 /****** Global Variables ******/
57 /* general info */
59 Display *dpy;
61 char *ProgName;
63 unsigned int ValidModMask = 0xff;
65 /* locale to use. NULL==POSIX or C */
66 char *Locale=NULL;
68 int wScreenCount=0;
70 WPreferences wPreferences;
73 proplist_t wDomainName;
74 proplist_t wAttributeDomainName;
76 WShortKey wKeyBindings[WKBD_LAST];
78 /* defaults domains */
79 WDDomain *WDWindowMaker = NULL;
80 WDDomain *WDRootMenu = NULL;
81 WDDomain *WDWindowAttributes = NULL;
84 /* XContexts */
85 XContext wWinContext;
86 XContext wAppWinContext;
87 XContext wStackContext;
89 /* Atoms */
90 Atom _XA_WM_STATE;
91 Atom _XA_WM_CHANGE_STATE;
92 Atom _XA_WM_PROTOCOLS;
93 Atom _XA_WM_TAKE_FOCUS;
94 Atom _XA_WM_DELETE_WINDOW;
95 Atom _XA_WM_SAVE_YOURSELF;
96 Atom _XA_WM_CLIENT_LEADER;
97 Atom _XA_WM_COLORMAP_WINDOWS;
98 Atom _XA_WM_COLORMAP_NOTIFY;
100 Atom _XA_GNUSTEP_WM_ATTR;
101 Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
102 Atom _XA_GNUSTEP_WM_RESIZEBAR;
104 Atom _XA_WINDOWMAKER_MENU;
105 Atom _XA_WINDOWMAKER_WM_PROTOCOLS;
106 Atom _XA_WINDOWMAKER_STATE;
108 Atom _XA_WINDOWMAKER_WM_FUNCTION;
109 Atom _XA_WINDOWMAKER_NOTICEBOARD;
110 Atom _XA_WINDOWMAKER_COMMAND;
112 #ifdef OFFIX_DND
113 Atom _XA_DND_PROTOCOL;
114 Atom _XA_DND_SELECTION;
115 #endif
117 /* cursors */
118 Cursor wCursor[WCUR_LAST];
120 /* last event timestamp for XSetInputFocus */
121 Time LastTimestamp;
122 /* timestamp on the last time we did XSetInputFocus() */
123 Time LastFocusChange;
125 #ifdef SHAPE
126 Bool wShapeSupported;
127 int wShapeEventBase;
128 #endif
130 #ifdef KEEP_XKB_LOCK_STATUS
131 Bool wXkbSupported;
132 int wXkbEventBase;
133 #endif
135 /* special flags */
136 char WProgramState = WSTATE_NORMAL;
137 char WDelayedActionSet = 0;
139 /* temporary stuff */
140 int wVisualID = -1;
143 /******** End Global Variables *****/
145 static char *DisplayName = NULL;
147 static char **Arguments;
149 static int ArgCount;
151 extern void EventLoop();
152 extern void StartUp();
154 static Bool multiHead = True;
156 /* stdi/o for log shell */
157 static int LogStdIn = -1, LogStdOut = -1, LogStdErr = -1;
160 void
161 Exit(int status)
163 #ifdef XSMP_ENABLED
164 wSessionDisconnectManager();
165 #endif
166 if (dpy)
167 XCloseDisplay(dpy);
169 exit(status);
173 void
174 Restart(char *manager, Bool abortOnFailure)
176 char *prog=NULL;
177 char *argv[MAX_RESTART_ARGS];
178 int i;
180 if (manager && manager[0]!=0) {
181 prog = argv[0] = strtok(manager, " ");
182 for (i=1; i<MAX_RESTART_ARGS; i++) {
183 argv[i]=strtok(NULL, " ");
184 if (argv[i]==NULL) {
185 break;
189 if (dpy) {
190 #ifdef XSMP_ENABLED
191 wSessionDisconnectManager();
192 #endif
193 XCloseDisplay(dpy);
194 dpy = NULL;
196 if (!prog) {
197 execvp(Arguments[0], Arguments);
198 wfatal(_("failed to restart Window Maker."));
199 } else {
200 execvp(prog, argv);
201 wsyserror(_("could not exec %s"), prog);
203 if (abortOnFailure)
204 exit(-1);
209 void
210 SetupEnvironment(WScreen *scr)
212 char *tmp, *ptr;
213 char buf[16];
215 if (multiHead) {
216 tmp = wmalloc(strlen(DisplayName)+64);
217 sprintf(tmp, "DISPLAY=%s", XDisplayName(DisplayName));
218 ptr = strchr(strchr(tmp, ':'), '.');
219 if (ptr)
220 *ptr = 0;
221 sprintf(buf, ".%i", scr->screen);
222 strcat(tmp, buf);
223 putenv(tmp);
225 tmp = wmalloc(60);
226 sprintf(tmp, "WRASTER_COLOR_RESOLUTION%i=%i", scr->screen,
227 scr->rcontext->attribs->colors_per_channel);
228 putenv(tmp);
234 typedef struct {
235 WScreen *scr;
236 char *command;
237 } _tuple;
240 static void
241 shellCommandHandler(pid_t pid, unsigned char status, _tuple *data)
243 if (status == 127) {
244 char *buffer;
246 buffer = wstrappend(_("Could not execute command: "), data->command);
248 wMessageDialog(data->scr, _("Error"), buffer, _("OK"), NULL, NULL);
249 free(buffer);
250 } else if (status != 127) {
252 printf("%s: %i\n", data->command, status);
256 free(data->command);
257 free(data);
261 void
262 ExecuteShellCommand(WScreen *scr, char *command)
264 static char *shell = NULL;
265 pid_t pid;
268 * This have a problem: if the shell is tcsh (not sure about others)
269 * and ~/.tcshrc have /bin/stty erase ^H somewhere on it, the shell
270 * will block and the command will not be executed.
271 if (!shell) {
272 shell = getenv("SHELL");
273 if (!shell)
274 shell = "/bin/sh";
277 shell = "/bin/sh";
279 pid = fork();
281 if (pid==0) {
283 SetupEnvironment(scr);
285 #ifdef HAVE_SETPGID
286 setpgid(0, 0);
287 #endif
288 execl(shell, shell, "-c", command, NULL);
289 wsyserror("could not execute %s -c %s", shell, command);
290 Exit(-1);
291 } else if (pid < 0) {
292 wsyserror("cannot fork a new process");
293 } else {
294 _tuple *data = wmalloc(sizeof(_tuple));
296 data->scr = scr;
297 data->command = wstrdup(command);
299 wAddDeathHandler(pid, (WDeathHandler*)shellCommandHandler, data);
305 *---------------------------------------------------------------------------
306 * StartLogShell
307 * Start a shell that will receive all stdin and stdout from processes
308 * forked by wmaker.
309 *---------------------------------------------------------------------------
311 void
312 StartLogShell(WScreen *scr)
314 int in_fd[2];
315 int out_fd[2];
316 int err_fd[2];
317 pid_t pid;
319 SetupEnvironment(scr);
321 if (pipe(in_fd) < 0) {
322 wsyserror("could not create pipe for log shell\n");
323 return;
325 if (pipe(out_fd) < 0) {
326 wsyserror("could not create pipe for log shell\n");
327 close(in_fd[0]);
328 close(in_fd[1]);
329 return;
331 if (pipe(err_fd) < 0) {
332 wsyserror("could not create pipe for log shell\n");
333 close(out_fd[0]);
334 close(out_fd[1]);
335 close(in_fd[0]);
336 close(in_fd[1]);
337 return;
340 pid = fork();
341 if (pid < 0) {
342 wsyserror("could not fork a new process for log shell\n");
343 return;
344 } else if (pid == 0) {
345 close(in_fd[0]);
346 close(out_fd[1]);
347 close(err_fd[1]);
349 close(0);
350 close(1);
351 close(2);
353 if (dup2(in_fd[1], 0) < 0) {
354 wsyserror("could not redirect stdin for log shell\n");
355 exit(1);
357 if (dup2(out_fd[1], 1) < 0) {
358 wsyserror("could not redirect stdout for log shell\n");
359 exit(1);
361 if (dup2(err_fd[1], 2) < 0) {
362 wsyserror("could not redirect stderr for log shell\n");
363 exit(1);
366 close(in_fd[1]);
367 close(out_fd[1]);
368 close(err_fd[1]);
370 execl("/bin/sh", "/bin/sh", "-c", wPreferences.logger_shell, NULL);
371 wsyserror("could not execute %s\n", wPreferences.logger_shell);
372 exit(1);
373 } else {
374 close(in_fd[1]);
375 close(out_fd[0]);
376 close(err_fd[0]);
378 LogStdIn = in_fd[1];
379 LogStdOut = out_fd[0];
380 LogStdErr = err_fd[0];
386 *---------------------------------------------------------------------
387 * wAbort--
388 * Do a major cleanup and exit the program
390 *----------------------------------------------------------------------
392 void
393 wAbort(Bool dumpCore)
395 int i;
396 WScreen *scr;
398 for (i=0; i<wScreenCount; i++) {
399 scr = wScreenWithNumber(i);
400 if (scr)
401 RestoreDesktop(scr);
403 printf(_("%s aborted.\n"), ProgName);
404 if (dumpCore)
405 abort();
406 else
407 exit(1);
411 void
412 print_help()
414 printf(_("Usage: %s [options]\n"), ProgName);
415 puts(_("The Window Maker window manager for the X window system"));
416 puts("");
417 puts(_(" -display host:dpy display to use"));
418 #ifdef USECPP
419 puts(_(" --no-cpp disable preprocessing of configuration files"));
420 #endif
421 puts(_(" --no-dock do not open the application Dock"));
422 puts(_(" --no-clip do not open the workspace Clip"));
424 puts(_(" --locale locale locale to use"));
426 puts(_(" --visual-id visualid visual id of visual to use"));
427 puts(_(" --static do not update or save configurations"));
428 #ifdef DEBUG
429 puts(_(" --synchronous turn on synchronous display mode"));
430 #endif
431 puts(_(" --version print version and exit"));
432 puts(_(" --help show this message"));
437 void
438 check_defaults()
440 char *path;
442 path = wdefaultspathfordomain("");
443 if (access(path, R_OK)!=0) {
444 wfatal(_("could not find user GNUstep directory (%s).\n"
445 "Make sure you have installed Window Maker correctly and run wmaker.inst"),
446 path);
447 exit(1);
450 free(path);
454 static void
455 execInitScript()
457 char *file;
458 char *paths = wstrappend(wusergnusteppath(), ":"DEF_CONFIG_PATHS);
460 file = wfindfile(paths, DEF_INIT_SCRIPT);
461 free(paths);
463 if (file) {
464 if (system(file) != 0) {
465 wsyserror(_("%s:could not execute initialization script"), file);
467 #if 0
468 if (fork()==0) {
469 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
470 wsyserror(_("%s:could not execute initialization script"), file);
471 exit(1);
473 #endif
474 free(file);
479 void
480 ExecExitScript()
482 char *file;
483 char *paths = wstrappend(wusergnusteppath(), ":"DEF_CONFIG_PATHS);
485 file = wfindfile(paths, DEF_EXIT_SCRIPT);
486 free(paths);
488 if (file) {
489 if (system(file) != 0) {
490 wsyserror(_("%s:could not execute exit script"), file);
492 #if 0
493 if (fork()==0) {
494 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
495 wsyserror(_("%s:could not execute exit script"), file);
496 exit(1);
498 #endif
499 free(file);
503 #if 0
504 char*
505 getFullPath(char *path)
507 char buffer[1024];
508 char *tmp;
509 char *basep = (char*)buffer;
511 if (*path != '/' && getcwd(buffer, 1023)) {
513 for (;;) {
514 if (strncmp(path, "../", 3)==0) {
515 path += 3;
516 basep = strchr(basep, '/');
517 if (!basep || *path==0)
518 break;
521 if (*path == '/' || strncmp(path, "./",2)==0) {
522 tmp =
526 * path
527 * ./path
528 * ../path
529 * ../../path
533 } else {
534 return wstrappend(path);
537 return tmp;
539 #endif
542 main(int argc, char **argv)
544 int i, restart=0;
545 char *str;
546 int d, s;
547 #ifdef DEBUG
548 Bool doSync = False;
549 #endif
551 wsetabort(wAbort);
553 /* for telling WPrefs what's the name of the wmaker binary being ran */
555 /* char *tmp;
557 tmp = getFullPath(argv[0]);*/
558 str = wstrappend("WMAKER_BIN_NAME=", argv[0]);
559 /* free(tmp);*/
560 putenv(str);
563 ArgCount = argc;
564 Arguments = argv;
566 WMInitializeApplication("WindowMaker", &argc, argv);
569 ProgName = strrchr(argv[0],'/');
570 if (!ProgName)
571 ProgName = argv[0];
572 else
573 ProgName++;
576 restart = 0;
578 memset(&wPreferences, 0, sizeof(WPreferences));
580 if (argc>1) {
581 for (i=1; i<argc; i++) {
582 #ifdef USECPP
583 if (strcmp(argv[i], "-nocpp")==0
584 || strcmp(argv[i], "--no-cpp")==0) {
585 wPreferences.flags.nocpp=1;
586 } else
587 #endif
588 if (strcmp(argv[i], "-nodock")==0
589 || strcmp(argv[i], "--no-dock")==0) {
590 wPreferences.flags.nodock=1;
591 } else if (strcmp(argv[i], "-noclip")==0
592 || strcmp(argv[i], "--no-clip")==0) {
593 wPreferences.flags.noclip=1;
594 } else if (strcmp(argv[i], "-version")==0
595 || strcmp(argv[i], "--version")==0) {
596 printf("Window Maker %s\n", VERSION);
597 exit(0);
598 } else if (strcmp(argv[i], "--global_defaults_path")==0) {
599 printf("%s/WindowMaker\n", SYSCONFDIR);
600 exit(0);
601 #ifdef DEBUG
602 } else if (strcmp(argv[i], "--synchronous")==0) {
603 doSync = 1;
604 #endif
605 } else if (strcmp(argv[i], "-locale")==0
606 || strcmp(argv[i], "--locale")==0) {
607 i++;
608 if (i>=argc) {
609 wwarning(_("too few arguments for %s"), argv[i-1]);
610 exit(0);
612 Locale = argv[i];
613 } else if (strcmp(argv[i], "-display")==0) {
614 i++;
615 if (i>=argc) {
616 wwarning(_("too few arguments for %s"), argv[i-1]);
617 exit(0);
619 DisplayName = argv[i];
620 } else if (strcmp(argv[i], "-visualid")==0
621 || strcmp(argv[i], "--visual-id")==0) {
622 i++;
623 if (i>=argc) {
624 wwarning(_("too few arguments for %s"), argv[i-1]);
625 exit(0);
627 if (sscanf(argv[i], "%i", &wVisualID)!=1) {
628 wwarning(_("bad value for visualid: \"%s\""), argv[i]);
629 exit(0);
631 } else if (strcmp(argv[i], "-static")==0
632 || strcmp(argv[i], "--static")==0) {
634 wPreferences.flags.noupdates = 1;
635 #ifdef XSMP_ENABLED
636 } else if (strcmp(argv[i], "-clientid")==0
637 || strcmp(argv[i], "-restore")==0) {
638 i++;
639 if (i>=argc) {
640 wwarning(_("too few arguments for %s"), argv[i-1]);
641 exit(0);
643 #endif
644 } else if (strcmp(argv[i], "--help")==0) {
645 print_help();
646 exit(0);
647 } else {
648 printf(_("%s: invalid argument '%s'\n"), argv[0], argv[i]);
649 printf(_("Try '%s --help' for more information\n"), argv[0]);
650 exit(1);
655 if (!wPreferences.flags.noupdates) {
656 /* check existence of Defaults DB directory */
657 check_defaults();
660 #if 0
661 tmp = getenv("LANG");
662 if (tmp) {
663 if (setlocale(LC_ALL,"") == NULL) {
664 wwarning("cannot set locale %s", tmp);
665 wwarning("falling back to C locale");
666 setlocale(LC_ALL,"C");
667 Locale = NULL;
668 } else {
669 if (strcmp(tmp, "C")==0 || strcmp(tmp, "POSIX")==0)
670 Locale = NULL;
671 else
672 Locale = tmp;
674 } else {
675 Locale = NULL;
677 #endif
678 if (!Locale) {
679 Locale = getenv("LANG");
681 setlocale(LC_ALL, Locale);
682 if (!Locale || strcmp(Locale, "C")==0 || strcmp(Locale, "POSIX")==0)
683 Locale = NULL;
684 #ifdef I18N
685 if (getenv("NLSPATH"))
686 bindtextdomain("WindowMaker", getenv("NLSPATH"));
687 else
688 bindtextdomain("WindowMaker", LOCALEDIR);
689 textdomain("WindowMaker");
691 if (!XSupportsLocale()) {
692 wwarning(_("X server does not support locale"));
694 if (XSetLocaleModifiers("") == NULL) {
695 wwarning(_("cannot set locale modifiers"));
697 #endif
699 if (Locale) {
700 char *ptr;
702 Locale = wstrdup(Locale);
703 ptr = strchr(Locale, '.');
704 if (ptr)
705 *ptr = 0;
709 /* open display */
710 dpy = XOpenDisplay(DisplayName);
711 if (dpy == NULL) {
712 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName));
713 exit(1);
716 if (fcntl(ConnectionNumber(dpy), F_SETFD, FD_CLOEXEC) < 0) {
717 wsyserror("error setting close-on-exec flag for X connection");
718 exit(1);
721 /* check if the user specified a complete display name (with screen).
722 * If so, only manage the specified screen */
723 if (DisplayName)
724 str = strchr(DisplayName, ':');
725 else
726 str = NULL;
728 if (str && sscanf(str, "%i.%i", &d, &s)==2)
729 multiHead = False;
731 DisplayName = XDisplayName(DisplayName);
732 str = wmalloc(strlen(DisplayName)+64);
733 sprintf(str, "DISPLAY=%s", DisplayName);
734 putenv(str);
736 #ifdef DEBUG
737 if (doSync)
738 XSynchronize(dpy, True);
739 #endif
741 wXModifierInitialize();
743 #ifdef XSMP_ENABLED
744 wSessionConnectManager(argv, argc);
745 #endif
747 StartUp(!multiHead);
749 if (wScreenCount==1)
750 multiHead = False;
752 execInitScript();
754 EventLoop();
755 return -1;