dwm-meillo

view main.c @ 616:f617c75a8405

improved the memory leak prevention
author arg@mig29
date Wed, 06 Dec 2006 11:19:06 +0100
parents f4285a97e3e6
children bda4880c462d
line source
1 /* (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
2 * See LICENSE file for license details.
3 */
5 #include "dwm.h"
6 #include <errno.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <sys/select.h>
12 #include <X11/cursorfont.h>
13 #include <X11/keysym.h>
14 #include <X11/Xatom.h>
15 #include <X11/Xproto.h>
17 /* extern */
19 char stext[1024];
20 Bool *seltag;
21 int bx, by, bw, bh, bmw, masterd, screen, sx, sy, sw, sh, wax, way, waw, wah;
22 unsigned int master, ntags, numlockmask;
23 Atom wmatom[WMLast], netatom[NetLast];
24 Bool running = True;
25 Bool issel = True;
26 Client *clients = NULL;
27 Client *sel = NULL;
28 Client *stack = NULL;
29 Cursor cursor[CurLast];
30 Display *dpy;
31 DC dc = {0};
32 Window root, barwin;
34 /* static */
36 static int (*xerrorxlib)(Display *, XErrorEvent *);
37 static Bool otherwm, readin;
39 static void
40 cleanup(void) {
41 close(STDIN_FILENO);
42 while(sel) {
43 resize(sel, True, TopLeft);
44 unmanage(sel);
45 }
46 if(dc.font.set)
47 XFreeFontSet(dpy, dc.font.set);
48 else
49 XFreeFont(dpy, dc.font.xfont);
50 XUngrabKey(dpy, AnyKey, AnyModifier, root);
51 XFreePixmap(dpy, dc.drawable);
52 XFreeGC(dpy, dc.gc);
53 XDestroyWindow(dpy, barwin);
54 XFreeCursor(dpy, cursor[CurNormal]);
55 XFreeCursor(dpy, cursor[CurResize]);
56 XFreeCursor(dpy, cursor[CurMove]);
57 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
58 XSync(dpy, False);
59 free(seltag);
60 }
62 static void
63 scan(void) {
64 unsigned int i, num;
65 Window *wins, d1, d2;
66 XWindowAttributes wa;
68 wins = NULL;
69 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
70 for(i = 0; i < num; i++) {
71 if(!XGetWindowAttributes(dpy, wins[i], &wa))
72 continue;
73 if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
74 continue;
75 if(wa.map_state == IsViewable)
76 manage(wins[i], &wa);
77 }
78 }
79 if(wins)
80 XFree(wins);
81 }
83 static void
84 setup(void) {
85 int i, j;
86 unsigned int mask;
87 Window w;
88 XModifierKeymap *modmap;
89 XSetWindowAttributes wa;
91 /* init atoms */
92 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
93 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
94 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
95 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
96 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
97 PropModeReplace, (unsigned char *) netatom, NetLast);
98 /* init cursors */
99 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
100 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
101 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
102 /* init modifier map */
103 modmap = XGetModifierMapping(dpy);
104 for (i = 0; i < 8; i++) {
105 for (j = 0; j < modmap->max_keypermod; j++) {
106 if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock))
107 numlockmask = (1 << i);
108 }
109 }
110 XFreeModifiermap(modmap);
111 /* select for events */
112 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
113 | EnterWindowMask | LeaveWindowMask;
114 wa.cursor = cursor[CurNormal];
115 XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
116 grabkeys();
117 initrregs();
118 for(ntags = 0; tags[ntags]; ntags++);
119 seltag = emallocz(sizeof(Bool) * ntags);
120 seltag[0] = True;
121 /* style */
122 dc.norm[ColBG] = getcolor(NORMBGCOLOR);
123 dc.norm[ColFG] = getcolor(NORMFGCOLOR);
124 dc.sel[ColBG] = getcolor(SELBGCOLOR);
125 dc.sel[ColFG] = getcolor(SELFGCOLOR);
126 dc.status[ColBG] = getcolor(STATUSBGCOLOR);
127 dc.status[ColFG] = getcolor(STATUSFGCOLOR);
128 setfont(FONT);
129 /* geometry */
130 bmw = textw(TILESYMBOL) > textw(FLOATSYMBOL) ? textw(TILESYMBOL) : textw(FLOATSYMBOL);
131 sx = sy = 0;
132 sw = DisplayWidth(dpy, screen);
133 sh = DisplayHeight(dpy, screen);
134 master = MASTER;
135 /* bar */
136 bx = sx;
137 by = sy;
138 bw = sw;
139 dc.h = bh = dc.font.height + 2;
140 wa.override_redirect = 1;
141 wa.background_pixmap = ParentRelative;
142 wa.event_mask = ButtonPressMask | ExposureMask;
143 barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen),
144 CopyFromParent, DefaultVisual(dpy, screen),
145 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
146 XDefineCursor(dpy, barwin, cursor[CurNormal]);
147 XMapRaised(dpy, barwin);
148 strcpy(stext, "dwm-"VERSION);
149 /* windowarea */
150 wax = sx;
151 way = sy + bh;
152 wah = sh - bh;
153 waw = sw;
154 /* pixmap for everything */
155 dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
156 dc.gc = XCreateGC(dpy, root, 0, 0);
157 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
158 /* multihead support */
159 issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
160 }
162 /*
163 * Startup Error handler to check if another window manager
164 * is already running.
165 */
166 static int
167 xerrorstart(Display *dsply, XErrorEvent *ee) {
168 otherwm = True;
169 return -1;
170 }
172 /* extern */
174 int
175 getproto(Window w) {
176 int i, format, protos, status;
177 unsigned long extra, res;
178 Atom *protocols, real;
180 protos = 0;
181 status = XGetWindowProperty(dpy, w, wmatom[WMProtocols], 0L, 20L, False,
182 XA_ATOM, &real, &format, &res, &extra, (unsigned char **)&protocols);
183 if(status != Success || protocols == 0)
184 return protos;
185 for(i = 0; i < res; i++)
186 if(protocols[i] == wmatom[WMDelete])
187 protos |= PROTODELWIN;
188 free(protocols);
189 return protos;
190 }
192 void
193 sendevent(Window w, Atom a, long value) {
194 XEvent e;
196 e.type = ClientMessage;
197 e.xclient.window = w;
198 e.xclient.message_type = a;
199 e.xclient.format = 32;
200 e.xclient.data.l[0] = value;
201 e.xclient.data.l[1] = CurrentTime;
202 XSendEvent(dpy, w, False, NoEventMask, &e);
203 XSync(dpy, False);
204 }
206 void
207 quit(Arg *arg) {
208 readin = running = False;
209 }
211 /* There's no way to check accesses to destroyed windows, thus those cases are
212 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
213 * default error handler, which may call exit.
214 */
215 int
216 xerror(Display *dpy, XErrorEvent *ee) {
217 if(ee->error_code == BadWindow
218 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
219 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
220 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
221 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
222 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
223 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
224 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
225 return 0;
226 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
227 ee->request_code, ee->error_code);
228 return xerrorxlib(dpy, ee); /* may call exit */
229 }
231 int
232 main(int argc, char *argv[]) {
233 char *p;
234 int r, xfd;
235 fd_set rd;
237 if(argc == 2 && !strncmp("-v", argv[1], 3)) {
238 fputs("dwm-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout);
239 exit(EXIT_SUCCESS);
240 }
241 else if(argc != 1)
242 eprint("usage: dwm [-v]\n");
243 dpy = XOpenDisplay(0);
244 if(!dpy)
245 eprint("dwm: cannot open display\n");
246 xfd = ConnectionNumber(dpy);
247 screen = DefaultScreen(dpy);
248 root = RootWindow(dpy, screen);
249 otherwm = False;
250 XSetErrorHandler(xerrorstart);
251 /* this causes an error if some other window manager is running */
252 XSelectInput(dpy, root, SubstructureRedirectMask);
253 XSync(dpy, False);
254 if(otherwm)
255 eprint("dwm: another window manager is already running\n");
257 XSync(dpy, False);
258 XSetErrorHandler(NULL);
259 xerrorxlib = XSetErrorHandler(xerror);
260 XSync(dpy, False);
261 setup();
262 drawstatus();
263 scan();
265 /* main event loop, also reads status text from stdin */
266 XSync(dpy, False);
267 procevent();
268 readin = True;
269 while(running) {
270 FD_ZERO(&rd);
271 if(readin)
272 FD_SET(STDIN_FILENO, &rd);
273 FD_SET(xfd, &rd);
274 if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
275 if(errno == EINTR)
276 continue;
277 eprint("select failed\n");
278 }
279 if(FD_ISSET(STDIN_FILENO, &rd)) {
280 switch(r = read(STDIN_FILENO, stext, sizeof stext - 1)) {
281 case -1:
282 strncpy(stext, strerror(errno), sizeof stext - 1);
283 stext[sizeof stext - 1] = '\0';
284 readin = False;
285 break;
286 case 0:
287 strncpy(stext, "EOF", 4);
288 readin = False;
289 break;
290 default:
291 for(stext[r] = '\0', p = stext + strlen(stext) - 1; p >= stext && *p == '\n'; *p-- = '\0');
292 for(p = stext + strlen(stext) - 1; p >= stext && *p != '\n'; --p);
293 if(p > stext)
294 strncpy(stext, p + 1, sizeof stext);
295 }
296 drawstatus();
297 }
298 if(FD_ISSET(xfd, &rd))
299 procevent();
300 }
301 cleanup();
302 XCloseDisplay(dpy);
303 return 0;
304 }