Ship forgotten UPGRADE file
[awesome.git] / client.c
blob63d369d221a9670d72e9ae83995b3dadd088880c
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 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)
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, True);
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 if(globalconf.scratch.client == c)
708 return globalconf.scratch.isvisible;
710 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
711 if(tag->selected && is_client_tagged(c, tag))
712 return True;
713 return False;
716 /** Set selected client transparency
717 * \param screen Screen ID
718 * \param arg unused arg
719 * \ingroup ui_callback
721 void
722 uicb_client_settrans(int screen __attribute__ ((unused)), char *arg)
724 double delta = 100.0, current_opacity = 100.0;
725 unsigned char *data;
726 Atom actual;
727 int format;
728 unsigned long n, left;
729 unsigned int current_opacity_raw = 0;
730 int set_prop = 0;
731 Client *sel = globalconf.focus->client;
733 if(!sel)
734 return;
736 XGetWindowProperty(globalconf.display, sel->win,
737 XInternAtom(globalconf.display, "_NET_WM_WINDOW_OPACITY", False),
738 0L, 1L, False, XA_CARDINAL, &actual, &format, &n, &left,
739 (unsigned char **) &data);
740 if(data)
742 memcpy(&current_opacity_raw, data, sizeof(unsigned int));
743 XFree(data);
744 current_opacity = (current_opacity_raw * 100.0) / 0xffffffff;
746 else
747 set_prop = 1;
749 delta = compute_new_value_from_arg(arg, current_opacity);
751 if(delta <= 0.0)
752 delta = 0.0;
753 else if(delta > 100.0)
755 delta = 100.0;
756 set_prop = 1;
759 if(delta == 100.0 && !set_prop)
760 window_settrans(sel->win, -1);
761 else
762 window_settrans(sel->win, delta);
765 static Client *
766 client_find_prev_visible(Client *sel)
768 Client *prev = NULL;
770 if(!sel) return NULL;
772 /* look for previous starting at sel */
773 for(prev = client_list_prev_cycle(&globalconf.clients, sel);
774 prev && (prev->skip || !client_isvisible(prev, sel->screen));
775 prev = client_list_prev_cycle(&globalconf.clients, prev));
777 return prev;
780 static Client *
781 client_find_next_visible(Client *sel)
783 Client *next = NULL;
785 if(!sel) return NULL;
787 for(next = sel->next; next && !client_isvisible(next, sel->screen); next = next->next);
788 if(!next)
789 for(next = globalconf.clients; next && !client_isvisible(next, sel->screen); next = next->next);
791 return next;
794 /** Swap current with previous client
795 * \param screen Screen ID
796 * \param arg nothing
797 * \ingroup ui_callback
799 void
800 uicb_client_swapprev(int screen __attribute__ ((unused)),
801 char *arg __attribute__ ((unused)))
803 Client *prev;
805 if((prev = client_find_prev_visible(globalconf.focus->client)))
807 client_list_swap(&globalconf.clients, prev, globalconf.focus->client);
808 globalconf.screens[prev->screen].need_arrange = True;
809 globalconf.drop_events |= EnterWindowMask;
813 /** Swap current with next client
814 * \param screen Screen ID
815 * \param arg nothing
816 * \ingroup ui_callback
818 void
819 uicb_client_swapnext(int screen __attribute__ ((unused)),
820 char *arg __attribute__ ((unused)))
822 Client *next;
824 if((next = client_find_next_visible(globalconf.focus->client)))
826 client_list_swap(&globalconf.clients, globalconf.focus->client, next);
827 globalconf.screens[next->screen].need_arrange = True;
828 globalconf.drop_events |= EnterWindowMask;
832 /** Move and resize client
833 * \param screen Screen ID
834 * \param arg x y w h
835 * \ingroup ui_callback
837 void
838 uicb_client_moveresize(int screen, char *arg)
840 int ox, oy, ow, oh;
841 char x[8], y[8], w[8], h[8];
842 int mx, my, dx, dy, nmx, nmy;
843 unsigned int dui;
844 Window dummy;
845 Area area;
846 Client *sel = globalconf.focus->client;
847 Tag **curtags = tags_get_current(screen);
849 if(curtags[0]->layout->arrange != layout_floating)
850 if(!sel || !sel->isfloating || sel->isfixed || !arg)
852 p_delete(&curtags);
853 return;
855 p_delete(&curtags);
856 if(sscanf(arg, "%s %s %s %s", x, y, w, h) != 4)
857 return;
858 area.x = (int) compute_new_value_from_arg(x, sel->geometry.x);
859 area.y = (int) compute_new_value_from_arg(y, sel->geometry.y);
860 area.width = (int) compute_new_value_from_arg(w, sel->geometry.width);
861 area.height = (int) compute_new_value_from_arg(h, sel->geometry.height);
863 ox = sel->geometry.x;
864 oy = sel->geometry.y;
865 ow = sel->geometry.width;
866 oh = sel->geometry.height;
868 Bool xqp = XQueryPointer(globalconf.display,
869 RootWindow(globalconf.display,
870 get_phys_screen(screen)),
871 &dummy, &dummy, &mx, &my, &dx, &dy, &dui);
872 client_resize(sel, area, globalconf.screens[sel->screen].resize_hints);
873 if (xqp && ox <= mx && (ox + ow) >= mx && oy <= my && (oy + oh) >= my)
875 nmx = mx - ox + sel->geometry.width - ow - 1 < 0 ? 0 : mx - ox + sel->geometry.width - ow - 1;
876 nmy = my - oy + sel->geometry.height - oh - 1 < 0 ? 0 : my - oy + sel->geometry.height - oh - 1;
877 XWarpPointer(globalconf.display,
878 None, sel->win,
879 0, 0, 0, 0, nmx, nmy);
883 void
884 client_kill(Client *c)
886 XEvent ev;
888 if(client_isprotodel(globalconf.display, c->win))
890 ev.type = ClientMessage;
891 ev.xclient.window = c->win;
892 ev.xclient.message_type = XInternAtom(globalconf.display, "WM_PROTOCOLS", False);
893 ev.xclient.format = 32;
894 ev.xclient.data.l[0] = XInternAtom(globalconf.display, "WM_DELETE_WINDOW", False);
895 ev.xclient.data.l[1] = CurrentTime;
896 XSendEvent(globalconf.display, c->win, False, NoEventMask, &ev);
898 else
899 XKillClient(globalconf.display, c->win);
902 /** Kill selected client
903 * \param screen Screen ID
904 * \param arg unused
905 * \ingroup ui_callback
907 void
908 uicb_client_kill(int screen __attribute__ ((unused)), char *arg __attribute__ ((unused)))
910 Client *sel = globalconf.focus->client;
912 if(sel)
913 client_kill(sel);
916 static void
917 client_maximize(Client *c, Area geometry)
920 if((c->ismax = !c->ismax))
922 c->wasfloating = c->isfloating;
923 c->m_geometry = c->geometry;
924 if(get_current_layout(c->screen)->arrange != layout_floating)
925 client_setfloating(c, True);
926 client_focus(c, c->screen, True);
927 client_resize(c, geometry, False);
928 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
930 else if(c->wasfloating)
932 client_setfloating(c, True);
933 client_resize(c, c->m_geometry, False);
934 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
936 else if(get_current_layout(c->screen)->arrange == layout_floating)
938 client_resize(c, c->m_geometry, False);
939 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
941 else
943 client_setfloating(c, False);
944 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
948 /** Toggle maximize for client
949 * \param screen Screen ID
950 * \param arg Unused
951 * \ingroup ui_callback
953 void
954 uicb_client_togglemax(int screen, char *arg __attribute__ ((unused)))
956 Client *sel = globalconf.focus->client;
957 Area area = screen_get_area(screen,
958 globalconf.screens[screen].statusbar,
959 &globalconf.screens[screen].padding);
961 if(sel)
963 area.width -= 2 * sel->border;
964 area.height -= 2 * sel->border;
965 client_maximize(sel, area);
969 /** Toggle vertical maximize for client
970 * \param screen Screen ID
971 * \param arg Unused
972 * \ingroup ui_callback
974 void
975 uicb_client_toggleverticalmax(int screen, char *arg __attribute__ ((unused)))
977 Client *sel = globalconf.focus->client;
978 Area area = screen_get_area(screen,
979 globalconf.screens[screen].statusbar,
980 &globalconf.screens[screen].padding);
982 if(sel)
984 area.x = sel->geometry.x;
985 area.width = sel->geometry.width;
986 area.height -= 2 * sel->border;
987 client_maximize(sel, area);
992 /** Toggle horizontal maximize for client
993 * \param screen Screen ID
994 * \param arg Unused
995 * \ingroup ui_callback
997 void
998 uicb_client_togglehorizontalmax(int screen, char *arg __attribute__ ((unused)))
1000 Client *sel = globalconf.focus->client;
1001 Area area = screen_get_area(screen,
1002 globalconf.screens[screen].statusbar,
1003 &globalconf.screens[screen].padding);
1005 if(sel)
1007 area.y = sel->geometry.y;
1008 area.height = sel->geometry.height;
1009 area.width -= 2 * sel->border;
1010 client_maximize(sel, area);
1014 /** Zoom client
1015 * \param screen Screen ID
1016 * \param arg Unused
1017 * \ingroup ui_callback
1019 void
1020 uicb_client_zoom(int screen, char *arg __attribute__ ((unused)))
1022 Client *c, *sel = globalconf.focus->client;
1024 if(!sel)
1025 return;
1027 for(c = globalconf.clients; !client_isvisible(c, screen); c = c->next);
1028 if(c == sel)
1029 for(sel = sel->next; sel && !client_isvisible(sel, screen); sel = sel->next);
1031 if(sel)
1033 client_list_detach(&globalconf.clients, sel);
1034 client_list_push(&globalconf.clients, sel);
1035 globalconf.screens[screen].need_arrange = True;
1036 globalconf.drop_events |= EnterWindowMask;
1040 /** Send focus to next client in stack
1041 * \param screen Screen ID
1042 * \param arg Unused
1043 * \ingroup ui_callback
1045 void
1046 uicb_client_focusnext(int screen, char *arg __attribute__ ((unused)))
1048 Client *c, *sel = globalconf.focus->client;
1050 if(!sel)
1051 return;
1052 for(c = sel->next; c && (c->skip || !client_isvisible(c, screen)); c = c->next);
1053 if(!c)
1054 for(c = globalconf.clients; c && (c->skip || !client_isvisible(c, screen)); c = c->next);
1055 if(c)
1056 client_focus(c, screen, True);
1059 /** Send focus to previous client in stack
1060 * \param screen Screen ID
1061 * \param arg Unused
1062 * \ingroup ui_callback
1064 void
1065 uicb_client_focusprev(int screen, char *arg __attribute__ ((unused)))
1067 Client *prev;
1069 if((prev = client_find_prev_visible(globalconf.focus->client)))
1070 client_focus(prev, screen, True);
1073 /** Toggle floating state of a client
1074 * \param screen Screen ID
1075 * \param arg unused
1076 * \ingroup ui_callback
1078 void
1079 uicb_client_togglefloating(int screen __attribute__ ((unused)),
1080 char *arg __attribute__ ((unused)))
1082 if(globalconf.focus->client)
1083 client_setfloating(globalconf.focus->client, !globalconf.focus->client->isfloating);
1086 /** Toggle scratch client attribute
1087 * \param screen screen number
1088 * \param arg unused argument
1089 * \ingroup ui_callback
1091 void
1092 uicb_client_setscratch(int screen,
1093 char *arg __attribute__ ((unused)))
1095 if(!globalconf.focus->client)
1096 return;
1098 if(globalconf.scratch.client == globalconf.focus->client)
1099 globalconf.scratch.client = NULL;
1100 else
1101 globalconf.scratch.client = globalconf.focus->client;
1103 widget_invalidate_cache(screen, WIDGET_CACHE_CLIENTS | WIDGET_CACHE_TAGS);
1104 globalconf.screens[screen].need_arrange = True;
1107 /** Toggle scratch client visibility
1108 * \param screen screen number
1109 * \param arg unused argument
1110 * \ingroup ui_callback
1112 void
1113 uicb_client_togglescratch(int screen,
1114 char *arg __attribute__ ((unused)))
1116 if(globalconf.scratch.client)
1118 globalconf.scratch.isvisible = !globalconf.scratch.isvisible;
1119 if(globalconf.scratch.isvisible)
1120 client_focus(globalconf.scratch.client, screen, True);
1121 globalconf.screens[globalconf.scratch.client->screen].need_arrange = True;
1122 widget_invalidate_cache(globalconf.scratch.client->screen, WIDGET_CACHE_CLIENTS);
1125 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80