Don't pass cairo surfaces around on the lua stack
[awesome.git] / objects / client.c
blob1fbbbf1127ea3be49062298b886966b2c2fcc0b4
1 /*
2 * client.c - client management
4 * Copyright © 2007-2009 Julien Danjou <julien@danjou.info>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <xcb/xcb_atom.h>
23 #include <xcb/xcb_image.h>
25 #include "objects/tag.h"
26 #include "ewmh.h"
27 #include "screen.h"
28 #include "systray.h"
29 #include "property.h"
30 #include "spawn.h"
31 #include "luaa.h"
32 #include "xwindow.h"
33 #include "common/atoms.h"
34 #include "common/xutil.h"
36 /** Collect a client.
37 * \param L The Lua VM state.
38 * \return The number of element pushed on stack.
40 static void
41 client_wipe(client_t *c)
43 key_array_wipe(&c->keys);
44 xcb_icccm_get_wm_protocols_reply_wipe(&c->protocols);
45 p_delete(&c->machine);
46 p_delete(&c->class);
47 p_delete(&c->instance);
48 p_delete(&c->icon_name);
49 p_delete(&c->alt_icon_name);
50 p_delete(&c->name);
51 p_delete(&c->alt_name);
52 if(c->icon)
53 cairo_surface_destroy(c->icon);
56 /** Change the clients urgency flag.
57 * \param L The Lua VM state.
58 * \param cidx The client index on the stack.
59 * \param urgent The new flag state.
61 void
62 client_set_urgent(lua_State *L, int cidx, bool urgent)
64 client_t *c = luaA_checkudata(L, cidx, &client_class);
66 if(c->urgent != urgent)
68 xcb_get_property_cookie_t hints =
69 xcb_icccm_get_wm_hints_unchecked(globalconf.connection, c->window);
71 c->urgent = urgent;
73 /* update ICCCM hints */
74 xcb_icccm_wm_hints_t wmh;
75 xcb_icccm_get_wm_hints_reply(globalconf.connection, hints, &wmh, NULL);
77 if(urgent)
78 wmh.flags |= XCB_ICCCM_WM_HINT_X_URGENCY;
79 else
80 wmh.flags &= ~XCB_ICCCM_WM_HINT_X_URGENCY;
82 xcb_icccm_set_wm_hints(globalconf.connection, c->window, &wmh);
84 luaA_object_emit_signal(L, cidx, "property::urgent", 0);
88 #define DO_CLIENT_SET_PROPERTY(prop) \
89 void \
90 client_set_##prop(lua_State *L, int cidx, fieldtypeof(client_t, prop) value) \
91 { \
92 client_t *c = luaA_checkudata(L, cidx, &client_class); \
93 if(c->prop != value) \
94 { \
95 c->prop = value; \
96 luaA_object_emit_signal(L, cidx, "property::" #prop, 0); \
97 } \
99 DO_CLIENT_SET_PROPERTY(group_window)
100 DO_CLIENT_SET_PROPERTY(type)
101 DO_CLIENT_SET_PROPERTY(transient_for)
102 DO_CLIENT_SET_PROPERTY(pid)
103 DO_CLIENT_SET_PROPERTY(skip_taskbar)
104 #undef DO_CLIENT_SET_PROPERTY
106 #define DO_CLIENT_SET_STRING_PROPERTY2(prop, signal) \
107 void \
108 client_set_##prop(lua_State *L, int cidx, char *value) \
110 client_t *c = luaA_checkudata(L, cidx, &client_class); \
111 p_delete(&c->prop); \
112 c->prop = value; \
113 luaA_object_emit_signal(L, cidx, "property::" #signal, 0); \
115 #define DO_CLIENT_SET_STRING_PROPERTY(prop) \
116 DO_CLIENT_SET_STRING_PROPERTY2(prop, prop)
117 DO_CLIENT_SET_STRING_PROPERTY(name)
118 DO_CLIENT_SET_STRING_PROPERTY2(alt_name, name)
119 DO_CLIENT_SET_STRING_PROPERTY(icon_name)
120 DO_CLIENT_SET_STRING_PROPERTY2(alt_icon_name, icon_name)
121 DO_CLIENT_SET_STRING_PROPERTY(role)
122 DO_CLIENT_SET_STRING_PROPERTY(machine)
123 #undef DO_CLIENT_SET_STRING_PROPERTY
125 void
126 client_set_class_instance(lua_State *L, int cidx, const char *class, const char *instance)
128 client_t *c = luaA_checkudata(L, cidx, &client_class);
129 p_delete(&c->class);
130 p_delete(&c->instance);
131 c->class = a_strdup(class);
132 luaA_object_emit_signal(L, cidx, "property::class", 0);
133 c->instance = a_strdup(instance);
134 luaA_object_emit_signal(L, cidx, "property::instance", 0);
137 /** Returns true if a client is tagged
138 * with one of the tags of the specified screen.
139 * \param c The client to check.
140 * \param screen Virtual screen.
141 * \return true if the client is visible, false otherwise.
143 bool
144 client_maybevisible(client_t *c)
146 if(c->sticky)
147 return true;
149 foreach(tag, c->screen->tags)
150 if(tag_get_selected(*tag) && is_client_tagged(c, *tag))
151 return true;
153 return false;
156 /** Get a client by its window.
157 * \param w The client window to find.
158 * \return A client pointer if found, NULL otherwise.
160 client_t *
161 client_getbywin(xcb_window_t w)
163 foreach(c, globalconf.clients)
164 if((*c)->window == w)
165 return *c;
167 return NULL;
170 /** Get a client by its frame window.
171 * \param w The client window to find.
172 * \return A client pointer if found, NULL otherwise.
174 client_t *
175 client_getbyframewin(xcb_window_t w)
177 foreach(c, globalconf.clients)
178 if((*c)->frame_window == w)
179 return *c;
181 return NULL;
184 /** Unfocus a client.
185 * \param c The client.
187 static void
188 client_unfocus(client_t *c)
190 globalconf.focus.client = NULL;
191 globalconf.focus.need_update = true;
193 luaA_object_push(globalconf.L, c);
194 luaA_object_emit_signal(globalconf.L, -1, "unfocus", 0);
195 lua_pop(globalconf.L, 1);
198 /** Check if client supports atom a protocol in WM_PROTOCOL.
199 * \param c The client.
200 * \param atom The protocol atom to check for.
201 * \return True if client has the atom in protocol, false otherwise.
203 bool
204 client_hasproto(client_t *c, xcb_atom_t atom)
206 for(uint32_t i = 0; i < c->protocols.atoms_len; i++)
207 if(c->protocols.atoms[i] == atom)
208 return true;
209 return false;
212 /** Prepare banning a client by running all needed lua events.
213 * \param c The client.
215 void client_ban_unfocus(client_t *c)
217 /* Wait until the last moment to take away the focus from the window. */
218 if(globalconf.focus.client == c)
219 client_unfocus(c);
222 /** Ban client and move it out of the viewport.
223 * \param c The client.
225 void
226 client_ban(client_t *c)
228 if(!c->isbanned)
230 xcb_unmap_window(globalconf.connection, c->frame_window);
232 c->isbanned = true;
234 client_ban_unfocus(c);
238 /** This is part of The Bob Marley Algorithm: we ignore enter and leave window
239 * in certain cases, like map/unmap or move, so we don't get spurious events.
241 void
242 client_ignore_enterleave_events(void)
244 foreach(c, globalconf.clients)
246 xcb_change_window_attributes(globalconf.connection,
247 (*c)->window,
248 XCB_CW_EVENT_MASK,
249 (const uint32_t []) { CLIENT_SELECT_INPUT_EVENT_MASK & ~(XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_LEAVE_WINDOW) });
250 xcb_change_window_attributes(globalconf.connection,
251 (*c)->frame_window,
252 XCB_CW_EVENT_MASK,
253 (const uint32_t []) { FRAME_SELECT_INPUT_EVENT_MASK & ~(XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_LEAVE_WINDOW) });
257 void
258 client_restore_enterleave_events(void)
260 foreach(c, globalconf.clients)
262 xcb_change_window_attributes(globalconf.connection,
263 (*c)->window,
264 XCB_CW_EVENT_MASK,
265 (const uint32_t []) { CLIENT_SELECT_INPUT_EVENT_MASK });
266 xcb_change_window_attributes(globalconf.connection,
267 (*c)->frame_window,
268 XCB_CW_EVENT_MASK,
269 (const uint32_t []) { FRAME_SELECT_INPUT_EVENT_MASK });
273 /** Record that a client got focus.
274 * \param c The client.
276 void
277 client_focus_update(client_t *c)
279 if(!client_maybevisible(c))
280 return;
282 if(globalconf.focus.client)
284 if (globalconf.focus.client != c)
285 client_unfocus(globalconf.focus.client);
286 else
287 /* Already focused */
288 return;
291 globalconf.focus.client = c;
293 /* according to EWMH, we have to remove the urgent state from a client */
294 luaA_object_push(globalconf.L, c);
295 client_set_urgent(globalconf.L, -1, false);
297 luaA_object_emit_signal(globalconf.L, -1, "focus", 0);
298 lua_pop(globalconf.L, 1);
301 /** Give focus to client, or to first client if client is NULL.
302 * \param c The client.
304 void
305 client_focus(client_t *c)
307 /* We have to set focus on first client */
308 if(!c && globalconf.clients.len && !(c = globalconf.clients.tab[0]))
309 return;
311 if(!client_maybevisible(c))
312 return;
314 client_focus_update(c);
315 globalconf.focus.need_update = true;
318 void
319 client_focus_refresh(void)
321 client_t *c = globalconf.focus.client;
322 xcb_window_t win = globalconf.screen->root;
324 if(!globalconf.focus.need_update)
325 return;
326 globalconf.focus.need_update = false;
328 if(c)
330 /* Make sure this window is unbanned and e.g. not minimized */
331 client_unban(c);
332 /* Sets focus on window - using xcb_set_input_focus or WM_TAKE_FOCUS */
333 if(!c->nofocus)
334 win = c->window;
335 else
336 /* Focus the root window to make sure the previously focused client
337 * doesn't get any input in case WM_TAKE_FOCUS gets ignored.
339 win = globalconf.screen->root;
341 if(client_hasproto(c, WM_TAKE_FOCUS))
342 xwindow_takefocus(c->window);
345 /* If nothing has the focused or the currently focused client doesn't want
346 * us to focus it, this sets the focus to the root window. This makes sure
347 * the previously focused client actually gets unfocused. Alternatively, the
348 * new client gets the input focus.
350 xcb_set_input_focus(globalconf.connection, XCB_INPUT_FOCUS_PARENT,
351 win, globalconf.timestamp);
354 static void
355 client_update_properties(client_t *c)
357 /* get all hints */
358 xcb_get_property_cookie_t wm_normal_hints = property_get_wm_normal_hints(c);
359 xcb_get_property_cookie_t wm_hints = property_get_wm_hints(c);
360 xcb_get_property_cookie_t wm_transient_for = property_get_wm_transient_for(c);
361 xcb_get_property_cookie_t wm_client_leader = property_get_wm_client_leader(c);
362 xcb_get_property_cookie_t wm_client_machine = property_get_wm_client_machine(c);
363 xcb_get_property_cookie_t wm_window_role = property_get_wm_window_role(c);
364 xcb_get_property_cookie_t net_wm_pid = property_get_net_wm_pid(c);
365 xcb_get_property_cookie_t net_wm_icon = property_get_net_wm_icon(c);
366 xcb_get_property_cookie_t wm_name = property_get_wm_name(c);
367 xcb_get_property_cookie_t net_wm_name = property_get_net_wm_name(c);
368 xcb_get_property_cookie_t wm_icon_name = property_get_wm_icon_name(c);
369 xcb_get_property_cookie_t net_wm_icon_name = property_get_net_wm_icon_name(c);
370 xcb_get_property_cookie_t wm_class = property_get_wm_class(c);
371 xcb_get_property_cookie_t wm_protocols = property_get_wm_protocols(c);
372 xcb_get_property_cookie_t opacity = xwindow_get_opacity_unchecked(c->window);
374 /* update strut */
375 ewmh_process_client_strut(c);
377 /* Now process all replies */
378 property_update_wm_normal_hints(c, wm_normal_hints);
379 property_update_wm_hints(c, wm_hints);
380 property_update_wm_transient_for(c, wm_transient_for);
381 property_update_wm_client_leader(c, wm_client_leader);
382 property_update_wm_client_machine(c, wm_client_machine);
383 property_update_wm_window_role(c, wm_window_role);
384 property_update_net_wm_pid(c, net_wm_pid);
385 property_update_net_wm_icon(c, net_wm_icon);
386 property_update_wm_name(c, wm_name);
387 property_update_net_wm_name(c, net_wm_name);
388 property_update_wm_icon_name(c, wm_icon_name);
389 property_update_net_wm_icon_name(c, net_wm_icon_name);
390 property_update_wm_class(c, wm_class);
391 property_update_wm_protocols(c, wm_protocols);
392 window_set_opacity(globalconf.L, -1, xwindow_get_opacity_from_cookie(opacity));
395 /** Manage a new client.
396 * \param w The window.
397 * \param wgeom Window geometry.
398 * \param startup True if we are managing at startup time.
400 void
401 client_manage(xcb_window_t w, xcb_get_geometry_reply_t *wgeom, bool startup)
403 const uint32_t select_input_val[] = { CLIENT_SELECT_INPUT_EVENT_MASK };
405 if(systray_iskdedockapp(w))
407 systray_request_handle(w, NULL);
408 return;
411 /* If this is a new client that just has been launched, then request its
412 * startup id. */
413 xcb_get_property_cookie_t startup_id_q = { 0 };
414 if(!startup)
415 startup_id_q = xcb_get_property(globalconf.connection, false, w,
416 _NET_STARTUP_ID, XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
418 /* Make sure the window is automatically mapped if awesome exits or dies. */
419 xcb_change_save_set(globalconf.connection, XCB_SET_MODE_INSERT, w);
421 client_t *c = client_new(globalconf.L);
422 xcb_screen_t *s = globalconf.screen;
424 /* consider the window banned */
425 c->isbanned = true;
426 /* Store window */
427 c->window = w;
428 c->frame_window = xcb_generate_id(globalconf.connection);
429 xcb_create_window(globalconf.connection, globalconf.default_depth, c->frame_window, s->root,
430 wgeom->x, wgeom->y, wgeom->width, wgeom->height,
431 wgeom->border_width, XCB_COPY_FROM_PARENT, globalconf.visual->visual_id,
432 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL | XCB_CW_BIT_GRAVITY
433 | XCB_CW_WIN_GRAVITY | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK
434 | XCB_CW_COLORMAP,
435 (const uint32_t [])
437 globalconf.screen->black_pixel,
438 globalconf.screen->black_pixel,
439 XCB_GRAVITY_NORTH_WEST,
440 XCB_GRAVITY_NORTH_WEST,
442 FRAME_SELECT_INPUT_EVENT_MASK,
443 globalconf.default_cmap
446 if (startup)
448 /* The client is already mapped, thus we must be sure that we don't send
449 * ourselves an UnmapNotify due to the xcb_reparent_window().
451 * Grab the server to make sure we don't lose any events.
453 uint32_t no_event[] = { 0 };
454 xcb_grab_server(globalconf.connection);
456 xcb_change_window_attributes(globalconf.connection,
457 globalconf.screen->root,
458 XCB_CW_EVENT_MASK,
459 no_event);
462 xcb_reparent_window(globalconf.connection, w, c->frame_window, 0, 0);
463 xcb_map_window(globalconf.connection, w);
465 if (startup)
467 xcb_change_window_attributes(globalconf.connection,
468 globalconf.screen->root,
469 XCB_CW_EVENT_MASK,
470 ROOT_WINDOW_EVENT_MASK);
471 xcb_ungrab_server(globalconf.connection);
474 /* Do this now so that we don't get any events for the above
475 * (Else, reparent could cause an UnmapNotify) */
476 xcb_change_window_attributes(globalconf.connection, w, XCB_CW_EVENT_MASK, select_input_val);
478 luaA_object_emit_signal(globalconf.L, -1, "property::window", 0);
480 /* The frame window gets the border, not the real client window */
481 xcb_configure_window(globalconf.connection, w,
482 XCB_CONFIG_WINDOW_BORDER_WIDTH,
483 (uint32_t[]) { 0 });
485 /* Move this window to the bottom of the stack. Without this we would force
486 * other windows which will be above this one to redraw themselves because
487 * this window occludes them for a tiny moment. The next stack_refresh()
488 * will fix this up and move the window to its correct place. */
489 xcb_configure_window(globalconf.connection, c->frame_window,
490 XCB_CONFIG_WINDOW_STACK_MODE,
491 (uint32_t[]) { XCB_STACK_MODE_BELOW});
493 /* Duplicate client and push it in client list */
494 lua_pushvalue(globalconf.L, -1);
495 client_array_push(&globalconf.clients, luaA_object_ref(globalconf.L, -1));
497 /* Set the right screen */
498 screen_client_moveto(c, screen_getbycoord(wgeom->x, wgeom->y), false);
500 /* Store initial geometry and emits signals so we inform that geometry have
501 * been set. */
502 #define HANDLE_GEOM(attr) \
503 c->geometry.attr = wgeom->attr; \
504 luaA_object_emit_signal(globalconf.L, -1, "property::" #attr, 0);
505 HANDLE_GEOM(x)
506 HANDLE_GEOM(y)
507 HANDLE_GEOM(width)
508 HANDLE_GEOM(height)
509 #undef HANDLE_GEOM
511 luaA_object_emit_signal(globalconf.L, -1, "property::geometry", 0);
513 /* Set border width */
514 window_set_border_width(globalconf.L, -1, wgeom->border_width);
516 /* we honor size hints by default */
517 c->size_hints_honor = true;
518 luaA_object_emit_signal(globalconf.L, -1, "property::size_hints_honor", 0);
520 /* update all properties */
521 client_update_properties(c);
523 /* Then check clients hints */
524 ewmh_client_check_hints(c);
526 /* Push client in stack */
527 client_raise(c);
529 /* Always stay in NORMAL_STATE. Even though iconified seems more
530 * appropriate sometimes. The only possible loss is that clients not using
531 * visibility events may continue to process data (when banned).
532 * Without any exposes or other events the cost should be fairly limited though.
534 * Some clients may expect the window to be unmapped when STATE_ICONIFIED.
535 * Two conflicting parts of the ICCCM v2.0 (section 4.1.4):
537 * "Normal -> Iconic - The client should send a ClientMessage event as described later in this section."
538 * (note no explicit mention of unmapping, while Normal->Widthdrawn does mention that)
540 * "Once a client's window has left the Withdrawn state, the window will be mapped
541 * if it is in the Normal state and the window will be unmapped if it is in the Iconic state."
543 * At this stage it's just safer to keep it in normal state and avoid confusion.
545 xwindow_set_state(c->window, XCB_ICCCM_WM_STATE_NORMAL);
547 if(!startup)
549 /* Request our response */
550 xcb_get_property_reply_t *reply =
551 xcb_get_property_reply(globalconf.connection, startup_id_q, NULL);
552 /* Say spawn that a client has been started, with startup id as argument */
553 char *startup_id = xutil_get_text_property_from_reply(reply);
554 p_delete(&reply);
555 spawn_start_notify(c, startup_id);
556 p_delete(&startup_id);
559 luaA_class_emit_signal(globalconf.L, &client_class, "list", 0);
561 /* client is still on top of the stack; push startup value,
562 * and emit signals with one arg */
563 lua_pushboolean(globalconf.L, startup);
564 luaA_object_emit_signal(globalconf.L, -2, "manage", 1);
565 /* pop client */
566 lua_pop(globalconf.L, 1);
569 /** Resize client window.
570 * The sizes given as parameters are with borders!
571 * \param c Client to resize.
572 * \param geometry New window geometry.
573 * \param hints Use size hints.
574 * \return true if an actual resize occurred.
576 bool
577 client_resize(client_t *c, area_t geometry)
579 area_t area;
581 /* offscreen appearance fixes */
582 area = display_area_get();
584 if(geometry.x > area.width)
585 geometry.x = area.width - geometry.width;
586 if(geometry.y > area.height)
587 geometry.y = area.height - geometry.height;
588 if(geometry.x + geometry.width < 0)
589 geometry.x = 0;
590 if(geometry.y + geometry.height < 0)
591 geometry.y = 0;
593 if(geometry.width == 0 || geometry.height == 0)
594 return false;
596 if(c->geometry.x != geometry.x
597 || c->geometry.y != geometry.y
598 || c->geometry.width != geometry.width
599 || c->geometry.height != geometry.height)
601 bool send_notice = false;
602 screen_t *new_screen = screen_getbycoord(geometry.x, geometry.y);
604 if(c->geometry.width == geometry.width
605 && c->geometry.height == geometry.height)
606 send_notice = true;
608 /* Also store geometry including border */
609 area_t old_geometry = c->geometry;
610 c->geometry = geometry;
612 /* Ignore all spurious enter/leave notify events */
613 client_ignore_enterleave_events();
615 xcb_configure_window(globalconf.connection, c->window,
616 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
617 (uint32_t[]) { geometry.width, geometry.height });
618 xcb_configure_window(globalconf.connection, c->frame_window,
619 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
620 (uint32_t[]) { geometry.x, geometry.y, geometry.width, geometry.height });
622 if(send_notice)
623 /* We are moving without changing the size, see ICCCM 4.2.3 */
624 xwindow_configure(c->window, geometry, c->border_width);
626 client_restore_enterleave_events();
628 screen_client_moveto(c, new_screen, false);
630 luaA_object_push(globalconf.L, c);
631 luaA_object_emit_signal(globalconf.L, -1, "property::geometry", 0);
632 if (old_geometry.x != geometry.x)
633 luaA_object_emit_signal(globalconf.L, -1, "property::x", 0);
634 if (old_geometry.y != geometry.y)
635 luaA_object_emit_signal(globalconf.L, -1, "property::y", 0);
636 if (old_geometry.width != geometry.width)
637 luaA_object_emit_signal(globalconf.L, -1, "property::width", 0);
638 if (old_geometry.height != geometry.height)
639 luaA_object_emit_signal(globalconf.L, -1, "property::height", 0);
640 lua_pop(globalconf.L, 1);
642 return true;
645 return false;
648 /** Set a client minimized, or not.
649 * \param L The Lua VM state.
650 * \param cidx The client index.
651 * \param s Set or not the client minimized.
653 void
654 client_set_minimized(lua_State *L, int cidx, bool s)
656 client_t *c = luaA_checkudata(L, cidx, &client_class);
658 if(c->minimized != s)
660 c->minimized = s;
661 banning_need_update();
662 if(s)
663 xwindow_set_state(c->window, XCB_ICCCM_WM_STATE_ICONIC);
664 else
665 xwindow_set_state(c->window, XCB_ICCCM_WM_STATE_NORMAL);
666 if(strut_has_value(&c->strut))
667 screen_emit_signal(globalconf.L, c->screen, "property::workarea", 0);
668 luaA_object_emit_signal(L, cidx, "property::minimized", 0);
672 /** Set a client hidden, or not.
673 * \param L The Lua VM state.
674 * \param cidx The client index.
675 * \param s Set or not the client hidden.
677 static void
678 client_set_hidden(lua_State *L, int cidx, bool s)
680 client_t *c = luaA_checkudata(L, cidx, &client_class);
682 if(c->hidden != s)
684 c->hidden = s;
685 banning_need_update();
686 if(strut_has_value(&c->strut))
687 screen_emit_signal(globalconf.L, c->screen, "property::workarea", 0);
688 luaA_object_emit_signal(L, cidx, "property::hidden", 0);
692 /** Set a client sticky, or not.
693 * \param L The Lua VM state.
694 * \param cidx The client index.
695 * \param s Set or not the client sticky.
697 void
698 client_set_sticky(lua_State *L, int cidx, bool s)
700 client_t *c = luaA_checkudata(L, cidx, &client_class);
702 if(c->sticky != s)
704 c->sticky = s;
705 banning_need_update();
706 luaA_object_emit_signal(L, cidx, "property::sticky", 0);
710 /** Set a client fullscreen, or not.
711 * \param L The Lua VM state.
712 * \param cidx The client index.
713 * \param s Set or not the client fullscreen.
715 void
716 client_set_fullscreen(lua_State *L, int cidx, bool s)
718 client_t *c = luaA_checkudata(L, cidx, &client_class);
720 if(c->fullscreen != s)
722 /* become fullscreen! */
723 if(s)
725 /* remove any max state */
726 client_set_maximized_horizontal(L, cidx, false);
727 client_set_maximized_vertical(L, cidx, false);
728 /* You can only be part of one of the special layers. */
729 client_set_below(L, cidx, false);
730 client_set_above(L, cidx, false);
731 client_set_ontop(L, cidx, false);
733 int abs_cidx = luaA_absindex(L, cidx); \
734 lua_pushboolean(L, s);
735 c->fullscreen = s;
736 luaA_object_emit_signal(L, abs_cidx, "request::fullscreen", 1);
737 luaA_object_emit_signal(L, abs_cidx, "property::fullscreen", 0);
738 stack_windows();
742 /** Set a client horizontally|vertically maximized.
743 * \param L The Lua VM state.
744 * \param cidx The client index.
745 * \param s The maximized status.
747 #define DO_FUNCTION_CLIENT_MAXIMIZED(type) \
748 void \
749 client_set_maximized_##type(lua_State *L, int cidx, bool s) \
751 client_t *c = luaA_checkudata(L, cidx, &client_class); \
752 if(c->maximized_##type != s) \
754 int abs_cidx = luaA_absindex(L, cidx); \
755 if(s) \
756 client_set_fullscreen(L, abs_cidx, false); \
757 lua_pushboolean(L, s); \
758 c->maximized_##type = s; \
759 luaA_object_emit_signal(L, abs_cidx, "request::maximized_" #type, 1); \
760 luaA_object_emit_signal(L, abs_cidx, "property::maximized_" #type, 0); \
761 stack_windows(); \
764 DO_FUNCTION_CLIENT_MAXIMIZED(vertical)
765 DO_FUNCTION_CLIENT_MAXIMIZED(horizontal)
766 #undef DO_FUNCTION_CLIENT_MAXIMIZED
768 /** Set a client above, or not.
769 * \param L The Lua VM state.
770 * \param cidx The client index.
771 * \param s Set or not the client above.
773 void
774 client_set_above(lua_State *L, int cidx, bool s)
776 client_t *c = luaA_checkudata(L, cidx, &client_class);
778 if(c->above != s)
780 /* You can only be part of one of the special layers. */
781 if(s)
783 client_set_below(L, cidx, false);
784 client_set_ontop(L, cidx, false);
785 client_set_fullscreen(L, cidx, false);
787 c->above = s;
788 stack_windows();
789 luaA_object_emit_signal(L, cidx, "property::above", 0);
793 /** Set a client below, or not.
794 * \param L The Lua VM state.
795 * \param cidx The client index.
796 * \param s Set or not the client below.
798 void
799 client_set_below(lua_State *L, int cidx, bool s)
801 client_t *c = luaA_checkudata(L, cidx, &client_class);
803 if(c->below != s)
805 /* You can only be part of one of the special layers. */
806 if(s)
808 client_set_above(L, cidx, false);
809 client_set_ontop(L, cidx, false);
810 client_set_fullscreen(L, cidx, false);
812 c->below = s;
813 stack_windows();
814 luaA_object_emit_signal(L, cidx, "property::below", 0);
818 /** Set a client modal, or not.
819 * \param L The Lua VM state.
820 * \param cidx The client index.
821 * \param s Set or not the client modal attribute.
823 void
824 client_set_modal(lua_State *L, int cidx, bool s)
826 client_t *c = luaA_checkudata(L, cidx, &client_class);
828 if(c->modal != s)
830 c->modal = s;
831 stack_windows();
832 luaA_object_emit_signal(L, cidx, "property::modal", 0);
836 /** Set a client ontop, or not.
837 * \param L The Lua VM state.
838 * \param cidx The client index.
839 * \param s Set or not the client ontop attribute.
841 void
842 client_set_ontop(lua_State *L, int cidx, bool s)
844 client_t *c = luaA_checkudata(L, cidx, &client_class);
846 if(c->ontop != s)
848 /* You can only be part of one of the special layers. */
849 if(s)
851 client_set_above(L, cidx, false);
852 client_set_below(L, cidx, false);
853 client_set_fullscreen(L, cidx, false);
855 c->ontop = s;
856 stack_windows();
857 luaA_object_emit_signal(L, cidx, "property::ontop", 0);
861 /** Unban a client and move it back into the viewport.
862 * \param c The client.
864 void
865 client_unban(client_t *c)
867 if(c->isbanned)
869 xcb_map_window(globalconf.connection, c->frame_window);
871 c->isbanned = false;
873 /* An unbanned client shouldn't be minimized or hidden */
874 luaA_object_push(globalconf.L, c);
875 client_set_minimized(globalconf.L, -1, false);
876 client_set_hidden(globalconf.L, -1, false);
877 lua_pop(globalconf.L, 1);
881 /** Unmanage a client.
882 * \param c The client.
883 * \param window_valid Is the client's window still valid?
885 void
886 client_unmanage(client_t *c, bool window_valid)
888 tag_array_t *tags = &c->screen->tags;
890 /* Reset transient_for attributes of widows that maybe referring to us */
891 foreach(_tc, globalconf.clients)
893 client_t *tc = *_tc;
894 if(tc->transient_for == c)
895 tc->transient_for = NULL;
898 if(globalconf.focus.client == c)
899 client_unfocus(c);
901 /* remove client from global list and everywhere else */
902 foreach(elem, globalconf.clients)
903 if(*elem == c)
905 client_array_remove(&globalconf.clients, elem);
906 break;
908 stack_client_remove(c);
909 for(int i = 0; i < tags->len; i++)
910 untag_client(c, tags->tab[i]);
912 luaA_object_push(globalconf.L, c);
913 luaA_object_emit_signal(globalconf.L, -1, "unmanage", 0);
914 lua_pop(globalconf.L, 1);
916 luaA_class_emit_signal(globalconf.L, &client_class, "list", 0);
918 if(strut_has_value(&c->strut))
919 screen_emit_signal(globalconf.L, c->screen, "property::workarea", 0);
921 /* Clear our event mask so that we don't receive any events from now on,
922 * especially not for the following requests. */
923 if(window_valid)
924 xcb_change_window_attributes(globalconf.connection,
925 c->window,
926 XCB_CW_EVENT_MASK,
927 (const uint32_t []) { 0 });
928 xcb_change_window_attributes(globalconf.connection,
929 c->frame_window,
930 XCB_CW_EVENT_MASK,
931 (const uint32_t []) { 0 });
933 if(window_valid)
935 xcb_unmap_window(globalconf.connection, c->window);
936 xcb_reparent_window(globalconf.connection, c->window, globalconf.screen->root,
937 c->geometry.x, c->geometry.y);
939 xcb_destroy_window(globalconf.connection, c->frame_window);
941 if(window_valid)
943 /* Remove this window from the save set since this shouldn't be made visible
944 * after a restart anymore. */
945 xcb_change_save_set(globalconf.connection, XCB_SET_MODE_DELETE, c->window);
947 /* Do this last to avoid races with clients. According to ICCCM, clients
948 * arent allowed to re-use the window until after this. */
949 xwindow_set_state(c->window, XCB_ICCCM_WM_STATE_WITHDRAWN);
952 /* set client as invalid */
953 c->window = XCB_NONE;
955 luaA_object_unref(globalconf.L, c);
958 /** Kill a client via a WM_DELETE_WINDOW request or KillClient if not
959 * supported.
960 * \param c The client to kill.
962 void
963 client_kill(client_t *c)
965 if(client_hasproto(c, WM_DELETE_WINDOW))
967 xcb_client_message_event_t ev;
969 /* Initialize all of event's fields first */
970 p_clear(&ev, 1);
972 ev.response_type = XCB_CLIENT_MESSAGE;
973 ev.window = c->window;
974 ev.format = 32;
975 ev.data.data32[1] = globalconf.timestamp;
976 ev.type = WM_PROTOCOLS;
977 ev.data.data32[0] = WM_DELETE_WINDOW;
979 xcb_send_event(globalconf.connection, false, c->window,
980 XCB_EVENT_MASK_NO_EVENT, (char *) &ev);
982 else
983 xcb_kill_client(globalconf.connection, c->window);
986 /** Get all clients into a table.
987 * \param L The Lua VM state.
988 * \return The number of elements pushed on stack.
989 * \luastack
990 * \lparam An optional screen number.
991 * \lreturn A table with all clients.
993 static int
994 luaA_client_get(lua_State *L)
996 int i = 1, screen;
998 screen = luaL_optnumber(L, 1, 0) - 1;
1000 lua_newtable(L);
1002 if(screen == -1)
1003 foreach(c, globalconf.clients)
1005 luaA_object_push(L, *c);
1006 lua_rawseti(L, -2, i++);
1008 else
1010 luaA_checkscreen(screen);
1011 foreach(c, globalconf.clients)
1012 if((*c)->screen == &globalconf.screens.tab[screen])
1014 luaA_object_push(L, *c);
1015 lua_rawseti(L, -2, i++);
1019 return 1;
1022 /** Check if a client is visible on its screen.
1023 * \param L The Lua VM state.
1024 * \return The number of elements pushed on stack.
1025 * \luastack
1026 * \lvalue A client.
1027 * \lreturn A boolean value, true if the client is visible, false otherwise.
1029 static int
1030 luaA_client_isvisible(lua_State *L)
1032 client_t *c = luaA_checkudata(L, 1, &client_class);
1033 lua_pushboolean(L, client_isvisible(c));
1034 return 1;
1037 /** Set a client icon.
1038 * \param L The Lua VM state.
1039 * \param cidx The client index on the stack.
1040 * \param iidx The image index on the stack.
1042 void
1043 client_set_icon(client_t *c, cairo_surface_t *s)
1045 if (s)
1046 s = draw_dup_image_surface(s);
1047 if(c->icon)
1048 cairo_surface_destroy(c->icon);
1049 c->icon = s;
1051 luaA_object_push(globalconf.L, c);
1052 luaA_object_emit_signal(globalconf.L, -1, "property::icon", 0);
1053 lua_pop(globalconf.L, 1);
1056 /** Kill a client.
1057 * \param L The Lua VM state.
1059 * \luastack
1060 * \lvalue A client.
1062 static int
1063 luaA_client_kill(lua_State *L)
1065 client_t *c = luaA_checkudata(L, 1, &client_class);
1066 client_kill(c);
1067 return 0;
1070 /** Swap a client with another one.
1071 * \param L The Lua VM state.
1072 * \luastack
1073 * \lvalue A client.
1074 * \lparam A client to swap with.
1076 static int
1077 luaA_client_swap(lua_State *L)
1079 client_t *c = luaA_checkudata(L, 1, &client_class);
1080 client_t *swap = luaA_checkudata(L, 2, &client_class);
1082 if(c != swap)
1084 client_t **ref_c = NULL, **ref_swap = NULL;
1085 foreach(item, globalconf.clients)
1087 if(*item == c)
1088 ref_c = item;
1089 else if(*item == swap)
1090 ref_swap = item;
1091 if(ref_c && ref_swap)
1092 break;
1094 /* swap ! */
1095 *ref_c = swap;
1096 *ref_swap = c;
1098 luaA_class_emit_signal(globalconf.L, &client_class, "list", 0);
1101 return 0;
1104 /** Access or set the client tags.
1105 * \param L The Lua VM state.
1106 * \return The number of elements pushed on stack.
1107 * \lparam A table with tags to set, or none to get the current tags table.
1108 * \return The clients tag.
1110 static int
1111 luaA_client_tags(lua_State *L)
1113 client_t *c = luaA_checkudata(L, 1, &client_class);
1114 tag_array_t *tags = &c->screen->tags;
1115 int j = 0;
1117 if(lua_gettop(L) == 2)
1119 luaA_checktable(L, 2);
1120 for(int i = 0; i < tags->len; i++)
1122 /* Only untag if we aren't going to add this tag again */
1123 bool found = false;
1124 lua_pushnil(L);
1125 while(lua_next(L, 2))
1127 tag_t *t = lua_touserdata(L, -1);
1128 /* Pop the value from lua_next */
1129 lua_pop(L, 1);
1130 if (t != tags->tab[i])
1131 continue;
1133 /* Pop the key from lua_next */
1134 lua_pop(L, 1);
1135 found = true;
1136 break;
1138 if(!found)
1139 untag_client(c, tags->tab[i]);
1141 lua_pushnil(L);
1142 while(lua_next(L, 2))
1143 tag_client(c);
1144 lua_pop(L, 1);
1147 lua_newtable(L);
1148 foreach(tag, *tags)
1149 if(is_client_tagged(c, *tag))
1151 luaA_object_push(L, *tag);
1152 lua_rawseti(L, -2, ++j);
1155 return 1;
1158 /** Raise a client on top of others which are on the same layer.
1159 * \param L The Lua VM state.
1160 * \luastack
1161 * \lvalue A client.
1163 static int
1164 luaA_client_raise(lua_State *L)
1166 client_t *c = luaA_checkudata(L, 1, &client_class);
1167 client_raise(c);
1168 return 0;
1171 /** Lower a client on bottom of others which are on the same layer.
1172 * \param L The Lua VM state.
1173 * \luastack
1174 * \lvalue A client.
1176 static int
1177 luaA_client_lower(lua_State *L)
1179 client_t *c = luaA_checkudata(L, 1, &client_class);
1181 stack_client_push(c);
1183 /* Traverse all transient layers. */
1184 for(client_t *tc = c->transient_for; tc; tc = tc->transient_for)
1185 stack_client_push(tc);
1187 return 0;
1190 /** Stop managing a client.
1191 * \param L The Lua VM state.
1192 * \return The number of elements pushed on stack.
1193 * \luastack
1194 * \lvalue A client.
1196 static int
1197 luaA_client_unmanage(lua_State *L)
1199 client_t *c = luaA_checkudata(L, 1, &client_class);
1200 client_unmanage(c, true);
1201 return 0;
1204 /** Return client geometry.
1205 * \param L The Lua VM state.
1206 * \return The number of elements pushed on stack.
1207 * \luastack
1208 * \lparam A table with new coordinates, or none.
1209 * \lreturn A table with client coordinates.
1211 static int
1212 luaA_client_geometry(lua_State *L)
1214 client_t *c = luaA_checkudata(L, 1, &client_class);
1216 if(lua_gettop(L) == 2 && !lua_isnil(L, 2))
1218 area_t geometry;
1220 luaA_checktable(L, 2);
1221 geometry.x = luaA_getopt_number(L, 2, "x", c->geometry.x);
1222 geometry.y = luaA_getopt_number(L, 2, "y", c->geometry.y);
1223 if(client_isfixed(c))
1225 geometry.width = c->geometry.width;
1226 geometry.height = c->geometry.height;
1228 else
1230 geometry.width = luaA_getopt_number(L, 2, "width", c->geometry.width);
1231 geometry.height = luaA_getopt_number(L, 2, "height", c->geometry.height);
1234 client_resize(c, geometry);
1237 return luaA_pusharea(L, c->geometry);
1240 static int
1241 luaA_client_set_screen(lua_State *L, client_t *c)
1243 int screen = luaL_checknumber(L, -1) - 1;
1244 luaA_checkscreen(screen);
1245 screen_client_moveto(c, &globalconf.screens.tab[screen], true);
1247 return 0;
1250 static int
1251 luaA_client_set_hidden(lua_State *L, client_t *c)
1253 client_set_hidden(L, -3, luaA_checkboolean(L, -1));
1254 return 0;
1257 static int
1258 luaA_client_set_minimized(lua_State *L, client_t *c)
1260 client_set_minimized(L, -3, luaA_checkboolean(L, -1));
1261 return 0;
1264 static int
1265 luaA_client_set_fullscreen(lua_State *L, client_t *c)
1267 client_set_fullscreen(L, -3, luaA_checkboolean(L, -1));
1268 return 0;
1271 static int
1272 luaA_client_set_modal(lua_State *L, client_t *c)
1274 client_set_modal(L, -3, luaA_checkboolean(L, -1));
1275 return 0;
1278 static int
1279 luaA_client_set_maximized_horizontal(lua_State *L, client_t *c)
1281 client_set_maximized_horizontal(L, -3, luaA_checkboolean(L, -1));
1282 return 0;
1285 static int
1286 luaA_client_set_maximized_vertical(lua_State *L, client_t *c)
1288 client_set_maximized_vertical(L, -3, luaA_checkboolean(L, -1));
1289 return 0;
1292 static int
1293 luaA_client_set_icon(lua_State *L, client_t *c)
1295 cairo_surface_t *surf = NULL;
1296 if(!lua_isnil(L, -1))
1297 surf = (cairo_surface_t *)lua_touserdata(L, -1);
1298 client_set_icon(c, surf);
1299 return 0;
1302 static int
1303 luaA_client_set_sticky(lua_State *L, client_t *c)
1305 client_set_sticky(L, -3, luaA_checkboolean(L, -1));
1306 return 0;
1309 static int
1310 luaA_client_set_size_hints_honor(lua_State *L, client_t *c)
1312 c->size_hints_honor = luaA_checkboolean(L, -1);
1313 luaA_object_emit_signal(L, -3, "property::size_hints_honor", 0);
1314 return 0;
1317 static int
1318 luaA_client_set_ontop(lua_State *L, client_t *c)
1320 client_set_ontop(L, -3, luaA_checkboolean(L, -1));
1321 return 0;
1324 static int
1325 luaA_client_set_below(lua_State *L, client_t *c)
1327 client_set_below(L, -3, luaA_checkboolean(L, -1));
1328 return 0;
1331 static int
1332 luaA_client_set_above(lua_State *L, client_t *c)
1334 client_set_above(L, -3, luaA_checkboolean(L, -1));
1335 return 0;
1338 static int
1339 luaA_client_set_urgent(lua_State *L, client_t *c)
1341 client_set_urgent(L, -3, luaA_checkboolean(L, -1));
1342 return 0;
1345 static int
1346 luaA_client_set_skip_taskbar(lua_State *L, client_t *c)
1348 client_set_skip_taskbar(L, -3, luaA_checkboolean(L, -1));
1349 return 0;
1352 static int
1353 luaA_client_get_name(lua_State *L, client_t *c)
1355 lua_pushstring(L, c->name ? c->name : c->alt_name);
1356 return 1;
1359 static int
1360 luaA_client_get_icon_name(lua_State *L, client_t *c)
1362 lua_pushstring(L, c->icon_name ? c->icon_name : c->alt_icon_name);
1363 return 1;
1366 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, class, lua_pushstring)
1367 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, instance, lua_pushstring)
1368 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, machine, lua_pushstring)
1369 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, role, lua_pushstring)
1370 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, transient_for, luaA_object_push)
1371 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, skip_taskbar, lua_pushboolean)
1372 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, leader_window, lua_pushnumber)
1373 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, group_window, lua_pushnumber)
1374 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, pid, lua_pushnumber)
1375 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, hidden, lua_pushboolean)
1376 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, minimized, lua_pushboolean)
1377 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, fullscreen, lua_pushboolean)
1378 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, modal, lua_pushboolean)
1379 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, ontop, lua_pushboolean)
1380 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, urgent, lua_pushboolean)
1381 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, above, lua_pushboolean)
1382 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, below, lua_pushboolean)
1383 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, sticky, lua_pushboolean)
1384 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, size_hints_honor, lua_pushboolean)
1385 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, maximized_horizontal, lua_pushboolean)
1386 LUA_OBJECT_EXPORT_PROPERTY(client, client_t, maximized_vertical, lua_pushboolean)
1388 static int
1389 luaA_client_get_content(lua_State *L, client_t *c)
1391 xcb_image_t *ximage = xcb_image_get(globalconf.connection,
1392 c->window,
1393 0, 0,
1394 c->geometry.width,
1395 c->geometry.height,
1396 ~0, XCB_IMAGE_FORMAT_Z_PIXMAP);
1397 cairo_surface_t *surface = NULL;
1399 if(ximage)
1401 if(ximage->bpp >= 24)
1403 uint32_t *data = p_new(uint32_t, ximage->width * ximage->height);
1405 for(int y = 0; y < ximage->height; y++)
1406 for(int x = 0; x < ximage->width; x++)
1408 data[y * ximage->width + x] = xcb_image_get_pixel(ximage, x, y);
1409 data[y * ximage->width + x] |= 0xff000000; /* set alpha to 0xff */
1412 surface = draw_surface_from_data(ximage->width, ximage->height, data);
1413 p_delete(&data);
1415 xcb_image_destroy(ximage);
1418 if (!surface)
1419 return 0;
1421 /* lua has to make sure to free the ref or we have a leak */
1422 lua_pushlightuserdata(L, surface);
1423 return 1;
1426 static int
1427 luaA_client_get_screen(lua_State *L, client_t *c)
1429 if(!c->screen)
1430 return 0;
1431 lua_pushnumber(L, 1 + screen_array_indexof(&globalconf.screens, c->screen));
1432 return 1;
1435 static int
1436 luaA_client_get_icon(lua_State *L, client_t *c)
1438 if(!c->icon)
1439 return 0;
1440 /* lua gets its own reference which it will have to destroy */
1441 lua_pushlightuserdata(L, cairo_surface_reference(c->icon));
1442 return 1;
1445 static int
1446 luaA_client_get_focusable(lua_State *L, client_t *c)
1448 bool ret;
1450 /* A client can be focused if it doesnt have the "nofocus" hint...*/
1451 if (!c->nofocus)
1452 ret = true;
1453 else
1454 /* ...or if it knows the WM_TAKE_FOCUS protocol */
1455 ret = client_hasproto(c, WM_TAKE_FOCUS);
1457 lua_pushboolean(L, ret);
1458 return 1;
1461 static int
1462 luaA_client_get_size_hints(lua_State *L, client_t *c)
1464 const char *u_or_p = NULL;
1466 lua_createtable(L, 0, 1);
1468 if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_US_POSITION)
1469 u_or_p = "user_position";
1470 else if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_POSITION)
1471 u_or_p = "program_position";
1473 if(u_or_p)
1475 lua_createtable(L, 0, 2);
1476 lua_pushnumber(L, c->size_hints.x);
1477 lua_setfield(L, -2, "x");
1478 lua_pushnumber(L, c->size_hints.y);
1479 lua_setfield(L, -2, "y");
1480 lua_setfield(L, -2, u_or_p);
1481 u_or_p = NULL;
1484 if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_US_SIZE)
1485 u_or_p = "user_size";
1486 else if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_SIZE)
1487 u_or_p = "program_size";
1489 if(u_or_p)
1491 lua_createtable(L, 0, 2);
1492 lua_pushnumber(L, c->size_hints.width);
1493 lua_setfield(L, -2, "width");
1494 lua_pushnumber(L, c->size_hints.height);
1495 lua_setfield(L, -2, "height");
1496 lua_setfield(L, -2, u_or_p);
1499 if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE)
1501 lua_pushnumber(L, c->size_hints.min_width);
1502 lua_setfield(L, -2, "min_width");
1503 lua_pushnumber(L, c->size_hints.min_height);
1504 lua_setfield(L, -2, "min_height");
1507 if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE)
1509 lua_pushnumber(L, c->size_hints.max_width);
1510 lua_setfield(L, -2, "max_width");
1511 lua_pushnumber(L, c->size_hints.max_height);
1512 lua_setfield(L, -2, "max_height");
1515 if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC)
1517 lua_pushnumber(L, c->size_hints.width_inc);
1518 lua_setfield(L, -2, "width_inc");
1519 lua_pushnumber(L, c->size_hints.height_inc);
1520 lua_setfield(L, -2, "height_inc");
1523 if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_ASPECT)
1525 lua_pushnumber(L, c->size_hints.min_aspect_num);
1526 lua_setfield(L, -2, "min_aspect_num");
1527 lua_pushnumber(L, c->size_hints.min_aspect_den);
1528 lua_setfield(L, -2, "min_aspect_den");
1529 lua_pushnumber(L, c->size_hints.max_aspect_num);
1530 lua_setfield(L, -2, "max_aspect_num");
1531 lua_pushnumber(L, c->size_hints.max_aspect_den);
1532 lua_setfield(L, -2, "max_aspect_den");
1535 if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_BASE_SIZE)
1537 lua_pushnumber(L, c->size_hints.base_width);
1538 lua_setfield(L, -2, "base_width");
1539 lua_pushnumber(L, c->size_hints.base_height);
1540 lua_setfield(L, -2, "base_height");
1543 if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_WIN_GRAVITY)
1545 switch(c->size_hints.win_gravity)
1547 default:
1548 lua_pushliteral(L, "north_west");
1549 break;
1550 case XCB_GRAVITY_NORTH:
1551 lua_pushliteral(L, "north");
1552 break;
1553 case XCB_GRAVITY_NORTH_EAST:
1554 lua_pushliteral(L, "north_east");
1555 break;
1556 case XCB_GRAVITY_WEST:
1557 lua_pushliteral(L, "west");
1558 break;
1559 case XCB_GRAVITY_CENTER:
1560 lua_pushliteral(L, "center");
1561 break;
1562 case XCB_GRAVITY_EAST:
1563 lua_pushliteral(L, "east");
1564 break;
1565 case XCB_GRAVITY_SOUTH_WEST:
1566 lua_pushliteral(L, "south_west");
1567 break;
1568 case XCB_GRAVITY_SOUTH:
1569 lua_pushliteral(L, "south");
1570 break;
1571 case XCB_GRAVITY_SOUTH_EAST:
1572 lua_pushliteral(L, "south_east");
1573 break;
1574 case XCB_GRAVITY_STATIC:
1575 lua_pushliteral(L, "static");
1576 break;
1578 lua_setfield(L, -2, "win_gravity");
1581 return 1;
1584 /** Get or set keys bindings for a client.
1585 * \param L The Lua VM state.
1586 * \return The number of element pushed on stack.
1587 * \luastack
1588 * \lvalue A client.
1589 * \lparam An array of key bindings objects, or nothing.
1590 * \return The array of key bindings objects of this client.
1592 static int
1593 luaA_client_keys(lua_State *L)
1595 client_t *c = luaA_checkudata(L, 1, &client_class);
1596 key_array_t *keys = &c->keys;
1598 if(lua_gettop(L) == 2)
1600 luaA_key_array_set(L, 1, 2, keys);
1601 luaA_object_emit_signal(L, 1, "property::keys", 0);
1602 xcb_ungrab_key(globalconf.connection, XCB_GRAB_ANY, c->frame_window, XCB_BUTTON_MASK_ANY);
1603 xwindow_grabkeys(c->frame_window, keys);
1606 return luaA_key_array_get(L, 1, keys);
1609 /* Client module.
1610 * \param L The Lua VM state.
1611 * \return The number of pushed elements.
1613 static int
1614 luaA_client_module_index(lua_State *L)
1616 const char *buf = luaL_checkstring(L, 2);
1618 if (A_STREQ(buf, "focus"))
1619 return luaA_object_push(globalconf.L, globalconf.focus.client);
1620 return 0;
1623 /* Client module new index.
1624 * \param L The Lua VM state.
1625 * \return The number of pushed elements.
1627 static int
1628 luaA_client_module_newindex(lua_State *L)
1630 const char *buf = luaL_checkstring(L, 2);
1631 client_t *c;
1633 if (A_STREQ(buf, "focus"))
1635 c = luaA_checkudata(L, 3, &client_class);
1636 client_focus(c);
1639 return 0;
1642 static bool
1643 client_checker(client_t *c)
1645 return c->window != XCB_NONE;
1648 void
1649 client_class_setup(lua_State *L)
1651 static const struct luaL_reg client_methods[] =
1653 LUA_CLASS_METHODS(client)
1654 { "get", luaA_client_get },
1655 { "__index", luaA_client_module_index },
1656 { "__newindex", luaA_client_module_newindex },
1657 { NULL, NULL }
1660 static const struct luaL_reg client_meta[] =
1662 LUA_OBJECT_META(client)
1663 LUA_CLASS_META
1664 { "keys", luaA_client_keys },
1665 { "isvisible", luaA_client_isvisible },
1666 { "geometry", luaA_client_geometry },
1667 { "tags", luaA_client_tags },
1668 { "kill", luaA_client_kill },
1669 { "swap", luaA_client_swap },
1670 { "raise", luaA_client_raise },
1671 { "lower", luaA_client_lower },
1672 { "unmanage", luaA_client_unmanage },
1673 { NULL, NULL }
1676 luaA_class_setup(L, &client_class, "client", &window_class,
1677 (lua_class_allocator_t) client_new,
1678 (lua_class_collector_t) client_wipe,
1679 (lua_class_checker_t) client_checker,
1680 luaA_class_index_miss_property, luaA_class_newindex_miss_property,
1681 client_methods, client_meta);
1682 luaA_class_add_property(&client_class, "name",
1683 NULL,
1684 (lua_class_propfunc_t) luaA_client_get_name,
1685 NULL);
1686 luaA_class_add_property(&client_class, "transient_for",
1687 NULL,
1688 (lua_class_propfunc_t) luaA_client_get_transient_for,
1689 NULL);
1690 luaA_class_add_property(&client_class, "skip_taskbar",
1691 (lua_class_propfunc_t) luaA_client_set_skip_taskbar,
1692 (lua_class_propfunc_t) luaA_client_get_skip_taskbar,
1693 (lua_class_propfunc_t) luaA_client_set_skip_taskbar);
1694 luaA_class_add_property(&client_class, "content",
1695 NULL,
1696 (lua_class_propfunc_t) luaA_client_get_content,
1697 NULL);
1698 luaA_class_add_property(&client_class, "type",
1699 NULL,
1700 (lua_class_propfunc_t) luaA_window_get_type,
1701 NULL);
1702 luaA_class_add_property(&client_class, "class",
1703 NULL,
1704 (lua_class_propfunc_t) luaA_client_get_class,
1705 NULL);
1706 luaA_class_add_property(&client_class, "instance",
1707 NULL,
1708 (lua_class_propfunc_t) luaA_client_get_instance,
1709 NULL);
1710 luaA_class_add_property(&client_class, "role",
1711 NULL,
1712 (lua_class_propfunc_t) luaA_client_get_role,
1713 NULL);
1714 luaA_class_add_property(&client_class, "pid",
1715 NULL,
1716 (lua_class_propfunc_t) luaA_client_get_pid,
1717 NULL);
1718 luaA_class_add_property(&client_class, "leader_window",
1719 NULL,
1720 (lua_class_propfunc_t) luaA_client_get_leader_window,
1721 NULL);
1722 luaA_class_add_property(&client_class, "machine",
1723 NULL,
1724 (lua_class_propfunc_t) luaA_client_get_machine,
1725 NULL);
1726 luaA_class_add_property(&client_class, "icon_name",
1727 NULL,
1728 (lua_class_propfunc_t) luaA_client_get_icon_name,
1729 NULL);
1730 luaA_class_add_property(&client_class, "screen",
1731 NULL,
1732 (lua_class_propfunc_t) luaA_client_get_screen,
1733 (lua_class_propfunc_t) luaA_client_set_screen);
1734 luaA_class_add_property(&client_class, "hidden",
1735 (lua_class_propfunc_t) luaA_client_set_hidden,
1736 (lua_class_propfunc_t) luaA_client_get_hidden,
1737 (lua_class_propfunc_t) luaA_client_set_hidden);
1738 luaA_class_add_property(&client_class, "minimized",
1739 (lua_class_propfunc_t) luaA_client_set_minimized,
1740 (lua_class_propfunc_t) luaA_client_get_minimized,
1741 (lua_class_propfunc_t) luaA_client_set_minimized);
1742 luaA_class_add_property(&client_class, "fullscreen",
1743 (lua_class_propfunc_t) luaA_client_set_fullscreen,
1744 (lua_class_propfunc_t) luaA_client_get_fullscreen,
1745 (lua_class_propfunc_t) luaA_client_set_fullscreen);
1746 luaA_class_add_property(&client_class, "modal",
1747 (lua_class_propfunc_t) luaA_client_set_modal,
1748 (lua_class_propfunc_t) luaA_client_get_modal,
1749 (lua_class_propfunc_t) luaA_client_set_modal);
1750 luaA_class_add_property(&client_class, "group_window",
1751 NULL,
1752 (lua_class_propfunc_t) luaA_client_get_group_window,
1753 NULL);
1754 luaA_class_add_property(&client_class, "maximized_horizontal",
1755 (lua_class_propfunc_t) luaA_client_set_maximized_horizontal,
1756 (lua_class_propfunc_t) luaA_client_get_maximized_horizontal,
1757 (lua_class_propfunc_t) luaA_client_set_maximized_horizontal);
1758 luaA_class_add_property(&client_class, "maximized_vertical",
1759 (lua_class_propfunc_t) luaA_client_set_maximized_vertical,
1760 (lua_class_propfunc_t) luaA_client_get_maximized_vertical,
1761 (lua_class_propfunc_t) luaA_client_set_maximized_vertical);
1762 luaA_class_add_property(&client_class, "icon",
1763 (lua_class_propfunc_t) luaA_client_set_icon,
1764 (lua_class_propfunc_t) luaA_client_get_icon,
1765 (lua_class_propfunc_t) luaA_client_set_icon);
1766 luaA_class_add_property(&client_class, "ontop",
1767 (lua_class_propfunc_t) luaA_client_set_ontop,
1768 (lua_class_propfunc_t) luaA_client_get_ontop,
1769 (lua_class_propfunc_t) luaA_client_set_ontop);
1770 luaA_class_add_property(&client_class, "above",
1771 (lua_class_propfunc_t) luaA_client_set_above,
1772 (lua_class_propfunc_t) luaA_client_get_above,
1773 (lua_class_propfunc_t) luaA_client_set_above);
1774 luaA_class_add_property(&client_class, "below",
1775 (lua_class_propfunc_t) luaA_client_set_below,
1776 (lua_class_propfunc_t) luaA_client_get_below,
1777 (lua_class_propfunc_t) luaA_client_set_below);
1778 luaA_class_add_property(&client_class, "sticky",
1779 (lua_class_propfunc_t) luaA_client_set_sticky,
1780 (lua_class_propfunc_t) luaA_client_get_sticky,
1781 (lua_class_propfunc_t) luaA_client_set_sticky);
1782 luaA_class_add_property(&client_class, "size_hints_honor",
1783 (lua_class_propfunc_t) luaA_client_set_size_hints_honor,
1784 (lua_class_propfunc_t) luaA_client_get_size_hints_honor,
1785 (lua_class_propfunc_t) luaA_client_set_size_hints_honor);
1786 luaA_class_add_property(&client_class, "urgent",
1787 (lua_class_propfunc_t) luaA_client_set_urgent,
1788 (lua_class_propfunc_t) luaA_client_get_urgent,
1789 (lua_class_propfunc_t) luaA_client_set_urgent);
1790 luaA_class_add_property(&client_class, "size_hints",
1791 NULL,
1792 (lua_class_propfunc_t) luaA_client_get_size_hints,
1793 NULL);
1794 luaA_class_add_property(&client_class, "focusable",
1795 NULL,
1796 (lua_class_propfunc_t) luaA_client_get_focusable,
1797 NULL);
1799 signal_add(&client_class.signals, "focus");
1800 signal_add(&client_class.signals, "list");
1801 signal_add(&client_class.signals, "manage");
1802 signal_add(&client_class.signals, "mouse::enter");
1803 signal_add(&client_class.signals, "mouse::leave");
1804 signal_add(&client_class.signals, "property::above");
1805 signal_add(&client_class.signals, "property::below");
1806 signal_add(&client_class.signals, "property::class");
1807 signal_add(&client_class.signals, "property::fullscreen");
1808 signal_add(&client_class.signals, "property::geometry");
1809 signal_add(&client_class.signals, "property::group_window");
1810 signal_add(&client_class.signals, "property::height");
1811 signal_add(&client_class.signals, "property::hidden");
1812 signal_add(&client_class.signals, "property::icon");
1813 signal_add(&client_class.signals, "property::icon_name");
1814 signal_add(&client_class.signals, "property::instance");
1815 signal_add(&client_class.signals, "property::keys");
1816 signal_add(&client_class.signals, "property::machine");
1817 signal_add(&client_class.signals, "property::maximized_horizontal");
1818 signal_add(&client_class.signals, "property::maximized_vertical");
1819 signal_add(&client_class.signals, "property::minimized");
1820 signal_add(&client_class.signals, "property::modal");
1821 signal_add(&client_class.signals, "property::name");
1822 signal_add(&client_class.signals, "property::ontop");
1823 signal_add(&client_class.signals, "property::pid");
1824 signal_add(&client_class.signals, "property::role");
1825 signal_add(&client_class.signals, "property::screen");
1826 signal_add(&client_class.signals, "property::size_hints_honor");
1827 signal_add(&client_class.signals, "property::skip_taskbar");
1828 signal_add(&client_class.signals, "property::sticky");
1829 signal_add(&client_class.signals, "property::struts");
1830 signal_add(&client_class.signals, "property::transient_for");
1831 signal_add(&client_class.signals, "property::type");
1832 signal_add(&client_class.signals, "property::urgent");
1833 signal_add(&client_class.signals, "property::width");
1834 signal_add(&client_class.signals, "property::window");
1835 signal_add(&client_class.signals, "property::x");
1836 signal_add(&client_class.signals, "property::y");
1837 signal_add(&client_class.signals, "request::fullscreen");
1838 signal_add(&client_class.signals, "request::maximized_horizontal");
1839 signal_add(&client_class.signals, "request::maximized_vertical");
1840 signal_add(&client_class.signals, "tagged");
1841 signal_add(&client_class.signals, "unfocus");
1842 signal_add(&client_class.signals, "unmanage");
1843 signal_add(&client_class.signals, "untagged");
1846 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80