all repos — dwm @ 292ccc4c43d9529cb6db0973fcab6e24c73607a5

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