aewl

view main.c @ 750:d6057999ab28

added rule for qiv
author meillo@marmaro.de
date Fri, 15 Jun 2007 23:15:21 +0200
parents 1cb9ae0a0104
children 743061dbe7f2
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 bh, bmw, screen, sx, sy, sw, sh, wax, way, waw, wah;
22 unsigned int master, nmaster, ntags, numlockmask;
23 Atom wmatom[WMLast], netatom[NetLast];
24 Bool running = True;
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;
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);
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 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
96 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
97 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
98 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
99 PropModeReplace, (unsigned char *) netatom, NetLast);
100 /* init cursors */
101 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
102 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
103 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
104 /* init modifier map */
105 numlockmask = 0;
106 modmap = XGetModifierMapping(dpy);
107 for (i = 0; i < 8; i++) {
108 for (j = 0; j < modmap->max_keypermod; j++) {
109 if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock))
110 numlockmask = (1 << i);
111 }
112 }
113 XFreeModifiermap(modmap);
114 /* select for events */
115 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
116 | EnterWindowMask | LeaveWindowMask;
117 wa.cursor = cursor[CurNormal];
118 XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
119 grabkeys();
120 initrregs();
121 for(ntags = 0; tags[ntags]; ntags++);
122 seltag = emallocz(sizeof(Bool) * ntags);
123 seltag[0] = True;
124 /* style */
125 dc.norm[ColBorder] = getcolor(NORMBORDERCOLOR);
126 dc.norm[ColBG] = getcolor(NORMBGCOLOR);
127 dc.norm[ColFG] = getcolor(NORMFGCOLOR);
128 dc.sel[ColBorder] = getcolor(SELBORDERCOLOR);
129 dc.sel[ColBG] = getcolor(SELBGCOLOR);
130 dc.sel[ColFG] = getcolor(SELFGCOLOR);
131 setfont(FONT);
132 /* geometry */
133 sx = sy = 0;
134 sw = DisplayWidth(dpy, screen);
135 sh = DisplayHeight(dpy, screen);
136 master = MASTER;
137 nmaster = NMASTER;
138 bmw = textw(TILESYMBOL) > textw(FLOATSYMBOL) ? textw(TILESYMBOL) : textw(FLOATSYMBOL);
139 /* bar */
140 dc.h = bh = dc.font.height + 2;
141 wa.override_redirect = 1;
142 wa.background_pixmap = ParentRelative;
143 wa.event_mask = ButtonPressMask | ExposureMask;
144 barwin = XCreateWindow(dpy, root, sx, sy + (TOPBAR ? 0 : sh - bh), sw, bh, 0,
145 DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen),
146 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
147 XDefineCursor(dpy, barwin, cursor[CurNormal]);
148 XMapRaised(dpy, barwin);
149 strcpy(stext, "dwm-"VERSION);
150 /* windowarea */
151 wax = sx;
152 way = sy + (TOPBAR ? bh : 0);
153 wah = sh - bh;
154 waw = sw;
155 /* pixmap for everything */
156 dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
157 dc.gc = XCreateGC(dpy, root, 0, 0);
158 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
159 /* multihead support */
160 selscreen = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
161 }
163 /*
164 * Startup Error handler to check if another window manager
165 * is already running.
166 */
167 static int
168 xerrorstart(Display *dsply, XErrorEvent *ee) {
169 otherwm = True;
170 return -1;
171 }
173 /* extern */
175 void
176 sendevent(Window w, Atom a, long value) {
177 XEvent e;
179 e.type = ClientMessage;
180 e.xclient.window = w;
181 e.xclient.message_type = a;
182 e.xclient.format = 32;
183 e.xclient.data.l[0] = value;
184 e.xclient.data.l[1] = CurrentTime;
185 XSendEvent(dpy, w, False, NoEventMask, &e);
186 XSync(dpy, False);
187 }
189 void
190 quit(Arg *arg) {
191 readin = running = False;
192 }
194 /* There's no way to check accesses to destroyed windows, thus those cases are
195 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
196 * default error handler, which may call exit.
197 */
198 int
199 xerror(Display *dpy, XErrorEvent *ee) {
200 if(ee->error_code == BadWindow
201 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
202 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
203 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
204 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
205 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
206 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
207 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
208 return 0;
209 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
210 ee->request_code, ee->error_code);
211 return xerrorxlib(dpy, ee); /* may call exit */
212 }
214 int
215 main(int argc, char *argv[]) {
216 char *p;
217 int r, xfd;
218 fd_set rd;
220 if(argc == 2 && !strncmp("-v", argv[1], 3)) {
221 fputs("dwm-"VERSION", (C)opyright MMVI-MMVII Anselm R. Garbe\n", stdout);
222 exit(EXIT_SUCCESS);
223 }
224 else if(argc != 1)
225 eprint("usage: dwm [-v]\n");
226 setlocale(LC_CTYPE, "");
227 dpy = XOpenDisplay(0);
228 if(!dpy)
229 eprint("dwm: cannot open display\n");
230 xfd = ConnectionNumber(dpy);
231 screen = DefaultScreen(dpy);
232 root = RootWindow(dpy, screen);
233 otherwm = False;
234 XSetErrorHandler(xerrorstart);
235 /* this causes an error if some other window manager is running */
236 XSelectInput(dpy, root, SubstructureRedirectMask);
237 XSync(dpy, False);
238 if(otherwm)
239 eprint("dwm: another window manager is already running\n");
241 XSync(dpy, False);
242 XSetErrorHandler(NULL);
243 xerrorxlib = XSetErrorHandler(xerror);
244 XSync(dpy, False);
245 setup();
246 drawstatus();
247 scan();
249 /* main event loop, also reads status text from stdin */
250 XSync(dpy, False);
251 procevent();
252 readin = True;
253 while(running) {
254 FD_ZERO(&rd);
255 if(readin)
256 FD_SET(STDIN_FILENO, &rd);
257 FD_SET(xfd, &rd);
258 if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
259 if(errno == EINTR)
260 continue;
261 eprint("select failed\n");
262 }
263 if(FD_ISSET(STDIN_FILENO, &rd)) {
264 switch(r = read(STDIN_FILENO, stext, sizeof stext - 1)) {
265 case -1:
266 strncpy(stext, strerror(errno), sizeof stext - 1);
267 stext[sizeof stext - 1] = '\0';
268 readin = False;
269 break;
270 case 0:
271 strncpy(stext, "EOF", 4);
272 readin = False;
273 break;
274 default:
275 for(stext[r] = '\0', p = stext + strlen(stext) - 1; p >= stext && *p == '\n'; *p-- = '\0');
276 for(; p >= stext && *p != '\n'; --p);
277 if(p > stext)
278 strncpy(stext, p + 1, sizeof stext);
279 }
280 drawstatus();
281 }
282 if(FD_ISSET(xfd, &rd))
283 procevent();
284 }
285 cleanup();
286 XCloseDisplay(dpy);
287 return 0;
288 }