client: remove transient_for intelligence in manage
[awesome.git] / client.c
blob236c0d16ccbe846141c55afbbc948accab6281c8
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 client_t *
36 luaA_client_checkudata(lua_State *L, int ud)
38 client_t *c = luaL_checkudata(L, ud, "client");
39 if(c->invalid)
40 luaL_error(L, "client is invalid\n");
41 return c;
44 /** Collect a client.
45 * \param L The Lua VM state.
46 * \return The number of element pushed on stack.
48 static int
49 luaA_client_gc(lua_State *L)
51 client_t *c = luaL_checkudata(L, 1, "client");
52 xcb_get_wm_protocols_reply_wipe(&c->protocols);
53 p_delete(&c->class);
54 p_delete(&c->startup_id);
55 p_delete(&c->instance);
56 p_delete(&c->icon_name);
57 p_delete(&c->name);
58 return luaA_object_gc(L);
61 /** Change the clients urgency flag.
62 * \param c The client
63 * \param urgent The new flag state
65 void
66 client_seturgent(client_t *c, bool urgent)
68 if(c->isurgent != urgent)
70 xcb_get_property_cookie_t hints =
71 xcb_get_wm_hints_unchecked(globalconf.connection, c->win);
73 c->isurgent = urgent;
74 ewmh_client_update_hints(c);
76 /* update ICCCM hints */
77 xcb_wm_hints_t wmh;
78 xcb_get_wm_hints_reply(globalconf.connection, hints, &wmh, NULL);
80 if(urgent)
81 wmh.flags |= XCB_WM_HINT_X_URGENCY;
82 else
83 wmh.flags &= ~XCB_WM_HINT_X_URGENCY;
85 xcb_set_wm_hints(globalconf.connection, c->win, &wmh);
87 hook_property(client, c, "urgent");
91 /** Returns true if a client is tagged
92 * with one of the tags of the specified screen.
93 * \param c The client to check.
94 * \param screen Virtual screen.
95 * \return true if the client is visible, false otherwise.
97 bool
98 client_maybevisible(client_t *c, screen_t *screen)
100 if(c->screen == screen)
102 if(c->issticky || c->type == WINDOW_TYPE_DESKTOP)
103 return true;
105 foreach(tag, screen->tags)
106 if((*tag)->selected && is_client_tagged(c, *tag))
107 return true;
109 return false;
112 /** Return the content of a client as an image.
113 * That's just take a screenshot.
114 * \param c The client.
115 * \return 1 if the image has been pushed on stack, false otherwise.
117 static int
118 client_getcontent(client_t *c)
120 xcb_image_t *ximage = xcb_image_get(globalconf.connection,
121 c->win,
122 0, 0,
123 c->geometries.internal.width,
124 c->geometries.internal.height,
125 ~0, XCB_IMAGE_FORMAT_Z_PIXMAP);
126 int retval = 0;
128 if(ximage)
130 if(ximage->bpp >= 24)
132 uint32_t *data = p_alloca(uint32_t, ximage->width * ximage->height);
134 for(int y = 0; y < ximage->height; y++)
135 for(int x = 0; x < ximage->width; x++)
137 data[y * ximage->width + x] = xcb_image_get_pixel(ximage, x, y);
138 data[y * ximage->width + x] |= 0xff000000; /* set alpha to 0xff */
141 retval = image_new_from_argb32(ximage->width, ximage->height, data);
143 xcb_image_destroy(ximage);
146 return retval;
149 /** Get a client by its window.
150 * \param w The client window to find.
151 * \return A client pointer if found, NULL otherwise.
153 client_t *
154 client_getbywin(xcb_window_t w)
156 foreach(c, globalconf.clients)
157 if((*c)->win == w)
158 return *c;
160 return NULL;
163 /** Record that a client lost focus.
164 * \param c Client being unfocused
166 void
167 client_unfocus_update(client_t *c)
169 globalconf.screens.tab[c->phys_screen].client_focus = NULL;
170 ewmh_update_net_active_window(c->phys_screen);
172 /* Call hook */
173 if(globalconf.hooks.unfocus != LUA_REFNIL)
175 client_push(globalconf.L, c);
176 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.unfocus, 1, 0);
181 /** Unfocus a client.
182 * \param c The client.
184 void
185 client_unfocus(client_t *c)
188 xcb_window_t root_win = xutil_screen_get(globalconf.connection, c->phys_screen)->root;
189 /* Set focus on root window, so no events leak to the current window.
190 * This kind of inlines client_setfocus(), but a root window will never have
191 * the WM_TAKE_FOCUS protocol.
193 xcb_set_input_focus(globalconf.connection, XCB_INPUT_FOCUS_PARENT,
194 root_win, XCB_CURRENT_TIME);
196 client_unfocus_update(c);
199 /** Check if client supports protocol a protocole in WM_PROTOCOL.
200 * \param c The client.
201 * \param atom The protocol atom to check for.
202 * \return True if client has the atom in protocol, false otherwise.
204 bool
205 client_hasproto(client_t *c, xcb_atom_t atom)
207 uint32_t i;
209 for(i = 0; i < c->protocols.atoms_len; i++)
210 if(c->protocols.atoms[i] == atom)
211 return true;
212 return false;
215 /** Sets focus on window - using xcb_set_input_focus or WM_TAKE_FOCUS
216 * \param c Client that should get focus
217 * \param set_input_focus Should we call xcb_set_input_focus
219 void
220 client_setfocus(client_t *c, bool set_input_focus)
222 bool takefocus = client_hasproto(c, WM_TAKE_FOCUS);
223 if(set_input_focus)
224 xcb_set_input_focus(globalconf.connection, XCB_INPUT_FOCUS_PARENT,
225 c->win, XCB_CURRENT_TIME);
226 if(takefocus)
227 window_takefocus(c->win);
230 /** Ban client and move it out of the viewport.
231 * \param c The client.
233 void
234 client_ban(client_t *c)
236 if(!c->isbanned)
238 xcb_unmap_window(globalconf.connection, c->win);
240 c->isbanned = true;
242 if(globalconf.screens.tab[c->phys_screen].prev_client_focus == c)
243 globalconf.screens.tab[c->phys_screen].prev_client_focus = NULL;
245 /* Wait until the last moment to take away the focus from the window. */
246 if(globalconf.screens.tab[c->phys_screen].client_focus == c)
247 client_unfocus(c);
251 /** This is part of The Bob Marley Algorithmm: we ignore enter and leave window
252 * in certain cases, like map/unmap or move, so we don't get spurious events.
254 void
255 client_ignore_enterleave_events(void)
257 foreach(c, globalconf.clients)
258 xcb_change_window_attributes(globalconf.connection,
259 (*c)->win,
260 XCB_CW_EVENT_MASK,
261 (const uint32_t []) { CLIENT_SELECT_INPUT_EVENT_MASK & ~(XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_LEAVE_WINDOW) });
264 void
265 client_restore_enterleave_events(void)
267 foreach(c, globalconf.clients)
268 xcb_change_window_attributes(globalconf.connection,
269 (*c)->win,
270 XCB_CW_EVENT_MASK,
271 (const uint32_t []) { CLIENT_SELECT_INPUT_EVENT_MASK });
274 /** Record that a client got focus.
275 * \param c Client being focused.
277 void
278 client_focus_update(client_t *c)
280 if(!client_maybevisible(c, c->screen))
282 /* Focus previously focused client */
283 client_focus(globalconf.screen_focus->prev_client_focus);
284 return;
287 if(globalconf.screen_focus
288 && globalconf.screen_focus->client_focus)
290 if (globalconf.screen_focus->client_focus != c)
291 client_unfocus_update(globalconf.screen_focus->client_focus);
292 else
293 /* Already focused */
294 return;
296 /* stop hiding client */
297 c->ishidden = false;
298 client_setminimized(c, false);
300 /* unban the client before focusing for consistency */
301 client_unban(c);
303 globalconf.screen_focus = &globalconf.screens.tab[c->phys_screen];
304 globalconf.screen_focus->prev_client_focus = c;
305 globalconf.screen_focus->client_focus = c;
307 /* according to EWMH, we have to remove the urgent state from a client */
308 client_seturgent(c, false);
310 ewmh_update_net_active_window(c->phys_screen);
312 /* execute hook */
313 if(globalconf.hooks.focus != LUA_REFNIL)
315 client_push(globalconf.L, c);
316 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.focus, 1, 0);
321 /** Give focus to client, or to first client if client is NULL.
322 * \param c The client or NULL.
324 void
325 client_focus(client_t *c)
327 /* We have to set focus on first client */
328 if(!c && globalconf.clients.len && !(c = globalconf.clients.tab[0]))
329 return;
331 if(!client_maybevisible(c, c->screen))
332 return;
334 if (!c->nofocus)
335 client_focus_update(c);
337 client_setfocus(c, !c->nofocus);
340 /** Stack a window below.
341 * \param c The client.
342 * \param previous The previous window on the stack.
343 * \return The next-previous!
345 static xcb_window_t
346 client_stack_above(client_t *c, xcb_window_t previous)
348 uint32_t config_win_vals[2];
350 config_win_vals[0] = previous;
351 config_win_vals[1] = XCB_STACK_MODE_ABOVE;
353 xcb_configure_window(globalconf.connection, c->win,
354 XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE,
355 config_win_vals);
357 config_win_vals[0] = c->win;
359 if(c->titlebar)
361 xcb_configure_window(globalconf.connection,
362 c->titlebar->window,
363 XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE,
364 config_win_vals);
365 previous = c->titlebar->window;
367 else
368 previous = c->win;
370 /* stack transient window on top of their parents */
371 foreach(node, globalconf.stack)
372 if((*node)->transient_for == c)
373 previous = client_stack_above(*node, previous);
375 return previous;
378 /** Stacking layout layers */
379 typedef enum
381 /** This one is a special layer */
382 LAYER_IGNORE,
383 LAYER_DESKTOP,
384 LAYER_BELOW,
385 LAYER_NORMAL,
386 LAYER_ABOVE,
387 LAYER_FULLSCREEN,
388 LAYER_ONTOP,
389 /** This one only used for counting and is not a real layer */
390 LAYER_COUNT
391 } layer_t;
393 /** Get the real layer of a client according to its attribute (fullscreen, …)
394 * \param c The client.
395 * \return The real layer.
397 static layer_t
398 client_layer_translator(client_t *c)
400 /* first deal with user set attributes */
401 if(c->isontop)
402 return LAYER_ONTOP;
403 else if(c->isfullscreen)
404 return LAYER_FULLSCREEN;
405 else if(c->isabove)
406 return LAYER_ABOVE;
407 else if(c->isbelow)
408 return LAYER_BELOW;
410 /* check for transient attr */
411 if(c->transient_for)
412 return LAYER_IGNORE;
414 /* then deal with windows type */
415 switch(c->type)
417 case WINDOW_TYPE_DESKTOP:
418 return LAYER_DESKTOP;
419 default:
420 break;
423 return LAYER_NORMAL;
426 /** Restack clients.
427 * \todo It might be worth stopping to restack everyone and only stack `c'
428 * relatively to the first matching in the list.
430 void
431 client_stack_refresh()
433 uint32_t config_win_vals[2];
434 layer_t layer;
436 if (!globalconf.client_need_stack_refresh)
437 return;
438 globalconf.client_need_stack_refresh = false;
440 config_win_vals[0] = XCB_NONE;
441 config_win_vals[1] = XCB_STACK_MODE_ABOVE;
443 /* stack desktop windows */
444 for(layer = LAYER_DESKTOP; layer < LAYER_BELOW; layer++)
445 foreach(node, globalconf.stack)
446 if(client_layer_translator(*node) == layer)
447 config_win_vals[0] = client_stack_above(*node,
448 config_win_vals[0]);
450 /* first stack not ontop wibox window */
451 foreach(_sb, globalconf.wiboxes)
453 wibox_t *sb = *_sb;
454 if(!sb->ontop)
456 xcb_configure_window(globalconf.connection,
457 sb->window,
458 XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE,
459 config_win_vals);
460 config_win_vals[0] = sb->window;
464 /* then stack clients */
465 for(layer = LAYER_BELOW; layer < LAYER_COUNT; layer++)
466 foreach(node, globalconf.stack)
467 if(client_layer_translator(*node) == layer)
468 config_win_vals[0] = client_stack_above(*node,
469 config_win_vals[0]);
471 /* then stack ontop wibox window */
472 foreach(_sb, globalconf.wiboxes)
474 wibox_t *sb = *_sb;
475 if(sb->ontop)
477 xcb_configure_window(globalconf.connection,
478 sb->window,
479 XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE,
480 config_win_vals);
481 config_win_vals[0] = sb->window;
486 /** Manage a new client.
487 * \param w The window.
488 * \param wgeom Window geometry.
489 * \param phys_screen Physical screen number.
490 * \param startup True if we are managing at startup time.
492 void
493 client_manage(xcb_window_t w, xcb_get_geometry_reply_t *wgeom, int phys_screen, bool startup)
495 xcb_get_property_cookie_t ewmh_icon_cookie;
496 screen_t *screen;
497 const uint32_t select_input_val[] = { CLIENT_SELECT_INPUT_EVENT_MASK };
499 if(systray_iskdedockapp(w))
501 systray_request_handle(w, phys_screen, NULL);
502 return;
505 /* Send request to get NET_WM_ICON property as soon as possible... */
506 ewmh_icon_cookie = ewmh_window_icon_get_unchecked(w);
507 xcb_change_window_attributes(globalconf.connection, w, XCB_CW_EVENT_MASK, select_input_val);
509 client_t *c = client_new(globalconf.L);
510 /* Push client in client list */
511 client_array_push(&globalconf.clients, client_ref(globalconf.L, -1));
514 screen = c->screen = screen_getbycoord(&globalconf.screens.tab[phys_screen],
515 wgeom->x, wgeom->y);
517 c->phys_screen = phys_screen;
519 /* consider the window banned */
520 c->isbanned = true;
522 /* Initial values */
523 c->win = w;
524 c->geometry.x = wgeom->x;
525 c->geometry.y = wgeom->y;
526 /* Border will be added later. */
527 c->geometry.width = wgeom->width;
528 c->geometry.height = wgeom->height;
529 /* Also set internal geometry (client_ban() needs it). */
530 c->geometries.internal.x = wgeom->x;
531 c->geometries.internal.y = wgeom->y;
532 c->geometries.internal.width = wgeom->width;
533 c->geometries.internal.height = wgeom->height;
534 client_setborder(c, wgeom->border_width);
536 if(ewmh_window_icon_get_reply(ewmh_icon_cookie))
538 client_push(globalconf.L, c);
539 c->icon = luaA_object_ref_item(globalconf.L, -1, -2);
540 lua_pop(globalconf.L, 1);
543 /* we honor size hints by default */
544 c->size_hints_honor = true;
546 /* update hints */
547 property_update_wm_normal_hints(c, NULL);
548 property_update_wm_hints(c, NULL);
549 property_update_wm_transient_for(c, NULL);
550 property_update_wm_client_leader(c, NULL);
552 /* Then check clients hints */
553 ewmh_client_check_hints(c);
555 /* move client to screen, but do not tag it */
556 screen_client_moveto(c, screen, false, true);
558 /* Push client in stack */
559 client_raise(c);
561 /* update window title */
562 property_update_wm_name(c);
563 property_update_wm_icon_name(c);
564 property_update_wm_class(c, NULL);
565 property_update_wm_protocols(c);
567 xutil_text_prop_get(globalconf.connection, c->win, _NET_STARTUP_ID, &c->startup_id, NULL);
569 /* update strut */
570 ewmh_process_client_strut(c, NULL);
572 ewmh_update_net_client_list(c->phys_screen);
574 /* Always stay in NORMAL_STATE. Even though iconified seems more
575 * appropriate sometimes. The only possible loss is that clients not using
576 * visibility events may continue to proces data (when banned).
577 * Without any exposes or other events the cost should be fairly limited though.
579 * Some clients may expect the window to be unmapped when STATE_ICONIFIED.
580 * Two conflicting parts of the ICCCM v2.0 (section 4.1.4):
582 * "Normal -> Iconic - The client should send a ClientMessage event as described later in this section."
583 * (note no explicit mention of unmapping, while Normal->Widthdrawn does mention that)
585 * "Once a client's window has left the Withdrawn state, the window will be mapped
586 * if it is in the Normal state and the window will be unmapped if it is in the Iconic state."
588 * At this stage it's just safer to keep it in normal state and avoid confusion.
590 window_state_set(c->win, XCB_WM_STATE_NORMAL);
592 /* Grab everything */
593 xcb_grab_key(globalconf.connection, true, w,
594 XCB_MOD_MASK_ANY, XCB_GRAB_ANY,
595 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_SYNC);
597 xcb_grab_button(globalconf.connection, false, w,
598 XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE,
599 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, XCB_NONE, XCB_NONE,
600 XCB_GRAB_ANY, XCB_MOD_MASK_ANY);
603 if(!startup)
604 spawn_start_notify(c);
606 /* Call hook to notify list change */
607 if(globalconf.hooks.clients != LUA_REFNIL)
608 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.clients, 0, 0);
610 /* call hook */
611 if(globalconf.hooks.manage != LUA_REFNIL)
613 client_push(globalconf.L, c);
614 lua_pushboolean(globalconf.L, startup);
615 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.manage, 2, 0);
619 /** Compute client geometry with respect to its geometry hints.
620 * \param c The client.
621 * \param geometry The geometry that the client might receive.
622 * \return The geometry the client must take respecting its hints.
624 area_t
625 client_geometry_hints(client_t *c, area_t geometry)
627 int32_t basew, baseh, minw, minh;
629 /* base size is substituted with min size if not specified */
630 if(c->size_hints.flags & XCB_SIZE_HINT_P_SIZE)
632 basew = c->size_hints.base_width;
633 baseh = c->size_hints.base_height;
635 else if(c->size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE)
637 basew = c->size_hints.min_width;
638 baseh = c->size_hints.min_height;
640 else
641 basew = baseh = 0;
643 /* min size is substituted with base size if not specified */
644 if(c->size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE)
646 minw = c->size_hints.min_width;
647 minh = c->size_hints.min_height;
649 else if(c->size_hints.flags & XCB_SIZE_HINT_P_SIZE)
651 minw = c->size_hints.base_width;
652 minh = c->size_hints.base_height;
654 else
655 minw = minh = 0;
657 if(c->size_hints.flags & XCB_SIZE_HINT_P_ASPECT
658 && c->size_hints.min_aspect_num > 0
659 && c->size_hints.min_aspect_den > 0
660 && geometry.height - baseh > 0
661 && geometry.width - basew > 0)
663 double dx = (double) (geometry.width - basew);
664 double dy = (double) (geometry.height - baseh);
665 double min = (double) c->size_hints.min_aspect_num / (double) c->size_hints.min_aspect_den;
666 double max = (double) c->size_hints.max_aspect_num / (double) c->size_hints.min_aspect_den;
667 double ratio = dx / dy;
668 if(max > 0 && min > 0 && ratio > 0)
670 if(ratio < min)
672 dy = (dx * min + dy) / (min * min + 1);
673 dx = dy * min;
674 geometry.width = (int) dx + basew;
675 geometry.height = (int) dy + baseh;
677 else if(ratio > max)
679 dy = (dx * min + dy) / (max * max + 1);
680 dx = dy * min;
681 geometry.width = (int) dx + basew;
682 geometry.height = (int) dy + baseh;
687 if(minw)
688 geometry.width = MAX(geometry.width, minw);
689 if(minh)
690 geometry.height = MAX(geometry.height, minh);
692 if(c->size_hints.flags & XCB_SIZE_HINT_P_MAX_SIZE)
694 if(c->size_hints.max_width)
695 geometry.width = MIN(geometry.width, c->size_hints.max_width);
696 if(c->size_hints.max_height)
697 geometry.height = MIN(geometry.height, c->size_hints.max_height);
700 if(c->size_hints.flags & (XCB_SIZE_HINT_P_RESIZE_INC | XCB_SIZE_HINT_BASE_SIZE)
701 && c->size_hints.width_inc && c->size_hints.height_inc)
703 uint16_t t1 = geometry.width, t2 = geometry.height;
704 unsigned_subtract(t1, basew);
705 unsigned_subtract(t2, baseh);
706 geometry.width -= t1 % c->size_hints.width_inc;
707 geometry.height -= t2 % c->size_hints.height_inc;
710 return geometry;
713 /** Resize client window.
714 * The sizse given as parameters are with titlebar and borders!
715 * \param c Client to resize.
716 * \param geometry New window geometry.
717 * \param hints Use size hints.
718 * \return true if an actual resize occurred.
720 bool
721 client_resize(client_t *c, area_t geometry, bool hints)
723 area_t geometry_internal;
724 area_t area;
726 /* offscreen appearance fixes */
727 area = display_area_get(c->phys_screen);
729 if(geometry.x > area.width)
730 geometry.x = area.width - geometry.width;
731 if(geometry.y > area.height)
732 geometry.y = area.height - geometry.height;
733 if(geometry.x + geometry.width < 0)
734 geometry.x = 0;
735 if(geometry.y + geometry.height < 0)
736 geometry.y = 0;
738 /* Real client geometry, please keep it contained to C code at the very least. */
739 geometry_internal = titlebar_geometry_remove(c->titlebar, c->border, geometry);
741 if(hints)
742 geometry_internal = client_geometry_hints(c, geometry_internal);
744 if(geometry_internal.width == 0 || geometry_internal.height == 0)
745 return false;
747 /* Also let client hints propegate to the "official" geometry. */
748 geometry = titlebar_geometry_add(c->titlebar, c->border, geometry_internal);
750 if(c->geometries.internal.x != geometry_internal.x
751 || c->geometries.internal.y != geometry_internal.y
752 || c->geometries.internal.width != geometry_internal.width
753 || c->geometries.internal.height != geometry_internal.height)
755 screen_t *new_screen = screen_getbycoord(c->screen,
756 geometry_internal.x, geometry_internal.y);
758 /* Values to configure a window is an array where values are
759 * stored according to 'value_mask' */
760 uint32_t values[4];
762 c->geometries.internal.x = values[0] = geometry_internal.x;
763 c->geometries.internal.y = values[1] = geometry_internal.y;
764 c->geometries.internal.width = values[2] = geometry_internal.width;
765 c->geometries.internal.height = values[3] = geometry_internal.height;
767 /* Also store geometry including border and titlebar. */
768 c->geometry = geometry;
770 titlebar_update_geometry(c);
772 /* Ignore all spurious enter/leave notify events */
773 client_ignore_enterleave_events();
775 xcb_configure_window(globalconf.connection, c->win,
776 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y
777 | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
778 values);
780 client_restore_enterleave_events();
782 screen_client_moveto(c, new_screen, true, false);
784 /* execute hook */
785 hook_property(client, c, "geometry");
787 return true;
790 return false;
793 /** Set a client minimized, or not.
794 * \param c The client.
795 * \param s Set or not the client minimized.
797 void
798 client_setminimized(client_t *c, bool s)
800 if(c->isminimized != s)
802 client_need_reban(c);
803 c->isminimized = s;
804 client_need_reban(c);
805 if(s)
806 window_state_set(c->win, XCB_WM_STATE_ICONIC);
807 else
808 window_state_set(c->win, XCB_WM_STATE_NORMAL);
809 ewmh_client_update_hints(c);
810 /* execute hook */
811 hook_property(client, c, "minimized");
815 /** Set a client sticky, or not.
816 * \param c The client.
817 * \param s Set or not the client sticky.
819 void
820 client_setsticky(client_t *c, bool s)
822 if(c->issticky != s)
824 client_need_reban(c);
825 c->issticky = s;
826 client_need_reban(c);
827 ewmh_client_update_hints(c);
828 hook_property(client, c, "sticky");
832 /** Set a client fullscreen, or not.
833 * \param c The client.
834 * \param s Set or not the client fullscreen.
836 void
837 client_setfullscreen(client_t *c, bool s)
839 if(c->isfullscreen != s)
841 area_t geometry;
843 /* Make sure the current geometry is stored without titlebar. */
844 if(s)
845 titlebar_ban(c->titlebar);
847 /* become fullscreen! */
848 if((c->isfullscreen = s))
850 /* remove any max state */
851 client_setmaxhoriz(c, false);
852 client_setmaxvert(c, false);
853 /* You can only be part of one of the special layers. */
854 client_setbelow(c, false);
855 client_setabove(c, false);
856 client_setontop(c, false);
858 geometry = screen_area_get(c->screen, false);
859 c->geometries.fullscreen = c->geometry;
860 c->border_fs = c->border;
861 client_setborder(c, 0);
863 else
865 geometry = c->geometries.fullscreen;
866 client_setborder(c, c->border_fs);
868 client_resize(c, geometry, false);
869 client_stack();
870 ewmh_client_update_hints(c);
871 hook_property(client, c, "fullscreen");
875 /** Set a client horizontally maximized.
876 * \param c The client.
877 * \param s The maximized status.
879 void
880 client_setmaxhoriz(client_t *c, bool s)
882 if(c->ismaxhoriz != s)
884 area_t geometry;
886 if((c->ismaxhoriz = s))
888 /* remove fullscreen mode */
889 client_setfullscreen(c, false);
891 geometry = screen_area_get(c->screen, true);
892 geometry.y = c->geometry.y;
893 geometry.height = c->geometry.height;
894 c->geometries.max.x = c->geometry.x;
895 c->geometries.max.width = c->geometry.width;
897 else
899 geometry = c->geometry;
900 geometry.x = c->geometries.max.x;
901 geometry.width = c->geometries.max.width;
904 client_resize(c, geometry, c->size_hints_honor);
905 client_stack();
906 ewmh_client_update_hints(c);
907 hook_property(client, c, "maximized_horizontal");
911 /** Set a client vertically maximized.
912 * \param c The client.
913 * \param s The maximized status.
915 void
916 client_setmaxvert(client_t *c, bool s)
918 if(c->ismaxvert != s)
920 area_t geometry;
922 if((c->ismaxvert = s))
924 /* remove fullscreen mode */
925 client_setfullscreen(c, false);
927 geometry = screen_area_get(c->screen, true);
928 geometry.x = c->geometry.x;
929 geometry.width = c->geometry.width;
930 c->geometries.max.y = c->geometry.y;
931 c->geometries.max.height = c->geometry.height;
933 else
935 geometry = c->geometry;
936 geometry.y = c->geometries.max.y;
937 geometry.height = c->geometries.max.height;
940 client_resize(c, geometry, c->size_hints_honor);
941 client_stack();
942 ewmh_client_update_hints(c);
943 hook_property(client, c, "maximized_vertical");
947 /** Set a client above, or not.
948 * \param c The client.
949 * \param s Set or not the client above.
951 void
952 client_setabove(client_t *c, bool s)
954 if(c->isabove != s)
956 /* You can only be part of one of the special layers. */
957 if(s)
959 client_setbelow(c, false);
960 client_setontop(c, false);
961 client_setfullscreen(c, false);
963 c->isabove = s;
964 client_stack();
965 ewmh_client_update_hints(c);
966 /* execute hook */
967 hook_property(client, c, "above");
971 /** Set a client below, or not.
972 * \param c The client.
973 * \param s Set or not the client below.
975 void
976 client_setbelow(client_t *c, bool s)
978 if(c->isbelow != s)
980 /* You can only be part of one of the special layers. */
981 if(s)
983 client_setabove(c, false);
984 client_setontop(c, false);
985 client_setfullscreen(c, false);
987 c->isbelow = s;
988 client_stack();
989 ewmh_client_update_hints(c);
990 /* execute hook */
991 hook_property(client, c, "below");
995 /** Set a client modal, or not.
996 * \param c The client.
997 * \param s Set or not the client moda.
999 void
1000 client_setmodal(client_t *c, bool s)
1002 if(c->ismodal != s)
1004 c->ismodal = s;
1005 client_stack();
1006 ewmh_client_update_hints(c);
1007 /* execute hook */
1008 hook_property(client, c, "modal");
1012 /** Set a client ontop, or not.
1013 * \param c The client.
1014 * \param s Set or not the client moda.
1016 void
1017 client_setontop(client_t *c, bool s)
1019 if(c->isontop != s)
1021 /* You can only be part of one of the special layers. */
1022 if(s)
1024 client_setabove(c, false);
1025 client_setbelow(c, false);
1026 client_setfullscreen(c, false);
1028 c->isontop = s;
1029 client_stack();
1030 /* execute hook */
1031 hook_property(client, c, "ontop");
1035 /** Unban a client and move it back into the viewport.
1036 * \param c The client.
1038 void
1039 client_unban(client_t *c)
1041 if(c->isbanned)
1043 xcb_map_window(globalconf.connection, c->win);
1045 c->isbanned = false;
1049 /** Unmanage a client.
1050 * \param c The client.
1052 void
1053 client_unmanage(client_t *c)
1055 tag_array_t *tags = &c->screen->tags;
1057 /* Reset transient_for attributes of widows that maybe refering to us */
1058 foreach(_tc, globalconf.clients)
1060 client_t *tc = *_tc;
1061 if(tc->transient_for == c)
1062 tc->transient_for = NULL;
1065 if(globalconf.screens.tab[c->phys_screen].prev_client_focus == c)
1066 globalconf.screens.tab[c->phys_screen].prev_client_focus = NULL;
1068 if(globalconf.screens.tab[c->phys_screen].client_focus == c)
1069 client_unfocus(c);
1071 /* remove client from global list and everywhere else */
1072 foreach(elem, globalconf.clients)
1073 if(*elem == c)
1075 client_array_remove(&globalconf.clients, elem);
1076 break;
1078 stack_client_remove(c);
1079 for(int i = 0; i < tags->len; i++)
1080 untag_client(c, tags->tab[i]);
1082 /* call hook */
1083 if(globalconf.hooks.unmanage != LUA_REFNIL)
1085 client_push(globalconf.L, c);
1086 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.unmanage, 1, 0);
1089 /* Call hook to notify list change */
1090 if(globalconf.hooks.clients != LUA_REFNIL)
1091 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.clients, 0, 0);
1093 window_state_set(c->win, XCB_WM_STATE_WITHDRAWN);
1095 titlebar_client_detach(c);
1097 ewmh_update_net_client_list(c->phys_screen);
1099 /* set client as invalid */
1100 c->invalid = true;
1102 client_unref(globalconf.L, c);
1105 /** Kill a client via a WM_DELETE_WINDOW request or KillClient if not
1106 * supported.
1107 * \param c The client to kill.
1109 void
1110 client_kill(client_t *c)
1112 if(client_hasproto(c, WM_DELETE_WINDOW))
1114 xcb_client_message_event_t ev;
1116 /* Initialize all of event's fields first */
1117 p_clear(&ev, 1);
1119 ev.response_type = XCB_CLIENT_MESSAGE;
1120 ev.window = c->win;
1121 ev.format = 32;
1122 ev.data.data32[1] = XCB_CURRENT_TIME;
1123 ev.type = WM_PROTOCOLS;
1124 ev.data.data32[0] = WM_DELETE_WINDOW;
1126 xcb_send_event(globalconf.connection, false, c->win,
1127 XCB_EVENT_MASK_NO_EVENT, (char *) &ev);
1129 else
1130 xcb_kill_client(globalconf.connection, c->win);
1133 /** Get all clients into a table.
1134 * \param L The Lua VM state.
1135 * \return The number of elements pushed on stack.
1136 * \luastack
1137 * \lparam An optional screen nunmber.
1138 * \lreturn A table with all clients.
1140 static int
1141 luaA_client_get(lua_State *L)
1143 int i = 1, screen;
1145 screen = luaL_optnumber(L, 1, 0) - 1;
1147 lua_newtable(L);
1149 if(screen == -1)
1150 foreach(c, globalconf.clients)
1152 client_push(L, *c);
1153 lua_rawseti(L, -2, i++);
1155 else
1157 luaA_checkscreen(screen);
1158 foreach(c, globalconf.clients)
1159 if((*c)->screen == &globalconf.screens.tab[screen])
1161 client_push(L, *c);
1162 lua_rawseti(L, -2, i++);
1166 return 1;
1169 /** Check if a client is visible on its screen.
1170 * \param L The Lua VM state.
1171 * \return The number of elements pushed on stack.
1172 * \luastack
1173 * \lvalue A client.
1174 * \lreturn A boolean value, true if the client is visible, false otherwise.
1176 static int
1177 luaA_client_isvisible(lua_State *L)
1179 client_t *c = luaA_client_checkudata(L, 1);
1180 lua_pushboolean(L, client_isvisible(c, c->screen));
1181 return 1;
1184 /** Set client border width.
1185 * \param c The client.
1186 * \param width The border width.
1188 void
1189 client_setborder(client_t *c, int width)
1191 uint32_t w = width;
1193 if(width > 0 && (c->type == WINDOW_TYPE_DOCK
1194 || c->type == WINDOW_TYPE_SPLASH
1195 || c->type == WINDOW_TYPE_DESKTOP
1196 || c->isfullscreen))
1197 return;
1199 if(width == c->border || width < 0)
1200 return;
1202 /* Update geometry with the new border. */
1203 c->geometry.width -= 2 * c->border;
1204 c->geometry.height -= 2 * c->border;
1206 c->border = width;
1207 xcb_configure_window(globalconf.connection, c->win,
1208 XCB_CONFIG_WINDOW_BORDER_WIDTH, &w);
1210 c->geometry.width += 2 * c->border;
1211 c->geometry.height += 2 * c->border;
1213 /* Changing border size also affects the size of the titlebar. */
1214 titlebar_update_geometry(c);
1216 hook_property(client, c, "border_width");
1219 /** Kill a client.
1220 * \param L The Lua VM state.
1222 * \luastack
1223 * \lvalue A client.
1225 static int
1226 luaA_client_kill(lua_State *L)
1228 client_t *c = luaA_client_checkudata(L, 1);
1229 client_kill(c);
1230 return 0;
1233 /** Swap a client with another one.
1234 * \param L The Lua VM state.
1235 * \luastack
1236 * \lvalue A client.
1237 * \lparam A client to swap with.
1239 static int
1240 luaA_client_swap(lua_State *L)
1242 client_t *c = luaA_client_checkudata(L, 1);
1243 client_t *swap = luaA_client_checkudata(L, 2);
1245 if(c != swap)
1247 client_t **ref_c = NULL, **ref_swap = NULL;
1248 foreach(item, globalconf.clients)
1250 if(*item == c)
1251 ref_c = item;
1252 else if(*item == swap)
1253 ref_swap = item;
1254 if(ref_c && ref_swap)
1255 break;
1257 /* swap ! */
1258 *ref_c = swap;
1259 *ref_swap = c;
1261 /* Call hook to notify list change */
1262 if(globalconf.hooks.clients != LUA_REFNIL)
1263 luaA_dofunction_from_registry(L, globalconf.hooks.clients, 0, 0);
1266 return 0;
1269 /** Access or set the client tags.
1270 * \param L The Lua VM state.
1271 * \return The number of elements pushed on stack.
1272 * \lparam A table with tags to set, or none to get the current tags table.
1273 * \return The clients tag.
1275 static int
1276 luaA_client_tags(lua_State *L)
1278 client_t *c = luaA_client_checkudata(L, 1);
1279 tag_array_t *tags = &c->screen->tags;
1280 int j = 0;
1282 if(lua_gettop(L) == 2)
1284 luaA_checktable(L, 2);
1285 for(int i = 0; i < tags->len; i++)
1286 untag_client(c, tags->tab[i]);
1287 lua_pushnil(L);
1288 while(lua_next(L, 2))
1289 tag_client(c);
1290 lua_pop(L, 1);
1293 lua_newtable(L);
1294 foreach(tag, *tags)
1295 if(is_client_tagged(c, *tag))
1297 tag_push(L, *tag);
1298 lua_rawseti(L, -2, ++j);
1301 return 1;
1304 /** Raise a client on top of others which are on the same layer.
1305 * \param L The Lua VM state.
1306 * \luastack
1307 * \lvalue A client.
1309 static int
1310 luaA_client_raise(lua_State *L)
1312 client_t *c = luaA_client_checkudata(L, 1);
1313 client_raise(c);
1314 return 0;
1317 /** Lower a client on bottom of others which are on the same layer.
1318 * \param L The Lua VM state.
1319 * \luastack
1320 * \lvalue A client.
1322 static int
1323 luaA_client_lower(lua_State *L)
1325 client_t *c = luaA_client_checkudata(L, 1);
1326 client_lower(c);
1327 return 0;
1330 /** Redraw a client by unmapping and mapping it quickly.
1331 * \param L The Lua VM state.
1333 * \luastack
1334 * \lvalue A client.
1336 static int
1337 luaA_client_redraw(lua_State *L)
1339 client_t *c = luaA_client_checkudata(L, 1);
1341 xcb_unmap_window(globalconf.connection, c->win);
1342 xcb_map_window(globalconf.connection, c->win);
1344 /* Set the focus on the current window if the redraw has been
1345 performed on the window where the pointer is currently on
1346 because after the unmapping/mapping, the focus is lost */
1347 if(globalconf.screen_focus->client_focus == c)
1349 client_unfocus(c);
1350 client_focus(c);
1353 return 0;
1356 /** Stop managing a client.
1357 * \param L The Lua VM state.
1358 * \return The number of elements pushed on stack.
1359 * \luastack
1360 * \lvalue A client.
1362 static int
1363 luaA_client_unmanage(lua_State *L)
1365 client_t *c = luaA_client_checkudata(L, 1);
1366 client_unmanage(c);
1367 return 0;
1370 /** Return client geometry.
1371 * \param L The Lua VM state.
1372 * \return The number of elements pushed on stack.
1373 * \luastack
1374 * \lparam A table with new coordinates, or none.
1375 * \lreturn A table with client coordinates.
1377 static int
1378 luaA_client_geometry(lua_State *L)
1380 client_t *c = luaA_client_checkudata(L, 1);
1382 if(lua_gettop(L) == 2)
1384 area_t geometry;
1386 luaA_checktable(L, 2);
1387 geometry.x = luaA_getopt_number(L, 2, "x", c->geometry.x);
1388 geometry.y = luaA_getopt_number(L, 2, "y", c->geometry.y);
1389 if(client_isfixed(c))
1391 geometry.width = c->geometry.width;
1392 geometry.height = c->geometry.height;
1394 else
1396 geometry.width = luaA_getopt_number(L, 2, "width", c->geometry.width);
1397 geometry.height = luaA_getopt_number(L, 2, "height", c->geometry.height);
1400 client_resize(c, geometry, c->size_hints_honor);
1403 return luaA_pusharea(L, c->geometry);
1406 /** Push a strut type to a table on stack.
1407 * \param L The Lua VM state.
1408 * \param struts The struts to push.
1409 * \return The number of elements pushed on stack.
1411 static inline int
1412 luaA_pushstruts(lua_State *L, strut_t struts)
1414 lua_createtable(L, 4, 0);
1415 lua_pushnumber(L, struts.left);
1416 lua_setfield(L, -2, "left");
1417 lua_pushnumber(L, struts.right);
1418 lua_setfield(L, -2, "right");
1419 lua_pushnumber(L, struts.top);
1420 lua_setfield(L, -2, "top");
1421 lua_pushnumber(L, struts.bottom);
1422 lua_setfield(L, -2, "bottom");
1423 return 1;
1426 /** Return client struts (reserved space at the edge of the screen).
1427 * \param L The Lua VM state.
1428 * \return The number of elements pushed on stack.
1429 * \luastack
1430 * \lparam A table with new strut values, or none.
1431 * \lreturn A table with strut values.
1433 static int
1434 luaA_client_struts(lua_State *L)
1436 client_t *c = luaA_client_checkudata(L, 1);
1438 if(lua_gettop(L) == 2)
1440 strut_t struts;
1441 area_t screen_area = display_area_get(c->phys_screen);
1443 struts.left = luaA_getopt_number(L, 2, "left", c->strut.left);
1444 struts.right = luaA_getopt_number(L, 2, "right", c->strut.right);
1445 struts.top = luaA_getopt_number(L, 2, "top", c->strut.top);
1446 struts.bottom = luaA_getopt_number(L, 2, "bottom", c->strut.bottom);
1448 if(struts.left != c->strut.left || struts.right != c->strut.right ||
1449 struts.top != c->strut.top || struts.bottom != c->strut.bottom) {
1450 /* Struts are not so well defined in the context of xinerama. So we just
1451 * give the entire root window and let the window manager decide. */
1452 struts.left_start_y = 0;
1453 struts.left_end_y = !struts.left ? 0 : screen_area.height;
1454 struts.right_start_y = 0;
1455 struts.right_end_y = !struts.right ? 0 : screen_area.height;
1456 struts.top_start_x = 0;
1457 struts.top_end_x = !struts.top ? 0 : screen_area.width;
1458 struts.bottom_start_x = 0;
1459 struts.bottom_end_x = !struts.bottom ? 0 : screen_area.width;
1461 c->strut = struts;
1463 ewmh_update_client_strut(c);
1465 hook_property(client, c, "struts");
1469 return luaA_pushstruts(L, c->strut);
1472 /** Client newindex.
1473 * \param L The Lua VM state.
1474 * \return The number of elements pushed on stack.
1476 static int
1477 luaA_client_newindex(lua_State *L)
1479 size_t len;
1480 client_t *c = luaA_client_checkudata(L, 1);
1481 const char *buf = luaL_checklstring(L, 2, &len);
1482 bool b;
1483 double d;
1484 int i;
1486 switch(a_tokenize(buf, len))
1488 case A_TK_SCREEN:
1489 if(globalconf.xinerama_is_active)
1491 i = luaL_checknumber(L, 3) - 1;
1492 luaA_checkscreen(i);
1493 screen_client_moveto(c, &globalconf.screens.tab[i], true, true);
1495 break;
1496 case A_TK_HIDE:
1497 b = luaA_checkboolean(L, 3);
1498 if(b != c->ishidden)
1500 client_need_reban(c);
1501 c->ishidden = b;
1502 client_need_reban(c);
1503 hook_property(client, c, "hide");
1505 break;
1506 case A_TK_MINIMIZED:
1507 client_setminimized(c, luaA_checkboolean(L, 3));
1508 break;
1509 case A_TK_FULLSCREEN:
1510 client_setfullscreen(c, luaA_checkboolean(L, 3));
1511 break;
1512 case A_TK_MAXIMIZED_HORIZONTAL:
1513 client_setmaxhoriz(c, luaA_checkboolean(L, 3));
1514 break;
1515 case A_TK_MAXIMIZED_VERTICAL:
1516 client_setmaxvert(c, luaA_checkboolean(L, 3));
1517 break;
1518 case A_TK_ICON:
1519 luaA_object_unref_item(L, 1, c->icon);
1520 c->icon = NULL;
1521 c->icon = luaA_object_ref_item(L, 1, 3);
1522 /* execute hook */
1523 hook_property(client, c, "icon");
1524 break;
1525 case A_TK_OPACITY:
1526 if(lua_isnil(L, 3))
1527 window_opacity_set(c->win, -1);
1528 else
1530 d = luaL_checknumber(L, 3);
1531 if(d >= 0 && d <= 1)
1532 window_opacity_set(c->win, d);
1534 break;
1535 case A_TK_STICKY:
1536 client_setsticky(c, luaA_checkboolean(L, 3));
1537 break;
1538 case A_TK_SIZE_HINTS_HONOR:
1539 c->size_hints_honor = luaA_checkboolean(L, 3);
1540 hook_property(client, c, "size_hints_honor");
1541 break;
1542 case A_TK_BORDER_WIDTH:
1543 client_setborder(c, luaL_checknumber(L, 3));
1544 break;
1545 case A_TK_ONTOP:
1546 client_setontop(c, luaA_checkboolean(L, 3));
1547 break;
1548 case A_TK_ABOVE:
1549 client_setabove(c, luaA_checkboolean(L, 3));
1550 break;
1551 case A_TK_BELOW:
1552 client_setbelow(c, luaA_checkboolean(L, 3));
1553 break;
1554 case A_TK_URGENT:
1555 client_seturgent(c, luaA_checkboolean(L, 3));
1556 break;
1557 case A_TK_BORDER_COLOR:
1558 if((buf = luaL_checklstring(L, 3, &len))
1559 && xcolor_init_reply(xcolor_init_unchecked(&c->border_color, buf, len)))
1560 xcb_change_window_attributes(globalconf.connection, c->win,
1561 XCB_CW_BORDER_PIXEL, &c->border_color.pixel);
1562 break;
1563 case A_TK_TITLEBAR:
1564 if(lua_isnil(L, 3))
1565 titlebar_client_detach(c);
1566 else
1567 titlebar_client_attach(c);
1568 break;
1569 case A_TK_SKIP_TASKBAR:
1570 c->skiptb = luaA_checkboolean(L, 3);
1571 hook_property(client, c, "skip_taskbar");
1572 break;
1573 case A_TK_KEYS:
1574 luaA_object_unref_item(L, 1, c->keys);
1575 c->keys = luaA_object_ref_item(L, 1, 3);
1576 break;
1577 case A_TK_BUTTONS:
1578 luaA_object_unref_item(L, 1, c->buttons);
1579 c->buttons = luaA_object_ref_item(L, 1, 3);
1580 break;
1581 default:
1582 return 0;
1585 return 0;
1588 /** Client object.
1589 * \param L The Lua VM state.
1590 * \return The number of elements pushed on stack.
1591 * \luastack
1592 * \lfield id The window X id.
1593 * \lfield name The client title.
1594 * \lfield skip_taskbar If true the client won't be shown in the tasklist.
1595 * \lfield type The window type (desktop, normal, dock, …).
1596 * \lfield class The client class.
1597 * \lfield instance The client instance.
1598 * \lfield pid The client PID, if available.
1599 * \lfield role The window role, if available.
1600 * \lfield machine The machine client is running on.
1601 * \lfield icon_name The client name when iconified.
1602 * \lfield screen Client screen number.
1603 * \lfield hide Define if the client must be hidden, i.e. never mapped,
1604 * invisible in taskbar.
1605 * \lfield minimized Define it the client must be iconify, i.e. only visible in
1606 * taskbar.
1607 * \lfield size_hints_honor Honor size hints, i.e. respect size ratio.
1608 * \lfield border_width The client border width.
1609 * \lfield border_color The client border color.
1610 * \lfield titlebar The client titlebar.
1611 * \lfield urgent The client urgent state.
1612 * \lfield content An image representing the client window content (screenshot).
1613 * \lfield focus The focused client.
1614 * \lfield opacity The client opacity between 0 and 1.
1615 * \lfield ontop The client is on top of every other windows.
1616 * \lfield above The client is above normal windows.
1617 * \lfield below The client is below normal windows.
1618 * \lfield fullscreen The client is fullscreen or not.
1619 * \lfield maximized_horizontal The client is maximized horizontally or not.
1620 * \lfield maximized_vertical The client is maximized vertically or not.
1621 * \lfield transient_for Return the client the window is transient for.
1622 * \lfield group_id Identification unique to a group of windows.
1623 * \lfield leader_id Identification unique to windows spawned by the same command.
1624 * \lfield size_hints A table with size hints of the client: user_position,
1625 * user_size, program_position, program_size, etc.
1627 static int
1628 luaA_client_index(lua_State *L)
1630 size_t len;
1631 ssize_t slen;
1632 client_t *c = luaA_client_checkudata(L, 1);
1633 const char *buf = luaL_checklstring(L, 2, &len);
1634 char *value;
1635 void *data;
1636 xcb_get_property_cookie_t prop_c;
1637 xcb_get_property_reply_t *prop_r = NULL;
1638 double d;
1640 if(luaA_usemetatable(L, 1, 2))
1641 return 1;
1643 switch(a_tokenize(buf, len))
1645 case A_TK_NAME:
1646 lua_pushstring(L, c->name);
1647 break;
1648 case A_TK_TRANSIENT_FOR:
1649 return client_push(globalconf.L, c->transient_for);
1650 case A_TK_SKIP_TASKBAR:
1651 lua_pushboolean(L, c->skiptb);
1652 break;
1653 case A_TK_CONTENT:
1654 return client_getcontent(c);
1655 case A_TK_TYPE:
1656 switch(c->type)
1658 case WINDOW_TYPE_DESKTOP:
1659 lua_pushliteral(L, "desktop");
1660 break;
1661 case WINDOW_TYPE_DOCK:
1662 lua_pushliteral(L, "dock");
1663 break;
1664 case WINDOW_TYPE_SPLASH:
1665 lua_pushliteral(L, "splash");
1666 break;
1667 case WINDOW_TYPE_DIALOG:
1668 lua_pushliteral(L, "dialog");
1669 break;
1670 case WINDOW_TYPE_MENU:
1671 lua_pushliteral(L, "menu");
1672 break;
1673 case WINDOW_TYPE_TOOLBAR:
1674 lua_pushliteral(L, "toolbar");
1675 break;
1676 case WINDOW_TYPE_UTILITY:
1677 lua_pushliteral(L, "utility");
1678 break;
1679 case WINDOW_TYPE_DROPDOWN_MENU:
1680 lua_pushliteral(L, "dropdown_menu");
1681 break;
1682 case WINDOW_TYPE_POPUP_MENU:
1683 lua_pushliteral(L, "popup_menu");
1684 break;
1685 case WINDOW_TYPE_TOOLTIP:
1686 lua_pushliteral(L, "tooltip");
1687 break;
1688 case WINDOW_TYPE_NOTIFICATION:
1689 lua_pushliteral(L, "notification");
1690 break;
1691 case WINDOW_TYPE_COMBO:
1692 lua_pushliteral(L, "combo");
1693 break;
1694 case WINDOW_TYPE_DND:
1695 lua_pushliteral(L, "dnd");
1696 break;
1697 case WINDOW_TYPE_NORMAL:
1698 lua_pushliteral(L, "normal");
1699 break;
1701 break;
1702 case A_TK_CLASS:
1703 lua_pushstring(L, c->class);
1704 break;
1705 case A_TK_INSTANCE:
1706 lua_pushstring(L, c->instance);
1707 break;
1708 case A_TK_ROLE:
1709 if(!xutil_text_prop_get(globalconf.connection, c->win,
1710 WM_WINDOW_ROLE, &value, &slen))
1711 return 0;
1712 lua_pushlstring(L, value, slen);
1713 p_delete(&value);
1714 break;
1715 case A_TK_PID:
1716 prop_c = xcb_get_property_unchecked(globalconf.connection, false, c->win, _NET_WM_PID, CARDINAL, 0L, 1L);
1717 prop_r = xcb_get_property_reply(globalconf.connection, prop_c, NULL);
1719 if(prop_r && prop_r->value_len && (data = xcb_get_property_value(prop_r)))
1720 lua_pushnumber(L, *(uint32_t *)data);
1721 else
1723 p_delete(&prop_r);
1724 return 0;
1726 break;
1727 case A_TK_ID:
1728 lua_pushnumber(L, c->win);
1729 break;
1730 case A_TK_LEADER_ID:
1731 lua_pushnumber(L, c->leader_win);
1732 break;
1733 case A_TK_MACHINE:
1734 if(!xutil_text_prop_get(globalconf.connection, c->win,
1735 WM_CLIENT_MACHINE, &value, &slen))
1736 return 0;
1737 lua_pushlstring(L, value, slen);
1738 p_delete(&value);
1739 break;
1740 case A_TK_ICON_NAME:
1741 lua_pushstring(L, c->icon_name);
1742 break;
1743 case A_TK_SCREEN:
1744 lua_pushnumber(L, 1 + screen_array_indexof(&globalconf.screens, c->screen));
1745 break;
1746 case A_TK_HIDE:
1747 lua_pushboolean(L, c->ishidden);
1748 break;
1749 case A_TK_MINIMIZED:
1750 lua_pushboolean(L, c->isminimized);
1751 break;
1752 case A_TK_FULLSCREEN:
1753 lua_pushboolean(L, c->isfullscreen);
1754 break;
1755 case A_TK_GROUP_ID:
1756 if(c->group_win)
1757 lua_pushnumber(L, c->group_win);
1758 else
1759 return 0;
1760 break;
1761 case A_TK_MAXIMIZED_HORIZONTAL:
1762 lua_pushboolean(L, c->ismaxhoriz);
1763 break;
1764 case A_TK_MAXIMIZED_VERTICAL:
1765 lua_pushboolean(L, c->ismaxvert);
1766 break;
1767 case A_TK_ICON:
1768 luaA_object_push_item(L, 1, c->icon);
1769 break;
1770 case A_TK_OPACITY:
1771 if((d = window_opacity_get(c->win)) >= 0)
1772 lua_pushnumber(L, d);
1773 else
1774 return 0;
1775 break;
1776 case A_TK_ONTOP:
1777 lua_pushboolean(L, c->isontop);
1778 break;
1779 case A_TK_ABOVE:
1780 lua_pushboolean(L, c->isabove);
1781 break;
1782 case A_TK_BELOW:
1783 lua_pushboolean(L, c->isbelow);
1784 break;
1785 case A_TK_STICKY:
1786 lua_pushboolean(L, c->issticky);
1787 break;
1788 case A_TK_SIZE_HINTS_HONOR:
1789 lua_pushboolean(L, c->size_hints_honor);
1790 break;
1791 case A_TK_BORDER_WIDTH:
1792 lua_pushnumber(L, c->border);
1793 break;
1794 case A_TK_BORDER_COLOR:
1795 luaA_pushxcolor(L, &c->border_color);
1796 break;
1797 case A_TK_TITLEBAR:
1798 return wibox_push(L, c->titlebar);
1799 case A_TK_URGENT:
1800 lua_pushboolean(L, c->isurgent);
1801 break;
1802 case A_TK_SIZE_HINTS:
1804 const char *u_or_p = NULL;
1806 lua_createtable(L, 0, 1);
1808 if(c->size_hints.flags & XCB_SIZE_HINT_US_POSITION)
1809 u_or_p = "user_position";
1810 else if(c->size_hints.flags & XCB_SIZE_HINT_P_POSITION)
1811 u_or_p = "program_position";
1813 if(u_or_p)
1815 lua_createtable(L, 0, 2);
1816 lua_pushnumber(L, c->size_hints.x);
1817 lua_setfield(L, -2, "x");
1818 lua_pushnumber(L, c->size_hints.y);
1819 lua_setfield(L, -2, "y");
1820 lua_setfield(L, -2, u_or_p);
1821 u_or_p = NULL;
1824 if(c->size_hints.flags & XCB_SIZE_HINT_US_SIZE)
1825 u_or_p = "user_size";
1826 else if(c->size_hints.flags & XCB_SIZE_HINT_P_SIZE)
1827 u_or_p = "program_size";
1829 if(u_or_p)
1831 lua_createtable(L, 0, 2);
1832 lua_pushnumber(L, c->size_hints.width);
1833 lua_setfield(L, -2, "width");
1834 lua_pushnumber(L, c->size_hints.height);
1835 lua_setfield(L, -2, "height");
1836 lua_setfield(L, -2, u_or_p);
1839 if(c->size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE)
1841 lua_pushnumber(L, c->size_hints.min_width);
1842 lua_setfield(L, -2, "min_width");
1843 lua_pushnumber(L, c->size_hints.min_height);
1844 lua_setfield(L, -2, "min_height");
1847 if(c->size_hints.flags & XCB_SIZE_HINT_P_MAX_SIZE)
1849 lua_pushnumber(L, c->size_hints.max_width);
1850 lua_setfield(L, -2, "max_width");
1851 lua_pushnumber(L, c->size_hints.max_height);
1852 lua_setfield(L, -2, "max_height");
1855 if(c->size_hints.flags & XCB_SIZE_HINT_P_RESIZE_INC)
1857 lua_pushnumber(L, c->size_hints.width_inc);
1858 lua_setfield(L, -2, "width_inc");
1859 lua_pushnumber(L, c->size_hints.height_inc);
1860 lua_setfield(L, -2, "height_inc");
1863 if(c->size_hints.flags & XCB_SIZE_HINT_P_ASPECT)
1865 lua_pushnumber(L, c->size_hints.min_aspect_num);
1866 lua_setfield(L, -2, "min_aspect_num");
1867 lua_pushnumber(L, c->size_hints.min_aspect_den);
1868 lua_setfield(L, -2, "min_aspect_den");
1869 lua_pushnumber(L, c->size_hints.max_aspect_num);
1870 lua_setfield(L, -2, "max_aspect_num");
1871 lua_pushnumber(L, c->size_hints.max_aspect_den);
1872 lua_setfield(L, -2, "max_aspect_den");
1875 if(c->size_hints.flags & XCB_SIZE_HINT_BASE_SIZE)
1877 lua_pushnumber(L, c->size_hints.base_width);
1878 lua_setfield(L, -2, "base_width");
1879 lua_pushnumber(L, c->size_hints.base_height);
1880 lua_setfield(L, -2, "base_height");
1883 if(c->size_hints.flags & XCB_SIZE_HINT_P_WIN_GRAVITY)
1885 switch(c->size_hints.win_gravity)
1887 default:
1888 lua_pushliteral(L, "north_west");
1889 break;
1890 case XCB_GRAVITY_NORTH:
1891 lua_pushliteral(L, "north");
1892 break;
1893 case XCB_GRAVITY_NORTH_EAST:
1894 lua_pushliteral(L, "north_east");
1895 break;
1896 case XCB_GRAVITY_WEST:
1897 lua_pushliteral(L, "west");
1898 break;
1899 case XCB_GRAVITY_CENTER:
1900 lua_pushliteral(L, "center");
1901 break;
1902 case XCB_GRAVITY_EAST:
1903 lua_pushliteral(L, "east");
1904 break;
1905 case XCB_GRAVITY_SOUTH_WEST:
1906 lua_pushliteral(L, "south_west");
1907 break;
1908 case XCB_GRAVITY_SOUTH:
1909 lua_pushliteral(L, "south");
1910 break;
1911 case XCB_GRAVITY_SOUTH_EAST:
1912 lua_pushliteral(L, "south_east");
1913 break;
1914 case XCB_GRAVITY_STATIC:
1915 lua_pushliteral(L, "static");
1916 break;
1918 lua_setfield(L, -2, "win_gravity");
1921 break;
1922 case A_TK_KEYS:
1923 return luaA_object_push_item(L, 1, c->keys);
1924 case A_TK_BUTTONS:
1925 return luaA_object_push_item(L, 1, c->buttons);
1926 default:
1927 return 0;
1930 return 1;
1933 /* Client module.
1934 * \param L The Lua VM state.
1935 * \return The number of pushed elements.
1937 static int
1938 luaA_client_module_index(lua_State *L)
1940 size_t len;
1941 const char *buf = luaL_checklstring(L, 2, &len);
1943 switch(a_tokenize(buf, len))
1945 case A_TK_FOCUS:
1946 return client_push(globalconf.L, globalconf.screen_focus->client_focus);
1947 break;
1948 default:
1949 return 0;
1953 /* Client module new index.
1954 * \param L The Lua VM state.
1955 * \return The number of pushed elements.
1957 static int
1958 luaA_client_module_newindex(lua_State *L)
1960 size_t len;
1961 const char *buf = luaL_checklstring(L, 2, &len);
1962 client_t *c;
1964 switch(a_tokenize(buf, len))
1966 case A_TK_FOCUS:
1967 c = luaA_client_checkudata(L, 3);
1968 client_focus(c);
1969 break;
1970 default:
1971 break;
1974 return 0;
1977 const struct luaL_reg awesome_client_methods[] =
1979 LUA_CLASS_METHODS(client)
1980 { "get", luaA_client_get },
1981 { "__index", luaA_client_module_index },
1982 { "__newindex", luaA_client_module_newindex },
1983 { NULL, NULL }
1985 const struct luaL_reg awesome_client_meta[] =
1987 LUA_OBJECT_META(client)
1988 { "isvisible", luaA_client_isvisible },
1989 { "geometry", luaA_client_geometry },
1990 { "struts", luaA_client_struts },
1991 { "tags", luaA_client_tags },
1992 { "kill", luaA_client_kill },
1993 { "swap", luaA_client_swap },
1994 { "raise", luaA_client_raise },
1995 { "lower", luaA_client_lower },
1996 { "redraw", luaA_client_redraw },
1997 { "unmanage", luaA_client_unmanage },
1998 { "__index", luaA_client_index },
1999 { "__newindex", luaA_client_newindex },
2000 { "__gc", luaA_client_gc },
2001 { NULL, NULL }
2004 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80