JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
82b2deea6c9dc9f325f13fa5a76d8f7d6b6f165a
[dwm.git] / wm.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 <errno.h>
7
8 #include <stdarg.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11
12 #include <sys/types.h>
13 #include <sys/time.h>
14
15 #include <X11/cursorfont.h>
16 #include <X11/Xatom.h>
17 #include <X11/Xproto.h>
18
19 #include "wm.h"
20
21 /* X structs */
22 Display *dpy;
23 Window root, barwin;
24 Atom wm_atom[WMLast], net_atom[NetLast];
25 Cursor cursor[CurLast];
26 XRectangle rect, barrect;
27 Bool running = True;
28 Bool sel_screen;
29
30 char statustext[1024], tag[256];
31 int screen;
32
33 Brush brush = {0};
34 Client *clients = NULL;
35 Client *stack = NULL;
36
37 static Bool other_wm_running;
38 static const char version[] = "gridwm - " VERSION ", (C)opyright MMVI Anselm R. Garbe\n";
39 static int (*x_error_handler) (Display *, XErrorEvent *);
40
41 static const char *status[] = {
42         "sh", "-c", "echo -n `date '+%Y/%m/%d %H:%M'`" 
43         " `uptime | sed 's/.*://; s/,//g'`"
44         " `acpi | awk '{print $4}' | sed 's/,//'`", 0
45 };
46
47 static void
48 usage()
49 {
50         fputs("usage: gridwm [-v]\n", stderr);
51         exit(1);
52 }
53
54 static void
55 scan_wins()
56 {
57         unsigned int i, num;
58         Window *wins;
59         XWindowAttributes wa;
60         Window d1, d2;
61
62         if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
63                 for(i = 0; i < num; i++) {
64                         if(!XGetWindowAttributes(dpy, wins[i], &wa))
65                                 continue;
66                         if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
67                                 continue;
68                         if(wa.map_state == IsViewable)
69                                 manage(wins[i], &wa);
70                 }
71         }
72         if(wins)
73                 XFree(wins);
74 }
75
76 static int
77 win_property(Window w, Atom a, Atom t, long l, unsigned char **prop)
78 {
79         Atom real;
80         int format;
81         unsigned long res, extra;
82         int status;
83
84         status = XGetWindowProperty(dpy, w, a, 0L, l, False, t, &real, &format,
85                         &res, &extra, prop);
86
87         if(status != Success || *prop == 0) {
88                 return 0;
89         }
90         if(res == 0) {
91                 free((void *) *prop);
92         }
93         return res;
94 }
95
96 int
97 win_proto(Window w)
98 {
99         Atom *protocols;
100         long res;
101         int protos = 0;
102         int i;
103
104         res = win_property(w, wm_atom[WMProtocols], XA_ATOM, 20L,
105                         ((unsigned char **) &protocols));
106         if(res <= 0) {
107                 return protos;
108         }
109         for(i = 0; i < res; i++) {
110                 if(protocols[i] == wm_atom[WMDelete])
111                         protos |= WM_PROTOCOL_DELWIN;
112         }
113         free((char *) protocols);
114         return protos;
115 }
116
117 void
118 send_message(Window w, Atom a, long value)
119 {
120         XEvent e;
121
122         e.type = ClientMessage;
123         e.xclient.window = w;
124         e.xclient.message_type = a;
125         e.xclient.format = 32;
126         e.xclient.data.l[0] = value;
127         e.xclient.data.l[1] = CurrentTime;
128         XSendEvent(dpy, w, False, NoEventMask, &e);
129         XFlush(dpy);
130 }
131
132 /*
133  * There's no way to check accesses to destroyed windows, thus
134  * those cases are ignored (especially on UnmapNotify's).
135  * Other types of errors call Xlib's default error handler, which
136  * calls exit().
137  */
138 int
139 error_handler(Display *dpy, XErrorEvent *error)
140 {
141         if(error->error_code == BadWindow
142                         || (error->request_code == X_SetInputFocus
143                                 && error->error_code == BadMatch)
144                         || (error->request_code == X_PolyText8
145                                 && error->error_code == BadDrawable)
146                         || (error->request_code == X_PolyFillRectangle
147                                 && error->error_code == BadDrawable)
148                         || (error->request_code == X_PolySegment
149                                 && error->error_code == BadDrawable)
150                         || (error->request_code == X_ConfigureWindow
151                                 && error->error_code == BadMatch)
152                         || (error->request_code == X_GrabKey
153                                 && error->error_code == BadAccess))
154                 return 0;
155         fprintf(stderr, "gridwm: fatal error: request code=%d, error code=%d\n",
156                         error->request_code, error->error_code);
157         return x_error_handler(dpy, error); /* may call exit() */
158 }
159
160 /*
161  * Startup Error handler to check if another window manager
162  * is already running.
163  */
164 static int
165 startup_error_handler(Display *dpy, XErrorEvent *error)
166 {
167         other_wm_running = True;
168         return -1;
169 }
170
171 static void
172 cleanup()
173 {
174         while(clients)
175                 unmanage(clients);
176         XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
177 }
178
179 int
180 main(int argc, char *argv[])
181 {
182         int i;
183         XSetWindowAttributes wa;
184         unsigned int mask;
185         Window w;
186         XEvent ev;
187         fd_set fds;
188         struct timeval t, timeout = {
189                 .tv_usec = 0,
190                 .tv_sec = STATUSDELAY,
191         };
192
193         /* command line args */
194         for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) {
195                 switch (argv[i][1]) {
196                 case 'v':
197                         fprintf(stdout, "%s", version);
198                         exit(0);
199                         break;
200                 default:
201                         usage();
202                         break;
203                 }
204         }
205
206         dpy = XOpenDisplay(0);
207         if(!dpy)
208                 error("gridwm: cannot connect X server\n");
209
210         screen = DefaultScreen(dpy);
211         root = RootWindow(dpy, screen);
212
213         /* check if another WM is already running */
214         other_wm_running = False;
215         XSetErrorHandler(startup_error_handler);
216         /* this causes an error if some other WM is running */
217         XSelectInput(dpy, root, SubstructureRedirectMask);
218         XFlush(dpy);
219
220         if(other_wm_running)
221                 error("gridwm: another window manager is already running\n");
222
223         rect.x = rect.y = 0;
224         rect.width = DisplayWidth(dpy, screen);
225         rect.height = DisplayHeight(dpy, screen);
226         sel_screen = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
227
228         XSetErrorHandler(0);
229         x_error_handler = XSetErrorHandler(error_handler);
230
231         /* init atoms */
232         wm_atom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
233         wm_atom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
234         net_atom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
235         net_atom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
236
237         XChangeProperty(dpy, root, net_atom[NetSupported], XA_ATOM, 32,
238                         PropModeReplace, (unsigned char *) net_atom, NetLast);
239
240
241         /* init cursors */
242         cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
243         cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
244         cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
245
246         update_keys();
247
248         /* style */
249         loadcolors(dpy, screen, &brush, BGCOLOR, FGCOLOR, BORDERCOLOR);
250         loadfont(dpy, &brush.font, FONT);
251
252         wa.override_redirect = 1;
253         wa.background_pixmap = ParentRelative;
254         wa.event_mask = ExposureMask;
255
256         barrect = rect;
257         barrect.height = labelheight(&brush.font);
258         barrect.y = rect.height - barrect.height;
259         barwin = XCreateWindow(dpy, root, barrect.x, barrect.y,
260                         barrect.width, barrect.height, 0, DefaultDepth(dpy, screen),
261                         CopyFromParent, DefaultVisual(dpy, screen),
262                         CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
263         XDefineCursor(dpy, barwin, cursor[CurNormal]);
264         XMapRaised(dpy, barwin);
265
266         brush.drawable = XCreatePixmap(dpy, root, rect.width, barrect.height,
267                         DefaultDepth(dpy, screen));
268         brush.gc = XCreateGC(dpy, root, 0, 0);
269
270         pipe_spawn(statustext, sizeof(statustext), dpy, (char **)status);
271         draw_bar();
272
273         wa.event_mask = SubstructureRedirectMask | EnterWindowMask \
274                                         | LeaveWindowMask;
275         wa.cursor = cursor[CurNormal];
276         XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
277
278         scan_wins();
279
280         while(running) {
281                 if(XPending(dpy) > 0) {
282                         XNextEvent(dpy, &ev);
283                         if(handler[ev.type])
284                                 (handler[ev.type]) (&ev); /* call handler */
285                         continue;
286                 }
287                 FD_ZERO(&fds);
288                 FD_SET(ConnectionNumber(dpy), &fds);
289                 t = timeout;
290                 if(select(ConnectionNumber(dpy) + 1, &fds, NULL, NULL, &t) > 0)
291                         continue;
292                 else if(errno != EINTR) {
293                         pipe_spawn(statustext, sizeof(statustext), dpy, (char **)status);
294                         draw_bar();
295                 }
296         }
297
298         cleanup();
299         XCloseDisplay(dpy);
300
301         return 0;
302 }