dwm-meillo

view wm.c @ 9:d567f430a81d

fixed several stuff (gridwm gets better and better)
author Anselm R. Garbe <garbeam@wmii.de>
date Tue, 11 Jul 2006 12:52:57 +0200
parents 7066ff2fe8bc
children 703255003abb
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 net_atom[NetLast];
20 Cursor cursor[CurLast];
21 XRectangle rect, barrect;
22 Bool running = True;
23 Client *clients = NULL;
25 char *bartext, tag[256];
26 int screen, sel_screen;
28 /* draw structs */
29 Brush brush = {0};
31 enum { WM_PROTOCOL_DELWIN = 1 };
33 static Bool other_wm_running;
34 static int (*x_error_handler) (Display *, XErrorEvent *);
35 static char version[] = "gridwm - " VERSION ", (C)opyright MMVI Anselm R. Garbe\n";
37 static void
38 usage()
39 {
40 fputs("usage: gridwm [-v]\n", stderr);
41 exit(1);
42 }
44 static void
45 scan_wins()
46 {
47 unsigned int i, num;
48 Window *wins;
49 XWindowAttributes wa;
50 Window d1, d2;
52 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
53 for(i = 0; i < num; i++) {
54 if(!XGetWindowAttributes(dpy, wins[i], &wa))
55 continue;
56 if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
57 continue;
58 if(wa.map_state == IsViewable)
59 manage(create_client(wins[i], &wa));
60 }
61 }
62 if(wins)
63 XFree(wins);
64 }
66 /*
67 * There's no way to check accesses to destroyed windows, thus
68 * those cases are ignored (especially on UnmapNotify's).
69 * Other types of errors call Xlib's default error handler, which
70 * calls exit().
71 */
72 static int
73 error_handler(Display *dpy, XErrorEvent *error)
74 {
75 if(error->error_code == BadWindow
76 || (error->request_code == X_SetInputFocus
77 && error->error_code == BadMatch)
78 || (error->request_code == X_PolyText8
79 && error->error_code == BadDrawable)
80 || (error->request_code == X_PolyFillRectangle
81 && error->error_code == BadDrawable)
82 || (error->request_code == X_PolySegment
83 && error->error_code == BadDrawable)
84 || (error->request_code == X_ConfigureWindow
85 && error->error_code == BadMatch)
86 || (error->request_code == X_GrabKey
87 && error->error_code == BadAccess))
88 return 0;
89 fprintf(stderr, "gridwm: fatal error: request code=%d, error code=%d\n",
90 error->request_code, error->error_code);
91 return x_error_handler(dpy, error); /* may call exit() */
92 }
94 /*
95 * Startup Error handler to check if another window manager
96 * is already running.
97 */
98 static int
99 startup_error_handler(Display *dpy, XErrorEvent *error)
100 {
101 other_wm_running = True;
102 return -1;
103 }
105 static void
106 cleanup()
107 {
108 /*
109 Client *c;
110 for(c=client; c; c=c->next)
111 reparent_client(c, root, c->sel->rect.x, c->sel->rect.y);
112 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
113 */
114 }
116 int
117 main(int argc, char *argv[])
118 {
119 int i;
120 XSetWindowAttributes wa;
121 unsigned int mask;
122 Window w;
123 XEvent ev;
125 /* command line args */
126 for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) {
127 switch (argv[i][1]) {
128 case 'v':
129 fprintf(stdout, "%s", version);
130 exit(0);
131 break;
132 default:
133 usage();
134 break;
135 }
136 }
138 dpy = XOpenDisplay(0);
139 if(!dpy)
140 error("gridwm: cannot connect X server\n");
142 screen = DefaultScreen(dpy);
143 root = RootWindow(dpy, screen);
145 /* check if another WM is already running */
146 other_wm_running = False;
147 XSetErrorHandler(startup_error_handler);
148 /* this causes an error if some other WM is running */
149 XSelectInput(dpy, root, SubstructureRedirectMask);
150 XFlush(dpy);
152 if(other_wm_running)
153 error("gridwm: another window manager is already running\n");
155 rect.x = rect.y = 0;
156 rect.width = DisplayWidth(dpy, screen);
157 rect.height = DisplayHeight(dpy, screen);
158 sel_screen = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
160 XSetErrorHandler(0);
161 x_error_handler = XSetErrorHandler(error_handler);
163 /* init atoms */
164 net_atom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
165 net_atom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
167 XChangeProperty(dpy, root, net_atom[NetSupported], XA_ATOM, 32,
168 PropModeReplace, (unsigned char *) net_atom, NetLast);
171 /* init cursors */
172 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
173 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
174 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
176 update_keys();
178 brush.drawable = XCreatePixmap(dpy, root, rect.width, rect.height,
179 DefaultDepth(dpy, screen));
180 brush.gc = XCreateGC(dpy, root, 0, 0);
182 /* style */
183 loadcolors(dpy, screen, &brush, BGCOLOR, FGCOLOR, BORDERCOLOR);
184 loadfont(dpy, &brush.font, FONT);
186 wa.override_redirect = 1;
187 wa.background_pixmap = ParentRelative;
188 wa.event_mask = ExposureMask;
190 barrect = rect;
191 barrect.height = labelheight(&brush.font);
192 barrect.y = rect.height - barrect.height;
193 barwin = XCreateWindow(dpy, root, barrect.x, barrect.y,
194 barrect.width, barrect.height, 0, DefaultDepth(dpy, screen),
195 CopyFromParent, DefaultVisual(dpy, screen),
196 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
197 bartext = NULL;
198 XDefineCursor(dpy, barwin, cursor[CurNormal]);
199 XMapRaised(dpy, barwin);
200 draw_bar();
202 wa.event_mask = SubstructureRedirectMask | EnterWindowMask \
203 | LeaveWindowMask;
204 wa.cursor = cursor[CurNormal];
205 XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
207 scan_wins();
209 while(running) {
210 XNextEvent(dpy, &ev);
211 if(handler[ev.type])
212 (handler[ev.type]) (&ev); /* call handler */
213 }
215 cleanup();
216 XCloseDisplay(dpy);
218 return 0;
219 }