aewl
view wm.c @ 7:49e2fc9fb94f
new stuff
author | Anselm R. Garbe <garbeam@wmii.de> |
---|---|
date | Tue, 11 Jul 2006 11:27:56 +0200 |
parents | e0cefb3981c8 |
children | 7066ff2fe8bc |
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 <stdarg.h>
7 #include <stdio.h>
8 #include <stdlib.h>
10 #include <X11/cursorfont.h>
11 #include <X11/Xatom.h>
12 #include <X11/Xproto.h>
14 #include "wm.h"
16 /* X structs */
17 Display *dpy;
18 Window root, barwin;
19 Atom wm_atom[WMLast], net_atom[NetLast];
20 Cursor cursor[CurLast];
21 XRectangle rect, barrect;
22 Bool running = True;
23 Client *client = NULL;
25 char *bartext, tag[256];
26 int screen, sel_screen;
27 unsigned int lock_mask, numlock_mask;
29 /* draw structs */
30 Brush brush = {0};
32 enum { WM_PROTOCOL_DELWIN = 1 };
34 static Bool other_wm_running;
35 static int (*x_error_handler) (Display *, XErrorEvent *);
36 static char version[] = "gridwm - " VERSION ", (C)opyright MMVI Anselm R. Garbe\n";
38 static void
39 usage()
40 {
41 fputs("usage: gridwm [-v]\n", stderr);
42 exit(1);
43 }
45 static void
46 scan_wins()
47 {
48 unsigned int i, num;
49 Window *wins;
50 XWindowAttributes wa;
51 Window d1, d2;
53 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
54 for(i = 0; i < num; i++) {
55 if(!XGetWindowAttributes(dpy, wins[i], &wa))
56 continue;
57 if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
58 continue;
59 if(wa.map_state == IsViewable)
60 manage(create_client(wins[i], &wa));
61 }
62 }
63 if(wins)
64 XFree(wins);
65 }
67 static int
68 win_property(Window w, Atom a, Atom t, long l, unsigned char **prop)
69 {
70 Atom real;
71 int format;
72 unsigned long res, extra;
73 int status;
75 status = XGetWindowProperty(dpy, w, a, 0L, l, False, t, &real, &format,
76 &res, &extra, prop);
78 if(status != Success || *prop == NULL) {
79 return 0;
80 }
81 if(res == 0)
82 free((void *) *prop);
83 return res;
84 }
86 int
87 win_proto(Window w)
88 {
89 Atom *protocols;
90 long res;
91 int protos = 0;
92 int i;
94 res = win_property(w, wm_atom[WMProtocols], XA_ATOM, 20L,
95 ((unsigned char **) &protocols));
96 if(res <= 0) {
97 return protos;
98 }
99 for(i = 0; i < res; i++) {
100 if(protocols[i] == wm_atom[WMDelete])
101 protos |= WM_PROTOCOL_DELWIN;
102 }
103 free((char *) protocols);
104 return protos;
105 }
107 /*
108 * There's no way to check accesses to destroyed windows, thus
109 * those cases are ignored (especially on UnmapNotify's).
110 * Other types of errors call Xlib's default error handler, which
111 * calls exit().
112 */
113 static int
114 error_handler(Display *dpy, XErrorEvent *error)
115 {
116 if(error->error_code == BadWindow
117 || (error->request_code == X_SetInputFocus
118 && error->error_code == BadMatch)
119 || (error->request_code == X_PolyText8
120 && error->error_code == BadDrawable)
121 || (error->request_code == X_PolyFillRectangle
122 && error->error_code == BadDrawable)
123 || (error->request_code == X_PolySegment
124 && error->error_code == BadDrawable)
125 || (error->request_code == X_ConfigureWindow
126 && error->error_code == BadMatch)
127 || (error->request_code == X_GrabKey
128 && error->error_code == BadAccess))
129 return 0;
130 fprintf(stderr, "gridwm: fatal error: request code=%d, error code=%d\n",
131 error->request_code, error->error_code);
132 return x_error_handler(dpy, error); /* may call exit() */
133 }
135 /*
136 * Startup Error handler to check if another window manager
137 * is already running.
138 */
139 static int
140 startup_error_handler(Display *dpy, XErrorEvent *error)
141 {
142 other_wm_running = True;
143 return -1;
144 }
146 static void
147 init_lock_keys()
148 {
149 XModifierKeymap *modmap;
150 KeyCode numlock;
151 int i;
152 static int masks[] = {
153 ShiftMask, LockMask, ControlMask, Mod1Mask,
154 Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
155 };
157 numlock_mask = 0;
158 modmap = XGetModifierMapping(dpy);
159 numlock = XKeysymToKeycode(dpy, XStringToKeysym("Num_Lock"));
161 if(modmap && modmap->max_keypermod > 0) {
162 int max = (sizeof(masks) / sizeof(int)) * modmap->max_keypermod;
163 for(i = 0; i < max; i++)
164 if(numlock && (modmap->modifiermap[i] == numlock))
165 numlock_mask = masks[i / modmap->max_keypermod];
166 }
167 XFreeModifiermap(modmap);
169 lock_mask = 255 & ~(numlock_mask | LockMask);
170 }
172 static void
173 cleanup()
174 {
175 /*
176 Client *c;
177 for(c=client; c; c=c->next)
178 reparent_client(c, root, c->sel->rect.x, c->sel->rect.y);
179 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
180 */
181 }
183 int
184 main(int argc, char *argv[])
185 {
186 int i;
187 XSetWindowAttributes wa;
188 unsigned int mask;
189 Window w;
190 XEvent ev;
192 /* command line args */
193 for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) {
194 switch (argv[i][1]) {
195 case 'v':
196 fprintf(stdout, "%s", version);
197 exit(0);
198 break;
199 default:
200 usage();
201 break;
202 }
203 }
205 dpy = XOpenDisplay(0);
206 if(!dpy)
207 error("gridwm: cannot connect X server\n");
209 screen = DefaultScreen(dpy);
210 root = RootWindow(dpy, screen);
212 /* check if another WM is already running */
213 other_wm_running = False;
214 XSetErrorHandler(startup_error_handler);
215 /* this causes an error if some other WM is running */
216 XSelectInput(dpy, root, SubstructureRedirectMask);
217 XFlush(dpy);
219 if(other_wm_running)
220 error("gridwm: another window manager is already running\n");
222 rect.x = rect.y = 0;
223 rect.width = DisplayWidth(dpy, screen);
224 rect.height = DisplayHeight(dpy, screen);
225 sel_screen = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
227 XSetErrorHandler(0);
228 x_error_handler = XSetErrorHandler(error_handler);
230 /* init atoms */
231 wm_atom[WMState] = XInternAtom(dpy, "WM_STATE", False);
232 wm_atom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
233 wm_atom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
234 net_atom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
235 net_atom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
237 XChangeProperty(dpy, root, net_atom[NetSupported], XA_ATOM, 32,
238 PropModeReplace, (unsigned char *) net_atom, NetLast);
241 /* init cursors */
242 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
243 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
244 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
246 init_lock_keys();
248 brush.drawable = XCreatePixmap(dpy, root, rect.width, rect.height,
249 DefaultDepth(dpy, screen));
250 brush.gc = XCreateGC(dpy, root, 0, 0);
252 /* style */
253 loadcolors(dpy, screen, &brush, BGCOLOR, FGCOLOR, BORDERCOLOR);
254 loadfont(dpy, &brush.font, FONT);
256 wa.override_redirect = 1;
257 wa.background_pixmap = ParentRelative;
258 wa.event_mask = ExposureMask;
260 barrect = rect;
261 barrect.height = labelheight(&brush.font);
262 barrect.y = rect.height - barrect.height;
263 barwin = XCreateWindow(dpy, root, barrect.x, barrect.y,
264 barrect.width, barrect.height, 0, DefaultDepth(dpy, screen),
265 CopyFromParent, DefaultVisual(dpy, screen),
266 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
267 bartext = NULL;
268 XDefineCursor(dpy, barwin, cursor[CurNormal]);
269 XMapRaised(dpy, barwin);
270 draw_bar();
272 wa.event_mask = SubstructureRedirectMask | EnterWindowMask | LeaveWindowMask;
273 wa.cursor = cursor[CurNormal];
274 XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
276 scan_wins();
278 while(running) {
279 XNextEvent(dpy, &ev);
280 if(handler[ev.type])
281 (handler[ev.type]) (&ev); /* call handler */
282 }
284 cleanup();
285 XCloseDisplay(dpy);
287 return 0;
288 }