view main.c @ 14:09e9101ee51e

adjust inverse border color
author paulo
date Thu, 05 May 2016 22:17:52 -0700
parents bc03b37b37ba
children
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 dc.inv[ColBorder] = initcolor(INVBORDERCOLOR);
185 dc.inv[ColBG] = initcolor(INVBGCOLOR);
186 dc.inv[ColFG] = initcolor(INVFGCOLOR);
187 initfont(FONT);
188 /* geometry */
189 sx = sy = 0;
190 sw = DisplayWidth(dpy, screen);
191 sh = DisplayHeight(dpy, screen);
192 initlayouts();
193 /* bar */
194 dc.h = bh = dc.font.height + 2;
195 wa.override_redirect = 1;
196 wa.background_pixmap = ParentRelative;
197 wa.event_mask = ButtonPressMask | ExposureMask;
198 barwin = XCreateWindow(dpy, root, sx, sy + (TOPBAR ? 0 : sh - bh), sw, bh, 0,
199 DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen),
200 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
201 XDefineCursor(dpy, barwin, cursor[CurNormal]);
202 XMapRaised(dpy, barwin);
203 strcpy(stext, "dwm-"VERSION);
204 /* taskbar */
205 if (TASKBAR) {
206 wa.override_redirect = 1;
207 wa.background_pixmap = ParentRelative;
208 wa.event_mask = ButtonPressMask | ExposureMask;
209 tbarwin = XCreateWindow(dpy, root, sx, sy + (TOPBAR ? sh - bh : 0 ), sw, bh, 0,
210 DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen),
211 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
212 XDefineCursor(dpy, tbarwin, cursor[CurNormal]);
213 XMapRaised(dpy, tbarwin);
214 }
215 /* windowarea */
216 wax = sx;
217 way = sy + (TASKBAR ? bh : (TOPBAR ? bh : 0));
218 wah = sh - bh - (TASKBAR ? bh : 0);
219 waw = sw;
220 /* pixmap for everything */
221 dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
222 dc.gc = XCreateGC(dpy, root, 0, 0);
223 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
224 /* multihead support */
225 selscreen = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
226 }
228 /*
229 * Startup Error handler to check if another window manager
230 * is already running.
231 */
232 static int
233 xerrorstart(Display *dsply, XErrorEvent *ee) {
234 otherwm = True;
235 return -1;
236 }
238 /* extern */
240 void
241 sendevent(Window w, Atom a, long value) {
242 XEvent e;
244 e.type = ClientMessage;
245 e.xclient.window = w;
246 e.xclient.message_type = a;
247 e.xclient.format = 32;
248 e.xclient.data.l[0] = value;
249 e.xclient.data.l[1] = CurrentTime;
250 XSendEvent(dpy, w, False, NoEventMask, &e);
251 XSync(dpy, False);
252 }
254 void
255 quit(Arg *arg) {
256 readin = running = False;
257 }
259 /* There's no way to check accesses to destroyed windows, thus those cases are
260 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
261 * default error handler, which may call exit.
262 */
263 int
264 xerror(Display *dpy, XErrorEvent *ee) {
265 if(ee->error_code == BadWindow
266 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
267 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
268 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
269 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
270 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
271 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
272 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
273 return 0;
274 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
275 ee->request_code, ee->error_code);
276 return xerrorxlib(dpy, ee); /* may call exit */
277 }
279 int
280 main(int argc, char *argv[]) {
281 char *p;
282 int r, xfd;
283 fd_set rd;
284 XEvent ev;
286 if(argc == 2 && !strncmp("-v", argv[1], 3))
287 eprint("dwm-"VERSION", (C)opyright MMVI-MMVII Anselm R. Garbe\n");
288 else if(argc != 1)
289 eprint("usage: dwm [-v]\n");
290 setlocale(LC_CTYPE, "");
291 if(!(dpy = XOpenDisplay(0)))
292 eprint("dwm: cannot open display\n");
293 xfd = ConnectionNumber(dpy);
294 screen = DefaultScreen(dpy);
295 root = RootWindow(dpy, screen);
296 otherwm = False;
297 XSetErrorHandler(xerrorstart);
298 /* this causes an error if some other window manager is running */
299 XSelectInput(dpy, root, SubstructureRedirectMask);
300 XSync(dpy, False);
301 if(otherwm)
302 eprint("dwm: another window manager is already running\n");
304 XSync(dpy, False);
305 XSetErrorHandler(NULL);
306 xerrorxlib = XSetErrorHandler(xerror);
307 XSync(dpy, False);
308 setup();
309 drawstatus();
310 scan();
312 /* main event loop, also reads status text from stdin */
313 XSync(dpy, False);
314 readin = True;
315 while(running) {
316 FD_ZERO(&rd);
317 if(readin)
318 FD_SET(STDIN_FILENO, &rd);
319 FD_SET(xfd, &rd);
320 if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
321 if(errno == EINTR)
322 continue;
323 eprint("select failed\n");
324 }
325 if(FD_ISSET(STDIN_FILENO, &rd)) {
326 switch(r = read(STDIN_FILENO, stext, sizeof stext - 1)) {
327 case -1:
328 strncpy(stext, strerror(errno), sizeof stext - 1);
329 stext[sizeof stext - 1] = '\0';
330 readin = False;
331 break;
332 case 0:
333 strncpy(stext, "EOF", 4);
334 readin = False;
335 break;
336 default:
337 for(stext[r] = '\0', p = stext + strlen(stext) - 1; p >= stext && *p == '\n'; *p-- = '\0');
338 for(; p >= stext && *p != '\n'; --p);
339 if(p > stext)
340 strncpy(stext, p + 1, sizeof stext);
341 }
342 drawstatus();
343 }
344 if(FD_ISSET(xfd, &rd))
345 while(XPending(dpy)) {
346 XNextEvent(dpy, &ev);
347 if(handler[ev.type])
348 (handler[ev.type])(&ev); /* call handler */
349 }
350 }
351 cleanup();
352 XCloseDisplay(dpy);
353 return 0;
354 }