2 * client.c - client management
4 * Copyright © 2007 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.
23 #include <X11/Xatom.h>
24 #include <X11/extensions/shape.h>
33 #include "statusbar.h"
36 #include "layouts/floating.h"
39 extern AwesomeConf globalconf
;
41 /** Load windows properties, restoring client's tag
42 * and floating state before awesome was restarted if any
43 * \todo this may bug if number of tags is != than before
45 * \param screen Screen ID
48 client_loadprops(Client
* c
, int screen
)
55 for(tag
= globalconf
.screens
[screen
].tags
; tag
; tag
= tag
->next
)
58 prop
= p_new(char, ntags
+ 2);
60 if(xgettextprop(c
->display
, c
->win
, AWESOMEPROPS_ATOM(c
->display
), prop
, ntags
+ 2))
62 for(i
= 0, tag
= globalconf
.screens
[screen
].tags
; tag
&& i
< ntags
&& prop
[i
]; i
++, tag
= tag
->next
)
65 tag_client(c
, tag
, screen
);
69 untag_client(c
, tag
, screen
);
71 if(i
<= ntags
&& prop
[i
])
72 c
->isfloating
= prop
[i
] == '1';
80 /** Check if client supports protocol WM_DELETE_WINDOW
81 * \param disp the display
82 * \param win the Window
83 * \return True if client has WM_DELETE_WINDOW
86 isprotodel(Display
*disp
, Window win
)
92 if(XGetWMProtocols(disp
, win
, &protocols
, &n
))
94 for(i
= 0; !ret
&& i
< n
; i
++)
95 if(protocols
[i
] == XInternAtom(disp
, "WM_DELETE_WINDOW", False
))
102 /** Swap two client in the linked list clients
103 * \param head pointer ito the client list head
104 * \param c1 first client
105 * \param c2 second client
108 client_swap(Client
**head
, Client
*c1
, Client
*c2
)
114 c2
->next
= (tmp
== c2
? c1
: tmp
);
118 c1
->prev
= (tmp
== c1
? c2
: tmp
);
136 /** Get a Client by its window
137 * \param list Client list to look info
138 * \param w Client window to find
142 get_client_bywin(Client
*list
, Window w
)
146 for(c
= list
; c
&& c
->win
!= w
; c
= c
->next
);
151 client_updatetitle(Client
*c
)
153 if(!xgettextprop(c
->display
, c
->win
, XInternAtom(c
->display
, "_NET_WM_NAME", False
), c
->name
, sizeof(c
->name
)))
154 xgettextprop(c
->display
, c
->win
, XInternAtom(c
->display
, "WM_NAME", False
), c
->name
, sizeof(c
->name
));
157 /** Ban client and unmap it
158 * \param c the client
161 client_ban(Client
* c
)
163 XUnmapWindow(c
->display
, c
->win
);
164 window_setstate(c
->display
, c
->win
, IconicState
);
167 /** Attach client to the beginning of the clients stack
168 * \param c the client
171 client_attach(Client
*c
)
173 if(globalconf
.clients
)
174 (globalconf
.clients
)->prev
= c
;
175 c
->next
= globalconf
.clients
;
176 globalconf
.clients
= c
;
179 /** Detach client from clients list
180 * \param c client to detach
183 client_detach(Client
*c
)
186 c
->prev
->next
= c
->next
;
188 c
->next
->prev
= c
->prev
;
189 if(c
== globalconf
.clients
)
190 globalconf
.clients
= c
->next
;
191 c
->next
= c
->prev
= NULL
;
194 /** Give focus to client, or to first client if c is NULL
196 * \param selscreen True if current screen is selected
197 * \param screen Screen ID
200 focus(Client
*c
, Bool selscreen
, int screen
)
202 /* unfocus current selected client */
203 if(globalconf
.focus
->client
)
205 window_grabbuttons(globalconf
.focus
->client
->display
, globalconf
.focus
->client
->phys_screen
,
206 globalconf
.focus
->client
->win
, False
, True
);
207 XSetWindowBorder(globalconf
.focus
->client
->display
, globalconf
.focus
->client
->win
,
208 globalconf
.screens
[screen
].colors_normal
[ColBorder
].pixel
);
209 window_settrans(globalconf
.focus
->client
->display
, globalconf
.focus
->client
->win
,
210 globalconf
.screens
[screen
].opacity_unfocused
);
213 /* if c is NULL or invisible, take next client in the stack */
214 if((!c
&& selscreen
) || (c
&& !client_isvisible(c
, screen
)))
215 for(c
= globalconf
.clients
; c
&& !client_isvisible(c
, screen
); c
= c
->next
);
219 XSetWindowBorder(globalconf
.display
, c
->win
, globalconf
.screens
[screen
].colors_selected
[ColBorder
].pixel
);
220 window_grabbuttons(c
->display
, c
->phys_screen
, c
->win
,
227 /* save old sel in focus history */
230 statusbar_draw(screen
);
232 if(globalconf
.focus
->client
)
234 XSetInputFocus(globalconf
.focus
->client
->display
,
235 globalconf
.focus
->client
->win
, RevertToPointerRoot
, CurrentTime
);
236 for(c
= globalconf
.clients
; c
; c
= c
->next
)
237 if(c
!= globalconf
.focus
->client
)
238 window_settrans(globalconf
.display
, globalconf
.focus
->client
->win
,
239 globalconf
.screens
[screen
].opacity_unfocused
);
240 window_settrans(globalconf
.display
, globalconf
.focus
->client
->win
, -1);
243 XSetInputFocus(globalconf
.display
,
244 RootWindow(globalconf
.display
, get_phys_screen(screen
)),
245 RevertToPointerRoot
, CurrentTime
);
248 /** Manage a new client
249 * \param w The window
250 * \param wa Window attributes
251 * \param screen Screen ID
254 client_manage(Window w
, XWindowAttributes
*wa
, int screen
)
256 Client
*c
, *t
= NULL
;
260 ScreenInfo
*screen_info
;
263 c
= p_new(Client
, 1);
266 c
->x
= c
->rx
= wa
->x
;
267 c
->y
= c
->ry
= wa
->y
;
268 c
->w
= c
->rw
= wa
->width
;
269 c
->h
= c
->rh
= wa
->height
;
270 c
->oldborder
= wa
->border_width
;
272 c
->display
= globalconf
.display
;
273 c
->screen
= get_screen_bycoord(c
->x
, c
->y
);
274 c
->phys_screen
= get_phys_screen(c
->screen
);
276 move_client_to_screen(c
, screen
, True
);
278 /* update window title */
279 client_updatetitle(c
);
281 /* loadprops or apply rules if no props */
282 if(!client_loadprops(c
, screen
))
283 tag_client_with_rules(c
);
285 screen_info
= get_screen_info(screen
, NULL
, NULL
);
287 /* if window request fullscreen mode */
288 if(c
->w
== screen_info
[screen
].width
&& c
->h
== screen_info
[screen
].height
)
290 c
->x
= screen_info
[screen
].x_org
;
291 c
->y
= screen_info
[screen
].y_org
;
293 c
->border
= wa
->border_width
;
297 ScreenInfo
*display_info
= get_display_info(c
->phys_screen
,
298 globalconf
.screens
[screen
].statusbar
,
299 &globalconf
.screens
[screen
].padding
);
301 if(c
->x
+ c
->w
+ 2 * c
->border
> display_info
->x_org
+ display_info
->width
)
302 c
->x
= c
->rx
= display_info
->x_org
+ display_info
->width
- c
->w
- 2 * c
->border
;
303 if(c
->y
+ c
->h
+ 2 * c
->border
> display_info
->y_org
+ display_info
->height
)
304 c
->y
= c
->ry
= display_info
->y_org
+ display_info
->height
- c
->h
- 2 * c
->border
;
305 if(c
->x
< display_info
->x_org
)
306 c
->x
= c
->rx
= display_info
->x_org
;
307 if(c
->y
< display_info
->y_org
)
308 c
->y
= c
->ry
= display_info
->y_org
;
310 c
->border
= globalconf
.screens
[screen
].borderpx
;
312 p_delete(&display_info
);
314 p_delete(&screen_info
);
317 wc
.border_width
= c
->border
;
318 XConfigureWindow(c
->display
, w
, CWBorderWidth
, &wc
);
319 XSetWindowBorder(c
->display
, w
, globalconf
.screens
[screen
].colors_normal
[ColBorder
].pixel
);
321 /* propagates border_width, if size doesn't change */
322 window_configure(c
->display
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
, c
->border
);
324 /* update sizehint */
325 client_updatesizehints(c
);
327 XSelectInput(c
->display
, w
, StructureNotifyMask
| PropertyChangeMask
| EnterWindowMask
);
330 if(globalconf
.have_shape
)
332 XShapeSelectInput(c
->display
, w
, ShapeNotifyMask
);
333 window_setshape(c
->display
, c
->phys_screen
, c
->win
);
337 window_grabbuttons(c
->display
, c
->phys_screen
, c
->win
, False
, True
);
339 /* check for transient and set tags like its parent */
340 if((rettrans
= XGetTransientForHint(c
->display
, w
, &trans
) == Success
)
341 && (t
= get_client_bywin(globalconf
.clients
, trans
)))
342 for(tag
= globalconf
.screens
[c
->screen
].tags
; tag
; tag
= tag
->next
)
343 if(is_client_tagged(t
, tag
, c
->screen
))
344 tag_client(c
, tag
, c
->screen
);
346 /* should be floating if transsient or fixed) */
348 c
->isfloating
= (rettrans
== Success
) || c
->isfixed
;
351 client_saveprops(c
, c
->screen
);
353 /* attach to the stack */
356 /* some windows require this */
357 XMoveResizeWindow(c
->display
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
359 focus(c
, True
, screen
);
361 /* rearrange to display new window */
366 client_resize(Client
*c
, int x
, int y
, int w
, int h
,
367 Bool sizehints
, Bool volatile_coords
)
369 double dx
, dy
, max
, min
, ratio
;
375 if(c
->minay
> 0 && c
->maxay
> 0 && (h
- c
->baseh
) > 0 && (w
- c
->basew
) > 0)
377 dx
= (double) (w
- c
->basew
);
378 dy
= (double) (h
- c
->baseh
);
379 min
= (double) (c
->minax
) / (double) (c
->minay
);
380 max
= (double) (c
->maxax
) / (double) (c
->maxay
);
382 if(max
> 0 && min
> 0 && ratio
> 0)
386 dy
= (dx
* min
+ dy
) / (min
* min
+ 1);
388 w
= (int) dx
+ c
->basew
;
389 h
= (int) dy
+ c
->baseh
;
393 dy
= (dx
* min
+ dy
) / (max
* max
+ 1);
395 w
= (int) dx
+ c
->basew
;
396 h
= (int) dy
+ c
->baseh
;
400 if(c
->minw
&& w
< c
->minw
)
402 if(c
->minh
&& h
< c
->minh
)
404 if(c
->maxw
&& w
> c
->maxw
)
406 if(c
->maxh
&& h
> c
->maxh
)
409 w
-= (w
- c
->basew
) % c
->incw
;
411 h
-= (h
- c
->baseh
) % c
->inch
;
415 /* offscreen appearance fixes */
416 si
= get_display_info(c
->phys_screen
,
418 &globalconf
.screens
[c
->screen
].padding
);
420 x
= si
->width
- w
- 2 * c
->border
;
422 y
= si
->height
- h
- 2 * c
->border
;
424 if(x
+ w
+ 2 * c
->border
< 0)
426 if(y
+ h
+ 2 * c
->border
< 0)
428 if(c
->x
!= x
|| c
->y
!= y
|| c
->w
!= w
|| c
->h
!= h
)
433 c
->h
= wc
.height
= h
;
436 || get_current_layout(c
->screen
)->arrange
== layout_floating
))
443 wc
.border_width
= c
->border
;
444 XConfigureWindow(c
->display
, c
->win
, CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
, &wc
);
445 window_configure(c
->display
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
, c
->border
);
446 XSync(c
->display
, False
);
447 if((c
->x
>= 0 || c
->y
>= 0) && XineramaIsActive(c
->display
))
449 int new_screen
= get_screen_bycoord(c
->x
, c
->y
);
450 if(c
->screen
!= new_screen
)
451 move_client_to_screen(c
, new_screen
, False
);
457 client_saveprops(Client
* c
, int screen
)
459 int i
= 0, ntags
= 0;
463 for(tag
= globalconf
.screens
[screen
].tags
; tag
; tag
= tag
->next
)
466 prop
= p_new(char, ntags
+ 2);
468 for(tag
= globalconf
.screens
[screen
].tags
; tag
; tag
= tag
->next
, i
++)
469 prop
[i
] = is_client_tagged(c
, tag
, screen
) ? '1' : '0';
472 prop
[i
] = c
->isfloating
? '1' : '0';
476 XChangeProperty(c
->display
, c
->win
, AWESOMEPROPS_ATOM(c
->display
), XA_STRING
, 8,
477 PropModeReplace
, (unsigned char *) prop
, i
);
483 client_unban(Client
*c
)
485 XMapWindow(c
->display
, c
->win
);
486 window_setstate(c
->display
, c
->win
, NormalState
);
490 client_unmanage(Client
*c
, long state
)
495 wc
.border_width
= c
->oldborder
;
496 /* The server grab construct avoids race conditions. */
497 XGrabServer(c
->display
);
498 XConfigureWindow(c
->display
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
500 if(globalconf
.focus
->client
== c
)
501 focus(NULL
, True
, c
->screen
);
502 focus_delete_client(c
);
503 for(tag
= globalconf
.screens
[c
->screen
].tags
; tag
; tag
= tag
->next
)
504 untag_client(c
, tag
, c
->screen
);
505 XUngrabButton(c
->display
, AnyButton
, AnyModifier
, c
->win
);
506 window_setstate(c
->display
, c
->win
, state
);
507 XSync(c
->display
, False
);
508 XSetErrorHandler(xerror
);
509 XUngrabServer(c
->display
);
510 if(state
!= NormalState
)
516 client_updatesizehints(Client
*c
)
521 if(!XGetWMNormalHints(c
->display
, c
->win
, &size
, &msize
) || !size
.flags
)
523 c
->flags
= size
.flags
;
524 if(c
->flags
& PBaseSize
)
526 c
->basew
= size
.base_width
;
527 c
->baseh
= size
.base_height
;
529 else if(c
->flags
& PMinSize
)
531 c
->basew
= size
.min_width
;
532 c
->baseh
= size
.min_height
;
535 c
->basew
= c
->baseh
= 0;
536 if(c
->flags
& PResizeInc
)
538 c
->incw
= size
.width_inc
;
539 c
->inch
= size
.height_inc
;
542 c
->incw
= c
->inch
= 0;
544 if(c
->flags
& PMaxSize
)
546 c
->maxw
= size
.max_width
;
547 c
->maxh
= size
.max_height
;
550 c
->maxw
= c
->maxh
= 0;
552 if(c
->flags
& PMinSize
)
554 c
->minw
= size
.min_width
;
555 c
->minh
= size
.min_height
;
557 else if(c
->flags
& PBaseSize
)
559 c
->minw
= size
.base_width
;
560 c
->minh
= size
.base_height
;
563 c
->minw
= c
->minh
= 0;
565 if(c
->flags
& PAspect
)
567 c
->minax
= size
.min_aspect
.x
;
568 c
->maxax
= size
.max_aspect
.x
;
569 c
->minay
= size
.min_aspect
.y
;
570 c
->maxay
= size
.max_aspect
.y
;
573 c
->minax
= c
->maxax
= c
->minay
= c
->maxay
= 0;
575 c
->isfixed
= (c
->maxw
&& c
->minw
&& c
->maxh
&& c
->minh
576 && c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
579 /** Returns True if a client is tagged
580 * with one of the tags
581 * \return True or False
584 client_isvisible(Client
*c
, int screen
)
588 if(c
->screen
!= screen
)
591 for(tag
= globalconf
.screens
[screen
].tags
; tag
; tag
= tag
->next
)
592 if(tag
->selected
&& is_client_tagged(c
, tag
, screen
))
597 /** Set selected client transparency
598 * \param screen Screen ID
599 * \param arg unused arg
600 * \ingroup ui_callback
603 uicb_client_settrans(int screen
__attribute__ ((unused
)), char *arg
)
605 double delta
= 100.0, current_opacity
= 100.0;
609 unsigned long n
, left
;
610 unsigned int current_opacity_raw
= 0;
612 Client
*sel
= globalconf
.focus
->client
;
617 XGetWindowProperty(globalconf
.display
, sel
->win
,
618 XInternAtom(sel
->display
, "_NET_WM_WINDOW_OPACITY", False
),
619 0L, 1L, False
, XA_CARDINAL
, &actual
, &format
, &n
, &left
,
620 (unsigned char **) &data
);
623 memcpy(¤t_opacity_raw
, data
, sizeof(unsigned int));
625 current_opacity
= (current_opacity_raw
* 100.0) / 0xffffffff;
630 delta
= compute_new_value_from_arg(arg
, current_opacity
);
634 else if(delta
> 100.0)
640 if(delta
== 100.0 && !set_prop
)
641 window_settrans(sel
->display
, sel
->win
, -1);
643 window_settrans(sel
->display
, sel
->win
, delta
);
648 * \param screen Screen ID
649 * \param arg X, +X or -X
650 * \ingroup ui_callback
653 uicb_setborder(int screen
, char *arg
)
658 if((globalconf
.screens
[screen
].borderpx
= (int) compute_new_value_from_arg(arg
, (double) globalconf
.screens
[screen
].borderpx
)) < 0)
659 globalconf
.screens
[screen
].borderpx
= 0;
662 /** Swap current with next client
663 * \param screen Screen ID
665 * \ingroup ui_callback
668 uicb_client_swapnext(int screen
, char *arg
__attribute__ ((unused
)))
670 Client
*next
, *sel
= globalconf
.focus
->client
;
675 for(next
= sel
->next
; next
&& !client_isvisible(next
, screen
); next
= next
->next
);
678 client_swap(&globalconf
.clients
, sel
, next
);
681 focus(sel
, True
, screen
);
685 /** Swap current with previous client
686 * \param screen Screen ID
688 * \ingroup ui_callback
691 uicb_client_swapprev(int screen
, char *arg
__attribute__ ((unused
)))
693 Client
*prev
, *sel
= globalconf
.focus
->client
;
698 for(prev
= sel
->prev
; prev
&& !client_isvisible(prev
, screen
); prev
= prev
->prev
);
701 client_swap(&globalconf
.clients
, prev
, sel
);
704 focus(sel
, True
, screen
);
708 /** Move and resize client
709 * \param screen Screen ID
711 * \ingroup ui_callback
714 uicb_client_moveresize(int screen
, char *arg
)
716 int nx
, ny
, nw
, nh
, ox
, oy
, ow
, oh
;
717 char x
[8], y
[8], w
[8], h
[8];
718 int mx
, my
, dx
, dy
, nmx
, nmy
;
721 Client
*sel
= globalconf
.focus
->client
;
723 if(get_current_layout(screen
)->arrange
!= layout_floating
)
724 if(!sel
|| !sel
->isfloating
|| sel
->isfixed
|| !arg
)
726 if(sscanf(arg
, "%s %s %s %s", x
, y
, w
, h
) != 4)
728 nx
= (int) compute_new_value_from_arg(x
, sel
->x
);
729 ny
= (int) compute_new_value_from_arg(y
, sel
->y
);
730 nw
= (int) compute_new_value_from_arg(w
, sel
->w
);
731 nh
= (int) compute_new_value_from_arg(h
, sel
->h
);
738 Bool xqp
= XQueryPointer(globalconf
.display
,
739 RootWindow(globalconf
.display
,
740 get_phys_screen(screen
)),
741 &dummy
, &dummy
, &mx
, &my
, &dx
, &dy
, &dui
);
742 client_resize(sel
, nx
, ny
, nw
, nh
, True
, False
);
743 if (xqp
&& ox
<= mx
&& (ox
+ ow
) >= mx
&& oy
<= my
&& (oy
+ oh
) >= my
)
745 nmx
= mx
- ox
+ sel
->w
- ow
- 1 < 0 ? 0 : mx
- ox
+ sel
->w
- ow
- 1;
746 nmy
= my
- oy
+ sel
->h
- oh
- 1 < 0 ? 0 : my
- oy
+ sel
->h
- oh
- 1;
747 XWarpPointer(globalconf
.display
,
749 0, 0, 0, 0, nmx
, nmy
);
753 /** Kill selected client
754 * \param screen Screen ID
756 * \ingroup ui_callback
759 uicb_client_kill(int screen
__attribute__ ((unused
)), char *arg
__attribute__ ((unused
)))
762 Client
*sel
= globalconf
.focus
->client
;
766 if(isprotodel(sel
->display
, sel
->win
))
768 ev
.type
= ClientMessage
;
769 ev
.xclient
.window
= sel
->win
;
770 ev
.xclient
.message_type
= XInternAtom(globalconf
.display
, "WM_PROTOCOLS", False
);
771 ev
.xclient
.format
= 32;
772 ev
.xclient
.data
.l
[0] = XInternAtom(globalconf
.display
, "WM_DELETE_WINDOW", False
);
773 ev
.xclient
.data
.l
[1] = CurrentTime
;
774 XSendEvent(globalconf
.display
, sel
->win
, False
, NoEventMask
, &ev
);
777 XKillClient(globalconf
.display
, sel
->win
);
779 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80