view main.c @ 6:0968b3739b8d

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