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.
25 #include <xcb/xcb_atom.h>
40 #include "layouts/floating.h"
41 #include "common/markup.h"
42 #include "common/atoms.h"
44 extern awesome_t globalconf
;
46 /** Create a new client userdata.
47 * \param L The Lua VM state.
48 * \param p A client pointer.
49 * \param The number of elements pushed on the stack.
52 luaA_client_userdata_new(lua_State
*L
, client_t
*p
)
54 client_t
**pp
= lua_newuserdata(L
, sizeof(client_t
*));
57 return luaA_settype(L
, "client");
60 DO_LUA_EQ(client_t
, client
, "client")
61 DO_LUA_GC(client_t
, client
, "client", client_unref
)
63 /** Load windows properties, restoring client's tag
64 * and floating state before awesome was restarted if any.
65 * \param c A client pointer.
66 * \param screen A virtual screen.
67 * \return True if client had property, false otherwise.
70 client_loadprops(client_t
* c
, screen_t
*screen
)
73 tag_array_t
*tags
= &screen
->tags
;
75 xcb_get_property_cookie_t floating_q
, fullscreen_q
;
76 xcb_get_property_reply_t
*reply
;
79 if(!xutil_text_prop_get(globalconf
.connection
, c
->win
, _AWESOME_TAGS
,
83 /* Send the GetProperty requests which will be processed later */
84 floating_q
= xcb_get_property_unchecked(globalconf
.connection
, false, c
->win
,
85 _AWESOME_FLOATING
, CARDINAL
, 0, 1);
87 fullscreen_q
= xcb_get_property_unchecked(globalconf
.connection
, false, c
->win
,
88 _AWESOME_FULLSCREEN
, CARDINAL
, 0, 1);
90 /* ignore property if the tag count isn't matching */
92 for(int i
= 0; i
< tags
->len
; i
++)
94 tag_client(c
, tags
->tab
[i
]);
96 untag_client(c
, tags
->tab
[i
]);
100 /* check for floating */
101 reply
= xcb_get_property_reply(globalconf
.connection
, floating_q
, NULL
);
103 if(reply
&& reply
->value_len
&& (data
= xcb_get_property_value(reply
)))
104 client_setfloating(c
, *(bool *) data
);
107 /* check for fullscreen */
108 reply
= xcb_get_property_reply(globalconf
.connection
, fullscreen_q
, NULL
);
110 if(reply
&& reply
->value_len
&& (data
= xcb_get_property_value(reply
)))
111 client_setfullscreen(c
, *(bool *) data
);
117 /** Check if client supports protocol a protocole in WM_PROTOCOL.
118 * \param win The window.
119 * \return True if client has the atom in protocol, false otherwise.
122 window_hasproto(xcb_window_t win
, xcb_atom_t atom
)
125 xcb_get_wm_protocols_reply_t protocols
;
128 if(xcb_get_wm_protocols_reply(globalconf
.connection
,
129 xcb_get_wm_protocols_unchecked(globalconf
.connection
,
133 for(i
= 0; !ret
&& i
< protocols
.atoms_len
; i
++)
134 if(protocols
.atoms
[i
] == atom
)
136 xcb_get_wm_protocols_reply_wipe(&protocols
);
141 /** Returns true if a client is tagged
142 * with one of the tags of the specified screen.
143 * \param c The client to check.
144 * \param screen Virtual screen number.
145 * \return true if the client is visible, false otherwise.
148 client_maybevisible(client_t
*c
, int screen
)
150 if(c
->screen
== screen
)
152 if(c
->issticky
|| c
->type
== WINDOW_TYPE_DESKTOP
)
155 tag_array_t
*tags
= &globalconf
.screens
[screen
].tags
;
157 for(int i
= 0; i
< tags
->len
; i
++)
158 if(tags
->tab
[i
]->selected
&& is_client_tagged(c
, tags
->tab
[i
]))
164 /** Get a client by its window.
165 * \param w The client window to find.
166 * \return A client pointer if found, NULL otherwise.
169 client_getbywin(xcb_window_t w
)
172 for(c
= globalconf
.clients
; c
&& c
->win
!= w
; c
= c
->next
);
176 /** Unfocus a client.
177 * \param c The client.
180 client_unfocus(client_t
*c
)
182 globalconf
.screens
[c
->phys_screen
].client_focus
= NULL
;
185 luaA_client_userdata_new(globalconf
.L
, c
);
186 luaA_dofunction(globalconf
.L
, globalconf
.hooks
.unfocus
, 1, 0);
188 widget_invalidate_cache(c
->screen
, WIDGET_CACHE_CLIENTS
);
189 ewmh_update_net_active_window(c
->phys_screen
);
192 /** Ban client and unmap it.
193 * \param c The client.
196 client_ban(client_t
*c
)
198 if(globalconf
.screen_focus
->client_focus
== c
)
200 xcb_unmap_window(globalconf
.connection
, c
->win
);
202 window_state_set(c
->win
, XCB_WM_STATE_ICONIC
);
204 window_state_set(c
->win
, XCB_WM_STATE_WITHDRAWN
);
206 xcb_unmap_window(globalconf
.connection
, c
->titlebar
->sw
.window
);
209 /** Give focus to client, or to first client if client is NULL.
210 * \param c The client or NULL.
211 * \return True if a window (even root) has received focus, false otherwise.
214 client_focus(client_t
*c
)
216 if(!client_maybevisible(c
, c
->screen
) || c
->nofocus
)
219 /* unfocus current selected client */
220 if(globalconf
.screen_focus
->client_focus
221 && c
!= globalconf
.screen_focus
->client_focus
)
222 client_unfocus(globalconf
.screen_focus
->client_focus
);
226 client_setminimized(c
, false);
228 /* unban the client before focusing or it will fail */
231 globalconf
.screen_focus
= &globalconf
.screens
[c
->phys_screen
];
232 globalconf
.screen_focus
->client_focus
= c
;
234 xcb_set_input_focus(globalconf
.connection
, XCB_INPUT_FOCUS_POINTER_ROOT
,
235 c
->win
, XCB_CURRENT_TIME
);
237 /* Some layouts use focused client differently, so call them back.
238 * And anyway, we have maybe unhidden */
239 client_need_arrange(c
);
242 luaA_client_userdata_new(globalconf
.L
, globalconf
.screen_focus
->client_focus
);
243 luaA_dofunction(globalconf
.L
, globalconf
.hooks
.focus
, 1, 0);
245 ewmh_update_net_active_window(c
->phys_screen
);
246 widget_invalidate_cache(c
->screen
, WIDGET_CACHE_CLIENTS
);
249 /** Stack a window below.
250 * \param c The client.
251 * \param previous The previous window on the stack.
252 * \param return The next-previous!
255 client_stack_below(client_t
*c
, xcb_window_t previous
)
257 uint32_t config_win_vals
[2];
259 config_win_vals
[0] = previous
;
260 config_win_vals
[1] = XCB_STACK_MODE_BELOW
;
264 xcb_configure_window(globalconf
.connection
,
265 c
->titlebar
->sw
.window
,
266 XCB_CONFIG_WINDOW_SIBLING
| XCB_CONFIG_WINDOW_STACK_MODE
,
268 config_win_vals
[0] = c
->titlebar
->sw
.window
;
270 xcb_configure_window(globalconf
.connection
, c
->win
,
271 XCB_CONFIG_WINDOW_SIBLING
| XCB_CONFIG_WINDOW_STACK_MODE
,
273 config_win_vals
[0] = c
->win
;
278 /** Get the real layer of a client according to its attribute (fullscreen, …)
279 * \param c The client.
280 * \return The real layer.
283 client_layer_translator(client_t
*c
)
289 else if(c
->isfullscreen
)
290 return LAYER_FULLSCREEN
;
293 else if(c
->isfloating
)
298 case WINDOW_TYPE_DOCK
:
300 case WINDOW_TYPE_SPLASH
:
301 case WINDOW_TYPE_DIALOG
:
303 case WINDOW_TYPE_DESKTOP
:
304 return LAYER_DESKTOP
;
311 * \todo It might be worth stopping to restack everyone and only stack `c'
312 * relatively to the first matching in the list.
317 uint32_t config_win_vals
[2];
322 config_win_vals
[0] = XCB_NONE
;
323 config_win_vals
[1] = XCB_STACK_MODE_BELOW
;
325 /* first stack modal and fullscreen windows */
326 for(layer
= LAYER_OUTOFSPACE
- 1; layer
>= LAYER_FULLSCREEN
; layer
--)
327 for(node
= globalconf
.stack
; node
; node
= node
->next
)
328 if(client_layer_translator(node
->client
) == layer
)
329 config_win_vals
[0] = client_stack_below(node
->client
, config_win_vals
[0]);
331 /* then stack ontop wibox window */
332 for(screen
= 0; screen
< globalconf
.nscreen
; screen
++)
333 for(int i
= 0; i
< globalconf
.screens
[screen
].wiboxes
.len
; i
++)
335 wibox_t
*sb
= globalconf
.screens
[screen
].wiboxes
.tab
[i
];
338 xcb_configure_window(globalconf
.connection
,
340 XCB_CONFIG_WINDOW_SIBLING
| XCB_CONFIG_WINDOW_STACK_MODE
,
342 config_win_vals
[0] = sb
->sw
.window
;
346 /* finally stack everything else */
347 for(layer
= LAYER_FULLSCREEN
- 1; layer
>= LAYER_TILE
; layer
--)
348 for(node
= globalconf
.stack
; node
; node
= node
->next
)
349 if(client_layer_translator(node
->client
) == layer
)
350 config_win_vals
[0] = client_stack_below(node
->client
, config_win_vals
[0]);
352 /* then stack not ontop wibox window */
353 for(screen
= 0; screen
< globalconf
.nscreen
; screen
++)
354 for(int i
= 0; i
< globalconf
.screens
[screen
].wiboxes
.len
; i
++)
356 wibox_t
*sb
= globalconf
.screens
[screen
].wiboxes
.tab
[i
];
359 xcb_configure_window(globalconf
.connection
,
361 XCB_CONFIG_WINDOW_SIBLING
| XCB_CONFIG_WINDOW_STACK_MODE
,
363 config_win_vals
[0] = sb
->sw
.window
;
367 /* finally stack everything else */
368 for(layer
= LAYER_TILE
- 1; layer
>= LAYER_DESKTOP
; layer
--)
369 for(node
= globalconf
.stack
; node
; node
= node
->next
)
370 if(client_layer_translator(node
->client
) == layer
)
371 config_win_vals
[0] = client_stack_below(node
->client
, config_win_vals
[0]);
374 /** Manage a new client.
375 * \param w The window.
376 * \param wgeom Window geometry.
377 * \param phys_screen Physical screen number.
378 * \param screen Virtual screen number where to manage client.
381 client_manage(xcb_window_t w
, xcb_get_geometry_reply_t
*wgeom
, int phys_screen
, int screen
)
383 xcb_get_property_cookie_t ewmh_icon_cookie
;
386 const uint32_t select_input_val
[] =
388 XCB_EVENT_MASK_STRUCTURE_NOTIFY
389 | XCB_EVENT_MASK_PROPERTY_CHANGE
390 | XCB_EVENT_MASK_ENTER_WINDOW
393 /* Send request to get NET_WM_ICON property as soon as possible... */
394 ewmh_icon_cookie
= ewmh_window_icon_get_unchecked(w
);
395 xcb_change_window_attributes(globalconf
.connection
, w
, XCB_CW_EVENT_MASK
, select_input_val
);
397 if(systray_iskdedockapp(w
))
399 systray_request_handle(w
, phys_screen
, NULL
);
403 c
= p_new(client_t
, 1);
405 c
->screen
= screen_getbycoord(screen
, wgeom
->x
, wgeom
->y
);
407 c
->phys_screen
= phys_screen
;
411 c
->geometry
.x
= c
->f_geometry
.x
= c
->m_geometry
.x
= wgeom
->x
;
412 c
->geometry
.y
= c
->f_geometry
.y
= c
->m_geometry
.y
= wgeom
->y
;
413 c
->geometry
.width
= c
->f_geometry
.width
= c
->m_geometry
.width
= wgeom
->width
;
414 c
->geometry
.height
= c
->f_geometry
.height
= c
->m_geometry
.height
= wgeom
->height
;
415 client_setborder(c
, wgeom
->border_width
);
416 if((icon
= ewmh_window_icon_get_reply(ewmh_icon_cookie
)))
417 c
->icon
= image_ref(&icon
);
419 /* we honor size hints by default */
420 c
->honorsizehints
= true;
423 property_update_wm_normal_hints(c
, NULL
);
424 property_update_wm_hints(c
, NULL
);
425 property_update_wm_transient_for(c
, NULL
);
428 screen
= c
->transient_for
->screen
;
430 /* Try to load props if any */
431 client_loadprops(c
, &globalconf
.screens
[screen
]);
433 /* move client to screen, but do not tag it for now */
434 screen_client_moveto(c
, screen
, false, true);
436 /* Then check clients hints */
437 ewmh_client_check_hints(c
);
439 /* Check if client has been tagged by loading props, or maybe with its
441 * If not, we tag it with current selected ones.
442 * This could be done on Lua side, but it's a sane behaviour. */
446 tag_array_t
*tags
= &globalconf
.screens
[screen
].tags
;
447 for(i
= 0; i
< tags
->len
; i
++)
448 if(is_client_tagged(c
, tags
->tab
[i
]))
451 /* if no tag, set current selected */
453 for(i
= 0; i
< tags
->len
; i
++)
454 if(tags
->tab
[i
]->selected
)
455 tag_client(c
, tags
->tab
[i
]);
458 /* Push client in client list */
459 client_list_push(&globalconf
.clients
, client_ref(&c
));
461 /* Push client in stack */
464 /* update window title */
465 property_update_wm_name(c
);
468 ewmh_client_strut_update(c
, NULL
);
470 ewmh_update_net_client_list(c
->phys_screen
);
471 widget_invalidate_cache(c
->screen
, WIDGET_CACHE_CLIENTS
);
473 /* Call hook to notify list change */
474 luaA_dofunction(globalconf
.L
, globalconf
.hooks
.clients
, 0, 0);
477 luaA_client_userdata_new(globalconf
.L
, c
);
478 luaA_dofunction(globalconf
.L
, globalconf
.hooks
.manage
, 1, 0);
481 /** Compute client geometry with respect to its geometry hints.
482 * \param c The client.
483 * \param geometry The geometry that the client might receive.
484 * \return The geometry the client must take respecting its hints.
487 client_geometry_hints(client_t
*c
, area_t geometry
)
489 double dx
, dy
, max
, min
, ratio
;
491 if(c
->minay
> 0 && c
->maxay
> 0 && (geometry
.height
- c
->baseh
) > 0
492 && (geometry
.width
- c
->basew
) > 0)
494 dx
= (double) (geometry
.width
- c
->basew
);
495 dy
= (double) (geometry
.height
- c
->baseh
);
496 min
= (double) (c
->minax
) / (double) (c
->minay
);
497 max
= (double) (c
->maxax
) / (double) (c
->maxay
);
499 if(max
> 0 && min
> 0 && ratio
> 0)
503 dy
= (dx
* min
+ dy
) / (min
* min
+ 1);
505 geometry
.width
= (int) dx
+ c
->basew
;
506 geometry
.height
= (int) dy
+ c
->baseh
;
510 dy
= (dx
* min
+ dy
) / (max
* max
+ 1);
512 geometry
.width
= (int) dx
+ c
->basew
;
513 geometry
.height
= (int) dy
+ c
->baseh
;
517 if(c
->minw
&& geometry
.width
< c
->minw
)
518 geometry
.width
= c
->minw
;
519 if(c
->minh
&& geometry
.height
< c
->minh
)
520 geometry
.height
= c
->minh
;
521 if(c
->maxw
&& geometry
.width
> c
->maxw
)
522 geometry
.width
= c
->maxw
;
523 if(c
->maxh
&& geometry
.height
> c
->maxh
)
524 geometry
.height
= c
->maxh
;
526 geometry
.width
-= (geometry
.width
- c
->basew
) % c
->incw
;
528 geometry
.height
-= (geometry
.height
- c
->baseh
) % c
->inch
;
533 /** Resize client window.
534 * \param c Client to resize.
535 * \param geometry New window geometry.
536 * \param hints Use size hints.
539 client_resize(client_t
*c
, area_t geometry
, bool hints
)
543 layout_t
*layout
= layout_get_current(c
->screen
);
545 /* Values to configure a window is an array where values are
546 * stored according to 'value_mask' */
549 if(c
->titlebar
&& !c
->ismoving
&& !client_isfloating(c
) && layout
!= layout_floating
)
550 geometry
= titlebar_geometry_remove(c
->titlebar
, c
->border
, geometry
);
553 geometry
= client_geometry_hints(c
, geometry
);
555 if(geometry
.width
<= 0 || geometry
.height
<= 0)
558 /* offscreen appearance fixes */
559 area
= display_area_get(c
->phys_screen
, NULL
,
560 &globalconf
.screens
[c
->screen
].padding
);
562 fixed
= client_isfixed(c
);
564 if(geometry
.x
> area
.width
)
565 geometry
.x
= area
.width
- geometry
.width
- 2 * c
->border
;
566 if(geometry
.y
> area
.height
)
567 geometry
.y
= area
.height
- geometry
.height
- 2 * c
->border
;
568 if(geometry
.x
+ geometry
.width
+ 2 * c
->border
< 0)
570 if(geometry
.y
+ geometry
.height
+ 2 * c
->border
< 0)
573 /* fixed windows can only change their x,y */
574 if((fixed
&& (c
->geometry
.x
!= geometry
.x
|| c
->geometry
.y
!= geometry
.y
))
575 || (!fixed
&& (c
->geometry
.x
!= geometry
.x
576 || c
->geometry
.y
!= geometry
.y
577 || c
->geometry
.width
!= geometry
.width
578 || c
->geometry
.height
!= geometry
.height
)))
580 new_screen
= screen_getbycoord(c
->screen
, geometry
.x
, geometry
.y
);
582 c
->geometry
.x
= values
[0] = geometry
.x
;
583 c
->geometry
.width
= values
[2] = geometry
.width
;
584 c
->geometry
.y
= values
[1] = geometry
.y
;
585 c
->geometry
.height
= values
[3] = geometry
.height
;
586 values
[4] = c
->border
;
588 /* save the floating geometry if the window is floating but not
590 if(c
->ismoving
|| client_isfloating(c
)
591 || layout_get_current(new_screen
) == layout_floating
)
593 c
->f_geometry
= geometry
;
595 titlebar_update_geometry_floating(c
);
597 xcb_configure_window(globalconf
.connection
, c
->win
,
598 XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
599 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
|
600 XCB_CONFIG_WINDOW_BORDER_WIDTH
,
602 window_configure(c
->win
, geometry
, c
->border
);
604 if(c
->screen
!= new_screen
)
605 screen_client_moveto(c
, new_screen
, true, false);
608 hooks_property(c
, "geometry");
612 /** Set a clinet floating.
613 * \param c The client.
614 * \param floating Set floating, or not.
615 * \param layer Layer to put the floating window onto.
618 client_setfloating(client_t
*c
, bool floating
)
620 if(c
->isfloating
!= floating
621 && (c
->type
== WINDOW_TYPE_NORMAL
))
623 if((c
->isfloating
= floating
))
625 client_resize(c
, c
->f_geometry
, false);
626 client_need_arrange(c
);
627 widget_invalidate_cache(c
->screen
, WIDGET_CACHE_CLIENTS
);
629 xcb_change_property(globalconf
.connection
,
630 XCB_PROP_MODE_REPLACE
,
631 c
->win
, _AWESOME_FLOATING
, CARDINAL
, 8, 1,
634 hooks_property(c
, "floating");
638 /** Set a client minimized, or not.
639 * \param c The client.
640 * \param s Set or not the client minimized.
643 client_setminimized(client_t
*c
, bool s
)
645 if(c
->isminimized
!= s
)
647 client_need_arrange(c
);
649 client_need_arrange(c
);
650 ewmh_client_update_hints(c
);
652 hooks_property(c
, "minimized");
656 /** Set a client sticky, or not.
657 * \param c The client.
658 * \param s Set or not the client sticky.
661 client_setsticky(client_t
*c
, bool s
)
665 client_need_arrange(c
);
667 client_need_arrange(c
);
668 ewmh_client_update_hints(c
);
669 hooks_property(c
, "sticky");
673 /** Set a client fullscreen, or not.
674 * \param c The client.
675 * \param s Set or not the client fullscreen.
678 client_setfullscreen(client_t
*c
, bool s
)
680 if(c
->isfullscreen
!= s
)
684 /* become fullscreen! */
685 if((c
->isfullscreen
= s
))
687 geometry
= screen_area_get(c
->screen
, NULL
, &globalconf
.screens
[c
->screen
].padding
, false);
688 c
->m_geometry
= c
->geometry
;
689 c
->oldborder
= c
->border
;
690 client_setborder(c
, 0);
694 geometry
= c
->m_geometry
;
695 client_setborder(c
, c
->oldborder
);
696 client_resize(c
, c
->m_geometry
, false);
698 client_resize(c
, geometry
, false);
699 client_need_arrange(c
);
701 xcb_change_property(globalconf
.connection
,
702 XCB_PROP_MODE_REPLACE
,
703 c
->win
, _AWESOME_FULLSCREEN
, CARDINAL
, 8, 1,
705 ewmh_client_update_hints(c
);
706 hooks_property(c
, "fullscreen");
710 /** Set a client above, or not.
711 * \param c The client.
712 * \param s Set or not the client above.
715 client_setabove(client_t
*c
, bool s
)
721 ewmh_client_update_hints(c
);
723 hooks_property(c
, "above");
727 /** Set a client below, or not.
728 * \param c The client.
729 * \param s Set or not the client below.
732 client_setbelow(client_t
*c
, bool s
)
738 ewmh_client_update_hints(c
);
740 hooks_property(c
, "below");
744 /** Set a client modal, or not.
745 * \param c The client.
746 * \param s Set or not the client moda.
749 client_setmodal(client_t
*c
, bool s
)
755 ewmh_client_update_hints(c
);
757 hooks_property(c
, "modal");
761 /** Set a client ontop, or not.
762 * \param c The client.
763 * \param s Set or not the client moda.
766 client_setontop(client_t
*c
, bool s
)
773 hooks_property(c
, "ontop");
777 /** Save client properties as an X property.
778 * \param c The client.
781 client_saveprops_tags(client_t
*c
)
783 tag_array_t
*tags
= &globalconf
.screens
[c
->screen
].tags
;
784 unsigned char *prop
= p_alloca(unsigned char, tags
->len
+ 1);
787 for(i
= 0; i
< tags
->len
; i
++)
788 prop
[i
] = is_client_tagged(c
, tags
->tab
[i
]) ? '1' : '0';
790 xcb_change_property(globalconf
.connection
, XCB_PROP_MODE_REPLACE
, c
->win
, _AWESOME_TAGS
, STRING
, 8, i
, prop
);
794 * \param c The client.
797 client_unban(client_t
*c
)
799 xcb_map_window(globalconf
.connection
, c
->win
);
800 window_state_set(c
->win
, XCB_WM_STATE_NORMAL
);
803 if(c
->isfullscreen
|| !c
->titlebar
->isvisible
)
804 xcb_unmap_window(globalconf
.connection
, c
->titlebar
->sw
.window
);
806 xcb_map_window(globalconf
.connection
, c
->titlebar
->sw
.window
);
810 /** Unmanage a client.
811 * \param c The client.
814 client_unmanage(client_t
*c
)
816 tag_array_t
*tags
= &globalconf
.screens
[c
->screen
].tags
;
818 if(globalconf
.screens
[c
->phys_screen
].client_focus
== c
)
821 /* remove client everywhere */
822 client_list_detach(&globalconf
.clients
, c
);
823 stack_client_delete(c
);
824 for(int i
= 0; i
< tags
->len
; i
++)
825 untag_client(c
, tags
->tab
[i
]);
828 luaA_client_userdata_new(globalconf
.L
, c
);
829 luaA_dofunction(globalconf
.L
, globalconf
.hooks
.unmanage
, 1, 0);
831 /* Call hook to notify list change */
832 luaA_dofunction(globalconf
.L
, globalconf
.hooks
.clients
, 0, 0);
834 /* The server grab construct avoids race conditions. */
835 xcb_grab_server(globalconf
.connection
);
837 xcb_configure_window(globalconf
.connection
, c
->win
,
838 XCB_CONFIG_WINDOW_BORDER_WIDTH
,
839 (uint32_t *) &c
->oldborder
);
841 xcb_ungrab_button(globalconf
.connection
, XCB_BUTTON_INDEX_ANY
, c
->win
,
842 XCB_BUTTON_MASK_ANY
);
843 window_state_set(c
->win
, XCB_WM_STATE_WITHDRAWN
);
845 xcb_flush(globalconf
.connection
);
846 xcb_ungrab_server(globalconf
.connection
);
848 titlebar_client_detach(c
);
850 ewmh_update_net_client_list(c
->phys_screen
);
852 /* delete properties */
853 xcb_delete_property(globalconf
.connection
, c
->win
, _AWESOME_TAGS
);
854 xcb_delete_property(globalconf
.connection
, c
->win
, _AWESOME_FLOATING
);
856 if(client_hasstrut(c
))
857 /* All the wiboxes (may) need to be repositioned */
858 for(int screen
= 0; screen
< globalconf
.nscreen
; screen
++)
859 for(int i
= 0; i
< globalconf
.screens
[screen
].wiboxes
.len
; i
++)
861 wibox_t
*s
= globalconf
.screens
[screen
].wiboxes
.tab
[i
];
862 wibox_position_update(s
);
865 /* set client as invalid */
871 /** Kill a client via a WM_DELETE_WINDOW request or KillClient if not
873 * \param c The client to kill.
876 client_kill(client_t
*c
)
878 if(window_hasproto(c
->win
, WM_DELETE_WINDOW
))
880 xcb_client_message_event_t ev
;
882 /* Initialize all of event's fields first */
885 ev
.response_type
= XCB_CLIENT_MESSAGE
;
888 ev
.data
.data32
[1] = XCB_CURRENT_TIME
;
889 ev
.type
= WM_PROTOCOLS
;
890 ev
.data
.data32
[0] = WM_DELETE_WINDOW
;
892 xcb_send_event(globalconf
.connection
, false, c
->win
,
893 XCB_EVENT_MASK_NO_EVENT
, (char *) &ev
);
896 xcb_kill_client(globalconf
.connection
, c
->win
);
899 /** Get all clients into a table.
900 * \param L The Lua VM state.
901 * \return The number of elements pushed on stack.
903 * \lparam An optional screen nunmber.
904 * \lreturn A table with all clients.
907 luaA_client_get(lua_State
*L
)
912 screen
= luaL_optnumber(L
, 1, 0) - 1;
916 if(screen
== SCREEN_UNDEF
)
917 for(c
= globalconf
.clients
; c
; c
= c
->next
)
919 luaA_client_userdata_new(globalconf
.L
, c
);
920 lua_rawseti(L
, -2, i
++);
924 luaA_checkscreen(screen
);
925 for(c
= globalconf
.clients
; c
; c
= c
->next
)
926 if(c
->screen
== screen
)
928 luaA_client_userdata_new(globalconf
.L
, c
);
929 lua_rawseti(L
, -2, i
++);
936 /** Get only visible clients for a screen (DEPRECATED).
937 * \param L The Lua VM state.
938 * \return The number of elements pushed on stack.
940 * \lparam A screen number.
941 * \lreturn A table with all visible clients for this screen.
944 luaA_client_visible_get(lua_State
*L
)
948 int screen
= luaL_checknumber(L
, 1) - 1;
950 luaA_checkscreen(screen
);
956 for(c
= globalconf
.clients
; c
; c
= c
->next
)
957 if(client_isvisible(c
, screen
))
959 luaA_client_userdata_new(L
, c
);
960 lua_rawseti(L
, -2, i
++);
966 /** Check if a client is visible on its screen.
967 * \param L The Lua VM state.
968 * \return The number of elements pushed on stack.
971 * \lreturn A boolean value, true if the client is visible, false otherwise.
974 luaA_client_isvisible(lua_State
*L
)
976 client_t
**c
= luaA_checkudata(L
, 1, "client");
977 lua_pushboolean(L
, client_isvisible(*c
, (*c
)->screen
));
981 /** Set client border width.
982 * \param c The client.
983 * \param width The border width.
986 client_setborder(client_t
*c
, int width
)
990 if(width
> 0 && (c
->type
== WINDOW_TYPE_DOCK
991 || c
->type
== WINDOW_TYPE_SPLASH
992 || c
->type
== WINDOW_TYPE_DESKTOP
996 if(width
== c
->border
|| width
< 0)
1000 xcb_configure_window(globalconf
.connection
, c
->win
,
1001 XCB_CONFIG_WINDOW_BORDER_WIDTH
, &w
);
1003 if(client_isvisible(c
, c
->screen
))
1005 if(client_isfloating(c
) || layout_get_current(c
->screen
) == layout_floating
)
1006 titlebar_update_geometry_floating(c
);
1008 globalconf
.screens
[c
->screen
].need_arrange
= true;
1011 hooks_property(c
, "border_width");
1015 * \param L The Lua VM state.
1021 luaA_client_kill(lua_State
*L
)
1023 client_t
**c
= luaA_checkudata(L
, 1, "client");
1028 /** Swap a client with another one.
1029 * \param L The Lua VM state.
1032 * \lparam A client to swap with.
1035 luaA_client_swap(lua_State
*L
)
1037 client_t
**c
= luaA_checkudata(L
, 1, "client");
1038 client_t
**swap
= luaA_checkudata(L
, 2, "client");
1039 client_list_swap(&globalconf
.clients
, *swap
, *c
);
1040 client_need_arrange(*c
);
1041 client_need_arrange(*swap
);
1042 widget_invalidate_cache((*c
)->screen
, WIDGET_CACHE_CLIENTS
);
1043 widget_invalidate_cache((*swap
)->screen
, WIDGET_CACHE_CLIENTS
);
1045 /* Call hook to notify list change */
1046 luaA_dofunction(L
, globalconf
.hooks
.clients
, 0, 0);
1051 /** Access or set the client tags.
1052 * \param L The Lua VM state.
1053 * \return The number of elements pushed on stack.
1054 * \lparam A table with tags to set, or none to get the current tags table.
1055 * \return The clients tag.
1058 luaA_client_tags(lua_State
*L
)
1062 client_t
**c
= luaA_checkudata(L
, 1, "client");
1065 if(lua_gettop(L
) == 2)
1067 luaA_checktable(L
, 2);
1068 tags
= &globalconf
.screens
[(*c
)->screen
].tags
;
1069 for(int i
= 0; i
< tags
->len
; i
++)
1070 untag_client(*c
, tags
->tab
[i
]);
1072 while(lua_next(L
, 2))
1074 tag
= luaA_checkudata(L
, -1, "tag");
1075 tag_client(*c
, *tag
);
1081 tags
= &globalconf
.screens
[(*c
)->screen
].tags
;
1083 for(int i
= 0; i
< tags
->len
; i
++)
1084 if(is_client_tagged(*c
, tags
->tab
[i
]))
1086 luaA_tag_userdata_new(L
, tags
->tab
[i
]);
1087 lua_rawseti(L
, -2, ++j
);
1093 /** Raise a client on top of others which are on the same layer.
1094 * \param L The Lua VM state.
1100 luaA_client_raise(lua_State
*L
)
1102 client_t
**c
= luaA_checkudata(L
, 1, "client");
1107 /** Redraw a client by unmapping and mapping it quickly.
1108 * \param L The Lua VM state.
1114 luaA_client_redraw(lua_State
*L
)
1116 client_t
**c
= luaA_checkudata(L
, 1, "client");
1118 xcb_unmap_window(globalconf
.connection
, (*c
)->win
);
1119 xcb_map_window(globalconf
.connection
, (*c
)->win
);
1121 /* Set the focus on the current window if the redraw has been
1122 performed on the window where the pointer is currently on
1123 because after the unmapping/mapping, the focus is lost */
1124 if(globalconf
.screen_focus
->client_focus
== *c
)
1125 xcb_set_input_focus(globalconf
.connection
, XCB_INPUT_FOCUS_POINTER_ROOT
,
1126 (*c
)->win
, XCB_CURRENT_TIME
);
1131 /** Return a formated string for a client.
1132 * \param L The Lua VM state.
1135 * \lreturn A string.
1138 luaA_client_tostring(lua_State
*L
)
1140 client_t
**p
= luaA_checkudata(L
, 1, "client");
1141 lua_pushfstring(L
, "[client udata(%p) name(%s)]", *p
, (*p
)->name
);
1145 /** Stop managing a client.
1146 * \param L The Lua VM state.
1147 * \return The number of elements pushed on stack.
1152 luaA_client_unmanage(lua_State
*L
)
1154 client_t
**c
= luaA_checkudata(L
, 1, "client");
1155 client_unmanage(*c
);
1159 /** Return client coordinates.
1160 * \param L The Lua VM state.
1161 * \return The number of elements pushed on stack.
1163 * \lparam A table with new coordinates, or none.
1164 * \lreturn A table with client coordinates.
1167 luaA_client_coords(lua_State
*L
)
1169 client_t
**c
= luaA_checkudata(L
, 1, "client");
1171 if(lua_gettop(L
) == 2)
1173 if((*c
)->isfloating
|| layout_get_current((*c
)->screen
) == layout_floating
)
1177 luaA_checktable(L
, 2);
1178 geometry
.x
= luaA_getopt_number(L
, 2, "x", (*c
)->geometry
.x
);
1179 geometry
.y
= luaA_getopt_number(L
, 2, "y", (*c
)->geometry
.y
);
1180 geometry
.width
= luaA_getopt_number(L
, 2, "width", (*c
)->geometry
.width
);
1181 geometry
.height
= luaA_getopt_number(L
, 2, "height", (*c
)->geometry
.height
);
1182 client_resize(*c
, geometry
, false);
1186 return luaA_pusharea(L
, (*c
)->geometry
);
1189 /** Return client coordinates, using also titlebar and border width.
1190 * \param L The Lua VM state.
1191 * \return The number of elements pushed on stack.
1193 * \lparam A table with new coordinates, or none.
1194 * \lreturn A table with client coordinates.
1197 luaA_client_fullcoords(lua_State
*L
)
1199 client_t
**c
= luaA_checkudata(L
, 1, "client");
1202 if(lua_gettop(L
) == 2)
1204 if((*c
)->isfloating
|| layout_get_current((*c
)->screen
) == layout_floating
)
1206 luaA_checktable(L
, 2);
1207 geometry
.x
= luaA_getopt_number(L
, 2, "x", (*c
)->geometry
.x
);
1208 geometry
.y
= luaA_getopt_number(L
, 2, "y", (*c
)->geometry
.y
);
1209 geometry
.width
= luaA_getopt_number(L
, 2, "width", (*c
)->geometry
.width
);
1210 geometry
.height
= luaA_getopt_number(L
, 2, "height", (*c
)->geometry
.height
);
1211 geometry
= titlebar_geometry_remove((*c
)->titlebar
,
1214 client_resize(*c
, geometry
, false);
1218 return luaA_pusharea(L
, titlebar_geometry_add((*c
)->titlebar
,
1223 /** Client newindex.
1224 * \param L The Lua VM state.
1225 * \return The number of elements pushed on stack.
1228 luaA_client_newindex(lua_State
*L
)
1231 client_t
**c
= luaA_checkudata(L
, 1, "client");
1232 const char *buf
= luaL_checklstring(L
, 2, &len
);
1240 luaL_error(L
, "client is invalid\n");
1242 switch(a_tokenize(buf
, len
))
1245 if(globalconf
.xinerama_is_active
)
1247 i
= luaL_checknumber(L
, 3) - 1;
1248 luaA_checkscreen(i
);
1249 if(i
!= (*c
)->screen
)
1250 screen_client_moveto(*c
, i
, true, true);
1254 b
= luaA_checkboolean(L
, 3);
1255 if(b
!= (*c
)->ishidden
)
1257 client_need_arrange(*c
);
1259 client_need_arrange(*c
);
1263 client_setminimized(*c
, luaA_checkboolean(L
, 3));
1265 case A_TK_FULLSCREEN
:
1266 client_setfullscreen(*c
, luaA_checkboolean(L
, 3));
1269 image
= luaA_checkudata(L
, 3, "image");
1270 image_unref(&(*c
)->icon
);
1272 (*c
)->icon
= *image
;
1273 widget_invalidate_cache((*c
)->screen
, WIDGET_CACHE_CLIENTS
);
1277 window_opacity_set((*c
)->win
, -1);
1280 d
= luaL_checknumber(L
, 3);
1281 if(d
>= 0 && d
<= 1)
1282 window_opacity_set((*c
)->win
, d
);
1286 client_setfloating(*c
, luaA_checkboolean(L
, 3));
1289 client_setsticky(*c
, luaA_checkboolean(L
, 3));
1291 case A_TK_HONORSIZEHINTS
:
1292 (*c
)->honorsizehints
= luaA_checkboolean(L
, 3);
1293 client_need_arrange(*c
);
1295 case A_TK_BORDER_WIDTH
:
1296 client_setborder(*c
, luaL_checknumber(L
, 3));
1299 client_setontop(*c
, luaA_checkboolean(L
, 3));
1301 case A_TK_BORDER_COLOR
:
1302 if((buf
= luaL_checklstring(L
, 3, &len
))
1303 && xcolor_init_reply(xcolor_init_unchecked(&(*c
)->border_color
, buf
, len
)))
1304 xcb_change_window_attributes(globalconf
.connection
, (*c
)->win
,
1305 XCB_CW_BORDER_PIXEL
, &(*c
)->border_color
.pixel
);
1309 titlebar_client_detach(*c
);
1312 t
= luaA_checkudata(L
, 3, "wibox");
1313 titlebar_client_attach(*c
, *t
);
1324 * \param L The Lua VM state.
1325 * \return The number of elements pushed on stack.
1327 * \lfield name The client title.
1328 * \lfield skip_taskbar True if the client does not want to be in taskbar.
1329 * \lfield type The window type (desktop, normal, dock, …).
1330 * \lfield class The client class.
1331 * \lfield instance The client instance.
1332 * \lfield pid The client PID, if available.
1333 * \lfield role The window role, if available.
1334 * \lfield machine The machine client is running on.
1335 * \lfield icon_name The client name when iconified.
1336 * \lfield screen Client screen number.
1337 * \lfield hide Define if the client must be hidden, i.e. never mapped,
1338 * invisible in taskbar.
1339 * \lfield minimize Define it the client must be iconify, i.e. only visible in
1341 * \lfield icon_path Path to the icon used to identify.
1342 * \lfield floating True always floating.
1343 * \lfield honorsizehints Honor size hints, i.e. respect size ratio.
1344 * \lfield border_width The client border width.
1345 * \lfield border_color The client border color.
1346 * \lfield titlebar The client titlebar.
1347 * \lfield urgent The client urgent state.
1348 * \lfield focus The focused client.
1349 * \lfield opacity The client opacity between 0 and 1.
1350 * \lfield ontop The client is on top of every other windows.
1351 * \lfield fullscreen The client is fullscreen or not.
1354 luaA_client_index(lua_State
*L
)
1358 client_t
**c
= luaA_checkudata(L
, 1, "client");
1359 const char *buf
= luaL_checklstring(L
, 2, &len
);
1362 xcb_get_wm_class_reply_t hint
;
1363 xcb_get_property_cookie_t prop_c
;
1364 xcb_get_property_reply_t
*prop_r
= NULL
;
1368 luaL_error(L
, "client is invalid\n");
1370 if(luaA_usemetatable(L
, 1, 2))
1373 switch(a_tokenize(buf
, len
))
1376 lua_pushstring(L
, (*c
)->name
);
1378 case A_TK_SKIP_TASKBAR
:
1379 lua_pushboolean(L
, (*c
)->skiptb
);
1384 case WINDOW_TYPE_DESKTOP
:
1385 lua_pushliteral(L
, "desktop");
1387 case WINDOW_TYPE_DOCK
:
1388 lua_pushliteral(L
, "dock");
1390 case WINDOW_TYPE_SPLASH
:
1391 lua_pushliteral(L
, "splash");
1393 case WINDOW_TYPE_DIALOG
:
1394 lua_pushliteral(L
, "dialog");
1397 lua_pushliteral(L
, "normal");
1402 if(!xcb_get_wm_class_reply(globalconf
.connection
,
1403 xcb_get_wm_class_unchecked(globalconf
.connection
, (*c
)->win
),
1406 lua_pushstring(L
, hint
.class);
1407 xcb_get_wm_class_reply_wipe(&hint
);
1410 if(!xcb_get_wm_class_reply(globalconf
.connection
,
1411 xcb_get_wm_class_unchecked(globalconf
.connection
, (*c
)->win
),
1414 lua_pushstring(L
, hint
.name
);
1415 xcb_get_wm_class_reply_wipe(&hint
);
1418 if(!xutil_text_prop_get(globalconf
.connection
, (*c
)->win
,
1419 WM_WINDOW_ROLE
, &value
, &slen
))
1421 lua_pushlstring(L
, value
, slen
);
1425 prop_c
= xcb_get_property_unchecked(globalconf
.connection
, false, (*c
)->win
, _NET_WM_PID
, CARDINAL
, 0L, 1L);
1426 prop_r
= xcb_get_property_reply(globalconf
.connection
, prop_c
, NULL
);
1428 if(prop_r
&& prop_r
->value_len
&& (data
= xcb_get_property_value(prop_r
)))
1429 lua_pushnumber(L
, *(uint32_t *)data
);
1437 if(!xutil_text_prop_get(globalconf
.connection
, (*c
)->win
,
1438 WM_CLIENT_MACHINE
, &value
, &slen
))
1440 lua_pushlstring(L
, value
, slen
);
1443 case A_TK_ICON_NAME
:
1444 if(!xutil_text_prop_get(globalconf
.connection
, (*c
)->win
,
1445 _NET_WM_ICON_NAME
, &value
, &slen
))
1446 if(!xutil_text_prop_get(globalconf
.connection
, (*c
)->win
,
1447 WM_ICON_NAME
, &value
, &slen
))
1449 lua_pushlstring(L
, value
, slen
);
1453 lua_pushnumber(L
, 1 + (*c
)->screen
);
1456 lua_pushboolean(L
, (*c
)->ishidden
);
1459 lua_pushboolean(L
, (*c
)->isminimized
);
1461 case A_TK_FULLSCREEN
:
1462 lua_pushboolean(L
, (*c
)->isfullscreen
);
1466 luaA_image_userdata_new(L
, (*c
)->icon
);
1471 if((d
= window_opacity_get((*c
)->win
)) >= 0)
1472 lua_pushnumber(L
, d
);
1477 lua_pushboolean(L
, (*c
)->isfloating
);
1480 lua_pushboolean(L
, (*c
)->isontop
);
1483 lua_pushboolean(L
, (*c
)->issticky
);
1485 case A_TK_HONORSIZEHINTS
:
1486 lua_pushboolean(L
, (*c
)->honorsizehints
);
1488 case A_TK_BORDER_WIDTH
:
1489 lua_pushnumber(L
, (*c
)->border
);
1491 case A_TK_BORDER_COLOR
:
1492 luaA_pushcolor(L
, &(*c
)->border_color
);
1496 return luaA_wibox_userdata_new(L
, (*c
)->titlebar
);
1499 lua_pushboolean(L
, (*c
)->isurgent
);
1501 case A_TK_SIZEHINTS
:
1503 lua_pushboolean(L
, (*c
)->size_hints
.flags
& XCB_SIZE_HINT_US_POSITION
);
1504 lua_setfield(L
, -2, "user_position");
1505 lua_pushboolean(L
, (*c
)->size_hints
.flags
& XCB_SIZE_HINT_US_SIZE
);
1506 lua_setfield(L
, -2, "user_size");
1507 lua_pushboolean(L
, (*c
)->size_hints
.flags
& XCB_SIZE_HINT_P_POSITION
);
1508 lua_setfield(L
, -2, "program_position");
1509 lua_pushboolean(L
, (*c
)->size_hints
.flags
& XCB_SIZE_HINT_P_SIZE
);
1510 lua_setfield(L
, -2, "program_size");
1519 /** Get or set mouse buttons bindings for a client.
1520 * \param L The Lua VM state.
1521 * \return The number of element pushed on stack.
1524 * \lparam An array of mouse button bindings objects, or nothing.
1525 * \return The array of mouse button bindings objects of this client.
1528 luaA_client_buttons(lua_State
*L
)
1530 client_t
**client
= luaA_checkudata(L
, 1, "client");
1531 button_array_t
*buttons
= &(*client
)->buttons
;
1533 if(lua_gettop(L
) == 2)
1534 luaA_button_array_set(L
, 2, buttons
);
1536 return luaA_button_array_get(L
, buttons
);
1540 * \param L The Lua VM state.
1541 * \return The number of pushed elements.
1544 luaA_client_module_index(lua_State
*L
)
1547 const char *buf
= luaL_checklstring(L
, 2, &len
);
1549 switch(a_tokenize(buf
, len
))
1552 if(globalconf
.screen_focus
->client_focus
)
1553 luaA_client_userdata_new(L
, globalconf
.screen_focus
->client_focus
);
1564 /* Client module new index.
1565 * \param L The Lua VM state.
1566 * \return The number of pushed elements.
1569 luaA_client_module_newindex(lua_State
*L
)
1572 const char *buf
= luaL_checklstring(L
, 2, &len
);
1575 switch(a_tokenize(buf
, len
))
1578 c
= luaA_checkudata(L
, 3, "client");
1588 const struct luaL_reg awesome_client_methods
[] =
1590 { "get", luaA_client_get
},
1591 { "visible_get", luaA_client_visible_get
},
1592 { "__index", luaA_client_module_index
},
1593 { "__newindex", luaA_client_module_newindex
},
1596 const struct luaL_reg awesome_client_meta
[] =
1598 { "isvisible", luaA_client_isvisible
},
1599 { "coords", luaA_client_coords
},
1600 { "fullcoords", luaA_client_fullcoords
},
1601 { "buttons", luaA_client_buttons
},
1602 { "tags", luaA_client_tags
},
1603 { "kill", luaA_client_kill
},
1604 { "swap", luaA_client_swap
},
1605 { "raise", luaA_client_raise
},
1606 { "redraw", luaA_client_redraw
},
1607 { "mouse_resize", luaA_client_mouse_resize
},
1608 { "mouse_move", luaA_client_mouse_move
},
1609 { "unmanage", luaA_client_unmanage
},
1610 { "__index", luaA_client_index
},
1611 { "__newindex", luaA_client_newindex
},
1612 { "__eq", luaA_client_eq
},
1613 { "__gc", luaA_client_gc
},
1614 { "__tostring", luaA_client_tostring
},
1618 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80