separated setup stuff into main.c:setup() - this makes main() more readable b5159dfd
Anselm R. Garbe · 2006-08-23 10:21 3 file(s) · +104 −99
dwm.h +8 −12
11 11
#define MOUSEMASK		(BUTTONMASK | PointerMotionMask)
12 12
#define PROTODELWIN		1
13 13
14 -
typedef union Arg Arg;
15 -
typedef struct Client Client;
16 -
typedef struct DC DC;
17 -
typedef struct Fnt Fnt;
18 -
19 -
union Arg {
14 +
typedef union {
20 15
	const char *cmd;
21 16
	int i;
22 -
};
17 +
} Arg;
23 18
24 19
/* atoms */
25 20
enum { NetSupported, NetWMName, NetLast };
28 23
/* cursor */
29 24
enum { CurNormal, CurResize, CurMove, CurLast };
30 25
31 -
/* windowcorners */
26 +
/* window corners */
32 27
typedef enum { TopLeft, TopRight, BotLeft, BotRight } Corner;
33 28
34 -
struct Fnt {
29 +
typedef struct {
35 30
	int ascent;
36 31
	int descent;
37 32
	int height;
38 33
	XFontSet set;
39 34
	XFontStruct *xfont;
40 -
};
35 +
} Fnt;
41 36
42 -
struct DC { /* draw context */
37 +
typedef struct { /* draw context */
43 38
	int x, y, w, h;
44 39
	unsigned long bg;
45 40
	unsigned long fg;
47 42
	Drawable drawable;
48 43
	Fnt font;
49 44
	GC gc;
50 -
};
45 +
} DC;
51 46
47 +
typedef struct Client Client;
52 48
struct Client {
53 49
	char name[256];
54 50
	int proto;
main.c +95 −86
15 15
#include <X11/Xatom.h>
16 16
#include <X11/Xproto.h>
17 17
18 +
/* extern */
19 +
20 +
char stext[1024];
21 +
Bool *seltag;
22 +
int screen, sx, sy, sw, sh, bx, by, bw, bh, mw;
23 +
unsigned int ntags, numlockmask;
24 +
Atom wmatom[WMLast], netatom[NetLast];
25 +
Bool running = True;
26 +
Bool issel = True;
27 +
Client *clients = NULL;
28 +
Client *sel = NULL;
29 +
Cursor cursor[CurLast];
30 +
Display *dpy;
31 +
DC dc = {0};
32 +
Window root, barwin;
33 +
18 34
/* static */
19 35
20 36
static int (*xerrorxlib)(Display *, XErrorEvent *);
62 78
		XFree(wins);
63 79
}
64 80
81 +
static void
82 +
setup()
83 +
{
84 +
	int i, j;
85 +
	unsigned int mask;
86 +
	Window w;
87 +
	XModifierKeymap *modmap;
88 +
	XSetWindowAttributes wa;
89 +
90 +
	/* init atoms */
91 +
	wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
92 +
	wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
93 +
	netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
94 +
	netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
95 +
	XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
96 +
			PropModeReplace, (unsigned char *) netatom, NetLast);
97 +
98 +
	/* init cursors */
99 +
	cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
100 +
	cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
101 +
	cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
102 +
103 +
	modmap = XGetModifierMapping(dpy);
104 +
	for (i = 0; i < 8; i++) {
105 +
		for (j = 0; j < modmap->max_keypermod; j++) {
106 +
			if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock))
107 +
				numlockmask = (1 << i);
108 +
		}
109 +
	}
110 +
	XFree(modmap);
111 +
112 +
	wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask | EnterWindowMask | LeaveWindowMask;
113 +
	wa.cursor = cursor[CurNormal];
114 +
	XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
115 +
116 +
	grabkeys();
117 +
	initrregs();
118 +
119 +
	for(ntags = 0; tags[ntags]; ntags++);
120 +
	seltag = emallocz(sizeof(Bool) * ntags);
121 +
	seltag[0] = True;
122 +
123 +
	/* style */
124 +
	dc.bg = getcolor(BGCOLOR);
125 +
	dc.fg = getcolor(FGCOLOR);
126 +
	dc.border = getcolor(BORDERCOLOR);
127 +
	setfont(FONT);
128 +
129 +
	sx = sy = 0;
130 +
	sw = DisplayWidth(dpy, screen);
131 +
	sh = DisplayHeight(dpy, screen);
132 +
	mw = (sw * MASTERW) / 100;
133 +
134 +
	bx = by = 0;
135 +
	bw = sw;
136 +
	dc.h = bh = dc.font.height + 4;
137 +
	wa.override_redirect = 1;
138 +
	wa.background_pixmap = ParentRelative;
139 +
	wa.event_mask = ButtonPressMask | ExposureMask;
140 +
	barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen),
141 +
			CopyFromParent, DefaultVisual(dpy, screen),
142 +
			CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
143 +
	XDefineCursor(dpy, barwin, cursor[CurNormal]);
144 +
	XMapRaised(dpy, barwin);
145 +
146 +
	dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
147 +
	dc.gc = XCreateGC(dpy, root, 0, 0);
148 +
149 +
	issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
150 +
151 +
	strcpy(stext, "dwm-"VERSION);
152 +
}
153 +
65 154
/*
66 155
 * Startup Error handler to check if another window manager
67 156
 * is already running.
75 164
76 165
/* extern */
77 166
78 -
char stext[1024];
79 -
Bool *seltag;
80 -
int screen, sx, sy, sw, sh, bx, by, bw, bh, mw;
81 -
unsigned int ntags, numlockmask;
82 -
Atom wmatom[WMLast], netatom[NetLast];
83 -
Bool running = True;
84 -
Bool issel = True;
85 -
Client *clients = NULL;
86 -
Client *sel = NULL;
87 -
Cursor cursor[CurLast];
88 -
Display *dpy;
89 -
DC dc = {0};
90 -
Window root, barwin;
91 -
92 167
int
93 168
getproto(Window w)
94 169
{
153 228
int
154 229
main(int argc, char *argv[])
155 230
{
156 -
	int i, j, xfd;
157 -
	unsigned int mask;
231 +
	int r, xfd;
158 232
	fd_set rd;
159 -
	Window w;
160 -
	XModifierKeymap *modmap;
161 -
	XSetWindowAttributes wa;
162 233
163 234
	if(argc == 2 && !strncmp("-v", argv[1], 3)) {
164 235
		fputs("dwm-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout);
189 260
	xerrorxlib = XSetErrorHandler(xerror);
190 261
	XSync(dpy, False);
191 262
192 -
	/* init atoms */
193 -
	wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
194 -
	wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
195 -
	netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
196 -
	netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
197 -
	XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
198 -
			PropModeReplace, (unsigned char *) netatom, NetLast);
199 -
200 -
	/* init cursors */
201 -
	cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
202 -
	cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
203 -
	cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
204 -
205 -
	modmap = XGetModifierMapping(dpy);
206 -
	for (i = 0; i < 8; i++) {
207 -
		for (j = 0; j < modmap->max_keypermod; j++) {
208 -
			if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock))
209 -
				numlockmask = (1 << i);
210 -
		}
211 -
	}
212 -
	XFree(modmap);
213 -
214 -
	wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask | EnterWindowMask | LeaveWindowMask;
215 -
	wa.cursor = cursor[CurNormal];
216 -
	XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
217 -
218 -
	grabkeys();
219 -
	initrregs();
220 -
221 -
	for(ntags = 0; tags[ntags]; ntags++);
222 -
	seltag = emallocz(sizeof(Bool) * ntags);
223 -
	seltag[0] = True;
224 -
225 -
	/* style */
226 -
	dc.bg = getcolor(BGCOLOR);
227 -
	dc.fg = getcolor(FGCOLOR);
228 -
	dc.border = getcolor(BORDERCOLOR);
229 -
	setfont(FONT);
230 -
231 -
	sx = sy = 0;
232 -
	sw = DisplayWidth(dpy, screen);
233 -
	sh = DisplayHeight(dpy, screen);
234 -
	mw = (sw * MASTERW) / 100;
235 -
236 -
	bx = by = 0;
237 -
	bw = sw;
238 -
	dc.h = bh = dc.font.height + 4;
239 -
	wa.override_redirect = 1;
240 -
	wa.background_pixmap = ParentRelative;
241 -
	wa.event_mask = ButtonPressMask | ExposureMask;
242 -
	barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen),
243 -
			CopyFromParent, DefaultVisual(dpy, screen),
244 -
			CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
245 -
	XDefineCursor(dpy, barwin, cursor[CurNormal]);
246 -
	XMapRaised(dpy, barwin);
247 -
248 -
	dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
249 -
	dc.gc = XCreateGC(dpy, root, 0, 0);
250 -
251 -
	strcpy(stext, "dwm-"VERSION);
263 +
	setup();
252 264
	drawstatus();
253 -
254 -
	issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
255 -
256 265
	scan();
257 266
258 267
	/* main event loop, also reads status text from stdin */
264 273
		if(readin)
265 274
			FD_SET(STDIN_FILENO, &rd);
266 275
		FD_SET(xfd, &rd);
267 -
		i = select(xfd + 1, &rd, NULL, NULL, NULL);
268 -
		if((i == -1) && (errno == EINTR))
276 +
		r = select(xfd + 1, &rd, NULL, NULL, NULL);
277 +
		if((r == -1) && (errno == EINTR))
269 278
			continue;
270 -
		if(i > 0) {
279 +
		if(r > 0) {
271 280
			if(readin && FD_ISSET(STDIN_FILENO, &rd)) {
272 281
				readin = NULL != fgets(stext, sizeof(stext), stdin);
273 282
				if(readin)
277 286
				drawstatus();
278 287
			}
279 288
		}
280 -
		else if(i < 0)
289 +
		else if(r < 0)
281 290
			eprint("select failed\n");
282 291
		procevent();
283 292
	}
view.c +1 −1
195 195
void
196 196
togglemode(Arg *arg)
197 197
{
198 -
	arrange = arrange == dofloat ? dotile : dofloat;
198 +
	arrange = (arrange == dofloat) ? dotile : dofloat;
199 199
	if(sel)
200 200
		arrange(NULL);
201 201
	else