aewl

view main.c @ 62:0a4b066ce254

focus on view change as well
author Anselm R. Garbe <garbeam@wmii.de>
date Fri, 14 Jul 2006 12:11:24 +0200
parents 24f9c674d03f
children 46d44d185464
line source
1 /*
2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
3 * See LICENSE file for license details.
4 */
6 #include <errno.h>
7 #include <stdarg.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
13 #include <X11/cursorfont.h>
14 #include <X11/Xatom.h>
15 #include <X11/Xproto.h>
17 #include "dwm.h"
19 /********** CUSTOMIZE **********/
21 char *tags[TLast] = {
22 [Tscratch] = "scratch",
23 [Tdev] = "dev",
24 [Twww] = "www",
25 [Twork] = "work",
26 };
28 /********** CUSTOMIZE **********/
30 /* X structs */
31 Display *dpy;
32 Window root, barwin;
33 Atom wm_atom[WMLast], net_atom[NetLast];
34 Cursor cursor[CurLast];
35 Bool running = True;
36 Bool issel;
38 int tsel = Tdev; /* default tag */
39 int screen, sx, sy, sw, sh, bx, by, bw, bh, mw;
40 char stext[1024];
42 DC dc = {0};
43 Client *clients = NULL;
44 Client *sel = NULL;
46 static Bool other_wm_running;
47 static const char version[] =
48 "dwm-" VERSION ", (C)opyright MMVI Anselm R. Garbe\n";
49 static int (*x_error_handler) (Display *, XErrorEvent *);
51 static void
52 usage() { error("usage: dwm [-v]\n"); }
54 static void
55 scan_wins()
56 {
57 unsigned int i, num;
58 Window *wins;
59 XWindowAttributes wa;
60 Window d1, d2;
62 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
63 for(i = 0; i < num; i++) {
64 if(!XGetWindowAttributes(dpy, wins[i], &wa))
65 continue;
66 if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
67 continue;
68 if(wa.map_state == IsViewable)
69 manage(wins[i], &wa);
70 }
71 }
72 if(wins)
73 XFree(wins);
74 }
76 static int
77 win_property(Window w, Atom a, Atom t, long l, unsigned char **prop)
78 {
79 Atom real;
80 int format;
81 unsigned long res, extra;
82 int status;
84 status = XGetWindowProperty(dpy, w, a, 0L, l, False, t, &real, &format,
85 &res, &extra, prop);
87 if(status != Success || *prop == 0) {
88 return 0;
89 }
90 if(res == 0) {
91 free((void *) *prop);
92 }
93 return res;
94 }
96 int
97 win_proto(Window w)
98 {
99 unsigned char *protocols;
100 long res;
101 int protos = 0;
102 int i;
104 res = win_property(w, wm_atom[WMProtocols], XA_ATOM, 20L, &protocols);
105 if(res <= 0) {
106 return protos;
107 }
108 for(i = 0; i < res; i++) {
109 if(protocols[i] == wm_atom[WMDelete])
110 protos |= WM_PROTOCOL_DELWIN;
111 }
112 free((char *) protocols);
113 return protos;
114 }
116 void
117 send_message(Window w, Atom a, long value)
118 {
119 XEvent e;
121 e.type = ClientMessage;
122 e.xclient.window = w;
123 e.xclient.message_type = a;
124 e.xclient.format = 32;
125 e.xclient.data.l[0] = value;
126 e.xclient.data.l[1] = CurrentTime;
127 XSendEvent(dpy, w, False, NoEventMask, &e);
128 XFlush(dpy);
129 }
131 /*
132 * There's no way to check accesses to destroyed windows, thus
133 * those cases are ignored (especially on UnmapNotify's).
134 * Other types of errors call Xlib's default error handler, which
135 * calls exit().
136 */
137 int
138 error_handler(Display *dpy, XErrorEvent *error)
139 {
140 if(error->error_code == BadWindow
141 || (error->request_code == X_SetInputFocus
142 && error->error_code == BadMatch)
143 || (error->request_code == X_PolyText8
144 && error->error_code == BadDrawable)
145 || (error->request_code == X_PolyFillRectangle
146 && error->error_code == BadDrawable)
147 || (error->request_code == X_PolySegment
148 && error->error_code == BadDrawable)
149 || (error->request_code == X_ConfigureWindow
150 && error->error_code == BadMatch)
151 || (error->request_code == X_GrabKey
152 && error->error_code == BadAccess))
153 return 0;
154 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
155 error->request_code, error->error_code);
156 return x_error_handler(dpy, error); /* may call exit() */
157 }
159 /*
160 * Startup Error handler to check if another window manager
161 * is already running.
162 */
163 static int
164 startup_error_handler(Display *dpy, XErrorEvent *error)
165 {
166 other_wm_running = True;
167 return -1;
168 }
170 static void
171 cleanup()
172 {
173 while(sel) {
174 resize(sel, True);
175 unmanage(sel);
176 }
177 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
178 }
180 void
181 quit(Arg *arg)
182 {
183 running = False;
184 }
186 int
187 main(int argc, char *argv[])
188 {
189 int i, n;
190 fd_set rd;
191 XSetWindowAttributes wa;
192 unsigned int mask;
193 Window w;
194 XEvent ev;
196 for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) {
197 switch (argv[i][1]) {
198 case 'v':
199 fprintf(stdout, "%s", version);
200 exit(0);
201 break;
202 default:
203 usage();
204 break;
205 }
206 }
208 dpy = XOpenDisplay(0);
209 if(!dpy)
210 error("dwm: cannot connect X server\n");
212 screen = DefaultScreen(dpy);
213 root = RootWindow(dpy, screen);
215 /* check if another WM is already running */
216 other_wm_running = False;
217 XSetErrorHandler(startup_error_handler);
218 /* this causes an error if some other WM is running */
219 XSelectInput(dpy, root, SubstructureRedirectMask);
220 XFlush(dpy);
222 if(other_wm_running)
223 error("dwm: another window manager is already running\n");
225 XSetErrorHandler(0);
226 x_error_handler = XSetErrorHandler(error_handler);
228 /* init atoms */
229 wm_atom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
230 wm_atom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
231 net_atom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
232 net_atom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
233 XChangeProperty(dpy, root, net_atom[NetSupported], XA_ATOM, 32,
234 PropModeReplace, (unsigned char *) net_atom, NetLast);
236 /* init cursors */
237 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
238 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
239 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
241 update_keys();
243 /* style */
244 dc.bg = initcolor(BGCOLOR);
245 dc.fg = initcolor(FGCOLOR);
246 dc.border = initcolor(BORDERCOLOR);
247 initfont(FONT);
249 sx = sy = 0;
250 sw = DisplayWidth(dpy, screen);
251 sh = DisplayHeight(dpy, screen);
252 mw = (sw * MASTERW) / 100;
254 wa.override_redirect = 1;
255 wa.background_pixmap = ParentRelative;
256 wa.event_mask = ButtonPressMask | ExposureMask;
258 bx = by = 0;
259 bw = sw;
260 dc.h = bh = dc.font.height + 4;
261 barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen),
262 CopyFromParent, DefaultVisual(dpy, screen),
263 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
264 XDefineCursor(dpy, barwin, cursor[CurNormal]);
265 XMapRaised(dpy, barwin);
267 dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
268 dc.gc = XCreateGC(dpy, root, 0, 0);
269 draw_bar();
271 issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
273 wa.event_mask = SubstructureRedirectMask | EnterWindowMask \
274 | LeaveWindowMask;
275 wa.cursor = cursor[CurNormal];
277 XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
279 strcpy(stext, "dwm-"VERSION);
280 scan_wins();
282 /* main event loop, reads status text from stdin as well */
283 Mainloop:
284 while(running) {
285 FD_ZERO(&rd);
286 FD_SET(0, &rd);
287 FD_SET(ConnectionNumber(dpy), &rd);
289 i = select(ConnectionNumber(dpy) + 1, &rd, 0, 0, 0);
290 if(i == -1 && errno == EINTR)
291 continue;
292 if(i < 0)
293 error("select failed\n");
294 else if(i > 0) {
295 if(FD_ISSET(ConnectionNumber(dpy), &rd) && XPending(dpy) > 0) {
296 XNextEvent(dpy, &ev);
297 if(handler[ev.type])
298 (handler[ev.type])(&ev); /* call handler */
299 }
300 if(FD_ISSET(0, &rd)) {
301 i = n = 0;
302 for(;;) {
303 if((i = getchar()) == EOF) {
304 stext[0] = 0;
305 goto Mainloop;
306 }
307 if(i == '\n' || n >= sizeof(stext) - 1)
308 break;
309 stext[n++] = i;
310 }
311 stext[n] = 0;
312 draw_bar();
313 }
314 }
315 }
317 cleanup();
318 XCloseDisplay(dpy);
320 return 0;
321 }