dwm-meillo

view client.c @ 647:fd2ea58b0b94

renamed drawtitle into drawclient
author Anselm R. Garbe <arg@suckless.org>
date Thu, 04 Jan 2007 14:17:25 +0100
parents 1ed8c40dde36
children a76799907854
line source
1 /* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
2 * See LICENSE file for license details.
3 */
4 #include "dwm.h"
5 #include <stdlib.h>
6 #include <string.h>
7 #include <X11/Xatom.h>
8 #include <X11/Xutil.h>
10 /* static functions */
12 static void
13 detachstack(Client *c) {
14 Client **tc;
15 for(tc=&stack; *tc && *tc != c; tc=&(*tc)->snext);
16 *tc = c->snext;
17 }
19 static void
20 grabbuttons(Client *c, Bool focused) {
21 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
23 if(focused) {
24 XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
25 GrabModeAsync, GrabModeSync, None, None);
26 XGrabButton(dpy, Button1, MODKEY | LockMask, c->win, False, BUTTONMASK,
27 GrabModeAsync, GrabModeSync, None, None);
28 XGrabButton(dpy, Button1, MODKEY | numlockmask, c->win, False, BUTTONMASK,
29 GrabModeAsync, GrabModeSync, None, None);
30 XGrabButton(dpy, Button1, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
31 GrabModeAsync, GrabModeSync, None, None);
33 XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
34 GrabModeAsync, GrabModeSync, None, None);
35 XGrabButton(dpy, Button2, MODKEY | LockMask, c->win, False, BUTTONMASK,
36 GrabModeAsync, GrabModeSync, None, None);
37 XGrabButton(dpy, Button2, MODKEY | numlockmask, c->win, False, BUTTONMASK,
38 GrabModeAsync, GrabModeSync, None, None);
39 XGrabButton(dpy, Button2, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
40 GrabModeAsync, GrabModeSync, None, None);
42 XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
43 GrabModeAsync, GrabModeSync, None, None);
44 XGrabButton(dpy, Button3, MODKEY | LockMask, c->win, False, BUTTONMASK,
45 GrabModeAsync, GrabModeSync, None, None);
46 XGrabButton(dpy, Button3, MODKEY | numlockmask, c->win, False, BUTTONMASK,
47 GrabModeAsync, GrabModeSync, None, None);
48 XGrabButton(dpy, Button3, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
49 GrabModeAsync, GrabModeSync, None, None);
50 }
51 else
52 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, BUTTONMASK,
53 GrabModeAsync, GrabModeSync, None, None);
54 }
56 static int
57 xerrordummy(Display *dsply, XErrorEvent *ee) {
58 return 0;
59 }
61 /* extern functions */
63 void
64 ban(Client *c) {
65 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
66 XMoveWindow(dpy, c->twin, c->tx + 2 * sw, c->ty);
67 }
69 void
70 configure(Client *c) {
71 XEvent synev;
73 synev.type = ConfigureNotify;
74 synev.xconfigure.display = dpy;
75 synev.xconfigure.event = c->win;
76 synev.xconfigure.window = c->win;
77 synev.xconfigure.x = c->x;
78 synev.xconfigure.y = c->y;
79 synev.xconfigure.width = c->w;
80 synev.xconfigure.height = c->h;
81 synev.xconfigure.border_width = c->border;
82 synev.xconfigure.above = None;
83 XSendEvent(dpy, c->win, True, NoEventMask, &synev);
84 }
86 void
87 focus(Client *c) {
88 Client *old;
90 if(!issel || (c && !isvisible(c)))
91 return;
92 if(!sel)
93 sel = c;
94 else if(sel != c) {
95 old = sel;
96 sel = c;
97 if(old) {
98 grabbuttons(old, False);
99 drawclient(old);
100 }
101 }
102 if(c) {
103 detachstack(c);
104 c->snext = stack;
105 stack = c;
106 grabbuttons(c, True);
107 drawclient(c);
108 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
109 }
110 else
111 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
112 }
114 Client *
115 getclient(Window w) {
116 Client *c;
118 for(c = clients; c; c = c->next)
119 if(c->win == w)
120 return c;
121 return NULL;
122 }
124 Client *
125 getctitle(Window w) {
126 Client *c;
128 for(c = clients; c; c = c->next)
129 if(c->twin == w)
130 return c;
131 return NULL;
132 }
134 void
135 killclient(Arg *arg) {
136 if(!sel)
137 return;
138 if(sel->proto & PROTODELWIN)
139 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
140 else
141 XKillClient(dpy, sel->win);
142 }
144 void
145 manage(Window w, XWindowAttributes *wa) {
146 Client *c;
147 Window trans;
148 XSetWindowAttributes twa;
150 c = emallocz(sizeof(Client));
151 c->tags = emallocz(ntags * sizeof(Bool));
152 c->win = w;
153 c->border = 0;
154 c->x = c->tx = wa->x;
155 c->y = c->ty = wa->y;
156 c->w = c->tw = wa->width;
157 c->h = wa->height;
158 c->th = bh;
159 updatesizehints(c);
160 if(c->x + c->w + 2 * BORDERPX > sw)
161 c->x = sw - c->w - 2 * BORDERPX;
162 if(c->x < sx)
163 c->x = sx;
164 if(c->y + c->h + 2 * BORDERPX > sh)
165 c->y = sh - c->h - 2 * BORDERPX;
166 if(c->h != sh && c->y < bh)
167 c->y = bh;
168 c->proto = getproto(c->win);
169 XSelectInput(dpy, c->win,
170 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
171 XGetTransientForHint(dpy, c->win, &trans);
172 twa.override_redirect = 1;
173 twa.background_pixmap = ParentRelative;
174 twa.event_mask = ExposureMask | EnterWindowMask;
175 c->twin = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
176 0, DefaultDepth(dpy, screen), CopyFromParent,
177 DefaultVisual(dpy, screen),
178 CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
179 grabbuttons(c, False);
180 updatetitle(c);
181 settags(c, getclient(trans));
182 if(!c->isfloat)
183 c->isfloat = trans || c->isfixed;
184 resizetitle(c);
185 if(clients)
186 clients->prev = c;
187 c->next = clients;
188 c->snext = stack;
189 stack = clients = c;
190 ban(c);
191 XMapWindow(dpy, c->win);
192 XMapWindow(dpy, c->twin);
193 if(isvisible(c))
194 focus(c);
195 arrange();
196 }
198 void
199 resize(Client *c, Bool sizehints, Corner sticky) {
200 int bottom = c->y + c->h;
201 int right = c->x + c->w;
202 XWindowChanges wc;
204 if(sizehints) {
205 if(c->incw)
206 c->w -= (c->w - c->basew) % c->incw;
207 if(c->inch)
208 c->h -= (c->h - c->baseh) % c->inch;
209 if(c->minw && c->w < c->minw)
210 c->w = c->minw;
211 if(c->minh && c->h < c->minh)
212 c->h = c->minh;
213 if(c->maxw && c->w > c->maxw)
214 c->w = c->maxw;
215 if(c->maxh && c->h > c->maxh)
216 c->h = c->maxh;
217 }
218 if(sticky == TopRight || sticky == BotRight)
219 c->x = right - c->w;
220 if(sticky == BotLeft || sticky == BotRight)
221 c->y = bottom - c->h;
222 /* offscreen appearance fixes */
223 if(c->x + c->w < sx)
224 c->x = sx;
225 if(c->y + c->h < bh)
226 c->y = bh;
227 if(c->x > sw)
228 c->x = sw - c->w;
229 if(c->y > sh)
230 c->y = sh - c->h;
231 resizetitle(c);
232 wc.x = c->x;
233 wc.y = c->y;
234 wc.width = c->w;
235 wc.height = c->h;
236 if(c->w == sw && c->h == sh)
237 wc.border_width = 0;
238 else
239 wc.border_width = BORDERPX;
240 XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
241 configure(c);
242 XSync(dpy, False);
243 }
245 void
246 resizetitle(Client *c) {
247 c->tw = textw(c->name);
248 if(c->tw > c->w)
249 c->tw = c->w + 2 * BORDERPX;
250 c->tx = c->x + c->w - c->tw + 2 * BORDERPX;
251 c->ty = c->y;
252 if(isvisible(c))
253 XMoveResizeWindow(dpy, c->twin, c->tx, c->ty, c->tw, c->th);
254 else
255 XMoveResizeWindow(dpy, c->twin, c->tx + 2 * sw, c->ty, c->tw, c->th);
256 }
258 void
259 updatesizehints(Client *c) {
260 long msize;
261 XSizeHints size;
263 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
264 size.flags = PSize;
265 c->flags = size.flags;
266 if(c->flags & PBaseSize) {
267 c->basew = size.base_width;
268 c->baseh = size.base_height;
269 }
270 else
271 c->basew = c->baseh = 0;
272 if(c->flags & PResizeInc) {
273 c->incw = size.width_inc;
274 c->inch = size.height_inc;
275 }
276 else
277 c->incw = c->inch = 0;
278 if(c->flags & PMaxSize) {
279 c->maxw = size.max_width;
280 c->maxh = size.max_height;
281 }
282 else
283 c->maxw = c->maxh = 0;
284 if(c->flags & PMinSize) {
285 c->minw = size.min_width;
286 c->minh = size.min_height;
287 }
288 else
289 c->minw = c->minh = 0;
290 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh &&
291 c->maxw == c->minw && c->maxh == c->minh);
292 }
294 void
295 updatetitle(Client *c) {
296 char **list = NULL;
297 int n;
298 XTextProperty name;
300 name.nitems = 0;
301 c->name[0] = 0;
302 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
303 if(!name.nitems)
304 XGetWMName(dpy, c->win, &name);
305 if(!name.nitems)
306 return;
307 if(name.encoding == XA_STRING)
308 strncpy(c->name, (char *)name.value, sizeof c->name);
309 else {
310 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
311 && n > 0 && *list)
312 {
313 strncpy(c->name, *list, sizeof c->name);
314 XFreeStringList(list);
315 }
316 }
317 XFree(name.value);
318 }
320 void
321 unmanage(Client *c) {
322 Client *nc;
324 /* The server grab construct avoids race conditions. */
325 XGrabServer(dpy);
326 XSetErrorHandler(xerrordummy);
327 detach(c);
328 detachstack(c);
329 if(sel == c) {
330 for(nc = stack; nc && !isvisible(nc); nc = nc->snext);
331 focus(nc);
332 }
333 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
334 XDestroyWindow(dpy, c->twin);
335 free(c->tags);
336 free(c);
337 XSync(dpy, False);
338 XSetErrorHandler(xerror);
339 XUngrabServer(dpy);
340 arrange();
341 }