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