Add client.maximized (a shortcut for ~_horizontal and ~_vertical)
[awesome.git] / objects / client.c
blob9639e8a2fa27f5707f7432288fdf5b829f18f8a0
1 /*
2 * client.c - client management
4 * Copyright © 2007-2009 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 <xcb/xcb_atom.h>
23 #include <xcb/shape.h>
24 #include <cairo-xcb.h>
26 #include "objects/tag.h"
27 #include "ewmh.h"
28 #include "screen.h"
29 #include "systray.h"
30 #include "property.h"
31 #include "spawn.h"
32 #include "luaa.h"
33 #include "xwindow.h"
34 #include "common/atoms.h"
35 #include "common/xutil.h"
37 static area_t titlebar_get_area(client_t *c, client_titlebar_t bar);
38 static drawable_t *titlebar_get_drawable(lua_State *L, client_t *c, int cl_idx, client_titlebar_t bar);
40 /** Collect a client.
41 * \param L The Lua VM state.
42 * \return The number of element pushed on stack.
44 static void
45 client_wipe(client_t *c)
47 key_array_wipe(&c->keys);
48 xcb_icccm_get_wm_protocols_reply_wipe(&c->protocols);
49 p_delete(&c->machine);
50 p_delete(&c->class);
51 p_delete(&c->instance);
52 p_delete(&c->icon_name);
53 p_delete(&c->alt_icon_name);
54 p_delete(&c->name);
55 p_delete(&c->alt_name);
56 if(c->icon)
57 cairo_surface_destroy(c->icon);
60 /** Change the clients urgency flag.
61 * \param L The Lua VM state.
62 * \param cidx The client index on the stack.
63 * \param urgent The new flag state.
65 void
66 client_set_urgent(lua_State *L, int cidx, bool urgent)
68 client_t *c = luaA_checkudata(L, cidx, &client_class);
70 if(c->urgent != urgent)
72 xcb_get_property_cookie_t hints =
73 xcb_icccm_get_wm_hints_unchecked(globalconf.connection, c->window);
75 c->urgent = urgent;
77 /* update ICCCM hints */
78 xcb_icccm_wm_hints_t wmh;
79 xcb_icccm_get_wm_hints_reply(globalconf.connection, hints, &wmh, NULL);
81 if(urgent)
82 wmh.flags |= XCB_ICCCM_WM_HINT_X_URGENCY;
83 else
84 wmh.flags &= ~XCB_ICCCM_WM_HINT_X_URGENCY;
86 xcb_icccm_set_wm_hints(globalconf.connection, c->window, &wmh);
88 luaA_object_emit_signal(L, cidx, "property::urgent", 0);
92 #define DO_CLIENT_SET_PROPERTY(prop) \
93 void \
94 client_set_##prop(lua_State *L, int cidx, fieldtypeof(client_t, prop) value) \
95 { \
96 client_t *c = luaA_checkudata(L, cidx, &client_class); \
97 if(c->prop != value) \
98 { \
99 c->prop = value; \
100 luaA_object_emit_signal(L, cidx, "property::" #prop, 0); \
103 DO_CLIENT_SET_PROPERTY(group_window)
104 DO_CLIENT_SET_PROPERTY(type)
105 DO_CLIENT_SET_PROPERTY(transient_for)
106 DO_CLIENT_SET_PROPERTY(pid)
107 DO_CLIENT_SET_PROPERTY(skip_taskbar)
108 #undef DO_CLIENT_SET_PROPERTY
110 #define DO_CLIENT_SET_STRING_PROPERTY2(prop, signal) \
111 void \
112 client_set_##prop(lua_State *L, int cidx, char *value) \
114 client_t *c = luaA_checkudata(L, cidx, &client_class); \
115 if (A_STREQ(c->prop, value)) \
117 p_delete(&value); \
118 return; \
120 p_delete(&c->prop); \
121 c->prop = value; \
122 luaA_object_emit_signal(L, cidx, "property::" #signal, 0); \
124 #define DO_CLIENT_SET_STRING_PROPERTY(prop) \
125 DO_CLIENT_SET_STRING_PROPERTY2(prop, prop)
126 DO_CLIENT_SET_STRING_PROPERTY(name)
127 DO_CLIENT_SET_STRING_PROPERTY2(alt_name, name)
128 DO_CLIENT_SET_STRING_PROPERTY(icon_name)
129 DO_CLIENT_SET_STRING_PROPERTY2(alt_icon_name, icon_name)
130 DO_CLIENT_SET_STRING_PROPERTY(role)
131 DO_CLIENT_SET_STRING_PROPERTY(machine)
132 #undef DO_CLIENT_SET_STRING_PROPERTY
134 void
135 client_set_class_instance(lua_State *L, int cidx, const char *class, const char *instance)
137 client_t *c = luaA_checkudata(L, cidx, &client_class);
138 p_delete(&c->class);
139 p_delete(&c->instance);
140 c->class = a_strdup(class);
141 luaA_object_emit_signal(L, cidx, "property::class", 0);
142 c->instance = a_strdup(instance);
143 luaA_object_emit_signal(L, cidx, "property::instance", 0);
146 /** Returns true if a client is tagged
147 * with one of the tags of the specified screen.
148 * \param c The client to check.
149 * \param screen Virtual screen.
150 * \return true if the client is visible, false otherwise.
152 bool
153 client_maybevisible(client_t *c)
155 if(c->sticky)
156 return true;
158 foreach(tag, globalconf.tags)
159 if(tag_get_selected(*tag) && is_client_tagged(c, *tag))
160 return true;
162 return false;
165 /** Get a client by its window.
166 * \param w The client window to find.
167 * \return A client pointer if found, NULL otherwise.
169 client_t *
170 client_getbywin(xcb_window_t w)
172 foreach(c, globalconf.clients)
173 if((*c)->window == w)
174 return *c;
176 return NULL;
179 /** Get a client by its frame window.
180 * \param w The client window to find.
181 * \return A client pointer if found, NULL otherwise.
183 client_t *
184 client_getbyframewin(xcb_window_t w)
186 foreach(c, globalconf.clients)
187 if((*c)->frame_window == w)
188 return *c;
190 return NULL;
193 /** Unfocus a client (internal).
194 * \param c The client.
196 static void
197 client_unfocus_internal(client_t *c)
199 globalconf.focus.client = NULL;
201 luaA_object_push(globalconf.L, c);
202 luaA_object_emit_signal(globalconf.L, -1, "unfocus", 0);
203 lua_pop(globalconf.L, 1);
206 /** Unfocus a client.
207 * \param c The client.
209 static void
210 client_unfocus(client_t *c)
212 client_unfocus_internal(c);
213 globalconf.focus.need_update = true;
216 /** Check if client supports atom a protocol in WM_PROTOCOL.
217 * \param c The client.
218 * \param atom The protocol atom to check for.
219 * \return True if client has the atom in protocol, false otherwise.
221 bool
222 client_hasproto(client_t *c, xcb_atom_t atom)
224 for(uint32_t i = 0; i < c->protocols.atoms_len; i++)
225 if(c->protocols.atoms[i] == atom)
226 return true;
227 return false;
230 /** Prepare banning a client by running all needed lua events.
231 * \param c The client.
233 void client_ban_unfocus(client_t *c)
235 /* Wait until the last moment to take away the focus from the window. */
236 if(globalconf.focus.client == c)
237 client_unfocus(c);
240 /** Ban client and move it out of the viewport.
241 * \param c The client.
243 void
244 client_ban(client_t *c)
246 if(!c->isbanned)
248 xcb_unmap_window(globalconf.connection, c->frame_window);
250 c->isbanned = true;
252 client_ban_unfocus(c);
256 /** This is part of The Bob Marley Algorithm: we ignore enter and leave window
257 * in certain cases, like map/unmap or move, so we don't get spurious events.
259 void
260 client_ignore_enterleave_events(void)
262 foreach(c, globalconf.clients)
264 xcb_change_window_attributes(globalconf.connection,
265 (*c)->window,
266 XCB_CW_EVENT_MASK,
267 (const uint32_t []) { CLIENT_SELECT_INPUT_EVENT_MASK & ~(XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_LEAVE_WINDOW) });
268 xcb_change_window_attributes(globalconf.connection,
269 (*c)->frame_window,
270 XCB_CW_EVENT_MASK,
271 (const uint32_t []) { FRAME_SELECT_INPUT_EVENT_MASK & ~(XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_LEAVE_WINDOW) });
275 void
276 client_restore_enterleave_events(void)
278 foreach(c, globalconf.clients)
280 xcb_change_window_attributes(globalconf.connection,
281 (*c)->window,
282 XCB_CW_EVENT_MASK,
283 (const uint32_t []) { CLIENT_SELECT_INPUT_EVENT_MASK });
284 xcb_change_window_attributes(globalconf.connection,
285 (*c)->frame_window,
286 XCB_CW_EVENT_MASK,
287 (const uint32_t []) { FRAME_SELECT_INPUT_EVENT_MASK });
291 /** Record that a client got focus.
292 * \param c The client.
294 void
295 client_focus_update(client_t *c)
297 if(!client_maybevisible(c))
298 return;
300 if(globalconf.focus.client)
302 if (globalconf.focus.client == c)
303 /* Already focused */
304 return;
306 /* When we are called due to a FocusIn event (=old focused client
307 * already unfocused), we don't want to cause a SetInputFocus,
308 * because the client which has focus now could be using globally
309 * active input model (or 'no input').
311 client_unfocus_internal(globalconf.focus.client);
314 globalconf.focus.client = c;
316 /* according to EWMH, we have to remove the urgent state from a client */
317 luaA_object_push(globalconf.L, c);
318 client_set_urgent(globalconf.L, -1, false);
320 luaA_object_emit_signal(globalconf.L, -1, "focus", 0);
321 lua_pop(globalconf.L, 1);
324 /** Give focus to client, or to first client if client is NULL.
325 * \param c The client.
327 void
328 client_focus(client_t *c)
330 /* We have to set focus on first client */
331 if(!c && globalconf.clients.len && !(c = globalconf.clients.tab[0]))
332 return;
334 if(!client_maybevisible(c) || c == globalconf.focus.client)
335 return;
337 client_focus_update(c);
338 globalconf.focus.need_update = true;
341 void
342 client_focus_refresh(void)
344 client_t *c = globalconf.focus.client;
345 xcb_window_t win = globalconf.screen->root;
347 if(!globalconf.focus.need_update)
348 return;
349 globalconf.focus.need_update = false;
351 if(c)
353 /* Make sure this window is unbanned and e.g. not minimized */
354 client_unban(c);
355 /* Sets focus on window - using xcb_set_input_focus or WM_TAKE_FOCUS */
356 if(!c->nofocus)
357 win = c->window;
358 else
359 /* Focus the root window to make sure the previously focused client
360 * doesn't get any input in case WM_TAKE_FOCUS gets ignored.
362 win = globalconf.screen->root;
364 if(client_hasproto(c, WM_TAKE_FOCUS))
365 xwindow_takefocus(c->window);
368 /* If nothing has the focused or the currently focused client doesn't want
369 * us to focus it, this sets the focus to the root window. This makes sure
370 * the previously focused client actually gets unfocused. Alternatively, the
371 * new client gets the input focus.
373 xcb_set_input_focus(globalconf.connection, XCB_INPUT_FOCUS_PARENT,
374 win, globalconf.timestamp);
377 static void
378 client_update_properties(client_t *c)
380 /* get all hints */
381 xcb_get_property_cookie_t wm_normal_hints = property_get_wm_normal_hints(c);
382 xcb_get_property_cookie_t wm_hints = property_get_wm_hints(c);
383 xcb_get_property_cookie_t wm_transient_for = property_get_wm_transient_for(c);
384 xcb_get_property_cookie_t wm_client_leader = property_get_wm_client_leader(c);
385 xcb_get_property_cookie_t wm_client_machine = property_get_wm_client_machine(c);
386 xcb_get_property_cookie_t wm_window_role = property_get_wm_window_role(c);
387 xcb_get_property_cookie_t net_wm_pid = property_get_net_wm_pid(c);
388 xcb_get_property_cookie_t net_wm_icon = property_get_net_wm_icon(c);
389 xcb_get_property_cookie_t wm_name = property_get_wm_name(c);
390 xcb_get_property_cookie_t net_wm_name = property_get_net_wm_name(c);
391 xcb_get_property_cookie_t wm_icon_name = property_get_wm_icon_name(c);
392 xcb_get_property_cookie_t net_wm_icon_name = property_get_net_wm_icon_name(c);
393 xcb_get_property_cookie_t wm_class = property_get_wm_class(c);
394 xcb_get_property_cookie_t wm_protocols = property_get_wm_protocols(c);
395 xcb_get_property_cookie_t opacity = xwindow_get_opacity_unchecked(c->window);
397 /* update strut */
398 ewmh_process_client_strut(c);
400 /* Now process all replies */
401 property_update_wm_normal_hints(c, wm_normal_hints);
402 property_update_wm_hints(c, wm_hints);
403 property_update_wm_transient_for(c, wm_transient_for);
404 property_update_wm_client_leader(c, wm_client_leader);
405 property_update_wm_client_machine(c, wm_client_machine);
406 property_update_wm_window_role(c, wm_window_role);
407 property_update_net_wm_pid(c, net_wm_pid);
408 property_update_net_wm_icon(c, net_wm_icon);
409 property_update_wm_name(c, wm_name);
410 property_update_net_wm_name(c, net_wm_name);
411 property_update_wm_icon_name(c, wm_icon_name);
412 property_update_net_wm_icon_name(c, net_wm_icon_name);
413 property_update_wm_class(c, wm_class);
414 property_update_wm_protocols(c, wm_protocols);
415 window_set_opacity(globalconf.L, -1, xwindow_get_opacity_from_cookie(opacity));
418 /** Manage a new client.
419 * \param w The window.
420 * \param wgeom Window geometry.
421 * \param startup True if we are managing at startup time.
423 void
424 client_manage(xcb_window_t w, xcb_get_geometry_reply_t *wgeom, bool startup)
426 const uint32_t select_input_val[] = { CLIENT_SELECT_INPUT_EVENT_MASK };
428 if(systray_iskdedockapp(w))
430 systray_request_handle(w, NULL);
431 return;
434 /* If this is a new client that just has been launched, then request its
435 * startup id. */
436 xcb_get_property_cookie_t startup_id_q = { 0 };
437 if(!startup)
438 startup_id_q = xcb_get_property(globalconf.connection, false, w,
439 _NET_STARTUP_ID, XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
441 /* Make sure the window is automatically mapped if awesome exits or dies. */
442 xcb_change_save_set(globalconf.connection, XCB_SET_MODE_INSERT, w);
443 if (globalconf.have_shape)
444 xcb_shape_select_input(globalconf.connection, w, 1);
446 client_t *c = client_new(globalconf.L);
447 xcb_screen_t *s = globalconf.screen;
449 /* consider the window banned */
450 c->isbanned = true;
451 /* Store window */
452 c->window = w;
453 c->frame_window = xcb_generate_id(globalconf.connection);
454 xcb_create_window(globalconf.connection, globalconf.default_depth, c->frame_window, s->root,
455 wgeom->x, wgeom->y, wgeom->width, wgeom->height,
456 wgeom->border_width, XCB_COPY_FROM_PARENT, globalconf.visual->visual_id,
457 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL | XCB_CW_BIT_GRAVITY
458 | XCB_CW_WIN_GRAVITY | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK
459 | XCB_CW_COLORMAP,
460 (const uint32_t [])
462 globalconf.screen->black_pixel,
463 globalconf.screen->black_pixel,
464 XCB_GRAVITY_NORTH_WEST,
465 XCB_GRAVITY_NORTH_WEST,
467 FRAME_SELECT_INPUT_EVENT_MASK,
468 globalconf.default_cmap
471 if (startup)
473 /* The client is already mapped, thus we must be sure that we don't send
474 * ourselves an UnmapNotify due to the xcb_reparent_window().
476 * Grab the server to make sure we don't lose any events.
478 uint32_t no_event[] = { 0 };
479 xcb_grab_server(globalconf.connection);
481 xcb_change_window_attributes(globalconf.connection,
482 globalconf.screen->root,
483 XCB_CW_EVENT_MASK,
484 no_event);
487 xcb_reparent_window(globalconf.connection, w, c->frame_window, 0, 0);
488 xcb_map_window(globalconf.connection, w);
490 if (startup)
492 xcb_change_window_attributes(globalconf.connection,
493 globalconf.screen->root,
494 XCB_CW_EVENT_MASK,
495 ROOT_WINDOW_EVENT_MASK);
496 xcb_ungrab_server(globalconf.connection);
499 /* Do this now so that we don't get any events for the above
500 * (Else, reparent could cause an UnmapNotify) */
501 xcb_change_window_attributes(globalconf.connection, w, XCB_CW_EVENT_MASK, select_input_val);
503 luaA_object_emit_signal(globalconf.L, -1, "property::window", 0);
505 /* The frame window gets the border, not the real client window */
506 xcb_configure_window(globalconf.connection, w,
507 XCB_CONFIG_WINDOW_BORDER_WIDTH,
508 (uint32_t[]) { 0 });
510 /* Move this window to the bottom of the stack. Without this we would force
511 * other windows which will be above this one to redraw themselves because
512 * this window occludes them for a tiny moment. The next stack_refresh()
513 * will fix this up and move the window to its correct place. */
514 xcb_configure_window(globalconf.connection, c->frame_window,
515 XCB_CONFIG_WINDOW_STACK_MODE,
516 (uint32_t[]) { XCB_STACK_MODE_BELOW});
518 /* Duplicate client and push it in client list */
519 lua_pushvalue(globalconf.L, -1);
520 client_array_push(&globalconf.clients, luaA_object_ref(globalconf.L, -1));
522 /* Set the right screen */
523 screen_client_moveto(c, screen_getbycoord(wgeom->x, wgeom->y), false);
525 /* Store initial geometry and emits signals so we inform that geometry have
526 * been set. */
527 #define HANDLE_GEOM(attr) \
528 c->geometry.attr = wgeom->attr; \
529 luaA_object_emit_signal(globalconf.L, -1, "property::" #attr, 0);
530 HANDLE_GEOM(x)
531 HANDLE_GEOM(y)
532 HANDLE_GEOM(width)
533 HANDLE_GEOM(height)
534 #undef HANDLE_GEOM
536 luaA_object_emit_signal(globalconf.L, -1, "property::geometry", 0);
538 /* Set border width */
539 window_set_border_width(globalconf.L, -1, wgeom->border_width);
541 /* we honor size hints by default */
542 c->size_hints_honor = true;
543 luaA_object_emit_signal(globalconf.L, -1, "property::size_hints_honor", 0);
545 /* update all properties */
546 client_update_properties(c);
548 /* Then check clients hints */
549 ewmh_client_check_hints(c);
551 /* Push client in stack */
552 stack_client_push(c);
554 /* Always stay in NORMAL_STATE. Even though iconified seems more
555 * appropriate sometimes. The only possible loss is that clients not using
556 * visibility events may continue to process data (when banned).
557 * Without any exposes or other events the cost should be fairly limited though.
559 * Some clients may expect the window to be unmapped when STATE_ICONIFIED.
560 * Two conflicting parts of the ICCCM v2.0 (section 4.1.4):
562 * "Normal -> Iconic - The client should send a ClientMessage event as described later in this section."
563 * (note no explicit mention of unmapping, while Normal->Widthdrawn does mention that)
565 * "Once a client's window has left the Withdrawn state, the window will be mapped
566 * if it is in the Normal state and the window will be unmapped if it is in the Iconic state."
568 * At this stage it's just safer to keep it in normal state and avoid confusion.
570 xwindow_set_state(c->window, XCB_ICCCM_WM_STATE_NORMAL);
572 if(!startup)
574 /* Request our response */
575 xcb_get_property_reply_t *reply =
576 xcb_get_property_reply(globalconf.connection, startup_id_q, NULL);
577 /* Say spawn that a client has been started, with startup id as argument */
578 char *startup_id = xutil_get_text_property_from_reply(reply);
579 p_delete(&reply);
580 spawn_start_notify(c, startup_id);
581 p_delete(&startup_id);
584 luaA_class_emit_signal(globalconf.L, &client_class, "list", 0);
586 /* client is still on top of the stack; push startup value,
587 * and emit signals with one arg */
588 lua_pushboolean(globalconf.L, startup);
589 luaA_object_emit_signal(globalconf.L, -2, "manage", 1);
590 /* pop client */
591 lua_pop(globalconf.L, 1);
594 static void
595 client_remove_titlebar_geometry(client_t *c, area_t *geometry)
597 geometry->x += c->titlebar[CLIENT_TITLEBAR_LEFT].size;
598 geometry->y += c->titlebar[CLIENT_TITLEBAR_TOP].size;
599 geometry->width -= c->titlebar[CLIENT_TITLEBAR_LEFT].size;
600 geometry->width -= c->titlebar[CLIENT_TITLEBAR_RIGHT].size;
601 geometry->height -= c->titlebar[CLIENT_TITLEBAR_TOP].size;
602 geometry->height -= c->titlebar[CLIENT_TITLEBAR_BOTTOM].size;
605 static void
606 client_add_titlebar_geometry(client_t *c, area_t *geometry)
608 geometry->x -= c->titlebar[CLIENT_TITLEBAR_LEFT].size;
609 geometry->y -= c->titlebar[CLIENT_TITLEBAR_TOP].size;
610 geometry->width += c->titlebar[CLIENT_TITLEBAR_LEFT].size;
611 geometry->width += c->titlebar[CLIENT_TITLEBAR_RIGHT].size;
612 geometry->height += c->titlebar[CLIENT_TITLEBAR_TOP].size;
613 geometry->height += c->titlebar[CLIENT_TITLEBAR_BOTTOM].size;
616 /** Send a synthetic configure event to a window.
618 void
619 client_send_configure(client_t *c)
621 area_t geometry = c->geometry;
623 if (!c->fullscreen)
624 client_remove_titlebar_geometry(c, &geometry);
625 xwindow_configure(c->window, geometry, c->border_width);
628 /** Apply size hints to the client's new geometry.
630 static area_t
631 client_apply_size_hints(client_t *c, area_t geometry)
633 int32_t minw = 0, minh = 0;
634 int32_t basew = 0, baseh = 0, real_basew = 0, real_baseh = 0;
636 if (c->fullscreen)
637 return geometry;
639 /* Size hints are applied to the window without any decoration */
640 client_remove_titlebar_geometry(c, &geometry);
642 if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_SIZE)
644 basew = c->size_hints.base_width;
645 baseh = c->size_hints.base_height;
646 real_basew = basew;
647 real_baseh = baseh;
649 else if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE)
651 /* base size is substituted with min size if not specified */
652 basew = c->size_hints.min_width;
653 baseh = c->size_hints.min_height;
656 if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE)
658 minw = c->size_hints.min_width;
659 minh = c->size_hints.min_height;
661 else if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_SIZE)
663 /* min size is substituted with base size if not specified */
664 minw = c->size_hints.base_width;
665 minh = c->size_hints.base_height;
668 /* Handle the size aspect ratio */
669 if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_ASPECT
670 && c->size_hints.min_aspect_den > 0
671 && c->size_hints.max_aspect_den > 0
672 && geometry.height > real_baseh
673 && geometry.width > real_basew)
675 /* ICCCM mandates:
676 * If a base size is provided along with the aspect ratio fields, the base size should be subtracted from the
677 * window size prior to checking that the aspect ratio falls in range. If a base size is not provided, nothing
678 * should be subtracted from the window size. (The minimum size is not to be used in place of the base size for
679 * this purpose.)
681 double dx = geometry.width - real_basew;
682 double dy = geometry.height - real_baseh;
683 double ratio = dx / dy;
684 double min = c->size_hints.min_aspect_num / (double) c->size_hints.min_aspect_den;
685 double max = c->size_hints.max_aspect_num / (double) c->size_hints.max_aspect_den;
687 if(max > 0 && min > 0 && ratio > 0)
689 if(ratio < min)
691 /* dx is lower than allowed, make dy lower to compensate this (+ 0.5 to force proper rounding). */
692 dy = dx / min + 0.5;
693 geometry.width = dx + real_basew;
694 geometry.height = dy + real_baseh;
695 } else if(ratio > max)
697 /* dx is too high, lower it (+0.5 for proper rounding) */
698 dx = dy * max + 0.5;
699 geometry.width = dx + real_basew;
700 geometry.height = dy + real_baseh;
705 /* Handle the minimum size */
706 geometry.width = MAX(geometry.width, minw);
707 geometry.height = MAX(geometry.height, minh);
709 /* Handle the maximum size */
710 if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE)
712 if(c->size_hints.max_width)
713 geometry.width = MIN(geometry.width, c->size_hints.max_width);
714 if(c->size_hints.max_height)
715 geometry.height = MIN(geometry.height, c->size_hints.max_height);
718 /* Handle the size increment */
719 if(c->size_hints.flags & (XCB_ICCCM_SIZE_HINT_P_RESIZE_INC | XCB_ICCCM_SIZE_HINT_BASE_SIZE)
720 && c->size_hints.width_inc && c->size_hints.height_inc)
722 uint16_t t1 = geometry.width, t2 = geometry.height;
723 unsigned_subtract(t1, basew);
724 unsigned_subtract(t2, baseh);
725 geometry.width -= t1 % c->size_hints.width_inc;
726 geometry.height -= t2 % c->size_hints.height_inc;
729 client_add_titlebar_geometry(c, &geometry);
730 return geometry;
733 static void
734 client_resize_do(client_t *c, area_t geometry, bool force_notice, bool honor_hints)
736 bool send_notice = force_notice;
737 bool hide_titlebars = c->fullscreen;
738 screen_t *new_screen = screen_getbycoord(geometry.x, geometry.y);
740 if (honor_hints)
741 geometry = client_apply_size_hints(c, geometry);
743 if(c->geometry.width == geometry.width
744 && c->geometry.height == geometry.height)
745 send_notice = true;
747 /* Also store geometry including border */
748 area_t old_geometry = c->geometry;
749 c->geometry = geometry;
751 /* Ignore all spurious enter/leave notify events */
752 client_ignore_enterleave_events();
754 /* Configure the client for its new size */
755 area_t real_geometry = geometry;
756 if (!hide_titlebars)
758 real_geometry.x = c->titlebar[CLIENT_TITLEBAR_LEFT].size;
759 real_geometry.y = c->titlebar[CLIENT_TITLEBAR_TOP].size;
760 real_geometry.width -= c->titlebar[CLIENT_TITLEBAR_LEFT].size;
761 real_geometry.width -= c->titlebar[CLIENT_TITLEBAR_RIGHT].size;
762 real_geometry.height -= c->titlebar[CLIENT_TITLEBAR_TOP].size;
763 real_geometry.height -= c->titlebar[CLIENT_TITLEBAR_BOTTOM].size;
764 } else {
765 real_geometry.x = 0;
766 real_geometry.y = 0;
769 xcb_configure_window(globalconf.connection, c->frame_window,
770 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
771 (uint32_t[]) { geometry.x, geometry.y, geometry.width, geometry.height });
772 xcb_configure_window(globalconf.connection, c->window,
773 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
774 (uint32_t[]) { real_geometry.x, real_geometry.y, real_geometry.width, real_geometry.height });
776 if(send_notice)
777 /* We are moving without changing the size, see ICCCM 4.2.3 */
778 client_send_configure(c);
780 client_restore_enterleave_events();
782 luaA_object_push(globalconf.L, c);
783 luaA_object_emit_signal(globalconf.L, -1, "property::geometry", 0);
784 if (old_geometry.x != geometry.x)
785 luaA_object_emit_signal(globalconf.L, -1, "property::x", 0);
786 if (old_geometry.y != geometry.y)
787 luaA_object_emit_signal(globalconf.L, -1, "property::y", 0);
788 if (old_geometry.width != geometry.width)
789 luaA_object_emit_signal(globalconf.L, -1, "property::width", 0);
790 if (old_geometry.height != geometry.height)
791 luaA_object_emit_signal(globalconf.L, -1, "property::height", 0);
792 lua_pop(globalconf.L, 1);
794 screen_client_moveto(c, new_screen, false);
796 /* Update all titlebars */
797 for (client_titlebar_t bar = CLIENT_TITLEBAR_TOP; bar < CLIENT_TITLEBAR_COUNT; bar++) {
798 if (c->titlebar[bar].drawable == NULL && c->titlebar[bar].size == 0)
799 continue;
801 luaA_object_push(globalconf.L, c);
802 drawable_t *drawable = titlebar_get_drawable(globalconf.L, c, -1, bar);
803 luaA_object_push_item(globalconf.L, -1, drawable);
805 area_t area = titlebar_get_area(c, bar);
807 /* Convert to global coordinates */
808 area.x += geometry.x;
809 area.y += geometry.y;
810 if (hide_titlebars)
811 area.width = area.height = 0;
813 if (old_geometry.width != geometry.width || old_geometry.height != geometry.height ||
814 drawable->geometry.width == 0 || drawable->geometry.height == 0) {
815 /* Get rid of the old state */
816 drawable_unset_surface(drawable);
817 if (c->titlebar[bar].pixmap != XCB_NONE)
818 xcb_free_pixmap(globalconf.connection, c->titlebar[bar].pixmap);
819 c->titlebar[bar].pixmap = XCB_NONE;
821 /* And get us some new state */
822 if (c->titlebar[bar].size != 0 && !hide_titlebars)
824 c->titlebar[bar].pixmap = xcb_generate_id(globalconf.connection);
825 xcb_create_pixmap(globalconf.connection, globalconf.default_depth, c->titlebar[bar].pixmap,
826 globalconf.screen->root, area.width, area.height);
827 cairo_surface_t *surface = cairo_xcb_surface_create(globalconf.connection,
828 c->titlebar[bar].pixmap, globalconf.visual,
829 area.width, area.height);
830 drawable_set_surface(drawable, -1, surface, area);
831 } else
832 drawable_set_geometry(drawable, -1, area);
833 } else
834 drawable_set_geometry(drawable, -1, area);
836 /* Pop the client and the drawable */
837 lua_pop(globalconf.L, 2);
841 /** Resize client window.
842 * The sizes given as parameters are with borders!
843 * \param c Client to resize.
844 * \param geometry New window geometry.
845 * \param honor_hints Use size hints.
846 * \return true if an actual resize occurred.
848 bool
849 client_resize(client_t *c, area_t geometry, bool honor_hints)
851 area_t area;
853 /* offscreen appearance fixes */
854 area = display_area_get();
856 if(geometry.x > area.width)
857 geometry.x = area.width - geometry.width;
858 if(geometry.y > area.height)
859 geometry.y = area.height - geometry.height;
860 if(geometry.x + geometry.width < 0)
861 geometry.x = 0;
862 if(geometry.y + geometry.height < 0)
863 geometry.y = 0;
865 if(geometry.width < c->titlebar[CLIENT_TITLEBAR_LEFT].size + c->titlebar[CLIENT_TITLEBAR_RIGHT].size)
866 return false;
867 if(geometry.height < c->titlebar[CLIENT_TITLEBAR_TOP].size + c->titlebar[CLIENT_TITLEBAR_BOTTOM].size)
868 return false;
870 if(geometry.width == 0 || geometry.height == 0)
871 return false;
873 if(c->geometry.x != geometry.x
874 || c->geometry.y != geometry.y
875 || c->geometry.width != geometry.width
876 || c->geometry.height != geometry.height)
878 client_resize_do(c, geometry, false, honor_hints);
880 return true;
883 return false;
886 /** Set a client minimized, or not.
887 * \param L The Lua VM state.
888 * \param cidx The client index.
889 * \param s Set or not the client minimized.
891 void
892 client_set_minimized(lua_State *L, int cidx, bool s)
894 client_t *c = luaA_checkudata(L, cidx, &client_class);
896 if(c->minimized != s)
898 c->minimized = s;
899 banning_need_update();
900 if(s)
901 xwindow_set_state(c->window, XCB_ICCCM_WM_STATE_ICONIC);
902 else
903 xwindow_set_state(c->window, XCB_ICCCM_WM_STATE_NORMAL);
904 if(strut_has_value(&c->strut))
905 screen_emit_signal(globalconf.L, c->screen, "property::workarea", 0);
906 luaA_object_emit_signal(L, cidx, "property::minimized", 0);
910 /** Set a client hidden, or not.
911 * \param L The Lua VM state.
912 * \param cidx The client index.
913 * \param s Set or not the client hidden.
915 static void
916 client_set_hidden(lua_State *L, int cidx, bool s)
918 client_t *c = luaA_checkudata(L, cidx, &client_class);
920 if(c->hidden != s)
922 c->hidden = s;
923 banning_need_update();
924 if(strut_has_value(&c->strut))
925 screen_emit_signal(globalconf.L, c->screen, "property::workarea", 0);
926 luaA_object_emit_signal(L, cidx, "property::hidden", 0);
930 /** Set a client sticky, or not.
931 * \param L The Lua VM state.
932 * \param cidx The client index.
933 * \param s Set or not the client sticky.
935 void
936 client_set_sticky(lua_State *L, int cidx, bool s)
938 client_t *c = luaA_checkudata(L, cidx, &client_class);
940 if(c->sticky != s)
942 c->sticky = s;
943 banning_need_update();
944 luaA_object_emit_signal(L, cidx, "property::sticky", 0);
948 /** Set a client fullscreen, or not.
949 * \param L The Lua VM state.
950 * \param cidx The client index.
951 * \param s Set or not the client fullscreen.
953 void
954 client_set_fullscreen(lua_State *L, int cidx, bool s)
956 client_t *c = luaA_checkudata(L, cidx, &client_class);
958 if(c->fullscreen != s)
960 /* become fullscreen! */
961 if(s)
963 /* remove any max state */
964 client_set_maximized(L, cidx, false);
965 /* You can only be part of one of the special layers. */
966 client_set_below(L, cidx, false);
967 client_set_above(L, cidx, false);
968 client_set_ontop(L, cidx, false);
970 int abs_cidx = luaA_absindex(L, cidx); \
971 lua_pushboolean(L, s);
972 c->fullscreen = s;
973 luaA_object_emit_signal(L, abs_cidx, "request::fullscreen", 1);
974 luaA_object_emit_signal(L, abs_cidx, "property::fullscreen", 0);
975 /* Force a client resize, so that titlebars get shown/hidden */
976 client_resize_do(c, c->geometry, true, false);
977 stack_windows();
981 /** Get a clients maximized state (horizontally and vertically).
982 * \param c The client.
983 * \return The maximized state.
985 static int
986 client_get_maximized(client_t *c)
988 return c->maximized_horizontal && c->maximized_vertical;
991 /** Set a client horizontally|vertically maximized.
992 * \param L The Lua VM state.
993 * \param cidx The client index.
994 * \param s The maximized status.
996 #define DO_FUNCTION_CLIENT_MAXIMIZED(type) \
997 void \
998 client_set_maximized_##type(lua_State *L, int cidx, bool s) \
1000 client_t *c = luaA_checkudata(L, cidx, &client_class); \
1001 if(c->maximized_##type != s) \
1003 int abs_cidx = luaA_absindex(L, cidx); \
1004 if(s) \
1005 client_set_fullscreen(L, abs_cidx, false); \
1006 lua_pushboolean(L, s); \
1007 int max_before = client_get_maximized(c); \
1008 c->maximized_##type = s; \
1009 luaA_object_emit_signal(L, abs_cidx, "request::maximized_" #type, 1); \
1010 luaA_object_emit_signal(L, abs_cidx, "property::maximized_" #type, 0); \
1011 if(max_before != client_get_maximized(c)) \
1012 luaA_object_emit_signal(L, abs_cidx, "property::maximized", 0); \
1013 stack_windows(); \
1016 DO_FUNCTION_CLIENT_MAXIMIZED(vertical)
1017 DO_FUNCTION_CLIENT_MAXIMIZED(horizontal)
1018 #undef DO_FUNCTION_CLIENT_MAXIMIZED
1020 /** Set a client maximized (horizontally and vertically).
1021 * \param L The Lua VM state.
1022 * \param cidx The client index.
1023 * \param s Set or not the client maximized attribute.
1025 void
1026 client_set_maximized(lua_State *L, int cidx, bool s)
1028 client_set_maximized_horizontal(L, cidx, s);
1029 client_set_maximized_vertical(L, cidx, s);
1032 /** Set a client above, or not.
1033 * \param L The Lua VM state.
1034 * \param cidx The client index.
1035 * \param s Set or not the client above.
1037 void
1038 client_set_above(lua_State *L, int cidx, bool s)
1040 client_t *c = luaA_checkudata(L, cidx, &client_class);
1042 if(c->above != s)
1044 /* You can only be part of one of the special layers. */
1045 if(s)
1047 client_set_below(L, cidx, false);
1048 client_set_ontop(L, cidx, false);
1049 client_set_fullscreen(L, cidx, false);
1051 c->above = s;
1052 stack_windows();
1053 luaA_object_emit_signal(L, cidx, "property::above", 0);
1057 /** Set a client below, or not.
1058 * \param L The Lua VM state.
1059 * \param cidx The client index.
1060 * \param s Set or not the client below.
1062 void
1063 client_set_below(lua_State *L, int cidx, bool s)
1065 client_t *c = luaA_checkudata(L, cidx, &client_class);
1067 if(c->below != s)
1069 /* You can only be part of one of the special layers. */
1070 if(s)
1072 client_set_above(L, cidx, false);
1073 client_set_ontop(L, cidx, false);
1074 client_set_fullscreen(L, cidx, false);
1076 c->below = s;
1077 stack_windows();
1078 luaA_object_emit_signal(L, cidx, "property::below", 0);
1082 /** Set a client modal, or not.
1083 * \param L The Lua VM state.
1084 * \param cidx The client index.
1085 * \param s Set or not the client modal attribute.
1087 void
1088 client_set_modal(lua_State *L, int cidx, bool s)
1090 client_t *c = luaA_checkudata(L, cidx, &client_class);
1092 if(c->modal != s)
1094 c->modal = s;
1095 stack_windows();
1096 luaA_object_emit_signal(L, cidx, "property::modal", 0);
1100 /** Set a client ontop, or not.
1101 * \param L The Lua VM state.
1102 * \param cidx The client index.
1103 * \param s Set or not the client ontop attribute.
1105 void
1106 client_set_ontop(lua_State *L, int cidx, bool s)
1108 client_t *c = luaA_checkudata(L, cidx, &client_class);
1110 if(c->ontop != s)
1112 /* You can only be part of one of the special layers. */
1113 if(s)
1115 client_set_above(L, cidx, false);
1116 client_set_below(L, cidx, false);
1117 client_set_fullscreen(L, cidx, false);
1119 c->ontop = s;
1120 stack_windows();
1121 luaA_object_emit_signal(L, cidx, "property::ontop", 0);
1125 /** Unban a client and move it back into the viewport.
1126 * \param c The client.
1128 void
1129 client_unban(client_t *c)
1131 if(c->isbanned)
1133 xcb_map_window(globalconf.connection, c->frame_window);
1135 c->isbanned = false;
1137 /* An unbanned client shouldn't be minimized or hidden */
1138 luaA_object_push(globalconf.L, c);
1139 client_set_minimized(globalconf.L, -1, false);
1140 client_set_hidden(globalconf.L, -1, false);
1141 lua_pop(globalconf.L, 1);
1145 /** Unmanage a client.
1146 * \param c The client.
1147 * \param window_valid Is the client's window still valid?
1149 void
1150 client_unmanage(client_t *c, bool window_valid)
1152 /* Reset transient_for attributes of widows that maybe referring to us */
1153 foreach(_tc, globalconf.clients)
1155 client_t *tc = *_tc;
1156 if(tc->transient_for == c)
1157 tc->transient_for = NULL;
1160 if(globalconf.focus.client == c)
1161 client_unfocus(c);
1163 /* remove client from global list and everywhere else */
1164 foreach(elem, globalconf.clients)
1165 if(*elem == c)
1167 client_array_remove(&globalconf.clients, elem);
1168 break;
1170 stack_client_remove(c);
1171 for(int i = 0; i < globalconf.tags.len; i++)
1172 untag_client(c, globalconf.tags.tab[i]);
1174 luaA_object_push(globalconf.L, c);
1175 luaA_object_emit_signal(globalconf.L, -1, "unmanage", 0);
1176 lua_pop(globalconf.L, 1);
1178 luaA_class_emit_signal(globalconf.L, &client_class, "list", 0);
1180 if(strut_has_value(&c->strut))
1181 screen_emit_signal(globalconf.L, c->screen, "property::workarea", 0);
1183 /* Get rid of all titlebars */
1184 for (client_titlebar_t bar = CLIENT_TITLEBAR_TOP; bar < CLIENT_TITLEBAR_COUNT; bar++) {
1185 if (c->titlebar[bar].drawable == NULL)
1186 continue;
1188 luaA_object_push(globalconf.L, c);
1189 luaA_object_push_item(globalconf.L, -1, c->titlebar[bar].drawable);
1191 /* Make the drawable unusable */
1192 drawable_unset_surface(c->titlebar[bar].drawable);
1193 if (c->titlebar[bar].pixmap != XCB_NONE)
1194 xcb_free_pixmap(globalconf.connection, c->titlebar[bar].pixmap);
1196 /* And forget about it */
1197 luaA_object_unref_item(globalconf.L, -2, c->titlebar[bar].drawable);
1198 c->titlebar[bar].drawable = NULL;
1199 lua_pop(globalconf.L, 2);
1202 /* Clear our event mask so that we don't receive any events from now on,
1203 * especially not for the following requests. */
1204 if(window_valid)
1205 xcb_change_window_attributes(globalconf.connection,
1206 c->window,
1207 XCB_CW_EVENT_MASK,
1208 (const uint32_t []) { 0 });
1209 xcb_change_window_attributes(globalconf.connection,
1210 c->frame_window,
1211 XCB_CW_EVENT_MASK,
1212 (const uint32_t []) { 0 });
1214 if(window_valid)
1216 xcb_unmap_window(globalconf.connection, c->window);
1217 xcb_reparent_window(globalconf.connection, c->window, globalconf.screen->root,
1218 c->geometry.x, c->geometry.y);
1220 xcb_destroy_window(globalconf.connection, c->frame_window);
1222 if(window_valid)
1224 /* Remove this window from the save set since this shouldn't be made visible
1225 * after a restart anymore. */
1226 xcb_change_save_set(globalconf.connection, XCB_SET_MODE_DELETE, c->window);
1227 if (globalconf.have_shape)
1228 xcb_shape_select_input(globalconf.connection, c->window, 0);
1230 /* Do this last to avoid races with clients. According to ICCCM, clients
1231 * arent allowed to re-use the window until after this. */
1232 xwindow_set_state(c->window, XCB_ICCCM_WM_STATE_WITHDRAWN);
1235 /* set client as invalid */
1236 c->window = XCB_NONE;
1238 luaA_object_unref(globalconf.L, c);
1241 /** Kill a client via a WM_DELETE_WINDOW request or KillClient if not
1242 * supported.
1243 * \param c The client to kill.
1245 void
1246 client_kill(client_t *c)
1248 if(client_hasproto(c, WM_DELETE_WINDOW))
1250 xcb_client_message_event_t ev;
1252 /* Initialize all of event's fields first */
1253 p_clear(&ev, 1);
1255 ev.response_type = XCB_CLIENT_MESSAGE;
1256 ev.window = c->window;
1257 ev.format = 32;
1258 ev.data.data32[1] = globalconf.timestamp;
1259 ev.type = WM_PROTOCOLS;
1260 ev.data.data32[0] = WM_DELETE_WINDOW;
1262 xcb_send_event(globalconf.connection, false, c->window,
1263 XCB_EVENT_MASK_NO_EVENT, (char *) &ev);
1265 else
1266 xcb_kill_client(globalconf.connection, c->window);
1269 /** Get all clients into a table.
1270 * \param L The Lua VM state.
1271 * \return The number of elements pushed on stack.
1272 * \luastack
1273 * \lparam An optional screen number.
1274 * \lreturn A table with all clients.
1276 static int
1277 luaA_client_get(lua_State *L)
1279 int i = 1, screen;
1281 screen = luaL_optnumber(L, 1, 0) - 1;
1283 lua_newtable(L);
1285 if(screen == -1)
1286 foreach(c, globalconf.clients)
1288 luaA_object_push(L, *c);
1289 lua_rawseti(L, -2, i++);
1291 else
1293 luaA_checkscreen(screen);
1294 foreach(c, globalconf.clients)
1295 if((*c)->screen == &globalconf.screens.tab[screen])
1297 luaA_object_push(L, *c);
1298 lua_rawseti(L, -2, i++);
1302 return 1;
1305 /** Check if a client is visible on its screen.
1306 * \param L The Lua VM state.
1307 * \return The number of elements pushed on stack.
1308 * \luastack
1309 * \lvalue A client.
1310 * \lreturn A boolean value, true if the client is visible, false otherwise.
1312 static int
1313 luaA_client_isvisible(lua_State *L)
1315 client_t *c = luaA_checkudata(L, 1, &client_class);
1316 lua_pushboolean(L, client_isvisible(c));
1317 return 1;
1320 /** Set a client icon.
1321 * \param L The Lua VM state.
1322 * \param cidx The client index on the stack.
1323 * \param iidx The image index on the stack.
1325 void
1326 client_set_icon(client_t *c, cairo_surface_t *s)
1328 if (s)
1329 s = draw_dup_image_surface(s);
1330 if(c->icon)
1331 cairo_surface_destroy(c->icon);
1332 c->icon = s;
1334 luaA_object_push(globalconf.L, c);
1335 luaA_object_emit_signal(globalconf.L, -1, "property::icon", 0);
1336 lua_pop(globalconf.L, 1);
1339 /** Kill a client.
1340 * \param L The Lua VM state.
1342 * \luastack
1343 * \lvalue A client.
1345 static int
1346 luaA_client_kill(lua_State *L)
1348 client_t *c = luaA_checkudata(L, 1, &client_class);
1349 client_kill(c);
1350 return 0;
1353 /** Swap a client with another one.
1354 * \param L The Lua VM state.
1355 * \luastack
1356 * \lvalue A client.
1357 * \lparam A client to swap with.
1359 static int
1360 luaA_client_swap(lua_State *L)
1362 client_t *c = luaA_checkudata(L, 1, &client_class);
1363 client_t *swap = luaA_checkudata(L, 2, &client_class);
1365 if(c != swap)
1367 client_t **ref_c = NULL, **ref_swap = NULL;
1368 foreach(item, globalconf.clients)
1370 if(*item == c)
1371 ref_c = item;
1372 else if(*item == swap)
1373 ref_swap = item;
1374 if(ref_c && ref_swap)
1375 break;
1377 /* swap ! */
1378 *ref_c = swap;
1379 *ref_swap = c;
1381 luaA_class_emit_signal(globalconf.L, &client_class, "list", 0);
1384 return 0;
1387 /** Access or set the client tags.
1388 * \param L The Lua VM state.
1389 * \return The number of elements pushed on stack.
1390 * \lparam A table with tags to set, or none to get the current tags table.
1391 * \return The clients tag.
1393 static int
1394 luaA_client_tags(lua_State *L)
1396 client_t *c = luaA_checkudata(L, 1, &client_class);
1397 int j = 0;
1399 if(lua_gettop(L) == 2)
1401 luaA_checktable(L, 2);
1402 for(int i = 0; i < globalconf.tags.len; i++)
1404 /* Only untag if we aren't going to add this tag again */
1405 bool found = false;
1406 lua_pushnil(L);
1407 while(lua_next(L, 2))
1409 tag_t *t = lua_touserdata(L, -1);
1410 /* Pop the value from lua_next */
1411 lua_pop(L, 1);
1412 if (t != globalconf.tags.tab[i])
1413 continue;
1415 /* Pop the key from lua_next */
1416 lua_pop(L, 1);
1417 found = true;
1418 break;
1420 if(!found)
1421 untag_client(c, globalconf.tags.tab[i]);
1423 lua_pushnil(L);
1424 while(lua_next(L, 2))
1425 tag_client(c);
1426 lua_pop(L, 1);
1429 lua_newtable(L);
1430 foreach(tag, globalconf.tags)
1431 if(is_client_tagged(c, *tag))
1433 luaA_object_push(L, *tag);
1434 lua_rawseti(L, -2, ++j);
1437 return 1;
1440 /** Raise a client on top of others which are on the same layer.
1441 * \param L The Lua VM state.
1442 * \luastack
1443 * \lvalue A client.
1445 static int
1446 luaA_client_raise(lua_State *L)
1448 client_t *c = luaA_checkudata(L, 1, &client_class);
1449 client_raise(c);
1450 return 0;
1453 /** Lower a client on bottom of others which are on the same layer.
1454 * \param L The Lua VM state.
1455 * \luastack
1456 * \lvalue A client.
1458 static int
1459 luaA_client_lower(lua_State *L)
1461 client_t *c = luaA_checkudata(L, 1, &client_class);
1463 stack_client_push(c);
1465 /* Traverse all transient layers. */
1466 for(client_t *tc = c->transient_for; tc; tc = tc->transient_for)
1467 stack_client_push(tc);
1469 return 0;
1472 /** Stop managing a client.
1473 * \param L The Lua VM state.
1474 * \return The number of elements pushed on stack.
1475 * \luastack
1476 * \lvalue A client.
1478 static int
1479 luaA_client_unmanage(lua_State *L)
1481 client_t *c = luaA_checkudata(L, 1, &client_class);
1482 client_unmanage(c, true);
1483 return 0;
1486 static area_t
1487 titlebar_get_area(client_t *c, client_titlebar_t bar)
1489 area_t result = c->geometry;
1490 result.x = result.y = 0;
1492 // Let's try some ascii art:
1493 // ---------------------------
1494 // | Top |
1495 // |-------------------------|
1496 // |L| |R|
1497 // |e| |i|
1498 // |f| |g|
1499 // |t| |h|
1500 // | | |t|
1501 // |-------------------------|
1502 // | Bottom |
1503 // ---------------------------
1505 switch (bar) {
1506 case CLIENT_TITLEBAR_BOTTOM:
1507 result.y = c->geometry.height - c->titlebar[bar].size;
1508 /* Fall through */
1509 case CLIENT_TITLEBAR_TOP:
1510 result.height = c->titlebar[bar].size;
1511 break;
1512 case CLIENT_TITLEBAR_RIGHT:
1513 result.x = c->geometry.width - c->titlebar[bar].size;
1514 /* Fall through */
1515 case CLIENT_TITLEBAR_LEFT:
1516 result.y = c->titlebar[CLIENT_TITLEBAR_TOP].size;
1517 result.width = c->titlebar[bar].size;
1518 result.height -= c->titlebar[CLIENT_TITLEBAR_TOP].size;
1519 result.height -= c->titlebar[CLIENT_TITLEBAR_BOTTOM].size;
1520 break;
1521 default:
1522 fatal("Unknown titlebar kind %d\n", (int) bar);
1525 return result;
1528 drawable_t *
1529 client_get_drawable_offset(client_t *c, int *x, int *y)
1531 for (client_titlebar_t bar = CLIENT_TITLEBAR_TOP; bar < CLIENT_TITLEBAR_COUNT; bar++) {
1532 area_t area = titlebar_get_area(c, bar);
1533 if (AREA_LEFT(area) > *x || AREA_RIGHT(area) <= *x)
1534 continue;
1535 if (AREA_TOP(area) > *y || AREA_BOTTOM(area) <= *y)
1536 continue;
1538 *x -= area.x;
1539 *y -= area.y;
1540 return c->titlebar[bar].drawable;
1543 return NULL;
1546 drawable_t *
1547 client_get_drawable(client_t *c, int x, int y)
1549 return client_get_drawable_offset(c, &x, &y);
1552 void
1553 client_refresh(client_t *c)
1555 for (client_titlebar_t bar = CLIENT_TITLEBAR_TOP; bar < CLIENT_TITLEBAR_COUNT; bar++) {
1556 if (c->titlebar[bar].drawable == NULL || c->titlebar[bar].drawable->surface == NULL)
1557 continue;
1559 area_t area = titlebar_get_area(c, bar);
1560 cairo_surface_flush(c->titlebar[bar].drawable->surface);
1561 xcb_copy_area(globalconf.connection, c->titlebar[bar].pixmap, c->frame_window,
1562 globalconf.gc, 0, 0, area.x, area.y, area.width, area.height);
1566 static drawable_t *
1567 titlebar_get_drawable(lua_State *L, client_t *c, int cl_idx, client_titlebar_t bar)
1569 if (c->titlebar[bar].drawable == NULL)
1571 cl_idx = luaA_absindex(L, cl_idx);
1572 drawable_allocator(L, (drawable_refresh_callback *) client_refresh, c);
1573 c->titlebar[bar].drawable = luaA_object_ref_item(L, cl_idx, -1);
1576 return c->titlebar[bar].drawable;
1579 static void
1580 titlebar_resize(client_t *c, client_titlebar_t bar, int size)
1582 if (size < 0)
1583 return;
1585 if (size == c->titlebar[bar].size)
1586 return;
1588 /* Now resize the client (and titlebars!) suitably (the client without
1589 * titlebars should keep its current size!) */
1590 area_t geometry = c->geometry;
1591 int change = size - c->titlebar[bar].size;
1592 switch (bar) {
1593 case CLIENT_TITLEBAR_TOP:
1594 case CLIENT_TITLEBAR_BOTTOM:
1595 geometry.height += change;
1596 break;
1597 case CLIENT_TITLEBAR_RIGHT:
1598 case CLIENT_TITLEBAR_LEFT:
1599 geometry.width += change;
1600 break;
1601 default:
1602 fatal("Unknown titlebar kind %d\n", (int) bar);
1605 c->titlebar[bar].size = size;
1606 client_resize_do(c, geometry, true, false);
1609 #define HANDLE_TITLEBAR(name, index) \
1610 static int \
1611 luaA_client_titlebar_ ## name(lua_State *L) \
1613 client_t *c = luaA_checkudata(L, 1, &client_class); \
1615 if (lua_gettop(L) == 2) \
1617 if (lua_isnil(L, 2)) \
1618 titlebar_resize(c, index, 0); \
1619 else \
1620 titlebar_resize(c, index, luaL_checknumber(L, 2)); \
1623 luaA_object_push_item(L, 1, titlebar_get_drawable(L, c, 1, index)); \
1624 lua_pushnumber(L, c->titlebar[index].size); \
1625 return 2; \
1627 HANDLE_TITLEBAR(top, CLIENT_TITLEBAR_TOP)
1628 HANDLE_TITLEBAR(right, CLIENT_TITLEBAR_RIGHT)
1629 HANDLE_TITLEBAR(bottom, CLIENT_TITLEBAR_BOTTOM)
1630 HANDLE_TITLEBAR(left, CLIENT_TITLEBAR_LEFT)
1632 /** Return client geometry.
1633 * \param L The Lua VM state.
1634 * \return The number of elements pushed on stack.
1635 * \luastack
1636 * \lparam A table with new coordinates, or none.
1637 * \lreturn A table with client coordinates.
1639 static int
1640 luaA_client_geometry(lua_State *L)
1642 client_t *c = luaA_checkudata(L, 1, &client_class);
1644 if(lua_gettop(L) == 2 && !lua_isnil(L, 2))
1646 area_t geometry;
1648 luaA_checktable(L, 2);
1649 geometry.x = luaA_getopt_number(L, 2, "x", c->geometry.x);
1650 geometry.y = luaA_getopt_number(L, 2, "y", c->geometry.y);
1651 if(client_isfixed(c))
1653 geometry.width = c->geometry.width;
1654 geometry.height = c->geometry.height;
1656 else
1658 geometry.width = luaA_getopt_number(L, 2, "width", c->geometry.width);
1659 geometry.height = luaA_getopt_number(L, 2, "height", c->geometry.height);
1662 client_resize(c, geometry, c->size_hints_honor);
1665 return luaA_pusharea(L, c->geometry);
1668 static int
1669 luaA_client_set_screen(lua_State *L, client_t *c)
1671 int screen = luaL_checknumber(L, -1) - 1;
1672 luaA_checkscreen(screen);
1673 screen_client_moveto(c, &globalconf.screens.tab[screen], true);
1675 return 0;
1678 static int
1679 luaA_client_set_hidden(lua_State *L, client_t *c)
1681 client_set_hidden(L, -3, luaA_checkboolean(L, -1));
1682 return 0;
1685 static int
1686 luaA_client_set_minimized(lua_State *L, client_t *c)
1688 client_set_minimized(L, -3, luaA_checkboolean(L, -1));
1689 return 0;
1692 static int
1693 luaA_client_set_fullscreen(lua_State *L, client_t *c)
1695 client_set_fullscreen(L, -3, luaA_checkboolean(L, -1));
1696 return 0;
1699 static int
1700 luaA_client_set_modal(lua_State *L, client_t *c)
1702 client_set_modal(L, -3, luaA_checkboolean(L, -1));
1703 return 0;
1706 static int
1707 luaA_client_set_maximized(lua_State *L, client_t *c)
1709 client_set_maximized(L, -3, luaA_checkboolean(L, -1));
1710 return 0;
1713 static int
1714 luaA_client_set_maximized_horizontal(lua_State *L, client_t *c)
1716 client_set_maximized_horizontal(L, -3, luaA_checkboolean(L, -1));
1717 return 0;
1720 static int
1721 luaA_client_set_maximized_vertical(lua_State *L, client_t *c)
1723 client_set_maximized_vertical(L, -3, luaA_checkboolean(L, -1));
1724 return 0;
1727 static int
1728 luaA_client_set_icon(lua_State *L, client_t *c)
1730 cairo_surface_t *surf = NULL;
1731 if(!lua_isnil(L, -1))
1732 surf = (cairo_surface_t *)lua_touserdata(L, -1);
1733 client_set_icon(c, surf);
1734 return 0;
1737 static int
1738 luaA_client_set_sticky(lua_State *L, client_t *c)
1740 client_set_sticky(L, -3, luaA_checkboolean(L, -1));
1741 return 0;
1744 static int
1745 luaA_client_set_size_hints_honor(lua_State *L, client_t *c)
1747 c->size_hints_honor = luaA_checkboolean(L, -1);
1748 luaA_object_emit_signal(L, -3, "property::size_hints_honor", 0);
1749 return 0;
1752 static int
1753 luaA_client_set_ontop(lua_State *L, client_t *c)
1755 client_set_ontop(L, -3, luaA_checkboolean(L, -1));
1756 return 0;
1759 static int
1760 luaA_client_set_below(lua_State *L, client_t *c)
1762 client_set_below(L, -3, luaA_checkboolean(L, -1));
1763 return 0;
1766 static int
1767 luaA_client_set_above(lua_State *L, client_t *c)
1769 client_set_above(L, -3, luaA_checkboolean(L, -1));
1770 return 0;
1773 static int
1774 luaA_client_set_urgent(lua_State *L, client_t *c)
1776 client_set_urgent(L, -3, luaA_checkboolean(L, -1));
1777 return 0;
1780 static int
1781 luaA_client_set_skip_taskbar(lua_State *L, client_t *c)
1783 client_set_skip_taskbar(L, -3, luaA_checkboolean(L, -1));
1784 return 0;
1787 static int
1788 luaA_client_get_name(lua_State *L, client_t *c)
1790 lua_pushstring(L, c->name ? c->name : c->alt_name);
1791 return 1;
1794 static int
1795 luaA_client_get_icon_name(lua_State *L, client_t *c)
1797 lua_pushstring(L, c->icon_name ? c->icon_name : c->alt_icon_name);
1798 return 1;
1801 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, class, lua_pushstring)
1802 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, instance, lua_pushstring)
1803 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, machine, lua_pushstring)
1804 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, role, lua_pushstring)
1805 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, transient_for, luaA_object_push)
1806 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, skip_taskbar, lua_pushboolean)
1807 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, leader_window, lua_pushnumber)
1808 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, group_window, lua_pushnumber)
1809 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, pid, lua_pushnumber)
1810 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, hidden, lua_pushboolean)
1811 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, minimized, lua_pushboolean)
1812 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, fullscreen, lua_pushboolean)
1813 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, modal, lua_pushboolean)
1814 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, ontop, lua_pushboolean)
1815 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, urgent, lua_pushboolean)
1816 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, above, lua_pushboolean)
1817 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, below, lua_pushboolean)
1818 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, sticky, lua_pushboolean)
1819 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, size_hints_honor, lua_pushboolean)
1820 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, maximized_horizontal, lua_pushboolean)
1821 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, maximized_vertical, lua_pushboolean)
1823 static int
1824 luaA_client_get_maximized(lua_State *L, client_t *c)
1826 lua_pushboolean(L, client_get_maximized(c));
1827 return 1;
1830 static int
1831 luaA_client_get_content(lua_State *L, client_t *c)
1833 xcb_get_window_attributes_cookie_t cookie;
1834 xcb_get_window_attributes_reply_t *attr;
1835 cairo_surface_t *surface;
1836 int width = c->geometry.width;
1837 int height = c->geometry.height;
1839 /* Just the client size without decorations */
1840 width -= c->titlebar[CLIENT_TITLEBAR_LEFT].size + c->titlebar[CLIENT_TITLEBAR_RIGHT].size;
1841 height -= c->titlebar[CLIENT_TITLEBAR_TOP].size + c->titlebar[CLIENT_TITLEBAR_BOTTOM].size;
1843 cookie = xcb_get_window_attributes(globalconf.connection, c->window);
1844 attr = xcb_get_window_attributes_reply(globalconf.connection, cookie, NULL);
1846 if (!attr)
1847 return 0;
1849 surface = cairo_xcb_surface_create(globalconf.connection, c->window,
1850 draw_find_visual(globalconf.screen, attr->visual),
1851 width, height);
1853 /* lua has to make sure to free the ref or we have a leak */
1854 lua_pushlightuserdata(L, surface);
1855 free(attr);
1856 return 1;
1859 static int
1860 luaA_client_get_screen(lua_State *L, client_t *c)
1862 if(!c->screen)
1863 return 0;
1864 lua_pushnumber(L, 1 + screen_array_indexof(&globalconf.screens, c->screen));
1865 return 1;
1868 static int
1869 luaA_client_get_icon(lua_State *L, client_t *c)
1871 if(!c->icon)
1872 return 0;
1873 /* lua gets its own reference which it will have to destroy */
1874 lua_pushlightuserdata(L, cairo_surface_reference(c->icon));
1875 return 1;
1878 static int
1879 luaA_client_get_focusable(lua_State *L, client_t *c)
1881 bool ret;
1883 /* A client can be focused if it doesnt have the "nofocus" hint...*/
1884 if (!c->nofocus)
1885 ret = true;
1886 else
1887 /* ...or if it knows the WM_TAKE_FOCUS protocol */
1888 ret = client_hasproto(c, WM_TAKE_FOCUS);
1890 lua_pushboolean(L, ret);
1891 return 1;
1894 static int
1895 luaA_client_get_size_hints(lua_State *L, client_t *c)
1897 const char *u_or_p = NULL;
1899 lua_createtable(L, 0, 1);
1901 if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_US_POSITION)
1902 u_or_p = "user_position";
1903 else if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_POSITION)
1904 u_or_p = "program_position";
1906 if(u_or_p)
1908 lua_createtable(L, 0, 2);
1909 lua_pushnumber(L, c->size_hints.x);
1910 lua_setfield(L, -2, "x");
1911 lua_pushnumber(L, c->size_hints.y);
1912 lua_setfield(L, -2, "y");
1913 lua_setfield(L, -2, u_or_p);
1914 u_or_p = NULL;
1917 if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_US_SIZE)
1918 u_or_p = "user_size";
1919 else if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_SIZE)
1920 u_or_p = "program_size";
1922 if(u_or_p)
1924 lua_createtable(L, 0, 2);
1925 lua_pushnumber(L, c->size_hints.width);
1926 lua_setfield(L, -2, "width");
1927 lua_pushnumber(L, c->size_hints.height);
1928 lua_setfield(L, -2, "height");
1929 lua_setfield(L, -2, u_or_p);
1932 if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE)
1934 lua_pushnumber(L, c->size_hints.min_width);
1935 lua_setfield(L, -2, "min_width");
1936 lua_pushnumber(L, c->size_hints.min_height);
1937 lua_setfield(L, -2, "min_height");
1940 if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE)
1942 lua_pushnumber(L, c->size_hints.max_width);
1943 lua_setfield(L, -2, "max_width");
1944 lua_pushnumber(L, c->size_hints.max_height);
1945 lua_setfield(L, -2, "max_height");
1948 if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC)
1950 lua_pushnumber(L, c->size_hints.width_inc);
1951 lua_setfield(L, -2, "width_inc");
1952 lua_pushnumber(L, c->size_hints.height_inc);
1953 lua_setfield(L, -2, "height_inc");
1956 if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_ASPECT)
1958 lua_pushnumber(L, c->size_hints.min_aspect_num);
1959 lua_setfield(L, -2, "min_aspect_num");
1960 lua_pushnumber(L, c->size_hints.min_aspect_den);
1961 lua_setfield(L, -2, "min_aspect_den");
1962 lua_pushnumber(L, c->size_hints.max_aspect_num);
1963 lua_setfield(L, -2, "max_aspect_num");
1964 lua_pushnumber(L, c->size_hints.max_aspect_den);
1965 lua_setfield(L, -2, "max_aspect_den");
1968 if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_BASE_SIZE)
1970 lua_pushnumber(L, c->size_hints.base_width);
1971 lua_setfield(L, -2, "base_width");
1972 lua_pushnumber(L, c->size_hints.base_height);
1973 lua_setfield(L, -2, "base_height");
1976 if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_WIN_GRAVITY)
1978 switch(c->size_hints.win_gravity)
1980 default:
1981 lua_pushliteral(L, "north_west");
1982 break;
1983 case XCB_GRAVITY_NORTH:
1984 lua_pushliteral(L, "north");
1985 break;
1986 case XCB_GRAVITY_NORTH_EAST:
1987 lua_pushliteral(L, "north_east");
1988 break;
1989 case XCB_GRAVITY_WEST:
1990 lua_pushliteral(L, "west");
1991 break;
1992 case XCB_GRAVITY_CENTER:
1993 lua_pushliteral(L, "center");
1994 break;
1995 case XCB_GRAVITY_EAST:
1996 lua_pushliteral(L, "east");
1997 break;
1998 case XCB_GRAVITY_SOUTH_WEST:
1999 lua_pushliteral(L, "south_west");
2000 break;
2001 case XCB_GRAVITY_SOUTH:
2002 lua_pushliteral(L, "south");
2003 break;
2004 case XCB_GRAVITY_SOUTH_EAST:
2005 lua_pushliteral(L, "south_east");
2006 break;
2007 case XCB_GRAVITY_STATIC:
2008 lua_pushliteral(L, "static");
2009 break;
2011 lua_setfield(L, -2, "win_gravity");
2014 return 1;
2017 /** Get the client's child window bounding shape.
2018 * \param L The Lua VM state.
2019 * \param client The client object.
2020 * \return The number of elements pushed on stack.
2022 static int
2023 luaA_client_get_client_shape_bounding(lua_State *L, client_t *c)
2025 cairo_surface_t *surf = xwindow_get_shape(c->window, XCB_SHAPE_SK_BOUNDING);
2026 if (!surf)
2027 return 0;
2028 /* lua has to make sure to free the ref or we have a leak */
2029 lua_pushlightuserdata(L, surf);
2030 return 1;
2033 /** Get the client's frame window bounding shape.
2034 * \param L The Lua VM state.
2035 * \param client The client object.
2036 * \return The number of elements pushed on stack.
2038 static int
2039 luaA_client_get_shape_bounding(lua_State *L, client_t *c)
2041 cairo_surface_t *surf = xwindow_get_shape(c->frame_window, XCB_SHAPE_SK_BOUNDING);
2042 if (!surf)
2043 return 0;
2044 /* lua has to make sure to free the ref or we have a leak */
2045 lua_pushlightuserdata(L, surf);
2046 return 1;
2049 /** Set the client's frame window bounding shape.
2050 * \param L The Lua VM state.
2051 * \param client The client object.
2052 * \return The number of elements pushed on stack.
2054 static int
2055 luaA_client_set_shape_bounding(lua_State *L, client_t *c)
2057 cairo_surface_t *surf = NULL;
2058 if(!lua_isnil(L, -1))
2059 surf = (cairo_surface_t *)lua_touserdata(L, -1);
2060 xwindow_set_shape(c->frame_window,
2061 c->geometry.width + (c->border_width * 2),
2062 c->geometry.height + (c->border_width * 2),
2063 XCB_SHAPE_SK_BOUNDING, surf, -c->border_width);
2064 luaA_object_emit_signal(L, -3, "property::shape_bounding", 0);
2065 return 0;
2068 /** Get the client's child window clip shape.
2069 * \param L The Lua VM state.
2070 * \param client The client object.
2071 * \return The number of elements pushed on stack.
2073 static int
2074 luaA_client_get_client_shape_clip(lua_State *L, client_t *c)
2076 cairo_surface_t *surf = xwindow_get_shape(c->window, XCB_SHAPE_SK_CLIP);
2077 if (!surf)
2078 return 0;
2079 /* lua has to make sure to free the ref or we have a leak */
2080 lua_pushlightuserdata(L, surf);
2081 return 1;
2084 /** Get the client's frame window clip shape.
2085 * \param L The Lua VM state.
2086 * \param client The client object.
2087 * \return The number of elements pushed on stack.
2089 static int
2090 luaA_client_get_shape_clip(lua_State *L, client_t *c)
2092 cairo_surface_t *surf = xwindow_get_shape(c->frame_window, XCB_SHAPE_SK_CLIP);
2093 if (!surf)
2094 return 0;
2095 /* lua has to make sure to free the ref or we have a leak */
2096 lua_pushlightuserdata(L, surf);
2097 return 1;
2100 /** Set the client's frame window clip shape.
2101 * \param L The Lua VM state.
2102 * \param client The client object.
2103 * \return The number of elements pushed on stack.
2105 static int
2106 luaA_client_set_shape_clip(lua_State *L, client_t *c)
2108 cairo_surface_t *surf = NULL;
2109 if(!lua_isnil(L, -1))
2110 surf = (cairo_surface_t *)lua_touserdata(L, -1);
2111 xwindow_set_shape(c->frame_window, c->geometry.width, c->geometry.height,
2112 XCB_SHAPE_SK_CLIP, surf, 0);
2113 luaA_object_emit_signal(L, -3, "property::shape_clip", 0);
2114 return 0;
2117 /** Get or set keys bindings for a client.
2118 * \param L The Lua VM state.
2119 * \return The number of element pushed on stack.
2120 * \luastack
2121 * \lvalue A client.
2122 * \lparam An array of key bindings objects, or nothing.
2123 * \return The array of key bindings objects of this client.
2125 static int
2126 luaA_client_keys(lua_State *L)
2128 client_t *c = luaA_checkudata(L, 1, &client_class);
2129 key_array_t *keys = &c->keys;
2131 if(lua_gettop(L) == 2)
2133 luaA_key_array_set(L, 1, 2, keys);
2134 luaA_object_emit_signal(L, 1, "property::keys", 0);
2135 xcb_ungrab_key(globalconf.connection, XCB_GRAB_ANY, c->frame_window, XCB_BUTTON_MASK_ANY);
2136 xwindow_grabkeys(c->frame_window, keys);
2139 return luaA_key_array_get(L, 1, keys);
2142 /* Client module.
2143 * \param L The Lua VM state.
2144 * \return The number of pushed elements.
2146 static int
2147 luaA_client_module_index(lua_State *L)
2149 const char *buf = luaL_checkstring(L, 2);
2151 if (A_STREQ(buf, "focus"))
2152 return luaA_object_push(globalconf.L, globalconf.focus.client);
2153 return 0;
2156 /* Client module new index.
2157 * \param L The Lua VM state.
2158 * \return The number of pushed elements.
2160 static int
2161 luaA_client_module_newindex(lua_State *L)
2163 const char *buf = luaL_checkstring(L, 2);
2164 client_t *c;
2166 if (A_STREQ(buf, "focus"))
2168 c = luaA_checkudata(L, 3, &client_class);
2169 client_focus(c);
2172 return 0;
2175 static bool
2176 client_checker(client_t *c)
2178 return c->window != XCB_NONE;
2181 void
2182 client_class_setup(lua_State *L)
2184 static const struct luaL_Reg client_methods[] =
2186 LUA_CLASS_METHODS(client)
2187 { "get", luaA_client_get },
2188 { "__index", luaA_client_module_index },
2189 { "__newindex", luaA_client_module_newindex },
2190 { NULL, NULL }
2193 static const struct luaL_Reg client_meta[] =
2195 LUA_OBJECT_META(client)
2196 LUA_CLASS_META
2197 { "keys", luaA_client_keys },
2198 { "isvisible", luaA_client_isvisible },
2199 { "geometry", luaA_client_geometry },
2200 { "tags", luaA_client_tags },
2201 { "kill", luaA_client_kill },
2202 { "swap", luaA_client_swap },
2203 { "raise", luaA_client_raise },
2204 { "lower", luaA_client_lower },
2205 { "unmanage", luaA_client_unmanage },
2206 { "titlebar_top", luaA_client_titlebar_top },
2207 { "titlebar_right", luaA_client_titlebar_right },
2208 { "titlebar_bottom", luaA_client_titlebar_bottom },
2209 { "titlebar_left", luaA_client_titlebar_left },
2210 { NULL, NULL }
2213 luaA_class_setup(L, &client_class, "client", &window_class,
2214 (lua_class_allocator_t) client_new,
2215 (lua_class_collector_t) client_wipe,
2216 (lua_class_checker_t) client_checker,
2217 luaA_class_index_miss_property, luaA_class_newindex_miss_property,
2218 client_methods, client_meta);
2219 luaA_class_add_property(&client_class, "name",
2220 NULL,
2221 (lua_class_propfunc_t) luaA_client_get_name,
2222 NULL);
2223 luaA_class_add_property(&client_class, "transient_for",
2224 NULL,
2225 (lua_class_propfunc_t) luaA_client_get_transient_for,
2226 NULL);
2227 luaA_class_add_property(&client_class, "skip_taskbar",
2228 (lua_class_propfunc_t) luaA_client_set_skip_taskbar,
2229 (lua_class_propfunc_t) luaA_client_get_skip_taskbar,
2230 (lua_class_propfunc_t) luaA_client_set_skip_taskbar);
2231 luaA_class_add_property(&client_class, "content",
2232 NULL,
2233 (lua_class_propfunc_t) luaA_client_get_content,
2234 NULL);
2235 luaA_class_add_property(&client_class, "type",
2236 NULL,
2237 (lua_class_propfunc_t) luaA_window_get_type,
2238 NULL);
2239 luaA_class_add_property(&client_class, "class",
2240 NULL,
2241 (lua_class_propfunc_t) luaA_client_get_class,
2242 NULL);
2243 luaA_class_add_property(&client_class, "instance",
2244 NULL,
2245 (lua_class_propfunc_t) luaA_client_get_instance,
2246 NULL);
2247 luaA_class_add_property(&client_class, "role",
2248 NULL,
2249 (lua_class_propfunc_t) luaA_client_get_role,
2250 NULL);
2251 luaA_class_add_property(&client_class, "pid",
2252 NULL,
2253 (lua_class_propfunc_t) luaA_client_get_pid,
2254 NULL);
2255 luaA_class_add_property(&client_class, "leader_window",
2256 NULL,
2257 (lua_class_propfunc_t) luaA_client_get_leader_window,
2258 NULL);
2259 luaA_class_add_property(&client_class, "machine",
2260 NULL,
2261 (lua_class_propfunc_t) luaA_client_get_machine,
2262 NULL);
2263 luaA_class_add_property(&client_class, "icon_name",
2264 NULL,
2265 (lua_class_propfunc_t) luaA_client_get_icon_name,
2266 NULL);
2267 luaA_class_add_property(&client_class, "screen",
2268 NULL,
2269 (lua_class_propfunc_t) luaA_client_get_screen,
2270 (lua_class_propfunc_t) luaA_client_set_screen);
2271 luaA_class_add_property(&client_class, "hidden",
2272 (lua_class_propfunc_t) luaA_client_set_hidden,
2273 (lua_class_propfunc_t) luaA_client_get_hidden,
2274 (lua_class_propfunc_t) luaA_client_set_hidden);
2275 luaA_class_add_property(&client_class, "minimized",
2276 (lua_class_propfunc_t) luaA_client_set_minimized,
2277 (lua_class_propfunc_t) luaA_client_get_minimized,
2278 (lua_class_propfunc_t) luaA_client_set_minimized);
2279 luaA_class_add_property(&client_class, "fullscreen",
2280 (lua_class_propfunc_t) luaA_client_set_fullscreen,
2281 (lua_class_propfunc_t) luaA_client_get_fullscreen,
2282 (lua_class_propfunc_t) luaA_client_set_fullscreen);
2283 luaA_class_add_property(&client_class, "modal",
2284 (lua_class_propfunc_t) luaA_client_set_modal,
2285 (lua_class_propfunc_t) luaA_client_get_modal,
2286 (lua_class_propfunc_t) luaA_client_set_modal);
2287 luaA_class_add_property(&client_class, "group_window",
2288 NULL,
2289 (lua_class_propfunc_t) luaA_client_get_group_window,
2290 NULL);
2291 luaA_class_add_property(&client_class, "maximized",
2292 (lua_class_propfunc_t) luaA_client_set_maximized,
2293 (lua_class_propfunc_t) luaA_client_get_maximized,
2294 (lua_class_propfunc_t) luaA_client_set_maximized);
2295 luaA_class_add_property(&client_class, "maximized_horizontal",
2296 (lua_class_propfunc_t) luaA_client_set_maximized_horizontal,
2297 (lua_class_propfunc_t) luaA_client_get_maximized_horizontal,
2298 (lua_class_propfunc_t) luaA_client_set_maximized_horizontal);
2299 luaA_class_add_property(&client_class, "maximized_vertical",
2300 (lua_class_propfunc_t) luaA_client_set_maximized_vertical,
2301 (lua_class_propfunc_t) luaA_client_get_maximized_vertical,
2302 (lua_class_propfunc_t) luaA_client_set_maximized_vertical);
2303 luaA_class_add_property(&client_class, "icon",
2304 (lua_class_propfunc_t) luaA_client_set_icon,
2305 (lua_class_propfunc_t) luaA_client_get_icon,
2306 (lua_class_propfunc_t) luaA_client_set_icon);
2307 luaA_class_add_property(&client_class, "ontop",
2308 (lua_class_propfunc_t) luaA_client_set_ontop,
2309 (lua_class_propfunc_t) luaA_client_get_ontop,
2310 (lua_class_propfunc_t) luaA_client_set_ontop);
2311 luaA_class_add_property(&client_class, "above",
2312 (lua_class_propfunc_t) luaA_client_set_above,
2313 (lua_class_propfunc_t) luaA_client_get_above,
2314 (lua_class_propfunc_t) luaA_client_set_above);
2315 luaA_class_add_property(&client_class, "below",
2316 (lua_class_propfunc_t) luaA_client_set_below,
2317 (lua_class_propfunc_t) luaA_client_get_below,
2318 (lua_class_propfunc_t) luaA_client_set_below);
2319 luaA_class_add_property(&client_class, "sticky",
2320 (lua_class_propfunc_t) luaA_client_set_sticky,
2321 (lua_class_propfunc_t) luaA_client_get_sticky,
2322 (lua_class_propfunc_t) luaA_client_set_sticky);
2323 luaA_class_add_property(&client_class, "size_hints_honor",
2324 (lua_class_propfunc_t) luaA_client_set_size_hints_honor,
2325 (lua_class_propfunc_t) luaA_client_get_size_hints_honor,
2326 (lua_class_propfunc_t) luaA_client_set_size_hints_honor);
2327 luaA_class_add_property(&client_class, "urgent",
2328 (lua_class_propfunc_t) luaA_client_set_urgent,
2329 (lua_class_propfunc_t) luaA_client_get_urgent,
2330 (lua_class_propfunc_t) luaA_client_set_urgent);
2331 luaA_class_add_property(&client_class, "size_hints",
2332 NULL,
2333 (lua_class_propfunc_t) luaA_client_get_size_hints,
2334 NULL);
2335 luaA_class_add_property(&client_class, "focusable",
2336 NULL,
2337 (lua_class_propfunc_t) luaA_client_get_focusable,
2338 NULL);
2339 luaA_class_add_property(&client_class, "shape_bounding",
2340 (lua_class_propfunc_t) luaA_client_set_shape_bounding,
2341 (lua_class_propfunc_t) luaA_client_get_shape_bounding,
2342 (lua_class_propfunc_t) luaA_client_set_shape_bounding);
2343 luaA_class_add_property(&client_class, "shape_clip",
2344 (lua_class_propfunc_t) luaA_client_set_shape_clip,
2345 (lua_class_propfunc_t) luaA_client_get_shape_clip,
2346 (lua_class_propfunc_t) luaA_client_set_shape_clip);
2347 luaA_class_add_property(&client_class, "client_shape_bounding",
2348 NULL,
2349 (lua_class_propfunc_t) luaA_client_get_client_shape_bounding,
2350 NULL);
2351 luaA_class_add_property(&client_class, "client_shape_clip",
2352 NULL,
2353 (lua_class_propfunc_t) luaA_client_get_client_shape_clip,
2354 NULL);
2356 signal_add(&client_class.signals, "focus");
2357 signal_add(&client_class.signals, "list");
2358 signal_add(&client_class.signals, "manage");
2359 signal_add(&client_class.signals, "button::press");
2360 signal_add(&client_class.signals, "button::release");
2361 signal_add(&client_class.signals, "mouse::enter");
2362 signal_add(&client_class.signals, "mouse::leave");
2363 signal_add(&client_class.signals, "mouse::move");
2364 signal_add(&client_class.signals, "property::above");
2365 signal_add(&client_class.signals, "property::below");
2366 signal_add(&client_class.signals, "property::class");
2367 signal_add(&client_class.signals, "property::fullscreen");
2368 signal_add(&client_class.signals, "property::geometry");
2369 signal_add(&client_class.signals, "property::group_window");
2370 signal_add(&client_class.signals, "property::height");
2371 signal_add(&client_class.signals, "property::hidden");
2372 signal_add(&client_class.signals, "property::icon");
2373 signal_add(&client_class.signals, "property::icon_name");
2374 signal_add(&client_class.signals, "property::instance");
2375 signal_add(&client_class.signals, "property::keys");
2376 signal_add(&client_class.signals, "property::machine");
2377 signal_add(&client_class.signals, "property::maximized");
2378 signal_add(&client_class.signals, "property::maximized_horizontal");
2379 signal_add(&client_class.signals, "property::maximized_vertical");
2380 signal_add(&client_class.signals, "property::minimized");
2381 signal_add(&client_class.signals, "property::modal");
2382 signal_add(&client_class.signals, "property::name");
2383 signal_add(&client_class.signals, "property::ontop");
2384 signal_add(&client_class.signals, "property::pid");
2385 signal_add(&client_class.signals, "property::role");
2386 signal_add(&client_class.signals, "property::screen");
2387 signal_add(&client_class.signals, "property::shape_bounding");
2388 signal_add(&client_class.signals, "property::shape_client_bounding");
2389 signal_add(&client_class.signals, "property::shape_client_clip");
2390 signal_add(&client_class.signals, "property::shape_clip");
2391 signal_add(&client_class.signals, "property::size_hints_honor");
2392 signal_add(&client_class.signals, "property::skip_taskbar");
2393 signal_add(&client_class.signals, "property::sticky");
2394 signal_add(&client_class.signals, "property::struts");
2395 signal_add(&client_class.signals, "property::transient_for");
2396 signal_add(&client_class.signals, "property::type");
2397 signal_add(&client_class.signals, "property::urgent");
2398 signal_add(&client_class.signals, "property::width");
2399 signal_add(&client_class.signals, "property::window");
2400 signal_add(&client_class.signals, "property::x");
2401 signal_add(&client_class.signals, "property::y");
2402 signal_add(&client_class.signals, "request::activate");
2403 signal_add(&client_class.signals, "request::fullscreen");
2404 signal_add(&client_class.signals, "request::maximized_horizontal");
2405 signal_add(&client_class.signals, "request::maximized_vertical");
2406 signal_add(&client_class.signals, "tagged");
2407 signal_add(&client_class.signals, "unfocus");
2408 signal_add(&client_class.signals, "unmanage");
2409 signal_add(&client_class.signals, "untagged");
2412 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80