dwm-meillo

view main.c @ 330:50a81ba123bf

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