all repos — dwm @ 44411d2d48206d6cd87ce5f9c0ddc365e598928b

fork of suckless dynamic window manager

main.c (view raw)

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