[progressbar] check_settings should be static
[awesome.git] / client.c
blob827a682db124a8c9703bf2aa0251c8881e9f7160
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_update(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 focus_add_client(NULL);
158 XSetWindowBorder(globalconf.display, c->win,
159 globalconf.screens[c->screen].styles.normal.border.pixel);
160 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
161 titlebar_update(c);
164 /** Ban client and unmap it
165 * \param c the client
167 void
168 client_ban(Client *c)
170 if(globalconf.focus->client == c)
171 client_unfocus(c);
172 XUnmapWindow(globalconf.display, c->win);
173 window_setstate(c->win, IconicState);
174 if(c->titlebar.position && c->titlebar.sw)
175 XUnmapWindow(globalconf.display, c->titlebar.sw->window);
178 /** Give focus to client, or to first client if c is NULL
179 * \param c client
180 * \param screen Screen ID
181 * \param raise raise window if true
182 * \return true if a window (even root) has received focus, false otherwise
184 Bool
185 client_focus(Client *c, int screen, Bool raise)
187 int phys_screen;
189 /* if c is NULL or invisible, take next client in the focus history */
190 if((!c || (c && (!client_isvisible(c, screen))))
191 && !(c = focus_get_current_client(screen)))
192 /* if c is still NULL take next client in the stack */
193 for(c = globalconf.clients; c && (c->skip || !client_isvisible(c, screen)); c = c->next);
195 /* if c is already the focused window, then stop */
196 if(c == globalconf.focus->client)
197 return False;
199 /* unfocus current selected client */
200 if(globalconf.focus->client)
201 client_unfocus(globalconf.focus->client);
203 if(c)
205 /* unban the client before focusing or it will fail */
206 client_unban(c);
207 /* save sel in focus history */
208 focus_add_client(c);
209 if(globalconf.screens[c->screen].opacity_unfocused != -1)
210 window_settrans(c->win, globalconf.screens[c->screen].opacity_focused);
211 XSetWindowBorder(globalconf.display, c->win,
212 globalconf.screens[screen].styles.focus.border.pixel);
213 titlebar_update(c);
214 XSetInputFocus(globalconf.display, c->win, RevertToPointerRoot, CurrentTime);
215 if(raise)
216 client_stack(c);
217 /* since we're dropping EnterWindow events and sometimes the window
218 * will appear under the mouse, grabbuttons */
219 window_grabbuttons(c->win, c->phys_screen);
220 phys_screen = c->phys_screen;
222 else
224 phys_screen = screen_virttophys(screen);
225 XSetInputFocus(globalconf.display,
226 RootWindow(globalconf.display, phys_screen),
227 RevertToPointerRoot, CurrentTime);
230 ewmh_update_net_active_window(phys_screen);
231 widget_invalidate_cache(screen, WIDGET_CACHE_CLIENTS);
233 return True;
236 void
237 client_stack(Client *c)
239 XWindowChanges wc;
240 Layout *curlay = layout_get_current(c->screen);
242 if(c->isfloating || curlay->arrange == layout_floating)
244 XRaiseWindow(globalconf.display, c->win);
245 if(c->titlebar.position && c->titlebar.sw)
246 XRaiseWindow(globalconf.display, c->titlebar.sw->window);
248 else
250 Client *client;
251 wc.stack_mode = Below;
252 wc.sibling = None;
253 for(client = globalconf.clients; client; client = client->next)
254 if(client != c && client_isvisible(client, c->screen) && client->isfloating)
256 if(client->titlebar.position && client->titlebar.sw)
258 XConfigureWindow(globalconf.display, client->titlebar.sw->window,
259 CWSibling | CWStackMode, &wc);
260 wc.sibling = client->titlebar.sw->window;
262 XConfigureWindow(globalconf.display, client->win, CWSibling | CWStackMode, &wc);
263 wc.sibling = client->win;
265 if(c->titlebar.position && c->titlebar.sw)
267 XConfigureWindow(globalconf.display, c->titlebar.sw->window,
268 CWSibling | CWStackMode, &wc);
269 wc.sibling = c->titlebar.sw->window;
271 XConfigureWindow(globalconf.display, c->win, CWSibling | CWStackMode, &wc);
272 wc.sibling = c->win;
273 for(client = globalconf.clients; client; client = client->next)
274 if(client != c && IS_TILED(client, c->screen))
276 if(client->titlebar.position && client->titlebar.sw)
278 XConfigureWindow(globalconf.display, client->titlebar.sw->window,
279 CWSibling | CWStackMode, &wc);
280 wc.sibling = client->titlebar.sw->window;
282 XConfigureWindow(globalconf.display, client->win, CWSibling | CWStackMode, &wc);
283 wc.sibling = client->win;
288 /** Manage a new client
289 * \param w The window
290 * \param wa Window attributes
291 * \param screen Screen ID
293 void
294 client_manage(Window w, XWindowAttributes *wa, int screen)
296 Client *c, *t = NULL;
297 Window trans;
298 Bool rettrans, retloadprops;
299 XWindowChanges wc;
300 Tag *tag;
301 Rule *rule;
302 area_t screen_geom;
303 long flags;
305 c = p_new(Client, 1);
307 c->screen = screen_get_bycoord(globalconf.screens_info, screen, wa->x, wa->y);
309 if(globalconf.screens_info->xinerama_is_active)
310 c->phys_screen = DefaultScreen(globalconf.display);
311 else
312 c->phys_screen = c->screen;
314 screen_geom = screen_get_area(c->screen,
315 globalconf.screens[screen].statusbar,
316 &globalconf.screens[screen].padding);
317 /* Initial values */
318 c->win = w;
319 c->geometry.x = c->f_geometry.x = c->m_geometry.x = MAX(wa->x, screen_geom.x);
320 c->geometry.y = c->f_geometry.y = c->m_geometry.y = MAX(wa->y, screen_geom.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)))
396 c->f_geometry = globalconf.screens[c->screen].floating_placement(c);
398 /* update titlebar with real floating info now */
399 if(c->isfloating)
400 titlebar_update_geometry_floating(c);
402 XSelectInput(globalconf.display, w, StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
404 /* handle xshape */
405 if(globalconf.have_shape)
407 XShapeSelectInput(globalconf.display, w, ShapeNotifyMask);
408 window_setshape(c->win, c->phys_screen);
411 /* attach to the stack */
412 if(rule)
413 switch(rule->ismaster)
415 case Yes:
416 client_list_push(&globalconf.clients, c);
417 break;
418 case No:
419 client_list_append(&globalconf.clients, c);
420 break;
421 case Maybe:
422 rule = NULL;
423 break;
426 if(!rule)
428 if(globalconf.screens[c->screen].new_become_master)
429 client_list_push(&globalconf.clients, c);
430 else
431 client_list_append(&globalconf.clients, c);
434 /* some windows require this */
435 XMoveResizeWindow(globalconf.display, c->win, c->geometry.x, c->geometry.y,
436 c->geometry.width, c->geometry.height);
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
581 XLowerWindow(globalconf.display, c->win);
582 if(c->ismax)
584 c->ismax = False;
585 client_resize(c, c->m_geometry, False);
588 if(client_isvisible(c, c->screen))
589 globalconf.screens[c->screen].need_arrange = True;
590 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
591 client_saveprops(c);
595 /** Save client properties as an X property
596 * \param c client
598 void
599 client_saveprops(Client *c)
601 int i = 0, ntags = 0;
602 char *prop;
603 Tag *tag;
605 for(tag = globalconf.screens[c->screen].tags; tag; tag = tag->next)
606 ntags++;
608 prop = p_new(char, ntags + 2);
610 for(tag = globalconf.screens[c->screen].tags; tag; tag = tag->next, i++)
611 prop[i] = is_client_tagged(c, tag) ? '1' : '0';
613 if(i <= ntags)
614 prop[i] = c->isfloating ? '1' : '0';
616 prop[++i] = '\0';
618 XChangeProperty(globalconf.display, c->win,
619 XInternAtom(globalconf.display, "_AWESOME_PROPERTIES", False),
620 XA_STRING, 8, PropModeReplace, (unsigned char *) prop, i);
622 p_delete(&prop);
625 void
626 client_unban(Client *c)
628 XMapWindow(globalconf.display, c->win);
629 window_setstate(c->win, NormalState);
630 if(c->titlebar.sw && c->titlebar.position != Off)
631 XMapWindow(globalconf.display, c->titlebar.sw->window);
634 void
635 client_unmanage(Client *c)
637 XWindowChanges wc;
638 Tag *tag;
640 wc.border_width = c->oldborder;
642 /* The server grab construct avoids race conditions. */
643 XGrabServer(globalconf.display);
645 XConfigureWindow(globalconf.display, c->win, CWBorderWidth, &wc); /* restore border */
647 /* remove client everywhere */
648 client_list_detach(&globalconf.clients, c);
649 focus_delete_client(c);
650 if(globalconf.scratch.client == c)
651 globalconf.scratch.client = NULL;
652 for(tag = globalconf.screens[c->screen].tags; tag; tag = tag->next)
653 untag_client(c, tag);
655 if(globalconf.focus->client == c)
656 client_focus(NULL, c->screen, True);
658 XUngrabButton(globalconf.display, AnyButton, AnyModifier, c->win);
659 window_setstate(c->win, WithdrawnState);
661 XSync(globalconf.display, False);
662 XUngrabServer(globalconf.display);
664 if(c->titlebar.sw)
665 simplewindow_delete(&c->titlebar.sw);
667 p_delete(&c);
670 void
671 client_updatewmhints(Client *c)
673 XWMHints *wmh;
675 if((wmh = XGetWMHints(globalconf.display, c->win)))
677 if((c->isurgent = ((wmh->flags & XUrgencyHint) && globalconf.focus->client != c)))
678 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
679 if((wmh->flags & StateHint) && wmh->initial_state == WithdrawnState)
681 c->border = 0;
682 c->skip = True;
684 XFree(wmh);
688 long
689 client_updatesizehints(Client *c)
691 long msize;
692 XSizeHints size;
694 if(!XGetWMNormalHints(globalconf.display, c->win, &size, &msize))
695 return 0L;
697 if(size.flags & PBaseSize)
699 c->basew = size.base_width;
700 c->baseh = size.base_height;
702 else if(size.flags & PMinSize)
704 c->basew = size.min_width;
705 c->baseh = size.min_height;
707 else
708 c->basew = c->baseh = 0;
709 if(size.flags & PResizeInc)
711 c->incw = size.width_inc;
712 c->inch = size.height_inc;
714 else
715 c->incw = c->inch = 0;
717 if(size.flags & PMaxSize)
719 c->maxw = size.max_width;
720 c->maxh = size.max_height;
722 else
723 c->maxw = c->maxh = 0;
725 if(size.flags & PMinSize)
727 c->minw = size.min_width;
728 c->minh = size.min_height;
730 else if(size.flags & PBaseSize)
732 c->minw = size.base_width;
733 c->minh = size.base_height;
735 else
736 c->minw = c->minh = 0;
738 if(size.flags & PAspect)
740 c->minax = size.min_aspect.x;
741 c->maxax = size.max_aspect.x;
742 c->minay = size.min_aspect.y;
743 c->maxay = size.max_aspect.y;
745 else
746 c->minax = c->maxax = c->minay = c->maxay = 0;
748 if(c->maxw && c->minw && c->maxh && c->minh
749 && c->maxw == c->minw && c->maxh == c->minh)
750 c->isfixed = True;
752 return size.flags;
755 /** Returns True if a client is tagged
756 * with one of the tags
757 * \return True or False
759 Bool
760 client_isvisible(Client *c, int screen)
762 Tag *tag;
764 if(!c || c->screen != screen)
765 return False;
767 if(globalconf.scratch.client == c)
768 return globalconf.scratch.isvisible;
770 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
771 if(tag->selected && is_client_tagged(c, tag))
772 return True;
773 return False;
776 /** Set selected client transparency.
777 * Argument should be a floating between 0 and 100, -1 to disable.
778 * \param screen Screen ID
779 * \param arg unused arg
780 * \ingroup ui_callback
782 void
783 uicb_client_settrans(int screen __attribute__ ((unused)), char *arg)
785 double delta = 1.0, current_opacity = 1.0;
786 unsigned char *data;
787 Atom actual;
788 int format;
789 unsigned long n, left;
790 unsigned int current_opacity_raw = 0;
791 int set_prop = 0;
792 Client *sel = globalconf.focus->client;
794 if(!sel)
795 return;
797 XGetWindowProperty(globalconf.display, sel->win,
798 XInternAtom(globalconf.display, "_NET_WM_WINDOW_OPACITY", False),
799 0L, 1L, False, XA_CARDINAL, &actual, &format, &n, &left,
800 (unsigned char **) &data);
801 if(data)
803 memcpy(&current_opacity_raw, data, sizeof(unsigned int));
804 XFree(data);
805 current_opacity = current_opacity_raw / 0xffffffff;
807 else
808 set_prop = 1;
810 delta = compute_new_value_from_arg(arg, current_opacity);
812 if(delta <= 0.0)
813 delta = 0.0;
814 else if(delta > 1.0)
816 delta = 1.0;
817 set_prop = 1;
820 if(delta == 1.0 && !set_prop)
821 window_settrans(sel->win, -1);
822 else
823 window_settrans(sel->win, delta);
826 /** Find a visible client on screen. Return next client or previous client if
827 * reverse is true.
828 * \param sel current selected client
829 * \param reverse return previous instead of next if true
830 * \return next or previous client
832 static Client *
833 client_find_visible(Client *sel, Bool reverse)
835 Client *next;
836 Client *(*client_iter)(Client **, Client *) = client_list_next_cycle;
838 if(!sel) return NULL;
840 if(reverse)
841 client_iter = client_list_prev_cycle;
843 /* look for previous or next starting at sel */
845 for(next = client_iter(&globalconf.clients, sel);
846 next && next != sel && (next->skip || !client_isvisible(next, sel->screen));
847 next = client_iter(&globalconf.clients, next));
849 return next;
852 /** Swap currently focused client with previous visible.
853 * \param screen Screen ID
854 * \param arg nothing
855 * \ingroup ui_callback
857 void
858 uicb_client_swapprev(int screen __attribute__ ((unused)), char *arg __attribute__ ((unused)))
860 Client *prev;
862 if((prev = client_find_visible(globalconf.focus->client, True)))
864 client_list_swap(&globalconf.clients, prev, globalconf.focus->client);
865 globalconf.screens[prev->screen].need_arrange = True;
869 /** Swap currently focused client with next visible.
870 * \param screen Screen ID
871 * \param arg nothing
872 * \ingroup ui_callback
874 void
875 uicb_client_swapnext(int screen __attribute__ ((unused)), char *arg __attribute__ ((unused)))
877 Client *next;
879 if((next = client_find_visible(globalconf.focus->client, False)))
881 client_list_swap(&globalconf.clients, globalconf.focus->client, next);
882 globalconf.screens[next->screen].need_arrange = True;
886 /** Move and resize a client. Argument should be in format "x y w h" with
887 * absolute (1, 20, 300, ...) or relatives (+10, -200, ...) values.
888 * \param screen Screen ID
889 * \param arg x y w h
890 * \ingroup ui_callback
892 void
893 uicb_client_moveresize(int screen, char *arg)
895 int ox, oy, ow, oh; /* old geometry */
896 char x[8], y[8], w[8], h[8];
897 int mx, my, dx, dy, nmx, nmy;
898 unsigned int dui;
899 Window dummy;
900 area_t geometry;
901 Client *sel = globalconf.focus->client;
902 Layout *curlay = layout_get_current(screen);
904 if(!sel || sel->isfixed || !arg ||
905 (curlay->arrange != layout_floating && !sel->isfloating))
906 return;
908 if(sscanf(arg, "%s %s %s %s", x, y, w, h) != 4)
909 return;
911 geometry.x = (int) compute_new_value_from_arg(x, sel->geometry.x);
912 geometry.y = (int) compute_new_value_from_arg(y, sel->geometry.y);
913 geometry.width = (int) compute_new_value_from_arg(w, sel->geometry.width);
914 geometry.height = (int) compute_new_value_from_arg(h, sel->geometry.height);
916 ox = sel->geometry.x;
917 oy = sel->geometry.y;
918 ow = sel->geometry.width;
919 oh = sel->geometry.height;
921 Bool xqp = XQueryPointer(globalconf.display,
922 RootWindow(globalconf.display,
923 sel->phys_screen),
924 &dummy, &dummy, &mx, &my, &dx, &dy, &dui);
925 if(globalconf.screens[sel->screen].resize_hints)
926 geometry = client_geometry_hints(sel, geometry);
927 client_resize(sel, geometry, False);
928 if (xqp && ox <= mx && (ox + 2 * sel->border + ow) >= mx &&
929 oy <= my && (oy + 2 * sel->border + oh) >= my)
931 nmx = mx - (ox + sel->border) + sel->geometry.width - ow;
932 nmy = my - (oy + sel->border) + sel->geometry.height - oh;
934 if(nmx < -sel->border) /* can happen on a resize */
935 nmx = -sel->border;
936 if(nmy < -sel->border)
937 nmy = -sel->border;
939 XWarpPointer(globalconf.display,
940 None, sel->win,
941 0, 0, 0, 0, nmx, nmy);
945 /** Kill a client via a WM_DELETE_WINDOW request or XKillClient if not
946 * supported.
947 * \param c the client to kill
949 void
950 client_kill(Client *c)
952 XEvent ev;
954 if(client_isprotodel(globalconf.display, c->win))
956 ev.type = ClientMessage;
957 ev.xclient.window = c->win;
958 ev.xclient.message_type = XInternAtom(globalconf.display, "WM_PROTOCOLS", False);
959 ev.xclient.format = 32;
960 ev.xclient.data.l[0] = XInternAtom(globalconf.display, "WM_DELETE_WINDOW", False);
961 ev.xclient.data.l[1] = CurrentTime;
962 XSendEvent(globalconf.display, c->win, False, NoEventMask, &ev);
964 else
965 XKillClient(globalconf.display, c->win);
968 /** Kill the currently focused client.
969 * \param screen Screen ID
970 * \param arg unused
971 * \ingroup ui_callback
973 void
974 uicb_client_kill(int screen __attribute__ ((unused)), char *arg __attribute__ ((unused)))
976 Client *sel = globalconf.focus->client;
978 if(sel)
979 client_kill(sel);
982 /** Maximize the client to the given geometry.
983 * \param c the client to maximize
984 * \param geometry the geometry to use for maximizing
986 static void
987 client_maximize(Client *c, area_t geometry)
989 if((c->ismax = !c->ismax))
991 /* disable titlebar */
992 c->titlebar.position = Off;
993 c->wasfloating = c->isfloating;
994 c->m_geometry = c->geometry;
995 if(layout_get_current(c->screen)->arrange != layout_floating)
996 client_setfloating(c, True);
997 client_focus(c, c->screen, True);
998 client_resize(c, geometry, False);
1000 else if(c->wasfloating)
1002 c->titlebar.position = c->titlebar.dposition;
1003 client_setfloating(c, True);
1004 client_resize(c, c->m_geometry, False);
1006 else if(layout_get_current(c->screen)->arrange == layout_floating)
1008 c->titlebar.position = c->titlebar.dposition;
1009 client_resize(c, c->m_geometry, False);
1011 else
1013 c->titlebar.position = c->titlebar.dposition;
1014 client_setfloating(c, False);
1016 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
1019 /** Toggle maximization state for the focused client.
1020 * \param screen Screen ID
1021 * \param arg Unused
1022 * \ingroup ui_callback
1024 void
1025 uicb_client_togglemax(int screen, char *arg __attribute__ ((unused)))
1027 Client *sel = globalconf.focus->client;
1028 area_t area = screen_get_area(screen,
1029 globalconf.screens[screen].statusbar,
1030 &globalconf.screens[screen].padding);
1032 if(sel)
1034 area.width -= 2 * sel->border;
1035 area.height -= 2 * sel->border;
1036 client_maximize(sel, area);
1040 /** Toggle vertical maximization for the focused client.
1041 * \param screen Screen ID
1042 * \param arg Unused
1043 * \ingroup ui_callback
1045 void
1046 uicb_client_toggleverticalmax(int screen, char *arg __attribute__ ((unused)))
1048 Client *sel = globalconf.focus->client;
1049 area_t area = screen_get_area(screen,
1050 globalconf.screens[screen].statusbar,
1051 &globalconf.screens[screen].padding);
1053 if(sel)
1055 area.x = sel->geometry.x;
1056 area.width = sel->geometry.width;
1057 area.height -= 2 * sel->border;
1058 client_maximize(sel, area);
1063 /** Toggle horizontal maximization for the focused client.
1064 * \param screen Screen ID
1065 * \param arg Unused
1066 * \ingroup ui_callback
1068 void
1069 uicb_client_togglehorizontalmax(int screen, char *arg __attribute__ ((unused)))
1071 Client *sel = globalconf.focus->client;
1072 area_t area = screen_get_area(screen,
1073 globalconf.screens[screen].statusbar,
1074 &globalconf.screens[screen].padding);
1076 if(sel)
1078 area.y = sel->geometry.y;
1079 area.height = sel->geometry.height;
1080 area.width -= 2 * sel->border;
1081 client_maximize(sel, area);
1085 /** Set the client the master window.
1086 * \param screen Screen ID
1087 * \param arg Unused
1088 * \ingroup ui_callback
1090 void
1091 uicb_client_zoom(int screen, char *arg __attribute__ ((unused)))
1093 Client *c, *sel = globalconf.focus->client;
1095 if(!sel)
1096 return;
1098 for(c = globalconf.clients; !client_isvisible(c, screen); c = c->next);
1099 if(c == sel)
1100 for(sel = sel->next; sel && !client_isvisible(sel, screen); sel = sel->next);
1102 if(sel)
1104 client_list_detach(&globalconf.clients, sel);
1105 client_list_push(&globalconf.clients, sel);
1106 globalconf.screens[screen].need_arrange = True;
1110 /** Setfocus to the next visible client in the stack.
1111 * \param screen Screen ID
1112 * \param arg Unused
1113 * \ingroup ui_callback
1115 void
1116 uicb_client_focusnext(int screen, char *arg __attribute__ ((unused)))
1118 Client *next;
1120 if((next = client_find_visible(globalconf.focus->client, False)))
1121 client_focus(next, screen, True);
1124 /** Setfocus to the previous visible client in the stack.
1125 * \param screen Screen ID
1126 * \param arg Unused
1127 * \ingroup ui_callback
1129 void
1130 uicb_client_focusprev(int screen, char *arg __attribute__ ((unused)))
1132 Client *prev;
1134 if((prev = client_find_visible(globalconf.focus->client, True)))
1135 client_focus(prev, screen, True);
1138 /** Toggle the floating state of the focused client.
1139 * \param screen Screen ID
1140 * \param arg unused
1141 * \ingroup ui_callback
1143 void
1144 uicb_client_togglefloating(int screen __attribute__ ((unused)), char *arg __attribute__ ((unused)))
1146 if(globalconf.focus->client)
1147 client_setfloating(globalconf.focus->client, !globalconf.focus->client->isfloating);
1150 /** Toggle the scratch client attribute on the focused client.
1151 * \param screen screen number
1152 * \param arg unused argument
1153 * \ingroup ui_callback
1155 void
1156 uicb_client_setscratch(int screen, char *arg __attribute__ ((unused)))
1158 if(!globalconf.focus->client)
1159 return;
1161 if(globalconf.scratch.client == globalconf.focus->client)
1162 globalconf.scratch.client = NULL;
1163 else
1164 globalconf.scratch.client = globalconf.focus->client;
1166 widget_invalidate_cache(screen, WIDGET_CACHE_CLIENTS | WIDGET_CACHE_TAGS);
1167 globalconf.screens[screen].need_arrange = True;
1170 /** Toggle scratch client visibility.
1171 * \param screen screen number
1172 * \param arg unused argument
1173 * \ingroup ui_callback
1175 void
1176 uicb_client_togglescratch(int screen, char *arg __attribute__ ((unused)))
1178 if(globalconf.scratch.client)
1180 globalconf.scratch.isvisible = !globalconf.scratch.isvisible;
1181 if(globalconf.scratch.isvisible)
1182 client_focus(globalconf.scratch.client, screen, True);
1183 globalconf.screens[globalconf.scratch.client->screen].need_arrange = True;
1184 widget_invalidate_cache(globalconf.scratch.client->screen, WIDGET_CACHE_CLIENTS);
1188 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80