aewl

view main.c @ 162:a6a31e485fbd

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