aewl

view main.c @ 685:3ae7b19db478

nah the last change feels bad
author Anselm R. Garbe <arg@suckless.org>
date Fri, 12 Jan 2007 13:06:15 +0100
parents 1438e35b622e
children 39fa5308d73f
line source
1 /* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
2 * See LICENSE file for license details.
3 */
5 #include "dwm.h"
6 #include <errno.h>
7 #include <locale.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>
18 /* extern */
20 char stext[1024], mtext[32];
21 Bool *seltag;
22 int bx, by, bw, bh, bmw, masterd, screen, sx, sy, sw, sh, wax, way, waw, wah;
23 unsigned int master, nmaster, ntags, numlockmask;
24 Atom wmatom[WMLast], netatom[NetLast];
25 Bool running = True;
26 Bool issel = True;
27 Client *clients = NULL;
28 Client *sel = NULL;
29 Client *stack = NULL;
30 Cursor cursor[CurLast];
31 Display *dpy;
32 DC dc = {0};
33 Window root, barwin;
35 /* static */
37 static int (*xerrorxlib)(Display *, XErrorEvent *);
38 static Bool otherwm, readin;
40 static void
41 cleanup(void) {
42 close(STDIN_FILENO);
43 while(stack) {
44 resize(stack, True, TopLeft);
45 unmanage(stack);
46 }
47 if(dc.font.set)
48 XFreeFontSet(dpy, dc.font.set);
49 else
50 XFreeFont(dpy, dc.font.xfont);
51 XUngrabKey(dpy, AnyKey, AnyModifier, root);
52 XFreePixmap(dpy, dc.drawable);
53 XFreeGC(dpy, dc.gc);
54 XDestroyWindow(dpy, barwin);
55 XFreeCursor(dpy, cursor[CurNormal]);
56 XFreeCursor(dpy, cursor[CurResize]);
57 XFreeCursor(dpy, cursor[CurMove]);
58 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
59 XSync(dpy, False);
60 free(seltag);
61 }
63 static void
64 scan(void) {
65 unsigned int i, num;
66 Window *wins, d1, d2;
67 XWindowAttributes wa;
69 wins = NULL;
70 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
71 for(i = 0; i < num; i++) {
72 if(!XGetWindowAttributes(dpy, wins[i], &wa))
73 continue;
74 if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
75 continue;
76 if(wa.map_state == IsViewable)
77 manage(wins[i], &wa);
78 }
79 }
80 if(wins)
81 XFree(wins);
82 }
84 static void
85 setup(void) {
86 int i, j;
87 unsigned int mask;
88 Window w;
89 XModifierKeymap *modmap;
90 XSetWindowAttributes wa;
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 /* init cursors */
100 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
101 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
102 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
103 /* init modifier map */
104 numlockmask = 0;
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 XFreeModifiermap(modmap);
113 /* select for events */
114 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
115 | EnterWindowMask | LeaveWindowMask;
116 wa.cursor = cursor[CurNormal];
117 XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
118 grabkeys();
119 initrregs();
120 for(ntags = 0; tags[ntags]; ntags++);
121 seltag = emallocz(sizeof(Bool) * ntags);
122 seltag[0] = True;
123 /* style */
124 dc.norm[ColBG] = getcolor(NORMBGCOLOR);
125 dc.norm[ColFG] = getcolor(NORMFGCOLOR);
126 dc.sel[ColBG] = getcolor(SELBGCOLOR);
127 dc.sel[ColFG] = getcolor(SELFGCOLOR);
128 dc.status[ColBG] = getcolor(STATUSBGCOLOR);
129 dc.status[ColFG] = getcolor(STATUSFGCOLOR);
130 setfont(FONT);
131 /* geometry */
132 sx = sy = 0;
133 sw = DisplayWidth(dpy, screen);
134 sh = DisplayHeight(dpy, screen);
135 master = MASTER;
136 nmaster = NMASTER;
137 updatemodetext();
138 /* bar */
139 bx = sx;
140 by = sy;
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 strcpy(stext, "dwm-"VERSION);
152 /* windowarea */
153 wax = sx;
154 way = sy + bh;
155 wah = sh - bh;
156 waw = sw;
157 /* pixmap for everything */
158 dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
159 dc.gc = XCreateGC(dpy, root, 0, 0);
160 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
161 /* multihead support */
162 issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
163 }
165 /*
166 * Startup Error handler to check if another window manager
167 * is already running.
168 */
169 static int
170 xerrorstart(Display *dsply, XErrorEvent *ee) {
171 otherwm = True;
172 return -1;
173 }
175 /* extern */
177 int
178 getproto(Window w) {
179 int i, format, protos, status;
180 unsigned long extra, res;
181 Atom *protocols, real;
183 protos = 0;
184 status = XGetWindowProperty(dpy, w, wmatom[WMProtocols], 0L, 20L, False,
185 XA_ATOM, &real, &format, &res, &extra, (unsigned char **)&protocols);
186 if(status != Success || protocols == 0)
187 return protos;
188 for(i = 0; i < res; i++)
189 if(protocols[i] == wmatom[WMDelete])
190 protos |= PROTODELWIN;
191 free(protocols);
192 return protos;
193 }
195 void
196 sendevent(Window w, Atom a, long value) {
197 XEvent e;
199 e.type = ClientMessage;
200 e.xclient.window = w;
201 e.xclient.message_type = a;
202 e.xclient.format = 32;
203 e.xclient.data.l[0] = value;
204 e.xclient.data.l[1] = CurrentTime;
205 XSendEvent(dpy, w, False, NoEventMask, &e);
206 XSync(dpy, False);
207 }
209 void
210 quit(Arg *arg) {
211 readin = running = False;
212 }
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 may call exit.
217 */
218 int
219 xerror(Display *dpy, XErrorEvent *ee) {
220 if(ee->error_code == BadWindow
221 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
222 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
223 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
224 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
225 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
226 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
227 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
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 }
234 int
235 main(int argc, char *argv[]) {
236 char *p;
237 int r, xfd;
238 fd_set rd;
240 if(argc == 2 && !strncmp("-v", argv[1], 3)) {
241 fputs("dwm-"VERSION", (C)opyright MMVI-MMVII Anselm R. Garbe\n", stdout);
242 exit(EXIT_SUCCESS);
243 }
244 else if(argc != 1)
245 eprint("usage: dwm [-v]\n");
246 setlocale(LC_CTYPE, "");
247 dpy = XOpenDisplay(0);
248 if(!dpy)
249 eprint("dwm: cannot open display\n");
250 xfd = ConnectionNumber(dpy);
251 screen = DefaultScreen(dpy);
252 root = RootWindow(dpy, screen);
253 otherwm = False;
254 XSetErrorHandler(xerrorstart);
255 /* this causes an error if some other window manager is running */
256 XSelectInput(dpy, root, SubstructureRedirectMask);
257 XSync(dpy, False);
258 if(otherwm)
259 eprint("dwm: another window manager is already running\n");
261 XSync(dpy, False);
262 XSetErrorHandler(NULL);
263 xerrorxlib = XSetErrorHandler(xerror);
264 XSync(dpy, False);
265 setup();
266 drawstatus();
267 scan();
269 /* main event loop, also reads status text from stdin */
270 XSync(dpy, False);
271 procevent();
272 readin = True;
273 while(running) {
274 FD_ZERO(&rd);
275 if(readin)
276 FD_SET(STDIN_FILENO, &rd);
277 FD_SET(xfd, &rd);
278 if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
279 if(errno == EINTR)
280 continue;
281 eprint("select failed\n");
282 }
283 if(FD_ISSET(STDIN_FILENO, &rd)) {
284 switch(r = read(STDIN_FILENO, stext, sizeof stext - 1)) {
285 case -1:
286 strncpy(stext, strerror(errno), sizeof stext - 1);
287 stext[sizeof stext - 1] = '\0';
288 readin = False;
289 break;
290 case 0:
291 strncpy(stext, "EOF", 4);
292 readin = False;
293 break;
294 default:
295 for(stext[r] = '\0', p = stext + strlen(stext) - 1; p >= stext && *p == '\n'; *p-- = '\0');
296 for(p = stext + strlen(stext) - 1; p >= stext && *p != '\n'; --p);
297 if(p > stext)
298 strncpy(stext, p + 1, sizeof stext);
299 }
300 drawstatus();
301 }
302 if(FD_ISSET(xfd, &rd))
303 procevent();
304 }
305 cleanup();
306 XCloseDisplay(dpy);
307 return 0;
308 }