luaa: split dofunction()
[awesome.git] / client.c
blob8bd4e13dabd5df7c07b3eb0a68d1998b4e412c28
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 "tag.h"
26 #include "ewmh.h"
27 #include "screen.h"
28 #include "titlebar.h"
29 #include "systray.h"
30 #include "property.h"
31 #include "spawn.h"
32 #include "common/atoms.h"
33 #include "common/xutil.h"
35 DO_LUA_TOSTRING(client_t, client, "client")
37 client_t *
38 luaA_client_checkudata(lua_State *L, int ud)
40 client_t *c = luaL_checkudata(L, ud, "client");
41 if(c->invalid)
42 luaL_error(L, "client is invalid\n");
43 return c;
46 /** Collect a client.
47 * \param L The Lua VM state.
48 * \return The number of element pushed on stack.
50 static int
51 luaA_client_gc(lua_State *L)
53 client_t *c = luaL_checkudata(L, 1, "client");
54 luaA_ref_array_wipe(&c->refs);
55 button_array_wipe(&c->buttons);
56 image_unref(L, c->icon);
57 p_delete(&c->class);
58 p_delete(&c->startup_id);
59 p_delete(&c->instance);
60 p_delete(&c->icon_name);
61 p_delete(&c->name);
62 return 0;
65 /** Change the clients urgency flag.
66 * \param c The client
67 * \param urgent The new flag state
69 void
70 client_seturgent(client_t *c, bool urgent)
72 if(c->isurgent != urgent)
74 xcb_get_property_cookie_t hints =
75 xcb_get_wm_hints_unchecked(globalconf.connection, c->win);
77 c->isurgent = urgent;
78 ewmh_client_update_hints(c);
80 /* update ICCCM hints */
81 xcb_wm_hints_t wmh;
82 xcb_get_wm_hints_reply(globalconf.connection, hints, &wmh, NULL);
84 if(urgent)
85 wmh.flags |= XCB_WM_HINT_X_URGENCY;
86 else
87 wmh.flags &= ~XCB_WM_HINT_X_URGENCY;
89 xcb_set_wm_hints(globalconf.connection, c->win, &wmh);
91 hook_property(client, c, "urgent");
95 /** Returns true if a client is tagged
96 * with one of the tags of the specified screen.
97 * \param c The client to check.
98 * \param screen Virtual screen.
99 * \return true if the client is visible, false otherwise.
101 bool
102 client_maybevisible(client_t *c, screen_t *screen)
104 if(c->screen == screen)
106 if(c->issticky || c->type == WINDOW_TYPE_DESKTOP)
107 return true;
109 foreach(tag, screen->tags)
110 if((*tag)->selected && is_client_tagged(c, *tag))
111 return true;
113 return false;
116 /** Return the content of a client as an image.
117 * That's just take a screenshot.
118 * \param c The client.
119 * \return 1 if the image has been pushed on stack, false otherwise.
121 static int
122 client_getcontent(client_t *c)
124 xcb_image_t *ximage = xcb_image_get(globalconf.connection,
125 c->win,
126 0, 0,
127 c->geometries.internal.width,
128 c->geometries.internal.height,
129 ~0, XCB_IMAGE_FORMAT_Z_PIXMAP);
130 int retval = 0;
132 if(ximage)
134 if(ximage->bpp >= 24)
136 uint32_t *data = p_alloca(uint32_t, ximage->width * ximage->height);
138 for(int y = 0; y < ximage->height; y++)
139 for(int x = 0; x < ximage->width; x++)
141 data[y * ximage->width + x] = xcb_image_get_pixel(ximage, x, y);
142 data[y * ximage->width + x] |= 0xff000000; /* set alpha to 0xff */
145 retval = image_new_from_argb32(ximage->width, ximage->height, data);
147 xcb_image_destroy(ximage);
150 return retval;
153 /** Get a client by its window.
154 * \param w The client window to find.
155 * \return A client pointer if found, NULL otherwise.
157 client_t *
158 client_getbywin(xcb_window_t w)
160 foreach(c, globalconf.clients)
161 if((*c)->win == w)
162 return *c;
164 return NULL;
167 /** Record that a client lost focus.
168 * \param c Client being unfocused
170 void
171 client_unfocus_update(client_t *c)
173 globalconf.screens.tab[c->phys_screen].client_focus = NULL;
174 ewmh_update_net_active_window(c->phys_screen);
176 /* Call hook */
177 if(globalconf.hooks.unfocus != LUA_REFNIL)
179 client_push(globalconf.L, c);
180 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.unfocus, 1, 0);
185 /** Unfocus a client.
186 * \param c The client.
188 void
189 client_unfocus(client_t *c)
192 xcb_window_t root_win = xutil_screen_get(globalconf.connection, c->phys_screen)->root;
193 /* Set focus on root window, so no events leak to the current window. */
194 window_setfocus(root_win, true);
196 client_unfocus_update(c);
199 /** Ban client and move it out of the viewport.
200 * \param c The client.
202 void
203 client_ban(client_t *c)
205 if(!c->isbanned)
207 xcb_unmap_window(globalconf.connection, c->win);
209 c->isbanned = true;
211 if(globalconf.screens.tab[c->phys_screen].prev_client_focus == c)
212 globalconf.screens.tab[c->phys_screen].prev_client_focus = NULL;
214 /* Wait until the last moment to take away the focus from the window. */
215 if(globalconf.screens.tab[c->phys_screen].client_focus == c)
216 client_unfocus(c);
220 /** Record that a client got focus.
221 * \param c Client being focused.
223 void
224 client_focus_update(client_t *c)
226 if(!client_maybevisible(c, c->screen))
228 /* Focus previously focused client */
229 client_focus(globalconf.screen_focus->prev_client_focus);
230 return;
233 if(globalconf.screen_focus
234 && globalconf.screen_focus->client_focus)
236 if (globalconf.screen_focus->client_focus != c)
237 client_unfocus_update(globalconf.screen_focus->client_focus);
238 else
239 /* Already focused */
240 return;
242 /* stop hiding client */
243 c->ishidden = false;
244 client_setminimized(c, false);
246 /* unban the client before focusing for consistency */
247 client_unban(c);
249 globalconf.screen_focus = &globalconf.screens.tab[c->phys_screen];
250 globalconf.screen_focus->prev_client_focus = c;
251 globalconf.screen_focus->client_focus = c;
253 /* according to EWMH, we have to remove the urgent state from a client */
254 client_seturgent(c, false);
256 ewmh_update_net_active_window(c->phys_screen);
258 /* execute hook */
259 if(globalconf.hooks.focus != LUA_REFNIL)
261 client_push(globalconf.L, c);
262 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.focus, 1, 0);
267 /** Give focus to client, or to first client if client is NULL.
268 * \param c The client or NULL.
270 void
271 client_focus(client_t *c)
273 /* We have to set focus on first client */
274 if(!c && globalconf.clients.len && !(c = globalconf.clients.tab[0]))
275 return;
277 if(!client_maybevisible(c, c->screen))
278 return;
280 if (!c->nofocus)
281 client_focus_update(c);
283 window_setfocus(c->win, !c->nofocus);
286 /** Stack a window below.
287 * \param c The client.
288 * \param previous The previous window on the stack.
289 * \param return The next-previous!
291 static xcb_window_t
292 client_stack_above(client_t *c, xcb_window_t previous)
294 uint32_t config_win_vals[2];
296 config_win_vals[0] = previous;
297 config_win_vals[1] = XCB_STACK_MODE_ABOVE;
299 xcb_configure_window(globalconf.connection, c->win,
300 XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE,
301 config_win_vals);
303 config_win_vals[0] = c->win;
305 if(c->titlebar)
307 xcb_configure_window(globalconf.connection,
308 c->titlebar->sw.window,
309 XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE,
310 config_win_vals);
311 previous = c->titlebar->sw.window;
313 else
314 previous = c->win;
316 /* stack transient window on top of their parents */
317 foreach(node, globalconf.stack)
318 if((*node)->transient_for == c)
319 previous = client_stack_above(*node, previous);
321 return previous;
324 /** Stacking layout layers */
325 typedef enum
327 /** This one is a special layer */
328 LAYER_IGNORE,
329 LAYER_DESKTOP,
330 LAYER_BELOW,
331 LAYER_NORMAL,
332 LAYER_ABOVE,
333 LAYER_FULLSCREEN,
334 LAYER_ONTOP,
335 /** This one only used for counting and is not a real layer */
336 LAYER_COUNT
337 } layer_t;
339 /** Get the real layer of a client according to its attribute (fullscreen, …)
340 * \param c The client.
341 * \return The real layer.
343 static layer_t
344 client_layer_translator(client_t *c)
346 /* first deal with user set attributes */
347 if(c->isontop)
348 return LAYER_ONTOP;
349 else if(c->isfullscreen)
350 return LAYER_FULLSCREEN;
351 else if(c->isabove)
352 return LAYER_ABOVE;
353 else if(c->isbelow)
354 return LAYER_BELOW;
356 /* check for transient attr */
357 if(c->transient_for)
358 return LAYER_IGNORE;
360 /* then deal with windows type */
361 switch(c->type)
363 case WINDOW_TYPE_DESKTOP:
364 return LAYER_DESKTOP;
365 default:
366 break;
369 return LAYER_NORMAL;
372 /** Restack clients.
373 * \todo It might be worth stopping to restack everyone and only stack `c'
374 * relatively to the first matching in the list.
376 static void
377 client_real_stack(void)
379 uint32_t config_win_vals[2];
380 layer_t layer;
382 config_win_vals[0] = XCB_NONE;
383 config_win_vals[1] = XCB_STACK_MODE_ABOVE;
385 /* stack desktop windows */
386 for(layer = LAYER_DESKTOP; layer < LAYER_BELOW; layer++)
387 foreach(node, globalconf.stack)
388 if(client_layer_translator(*node) == layer)
389 config_win_vals[0] = client_stack_above(*node,
390 config_win_vals[0]);
392 /* first stack not ontop wibox window */
393 foreach(_sb, globalconf.wiboxes)
395 wibox_t *sb = *_sb;
396 if(!sb->ontop)
398 xcb_configure_window(globalconf.connection,
399 sb->sw.window,
400 XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE,
401 config_win_vals);
402 config_win_vals[0] = sb->sw.window;
406 /* then stack clients */
407 for(layer = LAYER_BELOW; layer < LAYER_COUNT; layer++)
408 foreach(node, globalconf.stack)
409 if(client_layer_translator(*node) == layer)
410 config_win_vals[0] = client_stack_above(*node,
411 config_win_vals[0]);
413 /* then stack ontop wibox window */
414 foreach(_sb, globalconf.wiboxes)
416 wibox_t *sb = *_sb;
417 if(sb->ontop)
419 xcb_configure_window(globalconf.connection,
420 sb->sw.window,
421 XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE,
422 config_win_vals);
423 config_win_vals[0] = sb->sw.window;
428 void
429 client_stack_refresh()
431 if (!globalconf.client_need_stack_refresh)
432 return;
433 globalconf.client_need_stack_refresh = false;
434 client_real_stack();
437 /** Manage a new client.
438 * \param w The window.
439 * \param wgeom Window geometry.
440 * \param phys_screen Physical screen number.
441 * \param startup True if we are managing at startup time.
443 void
444 client_manage(xcb_window_t w, xcb_get_geometry_reply_t *wgeom, int phys_screen, bool startup)
446 xcb_get_property_cookie_t ewmh_icon_cookie;
447 client_t *c, *tc = NULL;
448 screen_t *screen;
449 const uint32_t select_input_val[] = { CLIENT_SELECT_INPUT_EVENT_MASK };
451 if(systray_iskdedockapp(w))
453 systray_request_handle(w, phys_screen, NULL);
454 return;
457 /* Send request to get NET_WM_ICON property as soon as possible... */
458 ewmh_icon_cookie = ewmh_window_icon_get_unchecked(w);
459 xcb_change_window_attributes(globalconf.connection, w, XCB_CW_EVENT_MASK, select_input_val);
461 c = client_new(globalconf.L);
462 /* Push client in client list */
463 client_array_push(&globalconf.clients, client_ref(globalconf.L));
466 screen = c->screen = screen_getbycoord(&globalconf.screens.tab[phys_screen],
467 wgeom->x, wgeom->y);
469 c->phys_screen = phys_screen;
471 /* consider the window banned */
472 c->isbanned = true;
474 /* Initial values */
475 c->win = w;
476 c->geometry.x = wgeom->x;
477 c->geometry.y = wgeom->y;
478 /* Border will be added later. */
479 c->geometry.width = wgeom->width;
480 c->geometry.height = wgeom->height;
481 /* Also set internal geometry (client_ban() needs it). */
482 c->geometries.internal.x = wgeom->x;
483 c->geometries.internal.y = wgeom->y;
484 c->geometries.internal.width = wgeom->width;
485 c->geometries.internal.height = wgeom->height;
486 client_setborder(c, wgeom->border_width);
488 if(ewmh_window_icon_get_reply(ewmh_icon_cookie))
489 c->icon = image_ref(globalconf.L);
491 /* we honor size hints by default */
492 c->size_hints_honor = true;
494 /* update hints */
495 property_update_wm_normal_hints(c, NULL);
496 property_update_wm_hints(c, NULL);
497 property_update_wm_transient_for(c, NULL);
498 property_update_wm_client_leader(c, NULL);
500 /* Recursively find the parent. */
501 for(tc = c; tc->transient_for; tc = tc->transient_for);
502 if(tc != c && tc->phys_screen == c->phys_screen)
503 screen = tc->screen;
505 /* Then check clients hints */
506 ewmh_client_check_hints(c);
508 /* move client to screen, but do not tag it */
509 screen_client_moveto(c, screen, false, true);
511 /* Push client in stack */
512 client_raise(c);
514 /* update window title */
515 property_update_wm_name(c);
516 property_update_wm_icon_name(c);
517 property_update_wm_class(c, NULL);
519 xutil_text_prop_get(globalconf.connection, c->win, _NET_STARTUP_ID, &c->startup_id, NULL);
521 /* update strut */
522 ewmh_process_client_strut(c, NULL);
524 ewmh_update_net_client_list(c->phys_screen);
526 /* Always stay in NORMAL_STATE. Even though iconified seems more
527 * appropriate sometimes. The only possible loss is that clients not using
528 * visibility events may continue to proces data (when banned).
529 * Without any exposes or other events the cost should be fairly limited though.
531 * Some clients may expect the window to be unmapped when STATE_ICONIFIED.
532 * Two conflicting parts of the ICCCM v2.0 (section 4.1.4):
534 * "Normal -> Iconic - The client should send a ClientMessage event as described later in this section."
535 * (note no explicit mention of unmapping, while Normal->Widthdrawn does mention that)
537 * "Once a client's window has left the Withdrawn state, the window will be mapped
538 * if it is in the Normal state and the window will be unmapped if it is in the Iconic state."
540 * At this stage it's just safer to keep it in normal state and avoid confusion.
542 window_state_set(c->win, XCB_WM_STATE_NORMAL);
544 if(!startup)
545 spawn_start_notify(c);
547 /* Call hook to notify list change */
548 if(globalconf.hooks.clients != LUA_REFNIL)
549 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.clients, 0, 0);
551 /* call hook */
552 if(globalconf.hooks.manage != LUA_REFNIL)
554 client_push(globalconf.L, c);
555 lua_pushboolean(globalconf.L, startup);
556 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.manage, 2, 0);
560 /** Compute client geometry with respect to its geometry hints.
561 * \param c The client.
562 * \param geometry The geometry that the client might receive.
563 * \return The geometry the client must take respecting its hints.
565 area_t
566 client_geometry_hints(client_t *c, area_t geometry)
568 int32_t basew, baseh, minw, minh;
570 /* base size is substituted with min size if not specified */
571 if(c->size_hints.flags & XCB_SIZE_HINT_P_SIZE)
573 basew = c->size_hints.base_width;
574 baseh = c->size_hints.base_height;
576 else if(c->size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE)
578 basew = c->size_hints.min_width;
579 baseh = c->size_hints.min_height;
581 else
582 basew = baseh = 0;
584 /* min size is substituted with base size if not specified */
585 if(c->size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE)
587 minw = c->size_hints.min_width;
588 minh = c->size_hints.min_height;
590 else if(c->size_hints.flags & XCB_SIZE_HINT_P_SIZE)
592 minw = c->size_hints.base_width;
593 minh = c->size_hints.base_height;
595 else
596 minw = minh = 0;
598 if(c->size_hints.flags & XCB_SIZE_HINT_P_ASPECT
599 && c->size_hints.min_aspect_num > 0
600 && c->size_hints.min_aspect_den > 0
601 && geometry.height - baseh > 0
602 && geometry.width - basew > 0)
604 double dx = (double) (geometry.width - basew);
605 double dy = (double) (geometry.height - baseh);
606 double min = (double) c->size_hints.min_aspect_num / (double) c->size_hints.min_aspect_den;
607 double max = (double) c->size_hints.max_aspect_num / (double) c->size_hints.min_aspect_den;
608 double ratio = dx / dy;
609 if(max > 0 && min > 0 && ratio > 0)
611 if(ratio < min)
613 dy = (dx * min + dy) / (min * min + 1);
614 dx = dy * min;
615 geometry.width = (int) dx + basew;
616 geometry.height = (int) dy + baseh;
618 else if(ratio > max)
620 dy = (dx * min + dy) / (max * max + 1);
621 dx = dy * min;
622 geometry.width = (int) dx + basew;
623 geometry.height = (int) dy + baseh;
628 if(minw)
629 geometry.width = MAX(geometry.width, minw);
630 if(minh)
631 geometry.height = MAX(geometry.height, minh);
633 if(c->size_hints.flags & XCB_SIZE_HINT_P_MAX_SIZE)
635 if(c->size_hints.max_width)
636 geometry.width = MIN(geometry.width, c->size_hints.max_width);
637 if(c->size_hints.max_height)
638 geometry.height = MIN(geometry.height, c->size_hints.max_height);
641 if(c->size_hints.flags & (XCB_SIZE_HINT_P_RESIZE_INC | XCB_SIZE_HINT_BASE_SIZE)
642 && c->size_hints.width_inc && c->size_hints.height_inc)
644 uint16_t t1 = geometry.width, t2 = geometry.height;
645 unsigned_subtract(t1, basew);
646 unsigned_subtract(t2, baseh);
647 geometry.width -= t1 % c->size_hints.width_inc;
648 geometry.height -= t2 % c->size_hints.height_inc;
651 return geometry;
654 /** Resize client window.
655 * The sizse given as parameters are with titlebar and borders!
656 * \param c Client to resize.
657 * \param geometry New window geometry.
658 * \param hints Use size hints.
659 * \return true if an actual resize occurred.
661 bool
662 client_resize(client_t *c, area_t geometry, bool hints)
664 area_t geometry_internal;
665 area_t area;
667 /* offscreen appearance fixes */
668 area = display_area_get(c->phys_screen);
670 if(geometry.x > area.width)
671 geometry.x = area.width - geometry.width;
672 if(geometry.y > area.height)
673 geometry.y = area.height - geometry.height;
674 if(geometry.x + geometry.width < 0)
675 geometry.x = 0;
676 if(geometry.y + geometry.height < 0)
677 geometry.y = 0;
679 /* Real client geometry, please keep it contained to C code at the very least. */
680 geometry_internal = titlebar_geometry_remove(c->titlebar, c->border, geometry);
682 if(hints)
683 geometry_internal = client_geometry_hints(c, geometry_internal);
685 if(geometry_internal.width == 0 || geometry_internal.height == 0)
686 return false;
688 /* Also let client hints propegate to the "official" geometry. */
689 geometry = titlebar_geometry_add(c->titlebar, c->border, geometry_internal);
691 if(c->geometries.internal.x != geometry_internal.x
692 || c->geometries.internal.y != geometry_internal.y
693 || c->geometries.internal.width != geometry_internal.width
694 || c->geometries.internal.height != geometry_internal.height)
696 screen_t *new_screen = screen_getbycoord(c->screen,
697 geometry_internal.x, geometry_internal.y);
699 /* Values to configure a window is an array where values are
700 * stored according to 'value_mask' */
701 uint32_t values[4];
703 c->geometries.internal.x = values[0] = geometry_internal.x;
704 c->geometries.internal.y = values[1] = geometry_internal.y;
705 c->geometries.internal.width = values[2] = geometry_internal.width;
706 c->geometries.internal.height = values[3] = geometry_internal.height;
708 /* Also store geometry including border and titlebar. */
709 c->geometry = geometry;
711 titlebar_update_geometry(c);
713 xcb_configure_window(globalconf.connection, c->win,
714 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y
715 | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
716 values);
718 screen_client_moveto(c, new_screen, true, false);
720 /* execute hook */
721 hook_property(client, c, "geometry");
723 return true;
726 return false;
729 /** Set a client minimized, or not.
730 * \param c The client.
731 * \param s Set or not the client minimized.
733 void
734 client_setminimized(client_t *c, bool s)
736 if(c->isminimized != s)
738 client_need_reban(c);
739 c->isminimized = s;
740 client_need_reban(c);
741 if(s)
742 window_state_set(c->win, XCB_WM_STATE_ICONIC);
743 else
744 window_state_set(c->win, XCB_WM_STATE_NORMAL);
745 ewmh_client_update_hints(c);
746 /* execute hook */
747 hook_property(client, c, "minimized");
751 /** Set a client sticky, or not.
752 * \param c The client.
753 * \param s Set or not the client sticky.
755 void
756 client_setsticky(client_t *c, bool s)
758 if(c->issticky != s)
760 client_need_reban(c);
761 c->issticky = s;
762 client_need_reban(c);
763 ewmh_client_update_hints(c);
764 hook_property(client, c, "sticky");
768 /** Set a client fullscreen, or not.
769 * \param c The client.
770 * \param s Set or not the client fullscreen.
772 void
773 client_setfullscreen(client_t *c, bool s)
775 if(c->isfullscreen != s)
777 area_t geometry;
779 /* Make sure the current geometry is stored without titlebar. */
780 if(s)
781 titlebar_ban(c->titlebar);
783 /* become fullscreen! */
784 if((c->isfullscreen = s))
786 /* remove any max state */
787 client_setmaxhoriz(c, false);
788 client_setmaxvert(c, false);
789 /* You can only be part of one of the special layers. */
790 client_setbelow(c, false);
791 client_setabove(c, false);
792 client_setontop(c, false);
794 geometry = screen_area_get(c->screen, false);
795 c->geometries.fullscreen = c->geometry;
796 c->border_fs = c->border;
797 client_setborder(c, 0);
799 else
801 geometry = c->geometries.fullscreen;
802 client_setborder(c, c->border_fs);
804 client_resize(c, geometry, false);
805 client_stack();
806 ewmh_client_update_hints(c);
807 hook_property(client, c, "fullscreen");
811 /** Set a client horizontally maximized.
812 * \param c The client.
813 * \param s The maximized status.
815 void
816 client_setmaxhoriz(client_t *c, bool s)
818 if(c->ismaxhoriz != s)
820 area_t geometry;
822 if((c->ismaxhoriz = s))
824 /* remove fullscreen mode */
825 client_setfullscreen(c, false);
827 geometry = screen_area_get(c->screen, true);
828 geometry.y = c->geometry.y;
829 geometry.height = c->geometry.height;
830 c->geometries.max.x = c->geometry.x;
831 c->geometries.max.width = c->geometry.width;
833 else
835 geometry = c->geometry;
836 geometry.x = c->geometries.max.x;
837 geometry.width = c->geometries.max.width;
840 client_resize(c, geometry, c->size_hints_honor);
841 client_stack();
842 ewmh_client_update_hints(c);
843 hook_property(client, c, "maximized_horizontal");
847 /** Set a client vertically maximized.
848 * \param c The client.
849 * \param s The maximized status.
851 void
852 client_setmaxvert(client_t *c, bool s)
854 if(c->ismaxvert != s)
856 area_t geometry;
858 if((c->ismaxvert = s))
860 /* remove fullscreen mode */
861 client_setfullscreen(c, false);
863 geometry = screen_area_get(c->screen, true);
864 geometry.x = c->geometry.x;
865 geometry.width = c->geometry.width;
866 c->geometries.max.y = c->geometry.y;
867 c->geometries.max.height = c->geometry.height;
869 else
871 geometry = c->geometry;
872 geometry.y = c->geometries.max.y;
873 geometry.height = c->geometries.max.height;
876 client_resize(c, geometry, c->size_hints_honor);
877 client_stack();
878 ewmh_client_update_hints(c);
879 hook_property(client, c, "maximized_vertical");
883 /** Set a client above, or not.
884 * \param c The client.
885 * \param s Set or not the client above.
887 void
888 client_setabove(client_t *c, bool s)
890 if(c->isabove != s)
892 /* You can only be part of one of the special layers. */
893 if(s)
895 client_setbelow(c, false);
896 client_setontop(c, false);
897 client_setfullscreen(c, false);
899 c->isabove = s;
900 client_stack();
901 ewmh_client_update_hints(c);
902 /* execute hook */
903 hook_property(client, c, "above");
907 /** Set a client below, or not.
908 * \param c The client.
909 * \param s Set or not the client below.
911 void
912 client_setbelow(client_t *c, bool s)
914 if(c->isbelow != s)
916 /* You can only be part of one of the special layers. */
917 if(s)
919 client_setabove(c, false);
920 client_setontop(c, false);
921 client_setfullscreen(c, false);
923 c->isbelow = s;
924 client_stack();
925 ewmh_client_update_hints(c);
926 /* execute hook */
927 hook_property(client, c, "below");
931 /** Set a client modal, or not.
932 * \param c The client.
933 * \param s Set or not the client moda.
935 void
936 client_setmodal(client_t *c, bool s)
938 if(c->ismodal != s)
940 c->ismodal = s;
941 client_stack();
942 ewmh_client_update_hints(c);
943 /* execute hook */
944 hook_property(client, c, "modal");
948 /** Set a client ontop, or not.
949 * \param c The client.
950 * \param s Set or not the client moda.
952 void
953 client_setontop(client_t *c, bool s)
955 if(c->isontop != s)
957 /* You can only be part of one of the special layers. */
958 if(s)
960 client_setabove(c, false);
961 client_setbelow(c, false);
962 client_setfullscreen(c, false);
964 c->isontop = s;
965 client_stack();
966 /* execute hook */
967 hook_property(client, c, "ontop");
971 /** Unban a client and move it back into the viewport.
972 * \param c The client.
974 void
975 client_unban(client_t *c)
977 if(c->isbanned)
979 xcb_map_window(globalconf.connection, c->win);
981 c->isbanned = false;
985 /** Unmanage a client.
986 * \param c The client.
988 void
989 client_unmanage(client_t *c)
991 tag_array_t *tags = &c->screen->tags;
993 /* Reset transient_for attributes of widows that maybe refering to us */
994 foreach(_tc, globalconf.clients)
996 client_t *tc = *_tc;
997 if(tc->transient_for == c)
998 tc->transient_for = NULL;
1001 if(globalconf.screens.tab[c->phys_screen].prev_client_focus == c)
1002 globalconf.screens.tab[c->phys_screen].prev_client_focus = NULL;
1004 if(globalconf.screens.tab[c->phys_screen].client_focus == c)
1005 client_unfocus(c);
1007 /* remove client from global list and everywhere else */
1008 foreach(elem, globalconf.clients)
1009 if(*elem == c)
1011 client_array_remove(&globalconf.clients, elem);
1012 break;
1014 stack_client_remove(c);
1015 for(int i = 0; i < tags->len; i++)
1016 untag_client(c, tags->tab[i]);
1018 /* call hook */
1019 if(globalconf.hooks.unmanage != LUA_REFNIL)
1021 client_push(globalconf.L, c);
1022 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.unmanage, 1, 0);
1025 /* Call hook to notify list change */
1026 if(globalconf.hooks.clients != LUA_REFNIL)
1027 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.clients, 0, 0);
1029 /* The server grab construct avoids race conditions. */
1030 xcb_grab_server(globalconf.connection);
1032 xcb_ungrab_button(globalconf.connection, XCB_BUTTON_INDEX_ANY, c->win,
1033 XCB_BUTTON_MASK_ANY);
1034 window_state_set(c->win, XCB_WM_STATE_WITHDRAWN);
1036 xcb_flush(globalconf.connection);
1037 xcb_ungrab_server(globalconf.connection);
1039 titlebar_client_detach(c);
1041 ewmh_update_net_client_list(c->phys_screen);
1043 /* set client as invalid */
1044 c->invalid = true;
1046 client_unref(globalconf.L, c);
1049 /** Kill a client via a WM_DELETE_WINDOW request or KillClient if not
1050 * supported.
1051 * \param c The client to kill.
1053 void
1054 client_kill(client_t *c)
1056 if(window_hasproto(c->win, WM_DELETE_WINDOW))
1058 xcb_client_message_event_t ev;
1060 /* Initialize all of event's fields first */
1061 p_clear(&ev, 1);
1063 ev.response_type = XCB_CLIENT_MESSAGE;
1064 ev.window = c->win;
1065 ev.format = 32;
1066 ev.data.data32[1] = XCB_CURRENT_TIME;
1067 ev.type = WM_PROTOCOLS;
1068 ev.data.data32[0] = WM_DELETE_WINDOW;
1070 xcb_send_event(globalconf.connection, false, c->win,
1071 XCB_EVENT_MASK_NO_EVENT, (char *) &ev);
1073 else
1074 xcb_kill_client(globalconf.connection, c->win);
1077 /** Get all clients into a table.
1078 * \param L The Lua VM state.
1079 * \return The number of elements pushed on stack.
1080 * \luastack
1081 * \lparam An optional screen nunmber.
1082 * \lreturn A table with all clients.
1084 static int
1085 luaA_client_get(lua_State *L)
1087 int i = 1, screen;
1089 screen = luaL_optnumber(L, 1, 0) - 1;
1091 lua_newtable(L);
1093 if(screen == -1)
1094 foreach(c, globalconf.clients)
1096 client_push(L, *c);
1097 lua_rawseti(L, -2, i++);
1099 else
1101 luaA_checkscreen(screen);
1102 foreach(c, globalconf.clients)
1103 if((*c)->screen == &globalconf.screens.tab[screen])
1105 client_push(L, *c);
1106 lua_rawseti(L, -2, i++);
1110 return 1;
1113 /** Check if a client is visible on its screen.
1114 * \param L The Lua VM state.
1115 * \return The number of elements pushed on stack.
1116 * \luastack
1117 * \lvalue A client.
1118 * \lreturn A boolean value, true if the client is visible, false otherwise.
1120 static int
1121 luaA_client_isvisible(lua_State *L)
1123 client_t *c = luaA_client_checkudata(L, 1);
1124 lua_pushboolean(L, client_isvisible(c, c->screen));
1125 return 1;
1128 /** Set client border width.
1129 * \param c The client.
1130 * \param width The border width.
1132 void
1133 client_setborder(client_t *c, int width)
1135 uint32_t w = width;
1137 if(width > 0 && (c->type == WINDOW_TYPE_DOCK
1138 || c->type == WINDOW_TYPE_SPLASH
1139 || c->type == WINDOW_TYPE_DESKTOP
1140 || c->isfullscreen))
1141 return;
1143 if(width == c->border || width < 0)
1144 return;
1146 /* Update geometry with the new border. */
1147 c->geometry.width -= 2 * c->border;
1148 c->geometry.height -= 2 * c->border;
1150 c->border = width;
1151 xcb_configure_window(globalconf.connection, c->win,
1152 XCB_CONFIG_WINDOW_BORDER_WIDTH, &w);
1154 c->geometry.width += 2 * c->border;
1155 c->geometry.height += 2 * c->border;
1157 /* Changing border size also affects the size of the titlebar. */
1158 titlebar_update_geometry(c);
1160 hook_property(client, c, "border_width");
1163 /** Kill a client.
1164 * \param L The Lua VM state.
1166 * \luastack
1167 * \lvalue A client.
1169 static int
1170 luaA_client_kill(lua_State *L)
1172 client_t *c = luaA_client_checkudata(L, 1);
1173 client_kill(c);
1174 return 0;
1177 /** Swap a client with another one.
1178 * \param L The Lua VM state.
1179 * \luastack
1180 * \lvalue A client.
1181 * \lparam A client to swap with.
1183 static int
1184 luaA_client_swap(lua_State *L)
1186 client_t *c = luaA_client_checkudata(L, 1);
1187 client_t *swap = luaA_client_checkudata(L, 2);
1189 if(c != swap)
1191 client_t **ref_c = NULL, **ref_swap = NULL;
1192 foreach(item, globalconf.clients)
1194 if(*item == c)
1195 ref_c = item;
1196 else if(*item == swap)
1197 ref_swap = item;
1198 if(ref_c && ref_swap)
1199 break;
1201 /* swap ! */
1202 *ref_c = swap;
1203 *ref_swap = c;
1205 /* Call hook to notify list change */
1206 if(globalconf.hooks.clients != LUA_REFNIL)
1207 luaA_dofunction_from_registry(L, globalconf.hooks.clients, 0, 0);
1210 return 0;
1213 /** Access or set the client tags.
1214 * \param L The Lua VM state.
1215 * \return The number of elements pushed on stack.
1216 * \lparam A table with tags to set, or none to get the current tags table.
1217 * \return The clients tag.
1219 static int
1220 luaA_client_tags(lua_State *L)
1222 client_t *c = luaA_client_checkudata(L, 1);
1223 tag_array_t *tags = &c->screen->tags;
1224 int j = 0;
1226 if(lua_gettop(L) == 2)
1228 luaA_checktable(L, 2);
1229 for(int i = 0; i < tags->len; i++)
1230 untag_client(c, tags->tab[i]);
1231 lua_pushnil(L);
1232 while(lua_next(L, 2))
1233 tag_client(c);
1234 lua_pop(L, 1);
1237 lua_newtable(L);
1238 foreach(tag, *tags)
1239 if(is_client_tagged(c, *tag))
1241 tag_push(L, *tag);
1242 lua_rawseti(L, -2, ++j);
1245 return 1;
1248 /** Raise a client on top of others which are on the same layer.
1249 * \param L The Lua VM state.
1250 * \luastack
1251 * \lvalue A client.
1253 static int
1254 luaA_client_raise(lua_State *L)
1256 client_t *c = luaA_client_checkudata(L, 1);
1257 client_raise(c);
1258 return 0;
1261 /** Lower a client on bottom of others which are on the same layer.
1262 * \param L The Lua VM state.
1263 * \luastack
1264 * \lvalue A client.
1266 static int
1267 luaA_client_lower(lua_State *L)
1269 client_t *c = luaA_client_checkudata(L, 1);
1270 client_lower(c);
1271 return 0;
1274 /** Redraw a client by unmapping and mapping it quickly.
1275 * \param L The Lua VM state.
1277 * \luastack
1278 * \lvalue A client.
1280 static int
1281 luaA_client_redraw(lua_State *L)
1283 client_t *c = luaA_client_checkudata(L, 1);
1285 xcb_unmap_window(globalconf.connection, c->win);
1286 xcb_map_window(globalconf.connection, c->win);
1288 /* Set the focus on the current window if the redraw has been
1289 performed on the window where the pointer is currently on
1290 because after the unmapping/mapping, the focus is lost */
1291 if(globalconf.screen_focus->client_focus == c)
1293 client_unfocus(c);
1294 client_focus(c);
1297 return 0;
1300 /** Stop managing a client.
1301 * \param L The Lua VM state.
1302 * \return The number of elements pushed on stack.
1303 * \luastack
1304 * \lvalue A client.
1306 static int
1307 luaA_client_unmanage(lua_State *L)
1309 client_t *c = luaA_client_checkudata(L, 1);
1310 client_unmanage(c);
1311 return 0;
1314 /** Return client geometry.
1315 * \param L The Lua VM state.
1316 * \return The number of elements pushed on stack.
1317 * \luastack
1318 * \lparam A table with new coordinates, or none.
1319 * \lreturn A table with client coordinates.
1321 static int
1322 luaA_client_geometry(lua_State *L)
1324 client_t *c = luaA_client_checkudata(L, 1);
1326 if(lua_gettop(L) == 2)
1328 area_t geometry;
1330 luaA_checktable(L, 2);
1331 geometry.x = luaA_getopt_number(L, 2, "x", c->geometry.x);
1332 geometry.y = luaA_getopt_number(L, 2, "y", c->geometry.y);
1333 if(client_isfixed(c))
1335 geometry.width = c->geometry.width;
1336 geometry.height = c->geometry.height;
1338 else
1340 geometry.width = luaA_getopt_number(L, 2, "width", c->geometry.width);
1341 geometry.height = luaA_getopt_number(L, 2, "height", c->geometry.height);
1344 client_resize(c, geometry, c->size_hints_honor);
1347 return luaA_pusharea(L, c->geometry);
1350 /** Push a strut type to a table on stack.
1351 * \param L The Lua VM state.
1352 * \param struts The struts to push.
1353 * \return The number of elements pushed on stack.
1355 static inline int
1356 luaA_pushstruts(lua_State *L, strut_t struts)
1358 lua_createtable(L, 4, 0);
1359 lua_pushnumber(L, struts.left);
1360 lua_setfield(L, -2, "left");
1361 lua_pushnumber(L, struts.right);
1362 lua_setfield(L, -2, "right");
1363 lua_pushnumber(L, struts.top);
1364 lua_setfield(L, -2, "top");
1365 lua_pushnumber(L, struts.bottom);
1366 lua_setfield(L, -2, "bottom");
1367 return 1;
1370 /** Return client struts (reserved space at the edge of the screen).
1371 * \param L The Lua VM state.
1372 * \return The number of elements pushed on stack.
1373 * \luastack
1374 * \lparam A table with new strut values, or none.
1375 * \lreturn A table with strut values.
1377 static int
1378 luaA_client_struts(lua_State *L)
1380 client_t *c = luaA_client_checkudata(L, 1);
1382 if(lua_gettop(L) == 2)
1384 strut_t struts;
1385 area_t screen_area = display_area_get(c->phys_screen);
1387 struts.left = luaA_getopt_number(L, 2, "left", c->strut.left);
1388 struts.right = luaA_getopt_number(L, 2, "right", c->strut.right);
1389 struts.top = luaA_getopt_number(L, 2, "top", c->strut.top);
1390 struts.bottom = luaA_getopt_number(L, 2, "bottom", c->strut.bottom);
1392 if(struts.left != c->strut.left || struts.right != c->strut.right ||
1393 struts.top != c->strut.top || struts.bottom != c->strut.bottom) {
1394 /* Struts are not so well defined in the context of xinerama. So we just
1395 * give the entire root window and let the window manager decide. */
1396 struts.left_start_y = 0;
1397 struts.left_end_y = !struts.left ? 0 : screen_area.height;
1398 struts.right_start_y = 0;
1399 struts.right_end_y = !struts.right ? 0 : screen_area.height;
1400 struts.top_start_x = 0;
1401 struts.top_end_x = !struts.top ? 0 : screen_area.width;
1402 struts.bottom_start_x = 0;
1403 struts.bottom_end_x = !struts.bottom ? 0 : screen_area.width;
1405 c->strut = struts;
1407 ewmh_update_client_strut(c);
1409 hook_property(client, c, "struts");
1413 return luaA_pushstruts(L, c->strut);
1416 /** Client newindex.
1417 * \param L The Lua VM state.
1418 * \return The number of elements pushed on stack.
1420 static int
1421 luaA_client_newindex(lua_State *L)
1423 size_t len;
1424 client_t *c = luaA_client_checkudata(L, 1);
1425 const char *buf = luaL_checklstring(L, 2, &len);
1426 bool b;
1427 double d;
1428 int i;
1430 switch(a_tokenize(buf, len))
1432 case A_TK_SCREEN:
1433 if(globalconf.xinerama_is_active)
1435 i = luaL_checknumber(L, 3) - 1;
1436 luaA_checkscreen(i);
1437 screen_client_moveto(c, &globalconf.screens.tab[i], true, true);
1439 break;
1440 case A_TK_HIDE:
1441 b = luaA_checkboolean(L, 3);
1442 if(b != c->ishidden)
1444 client_need_reban(c);
1445 c->ishidden = b;
1446 client_need_reban(c);
1447 hook_property(client, c, "hide");
1449 break;
1450 case A_TK_MINIMIZED:
1451 client_setminimized(c, luaA_checkboolean(L, 3));
1452 break;
1453 case A_TK_FULLSCREEN:
1454 client_setfullscreen(c, luaA_checkboolean(L, 3));
1455 break;
1456 case A_TK_MAXIMIZED_HORIZONTAL:
1457 client_setmaxhoriz(c, luaA_checkboolean(L, 3));
1458 break;
1459 case A_TK_MAXIMIZED_VERTICAL:
1460 client_setmaxvert(c, luaA_checkboolean(L, 3));
1461 break;
1462 case A_TK_ICON:
1463 image_unref(L, c->icon);
1464 c->icon = image_ref(L);
1465 /* execute hook */
1466 hook_property(client, c, "icon");
1467 break;
1468 case A_TK_OPACITY:
1469 if(lua_isnil(L, 3))
1470 window_opacity_set(c->win, -1);
1471 else
1473 d = luaL_checknumber(L, 3);
1474 if(d >= 0 && d <= 1)
1475 window_opacity_set(c->win, d);
1477 break;
1478 case A_TK_STICKY:
1479 client_setsticky(c, luaA_checkboolean(L, 3));
1480 break;
1481 case A_TK_SIZE_HINTS_HONOR:
1482 c->size_hints_honor = luaA_checkboolean(L, 3);
1483 hook_property(client, c, "size_hints_honor");
1484 break;
1485 case A_TK_BORDER_WIDTH:
1486 client_setborder(c, luaL_checknumber(L, 3));
1487 break;
1488 case A_TK_ONTOP:
1489 client_setontop(c, luaA_checkboolean(L, 3));
1490 break;
1491 case A_TK_ABOVE:
1492 client_setabove(c, luaA_checkboolean(L, 3));
1493 break;
1494 case A_TK_BELOW:
1495 client_setbelow(c, luaA_checkboolean(L, 3));
1496 break;
1497 case A_TK_URGENT:
1498 client_seturgent(c, luaA_checkboolean(L, 3));
1499 break;
1500 case A_TK_BORDER_COLOR:
1501 if((buf = luaL_checklstring(L, 3, &len))
1502 && xcolor_init_reply(xcolor_init_unchecked(&c->border_color, buf, len)))
1503 xcb_change_window_attributes(globalconf.connection, c->win,
1504 XCB_CW_BORDER_PIXEL, &c->border_color.pixel);
1505 break;
1506 case A_TK_TITLEBAR:
1507 if(lua_isnil(L, 3))
1508 titlebar_client_detach(c);
1509 else
1510 titlebar_client_attach(c);
1511 break;
1512 case A_TK_SKIP_TASKBAR:
1513 c->skiptb = luaA_checkboolean(L, 3);
1514 hook_property(client, c, "skip_taskbar");
1515 break;
1516 default:
1517 return 0;
1520 return 0;
1523 /** Client object.
1524 * \param L The Lua VM state.
1525 * \return The number of elements pushed on stack.
1526 * \luastack
1527 * \lfield id The window X id.
1528 * \lfield name The client title.
1529 * \lfield skip_taskbar If true the client won't be shown in the tasklist.
1530 * \lfield type The window type (desktop, normal, dock, …).
1531 * \lfield class The client class.
1532 * \lfield instance The client instance.
1533 * \lfield pid The client PID, if available.
1534 * \lfield role The window role, if available.
1535 * \lfield machine The machine client is running on.
1536 * \lfield icon_name The client name when iconified.
1537 * \lfield screen Client screen number.
1538 * \lfield hide Define if the client must be hidden, i.e. never mapped,
1539 * invisible in taskbar.
1540 * \lfield minimized Define it the client must be iconify, i.e. only visible in
1541 * taskbar.
1542 * \lfield size_hints_honor Honor size hints, i.e. respect size ratio.
1543 * \lfield border_width The client border width.
1544 * \lfield border_color The client border color.
1545 * \lfield titlebar The client titlebar.
1546 * \lfield urgent The client urgent state.
1547 * \lfield content An image representing the client window content (screenshot).
1548 * \lfield focus The focused client.
1549 * \lfield opacity The client opacity between 0 and 1.
1550 * \lfield ontop The client is on top of every other windows.
1551 * \lfield above The client is above normal windows.
1552 * \lfield below The client is below normal windows.
1553 * \lfield fullscreen The client is fullscreen or not.
1554 * \lfield maximized_horizontal The client is maximized horizontally or not.
1555 * \lfield maximized_vertical The client is maximized vertically or not.
1556 * \lfield transient_for Return the client the window is transient for.
1557 * \lfield group_id Identification unique to a group of windows.
1558 * \lfield leader_id Identification unique to windows spawned by the same command.
1559 * \lfield size_hints A table with size hints of the client: user_position,
1560 * user_size, program_position, program_size, etc.
1562 static int
1563 luaA_client_index(lua_State *L)
1565 size_t len;
1566 ssize_t slen;
1567 client_t *c = luaA_client_checkudata(L, 1);
1568 const char *buf = luaL_checklstring(L, 2, &len);
1569 char *value;
1570 void *data;
1571 xcb_get_property_cookie_t prop_c;
1572 xcb_get_property_reply_t *prop_r = NULL;
1573 double d;
1575 if(luaA_usemetatable(L, 1, 2))
1576 return 1;
1578 switch(a_tokenize(buf, len))
1580 case A_TK_NAME:
1581 lua_pushstring(L, c->name);
1582 break;
1583 case A_TK_TRANSIENT_FOR:
1584 return client_push(globalconf.L, c->transient_for);
1585 case A_TK_SKIP_TASKBAR:
1586 lua_pushboolean(L, c->skiptb);
1587 break;
1588 case A_TK_CONTENT:
1589 return client_getcontent(c);
1590 case A_TK_TYPE:
1591 switch(c->type)
1593 case WINDOW_TYPE_DESKTOP:
1594 lua_pushliteral(L, "desktop");
1595 break;
1596 case WINDOW_TYPE_DOCK:
1597 lua_pushliteral(L, "dock");
1598 break;
1599 case WINDOW_TYPE_SPLASH:
1600 lua_pushliteral(L, "splash");
1601 break;
1602 case WINDOW_TYPE_DIALOG:
1603 lua_pushliteral(L, "dialog");
1604 break;
1605 case WINDOW_TYPE_MENU:
1606 lua_pushliteral(L, "menu");
1607 break;
1608 case WINDOW_TYPE_TOOLBAR:
1609 lua_pushliteral(L, "toolbar");
1610 break;
1611 case WINDOW_TYPE_UTILITY:
1612 lua_pushliteral(L, "utility");
1613 break;
1614 case WINDOW_TYPE_DROPDOWN_MENU:
1615 lua_pushliteral(L, "dropdown_menu");
1616 break;
1617 case WINDOW_TYPE_POPUP_MENU:
1618 lua_pushliteral(L, "popup_menu");
1619 break;
1620 case WINDOW_TYPE_TOOLTIP:
1621 lua_pushliteral(L, "tooltip");
1622 break;
1623 case WINDOW_TYPE_NOTIFICATION:
1624 lua_pushliteral(L, "notification");
1625 break;
1626 case WINDOW_TYPE_COMBO:
1627 lua_pushliteral(L, "combo");
1628 break;
1629 case WINDOW_TYPE_DND:
1630 lua_pushliteral(L, "dnd");
1631 break;
1632 case WINDOW_TYPE_NORMAL:
1633 lua_pushliteral(L, "normal");
1634 break;
1636 break;
1637 case A_TK_CLASS:
1638 lua_pushstring(L, c->class);
1639 break;
1640 case A_TK_INSTANCE:
1641 lua_pushstring(L, c->instance);
1642 break;
1643 case A_TK_ROLE:
1644 if(!xutil_text_prop_get(globalconf.connection, c->win,
1645 WM_WINDOW_ROLE, &value, &slen))
1646 return 0;
1647 lua_pushlstring(L, value, slen);
1648 p_delete(&value);
1649 break;
1650 case A_TK_PID:
1651 prop_c = xcb_get_property_unchecked(globalconf.connection, false, c->win, _NET_WM_PID, CARDINAL, 0L, 1L);
1652 prop_r = xcb_get_property_reply(globalconf.connection, prop_c, NULL);
1654 if(prop_r && prop_r->value_len && (data = xcb_get_property_value(prop_r)))
1655 lua_pushnumber(L, *(uint32_t *)data);
1656 else
1658 p_delete(&prop_r);
1659 return 0;
1661 break;
1662 case A_TK_ID:
1663 lua_pushnumber(L, c->win);
1664 break;
1665 case A_TK_LEADER_ID:
1666 lua_pushnumber(L, c->leader_win);
1667 break;
1668 case A_TK_MACHINE:
1669 if(!xutil_text_prop_get(globalconf.connection, c->win,
1670 WM_CLIENT_MACHINE, &value, &slen))
1671 return 0;
1672 lua_pushlstring(L, value, slen);
1673 p_delete(&value);
1674 break;
1675 case A_TK_ICON_NAME:
1676 lua_pushstring(L, c->icon_name);
1677 break;
1678 case A_TK_SCREEN:
1679 lua_pushnumber(L, 1 + screen_array_indexof(&globalconf.screens, c->screen));
1680 break;
1681 case A_TK_HIDE:
1682 lua_pushboolean(L, c->ishidden);
1683 break;
1684 case A_TK_MINIMIZED:
1685 lua_pushboolean(L, c->isminimized);
1686 break;
1687 case A_TK_FULLSCREEN:
1688 lua_pushboolean(L, c->isfullscreen);
1689 break;
1690 case A_TK_GROUP_ID:
1691 if(c->group_win)
1692 lua_pushnumber(L, c->group_win);
1693 else
1694 return 0;
1695 break;
1696 case A_TK_MAXIMIZED_HORIZONTAL:
1697 lua_pushboolean(L, c->ismaxhoriz);
1698 break;
1699 case A_TK_MAXIMIZED_VERTICAL:
1700 lua_pushboolean(L, c->ismaxvert);
1701 break;
1702 case A_TK_ICON:
1703 image_push(L, c->icon);
1704 break;
1705 case A_TK_OPACITY:
1706 if((d = window_opacity_get(c->win)) >= 0)
1707 lua_pushnumber(L, d);
1708 else
1709 return 0;
1710 break;
1711 case A_TK_ONTOP:
1712 lua_pushboolean(L, c->isontop);
1713 break;
1714 case A_TK_ABOVE:
1715 lua_pushboolean(L, c->isabove);
1716 break;
1717 case A_TK_BELOW:
1718 lua_pushboolean(L, c->isbelow);
1719 break;
1720 case A_TK_STICKY:
1721 lua_pushboolean(L, c->issticky);
1722 break;
1723 case A_TK_SIZE_HINTS_HONOR:
1724 lua_pushboolean(L, c->size_hints_honor);
1725 break;
1726 case A_TK_BORDER_WIDTH:
1727 lua_pushnumber(L, c->border);
1728 break;
1729 case A_TK_BORDER_COLOR:
1730 luaA_pushxcolor(L, &c->border_color);
1731 break;
1732 case A_TK_TITLEBAR:
1733 return wibox_push(L, c->titlebar);
1734 case A_TK_URGENT:
1735 lua_pushboolean(L, c->isurgent);
1736 break;
1737 case A_TK_SIZE_HINTS:
1739 const char *u_or_p = NULL;
1741 lua_createtable(L, 0, 1);
1743 if(c->size_hints.flags & XCB_SIZE_HINT_US_POSITION)
1744 u_or_p = "user_position";
1745 else if(c->size_hints.flags & XCB_SIZE_HINT_P_POSITION)
1746 u_or_p = "program_position";
1748 if(u_or_p)
1750 lua_createtable(L, 0, 2);
1751 lua_pushnumber(L, c->size_hints.x);
1752 lua_setfield(L, -2, "x");
1753 lua_pushnumber(L, c->size_hints.y);
1754 lua_setfield(L, -2, "y");
1755 lua_setfield(L, -2, u_or_p);
1756 u_or_p = NULL;
1759 if(c->size_hints.flags & XCB_SIZE_HINT_US_SIZE)
1760 u_or_p = "user_size";
1761 else if(c->size_hints.flags & XCB_SIZE_HINT_P_SIZE)
1762 u_or_p = "program_size";
1764 if(u_or_p)
1766 lua_createtable(L, 0, 2);
1767 lua_pushnumber(L, c->size_hints.width);
1768 lua_setfield(L, -2, "width");
1769 lua_pushnumber(L, c->size_hints.height);
1770 lua_setfield(L, -2, "height");
1771 lua_setfield(L, -2, u_or_p);
1774 if(c->size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE)
1776 lua_pushnumber(L, c->size_hints.min_width);
1777 lua_setfield(L, -2, "min_width");
1778 lua_pushnumber(L, c->size_hints.min_height);
1779 lua_setfield(L, -2, "min_height");
1782 if(c->size_hints.flags & XCB_SIZE_HINT_P_MAX_SIZE)
1784 lua_pushnumber(L, c->size_hints.max_width);
1785 lua_setfield(L, -2, "max_width");
1786 lua_pushnumber(L, c->size_hints.max_height);
1787 lua_setfield(L, -2, "max_height");
1790 if(c->size_hints.flags & XCB_SIZE_HINT_P_RESIZE_INC)
1792 lua_pushnumber(L, c->size_hints.width_inc);
1793 lua_setfield(L, -2, "width_inc");
1794 lua_pushnumber(L, c->size_hints.height_inc);
1795 lua_setfield(L, -2, "height_inc");
1798 if(c->size_hints.flags & XCB_SIZE_HINT_P_ASPECT)
1800 lua_pushnumber(L, c->size_hints.min_aspect_num);
1801 lua_setfield(L, -2, "min_aspect_num");
1802 lua_pushnumber(L, c->size_hints.min_aspect_den);
1803 lua_setfield(L, -2, "min_aspect_den");
1804 lua_pushnumber(L, c->size_hints.max_aspect_num);
1805 lua_setfield(L, -2, "max_aspect_num");
1806 lua_pushnumber(L, c->size_hints.max_aspect_den);
1807 lua_setfield(L, -2, "max_aspect_den");
1810 if(c->size_hints.flags & XCB_SIZE_HINT_BASE_SIZE)
1812 lua_pushnumber(L, c->size_hints.base_width);
1813 lua_setfield(L, -2, "base_width");
1814 lua_pushnumber(L, c->size_hints.base_height);
1815 lua_setfield(L, -2, "base_height");
1818 if(c->size_hints.flags & XCB_SIZE_HINT_P_WIN_GRAVITY)
1820 switch(c->size_hints.win_gravity)
1822 default:
1823 lua_pushliteral(L, "north_west");
1824 break;
1825 case XCB_GRAVITY_NORTH:
1826 lua_pushliteral(L, "north");
1827 break;
1828 case XCB_GRAVITY_NORTH_EAST:
1829 lua_pushliteral(L, "north_east");
1830 break;
1831 case XCB_GRAVITY_WEST:
1832 lua_pushliteral(L, "west");
1833 break;
1834 case XCB_GRAVITY_CENTER:
1835 lua_pushliteral(L, "center");
1836 break;
1837 case XCB_GRAVITY_EAST:
1838 lua_pushliteral(L, "east");
1839 break;
1840 case XCB_GRAVITY_SOUTH_WEST:
1841 lua_pushliteral(L, "south_west");
1842 break;
1843 case XCB_GRAVITY_SOUTH:
1844 lua_pushliteral(L, "south");
1845 break;
1846 case XCB_GRAVITY_SOUTH_EAST:
1847 lua_pushliteral(L, "south_east");
1848 break;
1849 case XCB_GRAVITY_STATIC:
1850 lua_pushliteral(L, "static");
1851 break;
1853 lua_setfield(L, -2, "win_gravity");
1856 break;
1857 default:
1858 return 0;
1861 return 1;
1864 /** Get or set mouse buttons bindings for a client.
1865 * \param L The Lua VM state.
1866 * \return The number of element pushed on stack.
1867 * \luastack
1868 * \lvalue A client.
1869 * \lparam An array of mouse button bindings objects, or nothing.
1870 * \return The array of mouse button bindings objects of this client.
1872 static int
1873 luaA_client_buttons(lua_State *L)
1875 client_t *client = luaA_client_checkudata(L, 1);
1876 button_array_t *buttons = &client->buttons;
1878 if(lua_gettop(L) == 2)
1879 luaA_button_array_set(L, 2, buttons);
1881 window_buttons_grab(client->win, &client->buttons);
1883 return luaA_button_array_get(L, buttons);
1886 /** Get or set keys bindings for a client.
1887 * \param L The Lua VM state.
1888 * \return The number of element pushed on stack.
1889 * \luastack
1890 * \lvalue A client.
1891 * \lparam An array of key bindings objects, or nothing.
1892 * \return The array of key bindings objects of this client.
1894 static int
1895 luaA_client_keys(lua_State *L)
1897 client_t *c = luaA_client_checkudata(L, 1);
1898 key_array_t *keys = &c->keys;
1900 if(lua_gettop(L) == 2)
1902 luaA_key_array_set(L, 2, keys);
1903 xcb_ungrab_key(globalconf.connection, XCB_GRAB_ANY, c->win, XCB_BUTTON_MASK_ANY);
1904 window_grabkeys(c->win, keys);
1907 return luaA_key_array_get(L, keys);
1910 /* Client module.
1911 * \param L The Lua VM state.
1912 * \return The number of pushed elements.
1914 static int
1915 luaA_client_module_index(lua_State *L)
1917 size_t len;
1918 const char *buf = luaL_checklstring(L, 2, &len);
1920 switch(a_tokenize(buf, len))
1922 case A_TK_FOCUS:
1923 return client_push(globalconf.L, globalconf.screen_focus->client_focus);
1924 break;
1925 default:
1926 return 0;
1930 /* Client module new index.
1931 * \param L The Lua VM state.
1932 * \return The number of pushed elements.
1934 static int
1935 luaA_client_module_newindex(lua_State *L)
1937 size_t len;
1938 const char *buf = luaL_checklstring(L, 2, &len);
1939 client_t *c;
1941 switch(a_tokenize(buf, len))
1943 case A_TK_FOCUS:
1944 c = luaA_client_checkudata(L, 3);
1945 client_focus(c);
1946 break;
1947 default:
1948 break;
1951 return 0;
1954 const struct luaL_reg awesome_client_methods[] =
1956 { "get", luaA_client_get },
1957 { "__index", luaA_client_module_index },
1958 { "__newindex", luaA_client_module_newindex },
1959 { NULL, NULL }
1961 const struct luaL_reg awesome_client_meta[] =
1963 { "isvisible", luaA_client_isvisible },
1964 { "geometry", luaA_client_geometry },
1965 { "struts", luaA_client_struts },
1966 { "buttons", luaA_client_buttons },
1967 { "keys", luaA_client_keys },
1968 { "tags", luaA_client_tags },
1969 { "kill", luaA_client_kill },
1970 { "swap", luaA_client_swap },
1971 { "raise", luaA_client_raise },
1972 { "lower", luaA_client_lower },
1973 { "redraw", luaA_client_redraw },
1974 { "unmanage", luaA_client_unmanage },
1975 { "__index", luaA_client_index },
1976 { "__newindex", luaA_client_newindex },
1977 { "__gc", luaA_client_gc },
1978 { "__tostring", luaA_client_tostring },
1979 { NULL, NULL }
1982 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80