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