aewl
view client.c @ 232:98e9901b1dbb
disallow zoom on maximized clients
author | Anselm R.Garbe <arg@10ksloc.org> |
---|---|
date | Thu, 10 Aug 2006 10:16:12 +0200 |
parents | b92bbc2487c9 |
children | e5390f8e06b9 |
line source
1 /*
2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
3 * See LICENSE file for license details.
4 */
5 #include "dwm.h"
6 #include <stdlib.h>
7 #include <string.h>
8 #include <X11/Xatom.h>
9 #include <X11/Xutil.h>
11 /* static functions */
13 static void
14 resizetitle(Client *c)
15 {
16 int i;
18 c->tw = 0;
19 for(i = 0; i < ntags; i++)
20 if(c->tags[i])
21 c->tw += textw(tags[i]);
22 c->tw += textw(c->name);
23 if(c->tw > c->w)
24 c->tw = c->w + 2;
25 c->tx = c->x + c->w - c->tw + 2;
26 c->ty = c->y;
27 if(c->tags[tsel])
28 XMoveResizeWindow(dpy, c->title, c->tx, c->ty, c->tw, c->th);
29 else
30 XMoveResizeWindow(dpy, c->title, c->tx + 2 * sw, c->ty, c->tw, c->th);
32 }
34 static int
35 xerrordummy(Display *dsply, XErrorEvent *ee)
36 {
37 return 0;
38 }
40 /* extern functions */
42 void
43 ban(Client *c)
44 {
45 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
46 XMoveWindow(dpy, c->title, c->tx + 2 * sw, c->ty);
47 }
49 void
50 focus(Client *c)
51 {
52 Client *old = sel;
53 XEvent ev;
55 sel = c;
56 if(old && old != c)
57 drawtitle(old);
58 drawtitle(c);
59 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
60 XSync(dpy, False);
61 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
62 }
64 void
65 focusnext(Arg *arg)
66 {
67 Client *c;
69 if(!sel)
70 return;
72 if(sel->ismax)
73 togglemax(NULL);
75 if(!(c = getnext(sel->next)))
76 c = getnext(clients);
77 if(c) {
78 higher(c);
79 focus(c);
80 }
81 }
83 void
84 focusprev(Arg *arg)
85 {
86 Client *c;
88 if(!sel)
89 return;
91 if(sel->ismax)
92 togglemax(NULL);
94 if(!(c = getprev(sel->prev))) {
95 for(c = clients; c && c->next; c = c->next);
96 c = getprev(c);
97 }
98 if(c) {
99 higher(c);
100 focus(c);
101 }
102 }
104 Client *
105 getclient(Window w)
106 {
107 Client *c;
109 for(c = clients; c; c = c->next)
110 if(c->win == w)
111 return c;
112 return NULL;
113 }
115 Client *
116 getctitle(Window w)
117 {
118 Client *c;
120 for(c = clients; c; c = c->next)
121 if(c->title == w)
122 return c;
123 return NULL;
124 }
126 void
127 gravitate(Client *c, Bool invert)
128 {
129 int dx = 0, dy = 0;
131 switch(c->grav) {
132 default:
133 break;
134 case StaticGravity:
135 case NorthWestGravity:
136 case NorthGravity:
137 case NorthEastGravity:
138 dy = c->border;
139 break;
140 case EastGravity:
141 case CenterGravity:
142 case WestGravity:
143 dy = -(c->h / 2) + c->border;
144 break;
145 case SouthEastGravity:
146 case SouthGravity:
147 case SouthWestGravity:
148 dy = -(c->h);
149 break;
150 }
152 switch (c->grav) {
153 default:
154 break;
155 case StaticGravity:
156 case NorthWestGravity:
157 case WestGravity:
158 case SouthWestGravity:
159 dx = c->border;
160 break;
161 case NorthGravity:
162 case CenterGravity:
163 case SouthGravity:
164 dx = -(c->w / 2) + c->border;
165 break;
166 case NorthEastGravity:
167 case EastGravity:
168 case SouthEastGravity:
169 dx = -(c->w + c->border);
170 break;
171 }
173 if(invert) {
174 dx = -dx;
175 dy = -dy;
176 }
177 c->x += dx;
178 c->y += dy;
179 }
181 void
182 higher(Client *c)
183 {
184 XRaiseWindow(dpy, c->win);
185 XRaiseWindow(dpy, c->title);
186 }
188 void
189 killclient(Arg *arg)
190 {
191 if(!sel)
192 return;
193 if(sel->proto & PROTODELWIN)
194 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
195 else
196 XKillClient(dpy, sel->win);
197 }
199 void
200 manage(Window w, XWindowAttributes *wa)
201 {
202 Client *c;
203 Window trans;
204 XSetWindowAttributes twa;
206 c = emallocz(sizeof(Client));
207 c->tags = emallocz(ntags * sizeof(Bool));
208 c->win = w;
209 c->x = c->tx = wa->x;
210 c->y = c->ty = wa->y;
211 c->w = c->tw = wa->width;
212 c->h = wa->height;
213 c->th = bh;
215 c->border = 0;
216 setsize(c);
218 if(c->h != sh && c->y < bh)
219 c->y = c->ty = bh;
221 c->proto = getproto(c->win);
222 XSelectInput(dpy, c->win,
223 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
224 XGetTransientForHint(dpy, c->win, &trans);
225 twa.override_redirect = 1;
226 twa.background_pixmap = ParentRelative;
227 twa.event_mask = ExposureMask | EnterWindowMask;
229 c->title = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
230 0, DefaultDepth(dpy, screen), CopyFromParent,
231 DefaultVisual(dpy, screen),
232 CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
234 if(clients)
235 clients->prev = c;
236 c->next = clients;
237 clients = c;
239 XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
240 GrabModeAsync, GrabModeSync, None, None);
241 XGrabButton(dpy, Button1, MODKEY | LockMask, c->win, False, BUTTONMASK,
242 GrabModeAsync, GrabModeSync, None, None);
243 XGrabButton(dpy, Button1, MODKEY | NUMLOCKMASK, c->win, False, BUTTONMASK,
244 GrabModeAsync, GrabModeSync, None, None);
245 XGrabButton(dpy, Button1, MODKEY | NUMLOCKMASK | LockMask, c->win, False, BUTTONMASK,
246 GrabModeAsync, GrabModeSync, None, None);
248 XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
249 GrabModeAsync, GrabModeSync, None, None);
250 XGrabButton(dpy, Button2, MODKEY | LockMask, c->win, False, BUTTONMASK,
251 GrabModeAsync, GrabModeSync, None, None);
252 XGrabButton(dpy, Button2, MODKEY | NUMLOCKMASK, c->win, False, BUTTONMASK,
253 GrabModeAsync, GrabModeSync, None, None);
254 XGrabButton(dpy, Button2, MODKEY | NUMLOCKMASK | LockMask, c->win, False, BUTTONMASK,
255 GrabModeAsync, GrabModeSync, None, None);
257 XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
258 GrabModeAsync, GrabModeSync, None, None);
259 XGrabButton(dpy, Button3, MODKEY | LockMask, c->win, False, BUTTONMASK,
260 GrabModeAsync, GrabModeSync, None, None);
261 XGrabButton(dpy, Button3, MODKEY | NUMLOCKMASK, c->win, False, BUTTONMASK,
262 GrabModeAsync, GrabModeSync, None, None);
263 XGrabButton(dpy, Button3, MODKEY | NUMLOCKMASK | LockMask, c->win, False, BUTTONMASK,
264 GrabModeAsync, GrabModeSync, None, None);
266 settags(c);
267 if(!c->isfloat)
268 c->isfloat = trans
269 || (c->maxw && c->minw &&
270 c->maxw == c->minw && c->maxh == c->minh);
271 settitle(c);
272 arrange(NULL);
274 /* mapping the window now prevents flicker */
275 XMapRaised(dpy, c->win);
276 XMapRaised(dpy, c->title);
277 if(c->tags[tsel])
278 focus(c);
279 }
281 void
282 resize(Client *c, Bool sizehints, Corner sticky)
283 {
284 int bottom = c->y + c->h;
285 int right = c->x + c->w;
286 /*XConfigureEvent e;*/
287 XWindowChanges wc;
289 if(sizehints) {
290 if(c->incw)
291 c->w -= (c->w - c->basew) % c->incw;
292 if(c->inch)
293 c->h -= (c->h - c->baseh) % c->inch;
294 if(c->minw && c->w < c->minw)
295 c->w = c->minw;
296 if(c->minh && c->h < c->minh)
297 c->h = c->minh;
298 if(c->maxw && c->w > c->maxw)
299 c->w = c->maxw;
300 if(c->maxh && c->h > c->maxh)
301 c->h = c->maxh;
302 }
303 if(c->x > right) /* might happen on restart */
304 c->x = right - c->w;
305 if(c->y > bottom)
306 c->y = bottom - c->h;
307 if(sticky == TopRight || sticky == BotRight)
308 c->x = right - c->w;
309 if(sticky == BotLeft || sticky == BotRight)
310 c->y = bottom - c->h;
312 resizetitle(c);
313 wc.x = c->x;
314 wc.y = c->y;
315 wc.width = c->w;
316 wc.height = c->h;
317 if(c->w == sw && c->h == sh)
318 wc.border_width = 0;
319 else
320 wc.border_width = 1;
321 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
322 XSync(dpy, False);
323 }
325 void
326 setsize(Client *c)
327 {
328 long msize;
329 XSizeHints size;
331 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
332 size.flags = PSize;
333 c->flags = size.flags;
334 if(c->flags & PBaseSize) {
335 c->basew = size.base_width;
336 c->baseh = size.base_height;
337 }
338 else
339 c->basew = c->baseh = 0;
340 if(c->flags & PResizeInc) {
341 c->incw = size.width_inc;
342 c->inch = size.height_inc;
343 }
344 else
345 c->incw = c->inch = 0;
346 if(c->flags & PMaxSize) {
347 c->maxw = size.max_width;
348 c->maxh = size.max_height;
349 }
350 else
351 c->maxw = c->maxh = 0;
352 if(c->flags & PMinSize) {
353 c->minw = size.min_width;
354 c->minh = size.min_height;
355 }
356 else
357 c->minw = c->minh = 0;
358 if(c->flags & PWinGravity)
359 c->grav = size.win_gravity;
360 else
361 c->grav = NorthWestGravity;
362 }
364 void
365 settitle(Client *c)
366 {
367 char **list = NULL;
368 int n;
369 XTextProperty name;
371 name.nitems = 0;
372 c->name[0] = 0;
373 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
374 if(!name.nitems)
375 XGetWMName(dpy, c->win, &name);
376 if(!name.nitems)
377 return;
378 if(name.encoding == XA_STRING)
379 strncpy(c->name, (char *)name.value, sizeof(c->name));
380 else {
381 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
382 && n > 0 && *list)
383 {
384 strncpy(c->name, *list, sizeof(c->name));
385 XFreeStringList(list);
386 }
387 }
388 XFree(name.value);
389 resizetitle(c);
390 }
392 void
393 togglemax(Arg *arg)
394 {
395 int ox, oy, ow, oh;
396 XEvent ev;
398 if(!sel)
399 return;
401 if((sel->ismax = !sel->ismax)) {
402 ox = sel->x;
403 oy = sel->y;
404 ow = sel->w;
405 oh = sel->h;
406 sel->x = sx;
407 sel->y = sy + bh;
408 sel->w = sw - 2;
409 sel->h = sh - 2 - bh;
411 higher(sel);
412 resize(sel, False, TopLeft);
414 sel->x = ox;
415 sel->y = oy;
416 sel->w = ow;
417 sel->h = oh;
418 }
419 else
420 resize(sel, False, TopLeft);
421 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
422 }
424 void
425 unmanage(Client *c)
426 {
427 XGrabServer(dpy);
428 XSetErrorHandler(xerrordummy);
430 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
431 XDestroyWindow(dpy, c->title);
433 if(c->prev)
434 c->prev->next = c->next;
435 if(c->next)
436 c->next->prev = c->prev;
437 if(c == clients)
438 clients = c->next;
439 if(sel == c) {
440 sel = getnext(c->next);
441 if(!sel)
442 sel = getprev(c->prev);
443 if(!sel)
444 sel = clients;
445 }
446 free(c->tags);
447 free(c);
449 XSync(dpy, False);
450 XSetErrorHandler(xerror);
451 XUngrabServer(dpy);
452 arrange(NULL);
453 if(sel)
454 focus(sel);
455 }
457 void
458 zoom(Arg *arg)
459 {
460 Client *c;
462 if(!sel || (arrange != dotile) || sel->isfloat)
463 return;
465 if(sel == getnext(clients)) {
466 if((c = getnext(sel->next)))
467 sel = c;
468 else
469 return;
470 }
472 /* pop */
473 if(sel->prev)
474 sel->prev->next = sel->next;
475 if(sel->next)
476 sel->next->prev = sel->prev;
477 sel->prev = NULL;
478 if(clients)
479 clients->prev = sel;
480 sel->next = clients;
481 clients = sel;
482 arrange(NULL);
483 focus(sel);
484 }