image: Remove some code duplication
[awesome.git] / client.c
blob0d0e2e800dea1bbba1044e5c3d87930d4fb74370
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(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 /* Move all clients out of the physical viewport into negative coordinate space. */
208 /* They will all be put on top of each other. */
209 uint32_t request[2] = { - (c->geometries.internal.width + 2 * c->border),
210 - (c->geometries.internal.height + 2 * c->border) };
212 xcb_configure_window(globalconf.connection, c->win,
213 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
214 request);
216 c->isbanned = true;
218 if(globalconf.screens.tab[c->phys_screen].prev_client_focus == c)
219 globalconf.screens.tab[c->phys_screen].prev_client_focus = NULL;
221 /* Wait until the last moment to take away the focus from the window. */
222 if(globalconf.screens.tab[c->phys_screen].client_focus == c)
223 client_unfocus(c);
227 /** Record that a client got focus.
228 * \param c Client being focused.
230 void
231 client_focus_update(client_t *c)
233 if(!client_maybevisible(c, c->screen))
235 /* Focus previously focused client */
236 client_focus(globalconf.screen_focus->prev_client_focus);
237 return;
240 if(globalconf.screen_focus
241 && globalconf.screen_focus->client_focus)
243 if (globalconf.screen_focus->client_focus != c)
244 client_unfocus_update(globalconf.screen_focus->client_focus);
245 else
246 /* Already focused */
247 return;
249 /* stop hiding client */
250 c->ishidden = false;
251 client_setminimized(c, false);
253 /* unban the client before focusing for consistency */
254 client_unban(c);
256 globalconf.screen_focus = &globalconf.screens.tab[c->phys_screen];
257 globalconf.screen_focus->prev_client_focus = c;
258 globalconf.screen_focus->client_focus = c;
260 /* according to EWMH, we have to remove the urgent state from a client */
261 client_seturgent(c, false);
263 ewmh_update_net_active_window(c->phys_screen);
265 /* execute hook */
266 if(globalconf.hooks.focus != LUA_REFNIL)
268 client_push(globalconf.L, c);
269 luaA_dofunction(globalconf.L, globalconf.hooks.focus, 1, 0);
274 /** Give focus to client, or to first client if client is NULL.
275 * \param c The client or NULL.
277 void
278 client_focus(client_t *c)
280 /* We have to set focus on first client */
281 if(!c && globalconf.clients.len && !(c = globalconf.clients.tab[0]))
282 return;
284 if(!client_maybevisible(c, c->screen))
285 return;
287 if (!c->nofocus)
288 client_focus_update(c);
290 window_setfocus(c->win, !c->nofocus);
293 /** Stack a window below.
294 * \param c The client.
295 * \param previous The previous window on the stack.
296 * \param return The next-previous!
298 static xcb_window_t
299 client_stack_above(client_t *c, xcb_window_t previous)
301 uint32_t config_win_vals[2];
303 config_win_vals[0] = previous;
304 config_win_vals[1] = XCB_STACK_MODE_ABOVE;
306 xcb_configure_window(globalconf.connection, c->win,
307 XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE,
308 config_win_vals);
310 config_win_vals[0] = c->win;
312 if(c->titlebar)
314 xcb_configure_window(globalconf.connection,
315 c->titlebar->sw.window,
316 XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE,
317 config_win_vals);
318 previous = c->titlebar->sw.window;
320 else
321 previous = c->win;
323 /* stack transient window on top of their parents */
324 foreach(node, globalconf.stack)
325 if((*node)->transient_for == c)
326 previous = client_stack_above(*node, previous);
328 return previous;
331 /** Stacking layout layers */
332 typedef enum
334 /** This one is a special layer */
335 LAYER_IGNORE,
336 LAYER_DESKTOP,
337 LAYER_BELOW,
338 LAYER_NORMAL,
339 LAYER_ABOVE,
340 LAYER_FULLSCREEN,
341 LAYER_ONTOP,
342 /** This one only used for counting and is not a real layer */
343 LAYER_COUNT
344 } layer_t;
346 /** Get the real layer of a client according to its attribute (fullscreen, …)
347 * \param c The client.
348 * \return The real layer.
350 static layer_t
351 client_layer_translator(client_t *c)
353 /* first deal with user set attributes */
354 if(c->isontop)
355 return LAYER_ONTOP;
356 else if(c->isfullscreen)
357 return LAYER_FULLSCREEN;
358 else if(c->isabove)
359 return LAYER_ABOVE;
360 else if(c->isbelow)
361 return LAYER_BELOW;
363 /* check for transient attr */
364 if(c->transient_for)
365 return LAYER_IGNORE;
367 /* then deal with windows type */
368 switch(c->type)
370 case WINDOW_TYPE_DESKTOP:
371 return LAYER_DESKTOP;
372 default:
373 break;
376 return LAYER_NORMAL;
379 /** Restack clients.
380 * \todo It might be worth stopping to restack everyone and only stack `c'
381 * relatively to the first matching in the list.
383 static void
384 client_real_stack(void)
386 uint32_t config_win_vals[2];
387 layer_t layer;
389 config_win_vals[0] = XCB_NONE;
390 config_win_vals[1] = XCB_STACK_MODE_ABOVE;
392 /* stack desktop windows */
393 for(layer = LAYER_DESKTOP; layer < LAYER_BELOW; layer++)
394 foreach(node, globalconf.stack)
395 if(client_layer_translator(*node) == layer)
396 config_win_vals[0] = client_stack_above(*node,
397 config_win_vals[0]);
399 /* first stack not ontop wibox window */
400 foreach(_sb, globalconf.wiboxes)
402 wibox_t *sb = *_sb;
403 if(!sb->ontop)
405 xcb_configure_window(globalconf.connection,
406 sb->sw.window,
407 XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE,
408 config_win_vals);
409 config_win_vals[0] = sb->sw.window;
413 /* then stack clients */
414 for(layer = LAYER_BELOW; layer < LAYER_COUNT; layer++)
415 foreach(node, globalconf.stack)
416 if(client_layer_translator(*node) == layer)
417 config_win_vals[0] = client_stack_above(*node,
418 config_win_vals[0]);
420 /* then stack ontop wibox window */
421 foreach(_sb, globalconf.wiboxes)
423 wibox_t *sb = *_sb;
424 if(sb->ontop)
426 xcb_configure_window(globalconf.connection,
427 sb->sw.window,
428 XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE,
429 config_win_vals);
430 config_win_vals[0] = sb->sw.window;
435 void
436 client_stack_refresh()
438 if (!globalconf.client_need_stack_refresh)
439 return;
440 globalconf.client_need_stack_refresh = false;
441 client_real_stack();
444 /** Manage a new client.
445 * \param w The window.
446 * \param wgeom Window geometry.
447 * \param phys_screen Physical screen number.
448 * \param startup True if we are managing at startup time.
450 void
451 client_manage(xcb_window_t w, xcb_get_geometry_reply_t *wgeom, int phys_screen, bool startup)
453 xcb_get_property_cookie_t ewmh_icon_cookie;
454 client_t *c, *tc = NULL;
455 screen_t *screen;
456 const uint32_t select_input_val[] = { CLIENT_SELECT_INPUT_EVENT_MASK };
458 if(systray_iskdedockapp(w))
460 systray_request_handle(w, phys_screen, NULL);
461 return;
464 /* Send request to get NET_WM_ICON property as soon as possible... */
465 ewmh_icon_cookie = ewmh_window_icon_get_unchecked(w);
466 xcb_change_window_attributes(globalconf.connection, w, XCB_CW_EVENT_MASK, select_input_val);
468 c = client_new(globalconf.L);
469 /* Push client in client list */
470 client_array_push(&globalconf.clients, client_ref(globalconf.L));
473 screen = c->screen = screen_getbycoord(&globalconf.screens.tab[phys_screen],
474 wgeom->x, wgeom->y);
476 c->phys_screen = phys_screen;
478 /* Initial values */
479 c->win = w;
480 c->geometry.x = wgeom->x;
481 c->geometry.y = wgeom->y;
482 /* Border will be added later. */
483 c->geometry.width = wgeom->width;
484 c->geometry.height = wgeom->height;
485 /* Also set internal geometry (client_ban() needs it). */
486 c->geometries.internal.x = wgeom->x;
487 c->geometries.internal.y = wgeom->y;
488 c->geometries.internal.width = wgeom->width;
489 c->geometries.internal.height = wgeom->height;
490 client_setborder(c, wgeom->border_width);
492 if(ewmh_window_icon_get_reply(ewmh_icon_cookie))
493 c->icon = image_ref(globalconf.L);
495 /* we honor size hints by default */
496 c->size_hints_honor = true;
498 /* update hints */
499 property_update_wm_normal_hints(c, NULL);
500 property_update_wm_hints(c, NULL);
501 property_update_wm_transient_for(c, NULL);
502 property_update_wm_client_leader(c, NULL);
504 /* Recursively find the parent. */
505 for(tc = c; tc->transient_for; tc = tc->transient_for);
506 if(tc != c && tc->phys_screen == c->phys_screen)
507 screen = tc->screen;
509 /* Then check clients hints */
510 ewmh_client_check_hints(c);
512 /* move client to screen, but do not tag it */
513 screen_client_moveto(c, screen, false, true);
515 /* Push client in stack */
516 client_raise(c);
518 /* update window title */
519 property_update_wm_name(c);
520 property_update_wm_icon_name(c);
521 property_update_wm_class(c, NULL);
523 xutil_text_prop_get(globalconf.connection, c->win, _NET_STARTUP_ID, &c->startup_id, NULL);
525 /* update strut */
526 ewmh_process_client_strut(c, NULL);
528 ewmh_update_net_client_list(c->phys_screen);
530 /* Always stay in NORMAL_STATE. Even though iconified seems more
531 * appropriate sometimes. The only possible loss is that clients not using
532 * visibility events may continue to proces data (when banned).
533 * Without any exposes or other events the cost should be fairly limited though.
535 * Some clients may expect the window to be unmapped when STATE_ICONIFIED.
536 * Two conflicting parts of the ICCCM v2.0 (section 4.1.4):
538 * "Normal -> Iconic - The client should send a ClientMessage event as described later in this section."
539 * (note no explicit mention of unmapping, while Normal->Widthdrawn does mention that)
541 * "Once a client's window has left the Withdrawn state, the window will be mapped
542 * if it is in the Normal state and the window will be unmapped if it is in the Iconic state."
544 * At this stage it's just safer to keep it in normal state and avoid confusion.
546 window_state_set(c->win, XCB_WM_STATE_NORMAL);
548 /* Move window outside the viewport before mapping it. */
549 client_ban(c);
550 xcb_map_window(globalconf.connection, c->win);
552 if(!startup)
553 spawn_start_notify(c);
555 /* Call hook to notify list change */
556 if(globalconf.hooks.clients != LUA_REFNIL)
557 luaA_dofunction(globalconf.L, globalconf.hooks.clients, 0, 0);
559 /* call hook */
560 if(globalconf.hooks.manage != LUA_REFNIL)
562 client_push(globalconf.L, c);
563 lua_pushboolean(globalconf.L, startup);
564 luaA_dofunction(globalconf.L, globalconf.hooks.manage, 2, 0);
568 /** Compute client geometry with respect to its geometry hints.
569 * \param c The client.
570 * \param geometry The geometry that the client might receive.
571 * \return The geometry the client must take respecting its hints.
573 area_t
574 client_geometry_hints(client_t *c, area_t geometry)
576 int32_t basew, baseh, minw, minh;
578 /* base size is substituted with min size if not specified */
579 if(c->size_hints.flags & XCB_SIZE_HINT_P_SIZE)
581 basew = c->size_hints.base_width;
582 baseh = c->size_hints.base_height;
584 else if(c->size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE)
586 basew = c->size_hints.min_width;
587 baseh = c->size_hints.min_height;
589 else
590 basew = baseh = 0;
592 /* min size is substituted with base size if not specified */
593 if(c->size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE)
595 minw = c->size_hints.min_width;
596 minh = c->size_hints.min_height;
598 else if(c->size_hints.flags & XCB_SIZE_HINT_P_SIZE)
600 minw = c->size_hints.base_width;
601 minh = c->size_hints.base_height;
603 else
604 minw = minh = 0;
606 if(c->size_hints.flags & XCB_SIZE_HINT_P_ASPECT
607 && c->size_hints.min_aspect_num > 0
608 && c->size_hints.min_aspect_den > 0
609 && geometry.height - baseh > 0
610 && geometry.width - basew > 0)
612 double dx = (double) (geometry.width - basew);
613 double dy = (double) (geometry.height - baseh);
614 double min = (double) c->size_hints.min_aspect_num / (double) c->size_hints.min_aspect_den;
615 double max = (double) c->size_hints.max_aspect_num / (double) c->size_hints.min_aspect_den;
616 double ratio = dx / dy;
617 if(max > 0 && min > 0 && ratio > 0)
619 if(ratio < min)
621 dy = (dx * min + dy) / (min * min + 1);
622 dx = dy * min;
623 geometry.width = (int) dx + basew;
624 geometry.height = (int) dy + baseh;
626 else if(ratio > max)
628 dy = (dx * min + dy) / (max * max + 1);
629 dx = dy * min;
630 geometry.width = (int) dx + basew;
631 geometry.height = (int) dy + baseh;
636 if(minw)
637 geometry.width = MAX(geometry.width, minw);
638 if(minh)
639 geometry.height = MAX(geometry.height, minh);
641 if(c->size_hints.flags & XCB_SIZE_HINT_P_MAX_SIZE)
643 if(c->size_hints.max_width)
644 geometry.width = MIN(geometry.width, c->size_hints.max_width);
645 if(c->size_hints.max_height)
646 geometry.height = MIN(geometry.height, c->size_hints.max_height);
649 if(c->size_hints.flags & (XCB_SIZE_HINT_P_RESIZE_INC | XCB_SIZE_HINT_BASE_SIZE)
650 && c->size_hints.width_inc && c->size_hints.height_inc)
652 uint16_t t1 = geometry.width, t2 = geometry.height;
653 unsigned_subtract(t1, basew);
654 unsigned_subtract(t2, baseh);
655 geometry.width -= t1 % c->size_hints.width_inc;
656 geometry.height -= t2 % c->size_hints.height_inc;
659 return geometry;
662 /** Resize client window.
663 * The sizse given as parameters are with titlebar and borders!
664 * \param c Client to resize.
665 * \param geometry New window geometry.
666 * \param hints Use size hints.
667 * \return true if an actual resize occurred.
669 bool
670 client_resize(client_t *c, area_t geometry, bool hints)
672 area_t geometry_internal;
673 area_t area;
675 /* offscreen appearance fixes */
676 area = display_area_get(c->phys_screen);
678 if(geometry.x > area.width)
679 geometry.x = area.width - geometry.width;
680 if(geometry.y > area.height)
681 geometry.y = area.height - geometry.height;
682 if(geometry.x + geometry.width < 0)
683 geometry.x = 0;
684 if(geometry.y + geometry.height < 0)
685 geometry.y = 0;
687 /* Real client geometry, please keep it contained to C code at the very least. */
688 geometry_internal = titlebar_geometry_remove(c->titlebar, c->border, geometry);
690 if(hints)
691 geometry_internal = client_geometry_hints(c, geometry_internal);
693 if(geometry_internal.width == 0 || geometry_internal.height == 0)
694 return false;
696 /* Also let client hints propegate to the "official" geometry. */
697 geometry = titlebar_geometry_add(c->titlebar, c->border, geometry_internal);
699 if(c->geometries.internal.x != geometry_internal.x
700 || c->geometries.internal.y != geometry_internal.y
701 || c->geometries.internal.width != geometry_internal.width
702 || c->geometries.internal.height != geometry_internal.height)
704 screen_t *new_screen = screen_getbycoord(c->screen,
705 geometry_internal.x, geometry_internal.y);
707 /* Values to configure a window is an array where values are
708 * stored according to 'value_mask' */
709 uint32_t values[4];
711 c->geometries.internal.x = values[0] = geometry_internal.x;
712 c->geometries.internal.y = values[1] = geometry_internal.y;
713 c->geometries.internal.width = values[2] = geometry_internal.width;
714 c->geometries.internal.height = values[3] = geometry_internal.height;
716 /* Also store geometry including border and titlebar. */
717 c->geometry = geometry;
719 titlebar_update_geometry(c);
721 /* The idea is to give a client a resize even when banned. */
722 /* We just have to move the (x,y) to keep it out of the viewport. */
723 /* This at least doesn't break expectations about events. */
724 if(c->isbanned)
726 geometry_internal.x = values[0] = - (geometry_internal.width + 2 * c->border);
727 geometry_internal.y = values[1] = - (geometry_internal.height + 2 * c->border);
730 xcb_configure_window(globalconf.connection, c->win,
731 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y
732 | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
733 values);
735 screen_client_moveto(c, new_screen, true, false);
737 /* execute hook */
738 hook_property(client, c, "geometry");
740 return true;
743 return false;
746 /** Set a client minimized, or not.
747 * \param c The client.
748 * \param s Set or not the client minimized.
750 void
751 client_setminimized(client_t *c, bool s)
753 if(c->isminimized != s)
755 client_need_arrange(c);
756 c->isminimized = s;
757 client_need_arrange(c);
758 if(s)
759 window_state_set(c->win, XCB_WM_STATE_ICONIC);
760 else
761 window_state_set(c->win, XCB_WM_STATE_NORMAL);
762 ewmh_client_update_hints(c);
763 /* execute hook */
764 hook_property(client, c, "minimized");
768 /** Set a client sticky, or not.
769 * \param c The client.
770 * \param s Set or not the client sticky.
772 void
773 client_setsticky(client_t *c, bool s)
775 if(c->issticky != s)
777 client_need_arrange(c);
778 c->issticky = s;
779 client_need_arrange(c);
780 ewmh_client_update_hints(c);
781 hook_property(client, c, "sticky");
785 /** Set a client fullscreen, or not.
786 * \param c The client.
787 * \param s Set or not the client fullscreen.
789 void
790 client_setfullscreen(client_t *c, bool s)
792 if(c->isfullscreen != s)
794 area_t geometry;
796 /* Make sure the current geometry is stored without titlebar. */
797 if(s)
798 titlebar_ban(c->titlebar);
800 /* become fullscreen! */
801 if((c->isfullscreen = s))
803 /* remove any max state */
804 client_setmaxhoriz(c, false);
805 client_setmaxvert(c, false);
806 /* You can only be part of one of the special layers. */
807 client_setbelow(c, false);
808 client_setabove(c, false);
809 client_setontop(c, false);
811 geometry = screen_area_get(c->screen, false);
812 c->geometries.fullscreen = c->geometry;
813 c->border_fs = c->border;
814 client_setborder(c, 0);
816 else
818 geometry = c->geometries.fullscreen;
819 client_setborder(c, c->border_fs);
821 client_resize(c, geometry, false);
822 client_stack();
823 ewmh_client_update_hints(c);
824 hook_property(client, c, "fullscreen");
828 /** Set a client horizontally maximized.
829 * \param c The client.
830 * \param s The maximized status.
832 void
833 client_setmaxhoriz(client_t *c, bool s)
835 if(c->ismaxhoriz != s)
837 area_t geometry;
839 if((c->ismaxhoriz = s))
841 /* remove fullscreen mode */
842 client_setfullscreen(c, false);
844 geometry = screen_area_get(c->screen, true);
845 geometry.y = c->geometry.y;
846 geometry.height = c->geometry.height;
847 c->geometries.max.x = c->geometry.x;
848 c->geometries.max.width = c->geometry.width;
850 else
852 geometry = c->geometry;
853 geometry.x = c->geometries.max.x;
854 geometry.width = c->geometries.max.width;
857 client_resize(c, geometry, c->size_hints_honor);
858 client_stack();
859 ewmh_client_update_hints(c);
860 hook_property(client, c, "maximized_horizontal");
864 /** Set a client vertically maximized.
865 * \param c The client.
866 * \param s The maximized status.
868 void
869 client_setmaxvert(client_t *c, bool s)
871 if(c->ismaxvert != s)
873 area_t geometry;
875 if((c->ismaxvert = s))
877 /* remove fullscreen mode */
878 client_setfullscreen(c, false);
880 geometry = screen_area_get(c->screen, true);
881 geometry.x = c->geometry.x;
882 geometry.width = c->geometry.width;
883 c->geometries.max.y = c->geometry.y;
884 c->geometries.max.height = c->geometry.height;
886 else
888 geometry = c->geometry;
889 geometry.y = c->geometries.max.y;
890 geometry.height = c->geometries.max.height;
893 client_resize(c, geometry, c->size_hints_honor);
894 client_stack();
895 ewmh_client_update_hints(c);
896 hook_property(client, c, "maximized_vertical");
900 /** Set a client above, or not.
901 * \param c The client.
902 * \param s Set or not the client above.
904 void
905 client_setabove(client_t *c, bool s)
907 if(c->isabove != s)
909 /* You can only be part of one of the special layers. */
910 if(s)
912 client_setbelow(c, false);
913 client_setontop(c, false);
914 client_setfullscreen(c, false);
916 c->isabove = s;
917 client_stack();
918 ewmh_client_update_hints(c);
919 /* execute hook */
920 hook_property(client, c, "above");
924 /** Set a client below, or not.
925 * \param c The client.
926 * \param s Set or not the client below.
928 void
929 client_setbelow(client_t *c, bool s)
931 if(c->isbelow != s)
933 /* You can only be part of one of the special layers. */
934 if(s)
936 client_setabove(c, false);
937 client_setontop(c, false);
938 client_setfullscreen(c, false);
940 c->isbelow = s;
941 client_stack();
942 ewmh_client_update_hints(c);
943 /* execute hook */
944 hook_property(client, c, "below");
948 /** Set a client modal, or not.
949 * \param c The client.
950 * \param s Set or not the client moda.
952 void
953 client_setmodal(client_t *c, bool s)
955 if(c->ismodal != s)
957 c->ismodal = s;
958 client_stack();
959 ewmh_client_update_hints(c);
960 /* execute hook */
961 hook_property(client, c, "modal");
965 /** Set a client ontop, or not.
966 * \param c The client.
967 * \param s Set or not the client moda.
969 void
970 client_setontop(client_t *c, bool s)
972 if(c->isontop != s)
974 /* You can only be part of one of the special layers. */
975 if(s)
977 client_setabove(c, false);
978 client_setbelow(c, false);
979 client_setfullscreen(c, false);
981 c->isontop = s;
982 client_stack();
983 /* execute hook */
984 hook_property(client, c, "ontop");
988 /** Unban a client and move it back into the viewport.
989 * \param c The client.
991 void
992 client_unban(client_t *c)
994 if(c->isbanned)
996 /* Move the client back where it belongs. */
997 uint32_t request[] = { c->geometries.internal.x,
998 c->geometries.internal.y,
999 c->geometries.internal.width,
1000 c->geometries.internal.height };
1002 xcb_configure_window(globalconf.connection, c->win,
1003 XCB_CONFIG_WINDOW_X
1004 | XCB_CONFIG_WINDOW_Y
1005 | XCB_CONFIG_WINDOW_WIDTH
1006 | XCB_CONFIG_WINDOW_HEIGHT,
1007 request);
1009 c->isbanned = false;
1013 /** Unmanage a client.
1014 * \param c The client.
1016 void
1017 client_unmanage(client_t *c)
1019 tag_array_t *tags = &c->screen->tags;
1021 /* Reset transient_for attributes of widows that maybe refering to us */
1022 foreach(_tc, globalconf.clients)
1024 client_t *tc = *_tc;
1025 if(tc->transient_for == c)
1026 tc->transient_for = NULL;
1029 if(globalconf.screens.tab[c->phys_screen].prev_client_focus == c)
1030 globalconf.screens.tab[c->phys_screen].prev_client_focus = NULL;
1032 if(globalconf.screens.tab[c->phys_screen].client_focus == c)
1033 client_unfocus(c);
1035 /* remove client from global list and everywhere else */
1036 foreach(elem, globalconf.clients)
1037 if(*elem == c)
1039 client_array_remove(&globalconf.clients, elem);
1040 break;
1042 stack_client_remove(c);
1043 for(int i = 0; i < tags->len; i++)
1044 untag_client(c, tags->tab[i]);
1046 /* call hook */
1047 if(globalconf.hooks.unmanage != LUA_REFNIL)
1049 client_push(globalconf.L, c);
1050 luaA_dofunction(globalconf.L, globalconf.hooks.unmanage, 1, 0);
1053 /* Call hook to notify list change */
1054 if(globalconf.hooks.clients != LUA_REFNIL)
1055 luaA_dofunction(globalconf.L, globalconf.hooks.clients, 0, 0);
1057 /* The server grab construct avoids race conditions. */
1058 xcb_grab_server(globalconf.connection);
1060 xcb_ungrab_button(globalconf.connection, XCB_BUTTON_INDEX_ANY, c->win,
1061 XCB_BUTTON_MASK_ANY);
1062 window_state_set(c->win, XCB_WM_STATE_WITHDRAWN);
1064 xcb_flush(globalconf.connection);
1065 xcb_ungrab_server(globalconf.connection);
1067 titlebar_client_detach(c);
1069 ewmh_update_net_client_list(c->phys_screen);
1071 /* set client as invalid */
1072 c->invalid = true;
1074 client_unref(globalconf.L, c);
1077 /** Kill a client via a WM_DELETE_WINDOW request or KillClient if not
1078 * supported.
1079 * \param c The client to kill.
1081 void
1082 client_kill(client_t *c)
1084 if(window_hasproto(c->win, WM_DELETE_WINDOW))
1086 xcb_client_message_event_t ev;
1088 /* Initialize all of event's fields first */
1089 p_clear(&ev, 1);
1091 ev.response_type = XCB_CLIENT_MESSAGE;
1092 ev.window = c->win;
1093 ev.format = 32;
1094 ev.data.data32[1] = XCB_CURRENT_TIME;
1095 ev.type = WM_PROTOCOLS;
1096 ev.data.data32[0] = WM_DELETE_WINDOW;
1098 xcb_send_event(globalconf.connection, false, c->win,
1099 XCB_EVENT_MASK_NO_EVENT, (char *) &ev);
1101 else
1102 xcb_kill_client(globalconf.connection, c->win);
1105 /** Get all clients into a table.
1106 * \param L The Lua VM state.
1107 * \return The number of elements pushed on stack.
1108 * \luastack
1109 * \lparam An optional screen nunmber.
1110 * \lreturn A table with all clients.
1112 static int
1113 luaA_client_get(lua_State *L)
1115 int i = 1, screen;
1117 screen = luaL_optnumber(L, 1, 0) - 1;
1119 lua_newtable(L);
1121 if(screen == -1)
1122 foreach(c, globalconf.clients)
1124 client_push(L, *c);
1125 lua_rawseti(L, -2, i++);
1127 else
1129 luaA_checkscreen(screen);
1130 foreach(c, globalconf.clients)
1131 if((*c)->screen == &globalconf.screens.tab[screen])
1133 client_push(L, *c);
1134 lua_rawseti(L, -2, i++);
1138 return 1;
1141 /** Check if a client is visible on its screen.
1142 * \param L The Lua VM state.
1143 * \return The number of elements pushed on stack.
1144 * \luastack
1145 * \lvalue A client.
1146 * \lreturn A boolean value, true if the client is visible, false otherwise.
1148 static int
1149 luaA_client_isvisible(lua_State *L)
1151 client_t *c = luaA_client_checkudata(L, 1);
1152 lua_pushboolean(L, client_isvisible(c, c->screen));
1153 return 1;
1156 /** Set client border width.
1157 * \param c The client.
1158 * \param width The border width.
1160 void
1161 client_setborder(client_t *c, int width)
1163 uint32_t w = width;
1165 if(width > 0 && (c->type == WINDOW_TYPE_DOCK
1166 || c->type == WINDOW_TYPE_SPLASH
1167 || c->type == WINDOW_TYPE_DESKTOP
1168 || c->isfullscreen))
1169 return;
1171 if(width == c->border || width < 0)
1172 return;
1174 /* Update geometry with the new border. */
1175 c->geometry.width -= 2 * c->border;
1176 c->geometry.height -= 2 * c->border;
1178 c->border = width;
1179 xcb_configure_window(globalconf.connection, c->win,
1180 XCB_CONFIG_WINDOW_BORDER_WIDTH, &w);
1182 c->geometry.width += 2 * c->border;
1183 c->geometry.height += 2 * c->border;
1185 /* Changing border size also affects the size of the titlebar. */
1186 titlebar_update_geometry(c);
1188 hook_property(client, c, "border_width");
1191 /** Kill a client.
1192 * \param L The Lua VM state.
1194 * \luastack
1195 * \lvalue A client.
1197 static int
1198 luaA_client_kill(lua_State *L)
1200 client_t *c = luaA_client_checkudata(L, 1);
1201 client_kill(c);
1202 return 0;
1205 /** Swap a client with another one.
1206 * \param L The Lua VM state.
1207 * \luastack
1208 * \lvalue A client.
1209 * \lparam A client to swap with.
1211 static int
1212 luaA_client_swap(lua_State *L)
1214 client_t *c = luaA_client_checkudata(L, 1);
1215 client_t *swap = luaA_client_checkudata(L, 2);
1217 if(c != swap)
1219 client_t **ref_c = NULL, **ref_swap = NULL;
1220 foreach(item, globalconf.clients)
1222 if(*item == c)
1223 ref_c = item;
1224 else if(*item == swap)
1225 ref_swap = item;
1226 if(ref_c && ref_swap)
1227 break;
1229 /* swap ! */
1230 *ref_c = swap;
1231 *ref_swap = c;
1233 /* Call hook to notify list change */
1234 if(globalconf.hooks.clients != LUA_REFNIL)
1235 luaA_dofunction(L, globalconf.hooks.clients, 0, 0);
1238 return 0;
1241 /** Access or set the client tags.
1242 * \param L The Lua VM state.
1243 * \return The number of elements pushed on stack.
1244 * \lparam A table with tags to set, or none to get the current tags table.
1245 * \return The clients tag.
1247 static int
1248 luaA_client_tags(lua_State *L)
1250 client_t *c = luaA_client_checkudata(L, 1);
1251 tag_array_t *tags = &c->screen->tags;
1252 int j = 0;
1254 if(lua_gettop(L) == 2)
1256 luaA_checktable(L, 2);
1257 for(int i = 0; i < tags->len; i++)
1258 untag_client(c, tags->tab[i]);
1259 lua_pushnil(L);
1260 while(lua_next(L, 2))
1261 tag_client(c);
1262 lua_pop(L, 1);
1265 lua_newtable(L);
1266 foreach(tag, *tags)
1267 if(is_client_tagged(c, *tag))
1269 tag_push(L, *tag);
1270 lua_rawseti(L, -2, ++j);
1273 return 1;
1276 /** Raise a client on top of others which are on the same layer.
1277 * \param L The Lua VM state.
1278 * \luastack
1279 * \lvalue A client.
1281 static int
1282 luaA_client_raise(lua_State *L)
1284 client_t *c = luaA_client_checkudata(L, 1);
1285 client_raise(c);
1286 return 0;
1289 /** Lower a client on bottom of others which are on the same layer.
1290 * \param L The Lua VM state.
1291 * \luastack
1292 * \lvalue A client.
1294 static int
1295 luaA_client_lower(lua_State *L)
1297 client_t *c = luaA_client_checkudata(L, 1);
1298 client_lower(c);
1299 return 0;
1302 /** Redraw a client by unmapping and mapping it quickly.
1303 * \param L The Lua VM state.
1305 * \luastack
1306 * \lvalue A client.
1308 static int
1309 luaA_client_redraw(lua_State *L)
1311 client_t *c = luaA_client_checkudata(L, 1);
1313 xcb_unmap_window(globalconf.connection, c->win);
1314 xcb_map_window(globalconf.connection, c->win);
1316 /* Set the focus on the current window if the redraw has been
1317 performed on the window where the pointer is currently on
1318 because after the unmapping/mapping, the focus is lost */
1319 if(globalconf.screen_focus->client_focus == c)
1321 client_unfocus(c);
1322 client_focus(c);
1325 return 0;
1328 /** Stop managing a client.
1329 * \param L The Lua VM state.
1330 * \return The number of elements pushed on stack.
1331 * \luastack
1332 * \lvalue A client.
1334 static int
1335 luaA_client_unmanage(lua_State *L)
1337 client_t *c = luaA_client_checkudata(L, 1);
1338 client_unmanage(c);
1339 return 0;
1342 /** Return client geometry.
1343 * \param L The Lua VM state.
1344 * \return The number of elements pushed on stack.
1345 * \luastack
1346 * \lparam A table with new coordinates, or none.
1347 * \lreturn A table with client coordinates.
1349 static int
1350 luaA_client_geometry(lua_State *L)
1352 client_t *c = luaA_client_checkudata(L, 1);
1354 if(lua_gettop(L) == 2)
1356 area_t geometry;
1358 luaA_checktable(L, 2);
1359 geometry.x = luaA_getopt_number(L, 2, "x", c->geometry.x);
1360 geometry.y = luaA_getopt_number(L, 2, "y", c->geometry.y);
1361 if(client_isfixed(c))
1363 geometry.width = c->geometry.width;
1364 geometry.height = c->geometry.height;
1366 else
1368 geometry.width = luaA_getopt_number(L, 2, "width", c->geometry.width);
1369 geometry.height = luaA_getopt_number(L, 2, "height", c->geometry.height);
1372 client_resize(c, geometry, c->size_hints_honor);
1375 return luaA_pusharea(L, c->geometry);
1378 /** Push a strut type to a table on stack.
1379 * \param L The Lua VM state.
1380 * \param struts The struts to push.
1381 * \return The number of elements pushed on stack.
1383 static inline int
1384 luaA_pushstruts(lua_State *L, strut_t struts)
1386 lua_createtable(L, 4, 0);
1387 lua_pushnumber(L, struts.left);
1388 lua_setfield(L, -2, "left");
1389 lua_pushnumber(L, struts.right);
1390 lua_setfield(L, -2, "right");
1391 lua_pushnumber(L, struts.top);
1392 lua_setfield(L, -2, "top");
1393 lua_pushnumber(L, struts.bottom);
1394 lua_setfield(L, -2, "bottom");
1395 return 1;
1398 /** Return client struts (reserved space at the edge of the screen).
1399 * \param L The Lua VM state.
1400 * \return The number of elements pushed on stack.
1401 * \luastack
1402 * \lparam A table with new strut values, or none.
1403 * \lreturn A table with strut values.
1405 static int
1406 luaA_client_struts(lua_State *L)
1408 client_t *c = luaA_client_checkudata(L, 1);
1410 if(lua_gettop(L) == 2)
1412 strut_t struts;
1413 area_t screen_area = display_area_get(c->phys_screen);
1415 struts.left = luaA_getopt_number(L, 2, "left", c->strut.left);
1416 struts.right = luaA_getopt_number(L, 2, "right", c->strut.right);
1417 struts.top = luaA_getopt_number(L, 2, "top", c->strut.top);
1418 struts.bottom = luaA_getopt_number(L, 2, "bottom", c->strut.bottom);
1420 if(struts.left != c->strut.left || struts.right != c->strut.right ||
1421 struts.top != c->strut.top || struts.bottom != c->strut.bottom) {
1422 /* Struts are not so well defined in the context of xinerama. So we just
1423 * give the entire root window and let the window manager decide. */
1424 struts.left_start_y = 0;
1425 struts.left_end_y = !struts.left ? 0 : screen_area.height;
1426 struts.right_start_y = 0;
1427 struts.right_end_y = !struts.right ? 0 : screen_area.height;
1428 struts.top_start_x = 0;
1429 struts.top_end_x = !struts.top ? 0 : screen_area.width;
1430 struts.bottom_start_x = 0;
1431 struts.bottom_end_x = !struts.bottom ? 0 : screen_area.width;
1433 c->strut = struts;
1435 ewmh_update_client_strut(c);
1437 hook_property(client, c, "struts");
1441 return luaA_pushstruts(L, c->strut);
1444 /** Client newindex.
1445 * \param L The Lua VM state.
1446 * \return The number of elements pushed on stack.
1448 static int
1449 luaA_client_newindex(lua_State *L)
1451 size_t len;
1452 client_t *c = luaA_client_checkudata(L, 1);
1453 const char *buf = luaL_checklstring(L, 2, &len);
1454 bool b;
1455 double d;
1456 int i;
1458 switch(a_tokenize(buf, len))
1460 case A_TK_SCREEN:
1461 if(globalconf.xinerama_is_active)
1463 i = luaL_checknumber(L, 3) - 1;
1464 luaA_checkscreen(i);
1465 screen_client_moveto(c, &globalconf.screens.tab[i], true, true);
1467 break;
1468 case A_TK_HIDE:
1469 b = luaA_checkboolean(L, 3);
1470 if(b != c->ishidden)
1472 client_need_arrange(c);
1473 c->ishidden = b;
1474 client_need_arrange(c);
1475 hook_property(client, c, "hide");
1477 break;
1478 case A_TK_MINIMIZED:
1479 client_setminimized(c, luaA_checkboolean(L, 3));
1480 break;
1481 case A_TK_FULLSCREEN:
1482 client_setfullscreen(c, luaA_checkboolean(L, 3));
1483 break;
1484 case A_TK_MAXIMIZED_HORIZONTAL:
1485 client_setmaxhoriz(c, luaA_checkboolean(L, 3));
1486 break;
1487 case A_TK_MAXIMIZED_VERTICAL:
1488 client_setmaxvert(c, luaA_checkboolean(L, 3));
1489 break;
1490 case A_TK_ICON:
1491 image_unref(L, c->icon);
1492 c->icon = image_ref(L);
1493 /* execute hook */
1494 hook_property(client, c, "icon");
1495 break;
1496 case A_TK_OPACITY:
1497 if(lua_isnil(L, 3))
1498 window_opacity_set(c->win, -1);
1499 else
1501 d = luaL_checknumber(L, 3);
1502 if(d >= 0 && d <= 1)
1503 window_opacity_set(c->win, d);
1505 break;
1506 case A_TK_STICKY:
1507 client_setsticky(c, luaA_checkboolean(L, 3));
1508 break;
1509 case A_TK_SIZE_HINTS_HONOR:
1510 c->size_hints_honor = luaA_checkboolean(L, 3);
1511 hook_property(client, c, "size_hints_honor");
1512 break;
1513 case A_TK_BORDER_WIDTH:
1514 client_setborder(c, luaL_checknumber(L, 3));
1515 break;
1516 case A_TK_ONTOP:
1517 client_setontop(c, luaA_checkboolean(L, 3));
1518 break;
1519 case A_TK_ABOVE:
1520 client_setabove(c, luaA_checkboolean(L, 3));
1521 break;
1522 case A_TK_BELOW:
1523 client_setbelow(c, luaA_checkboolean(L, 3));
1524 break;
1525 case A_TK_URGENT:
1526 client_seturgent(c, luaA_checkboolean(L, 3));
1527 break;
1528 case A_TK_BORDER_COLOR:
1529 if((buf = luaL_checklstring(L, 3, &len))
1530 && xcolor_init_reply(xcolor_init_unchecked(&c->border_color, buf, len)))
1531 xcb_change_window_attributes(globalconf.connection, c->win,
1532 XCB_CW_BORDER_PIXEL, &c->border_color.pixel);
1533 break;
1534 case A_TK_TITLEBAR:
1535 if(lua_isnil(L, 3))
1536 titlebar_client_detach(c);
1537 else
1538 titlebar_client_attach(c);
1539 break;
1540 case A_TK_SKIP_TASKBAR:
1541 c->skiptb = luaA_checkboolean(L, 3);
1542 hook_property(client, c, "skip_taskbar");
1543 break;
1544 default:
1545 return 0;
1548 return 0;
1551 /** Client object.
1552 * \param L The Lua VM state.
1553 * \return The number of elements pushed on stack.
1554 * \luastack
1555 * \lfield id The window X id.
1556 * \lfield name The client title.
1557 * \lfield skip_taskbar If true the client won't be shown in the tasklist.
1558 * \lfield type The window type (desktop, normal, dock, …).
1559 * \lfield class The client class.
1560 * \lfield instance The client instance.
1561 * \lfield pid The client PID, if available.
1562 * \lfield role The window role, if available.
1563 * \lfield machine The machine client is running on.
1564 * \lfield icon_name The client name when iconified.
1565 * \lfield screen Client screen number.
1566 * \lfield hide Define if the client must be hidden, i.e. never mapped,
1567 * invisible in taskbar.
1568 * \lfield minimized Define it the client must be iconify, i.e. only visible in
1569 * taskbar.
1570 * \lfield size_hints_honor Honor size hints, i.e. respect size ratio.
1571 * \lfield border_width The client border width.
1572 * \lfield border_color The client border color.
1573 * \lfield titlebar The client titlebar.
1574 * \lfield urgent The client urgent state.
1575 * \lfield content An image representing the client window content (screenshot).
1576 * \lfield focus The focused client.
1577 * \lfield opacity The client opacity between 0 and 1.
1578 * \lfield ontop The client is on top of every other windows.
1579 * \lfield above The client is above normal windows.
1580 * \lfield below The client is below normal windows.
1581 * \lfield fullscreen The client is fullscreen or not.
1582 * \lfield maximized_horizontal The client is maximized horizontally or not.
1583 * \lfield maximized_vertical The client is maximized vertically or not.
1584 * \lfield transient_for Return the client the window is transient for.
1585 * \lfield group_id Identification unique to a group of windows.
1586 * \lfield leader_id Identification unique to windows spawned by the same command.
1587 * \lfield size_hints A table with size hints of the client: user_position,
1588 * user_size, program_position, program_size, etc.
1590 static int
1591 luaA_client_index(lua_State *L)
1593 size_t len;
1594 ssize_t slen;
1595 client_t *c = luaA_client_checkudata(L, 1);
1596 const char *buf = luaL_checklstring(L, 2, &len);
1597 char *value;
1598 void *data;
1599 xcb_get_property_cookie_t prop_c;
1600 xcb_get_property_reply_t *prop_r = NULL;
1601 double d;
1603 if(luaA_usemetatable(L, 1, 2))
1604 return 1;
1606 switch(a_tokenize(buf, len))
1608 case A_TK_NAME:
1609 lua_pushstring(L, c->name);
1610 break;
1611 case A_TK_TRANSIENT_FOR:
1612 return client_push(globalconf.L, c->transient_for);
1613 case A_TK_SKIP_TASKBAR:
1614 lua_pushboolean(L, c->skiptb);
1615 break;
1616 case A_TK_CONTENT:
1617 return client_getcontent(c);
1618 case A_TK_TYPE:
1619 switch(c->type)
1621 case WINDOW_TYPE_DESKTOP:
1622 lua_pushliteral(L, "desktop");
1623 break;
1624 case WINDOW_TYPE_DOCK:
1625 lua_pushliteral(L, "dock");
1626 break;
1627 case WINDOW_TYPE_SPLASH:
1628 lua_pushliteral(L, "splash");
1629 break;
1630 case WINDOW_TYPE_DIALOG:
1631 lua_pushliteral(L, "dialog");
1632 break;
1633 case WINDOW_TYPE_MENU:
1634 lua_pushliteral(L, "menu");
1635 break;
1636 case WINDOW_TYPE_TOOLBAR:
1637 lua_pushliteral(L, "toolbar");
1638 break;
1639 case WINDOW_TYPE_UTILITY:
1640 lua_pushliteral(L, "utility");
1641 break;
1642 case WINDOW_TYPE_DROPDOWN_MENU:
1643 lua_pushliteral(L, "dropdown_menu");
1644 break;
1645 case WINDOW_TYPE_POPUP_MENU:
1646 lua_pushliteral(L, "popup_menu");
1647 break;
1648 case WINDOW_TYPE_TOOLTIP:
1649 lua_pushliteral(L, "tooltip");
1650 break;
1651 case WINDOW_TYPE_NOTIFICATION:
1652 lua_pushliteral(L, "notification");
1653 break;
1654 case WINDOW_TYPE_COMBO:
1655 lua_pushliteral(L, "combo");
1656 break;
1657 case WINDOW_TYPE_DND:
1658 lua_pushliteral(L, "dnd");
1659 break;
1660 case WINDOW_TYPE_NORMAL:
1661 lua_pushliteral(L, "normal");
1662 break;
1664 break;
1665 case A_TK_CLASS:
1666 lua_pushstring(L, c->class);
1667 break;
1668 case A_TK_INSTANCE:
1669 lua_pushstring(L, c->instance);
1670 break;
1671 case A_TK_ROLE:
1672 if(!xutil_text_prop_get(globalconf.connection, c->win,
1673 WM_WINDOW_ROLE, &value, &slen))
1674 return 0;
1675 lua_pushlstring(L, value, slen);
1676 p_delete(&value);
1677 break;
1678 case A_TK_PID:
1679 prop_c = xcb_get_property_unchecked(globalconf.connection, false, c->win, _NET_WM_PID, CARDINAL, 0L, 1L);
1680 prop_r = xcb_get_property_reply(globalconf.connection, prop_c, NULL);
1682 if(prop_r && prop_r->value_len && (data = xcb_get_property_value(prop_r)))
1683 lua_pushnumber(L, *(uint32_t *)data);
1684 else
1686 p_delete(&prop_r);
1687 return 0;
1689 break;
1690 case A_TK_ID:
1691 lua_pushnumber(L, c->win);
1692 break;
1693 case A_TK_LEADER_ID:
1694 lua_pushnumber(L, c->leader_win);
1695 break;
1696 case A_TK_MACHINE:
1697 if(!xutil_text_prop_get(globalconf.connection, c->win,
1698 WM_CLIENT_MACHINE, &value, &slen))
1699 return 0;
1700 lua_pushlstring(L, value, slen);
1701 p_delete(&value);
1702 break;
1703 case A_TK_ICON_NAME:
1704 lua_pushstring(L, c->icon_name);
1705 break;
1706 case A_TK_SCREEN:
1707 lua_pushnumber(L, 1 + screen_array_indexof(&globalconf.screens, c->screen));
1708 break;
1709 case A_TK_HIDE:
1710 lua_pushboolean(L, c->ishidden);
1711 break;
1712 case A_TK_MINIMIZED:
1713 lua_pushboolean(L, c->isminimized);
1714 break;
1715 case A_TK_FULLSCREEN:
1716 lua_pushboolean(L, c->isfullscreen);
1717 break;
1718 case A_TK_GROUP_ID:
1719 if(c->group_win)
1720 lua_pushnumber(L, c->group_win);
1721 else
1722 return 0;
1723 break;
1724 case A_TK_MAXIMIZED_HORIZONTAL:
1725 lua_pushboolean(L, c->ismaxhoriz);
1726 break;
1727 case A_TK_MAXIMIZED_VERTICAL:
1728 lua_pushboolean(L, c->ismaxvert);
1729 break;
1730 case A_TK_ICON:
1731 image_push(L, c->icon);
1732 break;
1733 case A_TK_OPACITY:
1734 if((d = window_opacity_get(c->win)) >= 0)
1735 lua_pushnumber(L, d);
1736 else
1737 return 0;
1738 break;
1739 case A_TK_ONTOP:
1740 lua_pushboolean(L, c->isontop);
1741 break;
1742 case A_TK_ABOVE:
1743 lua_pushboolean(L, c->isabove);
1744 break;
1745 case A_TK_BELOW:
1746 lua_pushboolean(L, c->isbelow);
1747 break;
1748 case A_TK_STICKY:
1749 lua_pushboolean(L, c->issticky);
1750 break;
1751 case A_TK_SIZE_HINTS_HONOR:
1752 lua_pushboolean(L, c->size_hints_honor);
1753 break;
1754 case A_TK_BORDER_WIDTH:
1755 lua_pushnumber(L, c->border);
1756 break;
1757 case A_TK_BORDER_COLOR:
1758 luaA_pushxcolor(L, &c->border_color);
1759 break;
1760 case A_TK_TITLEBAR:
1761 return wibox_push(L, c->titlebar);
1762 case A_TK_URGENT:
1763 lua_pushboolean(L, c->isurgent);
1764 break;
1765 case A_TK_SIZE_HINTS:
1767 const char *u_or_p = NULL;
1769 lua_createtable(L, 0, 1);
1771 if(c->size_hints.flags & XCB_SIZE_HINT_US_POSITION)
1772 u_or_p = "user_position";
1773 else if(c->size_hints.flags & XCB_SIZE_HINT_P_POSITION)
1774 u_or_p = "program_position";
1776 if(u_or_p)
1778 lua_createtable(L, 0, 2);
1779 lua_pushnumber(L, c->size_hints.x);
1780 lua_setfield(L, -2, "x");
1781 lua_pushnumber(L, c->size_hints.y);
1782 lua_setfield(L, -2, "y");
1783 lua_setfield(L, -2, u_or_p);
1784 u_or_p = NULL;
1787 if(c->size_hints.flags & XCB_SIZE_HINT_US_SIZE)
1788 u_or_p = "user_size";
1789 else if(c->size_hints.flags & XCB_SIZE_HINT_P_SIZE)
1790 u_or_p = "program_size";
1792 if(u_or_p)
1794 lua_createtable(L, 0, 2);
1795 lua_pushnumber(L, c->size_hints.width);
1796 lua_setfield(L, -2, "width");
1797 lua_pushnumber(L, c->size_hints.height);
1798 lua_setfield(L, -2, "height");
1799 lua_setfield(L, -2, u_or_p);
1802 if(c->size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE)
1804 lua_pushnumber(L, c->size_hints.min_width);
1805 lua_setfield(L, -2, "min_width");
1806 lua_pushnumber(L, c->size_hints.min_height);
1807 lua_setfield(L, -2, "min_height");
1810 if(c->size_hints.flags & XCB_SIZE_HINT_P_MAX_SIZE)
1812 lua_pushnumber(L, c->size_hints.max_width);
1813 lua_setfield(L, -2, "max_width");
1814 lua_pushnumber(L, c->size_hints.max_height);
1815 lua_setfield(L, -2, "max_height");
1818 if(c->size_hints.flags & XCB_SIZE_HINT_P_RESIZE_INC)
1820 lua_pushnumber(L, c->size_hints.width_inc);
1821 lua_setfield(L, -2, "width_inc");
1822 lua_pushnumber(L, c->size_hints.height_inc);
1823 lua_setfield(L, -2, "height_inc");
1826 if(c->size_hints.flags & XCB_SIZE_HINT_P_ASPECT)
1828 lua_pushnumber(L, c->size_hints.min_aspect_num);
1829 lua_setfield(L, -2, "min_aspect_num");
1830 lua_pushnumber(L, c->size_hints.min_aspect_den);
1831 lua_setfield(L, -2, "min_aspect_den");
1832 lua_pushnumber(L, c->size_hints.max_aspect_num);
1833 lua_setfield(L, -2, "max_aspect_num");
1834 lua_pushnumber(L, c->size_hints.max_aspect_den);
1835 lua_setfield(L, -2, "max_aspect_den");
1838 if(c->size_hints.flags & XCB_SIZE_HINT_BASE_SIZE)
1840 lua_pushnumber(L, c->size_hints.base_width);
1841 lua_setfield(L, -2, "base_width");
1842 lua_pushnumber(L, c->size_hints.base_height);
1843 lua_setfield(L, -2, "base_height");
1846 if(c->size_hints.flags & XCB_SIZE_HINT_P_WIN_GRAVITY)
1848 switch(c->size_hints.win_gravity)
1850 default:
1851 lua_pushliteral(L, "north_west");
1852 break;
1853 case XCB_GRAVITY_NORTH:
1854 lua_pushliteral(L, "north");
1855 break;
1856 case XCB_GRAVITY_NORTH_EAST:
1857 lua_pushliteral(L, "north_east");
1858 break;
1859 case XCB_GRAVITY_WEST:
1860 lua_pushliteral(L, "west");
1861 break;
1862 case XCB_GRAVITY_CENTER:
1863 lua_pushliteral(L, "center");
1864 break;
1865 case XCB_GRAVITY_EAST:
1866 lua_pushliteral(L, "east");
1867 break;
1868 case XCB_GRAVITY_SOUTH_WEST:
1869 lua_pushliteral(L, "south_west");
1870 break;
1871 case XCB_GRAVITY_SOUTH:
1872 lua_pushliteral(L, "south");
1873 break;
1874 case XCB_GRAVITY_SOUTH_EAST:
1875 lua_pushliteral(L, "south_east");
1876 break;
1877 case XCB_GRAVITY_STATIC:
1878 lua_pushliteral(L, "static");
1879 break;
1881 lua_setfield(L, -2, "win_gravity");
1884 break;
1885 default:
1886 return 0;
1889 return 1;
1892 /** Get or set mouse buttons bindings for a client.
1893 * \param L The Lua VM state.
1894 * \return The number of element pushed on stack.
1895 * \luastack
1896 * \lvalue A client.
1897 * \lparam An array of mouse button bindings objects, or nothing.
1898 * \return The array of mouse button bindings objects of this client.
1900 static int
1901 luaA_client_buttons(lua_State *L)
1903 client_t *client = luaA_client_checkudata(L, 1);
1904 button_array_t *buttons = &client->buttons;
1906 if(lua_gettop(L) == 2)
1907 luaA_button_array_set(L, 2, buttons);
1909 window_buttons_grab(client->win, &client->buttons);
1911 return luaA_button_array_get(L, buttons);
1914 /** Get or set keys bindings for a client.
1915 * \param L The Lua VM state.
1916 * \return The number of element pushed on stack.
1917 * \luastack
1918 * \lvalue A client.
1919 * \lparam An array of key bindings objects, or nothing.
1920 * \return The array of key bindings objects of this client.
1922 static int
1923 luaA_client_keys(lua_State *L)
1925 client_t *c = luaA_client_checkudata(L, 1);
1926 key_array_t *keys = &c->keys;
1928 if(lua_gettop(L) == 2)
1930 luaA_key_array_set(L, 2, keys);
1931 xcb_ungrab_key(globalconf.connection, XCB_GRAB_ANY, c->win, XCB_BUTTON_MASK_ANY);
1932 window_grabkeys(c->win, keys);
1935 return luaA_key_array_get(L, keys);
1938 /* Client module.
1939 * \param L The Lua VM state.
1940 * \return The number of pushed elements.
1942 static int
1943 luaA_client_module_index(lua_State *L)
1945 size_t len;
1946 const char *buf = luaL_checklstring(L, 2, &len);
1948 switch(a_tokenize(buf, len))
1950 case A_TK_FOCUS:
1951 return client_push(globalconf.L, globalconf.screen_focus->client_focus);
1952 break;
1953 default:
1954 return 0;
1958 /* Client module new index.
1959 * \param L The Lua VM state.
1960 * \return The number of pushed elements.
1962 static int
1963 luaA_client_module_newindex(lua_State *L)
1965 size_t len;
1966 const char *buf = luaL_checklstring(L, 2, &len);
1967 client_t *c;
1969 switch(a_tokenize(buf, len))
1971 case A_TK_FOCUS:
1972 c = luaA_client_checkudata(L, 3);
1973 client_focus(c);
1974 break;
1975 default:
1976 break;
1979 return 0;
1982 const struct luaL_reg awesome_client_methods[] =
1984 { "get", luaA_client_get },
1985 { "__index", luaA_client_module_index },
1986 { "__newindex", luaA_client_module_newindex },
1987 { NULL, NULL }
1989 const struct luaL_reg awesome_client_meta[] =
1991 { "isvisible", luaA_client_isvisible },
1992 { "geometry", luaA_client_geometry },
1993 { "struts", luaA_client_struts },
1994 { "buttons", luaA_client_buttons },
1995 { "keys", luaA_client_keys },
1996 { "tags", luaA_client_tags },
1997 { "kill", luaA_client_kill },
1998 { "swap", luaA_client_swap },
1999 { "raise", luaA_client_raise },
2000 { "lower", luaA_client_lower },
2001 { "redraw", luaA_client_redraw },
2002 { "unmanage", luaA_client_unmanage },
2003 { "__index", luaA_client_index },
2004 { "__newindex", luaA_client_newindex },
2005 { "__gc", luaA_client_gc },
2006 { "__tostring", luaA_client_tostring },
2007 { NULL, NULL }
2010 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80