all repos — dwm @ 7ab8c8728168234f6b9e99d1be384323d1246b10

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