aewl

view main.c @ 178:e848966a1ac6

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