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