dwm-meillo
view main.c @ 328:083f1f3e1e93
removed winprop (merged into getproto)
author | Anselm R. Garbe <arg@10kloc.org> |
---|---|
date | Tue, 22 Aug 2006 17:38:59 +0200 |
parents | 73efaa15a635 |
children | cea0c98495bc |
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 status, format, protos = 0;
96 int i;
97 unsigned long extra, res;
98 Atom *protocols, real;
100 status = XGetWindowProperty(dpy, w, wmatom[WMProtocols], 0L, 20L,
101 False, 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 }
108 free(protocols);
109 return protos;
110 }
112 void
113 sendevent(Window w, Atom a, long value)
114 {
115 XEvent e;
117 e.type = ClientMessage;
118 e.xclient.window = w;
119 e.xclient.message_type = a;
120 e.xclient.format = 32;
121 e.xclient.data.l[0] = value;
122 e.xclient.data.l[1] = CurrentTime;
123 XSendEvent(dpy, w, False, NoEventMask, &e);
124 XSync(dpy, False);
125 }
127 void
128 quit(Arg *arg)
129 {
130 readin = running = False;
131 }
133 /*
134 * There's no way to check accesses to destroyed windows, thus those cases are
135 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
136 * default error handler, which calls exit().
137 */
138 int
139 xerror(Display *dpy, XErrorEvent *ee)
140 {
141 if(ee->error_code == BadWindow
142 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
143 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
144 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
145 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
146 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
147 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess))
148 return 0;
149 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
150 ee->request_code, ee->error_code);
151 return xerrorxlib(dpy, ee); /* may call exit() */
152 }
154 int
155 main(int argc, char *argv[])
156 {
157 int i, j, xfd;
158 unsigned int mask;
159 fd_set rd;
160 Window w;
161 XModifierKeymap *modmap;
162 XSetWindowAttributes wa;
164 if(argc == 2 && !strncmp("-v", argv[1], 3)) {
165 fputs("dwm-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout);
166 exit(EXIT_SUCCESS);
167 }
168 else if(argc != 1)
169 eprint("usage: dwm [-v]\n");
171 dpy = XOpenDisplay(0);
172 if(!dpy)
173 eprint("dwm: cannot open display\n");
175 xfd = ConnectionNumber(dpy);
176 screen = DefaultScreen(dpy);
177 root = RootWindow(dpy, screen);
179 otherwm = False;
180 XSetErrorHandler(xerrorstart);
181 /* this causes an error if some other window manager is running */
182 XSelectInput(dpy, root, SubstructureRedirectMask);
183 XSync(dpy, False);
185 if(otherwm)
186 eprint("dwm: another window manager is already running\n");
188 XSync(dpy, False);
189 XSetErrorHandler(NULL);
190 xerrorxlib = XSetErrorHandler(xerror);
191 XSync(dpy, False);
193 /* init atoms */
194 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
195 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
196 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
197 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
198 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
199 PropModeReplace, (unsigned char *) netatom, NetLast);
201 /* init cursors */
202 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
203 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
204 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
206 modmap = XGetModifierMapping(dpy);
207 for (i = 0; i < 8; i++) {
208 for (j = 0; j < modmap->max_keypermod; j++) {
209 if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock))
210 numlockmask = (1 << i);
211 }
212 }
213 XFree(modmap);
215 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask | EnterWindowMask | LeaveWindowMask;
216 wa.cursor = cursor[CurNormal];
217 XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
219 grabkeys();
220 initrregs();
222 for(ntags = 0; tags[ntags]; ntags++);
223 seltag = emallocz(sizeof(Bool) * ntags);
224 seltag[0] = True;
226 /* style */
227 dc.bg = getcolor(BGCOLOR);
228 dc.fg = getcolor(FGCOLOR);
229 dc.border = getcolor(BORDERCOLOR);
230 setfont(FONT);
232 sx = sy = 0;
233 sw = DisplayWidth(dpy, screen);
234 sh = DisplayHeight(dpy, screen);
235 mw = (sw * MASTERW) / 100;
237 bx = by = 0;
238 bw = sw;
239 dc.h = bh = dc.font.height + 4;
240 wa.override_redirect = 1;
241 wa.background_pixmap = ParentRelative;
242 wa.event_mask = ButtonPressMask | ExposureMask;
243 barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen),
244 CopyFromParent, DefaultVisual(dpy, screen),
245 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
246 XDefineCursor(dpy, barwin, cursor[CurNormal]);
247 XMapRaised(dpy, barwin);
249 dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
250 dc.gc = XCreateGC(dpy, root, 0, 0);
252 strcpy(stext, "dwm-"VERSION);
253 drawstatus();
255 issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
257 scan();
259 /* main event loop, also reads status text from stdin */
260 XSync(dpy, False);
261 procevent();
262 readin = True;
263 while(running) {
264 FD_ZERO(&rd);
265 if(readin)
266 FD_SET(STDIN_FILENO, &rd);
267 FD_SET(xfd, &rd);
268 i = select(xfd + 1, &rd, NULL, NULL, NULL);
269 if((i == -1) && (errno == EINTR))
270 continue;
271 if(i > 0) {
272 if(readin && FD_ISSET(STDIN_FILENO, &rd)) {
273 readin = NULL != fgets(stext, sizeof(stext), stdin);
274 if(readin)
275 stext[strlen(stext) - 1] = 0;
276 else
277 strcpy(stext, "broken pipe");
278 drawstatus();
279 }
280 }
281 else if(i < 0)
282 eprint("select failed\n");
283 procevent();
284 }
285 cleanup();
286 XCloseDisplay(dpy);
288 return 0;
289 }