all repos — dwm @ 16c67f32d62849792c8e6d4fdec22a1896f9c279

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 <stdarg.h>
  7#include <stdio.h>
  8#include <stdlib.h>
  9
 10#include <X11/cursorfont.h>
 11#include <X11/Xatom.h>
 12#include <X11/Xproto.h>
 13
 14#include "wm.h"
 15
 16/* X structs */
 17Display *dpy;
 18Window root, barwin;
 19Atom net_atom[NetLast];
 20Cursor cursor[CurLast];
 21XRectangle rect, barrect;
 22Bool running = True;
 23Client *clients = NULL;
 24
 25char *bartext, tag[256];
 26int screen, sel_screen;
 27
 28/* draw structs */
 29Brush brush = {0};
 30
 31enum { WM_PROTOCOL_DELWIN = 1 };
 32
 33static Bool other_wm_running;
 34static int (*x_error_handler) (Display *, XErrorEvent *);
 35static char version[] = "gridwm - " VERSION ", (C)opyright MMVI Anselm R. Garbe\n";
 36
 37static void
 38usage()
 39{
 40	fputs("usage: gridwm [-v]\n", stderr);
 41	exit(1);
 42}
 43
 44static void
 45scan_wins()
 46{
 47	unsigned int i, num;
 48	Window *wins;
 49	XWindowAttributes wa;
 50	Window d1, d2;
 51
 52	if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
 53		for(i = 0; i < num; i++) {
 54			if(!XGetWindowAttributes(dpy, wins[i], &wa))
 55				continue;
 56			if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
 57				continue;
 58			if(wa.map_state == IsViewable)
 59				manage(create_client(wins[i], &wa));
 60		}
 61	}
 62	if(wins)
 63		XFree(wins);
 64}
 65
 66/*
 67 * There's no way to check accesses to destroyed windows, thus
 68 * those cases are ignored (especially on UnmapNotify's).
 69 * Other types of errors call Xlib's default error handler, which
 70 * calls exit().
 71 */
 72static int
 73error_handler(Display *dpy, XErrorEvent *error)
 74{
 75	if(error->error_code == BadWindow
 76			|| (error->request_code == X_SetInputFocus
 77				&& error->error_code == BadMatch)
 78			|| (error->request_code == X_PolyText8
 79				&& error->error_code == BadDrawable)
 80			|| (error->request_code == X_PolyFillRectangle
 81				&& error->error_code == BadDrawable)
 82			|| (error->request_code == X_PolySegment
 83				&& error->error_code == BadDrawable)
 84			|| (error->request_code == X_ConfigureWindow
 85				&& error->error_code == BadMatch)
 86			|| (error->request_code == X_GrabKey
 87				&& error->error_code == BadAccess))
 88		return 0;
 89	fprintf(stderr, "gridwm: fatal error: request code=%d, error code=%d\n",
 90			error->request_code, error->error_code);
 91	return x_error_handler(dpy, error); /* may call exit() */
 92}
 93
 94/*
 95 * Startup Error handler to check if another window manager
 96 * is already running.
 97 */
 98static int
 99startup_error_handler(Display *dpy, XErrorEvent *error)
100{
101	other_wm_running = True;
102	return -1;
103}
104
105static void
106cleanup()
107{
108	/*
109	Client *c;
110	for(c=client; c; c=c->next)
111		reparent_client(c, root, c->sel->rect.x, c->sel->rect.y);
112	XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
113	*/
114}
115
116int
117main(int argc, char *argv[])
118{
119	int i;
120	XSetWindowAttributes wa;
121	unsigned int mask;
122	Window w;
123	XEvent ev;
124
125	/* command line args */
126	for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) {
127		switch (argv[i][1]) {
128		case 'v':
129			fprintf(stdout, "%s", version);
130			exit(0);
131			break;
132		default:
133			usage();
134			break;
135		}
136	}
137
138	dpy = XOpenDisplay(0);
139	if(!dpy)
140		error("gridwm: cannot connect X server\n");
141
142	screen = DefaultScreen(dpy);
143	root = RootWindow(dpy, screen);
144
145	/* check if another WM is already running */
146	other_wm_running = False;
147	XSetErrorHandler(startup_error_handler);
148	/* this causes an error if some other WM is running */
149	XSelectInput(dpy, root, SubstructureRedirectMask);
150	XFlush(dpy);
151
152	if(other_wm_running)
153		error("gridwm: another window manager is already running\n");
154
155	rect.x = rect.y = 0;
156	rect.width = DisplayWidth(dpy, screen);
157	rect.height = DisplayHeight(dpy, screen);
158	sel_screen = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
159
160	XSetErrorHandler(0);
161	x_error_handler = XSetErrorHandler(error_handler);
162
163	/* init atoms */
164	net_atom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
165	net_atom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
166
167	XChangeProperty(dpy, root, net_atom[NetSupported], XA_ATOM, 32,
168			PropModeReplace, (unsigned char *) net_atom, NetLast);
169
170
171	/* init cursors */
172	cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
173	cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
174	cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
175
176	update_keys();
177
178	brush.drawable = XCreatePixmap(dpy, root, rect.width, rect.height,
179			DefaultDepth(dpy, screen));
180	brush.gc = XCreateGC(dpy, root, 0, 0);
181
182	/* style */
183	loadcolors(dpy, screen, &brush, BGCOLOR, FGCOLOR, BORDERCOLOR);
184	loadfont(dpy, &brush.font, FONT);
185
186	wa.override_redirect = 1;
187	wa.background_pixmap = ParentRelative;
188	wa.event_mask = ExposureMask;
189
190	barrect = rect;
191	barrect.height = labelheight(&brush.font);
192	barrect.y = rect.height - barrect.height;
193	barwin = XCreateWindow(dpy, root, barrect.x, barrect.y,
194			barrect.width, barrect.height, 0, DefaultDepth(dpy, screen),
195			CopyFromParent, DefaultVisual(dpy, screen),
196			CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
197	bartext = NULL;
198	XDefineCursor(dpy, barwin, cursor[CurNormal]);
199	XMapRaised(dpy, barwin);
200	draw_bar();
201
202	wa.event_mask = SubstructureRedirectMask | EnterWindowMask \
203					| LeaveWindowMask;
204	wa.cursor = cursor[CurNormal];
205	XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
206
207	scan_wins();
208
209	while(running) {
210		XNextEvent(dpy, &ev);
211		if(handler[ev.type])
212			(handler[ev.type]) (&ev); /* call handler */
213	}
214
215	cleanup();
216	XCloseDisplay(dpy);
217
218	return 0;
219}