making Copyright notices more compact
[dwm.git] / main.c
1 /* © 2004-2007 Anselm R. Garbe <garbeam at gmail dot com>
2 * See LICENSE file for license details. */
3 #include "dwm.h"
4 #include <errno.h>
5 #include <locale.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <unistd.h>
10 #include <sys/select.h>
11 #include <X11/cursorfont.h>
12 #include <X11/keysym.h>
13 #include <X11/Xatom.h>
14 #include <X11/Xproto.h>
15
16 /* extern */
17
18 char stext[256];
19 int screen, sx, sy, sw, sh, wax, way, waw, wah;
20 unsigned int bh, ntags, numlockmask;
21 Atom wmatom[WMLast], netatom[NetLast];
22 Bool *seltag;
23 Bool selscreen = True;
24 Client *clients = NULL;
25 Client *sel = NULL;
26 Client *stack = NULL;
27 Cursor cursor[CurLast];
28 Display *dpy;
29 DC dc = {0};
30 Window root, barwin;
31
32 /* static */
33
34 static int (*xerrorxlib)(Display *, XErrorEvent *);
35 static Bool otherwm, readin;
36 static Bool running = True;
37
38 static void
39 cleanup(void) {
40 close(STDIN_FILENO);
41 while(stack) {
42 if(stack->isbanned)
43 XMoveWindow(dpy, stack->win, stack->x, stack->y);
44 unmanage(stack);
45 }
46 if(dc.font.set)
47 XFreeFontSet(dpy, dc.font.set);
48 else
49 XFreeFont(dpy, dc.font.xfont);
50 XUngrabKey(dpy, AnyKey, AnyModifier, root);
51 XFreePixmap(dpy, dc.drawable);
52 XFreeGC(dpy, dc.gc);
53 XDestroyWindow(dpy, barwin);
54 XFreeCursor(dpy, cursor[CurNormal]);
55 XFreeCursor(dpy, cursor[CurResize]);
56 XFreeCursor(dpy, cursor[CurMove]);
57 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
58 XSync(dpy, False);
59 free(seltag);
60 }
61
62 static unsigned long
63 initcolor(const char *colstr) {
64 Colormap cmap = DefaultColormap(dpy, screen);
65 XColor color;
66
67 if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
68 eprint("error, cannot allocate color '%s'\n", colstr);
69 return color.pixel;
70 }
71
72 static void
73 initfont(const char *fontstr) {
74 char *def, **missing;
75 int i, n;
76
77 missing = NULL;
78 if(dc.font.set)
79 XFreeFontSet(dpy, dc.font.set);
80 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
81 if(missing) {
82 while(n--)
83 fprintf(stderr, "missing fontset: %s\n", missing[n]);
84 XFreeStringList(missing);
85 }
86 if(dc.font.set) {
87 XFontSetExtents *font_extents;
88 XFontStruct **xfonts;
89 char **font_names;
90 dc.font.ascent = dc.font.descent = 0;
91 font_extents = XExtentsOfFontSet(dc.font.set);
92 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
93 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
94 if(dc.font.ascent < (*xfonts)->ascent)
95 dc.font.ascent = (*xfonts)->ascent;
96 if(dc.font.descent < (*xfonts)->descent)
97 dc.font.descent = (*xfonts)->descent;
98 xfonts++;
99 }
100 }
101 else {
102 if(dc.font.xfont)
103 XFreeFont(dpy, dc.font.xfont);
104 dc.font.xfont = NULL;
105 if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr)))
106 eprint("error, cannot load font: '%s'\n", fontstr);
107 dc.font.ascent = dc.font.xfont->ascent;
108 dc.font.descent = dc.font.xfont->descent;
109 }
110 dc.font.height = dc.font.ascent + dc.font.descent;
111 }
112
113 static void
114 scan(void) {
115 unsigned int i, num;
116 Window *wins, d1, d2;
117 XWindowAttributes wa;
118
119 wins = NULL;
120 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
121 for(i = 0; i < num; i++) {
122 if(!XGetWindowAttributes(dpy, wins[i], &wa)
123 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
124 continue;
125 if(wa.map_state == IsViewable)
126 manage(wins[i], &wa);
127 }
128 }
129 if(wins)
130 XFree(wins);
131 }
132
133 static void
134 setup(void) {
135 int i, j;
136 unsigned int mask;
137 Window w;
138 XModifierKeymap *modmap;
139 XSetWindowAttributes wa;
140
141 /* init atoms */
142 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
143 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
144 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
145 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
146 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
147 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
148 PropModeReplace, (unsigned char *) netatom, NetLast);
149 /* init cursors */
150 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
151 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
152 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
153 /* init modifier map */
154 numlockmask = 0;
155 modmap = XGetModifierMapping(dpy);
156 for (i = 0; i < 8; i++)
157 for (j = 0; j < modmap->max_keypermod; j++) {
158 if(modmap->modifiermap[i * modmap->max_keypermod + j]
159 == XKeysymToKeycode(dpy, XK_Num_Lock))
160 numlockmask = (1 << i);
161 }
162 XFreeModifiermap(modmap);
163 /* select for events */
164 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
165 | EnterWindowMask | LeaveWindowMask;
166 wa.cursor = cursor[CurNormal];
167 XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
168 grabkeys();
169 compileregs();
170 for(ntags = 0; tags[ntags]; ntags++);
171 seltag = emallocz(sizeof(Bool) * ntags);
172 seltag[0] = True;
173 /* style */
174 dc.norm[ColBorder] = initcolor(NORMBORDERCOLOR);
175 dc.norm[ColBG] = initcolor(NORMBGCOLOR);
176 dc.norm[ColFG] = initcolor(NORMFGCOLOR);
177 dc.sel[ColBorder] = initcolor(SELBORDERCOLOR);
178 dc.sel[ColBG] = initcolor(SELBGCOLOR);
179 dc.sel[ColFG] = initcolor(SELFGCOLOR);
180 initfont(FONT);
181 /* geometry */
182 sx = sy = 0;
183 sw = DisplayWidth(dpy, screen);
184 sh = DisplayHeight(dpy, screen);
185 initlayouts();
186 /* bar */
187 dc.h = bh = dc.font.height + 2;
188 wa.override_redirect = 1;
189 wa.background_pixmap = ParentRelative;
190 wa.event_mask = ButtonPressMask | ExposureMask;
191 barwin = XCreateWindow(dpy, root, sx, sy + (TOPBAR ? 0 : sh - bh), sw, bh, 0,
192 DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen),
193 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
194 XDefineCursor(dpy, barwin, cursor[CurNormal]);
195 XMapRaised(dpy, barwin);
196 strcpy(stext, "dwm-"VERSION);
197 /* windowarea */
198 wax = sx;
199 way = sy + (TOPBAR ? bh : 0);
200 wah = sh - bh;
201 waw = sw;
202 /* pixmap for everything */
203 dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
204 dc.gc = XCreateGC(dpy, root, 0, 0);
205 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
206 if(!dc.font.set)
207 XSetFont(dpy, dc.gc, dc.font.xfont->fid);
208 /* multihead support */
209 selscreen = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
210 }
211
212 /*
213 * Startup Error handler to check if another window manager
214 * is already running.
215 */
216 static int
217 xerrorstart(Display *dsply, XErrorEvent *ee) {
218 otherwm = True;
219 return -1;
220 }
221
222 /* extern */
223
224 void
225 quit(const char *arg) {
226 readin = running = False;
227 }
228
229 /* There's no way to check accesses to destroyed windows, thus those cases are
230 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
231 * default error handler, which may call exit.
232 */
233 int
234 xerror(Display *dpy, XErrorEvent *ee) {
235 if(ee->error_code == BadWindow
236 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
237 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
238 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
239 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
240 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
241 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
242 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
243 return 0;
244 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
245 ee->request_code, ee->error_code);
246 return xerrorxlib(dpy, ee); /* may call exit */
247 }
248
249 int
250 main(int argc, char *argv[]) {
251 char *p;
252 int r, xfd;
253 fd_set rd;
254 XEvent ev;
255
256 if(argc == 2 && !strncmp("-v", argv[1], 3))
257 eprint("dwm-"VERSION", © 2004-2007 Anselm R. Garbe\n");
258 else if(argc != 1)
259 eprint("usage: dwm [-v]\n");
260 setlocale(LC_CTYPE, "");
261 if(!(dpy = XOpenDisplay(0)))
262 eprint("dwm: cannot open display\n");
263 xfd = ConnectionNumber(dpy);
264 screen = DefaultScreen(dpy);
265 root = RootWindow(dpy, screen);
266 otherwm = False;
267 XSetErrorHandler(xerrorstart);
268 /* this causes an error if some other window manager is running */
269 XSelectInput(dpy, root, SubstructureRedirectMask);
270 XSync(dpy, False);
271 if(otherwm)
272 eprint("dwm: another window manager is already running\n");
273
274 XSync(dpy, False);
275 XSetErrorHandler(NULL);
276 xerrorxlib = XSetErrorHandler(xerror);
277 XSync(dpy, False);
278 setup();
279 drawstatus();
280 scan();
281
282 /* main event loop, also reads status text from stdin */
283 XSync(dpy, False);
284 readin = True;
285 while(running) {
286 FD_ZERO(&rd);
287 if(readin)
288 FD_SET(STDIN_FILENO, &rd);
289 FD_SET(xfd, &rd);
290 if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
291 if(errno == EINTR)
292 continue;
293 eprint("select failed\n");
294 }
295 if(FD_ISSET(STDIN_FILENO, &rd)) {
296 switch(r = read(STDIN_FILENO, stext, sizeof stext - 1)) {
297 case -1:
298 strncpy(stext, strerror(errno), sizeof stext - 1);
299 stext[sizeof stext - 1] = '\0';
300 readin = False;
301 break;
302 case 0:
303 strncpy(stext, "EOF", 4);
304 readin = False;
305 break;
306 default:
307 for(stext[r] = '\0', p = stext + strlen(stext) - 1; p >= stext && *p == '\n'; *p-- = '\0');
308 for(; p >= stext && *p != '\n'; --p);
309 if(p > stext)
310 strncpy(stext, p + 1, sizeof stext);
311 }
312 drawstatus();
313 }
314 while(XPending(dpy)) {
315 XNextEvent(dpy, &ev);
316 if(handler[ev.type])
317 (handler[ev.type])(&ev); /* call handler */
318 }
319 }
320 cleanup();
321 XCloseDisplay(dpy);
322 return 0;
323 }