aewl

view main.c @ 150:a26b32ff8911

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