dwm-meillo

view main.c @ 645:8e6d75c68b69

changed arg's color scheme
author arg@mig29
date Tue, 02 Jan 2007 16:25:01 +0100
parents 1ed8c40dde36
children f3b8c71a69d4
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[1024];
21 Bool *seltag;
22 int bx, by, bw, bh, bmw, masterd, screen, sx, sy, sw, sh, wax, way, waw, wah;
23 unsigned int master, 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 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, TopLeft);
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 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
96 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
97 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
98 PropModeReplace, (unsigned char *) netatom, NetLast);
99 /* init cursors */
100 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
101 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
102 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
103 /* init modifier map */
104 modmap = XGetModifierMapping(dpy);
105 for (i = 0; i < 8; i++) {
106 for (j = 0; j < modmap->max_keypermod; j++) {
107 if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock))
108 numlockmask = (1 << i);
109 }
110 }
111 XFreeModifiermap(modmap);
112 /* select for events */
113 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
114 | EnterWindowMask | LeaveWindowMask;
115 wa.cursor = cursor[CurNormal];
116 XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
117 grabkeys();
118 initrregs();
119 for(ntags = 0; tags[ntags]; ntags++);
120 seltag = emallocz(sizeof(Bool) * ntags);
121 seltag[0] = True;
122 /* style */
123 dc.norm[ColBG] = getcolor(NORMBGCOLOR);
124 dc.norm[ColFG] = getcolor(NORMFGCOLOR);
125 dc.sel[ColBG] = getcolor(SELBGCOLOR);
126 dc.sel[ColFG] = getcolor(SELFGCOLOR);
127 dc.status[ColBG] = getcolor(STATUSBGCOLOR);
128 dc.status[ColFG] = getcolor(STATUSFGCOLOR);
129 setfont(FONT);
130 /* geometry */
131 bmw = textw(TILESYMBOL) > textw(FLOATSYMBOL) ? textw(TILESYMBOL) : textw(FLOATSYMBOL);
132 sx = sy = 0;
133 sw = DisplayWidth(dpy, screen);
134 sh = DisplayHeight(dpy, screen);
135 master = MASTER;
136 /* bar */
137 bx = sx;
138 by = sy;
139 bw = sw;
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, bx, by, bw, bh, 0, DefaultDepth(dpy, screen),
145 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 + bh;
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 issel = 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 int
176 getproto(Window w) {
177 int i, format, protos, status;
178 unsigned long extra, res;
179 Atom *protocols, real;
181 protos = 0;
182 status = XGetWindowProperty(dpy, w, wmatom[WMProtocols], 0L, 20L, False,
183 XA_ATOM, &real, &format, &res, &extra, (unsigned char **)&protocols);
184 if(status != Success || protocols == 0)
185 return protos;
186 for(i = 0; i < res; i++)
187 if(protocols[i] == wmatom[WMDelete])
188 protos |= PROTODELWIN;
189 free(protocols);
190 return protos;
191 }
193 void
194 sendevent(Window w, Atom a, long value) {
195 XEvent e;
197 e.type = ClientMessage;
198 e.xclient.window = w;
199 e.xclient.message_type = a;
200 e.xclient.format = 32;
201 e.xclient.data.l[0] = value;
202 e.xclient.data.l[1] = CurrentTime;
203 XSendEvent(dpy, w, False, NoEventMask, &e);
204 XSync(dpy, False);
205 }
207 void
208 quit(Arg *arg) {
209 readin = running = False;
210 }
212 /* There's no way to check accesses to destroyed windows, thus those cases are
213 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
214 * default error handler, which may call exit.
215 */
216 int
217 xerror(Display *dpy, XErrorEvent *ee) {
218 if(ee->error_code == BadWindow
219 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
220 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
221 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
222 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
223 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
224 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
225 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
226 return 0;
227 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
228 ee->request_code, ee->error_code);
229 return xerrorxlib(dpy, ee); /* may call exit */
230 }
232 int
233 main(int argc, char *argv[]) {
234 char *p;
235 int r, xfd;
236 fd_set rd;
238 if(argc == 2 && !strncmp("-v", argv[1], 3)) {
239 fputs("dwm-"VERSION", (C)opyright MMVI-MMVII Anselm R. Garbe\n", stdout);
240 exit(EXIT_SUCCESS);
241 }
242 else if(argc != 1)
243 eprint("usage: dwm [-v]\n");
244 setlocale(LC_CTYPE, "");
245 dpy = XOpenDisplay(0);
246 if(!dpy)
247 eprint("dwm: cannot open display\n");
248 xfd = ConnectionNumber(dpy);
249 screen = DefaultScreen(dpy);
250 root = RootWindow(dpy, screen);
251 otherwm = False;
252 XSetErrorHandler(xerrorstart);
253 /* this causes an error if some other window manager is running */
254 XSelectInput(dpy, root, SubstructureRedirectMask);
255 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);
263 setup();
264 drawstatus();
265 scan();
267 /* main event loop, also reads status text from stdin */
268 XSync(dpy, False);
269 procevent();
270 readin = True;
271 while(running) {
272 FD_ZERO(&rd);
273 if(readin)
274 FD_SET(STDIN_FILENO, &rd);
275 FD_SET(xfd, &rd);
276 if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
277 if(errno == EINTR)
278 continue;
279 eprint("select failed\n");
280 }
281 if(FD_ISSET(STDIN_FILENO, &rd)) {
282 switch(r = read(STDIN_FILENO, stext, sizeof stext - 1)) {
283 case -1:
284 strncpy(stext, strerror(errno), sizeof stext - 1);
285 stext[sizeof stext - 1] = '\0';
286 readin = False;
287 break;
288 case 0:
289 strncpy(stext, "EOF", 4);
290 readin = False;
291 break;
292 default:
293 for(stext[r] = '\0', p = stext + strlen(stext) - 1; p >= stext && *p == '\n'; *p-- = '\0');
294 for(p = stext + strlen(stext) - 1; p >= stext && *p != '\n'; --p);
295 if(p > stext)
296 strncpy(stext, p + 1, sizeof stext);
297 }
298 drawstatus();
299 }
300 if(FD_ISSET(xfd, &rd))
301 procevent();
302 }
303 cleanup();
304 XCloseDisplay(dpy);
305 return 0;
306 }