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@59
|
7 #include <errno.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>
|
arg@138
|
12 #include <sys/select.h>
|
garbeam@0
|
13 #include <X11/cursorfont.h>
|
arg@291
|
14 #include <X11/keysym.h>
|
garbeam@0
|
15 #include <X11/Xatom.h>
|
garbeam@0
|
16 #include <X11/Xproto.h>
|
garbeam@0
|
17
|
garbeam@84
|
18 /* static */
|
garbeam@0
|
19
|
arg@123
|
20 static int (*xerrorxlib)(Display *, XErrorEvent *);
|
garbeam@75
|
21 static Bool otherwm;
|
garbeam@0
|
22
|
garbeam@0
|
23 static void
|
garbeam@76
|
24 cleanup()
|
garbeam@76
|
25 {
|
garbeam@76
|
26 while(sel) {
|
arg@99
|
27 resize(sel, True, TopLeft);
|
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;
|
arg@123
|
37 Window *wins, d1, d2;
|
garbeam@0
|
38 XWindowAttributes wa;
|
garbeam@0
|
39
|
garbeam@0
|
40 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
|
garbeam@0
|
41 for(i = 0; i < num; i++) {
|
garbeam@0
|
42 if(!XGetWindowAttributes(dpy, wins[i], &wa))
|
garbeam@0
|
43 continue;
|
garbeam@0
|
44 if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
|
garbeam@0
|
45 continue;
|
garbeam@0
|
46 if(wa.map_state == IsViewable)
|
garbeam@10
|
47 manage(wins[i], &wa);
|
garbeam@0
|
48 }
|
garbeam@0
|
49 }
|
garbeam@0
|
50 if(wins)
|
garbeam@0
|
51 XFree(wins);
|
garbeam@0
|
52 }
|
garbeam@0
|
53
|
garbeam@13
|
54 static int
|
garbeam@13
|
55 win_property(Window w, Atom a, Atom t, long l, unsigned char **prop)
|
garbeam@13
|
56 {
|
arg@123
|
57 int status, format;
|
arg@123
|
58 unsigned long res, extra;
|
garbeam@13
|
59 Atom real;
|
garbeam@13
|
60
|
garbeam@13
|
61 status = XGetWindowProperty(dpy, w, a, 0L, l, False, t, &real, &format,
|
garbeam@13
|
62 &res, &extra, prop);
|
garbeam@13
|
63
|
garbeam@13
|
64 if(status != Success || *prop == 0) {
|
garbeam@13
|
65 return 0;
|
garbeam@13
|
66 }
|
garbeam@13
|
67 if(res == 0) {
|
garbeam@13
|
68 free((void *) *prop);
|
garbeam@13
|
69 }
|
garbeam@13
|
70 return res;
|
garbeam@13
|
71 }
|
garbeam@13
|
72
|
garbeam@76
|
73 /*
|
garbeam@76
|
74 * Startup Error handler to check if another window manager
|
garbeam@76
|
75 * is already running.
|
garbeam@76
|
76 */
|
garbeam@76
|
77 static int
|
garbeam@76
|
78 xerrorstart(Display *dsply, XErrorEvent *ee)
|
garbeam@76
|
79 {
|
garbeam@76
|
80 otherwm = True;
|
garbeam@76
|
81 return -1;
|
garbeam@76
|
82 }
|
garbeam@76
|
83
|
garbeam@84
|
84 /* extern */
|
garbeam@84
|
85
|
garbeam@84
|
86 char stext[1024];
|
arg@262
|
87 Bool *seltag;
|
garbeam@84
|
88 int screen, sx, sy, sw, sh, bx, by, bw, bh, mw;
|
arg@291
|
89 unsigned int ntags, numlockmask;
|
garbeam@84
|
90 Atom wmatom[WMLast], netatom[NetLast];
|
garbeam@84
|
91 Bool running = True;
|
garbeam@84
|
92 Bool issel = True;
|
garbeam@84
|
93 Client *clients = NULL;
|
garbeam@84
|
94 Client *sel = NULL;
|
garbeam@84
|
95 Cursor cursor[CurLast];
|
garbeam@84
|
96 Display *dpy;
|
garbeam@84
|
97 DC dc = {0};
|
garbeam@84
|
98 Window root, barwin;
|
garbeam@76
|
99
|
garbeam@13
|
100 int
|
garbeam@75
|
101 getproto(Window w)
|
garbeam@13
|
102 {
|
garbeam@13
|
103 int protos = 0;
|
garbeam@13
|
104 int i;
|
arg@123
|
105 long res;
|
arg@203
|
106 Atom *protocols;
|
garbeam@13
|
107
|
arg@203
|
108 res = win_property(w, wmatom[WMProtocols], XA_ATOM, 20L,
|
arg@203
|
109 ((unsigned char **)&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])
|
arg@157
|
115 protos |= PROTODELWIN;
|
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
|
arg@123
|
151 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
|
arg@123
|
152 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
|
arg@123
|
153 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
|
arg@123
|
154 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
|
arg@123
|
155 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
|
arg@123
|
156 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess))
|
garbeam@0
|
157 return 0;
|
garbeam@34
|
158 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
|
arg@123
|
159 ee->request_code, ee->error_code);
|
garbeam@75
|
160 return xerrorxlib(dpy, ee); /* may call exit() */
|
garbeam@27
|
161 }
|
garbeam@27
|
162
|
garbeam@0
|
163 int
|
garbeam@0
|
164 main(int argc, char *argv[])
|
garbeam@0
|
165 {
|
arg@291
|
166 int i, j, xfd;
|
arg@123
|
167 unsigned int mask;
|
garbeam@59
|
168 fd_set rd;
|
arg@164
|
169 Bool readin = True;
|
garbeam@0
|
170 Window w;
|
garbeam@5
|
171 XEvent ev;
|
arg@291
|
172 XModifierKeymap *modmap;
|
arg@123
|
173 XSetWindowAttributes wa;
|
garbeam@0
|
174
|
arg@137
|
175 if(argc == 2 && !strncmp("-v", argv[1], 3)) {
|
arg@137
|
176 fputs("dwm-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout);
|
arg@137
|
177 exit(EXIT_SUCCESS);
|
garbeam@0
|
178 }
|
arg@137
|
179 else if(argc != 1)
|
arg@137
|
180 eprint("usage: dwm [-v]\n");
|
garbeam@0
|
181
|
garbeam@0
|
182 dpy = XOpenDisplay(0);
|
garbeam@0
|
183 if(!dpy)
|
arg@197
|
184 eprint("dwm: cannot open display\n");
|
garbeam@0
|
185
|
arg@265
|
186 xfd = ConnectionNumber(dpy);
|
garbeam@0
|
187 screen = DefaultScreen(dpy);
|
garbeam@0
|
188 root = RootWindow(dpy, screen);
|
garbeam@0
|
189
|
garbeam@75
|
190 otherwm = False;
|
garbeam@75
|
191 XSetErrorHandler(xerrorstart);
|
arg@197
|
192 /* this causes an error if some other window manager is running */
|
garbeam@0
|
193 XSelectInput(dpy, root, SubstructureRedirectMask);
|
garbeam@78
|
194 XSync(dpy, False);
|
garbeam@0
|
195
|
garbeam@75
|
196 if(otherwm)
|
garbeam@75
|
197 eprint("dwm: another window manager is already running\n");
|
garbeam@0
|
198
|
garbeam@78
|
199 XSetErrorHandler(NULL);
|
garbeam@75
|
200 xerrorxlib = XSetErrorHandler(xerror);
|
arg@275
|
201 XSync(dpy, False);
|
garbeam@0
|
202
|
garbeam@0
|
203 /* init atoms */
|
garbeam@77
|
204 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
|
garbeam@77
|
205 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
|
garbeam@77
|
206 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
|
garbeam@77
|
207 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
|
garbeam@77
|
208 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
|
garbeam@77
|
209 PropModeReplace, (unsigned char *) netatom, NetLast);
|
garbeam@0
|
210
|
garbeam@0
|
211 /* init cursors */
|
garbeam@0
|
212 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
|
garbeam@0
|
213 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
|
garbeam@0
|
214 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
|
garbeam@0
|
215
|
arg@291
|
216 modmap = XGetModifierMapping(dpy);
|
arg@291
|
217 for (i = 0; i < 8; i++) {
|
arg@291
|
218 for (j = 0; j < modmap->max_keypermod; j++) {
|
arg@291
|
219 if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock))
|
arg@291
|
220 numlockmask = (1 << i);
|
arg@291
|
221 }
|
arg@291
|
222 }
|
arg@291
|
223 XFree(modmap);
|
arg@291
|
224
|
arg@291
|
225 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask | EnterWindowMask | LeaveWindowMask;
|
arg@281
|
226 wa.cursor = cursor[CurNormal];
|
arg@281
|
227 XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
|
arg@281
|
228
|
garbeam@73
|
229 grabkeys();
|
arg@191
|
230 initrregs();
|
garbeam@0
|
231
|
arg@178
|
232 for(ntags = 0; tags[ntags]; ntags++);
|
arg@262
|
233 seltag = emallocz(sizeof(Bool) * ntags);
|
arg@262
|
234 seltag[DEFTAG] = True;
|
arg@178
|
235
|
garbeam@5
|
236 /* style */
|
garbeam@74
|
237 dc.bg = getcolor(BGCOLOR);
|
garbeam@74
|
238 dc.fg = getcolor(FGCOLOR);
|
arg@237
|
239 dc.border = getcolor(BORDERCOLOR);
|
garbeam@74
|
240 setfont(FONT);
|
garbeam@5
|
241
|
garbeam@57
|
242 sx = sy = 0;
|
garbeam@57
|
243 sw = DisplayWidth(dpy, screen);
|
garbeam@57
|
244 sh = DisplayHeight(dpy, screen);
|
garbeam@57
|
245 mw = (sw * MASTERW) / 100;
|
garbeam@5
|
246
|
arg@291
|
247 bx = by = 0;
|
arg@291
|
248 bw = sw;
|
arg@291
|
249 dc.h = bh = dc.font.height + 4;
|
garbeam@57
|
250 wa.override_redirect = 1;
|
garbeam@57
|
251 wa.background_pixmap = ParentRelative;
|
garbeam@58
|
252 wa.event_mask = ButtonPressMask | ExposureMask;
|
garbeam@57
|
253 barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen),
|
garbeam@57
|
254 CopyFromParent, DefaultVisual(dpy, screen),
|
garbeam@57
|
255 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
|
garbeam@57
|
256 XDefineCursor(dpy, barwin, cursor[CurNormal]);
|
garbeam@57
|
257 XMapRaised(dpy, barwin);
|
garbeam@57
|
258
|
garbeam@60
|
259 dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
|
garbeam@60
|
260 dc.gc = XCreateGC(dpy, root, 0, 0);
|
arg@222
|
261
|
arg@222
|
262 strcpy(stext, "dwm-"VERSION);
|
garbeam@74
|
263 drawstatus();
|
garbeam@60
|
264
|
garbeam@57
|
265 issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
|
garbeam@21
|
266
|
garbeam@75
|
267 scan();
|
garbeam@3
|
268
|
arg@214
|
269 /* main event loop, also reads status text from stdin */
|
arg@242
|
270 XSync(dpy, False);
|
arg@291
|
271 goto XLoop;
|
garbeam@5
|
272 while(running) {
|
garbeam@59
|
273 FD_ZERO(&rd);
|
arg@164
|
274 if(readin)
|
arg@164
|
275 FD_SET(STDIN_FILENO, &rd);
|
arg@265
|
276 FD_SET(xfd, &rd);
|
arg@266
|
277 i = select(xfd + 1, &rd, NULL, NULL, NULL);
|
garbeam@59
|
278 if(i == -1 && errno == EINTR)
|
garbeam@59
|
279 continue;
|
garbeam@59
|
280 if(i < 0)
|
garbeam@75
|
281 eprint("select failed\n");
|
garbeam@59
|
282 else if(i > 0) {
|
arg@164
|
283 if(readin && FD_ISSET(STDIN_FILENO, &rd)) {
|
arg@164
|
284 readin = NULL != fgets(stext, sizeof(stext), stdin);
|
arg@164
|
285 if(readin)
|
arg@164
|
286 stext[strlen(stext) - 1] = 0;
|
arg@162
|
287 else
|
arg@164
|
288 strcpy(stext, "broken pipe");
|
garbeam@74
|
289 drawstatus();
|
garbeam@59
|
290 }
|
arg@275
|
291 if(FD_ISSET(xfd, &rd)) {
|
arg@291
|
292 XLoop:
|
arg@275
|
293 while(XPending(dpy)) {
|
arg@275
|
294 XNextEvent(dpy, &ev);
|
arg@275
|
295 if(handler[ev.type])
|
arg@275
|
296 (handler[ev.type])(&ev); /* call handler */
|
arg@275
|
297 }
|
arg@275
|
298 }
|
garbeam@59
|
299 }
|
garbeam@5
|
300 }
|
garbeam@0
|
301 cleanup();
|
garbeam@0
|
302 XCloseDisplay(dpy);
|
garbeam@0
|
303
|
garbeam@0
|
304 return 0;
|
garbeam@0
|
305 }
|