all repos — dwm @ 2e836ecce1ba0629a6088b739c1092d27fccd72b

fork of suckless dynamic window manager

wm.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 <errno.h>
  7
  8#include <stdarg.h>
  9#include <stdio.h>
 10#include <stdlib.h>
 11
 12#include <sys/types.h>
 13#include <sys/time.h>
 14
 15#include <X11/cursorfont.h>
 16#include <X11/Xatom.h>
 17#include <X11/Xproto.h>
 18
 19#include "wm.h"
 20
 21/* X structs */
 22Display *dpy;
 23Window root, barwin;
 24Atom wm_atom[WMLast], net_atom[NetLast];
 25Cursor cursor[CurLast];
 26Bool running = True;
 27Bool sel_screen;
 28
 29char statustext[1024], tag[256];
 30int screen, sx, sy, sw, sh, bx, by, bw, bh;
 31
 32Brush brush = {0};
 33Client *clients = NULL;
 34Client *stack = NULL;
 35
 36static Bool other_wm_running;
 37static const char version[] = "gridwm - " VERSION ", (C)opyright MMVI Anselm R. Garbe\n";
 38static int (*x_error_handler) (Display *, XErrorEvent *);
 39
 40static const char *status[] = {
 41	"sh", "-c", "echo -n `date '+%Y-%m-%d %H:%M'`" 
 42	" `uptime | sed 's/.*://; s/,//g'`"
 43	" `acpi | awk '{print $4}' | sed 's/,//'`", 0
 44};
 45
 46static void
 47usage()
 48{
 49	fputs("usage: gridwm [-v]\n", stderr);
 50	exit(1);
 51}
 52
 53static void
 54scan_wins()
 55{
 56	unsigned int i, num;
 57	Window *wins;
 58	XWindowAttributes wa;
 59	Window d1, d2;
 60
 61	if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
 62		for(i = 0; i < num; i++) {
 63			if(!XGetWindowAttributes(dpy, wins[i], &wa))
 64				continue;
 65			if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
 66				continue;
 67			if(wa.map_state == IsViewable)
 68				manage(wins[i], &wa);
 69		}
 70	}
 71	if(wins)
 72		XFree(wins);
 73}
 74
 75static int
 76win_property(Window w, Atom a, Atom t, long l, unsigned char **prop)
 77{
 78	Atom real;
 79	int format;
 80	unsigned long res, extra;
 81	int status;
 82
 83	status = XGetWindowProperty(dpy, w, a, 0L, l, False, t, &real, &format,
 84			&res, &extra, prop);
 85
 86	if(status != Success || *prop == 0) {
 87		return 0;
 88	}
 89	if(res == 0) {
 90		free((void *) *prop);
 91	}
 92	return res;
 93}
 94
 95int
 96win_proto(Window w)
 97{
 98	Atom *protocols;
 99	long res;
100	int protos = 0;
101	int i;
102
103	res = win_property(w, wm_atom[WMProtocols], XA_ATOM, 20L,
104			((unsigned char **) &protocols));
105	if(res <= 0) {
106		return protos;
107	}
108	for(i = 0; i < res; i++) {
109		if(protocols[i] == wm_atom[WMDelete])
110			protos |= WM_PROTOCOL_DELWIN;
111	}
112	free((char *) protocols);
113	return protos;
114}
115
116void
117send_message(Window w, Atom a, long value)
118{
119	XEvent e;
120
121	e.type = ClientMessage;
122	e.xclient.window = w;
123	e.xclient.message_type = a;
124	e.xclient.format = 32;
125	e.xclient.data.l[0] = value;
126	e.xclient.data.l[1] = CurrentTime;
127	XSendEvent(dpy, w, False, NoEventMask, &e);
128	XFlush(dpy);
129}
130
131/*
132 * There's no way to check accesses to destroyed windows, thus
133 * those cases are ignored (especially on UnmapNotify's).
134 * Other types of errors call Xlib's default error handler, which
135 * calls exit().
136 */
137int
138error_handler(Display *dpy, XErrorEvent *error)
139{
140	if(error->error_code == BadWindow
141			|| (error->request_code == X_SetInputFocus
142				&& error->error_code == BadMatch)
143			|| (error->request_code == X_PolyText8
144				&& error->error_code == BadDrawable)
145			|| (error->request_code == X_PolyFillRectangle
146				&& error->error_code == BadDrawable)
147			|| (error->request_code == X_PolySegment
148				&& error->error_code == BadDrawable)
149			|| (error->request_code == X_ConfigureWindow
150				&& error->error_code == BadMatch)
151			|| (error->request_code == X_GrabKey
152				&& error->error_code == BadAccess))
153		return 0;
154	fprintf(stderr, "gridwm: fatal error: request code=%d, error code=%d\n",
155			error->request_code, error->error_code);
156	return x_error_handler(dpy, error); /* may call exit() */
157}
158
159/*
160 * Startup Error handler to check if another window manager
161 * is already running.
162 */
163static int
164startup_error_handler(Display *dpy, XErrorEvent *error)
165{
166	other_wm_running = True;
167	return -1;
168}
169
170static void
171cleanup()
172{
173	while(clients)
174		unmanage(clients);
175	XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
176}
177
178void
179run(void *aux)
180{
181	spawn(dpy, aux);
182}
183
184void
185quit(void *aux)
186{
187	running = False;
188}
189
190int
191main(int argc, char *argv[])
192{
193	int i;
194	XSetWindowAttributes wa;
195	unsigned int mask;
196	Window w;
197	XEvent ev;
198	fd_set fds;
199	struct timeval t, timeout = {
200		.tv_usec = 0,
201		.tv_sec = STATUSDELAY,
202	};
203
204	/* command line args */
205	for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) {
206		switch (argv[i][1]) {
207		case 'v':
208			fprintf(stdout, "%s", version);
209			exit(0);
210			break;
211		default:
212			usage();
213			break;
214		}
215	}
216
217	dpy = XOpenDisplay(0);
218	if(!dpy)
219		error("gridwm: cannot connect X server\n");
220
221	screen = DefaultScreen(dpy);
222	root = RootWindow(dpy, screen);
223
224	/* check if another WM is already running */
225	other_wm_running = False;
226	XSetErrorHandler(startup_error_handler);
227	/* this causes an error if some other WM is running */
228	XSelectInput(dpy, root, SubstructureRedirectMask);
229	XFlush(dpy);
230
231	if(other_wm_running)
232		error("gridwm: another window manager is already running\n");
233
234	sx = sy = 0;
235	sw = DisplayWidth(dpy, screen);
236	sh = DisplayHeight(dpy, screen);
237	sel_screen = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
238
239	XSetErrorHandler(0);
240	x_error_handler = XSetErrorHandler(error_handler);
241
242	/* init atoms */
243	wm_atom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
244	wm_atom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
245	net_atom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
246	net_atom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
247
248	XChangeProperty(dpy, root, net_atom[NetSupported], XA_ATOM, 32,
249			PropModeReplace, (unsigned char *) net_atom, NetLast);
250
251
252	/* init cursors */
253	cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
254	cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
255	cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
256
257	update_keys();
258
259	/* style */
260	loadcolors(dpy, screen, &brush, BGCOLOR, FGCOLOR, BORDERCOLOR);
261	loadfont(dpy, &brush.font, FONT);
262
263	wa.override_redirect = 1;
264	wa.background_pixmap = ParentRelative;
265	wa.event_mask = ExposureMask;
266
267	bx = by = 0;
268	bw = sw;
269	bh = texth(&brush.font);
270	barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen),
271			CopyFromParent, DefaultVisual(dpy, screen),
272			CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
273	XDefineCursor(dpy, barwin, cursor[CurNormal]);
274	XMapRaised(dpy, barwin);
275
276	brush.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
277	brush.gc = XCreateGC(dpy, root, 0, 0);
278
279	pipe_spawn(statustext, sizeof(statustext), dpy, (char **)status);
280	draw_bar();
281
282	wa.event_mask = SubstructureRedirectMask | EnterWindowMask \
283					| LeaveWindowMask;
284	wa.cursor = cursor[CurNormal];
285	XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
286
287	scan_wins();
288
289	while(running) {
290		if(XPending(dpy) > 0) {
291			XNextEvent(dpy, &ev);
292			if(handler[ev.type])
293				(handler[ev.type]) (&ev); /* call handler */
294			continue;
295		}
296		FD_ZERO(&fds);
297		FD_SET(ConnectionNumber(dpy), &fds);
298		t = timeout;
299		if(select(ConnectionNumber(dpy) + 1, &fds, NULL, NULL, &t) > 0)
300			continue;
301		else if(errno != EINTR) {
302			pipe_spawn(statustext, sizeof(statustext), dpy, (char **)status);
303			draw_bar();
304		}
305	}
306
307	cleanup();
308	XCloseDisplay(dpy);
309
310	return 0;
311}