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