tag: Improve tag property::index support (FS#1229)
[awesome.git] / objects / client.c
blob148f8a588c725b76e294d58ea376d21761e03ed9
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 "objects/client.h"
23 #include "common/atoms.h"
24 #include "common/xutil.h"
25 #include "event.h"
26 #include "ewmh.h"
27 #include "objects/drawable.h"
28 #include "objects/screen.h"
29 #include "objects/tag.h"
30 #include "property.h"
31 #include "spawn.h"
32 #include "systray.h"
33 #include "xwindow.h"
35 #include <xcb/xcb_atom.h>
36 #include <xcb/shape.h>
37 #include <cairo-xcb.h>
39 static area_t titlebar_get_area(client_t *c, client_titlebar_t bar);
40 static drawable_t *titlebar_get_drawable(lua_State *L, client_t *c, int cl_idx, client_titlebar_t bar);
42 /** Collect a client.
43 * \param L The Lua VM state.
44 * \return The number of element pushed on stack.
46 static void
47 client_wipe(client_t *c)
49 key_array_wipe(&c->keys);
50 xcb_icccm_get_wm_protocols_reply_wipe(&c->protocols);
51 p_delete(&c->machine);
52 p_delete(&c->class);
53 p_delete(&c->instance);
54 p_delete(&c->icon_name);
55 p_delete(&c->alt_icon_name);
56 p_delete(&c->name);
57 p_delete(&c->alt_name);
58 p_delete(&c->startup_id);
59 if(c->icon)
60 cairo_surface_destroy(c->icon);
63 /** Change the clients urgency flag.
64 * \param L The Lua VM state.
65 * \param cidx The client index on the stack.
66 * \param urgent The new flag state.
68 void
69 client_set_urgent(lua_State *L, int cidx, bool urgent)
71 client_t *c = luaA_checkudata(L, cidx, &client_class);
73 if(c->urgent != urgent)
75 xcb_get_property_cookie_t hints =
76 xcb_icccm_get_wm_hints_unchecked(globalconf.connection, c->window);
78 c->urgent = urgent;
80 /* update ICCCM hints */
81 xcb_icccm_wm_hints_t wmh;
82 xcb_icccm_get_wm_hints_reply(globalconf.connection, hints, &wmh, NULL);
84 if(urgent)
85 wmh.flags |= XCB_ICCCM_WM_HINT_X_URGENCY;
86 else
87 wmh.flags &= ~XCB_ICCCM_WM_HINT_X_URGENCY;
89 xcb_icccm_set_wm_hints(globalconf.connection, c->window, &wmh);
91 luaA_object_emit_signal(L, cidx, "property::urgent", 0);
95 #define DO_CLIENT_SET_PROPERTY(prop) \
96 void \
97 client_set_##prop(lua_State *L, int cidx, fieldtypeof(client_t, prop) value) \
98 { \
99 client_t *c = luaA_checkudata(L, cidx, &client_class); \
100 if(c->prop != value) \
102 c->prop = value; \
103 luaA_object_emit_signal(L, cidx, "property::" #prop, 0); \
106 DO_CLIENT_SET_PROPERTY(group_window)
107 DO_CLIENT_SET_PROPERTY(type)
108 DO_CLIENT_SET_PROPERTY(transient_for)
109 DO_CLIENT_SET_PROPERTY(pid)
110 DO_CLIENT_SET_PROPERTY(skip_taskbar)
111 #undef DO_CLIENT_SET_PROPERTY
113 #define DO_CLIENT_SET_STRING_PROPERTY2(prop, signal) \
114 void \
115 client_set_##prop(lua_State *L, int cidx, char *value) \
117 client_t *c = luaA_checkudata(L, cidx, &client_class); \
118 if (A_STREQ(c->prop, value)) \
120 p_delete(&value); \
121 return; \
123 p_delete(&c->prop); \
124 c->prop = value; \
125 luaA_object_emit_signal(L, cidx, "property::" #signal, 0); \
127 #define DO_CLIENT_SET_STRING_PROPERTY(prop) \
128 DO_CLIENT_SET_STRING_PROPERTY2(prop, prop)
129 DO_CLIENT_SET_STRING_PROPERTY(name)
130 DO_CLIENT_SET_STRING_PROPERTY2(alt_name, name)
131 DO_CLIENT_SET_STRING_PROPERTY(icon_name)
132 DO_CLIENT_SET_STRING_PROPERTY2(alt_icon_name, icon_name)
133 DO_CLIENT_SET_STRING_PROPERTY(role)
134 DO_CLIENT_SET_STRING_PROPERTY(machine)
135 #undef DO_CLIENT_SET_STRING_PROPERTY
137 void
138 client_set_class_instance(lua_State *L, int cidx, const char *class, const char *instance)
140 client_t *c = luaA_checkudata(L, cidx, &client_class);
141 p_delete(&c->class);
142 p_delete(&c->instance);
143 c->class = a_strdup(class);
144 luaA_object_emit_signal(L, cidx, "property::class", 0);
145 c->instance = a_strdup(instance);
146 luaA_object_emit_signal(L, cidx, "property::instance", 0);
149 /** Returns true if a client is tagged
150 * with one of the tags of the specified screen.
151 * \param c The client to check.
152 * \param screen Virtual screen.
153 * \return true if the client is visible, false otherwise.
155 bool
156 client_maybevisible(client_t *c)
158 if(c->sticky)
159 return true;
161 foreach(tag, globalconf.tags)
162 if(tag_get_selected(*tag) && is_client_tagged(c, *tag))
163 return true;
165 return false;
168 /** Get a client by its window.
169 * \param w The client window to find.
170 * \return A client pointer if found, NULL otherwise.
172 client_t *
173 client_getbywin(xcb_window_t w)
175 foreach(c, globalconf.clients)
176 if((*c)->window == w)
177 return *c;
179 return NULL;
182 /** Get a client by its frame window.
183 * \param w The client window to find.
184 * \return A client pointer if found, NULL otherwise.
186 client_t *
187 client_getbyframewin(xcb_window_t w)
189 foreach(c, globalconf.clients)
190 if((*c)->frame_window == w)
191 return *c;
193 return NULL;
196 /** Unfocus a client (internal).
197 * \param c The client.
199 static void
200 client_unfocus_internal(client_t *c)
202 globalconf.focus.client = NULL;
204 luaA_object_push(globalconf.L, c);
205 luaA_object_emit_signal(globalconf.L, -1, "unfocus", 0);
206 lua_pop(globalconf.L, 1);
209 /** Unfocus a client.
210 * \param c The client.
212 static void
213 client_unfocus(client_t *c)
215 client_unfocus_internal(c);
216 globalconf.focus.need_update = true;
219 /** Check if client supports atom a protocol in WM_PROTOCOL.
220 * \param c The client.
221 * \param atom The protocol atom to check for.
222 * \return True if client has the atom in protocol, false otherwise.
224 bool
225 client_hasproto(client_t *c, xcb_atom_t atom)
227 for(uint32_t i = 0; i < c->protocols.atoms_len; i++)
228 if(c->protocols.atoms[i] == atom)
229 return true;
230 return false;
233 /** Prepare banning a client by running all needed lua events.
234 * \param c The client.
236 void client_ban_unfocus(client_t *c)
238 /* Wait until the last moment to take away the focus from the window. */
239 if(globalconf.focus.client == c)
240 client_unfocus(c);
243 /** Ban client and move it out of the viewport.
244 * \param c The client.
246 void
247 client_ban(client_t *c)
249 if(!c->isbanned)
251 xcb_unmap_window(globalconf.connection, c->frame_window);
253 c->isbanned = true;
255 client_ban_unfocus(c);
259 /** This is part of The Bob Marley Algorithm: we ignore enter and leave window
260 * in certain cases, like map/unmap or move, so we don't get spurious events.
262 void
263 client_ignore_enterleave_events(void)
265 foreach(c, globalconf.clients)
267 xcb_change_window_attributes(globalconf.connection,
268 (*c)->window,
269 XCB_CW_EVENT_MASK,
270 (const uint32_t []) { CLIENT_SELECT_INPUT_EVENT_MASK & ~(XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_LEAVE_WINDOW) });
271 xcb_change_window_attributes(globalconf.connection,
272 (*c)->frame_window,
273 XCB_CW_EVENT_MASK,
274 (const uint32_t []) { FRAME_SELECT_INPUT_EVENT_MASK & ~(XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_LEAVE_WINDOW) });
278 void
279 client_restore_enterleave_events(void)
281 foreach(c, globalconf.clients)
283 xcb_change_window_attributes(globalconf.connection,
284 (*c)->window,
285 XCB_CW_EVENT_MASK,
286 (const uint32_t []) { CLIENT_SELECT_INPUT_EVENT_MASK });
287 xcb_change_window_attributes(globalconf.connection,
288 (*c)->frame_window,
289 XCB_CW_EVENT_MASK,
290 (const uint32_t []) { FRAME_SELECT_INPUT_EVENT_MASK });
294 /** Record that a client got focus.
295 * \param c The client.
297 void
298 client_focus_update(client_t *c)
300 if(!client_maybevisible(c))
301 return;
303 if(globalconf.focus.client)
305 if (globalconf.focus.client == c)
306 /* Already focused */
307 return;
309 /* When we are called due to a FocusIn event (=old focused client
310 * already unfocused), we don't want to cause a SetInputFocus,
311 * because the client which has focus now could be using globally
312 * active input model (or 'no input').
314 client_unfocus_internal(globalconf.focus.client);
317 globalconf.focus.client = c;
319 /* according to EWMH, we have to remove the urgent state from a client */
320 luaA_object_push(globalconf.L, c);
321 client_set_urgent(globalconf.L, -1, false);
323 luaA_object_emit_signal(globalconf.L, -1, "focus", 0);
324 lua_pop(globalconf.L, 1);
327 /** Give focus to client, or to first client if client is NULL.
328 * \param c The client.
330 void
331 client_focus(client_t *c)
333 /* We have to set focus on first client */
334 if(!c && globalconf.clients.len && !(c = globalconf.clients.tab[0]))
335 return;
337 if(!client_maybevisible(c) || c == globalconf.focus.client)
338 return;
340 client_focus_update(c);
341 globalconf.focus.need_update = true;
344 void
345 client_focus_refresh(void)
347 client_t *c = globalconf.focus.client;
348 xcb_window_t win = globalconf.screen->root;
350 if(!globalconf.focus.need_update)
351 return;
352 globalconf.focus.need_update = false;
354 if(c)
356 /* Make sure this window is unbanned and e.g. not minimized */
357 client_unban(c);
358 /* Sets focus on window - using xcb_set_input_focus or WM_TAKE_FOCUS */
359 if(!c->nofocus)
360 win = c->window;
361 else
362 /* Focus the root window to make sure the previously focused client
363 * doesn't get any input in case WM_TAKE_FOCUS gets ignored.
365 win = globalconf.screen->root;
367 if(client_hasproto(c, WM_TAKE_FOCUS))
368 xwindow_takefocus(c->window);
371 /* If nothing has the focused or the currently focused client doesn't want
372 * us to focus it, this sets the focus to the root window. This makes sure
373 * the previously focused client actually gets unfocused. Alternatively, the
374 * new client gets the input focus.
376 xcb_set_input_focus(globalconf.connection, XCB_INPUT_FOCUS_PARENT,
377 win, globalconf.timestamp);
380 static void
381 client_update_properties(client_t *c)
383 /* get all hints */
384 xcb_get_property_cookie_t wm_normal_hints = property_get_wm_normal_hints(c);
385 xcb_get_property_cookie_t wm_hints = property_get_wm_hints(c);
386 xcb_get_property_cookie_t wm_transient_for = property_get_wm_transient_for(c);
387 xcb_get_property_cookie_t wm_client_leader = property_get_wm_client_leader(c);
388 xcb_get_property_cookie_t wm_client_machine = property_get_wm_client_machine(c);
389 xcb_get_property_cookie_t wm_window_role = property_get_wm_window_role(c);
390 xcb_get_property_cookie_t net_wm_pid = property_get_net_wm_pid(c);
391 xcb_get_property_cookie_t net_wm_icon = property_get_net_wm_icon(c);
392 xcb_get_property_cookie_t wm_name = property_get_wm_name(c);
393 xcb_get_property_cookie_t net_wm_name = property_get_net_wm_name(c);
394 xcb_get_property_cookie_t wm_icon_name = property_get_wm_icon_name(c);
395 xcb_get_property_cookie_t net_wm_icon_name = property_get_net_wm_icon_name(c);
396 xcb_get_property_cookie_t wm_class = property_get_wm_class(c);
397 xcb_get_property_cookie_t wm_protocols = property_get_wm_protocols(c);
398 xcb_get_property_cookie_t opacity = xwindow_get_opacity_unchecked(c->window);
400 /* update strut */
401 ewmh_process_client_strut(c);
403 /* Now process all replies */
404 property_update_wm_normal_hints(c, wm_normal_hints);
405 property_update_wm_hints(c, wm_hints);
406 property_update_wm_transient_for(c, wm_transient_for);
407 property_update_wm_client_leader(c, wm_client_leader);
408 property_update_wm_client_machine(c, wm_client_machine);
409 property_update_wm_window_role(c, wm_window_role);
410 property_update_net_wm_pid(c, net_wm_pid);
411 property_update_net_wm_icon(c, net_wm_icon);
412 property_update_wm_name(c, wm_name);
413 property_update_net_wm_name(c, net_wm_name);
414 property_update_wm_icon_name(c, wm_icon_name);
415 property_update_net_wm_icon_name(c, net_wm_icon_name);
416 property_update_wm_class(c, wm_class);
417 property_update_wm_protocols(c, wm_protocols);
418 window_set_opacity(globalconf.L, -1, xwindow_get_opacity_from_cookie(opacity));
421 /** Manage a new client.
422 * \param w The window.
423 * \param wgeom Window geometry.
424 * \param startup True if we are managing at startup time.
426 void
427 client_manage(xcb_window_t w, xcb_get_geometry_reply_t *wgeom, xcb_get_window_attributes_reply_t *wattr)
429 const uint32_t select_input_val[] = { CLIENT_SELECT_INPUT_EVENT_MASK };
431 if(systray_iskdedockapp(w))
433 systray_request_handle(w, NULL);
434 return;
437 /* If this is a new client that just has been launched, then request its
438 * startup id. */
439 xcb_get_property_cookie_t startup_id_q = xcb_get_property(globalconf.connection, false,
440 w, _NET_STARTUP_ID,
441 XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
443 /* Make sure the window is automatically mapped if awesome exits or dies. */
444 xcb_change_save_set(globalconf.connection, XCB_SET_MODE_INSERT, w);
445 if (globalconf.have_shape)
446 xcb_shape_select_input(globalconf.connection, w, 1);
448 client_t *c = client_new(globalconf.L);
449 xcb_screen_t *s = globalconf.screen;
451 /* consider the window banned */
452 c->isbanned = true;
453 /* Store window and visual */
454 c->window = w;
455 c->visualtype = draw_find_visual(globalconf.screen, wattr->visual);
456 c->frame_window = xcb_generate_id(globalconf.connection);
457 xcb_create_window(globalconf.connection, globalconf.default_depth, c->frame_window, s->root,
458 wgeom->x, wgeom->y, wgeom->width, wgeom->height,
459 wgeom->border_width, XCB_COPY_FROM_PARENT, globalconf.visual->visual_id,
460 XCB_CW_BORDER_PIXEL | XCB_CW_BIT_GRAVITY | XCB_CW_WIN_GRAVITY
461 | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK | XCB_CW_COLORMAP,
462 (const uint32_t [])
464 globalconf.screen->black_pixel,
465 XCB_GRAVITY_NORTH_WEST,
466 XCB_GRAVITY_NORTH_WEST,
468 FRAME_SELECT_INPUT_EVENT_MASK,
469 globalconf.default_cmap
472 /* The client may already be mapped, thus we must be sure that we don't send
473 * ourselves an UnmapNotify due to the xcb_reparent_window().
475 * Grab the server to make sure we don't lose any events.
477 uint32_t no_event[] = { 0 };
478 xcb_grab_server(globalconf.connection);
480 xcb_change_window_attributes(globalconf.connection,
481 globalconf.screen->root,
482 XCB_CW_EVENT_MASK,
483 no_event);
484 xcb_reparent_window(globalconf.connection, w, c->frame_window, 0, 0);
485 xcb_map_window(globalconf.connection, w);
486 xcb_change_window_attributes(globalconf.connection,
487 globalconf.screen->root,
488 XCB_CW_EVENT_MASK,
489 ROOT_WINDOW_EVENT_MASK);
490 xcb_ungrab_server(globalconf.connection);
492 /* Do this now so that we don't get any events for the above
493 * (Else, reparent could cause an UnmapNotify) */
494 xcb_change_window_attributes(globalconf.connection, w, XCB_CW_EVENT_MASK, select_input_val);
496 luaA_object_emit_signal(globalconf.L, -1, "property::window", 0);
498 /* The frame window gets the border, not the real client window */
499 xcb_configure_window(globalconf.connection, w,
500 XCB_CONFIG_WINDOW_BORDER_WIDTH,
501 (uint32_t[]) { 0 });
503 /* Move this window to the bottom of the stack. Without this we would force
504 * other windows which will be above this one to redraw themselves because
505 * this window occludes them for a tiny moment. The next stack_refresh()
506 * will fix this up and move the window to its correct place. */
507 xcb_configure_window(globalconf.connection, c->frame_window,
508 XCB_CONFIG_WINDOW_STACK_MODE,
509 (uint32_t[]) { XCB_STACK_MODE_BELOW});
511 /* Duplicate client and push it in client list */
512 lua_pushvalue(globalconf.L, -1);
513 client_array_push(&globalconf.clients, luaA_object_ref(globalconf.L, -1));
515 /* Set the right screen */
516 screen_client_moveto(c, screen_getbycoord(wgeom->x, wgeom->y), false);
518 /* Store initial geometry and emits signals so we inform that geometry have
519 * been set. */
520 #define HANDLE_GEOM(attr) \
521 c->geometry.attr = wgeom->attr; \
522 luaA_object_emit_signal(globalconf.L, -1, "property::" #attr, 0);
523 HANDLE_GEOM(x)
524 HANDLE_GEOM(y)
525 HANDLE_GEOM(width)
526 HANDLE_GEOM(height)
527 #undef HANDLE_GEOM
529 luaA_object_emit_signal(globalconf.L, -1, "property::geometry", 0);
531 /* Set border width */
532 window_set_border_width(globalconf.L, -1, wgeom->border_width);
534 /* we honor size hints by default */
535 c->size_hints_honor = true;
536 luaA_object_emit_signal(globalconf.L, -1, "property::size_hints_honor", 0);
538 /* update all properties */
539 client_update_properties(c);
541 /* Then check clients hints */
542 ewmh_client_check_hints(c);
544 /* Push client in stack */
545 stack_client_push(c);
547 /* Always stay in NORMAL_STATE. Even though iconified seems more
548 * appropriate sometimes. The only possible loss is that clients not using
549 * visibility events may continue to process data (when banned).
550 * Without any exposes or other events the cost should be fairly limited though.
552 * Some clients may expect the window to be unmapped when STATE_ICONIFIED.
553 * Two conflicting parts of the ICCCM v2.0 (section 4.1.4):
555 * "Normal -> Iconic - The client should send a ClientMessage event as described later in this section."
556 * (note no explicit mention of unmapping, while Normal->Widthdrawn does mention that)
558 * "Once a client's window has left the Withdrawn state, the window will be mapped
559 * if it is in the Normal state and the window will be unmapped if it is in the Iconic state."
561 * At this stage it's just safer to keep it in normal state and avoid confusion.
563 xwindow_set_state(c->window, XCB_ICCCM_WM_STATE_NORMAL);
565 /* Request our response */
566 xcb_get_property_reply_t *reply =
567 xcb_get_property_reply(globalconf.connection, startup_id_q, NULL);
568 /* Say spawn that a client has been started, with startup id as argument */
569 char *startup_id = xutil_get_text_property_from_reply(reply);
570 c->startup_id = startup_id;
571 p_delete(&reply);
572 spawn_start_notify(c, startup_id);
574 luaA_class_emit_signal(globalconf.L, &client_class, "list", 0);
576 /* client is still on top of the stack; emit signal */
577 luaA_object_emit_signal(globalconf.L, -1, "manage", 0);
578 /* pop client */
579 lua_pop(globalconf.L, 1);
582 static void
583 client_remove_titlebar_geometry(client_t *c, area_t *geometry)
585 geometry->x += c->titlebar[CLIENT_TITLEBAR_LEFT].size;
586 geometry->y += c->titlebar[CLIENT_TITLEBAR_TOP].size;
587 geometry->width -= c->titlebar[CLIENT_TITLEBAR_LEFT].size;
588 geometry->width -= c->titlebar[CLIENT_TITLEBAR_RIGHT].size;
589 geometry->height -= c->titlebar[CLIENT_TITLEBAR_TOP].size;
590 geometry->height -= c->titlebar[CLIENT_TITLEBAR_BOTTOM].size;
593 static void
594 client_add_titlebar_geometry(client_t *c, area_t *geometry)
596 geometry->x -= c->titlebar[CLIENT_TITLEBAR_LEFT].size;
597 geometry->y -= c->titlebar[CLIENT_TITLEBAR_TOP].size;
598 geometry->width += c->titlebar[CLIENT_TITLEBAR_LEFT].size;
599 geometry->width += c->titlebar[CLIENT_TITLEBAR_RIGHT].size;
600 geometry->height += c->titlebar[CLIENT_TITLEBAR_TOP].size;
601 geometry->height += c->titlebar[CLIENT_TITLEBAR_BOTTOM].size;
604 /** Send a synthetic configure event to a window.
606 void
607 client_send_configure(client_t *c)
609 area_t geometry = c->geometry;
611 if (!c->fullscreen)
612 client_remove_titlebar_geometry(c, &geometry);
613 xwindow_configure(c->window, geometry, c->border_width);
616 /** Apply size hints to the client's new geometry.
618 static area_t
619 client_apply_size_hints(client_t *c, area_t geometry)
621 int32_t minw = 0, minh = 0;
622 int32_t basew = 0, baseh = 0, real_basew = 0, real_baseh = 0;
624 if (c->fullscreen)
625 return geometry;
627 /* Size hints are applied to the window without any decoration */
628 client_remove_titlebar_geometry(c, &geometry);
630 if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_SIZE)
632 basew = c->size_hints.base_width;
633 baseh = c->size_hints.base_height;
634 real_basew = basew;
635 real_baseh = baseh;
637 else if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE)
639 /* base size is substituted with min size if not specified */
640 basew = c->size_hints.min_width;
641 baseh = c->size_hints.min_height;
644 if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE)
646 minw = c->size_hints.min_width;
647 minh = c->size_hints.min_height;
649 else if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_SIZE)
651 /* min size is substituted with base size if not specified */
652 minw = c->size_hints.base_width;
653 minh = c->size_hints.base_height;
656 /* Handle the size aspect ratio */
657 if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_ASPECT
658 && c->size_hints.min_aspect_den > 0
659 && c->size_hints.max_aspect_den > 0
660 && geometry.height > real_baseh
661 && geometry.width > real_basew)
663 /* ICCCM mandates:
664 * If a base size is provided along with the aspect ratio fields, the base size should be subtracted from the
665 * window size prior to checking that the aspect ratio falls in range. If a base size is not provided, nothing
666 * should be subtracted from the window size. (The minimum size is not to be used in place of the base size for
667 * this purpose.)
669 double dx = geometry.width - real_basew;
670 double dy = geometry.height - real_baseh;
671 double ratio = dx / dy;
672 double min = c->size_hints.min_aspect_num / (double) c->size_hints.min_aspect_den;
673 double max = c->size_hints.max_aspect_num / (double) c->size_hints.max_aspect_den;
675 if(max > 0 && min > 0 && ratio > 0)
677 if(ratio < min)
679 /* dx is lower than allowed, make dy lower to compensate this (+ 0.5 to force proper rounding). */
680 dy = dx / min + 0.5;
681 geometry.width = dx + real_basew;
682 geometry.height = dy + real_baseh;
683 } else if(ratio > max)
685 /* dx is too high, lower it (+0.5 for proper rounding) */
686 dx = dy * max + 0.5;
687 geometry.width = dx + real_basew;
688 geometry.height = dy + real_baseh;
693 /* Handle the minimum size */
694 geometry.width = MAX(geometry.width, minw);
695 geometry.height = MAX(geometry.height, minh);
697 /* Handle the maximum size */
698 if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE)
700 if(c->size_hints.max_width)
701 geometry.width = MIN(geometry.width, c->size_hints.max_width);
702 if(c->size_hints.max_height)
703 geometry.height = MIN(geometry.height, c->size_hints.max_height);
706 /* Handle the size increment */
707 if(c->size_hints.flags & (XCB_ICCCM_SIZE_HINT_P_RESIZE_INC | XCB_ICCCM_SIZE_HINT_BASE_SIZE)
708 && c->size_hints.width_inc && c->size_hints.height_inc)
710 uint16_t t1 = geometry.width, t2 = geometry.height;
711 unsigned_subtract(t1, basew);
712 unsigned_subtract(t2, baseh);
713 geometry.width -= t1 % c->size_hints.width_inc;
714 geometry.height -= t2 % c->size_hints.height_inc;
717 client_add_titlebar_geometry(c, &geometry);
718 return geometry;
721 static void
722 client_resize_do(client_t *c, area_t geometry, bool force_notice, bool honor_hints)
724 bool send_notice = force_notice;
725 bool hide_titlebars = c->fullscreen;
726 screen_t *new_screen = screen_getbycoord(geometry.x, geometry.y);
728 if (honor_hints)
729 geometry = client_apply_size_hints(c, geometry);
731 if(c->geometry.width == geometry.width
732 && c->geometry.height == geometry.height)
733 send_notice = true;
735 /* Also store geometry including border */
736 area_t old_geometry = c->geometry;
737 c->geometry = geometry;
739 /* Ignore all spurious enter/leave notify events */
740 client_ignore_enterleave_events();
742 /* Configure the client for its new size */
743 area_t real_geometry = geometry;
744 if (!hide_titlebars)
746 real_geometry.x = c->titlebar[CLIENT_TITLEBAR_LEFT].size;
747 real_geometry.y = c->titlebar[CLIENT_TITLEBAR_TOP].size;
748 real_geometry.width -= c->titlebar[CLIENT_TITLEBAR_LEFT].size;
749 real_geometry.width -= c->titlebar[CLIENT_TITLEBAR_RIGHT].size;
750 real_geometry.height -= c->titlebar[CLIENT_TITLEBAR_TOP].size;
751 real_geometry.height -= c->titlebar[CLIENT_TITLEBAR_BOTTOM].size;
752 } else {
753 real_geometry.x = 0;
754 real_geometry.y = 0;
757 xcb_configure_window(globalconf.connection, c->frame_window,
758 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
759 (uint32_t[]) { geometry.x, geometry.y, geometry.width, geometry.height });
760 xcb_configure_window(globalconf.connection, c->window,
761 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
762 (uint32_t[]) { real_geometry.x, real_geometry.y, real_geometry.width, real_geometry.height });
764 if(send_notice)
765 /* We are moving without changing the size, see ICCCM 4.2.3 */
766 client_send_configure(c);
768 client_restore_enterleave_events();
770 luaA_object_push(globalconf.L, c);
771 luaA_object_emit_signal(globalconf.L, -1, "property::geometry", 0);
772 if (old_geometry.x != geometry.x)
773 luaA_object_emit_signal(globalconf.L, -1, "property::x", 0);
774 if (old_geometry.y != geometry.y)
775 luaA_object_emit_signal(globalconf.L, -1, "property::y", 0);
776 if (old_geometry.width != geometry.width)
777 luaA_object_emit_signal(globalconf.L, -1, "property::width", 0);
778 if (old_geometry.height != geometry.height)
779 luaA_object_emit_signal(globalconf.L, -1, "property::height", 0);
780 lua_pop(globalconf.L, 1);
782 screen_client_moveto(c, new_screen, false);
784 /* Update all titlebars */
785 for (client_titlebar_t bar = CLIENT_TITLEBAR_TOP; bar < CLIENT_TITLEBAR_COUNT; bar++) {
786 if (c->titlebar[bar].drawable == NULL && c->titlebar[bar].size == 0)
787 continue;
789 luaA_object_push(globalconf.L, c);
790 drawable_t *drawable = titlebar_get_drawable(globalconf.L, c, -1, bar);
791 luaA_object_push_item(globalconf.L, -1, drawable);
793 area_t area = titlebar_get_area(c, bar);
795 /* Convert to global coordinates */
796 area.x += geometry.x;
797 area.y += geometry.y;
798 if (hide_titlebars)
799 area.width = area.height = 0;
800 drawable_set_geometry(drawable, -1, area);
802 /* Pop the client and the drawable */
803 lua_pop(globalconf.L, 2);
807 /** Resize client window.
808 * The sizes given as parameters are with borders!
809 * \param c Client to resize.
810 * \param geometry New window geometry.
811 * \param honor_hints Use size hints.
812 * \return true if an actual resize occurred.
814 bool
815 client_resize(client_t *c, area_t geometry, bool honor_hints)
817 area_t area;
819 /* offscreen appearance fixes */
820 area = display_area_get();
822 if(geometry.x > area.width)
823 geometry.x = area.width - geometry.width;
824 if(geometry.y > area.height)
825 geometry.y = area.height - geometry.height;
826 if(geometry.x + geometry.width < 0)
827 geometry.x = 0;
828 if(geometry.y + geometry.height < 0)
829 geometry.y = 0;
831 if(geometry.width < c->titlebar[CLIENT_TITLEBAR_LEFT].size + c->titlebar[CLIENT_TITLEBAR_RIGHT].size)
832 return false;
833 if(geometry.height < c->titlebar[CLIENT_TITLEBAR_TOP].size + c->titlebar[CLIENT_TITLEBAR_BOTTOM].size)
834 return false;
836 if(geometry.width == 0 || geometry.height == 0)
837 return false;
839 if(c->geometry.x != geometry.x
840 || c->geometry.y != geometry.y
841 || c->geometry.width != geometry.width
842 || c->geometry.height != geometry.height)
844 client_resize_do(c, geometry, false, honor_hints);
846 return true;
849 return false;
852 static void
853 client_emit_property_workarea_on_screen(lua_State *L, client_t *c)
855 luaA_object_push(L, c->screen);
856 luaA_object_emit_signal(L, -1, "property::workarea", 0);
857 lua_pop(L, 1);
860 /** Set a client minimized, or not.
861 * \param L The Lua VM state.
862 * \param cidx The client index.
863 * \param s Set or not the client minimized.
865 void
866 client_set_minimized(lua_State *L, int cidx, bool s)
868 client_t *c = luaA_checkudata(L, cidx, &client_class);
870 if(c->minimized != s)
872 c->minimized = s;
873 banning_need_update();
874 if(s)
875 xwindow_set_state(c->window, XCB_ICCCM_WM_STATE_ICONIC);
876 else
877 xwindow_set_state(c->window, XCB_ICCCM_WM_STATE_NORMAL);
878 if(strut_has_value(&c->strut))
879 client_emit_property_workarea_on_screen(L, c);
880 luaA_object_emit_signal(L, cidx, "property::minimized", 0);
884 /** Set a client hidden, or not.
885 * \param L The Lua VM state.
886 * \param cidx The client index.
887 * \param s Set or not the client hidden.
889 static void
890 client_set_hidden(lua_State *L, int cidx, bool s)
892 client_t *c = luaA_checkudata(L, cidx, &client_class);
894 if(c->hidden != s)
896 c->hidden = s;
897 banning_need_update();
898 if(strut_has_value(&c->strut))
899 client_emit_property_workarea_on_screen(L, c);
900 luaA_object_emit_signal(L, cidx, "property::hidden", 0);
904 /** Set a client sticky, or not.
905 * \param L The Lua VM state.
906 * \param cidx The client index.
907 * \param s Set or not the client sticky.
909 void
910 client_set_sticky(lua_State *L, int cidx, bool s)
912 client_t *c = luaA_checkudata(L, cidx, &client_class);
914 if(c->sticky != s)
916 c->sticky = s;
917 banning_need_update();
918 luaA_object_emit_signal(L, cidx, "property::sticky", 0);
922 /** Set a client fullscreen, or not.
923 * \param L The Lua VM state.
924 * \param cidx The client index.
925 * \param s Set or not the client fullscreen.
927 void
928 client_set_fullscreen(lua_State *L, int cidx, bool s)
930 client_t *c = luaA_checkudata(L, cidx, &client_class);
932 if(c->fullscreen != s)
934 /* become fullscreen! */
935 if(s)
937 /* remove any max state */
938 client_set_maximized(L, cidx, false);
939 /* You can only be part of one of the special layers. */
940 client_set_below(L, cidx, false);
941 client_set_above(L, cidx, false);
942 client_set_ontop(L, cidx, false);
944 int abs_cidx = luaA_absindex(L, cidx); \
945 lua_pushboolean(L, s);
946 c->fullscreen = s;
947 luaA_object_emit_signal(L, abs_cidx, "request::fullscreen", 1);
948 luaA_object_emit_signal(L, abs_cidx, "property::fullscreen", 0);
949 /* Force a client resize, so that titlebars get shown/hidden */
950 client_resize_do(c, c->geometry, true, false);
951 stack_windows();
955 /** Get a clients maximized state (horizontally and vertically).
956 * \param c The client.
957 * \return The maximized state.
959 static int
960 client_get_maximized(client_t *c)
962 return c->maximized_horizontal && c->maximized_vertical;
965 /** Set a client horizontally|vertically maximized.
966 * \param L The Lua VM state.
967 * \param cidx The client index.
968 * \param s The maximized status.
970 #define DO_FUNCTION_CLIENT_MAXIMIZED(type) \
971 void \
972 client_set_maximized_##type(lua_State *L, int cidx, bool s) \
974 client_t *c = luaA_checkudata(L, cidx, &client_class); \
975 if(c->maximized_##type != s) \
977 int abs_cidx = luaA_absindex(L, cidx); \
978 if(s) \
979 client_set_fullscreen(L, abs_cidx, false); \
980 lua_pushboolean(L, s); \
981 int max_before = client_get_maximized(c); \
982 c->maximized_##type = s; \
983 luaA_object_emit_signal(L, abs_cidx, "request::maximized_" #type, 1); \
984 luaA_object_emit_signal(L, abs_cidx, "property::maximized_" #type, 0); \
985 if(max_before != client_get_maximized(c)) \
986 luaA_object_emit_signal(L, abs_cidx, "property::maximized", 0); \
987 stack_windows(); \
990 DO_FUNCTION_CLIENT_MAXIMIZED(vertical)
991 DO_FUNCTION_CLIENT_MAXIMIZED(horizontal)
992 #undef DO_FUNCTION_CLIENT_MAXIMIZED
994 /** Set a client maximized (horizontally and vertically).
995 * \param L The Lua VM state.
996 * \param cidx The client index.
997 * \param s Set or not the client maximized attribute.
999 void
1000 client_set_maximized(lua_State *L, int cidx, bool s)
1002 client_set_maximized_horizontal(L, cidx, s);
1003 client_set_maximized_vertical(L, cidx, s);
1006 /** Set a client above, or not.
1007 * \param L The Lua VM state.
1008 * \param cidx The client index.
1009 * \param s Set or not the client above.
1011 void
1012 client_set_above(lua_State *L, int cidx, bool s)
1014 client_t *c = luaA_checkudata(L, cidx, &client_class);
1016 if(c->above != s)
1018 /* You can only be part of one of the special layers. */
1019 if(s)
1021 client_set_below(L, cidx, false);
1022 client_set_ontop(L, cidx, false);
1023 client_set_fullscreen(L, cidx, false);
1025 c->above = s;
1026 stack_windows();
1027 luaA_object_emit_signal(L, cidx, "property::above", 0);
1031 /** Set a client below, or not.
1032 * \param L The Lua VM state.
1033 * \param cidx The client index.
1034 * \param s Set or not the client below.
1036 void
1037 client_set_below(lua_State *L, int cidx, bool s)
1039 client_t *c = luaA_checkudata(L, cidx, &client_class);
1041 if(c->below != s)
1043 /* You can only be part of one of the special layers. */
1044 if(s)
1046 client_set_above(L, cidx, false);
1047 client_set_ontop(L, cidx, false);
1048 client_set_fullscreen(L, cidx, false);
1050 c->below = s;
1051 stack_windows();
1052 luaA_object_emit_signal(L, cidx, "property::below", 0);
1056 /** Set a client modal, or not.
1057 * \param L The Lua VM state.
1058 * \param cidx The client index.
1059 * \param s Set or not the client modal attribute.
1061 void
1062 client_set_modal(lua_State *L, int cidx, bool s)
1064 client_t *c = luaA_checkudata(L, cidx, &client_class);
1066 if(c->modal != s)
1068 c->modal = s;
1069 stack_windows();
1070 luaA_object_emit_signal(L, cidx, "property::modal", 0);
1074 /** Set a client ontop, or not.
1075 * \param L The Lua VM state.
1076 * \param cidx The client index.
1077 * \param s Set or not the client ontop attribute.
1079 void
1080 client_set_ontop(lua_State *L, int cidx, bool s)
1082 client_t *c = luaA_checkudata(L, cidx, &client_class);
1084 if(c->ontop != s)
1086 /* You can only be part of one of the special layers. */
1087 if(s)
1089 client_set_above(L, cidx, false);
1090 client_set_below(L, cidx, false);
1091 client_set_fullscreen(L, cidx, false);
1093 c->ontop = s;
1094 stack_windows();
1095 luaA_object_emit_signal(L, cidx, "property::ontop", 0);
1099 /** Unban a client and move it back into the viewport.
1100 * \param c The client.
1102 void
1103 client_unban(client_t *c)
1105 if(c->isbanned)
1107 xcb_map_window(globalconf.connection, c->frame_window);
1109 c->isbanned = false;
1111 /* An unbanned client shouldn't be minimized or hidden */
1112 luaA_object_push(globalconf.L, c);
1113 client_set_minimized(globalconf.L, -1, false);
1114 client_set_hidden(globalconf.L, -1, false);
1115 lua_pop(globalconf.L, 1);
1119 /** Unmanage a client.
1120 * \param c The client.
1121 * \param window_valid Is the client's window still valid?
1123 void
1124 client_unmanage(client_t *c, bool window_valid)
1126 /* Reset transient_for attributes of windows that might be referring to us */
1127 foreach(_tc, globalconf.clients)
1129 client_t *tc = *_tc;
1130 if(tc->transient_for == c)
1131 tc->transient_for = NULL;
1134 if(globalconf.focus.client == c)
1135 client_unfocus(c);
1137 /* remove client from global list and everywhere else */
1138 foreach(elem, globalconf.clients)
1139 if(*elem == c)
1141 client_array_remove(&globalconf.clients, elem);
1142 break;
1144 stack_client_remove(c);
1145 for(int i = 0; i < globalconf.tags.len; i++)
1146 untag_client(c, globalconf.tags.tab[i]);
1148 luaA_object_push(globalconf.L, c);
1149 luaA_object_emit_signal(globalconf.L, -1, "unmanage", 0);
1150 lua_pop(globalconf.L, 1);
1152 luaA_class_emit_signal(globalconf.L, &client_class, "list", 0);
1154 if(strut_has_value(&c->strut))
1155 client_emit_property_workarea_on_screen(globalconf.L, c);
1157 /* Get rid of all titlebars */
1158 for (client_titlebar_t bar = CLIENT_TITLEBAR_TOP; bar < CLIENT_TITLEBAR_COUNT; bar++) {
1159 if (c->titlebar[bar].drawable == NULL)
1160 continue;
1162 /* Forget about the drawable */
1163 luaA_object_push(globalconf.L, c);
1164 luaA_object_unref_item(globalconf.L, -1, c->titlebar[bar].drawable);
1165 c->titlebar[bar].drawable = NULL;
1166 lua_pop(globalconf.L, 1);
1169 /* Clear our event mask so that we don't receive any events from now on,
1170 * especially not for the following requests. */
1171 if(window_valid)
1172 xcb_change_window_attributes(globalconf.connection,
1173 c->window,
1174 XCB_CW_EVENT_MASK,
1175 (const uint32_t []) { 0 });
1176 xcb_change_window_attributes(globalconf.connection,
1177 c->frame_window,
1178 XCB_CW_EVENT_MASK,
1179 (const uint32_t []) { 0 });
1181 if(window_valid)
1183 xcb_unmap_window(globalconf.connection, c->window);
1184 xcb_reparent_window(globalconf.connection, c->window, globalconf.screen->root,
1185 c->geometry.x, c->geometry.y);
1188 /* Ignore all spurious enter/leave notify events */
1189 client_ignore_enterleave_events();
1190 xcb_destroy_window(globalconf.connection, c->frame_window);
1191 client_restore_enterleave_events();
1193 if(window_valid)
1195 /* Remove this window from the save set since this shouldn't be made visible
1196 * after a restart anymore. */
1197 xcb_change_save_set(globalconf.connection, XCB_SET_MODE_DELETE, c->window);
1198 if (globalconf.have_shape)
1199 xcb_shape_select_input(globalconf.connection, c->window, 0);
1201 /* Do this last to avoid races with clients. According to ICCCM, clients
1202 * arent allowed to re-use the window until after this. */
1203 xwindow_set_state(c->window, XCB_ICCCM_WM_STATE_WITHDRAWN);
1206 /* set client as invalid */
1207 c->window = XCB_NONE;
1209 luaA_object_unref(globalconf.L, c);
1212 /** Kill a client via a WM_DELETE_WINDOW request or KillClient if not
1213 * supported.
1214 * \param c The client to kill.
1216 void
1217 client_kill(client_t *c)
1219 if(client_hasproto(c, WM_DELETE_WINDOW))
1221 xcb_client_message_event_t ev;
1223 /* Initialize all of event's fields first */
1224 p_clear(&ev, 1);
1226 ev.response_type = XCB_CLIENT_MESSAGE;
1227 ev.window = c->window;
1228 ev.format = 32;
1229 ev.data.data32[1] = globalconf.timestamp;
1230 ev.type = WM_PROTOCOLS;
1231 ev.data.data32[0] = WM_DELETE_WINDOW;
1233 xcb_send_event(globalconf.connection, false, c->window,
1234 XCB_EVENT_MASK_NO_EVENT, (char *) &ev);
1236 else
1237 xcb_kill_client(globalconf.connection, c->window);
1240 /** Get all clients into a table.
1241 * \param L The Lua VM state.
1242 * \return The number of elements pushed on stack.
1243 * \luastack
1244 * \lparam An optional screen number.
1245 * \lreturn A table with all clients.
1247 static int
1248 luaA_client_get(lua_State *L)
1250 int i = 1;
1251 screen_t *screen = NULL;
1253 if(!lua_isnoneornil(L, 1))
1254 screen = luaA_checkscreen(L, 1);
1256 lua_newtable(L);
1257 foreach(c, globalconf.clients)
1258 if(screen == NULL || (*c)->screen == screen)
1260 luaA_object_push(L, *c);
1261 lua_rawseti(L, -2, i++);
1264 return 1;
1267 /** Check if a client is visible on its screen.
1268 * \param L The Lua VM state.
1269 * \return The number of elements pushed on stack.
1270 * \luastack
1271 * \lvalue A client.
1272 * \lreturn A boolean value, true if the client is visible, false otherwise.
1274 static int
1275 luaA_client_isvisible(lua_State *L)
1277 client_t *c = luaA_checkudata(L, 1, &client_class);
1278 lua_pushboolean(L, client_isvisible(c));
1279 return 1;
1282 /** Set a client icon.
1283 * \param L The Lua VM state.
1284 * \param cidx The client index on the stack.
1285 * \param iidx The image index on the stack.
1287 void
1288 client_set_icon(client_t *c, cairo_surface_t *s)
1290 if (s)
1291 s = draw_dup_image_surface(s);
1292 if(c->icon)
1293 cairo_surface_destroy(c->icon);
1294 c->icon = s;
1296 luaA_object_push(globalconf.L, c);
1297 luaA_object_emit_signal(globalconf.L, -1, "property::icon", 0);
1298 lua_pop(globalconf.L, 1);
1301 /** Kill a client.
1302 * \param L The Lua VM state.
1304 * \luastack
1305 * \lvalue A client.
1307 static int
1308 luaA_client_kill(lua_State *L)
1310 client_t *c = luaA_checkudata(L, 1, &client_class);
1311 client_kill(c);
1312 return 0;
1315 /** Swap a client with another one.
1316 * \param L The Lua VM state.
1317 * \luastack
1318 * \lvalue A client.
1319 * \lparam A client to swap with.
1321 static int
1322 luaA_client_swap(lua_State *L)
1324 client_t *c = luaA_checkudata(L, 1, &client_class);
1325 client_t *swap = luaA_checkudata(L, 2, &client_class);
1327 if(c != swap)
1329 client_t **ref_c = NULL, **ref_swap = NULL;
1330 foreach(item, globalconf.clients)
1332 if(*item == c)
1333 ref_c = item;
1334 else if(*item == swap)
1335 ref_swap = item;
1336 if(ref_c && ref_swap)
1337 break;
1339 /* swap ! */
1340 *ref_c = swap;
1341 *ref_swap = c;
1343 luaA_class_emit_signal(globalconf.L, &client_class, "list", 0);
1346 return 0;
1349 /** Access or set the client tags.
1350 * \param L The Lua VM state.
1351 * \return The number of elements pushed on stack.
1352 * \lparam A table with tags to set, or none to get the current tags table.
1353 * \return The clients tag.
1355 static int
1356 luaA_client_tags(lua_State *L)
1358 client_t *c = luaA_checkudata(L, 1, &client_class);
1359 int j = 0;
1361 if(lua_gettop(L) == 2)
1363 luaA_checktable(L, 2);
1364 for(int i = 0; i < globalconf.tags.len; i++)
1366 /* Only untag if we aren't going to add this tag again */
1367 bool found = false;
1368 lua_pushnil(L);
1369 while(lua_next(L, 2))
1371 tag_t *t = lua_touserdata(L, -1);
1372 /* Pop the value from lua_next */
1373 lua_pop(L, 1);
1374 if (t != globalconf.tags.tab[i])
1375 continue;
1377 /* Pop the key from lua_next */
1378 lua_pop(L, 1);
1379 found = true;
1380 break;
1382 if(!found)
1383 untag_client(c, globalconf.tags.tab[i]);
1385 lua_pushnil(L);
1386 while(lua_next(L, 2))
1387 tag_client(c);
1388 lua_pop(L, 1);
1391 lua_newtable(L);
1392 foreach(tag, globalconf.tags)
1393 if(is_client_tagged(c, *tag))
1395 luaA_object_push(L, *tag);
1396 lua_rawseti(L, -2, ++j);
1399 return 1;
1402 /** Raise a client on top of others which are on the same layer.
1403 * \param L The Lua VM state.
1404 * \luastack
1405 * \lvalue A client.
1407 static int
1408 luaA_client_raise(lua_State *L)
1410 client_t *c = luaA_checkudata(L, 1, &client_class);
1411 client_raise(c);
1412 return 0;
1415 /** Lower a client on bottom of others which are on the same layer.
1416 * \param L The Lua VM state.
1417 * \luastack
1418 * \lvalue A client.
1420 static int
1421 luaA_client_lower(lua_State *L)
1423 client_t *c = luaA_checkudata(L, 1, &client_class);
1425 stack_client_push(c);
1427 /* Traverse all transient layers. */
1428 for(client_t *tc = c->transient_for; tc; tc = tc->transient_for)
1429 stack_client_push(tc);
1431 return 0;
1434 /** Stop managing a client.
1435 * \param L The Lua VM state.
1436 * \return The number of elements pushed on stack.
1437 * \luastack
1438 * \lvalue A client.
1440 static int
1441 luaA_client_unmanage(lua_State *L)
1443 client_t *c = luaA_checkudata(L, 1, &client_class);
1444 client_unmanage(c, true);
1445 return 0;
1448 static area_t
1449 titlebar_get_area(client_t *c, client_titlebar_t bar)
1451 area_t result = c->geometry;
1452 result.x = result.y = 0;
1454 // Let's try some ascii art:
1455 // ---------------------------
1456 // | Top |
1457 // |-------------------------|
1458 // |L| |R|
1459 // |e| |i|
1460 // |f| |g|
1461 // |t| |h|
1462 // | | |t|
1463 // |-------------------------|
1464 // | Bottom |
1465 // ---------------------------
1467 switch (bar) {
1468 case CLIENT_TITLEBAR_BOTTOM:
1469 result.y = c->geometry.height - c->titlebar[bar].size;
1470 /* Fall through */
1471 case CLIENT_TITLEBAR_TOP:
1472 result.height = c->titlebar[bar].size;
1473 break;
1474 case CLIENT_TITLEBAR_RIGHT:
1475 result.x = c->geometry.width - c->titlebar[bar].size;
1476 /* Fall through */
1477 case CLIENT_TITLEBAR_LEFT:
1478 result.y = c->titlebar[CLIENT_TITLEBAR_TOP].size;
1479 result.width = c->titlebar[bar].size;
1480 result.height -= c->titlebar[CLIENT_TITLEBAR_TOP].size;
1481 result.height -= c->titlebar[CLIENT_TITLEBAR_BOTTOM].size;
1482 break;
1483 default:
1484 fatal("Unknown titlebar kind %d\n", (int) bar);
1487 return result;
1490 drawable_t *
1491 client_get_drawable_offset(client_t *c, int *x, int *y)
1493 for (client_titlebar_t bar = CLIENT_TITLEBAR_TOP; bar < CLIENT_TITLEBAR_COUNT; bar++) {
1494 area_t area = titlebar_get_area(c, bar);
1495 if (AREA_LEFT(area) > *x || AREA_RIGHT(area) <= *x)
1496 continue;
1497 if (AREA_TOP(area) > *y || AREA_BOTTOM(area) <= *y)
1498 continue;
1500 *x -= area.x;
1501 *y -= area.y;
1502 return c->titlebar[bar].drawable;
1505 return NULL;
1508 drawable_t *
1509 client_get_drawable(client_t *c, int x, int y)
1511 return client_get_drawable_offset(c, &x, &y);
1514 static void
1515 client_refresh_titlebar_partial(client_t *c, client_titlebar_t bar, int16_t x, int16_t y, uint16_t width, uint16_t height)
1517 if(c->titlebar[bar].drawable == NULL
1518 || c->titlebar[bar].drawable->pixmap == XCB_NONE
1519 || !c->titlebar[bar].drawable->refreshed)
1520 return;
1522 /* Is the titlebar part of the area that should get redrawn? */
1523 area_t area = titlebar_get_area(c, bar);
1524 if (AREA_LEFT(area) >= x + width || AREA_RIGHT(area) <= x)
1525 return;
1526 if (AREA_TOP(area) >= y + height || AREA_BOTTOM(area) <= y)
1527 return;
1529 /* Redraw the affected parts */
1530 cairo_surface_flush(c->titlebar[bar].drawable->surface);
1531 xcb_copy_area(globalconf.connection, c->titlebar[bar].drawable->pixmap, c->frame_window,
1532 globalconf.gc, x - area.x, y - area.y, x, y, width, height);
1535 #define HANDLE_TITLEBAR_REFRESH(name, index) \
1536 static void \
1537 client_refresh_titlebar_ ## name(client_t *c) \
1539 area_t area = titlebar_get_area(c, index); \
1540 client_refresh_titlebar_partial(c, index, area.x, area.y, area.width, area.height); \
1542 HANDLE_TITLEBAR_REFRESH(top, CLIENT_TITLEBAR_TOP)
1543 HANDLE_TITLEBAR_REFRESH(right, CLIENT_TITLEBAR_RIGHT)
1544 HANDLE_TITLEBAR_REFRESH(bottom, CLIENT_TITLEBAR_BOTTOM)
1545 HANDLE_TITLEBAR_REFRESH(left, CLIENT_TITLEBAR_LEFT)
1548 * Refresh all titlebars that are in the specified rectangle
1550 void
1551 client_refresh_partial(client_t *c, int16_t x, int16_t y, uint16_t width, uint16_t height)
1553 for (client_titlebar_t bar = CLIENT_TITLEBAR_TOP; bar < CLIENT_TITLEBAR_COUNT; bar++) {
1554 client_refresh_titlebar_partial(c, bar, x, y, width, height);
1558 static drawable_t *
1559 titlebar_get_drawable(lua_State *L, client_t *c, int cl_idx, client_titlebar_t bar)
1561 if (c->titlebar[bar].drawable == NULL)
1563 cl_idx = luaA_absindex(L, cl_idx);
1564 switch (bar) {
1565 case CLIENT_TITLEBAR_TOP:
1566 drawable_allocator(L, (drawable_refresh_callback *) client_refresh_titlebar_top, c);
1567 break;
1568 case CLIENT_TITLEBAR_BOTTOM:
1569 drawable_allocator(L, (drawable_refresh_callback *) client_refresh_titlebar_bottom, c);
1570 break;
1571 case CLIENT_TITLEBAR_RIGHT:
1572 drawable_allocator(L, (drawable_refresh_callback *) client_refresh_titlebar_right, c);
1573 break;
1574 case CLIENT_TITLEBAR_LEFT:
1575 drawable_allocator(L, (drawable_refresh_callback *) client_refresh_titlebar_left, c);
1576 break;
1577 default:
1578 fatal("Unknown titlebar kind %d\n", (int) bar);
1580 c->titlebar[bar].drawable = luaA_object_ref_item(L, cl_idx, -1);
1583 return c->titlebar[bar].drawable;
1586 static void
1587 titlebar_resize(lua_State *L, int cidx, client_t *c, client_titlebar_t bar, int size)
1589 const char *property_name;
1591 if (size < 0)
1592 return;
1594 if (size == c->titlebar[bar].size)
1595 return;
1597 /* Now resize the client (and titlebars!) suitably (the client without
1598 * titlebars should keep its current size!) */
1599 area_t geometry = c->geometry;
1600 int change = size - c->titlebar[bar].size;
1601 switch (bar) {
1602 case CLIENT_TITLEBAR_TOP:
1603 geometry.height += change;
1604 property_name = "property::titlebar_top";
1605 break;
1606 case CLIENT_TITLEBAR_BOTTOM:
1607 geometry.height += change;
1608 property_name = "property::titlebar_bottom";
1609 break;
1610 case CLIENT_TITLEBAR_RIGHT:
1611 geometry.width += change;
1612 property_name = "property::titlebar_right";
1613 break;
1614 case CLIENT_TITLEBAR_LEFT:
1615 geometry.width += change;
1616 property_name = "property::titlebar_left";
1617 break;
1618 default:
1619 fatal("Unknown titlebar kind %d\n", (int) bar);
1622 c->titlebar[bar].size = size;
1623 client_resize_do(c, geometry, true, false);
1625 luaA_object_emit_signal(L, cidx, property_name, 0);
1628 #define HANDLE_TITLEBAR(name, index) \
1629 static int \
1630 luaA_client_titlebar_ ## name(lua_State *L) \
1632 client_t *c = luaA_checkudata(L, 1, &client_class); \
1634 if (lua_gettop(L) == 2) \
1636 if (lua_isnil(L, 2)) \
1637 titlebar_resize(L, 1, c, index, 0); \
1638 else \
1639 titlebar_resize(L, 1, c, index, luaL_checknumber(L, 2)); \
1642 luaA_object_push_item(L, 1, titlebar_get_drawable(L, c, 1, index)); \
1643 lua_pushnumber(L, c->titlebar[index].size); \
1644 return 2; \
1646 HANDLE_TITLEBAR(top, CLIENT_TITLEBAR_TOP)
1647 HANDLE_TITLEBAR(right, CLIENT_TITLEBAR_RIGHT)
1648 HANDLE_TITLEBAR(bottom, CLIENT_TITLEBAR_BOTTOM)
1649 HANDLE_TITLEBAR(left, CLIENT_TITLEBAR_LEFT)
1651 /** Return or set client geometry.
1652 * \param L The Lua VM state.
1653 * \return The number of elements pushed on stack.
1654 * \luastack
1655 * \lparam A table with new coordinates, or none.
1656 * \lreturn A table with client coordinates.
1658 static int
1659 luaA_client_geometry(lua_State *L)
1661 client_t *c = luaA_checkudata(L, 1, &client_class);
1663 if(lua_gettop(L) == 2 && !lua_isnil(L, 2))
1665 area_t geometry;
1667 luaA_checktable(L, 2);
1668 geometry.x = luaA_getopt_number(L, 2, "x", c->geometry.x);
1669 geometry.y = luaA_getopt_number(L, 2, "y", c->geometry.y);
1670 if(client_isfixed(c))
1672 geometry.width = c->geometry.width;
1673 geometry.height = c->geometry.height;
1675 else
1677 geometry.width = luaA_getopt_number(L, 2, "width", c->geometry.width);
1678 geometry.height = luaA_getopt_number(L, 2, "height", c->geometry.height);
1681 client_resize(c, geometry, c->size_hints_honor);
1684 return luaA_pusharea(L, c->geometry);
1687 static int
1688 luaA_client_set_screen(lua_State *L, client_t *c)
1690 screen_client_moveto(c, luaA_checkscreen(L, -1), true);
1691 return 0;
1694 static int
1695 luaA_client_set_hidden(lua_State *L, client_t *c)
1697 client_set_hidden(L, -3, luaA_checkboolean(L, -1));
1698 return 0;
1701 static int
1702 luaA_client_set_minimized(lua_State *L, client_t *c)
1704 client_set_minimized(L, -3, luaA_checkboolean(L, -1));
1705 return 0;
1708 static int
1709 luaA_client_set_fullscreen(lua_State *L, client_t *c)
1711 client_set_fullscreen(L, -3, luaA_checkboolean(L, -1));
1712 return 0;
1715 static int
1716 luaA_client_set_modal(lua_State *L, client_t *c)
1718 client_set_modal(L, -3, luaA_checkboolean(L, -1));
1719 return 0;
1722 static int
1723 luaA_client_set_maximized(lua_State *L, client_t *c)
1725 client_set_maximized(L, -3, luaA_checkboolean(L, -1));
1726 return 0;
1729 static int
1730 luaA_client_set_maximized_horizontal(lua_State *L, client_t *c)
1732 client_set_maximized_horizontal(L, -3, luaA_checkboolean(L, -1));
1733 return 0;
1736 static int
1737 luaA_client_set_maximized_vertical(lua_State *L, client_t *c)
1739 client_set_maximized_vertical(L, -3, luaA_checkboolean(L, -1));
1740 return 0;
1743 static int
1744 luaA_client_set_icon(lua_State *L, client_t *c)
1746 cairo_surface_t *surf = NULL;
1747 if(!lua_isnil(L, -1))
1748 surf = (cairo_surface_t *)lua_touserdata(L, -1);
1749 client_set_icon(c, surf);
1750 return 0;
1753 static int
1754 luaA_client_set_sticky(lua_State *L, client_t *c)
1756 client_set_sticky(L, -3, luaA_checkboolean(L, -1));
1757 return 0;
1760 static int
1761 luaA_client_set_size_hints_honor(lua_State *L, client_t *c)
1763 c->size_hints_honor = luaA_checkboolean(L, -1);
1764 luaA_object_emit_signal(L, -3, "property::size_hints_honor", 0);
1765 return 0;
1768 static int
1769 luaA_client_set_ontop(lua_State *L, client_t *c)
1771 client_set_ontop(L, -3, luaA_checkboolean(L, -1));
1772 return 0;
1775 static int
1776 luaA_client_set_below(lua_State *L, client_t *c)
1778 client_set_below(L, -3, luaA_checkboolean(L, -1));
1779 return 0;
1782 static int
1783 luaA_client_set_above(lua_State *L, client_t *c)
1785 client_set_above(L, -3, luaA_checkboolean(L, -1));
1786 return 0;
1789 static int
1790 luaA_client_set_urgent(lua_State *L, client_t *c)
1792 client_set_urgent(L, -3, luaA_checkboolean(L, -1));
1793 return 0;
1796 static int
1797 luaA_client_set_skip_taskbar(lua_State *L, client_t *c)
1799 client_set_skip_taskbar(L, -3, luaA_checkboolean(L, -1));
1800 return 0;
1803 static int
1804 luaA_client_get_name(lua_State *L, client_t *c)
1806 lua_pushstring(L, c->name ? c->name : c->alt_name);
1807 return 1;
1810 static int
1811 luaA_client_get_icon_name(lua_State *L, client_t *c)
1813 lua_pushstring(L, c->icon_name ? c->icon_name : c->alt_icon_name);
1814 return 1;
1817 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, class, lua_pushstring)
1818 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, instance, lua_pushstring)
1819 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, machine, lua_pushstring)
1820 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, role, lua_pushstring)
1821 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, transient_for, luaA_object_push)
1822 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, skip_taskbar, lua_pushboolean)
1823 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, leader_window, lua_pushnumber)
1824 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, group_window, lua_pushnumber)
1825 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, pid, lua_pushnumber)
1826 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, hidden, lua_pushboolean)
1827 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, minimized, lua_pushboolean)
1828 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, fullscreen, lua_pushboolean)
1829 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, modal, lua_pushboolean)
1830 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, ontop, lua_pushboolean)
1831 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, urgent, lua_pushboolean)
1832 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, above, lua_pushboolean)
1833 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, below, lua_pushboolean)
1834 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, sticky, lua_pushboolean)
1835 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, size_hints_honor, lua_pushboolean)
1836 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, maximized_horizontal, lua_pushboolean)
1837 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, maximized_vertical, lua_pushboolean)
1838 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, startup_id, lua_pushstring)
1840 static int
1841 luaA_client_get_maximized(lua_State *L, client_t *c)
1843 lua_pushboolean(L, client_get_maximized(c));
1844 return 1;
1847 static int
1848 luaA_client_get_content(lua_State *L, client_t *c)
1850 cairo_surface_t *surface;
1851 int width = c->geometry.width;
1852 int height = c->geometry.height;
1854 /* Just the client size without decorations */
1855 width -= c->titlebar[CLIENT_TITLEBAR_LEFT].size + c->titlebar[CLIENT_TITLEBAR_RIGHT].size;
1856 height -= c->titlebar[CLIENT_TITLEBAR_TOP].size + c->titlebar[CLIENT_TITLEBAR_BOTTOM].size;
1858 surface = cairo_xcb_surface_create(globalconf.connection, c->window,
1859 c->visualtype, width, height);
1861 /* lua has to make sure to free the ref or we have a leak */
1862 lua_pushlightuserdata(L, surface);
1863 return 1;
1866 static int
1867 luaA_client_get_screen(lua_State *L, client_t *c)
1869 if(!c->screen)
1870 return 0;
1871 lua_pushnumber(L, screen_get_index(c->screen));
1872 return 1;
1875 static int
1876 luaA_client_get_icon(lua_State *L, client_t *c)
1878 if(!c->icon)
1879 return 0;
1880 /* lua gets its own reference which it will have to destroy */
1881 lua_pushlightuserdata(L, cairo_surface_reference(c->icon));
1882 return 1;
1885 static int
1886 luaA_client_get_focusable(lua_State *L, client_t *c)
1888 bool ret;
1890 /* A client can be focused if it doesnt have the "nofocus" hint...*/
1891 if (!c->nofocus)
1892 ret = true;
1893 else
1894 /* ...or if it knows the WM_TAKE_FOCUS protocol */
1895 ret = client_hasproto(c, WM_TAKE_FOCUS);
1897 lua_pushboolean(L, ret);
1898 return 1;
1901 static int
1902 luaA_client_get_size_hints(lua_State *L, client_t *c)
1904 const char *u_or_p = NULL;
1906 lua_createtable(L, 0, 1);
1908 if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_US_POSITION)
1909 u_or_p = "user_position";
1910 else if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_POSITION)
1911 u_or_p = "program_position";
1913 if(u_or_p)
1915 lua_createtable(L, 0, 2);
1916 lua_pushnumber(L, c->size_hints.x);
1917 lua_setfield(L, -2, "x");
1918 lua_pushnumber(L, c->size_hints.y);
1919 lua_setfield(L, -2, "y");
1920 lua_setfield(L, -2, u_or_p);
1921 u_or_p = NULL;
1924 if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_US_SIZE)
1925 u_or_p = "user_size";
1926 else if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_SIZE)
1927 u_or_p = "program_size";
1929 if(u_or_p)
1931 lua_createtable(L, 0, 2);
1932 lua_pushnumber(L, c->size_hints.width);
1933 lua_setfield(L, -2, "width");
1934 lua_pushnumber(L, c->size_hints.height);
1935 lua_setfield(L, -2, "height");
1936 lua_setfield(L, -2, u_or_p);
1939 if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE)
1941 lua_pushnumber(L, c->size_hints.min_width);
1942 lua_setfield(L, -2, "min_width");
1943 lua_pushnumber(L, c->size_hints.min_height);
1944 lua_setfield(L, -2, "min_height");
1947 if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE)
1949 lua_pushnumber(L, c->size_hints.max_width);
1950 lua_setfield(L, -2, "max_width");
1951 lua_pushnumber(L, c->size_hints.max_height);
1952 lua_setfield(L, -2, "max_height");
1955 if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC)
1957 lua_pushnumber(L, c->size_hints.width_inc);
1958 lua_setfield(L, -2, "width_inc");
1959 lua_pushnumber(L, c->size_hints.height_inc);
1960 lua_setfield(L, -2, "height_inc");
1963 if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_ASPECT)
1965 lua_pushnumber(L, c->size_hints.min_aspect_num);
1966 lua_setfield(L, -2, "min_aspect_num");
1967 lua_pushnumber(L, c->size_hints.min_aspect_den);
1968 lua_setfield(L, -2, "min_aspect_den");
1969 lua_pushnumber(L, c->size_hints.max_aspect_num);
1970 lua_setfield(L, -2, "max_aspect_num");
1971 lua_pushnumber(L, c->size_hints.max_aspect_den);
1972 lua_setfield(L, -2, "max_aspect_den");
1975 if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_BASE_SIZE)
1977 lua_pushnumber(L, c->size_hints.base_width);
1978 lua_setfield(L, -2, "base_width");
1979 lua_pushnumber(L, c->size_hints.base_height);
1980 lua_setfield(L, -2, "base_height");
1983 if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_WIN_GRAVITY)
1985 switch(c->size_hints.win_gravity)
1987 default:
1988 lua_pushliteral(L, "north_west");
1989 break;
1990 case XCB_GRAVITY_NORTH:
1991 lua_pushliteral(L, "north");
1992 break;
1993 case XCB_GRAVITY_NORTH_EAST:
1994 lua_pushliteral(L, "north_east");
1995 break;
1996 case XCB_GRAVITY_WEST:
1997 lua_pushliteral(L, "west");
1998 break;
1999 case XCB_GRAVITY_CENTER:
2000 lua_pushliteral(L, "center");
2001 break;
2002 case XCB_GRAVITY_EAST:
2003 lua_pushliteral(L, "east");
2004 break;
2005 case XCB_GRAVITY_SOUTH_WEST:
2006 lua_pushliteral(L, "south_west");
2007 break;
2008 case XCB_GRAVITY_SOUTH:
2009 lua_pushliteral(L, "south");
2010 break;
2011 case XCB_GRAVITY_SOUTH_EAST:
2012 lua_pushliteral(L, "south_east");
2013 break;
2014 case XCB_GRAVITY_STATIC:
2015 lua_pushliteral(L, "static");
2016 break;
2018 lua_setfield(L, -2, "win_gravity");
2021 return 1;
2024 /** Get the client's child window bounding shape.
2025 * \param L The Lua VM state.
2026 * \param client The client object.
2027 * \return The number of elements pushed on stack.
2029 static int
2030 luaA_client_get_client_shape_bounding(lua_State *L, client_t *c)
2032 cairo_surface_t *surf = xwindow_get_shape(c->window, XCB_SHAPE_SK_BOUNDING);
2033 if (!surf)
2034 return 0;
2035 /* lua has to make sure to free the ref or we have a leak */
2036 lua_pushlightuserdata(L, surf);
2037 return 1;
2040 /** Get the client's frame window bounding shape.
2041 * \param L The Lua VM state.
2042 * \param client The client object.
2043 * \return The number of elements pushed on stack.
2045 static int
2046 luaA_client_get_shape_bounding(lua_State *L, client_t *c)
2048 cairo_surface_t *surf = xwindow_get_shape(c->frame_window, XCB_SHAPE_SK_BOUNDING);
2049 if (!surf)
2050 return 0;
2051 /* lua has to make sure to free the ref or we have a leak */
2052 lua_pushlightuserdata(L, surf);
2053 return 1;
2056 /** Set the client's frame window bounding shape.
2057 * \param L The Lua VM state.
2058 * \param client The client object.
2059 * \return The number of elements pushed on stack.
2061 static int
2062 luaA_client_set_shape_bounding(lua_State *L, client_t *c)
2064 cairo_surface_t *surf = NULL;
2065 if(!lua_isnil(L, -1))
2066 surf = (cairo_surface_t *)lua_touserdata(L, -1);
2067 xwindow_set_shape(c->frame_window,
2068 c->geometry.width + (c->border_width * 2),
2069 c->geometry.height + (c->border_width * 2),
2070 XCB_SHAPE_SK_BOUNDING, surf, -c->border_width);
2071 luaA_object_emit_signal(L, -3, "property::shape_bounding", 0);
2072 return 0;
2075 /** Get the client's child window clip shape.
2076 * \param L The Lua VM state.
2077 * \param client The client object.
2078 * \return The number of elements pushed on stack.
2080 static int
2081 luaA_client_get_client_shape_clip(lua_State *L, client_t *c)
2083 cairo_surface_t *surf = xwindow_get_shape(c->window, XCB_SHAPE_SK_CLIP);
2084 if (!surf)
2085 return 0;
2086 /* lua has to make sure to free the ref or we have a leak */
2087 lua_pushlightuserdata(L, surf);
2088 return 1;
2091 /** Get the client's frame window clip shape.
2092 * \param L The Lua VM state.
2093 * \param client The client object.
2094 * \return The number of elements pushed on stack.
2096 static int
2097 luaA_client_get_shape_clip(lua_State *L, client_t *c)
2099 cairo_surface_t *surf = xwindow_get_shape(c->frame_window, XCB_SHAPE_SK_CLIP);
2100 if (!surf)
2101 return 0;
2102 /* lua has to make sure to free the ref or we have a leak */
2103 lua_pushlightuserdata(L, surf);
2104 return 1;
2107 /** Set the client's frame window clip shape.
2108 * \param L The Lua VM state.
2109 * \param client The client object.
2110 * \return The number of elements pushed on stack.
2112 static int
2113 luaA_client_set_shape_clip(lua_State *L, client_t *c)
2115 cairo_surface_t *surf = NULL;
2116 if(!lua_isnil(L, -1))
2117 surf = (cairo_surface_t *)lua_touserdata(L, -1);
2118 xwindow_set_shape(c->frame_window, c->geometry.width, c->geometry.height,
2119 XCB_SHAPE_SK_CLIP, surf, 0);
2120 luaA_object_emit_signal(L, -3, "property::shape_clip", 0);
2121 return 0;
2124 /** Get or set keys bindings for a client.
2125 * \param L The Lua VM state.
2126 * \return The number of element pushed on stack.
2127 * \luastack
2128 * \lvalue A client.
2129 * \lparam An array of key bindings objects, or nothing.
2130 * \return The array of key bindings objects of this client.
2132 static int
2133 luaA_client_keys(lua_State *L)
2135 client_t *c = luaA_checkudata(L, 1, &client_class);
2136 key_array_t *keys = &c->keys;
2138 if(lua_gettop(L) == 2)
2140 luaA_key_array_set(L, 1, 2, keys);
2141 luaA_object_emit_signal(L, 1, "property::keys", 0);
2142 xcb_ungrab_key(globalconf.connection, XCB_GRAB_ANY, c->frame_window, XCB_BUTTON_MASK_ANY);
2143 xwindow_grabkeys(c->frame_window, keys);
2146 return luaA_key_array_get(L, 1, keys);
2149 /* Client module.
2150 * \param L The Lua VM state.
2151 * \return The number of pushed elements.
2153 static int
2154 luaA_client_module_index(lua_State *L)
2156 const char *buf = luaL_checkstring(L, 2);
2158 if (A_STREQ(buf, "focus"))
2159 return luaA_object_push(globalconf.L, globalconf.focus.client);
2160 return 0;
2163 /* Client module new index.
2164 * \param L The Lua VM state.
2165 * \return The number of pushed elements.
2167 static int
2168 luaA_client_module_newindex(lua_State *L)
2170 const char *buf = luaL_checkstring(L, 2);
2171 client_t *c;
2173 if (A_STREQ(buf, "focus"))
2175 c = luaA_checkudata(L, 3, &client_class);
2176 client_focus(c);
2179 return 0;
2182 static bool
2183 client_checker(client_t *c)
2185 return c->window != XCB_NONE;
2188 void
2189 client_class_setup(lua_State *L)
2191 static const struct luaL_Reg client_methods[] =
2193 LUA_CLASS_METHODS(client)
2194 { "get", luaA_client_get },
2195 { "__index", luaA_client_module_index },
2196 { "__newindex", luaA_client_module_newindex },
2197 { NULL, NULL }
2200 static const struct luaL_Reg client_meta[] =
2202 LUA_OBJECT_META(client)
2203 LUA_CLASS_META
2204 { "keys", luaA_client_keys },
2205 { "isvisible", luaA_client_isvisible },
2206 { "geometry", luaA_client_geometry },
2207 { "tags", luaA_client_tags },
2208 { "kill", luaA_client_kill },
2209 { "swap", luaA_client_swap },
2210 { "raise", luaA_client_raise },
2211 { "lower", luaA_client_lower },
2212 { "unmanage", luaA_client_unmanage },
2213 { "titlebar_top", luaA_client_titlebar_top },
2214 { "titlebar_right", luaA_client_titlebar_right },
2215 { "titlebar_bottom", luaA_client_titlebar_bottom },
2216 { "titlebar_left", luaA_client_titlebar_left },
2217 { NULL, NULL }
2220 luaA_class_setup(L, &client_class, "client", &window_class,
2221 (lua_class_allocator_t) client_new,
2222 (lua_class_collector_t) client_wipe,
2223 (lua_class_checker_t) client_checker,
2224 luaA_class_index_miss_property, luaA_class_newindex_miss_property,
2225 client_methods, client_meta);
2226 luaA_class_add_property(&client_class, "name",
2227 NULL,
2228 (lua_class_propfunc_t) luaA_client_get_name,
2229 NULL);
2230 luaA_class_add_property(&client_class, "transient_for",
2231 NULL,
2232 (lua_class_propfunc_t) luaA_client_get_transient_for,
2233 NULL);
2234 luaA_class_add_property(&client_class, "skip_taskbar",
2235 (lua_class_propfunc_t) luaA_client_set_skip_taskbar,
2236 (lua_class_propfunc_t) luaA_client_get_skip_taskbar,
2237 (lua_class_propfunc_t) luaA_client_set_skip_taskbar);
2238 luaA_class_add_property(&client_class, "content",
2239 NULL,
2240 (lua_class_propfunc_t) luaA_client_get_content,
2241 NULL);
2242 luaA_class_add_property(&client_class, "type",
2243 NULL,
2244 (lua_class_propfunc_t) luaA_window_get_type,
2245 NULL);
2246 luaA_class_add_property(&client_class, "class",
2247 NULL,
2248 (lua_class_propfunc_t) luaA_client_get_class,
2249 NULL);
2250 luaA_class_add_property(&client_class, "instance",
2251 NULL,
2252 (lua_class_propfunc_t) luaA_client_get_instance,
2253 NULL);
2254 luaA_class_add_property(&client_class, "role",
2255 NULL,
2256 (lua_class_propfunc_t) luaA_client_get_role,
2257 NULL);
2258 luaA_class_add_property(&client_class, "pid",
2259 NULL,
2260 (lua_class_propfunc_t) luaA_client_get_pid,
2261 NULL);
2262 luaA_class_add_property(&client_class, "leader_window",
2263 NULL,
2264 (lua_class_propfunc_t) luaA_client_get_leader_window,
2265 NULL);
2266 luaA_class_add_property(&client_class, "machine",
2267 NULL,
2268 (lua_class_propfunc_t) luaA_client_get_machine,
2269 NULL);
2270 luaA_class_add_property(&client_class, "icon_name",
2271 NULL,
2272 (lua_class_propfunc_t) luaA_client_get_icon_name,
2273 NULL);
2274 luaA_class_add_property(&client_class, "screen",
2275 NULL,
2276 (lua_class_propfunc_t) luaA_client_get_screen,
2277 (lua_class_propfunc_t) luaA_client_set_screen);
2278 luaA_class_add_property(&client_class, "hidden",
2279 (lua_class_propfunc_t) luaA_client_set_hidden,
2280 (lua_class_propfunc_t) luaA_client_get_hidden,
2281 (lua_class_propfunc_t) luaA_client_set_hidden);
2282 luaA_class_add_property(&client_class, "minimized",
2283 (lua_class_propfunc_t) luaA_client_set_minimized,
2284 (lua_class_propfunc_t) luaA_client_get_minimized,
2285 (lua_class_propfunc_t) luaA_client_set_minimized);
2286 luaA_class_add_property(&client_class, "fullscreen",
2287 (lua_class_propfunc_t) luaA_client_set_fullscreen,
2288 (lua_class_propfunc_t) luaA_client_get_fullscreen,
2289 (lua_class_propfunc_t) luaA_client_set_fullscreen);
2290 luaA_class_add_property(&client_class, "modal",
2291 (lua_class_propfunc_t) luaA_client_set_modal,
2292 (lua_class_propfunc_t) luaA_client_get_modal,
2293 (lua_class_propfunc_t) luaA_client_set_modal);
2294 luaA_class_add_property(&client_class, "group_window",
2295 NULL,
2296 (lua_class_propfunc_t) luaA_client_get_group_window,
2297 NULL);
2298 luaA_class_add_property(&client_class, "maximized",
2299 (lua_class_propfunc_t) luaA_client_set_maximized,
2300 (lua_class_propfunc_t) luaA_client_get_maximized,
2301 (lua_class_propfunc_t) luaA_client_set_maximized);
2302 luaA_class_add_property(&client_class, "maximized_horizontal",
2303 (lua_class_propfunc_t) luaA_client_set_maximized_horizontal,
2304 (lua_class_propfunc_t) luaA_client_get_maximized_horizontal,
2305 (lua_class_propfunc_t) luaA_client_set_maximized_horizontal);
2306 luaA_class_add_property(&client_class, "maximized_vertical",
2307 (lua_class_propfunc_t) luaA_client_set_maximized_vertical,
2308 (lua_class_propfunc_t) luaA_client_get_maximized_vertical,
2309 (lua_class_propfunc_t) luaA_client_set_maximized_vertical);
2310 luaA_class_add_property(&client_class, "icon",
2311 (lua_class_propfunc_t) luaA_client_set_icon,
2312 (lua_class_propfunc_t) luaA_client_get_icon,
2313 (lua_class_propfunc_t) luaA_client_set_icon);
2314 luaA_class_add_property(&client_class, "ontop",
2315 (lua_class_propfunc_t) luaA_client_set_ontop,
2316 (lua_class_propfunc_t) luaA_client_get_ontop,
2317 (lua_class_propfunc_t) luaA_client_set_ontop);
2318 luaA_class_add_property(&client_class, "above",
2319 (lua_class_propfunc_t) luaA_client_set_above,
2320 (lua_class_propfunc_t) luaA_client_get_above,
2321 (lua_class_propfunc_t) luaA_client_set_above);
2322 luaA_class_add_property(&client_class, "below",
2323 (lua_class_propfunc_t) luaA_client_set_below,
2324 (lua_class_propfunc_t) luaA_client_get_below,
2325 (lua_class_propfunc_t) luaA_client_set_below);
2326 luaA_class_add_property(&client_class, "sticky",
2327 (lua_class_propfunc_t) luaA_client_set_sticky,
2328 (lua_class_propfunc_t) luaA_client_get_sticky,
2329 (lua_class_propfunc_t) luaA_client_set_sticky);
2330 luaA_class_add_property(&client_class, "size_hints_honor",
2331 (lua_class_propfunc_t) luaA_client_set_size_hints_honor,
2332 (lua_class_propfunc_t) luaA_client_get_size_hints_honor,
2333 (lua_class_propfunc_t) luaA_client_set_size_hints_honor);
2334 luaA_class_add_property(&client_class, "urgent",
2335 (lua_class_propfunc_t) luaA_client_set_urgent,
2336 (lua_class_propfunc_t) luaA_client_get_urgent,
2337 (lua_class_propfunc_t) luaA_client_set_urgent);
2338 luaA_class_add_property(&client_class, "size_hints",
2339 NULL,
2340 (lua_class_propfunc_t) luaA_client_get_size_hints,
2341 NULL);
2342 luaA_class_add_property(&client_class, "focusable",
2343 NULL,
2344 (lua_class_propfunc_t) luaA_client_get_focusable,
2345 NULL);
2346 luaA_class_add_property(&client_class, "shape_bounding",
2347 (lua_class_propfunc_t) luaA_client_set_shape_bounding,
2348 (lua_class_propfunc_t) luaA_client_get_shape_bounding,
2349 (lua_class_propfunc_t) luaA_client_set_shape_bounding);
2350 luaA_class_add_property(&client_class, "shape_clip",
2351 (lua_class_propfunc_t) luaA_client_set_shape_clip,
2352 (lua_class_propfunc_t) luaA_client_get_shape_clip,
2353 (lua_class_propfunc_t) luaA_client_set_shape_clip);
2354 luaA_class_add_property(&client_class, "startup_id",
2355 NULL,
2356 (lua_class_propfunc_t) luaA_client_get_startup_id,
2357 NULL);
2358 luaA_class_add_property(&client_class, "client_shape_bounding",
2359 NULL,
2360 (lua_class_propfunc_t) luaA_client_get_client_shape_bounding,
2361 NULL);
2362 luaA_class_add_property(&client_class, "client_shape_clip",
2363 NULL,
2364 (lua_class_propfunc_t) luaA_client_get_client_shape_clip,
2365 NULL);
2367 signal_add(&client_class.signals, "focus");
2368 signal_add(&client_class.signals, "list");
2369 signal_add(&client_class.signals, "manage");
2370 signal_add(&client_class.signals, "button::press");
2371 signal_add(&client_class.signals, "button::release");
2372 signal_add(&client_class.signals, "mouse::enter");
2373 signal_add(&client_class.signals, "mouse::leave");
2374 signal_add(&client_class.signals, "mouse::move");
2375 signal_add(&client_class.signals, "property::above");
2376 signal_add(&client_class.signals, "property::below");
2377 signal_add(&client_class.signals, "property::class");
2378 signal_add(&client_class.signals, "property::fullscreen");
2379 signal_add(&client_class.signals, "property::geometry");
2380 signal_add(&client_class.signals, "property::group_window");
2381 signal_add(&client_class.signals, "property::height");
2382 signal_add(&client_class.signals, "property::hidden");
2383 signal_add(&client_class.signals, "property::icon");
2384 signal_add(&client_class.signals, "property::icon_name");
2385 signal_add(&client_class.signals, "property::instance");
2386 signal_add(&client_class.signals, "property::keys");
2387 signal_add(&client_class.signals, "property::machine");
2388 signal_add(&client_class.signals, "property::maximized");
2389 signal_add(&client_class.signals, "property::maximized_horizontal");
2390 signal_add(&client_class.signals, "property::maximized_vertical");
2391 signal_add(&client_class.signals, "property::minimized");
2392 signal_add(&client_class.signals, "property::modal");
2393 signal_add(&client_class.signals, "property::name");
2394 signal_add(&client_class.signals, "property::ontop");
2395 signal_add(&client_class.signals, "property::pid");
2396 signal_add(&client_class.signals, "property::role");
2397 signal_add(&client_class.signals, "property::screen");
2398 signal_add(&client_class.signals, "property::shape_bounding");
2399 signal_add(&client_class.signals, "property::shape_client_bounding");
2400 signal_add(&client_class.signals, "property::shape_client_clip");
2401 signal_add(&client_class.signals, "property::shape_clip");
2402 signal_add(&client_class.signals, "property::size_hints_honor");
2403 signal_add(&client_class.signals, "property::skip_taskbar");
2404 signal_add(&client_class.signals, "property::sticky");
2405 signal_add(&client_class.signals, "property::struts");
2406 signal_add(&client_class.signals, "property::titlebar_bottom");
2407 signal_add(&client_class.signals, "property::titlebar_left");
2408 signal_add(&client_class.signals, "property::titlebar_right");
2409 signal_add(&client_class.signals, "property::titlebar_top");
2410 signal_add(&client_class.signals, "property::transient_for");
2411 signal_add(&client_class.signals, "property::type");
2412 signal_add(&client_class.signals, "property::urgent");
2413 signal_add(&client_class.signals, "property::width");
2414 signal_add(&client_class.signals, "property::window");
2415 signal_add(&client_class.signals, "property::x");
2416 signal_add(&client_class.signals, "property::y");
2417 signal_add(&client_class.signals, "request::activate");
2418 signal_add(&client_class.signals, "request::fullscreen");
2419 signal_add(&client_class.signals, "request::maximized_horizontal");
2420 signal_add(&client_class.signals, "request::maximized_vertical");
2421 signal_add(&client_class.signals, "request::tag");
2422 signal_add(&client_class.signals, "tagged");
2423 signal_add(&client_class.signals, "unfocus");
2424 signal_add(&client_class.signals, "unmanage");
2425 signal_add(&client_class.signals, "untagged");
2428 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80