first version of awesome-menu
[awesome.git] / client.c
blob298b08208567db746cd0acf906a3903223f47262
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>
25 #include <X11/extensions/Xinerama.h>
27 #include "client.h"
28 #include "tag.h"
29 #include "rules.h"
30 #include "window.h"
31 #include "focus.h"
32 #include "ewmh.h"
33 #include "screen.h"
34 #include "widget.h"
35 #include "common/xutil.h"
36 #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(globalconf.display, 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(globalconf.display, c->win,
142 XInternAtom(globalconf.display, "_NET_WM_NAME", False), c->name, sizeof(c->name)))
143 xgettextprop(globalconf.display, c->win,
144 XInternAtom(globalconf.display, "WM_NAME", False), c->name, sizeof(c->name));
146 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
149 static void
150 client_unfocus(Client *c)
152 if(globalconf.screens[c->screen].opacity_unfocused != -1)
153 window_settrans(c->win, globalconf.screens[c->screen].opacity_unfocused);
154 XSetWindowBorder(globalconf.display, c->win,
155 globalconf.screens[c->screen].colors_normal[ColBorder].pixel);
156 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
157 focus_add_client(NULL);
160 /** Ban client and unmap it
161 * \param c the client
163 void
164 client_ban(Client *c)
166 if(globalconf.focus->client == c)
167 client_unfocus(c);
168 XUnmapWindow(globalconf.display, c->win);
169 window_setstate(c->win, IconicState);
172 /** Give focus to client, or to first client if c is NULL
173 * \param c client
174 * \param screen Screen ID
176 void
177 client_focus(Client *c, int screen, Bool raise)
179 int phys_screen = get_phys_screen(screen);
181 /* if c is NULL or invisible, take next client in the focus history */
182 if(!c || (c && !client_isvisible(c, screen)))
184 c = focus_get_current_client(screen);
185 /* if c is still NULL take next client in the stack */
186 if(!c)
187 for(c = globalconf.clients; c && (c->skip || !client_isvisible(c, screen)); c = c->next);
190 /* unfocus current selected client */
191 if(globalconf.focus->client)
192 client_unfocus(globalconf.focus->client);
194 if(c)
196 /* unban the client before focusing or it will fail */
197 client_unban(c);
198 /* save sel in focus history */
199 focus_add_client(c);
200 if(globalconf.screens[c->screen].opacity_unfocused != -1)
201 window_settrans(c->win, -1);
202 XSetWindowBorder(globalconf.display, c->win,
203 globalconf.screens[screen].colors_selected[ColBorder].pixel);
204 XSetInputFocus(globalconf.display, c->win, RevertToPointerRoot, CurrentTime);
205 if(raise)
207 XWindowChanges wc;
208 Layout *curlay = layout_get_current(screen);
209 if(c->isfloating || curlay->arrange == layout_floating)
210 XRaiseWindow(globalconf.display, c->win);
211 else
213 Client *client;
214 wc.stack_mode = Below;
215 wc.sibling = None;
216 for(client = globalconf.clients; client; client = client->next)
217 if(client != c && client_isvisible(client, c->screen) && client->isfloating)
219 XConfigureWindow(globalconf.display, client->win, CWSibling | CWStackMode, &wc);
220 wc.sibling = client->win;
222 XConfigureWindow(globalconf.display, c->win, CWSibling | CWStackMode, &wc);
223 wc.sibling = c->win;
224 for(client = globalconf.clients; client; client = client->next)
225 if(client != c && IS_TILED(client, c->screen))
227 XConfigureWindow(globalconf.display, client->win, CWSibling | CWStackMode, &wc);
228 wc.sibling = client->win;
232 /* since we're dropping EnterWindow events and sometimes the window
233 * will appear under the mouse, grabbuttons */
234 window_grabbuttons(phys_screen, c->win);
236 else
237 XSetInputFocus(globalconf.display,
238 RootWindow(globalconf.display, phys_screen),
239 RevertToPointerRoot, CurrentTime);
241 widget_invalidate_cache(screen, WIDGET_CACHE_CLIENTS);
242 ewmh_update_net_active_window(phys_screen);
245 /** Manage a new client
246 * \param w The window
247 * \param wa Window attributes
248 * \param screen Screen ID
250 void
251 client_manage(Window w, XWindowAttributes *wa, int screen)
253 Client *c, *t = NULL;
254 Window trans;
255 Bool rettrans, retloadprops;
256 XWindowChanges wc;
257 Tag *tag;
258 Rule *rule;
259 Area screen_geom;
260 int phys_screen = get_phys_screen(screen);
261 long flags;
263 c = p_new(Client, 1);
265 c->screen = screen_get_bycoord(screen, wa->x, wa->y);
267 screen_geom = screen_get_area(c->screen,
268 globalconf.screens[screen].statusbar,
269 &globalconf.screens[screen].padding);
270 /* Initial values */
271 c->win = w;
272 c->geometry.x = c->f_geometry.x = c->m_geometry.x = MAX(wa->x, screen_geom.x);
273 c->geometry.y = c->f_geometry.y = c->m_geometry.y = MAX(wa->y, screen_geom.y);
274 c->geometry.width = c->f_geometry.width = c->m_geometry.width = wa->width;
275 c->geometry.height = c->f_geometry.height = c->m_geometry.height = wa->height;
276 c->oldborder = wa->border_width;
277 c->newcomer = True;
279 c->border = globalconf.screens[screen].borderpx;
281 /* Set windows borders */
282 wc.border_width = c->border;
283 XConfigureWindow(globalconf.display, w, CWBorderWidth, &wc);
284 XSetWindowBorder(globalconf.display, w, globalconf.screens[screen].colors_normal[ColBorder].pixel);
285 /* propagates border_width, if size doesn't change */
286 window_configure(c->win, c->geometry, c->border);
288 /* update window title */
289 client_updatetitle(c);
291 /* update hints */
292 flags = client_updatesizehints(c);
293 client_updatewmhints(c);
295 /* Try to load props if any */
296 retloadprops = client_loadprops(c, screen);
298 /* Then check clients hints */
299 ewmh_check_client_hints(c);
301 /* Then apply rules if no props */
302 if(!retloadprops)
304 /* Get the client's rule */
305 if((rule = rule_matching_client(c)))
307 if(rule->screen != RULE_NOSCREEN)
308 move_client_to_screen(c, rule->screen, True);
309 else
310 move_client_to_screen(c, screen, True);
311 tag_client_with_rule(c, rule);
313 switch(rule->isfloating)
315 case Auto:
316 break;
317 case Yes:
318 client_setfloating(c, True);
319 break;
320 case No:
321 client_setfloating(c, False);
322 break;
325 if(rule->opacity >= 0.0f)
326 window_settrans(c->win, rule->opacity);
328 else
329 move_client_to_screen(c, screen, True);
332 /* check for transient and set tags like its parent,
333 * XGetTransientForHint returns 1 on success
335 if((rettrans = XGetTransientForHint(globalconf.display, w, &trans))
336 && (t = client_get_bywin(globalconf.clients, trans)))
337 for(tag = globalconf.screens[c->screen].tags; tag; tag = tag->next)
338 if(is_client_tagged(t, tag))
339 tag_client(c, tag);
341 /* should be floating if transsient or fixed */
342 if(!c->isfloating)
343 client_setfloating(c, rettrans || c->isfixed);
345 if(!(flags & (USPosition | PPosition)))
346 c->f_geometry =
347 globalconf.screens[c->screen].floating_placement(c->f_geometry, c->border, c->screen);
349 XSelectInput(globalconf.display, w, StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
351 /* handle xshape */
352 if(globalconf.have_shape)
354 XShapeSelectInput(globalconf.display, w, ShapeNotifyMask);
355 window_setshape(phys_screen, c->win);
358 /* attach to the stack */
359 if((rule = rule_matching_client(c)))
360 switch(rule->ismaster)
362 case Yes:
363 client_list_push(&globalconf.clients, c);
364 break;
365 case No:
366 client_list_append(&globalconf.clients, c);
367 break;
368 case Auto:
369 rule = NULL;
370 break;
373 if(!rule)
375 if(globalconf.screens[c->screen].new_become_master)
376 client_list_push(&globalconf.clients, c);
377 else
378 client_list_append(&globalconf.clients, c);
381 /* some windows require this */
382 XMoveResizeWindow(globalconf.display, c->win, c->geometry.x, c->geometry.y,
383 c->geometry.width, c->geometry.height);
385 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
386 ewmh_update_net_client_list(phys_screen);
389 /** Resize client window
390 * \param c client to resize
391 * \param geometry new window geometry
392 * \param sizehints respect size hints
393 * \param return True if resize has been done
395 Bool
396 client_resize(Client *c, Area geometry, Bool sizehints)
398 int new_screen;
399 double dx, dy, max, min, ratio;
400 Area area;
401 XWindowChanges wc;
403 if(sizehints)
405 if(c->minay > 0 && c->maxay > 0 && (geometry.height - c->baseh) > 0
406 && (geometry.width - c->basew) > 0)
408 dx = (double) (geometry.width - c->basew);
409 dy = (double) (geometry.height - c->baseh);
410 min = (double) (c->minax) / (double) (c->minay);
411 max = (double) (c->maxax) / (double) (c->maxay);
412 ratio = dx / dy;
413 if(max > 0 && min > 0 && ratio > 0)
415 if(ratio < min)
417 dy = (dx * min + dy) / (min * min + 1);
418 dx = dy * min;
419 geometry.width = (int) dx + c->basew;
420 geometry.height = (int) dy + c->baseh;
422 else if(ratio > max)
424 dy = (dx * min + dy) / (max * max + 1);
425 dx = dy * min;
426 geometry.width = (int) dx + c->basew;
427 geometry.height = (int) dy + c->baseh;
431 if(c->minw && geometry.width < c->minw)
432 geometry.width = c->minw;
433 if(c->minh && geometry.height < c->minh)
434 geometry.height = c->minh;
435 if(c->maxw && geometry.width > c->maxw)
436 geometry.width = c->maxw;
437 if(c->maxh && geometry.height > c->maxh)
438 geometry.height = c->maxh;
439 if(c->incw)
440 geometry.width -= (geometry.width - c->basew) % c->incw;
441 if(c->inch)
442 geometry.height -= (geometry.height - c->baseh) % c->inch;
444 if(geometry.width <= 0 || geometry.height <= 0)
445 return False;
446 /* offscreen appearance fixes */
447 area = get_display_area(get_phys_screen(c->screen),
448 NULL,
449 &globalconf.screens[c->screen].padding);
450 if(geometry.x > area.width)
451 geometry.x = area.width - geometry.width - 2 * c->border;
452 if(geometry.y > area.height)
453 geometry.y = area.height - geometry.height - 2 * c->border;
454 if(geometry.x + geometry.width + 2 * c->border < 0)
455 geometry.x = 0;
456 if(geometry.y + geometry.height + 2 * c->border < 0)
457 geometry.y = 0;
459 if(c->geometry.x != geometry.x || c->geometry.y != geometry.y
460 || c->geometry.width != geometry.width || c->geometry.height != geometry.height)
462 new_screen = screen_get_bycoord(c->screen, geometry.x, geometry.y);
464 c->geometry.x = wc.x = geometry.x;
465 c->geometry.y = wc.y = geometry.y;
466 c->geometry.width = wc.width = geometry.width;
467 c->geometry.height = wc.height = geometry.height;
468 wc.border_width = c->border;
470 /* save the floating geometry if the window is floating but not
471 * maximized */
472 if((c->isfloating ||
473 layout_get_current(new_screen)->arrange == layout_floating) && !c->ismax)
474 c->f_geometry = geometry;
476 XConfigureWindow(globalconf.display, c->win,
477 CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
478 window_configure(c->win, geometry, c->border);
480 if(c->screen != new_screen)
481 move_client_to_screen(c, new_screen, False);
483 return True;
485 return False;
488 void
489 client_setfloating(Client *c, Bool floating)
491 if(c->isfloating != floating)
493 if((c->isfloating = floating))
495 client_resize(c, c->f_geometry, False);
496 XRaiseWindow(globalconf.display, c->win);
498 else
500 XLowerWindow(globalconf.display, c->win);
501 if(c->ismax)
503 c->ismax = False;
504 client_resize(c, c->m_geometry, False);
507 if(client_isvisible(c, c->screen))
508 globalconf.screens[c->screen].need_arrange = True;
509 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
510 client_saveprops(c);
514 /** Save client properties as an X property
515 * \param c client
517 void
518 client_saveprops(Client *c)
520 int i = 0, ntags = 0;
521 char *prop;
522 Tag *tag;
524 for(tag = globalconf.screens[c->screen].tags; tag; tag = tag->next)
525 ntags++;
527 prop = p_new(char, ntags + 2);
529 for(tag = globalconf.screens[c->screen].tags; tag; tag = tag->next, i++)
530 prop[i] = is_client_tagged(c, tag) ? '1' : '0';
532 if(i <= ntags)
533 prop[i] = c->isfloating ? '1' : '0';
535 prop[++i] = '\0';
537 XChangeProperty(globalconf.display, c->win,
538 XInternAtom(globalconf.display, "_AWESOME_PROPERTIES", False),
539 XA_STRING, 8, PropModeReplace, (unsigned char *) prop, i);
541 p_delete(&prop);
544 void
545 client_unban(Client *c)
547 XMapWindow(globalconf.display, c->win);
548 window_setstate(c->win, NormalState);
551 void
552 client_unmanage(Client *c)
554 XWindowChanges wc;
555 Tag *tag;
557 wc.border_width = c->oldborder;
559 /* The server grab construct avoids race conditions. */
560 XGrabServer(globalconf.display);
562 XConfigureWindow(globalconf.display, c->win, CWBorderWidth, &wc); /* restore border */
564 /* remove client everywhere */
565 client_list_detach(&globalconf.clients, c);
566 focus_delete_client(c);
567 if(globalconf.scratch.client == c)
568 globalconf.scratch.client = NULL;
569 for(tag = globalconf.screens[c->screen].tags; tag; tag = tag->next)
570 untag_client(c, tag);
572 if(globalconf.focus->client == c)
573 client_focus(NULL, c->screen, True);
575 XUngrabButton(globalconf.display, AnyButton, AnyModifier, c->win);
576 window_setstate(c->win, WithdrawnState);
578 XSync(globalconf.display, False);
579 XUngrabServer(globalconf.display);
581 p_delete(&c);
584 void
585 client_updatewmhints(Client *c)
587 XWMHints *wmh;
589 if((wmh = XGetWMHints(globalconf.display, c->win)))
591 if((c->isurgent = (wmh->flags & XUrgencyHint)))
592 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
593 if((wmh->flags & StateHint) && wmh->initial_state == WithdrawnState)
595 c->border = 0;
596 c->skip = True;
598 XFree(wmh);
602 long
603 client_updatesizehints(Client *c)
605 long msize;
606 XSizeHints size;
608 if(!XGetWMNormalHints(globalconf.display, c->win, &size, &msize))
609 return 0L;
611 if(size.flags & PBaseSize)
613 c->basew = size.base_width;
614 c->baseh = size.base_height;
616 else if(size.flags & PMinSize)
618 c->basew = size.min_width;
619 c->baseh = size.min_height;
621 else
622 c->basew = c->baseh = 0;
623 if(size.flags & PResizeInc)
625 c->incw = size.width_inc;
626 c->inch = size.height_inc;
628 else
629 c->incw = c->inch = 0;
631 if(size.flags & PMaxSize)
633 c->maxw = size.max_width;
634 c->maxh = size.max_height;
636 else
637 c->maxw = c->maxh = 0;
639 if(size.flags & PMinSize)
641 c->minw = size.min_width;
642 c->minh = size.min_height;
644 else if(size.flags & PBaseSize)
646 c->minw = size.base_width;
647 c->minh = size.base_height;
649 else
650 c->minw = c->minh = 0;
652 if(size.flags & PAspect)
654 c->minax = size.min_aspect.x;
655 c->maxax = size.max_aspect.x;
656 c->minay = size.min_aspect.y;
657 c->maxay = size.max_aspect.y;
659 else
660 c->minax = c->maxax = c->minay = c->maxay = 0;
662 if(c->maxw && c->minw && c->maxh && c->minh
663 && c->maxw == c->minw && c->maxh == c->minh)
664 c->isfixed = True;
666 return size.flags;
669 /** Returns True if a client is tagged
670 * with one of the tags
671 * \return True or False
673 Bool
674 client_isvisible(Client *c, int screen)
676 Tag *tag;
678 if(!c || c->screen != screen)
679 return False;
681 if(globalconf.scratch.client == c)
682 return globalconf.scratch.isvisible;
684 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
685 if(tag->selected && is_client_tagged(c, tag))
686 return True;
687 return False;
690 /** Set selected client transparency
691 * \param screen Screen ID
692 * \param arg unused arg
693 * \ingroup ui_callback
695 void
696 uicb_client_settrans(int screen __attribute__ ((unused)), char *arg)
698 double delta = 100.0, current_opacity = 100.0;
699 unsigned char *data;
700 Atom actual;
701 int format;
702 unsigned long n, left;
703 unsigned int current_opacity_raw = 0;
704 int set_prop = 0;
705 Client *sel = globalconf.focus->client;
707 if(!sel)
708 return;
710 XGetWindowProperty(globalconf.display, sel->win,
711 XInternAtom(globalconf.display, "_NET_WM_WINDOW_OPACITY", False),
712 0L, 1L, False, XA_CARDINAL, &actual, &format, &n, &left,
713 (unsigned char **) &data);
714 if(data)
716 memcpy(&current_opacity_raw, data, sizeof(unsigned int));
717 XFree(data);
718 current_opacity = (current_opacity_raw * 100.0) / 0xffffffff;
720 else
721 set_prop = 1;
723 delta = compute_new_value_from_arg(arg, current_opacity);
725 if(delta <= 0.0)
726 delta = 0.0;
727 else if(delta > 100.0)
729 delta = 100.0;
730 set_prop = 1;
733 if(delta == 100.0 && !set_prop)
734 window_settrans(sel->win, -1);
735 else
736 window_settrans(sel->win, delta);
739 static Client *
740 client_find_prev_visible(Client *sel)
742 Client *prev = NULL;
744 if(!sel) return NULL;
746 /* look for previous starting at sel */
747 for(prev = client_list_prev_cycle(&globalconf.clients, sel);
748 prev && (prev->skip || !client_isvisible(prev, sel->screen));
749 prev = client_list_prev_cycle(&globalconf.clients, prev));
751 return prev;
754 static Client *
755 client_find_next_visible(Client *sel)
757 Client *next = NULL;
759 if(!sel) return NULL;
761 for(next = sel->next; next && !client_isvisible(next, sel->screen); next = next->next);
762 if(!next)
763 for(next = globalconf.clients; next && !client_isvisible(next, sel->screen); next = next->next);
765 return next;
768 /** Swap current with previous client
769 * \param screen Screen ID
770 * \param arg nothing
771 * \ingroup ui_callback
773 void
774 uicb_client_swapprev(int screen __attribute__ ((unused)),
775 char *arg __attribute__ ((unused)))
777 Client *prev;
779 if((prev = client_find_prev_visible(globalconf.focus->client)))
781 client_list_swap(&globalconf.clients, prev, globalconf.focus->client);
782 globalconf.screens[prev->screen].need_arrange = True;
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;
804 /** Move and resize client
805 * \param screen Screen ID
806 * \param arg x y w h
807 * \ingroup ui_callback
809 void
810 uicb_client_moveresize(int screen, char *arg)
812 int ox, oy, ow, oh; /* old geometry */
813 char x[8], y[8], w[8], h[8];
814 int mx, my, dx, dy, nmx, nmy;
815 unsigned int dui;
816 Window dummy;
817 Area area;
818 Client *sel = globalconf.focus->client;
819 Layout *curlay = layout_get_current(screen);
821 if(!sel || sel->isfixed || !arg ||
822 (curlay->arrange != layout_floating && !sel->isfloating))
823 return;
825 if(sscanf(arg, "%s %s %s %s", x, y, w, h) != 4)
826 return;
828 area.x = (int) compute_new_value_from_arg(x, sel->geometry.x);
829 area.y = (int) compute_new_value_from_arg(y, sel->geometry.y);
830 area.width = (int) compute_new_value_from_arg(w, sel->geometry.width);
831 area.height = (int) compute_new_value_from_arg(h, sel->geometry.height);
833 ox = sel->geometry.x;
834 oy = sel->geometry.y;
835 ow = sel->geometry.width;
836 oh = sel->geometry.height;
838 Bool xqp = XQueryPointer(globalconf.display,
839 RootWindow(globalconf.display,
840 get_phys_screen(screen)),
841 &dummy, &dummy, &mx, &my, &dx, &dy, &dui);
842 client_resize(sel, area, globalconf.screens[sel->screen].resize_hints);
843 if (xqp && ox <= mx && (ox + 2 * sel->border + ow) >= mx &&
844 oy <= my && (oy + 2 * sel->border + oh) >= my)
846 nmx = mx - (ox + sel->border) + sel->geometry.width - ow;
847 nmy = my - (oy + sel->border) + sel->geometry.height - oh;
849 if(nmx < -sel->border) /* can happen on a resize */
850 nmx = -sel->border;
851 if(nmy < -sel->border)
852 nmy = -sel->border;
854 XWarpPointer(globalconf.display,
855 None, sel->win,
856 0, 0, 0, 0, nmx, nmy);
860 void
861 client_kill(Client *c)
863 XEvent ev;
865 if(client_isprotodel(globalconf.display, c->win))
867 ev.type = ClientMessage;
868 ev.xclient.window = c->win;
869 ev.xclient.message_type = XInternAtom(globalconf.display, "WM_PROTOCOLS", False);
870 ev.xclient.format = 32;
871 ev.xclient.data.l[0] = XInternAtom(globalconf.display, "WM_DELETE_WINDOW", False);
872 ev.xclient.data.l[1] = CurrentTime;
873 XSendEvent(globalconf.display, c->win, False, NoEventMask, &ev);
875 else
876 XKillClient(globalconf.display, c->win);
879 /** Kill selected client
880 * \param screen Screen ID
881 * \param arg unused
882 * \ingroup ui_callback
884 void
885 uicb_client_kill(int screen __attribute__ ((unused)), char *arg __attribute__ ((unused)))
887 Client *sel = globalconf.focus->client;
889 if(sel)
890 client_kill(sel);
893 static void
894 client_maximize(Client *c, Area geometry)
897 if((c->ismax = !c->ismax))
899 c->wasfloating = c->isfloating;
900 c->m_geometry = c->geometry;
901 if(layout_get_current(c->screen)->arrange != layout_floating)
902 client_setfloating(c, True);
903 client_focus(c, c->screen, True);
904 client_resize(c, geometry, False);
905 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
907 else if(c->wasfloating)
909 client_setfloating(c, True);
910 client_resize(c, c->m_geometry, False);
911 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
913 else if(layout_get_current(c->screen)->arrange == layout_floating)
915 client_resize(c, c->m_geometry, False);
916 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
918 else
920 client_setfloating(c, False);
921 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
925 /** Toggle maximize for client
926 * \param screen Screen ID
927 * \param arg Unused
928 * \ingroup ui_callback
930 void
931 uicb_client_togglemax(int screen, char *arg __attribute__ ((unused)))
933 Client *sel = globalconf.focus->client;
934 Area area = screen_get_area(screen,
935 globalconf.screens[screen].statusbar,
936 &globalconf.screens[screen].padding);
938 if(sel)
940 area.width -= 2 * sel->border;
941 area.height -= 2 * sel->border;
942 client_maximize(sel, area);
946 /** Toggle vertical maximize for client
947 * \param screen Screen ID
948 * \param arg Unused
949 * \ingroup ui_callback
951 void
952 uicb_client_toggleverticalmax(int screen, char *arg __attribute__ ((unused)))
954 Client *sel = globalconf.focus->client;
955 Area area = screen_get_area(screen,
956 globalconf.screens[screen].statusbar,
957 &globalconf.screens[screen].padding);
959 if(sel)
961 area.x = sel->geometry.x;
962 area.width = sel->geometry.width;
963 area.height -= 2 * sel->border;
964 client_maximize(sel, area);
969 /** Toggle horizontal maximize for client
970 * \param screen Screen ID
971 * \param arg Unused
972 * \ingroup ui_callback
974 void
975 uicb_client_togglehorizontalmax(int screen, char *arg __attribute__ ((unused)))
977 Client *sel = globalconf.focus->client;
978 Area area = screen_get_area(screen,
979 globalconf.screens[screen].statusbar,
980 &globalconf.screens[screen].padding);
982 if(sel)
984 area.y = sel->geometry.y;
985 area.height = sel->geometry.height;
986 area.width -= 2 * sel->border;
987 client_maximize(sel, area);
991 /** Zoom client
992 * \param screen Screen ID
993 * \param arg Unused
994 * \ingroup ui_callback
996 void
997 uicb_client_zoom(int screen, char *arg __attribute__ ((unused)))
999 Client *c, *sel = globalconf.focus->client;
1001 if(!sel)
1002 return;
1004 for(c = globalconf.clients; !client_isvisible(c, screen); c = c->next);
1005 if(c == sel)
1006 for(sel = sel->next; sel && !client_isvisible(sel, screen); sel = sel->next);
1008 if(sel)
1010 client_list_detach(&globalconf.clients, sel);
1011 client_list_push(&globalconf.clients, sel);
1012 globalconf.screens[screen].need_arrange = True;
1016 /** Send focus to next client in stack
1017 * \param screen Screen ID
1018 * \param arg Unused
1019 * \ingroup ui_callback
1021 void
1022 uicb_client_focusnext(int screen, char *arg __attribute__ ((unused)))
1024 Client *c, *sel = globalconf.focus->client;
1026 if(!sel)
1027 return;
1028 for(c = sel->next; c && (c->skip || !client_isvisible(c, screen)); c = c->next);
1029 if(!c)
1030 for(c = globalconf.clients; c && (c->skip || !client_isvisible(c, screen)); c = c->next);
1031 if(c)
1032 client_focus(c, screen, True);
1035 /** Send focus to previous client in stack
1036 * \param screen Screen ID
1037 * \param arg Unused
1038 * \ingroup ui_callback
1040 void
1041 uicb_client_focusprev(int screen, char *arg __attribute__ ((unused)))
1043 Client *prev;
1045 if((prev = client_find_prev_visible(globalconf.focus->client)))
1046 client_focus(prev, screen, True);
1049 /** Toggle floating state of a client
1050 * \param screen Screen ID
1051 * \param arg unused
1052 * \ingroup ui_callback
1054 void
1055 uicb_client_togglefloating(int screen __attribute__ ((unused)),
1056 char *arg __attribute__ ((unused)))
1058 if(globalconf.focus->client)
1059 client_setfloating(globalconf.focus->client, !globalconf.focus->client->isfloating);
1062 /** Toggle scratch client attribute
1063 * \param screen screen number
1064 * \param arg unused argument
1065 * \ingroup ui_callback
1067 void
1068 uicb_client_setscratch(int screen,
1069 char *arg __attribute__ ((unused)))
1071 if(!globalconf.focus->client)
1072 return;
1074 if(globalconf.scratch.client == globalconf.focus->client)
1075 globalconf.scratch.client = NULL;
1076 else
1077 globalconf.scratch.client = globalconf.focus->client;
1079 widget_invalidate_cache(screen, WIDGET_CACHE_CLIENTS | WIDGET_CACHE_TAGS);
1080 globalconf.screens[screen].need_arrange = True;
1083 /** Toggle scratch client visibility
1084 * \param screen screen number
1085 * \param arg unused argument
1086 * \ingroup ui_callback
1088 void
1089 uicb_client_togglescratch(int screen,
1090 char *arg __attribute__ ((unused)))
1092 if(globalconf.scratch.client)
1094 globalconf.scratch.isvisible = !globalconf.scratch.isvisible;
1095 if(globalconf.scratch.isvisible)
1096 client_focus(globalconf.scratch.client, screen, True);
1097 globalconf.screens[globalconf.scratch.client->screen].need_arrange = True;
1098 widget_invalidate_cache(globalconf.scratch.client->screen, WIDGET_CACHE_CLIENTS);
1101 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80