dwm-meillo

view main.c @ 463:e93b0ad5aeb5

removed useless inclusion
author Anselm R. Garbe <arg@10kloc.org>
date Thu, 14 Sep 2006 11:07:44 +0200
parents 81fcd7ddafee
children 2d8af0d7920d
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/keysym.h>
15 #include <X11/Xatom.h>
16 #include <X11/Xproto.h>
18 /* extern */
20 char stext[1024];
21 Bool *seltag;
22 int bx, by, bw, bh, bmw, mw, screen, sx, sy, sw, sh;
23 unsigned int ntags, numlockmask;
24 Atom wmatom[WMLast], netatom[NetLast];
25 Bool running = True;
26 Bool issel = True;
27 Bool maximized = False;
28 Client *clients = NULL;
29 Client *sel = NULL;
30 Client *stack = NULL;
31 Cursor cursor[CurLast];
32 Display *dpy;
33 DC dc = {0};
34 Window root, barwin;
36 /* static */
38 static int (*xerrorxlib)(Display *, XErrorEvent *);
39 static Bool otherwm, readin;
41 static void
42 cleanup() {
43 close(STDIN_FILENO);
44 while(sel) {
45 resize(sel, True, TopLeft);
46 unmanage(sel);
47 }
48 if(dc.font.set)
49 XFreeFontSet(dpy, dc.font.set);
50 else
51 XFreeFont(dpy, dc.font.xfont);
52 XUngrabKey(dpy, AnyKey, AnyModifier, root);
53 XFreePixmap(dpy, dc.drawable);
54 XFreeGC(dpy, dc.gc);
55 XDestroyWindow(dpy, barwin);
56 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
57 XSync(dpy, False);
58 free(seltag);
59 }
61 static void
62 scan() {
63 unsigned int i, num;
64 Window *wins, d1, d2;
65 XWindowAttributes wa;
67 wins = NULL;
68 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
69 for(i = 0; i < num; i++) {
70 if(!XGetWindowAttributes(dpy, wins[i], &wa))
71 continue;
72 if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
73 continue;
74 if(wa.map_state == IsViewable)
75 manage(wins[i], &wa);
76 }
77 }
78 if(wins)
79 XFree(wins);
80 }
82 static void
83 setup() {
84 int i, j;
85 unsigned int mask;
86 Window w;
87 XModifierKeymap *modmap;
88 XSetWindowAttributes wa;
90 /* init atoms */
91 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
92 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
93 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
94 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
95 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
96 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);
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 XFree(modmap);
112 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
113 | EnterWindowMask | LeaveWindowMask;
114 wa.cursor = cursor[CurNormal];
115 XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
117 grabkeys();
118 initrregs();
120 for(ntags = 0; tags[ntags]; ntags++);
121 seltag = emallocz(sizeof(Bool) * ntags);
122 seltag[0] = True;
124 /* style */
125 dc.norm[ColBG] = getcolor(NORMBGCOLOR);
126 dc.norm[ColFG] = getcolor(NORMFGCOLOR);
127 dc.sel[ColBG] = getcolor(SELBGCOLOR);
128 dc.sel[ColFG] = getcolor(SELFGCOLOR);
129 dc.status[ColBG] = getcolor(STATUSBGCOLOR);
130 dc.status[ColFG] = getcolor(STATUSFGCOLOR);
131 setfont(FONT);
133 bmw = textw(FLOATSYMBOL) > textw(TILESYMBOL) ? textw(FLOATSYMBOL) : textw(TILESYMBOL);
134 sx = sy = 0;
135 sw = DisplayWidth(dpy, screen);
136 sh = DisplayHeight(dpy, screen);
137 mw = (sw * MASTERW) / 100;
139 bx = by = 0;
140 bw = sw;
141 dc.h = bh = dc.font.height + 2;
142 wa.override_redirect = 1;
143 wa.background_pixmap = ParentRelative;
144 wa.event_mask = ButtonPressMask | ExposureMask;
145 barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen),
146 CopyFromParent, DefaultVisual(dpy, screen),
147 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
148 XDefineCursor(dpy, barwin, cursor[CurNormal]);
149 XMapRaised(dpy, barwin);
151 dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
152 dc.gc = XCreateGC(dpy, root, 0, 0);
153 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
155 issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
156 strcpy(stext, "dwm-"VERSION);
157 }
159 /*
160 * Startup Error handler to check if another window manager
161 * is already running.
162 */
163 static int
164 xerrorstart(Display *dsply, XErrorEvent *ee) {
165 otherwm = True;
166 return -1;
167 }
169 /* extern */
171 int
172 getproto(Window w) {
173 int i, format, protos, status;
174 unsigned long extra, res;
175 Atom *protocols, real;
177 protos = 0;
178 status = XGetWindowProperty(dpy, w, wmatom[WMProtocols], 0L, 20L, False,
179 XA_ATOM, &real, &format, &res, &extra, (unsigned char **)&protocols);
180 if(status != Success || protocols == 0)
181 return protos;
182 for(i = 0; i < res; i++)
183 if(protocols[i] == wmatom[WMDelete])
184 protos |= PROTODELWIN;
185 free(protocols);
186 return protos;
187 }
189 void
190 sendevent(Window w, Atom a, long value) {
191 XEvent e;
193 e.type = ClientMessage;
194 e.xclient.window = w;
195 e.xclient.message_type = a;
196 e.xclient.format = 32;
197 e.xclient.data.l[0] = value;
198 e.xclient.data.l[1] = CurrentTime;
199 XSendEvent(dpy, w, False, NoEventMask, &e);
200 XSync(dpy, False);
201 }
203 void
204 quit(Arg *arg) {
205 readin = running = False;
206 }
208 /*
209 * There's no way to check accesses to destroyed windows, thus those cases are
210 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
211 * default error handler, which may call exit.
212 */
213 int
214 xerror(Display *dpy, XErrorEvent *ee) {
215 if(ee->error_code == BadWindow
216 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
217 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
218 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
219 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
220 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
221 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
222 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
223 return 0;
224 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
225 ee->request_code, ee->error_code);
226 return xerrorxlib(dpy, ee); /* may call exit */
227 }
229 int
230 main(int argc, char *argv[]) {
231 int r, xfd;
232 fd_set rd;
234 if(argc == 2 && !strncmp("-v", argv[1], 3)) {
235 fputs("dwm-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout);
236 exit(EXIT_SUCCESS);
237 }
238 else if(argc != 1)
239 eprint("usage: dwm [-v]\n");
241 dpy = XOpenDisplay(0);
242 if(!dpy)
243 eprint("dwm: cannot open display\n");
245 xfd = ConnectionNumber(dpy);
246 screen = DefaultScreen(dpy);
247 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);
255 if(otherwm)
256 eprint("dwm: another window manager is already running\n");
258 XSync(dpy, False);
259 XSetErrorHandler(NULL);
260 xerrorxlib = XSetErrorHandler(xerror);
261 XSync(dpy, False);
263 setup();
264 drawstatus();
265 scan();
267 /* main event loop, also reads status text from stdin */
268 XSync(dpy, False);
269 procevent();
270 readin = True;
271 while(running) {
272 FD_ZERO(&rd);
273 if(readin)
274 FD_SET(STDIN_FILENO, &rd);
275 FD_SET(xfd, &rd);
276 r = select(xfd + 1, &rd, NULL, NULL, NULL);
277 if((r == -1) && (errno == EINTR))
278 continue;
279 if(r > 0) {
280 if(readin && FD_ISSET(STDIN_FILENO, &rd)) {
281 readin = NULL != fgets(stext, sizeof(stext), stdin);
282 if(readin)
283 stext[strlen(stext) - 1] = 0;
284 else
285 strcpy(stext, "broken pipe");
286 drawstatus();
287 }
288 }
289 else if(r < 0)
290 eprint("select failed\n");
291 procevent();
292 }
293 cleanup();
294 XCloseDisplay(dpy);
296 return 0;
297 }