syncing wtextfield.c
[wmaker-crm.git] / src / main.c
blob4e5c4b9aaa640fe761deb545477bb8e1f986048a
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 /* stdi/o for log shell */
155 static int LogStdIn = -1, LogStdOut = -1, LogStdErr = -1;
158 void
159 Exit(int status)
161 #ifdef XSMP_ENABLED
162 wSessionDisconnectManager();
163 #endif
164 if (dpy)
165 XCloseDisplay(dpy);
167 exit(status);
171 void
172 Restart(char *manager, Bool abortOnFailure)
174 char *prog=NULL;
175 char *argv[MAX_RESTART_ARGS];
176 int i;
178 if (manager && manager[0]!=0) {
179 prog = argv[0] = strtok(manager, " ");
180 for (i=1; i<MAX_RESTART_ARGS; i++) {
181 argv[i]=strtok(NULL, " ");
182 if (argv[i]==NULL) {
183 break;
187 if (dpy) {
188 #ifdef XSMP_ENABLED
189 wSessionDisconnectManager();
190 #endif
191 XCloseDisplay(dpy);
192 dpy = NULL;
194 if (!prog) {
195 execvp(Arguments[0], Arguments);
196 wfatal(_("failed to restart Window Maker."));
197 } else {
198 execvp(prog, argv);
199 wsyserror(_("could not exec %s"), prog);
201 if (abortOnFailure)
202 exit(-1);
207 void
208 SetupEnvironment(WScreen *scr)
210 char *tmp, *ptr;
211 char buf[16];
213 if (wScreenCount > 1) {
214 tmp = wmalloc(strlen(DisplayName)+64);
215 sprintf(tmp, "DISPLAY=%s", XDisplayName(DisplayName));
216 ptr = strchr(strchr(tmp, ':'), '.');
217 if (ptr)
218 *ptr = 0;
219 sprintf(buf, ".%i", scr->screen);
220 strcat(tmp, buf);
221 putenv(tmp);
223 tmp = wmalloc(60);
224 sprintf(tmp, "WRASTER_COLOR_RESOLUTION%i=%i", scr->screen,
225 scr->rcontext->attribs->colors_per_channel);
226 putenv(tmp);
232 typedef struct {
233 WScreen *scr;
234 char *command;
235 } _tuple;
238 static void
239 shellCommandHandler(pid_t pid, unsigned char status, _tuple *data)
241 if (status == 127) {
242 char *buffer;
244 buffer = wstrappend(_("Could not execute command: "), data->command);
246 wMessageDialog(data->scr, _("Error"), buffer, _("OK"), NULL, NULL);
247 free(buffer);
248 } else if (status != 127) {
250 printf("%s: %i\n", data->command, status);
254 free(data->command);
255 free(data);
259 void
260 ExecuteShellCommand(WScreen *scr, char *command)
262 static char *shell = NULL;
263 pid_t pid;
266 * This have a problem: if the shell is tcsh (not sure about others)
267 * and ~/.tcshrc have /bin/stty erase ^H somewhere on it, the shell
268 * will block and the command will not be executed.
269 if (!shell) {
270 shell = getenv("SHELL");
271 if (!shell)
272 shell = "/bin/sh";
275 shell = "/bin/sh";
277 pid = fork();
279 if (pid==0) {
281 SetupEnvironment(scr);
283 #ifdef HAVE_SETPGID
284 setpgid(0, 0);
285 #endif
286 execl(shell, shell, "-c", command, NULL);
287 wsyserror("could not execute %s -c %s", shell, command);
288 Exit(-1);
289 } else if (pid < 0) {
290 wsyserror("cannot fork a new process");
291 } else {
292 _tuple *data = wmalloc(sizeof(_tuple));
294 data->scr = scr;
295 data->command = wstrdup(command);
297 wAddDeathHandler(pid, (WDeathHandler*)shellCommandHandler, data);
303 *---------------------------------------------------------------------------
304 * StartLogShell
305 * Start a shell that will receive all stdin and stdout from processes
306 * forked by wmaker.
307 *---------------------------------------------------------------------------
309 void
310 StartLogShell(WScreen *scr)
312 int in_fd[2];
313 int out_fd[2];
314 int err_fd[2];
315 pid_t pid;
317 SetupEnvironment(scr);
319 if (pipe(in_fd) < 0) {
320 wsyserror("could not create pipe for log shell\n");
321 return;
323 if (pipe(out_fd) < 0) {
324 wsyserror("could not create pipe for log shell\n");
325 close(in_fd[0]);
326 close(in_fd[1]);
327 return;
329 if (pipe(err_fd) < 0) {
330 wsyserror("could not create pipe for log shell\n");
331 close(out_fd[0]);
332 close(out_fd[1]);
333 close(in_fd[0]);
334 close(in_fd[1]);
335 return;
338 pid = fork();
339 if (pid < 0) {
340 wsyserror("could not fork a new process for log shell\n");
341 return;
342 } else if (pid == 0) {
343 close(in_fd[0]);
344 close(out_fd[1]);
345 close(err_fd[1]);
347 close(0);
348 close(1);
349 close(2);
351 if (dup2(in_fd[1], 0) < 0) {
352 wsyserror("could not redirect stdin for log shell\n");
353 exit(1);
355 if (dup2(out_fd[1], 1) < 0) {
356 wsyserror("could not redirect stdout for log shell\n");
357 exit(1);
359 if (dup2(err_fd[1], 2) < 0) {
360 wsyserror("could not redirect stderr for log shell\n");
361 exit(1);
364 close(in_fd[1]);
365 close(out_fd[1]);
366 close(err_fd[1]);
368 execl("/bin/sh", "/bin/sh", "-c", wPreferences.logger_shell, NULL);
369 wsyserror("could not execute %s\n", wPreferences.logger_shell);
370 exit(1);
371 } else {
372 close(in_fd[1]);
373 close(out_fd[0]);
374 close(err_fd[0]);
376 LogStdIn = in_fd[1];
377 LogStdOut = out_fd[0];
378 LogStdErr = err_fd[0];
384 *---------------------------------------------------------------------
385 * wAbort--
386 * Do a major cleanup and exit the program
388 *----------------------------------------------------------------------
390 void
391 wAbort(Bool dumpCore)
393 int i;
394 WScreen *scr;
396 for (i=0; i<wScreenCount; i++) {
397 scr = wScreenWithNumber(i);
398 if (scr)
399 RestoreDesktop(scr);
401 printf(_("%s aborted.\n"), ProgName);
402 if (dumpCore)
403 abort();
404 else
405 exit(1);
409 void
410 print_help()
412 printf(_("Usage: %s [options]\n"), ProgName);
413 puts(_("The Window Maker window manager for the X window system"));
414 puts("");
415 puts(_(" -display host:dpy display to use"));
416 #ifdef USECPP
417 puts(_(" --no-cpp disable preprocessing of configuration files"));
418 #endif
419 puts(_(" --no-dock do not open the application Dock"));
420 puts(_(" --no-clip do not open the workspace Clip"));
422 puts(_(" --locale locale locale to use"));
424 puts(_(" --visual-id visualid visual id of visual to use"));
425 puts(_(" --static do not update or save configurations"));
426 #ifdef DEBUG
427 puts(_(" --synchronous turn on synchronous display mode"));
428 #endif
429 puts(_(" --version print version and exit"));
430 puts(_(" --help show this message"));
435 void
436 check_defaults()
438 char *path;
440 path = wdefaultspathfordomain("");
441 if (access(path, R_OK)!=0) {
442 wfatal(_("could not find user GNUstep directory (%s).\n"
443 "Make sure you have installed Window Maker correctly and run wmaker.inst"),
444 path);
445 exit(1);
448 free(path);
452 static void
453 execInitScript()
455 char *file;
456 char *paths = wstrappend(wusergnusteppath(), ":"DEF_CONFIG_PATHS);
458 file = wfindfile(paths, DEF_INIT_SCRIPT);
459 free(paths);
461 if (file) {
462 if (system(file) != 0) {
463 wsyserror(_("%s:could not execute initialization script"), file);
465 #if 0
466 if (fork()==0) {
467 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
468 wsyserror(_("%s:could not execute initialization script"), file);
469 exit(1);
471 #endif
472 free(file);
477 void
478 ExecExitScript()
480 char *file;
481 char *paths = wstrappend(wusergnusteppath(), ":"DEF_CONFIG_PATHS);
483 file = wfindfile(paths, DEF_EXIT_SCRIPT);
484 free(paths);
486 if (file) {
487 if (system(file) != 0) {
488 wsyserror(_("%s:could not execute exit script"), file);
490 #if 0
491 if (fork()==0) {
492 execl("/bin/sh", "/bin/sh", "-c", file, NULL);
493 wsyserror(_("%s:could not execute exit script"), file);
494 exit(1);
496 #endif
497 free(file);
501 #if 0
502 char*
503 getFullPath(char *path)
505 char buffer[1024];
506 char *tmp;
507 char *basep = (char*)buffer;
509 if (*path != '/' && getcwd(buffer, 1023)) {
511 for (;;) {
512 if (strncmp(path, "../", 3)==0) {
513 path += 3;
514 basep = strchr(basep, '/');
515 if (!basep || *path==0)
516 break;
519 if (*path == '/' || strncmp(path, "./",2)==0) {
520 tmp =
524 * path
525 * ./path
526 * ../path
527 * ../../path
531 } else {
532 return wstrappend(path);
535 return tmp;
537 #endif
540 main(int argc, char **argv)
542 int i, restart=0;
543 Bool multiHead = True;
544 char *str;
545 int d, s;
546 #ifdef DEBUG
547 Bool doSync = False;
548 #endif
550 wsetabort(wAbort);
552 /* for telling WPrefs what's the name of the wmaker binary being ran */
554 /* char *tmp;
556 tmp = getFullPath(argv[0]);*/
557 str = wstrappend("WMAKER_BIN_NAME=", argv[0]);
558 /* free(tmp);*/
559 putenv(str);
562 ArgCount = argc;
563 Arguments = argv;
565 WMInitializeApplication("WindowMaker", &argc, argv);
568 ProgName = strrchr(argv[0],'/');
569 if (!ProgName)
570 ProgName = argv[0];
571 else
572 ProgName++;
575 restart = 0;
577 memset(&wPreferences, 0, sizeof(WPreferences));
579 if (argc>1) {
580 for (i=1; i<argc; i++) {
581 #ifdef USECPP
582 if (strcmp(argv[i], "-nocpp")==0
583 || strcmp(argv[i], "--no-cpp")==0) {
584 wPreferences.flags.nocpp=1;
585 } else
586 #endif
587 if (strcmp(argv[i], "-nodock")==0
588 || strcmp(argv[i], "--no-dock")==0) {
589 wPreferences.flags.nodock=1;
590 } else if (strcmp(argv[i], "-noclip")==0
591 || strcmp(argv[i], "--no-clip")==0) {
592 wPreferences.flags.noclip=1;
593 } else if (strcmp(argv[i], "-version")==0
594 || strcmp(argv[i], "--version")==0) {
595 printf("Window Maker %s\n", VERSION);
596 exit(0);
597 } else if (strcmp(argv[i], "--global_defaults_path")==0) {
598 printf("%s/WindowMaker\n", SYSCONFDIR);
599 exit(0);
600 #ifdef DEBUG
601 } else if (strcmp(argv[i], "--synchronous")==0) {
602 doSync = 1;
603 #endif
604 } else if (strcmp(argv[i], "-locale")==0
605 || strcmp(argv[i], "--locale")==0) {
606 i++;
607 if (i>=argc) {
608 wwarning(_("too few arguments for %s"), argv[i-1]);
609 exit(0);
611 Locale = argv[i];
612 } else if (strcmp(argv[i], "-display")==0) {
613 i++;
614 if (i>=argc) {
615 wwarning(_("too few arguments for %s"), argv[i-1]);
616 exit(0);
618 DisplayName = argv[i];
619 } else if (strcmp(argv[i], "-visualid")==0
620 || strcmp(argv[i], "--visual-id")==0) {
621 i++;
622 if (i>=argc) {
623 wwarning(_("too few arguments for %s"), argv[i-1]);
624 exit(0);
626 if (sscanf(argv[i], "%i", &wVisualID)!=1) {
627 wwarning(_("bad value for visualid: \"%s\""), argv[i]);
628 exit(0);
630 } else if (strcmp(argv[i], "-static")==0
631 || strcmp(argv[i], "--static")==0) {
633 wPreferences.flags.noupdates = 1;
634 #ifdef XSMP_ENABLED
635 } else if (strcmp(argv[i], "-clientid")==0
636 || strcmp(argv[i], "-restore")==0) {
637 i++;
638 if (i>=argc) {
639 wwarning(_("too few arguments for %s"), argv[i-1]);
640 exit(0);
642 #endif
643 } else if (strcmp(argv[i], "--help")==0) {
644 print_help();
645 exit(0);
646 } else {
647 printf(_("%s: invalid argument '%s'\n"), argv[0], argv[i]);
648 printf(_("Try '%s --help' for more information\n"), argv[0]);
649 exit(1);
654 if (!wPreferences.flags.noupdates) {
655 /* check existence of Defaults DB directory */
656 check_defaults();
659 #if 0
660 tmp = getenv("LANG");
661 if (tmp) {
662 if (setlocale(LC_ALL,"") == NULL) {
663 wwarning("cannot set locale %s", tmp);
664 wwarning("falling back to C locale");
665 setlocale(LC_ALL,"C");
666 Locale = NULL;
667 } else {
668 if (strcmp(tmp, "C")==0 || strcmp(tmp, "POSIX")==0)
669 Locale = NULL;
670 else
671 Locale = tmp;
673 } else {
674 Locale = NULL;
676 #endif
677 if (!Locale) {
678 Locale = getenv("LANG");
680 setlocale(LC_ALL, Locale);
681 if (!Locale || strcmp(Locale, "C")==0 || strcmp(Locale, "POSIX")==0)
682 Locale = NULL;
683 #ifdef I18N
684 if (getenv("NLSPATH"))
685 bindtextdomain("WindowMaker", getenv("NLSPATH"));
686 else
687 bindtextdomain("WindowMaker", LOCALEDIR);
688 textdomain("WindowMaker");
690 if (!XSupportsLocale()) {
691 wwarning(_("X server does not support locale"));
693 if (XSetLocaleModifiers("") == NULL) {
694 wwarning(_("cannot set locale modifiers"));
696 #endif
698 if (Locale) {
699 char *ptr;
701 Locale = wstrdup(Locale);
702 ptr = strchr(Locale, '.');
703 if (ptr)
704 *ptr = 0;
708 /* open display */
709 dpy = XOpenDisplay(DisplayName);
710 if (dpy == NULL) {
711 wfatal(_("could not open display \"%s\""), XDisplayName(DisplayName));
712 exit(1);
715 if (fcntl(ConnectionNumber(dpy), F_SETFD, FD_CLOEXEC) < 0) {
716 wsyserror("error setting close-on-exec flag for X connection");
717 exit(1);
720 /* check if the user specified a complete display name (with screen).
721 * If so, only manage the specified screen */
722 if (DisplayName)
723 str = strchr(DisplayName, ':');
724 else
725 str = NULL;
727 if (str && sscanf(str, "%i.%i", &d, &s)==2)
728 multiHead = False;
730 DisplayName = XDisplayName(DisplayName);
731 str = wmalloc(strlen(DisplayName)+64);
732 sprintf(str, "DISPLAY=%s", DisplayName);
733 putenv(str);
735 #ifdef DEBUG
736 if (doSync)
737 XSynchronize(dpy, True);
738 #endif
740 wXModifierInitialize();
742 #ifdef XSMP_ENABLED
743 wSessionConnectManager(argv, argc);
744 #endif
746 StartUp(!multiHead);
748 execInitScript();
750 EventLoop();
751 return -1;