JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
dffa4bafebc4430df0da7d7ae90301d1b577e249
[dwm.git] / main.c
1 /*
2  * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
3  * See LICENSE file for license details.
4  */
5
6 #include "dwm.h"
7
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>
17
18
19 /* static */
20
21 static int (*xerrorxlib)(Display *, XErrorEvent *);
22 static Bool otherwm;
23
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 }
33
34 static void
35 scan()
36 {
37         unsigned int i, num;
38         Window *wins, d1, d2;
39         XWindowAttributes wa;
40
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 }
54
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;
61
62         status = XGetWindowProperty(dpy, w, a, 0L, l, False, t, &real, &format,
63                         &res, &extra, prop);
64
65         if(status != Success || *prop == 0) {
66                 return 0;
67         }
68         if(res == 0) {
69                 free((void *) *prop);
70         }
71         return res;
72 }
73
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 }
84
85 /* extern */
86
87 char stext[1024];
88 /* CUSTOMIZE */
89 int tsel = Tdev; /* default tag */
90 /* END CUSTOMIZE */
91 int screen, sx, sy, sw, sh, bx, by, bw, bh, mw;
92 Atom wmatom[WMLast], netatom[NetLast];
93 Bool running = True;
94 Bool issel = True;
95 Client *clients = NULL;
96 Client *sel = NULL;
97 Cursor cursor[CurLast];
98 Display *dpy;
99 DC dc = {0};
100 Window root, barwin;
101
102 int
103 getproto(Window w)
104 {
105         int protos = 0;
106         int i;
107         long res;
108         unsigned char *protocols;
109
110         res = win_property(w, wmatom[WMProtocols], XA_ATOM, 20L, &protocols);
111         if(res <= 0) {
112                 return protos;
113         }
114         for(i = 0; i < res; i++) {
115                 if(protocols[i] == wmatom[WMDelete])
116                         protos |= WM_PROTOCOL_DELWIN;
117         }
118         free((char *) protocols);
119         return protos;
120 }
121
122 void
123 sendevent(Window w, Atom a, long value)
124 {
125         XEvent e;
126
127         e.type = ClientMessage;
128         e.xclient.window = w;
129         e.xclient.message_type = a;
130         e.xclient.format = 32;
131         e.xclient.data.l[0] = value;
132         e.xclient.data.l[1] = CurrentTime;
133         XSendEvent(dpy, w, False, NoEventMask, &e);
134         XSync(dpy, False);
135 }
136
137 void
138 quit(Arg *arg)
139 {
140         running = False;
141 }
142
143 /*
144  * There's no way to check accesses to destroyed windows, thus those cases are
145  * ignored (especially on UnmapNotify's).  Other types of errors call Xlibs
146  * default error handler, which calls exit().
147  */
148 int
149 xerror(Display *dpy, XErrorEvent *ee)
150 {
151         if(ee->error_code == BadWindow
152         || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
153         || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
154         || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
155         || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
156         || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
157         || (ee->request_code == X_GrabKey && ee->error_code == BadAccess))
158                 return 0;
159         fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
160                 ee->request_code, ee->error_code);
161         return xerrorxlib(dpy, ee); /* may call exit() */
162 }
163
164 int
165 main(int argc, char *argv[])
166 {
167         int i;
168         unsigned int mask;
169         fd_set rd;
170         Bool readin = True;
171         Window w;
172         XEvent ev;
173         XSetWindowAttributes wa;
174
175         if(argc == 2 && !strncmp("-v", argv[1], 3)) {
176                 fputs("dwm-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout);
177                 exit(EXIT_SUCCESS);
178         }
179         else if(argc != 1)
180                 eprint("usage: dwm [-v]\n");
181
182         dpy = XOpenDisplay(0);
183         if(!dpy)
184                 eprint("dwm: cannot connect X server\n");
185
186         screen = DefaultScreen(dpy);
187         root = RootWindow(dpy, screen);
188
189         /* check if another WM is already running */
190         otherwm = False;
191         XSetErrorHandler(xerrorstart);
192         /* this causes an error if some other WM is running */
193         XSelectInput(dpy, root, SubstructureRedirectMask);
194         XSync(dpy, False);
195
196         if(otherwm)
197                 eprint("dwm: another window manager is already running\n");
198
199         XSetErrorHandler(NULL);
200         xerrorxlib = XSetErrorHandler(xerror);
201
202         /* init atoms */
203         wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
204         wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
205         netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
206         netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
207         XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
208                         PropModeReplace, (unsigned char *) netatom, NetLast);
209
210         /* init cursors */
211         cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
212         cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
213         cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
214
215         grabkeys();
216
217         /* style */
218         dc.bg = getcolor(BGCOLOR);
219         dc.fg = getcolor(FGCOLOR);
220         dc.border = getcolor(BORDERCOLOR);
221         setfont(FONT);
222
223         sx = sy = 0;
224         sw = DisplayWidth(dpy, screen);
225         sh = DisplayHeight(dpy, screen);
226         mw = (sw * MASTERW) / 100;
227
228         wa.override_redirect = 1;
229         wa.background_pixmap = ParentRelative;
230         wa.event_mask = ButtonPressMask | ExposureMask;
231
232         bx = by = 0;
233         bw = sw;
234         dc.h = bh = dc.font.height + 4;
235         barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen),
236                         CopyFromParent, DefaultVisual(dpy, screen),
237                         CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
238         XDefineCursor(dpy, barwin, cursor[CurNormal]);
239         XMapRaised(dpy, barwin);
240
241         dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
242         dc.gc = XCreateGC(dpy, root, 0, 0);
243         drawstatus();
244
245         issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
246
247         wa.event_mask = SubstructureRedirectMask | EnterWindowMask | LeaveWindowMask;
248         wa.cursor = cursor[CurNormal];
249         XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
250
251         strcpy(stext, "dwm-"VERSION);
252         scan();
253
254         /* main event loop, reads status text from stdin as well */
255         while(running) {
256                 FD_ZERO(&rd);
257                 if(readin)
258                         FD_SET(STDIN_FILENO, &rd);
259                 FD_SET(ConnectionNumber(dpy), &rd);
260
261                 i = select(ConnectionNumber(dpy) + 1, &rd, 0, 0, 0);
262                 if(i == -1 && errno == EINTR)
263                         continue;
264                 if(i < 0)
265                         eprint("select failed\n");
266                 else if(i > 0) {
267                         if(FD_ISSET(ConnectionNumber(dpy), &rd)) {
268                                 while(XPending(dpy)) {
269                                         XNextEvent(dpy, &ev);
270                                         if(handler[ev.type])
271                                                 (handler[ev.type])(&ev); /* call handler */
272                                 }
273                         }
274                         if(readin && FD_ISSET(STDIN_FILENO, &rd)) {
275                                 readin = NULL != fgets(stext, sizeof(stext), stdin);
276                                 if(readin)
277                                         stext[strlen(stext) - 1] = 0;
278                                 else 
279                                         strcpy(stext, "broken pipe");
280                                 drawstatus();
281                         }
282                 }
283         }
284
285         cleanup();
286         XCloseDisplay(dpy);
287
288         return 0;
289 }