rev |
line source |
arg@644
|
1 /* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
|
garbeam@0
|
2 * See LICENSE file for license details.
|
garbeam@0
|
3 */
|
garbeam@0
|
4
|
garbeam@76
|
5 #include "dwm.h"
|
garbeam@59
|
6 #include <errno.h>
|
arg@619
|
7 #include <locale.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@688
|
20 char stext[256];
|
arg@697
|
21 int bh, bmw, screen, sx, sy, sw, sh, wax, way, waw, wah;
|
arg@650
|
22 unsigned int master, nmaster, ntags, numlockmask;
|
arg@333
|
23 Atom wmatom[WMLast], netatom[NetLast];
|
arg@333
|
24 Bool running = True;
|
arg@740
|
25 Bool *seltag;
|
arg@716
|
26 Bool selscreen = True;
|
arg@333
|
27 Client *clients = NULL;
|
arg@333
|
28 Client *sel = NULL;
|
arg@446
|
29 Client *stack = NULL;
|
arg@333
|
30 Cursor cursor[CurLast];
|
arg@333
|
31 Display *dpy;
|
arg@333
|
32 DC dc = {0};
|
arg@333
|
33 Window root, barwin;
|
arg@333
|
34
|
garbeam@84
|
35 /* static */
|
garbeam@0
|
36
|
arg@123
|
37 static int (*xerrorxlib)(Display *, XErrorEvent *);
|
arg@302
|
38 static Bool otherwm, readin;
|
garbeam@0
|
39
|
garbeam@0
|
40 static void
|
arg@487
|
41 cleanup(void) {
|
arg@302
|
42 close(STDIN_FILENO);
|
arg@645
|
43 while(stack) {
|
arg@708
|
44 resize(stack, True);
|
arg@645
|
45 unmanage(stack);
|
garbeam@76
|
46 }
|
arg@295
|
47 if(dc.font.set)
|
arg@295
|
48 XFreeFontSet(dpy, dc.font.set);
|
arg@295
|
49 else
|
arg@295
|
50 XFreeFont(dpy, dc.font.xfont);
|
arg@292
|
51 XUngrabKey(dpy, AnyKey, AnyModifier, root);
|
arg@295
|
52 XFreePixmap(dpy, dc.drawable);
|
arg@295
|
53 XFreeGC(dpy, dc.gc);
|
arg@309
|
54 XDestroyWindow(dpy, barwin);
|
arg@616
|
55 XFreeCursor(dpy, cursor[CurNormal]);
|
arg@616
|
56 XFreeCursor(dpy, cursor[CurResize]);
|
arg@616
|
57 XFreeCursor(dpy, cursor[CurMove]);
|
garbeam@76
|
58 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
|
arg@292
|
59 XSync(dpy, False);
|
arg@433
|
60 free(seltag);
|
garbeam@76
|
61 }
|
garbeam@0
|
62
|
garbeam@0
|
63 static void
|
arg@487
|
64 scan(void) {
|
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@487
|
85 setup(void) {
|
arg@333
|
86 int i, j;
|
arg@333
|
87 unsigned int mask;
|
arg@333
|
88 Window w;
|
arg@333
|
89 XModifierKeymap *modmap;
|
arg@333
|
90 XSetWindowAttributes wa;
|
arg@333
|
91
|
arg@333
|
92 /* init atoms */
|
arg@333
|
93 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
|
arg@333
|
94 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
|
arg@725
|
95 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", 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 /* init cursors */
|
arg@333
|
101 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
|
arg@333
|
102 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
|
arg@333
|
103 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
|
arg@532
|
104 /* init modifier map */
|
arg@679
|
105 numlockmask = 0;
|
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@616
|
113 XFreeModifiermap(modmap);
|
arg@532
|
114 /* select for events */
|
arg@461
|
115 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
|
arg@461
|
116 | EnterWindowMask | LeaveWindowMask;
|
arg@333
|
117 wa.cursor = cursor[CurNormal];
|
arg@333
|
118 XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
|
arg@333
|
119 grabkeys();
|
arg@333
|
120 initrregs();
|
arg@333
|
121 for(ntags = 0; tags[ntags]; ntags++);
|
arg@333
|
122 seltag = emallocz(sizeof(Bool) * ntags);
|
arg@333
|
123 seltag[0] = True;
|
arg@333
|
124 /* style */
|
arg@689
|
125 dc.norm[ColBorder] = getcolor(NORMBORDERCOLOR);
|
arg@353
|
126 dc.norm[ColBG] = getcolor(NORMBGCOLOR);
|
arg@353
|
127 dc.norm[ColFG] = getcolor(NORMFGCOLOR);
|
arg@689
|
128 dc.sel[ColBorder] = getcolor(SELBORDERCOLOR);
|
arg@353
|
129 dc.sel[ColBG] = getcolor(SELBGCOLOR);
|
arg@353
|
130 dc.sel[ColFG] = getcolor(SELFGCOLOR);
|
arg@333
|
131 setfont(FONT);
|
arg@532
|
132 /* geometry */
|
arg@333
|
133 sx = sy = 0;
|
arg@333
|
134 sw = DisplayWidth(dpy, screen);
|
arg@333
|
135 sh = DisplayHeight(dpy, screen);
|
arg@524
|
136 master = MASTER;
|
arg@650
|
137 nmaster = NMASTER;
|
arg@688
|
138 bmw = textw(TILESYMBOL) > textw(FLOATSYMBOL) ? textw(TILESYMBOL) : textw(FLOATSYMBOL);
|
arg@532
|
139 /* bar */
|
arg@353
|
140 dc.h = bh = dc.font.height + 2;
|
arg@333
|
141 wa.override_redirect = 1;
|
arg@333
|
142 wa.background_pixmap = ParentRelative;
|
arg@333
|
143 wa.event_mask = ButtonPressMask | ExposureMask;
|
arg@739
|
144 barwin = XCreateWindow(dpy, root, sx, sy + (TOPBAR ? 0 : sh - bh), sw, bh, 0,
|
arg@739
|
145 DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen),
|
arg@333
|
146 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
|
arg@333
|
147 XDefineCursor(dpy, barwin, cursor[CurNormal]);
|
arg@333
|
148 XMapRaised(dpy, barwin);
|
arg@532
|
149 strcpy(stext, "dwm-"VERSION);
|
arg@565
|
150 /* windowarea */
|
arg@565
|
151 wax = sx;
|
arg@739
|
152 way = sy + (TOPBAR ? bh : 0);
|
arg@565
|
153 wah = sh - bh;
|
arg@565
|
154 waw = sw;
|
arg@532
|
155 /* pixmap for everything */
|
arg@333
|
156 dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
|
arg@333
|
157 dc.gc = XCreateGC(dpy, root, 0, 0);
|
arg@344
|
158 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
|
arg@532
|
159 /* multihead support */
|
arg@716
|
160 selscreen = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
|
arg@333
|
161 }
|
arg@333
|
162
|
garbeam@76
|
163 /*
|
garbeam@76
|
164 * Startup Error handler to check if another window manager
|
garbeam@76
|
165 * is already running.
|
garbeam@76
|
166 */
|
garbeam@76
|
167 static int
|
arg@461
|
168 xerrorstart(Display *dsply, XErrorEvent *ee) {
|
garbeam@76
|
169 otherwm = True;
|
garbeam@76
|
170 return -1;
|
garbeam@76
|
171 }
|
garbeam@76
|
172
|
garbeam@84
|
173 /* extern */
|
garbeam@84
|
174
|
garbeam@13
|
175 void
|
arg@461
|
176 sendevent(Window w, Atom a, long value) {
|
garbeam@13
|
177 XEvent e;
|
garbeam@13
|
178
|
garbeam@13
|
179 e.type = ClientMessage;
|
garbeam@13
|
180 e.xclient.window = w;
|
garbeam@13
|
181 e.xclient.message_type = a;
|
garbeam@13
|
182 e.xclient.format = 32;
|
garbeam@13
|
183 e.xclient.data.l[0] = value;
|
garbeam@13
|
184 e.xclient.data.l[1] = CurrentTime;
|
garbeam@13
|
185 XSendEvent(dpy, w, False, NoEventMask, &e);
|
garbeam@79
|
186 XSync(dpy, False);
|
garbeam@13
|
187 }
|
garbeam@13
|
188
|
garbeam@76
|
189 void
|
arg@461
|
190 quit(Arg *arg) {
|
arg@302
|
191 readin = running = False;
|
garbeam@75
|
192 }
|
garbeam@75
|
193
|
arg@532
|
194 /* There's no way to check accesses to destroyed windows, thus those cases are
|
garbeam@84
|
195 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
|
arg@455
|
196 * default error handler, which may call exit.
|
garbeam@0
|
197 */
|
garbeam@10
|
198 int
|
arg@461
|
199 xerror(Display *dpy, XErrorEvent *ee) {
|
garbeam@75
|
200 if(ee->error_code == BadWindow
|
arg@123
|
201 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
|
arg@123
|
202 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
|
arg@123
|
203 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
|
arg@123
|
204 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
|
arg@123
|
205 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
|
arg@458
|
206 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
|
arg@458
|
207 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
|
garbeam@0
|
208 return 0;
|
garbeam@34
|
209 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
|
arg@123
|
210 ee->request_code, ee->error_code);
|
arg@455
|
211 return xerrorxlib(dpy, ee); /* may call exit */
|
garbeam@27
|
212 }
|
garbeam@27
|
213
|
garbeam@0
|
214 int
|
arg@461
|
215 main(int argc, char *argv[]) {
|
arg@581
|
216 char *p;
|
arg@333
|
217 int r, xfd;
|
garbeam@59
|
218 fd_set rd;
|
garbeam@0
|
219
|
arg@137
|
220 if(argc == 2 && !strncmp("-v", argv[1], 3)) {
|
arg@644
|
221 fputs("dwm-"VERSION", (C)opyright MMVI-MMVII Anselm R. Garbe\n", stdout);
|
arg@137
|
222 exit(EXIT_SUCCESS);
|
garbeam@0
|
223 }
|
arg@137
|
224 else if(argc != 1)
|
arg@137
|
225 eprint("usage: dwm [-v]\n");
|
arg@619
|
226 setlocale(LC_CTYPE, "");
|
garbeam@0
|
227 dpy = XOpenDisplay(0);
|
garbeam@0
|
228 if(!dpy)
|
arg@197
|
229 eprint("dwm: cannot open display\n");
|
arg@265
|
230 xfd = ConnectionNumber(dpy);
|
garbeam@0
|
231 screen = DefaultScreen(dpy);
|
garbeam@0
|
232 root = RootWindow(dpy, screen);
|
garbeam@75
|
233 otherwm = False;
|
garbeam@75
|
234 XSetErrorHandler(xerrorstart);
|
arg@197
|
235 /* this causes an error if some other window manager is running */
|
garbeam@0
|
236 XSelectInput(dpy, root, SubstructureRedirectMask);
|
garbeam@78
|
237 XSync(dpy, False);
|
garbeam@75
|
238 if(otherwm)
|
garbeam@75
|
239 eprint("dwm: another window manager is already running\n");
|
garbeam@0
|
240
|
arg@292
|
241 XSync(dpy, False);
|
garbeam@78
|
242 XSetErrorHandler(NULL);
|
garbeam@75
|
243 xerrorxlib = XSetErrorHandler(xerror);
|
arg@275
|
244 XSync(dpy, False);
|
arg@333
|
245 setup();
|
garbeam@74
|
246 drawstatus();
|
garbeam@75
|
247 scan();
|
garbeam@3
|
248
|
arg@214
|
249 /* main event loop, also reads status text from stdin */
|
arg@242
|
250 XSync(dpy, False);
|
arg@292
|
251 procevent();
|
arg@302
|
252 readin = True;
|
garbeam@5
|
253 while(running) {
|
garbeam@59
|
254 FD_ZERO(&rd);
|
arg@164
|
255 if(readin)
|
arg@164
|
256 FD_SET(STDIN_FILENO, &rd);
|
arg@265
|
257 FD_SET(xfd, &rd);
|
arg@578
|
258 if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
|
arg@578
|
259 if(errno == EINTR)
|
arg@578
|
260 continue;
|
arg@581
|
261 eprint("select failed\n");
|
arg@578
|
262 }
|
arg@578
|
263 if(FD_ISSET(STDIN_FILENO, &rd)) {
|
arg@581
|
264 switch(r = read(STDIN_FILENO, stext, sizeof stext - 1)) {
|
arg@578
|
265 case -1:
|
arg@581
|
266 strncpy(stext, strerror(errno), sizeof stext - 1);
|
arg@583
|
267 stext[sizeof stext - 1] = '\0';
|
arg@578
|
268 readin = False;
|
arg@578
|
269 break;
|
arg@578
|
270 case 0:
|
arg@583
|
271 strncpy(stext, "EOF", 4);
|
arg@578
|
272 readin = False;
|
arg@578
|
273 break;
|
arg@578
|
274 default:
|
arg@582
|
275 for(stext[r] = '\0', p = stext + strlen(stext) - 1; p >= stext && *p == '\n'; *p-- = '\0');
|
arg@744
|
276 for(; p >= stext && *p != '\n'; --p);
|
arg@581
|
277 if(p > stext)
|
arg@581
|
278 strncpy(stext, p + 1, sizeof stext);
|
garbeam@59
|
279 }
|
arg@578
|
280 drawstatus();
|
garbeam@59
|
281 }
|
arg@578
|
282 if(FD_ISSET(xfd, &rd))
|
arg@578
|
283 procevent();
|
garbeam@5
|
284 }
|
garbeam@0
|
285 cleanup();
|
garbeam@0
|
286 XCloseDisplay(dpy);
|
garbeam@0
|
287 return 0;
|
garbeam@0
|
288 }
|