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