change draw_color_new() proto to fill the struct and return status
[awesome.git] / client.c
blob4b9d0fb8a5f3dcebe8f7ac5fd5e9a4efc79343f0
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)
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 /** Compute smart coordinates for a client window
237 * \param geometry current/requested client geometry
238 * \param screen screen used
239 * \return new geometry
241 static Area
242 client_get_smart_geometry(Area geometry, int border, int screen)
244 Client *c;
245 Area newgeometry = { 0, 0, 0, 0, NULL };
246 Area *screen_geometry, *tmp, *arealist = NULL, *r;
247 Bool found = False;
249 screen_geometry = p_new(Area, 1);
250 tmp = p_new(Area, 1);
252 /* we need tmp because it may be free'd by in
253 * the area_list_remove process */
254 *screen_geometry = *tmp = screen_get_area(screen,
255 globalconf.screens[screen].statusbar,
256 &globalconf.screens[screen].padding);
258 area_list_push(&arealist, tmp);
260 for(c = globalconf.clients; c; c = c->next)
261 if(client_isvisible(c, screen))
263 newgeometry = c->f_geometry;
264 newgeometry.width += 2 * c->border;
265 newgeometry.height += 2 * c->border;
266 area_list_remove(&arealist, &newgeometry);
269 newgeometry.x = geometry.x;
270 newgeometry.y = geometry.y;
271 newgeometry.width = 0;
272 newgeometry.height = 0;
274 for(r = arealist; r; r = r->next)
275 if(r->width >= geometry.width && r->height >= geometry.height
276 && r->width * r->height > newgeometry.width * newgeometry.height)
278 found = True;
279 newgeometry = *r;
282 /* we did not found a space with enough space for our size:
283 * just take the biggest available and go in */
284 if(!found)
285 for(r = arealist; r; r = r->next)
286 if(r->width * r->height > newgeometry.width * newgeometry.height)
287 newgeometry = *r;
289 /* restore height and width */
290 newgeometry.width = geometry.width;
291 newgeometry.height = geometry.height;
293 /* fix offscreen */
294 if(AREA_RIGHT(newgeometry) > AREA_RIGHT(*screen_geometry))
295 newgeometry.x = screen_geometry->x + screen_geometry->width - (newgeometry.width + 2 * border);
297 if(AREA_BOTTOM(newgeometry) > AREA_BOTTOM(*screen_geometry))
298 newgeometry.y = screen_geometry->y + screen_geometry->height - (newgeometry.height + 2 * border);
299 area_list_wipe(&arealist);
300 p_delete(&screen_geometry);
302 return newgeometry;
305 /** Manage a new client
306 * \param w The window
307 * \param wa Window attributes
308 * \param screen Screen ID
310 void
311 client_manage(Window w, XWindowAttributes *wa, int screen)
313 Client *c, *t = NULL;
314 Window trans;
315 Bool rettrans;
316 XWindowChanges wc;
317 Tag *tag;
318 Rule *rule;
319 Area screen_geom;
320 int phys_screen = get_phys_screen(screen);
321 long flags;
323 c = p_new(Client, 1);
325 c->screen = screen_get_bycoord(wa->x, wa->y);
327 screen_geom = screen_get_area(c->screen,
328 globalconf.screens[screen].statusbar,
329 &globalconf.screens[screen].padding);
330 /* Initial values */
331 c->win = w;
332 c->geometry.x = c->f_geometry.x = c->m_geometry.x = MAX(wa->x, screen_geom.x);
333 c->geometry.y = c->f_geometry.y = c->m_geometry.y = MAX(wa->y, screen_geom.y);
334 c->geometry.width = c->f_geometry.width = c->m_geometry.width = wa->width;
335 c->geometry.height = c->f_geometry.height = c->m_geometry.height = wa->height;
336 c->oldborder = wa->border_width;
337 c->newcomer = True;
339 c->border = globalconf.screens[screen].borderpx;
341 /* Set windows borders */
342 wc.border_width = c->border;
343 XConfigureWindow(globalconf.display, w, CWBorderWidth, &wc);
344 XSetWindowBorder(globalconf.display, w, globalconf.screens[screen].colors_normal[ColBorder].pixel);
345 /* propagates border_width, if size doesn't change */
346 window_configure(c->win, c->geometry, c->border);
348 /* update window title */
349 client_updatetitle(c);
351 /* update hints */
352 flags = client_updatesizehints(c);
353 client_updatewmhints(c);
355 /* First check clients hints */
356 ewmh_check_client_hints(c);
358 /* loadprops or apply rules if no props */
359 if(!client_loadprops(c, screen))
361 /* Get the client's rule */
362 if((rule = rule_matching_client(c)))
364 if(rule->screen != RULE_NOSCREEN)
365 move_client_to_screen(c, rule->screen, True);
366 else
367 move_client_to_screen(c, screen, True);
368 tag_client_with_rule(c, rule);
370 switch(rule->isfloating)
372 case Auto:
373 break;
374 case Yes:
375 client_setfloating(c, True);
376 break;
377 case No:
378 client_setfloating(c, False);
379 break;
382 if(rule->opacity >= 0.0f)
383 window_settrans(c->win, rule->opacity);
385 else
386 move_client_to_screen(c, screen, True);
389 /* check for transient and set tags like its parent,
390 * XGetTransientForHint returns 1 on success
392 if((rettrans = XGetTransientForHint(globalconf.display, w, &trans))
393 && (t = client_get_bywin(globalconf.clients, trans)))
394 for(tag = globalconf.screens[c->screen].tags; tag; tag = tag->next)
395 if(is_client_tagged(t, tag))
396 tag_client(c, tag);
398 /* should be floating if transsient or fixed */
399 if(!c->isfloating)
400 client_setfloating(c, rettrans || c->isfixed);
402 if(!(flags & (USPosition | PPosition)))
403 c->f_geometry = client_get_smart_geometry(c->f_geometry, c->border, c->screen);
405 XSelectInput(globalconf.display, w, StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
407 /* handle xshape */
408 if(globalconf.have_shape)
410 XShapeSelectInput(globalconf.display, w, ShapeNotifyMask);
411 window_setshape(phys_screen, c->win);
414 /* attach to the stack */
415 if((rule = rule_matching_client(c)))
416 switch(rule->ismaster)
418 case Yes:
419 client_list_push(&globalconf.clients, c);
420 break;
421 case No:
422 client_list_append(&globalconf.clients, c);
423 break;
424 case Auto:
425 rule = NULL;
426 break;
429 if(!rule)
431 if(globalconf.screens[c->screen].new_become_master)
432 client_list_push(&globalconf.clients, c);
433 else
434 client_list_append(&globalconf.clients, c);
437 /* some windows require this */
438 XMoveResizeWindow(globalconf.display, c->win, c->geometry.x, c->geometry.y,
439 c->geometry.width, c->geometry.height);
441 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
442 ewmh_update_net_client_list(phys_screen);
445 /** Resize client window
446 * \param c client to resize
447 * \param geometry new window geometry
448 * \param sizehints respect size hints
449 * \param return True if resize has been done
451 Bool
452 client_resize(Client *c, Area geometry, Bool sizehints)
454 int new_screen;
455 double dx, dy, max, min, ratio;
456 Area area;
457 XWindowChanges wc;
459 if(sizehints)
461 if(c->minay > 0 && c->maxay > 0 && (geometry.height - c->baseh) > 0
462 && (geometry.width - c->basew) > 0)
464 dx = (double) (geometry.width - c->basew);
465 dy = (double) (geometry.height - c->baseh);
466 min = (double) (c->minax) / (double) (c->minay);
467 max = (double) (c->maxax) / (double) (c->maxay);
468 ratio = dx / dy;
469 if(max > 0 && min > 0 && ratio > 0)
471 if(ratio < min)
473 dy = (dx * min + dy) / (min * min + 1);
474 dx = dy * min;
475 geometry.width = (int) dx + c->basew;
476 geometry.height = (int) dy + c->baseh;
478 else if(ratio > max)
480 dy = (dx * min + dy) / (max * max + 1);
481 dx = dy * min;
482 geometry.width = (int) dx + c->basew;
483 geometry.height = (int) dy + c->baseh;
487 if(c->minw && geometry.width < c->minw)
488 geometry.width = c->minw;
489 if(c->minh && geometry.height < c->minh)
490 geometry.height = c->minh;
491 if(c->maxw && geometry.width > c->maxw)
492 geometry.width = c->maxw;
493 if(c->maxh && geometry.height > c->maxh)
494 geometry.height = c->maxh;
495 if(c->incw)
496 geometry.width -= (geometry.width - c->basew) % c->incw;
497 if(c->inch)
498 geometry.height -= (geometry.height - c->baseh) % c->inch;
500 if(geometry.width <= 0 || geometry.height <= 0)
501 return False;
502 /* offscreen appearance fixes */
503 area = get_display_area(get_phys_screen(c->screen),
504 NULL,
505 &globalconf.screens[c->screen].padding);
506 if(geometry.x > area.width)
507 geometry.x = area.width - geometry.width - 2 * c->border;
508 if(geometry.y > area.height)
509 geometry.y = area.height - geometry.height - 2 * c->border;
510 if(geometry.x + geometry.width + 2 * c->border < 0)
511 geometry.x = 0;
512 if(geometry.y + geometry.height + 2 * c->border < 0)
513 geometry.y = 0;
515 if(c->geometry.x != geometry.x || c->geometry.y != geometry.y
516 || c->geometry.width != geometry.width || c->geometry.height != geometry.height)
518 new_screen = screen_get_bycoord(geometry.x, geometry.y);
520 c->geometry.x = wc.x = geometry.x;
521 c->geometry.y = wc.y = geometry.y;
522 c->geometry.width = wc.width = geometry.width;
523 c->geometry.height = wc.height = geometry.height;
524 wc.border_width = c->border;
526 /* save the floating geometry if the window is floating but not
527 * maximized */
528 if((c->isfloating ||
529 layout_get_current(new_screen)->arrange == layout_floating) && !c->ismax)
530 c->f_geometry = geometry;
532 XConfigureWindow(globalconf.display, c->win,
533 CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
534 window_configure(c->win, geometry, c->border);
536 if(c->screen != new_screen)
537 move_client_to_screen(c, new_screen, False);
539 return True;
541 return False;
544 void
545 client_setfloating(Client *c, Bool floating)
547 if(c->isfloating != floating)
549 if((c->isfloating = floating))
551 client_resize(c, c->f_geometry, False);
552 XRaiseWindow(globalconf.display, c->win);
554 else
556 XLowerWindow(globalconf.display, c->win);
557 if(c->ismax)
559 c->ismax = False;
560 client_resize(c, c->m_geometry, False);
563 if(client_isvisible(c, c->screen))
564 globalconf.screens[c->screen].need_arrange = True;
565 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
566 client_saveprops(c);
570 /** Save client properties as an X property
571 * \param c client
573 void
574 client_saveprops(Client *c)
576 int i = 0, ntags = 0;
577 char *prop;
578 Tag *tag;
580 for(tag = globalconf.screens[c->screen].tags; tag; tag = tag->next)
581 ntags++;
583 prop = p_new(char, ntags + 2);
585 for(tag = globalconf.screens[c->screen].tags; tag; tag = tag->next, i++)
586 prop[i] = is_client_tagged(c, tag) ? '1' : '0';
588 if(i <= ntags)
589 prop[i] = c->isfloating ? '1' : '0';
591 prop[++i] = '\0';
593 XChangeProperty(globalconf.display, c->win,
594 XInternAtom(globalconf.display, "_AWESOME_PROPERTIES", False),
595 XA_STRING, 8, PropModeReplace, (unsigned char *) prop, i);
597 p_delete(&prop);
600 void
601 client_unban(Client *c)
603 XMapWindow(globalconf.display, c->win);
604 window_setstate(c->win, NormalState);
607 void
608 client_unmanage(Client *c)
610 XWindowChanges wc;
611 Tag *tag;
613 wc.border_width = c->oldborder;
615 /* The server grab construct avoids race conditions. */
616 XGrabServer(globalconf.display);
618 XConfigureWindow(globalconf.display, c->win, CWBorderWidth, &wc); /* restore border */
620 /* remove client everywhere */
621 client_list_detach(&globalconf.clients, c);
622 focus_delete_client(c);
623 for(tag = globalconf.screens[c->screen].tags; tag; tag = tag->next)
624 untag_client(c, tag);
626 if(globalconf.focus->client == c)
627 client_focus(NULL, c->screen, True);
629 XUngrabButton(globalconf.display, AnyButton, AnyModifier, c->win);
630 window_setstate(c->win, WithdrawnState);
632 XSync(globalconf.display, False);
633 XUngrabServer(globalconf.display);
635 p_delete(&c);
638 void
639 client_updatewmhints(Client *c)
641 XWMHints *wmh;
643 if((wmh = XGetWMHints(globalconf.display, c->win)))
645 if((c->isurgent = (wmh->flags & XUrgencyHint)))
646 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
647 if((wmh->flags & StateHint) && wmh->initial_state == WithdrawnState)
649 c->border = 0;
650 c->skip = True;
652 XFree(wmh);
656 long
657 client_updatesizehints(Client *c)
659 long msize;
660 XSizeHints size;
662 if(!XGetWMNormalHints(globalconf.display, c->win, &size, &msize))
663 return 0L;
665 if(size.flags & PBaseSize)
667 c->basew = size.base_width;
668 c->baseh = size.base_height;
670 else if(size.flags & PMinSize)
672 c->basew = size.min_width;
673 c->baseh = size.min_height;
675 else
676 c->basew = c->baseh = 0;
677 if(size.flags & PResizeInc)
679 c->incw = size.width_inc;
680 c->inch = size.height_inc;
682 else
683 c->incw = c->inch = 0;
685 if(size.flags & PMaxSize)
687 c->maxw = size.max_width;
688 c->maxh = size.max_height;
690 else
691 c->maxw = c->maxh = 0;
693 if(size.flags & PMinSize)
695 c->minw = size.min_width;
696 c->minh = size.min_height;
698 else if(size.flags & PBaseSize)
700 c->minw = size.base_width;
701 c->minh = size.base_height;
703 else
704 c->minw = c->minh = 0;
706 if(size.flags & PAspect)
708 c->minax = size.min_aspect.x;
709 c->maxax = size.max_aspect.x;
710 c->minay = size.min_aspect.y;
711 c->maxay = size.max_aspect.y;
713 else
714 c->minax = c->maxax = c->minay = c->maxay = 0;
716 if(c->maxw && c->minw && c->maxh && c->minh
717 && c->maxw == c->minw && c->maxh == c->minh)
718 c->isfixed = True;
720 return size.flags;
723 /** Returns True if a client is tagged
724 * with one of the tags
725 * \return True or False
727 Bool
728 client_isvisible(Client *c, int screen)
730 Tag *tag;
732 if(!c || c->screen != screen)
733 return False;
735 if(globalconf.scratch.client == c)
736 return globalconf.scratch.isvisible;
738 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
739 if(tag->selected && is_client_tagged(c, tag))
740 return True;
741 return False;
744 /** Set selected client transparency
745 * \param screen Screen ID
746 * \param arg unused arg
747 * \ingroup ui_callback
749 void
750 uicb_client_settrans(int screen __attribute__ ((unused)), char *arg)
752 double delta = 100.0, current_opacity = 100.0;
753 unsigned char *data;
754 Atom actual;
755 int format;
756 unsigned long n, left;
757 unsigned int current_opacity_raw = 0;
758 int set_prop = 0;
759 Client *sel = globalconf.focus->client;
761 if(!sel)
762 return;
764 XGetWindowProperty(globalconf.display, sel->win,
765 XInternAtom(globalconf.display, "_NET_WM_WINDOW_OPACITY", False),
766 0L, 1L, False, XA_CARDINAL, &actual, &format, &n, &left,
767 (unsigned char **) &data);
768 if(data)
770 memcpy(&current_opacity_raw, data, sizeof(unsigned int));
771 XFree(data);
772 current_opacity = (current_opacity_raw * 100.0) / 0xffffffff;
774 else
775 set_prop = 1;
777 delta = compute_new_value_from_arg(arg, current_opacity);
779 if(delta <= 0.0)
780 delta = 0.0;
781 else if(delta > 100.0)
783 delta = 100.0;
784 set_prop = 1;
787 if(delta == 100.0 && !set_prop)
788 window_settrans(sel->win, -1);
789 else
790 window_settrans(sel->win, delta);
793 static Client *
794 client_find_prev_visible(Client *sel)
796 Client *prev = NULL;
798 if(!sel) return NULL;
800 /* look for previous starting at sel */
801 for(prev = client_list_prev_cycle(&globalconf.clients, sel);
802 prev && (prev->skip || !client_isvisible(prev, sel->screen));
803 prev = client_list_prev_cycle(&globalconf.clients, prev));
805 return prev;
808 static Client *
809 client_find_next_visible(Client *sel)
811 Client *next = NULL;
813 if(!sel) return NULL;
815 for(next = sel->next; next && !client_isvisible(next, sel->screen); next = next->next);
816 if(!next)
817 for(next = globalconf.clients; next && !client_isvisible(next, sel->screen); next = next->next);
819 return next;
822 /** Swap current with previous client
823 * \param screen Screen ID
824 * \param arg nothing
825 * \ingroup ui_callback
827 void
828 uicb_client_swapprev(int screen __attribute__ ((unused)),
829 char *arg __attribute__ ((unused)))
831 Client *prev;
833 if((prev = client_find_prev_visible(globalconf.focus->client)))
835 client_list_swap(&globalconf.clients, prev, globalconf.focus->client);
836 globalconf.screens[prev->screen].need_arrange = True;
837 globalconf.drop_events |= EnterWindowMask;
841 /** Swap current with next client
842 * \param screen Screen ID
843 * \param arg nothing
844 * \ingroup ui_callback
846 void
847 uicb_client_swapnext(int screen __attribute__ ((unused)),
848 char *arg __attribute__ ((unused)))
850 Client *next;
852 if((next = client_find_next_visible(globalconf.focus->client)))
854 client_list_swap(&globalconf.clients, globalconf.focus->client, next);
855 globalconf.screens[next->screen].need_arrange = True;
856 globalconf.drop_events |= EnterWindowMask;
860 /** Move and resize client
861 * \param screen Screen ID
862 * \param arg x y w h
863 * \ingroup ui_callback
865 void
866 uicb_client_moveresize(int screen, char *arg)
868 int ox, oy, ow, oh;
869 char x[8], y[8], w[8], h[8];
870 int mx, my, dx, dy, nmx, nmy;
871 unsigned int dui;
872 Window dummy;
873 Area area;
874 Client *sel = globalconf.focus->client;
875 Layout *curlay = layout_get_current(screen);
877 if(curlay->arrange != layout_floating ||
878 !sel || !sel->isfloating || sel->isfixed || !arg)
879 return;
881 if(sscanf(arg, "%s %s %s %s", x, y, w, h) != 4)
882 return;
884 area.x = (int) compute_new_value_from_arg(x, sel->geometry.x);
885 area.y = (int) compute_new_value_from_arg(y, sel->geometry.y);
886 area.width = (int) compute_new_value_from_arg(w, sel->geometry.width);
887 area.height = (int) compute_new_value_from_arg(h, sel->geometry.height);
889 ox = sel->geometry.x;
890 oy = sel->geometry.y;
891 ow = sel->geometry.width;
892 oh = sel->geometry.height;
894 Bool xqp = XQueryPointer(globalconf.display,
895 RootWindow(globalconf.display,
896 get_phys_screen(screen)),
897 &dummy, &dummy, &mx, &my, &dx, &dy, &dui);
898 client_resize(sel, area, globalconf.screens[sel->screen].resize_hints);
899 if (xqp && ox <= mx && (ox + ow) >= mx && oy <= my && (oy + oh) >= my)
901 nmx = mx - ox + sel->geometry.width - ow - 1 < 0 ? 0 : mx - ox + sel->geometry.width - ow - 1;
902 nmy = my - oy + sel->geometry.height - oh - 1 < 0 ? 0 : my - oy + sel->geometry.height - oh - 1;
903 XWarpPointer(globalconf.display,
904 None, sel->win,
905 0, 0, 0, 0, nmx, nmy);
909 void
910 client_kill(Client *c)
912 XEvent ev;
914 if(client_isprotodel(globalconf.display, c->win))
916 ev.type = ClientMessage;
917 ev.xclient.window = c->win;
918 ev.xclient.message_type = XInternAtom(globalconf.display, "WM_PROTOCOLS", False);
919 ev.xclient.format = 32;
920 ev.xclient.data.l[0] = XInternAtom(globalconf.display, "WM_DELETE_WINDOW", False);
921 ev.xclient.data.l[1] = CurrentTime;
922 XSendEvent(globalconf.display, c->win, False, NoEventMask, &ev);
924 else
925 XKillClient(globalconf.display, c->win);
928 /** Kill selected client
929 * \param screen Screen ID
930 * \param arg unused
931 * \ingroup ui_callback
933 void
934 uicb_client_kill(int screen __attribute__ ((unused)), char *arg __attribute__ ((unused)))
936 Client *sel = globalconf.focus->client;
938 if(sel)
939 client_kill(sel);
942 static void
943 client_maximize(Client *c, Area geometry)
946 if((c->ismax = !c->ismax))
948 c->wasfloating = c->isfloating;
949 c->m_geometry = c->geometry;
950 if(layout_get_current(c->screen)->arrange != layout_floating)
951 client_setfloating(c, True);
952 client_focus(c, c->screen, True);
953 client_resize(c, geometry, False);
954 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
956 else if(c->wasfloating)
958 client_setfloating(c, True);
959 client_resize(c, c->m_geometry, False);
960 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
962 else if(layout_get_current(c->screen)->arrange == layout_floating)
964 client_resize(c, c->m_geometry, False);
965 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
967 else
969 client_setfloating(c, False);
970 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
974 /** Toggle maximize for client
975 * \param screen Screen ID
976 * \param arg Unused
977 * \ingroup ui_callback
979 void
980 uicb_client_togglemax(int screen, char *arg __attribute__ ((unused)))
982 Client *sel = globalconf.focus->client;
983 Area area = screen_get_area(screen,
984 globalconf.screens[screen].statusbar,
985 &globalconf.screens[screen].padding);
987 if(sel)
989 area.width -= 2 * sel->border;
990 area.height -= 2 * sel->border;
991 client_maximize(sel, area);
995 /** Toggle vertical maximize for client
996 * \param screen Screen ID
997 * \param arg Unused
998 * \ingroup ui_callback
1000 void
1001 uicb_client_toggleverticalmax(int screen, char *arg __attribute__ ((unused)))
1003 Client *sel = globalconf.focus->client;
1004 Area area = screen_get_area(screen,
1005 globalconf.screens[screen].statusbar,
1006 &globalconf.screens[screen].padding);
1008 if(sel)
1010 area.x = sel->geometry.x;
1011 area.width = sel->geometry.width;
1012 area.height -= 2 * sel->border;
1013 client_maximize(sel, area);
1018 /** Toggle horizontal maximize for client
1019 * \param screen Screen ID
1020 * \param arg Unused
1021 * \ingroup ui_callback
1023 void
1024 uicb_client_togglehorizontalmax(int screen, char *arg __attribute__ ((unused)))
1026 Client *sel = globalconf.focus->client;
1027 Area area = screen_get_area(screen,
1028 globalconf.screens[screen].statusbar,
1029 &globalconf.screens[screen].padding);
1031 if(sel)
1033 area.y = sel->geometry.y;
1034 area.height = sel->geometry.height;
1035 area.width -= 2 * sel->border;
1036 client_maximize(sel, area);
1040 /** Zoom client
1041 * \param screen Screen ID
1042 * \param arg Unused
1043 * \ingroup ui_callback
1045 void
1046 uicb_client_zoom(int screen, char *arg __attribute__ ((unused)))
1048 Client *c, *sel = globalconf.focus->client;
1050 if(!sel)
1051 return;
1053 for(c = globalconf.clients; !client_isvisible(c, screen); c = c->next);
1054 if(c == sel)
1055 for(sel = sel->next; sel && !client_isvisible(sel, screen); sel = sel->next);
1057 if(sel)
1059 client_list_detach(&globalconf.clients, sel);
1060 client_list_push(&globalconf.clients, sel);
1061 globalconf.screens[screen].need_arrange = True;
1062 globalconf.drop_events |= EnterWindowMask;
1066 /** Send focus to next client in stack
1067 * \param screen Screen ID
1068 * \param arg Unused
1069 * \ingroup ui_callback
1071 void
1072 uicb_client_focusnext(int screen, char *arg __attribute__ ((unused)))
1074 Client *c, *sel = globalconf.focus->client;
1076 if(!sel)
1077 return;
1078 for(c = sel->next; c && (c->skip || !client_isvisible(c, screen)); c = c->next);
1079 if(!c)
1080 for(c = globalconf.clients; c && (c->skip || !client_isvisible(c, screen)); c = c->next);
1081 if(c)
1082 client_focus(c, screen, True);
1085 /** Send focus to previous client in stack
1086 * \param screen Screen ID
1087 * \param arg Unused
1088 * \ingroup ui_callback
1090 void
1091 uicb_client_focusprev(int screen, char *arg __attribute__ ((unused)))
1093 Client *prev;
1095 if((prev = client_find_prev_visible(globalconf.focus->client)))
1096 client_focus(prev, screen, True);
1099 /** Toggle floating state of a client
1100 * \param screen Screen ID
1101 * \param arg unused
1102 * \ingroup ui_callback
1104 void
1105 uicb_client_togglefloating(int screen __attribute__ ((unused)),
1106 char *arg __attribute__ ((unused)))
1108 if(globalconf.focus->client)
1109 client_setfloating(globalconf.focus->client, !globalconf.focus->client->isfloating);
1112 /** Toggle scratch client attribute
1113 * \param screen screen number
1114 * \param arg unused argument
1115 * \ingroup ui_callback
1117 void
1118 uicb_client_setscratch(int screen,
1119 char *arg __attribute__ ((unused)))
1121 if(!globalconf.focus->client)
1122 return;
1124 if(globalconf.scratch.client == globalconf.focus->client)
1125 globalconf.scratch.client = NULL;
1126 else
1127 globalconf.scratch.client = globalconf.focus->client;
1129 widget_invalidate_cache(screen, WIDGET_CACHE_CLIENTS | WIDGET_CACHE_TAGS);
1130 globalconf.screens[screen].need_arrange = True;
1133 /** Toggle scratch client visibility
1134 * \param screen screen number
1135 * \param arg unused argument
1136 * \ingroup ui_callback
1138 void
1139 uicb_client_togglescratch(int screen,
1140 char *arg __attribute__ ((unused)))
1142 if(globalconf.scratch.client)
1144 globalconf.scratch.isvisible = !globalconf.scratch.isvisible;
1145 if(globalconf.scratch.isvisible)
1146 client_focus(globalconf.scratch.client, screen, True);
1147 globalconf.screens[globalconf.scratch.client->screen].need_arrange = True;
1148 widget_invalidate_cache(globalconf.scratch.client->screen, WIDGET_CACHE_CLIENTS);
1151 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80