cosmetic
[awesome.git] / client.c
blobda598a8f1bae7eafeae250f35b10c86f29522db5
1 /*
2 * client.c - client management
3 *
4 * Copyright © 2007 Julien Danjou <julien@danjou.info>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <stdio.h>
23 #include <X11/Xatom.h>
24 #include <X11/Xutil.h>
25 #include <X11/extensions/shape.h>
27 #include "screen.h"
28 #include "awesome.h"
29 #include "layout.h"
30 #include "tag.h"
31 #include "util.h"
32 #include "statusbar.h"
34 #include "layouts/floating.h"
36 /* extern */
37 extern Client *clients, *sel, *stack; /* global client list and stack */
39 /** Attach client stack to clients stacks
40 * \param c the client
41 */
42 static inline void
43 attachstack(Client * c)
45 c->snext = stack;
46 stack = c;
49 /** Detach client from stack
50 * \param c the client
52 static inline void
53 detachstack(Client * c)
55 Client **tc;
57 for(tc = &stack; *tc && *tc != c; tc = &(*tc)->snext);
58 *tc = c->snext;
61 /** Grab or ungrab buttons when a client is focused
62 * \param c client
63 * \param focused True if client is focused
64 * \param modkey Mod key mask
65 * \param numlockmask Numlock mask
67 static void
68 grabbuttons(Client * c, Bool focused, KeySym modkey, unsigned int numlockmask)
70 XUngrabButton(c->display, AnyButton, AnyModifier, c->win);
72 if(focused)
74 XGrabButton(c->display, Button1, modkey, c->win, False, BUTTONMASK,
75 GrabModeAsync, GrabModeSync, None, None);
76 XGrabButton(c->display, Button1, modkey | LockMask, c->win, False,
77 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
78 XGrabButton(c->display, Button1, modkey | numlockmask, c->win, False,
79 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
80 XGrabButton(c->display, Button1, modkey | numlockmask | LockMask,
81 c->win, False, BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
83 XGrabButton(c->display, Button2, modkey, c->win, False, BUTTONMASK,
84 GrabModeAsync, GrabModeSync, None, None);
85 XGrabButton(c->display, Button2, modkey | LockMask, c->win, False,
86 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
87 XGrabButton(c->display, Button2, modkey | numlockmask, c->win, False,
88 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
89 XGrabButton(c->display, Button2, modkey | numlockmask | LockMask,
90 c->win, False, BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
92 XGrabButton(c->display, Button3, modkey, c->win, False, BUTTONMASK,
93 GrabModeAsync, GrabModeSync, None, None);
94 XGrabButton(c->display, Button3, modkey | LockMask, c->win, False,
95 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
96 XGrabButton(c->display, Button3, modkey | numlockmask, c->win, False,
97 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
98 XGrabButton(c->display, Button3, modkey | numlockmask | LockMask,
99 c->win, False, BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
101 XUngrabButton(c->display, AnyButton, AnyModifier, RootWindow(c->display, c->screen));
103 else
105 XGrabButton(c->display, AnyButton, AnyModifier, c->win, False, BUTTONMASK,
106 GrabModeAsync, GrabModeSync, None, None);
107 XGrabButton(c->display, Button4, NoSymbol, RootWindow(c->display, c->screen), False, BUTTONMASK,
108 GrabModeAsync, GrabModeSync, None, None);
109 XGrabButton(c->display, Button4, LockMask, RootWindow(c->display, c->screen), False, BUTTONMASK,
110 GrabModeAsync, GrabModeSync, None, None);
111 XGrabButton(c->display, Button4, numlockmask, RootWindow(c->display, c->screen), False, BUTTONMASK,
112 GrabModeAsync, GrabModeSync, None, None);
113 XGrabButton(c->display, Button4, numlockmask | LockMask, RootWindow(c->display, c->screen), False, BUTTONMASK,
114 GrabModeAsync, GrabModeSync, None, None);
116 XGrabButton(c->display, Button5, NoSymbol, RootWindow(c->display, c->screen), False, BUTTONMASK,
117 GrabModeAsync, GrabModeSync, None, None);
118 XGrabButton(c->display, Button5, LockMask, RootWindow(c->display, c->screen), False, BUTTONMASK,
119 GrabModeAsync, GrabModeSync, None, None);
120 XGrabButton(c->display, Button5, numlockmask, RootWindow(c->display, c->screen), False, BUTTONMASK,
121 GrabModeAsync, GrabModeSync, None, None);
122 XGrabButton(c->display, Button5, numlockmask | LockMask, RootWindow(c->display, c->screen), False, BUTTONMASK,
123 GrabModeAsync, GrabModeSync, None, None);
128 /** Check if client supports protocol WM_DELETE_WINDOW
129 * \param c the client
130 * \return True if client has WM_DELETE_WINDOW
132 static Bool
133 isprotodel(Client * c)
135 int i, n;
136 Atom *protocols;
137 Bool ret = False;
139 if(XGetWMProtocols(c->display, c->win, &protocols, &n))
141 for(i = 0; !ret && i < n; i++)
142 if(protocols[i] == XInternAtom(c->display, "WM_DELETE_WINDOW", False))
143 ret = True;
144 XFree(protocols);
146 return ret;
149 /** Set client WM_STATE property
150 * \param c the client
151 * \param state no idea
153 static void
154 setclientstate(Client * c, long state)
156 long data[] = { state, None };
158 XChangeProperty(c->display, c->win, XInternAtom(c->display, "WM_STATE", False),
159 XInternAtom(c->display, "WM_STATE", False), 32,
160 PropModeReplace, (unsigned char *) data, 2);
163 /** Set client transparency using composite
164 * \param c client
165 * \param opacity opacity percentage
167 static void
168 setclienttrans(Client *c, double opacity)
170 unsigned int real_opacity = 0xffffffff;
172 if(opacity >= 0 && opacity <= 100)
174 real_opacity = ((opacity / 100.0) * 0xffffffff);
175 XChangeProperty(c->display, c->win,
176 XInternAtom(c->display, "_NET_WM_WINDOW_OPACITY", False),
177 XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &real_opacity, 1L);
179 else
180 XDeleteProperty(c->display, c->win, XInternAtom(c->display, "_NET_WM_WINDOW_OPACITY", False));
182 XSync(c->display, False);
185 /** Attach client to the beginning of the clients stack
186 * \param c the client
188 inline void
189 attach(Client * c)
191 if(clients)
192 clients->prev = c;
193 c->next = clients;
194 clients = c;
197 inline void
198 updatetitle(Client * c)
200 if(!xgettextprop(c->display, c->win, XInternAtom(c->display, "_NET_WM_NAME", False), c->name, sizeof c->name))
201 xgettextprop(c->display, c->win, XInternAtom(c->display, "WM_NAME", False), c->name, sizeof c->name);
204 /** Ban client
205 * \param c the client
207 void
208 ban(Client * c)
210 if(c->isbanned)
211 return;
212 XUnmapWindow(c->display, c->win);
213 setclientstate(c, IconicState);
214 c->isbanned = True;
215 c->unmapped++;
218 /** Configure client
219 * \param c the client
221 void
222 configure(Client * c)
224 XConfigureEvent ce;
226 ce.type = ConfigureNotify;
227 ce.display = c->display;
228 ce.event = c->win;
229 ce.window = c->win;
230 ce.x = c->x;
231 ce.y = c->y;
232 ce.width = c->w;
233 ce.height = c->h;
234 ce.border_width = c->border;
235 ce.above = None;
236 ce.override_redirect = False;
237 XSendEvent(c->display, c->win, False, StructureNotifyMask, (XEvent *) & ce);
240 void
241 detach(Client * c)
243 if(c->prev)
244 c->prev->next = c->next;
245 if(c->next)
246 c->next->prev = c->prev;
247 if(c == clients)
248 clients = c->next;
249 c->next = c->prev = NULL;
252 /** Give focus to client, or to first client if c is NULL
253 * \param disp Display ref
254 * \param screen Screen number
255 * \param drawcontext drawcontext ref
256 * \param c client
257 * \param selscreen True if current screen is selected
258 * \param awesomeconf awesome config
260 void
261 focus(Display *disp, int screen, DC *drawcontext, Client * c, Bool selscreen, awesome_config *awesomeconf)
263 /* if c is NULL or invisible, take next client in the stack */
264 if((!c && selscreen) || (c && !isvisible(c, screen, awesomeconf->selected_tags, awesomeconf->ntags)))
265 for(c = stack; c && !isvisible(c, screen, awesomeconf->selected_tags, awesomeconf->ntags); c = c->snext);
267 /* if a client was selected but it's not the current client, unfocus it */
268 if(sel && sel != c)
270 grabbuttons(sel, False, awesomeconf->modkey, awesomeconf->numlockmask);
271 XSetWindowBorder(sel->display, sel->win, drawcontext->norm[ColBorder]);
272 setclienttrans(sel, awesomeconf->opacity_unfocused);
274 if(c)
276 detachstack(c);
277 attachstack(c);
278 grabbuttons(c, True, awesomeconf->modkey, awesomeconf->numlockmask);
280 if(!selscreen)
281 return;
282 sel = c;
283 drawstatusbar(disp, screen, drawcontext, awesomeconf);
284 if(sel)
286 XSetWindowBorder(sel->display, sel->win, drawcontext->sel[ColBorder]);
287 XSetInputFocus(sel->display, sel->win, RevertToPointerRoot, CurrentTime);
288 for(c = stack; c; c = c->snext)
289 if(c != sel)
290 setclienttrans(c, awesomeconf->opacity_unfocused);
291 setclienttrans(sel, -1);
293 else
294 XSetInputFocus(disp, RootWindow(disp, screen), RevertToPointerRoot, CurrentTime);
297 /** Kill selected client
298 * \param disp Display ref
299 * \param awesomeconf awesome config
300 * \param arg unused
301 * \ingroup ui_callback
303 void
304 uicb_killclient(Display *disp __attribute__ ((unused)),
305 int screen __attribute__ ((unused)),
306 DC *drawcontext __attribute__ ((unused)),
307 awesome_config *awesomeconf __attribute__ ((unused)),
308 const char *arg __attribute__ ((unused)))
310 XEvent ev;
312 if(!sel)
313 return;
314 if(isprotodel(sel))
316 ev.type = ClientMessage;
317 ev.xclient.window = sel->win;
318 ev.xclient.message_type = XInternAtom(disp, "WM_PROTOCOLS", False);
319 ev.xclient.format = 32;
320 ev.xclient.data.l[0] = XInternAtom(disp, "WM_DELETE_WINDOW", False);
321 ev.xclient.data.l[1] = CurrentTime;
322 XSendEvent(sel->display, sel->win, False, NoEventMask, &ev);
324 else
325 XKillClient(sel->display, sel->win);
328 static Bool
329 loadprops(Client * c, int ntags)
331 int i;
332 char *prop;
333 Bool result = False;
335 prop = p_new(char, ntags + 2);
337 if(xgettextprop(c->display, c->win, AWESOMEPROPS_ATOM(c->display), prop, ntags + 2))
339 for(i = 0; i < ntags && prop[i]; i++)
340 if((c->tags[i] = prop[i] == '1'))
341 result = True;
342 if(i <= ntags && prop[i])
343 c->isfloating = prop[i] == '1';
346 p_delete(&prop);
348 return result;
351 void
352 manage(Display * disp, int screen, DC *drawcontext, Window w, XWindowAttributes * wa, awesome_config *awesomeconf)
354 int i;
355 Client *c, *t = NULL;
356 Window trans;
357 Status rettrans;
358 XWindowChanges wc;
360 c = p_new(Client, 1);
361 c->tags = p_new(Bool, awesomeconf->ntags);
362 c->win = w;
363 c->ftview = True;
364 c->x = c->rw = wa->x;
365 c->y = c->ry = wa->y;
366 c->w = c->rw = wa->width;
367 c->h = c->rh = wa->height;
368 c->oldborder = wa->border_width;
369 c->display = disp;
370 c->screen = screen;
371 if(c->w == DisplayWidth(disp, screen)
372 && c->h == DisplayHeight(disp, screen))
374 c->x = 0;
375 c->y = 0;
376 c->border = wa->border_width;
378 else
380 ScreenInfo *si = get_display_info(disp, screen, awesomeconf->statusbar);
382 if(c->x + c->w + 2 * c->border > si->x_org + si->width)
383 c->x = c->rx = si->x_org + si->width - c->w - 2 * c->border;
384 if(c->y + c->h + 2 * c->border > si->y_org + si->height)
385 c->y = c->ry = si->y_org + si->height - c->h - 2 * c->border;
386 if(c->x < si->x_org)
387 c->x = c->rx = si->x_org;
388 if(c->y < si->y_org)
389 c->y = c->ry = si->y_org;
390 c->border = awesomeconf->borderpx;
391 XFree(si);
393 wc.border_width = c->border;
394 XConfigureWindow(disp, w, CWBorderWidth, &wc);
395 XSetWindowBorder(disp, w, drawcontext->norm[ColBorder]);
396 configure(c); /* propagates border_width, if size doesn't change */
397 updatesizehints(c);
398 XSelectInput(disp, w, StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
399 if(awesomeconf->have_shape)
401 XShapeSelectInput(disp, w, ShapeNotifyMask);
402 set_shape(c);
404 grabbuttons(c, False, awesomeconf->modkey, awesomeconf->numlockmask);
405 updatetitle(c);
406 if((rettrans = XGetTransientForHint(disp, w, &trans) == Success))
407 for(t = clients; t && t->win != trans; t = t->next);
408 if(t)
409 for(i = 0; i < awesomeconf->ntags; i++)
410 c->tags[i] = t->tags[i];
411 if(!loadprops(c, awesomeconf->ntags))
412 applyrules(c, awesomeconf);
413 if(!c->isfloating)
414 c->isfloating = (rettrans == Success) || c->isfixed;
415 saveprops(c, awesomeconf->ntags);
416 attach(c);
417 attachstack(c);
418 XMoveResizeWindow(disp, c->win, c->x, c->y, c->w, c->h); /* some windows require this */
419 c->isbanned = True;
420 arrange(disp, screen, drawcontext, awesomeconf);
423 void
424 resize(Client * c, int x, int y, int w, int h, Bool sizehints)
426 double dx, dy, max, min, ratio;
427 XWindowChanges wc;
429 if(sizehints)
431 if(c->minay > 0 && c->maxay > 0 && (h - c->baseh) > 0 && (w - c->basew) > 0)
433 dx = (double) (w - c->basew);
434 dy = (double) (h - c->baseh);
435 min = (double) (c->minax) / (double) (c->minay);
436 max = (double) (c->maxax) / (double) (c->maxay);
437 ratio = dx / dy;
438 if(max > 0 && min > 0 && ratio > 0)
440 if(ratio < min)
442 dy = (dx * min + dy) / (min * min + 1);
443 dx = dy * min;
444 w = (int) dx + c->basew;
445 h = (int) dy + c->baseh;
447 else if(ratio > max)
449 dy = (dx * min + dy) / (max * max + 1);
450 dx = dy * min;
451 w = (int) dx + c->basew;
452 h = (int) dy + c->baseh;
456 if(c->minw && w < c->minw)
457 w = c->minw;
458 if(c->minh && h < c->minh)
459 h = c->minh;
460 if(c->maxw && w > c->maxw)
461 w = c->maxw;
462 if(c->maxh && h > c->maxh)
463 h = c->maxh;
464 if(c->incw)
465 w -= (w - c->basew) % c->incw;
466 if(c->inch)
467 h -= (h - c->baseh) % c->inch;
469 if(w <= 0 || h <= 0)
470 return;
471 /* offscreen appearance fixes */
472 if(x > DisplayWidth(c->display, c->screen))
473 x = DisplayWidth(c->display, c->screen) - w - 2 * c->border;
474 if(y > DisplayHeight(c->display, c->screen))
475 y = DisplayHeight(c->display, c->screen) - h - 2 * c->border;
476 if(x + w + 2 * c->border < 0)
477 x = 0;
478 if(y + h + 2 * c->border < 0)
479 y = 0;
480 if(c->x != x || c->y != y || c->w != w || c->h != h)
482 c->x = wc.x = x;
483 c->y = wc.y = y;
484 c->w = wc.width = w;
485 c->h = wc.height = h;
486 wc.border_width = c->border;
487 XConfigureWindow(c->display, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
488 configure(c);
489 XSync(c->display, False);
493 void
494 uicb_moveresize(Display *disp __attribute__ ((unused)),
495 int screen __attribute__ ((unused)),
496 DC *drawcontext __attribute__ ((unused)),
497 awesome_config *awesomeconf,
498 const char *arg)
500 int x, y, w, h, nx, ny, nw, nh, ox, oy, ow, oh;
501 char xabs, yabs, wabs, habs;
502 int mx, my, dx, dy, nmx, nmy;
503 unsigned int dui;
504 Window dummy;
506 if(!IS_ARRANGE(floating))
507 if(!sel || !sel->isfloating || sel->isfixed || !arg)
508 return;
509 if(sscanf(arg, "%d%c %d%c %d%c %d%c", &x, &xabs, &y, &yabs, &w, &wabs, &h, &habs) != 8)
510 return;
511 nx = xabs == 'x' ? sel->x + x : x;
512 ny = yabs == 'y' ? sel->y + y : y;
513 nw = wabs == 'w' ? sel->w + w : w;
514 nh = habs == 'h' ? sel->h + h : h;
516 ox = sel->x;
517 oy = sel->y;
518 ow = sel->w;
519 oh = sel->h;
521 Bool xqp = XQueryPointer(sel->display, RootWindow(sel->display, sel->screen), &dummy, &dummy, &mx, &my, &dx, &dy, &dui);
522 resize(sel, nx, ny, nw, nh, True);
523 if (xqp && ox <= mx && (ox + ow) >= mx && oy <= my && (oy + oh) >= my)
525 nmx = mx-ox+sel->w-ow-1 < 0 ? 0 : mx-ox+sel->w-ow-1;
526 nmy = my-oy+sel->h-oh-1 < 0 ? 0 : my-oy+sel->h-oh-1;
527 XWarpPointer(sel->display, None, sel->win, 0, 0, 0, 0, nmx, nmy);
531 void
532 saveprops(Client * c, int ntags)
534 int i;
535 char *prop;
537 prop = p_new(char, ntags + 2);
539 for(i = 0; i < ntags; i++)
540 prop[i] = c->tags[i] ? '1' : '0';
542 if(i <= ntags)
543 prop[i] = c->isfloating ? '1' : '0';
545 prop[++i] = '\0';
547 XChangeProperty(c->display, c->win, AWESOMEPROPS_ATOM(c->display), XA_STRING, 8,
548 PropModeReplace, (unsigned char *) prop, i);
550 p_delete(&prop);
553 void
554 unban(Client * c)
556 if(!c->isbanned)
557 return;
558 XMapWindow(c->display, c->win);
559 setclientstate(c, NormalState);
560 c->isbanned = False;
563 void
564 unmanage(Client * c, DC *drawcontext, long state, awesome_config *awesomeconf)
566 XWindowChanges wc;
568 wc.border_width = c->oldborder;
569 /* The server grab construct avoids race conditions. */
570 XGrabServer(c->display);
571 XConfigureWindow(c->display, c->win, CWBorderWidth, &wc); /* restore border */
572 detach(c);
573 detachstack(c);
574 if(sel == c)
575 focus(c->display, c->screen, drawcontext, NULL, True, awesomeconf);
576 XUngrabButton(c->display, AnyButton, AnyModifier, c->win);
577 setclientstate(c, state);
578 XSync(c->display, False);
579 XSetErrorHandler(xerror);
580 XUngrabServer(c->display);
581 if(state != NormalState)
582 arrange(c->display, c->screen, drawcontext, awesomeconf);
583 p_delete(&c->tags);
584 p_delete(&c);
587 void
588 updatesizehints(Client * c)
590 long msize;
591 XSizeHints size;
593 if(!XGetWMNormalHints(c->display, c->win, &size, &msize) || !size.flags)
594 size.flags = PSize;
595 c->flags = size.flags;
596 if(c->flags & PBaseSize)
598 c->basew = size.base_width;
599 c->baseh = size.base_height;
601 else if(c->flags & PMinSize)
603 c->basew = size.min_width;
604 c->baseh = size.min_height;
606 else
607 c->basew = c->baseh = 0;
608 if(c->flags & PResizeInc)
610 c->incw = size.width_inc;
611 c->inch = size.height_inc;
613 else
614 c->incw = c->inch = 0;
616 if(c->flags & PMaxSize)
618 c->maxw = size.max_width;
619 c->maxh = size.max_height;
621 else
622 c->maxw = c->maxh = 0;
624 if(c->flags & PMinSize)
626 c->minw = size.min_width;
627 c->minh = size.min_height;
629 else if(c->flags & PBaseSize)
631 c->minw = size.base_width;
632 c->minh = size.base_height;
634 else
635 c->minw = c->minh = 0;
637 if(c->flags & PAspect)
639 c->minax = size.min_aspect.x;
640 c->maxax = size.max_aspect.x;
641 c->minay = size.min_aspect.y;
642 c->maxay = size.max_aspect.y;
644 else
645 c->minax = c->maxax = c->minay = c->maxay = 0;
647 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
648 && c->maxw == c->minw && c->maxh == c->minh);
651 void
652 set_shape(Client *c)
654 int bounding_shaped;
655 int i, b; unsigned int u; /* dummies */
656 /* Logic to decide if we have a shaped window cribbed from fvwm-2.5.10. */
657 if (XShapeQueryExtents(c->display, c->win, &bounding_shaped, &i, &i,
658 &u, &u, &b, &i, &i, &u, &u) && bounding_shaped)
659 XShapeCombineShape(c->display, RootWindow(c->display, c->screen), ShapeBounding, 0, 0, c->win, ShapeBounding, ShapeSet);
662 void
663 uicb_settrans(Display *disp __attribute__ ((unused)),
664 int screen __attribute__ ((unused)),
665 DC *drawcontext __attribute__ ((unused)),
666 awesome_config *awesomeconf __attribute__ ((unused)),
667 const char *arg)
669 double delta = 100.0, current_opacity = 0.0;
670 unsigned char *data;
671 Atom actual;
672 int format;
673 unsigned long n, left;
674 unsigned int current_opacity_raw = 0;
675 int set_prop = 0;
677 if(!sel)
678 return;
680 XGetWindowProperty(sel->display, sel->win, XInternAtom(sel->display, "_NET_WM_WINDOW_OPACITY", False),
681 0L, 1L, False, XA_CARDINAL, &actual, &format, &n, &left,
682 (unsigned char **) &data);
683 if(data)
685 memcpy(&current_opacity_raw, data, sizeof(unsigned int));
686 XFree(data);
687 current_opacity = (current_opacity_raw * 100.0) / 0xffffffff;
689 else
690 set_prop = 1;
692 delta = compute_new_value_from_arg(arg, current_opacity);
694 if(delta <= 0.0)
695 delta = 0.0;
696 else if(delta > 100.0)
698 delta = 100.0;
699 set_prop = 1;
702 if(delta == 100.0 && !set_prop)
703 setclienttrans(sel, -1);
704 else
705 setclienttrans(sel, delta);