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