dwm-meillo

view main.c @ 123:61490330e90a

cleaned up code
author arg@10ksloc.org
date Thu, 20 Jul 2006 12:18:06 +0200
parents a19556fe83b5
children 1489f6b97714
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"
8 #include <errno.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include <X11/cursorfont.h>
14 #include <X11/Xatom.h>
15 #include <X11/Xproto.h>
18 /* static */
20 static int (*xerrorxlib)(Display *, XErrorEvent *);
21 static Bool otherwm;
23 static void
24 cleanup()
25 {
26 while(sel) {
27 resize(sel, True, TopLeft);
28 unmanage(sel);
29 }
30 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
31 }
33 static void
34 scan()
35 {
36 unsigned int i, num;
37 Window *wins, d1, d2;
38 XWindowAttributes wa;
40 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
41 for(i = 0; i < num; i++) {
42 if(!XGetWindowAttributes(dpy, wins[i], &wa))
43 continue;
44 if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
45 continue;
46 if(wa.map_state == IsViewable)
47 manage(wins[i], &wa);
48 }
49 }
50 if(wins)
51 XFree(wins);
52 }
54 static int
55 win_property(Window w, Atom a, Atom t, long l, unsigned char **prop)
56 {
57 int status, format;
58 unsigned long res, extra;
59 Atom real;
61 status = XGetWindowProperty(dpy, w, a, 0L, l, False, t, &real, &format,
62 &res, &extra, prop);
64 if(status != Success || *prop == 0) {
65 return 0;
66 }
67 if(res == 0) {
68 free((void *) *prop);
69 }
70 return res;
71 }
73 /*
74 * Startup Error handler to check if another window manager
75 * is already running.
76 */
77 static int
78 xerrorstart(Display *dsply, XErrorEvent *ee)
79 {
80 otherwm = True;
81 return -1;
82 }
84 /* extern */
86 char stext[1024];
87 int tsel = Tdev; /* default tag */
88 int screen, sx, sy, sw, sh, bx, by, bw, bh, mw;
89 Atom wmatom[WMLast], netatom[NetLast];
90 Bool running = True;
91 Bool issel = True;
92 Client *clients = NULL;
93 Client *sel = NULL;
94 Cursor cursor[CurLast];
95 Display *dpy;
96 DC dc = {0};
97 Window root, barwin;
99 int
100 getproto(Window w)
101 {
102 int protos = 0;
103 int i;
104 long res;
105 unsigned char *protocols;
107 res = win_property(w, wmatom[WMProtocols], XA_ATOM, 20L, &protocols);
108 if(res <= 0) {
109 return protos;
110 }
111 for(i = 0; i < res; i++) {
112 if(protocols[i] == wmatom[WMDelete])
113 protos |= WM_PROTOCOL_DELWIN;
114 }
115 free((char *) protocols);
116 return protos;
117 }
119 void
120 sendevent(Window w, Atom a, long value)
121 {
122 XEvent e;
124 e.type = ClientMessage;
125 e.xclient.window = w;
126 e.xclient.message_type = a;
127 e.xclient.format = 32;
128 e.xclient.data.l[0] = value;
129 e.xclient.data.l[1] = CurrentTime;
130 XSendEvent(dpy, w, False, NoEventMask, &e);
131 XSync(dpy, False);
132 }
134 void
135 quit(Arg *arg)
136 {
137 running = False;
138 }
140 /*
141 * There's no way to check accesses to destroyed windows, thus those cases are
142 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
143 * default error handler, which calls exit().
144 */
145 int
146 xerror(Display *dpy, XErrorEvent *ee)
147 {
148 if(ee->error_code == BadWindow
149 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
150 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
151 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
152 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
153 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
154 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess))
155 return 0;
156 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
157 ee->request_code, ee->error_code);
158 return xerrorxlib(dpy, ee); /* may call exit() */
159 }
161 int
162 main(int argc, char *argv[])
163 {
164 int i, n;
165 unsigned int mask;
166 fd_set rd;
167 Bool readstdin = True;
168 Window w;
169 XEvent ev;
170 XSetWindowAttributes wa;
172 for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) {
173 switch (argv[i][1]) {
174 default:
175 eprint("usage: dwm [-v]\n");
176 break;
177 case 'v':
178 fputs("dwm-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout);
179 exit(EXIT_SUCCESS);
180 break;
181 }
182 }
184 dpy = XOpenDisplay(0);
185 if(!dpy)
186 eprint("dwm: cannot connect X server\n");
188 screen = DefaultScreen(dpy);
189 root = RootWindow(dpy, screen);
191 /* check if another WM is already running */
192 otherwm = False;
193 XSetErrorHandler(xerrorstart);
194 /* this causes an error if some other WM is running */
195 XSelectInput(dpy, root, SubstructureRedirectMask);
196 XSync(dpy, False);
198 if(otherwm)
199 eprint("dwm: another window manager is already running\n");
201 XSetErrorHandler(NULL);
202 xerrorxlib = XSetErrorHandler(xerror);
204 /* init atoms */
205 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
206 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
207 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
208 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
209 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
210 PropModeReplace, (unsigned char *) netatom, NetLast);
212 /* init cursors */
213 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
214 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
215 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
217 grabkeys();
219 /* style */
220 dc.bg = getcolor(BGCOLOR);
221 dc.fg = getcolor(FGCOLOR);
222 dc.border = getcolor(BORDERCOLOR);
223 setfont(FONT);
225 sx = sy = 0;
226 sw = DisplayWidth(dpy, screen);
227 sh = DisplayHeight(dpy, screen);
228 mw = (sw * MASTERW) / 100;
230 wa.override_redirect = 1;
231 wa.background_pixmap = ParentRelative;
232 wa.event_mask = ButtonPressMask | ExposureMask;
234 bx = by = 0;
235 bw = sw;
236 dc.h = bh = dc.font.height + 4;
237 barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen),
238 CopyFromParent, DefaultVisual(dpy, screen),
239 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
240 XDefineCursor(dpy, barwin, cursor[CurNormal]);
241 XMapRaised(dpy, barwin);
243 dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
244 dc.gc = XCreateGC(dpy, root, 0, 0);
245 drawstatus();
247 issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
249 wa.event_mask = SubstructureRedirectMask | EnterWindowMask | LeaveWindowMask;
250 wa.cursor = cursor[CurNormal];
251 XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
253 strcpy(stext, "dwm-"VERSION);
254 scan();
256 /* main event loop, reads status text from stdin as well */
257 Mainloop:
258 while(running) {
259 FD_ZERO(&rd);
260 if(readstdin)
261 FD_SET(STDIN_FILENO, &rd);
262 FD_SET(ConnectionNumber(dpy), &rd);
264 i = select(ConnectionNumber(dpy) + 1, &rd, 0, 0, 0);
265 if(i == -1 && errno == EINTR)
266 continue;
267 if(i < 0)
268 eprint("select failed\n");
269 else if(i > 0) {
270 if(FD_ISSET(ConnectionNumber(dpy), &rd)) {
271 while(XPending(dpy)) {
272 XNextEvent(dpy, &ev);
273 if(handler[ev.type])
274 (handler[ev.type])(&ev); /* call handler */
275 }
276 }
277 if(readstdin && FD_ISSET(STDIN_FILENO, &rd)) {
278 i = n = 0;
279 for(;;) {
280 if((i = getchar()) == EOF) {
281 /* broken pipe/end of producer */
282 readstdin = False;
283 strcpy(stext, "broken pipe");
284 goto Mainloop;
285 }
286 if(i == '\n' || n >= sizeof(stext) - 1)
287 break;
288 stext[n++] = i;
289 }
290 stext[n] = 0;
291 drawstatus();
292 }
293 }
294 }
296 cleanup();
297 XCloseDisplay(dpy);
299 return 0;
300 }