reuse cairo context stuff
[awesome.git] / client.c
blob9f5ae32160a51083d79d1aceca8942ee2242dfea
1 /*
2 * client.c - client management
4 * Copyright © 2007-2008 Julien Danjou <julien@danjou.info>
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/extensions/shape.h>
26 #include "client.h"
27 #include "tag.h"
28 #include "rules.h"
29 #include "window.h"
30 #include "focus.h"
31 #include "ewmh.h"
32 #include "screen.h"
33 #include "widget.h"
34 #include "xutil.h"
35 #include "layouts/floating.h"
38 extern AwesomeConf globalconf;
40 /** Load windows properties, restoring client's tag
41 * and floating state before awesome was restarted if any
42 * \todo this may bug if number of tags is != than before
43 * \param c Client ref
44 * \param screen Screen ID
45 * \return true if client had property
47 static Bool
48 client_loadprops(Client * c, int screen)
50 int i, ntags = 0;
51 Tag *tag;
52 char *prop;
53 Bool result = False;
55 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
56 ntags++;
58 prop = p_new(char, ntags + 2);
60 if(xgettextprop(c->win,
61 XInternAtom(globalconf.display, "_AWESOME_PROPERTIES", False),
62 prop, ntags + 2))
64 for(i = 0, tag = globalconf.screens[screen].tags; tag && i < ntags && prop[i]; i++, tag = tag->next)
65 if(prop[i] == '1')
67 tag_client(c, tag);
68 result = True;
70 else
71 untag_client(c, tag);
73 if(i <= ntags && prop[i])
74 client_setfloating(c, prop[i] == '1');
77 p_delete(&prop);
79 return result;
82 /** Check if client supports protocol WM_DELETE_WINDOW
83 * \param disp the display
84 * \param win the Window
85 * \return True if client has WM_DELETE_WINDOW
87 static Bool
88 client_isprotodel(Display *disp, Window win)
90 int i, n;
91 Atom *protocols;
92 Bool ret = False;
94 if(XGetWMProtocols(disp, win, &protocols, &n))
96 for(i = 0; !ret && i < n; i++)
97 if(protocols[i] == XInternAtom(disp, "WM_DELETE_WINDOW", False))
98 ret = True;
99 XFree(protocols);
101 return ret;
104 /** Get a Client by its window
105 * \param list Client list to look info
106 * \param w Client window to find
107 * \return client
109 Client *
110 client_get_bywin(Client *list, Window w)
112 Client *c;
114 for(c = list; c && c->win != w; c = c->next);
115 return c;
118 /** Get a client by its name
119 * \param list Client list
120 * \param name name to search
121 * \return first matching client
123 Client *
124 client_get_byname(Client *list, char *name)
126 Client *c;
128 for(c = list; c; c = c->next)
129 if(strstr(c->name, name))
130 return c;
132 return NULL;
135 /** Update client name attribute with its title
136 * \param c the client
138 void
139 client_updatetitle(Client *c)
141 if(!xgettextprop(c->win, XInternAtom(globalconf.display, "_NET_WM_NAME", False), c->name, sizeof(c->name)))
142 xgettextprop(c->win, XInternAtom(globalconf.display, "WM_NAME", False), c->name, sizeof(c->name));
144 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
147 static void
148 client_unfocus(Client *c)
150 XSetWindowBorder(globalconf.display, c->win,
151 globalconf.screens[c->screen].colors_normal[ColBorder].pixel);
152 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
153 focus_add_client(NULL);
156 /** Ban client and unmap it
157 * \param c the client
159 void
160 client_ban(Client *c)
162 if(globalconf.focus->client == c)
163 client_unfocus(c);
164 XUnmapWindow(globalconf.display, c->win);
165 window_setstate(c->win, IconicState);
168 /** Give focus to client, or to first client if c is NULL
169 * \param c client
170 * \param screen Screen ID
172 void
173 client_focus(Client *c, int screen, Bool from_mouse)
175 int phys_screen = get_phys_screen(screen);
177 /* if c is NULL or invisible, take next client in the focus history */
178 if(!c || (c && !client_isvisible(c, screen)))
180 c = focus_get_current_client(screen);
181 /* if c is still NULL take next client in the stack */
182 if(!c)
183 for(c = globalconf.clients; c && (c->skip || !client_isvisible(c, screen)); c = c->next);
186 /* unfocus current selected client */
187 if(globalconf.focus->client)
188 client_unfocus(globalconf.focus->client);
190 if(c)
192 /* save sel in focus history */
193 focus_add_client(c);
194 XSetWindowBorder(globalconf.display, c->win,
195 globalconf.screens[screen].colors_selected[ColBorder].pixel);
196 XSetInputFocus(globalconf.display, c->win, RevertToPointerRoot, CurrentTime);
197 if(!from_mouse
198 || !globalconf.screens[screen].sloppy_focus
199 || globalconf.screens[screen].sloppy_focus_raise)
200 XRaiseWindow(globalconf.display, c->win);
201 /* since we're dropping EnterWindow events and sometimes the window
202 * will appear under the mouse, grabbuttons */
203 window_grabbuttons(phys_screen, c->win);
205 else
206 XSetInputFocus(globalconf.display,
207 RootWindow(globalconf.display, phys_screen),
208 RevertToPointerRoot, CurrentTime);
210 widget_invalidate_cache(screen, WIDGET_CACHE_CLIENTS);
211 ewmh_update_net_active_window(phys_screen);
212 globalconf.drop_events |= EnterWindowMask;
215 /** Compute smart coordinates for a client window
216 * \param geometry current/requested client geometry
217 * \param screen screen used
218 * \return new geometry
220 static Area
221 client_get_smart_geometry(Area geometry, int border, int screen)
223 Client *c;
224 Area newgeometry = { 0, 0, 0, 0, NULL };
225 Area *screen_geometry, *arealist = NULL, *r;
226 Bool found = False;
228 screen_geometry = p_new(Area, 1);
230 *screen_geometry = screen_get_area(screen,
231 globalconf.screens[screen].statusbar,
232 &globalconf.screens[screen].padding);
234 area_list_push(&arealist, screen_geometry);
236 for(c = globalconf.clients; c; c = c->next)
237 if(client_isvisible(c, screen))
239 newgeometry = c->f_geometry;
240 newgeometry.width += 2 * c->border;
241 newgeometry.height += 2 * c->border;
242 area_list_remove(&arealist, &newgeometry);
245 newgeometry.x = geometry.x;
246 newgeometry.y = geometry.y;
247 newgeometry.width = 0;
248 newgeometry.height = 0;
250 for(r = arealist; r; r = r->next)
251 if(r->width >= geometry.width && r->height >= geometry.height
252 && r->width * r->height > newgeometry.width * newgeometry.height)
254 found = True;
255 newgeometry = *r;
258 /* we did not found a space with enough space for our size:
259 * just take the biggest available and go in */
260 if(!found)
261 for(r = arealist; r; r = r->next)
262 if(r->width * r->height > newgeometry.width * newgeometry.height)
263 newgeometry = *r;
265 /* restore height and width */
266 newgeometry.width = geometry.width;
267 newgeometry.height = geometry.height;
269 /* fix offscreen */
270 if(AREA_RIGHT(newgeometry) > AREA_RIGHT(*screen_geometry))
271 newgeometry.x = screen_geometry->x + screen_geometry->width - newgeometry.width - 2 * border;
273 if(AREA_BOTTOM(newgeometry) > AREA_BOTTOM(*screen_geometry))
274 newgeometry.y = screen_geometry->y + screen_geometry->height - newgeometry.height - 2 * border;
276 area_list_wipe(&arealist);
278 return newgeometry;
281 /** Manage a new client
282 * \param w The window
283 * \param wa Window attributes
284 * \param screen Screen ID
286 void
287 client_manage(Window w, XWindowAttributes *wa, int screen)
289 Client *c, *t = NULL;
290 Window trans;
291 Bool rettrans;
292 XWindowChanges wc;
293 Tag *tag;
294 Rule *rule;
295 Area screen_geom;
296 int phys_screen = get_phys_screen(screen);
298 c = p_new(Client, 1);
300 c->screen = screen_get_bycoord(wa->x, wa->y);
302 screen_geom = screen_get_area(c->screen,
303 globalconf.screens[screen].statusbar,
304 &globalconf.screens[screen].padding);
305 /* Initial values */
306 c->win = w;
307 c->geometry.x = c->f_geometry.x = c->m_geometry.x = MAX(wa->x, screen_geom.x);
308 c->geometry.y = c->f_geometry.y = c->m_geometry.y = MAX(wa->y, screen_geom.y);
309 c->geometry.width = c->f_geometry.width = c->m_geometry.width = wa->width;
310 c->geometry.height = c->f_geometry.height = c->m_geometry.height = wa->height;
311 c->oldborder = wa->border_width;
312 c->newcomer = True;
314 c->border = globalconf.screens[screen].borderpx;
316 /* Set windows borders */
317 wc.border_width = c->border;
318 XConfigureWindow(globalconf.display, w, CWBorderWidth, &wc);
319 XSetWindowBorder(globalconf.display, w, globalconf.screens[screen].colors_normal[ColBorder].pixel);
320 /* propagates border_width, if size doesn't change */
321 window_configure(c->win, c->geometry, c->border);
323 /* update window title */
324 client_updatetitle(c);
326 /* update hints */
327 client_updatesizehints(c);
328 client_updatewmhints(c);
330 /* First check clients hints */
331 ewmh_check_client_hints(c);
333 /* loadprops or apply rules if no props */
334 if(!client_loadprops(c, screen))
336 /* Get the client's rule */
337 if((rule = rule_matching_client(c)))
339 if(rule->screen != RULE_NOSCREEN)
340 move_client_to_screen(c, rule->screen, True);
341 else
342 move_client_to_screen(c, screen, True);
343 tag_client_with_rule(c, rule);
345 switch(rule->isfloating)
347 case Auto:
348 break;
349 case Float:
350 client_setfloating(c, True);
351 break;
352 case Tile:
353 client_setfloating(c, False);
354 break;
357 if(rule->opacity >= 0.0f)
358 window_settrans(c->win, rule->opacity);
360 else
361 move_client_to_screen(c, screen, True);
362 /* check for transient and set tags like its parent,
363 * XGetTransientForHint returns 1 on success
365 if((rettrans = XGetTransientForHint(globalconf.display, w, &trans))
366 && (t = client_get_bywin(globalconf.clients, trans)))
367 for(tag = globalconf.screens[c->screen].tags; tag; tag = tag->next)
368 if(is_client_tagged(t, tag))
369 tag_client(c, tag);
371 /* should be floating if transsient or fixed */
372 if(!c->isfloating)
373 client_setfloating(c, rettrans || c->isfixed);
376 c->f_geometry = client_get_smart_geometry(c->f_geometry, c->border, c->screen);
378 XSelectInput(globalconf.display, w, StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
380 /* handle xshape */
381 if(globalconf.have_shape)
383 XShapeSelectInput(globalconf.display, w, ShapeNotifyMask);
384 window_setshape(phys_screen, c->win);
387 /* attach to the stack */
388 if((rule = rule_matching_client(c)) && rule->not_master)
389 client_list_append(&globalconf.clients, c);
390 else if(globalconf.screens[c->screen].new_become_master)
391 client_list_push(&globalconf.clients, c);
392 else
393 client_list_append(&globalconf.clients, c);
395 /* some windows require this */
396 XMoveResizeWindow(globalconf.display, c->win, c->geometry.x, c->geometry.y,
397 c->geometry.width, c->geometry.height);
399 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
400 ewmh_update_net_client_list(phys_screen);
403 /** Resize client window
404 * \param c client to resize
405 * \param geometry new window geometry
406 * \param sizehints respect size hints
407 * \param return True if resize has been done
409 Bool
410 client_resize(Client *c, Area geometry, Bool sizehints)
412 int new_screen;
413 double dx, dy, max, min, ratio;
414 Area area;
415 XWindowChanges wc;
417 if(sizehints)
419 if(c->minay > 0 && c->maxay > 0 && (geometry.height - c->baseh) > 0
420 && (geometry.width - c->basew) > 0)
422 dx = (double) (geometry.width - c->basew);
423 dy = (double) (geometry.height - c->baseh);
424 min = (double) (c->minax) / (double) (c->minay);
425 max = (double) (c->maxax) / (double) (c->maxay);
426 ratio = dx / dy;
427 if(max > 0 && min > 0 && ratio > 0)
429 if(ratio < min)
431 dy = (dx * min + dy) / (min * min + 1);
432 dx = dy * min;
433 geometry.width = (int) dx + c->basew;
434 geometry.height = (int) dy + c->baseh;
436 else if(ratio > max)
438 dy = (dx * min + dy) / (max * max + 1);
439 dx = dy * min;
440 geometry.width = (int) dx + c->basew;
441 geometry.height = (int) dy + c->baseh;
445 if(c->minw && geometry.width < c->minw)
446 geometry.width = c->minw;
447 if(c->minh && geometry.height < c->minh)
448 geometry.height = c->minh;
449 if(c->maxw && geometry.width > c->maxw)
450 geometry.width = c->maxw;
451 if(c->maxh && geometry.height > c->maxh)
452 geometry.height = c->maxh;
453 if(c->incw)
454 geometry.width -= (geometry.width - c->basew) % c->incw;
455 if(c->inch)
456 geometry.height -= (geometry.height - c->baseh) % c->inch;
458 if(geometry.width <= 0 || geometry.height <= 0)
459 return False;
460 /* offscreen appearance fixes */
461 area = get_display_area(get_phys_screen(c->screen),
462 NULL,
463 &globalconf.screens[c->screen].padding);
464 if(geometry.x > area.width)
465 geometry.x = area.width - geometry.width - 2 * c->border;
466 if(geometry.y > area.height)
467 geometry.y = area.height - geometry.height - 2 * c->border;
468 if(geometry.x + geometry.width + 2 * c->border < 0)
469 geometry.x = 0;
470 if(geometry.y + geometry.height + 2 * c->border < 0)
471 geometry.y = 0;
473 if(c->geometry.x != geometry.x || c->geometry.y != geometry.y
474 || c->geometry.width != geometry.width || c->geometry.height != geometry.height)
476 new_screen = screen_get_bycoord(geometry.x, geometry.y);
478 c->geometry.x = wc.x = geometry.x;
479 c->geometry.y = wc.y = geometry.y;
480 c->geometry.width = wc.width = geometry.width;
481 c->geometry.height = wc.height = geometry.height;
482 wc.border_width = c->border;
484 /* save the floating geometry if the window is floating but not
485 * maximized */
486 if((c->isfloating ||
487 get_current_layout(new_screen)->arrange == layout_floating) && !c->ismax)
488 c->f_geometry = geometry;
490 XConfigureWindow(globalconf.display, c->win,
491 CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
492 window_configure(c->win, geometry, c->border);
494 if(c->screen != new_screen)
495 move_client_to_screen(c, new_screen, False);
497 return True;
499 return False;
502 void
503 client_setfloating(Client *c, Bool floating)
505 if(c->isfloating != floating)
507 if((c->isfloating = floating))
508 client_resize(c, c->f_geometry, False);
509 else if(c->ismax)
511 c->ismax = False;
512 client_resize(c, c->m_geometry, False);
514 if(client_isvisible(c, c->screen))
515 globalconf.screens[c->screen].need_arrange = True;
516 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
517 client_saveprops(c);
521 /** Save client properties as an X property
522 * \param c client
524 void
525 client_saveprops(Client *c)
527 int i = 0, ntags = 0;
528 char *prop;
529 Tag *tag;
531 for(tag = globalconf.screens[c->screen].tags; tag; tag = tag->next)
532 ntags++;
534 prop = p_new(char, ntags + 2);
536 for(tag = globalconf.screens[c->screen].tags; tag; tag = tag->next, i++)
537 prop[i] = is_client_tagged(c, tag) ? '1' : '0';
539 if(i <= ntags)
540 prop[i] = c->isfloating ? '1' : '0';
542 prop[++i] = '\0';
544 XChangeProperty(globalconf.display, c->win,
545 XInternAtom(globalconf.display, "_AWESOME_PROPERTIES", False),
546 XA_STRING, 8, PropModeReplace, (unsigned char *) prop, i);
548 p_delete(&prop);
551 void
552 client_unban(Client *c)
554 XMapWindow(globalconf.display, c->win);
555 window_setstate(c->win, NormalState);
558 void
559 client_unmanage(Client *c)
561 XWindowChanges wc;
562 Tag *tag;
564 wc.border_width = c->oldborder;
566 /* The server grab construct avoids race conditions. */
567 XGrabServer(globalconf.display);
569 XConfigureWindow(globalconf.display, c->win, CWBorderWidth, &wc); /* restore border */
571 /* remove client everywhere */
572 client_list_detach(&globalconf.clients, c);
573 focus_delete_client(c);
574 for(tag = globalconf.screens[c->screen].tags; tag; tag = tag->next)
575 untag_client(c, tag);
577 if(globalconf.focus->client == c)
578 client_focus(NULL, c->screen, False);
580 XUngrabButton(globalconf.display, AnyButton, AnyModifier, c->win);
581 window_setstate(c->win, WithdrawnState);
583 XSync(globalconf.display, False);
584 XUngrabServer(globalconf.display);
586 p_delete(&c);
589 void
590 client_updatewmhints(Client *c)
592 XWMHints *wmh;
594 if((wmh = XGetWMHints(globalconf.display, c->win)))
596 if((c->isurgent = (wmh->flags & XUrgencyHint)))
597 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
598 if((wmh->flags & StateHint) && wmh->initial_state == WithdrawnState)
600 c->border = 0;
601 c->skip = True;
603 XFree(wmh);
607 void
608 client_updatesizehints(Client *c)
610 long msize;
611 XSizeHints size;
613 if(!XGetWMNormalHints(globalconf.display, c->win, &size, &msize) || !size.flags)
614 size.flags = PSize;
615 if(size.flags & PBaseSize)
617 c->basew = size.base_width;
618 c->baseh = size.base_height;
620 else if(size.flags & PMinSize)
622 c->basew = size.min_width;
623 c->baseh = size.min_height;
625 else
626 c->basew = c->baseh = 0;
627 if(size.flags & PResizeInc)
629 c->incw = size.width_inc;
630 c->inch = size.height_inc;
632 else
633 c->incw = c->inch = 0;
635 if(size.flags & PMaxSize)
637 c->maxw = size.max_width;
638 c->maxh = size.max_height;
640 else
641 c->maxw = c->maxh = 0;
643 if(size.flags & PMinSize)
645 c->minw = size.min_width;
646 c->minh = size.min_height;
648 else if(size.flags & PBaseSize)
650 c->minw = size.base_width;
651 c->minh = size.base_height;
653 else
654 c->minw = c->minh = 0;
656 if(size.flags & PAspect)
658 c->minax = size.min_aspect.x;
659 c->maxax = size.max_aspect.x;
660 c->minay = size.min_aspect.y;
661 c->maxay = size.max_aspect.y;
663 else
664 c->minax = c->maxax = c->minay = c->maxay = 0;
666 if(c->maxw && c->minw && c->maxh && c->minh
667 && c->maxw == c->minw && c->maxh == c->minh)
668 c->isfixed = True;
671 /** Returns True if a client is tagged
672 * with one of the tags
673 * \return True or False
675 Bool
676 client_isvisible(Client *c, int screen)
678 Tag *tag;
680 if(!c || c->screen != screen)
681 return False;
683 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
684 if(tag->selected && is_client_tagged(c, tag))
685 return True;
686 return False;
689 /** Set selected client transparency
690 * \param screen Screen ID
691 * \param arg unused arg
692 * \ingroup ui_callback
694 void
695 uicb_client_settrans(int screen __attribute__ ((unused)), char *arg)
697 double delta = 100.0, current_opacity = 100.0;
698 unsigned char *data;
699 Atom actual;
700 int format;
701 unsigned long n, left;
702 unsigned int current_opacity_raw = 0;
703 int set_prop = 0;
704 Client *sel = globalconf.focus->client;
706 if(!sel)
707 return;
709 XGetWindowProperty(globalconf.display, sel->win,
710 XInternAtom(globalconf.display, "_NET_WM_WINDOW_OPACITY", False),
711 0L, 1L, False, XA_CARDINAL, &actual, &format, &n, &left,
712 (unsigned char **) &data);
713 if(data)
715 memcpy(&current_opacity_raw, data, sizeof(unsigned int));
716 XFree(data);
717 current_opacity = (current_opacity_raw * 100.0) / 0xffffffff;
719 else
720 set_prop = 1;
722 delta = compute_new_value_from_arg(arg, current_opacity);
724 if(delta <= 0.0)
725 delta = 0.0;
726 else if(delta > 100.0)
728 delta = 100.0;
729 set_prop = 1;
732 if(delta == 100.0 && !set_prop)
733 window_settrans(sel->win, -1);
734 else
735 window_settrans(sel->win, delta);
738 static Client *
739 client_find_prev_visible(Client *sel)
741 Client *prev = NULL;
743 if(!sel) return NULL;
745 /* look for previous starting at sel */
746 for(prev = client_list_prev_cycle(&globalconf.clients, sel);
747 prev && (prev->skip || !client_isvisible(prev, sel->screen));
748 prev = client_list_prev_cycle(&globalconf.clients, prev));
750 return prev;
753 static Client *
754 client_find_next_visible(Client *sel)
756 Client *next = NULL;
758 if(!sel) return NULL;
760 for(next = sel->next; next && !client_isvisible(next, sel->screen); next = next->next);
761 if(!next)
762 for(next = globalconf.clients; next && !client_isvisible(next, sel->screen); next = next->next);
764 return next;
767 /** Swap current with previous client
768 * \param screen Screen ID
769 * \param arg nothing
770 * \ingroup ui_callback
772 void
773 uicb_client_swapprev(int screen __attribute__ ((unused)),
774 char *arg __attribute__ ((unused)))
776 Client *prev;
778 if((prev = client_find_prev_visible(globalconf.focus->client)))
780 client_list_swap(&globalconf.clients, prev, globalconf.focus->client);
781 globalconf.screens[prev->screen].need_arrange = True;
782 globalconf.drop_events |= EnterWindowMask;
786 /** Swap current with next client
787 * \param screen Screen ID
788 * \param arg nothing
789 * \ingroup ui_callback
791 void
792 uicb_client_swapnext(int screen __attribute__ ((unused)),
793 char *arg __attribute__ ((unused)))
795 Client *next;
797 if((next = client_find_next_visible(globalconf.focus->client)))
799 client_list_swap(&globalconf.clients, globalconf.focus->client, next);
800 globalconf.screens[next->screen].need_arrange = True;
801 globalconf.drop_events |= EnterWindowMask;
805 /** Move and resize client
806 * \param screen Screen ID
807 * \param arg x y w h
808 * \ingroup ui_callback
810 void
811 uicb_client_moveresize(int screen, char *arg)
813 int ox, oy, ow, oh;
814 char x[8], y[8], w[8], h[8];
815 int mx, my, dx, dy, nmx, nmy;
816 unsigned int dui;
817 Window dummy;
818 Area area;
819 Client *sel = globalconf.focus->client;
820 Tag **curtags = tags_get_current(screen);
822 if(curtags[0]->layout->arrange != layout_floating)
823 if(!sel || !sel->isfloating || sel->isfixed || !arg)
825 p_delete(&curtags);
826 return;
828 p_delete(&curtags);
829 if(sscanf(arg, "%s %s %s %s", x, y, w, h) != 4)
830 return;
831 area.x = (int) compute_new_value_from_arg(x, sel->geometry.x);
832 area.y = (int) compute_new_value_from_arg(y, sel->geometry.y);
833 area.width = (int) compute_new_value_from_arg(w, sel->geometry.width);
834 area.height = (int) compute_new_value_from_arg(h, sel->geometry.height);
836 ox = sel->geometry.x;
837 oy = sel->geometry.y;
838 ow = sel->geometry.width;
839 oh = sel->geometry.height;
841 Bool xqp = XQueryPointer(globalconf.display,
842 RootWindow(globalconf.display,
843 get_phys_screen(screen)),
844 &dummy, &dummy, &mx, &my, &dx, &dy, &dui);
845 client_resize(sel, area, globalconf.screens[sel->screen].resize_hints);
846 if (xqp && ox <= mx && (ox + ow) >= mx && oy <= my && (oy + oh) >= my)
848 nmx = mx - ox + sel->geometry.width - ow - 1 < 0 ? 0 : mx - ox + sel->geometry.width - ow - 1;
849 nmy = my - oy + sel->geometry.height - oh - 1 < 0 ? 0 : my - oy + sel->geometry.height - oh - 1;
850 XWarpPointer(globalconf.display,
851 None, sel->win,
852 0, 0, 0, 0, nmx, nmy);
856 void
857 client_kill(Client *c)
859 XEvent ev;
861 if(client_isprotodel(globalconf.display, c->win))
863 ev.type = ClientMessage;
864 ev.xclient.window = c->win;
865 ev.xclient.message_type = XInternAtom(globalconf.display, "WM_PROTOCOLS", False);
866 ev.xclient.format = 32;
867 ev.xclient.data.l[0] = XInternAtom(globalconf.display, "WM_DELETE_WINDOW", False);
868 ev.xclient.data.l[1] = CurrentTime;
869 XSendEvent(globalconf.display, c->win, False, NoEventMask, &ev);
871 else
872 XKillClient(globalconf.display, c->win);
875 /** Kill selected client
876 * \param screen Screen ID
877 * \param arg unused
878 * \ingroup ui_callback
880 void
881 uicb_client_kill(int screen __attribute__ ((unused)), char *arg __attribute__ ((unused)))
883 Client *sel = globalconf.focus->client;
885 if(sel)
886 client_kill(sel);
889 static void
890 client_maximize(Client *c, Area geometry)
893 if((c->ismax = !c->ismax))
895 c->wasfloating = c->isfloating;
896 c->m_geometry = c->geometry;
897 if(get_current_layout(c->screen)->arrange != layout_floating)
898 client_setfloating(c, True);
899 client_resize(c, geometry, False);
900 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
902 else if(c->wasfloating)
904 client_setfloating(c, True);
905 client_resize(c, c->m_geometry, False);
906 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
908 else if(get_current_layout(c->screen)->arrange == layout_floating)
910 client_resize(c, c->m_geometry, False);
911 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
913 else
915 client_setfloating(c, False);
916 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
920 /** Toggle maximize for client
921 * \param screen Screen ID
922 * \param arg Unused
923 * \ingroup ui_callback
925 void
926 uicb_client_togglemax(int screen, char *arg __attribute__ ((unused)))
928 Client *sel = globalconf.focus->client;
929 Area area = screen_get_area(screen,
930 globalconf.screens[screen].statusbar,
931 &globalconf.screens[screen].padding);
933 if(sel)
935 area.width -= 2 * sel->border;
936 area.height -= 2 * sel->border;
937 client_maximize(sel, area);
941 /** Toggle vertical maximize for client
942 * \param screen Screen ID
943 * \param arg Unused
944 * \ingroup ui_callback
946 void
947 uicb_client_toggleverticalmax(int screen, char *arg __attribute__ ((unused)))
949 Client *sel = globalconf.focus->client;
950 Area area = screen_get_area(screen,
951 globalconf.screens[screen].statusbar,
952 &globalconf.screens[screen].padding);
954 if(sel)
956 area.x = sel->geometry.x;
957 area.width = sel->geometry.width;
958 area.height -= 2 * sel->border;
959 client_maximize(sel, area);
964 /** Toggle horizontal maximize for client
965 * \param screen Screen ID
966 * \param arg Unused
967 * \ingroup ui_callback
969 void
970 uicb_client_togglehorizontalmax(int screen, char *arg __attribute__ ((unused)))
972 Client *sel = globalconf.focus->client;
973 Area area = screen_get_area(screen,
974 globalconf.screens[screen].statusbar,
975 &globalconf.screens[screen].padding);
977 if(sel)
979 area.y = sel->geometry.y;
980 area.height = sel->geometry.height;
981 area.width -= 2 * sel->border;
982 client_maximize(sel, area);
986 /** Zoom client
987 * \param screen Screen ID
988 * \param arg Unused
989 * \ingroup ui_callback
991 void
992 uicb_client_zoom(int screen, char *arg __attribute__ ((unused)))
994 Client *c, *sel = globalconf.focus->client;
996 for(c = globalconf.clients; !client_isvisible(c, screen); c = c->next);
997 if(c == sel)
998 for(sel = sel->next; sel && !client_isvisible(sel, screen); sel = sel->next);
1000 if(!sel)
1001 return;
1003 client_list_detach(&globalconf.clients, sel);
1004 client_list_push(&globalconf.clients, sel);
1005 globalconf.screens[screen].need_arrange = True;
1006 globalconf.drop_events |= EnterWindowMask;
1009 /** Send focus to next client in stack
1010 * \param screen Screen ID
1011 * \param arg Unused
1012 * \ingroup ui_callback
1014 void
1015 uicb_client_focusnext(int screen, char *arg __attribute__ ((unused)))
1017 Client *c, *sel = globalconf.focus->client;
1019 if(!sel)
1020 return;
1021 for(c = sel->next; c && (c->skip || !client_isvisible(c, screen)); c = c->next);
1022 if(!c)
1023 for(c = globalconf.clients; c && (c->skip || !client_isvisible(c, screen)); c = c->next);
1024 if(c)
1025 client_focus(c, screen, False);
1028 /** Send focus to previous client in stack
1029 * \param screen Screen ID
1030 * \param arg Unused
1031 * \ingroup ui_callback
1033 void
1034 uicb_client_focusprev(int screen, char *arg __attribute__ ((unused)))
1036 Client *prev;
1038 if((prev = client_find_prev_visible(globalconf.focus->client)))
1039 client_focus(prev, screen, False);
1042 /** Toggle floating state of a client
1043 * \param screen Screen ID
1044 * \param arg unused
1045 * \ingroup ui_callback
1047 void
1048 uicb_client_togglefloating(int screen __attribute__ ((unused)),
1049 char *arg __attribute__ ((unused)))
1051 if(globalconf.focus->client)
1052 client_setfloating(globalconf.focus->client, !globalconf.focus->client->isfloating);
1055 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80