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