Mercurial > aewl
annotate main.c @ 53:529901e6a227
added mini stuff
author | Anselm R. Garbe <garbeam@wmii.de> |
---|---|
date | Thu, 13 Jul 2006 21:42:17 +0200 |
parents | d18f6dd0cf23 |
children | f005d46462e8 |
rev | line source |
---|---|
0 | 1 /* |
2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com> | |
3 * See LICENSE file for license details. | |
4 */ | |
5 | |
6 #include <stdarg.h> | |
7 #include <stdio.h> | |
8 #include <stdlib.h> | |
9 | |
10 #include <X11/cursorfont.h> | |
11 #include <X11/Xatom.h> | |
12 #include <X11/Xproto.h> | |
13 | |
43 | 14 #include "dwm.h" |
0 | 15 |
31 | 16 /********** CUSTOMIZE **********/ |
17 | |
18 char *tags[TLast] = { | |
19 [Tscratch] = "scratch", | |
20 [Tdev] = "dev", | |
21 [Tirc] = "irc", | |
22 [Twww] = "www", | |
23 [Twork] = "work", | |
24 }; | |
25 | |
26 /********** CUSTOMIZE **********/ | |
27 | |
3
e969f3575b7a
several new changes, made gridmenu working
Anselm R. Garbe <garbeam@wmii.de>
parents:
2
diff
changeset
|
28 /* X structs */ |
0 | 29 Display *dpy; |
5 | 30 Window root, barwin; |
13
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
31 Atom wm_atom[WMLast], net_atom[NetLast]; |
0 | 32 Cursor cursor[CurLast]; |
5 | 33 Bool running = True; |
31 | 34 Bool issel; |
3
e969f3575b7a
several new changes, made gridmenu working
Anselm R. Garbe <garbeam@wmii.de>
parents:
2
diff
changeset
|
35 |
31 | 36 int tsel = Tdev; /* default tag */ |
51 | 37 int screen, sx, sy, sw, sh, mw, th; |
3
e969f3575b7a
several new changes, made gridmenu working
Anselm R. Garbe <garbeam@wmii.de>
parents:
2
diff
changeset
|
38 |
34 | 39 DC dc = {0}; |
50
148f25ed0ad7
several other additions/fixes, dwm is quite usable already
Anselm R. Garbe <garbeam@wmii.de>
parents:
49
diff
changeset
|
40 Client *clients = NULL; |
148f25ed0ad7
several other additions/fixes, dwm is quite usable already
Anselm R. Garbe <garbeam@wmii.de>
parents:
49
diff
changeset
|
41 Client *sel = NULL; |
0 | 42 |
43 static Bool other_wm_running; | |
31 | 44 static const char version[] = |
34 | 45 "dwm - " VERSION ", (C)opyright MMVI Anselm R. Garbe\n"; |
0 | 46 static int (*x_error_handler) (Display *, XErrorEvent *); |
47 | |
48 static void | |
34 | 49 usage() { error("usage: dwm [-v]\n"); } |
0 | 50 |
51 static void | |
52 scan_wins() | |
53 { | |
54 unsigned int i, num; | |
55 Window *wins; | |
56 XWindowAttributes wa; | |
57 Window d1, d2; | |
58 | |
59 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) { | |
60 for(i = 0; i < num; i++) { | |
61 if(!XGetWindowAttributes(dpy, wins[i], &wa)) | |
62 continue; | |
63 if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1)) | |
64 continue; | |
65 if(wa.map_state == IsViewable) | |
10
703255003abb
changed how manage client works
Anselm R. Garbe <garbeam@wmii.de>
parents:
9
diff
changeset
|
66 manage(wins[i], &wa); |
0 | 67 } |
68 } | |
69 if(wins) | |
70 XFree(wins); | |
71 } | |
72 | |
13
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
73 static int |
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
74 win_property(Window w, Atom a, Atom t, long l, unsigned char **prop) |
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
75 { |
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
76 Atom real; |
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
77 int format; |
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
78 unsigned long res, extra; |
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
79 int status; |
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
80 |
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
81 status = XGetWindowProperty(dpy, w, a, 0L, l, False, t, &real, &format, |
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
82 &res, &extra, prop); |
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
83 |
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
84 if(status != Success || *prop == 0) { |
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
85 return 0; |
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
86 } |
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
87 if(res == 0) { |
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
88 free((void *) *prop); |
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
89 } |
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
90 return res; |
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
91 } |
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
92 |
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
93 int |
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
94 win_proto(Window w) |
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
95 { |
30
2e0fb4130bfb
new stuff, fixed several issues
Anselm R. Garbe <garbeam@wmii.de>
parents:
27
diff
changeset
|
96 unsigned char *protocols; |
13
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
97 long res; |
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
98 int protos = 0; |
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
99 int i; |
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
100 |
30
2e0fb4130bfb
new stuff, fixed several issues
Anselm R. Garbe <garbeam@wmii.de>
parents:
27
diff
changeset
|
101 res = win_property(w, wm_atom[WMProtocols], XA_ATOM, 20L, &protocols); |
13
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
102 if(res <= 0) { |
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
103 return protos; |
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
104 } |
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
105 for(i = 0; i < res; i++) { |
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
106 if(protocols[i] == wm_atom[WMDelete]) |
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
107 protos |= WM_PROTOCOL_DELWIN; |
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
108 } |
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
109 free((char *) protocols); |
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
110 return protos; |
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
111 } |
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
112 |
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
113 void |
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
114 send_message(Window w, Atom a, long value) |
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
115 { |
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
116 XEvent e; |
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
117 |
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
118 e.type = ClientMessage; |
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
119 e.xclient.window = w; |
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
120 e.xclient.message_type = a; |
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
121 e.xclient.format = 32; |
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
122 e.xclient.data.l[0] = value; |
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
123 e.xclient.data.l[1] = CurrentTime; |
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
124 XSendEvent(dpy, w, False, NoEventMask, &e); |
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
125 XFlush(dpy); |
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
126 } |
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
127 |
0 | 128 /* |
129 * There's no way to check accesses to destroyed windows, thus | |
130 * those cases are ignored (especially on UnmapNotify's). | |
131 * Other types of errors call Xlib's default error handler, which | |
132 * calls exit(). | |
133 */ | |
10
703255003abb
changed how manage client works
Anselm R. Garbe <garbeam@wmii.de>
parents:
9
diff
changeset
|
134 int |
0 | 135 error_handler(Display *dpy, XErrorEvent *error) |
136 { | |
137 if(error->error_code == BadWindow | |
138 || (error->request_code == X_SetInputFocus | |
139 && error->error_code == BadMatch) | |
140 || (error->request_code == X_PolyText8 | |
141 && error->error_code == BadDrawable) | |
142 || (error->request_code == X_PolyFillRectangle | |
143 && error->error_code == BadDrawable) | |
144 || (error->request_code == X_PolySegment | |
145 && error->error_code == BadDrawable) | |
146 || (error->request_code == X_ConfigureWindow | |
147 && error->error_code == BadMatch) | |
148 || (error->request_code == X_GrabKey | |
149 && error->error_code == BadAccess)) | |
150 return 0; | |
34 | 151 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n", |
0 | 152 error->request_code, error->error_code); |
153 return x_error_handler(dpy, error); /* may call exit() */ | |
154 } | |
155 | |
156 /* | |
157 * Startup Error handler to check if another window manager | |
158 * is already running. | |
159 */ | |
160 static int | |
161 startup_error_handler(Display *dpy, XErrorEvent *error) | |
162 { | |
163 other_wm_running = True; | |
164 return -1; | |
165 } | |
166 | |
167 static void | |
168 cleanup() | |
169 { | |
50
148f25ed0ad7
several other additions/fixes, dwm is quite usable already
Anselm R. Garbe <garbeam@wmii.de>
parents:
49
diff
changeset
|
170 while(sel) { |
52
d18f6dd0cf23
fixed several things, nearly feature complete
Anselm R. Garbe <garbeam@wmii.de>
parents:
51
diff
changeset
|
171 resize(sel, True); |
50
148f25ed0ad7
several other additions/fixes, dwm is quite usable already
Anselm R. Garbe <garbeam@wmii.de>
parents:
49
diff
changeset
|
172 unmanage(sel); |
148f25ed0ad7
several other additions/fixes, dwm is quite usable already
Anselm R. Garbe <garbeam@wmii.de>
parents:
49
diff
changeset
|
173 } |
0 | 174 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime); |
175 } | |
176 | |
27
f96fb3fd8203
added grid mode on Mod1Mask g
Anselm R. Garbe <garbeam@wmii.de>
parents:
26
diff
changeset
|
177 void |
49
466591c2f967
implemented tagging a client
Anselm R. Garbe <garbeam@wmii.de>
parents:
43
diff
changeset
|
178 quit(Arg *arg) |
27
f96fb3fd8203
added grid mode on Mod1Mask g
Anselm R. Garbe <garbeam@wmii.de>
parents:
26
diff
changeset
|
179 { |
f96fb3fd8203
added grid mode on Mod1Mask g
Anselm R. Garbe <garbeam@wmii.de>
parents:
26
diff
changeset
|
180 running = False; |
f96fb3fd8203
added grid mode on Mod1Mask g
Anselm R. Garbe <garbeam@wmii.de>
parents:
26
diff
changeset
|
181 } |
f96fb3fd8203
added grid mode on Mod1Mask g
Anselm R. Garbe <garbeam@wmii.de>
parents:
26
diff
changeset
|
182 |
0 | 183 int |
184 main(int argc, char *argv[]) | |
185 { | |
186 int i; | |
187 XSetWindowAttributes wa; | |
188 unsigned int mask; | |
189 Window w; | |
5 | 190 XEvent ev; |
0 | 191 |
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 } | |
204 | |
205 dpy = XOpenDisplay(0); | |
206 if(!dpy) | |
34 | 207 error("dwm: cannot connect X server\n"); |
0 | 208 |
209 screen = DefaultScreen(dpy); | |
210 root = RootWindow(dpy, screen); | |
211 | |
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); | |
3
e969f3575b7a
several new changes, made gridmenu working
Anselm R. Garbe <garbeam@wmii.de>
parents:
2
diff
changeset
|
217 XFlush(dpy); |
0 | 218 |
219 if(other_wm_running) | |
34 | 220 error("dwm: another window manager is already running\n"); |
0 | 221 |
26
e8f627998d6f
simplified several portions of code through replacing rect structs with x,y,h,w counterparts (much more readable)
Anselm R. Garbe <garbeam@wmii.de>
parents:
21
diff
changeset
|
222 sx = sy = 0; |
e8f627998d6f
simplified several portions of code through replacing rect structs with x,y,h,w counterparts (much more readable)
Anselm R. Garbe <garbeam@wmii.de>
parents:
21
diff
changeset
|
223 sw = DisplayWidth(dpy, screen); |
e8f627998d6f
simplified several portions of code through replacing rect structs with x,y,h,w counterparts (much more readable)
Anselm R. Garbe <garbeam@wmii.de>
parents:
21
diff
changeset
|
224 sh = DisplayHeight(dpy, screen); |
51 | 225 mw = (sw * MASTERW) / 100; |
31 | 226 issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask); |
0 | 227 |
228 XSetErrorHandler(0); | |
229 x_error_handler = XSetErrorHandler(error_handler); | |
230 | |
231 /* init atoms */ | |
13
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
232 wm_atom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False); |
5cc5e55a132d
added protocol killing stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
10
diff
changeset
|
233 wm_atom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False); |
0 | 234 net_atom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False); |
235 net_atom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False); | |
236 | |
237 XChangeProperty(dpy, root, net_atom[NetSupported], XA_ATOM, 32, | |
238 PropModeReplace, (unsigned char *) net_atom, NetLast); | |
239 | |
240 | |
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); | |
245 | |
8 | 246 update_keys(); |
0 | 247 |
5 | 248 /* style */ |
43 | 249 dc.bg = initcolor(BGCOLOR); |
250 dc.fg = initcolor(FGCOLOR); | |
251 dc.border = initcolor(BORDERCOLOR); | |
252 initfont(FONT); | |
5 | 253 |
43 | 254 th = dc.font.height + 4; |
5 | 255 |
34 | 256 dc.drawable = XCreatePixmap(dpy, root, sw, th, DefaultDepth(dpy, screen)); |
257 dc.gc = XCreateGC(dpy, root, 0, 0); | |
21
3ef108a5ca0a
implemented draw_client stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
16
diff
changeset
|
258 |
9
d567f430a81d
fixed several stuff (gridwm gets better and better)
Anselm R. Garbe <garbeam@wmii.de>
parents:
8
diff
changeset
|
259 wa.event_mask = SubstructureRedirectMask | EnterWindowMask \ |
d567f430a81d
fixed several stuff (gridwm gets better and better)
Anselm R. Garbe <garbeam@wmii.de>
parents:
8
diff
changeset
|
260 | LeaveWindowMask; |
0 | 261 wa.cursor = cursor[CurNormal]; |
262 XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa); | |
263 | |
5 | 264 scan_wins(); |
3
e969f3575b7a
several new changes, made gridmenu working
Anselm R. Garbe <garbeam@wmii.de>
parents:
2
diff
changeset
|
265 |
5 | 266 while(running) { |
32 | 267 XNextEvent(dpy, &ev); |
268 if(handler[ev.type]) | |
269 (handler[ev.type])(&ev); /* call handler */ | |
5 | 270 } |
0 | 271 |
272 cleanup(); | |
273 XCloseDisplay(dpy); | |
274 | |
275 return 0; | |
276 } |