dwm-meillo

view main.c @ 702:94c82dffe1d5

personally I prefer 2px borders
author Anselm R. Garbe <arg@suckless.org>
date Thu, 18 Jan 2007 11:46:39 +0100
parents cbec08a54a15
children a2d568a5cdb8
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 Bool *seltag;
22 int bh, bmw, screen, sx, sy, sw, sh, wax, way, waw, wah;
23 unsigned int master, nmaster, 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 numlockmask = 0;
105 modmap = XGetModifierMapping(dpy);
106 for (i = 0; i < 8; i++) {
107 for (j = 0; j < modmap->max_keypermod; j++) {
108 if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock))
109 numlockmask = (1 << i);
110 }
111 }
112 XFreeModifiermap(modmap);
113 /* select for events */
114 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
115 | EnterWindowMask | LeaveWindowMask;
116 wa.cursor = cursor[CurNormal];
117 XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
118 grabkeys();
119 initrregs();
120 for(ntags = 0; tags[ntags]; ntags++);
121 seltag = emallocz(sizeof(Bool) * ntags);
122 seltag[0] = True;
123 /* style */
124 dc.norm[ColBorder] = getcolor(NORMBORDERCOLOR);
125 dc.norm[ColBG] = getcolor(NORMBGCOLOR);
126 dc.norm[ColFG] = getcolor(NORMFGCOLOR);
127 dc.sel[ColBorder] = getcolor(SELBORDERCOLOR);
128 dc.sel[ColBG] = getcolor(SELBGCOLOR);
129 dc.sel[ColFG] = getcolor(SELFGCOLOR);
130 setfont(FONT);
131 /* geometry */
132 sx = sy = 0;
133 sw = DisplayWidth(dpy, screen);
134 sh = DisplayHeight(dpy, screen);
135 master = MASTER;
136 nmaster = NMASTER;
137 bmw = textw(TILESYMBOL) > textw(FLOATSYMBOL) ? textw(TILESYMBOL) : textw(FLOATSYMBOL);
138 /* bar */
139 dc.h = bh = dc.font.height + 2;
140 wa.override_redirect = 1;
141 wa.background_pixmap = ParentRelative;
142 wa.event_mask = ButtonPressMask | ExposureMask;
143 barwin = XCreateWindow(dpy, root, sx, sy, sw, bh, 0, DefaultDepth(dpy, screen),
144 CopyFromParent, DefaultVisual(dpy, screen),
145 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
146 XDefineCursor(dpy, barwin, cursor[CurNormal]);
147 XMapRaised(dpy, barwin);
148 strcpy(stext, "dwm-"VERSION);
149 /* windowarea */
150 wax = sx;
151 way = sy + bh;
152 wah = sh - bh;
153 waw = sw;
154 /* pixmap for everything */
155 dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
156 dc.gc = XCreateGC(dpy, root, 0, 0);
157 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
158 /* multihead support */
159 issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
160 }
162 /*
163 * Startup Error handler to check if another window manager
164 * is already running.
165 */
166 static int
167 xerrorstart(Display *dsply, XErrorEvent *ee) {
168 otherwm = True;
169 return -1;
170 }
172 /* extern */
174 int
175 getproto(Window w) {
176 int i, format, protos, status;
177 unsigned long extra, res;
178 Atom *protocols, real;
180 protos = 0;
181 status = XGetWindowProperty(dpy, w, wmatom[WMProtocols], 0L, 20L, False,
182 XA_ATOM, &real, &format, &res, &extra, (unsigned char **)&protocols);
183 if(status != Success || protocols == 0)
184 return protos;
185 for(i = 0; i < res; i++)
186 if(protocols[i] == wmatom[WMDelete])
187 protos |= PROTODELWIN;
188 free(protocols);
189 return protos;
190 }
192 void
193 sendevent(Window w, Atom a, long value) {
194 XEvent e;
196 e.type = ClientMessage;
197 e.xclient.window = w;
198 e.xclient.message_type = a;
199 e.xclient.format = 32;
200 e.xclient.data.l[0] = value;
201 e.xclient.data.l[1] = CurrentTime;
202 XSendEvent(dpy, w, False, NoEventMask, &e);
203 XSync(dpy, False);
204 }
206 void
207 quit(Arg *arg) {
208 readin = running = False;
209 }
211 /* There's no way to check accesses to destroyed windows, thus those cases are
212 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
213 * default error handler, which may call exit.
214 */
215 int
216 xerror(Display *dpy, XErrorEvent *ee) {
217 if(ee->error_code == BadWindow
218 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
219 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
220 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
221 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
222 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
223 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
224 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
225 return 0;
226 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
227 ee->request_code, ee->error_code);
228 return xerrorxlib(dpy, ee); /* may call exit */
229 }
231 int
232 main(int argc, char *argv[]) {
233 char *p;
234 int r, xfd;
235 fd_set rd;
237 if(argc == 2 && !strncmp("-v", argv[1], 3)) {
238 fputs("dwm-"VERSION", (C)opyright MMVI-MMVII Anselm R. Garbe\n", stdout);
239 exit(EXIT_SUCCESS);
240 }
241 else if(argc != 1)
242 eprint("usage: dwm [-v]\n");
243 setlocale(LC_CTYPE, "");
244 dpy = XOpenDisplay(0);
245 if(!dpy)
246 eprint("dwm: cannot open display\n");
247 xfd = ConnectionNumber(dpy);
248 screen = DefaultScreen(dpy);
249 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);
255 if(otherwm)
256 eprint("dwm: another window manager is already running\n");
258 XSync(dpy, False);
259 XSetErrorHandler(NULL);
260 xerrorxlib = XSetErrorHandler(xerror);
261 XSync(dpy, False);
262 setup();
263 drawstatus();
264 scan();
266 /* main event loop, also reads status text from stdin */
267 XSync(dpy, False);
268 procevent();
269 readin = True;
270 while(running) {
271 FD_ZERO(&rd);
272 if(readin)
273 FD_SET(STDIN_FILENO, &rd);
274 FD_SET(xfd, &rd);
275 if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
276 if(errno == EINTR)
277 continue;
278 eprint("select failed\n");
279 }
280 if(FD_ISSET(STDIN_FILENO, &rd)) {
281 switch(r = read(STDIN_FILENO, stext, sizeof stext - 1)) {
282 case -1:
283 strncpy(stext, strerror(errno), sizeof stext - 1);
284 stext[sizeof stext - 1] = '\0';
285 readin = False;
286 break;
287 case 0:
288 strncpy(stext, "EOF", 4);
289 readin = False;
290 break;
291 default:
292 for(stext[r] = '\0', p = stext + strlen(stext) - 1; p >= stext && *p == '\n'; *p-- = '\0');
293 for(p = stext + strlen(stext) - 1; p >= stext && *p != '\n'; --p);
294 if(p > stext)
295 strncpy(stext, p + 1, sizeof stext);
296 }
297 drawstatus();
298 }
299 if(FD_ISSET(xfd, &rd))
300 procevent();
301 }
302 cleanup();
303 XCloseDisplay(dpy);
304 return 0;
305 }