File: init.c

package info (click to toggle)
aewm 1.2.0-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 264 kB
  • ctags: 259
  • sloc: ansic: 2,017; makefile: 139; sh: 22
file content (239 lines) | stat: -rw-r--r-- 6,957 bytes parent folder | download
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/* aewm - a minimalist X11 window mananager. vim:noet:sw=4:ts=4
 * Copyright 1998-2001 Decklin Foster <decklin@red-bean.com>
 * This program is free software; see LICENSE for details. */

#include <string.h>
#include <signal.h>
#include <X11/cursorfont.h>
#include "aewm.h"
#include "parser.h"

Display *dpy;
Window root;
int screen;
XFontStruct *font;
#ifdef XFT
XftFont *xftfont;
XftColor xft_fg;
#endif
GC invert_gc, string_gc, border_gc;
XColor fg, bg, bd;
Cursor move_curs, resize_curs;
Atom wm_protos, wm_delete, wm_state, wm_change_state;
#ifdef GNOME_PDA
Atom gnome_pda;
#endif
#ifdef MWM_HINTS
Atom mwm_hints;
#endif
Client *head_client;
static char *opt_font = DEF_FONT;
static char *opt_fg = DEF_FG;
static char *opt_bg = DEF_BG;
static char *opt_bd = DEF_BD;
char *opt_new1 = DEF_NEW1;
char *opt_new2 = DEF_NEW2;
char *opt_new3 = DEF_NEW3;
int opt_bw = DEF_BW;
int opt_pad = DEF_PAD;
#ifdef SHAPE
Bool shape;
int shape_event;
#endif

static void scan_wins(void);
static void setup_display(void);
static void read_config_helper(FILE *);
static void read_config(char *);

int main(int argc, char **argv)
{
	int i;
	struct sigaction act;

	read_config(NULL);

#define USAGE \
	"usage: aewm [--config|-rc <file>]\n" \
	"            [--font|-fn <font>]\n" \
	"            [--fgcolor|-fg <color>]\n" \
	"            [--bgcolor|-bg <color>]\n" \
	"            [--bdcolor|-bd <color>]\n" \
	"            [--bdwidth|-bw <integer>]\n" \
	"            [--padding|-p <integer>]\n" \
	"            [--new1|-1 <command>]\n" \
	"            [--new2|-2 <command>]\n" \
	"            [--new3|-3 <command>]\n" \
	"            [--help|-h]\n" \
	"            [--version|-v]\n"

	for (i = 1; i < argc; i++) {
		if ARG("config", "rc", 1)  { read_config(argv[++i]); continue; }
		if ARG("font", "fn", 1)    { opt_font = argv[++i]; continue; }
		if ARG("fgcolor", "fg", 1) { opt_fg   = argv[++i]; continue; }
		if ARG("bgcolor", "bg", 1) { opt_bg   = argv[++i]; continue; }
		if ARG("bdcolor", "bd", 1) { opt_bd   = argv[++i]; continue; }
		if ARG("bdwidth", "bw", 1) { opt_bw   = atoi(argv[++i]); continue; }
		if ARG("padding", "p", 1)  { opt_pad  = atoi(argv[++i]); continue; }
		if ARG("new1", "1", 1)     { opt_new1 = argv[++i]; continue; }
		if ARG("new2", "2", 1)     { opt_new2 = argv[++i]; continue; }
		if ARG("new3", "3", 1)     { opt_new3 = argv[++i]; continue; }
		if ARG("version", "v",0)   { printf("aewm: version " VERSION "\n"); exit(0); }
		if ARG("help", "h",0)      { printf(USAGE); exit(0); }
		/* if we get here, it must be a bad option */
		err("unknown option: '%s'\n" USAGE, argv[i]);
		exit(2);
	}

	act.sa_handler = sig_handler;
	act.sa_flags = 0;
	sigaction(SIGTERM, &act, NULL);
	sigaction(SIGINT, &act, NULL);
	sigaction(SIGHUP, &act, NULL);
	sigaction(SIGCHLD, &act, NULL);

	setup_display();
	scan_wins();
	do_event_loop();
	return 1; /* just another brick in the -Wall */
}

static void scan_wins(void)
{
	unsigned int nwins, i;
	Window dummyw1, dummyw2, *wins;
	XWindowAttributes attr;

	XQueryTree(dpy, root, &dummyw1, &dummyw2, &wins, &nwins);
	for (i = 0; i < nwins; i++) {
		XGetWindowAttributes(dpy, wins[i], &attr);
		if (!attr.override_redirect && attr.map_state == IsViewable)
			make_new_client(wins[i]);
	}
	XFree(wins);
}

static void setup_display(void)
{
	XColor dummyc;
	XGCValues gv;
	XSetWindowAttributes sattr;
#ifdef SHAPE
	int dummy;
#endif

	dpy = XOpenDisplay(NULL);

	if (!dpy) {
		err("can't open display '%s' (is $DISPLAY set properly?)",
			getenv("DISPLAY"));
		exit(1);
	}

	XSetErrorHandler(handle_xerror);
	screen = DefaultScreen(dpy);
	root = RootWindow(dpy, screen);

	wm_protos = XInternAtom(dpy, "WM_PROTOCOLS", False);
	wm_delete = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
	wm_state = XInternAtom(dpy, "WM_STATE", False);
	wm_change_state = XInternAtom(dpy, "WM_CHANGE_STATE", False);
#ifdef GNOME_PDA
	gnome_pda = XInternAtom(dpy, "GNOME_PANEL_DESKTOP_AREA", False);
#endif
#ifdef MWM_HINTS
	mwm_hints = XInternAtom(dpy, _XA_MWM_HINTS, False);
#endif

	XAllocNamedColor(dpy, DefaultColormap(dpy, screen), opt_fg, &fg, &dummyc);
	XAllocNamedColor(dpy, DefaultColormap(dpy, screen), opt_bg, &bg, &dummyc);
	XAllocNamedColor(dpy, DefaultColormap(dpy, screen), opt_bd, &bd, &dummyc);

	font = XLoadQueryFont(dpy, opt_font);
	if (!font) { err("font '%s' not found", opt_font); exit(1); }

#ifdef XFT
	xft_fg.color.red = fg.red;
	xft_fg.color.green = fg.green;
	xft_fg.color.blue = fg.blue;
	xft_fg.color.alpha = 0xffff;
	xft_fg.pixel = fg.pixel;

	xftfont = XftFontOpenXlfd(dpy, DefaultScreen(dpy), opt_font);
	if (!xftfont) { err("font '%s' not found", opt_font); exit(1); }
#endif

#ifdef SHAPE
	shape = XShapeQueryExtension(dpy, &shape_event, &dummy);
#endif

	move_curs = XCreateFontCursor(dpy, XC_fleur);
	resize_curs = XCreateFontCursor(dpy, XC_plus);

	gv.function = GXcopy;
	gv.foreground = fg.pixel;
	gv.font = font->fid;
	string_gc = XCreateGC(dpy, root, GCFunction|GCForeground|GCFont, &gv);

	gv.foreground = bd.pixel;
	gv.line_width = opt_bw;
	border_gc = XCreateGC(dpy, root, GCFunction|GCForeground|GCLineWidth, &gv);

	gv.function = GXinvert;
	gv.subwindow_mode = IncludeInferiors;
	invert_gc = XCreateGC(dpy, root, GCFunction|GCSubwindowMode|GCLineWidth|GCFont, &gv);

	sattr.event_mask = ChildMask|ColormapChangeMask|ButtonMask;
	XChangeWindowAttributes(dpy, root, CWEventMask, &sattr);
}

static void read_config_helper(FILE *rc)
{
	char buf[BUF_SIZE], token[BUF_SIZE], *p;

	while (fgets(buf, sizeof buf, rc)) {
		/* comments and blank lines are skipped */
		if (buf[0] == '#' || buf[0] == '\n') continue;

		p = buf;
		while (get_token(&p, token)) {
			if (strcmp(token, "font") == 0) {
				if (get_token(&p, token)) opt_font = strdup(token);
			} else if (strcmp(token, "fgcolor") == 0) {
				if (get_token(&p, token)) opt_fg = strdup(token);
			} else if (strcmp(token, "bgcolor") == 0) {
				if (get_token(&p, token)) opt_bg = strdup(token);
			} else if (strcmp(token, "bdcolor") == 0) {
				if (get_token(&p, token)) opt_bd = strdup(token);
			} else if (strcmp(token, "bdwidth") == 0) {
				if (get_token(&p, token)) opt_bw = atoi(strdup(token));
			} else if (strcmp(token, "padding") == 0) {
				if (get_token(&p, token)) opt_pad = atoi(strdup(token));
			} else if (strcmp(token, "button1") == 0) {
				if (get_token(&p, token)) opt_new1 = strdup(token);
			} else if (strcmp(token, "button2") == 0) {
				if (get_token(&p, token)) opt_new2 = strdup(token);
			} else if (strcmp(token, "button3") == 0) {
				if (get_token(&p, token)) opt_new3 = strdup(token);
			}
		}
	}
}

static void read_config(char *rcfile)
{
	char defrc_buf[BUF_SIZE];
	FILE *rc;

	if (rcfile && (rc = fopen(rcfile, "r"))) goto opened;
	snprintf(defrc_buf, sizeof defrc_buf, "%s/.aewm/aewmrc", getenv("HOME"));
	if ((rc = fopen(defrc_buf, "r"))) goto opened;
	if ((rc = fopen(DEF_RC, "r"))) goto opened;

	err("can't find any rc files");
	exit(1);

opened:
	read_config_helper(rc);
	fclose(rc);
}