all repos — dwm @ 2e68f22118438fed885c8cc3ccb8be94437eb327

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