introduce fuzzy logic: fix problem with not_master
[awesome.git] / client.c
blob0e7fe9ca26bdf0c0f6354c0d9c3af3019110d514
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 "screen.h"
33 #include "widget.h"
34 #include "xutil.h"
35 #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 from_mouse)
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 /* save sel in focus history */
195 focus_add_client(c);
196 if(globalconf.screens[c->screen].opacity_unfocused != -1)
197 window_settrans(c->win, globalconf.screens[screen].opacity_unfocused);
198 XSetWindowBorder(globalconf.display, c->win,
199 globalconf.screens[screen].colors_selected[ColBorder].pixel);
200 XSetInputFocus(globalconf.display, c->win, RevertToPointerRoot, CurrentTime);
201 if(!from_mouse
202 || !globalconf.screens[screen].sloppy_focus
203 || globalconf.screens[screen].sloppy_focus_raise)
204 XRaiseWindow(globalconf.display, c->win);
205 /* since we're dropping EnterWindow events and sometimes the window
206 * will appear under the mouse, grabbuttons */
207 window_grabbuttons(phys_screen, c->win);
209 else
210 XSetInputFocus(globalconf.display,
211 RootWindow(globalconf.display, phys_screen),
212 RevertToPointerRoot, CurrentTime);
214 widget_invalidate_cache(screen, WIDGET_CACHE_CLIENTS);
215 ewmh_update_net_active_window(phys_screen);
216 globalconf.drop_events |= EnterWindowMask;
219 /** Compute smart coordinates for a client window
220 * \param geometry current/requested client geometry
221 * \param screen screen used
222 * \return new geometry
224 static Area
225 client_get_smart_geometry(Area geometry, int border, int screen)
227 Client *c;
228 Area newgeometry = { 0, 0, 0, 0, NULL };
229 Area *screen_geometry, *arealist = NULL, *r;
230 Bool found = False;
232 screen_geometry = p_new(Area, 1);
234 *screen_geometry = screen_get_area(screen,
235 globalconf.screens[screen].statusbar,
236 &globalconf.screens[screen].padding);
238 area_list_push(&arealist, screen_geometry);
240 for(c = globalconf.clients; c; c = c->next)
241 if(client_isvisible(c, screen))
243 newgeometry = c->f_geometry;
244 newgeometry.width += 2 * c->border;
245 newgeometry.height += 2 * c->border;
246 area_list_remove(&arealist, &newgeometry);
249 newgeometry.x = geometry.x;
250 newgeometry.y = geometry.y;
251 newgeometry.width = 0;
252 newgeometry.height = 0;
254 for(r = arealist; r; r = r->next)
255 if(r->width >= geometry.width && r->height >= geometry.height
256 && r->width * r->height > newgeometry.width * newgeometry.height)
258 found = True;
259 newgeometry = *r;
262 /* we did not found a space with enough space for our size:
263 * just take the biggest available and go in */
264 if(!found)
265 for(r = arealist; r; r = r->next)
266 if(r->width * r->height > newgeometry.width * newgeometry.height)
267 newgeometry = *r;
269 /* restore height and width */
270 newgeometry.width = geometry.width;
271 newgeometry.height = geometry.height;
273 /* fix offscreen */
274 if(AREA_RIGHT(newgeometry) > AREA_RIGHT(*screen_geometry))
275 newgeometry.x = screen_geometry->x + screen_geometry->width - newgeometry.width - 2 * border;
277 if(AREA_BOTTOM(newgeometry) > AREA_BOTTOM(*screen_geometry))
278 newgeometry.y = screen_geometry->y + screen_geometry->height - newgeometry.height - 2 * border;
280 area_list_wipe(&arealist);
282 return newgeometry;
285 /** Manage a new client
286 * \param w The window
287 * \param wa Window attributes
288 * \param screen Screen ID
290 void
291 client_manage(Window w, XWindowAttributes *wa, int screen)
293 Client *c, *t = NULL;
294 Window trans;
295 Bool rettrans;
296 XWindowChanges wc;
297 Tag *tag;
298 Rule *rule;
299 Area screen_geom;
300 int phys_screen = get_phys_screen(screen);
301 long flags;
303 c = p_new(Client, 1);
305 c->screen = screen_get_bycoord(wa->x, wa->y);
307 screen_geom = screen_get_area(c->screen,
308 globalconf.screens[screen].statusbar,
309 &globalconf.screens[screen].padding);
310 /* Initial values */
311 c->win = w;
312 c->geometry.x = c->f_geometry.x = c->m_geometry.x = MAX(wa->x, screen_geom.x);
313 c->geometry.y = c->f_geometry.y = c->m_geometry.y = MAX(wa->y, screen_geom.y);
314 c->geometry.width = c->f_geometry.width = c->m_geometry.width = wa->width;
315 c->geometry.height = c->f_geometry.height = c->m_geometry.height = wa->height;
316 c->oldborder = wa->border_width;
317 c->newcomer = True;
319 c->border = globalconf.screens[screen].borderpx;
321 /* Set windows borders */
322 wc.border_width = c->border;
323 XConfigureWindow(globalconf.display, w, CWBorderWidth, &wc);
324 XSetWindowBorder(globalconf.display, w, globalconf.screens[screen].colors_normal[ColBorder].pixel);
325 /* propagates border_width, if size doesn't change */
326 window_configure(c->win, c->geometry, c->border);
328 /* update window title */
329 client_updatetitle(c);
331 /* update hints */
332 flags = client_updatesizehints(c);
333 client_updatewmhints(c);
335 /* First check clients hints */
336 ewmh_check_client_hints(c);
338 /* loadprops or apply rules if no props */
339 if(!client_loadprops(c, screen))
341 /* Get the client's rule */
342 if((rule = rule_matching_client(c)))
344 if(rule->screen != RULE_NOSCREEN)
345 move_client_to_screen(c, rule->screen, True);
346 else
347 move_client_to_screen(c, screen, True);
348 tag_client_with_rule(c, rule);
350 switch(rule->isfloating)
352 case Auto:
353 break;
354 case Yes:
355 client_setfloating(c, True);
356 break;
357 case No:
358 client_setfloating(c, False);
359 break;
362 if(rule->opacity >= 0.0f)
363 window_settrans(c->win, rule->opacity);
365 else
366 move_client_to_screen(c, screen, True);
367 /* check for transient and set tags like its parent,
368 * XGetTransientForHint returns 1 on success
370 if((rettrans = XGetTransientForHint(globalconf.display, w, &trans))
371 && (t = client_get_bywin(globalconf.clients, trans)))
372 for(tag = globalconf.screens[c->screen].tags; tag; tag = tag->next)
373 if(is_client_tagged(t, tag))
374 tag_client(c, tag);
376 /* should be floating if transsient or fixed */
377 if(!c->isfloating)
378 client_setfloating(c, rettrans || c->isfixed);
381 if(!(flags & (USPosition | PPosition)))
382 c->f_geometry = client_get_smart_geometry(c->f_geometry, c->border, c->screen);
384 XSelectInput(globalconf.display, w, StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
386 /* handle xshape */
387 if(globalconf.have_shape)
389 XShapeSelectInput(globalconf.display, w, ShapeNotifyMask);
390 window_setshape(phys_screen, c->win);
393 /* attach to the stack */
394 if((rule = rule_matching_client(c)))
395 switch(rule->ismaster)
397 case Yes:
398 client_list_push(&globalconf.clients, c);
399 break;
400 case No:
401 client_list_append(&globalconf.clients, c);
402 break;
403 case Auto:
404 rule = NULL;
405 break;
408 if(!rule)
410 if(globalconf.screens[c->screen].new_become_master)
411 client_list_push(&globalconf.clients, c);
412 else
413 client_list_append(&globalconf.clients, c);
416 /* some windows require this */
417 XMoveResizeWindow(globalconf.display, c->win, c->geometry.x, c->geometry.y,
418 c->geometry.width, c->geometry.height);
420 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
421 ewmh_update_net_client_list(phys_screen);
424 /** Resize client window
425 * \param c client to resize
426 * \param geometry new window geometry
427 * \param sizehints respect size hints
428 * \param return True if resize has been done
430 Bool
431 client_resize(Client *c, Area geometry, Bool sizehints)
433 int new_screen;
434 double dx, dy, max, min, ratio;
435 Area area;
436 XWindowChanges wc;
438 if(sizehints)
440 if(c->minay > 0 && c->maxay > 0 && (geometry.height - c->baseh) > 0
441 && (geometry.width - c->basew) > 0)
443 dx = (double) (geometry.width - c->basew);
444 dy = (double) (geometry.height - c->baseh);
445 min = (double) (c->minax) / (double) (c->minay);
446 max = (double) (c->maxax) / (double) (c->maxay);
447 ratio = dx / dy;
448 if(max > 0 && min > 0 && ratio > 0)
450 if(ratio < min)
452 dy = (dx * min + dy) / (min * min + 1);
453 dx = dy * min;
454 geometry.width = (int) dx + c->basew;
455 geometry.height = (int) dy + c->baseh;
457 else if(ratio > max)
459 dy = (dx * min + dy) / (max * max + 1);
460 dx = dy * min;
461 geometry.width = (int) dx + c->basew;
462 geometry.height = (int) dy + c->baseh;
466 if(c->minw && geometry.width < c->minw)
467 geometry.width = c->minw;
468 if(c->minh && geometry.height < c->minh)
469 geometry.height = c->minh;
470 if(c->maxw && geometry.width > c->maxw)
471 geometry.width = c->maxw;
472 if(c->maxh && geometry.height > c->maxh)
473 geometry.height = c->maxh;
474 if(c->incw)
475 geometry.width -= (geometry.width - c->basew) % c->incw;
476 if(c->inch)
477 geometry.height -= (geometry.height - c->baseh) % c->inch;
479 if(geometry.width <= 0 || geometry.height <= 0)
480 return False;
481 /* offscreen appearance fixes */
482 area = get_display_area(get_phys_screen(c->screen),
483 NULL,
484 &globalconf.screens[c->screen].padding);
485 if(geometry.x > area.width)
486 geometry.x = area.width - geometry.width - 2 * c->border;
487 if(geometry.y > area.height)
488 geometry.y = area.height - geometry.height - 2 * c->border;
489 if(geometry.x + geometry.width + 2 * c->border < 0)
490 geometry.x = 0;
491 if(geometry.y + geometry.height + 2 * c->border < 0)
492 geometry.y = 0;
494 if(c->geometry.x != geometry.x || c->geometry.y != geometry.y
495 || c->geometry.width != geometry.width || c->geometry.height != geometry.height)
497 new_screen = screen_get_bycoord(geometry.x, geometry.y);
499 c->geometry.x = wc.x = geometry.x;
500 c->geometry.y = wc.y = geometry.y;
501 c->geometry.width = wc.width = geometry.width;
502 c->geometry.height = wc.height = geometry.height;
503 wc.border_width = c->border;
505 /* save the floating geometry if the window is floating but not
506 * maximized */
507 if((c->isfloating ||
508 get_current_layout(new_screen)->arrange == layout_floating) && !c->ismax)
509 c->f_geometry = geometry;
511 XConfigureWindow(globalconf.display, c->win,
512 CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
513 window_configure(c->win, geometry, c->border);
515 if(c->screen != new_screen)
516 move_client_to_screen(c, new_screen, False);
518 return True;
520 return False;
523 void
524 client_setfloating(Client *c, Bool floating)
526 if(c->isfloating != floating)
528 if((c->isfloating = floating))
529 client_resize(c, c->f_geometry, False);
530 else if(c->ismax)
532 c->ismax = False;
533 client_resize(c, c->m_geometry, False);
535 if(client_isvisible(c, c->screen))
536 globalconf.screens[c->screen].need_arrange = True;
537 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
538 client_saveprops(c);
542 /** Save client properties as an X property
543 * \param c client
545 void
546 client_saveprops(Client *c)
548 int i = 0, ntags = 0;
549 char *prop;
550 Tag *tag;
552 for(tag = globalconf.screens[c->screen].tags; tag; tag = tag->next)
553 ntags++;
555 prop = p_new(char, ntags + 2);
557 for(tag = globalconf.screens[c->screen].tags; tag; tag = tag->next, i++)
558 prop[i] = is_client_tagged(c, tag) ? '1' : '0';
560 if(i <= ntags)
561 prop[i] = c->isfloating ? '1' : '0';
563 prop[++i] = '\0';
565 XChangeProperty(globalconf.display, c->win,
566 XInternAtom(globalconf.display, "_AWESOME_PROPERTIES", False),
567 XA_STRING, 8, PropModeReplace, (unsigned char *) prop, i);
569 p_delete(&prop);
572 void
573 client_unban(Client *c)
575 XMapWindow(globalconf.display, c->win);
576 window_setstate(c->win, NormalState);
579 void
580 client_unmanage(Client *c)
582 XWindowChanges wc;
583 Tag *tag;
585 wc.border_width = c->oldborder;
587 /* The server grab construct avoids race conditions. */
588 XGrabServer(globalconf.display);
590 XConfigureWindow(globalconf.display, c->win, CWBorderWidth, &wc); /* restore border */
592 /* remove client everywhere */
593 client_list_detach(&globalconf.clients, c);
594 focus_delete_client(c);
595 for(tag = globalconf.screens[c->screen].tags; tag; tag = tag->next)
596 untag_client(c, tag);
598 if(globalconf.focus->client == c)
599 client_focus(NULL, c->screen, False);
601 XUngrabButton(globalconf.display, AnyButton, AnyModifier, c->win);
602 window_setstate(c->win, WithdrawnState);
604 XSync(globalconf.display, False);
605 XUngrabServer(globalconf.display);
607 p_delete(&c);
610 void
611 client_updatewmhints(Client *c)
613 XWMHints *wmh;
615 if((wmh = XGetWMHints(globalconf.display, c->win)))
617 if((c->isurgent = (wmh->flags & XUrgencyHint)))
618 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
619 if((wmh->flags & StateHint) && wmh->initial_state == WithdrawnState)
621 c->border = 0;
622 c->skip = True;
624 XFree(wmh);
628 long
629 client_updatesizehints(Client *c)
631 long msize;
632 XSizeHints size;
634 if(!XGetWMNormalHints(globalconf.display, c->win, &size, &msize))
635 return 0L;
637 if(size.flags & PBaseSize)
639 c->basew = size.base_width;
640 c->baseh = size.base_height;
642 else if(size.flags & PMinSize)
644 c->basew = size.min_width;
645 c->baseh = size.min_height;
647 else
648 c->basew = c->baseh = 0;
649 if(size.flags & PResizeInc)
651 c->incw = size.width_inc;
652 c->inch = size.height_inc;
654 else
655 c->incw = c->inch = 0;
657 if(size.flags & PMaxSize)
659 c->maxw = size.max_width;
660 c->maxh = size.max_height;
662 else
663 c->maxw = c->maxh = 0;
665 if(size.flags & PMinSize)
667 c->minw = size.min_width;
668 c->minh = size.min_height;
670 else if(size.flags & PBaseSize)
672 c->minw = size.base_width;
673 c->minh = size.base_height;
675 else
676 c->minw = c->minh = 0;
678 if(size.flags & PAspect)
680 c->minax = size.min_aspect.x;
681 c->maxax = size.max_aspect.x;
682 c->minay = size.min_aspect.y;
683 c->maxay = size.max_aspect.y;
685 else
686 c->minax = c->maxax = c->minay = c->maxay = 0;
688 if(c->maxw && c->minw && c->maxh && c->minh
689 && c->maxw == c->minw && c->maxh == c->minh)
690 c->isfixed = True;
692 return size.flags;
695 /** Returns True if a client is tagged
696 * with one of the tags
697 * \return True or False
699 Bool
700 client_isvisible(Client *c, int screen)
702 Tag *tag;
704 if(!c || c->screen != screen)
705 return False;
707 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
708 if(tag->selected && is_client_tagged(c, tag))
709 return True;
710 return False;
713 /** Set selected client transparency
714 * \param screen Screen ID
715 * \param arg unused arg
716 * \ingroup ui_callback
718 void
719 uicb_client_settrans(int screen __attribute__ ((unused)), char *arg)
721 double delta = 100.0, current_opacity = 100.0;
722 unsigned char *data;
723 Atom actual;
724 int format;
725 unsigned long n, left;
726 unsigned int current_opacity_raw = 0;
727 int set_prop = 0;
728 Client *sel = globalconf.focus->client;
730 if(!sel)
731 return;
733 XGetWindowProperty(globalconf.display, sel->win,
734 XInternAtom(globalconf.display, "_NET_WM_WINDOW_OPACITY", False),
735 0L, 1L, False, XA_CARDINAL, &actual, &format, &n, &left,
736 (unsigned char **) &data);
737 if(data)
739 memcpy(&current_opacity_raw, data, sizeof(unsigned int));
740 XFree(data);
741 current_opacity = (current_opacity_raw * 100.0) / 0xffffffff;
743 else
744 set_prop = 1;
746 delta = compute_new_value_from_arg(arg, current_opacity);
748 if(delta <= 0.0)
749 delta = 0.0;
750 else if(delta > 100.0)
752 delta = 100.0;
753 set_prop = 1;
756 if(delta == 100.0 && !set_prop)
757 window_settrans(sel->win, -1);
758 else
759 window_settrans(sel->win, delta);
762 static Client *
763 client_find_prev_visible(Client *sel)
765 Client *prev = NULL;
767 if(!sel) return NULL;
769 /* look for previous starting at sel */
770 for(prev = client_list_prev_cycle(&globalconf.clients, sel);
771 prev && (prev->skip || !client_isvisible(prev, sel->screen));
772 prev = client_list_prev_cycle(&globalconf.clients, prev));
774 return prev;
777 static Client *
778 client_find_next_visible(Client *sel)
780 Client *next = NULL;
782 if(!sel) return NULL;
784 for(next = sel->next; next && !client_isvisible(next, sel->screen); next = next->next);
785 if(!next)
786 for(next = globalconf.clients; next && !client_isvisible(next, sel->screen); next = next->next);
788 return next;
791 /** Swap current with previous client
792 * \param screen Screen ID
793 * \param arg nothing
794 * \ingroup ui_callback
796 void
797 uicb_client_swapprev(int screen __attribute__ ((unused)),
798 char *arg __attribute__ ((unused)))
800 Client *prev;
802 if((prev = client_find_prev_visible(globalconf.focus->client)))
804 client_list_swap(&globalconf.clients, prev, globalconf.focus->client);
805 globalconf.screens[prev->screen].need_arrange = True;
806 globalconf.drop_events |= EnterWindowMask;
810 /** Swap current with next client
811 * \param screen Screen ID
812 * \param arg nothing
813 * \ingroup ui_callback
815 void
816 uicb_client_swapnext(int screen __attribute__ ((unused)),
817 char *arg __attribute__ ((unused)))
819 Client *next;
821 if((next = client_find_next_visible(globalconf.focus->client)))
823 client_list_swap(&globalconf.clients, globalconf.focus->client, next);
824 globalconf.screens[next->screen].need_arrange = True;
825 globalconf.drop_events |= EnterWindowMask;
829 /** Move and resize client
830 * \param screen Screen ID
831 * \param arg x y w h
832 * \ingroup ui_callback
834 void
835 uicb_client_moveresize(int screen, char *arg)
837 int ox, oy, ow, oh;
838 char x[8], y[8], w[8], h[8];
839 int mx, my, dx, dy, nmx, nmy;
840 unsigned int dui;
841 Window dummy;
842 Area area;
843 Client *sel = globalconf.focus->client;
844 Tag **curtags = tags_get_current(screen);
846 if(curtags[0]->layout->arrange != layout_floating)
847 if(!sel || !sel->isfloating || sel->isfixed || !arg)
849 p_delete(&curtags);
850 return;
852 p_delete(&curtags);
853 if(sscanf(arg, "%s %s %s %s", x, y, w, h) != 4)
854 return;
855 area.x = (int) compute_new_value_from_arg(x, sel->geometry.x);
856 area.y = (int) compute_new_value_from_arg(y, sel->geometry.y);
857 area.width = (int) compute_new_value_from_arg(w, sel->geometry.width);
858 area.height = (int) compute_new_value_from_arg(h, sel->geometry.height);
860 ox = sel->geometry.x;
861 oy = sel->geometry.y;
862 ow = sel->geometry.width;
863 oh = sel->geometry.height;
865 Bool xqp = XQueryPointer(globalconf.display,
866 RootWindow(globalconf.display,
867 get_phys_screen(screen)),
868 &dummy, &dummy, &mx, &my, &dx, &dy, &dui);
869 client_resize(sel, area, globalconf.screens[sel->screen].resize_hints);
870 if (xqp && ox <= mx && (ox + ow) >= mx && oy <= my && (oy + oh) >= my)
872 nmx = mx - ox + sel->geometry.width - ow - 1 < 0 ? 0 : mx - ox + sel->geometry.width - ow - 1;
873 nmy = my - oy + sel->geometry.height - oh - 1 < 0 ? 0 : my - oy + sel->geometry.height - oh - 1;
874 XWarpPointer(globalconf.display,
875 None, sel->win,
876 0, 0, 0, 0, nmx, nmy);
880 void
881 client_kill(Client *c)
883 XEvent ev;
885 if(client_isprotodel(globalconf.display, c->win))
887 ev.type = ClientMessage;
888 ev.xclient.window = c->win;
889 ev.xclient.message_type = XInternAtom(globalconf.display, "WM_PROTOCOLS", False);
890 ev.xclient.format = 32;
891 ev.xclient.data.l[0] = XInternAtom(globalconf.display, "WM_DELETE_WINDOW", False);
892 ev.xclient.data.l[1] = CurrentTime;
893 XSendEvent(globalconf.display, c->win, False, NoEventMask, &ev);
895 else
896 XKillClient(globalconf.display, c->win);
899 /** Kill selected client
900 * \param screen Screen ID
901 * \param arg unused
902 * \ingroup ui_callback
904 void
905 uicb_client_kill(int screen __attribute__ ((unused)), char *arg __attribute__ ((unused)))
907 Client *sel = globalconf.focus->client;
909 if(sel)
910 client_kill(sel);
913 static void
914 client_maximize(Client *c, Area geometry)
917 if((c->ismax = !c->ismax))
919 c->wasfloating = c->isfloating;
920 c->m_geometry = c->geometry;
921 if(get_current_layout(c->screen)->arrange != layout_floating)
922 client_setfloating(c, True);
923 client_resize(c, geometry, False);
924 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
926 else if(c->wasfloating)
928 client_setfloating(c, True);
929 client_resize(c, c->m_geometry, False);
930 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
932 else if(get_current_layout(c->screen)->arrange == layout_floating)
934 client_resize(c, c->m_geometry, False);
935 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
937 else
939 client_setfloating(c, False);
940 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
944 /** Toggle maximize for client
945 * \param screen Screen ID
946 * \param arg Unused
947 * \ingroup ui_callback
949 void
950 uicb_client_togglemax(int screen, char *arg __attribute__ ((unused)))
952 Client *sel = globalconf.focus->client;
953 Area area = screen_get_area(screen,
954 globalconf.screens[screen].statusbar,
955 &globalconf.screens[screen].padding);
957 if(sel)
959 area.width -= 2 * sel->border;
960 area.height -= 2 * sel->border;
961 client_maximize(sel, area);
965 /** Toggle vertical maximize for client
966 * \param screen Screen ID
967 * \param arg Unused
968 * \ingroup ui_callback
970 void
971 uicb_client_toggleverticalmax(int screen, char *arg __attribute__ ((unused)))
973 Client *sel = globalconf.focus->client;
974 Area area = screen_get_area(screen,
975 globalconf.screens[screen].statusbar,
976 &globalconf.screens[screen].padding);
978 if(sel)
980 area.x = sel->geometry.x;
981 area.width = sel->geometry.width;
982 area.height -= 2 * sel->border;
983 client_maximize(sel, area);
988 /** Toggle horizontal maximize for client
989 * \param screen Screen ID
990 * \param arg Unused
991 * \ingroup ui_callback
993 void
994 uicb_client_togglehorizontalmax(int screen, char *arg __attribute__ ((unused)))
996 Client *sel = globalconf.focus->client;
997 Area area = screen_get_area(screen,
998 globalconf.screens[screen].statusbar,
999 &globalconf.screens[screen].padding);
1001 if(sel)
1003 area.y = sel->geometry.y;
1004 area.height = sel->geometry.height;
1005 area.width -= 2 * sel->border;
1006 client_maximize(sel, area);
1010 /** Zoom client
1011 * \param screen Screen ID
1012 * \param arg Unused
1013 * \ingroup ui_callback
1015 void
1016 uicb_client_zoom(int screen, char *arg __attribute__ ((unused)))
1018 Client *c, *sel = globalconf.focus->client;
1020 if(!sel)
1021 return;
1023 for(c = globalconf.clients; !client_isvisible(c, screen); c = c->next);
1024 if(c == sel)
1025 for(sel = sel->next; sel && !client_isvisible(sel, screen); sel = sel->next);
1027 if(sel)
1029 client_list_detach(&globalconf.clients, sel);
1030 client_list_push(&globalconf.clients, sel);
1031 globalconf.screens[screen].need_arrange = True;
1032 globalconf.drop_events |= EnterWindowMask;
1036 /** Send focus to next client in stack
1037 * \param screen Screen ID
1038 * \param arg Unused
1039 * \ingroup ui_callback
1041 void
1042 uicb_client_focusnext(int screen, char *arg __attribute__ ((unused)))
1044 Client *c, *sel = globalconf.focus->client;
1046 if(!sel)
1047 return;
1048 for(c = sel->next; c && (c->skip || !client_isvisible(c, screen)); c = c->next);
1049 if(!c)
1050 for(c = globalconf.clients; c && (c->skip || !client_isvisible(c, screen)); c = c->next);
1051 if(c)
1052 client_focus(c, screen, False);
1055 /** Send focus to previous client in stack
1056 * \param screen Screen ID
1057 * \param arg Unused
1058 * \ingroup ui_callback
1060 void
1061 uicb_client_focusprev(int screen, char *arg __attribute__ ((unused)))
1063 Client *prev;
1065 if((prev = client_find_prev_visible(globalconf.focus->client)))
1066 client_focus(prev, screen, False);
1069 /** Toggle floating state of a client
1070 * \param screen Screen ID
1071 * \param arg unused
1072 * \ingroup ui_callback
1074 void
1075 uicb_client_togglefloating(int screen __attribute__ ((unused)),
1076 char *arg __attribute__ ((unused)))
1078 if(globalconf.focus->client)
1079 client_setfloating(globalconf.focus->client, !globalconf.focus->client->isfloating);
1082 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80