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