dwm-meillo

view main.c @ 289:6562340b9ffc

applied viewsel.patch
author Anselm R.Garbe <arg@10ksloc.org>
date Mon, 14 Aug 2006 18:46:07 +0200
parents c1187ddcd7f7
children 8e6e0aa5e2ae
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 Bool *seltag;
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 Atom *protocols;
107 res = win_property(w, wmatom[WMProtocols], XA_ATOM, 20L,
108 ((unsigned char **)&protocols));
109 if(res <= 0) {
110 return protos;
111 }
112 for(i = 0; i < res; i++) {
113 if(protocols[i] == wmatom[WMDelete])
114 protos |= PROTODELWIN;
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, xfd;
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 open display\n");
184 xfd = ConnectionNumber(dpy);
185 screen = DefaultScreen(dpy);
186 root = RootWindow(dpy, screen);
188 otherwm = False;
189 XSetErrorHandler(xerrorstart);
190 /* this causes an error if some other window manager 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);
199 XSync(dpy, False);
201 /* init atoms */
202 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
203 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
204 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
205 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
206 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
207 PropModeReplace, (unsigned char *) netatom, NetLast);
209 /* init cursors */
210 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
211 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
212 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
214 wa.event_mask = SubstructureRedirectMask | EnterWindowMask | LeaveWindowMask;
215 wa.cursor = cursor[CurNormal];
216 XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
218 grabkeys();
219 initrregs();
221 for(ntags = 0; tags[ntags]; ntags++);
222 seltag = emallocz(sizeof(Bool) * ntags);
223 seltag[DEFTAG] = True;
225 /* style */
226 dc.bg = getcolor(BGCOLOR);
227 dc.fg = getcolor(FGCOLOR);
228 dc.border = getcolor(BORDERCOLOR);
229 setfont(FONT);
231 sx = sy = 0;
232 sw = DisplayWidth(dpy, screen);
233 sh = DisplayHeight(dpy, screen);
234 mw = (sw * MASTERW) / 100;
236 wa.override_redirect = 1;
237 wa.background_pixmap = ParentRelative;
238 wa.event_mask = ButtonPressMask | ExposureMask;
240 bx = by = 0;
241 bw = sw;
242 dc.h = bh = dc.font.height + 4;
243 barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen),
244 CopyFromParent, DefaultVisual(dpy, screen),
245 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
246 XDefineCursor(dpy, barwin, cursor[CurNormal]);
247 XMapRaised(dpy, barwin);
249 dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
250 dc.gc = XCreateGC(dpy, root, 0, 0);
252 strcpy(stext, "dwm-"VERSION);
253 drawstatus();
255 issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
257 scan();
259 /* main event loop, also reads status text from stdin */
260 XSync(dpy, False);
261 while(running) {
262 FD_ZERO(&rd);
263 if(readin)
264 FD_SET(STDIN_FILENO, &rd);
265 FD_SET(xfd, &rd);
266 i = select(xfd + 1, &rd, NULL, NULL, NULL);
267 if(i == -1 && errno == EINTR)
268 continue;
269 if(i < 0)
270 eprint("select failed\n");
271 else if(i > 0) {
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 if(FD_ISSET(xfd, &rd)) {
281 while(XPending(dpy)) {
282 XNextEvent(dpy, &ev);
283 if(handler[ev.type])
284 (handler[ev.type])(&ev); /* call handler */
285 }
286 }
287 }
288 }
289 cleanup();
290 XCloseDisplay(dpy);
292 return 0;
293 }