- s/sprintf/snprintf
[wmaker-crm.git] / src / main.c
blob3f39c1c3c57940b18890bc00dc608a30696dc9b0
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"
52 #include "dialog.h"
54 #include <proplist.h>
56 /****** Global Variables ******/
58 /* general info */
60 Display *dpy;
62 char *ProgName;
64 unsigned int ValidModMask = 0xff;
66 /* locale to use. NULL==POSIX or C */
67 char *Locale=NULL;
69 int wScreenCount=0;
71 WPreferences wPreferences;
74 proplist_t wDomainName;
75 proplist_t wAttributeDomainName;
77 WShortKey wKeyBindings[WKBD_LAST];
79 /* defaults domains */
80 WDDomain *WDWindowMaker = NULL;
81 WDDomain *WDWindowAttributes = NULL;
82 WDDomain *WDRootMenu = 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;
103 Atom _XA_GNUSTEP_TITLEBAR_STATE;
105 Atom _XA_WINDOWMAKER_MENU;
106 Atom _XA_WINDOWMAKER_WM_PROTOCOLS;
107 Atom _XA_WINDOWMAKER_STATE;
109 Atom _XA_WINDOWMAKER_WM_FUNCTION;
110 Atom _XA_WINDOWMAKER_NOTICEBOARD;
111 Atom _XA_WINDOWMAKER_COMMAND;
113 Atom _XA_WINDOWMAKER_ICON_SIZE;
114 Atom _XA_WINDOWMAKER_ICON_TILE;
117 #ifdef OFFIX_DND
118 Atom _XA_DND_PROTOCOL;
119 Atom _XA_DND_SELECTION;
120 #endif
122 /* cursors */
123 Cursor wCursor[WCUR_LAST];
125 /* last event timestamp for XSetInputFocus */
126 Time LastTimestamp;
127 /* timestamp on the last time we did XSetInputFocus() */
128 Time LastFocusChange;
130 #ifdef SHAPE
131 Bool wShapeSupported;
132 int wShapeEventBase;
133 #endif
135 #ifdef KEEP_XKB_LOCK_STATUS
136 Bool wXkbSupported;
137 int wXkbEventBase;
138 #endif
140 /* special flags */
141 char WProgramSigState = 0;
142 char WProgramState = WSTATE_NORMAL;
143 char WDelayedActionSet = 0;
146 /* temporary stuff */
147 int wVisualID = -1;
150 /******** End Global Variables *****/
152 static char *DisplayName = NULL;
154 static char **Arguments;
156 static int ArgCount;
158 extern void EventLoop();
159 extern void StartUp();
161 static Bool multiHead = True;
163 /* stdi/o for log shell */
164 static int LogStdIn = -1, LogStdOut = -1, LogStdErr = -1;
167 void
168 Exit(int status)
170 #ifdef XSMP_ENABLED
171 wSessionDisconnectManager();
172 #endif
173 if (dpy)
174 XCloseDisplay(dpy);
176 exit(status);
180 void
181 Restart(char *manager, Bool abortOnFailure)
183 char *prog=NULL;
184 char *argv[MAX_RESTART_ARGS];
185 int i;
187 if (manager && manager[0]!=0) {
188 prog = argv[0] = strtok(manager, " ");
189 for (i=1; i<MAX_RESTART_ARGS; i++) {
190 argv[i]=strtok(NULL, " ");
191 if (argv[i]==NULL) {
192 break;
196 if (dpy) {
197 #ifdef XSMP_ENABLED
198 wSessionDisconnectManager();
199 #endif
200 XCloseDisplay(dpy);
201 dpy = NULL;
203 if (!prog) {
204 execvp(Arguments[0], Arguments);
205 wfatal(_("failed to restart Window Maker."));
206 } else {
207 execvp(prog, argv);
208 wsyserror(_("could not exec %s"), prog);
210 if (abortOnFailure)
211 exit(-1);
216 void
217 SetupEnvironment(WScreen *scr)
219 char *tmp, *ptr;
220 char buf[16];
222 if (multiHead) {
223 int len = strlen(DisplayName)+64;
224 tmp = wmalloc(len);
225 snprintf(tmp, len, "DISPLAY=%s", XDisplayName(DisplayName));
226 ptr = strchr(strchr(tmp, ':'), '.');
227 if (ptr)
228 *ptr = 0;
229 snprintf(buf, sizeof(buf), ".%i", scr->screen);
230 strcat(tmp, buf);
231 putenv(tmp);
233 tmp = wmalloc(60);
234 snprintf(tmp, 60, "WRASTER_COLOR_RESOLUTION%i=%i", scr->screen,
235 scr->rcontext->attribs->colors_per_channel);
236 putenv(tmp);
242 typedef struct {
243 WScreen *scr;
244 char *command;
245 } _tuple;
248 static void
249 shellCommandHandler(pid_t pid, unsigned char status, _tuple *data)
251 if (status == 127) {
252 char *buffer;
254 buffer = wstrconcat(_("Could not execute command: "), data->command);
256 wMessageDialog(data->scr, _("Error"), buffer, _("OK"), NULL, NULL);
257 wfree(buffer);
258 } else if (status != 127) {
260 printf("%s: %i\n", data->command, status);
264 wfree(data->command);
265 wfree(data);
269 void
270 ExecuteShellCommand(WScreen *scr, char *command)
272 static char *shell = NULL;
273 pid_t pid;
276 * This have a problem: if the shell is tcsh (not sure about others)
277 * and ~/.tcshrc have /bin/stty erase ^H somewhere on it, the shell
278 * will block and the command will not be executed.
279 if (!shell) {
280 shell = getenv("SHELL");
281 if (!shell)
282 shell = "/bin/sh";
285 shell = "/bin/sh";
287 pid = fork();
289 if (pid==0) {
291 SetupEnvironment(scr);
293 #ifdef HAVE_SETPGID
294 setpgid(0, 0);
295 #endif
296 execl(shell, shell, "-c", command, NULL);
297 wsyserror("could not execute %s -c %s", shell, command);
298 Exit(-1);
299 } else if (pid < 0) {
300 wsyserror("cannot fork a new process");
301 } else {
302 _tuple *data = wmalloc(sizeof(_tuple));
304 data->scr = scr;
305 data->command = wstrdup(command);
307 wAddDeathHandler(pid, (WDeathHandler*)shellCommandHandler, data);
313 *---------------------------------------------------------------------------
314 * StartLogShell
315 * Start a shell that will receive all stdin and stdout from processes
316 * forked by wmaker.
317 *---------------------------------------------------------------------------
319 void
320 StartLogShell(WScreen *scr)
322 int in_fd[2];
323 int out_fd[2];
324 int err_fd[2];
325 pid_t pid;
327 SetupEnvironment(scr);
329 if (pipe(in_fd) < 0) {
330 wsyserror("could not create pipe for log shell\n");
331 return;
333 if (pipe(out_fd) < 0) {
334 wsyserror("could not create pipe for log shell\n");
335 close(in_fd[0]);
336 close(in_fd[1]);
337 return;
339 if (pipe(err_fd) < 0) {
340 wsyserror("could not create pipe for log shell\n");
341 close(out_fd[0]);
342 close(out_fd[1]);
343 close(in_fd[0]);
344 close(in_fd[1]);
345 return;
348 pid = fork();
349 if (pid < 0) {
350 wsyserror("could not fork a new process for log shell\n");
351 return;
352 } else if (pid == 0) {
353 close(in_fd[0]);
354 close(out_fd[1]);
355 close(err_fd[1]);
357 close(0);
358 close(1);
359 close(2);
361 if (dup2(in_fd[1], 0) < 0) {
362 wsyserror("could not redirect stdin for log shell\n");
363 exit(1);
365 if (dup2(out_fd[1], 1) < 0) {
366 wsyserror("could not redirect stdout for log shell\n");
367 exit(1);
369 if (dup2(err_fd[1], 2) < 0) {
370 wsyserror("could not redirect stderr for log shell\n");
371 exit(1);
374 close(in_fd[1]);
375 close(out_fd[1]);
376 close(err_fd[1]);
378 execl("/bin/sh", "/bin/sh", "-c", wPreferences.logger_shell, NULL);
379 wsyserror("could not execute %s\n", wPreferences.logger_shell);
380 exit(1);
381 } else {
382 close(in_fd[1]);
383 close(out_fd[0]);
384 close(err_fd[0]);
386 LogStdIn = in_fd[1];
387 LogStdOut = out_fd[0];
388 LogStdErr = err_fd[0];
394 *---------------------------------------------------------------------
395 * wAbort--
396 * Do a major cleanup and exit the program
398 *----------------------------------------------------------------------
400 void
401 wAbort(Bool dumpCore)
403 int i;
404 WScreen *scr;
406 for (i=0; i<wScreenCount; i++) {
407 scr = wScreenWithNumber(i);
408 if (scr)
409 RestoreDesktop(scr);
411 printf(_("%s aborted.\n"), ProgName);
412 if (dumpCore)
413 abort();
414 else
415 exit(1);
419 void
420 print_help()
422 printf(_("Usage: %s [options]\n"), ProgName);
423 puts(_("The Window Maker window manager for the X window system"));
424 puts("");
425 puts(_(" -display host:dpy display to use"));
426 #ifdef USECPP
427 puts(_(" --no-cpp disable preprocessing of configuration files"));
428 #endif
429 puts(_(" --no-dock do not open the application Dock"));
430 puts(_(" --no-clip do not open the workspace Clip"));
431 puts(_(" --no-autolaunch do not autolaunch applications"));
432 puts(_(" --dont-restore do not restore saved session"));
434 puts(_(" --locale locale locale to use"));
436 puts(_(" --create-stdcmap create the standard colormap hint in PseudoColor visuals"));
437 puts(_(" --visual-id visualid visual id of visual to use"));
438 puts(_(" --static do not update or save configurations"));
439 puts(_(" --no-polling do not periodically check for configuration updates"));
440 #ifdef DEBUG
441 puts(_(" --synchronous turn on synchronous display mode"));
442 #endif
443 puts(_(" --version print version and exit"));
444 puts(_(" --help show this message"));
449 void
450 check_defaults()
452 char *path;
454 path = wdefaultspathfordomain("");
455 if (access(path, R_OK)!=0) {
456 wfatal(_("could not find user GNUstep directory (%s).\n"
457 "Make sure you have installed Window Maker correctly and run wmaker.inst"),
458 path);
459 exit(1);
462 wfree(path);
466 static void
467 execInitScript()
469 char *file;
470 char *paths = wstrconcat(wusergnusteppath(), ":"DEF_CONFIG_PATHS);
472 file = wfindfile(paths, DEF_INIT_SCRIPT);
473 wfree(paths);
475 if (file) {
476 if (system(file) != 0) {
477 wsyserror(_("%s:could not execute initialization script"), file);
479 #if 0
480 if (fork()==0) {
481 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
482 wsyserror(_("%s:could not execute initialization script"), file);
483 exit(1);
485 #endif
486 wfree(file);
491 void
492 ExecExitScript()
494 char *file;
495 char *paths = wstrconcat(wusergnusteppath(), ":"DEF_CONFIG_PATHS);
497 file = wfindfile(paths, DEF_EXIT_SCRIPT);
498 wfree(paths);
500 if (file) {
501 if (system(file) != 0) {
502 wsyserror(_("%s:could not execute exit script"), file);
504 #if 0
505 if (fork()==0) {
506 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
507 wsyserror(_("%s:could not execute exit script"), file);
508 exit(1);
510 #endif
511 wfree(file);
515 #if 0
516 char*
517 getFullPath(char *path)
519 char buffer[1024];
520 char *tmp;
521 char *basep = (char*)buffer;
523 if (*path != '/' && getcwd(buffer, 1023)) {
525 for (;;) {
526 if (strncmp(path, "../", 3)==0) {
527 path += 3;
528 basep = strchr(basep, '/');
529 if (!basep || *path==0)
530 break;
533 if (*path == '/' || strncmp(path, "./",2)==0) {
534 tmp =
538 * path
539 * ./path
540 * ../path
541 * ../../path
545 } else {
546 return wstrconcat(path);
549 return tmp;
551 #endif
554 main(int argc, char **argv)
556 int i, restart=0;
557 char *str;
558 int d, s;
559 #ifdef DEBUG
560 Bool doSync = False;
561 #endif
562 setlocale(LC_ALL, "");
564 wsetabort(wAbort);
566 /* for telling WPrefs what's the name of the wmaker binary being ran */
568 str = wstrconcat("WMAKER_BIN_NAME=", argv[0]);
569 putenv(str);
571 ArgCount = argc;
572 Arguments = argv;
574 WMInitializeApplication("WindowMaker", &argc, argv);
577 ProgName = strrchr(argv[0],'/');
578 if (!ProgName)
579 ProgName = argv[0];
580 else
581 ProgName++;
584 restart = 0;
586 memset(&wPreferences, 0, sizeof(WPreferences));
588 if (argc>1) {
589 for (i=1; i<argc; i++) {
590 #ifdef USECPP
591 if (strcmp(argv[i], "-nocpp")==0
592 || strcmp(argv[i], "--no-cpp")==0) {
593 wPreferences.flags.nocpp=1;
594 } else
595 #endif
596 if (strcmp(argv[i], "-no-autolaunch")==0
597 || strcmp(argv[i], "--no-autolaunch")==0) {
598 wPreferences.flags.noautolaunch = 1;
599 } else if (strcmp(argv[i], "-dont-restore")==0
600 || strcmp(argv[i], "--dont-restore")==0) {
601 wPreferences.flags.norestore = 1;
602 } else if (strcmp(argv[i], "-nodock")==0
603 || strcmp(argv[i], "--no-dock")==0) {
604 wPreferences.flags.nodock=1;
605 } else if (strcmp(argv[i], "-noclip")==0
606 || strcmp(argv[i], "--no-clip")==0) {
607 wPreferences.flags.noclip=1;
608 } else if (strcmp(argv[i], "-version")==0
609 || strcmp(argv[i], "--version")==0) {
610 printf("Window Maker %s\n", VERSION);
611 exit(0);
612 } else if (strcmp(argv[i], "--global_defaults_path")==0) {
613 printf("%s/WindowMaker\n", SYSCONFDIR);
614 exit(0);
615 #ifdef DEBUG
616 } else if (strcmp(argv[i], "--synchronous")==0) {
617 doSync = 1;
618 #endif
619 } else if (strcmp(argv[i], "-locale")==0
620 || strcmp(argv[i], "--locale")==0) {
621 i++;
622 if (i>=argc) {
623 wwarning(_("too few arguments for %s"), argv[i-1]);
624 exit(0);
626 Locale = argv[i];
627 } else if (strcmp(argv[i], "-display")==0
628 || strcmp(argv[i], "--display")==0) {
629 i++;
630 if (i>=argc) {
631 wwarning(_("too few arguments for %s"), argv[i-1]);
632 exit(0);
634 DisplayName = argv[i];
635 } else if (strcmp(argv[i], "-visualid")==0
636 || strcmp(argv[i], "--visual-id")==0) {
637 i++;
638 if (i>=argc) {
639 wwarning(_("too few arguments for %s"), argv[i-1]);
640 exit(0);
642 if (sscanf(argv[i], "%i", &wVisualID)!=1) {
643 wwarning(_("bad value for visualid: \"%s\""), argv[i]);
644 exit(0);
646 } else if (strcmp(argv[i], "-static")==0
647 || strcmp(argv[i], "--static")==0) {
649 wPreferences.flags.noupdates = 1;
650 } else if (strcmp(argv[i], "-nopolling")==0
651 || strcmp(argv[i], "--no-polling")==0) {
653 wPreferences.flags.nopolling = 1;
654 #ifdef XSMP_ENABLED
655 } else if (strcmp(argv[i], "-clientid")==0
656 || strcmp(argv[i], "-restore")==0) {
657 i++;
658 if (i>=argc) {
659 wwarning(_("too few arguments for %s"), argv[i-1]);
660 exit(0);
662 #endif
663 } else if (strcmp(argv[i], "--help")==0) {
664 print_help();
665 exit(0);
666 } else {
667 printf(_("%s: invalid argument '%s'\n"), argv[0], argv[i]);
668 printf(_("Try '%s --help' for more information\n"), argv[0]);
669 exit(1);
674 if (!wPreferences.flags.noupdates) {
675 /* check existence of Defaults DB directory */
676 check_defaults();
680 if (!Locale) {
681 Locale = getenv("LC_ALL");
683 if (!Locale) {
684 Locale = getenv("LANG");
687 setlocale(LC_ALL, "");
689 if (!Locale || strcmp(Locale, "C")==0 || strcmp(Locale, "POSIX")==0)
690 Locale = NULL;
691 #ifdef I18N
692 if (getenv("NLSPATH"))
693 bindtextdomain("WindowMaker", getenv("NLSPATH"));
694 else
695 bindtextdomain("WindowMaker", LOCALEDIR);
696 textdomain("WindowMaker");
698 if (!XSupportsLocale()) {
699 wwarning(_("X server does not support locale"));
702 if (XSetLocaleModifiers("") == NULL) {
703 wwarning(_("cannot set locale modifiers"));
705 #endif
707 if (Locale) {
708 char *ptr;
710 Locale = wstrdup(Locale);
711 ptr = strchr(Locale, '.');
712 if (ptr)
713 *ptr = 0;
716 /* open display */
717 dpy = XOpenDisplay(DisplayName);
718 if (dpy == NULL) {
719 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName));
720 exit(1);
723 if (fcntl(ConnectionNumber(dpy), F_SETFD, FD_CLOEXEC) < 0) {
724 wsyserror("error setting close-on-exec flag for X connection");
725 exit(1);
728 /* check if the user specified a complete display name (with screen).
729 * If so, only manage the specified screen */
730 if (DisplayName)
731 str = strchr(DisplayName, ':');
732 else
733 str = NULL;
735 if (str && sscanf(str, ":%i.%i", &d, &s)==2)
736 multiHead = False;
738 DisplayName = XDisplayName(DisplayName);
740 int len = strlen(DisplayName)+64;
741 str = wmalloc(len);
742 snprintf(str, len, "DISPLAY=%s", DisplayName);
744 putenv(str);
746 #ifdef DEBUG
747 if (doSync)
748 XSynchronize(dpy, True);
749 #endif
751 wXModifierInitialize();
753 #ifdef XSMP_ENABLED
754 wSessionConnectManager(argv, argc);
755 #endif
757 StartUp(!multiHead);
759 if (wScreenCount==1)
760 multiHead = False;
762 execInitScript();
764 EventLoop();
765 return -1;