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