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@0
|
6 #include <stdarg.h>
|
garbeam@0
|
7 #include <stdio.h>
|
garbeam@0
|
8 #include <stdlib.h>
|
garbeam@0
|
9
|
garbeam@0
|
10 #include <X11/cursorfont.h>
|
garbeam@0
|
11 #include <X11/Xatom.h>
|
garbeam@0
|
12 #include <X11/Xproto.h>
|
garbeam@0
|
13
|
garbeam@0
|
14 #include "wm.h"
|
garbeam@0
|
15
|
garbeam@31
|
16 /********** CUSTOMIZE **********/
|
garbeam@31
|
17
|
garbeam@31
|
18 char *tags[TLast] = {
|
garbeam@31
|
19 [Tscratch] = "scratch",
|
garbeam@31
|
20 [Tdev] = "dev",
|
garbeam@31
|
21 [Tirc] = "irc",
|
garbeam@31
|
22 [Twww] = "www",
|
garbeam@31
|
23 [Twork] = "work",
|
garbeam@31
|
24 };
|
garbeam@31
|
25
|
garbeam@31
|
26 /********** CUSTOMIZE **********/
|
garbeam@31
|
27
|
garbeam@3
|
28 /* X structs */
|
garbeam@0
|
29 Display *dpy;
|
garbeam@5
|
30 Window root, barwin;
|
garbeam@13
|
31 Atom wm_atom[WMLast], net_atom[NetLast];
|
garbeam@0
|
32 Cursor cursor[CurLast];
|
garbeam@5
|
33 Bool running = True;
|
garbeam@31
|
34 Bool issel;
|
garbeam@3
|
35
|
garbeam@31
|
36 char stext[1024];
|
garbeam@31
|
37 int tsel = Tdev; /* default tag */
|
garbeam@32
|
38 int screen, sx, sy, sw, sh, th;
|
garbeam@3
|
39
|
garbeam@34
|
40 DC dc = {0};
|
garbeam@10
|
41 Client *clients = NULL;
|
garbeam@13
|
42 Client *stack = NULL;
|
garbeam@0
|
43
|
garbeam@0
|
44 static Bool other_wm_running;
|
garbeam@31
|
45 static const char version[] =
|
garbeam@34
|
46 "dwm - " VERSION ", (C)opyright MMVI Anselm R. Garbe\n";
|
garbeam@0
|
47 static int (*x_error_handler) (Display *, XErrorEvent *);
|
garbeam@0
|
48
|
garbeam@0
|
49 static void
|
garbeam@34
|
50 usage() { error("usage: dwm [-v]\n"); }
|
garbeam@0
|
51
|
garbeam@0
|
52 static void
|
garbeam@0
|
53 scan_wins()
|
garbeam@0
|
54 {
|
garbeam@0
|
55 unsigned int i, num;
|
garbeam@0
|
56 Window *wins;
|
garbeam@0
|
57 XWindowAttributes wa;
|
garbeam@0
|
58 Window d1, d2;
|
garbeam@0
|
59
|
garbeam@0
|
60 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
|
garbeam@0
|
61 for(i = 0; i < num; i++) {
|
garbeam@0
|
62 if(!XGetWindowAttributes(dpy, wins[i], &wa))
|
garbeam@0
|
63 continue;
|
garbeam@0
|
64 if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
|
garbeam@0
|
65 continue;
|
garbeam@0
|
66 if(wa.map_state == IsViewable)
|
garbeam@10
|
67 manage(wins[i], &wa);
|
garbeam@0
|
68 }
|
garbeam@0
|
69 }
|
garbeam@0
|
70 if(wins)
|
garbeam@0
|
71 XFree(wins);
|
garbeam@0
|
72 }
|
garbeam@0
|
73
|
garbeam@13
|
74 static int
|
garbeam@13
|
75 win_property(Window w, Atom a, Atom t, long l, unsigned char **prop)
|
garbeam@13
|
76 {
|
garbeam@13
|
77 Atom real;
|
garbeam@13
|
78 int format;
|
garbeam@13
|
79 unsigned long res, extra;
|
garbeam@13
|
80 int status;
|
garbeam@13
|
81
|
garbeam@13
|
82 status = XGetWindowProperty(dpy, w, a, 0L, l, False, t, &real, &format,
|
garbeam@13
|
83 &res, &extra, prop);
|
garbeam@13
|
84
|
garbeam@13
|
85 if(status != Success || *prop == 0) {
|
garbeam@13
|
86 return 0;
|
garbeam@13
|
87 }
|
garbeam@13
|
88 if(res == 0) {
|
garbeam@13
|
89 free((void *) *prop);
|
garbeam@13
|
90 }
|
garbeam@13
|
91 return res;
|
garbeam@13
|
92 }
|
garbeam@13
|
93
|
garbeam@13
|
94 int
|
garbeam@13
|
95 win_proto(Window w)
|
garbeam@13
|
96 {
|
garbeam@30
|
97 unsigned char *protocols;
|
garbeam@13
|
98 long res;
|
garbeam@13
|
99 int protos = 0;
|
garbeam@13
|
100 int i;
|
garbeam@13
|
101
|
garbeam@30
|
102 res = win_property(w, wm_atom[WMProtocols], XA_ATOM, 20L, &protocols);
|
garbeam@13
|
103 if(res <= 0) {
|
garbeam@13
|
104 return protos;
|
garbeam@13
|
105 }
|
garbeam@13
|
106 for(i = 0; i < res; i++) {
|
garbeam@13
|
107 if(protocols[i] == wm_atom[WMDelete])
|
garbeam@13
|
108 protos |= WM_PROTOCOL_DELWIN;
|
garbeam@13
|
109 }
|
garbeam@13
|
110 free((char *) protocols);
|
garbeam@13
|
111 return protos;
|
garbeam@13
|
112 }
|
garbeam@13
|
113
|
garbeam@13
|
114 void
|
garbeam@13
|
115 send_message(Window w, Atom a, long value)
|
garbeam@13
|
116 {
|
garbeam@13
|
117 XEvent e;
|
garbeam@13
|
118
|
garbeam@13
|
119 e.type = ClientMessage;
|
garbeam@13
|
120 e.xclient.window = w;
|
garbeam@13
|
121 e.xclient.message_type = a;
|
garbeam@13
|
122 e.xclient.format = 32;
|
garbeam@13
|
123 e.xclient.data.l[0] = value;
|
garbeam@13
|
124 e.xclient.data.l[1] = CurrentTime;
|
garbeam@13
|
125 XSendEvent(dpy, w, False, NoEventMask, &e);
|
garbeam@13
|
126 XFlush(dpy);
|
garbeam@13
|
127 }
|
garbeam@13
|
128
|
garbeam@0
|
129 /*
|
garbeam@0
|
130 * There's no way to check accesses to destroyed windows, thus
|
garbeam@0
|
131 * those cases are ignored (especially on UnmapNotify's).
|
garbeam@0
|
132 * Other types of errors call Xlib's default error handler, which
|
garbeam@0
|
133 * calls exit().
|
garbeam@0
|
134 */
|
garbeam@10
|
135 int
|
garbeam@0
|
136 error_handler(Display *dpy, XErrorEvent *error)
|
garbeam@0
|
137 {
|
garbeam@0
|
138 if(error->error_code == BadWindow
|
garbeam@0
|
139 || (error->request_code == X_SetInputFocus
|
garbeam@0
|
140 && error->error_code == BadMatch)
|
garbeam@0
|
141 || (error->request_code == X_PolyText8
|
garbeam@0
|
142 && error->error_code == BadDrawable)
|
garbeam@0
|
143 || (error->request_code == X_PolyFillRectangle
|
garbeam@0
|
144 && error->error_code == BadDrawable)
|
garbeam@0
|
145 || (error->request_code == X_PolySegment
|
garbeam@0
|
146 && error->error_code == BadDrawable)
|
garbeam@0
|
147 || (error->request_code == X_ConfigureWindow
|
garbeam@0
|
148 && error->error_code == BadMatch)
|
garbeam@0
|
149 || (error->request_code == X_GrabKey
|
garbeam@0
|
150 && error->error_code == BadAccess))
|
garbeam@0
|
151 return 0;
|
garbeam@34
|
152 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
|
garbeam@0
|
153 error->request_code, error->error_code);
|
garbeam@0
|
154 return x_error_handler(dpy, error); /* may call exit() */
|
garbeam@0
|
155 }
|
garbeam@0
|
156
|
garbeam@0
|
157 /*
|
garbeam@0
|
158 * Startup Error handler to check if another window manager
|
garbeam@0
|
159 * is already running.
|
garbeam@0
|
160 */
|
garbeam@0
|
161 static int
|
garbeam@0
|
162 startup_error_handler(Display *dpy, XErrorEvent *error)
|
garbeam@0
|
163 {
|
garbeam@0
|
164 other_wm_running = True;
|
garbeam@0
|
165 return -1;
|
garbeam@0
|
166 }
|
garbeam@0
|
167
|
garbeam@0
|
168 static void
|
garbeam@0
|
169 cleanup()
|
garbeam@0
|
170 {
|
garbeam@14
|
171 while(clients)
|
garbeam@14
|
172 unmanage(clients);
|
garbeam@0
|
173 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
|
garbeam@0
|
174 }
|
garbeam@0
|
175
|
garbeam@27
|
176 void
|
garbeam@27
|
177 quit(void *aux)
|
garbeam@27
|
178 {
|
garbeam@27
|
179 running = False;
|
garbeam@27
|
180 }
|
garbeam@27
|
181
|
garbeam@0
|
182 int
|
garbeam@0
|
183 main(int argc, char *argv[])
|
garbeam@0
|
184 {
|
garbeam@0
|
185 int i;
|
garbeam@0
|
186 XSetWindowAttributes wa;
|
garbeam@0
|
187 unsigned int mask;
|
garbeam@0
|
188 Window w;
|
garbeam@5
|
189 XEvent ev;
|
garbeam@0
|
190
|
garbeam@0
|
191 /* command line args */
|
garbeam@0
|
192 for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) {
|
garbeam@0
|
193 switch (argv[i][1]) {
|
garbeam@0
|
194 case 'v':
|
garbeam@0
|
195 fprintf(stdout, "%s", version);
|
garbeam@0
|
196 exit(0);
|
garbeam@0
|
197 break;
|
garbeam@0
|
198 default:
|
garbeam@0
|
199 usage();
|
garbeam@0
|
200 break;
|
garbeam@0
|
201 }
|
garbeam@0
|
202 }
|
garbeam@0
|
203
|
garbeam@0
|
204 dpy = XOpenDisplay(0);
|
garbeam@0
|
205 if(!dpy)
|
garbeam@34
|
206 error("dwm: cannot connect X server\n");
|
garbeam@0
|
207
|
garbeam@0
|
208 screen = DefaultScreen(dpy);
|
garbeam@0
|
209 root = RootWindow(dpy, screen);
|
garbeam@0
|
210
|
garbeam@0
|
211 /* check if another WM is already running */
|
garbeam@0
|
212 other_wm_running = False;
|
garbeam@0
|
213 XSetErrorHandler(startup_error_handler);
|
garbeam@0
|
214 /* this causes an error if some other WM is running */
|
garbeam@0
|
215 XSelectInput(dpy, root, SubstructureRedirectMask);
|
garbeam@3
|
216 XFlush(dpy);
|
garbeam@0
|
217
|
garbeam@0
|
218 if(other_wm_running)
|
garbeam@34
|
219 error("dwm: another window manager is already running\n");
|
garbeam@0
|
220
|
garbeam@26
|
221 sx = sy = 0;
|
garbeam@26
|
222 sw = DisplayWidth(dpy, screen);
|
garbeam@26
|
223 sh = DisplayHeight(dpy, screen);
|
garbeam@31
|
224 issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
|
garbeam@0
|
225
|
garbeam@0
|
226 XSetErrorHandler(0);
|
garbeam@0
|
227 x_error_handler = XSetErrorHandler(error_handler);
|
garbeam@0
|
228
|
garbeam@0
|
229 /* init atoms */
|
garbeam@13
|
230 wm_atom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
|
garbeam@13
|
231 wm_atom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
|
garbeam@0
|
232 net_atom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
|
garbeam@0
|
233 net_atom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
|
garbeam@0
|
234
|
garbeam@0
|
235 XChangeProperty(dpy, root, net_atom[NetSupported], XA_ATOM, 32,
|
garbeam@0
|
236 PropModeReplace, (unsigned char *) net_atom, NetLast);
|
garbeam@0
|
237
|
garbeam@0
|
238
|
garbeam@0
|
239 /* init cursors */
|
garbeam@0
|
240 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
|
garbeam@0
|
241 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
|
garbeam@0
|
242 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
|
garbeam@0
|
243
|
garbeam@8
|
244 update_keys();
|
garbeam@0
|
245
|
garbeam@5
|
246 /* style */
|
garbeam@34
|
247 initcolors(BGCOLOR, FGCOLOR, BORDERCOLOR);
|
garbeam@34
|
248 initfont(&dc.font, FONT);
|
garbeam@5
|
249
|
garbeam@34
|
250 th = texth(&dc.font);
|
garbeam@5
|
251
|
garbeam@34
|
252 dc.drawable = XCreatePixmap(dpy, root, sw, th, DefaultDepth(dpy, screen));
|
garbeam@34
|
253 dc.gc = XCreateGC(dpy, root, 0, 0);
|
garbeam@21
|
254
|
garbeam@9
|
255 wa.event_mask = SubstructureRedirectMask | EnterWindowMask \
|
garbeam@9
|
256 | LeaveWindowMask;
|
garbeam@0
|
257 wa.cursor = cursor[CurNormal];
|
garbeam@0
|
258 XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
|
garbeam@0
|
259
|
garbeam@5
|
260 scan_wins();
|
garbeam@3
|
261
|
garbeam@5
|
262 while(running) {
|
garbeam@32
|
263 XNextEvent(dpy, &ev);
|
garbeam@32
|
264 if(handler[ev.type])
|
garbeam@32
|
265 (handler[ev.type])(&ev); /* call handler */
|
garbeam@5
|
266 }
|
garbeam@0
|
267
|
garbeam@0
|
268 cleanup();
|
garbeam@0
|
269 XCloseDisplay(dpy);
|
garbeam@0
|
270
|
garbeam@0
|
271 return 0;
|
garbeam@0
|
272 }
|