all repos — dwm @ b5c2412d848011ebf0f29c73d4ca01f52006990c

fork of suckless dynamic window manager

main.c (view raw)

  1/*
  2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
  3 * See LICENSE file for license details.
  4 */
  5
  6#include "dwm.h"
  7#include <errno.h>
  8#include <stdio.h>
  9#include <stdlib.h>
 10#include <string.h>
 11#include <unistd.h>
 12#include <sys/select.h>
 13#include <X11/cursorfont.h>
 14#include <X11/keysym.h>
 15#include <X11/Xatom.h>
 16#include <X11/Xproto.h>
 17
 18/* static */
 19
 20static int (*xerrorxlib)(Display *, XErrorEvent *);
 21static Bool otherwm, readin;
 22
 23static void
 24cleanup()
 25{
 26	close(STDIN_FILENO);
 27	while(sel) {
 28		resize(sel, True, TopLeft);
 29		unmanage(sel);
 30	}
 31	if(dc.font.set)
 32		XFreeFontSet(dpy, dc.font.set);
 33	else
 34		XFreeFont(dpy, dc.font.xfont);
 35	XUngrabKey(dpy, AnyKey, AnyModifier, root);
 36	XDestroyWindow(dpy, barwin);
 37	XFreePixmap(dpy, dc.drawable);
 38	XFreeGC(dpy, dc.gc);
 39	XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
 40	XSync(dpy, False);
 41}
 42
 43static void
 44scan()
 45{
 46	unsigned int i, num;
 47	Window *wins, d1, d2;
 48	XWindowAttributes wa;
 49
 50	wins = NULL;
 51	if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
 52		for(i = 0; i < num; i++) {
 53			if(!XGetWindowAttributes(dpy, wins[i], &wa))
 54				continue;
 55			if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
 56				continue;
 57			if(wa.map_state == IsViewable)
 58				manage(wins[i], &wa);
 59		}
 60	}
 61	if(wins)
 62		XFree(wins);
 63}
 64
 65static int
 66win_property(Window w, Atom a, Atom t, long l, unsigned char **prop)
 67{
 68	int status, format;
 69	unsigned long res, extra;
 70	Atom real;
 71
 72	status = XGetWindowProperty(dpy, w, a, 0L, l, False, t, &real, &format,
 73			&res, &extra, prop);
 74
 75	if(status != Success || *prop == 0) {
 76		return 0;
 77	}
 78	if(res == 0) {
 79		free((void *) *prop);
 80	}
 81	return res;
 82}
 83
 84/*
 85 * Startup Error handler to check if another window manager
 86 * is already running.
 87 */
 88static int
 89xerrorstart(Display *dsply, XErrorEvent *ee)
 90{
 91	otherwm = True;
 92	return -1;
 93}
 94
 95/* extern */
 96
 97char stext[1024];
 98Bool *seltag;
 99int screen, sx, sy, sw, sh, bx, by, bw, bh, mw;
100unsigned int ntags, numlockmask;
101Atom wmatom[WMLast], netatom[NetLast];
102Bool running = True;
103Bool issel = True;
104Client *clients = NULL;
105Client *sel = NULL;
106Cursor cursor[CurLast];
107Display *dpy;
108DC dc = {0};
109Window root, barwin;
110
111int
112getproto(Window w)
113{
114	int protos = 0;
115	int i;
116	long res;
117	Atom *protocols;
118
119	res = win_property(w, wmatom[WMProtocols], XA_ATOM, 20L,
120			((unsigned char **)&protocols));
121	if(res <= 0) {
122		return protos;
123	}
124	for(i = 0; i < res; i++) {
125		if(protocols[i] == wmatom[WMDelete])
126			protos |= PROTODELWIN;
127	}
128	free((char *) protocols);
129	return protos;
130}
131
132void
133sendevent(Window w, Atom a, long value)
134{
135	XEvent e;
136
137	e.type = ClientMessage;
138	e.xclient.window = w;
139	e.xclient.message_type = a;
140	e.xclient.format = 32;
141	e.xclient.data.l[0] = value;
142	e.xclient.data.l[1] = CurrentTime;
143	XSendEvent(dpy, w, False, NoEventMask, &e);
144	XSync(dpy, False);
145}
146
147void
148quit(Arg *arg)
149{
150	readin = running = False;
151}
152
153/*
154 * There's no way to check accesses to destroyed windows, thus those cases are
155 * ignored (especially on UnmapNotify's).  Other types of errors call Xlibs
156 * default error handler, which calls exit().
157 */
158int
159xerror(Display *dpy, XErrorEvent *ee)
160{
161	if(ee->error_code == BadWindow
162	|| (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
163	|| (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
164	|| (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
165	|| (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
166	|| (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
167	|| (ee->request_code == X_GrabKey && ee->error_code == BadAccess))
168		return 0;
169	fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
170		ee->request_code, ee->error_code);
171	return xerrorxlib(dpy, ee); /* may call exit() */
172}
173
174int
175main(int argc, char *argv[])
176{
177	int i, j, xfd;
178	unsigned int mask;
179	fd_set rd;
180	Window w;
181	XModifierKeymap *modmap;
182	XSetWindowAttributes wa;
183
184	if(argc == 2 && !strncmp("-v", argv[1], 3)) {
185		fputs("dwm-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout);
186		exit(EXIT_SUCCESS);
187	}
188	else if(argc != 1)
189		eprint("usage: dwm [-v]\n");
190
191	dpy = XOpenDisplay(0);
192	if(!dpy)
193		eprint("dwm: cannot open display\n");
194
195	xfd = ConnectionNumber(dpy);
196	screen = DefaultScreen(dpy);
197	root = RootWindow(dpy, screen);
198
199	otherwm = False;
200	XSetErrorHandler(xerrorstart);
201	/* this causes an error if some other window manager is running */
202	XSelectInput(dpy, root, SubstructureRedirectMask);
203	XSync(dpy, False);
204
205	if(otherwm)
206		eprint("dwm: another window manager is already running\n");
207
208	XSync(dpy, False);
209	XSetErrorHandler(NULL);
210	xerrorxlib = XSetErrorHandler(xerror);
211	XSync(dpy, False);
212
213	/* init atoms */
214	wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
215	wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
216	netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
217	netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
218	XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
219			PropModeReplace, (unsigned char *) netatom, NetLast);
220
221	/* init cursors */
222	cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
223	cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
224	cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
225
226	modmap = XGetModifierMapping(dpy);
227	for (i = 0; i < 8; i++) {
228		for (j = 0; j < modmap->max_keypermod; j++) {
229			if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock))
230				numlockmask = (1 << i);
231		}
232	}
233	XFree(modmap);
234
235	wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask | EnterWindowMask | LeaveWindowMask;
236	wa.cursor = cursor[CurNormal];
237	XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
238
239	grabkeys();
240	initrregs();
241
242	for(ntags = 0; tags[ntags]; ntags++);
243	seltag = emallocz(sizeof(Bool) * ntags);
244	seltag[DEFTAG] = True;
245
246	/* style */
247	dc.bg = getcolor(BGCOLOR);
248	dc.fg = getcolor(FGCOLOR);
249	dc.border = getcolor(BORDERCOLOR);
250	setfont(FONT);
251
252	sx = sy = 0;
253	sw = DisplayWidth(dpy, screen);
254	sh = DisplayHeight(dpy, screen);
255	mw = (sw * MASTERW) / 100;
256
257	bx = by = 0;
258	bw = sw;
259	dc.h = bh = dc.font.height + 4;
260	wa.override_redirect = 1;
261	wa.background_pixmap = ParentRelative;
262	wa.event_mask = ButtonPressMask | ExposureMask;
263	barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen),
264			CopyFromParent, DefaultVisual(dpy, screen),
265			CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
266	XDefineCursor(dpy, barwin, cursor[CurNormal]);
267	XMapRaised(dpy, barwin);
268
269	dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
270	dc.gc = XCreateGC(dpy, root, 0, 0);
271
272	strcpy(stext, "dwm-"VERSION);
273	drawstatus();
274
275	issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
276
277	scan();
278
279	/* main event loop, also reads status text from stdin */
280	XSync(dpy, False);
281	procevent();
282	readin = True;
283	while(running) {
284		FD_ZERO(&rd);
285		if(readin)
286			FD_SET(STDIN_FILENO, &rd);
287		FD_SET(xfd, &rd);
288		i = select(xfd + 1, &rd, NULL, NULL, NULL);
289		if(i == -1 && errno == EINTR)
290			continue;
291		if(i < 0)
292			eprint("select failed\n");
293		else if(i > 0) {
294			if(readin && FD_ISSET(STDIN_FILENO, &rd)) {
295				readin = NULL != fgets(stext, sizeof(stext), stdin);
296				if(readin)
297					stext[strlen(stext) - 1] = 0;
298				else 
299					strcpy(stext, "broken pipe");
300				drawstatus();
301			}
302		}
303		procevent();
304	}
305	cleanup();
306	XCloseDisplay(dpy);
307
308	return 0;
309}