dwm-meillo

view main.c @ 657:ce3538c5c0d9

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