tasklist: add/use a single icon for "maximized"
[awesome.git] / objects / client.c
blobf8eff42ba167a30e5e11ef6e54f4567d311bc990
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(client_t *c, client_titlebar_t bar, int size)
1589 if (size < 0)
1590 return;
1592 if (size == c->titlebar[bar].size)
1593 return;
1595 /* Now resize the client (and titlebars!) suitably (the client without
1596 * titlebars should keep its current size!) */
1597 area_t geometry = c->geometry;
1598 int change = size - c->titlebar[bar].size;
1599 switch (bar) {
1600 case CLIENT_TITLEBAR_TOP:
1601 case CLIENT_TITLEBAR_BOTTOM:
1602 geometry.height += change;
1603 break;
1604 case CLIENT_TITLEBAR_RIGHT:
1605 case CLIENT_TITLEBAR_LEFT:
1606 geometry.width += change;
1607 break;
1608 default:
1609 fatal("Unknown titlebar kind %d\n", (int) bar);
1612 c->titlebar[bar].size = size;
1613 client_resize_do(c, geometry, true, false);
1616 #define HANDLE_TITLEBAR(name, index) \
1617 static int \
1618 luaA_client_titlebar_ ## name(lua_State *L) \
1620 client_t *c = luaA_checkudata(L, 1, &client_class); \
1622 if (lua_gettop(L) == 2) \
1624 if (lua_isnil(L, 2)) \
1625 titlebar_resize(c, index, 0); \
1626 else \
1627 titlebar_resize(c, index, luaL_checknumber(L, 2)); \
1630 luaA_object_push_item(L, 1, titlebar_get_drawable(L, c, 1, index)); \
1631 lua_pushnumber(L, c->titlebar[index].size); \
1632 return 2; \
1634 HANDLE_TITLEBAR(top, CLIENT_TITLEBAR_TOP)
1635 HANDLE_TITLEBAR(right, CLIENT_TITLEBAR_RIGHT)
1636 HANDLE_TITLEBAR(bottom, CLIENT_TITLEBAR_BOTTOM)
1637 HANDLE_TITLEBAR(left, CLIENT_TITLEBAR_LEFT)
1639 /** Return or set client geometry.
1640 * \param L The Lua VM state.
1641 * \return The number of elements pushed on stack.
1642 * \luastack
1643 * \lparam A table with new coordinates, or none.
1644 * \lreturn A table with client coordinates.
1646 static int
1647 luaA_client_geometry(lua_State *L)
1649 client_t *c = luaA_checkudata(L, 1, &client_class);
1651 if(lua_gettop(L) == 2 && !lua_isnil(L, 2))
1653 area_t geometry;
1655 luaA_checktable(L, 2);
1656 geometry.x = luaA_getopt_number(L, 2, "x", c->geometry.x);
1657 geometry.y = luaA_getopt_number(L, 2, "y", c->geometry.y);
1658 if(client_isfixed(c))
1660 geometry.width = c->geometry.width;
1661 geometry.height = c->geometry.height;
1663 else
1665 geometry.width = luaA_getopt_number(L, 2, "width", c->geometry.width);
1666 geometry.height = luaA_getopt_number(L, 2, "height", c->geometry.height);
1669 client_resize(c, geometry, c->size_hints_honor);
1672 return luaA_pusharea(L, c->geometry);
1675 static int
1676 luaA_client_set_screen(lua_State *L, client_t *c)
1678 screen_client_moveto(c, luaA_checkscreen(L, -1), true);
1679 return 0;
1682 static int
1683 luaA_client_set_hidden(lua_State *L, client_t *c)
1685 client_set_hidden(L, -3, luaA_checkboolean(L, -1));
1686 return 0;
1689 static int
1690 luaA_client_set_minimized(lua_State *L, client_t *c)
1692 client_set_minimized(L, -3, luaA_checkboolean(L, -1));
1693 return 0;
1696 static int
1697 luaA_client_set_fullscreen(lua_State *L, client_t *c)
1699 client_set_fullscreen(L, -3, luaA_checkboolean(L, -1));
1700 return 0;
1703 static int
1704 luaA_client_set_modal(lua_State *L, client_t *c)
1706 client_set_modal(L, -3, luaA_checkboolean(L, -1));
1707 return 0;
1710 static int
1711 luaA_client_set_maximized(lua_State *L, client_t *c)
1713 client_set_maximized(L, -3, luaA_checkboolean(L, -1));
1714 return 0;
1717 static int
1718 luaA_client_set_maximized_horizontal(lua_State *L, client_t *c)
1720 client_set_maximized_horizontal(L, -3, luaA_checkboolean(L, -1));
1721 return 0;
1724 static int
1725 luaA_client_set_maximized_vertical(lua_State *L, client_t *c)
1727 client_set_maximized_vertical(L, -3, luaA_checkboolean(L, -1));
1728 return 0;
1731 static int
1732 luaA_client_set_icon(lua_State *L, client_t *c)
1734 cairo_surface_t *surf = NULL;
1735 if(!lua_isnil(L, -1))
1736 surf = (cairo_surface_t *)lua_touserdata(L, -1);
1737 client_set_icon(c, surf);
1738 return 0;
1741 static int
1742 luaA_client_set_sticky(lua_State *L, client_t *c)
1744 client_set_sticky(L, -3, luaA_checkboolean(L, -1));
1745 return 0;
1748 static int
1749 luaA_client_set_size_hints_honor(lua_State *L, client_t *c)
1751 c->size_hints_honor = luaA_checkboolean(L, -1);
1752 luaA_object_emit_signal(L, -3, "property::size_hints_honor", 0);
1753 return 0;
1756 static int
1757 luaA_client_set_ontop(lua_State *L, client_t *c)
1759 client_set_ontop(L, -3, luaA_checkboolean(L, -1));
1760 return 0;
1763 static int
1764 luaA_client_set_below(lua_State *L, client_t *c)
1766 client_set_below(L, -3, luaA_checkboolean(L, -1));
1767 return 0;
1770 static int
1771 luaA_client_set_above(lua_State *L, client_t *c)
1773 client_set_above(L, -3, luaA_checkboolean(L, -1));
1774 return 0;
1777 static int
1778 luaA_client_set_urgent(lua_State *L, client_t *c)
1780 client_set_urgent(L, -3, luaA_checkboolean(L, -1));
1781 return 0;
1784 static int
1785 luaA_client_set_skip_taskbar(lua_State *L, client_t *c)
1787 client_set_skip_taskbar(L, -3, luaA_checkboolean(L, -1));
1788 return 0;
1791 static int
1792 luaA_client_get_name(lua_State *L, client_t *c)
1794 lua_pushstring(L, c->name ? c->name : c->alt_name);
1795 return 1;
1798 static int
1799 luaA_client_get_icon_name(lua_State *L, client_t *c)
1801 lua_pushstring(L, c->icon_name ? c->icon_name : c->alt_icon_name);
1802 return 1;
1805 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, class, lua_pushstring)
1806 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, instance, lua_pushstring)
1807 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, machine, lua_pushstring)
1808 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, role, lua_pushstring)
1809 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, transient_for, luaA_object_push)
1810 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, skip_taskbar, lua_pushboolean)
1811 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, leader_window, lua_pushnumber)
1812 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, group_window, lua_pushnumber)
1813 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, pid, lua_pushnumber)
1814 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, hidden, lua_pushboolean)
1815 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, minimized, lua_pushboolean)
1816 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, fullscreen, lua_pushboolean)
1817 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, modal, lua_pushboolean)
1818 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, ontop, lua_pushboolean)
1819 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, urgent, lua_pushboolean)
1820 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, above, lua_pushboolean)
1821 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, below, lua_pushboolean)
1822 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, sticky, lua_pushboolean)
1823 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, size_hints_honor, lua_pushboolean)
1824 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, maximized_horizontal, lua_pushboolean)
1825 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, maximized_vertical, lua_pushboolean)
1826 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, startup_id, lua_pushstring)
1828 static int
1829 luaA_client_get_maximized(lua_State *L, client_t *c)
1831 lua_pushboolean(L, client_get_maximized(c));
1832 return 1;
1835 static int
1836 luaA_client_get_content(lua_State *L, client_t *c)
1838 cairo_surface_t *surface;
1839 int width = c->geometry.width;
1840 int height = c->geometry.height;
1842 /* Just the client size without decorations */
1843 width -= c->titlebar[CLIENT_TITLEBAR_LEFT].size + c->titlebar[CLIENT_TITLEBAR_RIGHT].size;
1844 height -= c->titlebar[CLIENT_TITLEBAR_TOP].size + c->titlebar[CLIENT_TITLEBAR_BOTTOM].size;
1846 surface = cairo_xcb_surface_create(globalconf.connection, c->window,
1847 c->visualtype, width, height);
1849 /* lua has to make sure to free the ref or we have a leak */
1850 lua_pushlightuserdata(L, surface);
1851 return 1;
1854 static int
1855 luaA_client_get_screen(lua_State *L, client_t *c)
1857 if(!c->screen)
1858 return 0;
1859 lua_pushnumber(L, screen_get_index(c->screen));
1860 return 1;
1863 static int
1864 luaA_client_get_icon(lua_State *L, client_t *c)
1866 if(!c->icon)
1867 return 0;
1868 /* lua gets its own reference which it will have to destroy */
1869 lua_pushlightuserdata(L, cairo_surface_reference(c->icon));
1870 return 1;
1873 static int
1874 luaA_client_get_focusable(lua_State *L, client_t *c)
1876 bool ret;
1878 /* A client can be focused if it doesnt have the "nofocus" hint...*/
1879 if (!c->nofocus)
1880 ret = true;
1881 else
1882 /* ...or if it knows the WM_TAKE_FOCUS protocol */
1883 ret = client_hasproto(c, WM_TAKE_FOCUS);
1885 lua_pushboolean(L, ret);
1886 return 1;
1889 static int
1890 luaA_client_get_size_hints(lua_State *L, client_t *c)
1892 const char *u_or_p = NULL;
1894 lua_createtable(L, 0, 1);
1896 if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_US_POSITION)
1897 u_or_p = "user_position";
1898 else if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_POSITION)
1899 u_or_p = "program_position";
1901 if(u_or_p)
1903 lua_createtable(L, 0, 2);
1904 lua_pushnumber(L, c->size_hints.x);
1905 lua_setfield(L, -2, "x");
1906 lua_pushnumber(L, c->size_hints.y);
1907 lua_setfield(L, -2, "y");
1908 lua_setfield(L, -2, u_or_p);
1909 u_or_p = NULL;
1912 if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_US_SIZE)
1913 u_or_p = "user_size";
1914 else if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_SIZE)
1915 u_or_p = "program_size";
1917 if(u_or_p)
1919 lua_createtable(L, 0, 2);
1920 lua_pushnumber(L, c->size_hints.width);
1921 lua_setfield(L, -2, "width");
1922 lua_pushnumber(L, c->size_hints.height);
1923 lua_setfield(L, -2, "height");
1924 lua_setfield(L, -2, u_or_p);
1927 if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE)
1929 lua_pushnumber(L, c->size_hints.min_width);
1930 lua_setfield(L, -2, "min_width");
1931 lua_pushnumber(L, c->size_hints.min_height);
1932 lua_setfield(L, -2, "min_height");
1935 if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE)
1937 lua_pushnumber(L, c->size_hints.max_width);
1938 lua_setfield(L, -2, "max_width");
1939 lua_pushnumber(L, c->size_hints.max_height);
1940 lua_setfield(L, -2, "max_height");
1943 if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC)
1945 lua_pushnumber(L, c->size_hints.width_inc);
1946 lua_setfield(L, -2, "width_inc");
1947 lua_pushnumber(L, c->size_hints.height_inc);
1948 lua_setfield(L, -2, "height_inc");
1951 if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_ASPECT)
1953 lua_pushnumber(L, c->size_hints.min_aspect_num);
1954 lua_setfield(L, -2, "min_aspect_num");
1955 lua_pushnumber(L, c->size_hints.min_aspect_den);
1956 lua_setfield(L, -2, "min_aspect_den");
1957 lua_pushnumber(L, c->size_hints.max_aspect_num);
1958 lua_setfield(L, -2, "max_aspect_num");
1959 lua_pushnumber(L, c->size_hints.max_aspect_den);
1960 lua_setfield(L, -2, "max_aspect_den");
1963 if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_BASE_SIZE)
1965 lua_pushnumber(L, c->size_hints.base_width);
1966 lua_setfield(L, -2, "base_width");
1967 lua_pushnumber(L, c->size_hints.base_height);
1968 lua_setfield(L, -2, "base_height");
1971 if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_WIN_GRAVITY)
1973 switch(c->size_hints.win_gravity)
1975 default:
1976 lua_pushliteral(L, "north_west");
1977 break;
1978 case XCB_GRAVITY_NORTH:
1979 lua_pushliteral(L, "north");
1980 break;
1981 case XCB_GRAVITY_NORTH_EAST:
1982 lua_pushliteral(L, "north_east");
1983 break;
1984 case XCB_GRAVITY_WEST:
1985 lua_pushliteral(L, "west");
1986 break;
1987 case XCB_GRAVITY_CENTER:
1988 lua_pushliteral(L, "center");
1989 break;
1990 case XCB_GRAVITY_EAST:
1991 lua_pushliteral(L, "east");
1992 break;
1993 case XCB_GRAVITY_SOUTH_WEST:
1994 lua_pushliteral(L, "south_west");
1995 break;
1996 case XCB_GRAVITY_SOUTH:
1997 lua_pushliteral(L, "south");
1998 break;
1999 case XCB_GRAVITY_SOUTH_EAST:
2000 lua_pushliteral(L, "south_east");
2001 break;
2002 case XCB_GRAVITY_STATIC:
2003 lua_pushliteral(L, "static");
2004 break;
2006 lua_setfield(L, -2, "win_gravity");
2009 return 1;
2012 /** Get the client's child window bounding shape.
2013 * \param L The Lua VM state.
2014 * \param client The client object.
2015 * \return The number of elements pushed on stack.
2017 static int
2018 luaA_client_get_client_shape_bounding(lua_State *L, client_t *c)
2020 cairo_surface_t *surf = xwindow_get_shape(c->window, XCB_SHAPE_SK_BOUNDING);
2021 if (!surf)
2022 return 0;
2023 /* lua has to make sure to free the ref or we have a leak */
2024 lua_pushlightuserdata(L, surf);
2025 return 1;
2028 /** Get the client's frame window bounding shape.
2029 * \param L The Lua VM state.
2030 * \param client The client object.
2031 * \return The number of elements pushed on stack.
2033 static int
2034 luaA_client_get_shape_bounding(lua_State *L, client_t *c)
2036 cairo_surface_t *surf = xwindow_get_shape(c->frame_window, XCB_SHAPE_SK_BOUNDING);
2037 if (!surf)
2038 return 0;
2039 /* lua has to make sure to free the ref or we have a leak */
2040 lua_pushlightuserdata(L, surf);
2041 return 1;
2044 /** Set the client's frame window bounding shape.
2045 * \param L The Lua VM state.
2046 * \param client The client object.
2047 * \return The number of elements pushed on stack.
2049 static int
2050 luaA_client_set_shape_bounding(lua_State *L, client_t *c)
2052 cairo_surface_t *surf = NULL;
2053 if(!lua_isnil(L, -1))
2054 surf = (cairo_surface_t *)lua_touserdata(L, -1);
2055 xwindow_set_shape(c->frame_window,
2056 c->geometry.width + (c->border_width * 2),
2057 c->geometry.height + (c->border_width * 2),
2058 XCB_SHAPE_SK_BOUNDING, surf, -c->border_width);
2059 luaA_object_emit_signal(L, -3, "property::shape_bounding", 0);
2060 return 0;
2063 /** Get the client's child window clip shape.
2064 * \param L The Lua VM state.
2065 * \param client The client object.
2066 * \return The number of elements pushed on stack.
2068 static int
2069 luaA_client_get_client_shape_clip(lua_State *L, client_t *c)
2071 cairo_surface_t *surf = xwindow_get_shape(c->window, XCB_SHAPE_SK_CLIP);
2072 if (!surf)
2073 return 0;
2074 /* lua has to make sure to free the ref or we have a leak */
2075 lua_pushlightuserdata(L, surf);
2076 return 1;
2079 /** Get the client's frame window clip shape.
2080 * \param L The Lua VM state.
2081 * \param client The client object.
2082 * \return The number of elements pushed on stack.
2084 static int
2085 luaA_client_get_shape_clip(lua_State *L, client_t *c)
2087 cairo_surface_t *surf = xwindow_get_shape(c->frame_window, XCB_SHAPE_SK_CLIP);
2088 if (!surf)
2089 return 0;
2090 /* lua has to make sure to free the ref or we have a leak */
2091 lua_pushlightuserdata(L, surf);
2092 return 1;
2095 /** Set the client's frame window clip shape.
2096 * \param L The Lua VM state.
2097 * \param client The client object.
2098 * \return The number of elements pushed on stack.
2100 static int
2101 luaA_client_set_shape_clip(lua_State *L, client_t *c)
2103 cairo_surface_t *surf = NULL;
2104 if(!lua_isnil(L, -1))
2105 surf = (cairo_surface_t *)lua_touserdata(L, -1);
2106 xwindow_set_shape(c->frame_window, c->geometry.width, c->geometry.height,
2107 XCB_SHAPE_SK_CLIP, surf, 0);
2108 luaA_object_emit_signal(L, -3, "property::shape_clip", 0);
2109 return 0;
2112 /** Get or set keys bindings for a client.
2113 * \param L The Lua VM state.
2114 * \return The number of element pushed on stack.
2115 * \luastack
2116 * \lvalue A client.
2117 * \lparam An array of key bindings objects, or nothing.
2118 * \return The array of key bindings objects of this client.
2120 static int
2121 luaA_client_keys(lua_State *L)
2123 client_t *c = luaA_checkudata(L, 1, &client_class);
2124 key_array_t *keys = &c->keys;
2126 if(lua_gettop(L) == 2)
2128 luaA_key_array_set(L, 1, 2, keys);
2129 luaA_object_emit_signal(L, 1, "property::keys", 0);
2130 xcb_ungrab_key(globalconf.connection, XCB_GRAB_ANY, c->frame_window, XCB_BUTTON_MASK_ANY);
2131 xwindow_grabkeys(c->frame_window, keys);
2134 return luaA_key_array_get(L, 1, keys);
2137 /* Client module.
2138 * \param L The Lua VM state.
2139 * \return The number of pushed elements.
2141 static int
2142 luaA_client_module_index(lua_State *L)
2144 const char *buf = luaL_checkstring(L, 2);
2146 if (A_STREQ(buf, "focus"))
2147 return luaA_object_push(globalconf.L, globalconf.focus.client);
2148 return 0;
2151 /* Client module new index.
2152 * \param L The Lua VM state.
2153 * \return The number of pushed elements.
2155 static int
2156 luaA_client_module_newindex(lua_State *L)
2158 const char *buf = luaL_checkstring(L, 2);
2159 client_t *c;
2161 if (A_STREQ(buf, "focus"))
2163 c = luaA_checkudata(L, 3, &client_class);
2164 client_focus(c);
2167 return 0;
2170 static bool
2171 client_checker(client_t *c)
2173 return c->window != XCB_NONE;
2176 void
2177 client_class_setup(lua_State *L)
2179 static const struct luaL_Reg client_methods[] =
2181 LUA_CLASS_METHODS(client)
2182 { "get", luaA_client_get },
2183 { "__index", luaA_client_module_index },
2184 { "__newindex", luaA_client_module_newindex },
2185 { NULL, NULL }
2188 static const struct luaL_Reg client_meta[] =
2190 LUA_OBJECT_META(client)
2191 LUA_CLASS_META
2192 { "keys", luaA_client_keys },
2193 { "isvisible", luaA_client_isvisible },
2194 { "geometry", luaA_client_geometry },
2195 { "tags", luaA_client_tags },
2196 { "kill", luaA_client_kill },
2197 { "swap", luaA_client_swap },
2198 { "raise", luaA_client_raise },
2199 { "lower", luaA_client_lower },
2200 { "unmanage", luaA_client_unmanage },
2201 { "titlebar_top", luaA_client_titlebar_top },
2202 { "titlebar_right", luaA_client_titlebar_right },
2203 { "titlebar_bottom", luaA_client_titlebar_bottom },
2204 { "titlebar_left", luaA_client_titlebar_left },
2205 { NULL, NULL }
2208 luaA_class_setup(L, &client_class, "client", &window_class,
2209 (lua_class_allocator_t) client_new,
2210 (lua_class_collector_t) client_wipe,
2211 (lua_class_checker_t) client_checker,
2212 luaA_class_index_miss_property, luaA_class_newindex_miss_property,
2213 client_methods, client_meta);
2214 luaA_class_add_property(&client_class, "name",
2215 NULL,
2216 (lua_class_propfunc_t) luaA_client_get_name,
2217 NULL);
2218 luaA_class_add_property(&client_class, "transient_for",
2219 NULL,
2220 (lua_class_propfunc_t) luaA_client_get_transient_for,
2221 NULL);
2222 luaA_class_add_property(&client_class, "skip_taskbar",
2223 (lua_class_propfunc_t) luaA_client_set_skip_taskbar,
2224 (lua_class_propfunc_t) luaA_client_get_skip_taskbar,
2225 (lua_class_propfunc_t) luaA_client_set_skip_taskbar);
2226 luaA_class_add_property(&client_class, "content",
2227 NULL,
2228 (lua_class_propfunc_t) luaA_client_get_content,
2229 NULL);
2230 luaA_class_add_property(&client_class, "type",
2231 NULL,
2232 (lua_class_propfunc_t) luaA_window_get_type,
2233 NULL);
2234 luaA_class_add_property(&client_class, "class",
2235 NULL,
2236 (lua_class_propfunc_t) luaA_client_get_class,
2237 NULL);
2238 luaA_class_add_property(&client_class, "instance",
2239 NULL,
2240 (lua_class_propfunc_t) luaA_client_get_instance,
2241 NULL);
2242 luaA_class_add_property(&client_class, "role",
2243 NULL,
2244 (lua_class_propfunc_t) luaA_client_get_role,
2245 NULL);
2246 luaA_class_add_property(&client_class, "pid",
2247 NULL,
2248 (lua_class_propfunc_t) luaA_client_get_pid,
2249 NULL);
2250 luaA_class_add_property(&client_class, "leader_window",
2251 NULL,
2252 (lua_class_propfunc_t) luaA_client_get_leader_window,
2253 NULL);
2254 luaA_class_add_property(&client_class, "machine",
2255 NULL,
2256 (lua_class_propfunc_t) luaA_client_get_machine,
2257 NULL);
2258 luaA_class_add_property(&client_class, "icon_name",
2259 NULL,
2260 (lua_class_propfunc_t) luaA_client_get_icon_name,
2261 NULL);
2262 luaA_class_add_property(&client_class, "screen",
2263 NULL,
2264 (lua_class_propfunc_t) luaA_client_get_screen,
2265 (lua_class_propfunc_t) luaA_client_set_screen);
2266 luaA_class_add_property(&client_class, "hidden",
2267 (lua_class_propfunc_t) luaA_client_set_hidden,
2268 (lua_class_propfunc_t) luaA_client_get_hidden,
2269 (lua_class_propfunc_t) luaA_client_set_hidden);
2270 luaA_class_add_property(&client_class, "minimized",
2271 (lua_class_propfunc_t) luaA_client_set_minimized,
2272 (lua_class_propfunc_t) luaA_client_get_minimized,
2273 (lua_class_propfunc_t) luaA_client_set_minimized);
2274 luaA_class_add_property(&client_class, "fullscreen",
2275 (lua_class_propfunc_t) luaA_client_set_fullscreen,
2276 (lua_class_propfunc_t) luaA_client_get_fullscreen,
2277 (lua_class_propfunc_t) luaA_client_set_fullscreen);
2278 luaA_class_add_property(&client_class, "modal",
2279 (lua_class_propfunc_t) luaA_client_set_modal,
2280 (lua_class_propfunc_t) luaA_client_get_modal,
2281 (lua_class_propfunc_t) luaA_client_set_modal);
2282 luaA_class_add_property(&client_class, "group_window",
2283 NULL,
2284 (lua_class_propfunc_t) luaA_client_get_group_window,
2285 NULL);
2286 luaA_class_add_property(&client_class, "maximized",
2287 (lua_class_propfunc_t) luaA_client_set_maximized,
2288 (lua_class_propfunc_t) luaA_client_get_maximized,
2289 (lua_class_propfunc_t) luaA_client_set_maximized);
2290 luaA_class_add_property(&client_class, "maximized_horizontal",
2291 (lua_class_propfunc_t) luaA_client_set_maximized_horizontal,
2292 (lua_class_propfunc_t) luaA_client_get_maximized_horizontal,
2293 (lua_class_propfunc_t) luaA_client_set_maximized_horizontal);
2294 luaA_class_add_property(&client_class, "maximized_vertical",
2295 (lua_class_propfunc_t) luaA_client_set_maximized_vertical,
2296 (lua_class_propfunc_t) luaA_client_get_maximized_vertical,
2297 (lua_class_propfunc_t) luaA_client_set_maximized_vertical);
2298 luaA_class_add_property(&client_class, "icon",
2299 (lua_class_propfunc_t) luaA_client_set_icon,
2300 (lua_class_propfunc_t) luaA_client_get_icon,
2301 (lua_class_propfunc_t) luaA_client_set_icon);
2302 luaA_class_add_property(&client_class, "ontop",
2303 (lua_class_propfunc_t) luaA_client_set_ontop,
2304 (lua_class_propfunc_t) luaA_client_get_ontop,
2305 (lua_class_propfunc_t) luaA_client_set_ontop);
2306 luaA_class_add_property(&client_class, "above",
2307 (lua_class_propfunc_t) luaA_client_set_above,
2308 (lua_class_propfunc_t) luaA_client_get_above,
2309 (lua_class_propfunc_t) luaA_client_set_above);
2310 luaA_class_add_property(&client_class, "below",
2311 (lua_class_propfunc_t) luaA_client_set_below,
2312 (lua_class_propfunc_t) luaA_client_get_below,
2313 (lua_class_propfunc_t) luaA_client_set_below);
2314 luaA_class_add_property(&client_class, "sticky",
2315 (lua_class_propfunc_t) luaA_client_set_sticky,
2316 (lua_class_propfunc_t) luaA_client_get_sticky,
2317 (lua_class_propfunc_t) luaA_client_set_sticky);
2318 luaA_class_add_property(&client_class, "size_hints_honor",
2319 (lua_class_propfunc_t) luaA_client_set_size_hints_honor,
2320 (lua_class_propfunc_t) luaA_client_get_size_hints_honor,
2321 (lua_class_propfunc_t) luaA_client_set_size_hints_honor);
2322 luaA_class_add_property(&client_class, "urgent",
2323 (lua_class_propfunc_t) luaA_client_set_urgent,
2324 (lua_class_propfunc_t) luaA_client_get_urgent,
2325 (lua_class_propfunc_t) luaA_client_set_urgent);
2326 luaA_class_add_property(&client_class, "size_hints",
2327 NULL,
2328 (lua_class_propfunc_t) luaA_client_get_size_hints,
2329 NULL);
2330 luaA_class_add_property(&client_class, "focusable",
2331 NULL,
2332 (lua_class_propfunc_t) luaA_client_get_focusable,
2333 NULL);
2334 luaA_class_add_property(&client_class, "shape_bounding",
2335 (lua_class_propfunc_t) luaA_client_set_shape_bounding,
2336 (lua_class_propfunc_t) luaA_client_get_shape_bounding,
2337 (lua_class_propfunc_t) luaA_client_set_shape_bounding);
2338 luaA_class_add_property(&client_class, "shape_clip",
2339 (lua_class_propfunc_t) luaA_client_set_shape_clip,
2340 (lua_class_propfunc_t) luaA_client_get_shape_clip,
2341 (lua_class_propfunc_t) luaA_client_set_shape_clip);
2342 luaA_class_add_property(&client_class, "startup_id",
2343 NULL,
2344 (lua_class_propfunc_t) luaA_client_get_startup_id,
2345 NULL);
2346 luaA_class_add_property(&client_class, "client_shape_bounding",
2347 NULL,
2348 (lua_class_propfunc_t) luaA_client_get_client_shape_bounding,
2349 NULL);
2350 luaA_class_add_property(&client_class, "client_shape_clip",
2351 NULL,
2352 (lua_class_propfunc_t) luaA_client_get_client_shape_clip,
2353 NULL);
2355 signal_add(&client_class.signals, "focus");
2356 signal_add(&client_class.signals, "list");
2357 signal_add(&client_class.signals, "manage");
2358 signal_add(&client_class.signals, "button::press");
2359 signal_add(&client_class.signals, "button::release");
2360 signal_add(&client_class.signals, "mouse::enter");
2361 signal_add(&client_class.signals, "mouse::leave");
2362 signal_add(&client_class.signals, "mouse::move");
2363 signal_add(&client_class.signals, "property::above");
2364 signal_add(&client_class.signals, "property::below");
2365 signal_add(&client_class.signals, "property::class");
2366 signal_add(&client_class.signals, "property::fullscreen");
2367 signal_add(&client_class.signals, "property::geometry");
2368 signal_add(&client_class.signals, "property::group_window");
2369 signal_add(&client_class.signals, "property::height");
2370 signal_add(&client_class.signals, "property::hidden");
2371 signal_add(&client_class.signals, "property::icon");
2372 signal_add(&client_class.signals, "property::icon_name");
2373 signal_add(&client_class.signals, "property::instance");
2374 signal_add(&client_class.signals, "property::keys");
2375 signal_add(&client_class.signals, "property::machine");
2376 signal_add(&client_class.signals, "property::maximized");
2377 signal_add(&client_class.signals, "property::maximized_horizontal");
2378 signal_add(&client_class.signals, "property::maximized_vertical");
2379 signal_add(&client_class.signals, "property::minimized");
2380 signal_add(&client_class.signals, "property::modal");
2381 signal_add(&client_class.signals, "property::name");
2382 signal_add(&client_class.signals, "property::ontop");
2383 signal_add(&client_class.signals, "property::pid");
2384 signal_add(&client_class.signals, "property::role");
2385 signal_add(&client_class.signals, "property::screen");
2386 signal_add(&client_class.signals, "property::shape_bounding");
2387 signal_add(&client_class.signals, "property::shape_client_bounding");
2388 signal_add(&client_class.signals, "property::shape_client_clip");
2389 signal_add(&client_class.signals, "property::shape_clip");
2390 signal_add(&client_class.signals, "property::size_hints_honor");
2391 signal_add(&client_class.signals, "property::skip_taskbar");
2392 signal_add(&client_class.signals, "property::sticky");
2393 signal_add(&client_class.signals, "property::struts");
2394 signal_add(&client_class.signals, "property::transient_for");
2395 signal_add(&client_class.signals, "property::type");
2396 signal_add(&client_class.signals, "property::urgent");
2397 signal_add(&client_class.signals, "property::width");
2398 signal_add(&client_class.signals, "property::window");
2399 signal_add(&client_class.signals, "property::x");
2400 signal_add(&client_class.signals, "property::y");
2401 signal_add(&client_class.signals, "request::activate");
2402 signal_add(&client_class.signals, "request::fullscreen");
2403 signal_add(&client_class.signals, "request::maximized_horizontal");
2404 signal_add(&client_class.signals, "request::maximized_vertical");
2405 signal_add(&client_class.signals, "request::tag");
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