all repos — dwm @ da2bbd371c522d63d737d43a127601a3fdbcb9d8

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