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>
26 #include <xcb/xcb_icccm.h>
39 #include "layouts/floating.h"
40 #include "common/markup.h"
41 #include "common/atoms.h"
43 extern awesome_t globalconf
;
44 extern const name_func_link_t FloatingPlacementList
[];
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
;
76 if(!xutil_text_prop_get(globalconf
.connection
, c
->win
, _AWESOME_PROPERTIES
,
80 if(len
!= tags
->len
+ 2)
82 /* ignore property if the tag count isn't matching */
87 for(int i
= 0; i
< tags
->len
; i
++)
89 tag_client(c
, tags
->tab
[i
]);
91 untag_client(c
, tags
->tab
[i
]);
93 client_setlayer(c
, prop
[tags
->len
+ 1] - '0');
94 client_setfloating(c
, prop
[tags
->len
] == '1');
99 /** Check if client supports protocol WM_DELETE_WINDOW.
100 * \param win The window.
101 * \return True if client has WM_DELETE_WINDOW, false otherwise.
104 window_isprotodel(xcb_window_t win
)
107 xcb_atom_t
*protocols
;
110 if(xcb_get_wm_protocols(globalconf
.connection
, win
, &n
, &protocols
))
112 for(i
= 0; !ret
&& i
< n
; i
++)
113 if(protocols
[i
] == WM_DELETE_WINDOW
)
115 p_delete(&protocols
);
120 /** Returns true if a client is tagged
121 * with one of the tags of the specified screen.
122 * \param c The client to check.
123 * \param screen Virtual screen number.
124 * \return true if the client is visible, false otherwise.
127 client_maybevisible(client_t
*c
, int screen
)
129 if(c
->screen
== screen
)
131 tag_array_t
*tags
= &globalconf
.screens
[screen
].tags
;
132 for(int i
= 0; i
< tags
->len
; i
++)
133 if(tags
->tab
[i
]->selected
&& is_client_tagged(c
, tags
->tab
[i
]))
139 /** Returns true if a client is tagged
140 * with one of the tags of the specified screen and is not hidden.
141 * \param c The client to check.
142 * \param screen Virtual screen number.
143 * \return true if the client is visible, false otherwise.
146 client_isvisible(client_t
*c
, int screen
)
148 return (!c
->ishidden
&& client_maybevisible(c
, screen
));
151 /** Get a client by its window.
152 * \param w The client window to find.
153 * \return A client pointer if found, NULL otherwise.
156 client_getbywin(xcb_window_t w
)
159 for(c
= globalconf
.clients
; c
&& c
->win
!= w
; c
= c
->next
);
163 /** Update client name attribute with its new title.
164 * \param c The client.
165 * \param Return true if it has been updated.
168 client_updatetitle(client_t
*c
)
173 if(!xutil_text_prop_get(globalconf
.connection
, c
->win
, _NET_WM_NAME
, &name
, &len
))
174 if(!xutil_text_prop_get(globalconf
.connection
, c
->win
, WM_NAME
, &name
, &len
))
179 if((utf8
= draw_iso2utf8(name
, len
)))
185 luaA_client_userdata_new(globalconf
.L
, c
);
186 luaA_dofunction(globalconf
.L
, globalconf
.hooks
.titleupdate
, 1, 0);
188 widget_invalidate_cache(c
->screen
, WIDGET_CACHE_CLIENTS
);
193 /** Unfocus a client.
194 * \param c The client.
197 client_unfocus(client_t
*c
)
199 globalconf
.screens
[c
->screen
].client_focus
= NULL
;
202 luaA_client_userdata_new(globalconf
.L
, c
);
203 luaA_dofunction(globalconf
.L
, globalconf
.hooks
.unfocus
, 1, 0);
205 widget_invalidate_cache(c
->screen
, WIDGET_CACHE_CLIENTS
);
206 ewmh_update_net_active_window(c
->phys_screen
);
209 /** Ban client and unmap it.
210 * \param c The client.
213 client_ban(client_t
*c
)
215 if(globalconf
.screen_focus
->client_focus
== c
)
217 xcb_unmap_window(globalconf
.connection
, c
->win
);
219 window_state_set(c
->win
, XCB_WM_ICONIC_STATE
);
221 window_state_set(c
->win
, XCB_WM_WITHDRAWN_STATE
);
222 if(c
->titlebar
&& c
->titlebar
->position
&& c
->titlebar
->sw
)
223 xcb_unmap_window(globalconf
.connection
, c
->titlebar
->sw
->window
);
226 /** Give focus to client, or to first client if client is NULL.
227 * \param c The client or NULL.
228 * \return True if a window (even root) has received focus, false otherwise.
231 client_focus(client_t
*c
)
233 if(!client_maybevisible(c
, c
->screen
))
236 /* unfocus current selected client */
237 if(globalconf
.screen_focus
->client_focus
238 && c
!= globalconf
.screen_focus
->client_focus
)
239 client_unfocus(globalconf
.screen_focus
->client_focus
);
244 /* unban the client before focusing or it will fail */
247 globalconf
.screen_focus
= &globalconf
.screens
[c
->screen
];
248 globalconf
.screen_focus
->client_focus
= c
;
250 xcb_set_input_focus(globalconf
.connection
, XCB_INPUT_FOCUS_POINTER_ROOT
,
251 c
->win
, XCB_CURRENT_TIME
);
253 /* Some layouts use focused client differently, so call them back.
254 * And anyway, we have maybe unhidden */
255 globalconf
.screens
[c
->screen
].need_arrange
= true;
258 luaA_client_userdata_new(globalconf
.L
, globalconf
.screen_focus
->client_focus
);
259 luaA_dofunction(globalconf
.L
, globalconf
.hooks
.focus
, 1, 0);
261 ewmh_update_net_active_window(c
->phys_screen
);
262 widget_invalidate_cache(c
->screen
, WIDGET_CACHE_CLIENTS
);
266 * \todo It might be worth stopping to restack everyone and only stack `c'
267 * relatively to the first matching in the list.
272 uint32_t config_win_vals
[2];
277 xembed_window_t
*emwin
;
279 config_win_vals
[0] = XCB_NONE
;
280 config_win_vals
[1] = XCB_STACK_MODE_BELOW
;
282 /* first stack fullscreen and modal windows */
283 for(layer
= LAYER_OUTOFSPACE
- 1; layer
>= LAYER_FULLSCREEN
; layer
--)
284 for(node
= globalconf
.stack
; node
; node
= node
->next
)
285 if(node
->client
->layer
== layer
)
287 if(node
->client
->titlebar
288 && node
->client
->titlebar
->sw
289 && node
->client
->titlebar
->position
)
291 xcb_configure_window(globalconf
.connection
,
292 node
->client
->titlebar
->sw
->window
,
293 XCB_CONFIG_WINDOW_SIBLING
| XCB_CONFIG_WINDOW_STACK_MODE
,
295 config_win_vals
[0] = node
->client
->titlebar
->sw
->window
;
297 xcb_configure_window(globalconf
.connection
, node
->client
->win
,
298 XCB_CONFIG_WINDOW_SIBLING
| XCB_CONFIG_WINDOW_STACK_MODE
,
300 config_win_vals
[0] = node
->client
->win
;
303 /* then stack systray windows */
304 for(emwin
= globalconf
.embedded
; emwin
; emwin
= emwin
->next
)
306 xcb_configure_window(globalconf
.connection
,
308 XCB_CONFIG_WINDOW_SIBLING
| XCB_CONFIG_WINDOW_STACK_MODE
,
310 config_win_vals
[0] = emwin
->win
;
313 /* then stack statusbar window */
314 for(screen
= 0; screen
< globalconf
.screens_info
->nscreen
; screen
++)
315 for(sb
= globalconf
.screens
[screen
].statusbar
; sb
; sb
= sb
->next
)
318 xcb_configure_window(globalconf
.connection
,
320 XCB_CONFIG_WINDOW_SIBLING
| XCB_CONFIG_WINDOW_STACK_MODE
,
322 config_win_vals
[0] = sb
->sw
->window
;
325 /* finally stack everything else */
326 for(layer
= LAYER_FULLSCREEN
- 1; layer
>= LAYER_DESKTOP
; layer
--)
327 for(node
= globalconf
.stack
; node
; node
= node
->next
)
328 if(node
->client
->layer
== layer
)
330 if(node
->client
->titlebar
331 && node
->client
->titlebar
->sw
332 && node
->client
->titlebar
->position
)
334 xcb_configure_window(globalconf
.connection
,
335 node
->client
->titlebar
->sw
->window
,
336 XCB_CONFIG_WINDOW_SIBLING
| XCB_CONFIG_WINDOW_STACK_MODE
,
338 config_win_vals
[0] = node
->client
->titlebar
->sw
->window
;
340 xcb_configure_window(globalconf
.connection
, node
->client
->win
,
341 XCB_CONFIG_WINDOW_SIBLING
| XCB_CONFIG_WINDOW_STACK_MODE
,
343 config_win_vals
[0] = node
->client
->win
;
347 /** Put client on top of the stack
348 * \param c The client to raise.
351 client_raise(client_t
*c
)
353 /* Push c on top of the stack. */
354 stack_client_push(c
);
358 /** Manage a new client.
359 * \param w The window.
360 * \param wgeom Window geometry.
361 * \param screen Virtual screen number where to manage client.
364 client_manage(xcb_window_t w
, xcb_get_geometry_reply_t
*wgeom
, int screen
)
366 xcb_get_property_cookie_t ewmh_icon_cookie
;
367 client_t
*c
, *t
= NULL
;
369 bool rettrans
, retloadprops
;
370 xcb_size_hints_t
*u_size_hints
;
371 const uint32_t select_input_val
[] =
373 XCB_EVENT_MASK_STRUCTURE_NOTIFY
374 | XCB_EVENT_MASK_PROPERTY_CHANGE
375 | XCB_EVENT_MASK_ENTER_WINDOW
378 /* Send request to get NET_WM_ICON property as soon as possible... */
379 ewmh_icon_cookie
= ewmh_window_icon_get_unchecked(w
);
380 xcb_change_window_attributes(globalconf
.connection
, w
, XCB_CW_EVENT_MASK
, select_input_val
);
382 if(systray_iskdedockapp(w
))
384 systray_request_handle(w
, screen_virttophys(screen
), NULL
);
388 c
= p_new(client_t
, 1);
390 c
->screen
= screen_get_bycoord(globalconf
.screens_info
, screen
, wgeom
->x
, wgeom
->y
);
392 if(globalconf
.screens_info
->xinerama_is_active
)
393 c
->phys_screen
= globalconf
.default_screen
;
395 c
->phys_screen
= c
->screen
;
399 c
->geometry
.x
= c
->f_geometry
.x
= c
->m_geometry
.x
= wgeom
->x
;
400 c
->geometry
.y
= c
->f_geometry
.y
= c
->m_geometry
.y
= wgeom
->y
;
401 c
->geometry
.width
= c
->f_geometry
.width
= c
->m_geometry
.width
= wgeom
->width
;
402 c
->geometry
.height
= c
->f_geometry
.height
= c
->m_geometry
.height
= wgeom
->height
;
403 c
->layer
= c
->oldlayer
= LAYER_TILE
;
404 client_setborder(c
, wgeom
->border_width
);
405 c
->icon
= ewmh_window_icon_get_reply(ewmh_icon_cookie
);
408 u_size_hints
= client_updatesizehints(c
);
409 client_updatewmhints(c
);
411 /* Try to load props if any */
412 if(!(retloadprops
= client_loadprops(c
, &globalconf
.screens
[screen
])))
413 screen_client_moveto(c
, screen
, true);
415 /* Then check clients hints */
416 ewmh_check_client_hints(c
);
418 /* check for transient and set tags like its parent */
419 if((rettrans
= xcb_get_wm_transient_for(globalconf
.connection
, w
, &trans
))
420 && (t
= client_getbywin(trans
)))
422 tag_array_t
*tags
= &globalconf
.screens
[c
->screen
].tags
;
423 for(int i
= 0; i
< tags
->len
; i
++)
424 if(is_client_tagged(t
, tags
->tab
[i
]))
425 tag_client(c
, tags
->tab
[i
]);
428 /* should be floating if transsient or fixed */
429 if(rettrans
|| c
->isfixed
)
430 client_setfloating(c
, true);
432 /* Push client in client list */
433 client_list_push(&globalconf
.clients
, c
);
435 /* Push client in stack */
438 /* update window title */
439 client_updatetitle(c
);
441 ewmh_update_net_client_list(c
->phys_screen
);
442 widget_invalidate_cache(c
->screen
, WIDGET_CACHE_CLIENTS
);
445 luaA_client_userdata_new(globalconf
.L
, c
);
446 luaA_dofunction(globalconf
.L
, globalconf
.hooks
.manage
, 1, 0);
448 if(c
->floating_placement
451 && !(xcb_size_hints_get_flags(u_size_hints
) & (XCB_SIZE_US_POSITION_HINT
452 | XCB_SIZE_P_POSITION_HINT
)))
455 client_resize(c
, c
->floating_placement(c
), false);
457 c
->f_geometry
= c
->floating_placement(c
);
461 xcb_free_size_hints(u_size_hints
);
464 /** Compute client geometry with respect to its geometry hints.
465 * \param c The client.
466 * \param geometry The geometry that the client might receive.
467 * \return The geometry the client must take respecting its hints.
470 client_geometry_hints(client_t
*c
, area_t geometry
)
472 double dx
, dy
, max
, min
, ratio
;
474 if(c
->minay
> 0 && c
->maxay
> 0 && (geometry
.height
- c
->baseh
) > 0
475 && (geometry
.width
- c
->basew
) > 0)
477 dx
= (double) (geometry
.width
- c
->basew
);
478 dy
= (double) (geometry
.height
- c
->baseh
);
479 min
= (double) (c
->minax
) / (double) (c
->minay
);
480 max
= (double) (c
->maxax
) / (double) (c
->maxay
);
482 if(max
> 0 && min
> 0 && ratio
> 0)
486 dy
= (dx
* min
+ dy
) / (min
* min
+ 1);
488 geometry
.width
= (int) dx
+ c
->basew
;
489 geometry
.height
= (int) dy
+ c
->baseh
;
493 dy
= (dx
* min
+ dy
) / (max
* max
+ 1);
495 geometry
.width
= (int) dx
+ c
->basew
;
496 geometry
.height
= (int) dy
+ c
->baseh
;
500 if(c
->minw
&& geometry
.width
< c
->minw
)
501 geometry
.width
= c
->minw
;
502 if(c
->minh
&& geometry
.height
< c
->minh
)
503 geometry
.height
= c
->minh
;
504 if(c
->maxw
&& geometry
.width
> c
->maxw
)
505 geometry
.width
= c
->maxw
;
506 if(c
->maxh
&& geometry
.height
> c
->maxh
)
507 geometry
.height
= c
->maxh
;
509 geometry
.width
-= (geometry
.width
- c
->basew
) % c
->incw
;
511 geometry
.height
-= (geometry
.height
- c
->baseh
) % c
->inch
;
516 /** Resize client window.
517 * \param c Client to resize.
518 * \param geometry New window geometry.
519 * \param hints Use size hints.
520 * \return True if the client has been resized.
523 client_resize(client_t
*c
, area_t geometry
, bool hints
)
527 layout_t
*layout
= layout_get_current(c
->screen
);
528 bool resized
= false;
529 /* Values to configure a window is an array where values are
530 * stored according to 'value_mask' */
533 if(c
->titlebar
&& !c
->ismoving
&& !c
->isfloating
&& layout
!= layout_floating
)
534 geometry
= titlebar_geometry_remove(c
->titlebar
, c
->border
, geometry
);
537 geometry
= client_geometry_hints(c
, geometry
);
539 if(geometry
.width
<= 0 || geometry
.height
<= 0)
542 /* offscreen appearance fixes */
543 area
= display_area_get(c
->phys_screen
, NULL
,
544 &globalconf
.screens
[c
->screen
].padding
);
546 if(geometry
.x
> area
.width
)
547 geometry
.x
= area
.width
- geometry
.width
- 2 * c
->border
;
548 if(geometry
.y
> area
.height
)
549 geometry
.y
= area
.height
- geometry
.height
- 2 * c
->border
;
550 if(geometry
.x
+ geometry
.width
+ 2 * c
->border
< 0)
552 if(geometry
.y
+ geometry
.height
+ 2 * c
->border
< 0)
555 if(c
->geometry
.x
!= geometry
.x
|| c
->geometry
.y
!= geometry
.y
556 || c
->geometry
.width
!= geometry
.width
|| c
->geometry
.height
!= geometry
.height
)
559 screen_get_bycoord(globalconf
.screens_info
, c
->screen
, geometry
.x
, geometry
.y
);
561 c
->geometry
.x
= values
[0] = geometry
.x
;
562 c
->geometry
.width
= values
[2] = geometry
.width
;
563 c
->geometry
.y
= values
[1] = geometry
.y
;
564 c
->geometry
.height
= values
[3] = geometry
.height
;
565 values
[4] = c
->border
;
567 /* save the floating geometry if the window is floating but not
569 if(c
->ismoving
|| c
->isfloating
570 || layout_get_current(new_screen
) == layout_floating
)
572 titlebar_update_geometry_floating(c
);
574 c
->f_geometry
= geometry
;
577 xcb_configure_window(globalconf
.connection
, c
->win
,
578 XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
579 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
|
580 XCB_CONFIG_WINDOW_BORDER_WIDTH
,
582 window_configure(c
->win
, geometry
, c
->border
);
584 if(c
->screen
!= new_screen
)
585 screen_client_moveto(c
, new_screen
, false);
590 /* call it again like it was floating,
591 * we want it to be sticked to the window */
592 if(!c
->ismoving
&& !c
->isfloating
&& layout
!= layout_floating
)
593 titlebar_update_geometry_floating(c
);
598 /* Set the client layer.
599 * \param c The client.
600 * \param layer The layer.
603 client_setlayer(client_t
*c
, layer_t layer
)
610 /** Set a clinet floating.
611 * \param c The client.
612 * \param floating Set floating, or not.
613 * \param layer Layer to put the floating window onto.
616 client_setfloating(client_t
*c
, bool floating
)
618 if(c
->isfloating
!= floating
)
620 if((c
->isfloating
= floating
))
622 client_setlayer(c
, MAX(c
->layer
, LAYER_FLOAT
));
623 client_resize(c
, c
->f_geometry
, false);
624 titlebar_update_geometry_floating(c
);
629 client_setlayer(c
, c
->oldlayer
);
630 client_resize(c
, c
->m_geometry
, false);
633 client_setlayer(c
, c
->oldlayer
);
634 if(client_isvisible(c
, c
->screen
))
635 globalconf
.screens
[c
->screen
].need_arrange
= true;
636 widget_invalidate_cache(c
->screen
, WIDGET_CACHE_CLIENTS
);
641 /** Save client properties as an X property.
642 * \param c The client.
645 client_saveprops(client_t
*c
)
647 tag_array_t
*tags
= &globalconf
.screens
[c
->screen
].tags
;
648 unsigned char *prop
= p_alloca(unsigned char, tags
->len
+ 3);
651 for(i
= 0; i
< tags
->len
; i
++)
652 prop
[i
] = is_client_tagged(c
, tags
->tab
[i
]) ? '1' : '0';
654 prop
[i
++] = c
->isfloating
? '1' : '0';
655 prop
[i
++] = '0' + c
->layer
;
657 xcb_change_property(globalconf
.connection
, XCB_PROP_MODE_REPLACE
, c
->win
, _AWESOME_PROPERTIES
, STRING
, 8, i
, prop
);
661 * \param c The client.
664 client_unban(client_t
*c
)
666 xcb_map_window(globalconf
.connection
, c
->win
);
667 window_state_set(c
->win
, XCB_WM_NORMAL_STATE
);
668 if(c
->titlebar
&& c
->titlebar
->sw
&& c
->titlebar
->position
)
669 xcb_map_window(globalconf
.connection
, c
->titlebar
->sw
->window
);
672 /** Unmanage a client.
673 * \param c The client.
676 client_unmanage(client_t
*c
)
678 tag_array_t
*tags
= &globalconf
.screens
[c
->screen
].tags
;
680 if(globalconf
.screens
[c
->screen
].client_focus
== c
)
684 luaA_client_userdata_new(globalconf
.L
, c
);
685 luaA_dofunction(globalconf
.L
, globalconf
.hooks
.unmanage
, 1, 0);
687 /* The server grab construct avoids race conditions. */
688 xcb_grab_server(globalconf
.connection
);
690 xcb_configure_window(globalconf
.connection
, c
->win
,
691 XCB_CONFIG_WINDOW_BORDER_WIDTH
,
692 (uint32_t *) &c
->oldborder
);
694 xcb_ungrab_button(globalconf
.connection
, XCB_BUTTON_INDEX_ANY
, c
->win
,
696 window_state_set(c
->win
, XCB_WM_WITHDRAWN_STATE
);
698 xcb_aux_sync(globalconf
.connection
);
699 xcb_ungrab_server(globalconf
.connection
);
701 /* remove client everywhere */
702 client_list_detach(&globalconf
.clients
, c
);
703 stack_client_delete(c
);
704 for(int i
= 0; i
< tags
->len
; i
++)
705 untag_client(c
, tags
->tab
[i
]);
709 simplewindow_delete(&c
->titlebar
->sw
);
710 titlebar_unref(&c
->titlebar
);
713 ewmh_update_net_client_list(c
->phys_screen
);
715 /* delete properties */
716 xcb_delete_property(globalconf
.connection
, c
->win
, _AWESOME_PROPERTIES
);
718 /* set client as invalid */
724 /** Update the WM hints of a client.
725 * \param c The client.
728 client_updatewmhints(client_t
*c
)
731 uint32_t wm_hints_flags
;
733 if((wmh
= xcb_get_wm_hints(globalconf
.connection
, c
->win
)))
735 wm_hints_flags
= xcb_wm_hints_get_flags(wmh
);
736 if((c
->isurgent
= xcb_wm_hints_get_urgency(wmh
)))
739 luaA_client_userdata_new(globalconf
.L
, c
);
740 luaA_dofunction(globalconf
.L
, globalconf
.hooks
.urgent
, 1, 0);
742 widget_invalidate_cache(c
->screen
, WIDGET_CACHE_CLIENTS
);
744 if((wm_hints_flags
& XCB_WM_STATE_HINT
) &&
745 (xcb_wm_hints_get_initial_state(wmh
) == XCB_WM_WITHDRAWN_STATE
))
747 client_setborder(c
, 0);
750 xcb_free_wm_hints(wmh
);
754 /** Update the size hints of a client.
755 * \param c The client.
756 * \return A pointer to a xcb_size_hints_t.
759 client_updatesizehints(client_t
*c
)
762 xcb_size_hints_t
*size
;
765 if(!(size
= xcb_get_wm_normal_hints(globalconf
.connection
, c
->win
, &msize
)))
768 size_flags
= xcb_size_hints_get_flags(size
);
770 if((size_flags
& XCB_SIZE_P_SIZE_HINT
))
771 xcb_size_hints_get_base_size(size
, &c
->basew
, &c
->baseh
);
772 else if((size_flags
& XCB_SIZE_P_MIN_SIZE_HINT
))
773 xcb_size_hints_get_min_size(size
, &c
->basew
, &c
->baseh
);
775 c
->basew
= c
->baseh
= 0;
776 if((size_flags
& XCB_SIZE_P_RESIZE_INC_HINT
))
777 xcb_size_hints_get_increase(size
, &c
->incw
, &c
->inch
);
779 c
->incw
= c
->inch
= 0;
781 if((size_flags
& XCB_SIZE_P_MAX_SIZE_HINT
))
782 xcb_size_hints_get_max_size(size
, &c
->maxw
, &c
->maxh
);
784 c
->maxw
= c
->maxh
= 0;
786 if((size_flags
& XCB_SIZE_P_MIN_SIZE_HINT
))
787 xcb_size_hints_get_min_size(size
, &c
->minw
, &c
->minh
);
788 else if((size_flags
& XCB_SIZE_BASE_SIZE_HINT
))
789 xcb_size_hints_get_base_size(size
, &c
->minw
, &c
->minh
);
791 c
->minw
= c
->minh
= 0;
793 if((size_flags
& XCB_SIZE_P_ASPECT_HINT
))
795 xcb_size_hints_get_min_aspect(size
, &c
->minax
, &c
->minay
);
796 xcb_size_hints_get_max_aspect(size
, &c
->maxax
, &c
->maxay
);
799 c
->minax
= c
->maxax
= c
->minay
= c
->maxay
= 0;
801 if(c
->maxw
&& c
->minw
&& c
->maxh
&& c
->minh
802 && c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
)
805 c
->hassizehints
= !(!c
->basew
&& !c
->baseh
&& !c
->incw
&& !c
->inch
806 && !c
->maxw
&& !c
->maxh
&& !c
->minw
&& !c
->minh
807 && !c
->minax
&& !c
->maxax
&& !c
->minax
&& !c
->minay
);
811 /** Kill a client via a WM_DELETE_WINDOW request or XKillClient if not
813 * \param c The client to kill.
816 client_kill(client_t
*c
)
818 xcb_client_message_event_t ev
;
820 if(window_isprotodel(c
->win
))
822 /* Initialize all of event's fields first */
825 ev
.response_type
= XCB_CLIENT_MESSAGE
;
828 ev
.data
.data32
[1] = XCB_CURRENT_TIME
;
829 ev
.type
= WM_PROTOCOLS
;
830 ev
.data
.data32
[0] = WM_DELETE_WINDOW
;
832 xcb_send_event(globalconf
.connection
, false, c
->win
,
833 XCB_EVENT_MASK_NO_EVENT
, (char *) &ev
);
836 xcb_kill_client(globalconf
.connection
, c
->win
);
839 /** Get all clients into a table.
840 * \param L The Lua VM state.
842 * \lreturn A table with all clients.
845 luaA_client_get(lua_State
*L
)
852 for(c
= globalconf
.clients
; c
; c
= c
->next
)
854 luaA_client_userdata_new(globalconf
.L
, c
);
855 lua_rawseti(L
, -2, i
++);
861 /** Add mouse bindings over clients's window.
862 * \param L The Lua VM state.
865 * \lparam A button binding.
868 luaA_client_mouse_add(lua_State
*L
)
870 client_t
**c
= luaA_checkudata(L
, 1, "client");
871 button_t
**b
= luaA_checkudata(L
, 2, "mouse");
872 button_list_push(&(*c
)->buttons
, *b
);
877 /** Remove mouse bindings over clients's window.
878 * \param L The Lua VM state.
881 * \lparam A button binding.
884 luaA_client_mouse_remove(lua_State
*L
)
886 client_t
**c
= luaA_checkudata(L
, 1, "client");
887 button_t
**b
= luaA_checkudata(L
, 1, "mouse");
888 button_list_detach(&(*c
)->buttons
, *b
);
893 /** Get only visible clients for a screen.
894 * \param L The Lua VM state.
896 * \lparam A screen number.
897 * \lreturn A table with all visible clients for this screen.
900 luaA_client_visible_get(lua_State
*L
)
904 int screen
= luaL_checknumber(L
, 1) - 1;
906 luaA_checkscreen(screen
);
910 for(c
= globalconf
.clients
; c
; c
= c
->next
)
911 if(!c
->skip
&& !c
->ishidden
&& client_isvisible(c
, screen
))
913 luaA_client_userdata_new(globalconf
.L
, c
);
914 lua_rawseti(L
, -2, i
++);
920 /** Get the currently focused client (DEPRECATED).
921 * \param L The Lua VM state.
923 * \lreturn The currently focused client.
926 luaA_client_focus_get(lua_State
*L
)
928 if(globalconf
.screen_focus
->client_focus
)
929 return luaA_client_userdata_new(L
, globalconf
.screen_focus
->client_focus
);
934 /** Set client border width.
935 * \param c The client.
936 * \param width The border width.
939 client_setborder(client_t
*c
, int width
)
943 if((c
->noborder
&& width
> 0) || width
== c
->border
|| width
< 0)
947 xcb_configure_window(globalconf
.connection
, c
->win
,
948 XCB_CONFIG_WINDOW_BORDER_WIDTH
, &w
);
950 if(c
->isfloating
|| layout_get_current(c
->screen
) == layout_floating
)
951 titlebar_update_geometry_floating(c
);
953 globalconf
.screens
[c
->screen
].need_arrange
= true;
957 * \param L The Lua VM state.
963 luaA_client_kill(lua_State
*L
)
965 client_t
**c
= luaA_checkudata(L
, 1, "client");
970 /** Swap a client with another one.
971 * \param L The Lua VM state.
974 * \lparam A client to swap with.
977 luaA_client_swap(lua_State
*L
)
979 client_t
**c
= luaA_checkudata(L
, 1, "client");
980 client_t
**swap
= luaA_checkudata(L
, 2, "client");
981 client_list_swap(&globalconf
.clients
, *swap
, *c
);
982 globalconf
.screens
[(*c
)->screen
].need_arrange
= true;
983 globalconf
.screens
[(*swap
)->screen
].need_arrange
= true;
984 widget_invalidate_cache((*c
)->screen
, WIDGET_CACHE_CLIENTS
);
985 widget_invalidate_cache((*swap
)->screen
, WIDGET_CACHE_CLIENTS
);
989 /** Access or set the client tags.
990 * \param L The Lua VM state.
991 * \return The number of elements pushed on stack.
992 * \lparam A table with tags to set, or none to get the current tags table.
993 * \return The clients tag.
996 luaA_client_tags(lua_State
*L
)
1000 client_t
**c
= luaA_checkudata(L
, 1, "client");
1002 if(lua_gettop(L
) == 2)
1004 luaA_checktable(L
, 2);
1005 tags
= &globalconf
.screens
[(*c
)->screen
].tags
;
1006 for(int i
= 0; i
< tags
->len
; i
++)
1007 untag_client(*c
, tags
->tab
[i
]);
1009 while(lua_next(L
, 2))
1011 tag
= luaA_checkudata(L
, -1, "tag");
1012 tag_client(*c
, *tag
);
1018 tags
= &globalconf
.screens
[(*c
)->screen
].tags
;
1020 for(int i
= 0; i
< tags
->len
; i
++)
1021 if(is_client_tagged(*c
, tags
->tab
[i
]))
1023 luaA_tag_userdata_new(L
, tags
->tab
[i
]);
1024 luaA_tag_userdata_new(L
, tags
->tab
[i
]);
1032 /** Focus a client (DEPRECATED).
1033 * \param L The Lua VM state.
1039 luaA_client_focus_set(lua_State
*L
)
1041 client_t
**c
= luaA_checkudata(L
, 1, "client");
1047 /** Raise a client on top of others which are on the same layer.
1048 * \param L The Lua VM state.
1054 luaA_client_raise(lua_State
*L
)
1056 client_t
**c
= luaA_checkudata(L
, 1, "client");
1061 /** Redraw a client by unmapping and mapping it quickly.
1062 * \param L The Lua VM state.
1068 luaA_client_redraw(lua_State
*L
)
1070 client_t
**c
= luaA_checkudata(L
, 1, "client");
1071 xcb_unmap_window(globalconf
.connection
, (*c
)->win
);
1072 xcb_map_window(globalconf
.connection
, (*c
)->win
);
1076 /** Return a formated string for a client.
1077 * \param L The Lua VM state.
1080 * \lreturn A string.
1083 luaA_client_tostring(lua_State
*L
)
1085 client_t
**p
= luaA_checkudata(L
, 1, "client");
1086 lua_pushfstring(L
, "[client udata(%p) name(%s)]", *p
, (*p
)->name
);
1090 /** Stop managing a client.
1091 * \param L The Lua VM state.
1096 luaA_client_unmanage(lua_State
*L
)
1098 client_t
**c
= luaA_checkudata(L
, 1, "client");
1099 client_unmanage(*c
);
1103 /** Client newindex.
1104 * \param L The Lua VM state.
1105 * \return The number of elements pushed on stack.
1108 luaA_client_newindex(lua_State
*L
)
1111 client_t
**c
= luaA_checkudata(L
, 1, "client");
1112 const char *buf
= luaL_checklstring(L
, 2, &len
);
1116 titlebar_t
**t
= NULL
;
1119 luaL_error(L
, "client is invalid\n");
1121 switch(a_tokenize(buf
, len
))
1124 buf
= luaL_checklstring(L
, 3, &len
);
1125 p_delete(&(*c
)->name
);
1126 a_iso2utf8(&(*c
)->name
, buf
, len
);
1127 widget_invalidate_cache((*c
)->screen
, WIDGET_CACHE_CLIENTS
);
1129 case A_TK_FLOATING_PLACEMENT
:
1130 (*c
)->floating_placement
= name_func_lookup(luaL_checkstring(L
, 3),
1131 FloatingPlacementList
);
1134 if(globalconf
.screens_info
->xinerama_is_active
)
1136 i
= luaL_checknumber(L
, 3) - 1;
1137 luaA_checkscreen(i
);
1138 if(i
!= (*c
)->screen
)
1139 screen_client_moveto(*c
, i
, true);
1143 b
= luaA_checkboolean(L
, 3);
1144 if(b
!= (*c
)->ishidden
)
1147 if(client_isvisible(*c
, (*c
)->screen
))
1148 globalconf
.screens
[(*c
)->screen
].need_arrange
= true;
1151 case A_TK_ICON_PATH
:
1152 buf
= luaL_checkstring(L
, 3);
1153 p_delete(&(*c
)->icon_path
);
1154 (*c
)->icon_path
= a_strdup(buf
);
1155 widget_invalidate_cache((*c
)->screen
, WIDGET_CACHE_CLIENTS
);
1158 d
= luaL_checknumber(L
, 3);
1159 if(d
== -1 || (d
>= 0 && d
<= 1))
1160 window_trans_set((*c
)->win
, d
);
1163 client_setfloating(*c
, luaA_checkboolean(L
, 3));
1165 case A_TK_HONORSIZEHINTS
:
1166 (*c
)->honorsizehints
= luaA_checkboolean(L
, 3);
1167 if(client_isvisible(*c
, (*c
)->screen
))
1168 globalconf
.screens
[(*c
)->screen
].need_arrange
= true;
1170 case A_TK_BORDER_WIDTH
:
1171 client_setborder(*c
, luaL_checknumber(L
, 3));
1173 case A_TK_BORDER_COLOR
:
1174 if((buf
= luaL_checklstring(L
, 3, &len
))
1175 && xcolor_init_reply(globalconf
.connection
,
1176 xcolor_init_unchecked(globalconf
.connection
,
1177 &(*c
)->border_color
,
1178 (*c
)->phys_screen
, buf
,
1180 xcb_change_window_attributes(globalconf
.connection
, (*c
)->win
,
1181 XCB_CW_BORDER_PIXEL
, &(*c
)->border_color
.pixel
);
1184 if((*c
)->isfloating
|| layout_get_current((*c
)->screen
) == layout_floating
)
1187 luaA_checktable(L
, 3);
1188 geometry
.x
= luaA_getopt_number(L
, 3, "x", (*c
)->geometry
.x
);
1189 geometry
.y
= luaA_getopt_number(L
, 3, "y", (*c
)->geometry
.y
);
1190 geometry
.width
= luaA_getopt_number(L
, 3, "width", (*c
)->geometry
.width
);
1191 geometry
.height
= luaA_getopt_number(L
, 3, "height", (*c
)->geometry
.height
);
1192 client_resize(*c
, geometry
, false);
1196 if(!lua_isnil(L
, 3))
1198 t
= luaA_checkudata(L
, 3, "titlebar");
1199 if(client_getbytitlebar(*t
))
1200 luaL_error(L
, "titlebar is already used by another client");
1203 /* If client had a titlebar, unref it */
1206 simplewindow_delete(&(*c
)->titlebar
->sw
);
1207 titlebar_unref(&(*c
)->titlebar
);
1208 globalconf
.screens
[(*c
)->screen
].need_arrange
= true;
1213 /* Attach titlebar to client */
1214 (*c
)->titlebar
= *t
;
1227 * \param L The Lua VM state.
1228 * \return The number of elements pushed on stack.
1230 * \lfield name The client title.
1231 * \lfield class The client class.
1232 * \lfield instance The client instance.
1233 * \lfield pid The client PID, if available.
1234 * \lfield machine The machine client is running on.
1235 * \lfield icon_name The client name when iconified.
1236 * \lfield floating_placement The floating placement used for this client.
1237 * \lfield screen Client screen number.
1238 * \lfield hide Define if the client must be hidden, i.e. never mapped.
1239 * \lfield icon_path Path to the icon used to identify.
1240 * \lfield floating True always floating.
1241 * \lfield honorsizehints Honor size hints, i.e. respect size ratio.
1242 * \lfield border_width The client border width.
1243 * \lfield border_color The client border color.
1244 * \lfield coords The client coordinates.
1245 * \lfield titlebar The client titlebar.
1246 * \lfield urgent The client urgent state.
1247 * \lfield focus The focused client.
1250 luaA_client_index(lua_State
*L
)
1254 client_t
**c
= luaA_checkudata(L
, 1, "client");
1255 const char *buf
= luaL_checklstring(L
, 2, &len
);
1258 xutil_class_hint_t hint
;
1259 xcb_get_property_cookie_t prop_c
;
1260 xcb_get_property_reply_t
*prop_r
= NULL
;
1263 luaL_error(L
, "client is invalid\n");
1265 if(luaA_usemetatable(L
, 1, 2))
1268 switch(a_tokenize(buf
, len
))
1271 lua_pushstring(L
, (*c
)->name
);
1274 if(!xutil_class_hint_get(globalconf
.connection
, (*c
)->win
, &hint
))
1276 lua_pushstring(L
, hint
.res_class
);
1279 if(!xutil_class_hint_get(globalconf
.connection
, (*c
)->win
, &hint
))
1281 lua_pushstring(L
, hint
.res_name
);
1284 prop_c
= xcb_get_property_unchecked(globalconf
.connection
, false, (*c
)->win
, _NET_WM_PID
, CARDINAL
, 0L, 1L);
1285 prop_r
= xcb_get_property_reply(globalconf
.connection
, prop_c
, NULL
);
1287 if(prop_r
&& prop_r
->value_len
&& (data
= xcb_get_property_value(prop_r
)))
1288 lua_pushnumber(L
, *(uint32_t *)data
);
1296 if(!xutil_text_prop_get(globalconf
.connection
, (*c
)->win
,
1297 WM_CLIENT_MACHINE
, &value
, &slen
))
1299 lua_pushlstring(L
, value
, slen
);
1302 case A_TK_ICON_NAME
:
1303 if(!xutil_text_prop_get(globalconf
.connection
, (*c
)->win
,
1304 _NET_WM_ICON_NAME
, &value
, &slen
))
1305 if(!xutil_text_prop_get(globalconf
.connection
, (*c
)->win
,
1306 WM_ICON_NAME
, &value
, &slen
))
1308 lua_pushlstring(L
, value
, slen
);
1311 case A_TK_FLOATING_PLACEMENT
:
1312 lua_pushstring(L
, name_func_rlookup((*c
)->floating_placement
,
1313 FloatingPlacementList
));
1316 lua_pushnumber(L
, 1 + (*c
)->screen
);
1319 lua_pushboolean(L
, (*c
)->ishidden
);
1321 case A_TK_ICON_PATH
:
1322 lua_pushstring(L
, (*c
)->icon_path
);
1325 lua_pushboolean(L
, (*c
)->isfloating
);
1327 case A_TK_HONORSIZEHINTS
:
1328 lua_pushboolean(L
, (*c
)->honorsizehints
);
1330 case A_TK_BORDER_WIDTH
:
1331 lua_pushnumber(L
, (*c
)->border
);
1333 case A_TK_BORDER_COLOR
:
1334 luaA_pushcolor(L
, &(*c
)->border_color
);
1338 lua_pushnumber(L
, (*c
)->geometry
.width
);
1339 lua_setfield(L
, -2, "width");
1340 lua_pushnumber(L
, (*c
)->geometry
.height
);
1341 lua_setfield(L
, -2, "height");
1342 lua_pushnumber(L
, (*c
)->geometry
.x
);
1343 lua_setfield(L
, -2, "x");
1344 lua_pushnumber(L
, (*c
)->geometry
.y
);
1345 lua_setfield(L
, -2, "y");
1349 return luaA_titlebar_userdata_new(globalconf
.L
, (*c
)->titlebar
);
1352 lua_pushboolean(L
, (*c
)->isurgent
);
1362 * \param L The Lua VM state.
1363 * \return The number of pushed elements.
1366 luaA_client_module_index(lua_State
*L
)
1369 const char *buf
= luaL_checklstring(L
, 2, &len
);
1371 switch(a_tokenize(buf
, len
))
1374 if(globalconf
.screen_focus
->client_focus
)
1375 luaA_client_userdata_new(L
, globalconf
.screen_focus
->client_focus
);
1386 /* Client module new index.
1387 * \param L The Lua VM state.
1388 * \return The number of pushed elements.
1391 luaA_client_module_newindex(lua_State
*L
)
1394 const char *buf
= luaL_checklstring(L
, 2, &len
);
1397 switch(a_tokenize(buf
, len
))
1400 c
= luaA_checkudata(L
, 3, "client");
1410 const struct luaL_reg awesome_client_methods
[] =
1412 { "get", luaA_client_get
},
1413 { "focus_get", luaA_client_focus_get
},
1414 { "visible_get", luaA_client_visible_get
},
1415 { "__index", luaA_client_module_index
},
1416 { "__newindex", luaA_client_module_newindex
},
1419 const struct luaL_reg awesome_client_meta
[] =
1421 { "tags", luaA_client_tags
},
1422 { "kill", luaA_client_kill
},
1423 { "swap", luaA_client_swap
},
1424 { "focus_set", luaA_client_focus_set
},
1425 { "raise", luaA_client_raise
},
1426 { "redraw", luaA_client_redraw
},
1427 { "mouse_resize", luaA_client_mouse_resize
},
1428 { "mouse_move", luaA_client_mouse_move
},
1429 { "unmanage", luaA_client_unmanage
},
1430 { "mouse_add", luaA_client_mouse_add
},
1431 { "mouse_remove", luaA_client_mouse_remove
},
1432 { "__index", luaA_client_index
},
1433 { "__newindex", luaA_client_newindex
},
1434 { "__eq", luaA_client_eq
},
1435 { "__gc", luaA_client_gc
},
1436 { "__tostring", luaA_client_tostring
},
1440 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80