dwm-meillo

view main.c @ 516:e5f8d6ac509c

added symbols for different modes
author Anselm R. Garbe <arg@10kloc.org>
date Fri, 29 Sep 2006 18:08:20 +0200
parents d6d1d0033e3c
children 1a9a0877650c
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, master, 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 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(sel) {
44 resize(sel, True, TopLeft);
45 unmanage(sel);
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 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
56 XSync(dpy, False);
57 free(seltag);
58 }
60 static void
61 scan(void) {
62 unsigned int i, num;
63 Window *wins, d1, d2;
64 XWindowAttributes wa;
66 wins = NULL;
67 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
68 for(i = 0; i < num; i++) {
69 if(!XGetWindowAttributes(dpy, wins[i], &wa))
70 continue;
71 if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
72 continue;
73 if(wa.map_state == IsViewable)
74 manage(wins[i], &wa);
75 }
76 }
77 if(wins)
78 XFree(wins);
79 }
81 static void
82 setup(void) {
83 int i, j;
84 unsigned int mask;
85 Window w;
86 XModifierKeymap *modmap;
87 XSetWindowAttributes wa;
89 /* init atoms */
90 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
91 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
92 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
93 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
94 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
95 PropModeReplace, (unsigned char *) netatom, NetLast);
97 /* init cursors */
98 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
99 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
100 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
102 modmap = XGetModifierMapping(dpy);
103 for (i = 0; i < 8; i++) {
104 for (j = 0; j < modmap->max_keypermod; j++) {
105 if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock))
106 numlockmask = (1 << i);
107 }
108 }
109 XFree(modmap);
111 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
112 | EnterWindowMask | LeaveWindowMask;
113 wa.cursor = cursor[CurNormal];
114 XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
116 grabkeys();
117 initrregs();
119 for(ntags = 0; tags[ntags]; ntags++);
120 seltag = emallocz(sizeof(Bool) * ntags);
121 seltag[0] = True;
123 /* style */
124 dc.norm[ColBG] = getcolor(NORMBGCOLOR);
125 dc.norm[ColFG] = getcolor(NORMFGCOLOR);
126 dc.sel[ColBG] = getcolor(SELBGCOLOR);
127 dc.sel[ColFG] = getcolor(SELFGCOLOR);
128 dc.status[ColBG] = getcolor(STATUSBGCOLOR);
129 dc.status[ColFG] = getcolor(STATUSFGCOLOR);
130 setfont(FONT);
132 bmw = textw(VSTACKSYMBOL) > textw(BSTACKSYMBOL) ?
133 textw(VSTACKSYMBOL) : textw(BSTACKSYMBOL);
134 bmw = bmw > textw(FLOATSYMBOL) ?
135 bmw : textw(FLOATSYMBOL);
136 sx = sy = 0;
137 sw = DisplayWidth(dpy, screen);
138 sh = DisplayHeight(dpy, screen);
139 master = ((stackpos == StackBottom ? sh - bh : sw) * MASTER) / 100;
141 bx = by = 0;
142 bw = sw;
143 dc.h = bh = dc.font.height + 2;
144 wa.override_redirect = 1;
145 wa.background_pixmap = ParentRelative;
146 wa.event_mask = ButtonPressMask | ExposureMask;
147 barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen),
148 CopyFromParent, DefaultVisual(dpy, screen),
149 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
150 XDefineCursor(dpy, barwin, cursor[CurNormal]);
151 XMapRaised(dpy, barwin);
153 dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
154 dc.gc = XCreateGC(dpy, root, 0, 0);
155 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
157 issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
158 strcpy(stext, "dwm-"VERSION);
159 }
161 /*
162 * Startup Error handler to check if another window manager
163 * is already running.
164 */
165 static int
166 xerrorstart(Display *dsply, XErrorEvent *ee) {
167 otherwm = True;
168 return -1;
169 }
171 /* extern */
173 int
174 getproto(Window w) {
175 int i, format, protos, status;
176 unsigned long extra, res;
177 Atom *protocols, real;
179 protos = 0;
180 status = XGetWindowProperty(dpy, w, wmatom[WMProtocols], 0L, 20L, False,
181 XA_ATOM, &real, &format, &res, &extra, (unsigned char **)&protocols);
182 if(status != Success || protocols == 0)
183 return protos;
184 for(i = 0; i < res; i++)
185 if(protocols[i] == wmatom[WMDelete])
186 protos |= PROTODELWIN;
187 free(protocols);
188 return protos;
189 }
191 void
192 sendevent(Window w, Atom a, long value) {
193 XEvent e;
195 e.type = ClientMessage;
196 e.xclient.window = w;
197 e.xclient.message_type = a;
198 e.xclient.format = 32;
199 e.xclient.data.l[0] = value;
200 e.xclient.data.l[1] = CurrentTime;
201 XSendEvent(dpy, w, False, NoEventMask, &e);
202 XSync(dpy, False);
203 }
205 void
206 quit(Arg *arg) {
207 readin = running = False;
208 }
210 /*
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 int r, xfd;
234 fd_set rd;
236 if(argc == 2 && !strncmp("-v", argv[1], 3)) {
237 fputs("dwm-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout);
238 exit(EXIT_SUCCESS);
239 }
240 else if(argc != 1)
241 eprint("usage: dwm [-v]\n");
243 dpy = XOpenDisplay(0);
244 if(!dpy)
245 eprint("dwm: cannot open display\n");
247 xfd = ConnectionNumber(dpy);
248 screen = DefaultScreen(dpy);
249 root = RootWindow(dpy, screen);
251 otherwm = False;
252 XSetErrorHandler(xerrorstart);
253 /* this causes an error if some other window manager is running */
254 XSelectInput(dpy, root, SubstructureRedirectMask);
255 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);
265 setup();
266 drawstatus();
267 scan();
269 /* main event loop, also reads status text from stdin */
270 XSync(dpy, False);
271 procevent();
272 readin = True;
273 while(running) {
274 FD_ZERO(&rd);
275 if(readin)
276 FD_SET(STDIN_FILENO, &rd);
277 FD_SET(xfd, &rd);
278 r = select(xfd + 1, &rd, NULL, NULL, NULL);
279 if((r == -1) && (errno == EINTR))
280 continue;
281 if(r > 0) {
282 if(readin && FD_ISSET(STDIN_FILENO, &rd)) {
283 readin = NULL != fgets(stext, sizeof(stext), stdin);
284 if(readin)
285 stext[strlen(stext) - 1] = 0;
286 else
287 strcpy(stext, "broken pipe");
288 drawstatus();
289 }
290 }
291 else if(r < 0)
292 eprint("select failed\n");
293 procevent();
294 }
295 cleanup();
296 XCloseDisplay(dpy);
298 return 0;
299 }