simply ignore XSetInputFocus errors
[awesome.git] / client.c
blob5e1ada7b373aee1e5d56514e05f24e973381ecab
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 XSetWindowBorder(globalconf.display, c->win,
151 globalconf.screens[c->screen].colors_normal[ColBorder].pixel);
152 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
153 focus_add_client(NULL);
156 /** Ban client and unmap it
157 * \param c the client
159 void
160 client_ban(Client *c)
162 if(globalconf.focus->client == c)
163 client_unfocus(c);
164 XUnmapWindow(globalconf.display, c->win);
165 window_setstate(c->win, IconicState);
168 /** Give focus to client, or to first client if c is NULL
169 * \param c client
170 * \param screen Screen ID
172 void
173 client_focus(Client *c, int screen, Bool from_mouse)
175 int phys_screen = get_phys_screen(screen);
177 /* if c is NULL or invisible, take next client in the focus history */
178 if(!c || (c && !client_isvisible(c, screen)))
180 c = focus_get_current_client(screen);
181 /* if c is still NULL take next client in the stack */
182 if(!c)
183 for(c = globalconf.clients; c && (c->skip || !client_isvisible(c, screen)); c = c->next);
186 /* unfocus current selected client */
187 if(globalconf.focus->client)
188 client_unfocus(globalconf.focus->client);
190 if(c)
192 /* save sel in focus history */
193 focus_add_client(c);
194 XSetWindowBorder(globalconf.display, c->win,
195 globalconf.screens[screen].colors_selected[ColBorder].pixel);
196 XSetInputFocus(globalconf.display, c->win, RevertToPointerRoot, CurrentTime);
197 if(!from_mouse
198 || !globalconf.screens[screen].sloppy_focus
199 || globalconf.screens[screen].sloppy_focus_raise)
200 XRaiseWindow(globalconf.display, c->win);
201 /* since we're dropping EnterWindow events and sometimes the window
202 * will appear under the mouse, grabbuttons */
203 window_grabbuttons(phys_screen, c->win);
205 else
206 XSetInputFocus(globalconf.display,
207 RootWindow(globalconf.display, phys_screen),
208 RevertToPointerRoot, CurrentTime);
210 widget_invalidate_cache(screen, WIDGET_CACHE_CLIENTS);
211 ewmh_update_net_active_window(phys_screen);
212 globalconf.drop_events |= EnterWindowMask;
215 /** Compute smart coordinates for a client window
216 * \param geometry current/requested client geometry
217 * \param screen screen used
218 * \return new geometry
220 static Area
221 client_get_smart_geometry(Area geometry, int border, int screen)
223 Client *c;
224 Area newgeometry = { 0, 0, 0, 0, NULL };
225 Area *screen_geometry, *arealist = NULL, *r;
226 Bool found = False;
228 screen_geometry = p_new(Area, 1);
230 *screen_geometry = screen_get_area(screen,
231 globalconf.screens[screen].statusbar,
232 &globalconf.screens[screen].padding);
234 area_list_push(&arealist, screen_geometry);
236 for(c = globalconf.clients; c; c = c->next)
237 if(client_isvisible(c, screen))
239 newgeometry = c->f_geometry;
240 newgeometry.width += 2 * c->border;
241 newgeometry.height += 2 * c->border;
242 area_list_remove(&arealist, &newgeometry);
245 newgeometry.x = geometry.x;
246 newgeometry.y = geometry.y;
247 newgeometry.width = 0;
248 newgeometry.height = 0;
250 for(r = arealist; r; r = r->next)
251 if(r->width >= geometry.width && r->height >= geometry.height
252 && r->width * r->height > newgeometry.width * newgeometry.height)
254 found = True;
255 newgeometry = *r;
258 /* we did not found a space with enough space for our size:
259 * just take the biggest available and go in */
260 if(!found)
261 for(r = arealist; r; r = r->next)
262 if(r->width * r->height > newgeometry.width * newgeometry.height)
263 newgeometry = *r;
265 /* restore height and width */
266 newgeometry.width = geometry.width;
267 newgeometry.height = geometry.height;
269 /* fix offscreen */
270 if(AREA_RIGHT(newgeometry) > AREA_RIGHT(*screen_geometry))
271 newgeometry.x = screen_geometry->x + screen_geometry->width - newgeometry.width - 2 * border;
273 if(AREA_BOTTOM(newgeometry) > AREA_BOTTOM(*screen_geometry))
274 newgeometry.y = screen_geometry->y + screen_geometry->height - newgeometry.height - 2 * border;
276 area_list_wipe(&arealist);
278 return newgeometry;
281 /** Manage a new client
282 * \param w The window
283 * \param wa Window attributes
284 * \param screen Screen ID
286 void
287 client_manage(Window w, XWindowAttributes *wa, int screen)
289 Client *c, *t = NULL;
290 Window trans;
291 Bool rettrans;
292 XWindowChanges wc;
293 Tag *tag;
294 Rule *rule;
295 Area screen_geom;
296 int phys_screen = get_phys_screen(screen);
297 long flags;
299 c = p_new(Client, 1);
301 c->screen = screen_get_bycoord(wa->x, wa->y);
303 screen_geom = screen_get_area(c->screen,
304 globalconf.screens[screen].statusbar,
305 &globalconf.screens[screen].padding);
306 /* Initial values */
307 c->win = w;
308 c->geometry.x = c->f_geometry.x = c->m_geometry.x = MAX(wa->x, screen_geom.x);
309 c->geometry.y = c->f_geometry.y = c->m_geometry.y = MAX(wa->y, screen_geom.y);
310 c->geometry.width = c->f_geometry.width = c->m_geometry.width = wa->width;
311 c->geometry.height = c->f_geometry.height = c->m_geometry.height = wa->height;
312 c->oldborder = wa->border_width;
313 c->newcomer = True;
315 c->border = globalconf.screens[screen].borderpx;
317 /* Set windows borders */
318 wc.border_width = c->border;
319 XConfigureWindow(globalconf.display, w, CWBorderWidth, &wc);
320 XSetWindowBorder(globalconf.display, w, globalconf.screens[screen].colors_normal[ColBorder].pixel);
321 /* propagates border_width, if size doesn't change */
322 window_configure(c->win, c->geometry, c->border);
324 /* update window title */
325 client_updatetitle(c);
327 /* update hints */
328 flags = client_updatesizehints(c);
329 client_updatewmhints(c);
331 /* First check clients hints */
332 ewmh_check_client_hints(c);
334 /* loadprops or apply rules if no props */
335 if(!client_loadprops(c, screen))
337 /* Get the client's rule */
338 if((rule = rule_matching_client(c)))
340 if(rule->screen != RULE_NOSCREEN)
341 move_client_to_screen(c, rule->screen, True);
342 else
343 move_client_to_screen(c, screen, True);
344 tag_client_with_rule(c, rule);
346 switch(rule->isfloating)
348 case Auto:
349 break;
350 case Float:
351 client_setfloating(c, True);
352 break;
353 case Tile:
354 client_setfloating(c, False);
355 break;
358 if(rule->opacity >= 0.0f)
359 window_settrans(c->win, rule->opacity);
361 else
362 move_client_to_screen(c, screen, True);
363 /* check for transient and set tags like its parent,
364 * XGetTransientForHint returns 1 on success
366 if((rettrans = XGetTransientForHint(globalconf.display, w, &trans))
367 && (t = client_get_bywin(globalconf.clients, trans)))
368 for(tag = globalconf.screens[c->screen].tags; tag; tag = tag->next)
369 if(is_client_tagged(t, tag))
370 tag_client(c, tag);
372 /* should be floating if transsient or fixed */
373 if(!c->isfloating)
374 client_setfloating(c, rettrans || c->isfixed);
377 if(!(flags & (USPosition | PPosition)))
378 c->f_geometry = client_get_smart_geometry(c->f_geometry, c->border, c->screen);
380 XSelectInput(globalconf.display, w, StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
382 /* handle xshape */
383 if(globalconf.have_shape)
385 XShapeSelectInput(globalconf.display, w, ShapeNotifyMask);
386 window_setshape(phys_screen, c->win);
389 /* attach to the stack */
390 if((rule = rule_matching_client(c)) && rule->not_master)
391 client_list_append(&globalconf.clients, c);
392 else if(globalconf.screens[c->screen].new_become_master)
393 client_list_push(&globalconf.clients, c);
394 else
395 client_list_append(&globalconf.clients, c);
397 /* some windows require this */
398 XMoveResizeWindow(globalconf.display, c->win, c->geometry.x, c->geometry.y,
399 c->geometry.width, c->geometry.height);
401 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
402 ewmh_update_net_client_list(phys_screen);
405 /** Resize client window
406 * \param c client to resize
407 * \param geometry new window geometry
408 * \param sizehints respect size hints
409 * \param return True if resize has been done
411 Bool
412 client_resize(Client *c, Area geometry, Bool sizehints)
414 int new_screen;
415 double dx, dy, max, min, ratio;
416 Area area;
417 XWindowChanges wc;
419 if(sizehints)
421 if(c->minay > 0 && c->maxay > 0 && (geometry.height - c->baseh) > 0
422 && (geometry.width - c->basew) > 0)
424 dx = (double) (geometry.width - c->basew);
425 dy = (double) (geometry.height - c->baseh);
426 min = (double) (c->minax) / (double) (c->minay);
427 max = (double) (c->maxax) / (double) (c->maxay);
428 ratio = dx / dy;
429 if(max > 0 && min > 0 && ratio > 0)
431 if(ratio < min)
433 dy = (dx * min + dy) / (min * min + 1);
434 dx = dy * min;
435 geometry.width = (int) dx + c->basew;
436 geometry.height = (int) dy + c->baseh;
438 else if(ratio > max)
440 dy = (dx * min + dy) / (max * max + 1);
441 dx = dy * min;
442 geometry.width = (int) dx + c->basew;
443 geometry.height = (int) dy + c->baseh;
447 if(c->minw && geometry.width < c->minw)
448 geometry.width = c->minw;
449 if(c->minh && geometry.height < c->minh)
450 geometry.height = c->minh;
451 if(c->maxw && geometry.width > c->maxw)
452 geometry.width = c->maxw;
453 if(c->maxh && geometry.height > c->maxh)
454 geometry.height = c->maxh;
455 if(c->incw)
456 geometry.width -= (geometry.width - c->basew) % c->incw;
457 if(c->inch)
458 geometry.height -= (geometry.height - c->baseh) % c->inch;
460 if(geometry.width <= 0 || geometry.height <= 0)
461 return False;
462 /* offscreen appearance fixes */
463 area = get_display_area(get_phys_screen(c->screen),
464 NULL,
465 &globalconf.screens[c->screen].padding);
466 if(geometry.x > area.width)
467 geometry.x = area.width - geometry.width - 2 * c->border;
468 if(geometry.y > area.height)
469 geometry.y = area.height - geometry.height - 2 * c->border;
470 if(geometry.x + geometry.width + 2 * c->border < 0)
471 geometry.x = 0;
472 if(geometry.y + geometry.height + 2 * c->border < 0)
473 geometry.y = 0;
475 if(c->geometry.x != geometry.x || c->geometry.y != geometry.y
476 || c->geometry.width != geometry.width || c->geometry.height != geometry.height)
478 new_screen = screen_get_bycoord(geometry.x, geometry.y);
480 c->geometry.x = wc.x = geometry.x;
481 c->geometry.y = wc.y = geometry.y;
482 c->geometry.width = wc.width = geometry.width;
483 c->geometry.height = wc.height = geometry.height;
484 wc.border_width = c->border;
486 /* save the floating geometry if the window is floating but not
487 * maximized */
488 if((c->isfloating ||
489 get_current_layout(new_screen)->arrange == layout_floating) && !c->ismax)
490 c->f_geometry = geometry;
492 XConfigureWindow(globalconf.display, c->win,
493 CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
494 window_configure(c->win, geometry, c->border);
496 if(c->screen != new_screen)
497 move_client_to_screen(c, new_screen, False);
499 return True;
501 return False;
504 void
505 client_setfloating(Client *c, Bool floating)
507 if(c->isfloating != floating)
509 if((c->isfloating = floating))
510 client_resize(c, c->f_geometry, False);
511 else if(c->ismax)
513 c->ismax = False;
514 client_resize(c, c->m_geometry, False);
516 if(client_isvisible(c, c->screen))
517 globalconf.screens[c->screen].need_arrange = True;
518 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
519 client_saveprops(c);
523 /** Save client properties as an X property
524 * \param c client
526 void
527 client_saveprops(Client *c)
529 int i = 0, ntags = 0;
530 char *prop;
531 Tag *tag;
533 for(tag = globalconf.screens[c->screen].tags; tag; tag = tag->next)
534 ntags++;
536 prop = p_new(char, ntags + 2);
538 for(tag = globalconf.screens[c->screen].tags; tag; tag = tag->next, i++)
539 prop[i] = is_client_tagged(c, tag) ? '1' : '0';
541 if(i <= ntags)
542 prop[i] = c->isfloating ? '1' : '0';
544 prop[++i] = '\0';
546 XChangeProperty(globalconf.display, c->win,
547 XInternAtom(globalconf.display, "_AWESOME_PROPERTIES", False),
548 XA_STRING, 8, PropModeReplace, (unsigned char *) prop, i);
550 p_delete(&prop);
553 void
554 client_unban(Client *c)
556 XMapWindow(globalconf.display, c->win);
557 window_setstate(c->win, NormalState);
560 void
561 client_unmanage(Client *c)
563 XWindowChanges wc;
564 Tag *tag;
566 wc.border_width = c->oldborder;
568 /* The server grab construct avoids race conditions. */
569 XGrabServer(globalconf.display);
571 XConfigureWindow(globalconf.display, c->win, CWBorderWidth, &wc); /* restore border */
573 /* remove client everywhere */
574 client_list_detach(&globalconf.clients, c);
575 focus_delete_client(c);
576 for(tag = globalconf.screens[c->screen].tags; tag; tag = tag->next)
577 untag_client(c, tag);
579 if(globalconf.focus->client == c)
580 client_focus(NULL, c->screen, False);
582 XUngrabButton(globalconf.display, AnyButton, AnyModifier, c->win);
583 window_setstate(c->win, WithdrawnState);
585 XSync(globalconf.display, False);
586 XUngrabServer(globalconf.display);
588 p_delete(&c);
591 void
592 client_updatewmhints(Client *c)
594 XWMHints *wmh;
596 if((wmh = XGetWMHints(globalconf.display, c->win)))
598 if((c->isurgent = (wmh->flags & XUrgencyHint)))
599 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
600 if((wmh->flags & StateHint) && wmh->initial_state == WithdrawnState)
602 c->border = 0;
603 c->skip = True;
605 XFree(wmh);
609 long
610 client_updatesizehints(Client *c)
612 long msize;
613 XSizeHints size;
615 if(!XGetWMNormalHints(globalconf.display, c->win, &size, &msize))
616 return 0L;
618 if(size.flags & PBaseSize)
620 c->basew = size.base_width;
621 c->baseh = size.base_height;
623 else if(size.flags & PMinSize)
625 c->basew = size.min_width;
626 c->baseh = size.min_height;
628 else
629 c->basew = c->baseh = 0;
630 if(size.flags & PResizeInc)
632 c->incw = size.width_inc;
633 c->inch = size.height_inc;
635 else
636 c->incw = c->inch = 0;
638 if(size.flags & PMaxSize)
640 c->maxw = size.max_width;
641 c->maxh = size.max_height;
643 else
644 c->maxw = c->maxh = 0;
646 if(size.flags & PMinSize)
648 c->minw = size.min_width;
649 c->minh = size.min_height;
651 else if(size.flags & PBaseSize)
653 c->minw = size.base_width;
654 c->minh = size.base_height;
656 else
657 c->minw = c->minh = 0;
659 if(size.flags & PAspect)
661 c->minax = size.min_aspect.x;
662 c->maxax = size.max_aspect.x;
663 c->minay = size.min_aspect.y;
664 c->maxay = size.max_aspect.y;
666 else
667 c->minax = c->maxax = c->minay = c->maxay = 0;
669 if(c->maxw && c->minw && c->maxh && c->minh
670 && c->maxw == c->minw && c->maxh == c->minh)
671 c->isfixed = True;
673 return size.flags;
676 /** Returns True if a client is tagged
677 * with one of the tags
678 * \return True or False
680 Bool
681 client_isvisible(Client *c, int screen)
683 Tag *tag;
685 if(!c || c->screen != screen)
686 return False;
688 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
689 if(tag->selected && is_client_tagged(c, tag))
690 return True;
691 return False;
694 /** Set selected client transparency
695 * \param screen Screen ID
696 * \param arg unused arg
697 * \ingroup ui_callback
699 void
700 uicb_client_settrans(int screen __attribute__ ((unused)), char *arg)
702 double delta = 100.0, current_opacity = 100.0;
703 unsigned char *data;
704 Atom actual;
705 int format;
706 unsigned long n, left;
707 unsigned int current_opacity_raw = 0;
708 int set_prop = 0;
709 Client *sel = globalconf.focus->client;
711 if(!sel)
712 return;
714 XGetWindowProperty(globalconf.display, sel->win,
715 XInternAtom(globalconf.display, "_NET_WM_WINDOW_OPACITY", False),
716 0L, 1L, False, XA_CARDINAL, &actual, &format, &n, &left,
717 (unsigned char **) &data);
718 if(data)
720 memcpy(&current_opacity_raw, data, sizeof(unsigned int));
721 XFree(data);
722 current_opacity = (current_opacity_raw * 100.0) / 0xffffffff;
724 else
725 set_prop = 1;
727 delta = compute_new_value_from_arg(arg, current_opacity);
729 if(delta <= 0.0)
730 delta = 0.0;
731 else if(delta > 100.0)
733 delta = 100.0;
734 set_prop = 1;
737 if(delta == 100.0 && !set_prop)
738 window_settrans(sel->win, -1);
739 else
740 window_settrans(sel->win, delta);
743 static Client *
744 client_find_prev_visible(Client *sel)
746 Client *prev = NULL;
748 if(!sel) return NULL;
750 /* look for previous starting at sel */
751 for(prev = client_list_prev_cycle(&globalconf.clients, sel);
752 prev && (prev->skip || !client_isvisible(prev, sel->screen));
753 prev = client_list_prev_cycle(&globalconf.clients, prev));
755 return prev;
758 static Client *
759 client_find_next_visible(Client *sel)
761 Client *next = NULL;
763 if(!sel) return NULL;
765 for(next = sel->next; next && !client_isvisible(next, sel->screen); next = next->next);
766 if(!next)
767 for(next = globalconf.clients; next && !client_isvisible(next, sel->screen); next = next->next);
769 return next;
772 /** Swap current with previous client
773 * \param screen Screen ID
774 * \param arg nothing
775 * \ingroup ui_callback
777 void
778 uicb_client_swapprev(int screen __attribute__ ((unused)),
779 char *arg __attribute__ ((unused)))
781 Client *prev;
783 if((prev = client_find_prev_visible(globalconf.focus->client)))
785 client_list_swap(&globalconf.clients, prev, globalconf.focus->client);
786 globalconf.screens[prev->screen].need_arrange = True;
787 globalconf.drop_events |= EnterWindowMask;
791 /** Swap current with next client
792 * \param screen Screen ID
793 * \param arg nothing
794 * \ingroup ui_callback
796 void
797 uicb_client_swapnext(int screen __attribute__ ((unused)),
798 char *arg __attribute__ ((unused)))
800 Client *next;
802 if((next = client_find_next_visible(globalconf.focus->client)))
804 client_list_swap(&globalconf.clients, globalconf.focus->client, next);
805 globalconf.screens[next->screen].need_arrange = True;
806 globalconf.drop_events |= EnterWindowMask;
810 /** Move and resize client
811 * \param screen Screen ID
812 * \param arg x y w h
813 * \ingroup ui_callback
815 void
816 uicb_client_moveresize(int screen, char *arg)
818 int ox, oy, ow, oh;
819 char x[8], y[8], w[8], h[8];
820 int mx, my, dx, dy, nmx, nmy;
821 unsigned int dui;
822 Window dummy;
823 Area area;
824 Client *sel = globalconf.focus->client;
825 Tag **curtags = tags_get_current(screen);
827 if(curtags[0]->layout->arrange != layout_floating)
828 if(!sel || !sel->isfloating || sel->isfixed || !arg)
830 p_delete(&curtags);
831 return;
833 p_delete(&curtags);
834 if(sscanf(arg, "%s %s %s %s", x, y, w, h) != 4)
835 return;
836 area.x = (int) compute_new_value_from_arg(x, sel->geometry.x);
837 area.y = (int) compute_new_value_from_arg(y, sel->geometry.y);
838 area.width = (int) compute_new_value_from_arg(w, sel->geometry.width);
839 area.height = (int) compute_new_value_from_arg(h, sel->geometry.height);
841 ox = sel->geometry.x;
842 oy = sel->geometry.y;
843 ow = sel->geometry.width;
844 oh = sel->geometry.height;
846 Bool xqp = XQueryPointer(globalconf.display,
847 RootWindow(globalconf.display,
848 get_phys_screen(screen)),
849 &dummy, &dummy, &mx, &my, &dx, &dy, &dui);
850 client_resize(sel, area, globalconf.screens[sel->screen].resize_hints);
851 if (xqp && ox <= mx && (ox + ow) >= mx && oy <= my && (oy + oh) >= my)
853 nmx = mx - ox + sel->geometry.width - ow - 1 < 0 ? 0 : mx - ox + sel->geometry.width - ow - 1;
854 nmy = my - oy + sel->geometry.height - oh - 1 < 0 ? 0 : my - oy + sel->geometry.height - oh - 1;
855 XWarpPointer(globalconf.display,
856 None, sel->win,
857 0, 0, 0, 0, nmx, nmy);
861 void
862 client_kill(Client *c)
864 XEvent ev;
866 if(client_isprotodel(globalconf.display, c->win))
868 ev.type = ClientMessage;
869 ev.xclient.window = c->win;
870 ev.xclient.message_type = XInternAtom(globalconf.display, "WM_PROTOCOLS", False);
871 ev.xclient.format = 32;
872 ev.xclient.data.l[0] = XInternAtom(globalconf.display, "WM_DELETE_WINDOW", False);
873 ev.xclient.data.l[1] = CurrentTime;
874 XSendEvent(globalconf.display, c->win, False, NoEventMask, &ev);
876 else
877 XKillClient(globalconf.display, c->win);
880 /** Kill selected client
881 * \param screen Screen ID
882 * \param arg unused
883 * \ingroup ui_callback
885 void
886 uicb_client_kill(int screen __attribute__ ((unused)), char *arg __attribute__ ((unused)))
888 Client *sel = globalconf.focus->client;
890 if(sel)
891 client_kill(sel);
894 static void
895 client_maximize(Client *c, Area geometry)
898 if((c->ismax = !c->ismax))
900 c->wasfloating = c->isfloating;
901 c->m_geometry = c->geometry;
902 if(get_current_layout(c->screen)->arrange != layout_floating)
903 client_setfloating(c, True);
904 client_resize(c, geometry, False);
905 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
907 else if(c->wasfloating)
909 client_setfloating(c, True);
910 client_resize(c, c->m_geometry, False);
911 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
913 else if(get_current_layout(c->screen)->arrange == layout_floating)
915 client_resize(c, c->m_geometry, False);
916 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
918 else
920 client_setfloating(c, False);
921 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
925 /** Toggle maximize for client
926 * \param screen Screen ID
927 * \param arg Unused
928 * \ingroup ui_callback
930 void
931 uicb_client_togglemax(int screen, char *arg __attribute__ ((unused)))
933 Client *sel = globalconf.focus->client;
934 Area area = screen_get_area(screen,
935 globalconf.screens[screen].statusbar,
936 &globalconf.screens[screen].padding);
938 if(sel)
940 area.width -= 2 * sel->border;
941 area.height -= 2 * sel->border;
942 client_maximize(sel, area);
946 /** Toggle vertical maximize for client
947 * \param screen Screen ID
948 * \param arg Unused
949 * \ingroup ui_callback
951 void
952 uicb_client_toggleverticalmax(int screen, char *arg __attribute__ ((unused)))
954 Client *sel = globalconf.focus->client;
955 Area area = screen_get_area(screen,
956 globalconf.screens[screen].statusbar,
957 &globalconf.screens[screen].padding);
959 if(sel)
961 area.x = sel->geometry.x;
962 area.width = sel->geometry.width;
963 area.height -= 2 * sel->border;
964 client_maximize(sel, area);
969 /** Toggle horizontal maximize for client
970 * \param screen Screen ID
971 * \param arg Unused
972 * \ingroup ui_callback
974 void
975 uicb_client_togglehorizontalmax(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.y = sel->geometry.y;
985 area.height = sel->geometry.height;
986 area.width -= 2 * sel->border;
987 client_maximize(sel, area);
991 /** Zoom client
992 * \param screen Screen ID
993 * \param arg Unused
994 * \ingroup ui_callback
996 void
997 uicb_client_zoom(int screen, char *arg __attribute__ ((unused)))
999 Client *c, *sel = globalconf.focus->client;
1001 for(c = globalconf.clients; !client_isvisible(c, screen); c = c->next);
1002 if(c == sel)
1003 for(sel = sel->next; sel && !client_isvisible(sel, screen); sel = sel->next);
1005 if(!sel)
1006 return;
1008 client_list_detach(&globalconf.clients, sel);
1009 client_list_push(&globalconf.clients, sel);
1010 globalconf.screens[screen].need_arrange = True;
1011 globalconf.drop_events |= EnterWindowMask;
1014 /** Send focus to next client in stack
1015 * \param screen Screen ID
1016 * \param arg Unused
1017 * \ingroup ui_callback
1019 void
1020 uicb_client_focusnext(int screen, char *arg __attribute__ ((unused)))
1022 Client *c, *sel = globalconf.focus->client;
1024 if(!sel)
1025 return;
1026 for(c = sel->next; c && (c->skip || !client_isvisible(c, screen)); c = c->next);
1027 if(!c)
1028 for(c = globalconf.clients; c && (c->skip || !client_isvisible(c, screen)); c = c->next);
1029 if(c)
1030 client_focus(c, screen, False);
1033 /** Send focus to previous client in stack
1034 * \param screen Screen ID
1035 * \param arg Unused
1036 * \ingroup ui_callback
1038 void
1039 uicb_client_focusprev(int screen, char *arg __attribute__ ((unused)))
1041 Client *prev;
1043 if((prev = client_find_prev_visible(globalconf.focus->client)))
1044 client_focus(prev, screen, False);
1047 /** Toggle floating state of a client
1048 * \param screen Screen ID
1049 * \param arg unused
1050 * \ingroup ui_callback
1052 void
1053 uicb_client_togglefloating(int screen __attribute__ ((unused)),
1054 char *arg __attribute__ ((unused)))
1056 if(globalconf.focus->client)
1057 client_setfloating(globalconf.focus->client, !globalconf.focus->client->isfloating);
1060 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80