[docs] rcskeleton improving and completing
[awesome.git] / client.c
blob768e8c1ee68984d7eefdb2c6d70d8005f6cb3c57
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 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_draw(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_focused != -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_draw(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 long flags;
304 c = p_new(Client, 1);
306 c->screen = screen_get_bycoord(globalconf.screens_info, screen, wa->x, wa->y);
308 if(globalconf.screens_info->xinerama_is_active)
309 c->phys_screen = DefaultScreen(globalconf.display);
310 else
311 c->phys_screen = c->screen;
313 /* Initial values */
314 c->win = w;
315 c->geometry.x = c->f_geometry.x = c->m_geometry.x = wa->x;
316 c->geometry.y = c->f_geometry.y = c->m_geometry.y = wa->y;
317 c->geometry.width = c->f_geometry.width = c->m_geometry.width = wa->width;
318 c->geometry.height = c->f_geometry.height = c->m_geometry.height = wa->height;
319 c->oldborder = wa->border_width;
320 c->newcomer = True;
322 /* Set windows borders */
323 wc.border_width = c->border = globalconf.screens[screen].borderpx;
324 XConfigureWindow(globalconf.display, w, CWBorderWidth, &wc);
325 XSetWindowBorder(globalconf.display, w, c->border);
326 /* propagates border_width, if size doesn't change */
327 window_configure(c->win, c->geometry, c->border);
329 /* update window title */
330 client_updatetitle(c);
332 /* update hints */
333 flags = client_updatesizehints(c);
334 client_updatewmhints(c);
336 /* Try to load props if any */
337 if(!(retloadprops = client_loadprops(c, screen)))
338 move_client_to_screen(c, screen, True);
340 /* Then check clients hints */
341 ewmh_check_client_hints(c);
343 /* default titlebar position */
344 c->titlebar = globalconf.screens[screen].titlebar_default;
346 /* get the matching rule if any */
347 rule = rule_matching_client(c);
349 /* Then apply rules if no props */
350 if(!retloadprops && rule)
352 if(rule->screen != RULE_NOSCREEN)
353 move_client_to_screen(c, rule->screen, True);
354 tag_client_with_rule(c, rule);
356 switch(rule->isfloating)
358 case Maybe:
359 break;
360 case Yes:
361 client_setfloating(c, True);
362 break;
363 case No:
364 client_setfloating(c, False);
365 break;
368 if(rule->opacity >= 0.0f)
369 window_settrans(c->win, rule->opacity);
372 /* check for transient and set tags like its parent,
373 * XGetTransientForHint returns 1 on success
375 if((rettrans = XGetTransientForHint(globalconf.display, w, &trans))
376 && (t = client_get_bywin(globalconf.clients, trans)))
377 for(tag = globalconf.screens[c->screen].tags; tag; tag = tag->next)
378 if(is_client_tagged(t, tag))
379 tag_client(c, tag);
381 /* should be floating if transsient or fixed */
382 if(rettrans || c->isfixed)
383 client_setfloating(c, True);
385 /* titlebar init */
386 if(rule && rule->titlebar.position != Auto)
387 c->titlebar = rule->titlebar;
389 titlebar_init(c);
391 if(!retloadprops && !(flags & (USPosition | PPosition)))
393 if(c->isfloating && !c->ismax)
394 client_resize(c, globalconf.screens[c->screen].floating_placement(c), False);
395 else
396 c->f_geometry = globalconf.screens[c->screen].floating_placement(c);
399 /* update titlebar with real floating info now */
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 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
435 ewmh_update_net_client_list(c->phys_screen);
438 static area_t
439 client_geometry_hints(Client *c, area_t geometry)
441 double dx, dy, max, min, ratio;
443 if(c->minay > 0 && c->maxay > 0 && (geometry.height - c->baseh) > 0
444 && (geometry.width - c->basew) > 0)
446 dx = (double) (geometry.width - c->basew);
447 dy = (double) (geometry.height - c->baseh);
448 min = (double) (c->minax) / (double) (c->minay);
449 max = (double) (c->maxax) / (double) (c->maxay);
450 ratio = dx / dy;
451 if(max > 0 && min > 0 && ratio > 0)
453 if(ratio < min)
455 dy = (dx * min + dy) / (min * min + 1);
456 dx = dy * min;
457 geometry.width = (int) dx + c->basew;
458 geometry.height = (int) dy + c->baseh;
460 else if(ratio > max)
462 dy = (dx * min + dy) / (max * max + 1);
463 dx = dy * min;
464 geometry.width = (int) dx + c->basew;
465 geometry.height = (int) dy + c->baseh;
469 if(c->minw && geometry.width < c->minw)
470 geometry.width = c->minw;
471 if(c->minh && geometry.height < c->minh)
472 geometry.height = c->minh;
473 if(c->maxw && geometry.width > c->maxw)
474 geometry.width = c->maxw;
475 if(c->maxh && geometry.height > c->maxh)
476 geometry.height = c->maxh;
477 if(c->incw)
478 geometry.width -= (geometry.width - c->basew) % c->incw;
479 if(c->inch)
480 geometry.height -= (geometry.height - c->baseh) % c->inch;
482 return geometry;
485 /** Resize client window
486 * \param c client to resize
487 * \param geometry new window geometry
488 * \param hints use resize hints
489 * \param return True if resize has been done
491 Bool
492 client_resize(Client *c, area_t geometry, Bool hints)
494 int new_screen;
495 area_t area;
496 XWindowChanges wc;
497 Layout *layout = layout_get_current(c->screen);
498 Bool resized = False;
500 if(!c->ismoving && !c->isfloating && layout->arrange != layout_floating)
502 titlebar_update_geometry(c, geometry);
503 geometry = titlebar_geometry_remove(&c->titlebar, geometry);
506 if(hints)
507 geometry = client_geometry_hints(c, geometry);
509 if(geometry.width <= 0 || geometry.height <= 0)
510 return False;
512 /* offscreen appearance fixes */
513 area = get_display_area(c->phys_screen, NULL,
514 &globalconf.screens[c->screen].padding);
516 if(geometry.x > area.width)
517 geometry.x = area.width - geometry.width - 2 * c->border;
518 if(geometry.y > area.height)
519 geometry.y = area.height - geometry.height - 2 * c->border;
520 if(geometry.x + geometry.width + 2 * c->border < 0)
521 geometry.x = 0;
522 if(geometry.y + geometry.height + 2 * c->border < 0)
523 geometry.y = 0;
525 if(c->geometry.x != geometry.x || c->geometry.y != geometry.y
526 || c->geometry.width != geometry.width || c->geometry.height != geometry.height)
528 new_screen =
529 screen_get_bycoord(globalconf.screens_info, c->screen, geometry.x, geometry.y);
531 c->geometry.x = wc.x = geometry.x;
532 c->geometry.width = wc.width = geometry.width;
533 c->geometry.y = wc.y = geometry.y;
534 c->geometry.height = wc.height = geometry.height;
535 wc.border_width = c->border;
537 /* save the floating geometry if the window is floating but not
538 * maximized */
539 if(c->ismoving || c->isfloating
540 || layout_get_current(new_screen)->arrange == layout_floating)
542 if(!c->ismax)
543 c->f_geometry = geometry;
544 titlebar_update_geometry_floating(c);
547 XConfigureWindow(globalconf.display, c->win,
548 CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
549 window_configure(c->win, geometry, c->border);
551 if(c->screen != new_screen)
552 move_client_to_screen(c, new_screen, False);
554 resized = True;
557 /* call it again like it was floating,
558 * we want it to be sticked to the window */
559 if(!c->ismoving && !c->isfloating && layout->arrange != layout_floating)
560 titlebar_update_geometry_floating(c);
562 return resized;
565 void
566 client_setfloating(Client *c, Bool floating)
568 if(c->isfloating != floating)
570 if((c->isfloating = floating))
572 client_resize(c, c->f_geometry, False);
573 XRaiseWindow(globalconf.display, c->win);
575 else if(c->ismax)
577 c->ismax = False;
578 client_resize(c, c->m_geometry, False);
580 if(client_isvisible(c, c->screen))
581 globalconf.screens[c->screen].need_arrange = True;
582 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
583 client_saveprops(c);
587 /** Save client properties as an X property
588 * \param c client
590 void
591 client_saveprops(Client *c)
593 int i = 0, ntags = 0;
594 char *prop;
595 Tag *tag;
597 for(tag = globalconf.screens[c->screen].tags; tag; tag = tag->next)
598 ntags++;
600 prop = p_new(char, ntags + 2);
602 for(tag = globalconf.screens[c->screen].tags; tag; tag = tag->next, i++)
603 prop[i] = is_client_tagged(c, tag) ? '1' : '0';
605 if(i <= ntags)
606 prop[i] = c->isfloating ? '1' : '0';
608 prop[++i] = '\0';
610 XChangeProperty(globalconf.display, c->win,
611 XInternAtom(globalconf.display, "_AWESOME_PROPERTIES", False),
612 XA_STRING, 8, PropModeReplace, (unsigned char *) prop, i);
614 p_delete(&prop);
617 void
618 client_unban(Client *c)
620 XMapWindow(globalconf.display, c->win);
621 window_setstate(c->win, NormalState);
622 if(c->titlebar.sw && c->titlebar.position != Off)
623 XMapWindow(globalconf.display, c->titlebar.sw->window);
626 void
627 client_unmanage(Client *c)
629 XWindowChanges wc;
630 Tag *tag;
632 wc.border_width = c->oldborder;
634 /* The server grab construct avoids race conditions. */
635 XGrabServer(globalconf.display);
637 XConfigureWindow(globalconf.display, c->win, CWBorderWidth, &wc); /* restore border */
639 /* remove client everywhere */
640 client_list_detach(&globalconf.clients, c);
641 focus_delete_client(c);
642 if(globalconf.scratch.client == c)
643 globalconf.scratch.client = NULL;
644 for(tag = globalconf.screens[c->screen].tags; tag; tag = tag->next)
645 untag_client(c, tag);
647 if(globalconf.focus->client == c)
648 client_focus(NULL, c->screen, True);
650 XUngrabButton(globalconf.display, AnyButton, AnyModifier, c->win);
651 window_setstate(c->win, WithdrawnState);
653 XSync(globalconf.display, False);
654 XUngrabServer(globalconf.display);
656 if(c->titlebar.sw)
657 simplewindow_delete(&c->titlebar.sw);
659 p_delete(&c);
662 void
663 client_updatewmhints(Client *c)
665 XWMHints *wmh;
667 if((wmh = XGetWMHints(globalconf.display, c->win)))
669 if((c->isurgent = ((wmh->flags & XUrgencyHint) && globalconf.focus->client != c)))
671 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
672 titlebar_draw(c);
674 if((wmh->flags & StateHint) && wmh->initial_state == WithdrawnState)
676 c->border = 0;
677 c->skip = True;
679 XFree(wmh);
683 long
684 client_updatesizehints(Client *c)
686 long msize;
687 XSizeHints size;
689 if(!XGetWMNormalHints(globalconf.display, c->win, &size, &msize))
690 return 0L;
692 if(size.flags & PBaseSize)
694 c->basew = size.base_width;
695 c->baseh = size.base_height;
697 else if(size.flags & PMinSize)
699 c->basew = size.min_width;
700 c->baseh = size.min_height;
702 else
703 c->basew = c->baseh = 0;
704 if(size.flags & PResizeInc)
706 c->incw = size.width_inc;
707 c->inch = size.height_inc;
709 else
710 c->incw = c->inch = 0;
712 if(size.flags & PMaxSize)
714 c->maxw = size.max_width;
715 c->maxh = size.max_height;
717 else
718 c->maxw = c->maxh = 0;
720 if(size.flags & PMinSize)
722 c->minw = size.min_width;
723 c->minh = size.min_height;
725 else if(size.flags & PBaseSize)
727 c->minw = size.base_width;
728 c->minh = size.base_height;
730 else
731 c->minw = c->minh = 0;
733 if(size.flags & PAspect)
735 c->minax = size.min_aspect.x;
736 c->maxax = size.max_aspect.x;
737 c->minay = size.min_aspect.y;
738 c->maxay = size.max_aspect.y;
740 else
741 c->minax = c->maxax = c->minay = c->maxay = 0;
743 if(c->maxw && c->minw && c->maxh && c->minh
744 && c->maxw == c->minw && c->maxh == c->minh)
745 c->isfixed = True;
747 return size.flags;
750 /** Returns True if a client is tagged
751 * with one of the tags
752 * \return True or False
754 Bool
755 client_isvisible(Client *c, int screen)
757 Tag *tag;
759 if(!c || c->screen != screen)
760 return False;
762 if(globalconf.scratch.client == c)
763 return globalconf.scratch.isvisible;
765 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
766 if(tag->selected && is_client_tagged(c, tag))
767 return True;
768 return False;
771 /** Set selected client transparency.
772 * Argument should be a floating between 0 and 100, -1 to disable.
773 * \param screen Screen ID
774 * \param arg unused arg
775 * \ingroup ui_callback
777 void
778 uicb_client_settrans(int screen __attribute__ ((unused)), char *arg)
780 double delta = 1.0, current_opacity = 1.0;
781 unsigned char *data;
782 Atom actual;
783 int format;
784 unsigned long n, left;
785 unsigned int current_opacity_raw = 0;
786 int set_prop = 0;
787 Client *sel = globalconf.focus->client;
789 if(!sel)
790 return;
792 XGetWindowProperty(globalconf.display, sel->win,
793 XInternAtom(globalconf.display, "_NET_WM_WINDOW_OPACITY", False),
794 0L, 1L, False, XA_CARDINAL, &actual, &format, &n, &left,
795 (unsigned char **) &data);
796 if(data)
798 memcpy(&current_opacity_raw, data, sizeof(unsigned int));
799 XFree(data);
800 current_opacity = current_opacity_raw / 0xffffffff;
802 else
803 set_prop = 1;
805 delta = compute_new_value_from_arg(arg, current_opacity);
807 if(delta <= 0.0)
808 delta = 0.0;
809 else if(delta > 1.0)
811 delta = 1.0;
812 set_prop = 1;
815 if(delta == 1.0 && !set_prop)
816 window_settrans(sel->win, -1);
817 else
818 window_settrans(sel->win, delta);
821 /** Find a visible client on screen. Return next client or previous client if
822 * reverse is true.
823 * \param sel current selected client
824 * \param reverse return previous instead of next if true
825 * \return next or previous client
827 static Client *
828 client_find_visible(Client *sel, Bool reverse)
830 Client *next;
831 Client *(*client_iter)(Client **, Client *) = client_list_next_cycle;
833 if(!sel) return NULL;
835 if(reverse)
836 client_iter = client_list_prev_cycle;
838 /* look for previous or next starting at sel */
840 for(next = client_iter(&globalconf.clients, sel);
841 next && next != sel && (next->skip || !client_isvisible(next, sel->screen));
842 next = client_iter(&globalconf.clients, next));
844 return next;
847 /** Swap currently focused client with previous visible.
848 * \param screen Screen ID
849 * \param arg nothing
850 * \ingroup ui_callback
852 void
853 uicb_client_swapprev(int screen __attribute__ ((unused)), char *arg __attribute__ ((unused)))
855 Client *prev;
857 if((prev = client_find_visible(globalconf.focus->client, True)))
859 client_list_swap(&globalconf.clients, prev, globalconf.focus->client);
860 globalconf.screens[prev->screen].need_arrange = True;
864 /** Swap currently focused client with next visible.
865 * \param screen Screen ID
866 * \param arg nothing
867 * \ingroup ui_callback
869 void
870 uicb_client_swapnext(int screen __attribute__ ((unused)), char *arg __attribute__ ((unused)))
872 Client *next;
874 if((next = client_find_visible(globalconf.focus->client, False)))
876 client_list_swap(&globalconf.clients, globalconf.focus->client, next);
877 globalconf.screens[next->screen].need_arrange = True;
881 /** Move and resize a client. Argument should be in format "x y w h" with
882 * absolute (1, 20, 300, ...) or relatives (+10, -200, ...) values.
883 * \param screen Screen ID
884 * \param arg x y w h
885 * \ingroup ui_callback
887 void
888 uicb_client_moveresize(int screen, char *arg)
890 int ox, oy, ow, oh; /* old geometry */
891 char x[8], y[8], w[8], h[8];
892 int mx, my, dx, dy, nmx, nmy;
893 unsigned int dui;
894 Window dummy;
895 area_t geometry;
896 Client *sel = globalconf.focus->client;
897 Layout *curlay = layout_get_current(screen);
899 if(!sel || sel->isfixed || !arg ||
900 (curlay->arrange != layout_floating && !sel->isfloating))
901 return;
903 if(sscanf(arg, "%s %s %s %s", x, y, w, h) != 4)
904 return;
906 geometry.x = (int) compute_new_value_from_arg(x, sel->geometry.x);
907 geometry.y = (int) compute_new_value_from_arg(y, sel->geometry.y);
908 geometry.width = (int) compute_new_value_from_arg(w, sel->geometry.width);
909 geometry.height = (int) compute_new_value_from_arg(h, sel->geometry.height);
911 ox = sel->geometry.x;
912 oy = sel->geometry.y;
913 ow = sel->geometry.width;
914 oh = sel->geometry.height;
916 Bool xqp = XQueryPointer(globalconf.display,
917 RootWindow(globalconf.display,
918 sel->phys_screen),
919 &dummy, &dummy, &mx, &my, &dx, &dy, &dui);
920 if(globalconf.screens[sel->screen].resize_hints)
921 geometry = client_geometry_hints(sel, geometry);
922 client_resize(sel, geometry, False);
923 if (xqp && ox <= mx && (ox + 2 * sel->border + ow) >= mx &&
924 oy <= my && (oy + 2 * sel->border + oh) >= my)
926 nmx = mx - (ox + sel->border) + sel->geometry.width - ow;
927 nmy = my - (oy + sel->border) + sel->geometry.height - oh;
929 if(nmx < -sel->border) /* can happen on a resize */
930 nmx = -sel->border;
931 if(nmy < -sel->border)
932 nmy = -sel->border;
934 XWarpPointer(globalconf.display,
935 None, sel->win,
936 0, 0, 0, 0, nmx, nmy);
940 /** Kill a client via a WM_DELETE_WINDOW request or XKillClient if not
941 * supported.
942 * \param c the client to kill
944 void
945 client_kill(Client *c)
947 XEvent ev;
949 if(client_isprotodel(globalconf.display, c->win))
951 ev.type = ClientMessage;
952 ev.xclient.window = c->win;
953 ev.xclient.message_type = XInternAtom(globalconf.display, "WM_PROTOCOLS", False);
954 ev.xclient.format = 32;
955 ev.xclient.data.l[0] = XInternAtom(globalconf.display, "WM_DELETE_WINDOW", False);
956 ev.xclient.data.l[1] = CurrentTime;
957 XSendEvent(globalconf.display, c->win, False, NoEventMask, &ev);
959 else
960 XKillClient(globalconf.display, c->win);
963 /** Kill the currently focused client.
964 * \param screen Screen ID
965 * \param arg unused
966 * \ingroup ui_callback
968 void
969 uicb_client_kill(int screen __attribute__ ((unused)), char *arg __attribute__ ((unused)))
971 Client *sel = globalconf.focus->client;
973 if(sel)
974 client_kill(sel);
977 /** Maximize the client to the given geometry.
978 * \param c the client to maximize
979 * \param geometry the geometry to use for maximizing
981 static void
982 client_maximize(Client *c, area_t geometry)
984 if((c->ismax = !c->ismax))
986 /* disable titlebar */
987 c->titlebar.position = Off;
988 c->wasfloating = c->isfloating;
989 c->m_geometry = c->geometry;
990 if(layout_get_current(c->screen)->arrange != layout_floating)
991 client_setfloating(c, True);
992 client_focus(c, c->screen, True);
993 client_resize(c, geometry, False);
995 else if(c->wasfloating)
997 c->titlebar.position = c->titlebar.dposition;
998 client_setfloating(c, True);
999 client_resize(c, c->m_geometry, False);
1001 else if(layout_get_current(c->screen)->arrange == layout_floating)
1003 c->titlebar.position = c->titlebar.dposition;
1004 client_resize(c, c->m_geometry, False);
1006 else
1008 c->titlebar.position = c->titlebar.dposition;
1009 client_setfloating(c, False);
1011 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
1014 /** Toggle maximization state for the focused client.
1015 * \param screen Screen ID
1016 * \param arg Unused
1017 * \ingroup ui_callback
1019 void
1020 uicb_client_togglemax(int screen, char *arg __attribute__ ((unused)))
1022 Client *sel = globalconf.focus->client;
1023 area_t area = screen_get_area(screen,
1024 globalconf.screens[screen].statusbar,
1025 &globalconf.screens[screen].padding);
1027 if(sel)
1029 area.width -= 2 * sel->border;
1030 area.height -= 2 * sel->border;
1031 client_maximize(sel, area);
1035 /** Toggle vertical maximization for the focused client.
1036 * \param screen Screen ID
1037 * \param arg Unused
1038 * \ingroup ui_callback
1040 void
1041 uicb_client_toggleverticalmax(int screen, char *arg __attribute__ ((unused)))
1043 Client *sel = globalconf.focus->client;
1044 area_t area = screen_get_area(screen,
1045 globalconf.screens[screen].statusbar,
1046 &globalconf.screens[screen].padding);
1048 if(sel)
1050 area.x = sel->geometry.x;
1051 area.width = sel->geometry.width;
1052 area.height -= 2 * sel->border;
1053 client_maximize(sel, area);
1058 /** Toggle horizontal maximization for the focused client.
1059 * \param screen Screen ID
1060 * \param arg Unused
1061 * \ingroup ui_callback
1063 void
1064 uicb_client_togglehorizontalmax(int screen, char *arg __attribute__ ((unused)))
1066 Client *sel = globalconf.focus->client;
1067 area_t area = screen_get_area(screen,
1068 globalconf.screens[screen].statusbar,
1069 &globalconf.screens[screen].padding);
1071 if(sel)
1073 area.y = sel->geometry.y;
1074 area.height = sel->geometry.height;
1075 area.width -= 2 * sel->border;
1076 client_maximize(sel, area);
1080 /** Set the client the master window.
1081 * \param screen Screen ID
1082 * \param arg Unused
1083 * \ingroup ui_callback
1085 void
1086 uicb_client_zoom(int screen, char *arg __attribute__ ((unused)))
1088 Client *c, *sel = globalconf.focus->client;
1090 if(!sel)
1091 return;
1093 for(c = globalconf.clients; !client_isvisible(c, screen); c = c->next);
1094 if(c == sel)
1095 for(sel = sel->next; sel && !client_isvisible(sel, screen); sel = sel->next);
1097 if(sel)
1099 client_list_detach(&globalconf.clients, sel);
1100 client_list_push(&globalconf.clients, sel);
1101 globalconf.screens[screen].need_arrange = True;
1105 /** Setfocus to the next visible client in the stack.
1106 * \param screen Screen ID
1107 * \param arg Unused
1108 * \ingroup ui_callback
1110 void
1111 uicb_client_focusnext(int screen, char *arg __attribute__ ((unused)))
1113 Client *next;
1115 if((next = client_find_visible(globalconf.focus->client, False)))
1116 client_focus(next, screen, True);
1119 /** Setfocus to the previous visible client in the stack.
1120 * \param screen Screen ID
1121 * \param arg Unused
1122 * \ingroup ui_callback
1124 void
1125 uicb_client_focusprev(int screen, char *arg __attribute__ ((unused)))
1127 Client *prev;
1129 if((prev = client_find_visible(globalconf.focus->client, True)))
1130 client_focus(prev, screen, True);
1133 /** Toggle the floating state of the focused client.
1134 * \param screen Screen ID
1135 * \param arg unused
1136 * \ingroup ui_callback
1138 void
1139 uicb_client_togglefloating(int screen __attribute__ ((unused)), char *arg __attribute__ ((unused)))
1141 if(globalconf.focus->client)
1142 client_setfloating(globalconf.focus->client, !globalconf.focus->client->isfloating);
1145 /** Toggle the scratch client attribute on the focused client.
1146 * \param screen screen number
1147 * \param arg unused argument
1148 * \ingroup ui_callback
1150 void
1151 uicb_client_setscratch(int screen, char *arg __attribute__ ((unused)))
1153 if(!globalconf.focus->client)
1154 return;
1156 if(globalconf.scratch.client == globalconf.focus->client)
1157 globalconf.scratch.client = NULL;
1158 else
1159 globalconf.scratch.client = globalconf.focus->client;
1161 widget_invalidate_cache(screen, WIDGET_CACHE_CLIENTS | WIDGET_CACHE_TAGS);
1162 globalconf.screens[screen].need_arrange = True;
1165 /** Toggle scratch client visibility.
1166 * \param screen screen number
1167 * \param arg unused argument
1168 * \ingroup ui_callback
1170 void
1171 uicb_client_togglescratch(int screen, char *arg __attribute__ ((unused)))
1173 if(globalconf.scratch.client)
1175 globalconf.scratch.isvisible = !globalconf.scratch.isvisible;
1176 if(globalconf.scratch.isvisible)
1177 client_focus(globalconf.scratch.client, screen, True);
1178 globalconf.screens[globalconf.scratch.client->screen].need_arrange = True;
1179 widget_invalidate_cache(globalconf.scratch.client->screen, WIDGET_CACHE_CLIENTS);
1183 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80