Fix segfault after viewing same tag twice and doing a tag_prev_selected.
[awesome.git] / client.c
blob433753df65fdfaaeb0f262d0a6f9f8c422355148
1 /*
2 * client.c - client management
4 * Copyright © 2007-2008 Julien Danjou <julien@danjou.info>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <stdio.h>
23 #include <X11/Xatom.h>
24 #include <X11/extensions/shape.h>
25 #include <X11/extensions/Xinerama.h>
27 #include "client.h"
28 #include "tag.h"
29 #include "rules.h"
30 #include "window.h"
31 #include "focus.h"
32 #include "ewmh.h"
33 #include "screen.h"
34 #include "widget.h"
35 #include "xutil.h"
36 #include "layouts/floating.h"
38 extern AwesomeConf globalconf;
40 /** Load windows properties, restoring client's tag
41 * and floating state before awesome was restarted if any
42 * \todo this may bug if number of tags is != than before
43 * \param c Client ref
44 * \param screen Screen ID
45 * \return true if client had property
47 static Bool
48 client_loadprops(Client * c, int screen)
50 int i, ntags = 0;
51 Tag *tag;
52 char *prop;
53 Bool result = False;
55 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
56 ntags++;
58 prop = p_new(char, ntags + 2);
60 if(xgettextprop(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(c->win, XInternAtom(globalconf.display, "_NET_WM_NAME", False), c->name, sizeof(c->name)))
142 xgettextprop(c->win, XInternAtom(globalconf.display, "WM_NAME", False), c->name, sizeof(c->name));
144 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
147 static void
148 client_unfocus(Client *c)
150 if(globalconf.screens[c->screen].opacity_unfocused != -1)
151 window_settrans(c->win, globalconf.screens[c->screen].opacity_unfocused);
152 XSetWindowBorder(globalconf.display, c->win,
153 globalconf.screens[c->screen].colors_normal[ColBorder].pixel);
154 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
155 focus_add_client(NULL);
158 /** Ban client and unmap it
159 * \param c the client
161 void
162 client_ban(Client *c)
164 if(globalconf.focus->client == c)
165 client_unfocus(c);
166 XUnmapWindow(globalconf.display, c->win);
167 window_setstate(c->win, IconicState);
170 /** Give focus to client, or to first client if c is NULL
171 * \param c client
172 * \param screen Screen ID
174 void
175 client_focus(Client *c, int screen, Bool raise)
177 int phys_screen = get_phys_screen(screen);
179 /* if c is NULL or invisible, take next client in the focus history */
180 if(!c || (c && !client_isvisible(c, screen)))
182 c = focus_get_current_client(screen);
183 /* if c is still NULL take next client in the stack */
184 if(!c)
185 for(c = globalconf.clients; c && (c->skip || !client_isvisible(c, screen)); c = c->next);
188 /* unfocus current selected client */
189 if(globalconf.focus->client)
190 client_unfocus(globalconf.focus->client);
192 if(c)
194 /* unban the client before focusing or it will fail */
195 client_unban(c);
196 /* save sel in focus history */
197 focus_add_client(c);
198 if(globalconf.screens[c->screen].opacity_unfocused != -1)
199 window_settrans(c->win, globalconf.screens[screen].opacity_unfocused);
200 XSetWindowBorder(globalconf.display, c->win,
201 globalconf.screens[screen].colors_selected[ColBorder].pixel);
202 XSetInputFocus(globalconf.display, c->win, RevertToPointerRoot, CurrentTime);
203 if(raise)
205 XWindowChanges wc;
206 Layout *curlay = layout_get_current(screen);
207 if(c->isfloating || curlay->arrange == layout_floating)
208 XRaiseWindow(globalconf.display, c->win);
209 else
211 Client *client;
212 wc.stack_mode = Below;
213 wc.sibling = c->win;
214 for(client = globalconf.clients; client; client = client->next)
215 if(client != c && IS_TILED(client, c->screen))
217 XConfigureWindow(globalconf.display, client->win, CWSibling | CWStackMode, &wc);
218 wc.sibling = client->win;
222 /* since we're dropping EnterWindow events and sometimes the window
223 * will appear under the mouse, grabbuttons */
224 window_grabbuttons(phys_screen, c->win);
226 else
227 XSetInputFocus(globalconf.display,
228 RootWindow(globalconf.display, phys_screen),
229 RevertToPointerRoot, CurrentTime);
231 widget_invalidate_cache(screen, WIDGET_CACHE_CLIENTS);
232 ewmh_update_net_active_window(phys_screen);
233 globalconf.drop_events |= EnterWindowMask;
236 /** Manage a new client
237 * \param w The window
238 * \param wa Window attributes
239 * \param screen Screen ID
241 void
242 client_manage(Window w, XWindowAttributes *wa, int screen)
244 Client *c, *t = NULL;
245 Window trans;
246 Bool rettrans;
247 XWindowChanges wc;
248 Tag *tag;
249 Rule *rule;
250 Area screen_geom;
251 int phys_screen = get_phys_screen(screen);
252 long flags;
254 c = p_new(Client, 1);
256 c->screen = screen_get_bycoord(screen, wa->x, wa->y);
258 screen_geom = screen_get_area(c->screen,
259 globalconf.screens[screen].statusbar,
260 &globalconf.screens[screen].padding);
261 /* Initial values */
262 c->win = w;
263 c->geometry.x = c->f_geometry.x = c->m_geometry.x = MAX(wa->x, screen_geom.x);
264 c->geometry.y = c->f_geometry.y = c->m_geometry.y = MAX(wa->y, screen_geom.y);
265 c->geometry.width = c->f_geometry.width = c->m_geometry.width = wa->width;
266 c->geometry.height = c->f_geometry.height = c->m_geometry.height = wa->height;
267 c->oldborder = wa->border_width;
268 c->newcomer = True;
270 c->border = globalconf.screens[screen].borderpx;
272 /* Set windows borders */
273 wc.border_width = c->border;
274 XConfigureWindow(globalconf.display, w, CWBorderWidth, &wc);
275 XSetWindowBorder(globalconf.display, w, globalconf.screens[screen].colors_normal[ColBorder].pixel);
276 /* propagates border_width, if size doesn't change */
277 window_configure(c->win, c->geometry, c->border);
279 /* update window title */
280 client_updatetitle(c);
282 /* update hints */
283 flags = client_updatesizehints(c);
284 client_updatewmhints(c);
286 /* First check clients hints */
287 ewmh_check_client_hints(c);
289 /* loadprops or apply rules if no props */
290 if(!client_loadprops(c, screen))
292 /* Get the client's rule */
293 if((rule = rule_matching_client(c)))
295 if(rule->screen != RULE_NOSCREEN)
296 move_client_to_screen(c, rule->screen, True);
297 else
298 move_client_to_screen(c, screen, True);
299 tag_client_with_rule(c, rule);
301 switch(rule->isfloating)
303 case Auto:
304 break;
305 case Yes:
306 client_setfloating(c, True);
307 break;
308 case No:
309 client_setfloating(c, False);
310 break;
313 if(rule->opacity >= 0.0f)
314 window_settrans(c->win, rule->opacity);
316 else
317 move_client_to_screen(c, screen, True);
320 /* check for transient and set tags like its parent,
321 * XGetTransientForHint returns 1 on success
323 if((rettrans = XGetTransientForHint(globalconf.display, w, &trans))
324 && (t = client_get_bywin(globalconf.clients, trans)))
325 for(tag = globalconf.screens[c->screen].tags; tag; tag = tag->next)
326 if(is_client_tagged(t, tag))
327 tag_client(c, tag);
329 /* should be floating if transsient or fixed */
330 if(!c->isfloating)
331 client_setfloating(c, rettrans || c->isfixed);
333 if(!(flags & (USPosition | PPosition)))
334 c->f_geometry =
335 globalconf.screens[c->screen].floating_placement(c->f_geometry, c->border, c->screen);
337 XSelectInput(globalconf.display, w, StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
339 /* handle xshape */
340 if(globalconf.have_shape)
342 XShapeSelectInput(globalconf.display, w, ShapeNotifyMask);
343 window_setshape(phys_screen, c->win);
346 /* attach to the stack */
347 if((rule = rule_matching_client(c)))
348 switch(rule->ismaster)
350 case Yes:
351 client_list_push(&globalconf.clients, c);
352 break;
353 case No:
354 client_list_append(&globalconf.clients, c);
355 break;
356 case Auto:
357 rule = NULL;
358 break;
361 if(!rule)
363 if(globalconf.screens[c->screen].new_become_master)
364 client_list_push(&globalconf.clients, c);
365 else
366 client_list_append(&globalconf.clients, c);
369 /* some windows require this */
370 XMoveResizeWindow(globalconf.display, c->win, c->geometry.x, c->geometry.y,
371 c->geometry.width, c->geometry.height);
373 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
374 ewmh_update_net_client_list(phys_screen);
377 /** Resize client window
378 * \param c client to resize
379 * \param geometry new window geometry
380 * \param sizehints respect size hints
381 * \param return True if resize has been done
383 Bool
384 client_resize(Client *c, Area geometry, Bool sizehints)
386 int new_screen;
387 double dx, dy, max, min, ratio;
388 Area area;
389 XWindowChanges wc;
391 if(sizehints)
393 if(c->minay > 0 && c->maxay > 0 && (geometry.height - c->baseh) > 0
394 && (geometry.width - c->basew) > 0)
396 dx = (double) (geometry.width - c->basew);
397 dy = (double) (geometry.height - c->baseh);
398 min = (double) (c->minax) / (double) (c->minay);
399 max = (double) (c->maxax) / (double) (c->maxay);
400 ratio = dx / dy;
401 if(max > 0 && min > 0 && ratio > 0)
403 if(ratio < min)
405 dy = (dx * min + dy) / (min * min + 1);
406 dx = dy * min;
407 geometry.width = (int) dx + c->basew;
408 geometry.height = (int) dy + c->baseh;
410 else if(ratio > max)
412 dy = (dx * min + dy) / (max * max + 1);
413 dx = dy * min;
414 geometry.width = (int) dx + c->basew;
415 geometry.height = (int) dy + c->baseh;
419 if(c->minw && geometry.width < c->minw)
420 geometry.width = c->minw;
421 if(c->minh && geometry.height < c->minh)
422 geometry.height = c->minh;
423 if(c->maxw && geometry.width > c->maxw)
424 geometry.width = c->maxw;
425 if(c->maxh && geometry.height > c->maxh)
426 geometry.height = c->maxh;
427 if(c->incw)
428 geometry.width -= (geometry.width - c->basew) % c->incw;
429 if(c->inch)
430 geometry.height -= (geometry.height - c->baseh) % c->inch;
432 if(geometry.width <= 0 || geometry.height <= 0)
433 return False;
434 /* offscreen appearance fixes */
435 area = get_display_area(get_phys_screen(c->screen),
436 NULL,
437 &globalconf.screens[c->screen].padding);
438 if(geometry.x > area.width)
439 geometry.x = area.width - geometry.width - 2 * c->border;
440 if(geometry.y > area.height)
441 geometry.y = area.height - geometry.height - 2 * c->border;
442 if(geometry.x + geometry.width + 2 * c->border < 0)
443 geometry.x = 0;
444 if(geometry.y + geometry.height + 2 * c->border < 0)
445 geometry.y = 0;
447 if(c->geometry.x != geometry.x || c->geometry.y != geometry.y
448 || c->geometry.width != geometry.width || c->geometry.height != geometry.height)
450 new_screen = screen_get_bycoord(c->screen, geometry.x, geometry.y);
452 c->geometry.x = wc.x = geometry.x;
453 c->geometry.y = wc.y = geometry.y;
454 c->geometry.width = wc.width = geometry.width;
455 c->geometry.height = wc.height = geometry.height;
456 wc.border_width = c->border;
458 /* save the floating geometry if the window is floating but not
459 * maximized */
460 if((c->isfloating ||
461 layout_get_current(new_screen)->arrange == layout_floating) && !c->ismax)
462 c->f_geometry = geometry;
464 XConfigureWindow(globalconf.display, c->win,
465 CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
466 window_configure(c->win, geometry, c->border);
468 if(c->screen != new_screen)
469 move_client_to_screen(c, new_screen, False);
471 return True;
473 return False;
476 void
477 client_setfloating(Client *c, Bool floating)
479 if(c->isfloating != floating)
481 if((c->isfloating = floating))
483 client_resize(c, c->f_geometry, False);
484 XRaiseWindow(globalconf.display, c->win);
486 else
488 XLowerWindow(globalconf.display, c->win);
489 if(c->ismax)
491 c->ismax = False;
492 client_resize(c, c->m_geometry, False);
495 if(client_isvisible(c, c->screen))
496 globalconf.screens[c->screen].need_arrange = True;
497 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
498 client_saveprops(c);
502 /** Save client properties as an X property
503 * \param c client
505 void
506 client_saveprops(Client *c)
508 int i = 0, ntags = 0;
509 char *prop;
510 Tag *tag;
512 for(tag = globalconf.screens[c->screen].tags; tag; tag = tag->next)
513 ntags++;
515 prop = p_new(char, ntags + 2);
517 for(tag = globalconf.screens[c->screen].tags; tag; tag = tag->next, i++)
518 prop[i] = is_client_tagged(c, tag) ? '1' : '0';
520 if(i <= ntags)
521 prop[i] = c->isfloating ? '1' : '0';
523 prop[++i] = '\0';
525 XChangeProperty(globalconf.display, c->win,
526 XInternAtom(globalconf.display, "_AWESOME_PROPERTIES", False),
527 XA_STRING, 8, PropModeReplace, (unsigned char *) prop, i);
529 p_delete(&prop);
532 void
533 client_unban(Client *c)
535 XMapWindow(globalconf.display, c->win);
536 window_setstate(c->win, NormalState);
539 void
540 client_unmanage(Client *c)
542 XWindowChanges wc;
543 Tag *tag;
545 wc.border_width = c->oldborder;
547 /* The server grab construct avoids race conditions. */
548 XGrabServer(globalconf.display);
550 XConfigureWindow(globalconf.display, c->win, CWBorderWidth, &wc); /* restore border */
552 /* remove client everywhere */
553 client_list_detach(&globalconf.clients, c);
554 focus_delete_client(c);
555 if(globalconf.scratch.client == c)
556 globalconf.scratch.client = NULL;
557 for(tag = globalconf.screens[c->screen].tags; tag; tag = tag->next)
558 untag_client(c, tag);
560 if(globalconf.focus->client == c)
561 client_focus(NULL, c->screen, True);
563 XUngrabButton(globalconf.display, AnyButton, AnyModifier, c->win);
564 window_setstate(c->win, WithdrawnState);
566 XSync(globalconf.display, False);
567 XUngrabServer(globalconf.display);
569 p_delete(&c);
572 void
573 client_updatewmhints(Client *c)
575 XWMHints *wmh;
577 if((wmh = XGetWMHints(globalconf.display, c->win)))
579 if((c->isurgent = (wmh->flags & XUrgencyHint)))
580 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
581 if((wmh->flags & StateHint) && wmh->initial_state == WithdrawnState)
583 c->border = 0;
584 c->skip = True;
586 XFree(wmh);
590 long
591 client_updatesizehints(Client *c)
593 long msize;
594 XSizeHints size;
596 if(!XGetWMNormalHints(globalconf.display, c->win, &size, &msize))
597 return 0L;
599 if(size.flags & PBaseSize)
601 c->basew = size.base_width;
602 c->baseh = size.base_height;
604 else if(size.flags & PMinSize)
606 c->basew = size.min_width;
607 c->baseh = size.min_height;
609 else
610 c->basew = c->baseh = 0;
611 if(size.flags & PResizeInc)
613 c->incw = size.width_inc;
614 c->inch = size.height_inc;
616 else
617 c->incw = c->inch = 0;
619 if(size.flags & PMaxSize)
621 c->maxw = size.max_width;
622 c->maxh = size.max_height;
624 else
625 c->maxw = c->maxh = 0;
627 if(size.flags & PMinSize)
629 c->minw = size.min_width;
630 c->minh = size.min_height;
632 else if(size.flags & PBaseSize)
634 c->minw = size.base_width;
635 c->minh = size.base_height;
637 else
638 c->minw = c->minh = 0;
640 if(size.flags & PAspect)
642 c->minax = size.min_aspect.x;
643 c->maxax = size.max_aspect.x;
644 c->minay = size.min_aspect.y;
645 c->maxay = size.max_aspect.y;
647 else
648 c->minax = c->maxax = c->minay = c->maxay = 0;
650 if(c->maxw && c->minw && c->maxh && c->minh
651 && c->maxw == c->minw && c->maxh == c->minh)
652 c->isfixed = True;
654 return size.flags;
657 /** Returns True if a client is tagged
658 * with one of the tags
659 * \return True or False
661 Bool
662 client_isvisible(Client *c, int screen)
664 Tag *tag;
666 if(!c || c->screen != screen)
667 return False;
669 if(globalconf.scratch.client == c)
670 return globalconf.scratch.isvisible;
672 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
673 if(tag->selected && is_client_tagged(c, tag))
674 return True;
675 return False;
678 /** Set selected client transparency
679 * \param screen Screen ID
680 * \param arg unused arg
681 * \ingroup ui_callback
683 void
684 uicb_client_settrans(int screen __attribute__ ((unused)), char *arg)
686 double delta = 100.0, current_opacity = 100.0;
687 unsigned char *data;
688 Atom actual;
689 int format;
690 unsigned long n, left;
691 unsigned int current_opacity_raw = 0;
692 int set_prop = 0;
693 Client *sel = globalconf.focus->client;
695 if(!sel)
696 return;
698 XGetWindowProperty(globalconf.display, sel->win,
699 XInternAtom(globalconf.display, "_NET_WM_WINDOW_OPACITY", False),
700 0L, 1L, False, XA_CARDINAL, &actual, &format, &n, &left,
701 (unsigned char **) &data);
702 if(data)
704 memcpy(&current_opacity_raw, data, sizeof(unsigned int));
705 XFree(data);
706 current_opacity = (current_opacity_raw * 100.0) / 0xffffffff;
708 else
709 set_prop = 1;
711 delta = compute_new_value_from_arg(arg, current_opacity);
713 if(delta <= 0.0)
714 delta = 0.0;
715 else if(delta > 100.0)
717 delta = 100.0;
718 set_prop = 1;
721 if(delta == 100.0 && !set_prop)
722 window_settrans(sel->win, -1);
723 else
724 window_settrans(sel->win, delta);
727 static Client *
728 client_find_prev_visible(Client *sel)
730 Client *prev = NULL;
732 if(!sel) return NULL;
734 /* look for previous starting at sel */
735 for(prev = client_list_prev_cycle(&globalconf.clients, sel);
736 prev && (prev->skip || !client_isvisible(prev, sel->screen));
737 prev = client_list_prev_cycle(&globalconf.clients, prev));
739 return prev;
742 static Client *
743 client_find_next_visible(Client *sel)
745 Client *next = NULL;
747 if(!sel) return NULL;
749 for(next = sel->next; next && !client_isvisible(next, sel->screen); next = next->next);
750 if(!next)
751 for(next = globalconf.clients; next && !client_isvisible(next, sel->screen); next = next->next);
753 return next;
756 /** Swap current with previous client
757 * \param screen Screen ID
758 * \param arg nothing
759 * \ingroup ui_callback
761 void
762 uicb_client_swapprev(int screen __attribute__ ((unused)),
763 char *arg __attribute__ ((unused)))
765 Client *prev;
767 if((prev = client_find_prev_visible(globalconf.focus->client)))
769 client_list_swap(&globalconf.clients, prev, globalconf.focus->client);
770 globalconf.screens[prev->screen].need_arrange = True;
771 globalconf.drop_events |= EnterWindowMask;
775 /** Swap current with next client
776 * \param screen Screen ID
777 * \param arg nothing
778 * \ingroup ui_callback
780 void
781 uicb_client_swapnext(int screen __attribute__ ((unused)),
782 char *arg __attribute__ ((unused)))
784 Client *next;
786 if((next = client_find_next_visible(globalconf.focus->client)))
788 client_list_swap(&globalconf.clients, globalconf.focus->client, next);
789 globalconf.screens[next->screen].need_arrange = True;
790 globalconf.drop_events |= EnterWindowMask;
794 /** Move and resize client
795 * \param screen Screen ID
796 * \param arg x y w h
797 * \ingroup ui_callback
799 void
800 uicb_client_moveresize(int screen, char *arg)
802 int ox, oy, ow, oh;
803 char x[8], y[8], w[8], h[8];
804 int mx, my, dx, dy, nmx, nmy;
805 unsigned int dui;
806 Window dummy;
807 Area area;
808 Client *sel = globalconf.focus->client;
809 Layout *curlay = layout_get_current(screen);
811 if(curlay->arrange != layout_floating ||
812 !sel || !sel->isfloating || sel->isfixed || !arg)
813 return;
815 if(sscanf(arg, "%s %s %s %s", x, y, w, h) != 4)
816 return;
818 area.x = (int) compute_new_value_from_arg(x, sel->geometry.x);
819 area.y = (int) compute_new_value_from_arg(y, sel->geometry.y);
820 area.width = (int) compute_new_value_from_arg(w, sel->geometry.width);
821 area.height = (int) compute_new_value_from_arg(h, sel->geometry.height);
823 ox = sel->geometry.x;
824 oy = sel->geometry.y;
825 ow = sel->geometry.width;
826 oh = sel->geometry.height;
828 Bool xqp = XQueryPointer(globalconf.display,
829 RootWindow(globalconf.display,
830 get_phys_screen(screen)),
831 &dummy, &dummy, &mx, &my, &dx, &dy, &dui);
832 client_resize(sel, area, globalconf.screens[sel->screen].resize_hints);
833 if (xqp && ox <= mx && (ox + ow) >= mx && oy <= my && (oy + oh) >= my)
835 nmx = mx - ox + sel->geometry.width - ow - 1 < 0 ? 0 : mx - ox + sel->geometry.width - ow - 1;
836 nmy = my - oy + sel->geometry.height - oh - 1 < 0 ? 0 : my - oy + sel->geometry.height - oh - 1;
837 XWarpPointer(globalconf.display,
838 None, sel->win,
839 0, 0, 0, 0, nmx, nmy);
843 void
844 client_kill(Client *c)
846 XEvent ev;
848 if(client_isprotodel(globalconf.display, c->win))
850 ev.type = ClientMessage;
851 ev.xclient.window = c->win;
852 ev.xclient.message_type = XInternAtom(globalconf.display, "WM_PROTOCOLS", False);
853 ev.xclient.format = 32;
854 ev.xclient.data.l[0] = XInternAtom(globalconf.display, "WM_DELETE_WINDOW", False);
855 ev.xclient.data.l[1] = CurrentTime;
856 XSendEvent(globalconf.display, c->win, False, NoEventMask, &ev);
858 else
859 XKillClient(globalconf.display, c->win);
862 /** Kill selected client
863 * \param screen Screen ID
864 * \param arg unused
865 * \ingroup ui_callback
867 void
868 uicb_client_kill(int screen __attribute__ ((unused)), char *arg __attribute__ ((unused)))
870 Client *sel = globalconf.focus->client;
872 if(sel)
873 client_kill(sel);
876 static void
877 client_maximize(Client *c, Area geometry)
880 if((c->ismax = !c->ismax))
882 c->wasfloating = c->isfloating;
883 c->m_geometry = c->geometry;
884 if(layout_get_current(c->screen)->arrange != layout_floating)
885 client_setfloating(c, True);
886 client_focus(c, c->screen, True);
887 client_resize(c, geometry, False);
888 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
890 else if(c->wasfloating)
892 client_setfloating(c, True);
893 client_resize(c, c->m_geometry, False);
894 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
896 else if(layout_get_current(c->screen)->arrange == layout_floating)
898 client_resize(c, c->m_geometry, False);
899 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
901 else
903 client_setfloating(c, False);
904 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
908 /** Toggle maximize for client
909 * \param screen Screen ID
910 * \param arg Unused
911 * \ingroup ui_callback
913 void
914 uicb_client_togglemax(int screen, char *arg __attribute__ ((unused)))
916 Client *sel = globalconf.focus->client;
917 Area area = screen_get_area(screen,
918 globalconf.screens[screen].statusbar,
919 &globalconf.screens[screen].padding);
921 if(sel)
923 area.width -= 2 * sel->border;
924 area.height -= 2 * sel->border;
925 client_maximize(sel, area);
929 /** Toggle vertical maximize for client
930 * \param screen Screen ID
931 * \param arg Unused
932 * \ingroup ui_callback
934 void
935 uicb_client_toggleverticalmax(int screen, char *arg __attribute__ ((unused)))
937 Client *sel = globalconf.focus->client;
938 Area area = screen_get_area(screen,
939 globalconf.screens[screen].statusbar,
940 &globalconf.screens[screen].padding);
942 if(sel)
944 area.x = sel->geometry.x;
945 area.width = sel->geometry.width;
946 area.height -= 2 * sel->border;
947 client_maximize(sel, area);
952 /** Toggle horizontal maximize for client
953 * \param screen Screen ID
954 * \param arg Unused
955 * \ingroup ui_callback
957 void
958 uicb_client_togglehorizontalmax(int screen, char *arg __attribute__ ((unused)))
960 Client *sel = globalconf.focus->client;
961 Area area = screen_get_area(screen,
962 globalconf.screens[screen].statusbar,
963 &globalconf.screens[screen].padding);
965 if(sel)
967 area.y = sel->geometry.y;
968 area.height = sel->geometry.height;
969 area.width -= 2 * sel->border;
970 client_maximize(sel, area);
974 /** Zoom client
975 * \param screen Screen ID
976 * \param arg Unused
977 * \ingroup ui_callback
979 void
980 uicb_client_zoom(int screen, char *arg __attribute__ ((unused)))
982 Client *c, *sel = globalconf.focus->client;
984 if(!sel)
985 return;
987 for(c = globalconf.clients; !client_isvisible(c, screen); c = c->next);
988 if(c == sel)
989 for(sel = sel->next; sel && !client_isvisible(sel, screen); sel = sel->next);
991 if(sel)
993 client_list_detach(&globalconf.clients, sel);
994 client_list_push(&globalconf.clients, sel);
995 globalconf.screens[screen].need_arrange = True;
996 globalconf.drop_events |= EnterWindowMask;
1000 /** Send focus to next client in stack
1001 * \param screen Screen ID
1002 * \param arg Unused
1003 * \ingroup ui_callback
1005 void
1006 uicb_client_focusnext(int screen, char *arg __attribute__ ((unused)))
1008 Client *c, *sel = globalconf.focus->client;
1010 if(!sel)
1011 return;
1012 for(c = sel->next; c && (c->skip || !client_isvisible(c, screen)); c = c->next);
1013 if(!c)
1014 for(c = globalconf.clients; c && (c->skip || !client_isvisible(c, screen)); c = c->next);
1015 if(c)
1016 client_focus(c, screen, True);
1019 /** Send focus to previous client in stack
1020 * \param screen Screen ID
1021 * \param arg Unused
1022 * \ingroup ui_callback
1024 void
1025 uicb_client_focusprev(int screen, char *arg __attribute__ ((unused)))
1027 Client *prev;
1029 if((prev = client_find_prev_visible(globalconf.focus->client)))
1030 client_focus(prev, screen, True);
1033 /** Toggle floating state of a client
1034 * \param screen Screen ID
1035 * \param arg unused
1036 * \ingroup ui_callback
1038 void
1039 uicb_client_togglefloating(int screen __attribute__ ((unused)),
1040 char *arg __attribute__ ((unused)))
1042 if(globalconf.focus->client)
1043 client_setfloating(globalconf.focus->client, !globalconf.focus->client->isfloating);
1046 /** Toggle scratch client attribute
1047 * \param screen screen number
1048 * \param arg unused argument
1049 * \ingroup ui_callback
1051 void
1052 uicb_client_setscratch(int screen,
1053 char *arg __attribute__ ((unused)))
1055 if(!globalconf.focus->client)
1056 return;
1058 if(globalconf.scratch.client == globalconf.focus->client)
1059 globalconf.scratch.client = NULL;
1060 else
1061 globalconf.scratch.client = globalconf.focus->client;
1063 widget_invalidate_cache(screen, WIDGET_CACHE_CLIENTS | WIDGET_CACHE_TAGS);
1064 globalconf.screens[screen].need_arrange = True;
1067 /** Toggle scratch client visibility
1068 * \param screen screen number
1069 * \param arg unused argument
1070 * \ingroup ui_callback
1072 void
1073 uicb_client_togglescratch(int screen,
1074 char *arg __attribute__ ((unused)))
1076 if(globalconf.scratch.client)
1078 globalconf.scratch.isvisible = !globalconf.scratch.isvisible;
1079 if(globalconf.scratch.isvisible)
1080 client_focus(globalconf.scratch.client, screen, True);
1081 globalconf.screens[globalconf.scratch.client->screen].need_arrange = True;
1082 widget_invalidate_cache(globalconf.scratch.client->screen, WIDGET_CACHE_CLIENTS);
1085 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80