Simplify next/prev stuff
[awesome.git] / client.c
blob165f48a3d0766bc21febd32254cb7796aa32b8c6
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 "layouts/floating.h"
35 #include "common/xutil.h"
36 #include "common/xscreen.h"
38 extern AwesomeConf globalconf;
40 /** Load windows properties, restoring client's tag
41 * and floating state before awesome was restarted if any
42 * \todo this may bug if number of tags is != than before
43 * \param c Client ref
44 * \param screen Screen ID
45 * \return true if client had property
47 static Bool
48 client_loadprops(Client * c, int screen)
50 int i, ntags = 0;
51 Tag *tag;
52 char *prop;
53 Bool result = False;
55 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
56 ntags++;
58 prop = p_new(char, ntags + 2);
60 if(xgettextprop(globalconf.display, c->win,
61 XInternAtom(globalconf.display, "_AWESOME_PROPERTIES", False),
62 prop, ntags + 2))
64 for(i = 0, tag = globalconf.screens[screen].tags; tag && i < ntags && prop[i]; i++, tag = tag->next)
65 if(prop[i] == '1')
67 tag_client(c, tag);
68 result = True;
70 else
71 untag_client(c, tag);
73 if(i <= ntags && prop[i])
74 client_setfloating(c, prop[i] == '1');
77 p_delete(&prop);
79 return result;
82 /** Check if client supports protocol WM_DELETE_WINDOW
83 * \param disp the display
84 * \param win the Window
85 * \return True if client has WM_DELETE_WINDOW
87 static Bool
88 client_isprotodel(Display *disp, Window win)
90 int i, n;
91 Atom *protocols;
92 Bool ret = False;
94 if(XGetWMProtocols(disp, win, &protocols, &n))
96 for(i = 0; !ret && i < n; i++)
97 if(protocols[i] == XInternAtom(disp, "WM_DELETE_WINDOW", False))
98 ret = True;
99 XFree(protocols);
101 return ret;
104 /** Get a Client by its window
105 * \param list Client list to look info
106 * \param w Client window to find
107 * \return client
109 Client *
110 client_get_bywin(Client *list, Window w)
112 Client *c;
114 for(c = list; c && c->win != w; c = c->next);
115 return c;
118 /** Get a client by its name
119 * \param list Client list
120 * \param name name to search
121 * \return first matching client
123 Client *
124 client_get_byname(Client *list, char *name)
126 Client *c;
128 for(c = list; c; c = c->next)
129 if(strstr(c->name, name))
130 return c;
132 return NULL;
135 /** Update client name attribute with its title
136 * \param c the client
138 void
139 client_updatetitle(Client *c)
141 if(!xgettextprop(globalconf.display, c->win,
142 XInternAtom(globalconf.display, "_NET_WM_NAME", False), c->name, sizeof(c->name)))
143 xgettextprop(globalconf.display, c->win,
144 XInternAtom(globalconf.display, "WM_NAME", False), c->name, sizeof(c->name));
146 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
149 static void
150 client_unfocus(Client *c)
152 if(globalconf.screens[c->screen].opacity_unfocused != -1)
153 window_settrans(c->win, globalconf.screens[c->screen].opacity_unfocused);
154 XSetWindowBorder(globalconf.display, c->win,
155 globalconf.screens[c->screen].styles.normal.border.pixel);
156 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
157 focus_add_client(NULL);
160 /** Ban client and unmap it
161 * \param c the client
163 void
164 client_ban(Client *c)
166 if(globalconf.focus->client == c)
167 client_unfocus(c);
168 XUnmapWindow(globalconf.display, c->win);
169 window_setstate(c->win, IconicState);
172 /** Give focus to client, or to first client if c is NULL
173 * \param c client
174 * \param screen Screen ID
176 void
177 client_focus(Client *c, int screen, Bool raise)
179 int phys_screen = get_phys_screen(screen);
181 /* if c is NULL or invisible, take next client in the focus history */
182 if(!c || (c && !client_isvisible(c, screen)))
184 c = focus_get_current_client(screen);
185 /* if c is still NULL take next client in the stack */
186 if(!c)
187 for(c = globalconf.clients; c && (c->skip || !client_isvisible(c, screen)); c = c->next);
190 /* unfocus current selected client */
191 if(globalconf.focus->client)
192 client_unfocus(globalconf.focus->client);
194 if(c)
196 /* unban the client before focusing or it will fail */
197 client_unban(c);
198 /* save sel in focus history */
199 focus_add_client(c);
200 if(globalconf.screens[c->screen].opacity_unfocused != -1)
201 window_settrans(c->win, -1);
202 XSetWindowBorder(globalconf.display, c->win,
203 globalconf.screens[screen].styles.focus.border.pixel);
204 XSetInputFocus(globalconf.display, c->win, RevertToPointerRoot, CurrentTime);
205 if(raise)
207 XWindowChanges wc;
208 Layout *curlay = layout_get_current(screen);
209 if(c->isfloating || curlay->arrange == layout_floating)
210 XRaiseWindow(globalconf.display, c->win);
211 else
213 Client *client;
214 wc.stack_mode = Below;
215 wc.sibling = None;
216 for(client = globalconf.clients; client; client = client->next)
217 if(client != c && client_isvisible(client, c->screen) && client->isfloating)
219 XConfigureWindow(globalconf.display, client->win, CWSibling | CWStackMode, &wc);
220 wc.sibling = client->win;
222 XConfigureWindow(globalconf.display, c->win, CWSibling | CWStackMode, &wc);
223 wc.sibling = c->win;
224 for(client = globalconf.clients; client; client = client->next)
225 if(client != c && IS_TILED(client, c->screen))
227 XConfigureWindow(globalconf.display, client->win, CWSibling | CWStackMode, &wc);
228 wc.sibling = client->win;
232 /* since we're dropping EnterWindow events and sometimes the window
233 * will appear under the mouse, grabbuttons */
234 window_grabbuttons(phys_screen, c->win);
236 else
237 XSetInputFocus(globalconf.display,
238 RootWindow(globalconf.display, phys_screen),
239 RevertToPointerRoot, CurrentTime);
241 widget_invalidate_cache(screen, WIDGET_CACHE_CLIENTS);
242 ewmh_update_net_active_window(phys_screen);
245 /** Manage a new client
246 * \param w The window
247 * \param wa Window attributes
248 * \param screen Screen ID
250 void
251 client_manage(Window w, XWindowAttributes *wa, int screen)
253 Client *c, *t = NULL;
254 Window trans;
255 Bool rettrans, retloadprops;
256 XWindowChanges wc;
257 Tag *tag;
258 Rule *rule;
259 area_t screen_geom;
260 int phys_screen = get_phys_screen(screen);
261 long flags;
263 c = p_new(Client, 1);
265 c->screen = screen_get_bycoord(globalconf.screens_info, screen, wa->x, wa->y);
267 screen_geom = screen_get_area(c->screen,
268 globalconf.screens[screen].statusbar,
269 &globalconf.screens[screen].padding);
270 /* Initial values */
271 c->win = w;
272 c->geometry.x = c->f_geometry.x = c->m_geometry.x = MAX(wa->x, screen_geom.x);
273 c->geometry.y = c->f_geometry.y = c->m_geometry.y = MAX(wa->y, screen_geom.y);
274 c->geometry.width = c->f_geometry.width = c->m_geometry.width = wa->width;
275 c->geometry.height = c->f_geometry.height = c->m_geometry.height = wa->height;
276 c->oldborder = wa->border_width;
277 c->newcomer = True;
279 c->border = globalconf.screens[screen].borderpx;
281 /* Set windows borders */
282 wc.border_width = c->border;
283 XConfigureWindow(globalconf.display, w, CWBorderWidth, &wc);
284 XSetWindowBorder(globalconf.display, w,
285 globalconf.screens[screen].styles.normal.border.pixel);
286 /* propagates border_width, if size doesn't change */
287 window_configure(c->win, c->geometry, c->border);
289 /* update window title */
290 client_updatetitle(c);
292 /* update hints */
293 flags = client_updatesizehints(c);
294 client_updatewmhints(c);
296 /* Try to load props if any */
297 retloadprops = client_loadprops(c, screen);
299 /* Then check clients hints */
300 ewmh_check_client_hints(c);
302 /* Then apply rules if no props */
303 if(!retloadprops)
305 /* Get the client's rule */
306 if((rule = rule_matching_client(c)))
308 if(rule->screen != RULE_NOSCREEN)
309 move_client_to_screen(c, rule->screen, True);
310 else
311 move_client_to_screen(c, screen, True);
312 tag_client_with_rule(c, rule);
314 switch(rule->isfloating)
316 case Auto:
317 break;
318 case Yes:
319 client_setfloating(c, True);
320 break;
321 case No:
322 client_setfloating(c, False);
323 break;
326 if(rule->opacity >= 0.0f)
327 window_settrans(c->win, rule->opacity);
329 else
330 move_client_to_screen(c, screen, True);
333 /* check for transient and set tags like its parent,
334 * XGetTransientForHint returns 1 on success
336 if((rettrans = XGetTransientForHint(globalconf.display, w, &trans))
337 && (t = client_get_bywin(globalconf.clients, trans)))
338 for(tag = globalconf.screens[c->screen].tags; tag; tag = tag->next)
339 if(is_client_tagged(t, tag))
340 tag_client(c, tag);
342 /* should be floating if transsient or fixed */
343 if(!c->isfloating)
344 client_setfloating(c, rettrans || c->isfixed);
346 if(!(flags & (USPosition | PPosition)))
347 c->f_geometry =
348 globalconf.screens[c->screen].floating_placement(c->f_geometry, c->border, c->screen);
350 XSelectInput(globalconf.display, w, StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
352 /* handle xshape */
353 if(globalconf.have_shape)
355 XShapeSelectInput(globalconf.display, w, ShapeNotifyMask);
356 window_setshape(phys_screen, c->win);
359 /* attach to the stack */
360 if((rule = rule_matching_client(c)))
361 switch(rule->ismaster)
363 case Yes:
364 client_list_push(&globalconf.clients, c);
365 break;
366 case No:
367 client_list_append(&globalconf.clients, c);
368 break;
369 case Auto:
370 rule = NULL;
371 break;
374 if(!rule)
376 if(globalconf.screens[c->screen].new_become_master)
377 client_list_push(&globalconf.clients, c);
378 else
379 client_list_append(&globalconf.clients, c);
382 /* some windows require this */
383 XMoveResizeWindow(globalconf.display, c->win, c->geometry.x, c->geometry.y,
384 c->geometry.width, c->geometry.height);
386 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
387 ewmh_update_net_client_list(phys_screen);
390 /** Resize client window
391 * \param c client to resize
392 * \param geometry new window geometry
393 * \param sizehints respect size hints
394 * \param return True if resize has been done
396 Bool
397 client_resize(Client *c, area_t geometry, Bool sizehints)
399 int new_screen;
400 double dx, dy, max, min, ratio;
401 area_t area;
402 XWindowChanges wc;
404 if(sizehints)
406 if(c->minay > 0 && c->maxay > 0 && (geometry.height - c->baseh) > 0
407 && (geometry.width - c->basew) > 0)
409 dx = (double) (geometry.width - c->basew);
410 dy = (double) (geometry.height - c->baseh);
411 min = (double) (c->minax) / (double) (c->minay);
412 max = (double) (c->maxax) / (double) (c->maxay);
413 ratio = dx / dy;
414 if(max > 0 && min > 0 && ratio > 0)
416 if(ratio < min)
418 dy = (dx * min + dy) / (min * min + 1);
419 dx = dy * min;
420 geometry.width = (int) dx + c->basew;
421 geometry.height = (int) dy + c->baseh;
423 else if(ratio > max)
425 dy = (dx * min + dy) / (max * max + 1);
426 dx = dy * min;
427 geometry.width = (int) dx + c->basew;
428 geometry.height = (int) dy + c->baseh;
432 if(c->minw && geometry.width < c->minw)
433 geometry.width = c->minw;
434 if(c->minh && geometry.height < c->minh)
435 geometry.height = c->minh;
436 if(c->maxw && geometry.width > c->maxw)
437 geometry.width = c->maxw;
438 if(c->maxh && geometry.height > c->maxh)
439 geometry.height = c->maxh;
440 if(c->incw)
441 geometry.width -= (geometry.width - c->basew) % c->incw;
442 if(c->inch)
443 geometry.height -= (geometry.height - c->baseh) % c->inch;
445 if(geometry.width <= 0 || geometry.height <= 0)
446 return False;
447 /* offscreen appearance fixes */
448 area = get_display_area(get_phys_screen(c->screen),
449 NULL,
450 &globalconf.screens[c->screen].padding);
451 if(geometry.x > area.width)
452 geometry.x = area.width - geometry.width - 2 * c->border;
453 if(geometry.y > area.height)
454 geometry.y = area.height - geometry.height - 2 * c->border;
455 if(geometry.x + geometry.width + 2 * c->border < 0)
456 geometry.x = 0;
457 if(geometry.y + geometry.height + 2 * c->border < 0)
458 geometry.y = 0;
460 if(c->geometry.x != geometry.x || c->geometry.y != geometry.y
461 || c->geometry.width != geometry.width || c->geometry.height != geometry.height)
463 new_screen = screen_get_bycoord(globalconf.screens_info, c->screen, geometry.x, geometry.y);
465 c->geometry.x = wc.x = geometry.x;
466 c->geometry.y = wc.y = geometry.y;
467 c->geometry.width = wc.width = geometry.width;
468 c->geometry.height = wc.height = geometry.height;
469 wc.border_width = c->border;
471 /* save the floating geometry if the window is floating but not
472 * maximized */
473 if((c->isfloating ||
474 layout_get_current(new_screen)->arrange == layout_floating) && !c->ismax)
475 c->f_geometry = geometry;
477 XConfigureWindow(globalconf.display, c->win,
478 CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
479 window_configure(c->win, geometry, c->border);
481 if(c->screen != new_screen)
482 move_client_to_screen(c, new_screen, False);
484 return True;
486 return False;
489 void
490 client_setfloating(Client *c, Bool floating)
492 if(c->isfloating != floating)
494 if((c->isfloating = floating))
496 client_resize(c, c->f_geometry, False);
497 XRaiseWindow(globalconf.display, c->win);
499 else
501 XLowerWindow(globalconf.display, c->win);
502 if(c->ismax)
504 c->ismax = False;
505 client_resize(c, c->m_geometry, False);
508 if(client_isvisible(c, c->screen))
509 globalconf.screens[c->screen].need_arrange = True;
510 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
511 client_saveprops(c);
515 /** Save client properties as an X property
516 * \param c client
518 void
519 client_saveprops(Client *c)
521 int i = 0, ntags = 0;
522 char *prop;
523 Tag *tag;
525 for(tag = globalconf.screens[c->screen].tags; tag; tag = tag->next)
526 ntags++;
528 prop = p_new(char, ntags + 2);
530 for(tag = globalconf.screens[c->screen].tags; tag; tag = tag->next, i++)
531 prop[i] = is_client_tagged(c, tag) ? '1' : '0';
533 if(i <= ntags)
534 prop[i] = c->isfloating ? '1' : '0';
536 prop[++i] = '\0';
538 XChangeProperty(globalconf.display, c->win,
539 XInternAtom(globalconf.display, "_AWESOME_PROPERTIES", False),
540 XA_STRING, 8, PropModeReplace, (unsigned char *) prop, i);
542 p_delete(&prop);
545 void
546 client_unban(Client *c)
548 XMapWindow(globalconf.display, c->win);
549 window_setstate(c->win, NormalState);
552 void
553 client_unmanage(Client *c)
555 XWindowChanges wc;
556 Tag *tag;
558 wc.border_width = c->oldborder;
560 /* The server grab construct avoids race conditions. */
561 XGrabServer(globalconf.display);
563 XConfigureWindow(globalconf.display, c->win, CWBorderWidth, &wc); /* restore border */
565 /* remove client everywhere */
566 client_list_detach(&globalconf.clients, c);
567 focus_delete_client(c);
568 if(globalconf.scratch.client == c)
569 globalconf.scratch.client = NULL;
570 for(tag = globalconf.screens[c->screen].tags; tag; tag = tag->next)
571 untag_client(c, tag);
573 if(globalconf.focus->client == c)
574 client_focus(NULL, c->screen, True);
576 XUngrabButton(globalconf.display, AnyButton, AnyModifier, c->win);
577 window_setstate(c->win, WithdrawnState);
579 XSync(globalconf.display, False);
580 XUngrabServer(globalconf.display);
582 p_delete(&c);
585 void
586 client_updatewmhints(Client *c)
588 XWMHints *wmh;
590 if((wmh = XGetWMHints(globalconf.display, c->win)))
592 if((c->isurgent = (wmh->flags & XUrgencyHint)))
593 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
594 if((wmh->flags & StateHint) && wmh->initial_state == WithdrawnState)
596 c->border = 0;
597 c->skip = True;
599 XFree(wmh);
603 long
604 client_updatesizehints(Client *c)
606 long msize;
607 XSizeHints size;
609 if(!XGetWMNormalHints(globalconf.display, c->win, &size, &msize))
610 return 0L;
612 if(size.flags & PBaseSize)
614 c->basew = size.base_width;
615 c->baseh = size.base_height;
617 else if(size.flags & PMinSize)
619 c->basew = size.min_width;
620 c->baseh = size.min_height;
622 else
623 c->basew = c->baseh = 0;
624 if(size.flags & PResizeInc)
626 c->incw = size.width_inc;
627 c->inch = size.height_inc;
629 else
630 c->incw = c->inch = 0;
632 if(size.flags & PMaxSize)
634 c->maxw = size.max_width;
635 c->maxh = size.max_height;
637 else
638 c->maxw = c->maxh = 0;
640 if(size.flags & PMinSize)
642 c->minw = size.min_width;
643 c->minh = size.min_height;
645 else if(size.flags & PBaseSize)
647 c->minw = size.base_width;
648 c->minh = size.base_height;
650 else
651 c->minw = c->minh = 0;
653 if(size.flags & PAspect)
655 c->minax = size.min_aspect.x;
656 c->maxax = size.max_aspect.x;
657 c->minay = size.min_aspect.y;
658 c->maxay = size.max_aspect.y;
660 else
661 c->minax = c->maxax = c->minay = c->maxay = 0;
663 if(c->maxw && c->minw && c->maxh && c->minh
664 && c->maxw == c->minw && c->maxh == c->minh)
665 c->isfixed = True;
667 return size.flags;
670 /** Returns True if a client is tagged
671 * with one of the tags
672 * \return True or False
674 Bool
675 client_isvisible(Client *c, int screen)
677 Tag *tag;
679 if(!c || c->screen != screen)
680 return False;
682 if(globalconf.scratch.client == c)
683 return globalconf.scratch.isvisible;
685 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
686 if(tag->selected && is_client_tagged(c, tag))
687 return True;
688 return False;
691 /** Set selected client transparency
692 * \param screen Screen ID
693 * \param arg unused arg
694 * \ingroup ui_callback
696 void
697 uicb_client_settrans(int screen __attribute__ ((unused)), char *arg)
699 double delta = 100.0, current_opacity = 100.0;
700 unsigned char *data;
701 Atom actual;
702 int format;
703 unsigned long n, left;
704 unsigned int current_opacity_raw = 0;
705 int set_prop = 0;
706 Client *sel = globalconf.focus->client;
708 if(!sel)
709 return;
711 XGetWindowProperty(globalconf.display, sel->win,
712 XInternAtom(globalconf.display, "_NET_WM_WINDOW_OPACITY", False),
713 0L, 1L, False, XA_CARDINAL, &actual, &format, &n, &left,
714 (unsigned char **) &data);
715 if(data)
717 memcpy(&current_opacity_raw, data, sizeof(unsigned int));
718 XFree(data);
719 current_opacity = (current_opacity_raw * 100.0) / 0xffffffff;
721 else
722 set_prop = 1;
724 delta = compute_new_value_from_arg(arg, current_opacity);
726 if(delta <= 0.0)
727 delta = 0.0;
728 else if(delta > 100.0)
730 delta = 100.0;
731 set_prop = 1;
734 if(delta == 100.0 && !set_prop)
735 window_settrans(sel->win, -1);
736 else
737 window_settrans(sel->win, delta);
740 static Client *
741 client_find_visible(Client *sel, Bool reverse)
743 Client *next;
744 Client *(*client_iter)(Client **, Client *) = client_list_next_cycle;
746 if(!sel) return NULL;
748 if(reverse)
749 client_iter = client_list_prev_cycle;
751 /* look for previous or next starting at sel */
753 for(next = client_iter(&globalconf.clients, sel);
754 next && (next->skip || !client_isvisible(next, sel->screen));
755 next = client_iter(&globalconf.clients, next));
757 return next;
760 /** Swap current with previous client
761 * \param screen Screen ID
762 * \param arg nothing
763 * \ingroup ui_callback
765 void
766 uicb_client_swapprev(int screen __attribute__ ((unused)),
767 char *arg __attribute__ ((unused)))
769 Client *prev;
771 if((prev = client_find_visible(globalconf.focus->client, True)))
773 client_list_swap(&globalconf.clients, prev, globalconf.focus->client);
774 globalconf.screens[prev->screen].need_arrange = True;
778 /** Swap current with next client
779 * \param screen Screen ID
780 * \param arg nothing
781 * \ingroup ui_callback
783 void
784 uicb_client_swapnext(int screen __attribute__ ((unused)),
785 char *arg __attribute__ ((unused)))
787 Client *next;
789 if((next = client_find_visible(globalconf.focus->client, False)))
791 client_list_swap(&globalconf.clients, globalconf.focus->client, next);
792 globalconf.screens[next->screen].need_arrange = True;
796 /** Move and resize client
797 * \param screen Screen ID
798 * \param arg x y w h
799 * \ingroup ui_callback
801 void
802 uicb_client_moveresize(int screen, char *arg)
804 int ox, oy, ow, oh; /* old geometry */
805 char x[8], y[8], w[8], h[8];
806 int mx, my, dx, dy, nmx, nmy;
807 unsigned int dui;
808 Window dummy;
809 area_t area;
810 Client *sel = globalconf.focus->client;
811 Layout *curlay = layout_get_current(screen);
813 if(!sel || sel->isfixed || !arg ||
814 (curlay->arrange != layout_floating && !sel->isfloating))
815 return;
817 if(sscanf(arg, "%s %s %s %s", x, y, w, h) != 4)
818 return;
820 area.x = (int) compute_new_value_from_arg(x, sel->geometry.x);
821 area.y = (int) compute_new_value_from_arg(y, sel->geometry.y);
822 area.width = (int) compute_new_value_from_arg(w, sel->geometry.width);
823 area.height = (int) compute_new_value_from_arg(h, sel->geometry.height);
825 ox = sel->geometry.x;
826 oy = sel->geometry.y;
827 ow = sel->geometry.width;
828 oh = sel->geometry.height;
830 Bool xqp = XQueryPointer(globalconf.display,
831 RootWindow(globalconf.display,
832 get_phys_screen(screen)),
833 &dummy, &dummy, &mx, &my, &dx, &dy, &dui);
834 client_resize(sel, area, globalconf.screens[sel->screen].resize_hints);
835 if (xqp && ox <= mx && (ox + 2 * sel->border + ow) >= mx &&
836 oy <= my && (oy + 2 * sel->border + oh) >= my)
838 nmx = mx - (ox + sel->border) + sel->geometry.width - ow;
839 nmy = my - (oy + sel->border) + sel->geometry.height - oh;
841 if(nmx < -sel->border) /* can happen on a resize */
842 nmx = -sel->border;
843 if(nmy < -sel->border)
844 nmy = -sel->border;
846 XWarpPointer(globalconf.display,
847 None, sel->win,
848 0, 0, 0, 0, nmx, nmy);
852 void
853 client_kill(Client *c)
855 XEvent ev;
857 if(client_isprotodel(globalconf.display, c->win))
859 ev.type = ClientMessage;
860 ev.xclient.window = c->win;
861 ev.xclient.message_type = XInternAtom(globalconf.display, "WM_PROTOCOLS", False);
862 ev.xclient.format = 32;
863 ev.xclient.data.l[0] = XInternAtom(globalconf.display, "WM_DELETE_WINDOW", False);
864 ev.xclient.data.l[1] = CurrentTime;
865 XSendEvent(globalconf.display, c->win, False, NoEventMask, &ev);
867 else
868 XKillClient(globalconf.display, c->win);
871 /** Kill selected client
872 * \param screen Screen ID
873 * \param arg unused
874 * \ingroup ui_callback
876 void
877 uicb_client_kill(int screen __attribute__ ((unused)), char *arg __attribute__ ((unused)))
879 Client *sel = globalconf.focus->client;
881 if(sel)
882 client_kill(sel);
885 static void
886 client_maximize(Client *c, area_t geometry)
889 if((c->ismax = !c->ismax))
891 c->wasfloating = c->isfloating;
892 c->m_geometry = c->geometry;
893 if(layout_get_current(c->screen)->arrange != layout_floating)
894 client_setfloating(c, True);
895 client_focus(c, c->screen, True);
896 client_resize(c, geometry, False);
897 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
899 else if(c->wasfloating)
901 client_setfloating(c, True);
902 client_resize(c, c->m_geometry, False);
903 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
905 else if(layout_get_current(c->screen)->arrange == layout_floating)
907 client_resize(c, c->m_geometry, False);
908 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
910 else
912 client_setfloating(c, False);
913 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
917 /** Toggle maximize for client
918 * \param screen Screen ID
919 * \param arg Unused
920 * \ingroup ui_callback
922 void
923 uicb_client_togglemax(int screen, char *arg __attribute__ ((unused)))
925 Client *sel = globalconf.focus->client;
926 area_t area = screen_get_area(screen,
927 globalconf.screens[screen].statusbar,
928 &globalconf.screens[screen].padding);
930 if(sel)
932 area.width -= 2 * sel->border;
933 area.height -= 2 * sel->border;
934 client_maximize(sel, area);
938 /** Toggle vertical maximize for client
939 * \param screen Screen ID
940 * \param arg Unused
941 * \ingroup ui_callback
943 void
944 uicb_client_toggleverticalmax(int screen, char *arg __attribute__ ((unused)))
946 Client *sel = globalconf.focus->client;
947 area_t area = screen_get_area(screen,
948 globalconf.screens[screen].statusbar,
949 &globalconf.screens[screen].padding);
951 if(sel)
953 area.x = sel->geometry.x;
954 area.width = sel->geometry.width;
955 area.height -= 2 * sel->border;
956 client_maximize(sel, area);
961 /** Toggle horizontal maximize for client
962 * \param screen Screen ID
963 * \param arg Unused
964 * \ingroup ui_callback
966 void
967 uicb_client_togglehorizontalmax(int screen, char *arg __attribute__ ((unused)))
969 Client *sel = globalconf.focus->client;
970 area_t area = screen_get_area(screen,
971 globalconf.screens[screen].statusbar,
972 &globalconf.screens[screen].padding);
974 if(sel)
976 area.y = sel->geometry.y;
977 area.height = sel->geometry.height;
978 area.width -= 2 * sel->border;
979 client_maximize(sel, area);
983 /** Zoom client
984 * \param screen Screen ID
985 * \param arg Unused
986 * \ingroup ui_callback
988 void
989 uicb_client_zoom(int screen, char *arg __attribute__ ((unused)))
991 Client *c, *sel = globalconf.focus->client;
993 if(!sel)
994 return;
996 for(c = globalconf.clients; !client_isvisible(c, screen); c = c->next);
997 if(c == sel)
998 for(sel = sel->next; sel && !client_isvisible(sel, screen); sel = sel->next);
1000 if(sel)
1002 client_list_detach(&globalconf.clients, sel);
1003 client_list_push(&globalconf.clients, sel);
1004 globalconf.screens[screen].need_arrange = True;
1008 /** Send focus to next client in stack
1009 * \param screen Screen ID
1010 * \param arg Unused
1011 * \ingroup ui_callback
1013 void
1014 uicb_client_focusnext(int screen, char *arg __attribute__ ((unused)))
1016 Client *next;
1018 if((next = client_find_visible(globalconf.focus->client, False)))
1019 client_focus(next, screen, True);
1022 /** Send focus to previous client in stack
1023 * \param screen Screen ID
1024 * \param arg Unused
1025 * \ingroup ui_callback
1027 void
1028 uicb_client_focusprev(int screen, char *arg __attribute__ ((unused)))
1030 Client *prev;
1032 if((prev = client_find_visible(globalconf.focus->client, True)))
1033 client_focus(prev, screen, True);
1036 /** Toggle floating state of a client
1037 * \param screen Screen ID
1038 * \param arg unused
1039 * \ingroup ui_callback
1041 void
1042 uicb_client_togglefloating(int screen __attribute__ ((unused)),
1043 char *arg __attribute__ ((unused)))
1045 if(globalconf.focus->client)
1046 client_setfloating(globalconf.focus->client, !globalconf.focus->client->isfloating);
1049 /** Toggle scratch client attribute
1050 * \param screen screen number
1051 * \param arg unused argument
1052 * \ingroup ui_callback
1054 void
1055 uicb_client_setscratch(int screen,
1056 char *arg __attribute__ ((unused)))
1058 if(!globalconf.focus->client)
1059 return;
1061 if(globalconf.scratch.client == globalconf.focus->client)
1062 globalconf.scratch.client = NULL;
1063 else
1064 globalconf.scratch.client = globalconf.focus->client;
1066 widget_invalidate_cache(screen, WIDGET_CACHE_CLIENTS | WIDGET_CACHE_TAGS);
1067 globalconf.screens[screen].need_arrange = True;
1070 /** Toggle scratch client visibility
1071 * \param screen screen number
1072 * \param arg unused argument
1073 * \ingroup ui_callback
1075 void
1076 uicb_client_togglescratch(int screen,
1077 char *arg __attribute__ ((unused)))
1079 if(globalconf.scratch.client)
1081 globalconf.scratch.isvisible = !globalconf.scratch.isvisible;
1082 if(globalconf.scratch.isvisible)
1083 client_focus(globalconf.scratch.client, screen, True);
1084 globalconf.screens[globalconf.scratch.client->screen].need_arrange = True;
1085 widget_invalidate_cache(globalconf.scratch.client->screen, WIDGET_CACHE_CLIENTS);
1088 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80