[widgets/graph] offset fixed now.
[awesome.git] / client.c
blobf166f5e3fd2fddce4de828977f360e97e4ff7dab
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 "widget.h"
33 #include "screen.h"
34 #include "titlebar.h"
35 #include "layouts/floating.h"
36 #include "common/xutil.h"
37 #include "common/xscreen.h"
39 extern AwesomeConf globalconf;
41 /** Load windows properties, restoring client's tag
42 * and floating state before awesome was restarted if any
43 * \todo this may bug if number of tags is != than before
44 * \param c Client ref
45 * \param screen Screen ID
46 * \return true if client had property
48 static Bool
49 client_loadprops(Client * c, int screen)
51 int i, ntags = 0;
52 Tag *tag;
53 char *prop;
54 Bool result = False;
56 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
57 ntags++;
59 prop = p_new(char, ntags + 2);
61 if(xgettextprop(globalconf.display, c->win,
62 XInternAtom(globalconf.display, "_AWESOME_PROPERTIES", False),
63 prop, ntags + 2))
65 for(i = 0, tag = globalconf.screens[screen].tags; tag && i < ntags && prop[i]; i++, tag = tag->next)
66 if(prop[i] == '1')
68 tag_client(c, tag);
69 result = True;
71 else
72 untag_client(c, tag);
74 if(i <= ntags && prop[i])
75 client_setfloating(c, prop[i] == '1');
78 p_delete(&prop);
80 return result;
83 /** Check if client supports protocol WM_DELETE_WINDOW
84 * \param disp the display
85 * \param win the Window
86 * \return True if client has WM_DELETE_WINDOW
88 static Bool
89 client_isprotodel(Display *disp, Window win)
91 int i, n;
92 Atom *protocols;
93 Bool ret = False;
95 if(XGetWMProtocols(disp, win, &protocols, &n))
97 for(i = 0; !ret && i < n; i++)
98 if(protocols[i] == XInternAtom(disp, "WM_DELETE_WINDOW", False))
99 ret = True;
100 XFree(protocols);
102 return ret;
105 /** Get a Client by its window
106 * \param list Client list to look info
107 * \param w Client window to find
108 * \return client
110 Client *
111 client_get_bywin(Client *list, Window w)
113 Client *c;
115 for(c = list; c && c->win != w; c = c->next);
116 return c;
119 /** Get a client by its name
120 * \param list Client list
121 * \param name name to search
122 * \return first matching client
124 Client *
125 client_get_byname(Client *list, char *name)
127 Client *c;
129 for(c = list; c; c = c->next)
130 if(strstr(c->name, name))
131 return c;
133 return NULL;
136 /** Update client name attribute with its title
137 * \param c the client
139 void
140 client_updatetitle(Client *c)
142 if(!xgettextprop(globalconf.display, c->win,
143 XInternAtom(globalconf.display, "_NET_WM_NAME", False), c->name, sizeof(c->name)))
144 xgettextprop(globalconf.display, c->win,
145 XInternAtom(globalconf.display, "WM_NAME", False), c->name, sizeof(c->name));
147 titlebar_draw(c);
149 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
152 static void
153 client_unfocus(Client *c)
155 if(globalconf.screens[c->screen].opacity_unfocused != -1)
156 window_settrans(c->win, globalconf.screens[c->screen].opacity_unfocused);
157 else if(globalconf.screens[c->screen].opacity_focused != -1)
158 window_settrans(c->win, -1);
159 focus_add_client(NULL);
160 XSetWindowBorder(globalconf.display, c->win,
161 globalconf.screens[c->screen].styles.normal.border.pixel);
162 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
163 titlebar_draw(c);
166 /** Ban client and unmap it
167 * \param c the client
169 void
170 client_ban(Client *c)
172 if(globalconf.focus->client == c)
173 client_unfocus(c);
174 XUnmapWindow(globalconf.display, c->win);
175 window_setstate(c->win, IconicState);
176 if(c->titlebar.position && c->titlebar.sw)
177 XUnmapWindow(globalconf.display, c->titlebar.sw->window);
180 /** Give focus to client, or to first client if c is NULL
181 * \param c client
182 * \param screen Screen ID
183 * \param raise raise window if true
184 * \return true if a window (even root) has received focus, false otherwise
186 Bool
187 client_focus(Client *c, int screen, Bool raise)
189 int phys_screen;
191 /* if c is NULL or invisible, take next client in the focus history */
192 if((!c || (c && (!client_isvisible(c, screen))))
193 && !(c = focus_get_current_client(screen)))
194 /* if c is still NULL take next client in the stack */
195 for(c = globalconf.clients; c && (c->skip || !client_isvisible(c, screen)); c = c->next);
197 /* if c is already the focused window, then stop */
198 if(c == globalconf.focus->client)
199 return False;
201 /* unfocus current selected client */
202 if(globalconf.focus->client)
203 client_unfocus(globalconf.focus->client);
205 if(c)
207 /* unban the client before focusing or it will fail */
208 client_unban(c);
209 /* save sel in focus history */
210 focus_add_client(c);
211 if(globalconf.screens[c->screen].opacity_focused != -1)
212 window_settrans(c->win, globalconf.screens[c->screen].opacity_focused);
213 else if(globalconf.screens[c->screen].opacity_unfocused != -1)
214 window_settrans(c->win, -1);
215 XSetWindowBorder(globalconf.display, c->win,
216 globalconf.screens[screen].styles.focus.border.pixel);
217 titlebar_draw(c);
218 XSetInputFocus(globalconf.display, c->win, RevertToPointerRoot, CurrentTime);
219 if(raise)
220 client_stack(c);
221 /* since we're dropping EnterWindow events and sometimes the window
222 * will appear under the mouse, grabbuttons */
223 window_grabbuttons(c->win, c->phys_screen);
224 phys_screen = c->phys_screen;
226 else
228 phys_screen = screen_virttophys(screen);
229 XSetInputFocus(globalconf.display,
230 RootWindow(globalconf.display, phys_screen),
231 RevertToPointerRoot, CurrentTime);
234 ewmh_update_net_active_window(phys_screen);
235 widget_invalidate_cache(screen, WIDGET_CACHE_CLIENTS);
237 return True;
240 void
241 client_stack(Client *c)
243 XWindowChanges wc;
244 Layout *curlay = layout_get_current(c->screen);
246 if(c->isfloating || curlay->arrange == layout_floating)
248 XRaiseWindow(globalconf.display, c->win);
249 if(c->titlebar.position && c->titlebar.sw)
250 XRaiseWindow(globalconf.display, c->titlebar.sw->window);
252 else
254 Client *client;
255 wc.stack_mode = Below;
256 wc.sibling = None;
257 for(client = globalconf.clients; client; client = client->next)
258 if(client != c && client_isvisible(client, c->screen) && client->isfloating)
260 if(client->titlebar.position && client->titlebar.sw)
262 XConfigureWindow(globalconf.display, client->titlebar.sw->window,
263 CWSibling | CWStackMode, &wc);
264 wc.sibling = client->titlebar.sw->window;
266 XConfigureWindow(globalconf.display, client->win, CWSibling | CWStackMode, &wc);
267 wc.sibling = client->win;
269 if(c->titlebar.position && c->titlebar.sw)
271 XConfigureWindow(globalconf.display, c->titlebar.sw->window,
272 CWSibling | CWStackMode, &wc);
273 wc.sibling = c->titlebar.sw->window;
275 XConfigureWindow(globalconf.display, c->win, CWSibling | CWStackMode, &wc);
276 wc.sibling = c->win;
277 for(client = globalconf.clients; client; client = client->next)
278 if(client != c && IS_TILED(client, c->screen))
280 if(client->titlebar.position && client->titlebar.sw)
282 XConfigureWindow(globalconf.display, client->titlebar.sw->window,
283 CWSibling | CWStackMode, &wc);
284 wc.sibling = client->titlebar.sw->window;
286 XConfigureWindow(globalconf.display, client->win, CWSibling | CWStackMode, &wc);
287 wc.sibling = client->win;
292 /** Manage a new client
293 * \param w The window
294 * \param wa Window attributes
295 * \param screen Screen ID
297 void
298 client_manage(Window w, XWindowAttributes *wa, int screen)
300 Client *c, *t = NULL;
301 Window trans;
302 Bool rettrans, retloadprops;
303 XWindowChanges wc;
304 Tag *tag;
305 Rule *rule;
306 long flags;
308 c = p_new(Client, 1);
310 c->screen = screen_get_bycoord(globalconf.screens_info, screen, wa->x, wa->y);
312 if(globalconf.screens_info->xinerama_is_active)
313 c->phys_screen = DefaultScreen(globalconf.display);
314 else
315 c->phys_screen = c->screen;
317 /* Initial values */
318 c->win = w;
319 c->geometry.x = c->f_geometry.x = c->m_geometry.x = wa->x;
320 c->geometry.y = c->f_geometry.y = c->m_geometry.y = wa->y;
321 c->geometry.width = c->f_geometry.width = c->m_geometry.width = wa->width;
322 c->geometry.height = c->f_geometry.height = c->m_geometry.height = wa->height;
323 c->oldborder = wa->border_width;
324 c->newcomer = True;
326 /* Set windows borders */
327 wc.border_width = c->border = globalconf.screens[screen].borderpx;
328 XConfigureWindow(globalconf.display, w, CWBorderWidth, &wc);
329 XSetWindowBorder(globalconf.display, w, c->border);
330 /* propagates border_width, if size doesn't change */
331 window_configure(c->win, c->geometry, c->border);
333 /* update window title */
334 client_updatetitle(c);
336 /* update hints */
337 flags = client_updatesizehints(c);
338 client_updatewmhints(c);
340 /* Try to load props if any */
341 if(!(retloadprops = client_loadprops(c, screen)))
342 move_client_to_screen(c, screen, True);
344 /* Then check clients hints */
345 ewmh_check_client_hints(c);
347 /* default titlebar position */
348 c->titlebar = globalconf.screens[screen].titlebar_default;
350 /* get the matching rule if any */
351 rule = rule_matching_client(c);
353 /* Then apply rules if no props */
354 if(!retloadprops && rule)
356 if(rule->screen != RULE_NOSCREEN)
357 move_client_to_screen(c, rule->screen, True);
358 tag_client_with_rule(c, rule);
360 switch(rule->isfloating)
362 case Maybe:
363 break;
364 case Yes:
365 client_setfloating(c, True);
366 break;
367 case No:
368 client_setfloating(c, False);
369 break;
372 if(rule->opacity >= 0.0f)
373 window_settrans(c->win, rule->opacity);
376 /* check for transient and set tags like its parent,
377 * XGetTransientForHint returns 1 on success
379 if((rettrans = XGetTransientForHint(globalconf.display, w, &trans))
380 && (t = client_get_bywin(globalconf.clients, trans)))
381 for(tag = globalconf.screens[c->screen].tags; tag; tag = tag->next)
382 if(is_client_tagged(t, tag))
383 tag_client(c, tag);
385 /* should be floating if transsient or fixed */
386 if(rettrans || c->isfixed)
387 client_setfloating(c, True);
389 /* titlebar init */
390 if(rule && rule->titlebar.position != Auto)
391 c->titlebar = rule->titlebar;
393 titlebar_init(c);
395 if(!retloadprops && !(flags & (USPosition | PPosition)))
397 if(c->isfloating && !c->ismax)
398 client_resize(c, globalconf.screens[c->screen].floating_placement(c), False);
399 else
400 c->f_geometry = globalconf.screens[c->screen].floating_placement(c);
403 /* update titlebar with real floating info now */
404 titlebar_update_geometry_floating(c);
406 XSelectInput(globalconf.display, w, StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
408 /* handle xshape */
409 if(globalconf.have_shape)
411 XShapeSelectInput(globalconf.display, w, ShapeNotifyMask);
412 window_setshape(c->win, c->phys_screen);
415 /* attach to the stack */
416 if(rule)
417 switch(rule->ismaster)
419 case Yes:
420 client_list_push(&globalconf.clients, c);
421 break;
422 case No:
423 client_list_append(&globalconf.clients, c);
424 break;
425 case Maybe:
426 rule = NULL;
427 break;
430 if(!rule)
432 if(globalconf.screens[c->screen].new_become_master)
433 client_list_push(&globalconf.clients, c);
434 else
435 client_list_append(&globalconf.clients, c);
438 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
439 ewmh_update_net_client_list(c->phys_screen);
442 static area_t
443 client_geometry_hints(Client *c, area_t geometry)
445 double dx, dy, max, min, ratio;
447 if(c->minay > 0 && c->maxay > 0 && (geometry.height - c->baseh) > 0
448 && (geometry.width - c->basew) > 0)
450 dx = (double) (geometry.width - c->basew);
451 dy = (double) (geometry.height - c->baseh);
452 min = (double) (c->minax) / (double) (c->minay);
453 max = (double) (c->maxax) / (double) (c->maxay);
454 ratio = dx / dy;
455 if(max > 0 && min > 0 && ratio > 0)
457 if(ratio < min)
459 dy = (dx * min + dy) / (min * min + 1);
460 dx = dy * min;
461 geometry.width = (int) dx + c->basew;
462 geometry.height = (int) dy + c->baseh;
464 else if(ratio > max)
466 dy = (dx * min + dy) / (max * max + 1);
467 dx = dy * min;
468 geometry.width = (int) dx + c->basew;
469 geometry.height = (int) dy + c->baseh;
473 if(c->minw && geometry.width < c->minw)
474 geometry.width = c->minw;
475 if(c->minh && geometry.height < c->minh)
476 geometry.height = c->minh;
477 if(c->maxw && geometry.width > c->maxw)
478 geometry.width = c->maxw;
479 if(c->maxh && geometry.height > c->maxh)
480 geometry.height = c->maxh;
481 if(c->incw)
482 geometry.width -= (geometry.width - c->basew) % c->incw;
483 if(c->inch)
484 geometry.height -= (geometry.height - c->baseh) % c->inch;
486 return geometry;
489 /** Resize client window
490 * \param c client to resize
491 * \param geometry new window geometry
492 * \param hints use resize hints
493 * \param return True if resize has been done
495 Bool
496 client_resize(Client *c, area_t geometry, Bool hints)
498 int new_screen;
499 area_t area;
500 XWindowChanges wc;
501 Layout *layout = layout_get_current(c->screen);
502 Bool resized = False;
504 if(!c->ismoving && !c->isfloating && layout->arrange != layout_floating)
506 titlebar_update_geometry(c, geometry);
507 geometry = titlebar_geometry_remove(&c->titlebar, geometry);
510 if(hints)
511 geometry = client_geometry_hints(c, geometry);
513 if(geometry.width <= 0 || geometry.height <= 0)
514 return False;
516 /* offscreen appearance fixes */
517 area = get_display_area(c->phys_screen, NULL,
518 &globalconf.screens[c->screen].padding);
520 if(geometry.x > area.width)
521 geometry.x = area.width - geometry.width - 2 * c->border;
522 if(geometry.y > area.height)
523 geometry.y = area.height - geometry.height - 2 * c->border;
524 if(geometry.x + geometry.width + 2 * c->border < 0)
525 geometry.x = 0;
526 if(geometry.y + geometry.height + 2 * c->border < 0)
527 geometry.y = 0;
529 if(c->geometry.x != geometry.x || c->geometry.y != geometry.y
530 || c->geometry.width != geometry.width || c->geometry.height != geometry.height)
532 new_screen =
533 screen_get_bycoord(globalconf.screens_info, c->screen, geometry.x, geometry.y);
535 c->geometry.x = wc.x = geometry.x;
536 c->geometry.width = wc.width = geometry.width;
537 c->geometry.y = wc.y = geometry.y;
538 c->geometry.height = wc.height = geometry.height;
539 wc.border_width = c->border;
541 /* save the floating geometry if the window is floating but not
542 * maximized */
543 if(c->ismoving || c->isfloating
544 || layout_get_current(new_screen)->arrange == layout_floating)
546 if(!c->ismax)
547 c->f_geometry = geometry;
548 titlebar_update_geometry_floating(c);
551 XConfigureWindow(globalconf.display, c->win,
552 CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
553 window_configure(c->win, geometry, c->border);
555 if(c->screen != new_screen)
556 move_client_to_screen(c, new_screen, False);
558 resized = True;
561 /* call it again like it was floating,
562 * we want it to be sticked to the window */
563 if(!c->ismoving && !c->isfloating && layout->arrange != layout_floating)
564 titlebar_update_geometry_floating(c);
566 return resized;
569 void
570 client_setfloating(Client *c, Bool floating)
572 if(c->isfloating != floating)
574 if((c->isfloating = floating))
576 client_resize(c, c->f_geometry, False);
577 XRaiseWindow(globalconf.display, c->win);
579 else if(c->ismax)
581 c->ismax = False;
582 client_resize(c, c->m_geometry, False);
584 if(client_isvisible(c, c->screen))
585 globalconf.screens[c->screen].need_arrange = True;
586 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
587 client_saveprops(c);
591 /** Save client properties as an X property
592 * \param c client
594 void
595 client_saveprops(Client *c)
597 int i = 0, ntags = 0;
598 char *prop;
599 Tag *tag;
601 for(tag = globalconf.screens[c->screen].tags; tag; tag = tag->next)
602 ntags++;
604 prop = p_new(char, ntags + 2);
606 for(tag = globalconf.screens[c->screen].tags; tag; tag = tag->next, i++)
607 prop[i] = is_client_tagged(c, tag) ? '1' : '0';
609 if(i <= ntags)
610 prop[i] = c->isfloating ? '1' : '0';
612 prop[++i] = '\0';
614 XChangeProperty(globalconf.display, c->win,
615 XInternAtom(globalconf.display, "_AWESOME_PROPERTIES", False),
616 XA_STRING, 8, PropModeReplace, (unsigned char *) prop, i);
618 p_delete(&prop);
621 void
622 client_unban(Client *c)
624 XMapWindow(globalconf.display, c->win);
625 window_setstate(c->win, NormalState);
626 if(c->titlebar.sw && c->titlebar.position != Off)
627 XMapWindow(globalconf.display, c->titlebar.sw->window);
630 void
631 client_unmanage(Client *c)
633 XWindowChanges wc;
634 Tag *tag;
636 wc.border_width = c->oldborder;
638 /* The server grab construct avoids race conditions. */
639 XGrabServer(globalconf.display);
641 XConfigureWindow(globalconf.display, c->win, CWBorderWidth, &wc); /* restore border */
643 /* remove client everywhere */
644 client_list_detach(&globalconf.clients, c);
645 focus_delete_client(c);
646 if(globalconf.scratch.client == c)
647 globalconf.scratch.client = NULL;
648 for(tag = globalconf.screens[c->screen].tags; tag; tag = tag->next)
649 untag_client(c, tag);
651 if(globalconf.focus->client == c)
652 client_focus(NULL, c->screen, True);
654 XUngrabButton(globalconf.display, AnyButton, AnyModifier, c->win);
655 window_setstate(c->win, WithdrawnState);
657 XSync(globalconf.display, False);
658 XUngrabServer(globalconf.display);
660 if(c->titlebar.sw)
661 simplewindow_delete(&c->titlebar.sw);
663 p_delete(&c);
666 void
667 client_updatewmhints(Client *c)
669 XWMHints *wmh;
671 if((wmh = XGetWMHints(globalconf.display, c->win)))
673 if((c->isurgent = ((wmh->flags & XUrgencyHint) && globalconf.focus->client != c)))
675 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
676 titlebar_draw(c);
678 if((wmh->flags & StateHint) && wmh->initial_state == WithdrawnState)
680 c->border = 0;
681 c->skip = True;
683 XFree(wmh);
687 long
688 client_updatesizehints(Client *c)
690 long msize;
691 XSizeHints size;
693 if(!XGetWMNormalHints(globalconf.display, c->win, &size, &msize))
694 return 0L;
696 if(size.flags & PBaseSize)
698 c->basew = size.base_width;
699 c->baseh = size.base_height;
701 else if(size.flags & PMinSize)
703 c->basew = size.min_width;
704 c->baseh = size.min_height;
706 else
707 c->basew = c->baseh = 0;
708 if(size.flags & PResizeInc)
710 c->incw = size.width_inc;
711 c->inch = size.height_inc;
713 else
714 c->incw = c->inch = 0;
716 if(size.flags & PMaxSize)
718 c->maxw = size.max_width;
719 c->maxh = size.max_height;
721 else
722 c->maxw = c->maxh = 0;
724 if(size.flags & PMinSize)
726 c->minw = size.min_width;
727 c->minh = size.min_height;
729 else if(size.flags & PBaseSize)
731 c->minw = size.base_width;
732 c->minh = size.base_height;
734 else
735 c->minw = c->minh = 0;
737 if(size.flags & PAspect)
739 c->minax = size.min_aspect.x;
740 c->maxax = size.max_aspect.x;
741 c->minay = size.min_aspect.y;
742 c->maxay = size.max_aspect.y;
744 else
745 c->minax = c->maxax = c->minay = c->maxay = 0;
747 if(c->maxw && c->minw && c->maxh && c->minh
748 && c->maxw == c->minw && c->maxh == c->minh)
749 c->isfixed = True;
751 return size.flags;
754 /** Returns True if a client is tagged
755 * with one of the tags
756 * \return True or False
758 Bool
759 client_isvisible(Client *c, int screen)
761 Tag *tag;
763 if(!c || c->screen != screen)
764 return False;
766 if(globalconf.scratch.client == c)
767 return globalconf.scratch.isvisible;
769 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
770 if(tag->selected && is_client_tagged(c, tag))
771 return True;
772 return False;
775 /** Set the transparency of the selected client.
776 * Argument should be a floating between 0 and 100, -1 to disable.
777 * \param screen Screen ID
778 * \param arg unused arg
779 * \ingroup ui_callback
781 void
782 uicb_client_settrans(int screen __attribute__ ((unused)), char *arg)
784 double delta = 1.0, current_opacity = 1.0;
785 unsigned char *data;
786 Atom actual;
787 int format;
788 unsigned long n, left;
789 unsigned int current_opacity_raw = 0;
790 int set_prop = 0;
791 Client *sel = globalconf.focus->client;
793 if(!sel)
794 return;
796 XGetWindowProperty(globalconf.display, sel->win,
797 XInternAtom(globalconf.display, "_NET_WM_WINDOW_OPACITY", False),
798 0L, 1L, False, XA_CARDINAL, &actual, &format, &n, &left,
799 (unsigned char **) &data);
800 if(data)
802 memcpy(&current_opacity_raw, data, sizeof(unsigned int));
803 XFree(data);
804 current_opacity = current_opacity_raw / 0xffffffff;
806 else
807 set_prop = 1;
809 delta = compute_new_value_from_arg(arg, current_opacity);
811 if(delta <= 0.0)
812 delta = 0.0;
813 else if(delta > 1.0)
815 delta = 1.0;
816 set_prop = 1;
819 if(delta == 1.0 && !set_prop)
820 window_settrans(sel->win, -1);
821 else
822 window_settrans(sel->win, delta);
825 /** Find a visible client on screen. Return next client or previous client if
826 * reverse is true.
827 * \param sel current selected client
828 * \param reverse return previous instead of next if true
829 * \return next or previous client
831 static Client *
832 client_find_visible(Client *sel, Bool reverse)
834 Client *next;
835 Client *(*client_iter)(Client **, Client *) = client_list_next_cycle;
837 if(!sel) return NULL;
839 if(reverse)
840 client_iter = client_list_prev_cycle;
842 /* look for previous or next starting at sel */
844 for(next = client_iter(&globalconf.clients, sel);
845 next && next != sel && (next->skip || !client_isvisible(next, sel->screen));
846 next = client_iter(&globalconf.clients, next));
848 return next;
851 /** Swap the currently focused client with the previous visible one.
852 * \param screen Screen ID
853 * \param arg nothing
854 * \ingroup ui_callback
856 void
857 uicb_client_swapprev(int screen __attribute__ ((unused)), char *arg __attribute__ ((unused)))
859 Client *prev;
861 if((prev = client_find_visible(globalconf.focus->client, True)))
863 client_list_swap(&globalconf.clients, prev, globalconf.focus->client);
864 globalconf.screens[prev->screen].need_arrange = True;
868 /** Swap the currently focused client with the next visible one.
869 * \param screen Screen ID
870 * \param arg nothing
871 * \ingroup ui_callback
873 void
874 uicb_client_swapnext(int screen __attribute__ ((unused)), char *arg __attribute__ ((unused)))
876 Client *next;
878 if((next = client_find_visible(globalconf.focus->client, False)))
880 client_list_swap(&globalconf.clients, globalconf.focus->client, next);
881 globalconf.screens[next->screen].need_arrange = True;
885 /** Move and resize a client. Argument should be in format "x y w h" with
886 * absolute (1, 20, 300, ...) or relative (+10, -200, ...) values.
887 * \param screen Screen ID
888 * \param arg x y w h
889 * \ingroup ui_callback
891 void
892 uicb_client_moveresize(int screen, char *arg)
894 int ox, oy, ow, oh; /* old geometry */
895 char x[8], y[8], w[8], h[8];
896 int mx, my, dx, dy, nmx, nmy;
897 unsigned int dui;
898 Window dummy;
899 area_t geometry;
900 Client *sel = globalconf.focus->client;
901 Layout *curlay = layout_get_current(screen);
903 if(!sel || sel->isfixed || !arg ||
904 (curlay->arrange != layout_floating && !sel->isfloating))
905 return;
907 if(sscanf(arg, "%s %s %s %s", x, y, w, h) != 4)
908 return;
910 geometry.x = (int) compute_new_value_from_arg(x, sel->geometry.x);
911 geometry.y = (int) compute_new_value_from_arg(y, sel->geometry.y);
912 geometry.width = (int) compute_new_value_from_arg(w, sel->geometry.width);
913 geometry.height = (int) compute_new_value_from_arg(h, sel->geometry.height);
915 ox = sel->geometry.x;
916 oy = sel->geometry.y;
917 ow = sel->geometry.width;
918 oh = sel->geometry.height;
920 Bool xqp = XQueryPointer(globalconf.display,
921 RootWindow(globalconf.display,
922 sel->phys_screen),
923 &dummy, &dummy, &mx, &my, &dx, &dy, &dui);
924 if(globalconf.screens[sel->screen].resize_hints)
925 geometry = client_geometry_hints(sel, geometry);
926 client_resize(sel, geometry, False);
927 if (xqp && ox <= mx && (ox + 2 * sel->border + ow) >= mx &&
928 oy <= my && (oy + 2 * sel->border + oh) >= my)
930 nmx = mx - (ox + sel->border) + sel->geometry.width - ow;
931 nmy = my - (oy + sel->border) + sel->geometry.height - oh;
933 if(nmx < -sel->border) /* can happen on a resize */
934 nmx = -sel->border;
935 if(nmy < -sel->border)
936 nmy = -sel->border;
938 XWarpPointer(globalconf.display,
939 None, sel->win,
940 0, 0, 0, 0, nmx, nmy);
944 /** Kill a client via a WM_DELETE_WINDOW request or XKillClient if not
945 * supported.
946 * \param c the client to kill
948 void
949 client_kill(Client *c)
951 XEvent ev;
953 if(client_isprotodel(globalconf.display, c->win))
955 ev.type = ClientMessage;
956 ev.xclient.window = c->win;
957 ev.xclient.message_type = XInternAtom(globalconf.display, "WM_PROTOCOLS", False);
958 ev.xclient.format = 32;
959 ev.xclient.data.l[0] = XInternAtom(globalconf.display, "WM_DELETE_WINDOW", False);
960 ev.xclient.data.l[1] = CurrentTime;
961 XSendEvent(globalconf.display, c->win, False, NoEventMask, &ev);
963 else
964 XKillClient(globalconf.display, c->win);
967 /** Kill the currently focused client.
968 * \param screen Screen ID
969 * \param arg unused
970 * \ingroup ui_callback
972 void
973 uicb_client_kill(int screen __attribute__ ((unused)), char *arg __attribute__ ((unused)))
975 Client *sel = globalconf.focus->client;
977 if(sel)
978 client_kill(sel);
981 /** Maximize the client to the given geometry.
982 * \param c the client to maximize
983 * \param geometry the geometry to use for maximizing
985 static void
986 client_maximize(Client *c, area_t geometry)
988 if((c->ismax = !c->ismax))
990 /* disable titlebar */
991 c->titlebar.position = Off;
992 c->wasfloating = c->isfloating;
993 c->m_geometry = c->geometry;
994 if(layout_get_current(c->screen)->arrange != layout_floating)
995 client_setfloating(c, True);
996 client_focus(c, c->screen, True);
997 client_resize(c, geometry, False);
999 else if(c->wasfloating)
1001 c->titlebar.position = c->titlebar.dposition;
1002 client_setfloating(c, True);
1003 client_resize(c, c->m_geometry, False);
1005 else if(layout_get_current(c->screen)->arrange == layout_floating)
1007 c->titlebar.position = c->titlebar.dposition;
1008 client_resize(c, c->m_geometry, False);
1010 else
1012 c->titlebar.position = c->titlebar.dposition;
1013 client_setfloating(c, False);
1015 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
1018 /** Toggle maximization state for the focused client.
1019 * \param screen Screen ID
1020 * \param arg Unused
1021 * \ingroup ui_callback
1023 void
1024 uicb_client_togglemax(int screen, char *arg __attribute__ ((unused)))
1026 Client *sel = globalconf.focus->client;
1027 area_t area = screen_get_area(screen,
1028 globalconf.screens[screen].statusbar,
1029 &globalconf.screens[screen].padding);
1031 if(sel)
1033 area.width -= 2 * sel->border;
1034 area.height -= 2 * sel->border;
1035 client_maximize(sel, area);
1039 /** Toggle vertical maximization for the focused client.
1040 * \param screen Screen ID
1041 * \param arg Unused
1042 * \ingroup ui_callback
1044 void
1045 uicb_client_toggleverticalmax(int screen, char *arg __attribute__ ((unused)))
1047 Client *sel = globalconf.focus->client;
1048 area_t area = screen_get_area(screen,
1049 globalconf.screens[screen].statusbar,
1050 &globalconf.screens[screen].padding);
1052 if(sel)
1054 area.x = sel->geometry.x;
1055 area.width = sel->geometry.width;
1056 area.height -= 2 * sel->border;
1057 client_maximize(sel, area);
1062 /** Toggle horizontal maximization for the focused client.
1063 * \param screen Screen ID
1064 * \param arg Unused
1065 * \ingroup ui_callback
1067 void
1068 uicb_client_togglehorizontalmax(int screen, char *arg __attribute__ ((unused)))
1070 Client *sel = globalconf.focus->client;
1071 area_t area = screen_get_area(screen,
1072 globalconf.screens[screen].statusbar,
1073 &globalconf.screens[screen].padding);
1075 if(sel)
1077 area.y = sel->geometry.y;
1078 area.height = sel->geometry.height;
1079 area.width -= 2 * sel->border;
1080 client_maximize(sel, area);
1084 /** Move the client to the master area.
1085 * \param screen Screen ID
1086 * \param arg Unused
1087 * \ingroup ui_callback
1089 void
1090 uicb_client_zoom(int screen, char *arg __attribute__ ((unused)))
1092 Client *c, *sel = globalconf.focus->client;
1094 if(!sel)
1095 return;
1097 for(c = globalconf.clients; !client_isvisible(c, screen); c = c->next);
1098 if(c == sel)
1099 for(sel = sel->next; sel && !client_isvisible(sel, screen); sel = sel->next);
1101 if(sel)
1103 client_list_detach(&globalconf.clients, sel);
1104 client_list_push(&globalconf.clients, sel);
1105 globalconf.screens[screen].need_arrange = True;
1109 /** Give focus to the next visible client in the stack.
1110 * \param screen Screen ID
1111 * \param arg Unused
1112 * \ingroup ui_callback
1114 void
1115 uicb_client_focusnext(int screen, char *arg __attribute__ ((unused)))
1117 Client *next;
1119 if((next = client_find_visible(globalconf.focus->client, False)))
1120 client_focus(next, screen, True);
1123 /** Give focus to the previous visible client in the stack.
1124 * \param screen Screen ID
1125 * \param arg Unused
1126 * \ingroup ui_callback
1128 void
1129 uicb_client_focusprev(int screen, char *arg __attribute__ ((unused)))
1131 Client *prev;
1133 if((prev = client_find_visible(globalconf.focus->client, True)))
1134 client_focus(prev, screen, True);
1137 /** Toggle the floating state of the focused client.
1138 * \param screen Screen ID
1139 * \param arg unused
1140 * \ingroup ui_callback
1142 void
1143 uicb_client_togglefloating(int screen __attribute__ ((unused)), char *arg __attribute__ ((unused)))
1145 if(globalconf.focus->client)
1146 client_setfloating(globalconf.focus->client, !globalconf.focus->client->isfloating);
1149 /** Toggle the scratch client attribute on the focused client.
1150 * \param screen screen number
1151 * \param arg unused argument
1152 * \ingroup ui_callback
1154 void
1155 uicb_client_setscratch(int screen, char *arg __attribute__ ((unused)))
1157 if(!globalconf.focus->client)
1158 return;
1160 if(globalconf.scratch.client == globalconf.focus->client)
1161 globalconf.scratch.client = NULL;
1162 else
1163 globalconf.scratch.client = globalconf.focus->client;
1165 widget_invalidate_cache(screen, WIDGET_CACHE_CLIENTS | WIDGET_CACHE_TAGS);
1166 globalconf.screens[screen].need_arrange = True;
1169 /** Toggle the scratch client's visibility.
1170 * \param screen screen number
1171 * \param arg unused argument
1172 * \ingroup ui_callback
1174 void
1175 uicb_client_togglescratch(int screen, char *arg __attribute__ ((unused)))
1177 if(globalconf.scratch.client)
1179 globalconf.scratch.isvisible = !globalconf.scratch.isvisible;
1180 if(globalconf.scratch.isvisible)
1181 client_focus(globalconf.scratch.client, screen, True);
1182 globalconf.screens[globalconf.scratch.client->screen].need_arrange = True;
1183 widget_invalidate_cache(globalconf.scratch.client->screen, WIDGET_CACHE_CLIENTS);
1187 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80