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