all repos — dwm @ ee31e38dc75832a66cb0fc01bcf2f419ac96a20b

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