dwm-meillo

view main.c @ 350:b10852dbbffe

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