dwm-meillo

view main.c @ 137:77922a389fa8

simplified main.c, switching back to single urxvt usage
author arg@10ksloc.org
date Fri, 21 Jul 2006 14:11:38 +0200
parents 1489f6b97714
children c1185dc7a36e
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 <X11/cursorfont.h>
14 #include <X11/Xatom.h>
15 #include <X11/Xproto.h>
18 /* static */
20 static int (*xerrorxlib)(Display *, XErrorEvent *);
21 static Bool otherwm;
23 static void
24 cleanup()
25 {
26 while(sel) {
27 resize(sel, True, TopLeft);
28 unmanage(sel);
29 }
30 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
31 }
33 static void
34 scan()
35 {
36 unsigned int i, num;
37 Window *wins, d1, d2;
38 XWindowAttributes wa;
40 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
41 for(i = 0; i < num; i++) {
42 if(!XGetWindowAttributes(dpy, wins[i], &wa))
43 continue;
44 if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
45 continue;
46 if(wa.map_state == IsViewable)
47 manage(wins[i], &wa);
48 }
49 }
50 if(wins)
51 XFree(wins);
52 }
54 static int
55 win_property(Window w, Atom a, Atom t, long l, unsigned char **prop)
56 {
57 int status, format;
58 unsigned long res, extra;
59 Atom real;
61 status = XGetWindowProperty(dpy, w, a, 0L, l, False, t, &real, &format,
62 &res, &extra, prop);
64 if(status != Success || *prop == 0) {
65 return 0;
66 }
67 if(res == 0) {
68 free((void *) *prop);
69 }
70 return res;
71 }
73 /*
74 * Startup Error handler to check if another window manager
75 * is already running.
76 */
77 static int
78 xerrorstart(Display *dsply, XErrorEvent *ee)
79 {
80 otherwm = True;
81 return -1;
82 }
84 /* extern */
86 char stext[1024];
87 /* CUSTOMIZE */
88 int tsel = Tdev; /* default tag */
89 /* END CUSTOMIZE */
90 int screen, sx, sy, sw, sh, bx, by, bw, bh, mw;
91 Atom wmatom[WMLast], netatom[NetLast];
92 Bool running = True;
93 Bool issel = True;
94 Client *clients = NULL;
95 Client *sel = NULL;
96 Cursor cursor[CurLast];
97 Display *dpy;
98 DC dc = {0};
99 Window root, barwin;
101 int
102 getproto(Window w)
103 {
104 int protos = 0;
105 int i;
106 long res;
107 unsigned char *protocols;
109 res = win_property(w, wmatom[WMProtocols], XA_ATOM, 20L, &protocols);
110 if(res <= 0) {
111 return protos;
112 }
113 for(i = 0; i < res; i++) {
114 if(protocols[i] == wmatom[WMDelete])
115 protos |= WM_PROTOCOL_DELWIN;
116 }
117 free((char *) protocols);
118 return protos;
119 }
121 void
122 sendevent(Window w, Atom a, long value)
123 {
124 XEvent e;
126 e.type = ClientMessage;
127 e.xclient.window = w;
128 e.xclient.message_type = a;
129 e.xclient.format = 32;
130 e.xclient.data.l[0] = value;
131 e.xclient.data.l[1] = CurrentTime;
132 XSendEvent(dpy, w, False, NoEventMask, &e);
133 XSync(dpy, False);
134 }
136 void
137 quit(Arg *arg)
138 {
139 running = False;
140 }
142 /*
143 * There's no way to check accesses to destroyed windows, thus those cases are
144 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
145 * default error handler, which calls exit().
146 */
147 int
148 xerror(Display *dpy, XErrorEvent *ee)
149 {
150 if(ee->error_code == BadWindow
151 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
152 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
153 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
154 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
155 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
156 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess))
157 return 0;
158 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
159 ee->request_code, ee->error_code);
160 return xerrorxlib(dpy, ee); /* may call exit() */
161 }
163 int
164 main(int argc, char *argv[])
165 {
166 int i, n;
167 unsigned int mask;
168 fd_set rd;
169 Bool readin = True;
170 Window w;
171 XEvent ev;
172 XSetWindowAttributes wa;
174 if(argc == 2 && !strncmp("-v", argv[1], 3)) {
175 fputs("dwm-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout);
176 exit(EXIT_SUCCESS);
177 }
178 else if(argc != 1)
179 eprint("usage: dwm [-v]\n");
181 dpy = XOpenDisplay(0);
182 if(!dpy)
183 eprint("dwm: cannot connect X server\n");
185 screen = DefaultScreen(dpy);
186 root = RootWindow(dpy, screen);
188 /* check if another WM is already running */
189 otherwm = False;
190 XSetErrorHandler(xerrorstart);
191 /* this causes an error if some other WM is running */
192 XSelectInput(dpy, root, SubstructureRedirectMask);
193 XSync(dpy, False);
195 if(otherwm)
196 eprint("dwm: another window manager is already running\n");
198 XSetErrorHandler(NULL);
199 xerrorxlib = XSetErrorHandler(xerror);
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 grabkeys();
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 }