make draw_text multibyte UTF-8 aware
[awesome.git] / client.c
blobf8370ebe688d3d57601666d4c419fc77079e6ccf
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 /* 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(!from_mouse
204 || (globalconf.screens[screen].sloppy_focus && globalconf.screens[screen].sloppy_focus_raise))
205 XRaiseWindow(globalconf.display, c->win);
206 /* since we're dropping EnterWindow events and sometimes the window
207 * will appear under the mouse, grabbuttons */
208 window_grabbuttons(phys_screen, c->win);
210 else
211 XSetInputFocus(globalconf.display,
212 RootWindow(globalconf.display, phys_screen),
213 RevertToPointerRoot, CurrentTime);
215 widget_invalidate_cache(screen, WIDGET_CACHE_CLIENTS);
216 ewmh_update_net_active_window(phys_screen);
217 globalconf.drop_events |= EnterWindowMask;
220 /** Compute smart coordinates for a client window
221 * \param geometry current/requested client geometry
222 * \param screen screen used
223 * \return new geometry
225 static Area
226 client_get_smart_geometry(Area geometry, int border, int screen)
228 Client *c;
229 Area newgeometry = { 0, 0, 0, 0, NULL };
230 Area *screen_geometry, *arealist = NULL, *r;
231 Bool found = False;
233 screen_geometry = p_new(Area, 1);
235 *screen_geometry = screen_get_area(screen,
236 globalconf.screens[screen].statusbar,
237 &globalconf.screens[screen].padding);
239 area_list_push(&arealist, screen_geometry);
241 for(c = globalconf.clients; c; c = c->next)
242 if(client_isvisible(c, screen))
244 newgeometry = c->f_geometry;
245 newgeometry.width += 2 * c->border;
246 newgeometry.height += 2 * c->border;
247 area_list_remove(&arealist, &newgeometry);
250 newgeometry.x = geometry.x;
251 newgeometry.y = geometry.y;
252 newgeometry.width = 0;
253 newgeometry.height = 0;
255 for(r = arealist; r; r = r->next)
256 if(r->width >= geometry.width && r->height >= geometry.height
257 && r->width * r->height > newgeometry.width * newgeometry.height)
259 found = True;
260 newgeometry = *r;
263 /* we did not found a space with enough space for our size:
264 * just take the biggest available and go in */
265 if(!found)
266 for(r = arealist; r; r = r->next)
267 if(r->width * r->height > newgeometry.width * newgeometry.height)
268 newgeometry = *r;
270 /* restore height and width */
271 newgeometry.width = geometry.width;
272 newgeometry.height = geometry.height;
274 /* fix offscreen */
275 if(AREA_RIGHT(newgeometry) > AREA_RIGHT(*screen_geometry))
276 newgeometry.x = screen_geometry->x + screen_geometry->width - newgeometry.width - 2 * border;
278 if(AREA_BOTTOM(newgeometry) > AREA_BOTTOM(*screen_geometry))
279 newgeometry.y = screen_geometry->y + screen_geometry->height - newgeometry.height - 2 * border;
281 area_list_wipe(&arealist);
283 return newgeometry;
286 /** Manage a new client
287 * \param w The window
288 * \param wa Window attributes
289 * \param screen Screen ID
291 void
292 client_manage(Window w, XWindowAttributes *wa, int screen)
294 Client *c, *t = NULL;
295 Window trans;
296 Bool rettrans;
297 XWindowChanges wc;
298 Tag *tag;
299 Rule *rule;
300 Area screen_geom;
301 int phys_screen = get_phys_screen(screen);
302 long flags;
304 c = p_new(Client, 1);
306 c->screen = screen_get_bycoord(wa->x, wa->y);
308 screen_geom = screen_get_area(c->screen,
309 globalconf.screens[screen].statusbar,
310 &globalconf.screens[screen].padding);
311 /* Initial values */
312 c->win = w;
313 c->geometry.x = c->f_geometry.x = c->m_geometry.x = MAX(wa->x, screen_geom.x);
314 c->geometry.y = c->f_geometry.y = c->m_geometry.y = MAX(wa->y, screen_geom.y);
315 c->geometry.width = c->f_geometry.width = c->m_geometry.width = wa->width;
316 c->geometry.height = c->f_geometry.height = c->m_geometry.height = wa->height;
317 c->oldborder = wa->border_width;
318 c->newcomer = True;
320 c->border = globalconf.screens[screen].borderpx;
322 /* Set windows borders */
323 wc.border_width = c->border;
324 XConfigureWindow(globalconf.display, w, CWBorderWidth, &wc);
325 XSetWindowBorder(globalconf.display, w, globalconf.screens[screen].colors_normal[ColBorder].pixel);
326 /* propagates border_width, if size doesn't change */
327 window_configure(c->win, c->geometry, c->border);
329 /* update window title */
330 client_updatetitle(c);
332 /* update hints */
333 flags = client_updatesizehints(c);
334 client_updatewmhints(c);
336 /* First check clients hints */
337 ewmh_check_client_hints(c);
339 /* loadprops or apply rules if no props */
340 if(!client_loadprops(c, screen))
342 /* Get the client's rule */
343 if((rule = rule_matching_client(c)))
345 if(rule->screen != RULE_NOSCREEN)
346 move_client_to_screen(c, rule->screen, True);
347 else
348 move_client_to_screen(c, screen, True);
349 tag_client_with_rule(c, rule);
351 switch(rule->isfloating)
353 case Auto:
354 break;
355 case Yes:
356 client_setfloating(c, True);
357 break;
358 case No:
359 client_setfloating(c, False);
360 break;
363 if(rule->opacity >= 0.0f)
364 window_settrans(c->win, rule->opacity);
366 else
367 move_client_to_screen(c, screen, True);
368 /* check for transient and set tags like its parent,
369 * XGetTransientForHint returns 1 on success
371 if((rettrans = XGetTransientForHint(globalconf.display, w, &trans))
372 && (t = client_get_bywin(globalconf.clients, trans)))
373 for(tag = globalconf.screens[c->screen].tags; tag; tag = tag->next)
374 if(is_client_tagged(t, tag))
375 tag_client(c, tag);
377 /* should be floating if transsient or fixed */
378 if(!c->isfloating)
379 client_setfloating(c, rettrans || c->isfixed);
382 if(!(flags & (USPosition | PPosition)))
383 c->f_geometry = client_get_smart_geometry(c->f_geometry, c->border, c->screen);
385 XSelectInput(globalconf.display, w, StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
387 /* handle xshape */
388 if(globalconf.have_shape)
390 XShapeSelectInput(globalconf.display, w, ShapeNotifyMask);
391 window_setshape(phys_screen, c->win);
394 /* attach to the stack */
395 if((rule = rule_matching_client(c)))
396 switch(rule->ismaster)
398 case Yes:
399 client_list_push(&globalconf.clients, c);
400 break;
401 case No:
402 client_list_append(&globalconf.clients, c);
403 break;
404 case Auto:
405 rule = NULL;
406 break;
409 if(!rule)
411 if(globalconf.screens[c->screen].new_become_master)
412 client_list_push(&globalconf.clients, c);
413 else
414 client_list_append(&globalconf.clients, c);
417 /* some windows require this */
418 XMoveResizeWindow(globalconf.display, c->win, c->geometry.x, c->geometry.y,
419 c->geometry.width, c->geometry.height);
421 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
422 ewmh_update_net_client_list(phys_screen);
425 /** Resize client window
426 * \param c client to resize
427 * \param geometry new window geometry
428 * \param sizehints respect size hints
429 * \param return True if resize has been done
431 Bool
432 client_resize(Client *c, Area geometry, Bool sizehints)
434 int new_screen;
435 double dx, dy, max, min, ratio;
436 Area area;
437 XWindowChanges wc;
439 if(sizehints)
441 if(c->minay > 0 && c->maxay > 0 && (geometry.height - c->baseh) > 0
442 && (geometry.width - c->basew) > 0)
444 dx = (double) (geometry.width - c->basew);
445 dy = (double) (geometry.height - c->baseh);
446 min = (double) (c->minax) / (double) (c->minay);
447 max = (double) (c->maxax) / (double) (c->maxay);
448 ratio = dx / dy;
449 if(max > 0 && min > 0 && ratio > 0)
451 if(ratio < min)
453 dy = (dx * min + dy) / (min * min + 1);
454 dx = dy * min;
455 geometry.width = (int) dx + c->basew;
456 geometry.height = (int) dy + c->baseh;
458 else if(ratio > max)
460 dy = (dx * min + dy) / (max * max + 1);
461 dx = dy * min;
462 geometry.width = (int) dx + c->basew;
463 geometry.height = (int) dy + c->baseh;
467 if(c->minw && geometry.width < c->minw)
468 geometry.width = c->minw;
469 if(c->minh && geometry.height < c->minh)
470 geometry.height = c->minh;
471 if(c->maxw && geometry.width > c->maxw)
472 geometry.width = c->maxw;
473 if(c->maxh && geometry.height > c->maxh)
474 geometry.height = c->maxh;
475 if(c->incw)
476 geometry.width -= (geometry.width - c->basew) % c->incw;
477 if(c->inch)
478 geometry.height -= (geometry.height - c->baseh) % c->inch;
480 if(geometry.width <= 0 || geometry.height <= 0)
481 return False;
482 /* offscreen appearance fixes */
483 area = get_display_area(get_phys_screen(c->screen),
484 NULL,
485 &globalconf.screens[c->screen].padding);
486 if(geometry.x > area.width)
487 geometry.x = area.width - geometry.width - 2 * c->border;
488 if(geometry.y > area.height)
489 geometry.y = area.height - geometry.height - 2 * c->border;
490 if(geometry.x + geometry.width + 2 * c->border < 0)
491 geometry.x = 0;
492 if(geometry.y + geometry.height + 2 * c->border < 0)
493 geometry.y = 0;
495 if(c->geometry.x != geometry.x || c->geometry.y != geometry.y
496 || c->geometry.width != geometry.width || c->geometry.height != geometry.height)
498 new_screen = screen_get_bycoord(geometry.x, geometry.y);
500 c->geometry.x = wc.x = geometry.x;
501 c->geometry.y = wc.y = geometry.y;
502 c->geometry.width = wc.width = geometry.width;
503 c->geometry.height = wc.height = geometry.height;
504 wc.border_width = c->border;
506 /* save the floating geometry if the window is floating but not
507 * maximized */
508 if((c->isfloating ||
509 get_current_layout(new_screen)->arrange == layout_floating) && !c->ismax)
510 c->f_geometry = geometry;
512 XConfigureWindow(globalconf.display, c->win,
513 CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
514 window_configure(c->win, geometry, c->border);
516 if(c->screen != new_screen)
517 move_client_to_screen(c, new_screen, False);
519 return True;
521 return False;
524 void
525 client_setfloating(Client *c, Bool floating)
527 if(c->isfloating != floating)
529 if((c->isfloating = floating))
530 client_resize(c, c->f_geometry, False);
531 else if(c->ismax)
533 c->ismax = False;
534 client_resize(c, c->m_geometry, False);
536 if(client_isvisible(c, c->screen))
537 globalconf.screens[c->screen].need_arrange = True;
538 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
539 client_saveprops(c);
543 /** Save client properties as an X property
544 * \param c client
546 void
547 client_saveprops(Client *c)
549 int i = 0, ntags = 0;
550 char *prop;
551 Tag *tag;
553 for(tag = globalconf.screens[c->screen].tags; tag; tag = tag->next)
554 ntags++;
556 prop = p_new(char, ntags + 2);
558 for(tag = globalconf.screens[c->screen].tags; tag; tag = tag->next, i++)
559 prop[i] = is_client_tagged(c, tag) ? '1' : '0';
561 if(i <= ntags)
562 prop[i] = c->isfloating ? '1' : '0';
564 prop[++i] = '\0';
566 XChangeProperty(globalconf.display, c->win,
567 XInternAtom(globalconf.display, "_AWESOME_PROPERTIES", False),
568 XA_STRING, 8, PropModeReplace, (unsigned char *) prop, i);
570 p_delete(&prop);
573 void
574 client_unban(Client *c)
576 XMapWindow(globalconf.display, c->win);
577 window_setstate(c->win, NormalState);
580 void
581 client_unmanage(Client *c)
583 XWindowChanges wc;
584 Tag *tag;
586 wc.border_width = c->oldborder;
588 /* The server grab construct avoids race conditions. */
589 XGrabServer(globalconf.display);
591 XConfigureWindow(globalconf.display, c->win, CWBorderWidth, &wc); /* restore border */
593 /* remove client everywhere */
594 client_list_detach(&globalconf.clients, c);
595 focus_delete_client(c);
596 for(tag = globalconf.screens[c->screen].tags; tag; tag = tag->next)
597 untag_client(c, tag);
599 if(globalconf.focus->client == c)
600 client_focus(NULL, c->screen, False);
602 XUngrabButton(globalconf.display, AnyButton, AnyModifier, c->win);
603 window_setstate(c->win, WithdrawnState);
605 XSync(globalconf.display, False);
606 XUngrabServer(globalconf.display);
608 p_delete(&c);
611 void
612 client_updatewmhints(Client *c)
614 XWMHints *wmh;
616 if((wmh = XGetWMHints(globalconf.display, c->win)))
618 if((c->isurgent = (wmh->flags & XUrgencyHint)))
619 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
620 if((wmh->flags & StateHint) && wmh->initial_state == WithdrawnState)
622 c->border = 0;
623 c->skip = True;
625 XFree(wmh);
629 long
630 client_updatesizehints(Client *c)
632 long msize;
633 XSizeHints size;
635 if(!XGetWMNormalHints(globalconf.display, c->win, &size, &msize))
636 return 0L;
638 if(size.flags & PBaseSize)
640 c->basew = size.base_width;
641 c->baseh = size.base_height;
643 else if(size.flags & PMinSize)
645 c->basew = size.min_width;
646 c->baseh = size.min_height;
648 else
649 c->basew = c->baseh = 0;
650 if(size.flags & PResizeInc)
652 c->incw = size.width_inc;
653 c->inch = size.height_inc;
655 else
656 c->incw = c->inch = 0;
658 if(size.flags & PMaxSize)
660 c->maxw = size.max_width;
661 c->maxh = size.max_height;
663 else
664 c->maxw = c->maxh = 0;
666 if(size.flags & PMinSize)
668 c->minw = size.min_width;
669 c->minh = size.min_height;
671 else if(size.flags & PBaseSize)
673 c->minw = size.base_width;
674 c->minh = size.base_height;
676 else
677 c->minw = c->minh = 0;
679 if(size.flags & PAspect)
681 c->minax = size.min_aspect.x;
682 c->maxax = size.max_aspect.x;
683 c->minay = size.min_aspect.y;
684 c->maxay = size.max_aspect.y;
686 else
687 c->minax = c->maxax = c->minay = c->maxay = 0;
689 if(c->maxw && c->minw && c->maxh && c->minh
690 && c->maxw == c->minw && c->maxh == c->minh)
691 c->isfixed = True;
693 return size.flags;
696 /** Returns True if a client is tagged
697 * with one of the tags
698 * \return True or False
700 Bool
701 client_isvisible(Client *c, int screen)
703 Tag *tag;
705 if(!c || c->screen != screen)
706 return False;
708 if(globalconf.scratch.client == c)
709 return globalconf.scratch.isvisible;
711 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
712 if(tag->selected && is_client_tagged(c, tag))
713 return True;
714 return False;
717 /** Set selected client transparency
718 * \param screen Screen ID
719 * \param arg unused arg
720 * \ingroup ui_callback
722 void
723 uicb_client_settrans(int screen __attribute__ ((unused)), char *arg)
725 double delta = 100.0, current_opacity = 100.0;
726 unsigned char *data;
727 Atom actual;
728 int format;
729 unsigned long n, left;
730 unsigned int current_opacity_raw = 0;
731 int set_prop = 0;
732 Client *sel = globalconf.focus->client;
734 if(!sel)
735 return;
737 XGetWindowProperty(globalconf.display, sel->win,
738 XInternAtom(globalconf.display, "_NET_WM_WINDOW_OPACITY", False),
739 0L, 1L, False, XA_CARDINAL, &actual, &format, &n, &left,
740 (unsigned char **) &data);
741 if(data)
743 memcpy(&current_opacity_raw, data, sizeof(unsigned int));
744 XFree(data);
745 current_opacity = (current_opacity_raw * 100.0) / 0xffffffff;
747 else
748 set_prop = 1;
750 delta = compute_new_value_from_arg(arg, current_opacity);
752 if(delta <= 0.0)
753 delta = 0.0;
754 else if(delta > 100.0)
756 delta = 100.0;
757 set_prop = 1;
760 if(delta == 100.0 && !set_prop)
761 window_settrans(sel->win, -1);
762 else
763 window_settrans(sel->win, delta);
766 static Client *
767 client_find_prev_visible(Client *sel)
769 Client *prev = NULL;
771 if(!sel) return NULL;
773 /* look for previous starting at sel */
774 for(prev = client_list_prev_cycle(&globalconf.clients, sel);
775 prev && (prev->skip || !client_isvisible(prev, sel->screen));
776 prev = client_list_prev_cycle(&globalconf.clients, prev));
778 return prev;
781 static Client *
782 client_find_next_visible(Client *sel)
784 Client *next = NULL;
786 if(!sel) return NULL;
788 for(next = sel->next; next && !client_isvisible(next, sel->screen); next = next->next);
789 if(!next)
790 for(next = globalconf.clients; next && !client_isvisible(next, sel->screen); next = next->next);
792 return next;
795 /** Swap current with previous client
796 * \param screen Screen ID
797 * \param arg nothing
798 * \ingroup ui_callback
800 void
801 uicb_client_swapprev(int screen __attribute__ ((unused)),
802 char *arg __attribute__ ((unused)))
804 Client *prev;
806 if((prev = client_find_prev_visible(globalconf.focus->client)))
808 client_list_swap(&globalconf.clients, prev, globalconf.focus->client);
809 globalconf.screens[prev->screen].need_arrange = True;
810 globalconf.drop_events |= EnterWindowMask;
814 /** Swap current with next client
815 * \param screen Screen ID
816 * \param arg nothing
817 * \ingroup ui_callback
819 void
820 uicb_client_swapnext(int screen __attribute__ ((unused)),
821 char *arg __attribute__ ((unused)))
823 Client *next;
825 if((next = client_find_next_visible(globalconf.focus->client)))
827 client_list_swap(&globalconf.clients, globalconf.focus->client, next);
828 globalconf.screens[next->screen].need_arrange = True;
829 globalconf.drop_events |= EnterWindowMask;
833 /** Move and resize client
834 * \param screen Screen ID
835 * \param arg x y w h
836 * \ingroup ui_callback
838 void
839 uicb_client_moveresize(int screen, char *arg)
841 int ox, oy, ow, oh;
842 char x[8], y[8], w[8], h[8];
843 int mx, my, dx, dy, nmx, nmy;
844 unsigned int dui;
845 Window dummy;
846 Area area;
847 Client *sel = globalconf.focus->client;
848 Tag **curtags = tags_get_current(screen);
850 if(curtags[0]->layout->arrange != layout_floating)
851 if(!sel || !sel->isfloating || sel->isfixed || !arg)
853 p_delete(&curtags);
854 return;
856 p_delete(&curtags);
857 if(sscanf(arg, "%s %s %s %s", x, y, w, h) != 4)
858 return;
859 area.x = (int) compute_new_value_from_arg(x, sel->geometry.x);
860 area.y = (int) compute_new_value_from_arg(y, sel->geometry.y);
861 area.width = (int) compute_new_value_from_arg(w, sel->geometry.width);
862 area.height = (int) compute_new_value_from_arg(h, sel->geometry.height);
864 ox = sel->geometry.x;
865 oy = sel->geometry.y;
866 ow = sel->geometry.width;
867 oh = sel->geometry.height;
869 Bool xqp = XQueryPointer(globalconf.display,
870 RootWindow(globalconf.display,
871 get_phys_screen(screen)),
872 &dummy, &dummy, &mx, &my, &dx, &dy, &dui);
873 client_resize(sel, area, globalconf.screens[sel->screen].resize_hints);
874 if (xqp && ox <= mx && (ox + ow) >= mx && oy <= my && (oy + oh) >= my)
876 nmx = mx - ox + sel->geometry.width - ow - 1 < 0 ? 0 : mx - ox + sel->geometry.width - ow - 1;
877 nmy = my - oy + sel->geometry.height - oh - 1 < 0 ? 0 : my - oy + sel->geometry.height - oh - 1;
878 XWarpPointer(globalconf.display,
879 None, sel->win,
880 0, 0, 0, 0, nmx, nmy);
884 void
885 client_kill(Client *c)
887 XEvent ev;
889 if(client_isprotodel(globalconf.display, c->win))
891 ev.type = ClientMessage;
892 ev.xclient.window = c->win;
893 ev.xclient.message_type = XInternAtom(globalconf.display, "WM_PROTOCOLS", False);
894 ev.xclient.format = 32;
895 ev.xclient.data.l[0] = XInternAtom(globalconf.display, "WM_DELETE_WINDOW", False);
896 ev.xclient.data.l[1] = CurrentTime;
897 XSendEvent(globalconf.display, c->win, False, NoEventMask, &ev);
899 else
900 XKillClient(globalconf.display, c->win);
903 /** Kill selected client
904 * \param screen Screen ID
905 * \param arg unused
906 * \ingroup ui_callback
908 void
909 uicb_client_kill(int screen __attribute__ ((unused)), char *arg __attribute__ ((unused)))
911 Client *sel = globalconf.focus->client;
913 if(sel)
914 client_kill(sel);
917 static void
918 client_maximize(Client *c, Area geometry)
921 if((c->ismax = !c->ismax))
923 c->wasfloating = c->isfloating;
924 c->m_geometry = c->geometry;
925 if(get_current_layout(c->screen)->arrange != layout_floating)
926 client_setfloating(c, 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, False);
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, False);
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, False);
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