File: nawm.c

package info (click to toggle)
nawm 0.0.20030130-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 360 kB
  • ctags: 496
  • sloc: ansic: 3,158; sh: 1,582; yacc: 408; lex: 406; makefile: 153
file content (212 lines) | stat: -rw-r--r-- 4,515 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/* nawm.c: startup and general-purpose routines */

/* Copyright (C) 1999 by the Massachusetts Institute of Technology.
 *
 * Permission to use, copy, modify, and distribute this
 * software and its documentation for any purpose and without
 * fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright
 * notice and this permission notice appear in supporting
 * documentation, and that the name of M.I.T. not be used in
 * advertising or publicity pertaining to distribution of the
 * software without specific, written prior permission.
 * M.I.T. makes no representations about the suitability of
 * this software for any purpose.  It is provided "as is"
 * without express or implied warranty.
 */

#include <sys/types.h>
#include <sys/wait.h>
#include <sys/param.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <signal.h>
#include "nawm.h"
#include "lang.h"
#include "builtins.h"
#include "cache.h"

void usage(void);
int x_error(Display *, XErrorEvent *);
void turn_undead(int signum);
void nawm_set_window_manager(char *);

Display *dpy;
Window root;
int screen, screenwidth, screenheight, error_mode = SPEW_ON_ERROR;
int window_manager;
extern node *cmds;
extern int yydebug, cachedebug;
extern variable CURRENTWINDOW, ROOT, SCREENHEIGHT, SCREENWIDTH;
char **Argv;
long options;

int main(int argc, char **argv)
{
  extern char *optarg;
  char *home;
  int opt, readsomething = 0;
  struct sigaction sa;
  nawmval pw;

  if (!(dpy = XOpenDisplay(NULL)))
    {
      fprintf(stderr, "nawm: can't open display\n");
      exit(1);
    }

  Argv = argv;
  screen = DefaultScreen(dpy);
  assign_var(&ROOT, (nawmval)(root = RootWindow(dpy, screen)));
  assign_var(&SCREENWIDTH,
	     (nawmval)(screenwidth = DisplayWidth(dpy, screen)));
  assign_var(&SCREENHEIGHT,
	     (nawmval)(screenheight = DisplayHeight(dpy, screen)));
  XSetErrorHandler(x_error);

  sigemptyset(&sa.sa_mask);
  sa.sa_flags = 0;
  sa.sa_handler = turn_undead;
  sigaction(SIGCHLD, &sa, NULL);

  initgc();
  inittypes();
  initbindings();
  initcache();
  initbuiltins();
  initkeymap();
  initmodules();
  initlex();
  initrunscopes();

  /* Can't run pointerwindow_fun until after initcache() */
  pointerwindow_fun(0, NULL, &pw);
  assign_var(&CURRENTWINDOW, pw);

  if (getenv("WINDOW_MANAGER"))
    {
      char *wm = getenv("WINDOW_MANAGER");
      if (strchr(wm, '/'))
	wm = strrchr(wm, '/') + 1;
      
      nawm_set_window_manager(wm);
    }

  while ((opt = getopt(argc, argv, "d:e:f:i:o:w:")) != -1)
    {
      switch(opt)
	{
	case 'd':
	  if (strchr(optarg, 'c'))
	    {
	      sa.sa_handler = dumpcache;
	      sigaction(SIGINT, &sa, NULL);
	      cachedebug = 1;
	    }
	  if (strchr(optarg, 'y'))
	    yydebug = 1;
	  break;

	case 'e':
	  parse_string(optarg);
	  eval_cmds(cmds);
	  readsomething = 1;
	  break;

	case 'f':
	  parse_file(optarg);
	  readsomething = 1;
	  break;

	case 'i':
	  load_module(optarg);
	  break;

	case 'o':
	  do_option(optarg);
	  break;

	case 'w':
	  nawm_set_window_manager(optarg);
	  break;

	default:
	  usage();
	}
    }

  if (argc != optind)
    usage();

  if (!readsomething && (home = getenv("HOME")))
    {
      char file[MAXPATHLEN];
      sprintf(file, "%s/.nawmrc", home);
      parse_file(file);
    }

  run();
  XCloseDisplay(dpy);

  return 0;
}

void nawm_set_window_manager(char *wm)
{
  if (!strcmp(wm, "nawm"))
    window_manager = NAWM_WM_NAWM;
  else if (!strcmp(wm, "vtwm.gamma"))
    window_manager = NAWM_WM_VTWM_GAMMA;
  else
    window_manager = NAWM_WM_UNKNOWN;
}

void do_option(char *opt)
{
  if (!strcmp(opt, "nocapslock"))
    options |= NAWM_OPT_NOCAPSLOCK;
  else
    die("Unrecognized option \"%s\"", opt);
}

void usage(void)
{
  die("Usage: nawm [-i module] [-o option] [-w windowmanager]\n"
      "            [-f configfile] [-e commands] ...");
}

int x_error(Display *dpy, XErrorEvent *xee)
{
  if (xee->error_code == BadWindow)
    return 0;

  if (error_mode == BEEP_ON_ERROR)
    XBell(dpy, 100);
  else if(error_mode == SPEW_ON_ERROR)
    {
      char tmp[80];
      XGetErrorText(dpy, xee->error_code, tmp, 80);
      fprintf(stderr, "nawm X error: %s\n", tmp);
    }
  return 0;
}

void die(char *format, ...)
{
  va_list ap;

  va_start(ap, format);
  vfprintf(stderr, format, ap);
  va_end(ap);
  fprintf(stderr, "\n");
  exit(1);
}

void turn_undead(int signum)
{
  while (waitpid(-1, NULL, WNOHANG) > 0)
    ;
}