aewl

view main.c @ 293:093744cc5353

applied jk_to_tab patch
author Anselm R.Garbe <arg@10ksloc.org>
date Tue, 15 Aug 2006 08:25:11 +0200
parents 4aa632b6ba66
children 7565875d07d3
line source
1 /*
2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
3 * See LICENSE file for license details.
4 */
6 #include "dwm.h"
7 #include <errno.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 /* static */
20 static int (*xerrorxlib)(Display *, XErrorEvent *);
21 static Bool otherwm;
23 static void
24 cleanup()
25 {
26 while(sel) {
27 resize(sel, True, TopLeft);
28 unmanage(sel);
29 }
30 XUngrabKey(dpy, AnyKey, AnyModifier, root);
31 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
32 XSync(dpy, False);
33 }
35 static void
36 scan()
37 {
38 unsigned int i, num;
39 Window *wins, d1, d2;
40 XWindowAttributes wa;
42 wins = NULL;
43 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
44 for(i = 0; i < num; i++) {
45 if(!XGetWindowAttributes(dpy, wins[i], &wa))
46 continue;
47 if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
48 continue;
49 if(wa.map_state == IsViewable)
50 manage(wins[i], &wa);
51 }
52 }
53 if(wins)
54 XFree(wins);
55 }
57 static int
58 win_property(Window w, Atom a, Atom t, long l, unsigned char **prop)
59 {
60 int status, format;
61 unsigned long res, extra;
62 Atom real;
64 status = XGetWindowProperty(dpy, w, a, 0L, l, False, t, &real, &format,
65 &res, &extra, prop);
67 if(status != Success || *prop == 0) {
68 return 0;
69 }
70 if(res == 0) {
71 free((void *) *prop);
72 }
73 return res;
74 }
76 /*
77 * Startup Error handler to check if another window manager
78 * is already running.
79 */
80 static int
81 xerrorstart(Display *dsply, XErrorEvent *ee)
82 {
83 otherwm = True;
84 return -1;
85 }
87 /* extern */
89 char stext[1024];
90 Bool *seltag;
91 int screen, sx, sy, sw, sh, bx, by, bw, bh, mw;
92 unsigned int ntags, numlockmask;
93 Atom wmatom[WMLast], netatom[NetLast];
94 Bool running = True;
95 Bool issel = True;
96 Client *clients = NULL;
97 Client *sel = NULL;
98 Cursor cursor[CurLast];
99 Display *dpy;
100 DC dc = {0};
101 Window root, barwin;
103 int
104 getproto(Window w)
105 {
106 int protos = 0;
107 int i;
108 long res;
109 Atom *protocols;
111 res = win_property(w, wmatom[WMProtocols], XA_ATOM, 20L,
112 ((unsigned char **)&protocols));
113 if(res <= 0) {
114 return protos;
115 }
116 for(i = 0; i < res; i++) {
117 if(protocols[i] == wmatom[WMDelete])
118 protos |= PROTODELWIN;
119 }
120 free((char *) protocols);
121 return protos;
122 }
124 void
125 sendevent(Window w, Atom a, long value)
126 {
127 XEvent e;
129 e.type = ClientMessage;
130 e.xclient.window = w;
131 e.xclient.message_type = a;
132 e.xclient.format = 32;
133 e.xclient.data.l[0] = value;
134 e.xclient.data.l[1] = CurrentTime;
135 XSendEvent(dpy, w, False, NoEventMask, &e);
136 XSync(dpy, False);
137 }
139 void
140 quit(Arg *arg)
141 {
142 running = False;
143 }
145 /*
146 * There's no way to check accesses to destroyed windows, thus those cases are
147 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
148 * default error handler, which calls exit().
149 */
150 int
151 xerror(Display *dpy, XErrorEvent *ee)
152 {
153 if(ee->error_code == BadWindow
154 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
155 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
156 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
157 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
158 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
159 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess))
160 return 0;
161 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
162 ee->request_code, ee->error_code);
163 return xerrorxlib(dpy, ee); /* may call exit() */
164 }
166 int
167 main(int argc, char *argv[])
168 {
169 int i, j, xfd;
170 unsigned int mask;
171 fd_set rd;
172 Bool readin = True;
173 Window w;
174 XModifierKeymap *modmap;
175 XSetWindowAttributes wa;
177 if(argc == 2 && !strncmp("-v", argv[1], 3)) {
178 fputs("dwm-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout);
179 exit(EXIT_SUCCESS);
180 }
181 else if(argc != 1)
182 eprint("usage: dwm [-v]\n");
184 dpy = XOpenDisplay(0);
185 if(!dpy)
186 eprint("dwm: cannot open display\n");
188 xfd = ConnectionNumber(dpy);
189 screen = DefaultScreen(dpy);
190 root = RootWindow(dpy, screen);
192 otherwm = False;
193 XSetErrorHandler(xerrorstart);
194 /* this causes an error if some other window manager is running */
195 XSelectInput(dpy, root, SubstructureRedirectMask);
196 XSync(dpy, False);
198 if(otherwm)
199 eprint("dwm: another window manager is already running\n");
201 XSync(dpy, False);
202 XSetErrorHandler(NULL);
203 xerrorxlib = XSetErrorHandler(xerror);
204 XSync(dpy, False);
206 /* init atoms */
207 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
208 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
209 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
210 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
211 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
212 PropModeReplace, (unsigned char *) netatom, NetLast);
214 /* init cursors */
215 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
216 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
217 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
219 modmap = XGetModifierMapping(dpy);
220 for (i = 0; i < 8; i++) {
221 for (j = 0; j < modmap->max_keypermod; j++) {
222 if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock))
223 numlockmask = (1 << i);
224 }
225 }
226 XFree(modmap);
228 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask | EnterWindowMask | LeaveWindowMask;
229 wa.cursor = cursor[CurNormal];
230 XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
232 grabkeys();
233 initrregs();
235 for(ntags = 0; tags[ntags]; ntags++);
236 seltag = emallocz(sizeof(Bool) * ntags);
237 seltag[DEFTAG] = True;
239 /* style */
240 dc.bg = getcolor(BGCOLOR);
241 dc.fg = getcolor(FGCOLOR);
242 dc.border = getcolor(BORDERCOLOR);
243 setfont(FONT);
245 sx = sy = 0;
246 sw = DisplayWidth(dpy, screen);
247 sh = DisplayHeight(dpy, screen);
248 mw = (sw * MASTERW) / 100;
250 bx = by = 0;
251 bw = sw;
252 dc.h = bh = dc.font.height + 4;
253 wa.override_redirect = 1;
254 wa.background_pixmap = ParentRelative;
255 wa.event_mask = ButtonPressMask | ExposureMask;
256 barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen),
257 CopyFromParent, DefaultVisual(dpy, screen),
258 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
259 XDefineCursor(dpy, barwin, cursor[CurNormal]);
260 XMapRaised(dpy, barwin);
262 dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
263 dc.gc = XCreateGC(dpy, root, 0, 0);
265 strcpy(stext, "dwm-"VERSION);
266 drawstatus();
268 issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
270 scan();
272 /* main event loop, also reads status text from stdin */
273 XSync(dpy, False);
274 procevent();
275 while(running) {
276 FD_ZERO(&rd);
277 if(readin)
278 FD_SET(STDIN_FILENO, &rd);
279 FD_SET(xfd, &rd);
280 i = select(xfd + 1, &rd, NULL, NULL, NULL);
281 if(i == -1 && errno == EINTR)
282 continue;
283 if(i < 0)
284 eprint("select failed\n");
285 else if(i > 0) {
286 if(readin && FD_ISSET(STDIN_FILENO, &rd)) {
287 readin = NULL != fgets(stext, sizeof(stext), stdin);
288 if(readin)
289 stext[strlen(stext) - 1] = 0;
290 else
291 strcpy(stext, "broken pipe");
292 drawstatus();
293 }
294 }
295 procevent();
296 }
297 cleanup();
298 XCloseDisplay(dpy);
300 return 0;
301 }