titlebar: fix modkey for buttons
[awesome.git] / client.c
blob5589fa8dad6590e2c938225aead4a3b409315b37
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 client_t *c, *tc = NULL;
497 screen_t *screen;
498 const uint32_t select_input_val[] = { CLIENT_SELECT_INPUT_EVENT_MASK };
500 if(systray_iskdedockapp(w))
502 systray_request_handle(w, phys_screen, NULL);
503 return;
506 /* Send request to get NET_WM_ICON property as soon as possible... */
507 ewmh_icon_cookie = ewmh_window_icon_get_unchecked(w);
508 xcb_change_window_attributes(globalconf.connection, w, XCB_CW_EVENT_MASK, select_input_val);
510 c = client_new(globalconf.L);
511 /* Push client in client list */
512 client_array_push(&globalconf.clients, client_ref(globalconf.L, -1));
515 screen = c->screen = screen_getbycoord(&globalconf.screens.tab[phys_screen],
516 wgeom->x, wgeom->y);
518 c->phys_screen = phys_screen;
520 /* consider the window banned */
521 c->isbanned = true;
523 /* Initial values */
524 c->win = w;
525 c->geometry.x = wgeom->x;
526 c->geometry.y = wgeom->y;
527 /* Border will be added later. */
528 c->geometry.width = wgeom->width;
529 c->geometry.height = wgeom->height;
530 /* Also set internal geometry (client_ban() needs it). */
531 c->geometries.internal.x = wgeom->x;
532 c->geometries.internal.y = wgeom->y;
533 c->geometries.internal.width = wgeom->width;
534 c->geometries.internal.height = wgeom->height;
535 client_setborder(c, wgeom->border_width);
537 if(ewmh_window_icon_get_reply(ewmh_icon_cookie))
539 client_push(globalconf.L, c);
540 c->icon = luaA_object_ref_item(globalconf.L, -1, -2);
541 lua_pop(globalconf.L, 1);
544 /* we honor size hints by default */
545 c->size_hints_honor = true;
547 /* update hints */
548 property_update_wm_normal_hints(c, NULL);
549 property_update_wm_hints(c, NULL);
550 property_update_wm_transient_for(c, NULL);
551 property_update_wm_client_leader(c, NULL);
553 /* Recursively find the parent. */
554 for(tc = c; tc->transient_for; tc = tc->transient_for);
555 if(tc != c && tc->phys_screen == c->phys_screen)
556 screen = tc->screen;
558 /* Then check clients hints */
559 ewmh_client_check_hints(c);
561 /* move client to screen, but do not tag it */
562 screen_client_moveto(c, screen, false, true);
564 /* Push client in stack */
565 client_raise(c);
567 /* update window title */
568 property_update_wm_name(c);
569 property_update_wm_icon_name(c);
570 property_update_wm_class(c, NULL);
571 property_update_wm_protocols(c);
573 xutil_text_prop_get(globalconf.connection, c->win, _NET_STARTUP_ID, &c->startup_id, NULL);
575 /* update strut */
576 ewmh_process_client_strut(c, NULL);
578 ewmh_update_net_client_list(c->phys_screen);
580 /* Always stay in NORMAL_STATE. Even though iconified seems more
581 * appropriate sometimes. The only possible loss is that clients not using
582 * visibility events may continue to proces data (when banned).
583 * Without any exposes or other events the cost should be fairly limited though.
585 * Some clients may expect the window to be unmapped when STATE_ICONIFIED.
586 * Two conflicting parts of the ICCCM v2.0 (section 4.1.4):
588 * "Normal -> Iconic - The client should send a ClientMessage event as described later in this section."
589 * (note no explicit mention of unmapping, while Normal->Widthdrawn does mention that)
591 * "Once a client's window has left the Withdrawn state, the window will be mapped
592 * if it is in the Normal state and the window will be unmapped if it is in the Iconic state."
594 * At this stage it's just safer to keep it in normal state and avoid confusion.
596 window_state_set(c->win, XCB_WM_STATE_NORMAL);
598 /* Grab everything */
599 xcb_grab_key(globalconf.connection, true, w,
600 XCB_MOD_MASK_ANY, XCB_GRAB_ANY,
601 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_SYNC);
603 xcb_grab_button(globalconf.connection, false, w,
604 XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE,
605 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, XCB_NONE, XCB_NONE,
606 XCB_GRAB_ANY, XCB_MOD_MASK_ANY);
609 if(!startup)
610 spawn_start_notify(c);
612 /* Call hook to notify list change */
613 if(globalconf.hooks.clients != LUA_REFNIL)
614 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.clients, 0, 0);
616 /* call hook */
617 if(globalconf.hooks.manage != LUA_REFNIL)
619 client_push(globalconf.L, c);
620 lua_pushboolean(globalconf.L, startup);
621 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.manage, 2, 0);
625 /** Compute client geometry with respect to its geometry hints.
626 * \param c The client.
627 * \param geometry The geometry that the client might receive.
628 * \return The geometry the client must take respecting its hints.
630 area_t
631 client_geometry_hints(client_t *c, area_t geometry)
633 int32_t basew, baseh, minw, minh;
635 /* base size is substituted with min size if not specified */
636 if(c->size_hints.flags & XCB_SIZE_HINT_P_SIZE)
638 basew = c->size_hints.base_width;
639 baseh = c->size_hints.base_height;
641 else if(c->size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE)
643 basew = c->size_hints.min_width;
644 baseh = c->size_hints.min_height;
646 else
647 basew = baseh = 0;
649 /* min size is substituted with base size if not specified */
650 if(c->size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE)
652 minw = c->size_hints.min_width;
653 minh = c->size_hints.min_height;
655 else if(c->size_hints.flags & XCB_SIZE_HINT_P_SIZE)
657 minw = c->size_hints.base_width;
658 minh = c->size_hints.base_height;
660 else
661 minw = minh = 0;
663 if(c->size_hints.flags & XCB_SIZE_HINT_P_ASPECT
664 && c->size_hints.min_aspect_num > 0
665 && c->size_hints.min_aspect_den > 0
666 && geometry.height - baseh > 0
667 && geometry.width - basew > 0)
669 double dx = (double) (geometry.width - basew);
670 double dy = (double) (geometry.height - baseh);
671 double min = (double) c->size_hints.min_aspect_num / (double) c->size_hints.min_aspect_den;
672 double max = (double) c->size_hints.max_aspect_num / (double) c->size_hints.min_aspect_den;
673 double ratio = dx / dy;
674 if(max > 0 && min > 0 && ratio > 0)
676 if(ratio < min)
678 dy = (dx * min + dy) / (min * min + 1);
679 dx = dy * min;
680 geometry.width = (int) dx + basew;
681 geometry.height = (int) dy + baseh;
683 else if(ratio > max)
685 dy = (dx * min + dy) / (max * max + 1);
686 dx = dy * min;
687 geometry.width = (int) dx + basew;
688 geometry.height = (int) dy + baseh;
693 if(minw)
694 geometry.width = MAX(geometry.width, minw);
695 if(minh)
696 geometry.height = MAX(geometry.height, minh);
698 if(c->size_hints.flags & XCB_SIZE_HINT_P_MAX_SIZE)
700 if(c->size_hints.max_width)
701 geometry.width = MIN(geometry.width, c->size_hints.max_width);
702 if(c->size_hints.max_height)
703 geometry.height = MIN(geometry.height, c->size_hints.max_height);
706 if(c->size_hints.flags & (XCB_SIZE_HINT_P_RESIZE_INC | XCB_SIZE_HINT_BASE_SIZE)
707 && c->size_hints.width_inc && c->size_hints.height_inc)
709 uint16_t t1 = geometry.width, t2 = geometry.height;
710 unsigned_subtract(t1, basew);
711 unsigned_subtract(t2, baseh);
712 geometry.width -= t1 % c->size_hints.width_inc;
713 geometry.height -= t2 % c->size_hints.height_inc;
716 return geometry;
719 /** Resize client window.
720 * The sizse given as parameters are with titlebar and borders!
721 * \param c Client to resize.
722 * \param geometry New window geometry.
723 * \param hints Use size hints.
724 * \return true if an actual resize occurred.
726 bool
727 client_resize(client_t *c, area_t geometry, bool hints)
729 area_t geometry_internal;
730 area_t area;
732 /* offscreen appearance fixes */
733 area = display_area_get(c->phys_screen);
735 if(geometry.x > area.width)
736 geometry.x = area.width - geometry.width;
737 if(geometry.y > area.height)
738 geometry.y = area.height - geometry.height;
739 if(geometry.x + geometry.width < 0)
740 geometry.x = 0;
741 if(geometry.y + geometry.height < 0)
742 geometry.y = 0;
744 /* Real client geometry, please keep it contained to C code at the very least. */
745 geometry_internal = titlebar_geometry_remove(c->titlebar, c->border, geometry);
747 if(hints)
748 geometry_internal = client_geometry_hints(c, geometry_internal);
750 if(geometry_internal.width == 0 || geometry_internal.height == 0)
751 return false;
753 /* Also let client hints propegate to the "official" geometry. */
754 geometry = titlebar_geometry_add(c->titlebar, c->border, geometry_internal);
756 if(c->geometries.internal.x != geometry_internal.x
757 || c->geometries.internal.y != geometry_internal.y
758 || c->geometries.internal.width != geometry_internal.width
759 || c->geometries.internal.height != geometry_internal.height)
761 screen_t *new_screen = screen_getbycoord(c->screen,
762 geometry_internal.x, geometry_internal.y);
764 /* Values to configure a window is an array where values are
765 * stored according to 'value_mask' */
766 uint32_t values[4];
768 c->geometries.internal.x = values[0] = geometry_internal.x;
769 c->geometries.internal.y = values[1] = geometry_internal.y;
770 c->geometries.internal.width = values[2] = geometry_internal.width;
771 c->geometries.internal.height = values[3] = geometry_internal.height;
773 /* Also store geometry including border and titlebar. */
774 c->geometry = geometry;
776 titlebar_update_geometry(c);
778 /* Ignore all spurious enter/leave notify events */
779 client_ignore_enterleave_events();
781 xcb_configure_window(globalconf.connection, c->win,
782 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y
783 | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
784 values);
786 client_restore_enterleave_events();
788 screen_client_moveto(c, new_screen, true, false);
790 /* execute hook */
791 hook_property(client, c, "geometry");
793 return true;
796 return false;
799 /** Set a client minimized, or not.
800 * \param c The client.
801 * \param s Set or not the client minimized.
803 void
804 client_setminimized(client_t *c, bool s)
806 if(c->isminimized != s)
808 client_need_reban(c);
809 c->isminimized = s;
810 client_need_reban(c);
811 if(s)
812 window_state_set(c->win, XCB_WM_STATE_ICONIC);
813 else
814 window_state_set(c->win, XCB_WM_STATE_NORMAL);
815 ewmh_client_update_hints(c);
816 /* execute hook */
817 hook_property(client, c, "minimized");
821 /** Set a client sticky, or not.
822 * \param c The client.
823 * \param s Set or not the client sticky.
825 void
826 client_setsticky(client_t *c, bool s)
828 if(c->issticky != s)
830 client_need_reban(c);
831 c->issticky = s;
832 client_need_reban(c);
833 ewmh_client_update_hints(c);
834 hook_property(client, c, "sticky");
838 /** Set a client fullscreen, or not.
839 * \param c The client.
840 * \param s Set or not the client fullscreen.
842 void
843 client_setfullscreen(client_t *c, bool s)
845 if(c->isfullscreen != s)
847 area_t geometry;
849 /* Make sure the current geometry is stored without titlebar. */
850 if(s)
851 titlebar_ban(c->titlebar);
853 /* become fullscreen! */
854 if((c->isfullscreen = s))
856 /* remove any max state */
857 client_setmaxhoriz(c, false);
858 client_setmaxvert(c, false);
859 /* You can only be part of one of the special layers. */
860 client_setbelow(c, false);
861 client_setabove(c, false);
862 client_setontop(c, false);
864 geometry = screen_area_get(c->screen, false);
865 c->geometries.fullscreen = c->geometry;
866 c->border_fs = c->border;
867 client_setborder(c, 0);
869 else
871 geometry = c->geometries.fullscreen;
872 client_setborder(c, c->border_fs);
874 client_resize(c, geometry, false);
875 client_stack();
876 ewmh_client_update_hints(c);
877 hook_property(client, c, "fullscreen");
881 /** Set a client horizontally maximized.
882 * \param c The client.
883 * \param s The maximized status.
885 void
886 client_setmaxhoriz(client_t *c, bool s)
888 if(c->ismaxhoriz != s)
890 area_t geometry;
892 if((c->ismaxhoriz = s))
894 /* remove fullscreen mode */
895 client_setfullscreen(c, false);
897 geometry = screen_area_get(c->screen, true);
898 geometry.y = c->geometry.y;
899 geometry.height = c->geometry.height;
900 c->geometries.max.x = c->geometry.x;
901 c->geometries.max.width = c->geometry.width;
903 else
905 geometry = c->geometry;
906 geometry.x = c->geometries.max.x;
907 geometry.width = c->geometries.max.width;
910 client_resize(c, geometry, c->size_hints_honor);
911 client_stack();
912 ewmh_client_update_hints(c);
913 hook_property(client, c, "maximized_horizontal");
917 /** Set a client vertically maximized.
918 * \param c The client.
919 * \param s The maximized status.
921 void
922 client_setmaxvert(client_t *c, bool s)
924 if(c->ismaxvert != s)
926 area_t geometry;
928 if((c->ismaxvert = s))
930 /* remove fullscreen mode */
931 client_setfullscreen(c, false);
933 geometry = screen_area_get(c->screen, true);
934 geometry.x = c->geometry.x;
935 geometry.width = c->geometry.width;
936 c->geometries.max.y = c->geometry.y;
937 c->geometries.max.height = c->geometry.height;
939 else
941 geometry = c->geometry;
942 geometry.y = c->geometries.max.y;
943 geometry.height = c->geometries.max.height;
946 client_resize(c, geometry, c->size_hints_honor);
947 client_stack();
948 ewmh_client_update_hints(c);
949 hook_property(client, c, "maximized_vertical");
953 /** Set a client above, or not.
954 * \param c The client.
955 * \param s Set or not the client above.
957 void
958 client_setabove(client_t *c, bool s)
960 if(c->isabove != s)
962 /* You can only be part of one of the special layers. */
963 if(s)
965 client_setbelow(c, false);
966 client_setontop(c, false);
967 client_setfullscreen(c, false);
969 c->isabove = s;
970 client_stack();
971 ewmh_client_update_hints(c);
972 /* execute hook */
973 hook_property(client, c, "above");
977 /** Set a client below, or not.
978 * \param c The client.
979 * \param s Set or not the client below.
981 void
982 client_setbelow(client_t *c, bool s)
984 if(c->isbelow != s)
986 /* You can only be part of one of the special layers. */
987 if(s)
989 client_setabove(c, false);
990 client_setontop(c, false);
991 client_setfullscreen(c, false);
993 c->isbelow = s;
994 client_stack();
995 ewmh_client_update_hints(c);
996 /* execute hook */
997 hook_property(client, c, "below");
1001 /** Set a client modal, or not.
1002 * \param c The client.
1003 * \param s Set or not the client moda.
1005 void
1006 client_setmodal(client_t *c, bool s)
1008 if(c->ismodal != s)
1010 c->ismodal = s;
1011 client_stack();
1012 ewmh_client_update_hints(c);
1013 /* execute hook */
1014 hook_property(client, c, "modal");
1018 /** Set a client ontop, or not.
1019 * \param c The client.
1020 * \param s Set or not the client moda.
1022 void
1023 client_setontop(client_t *c, bool s)
1025 if(c->isontop != s)
1027 /* You can only be part of one of the special layers. */
1028 if(s)
1030 client_setabove(c, false);
1031 client_setbelow(c, false);
1032 client_setfullscreen(c, false);
1034 c->isontop = s;
1035 client_stack();
1036 /* execute hook */
1037 hook_property(client, c, "ontop");
1041 /** Unban a client and move it back into the viewport.
1042 * \param c The client.
1044 void
1045 client_unban(client_t *c)
1047 if(c->isbanned)
1049 xcb_map_window(globalconf.connection, c->win);
1051 c->isbanned = false;
1055 /** Unmanage a client.
1056 * \param c The client.
1058 void
1059 client_unmanage(client_t *c)
1061 tag_array_t *tags = &c->screen->tags;
1063 /* Reset transient_for attributes of widows that maybe refering to us */
1064 foreach(_tc, globalconf.clients)
1066 client_t *tc = *_tc;
1067 if(tc->transient_for == c)
1068 tc->transient_for = NULL;
1071 if(globalconf.screens.tab[c->phys_screen].prev_client_focus == c)
1072 globalconf.screens.tab[c->phys_screen].prev_client_focus = NULL;
1074 if(globalconf.screens.tab[c->phys_screen].client_focus == c)
1075 client_unfocus(c);
1077 /* remove client from global list and everywhere else */
1078 foreach(elem, globalconf.clients)
1079 if(*elem == c)
1081 client_array_remove(&globalconf.clients, elem);
1082 break;
1084 stack_client_remove(c);
1085 for(int i = 0; i < tags->len; i++)
1086 untag_client(c, tags->tab[i]);
1088 /* call hook */
1089 if(globalconf.hooks.unmanage != LUA_REFNIL)
1091 client_push(globalconf.L, c);
1092 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.unmanage, 1, 0);
1095 /* Call hook to notify list change */
1096 if(globalconf.hooks.clients != LUA_REFNIL)
1097 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.clients, 0, 0);
1099 window_state_set(c->win, XCB_WM_STATE_WITHDRAWN);
1101 titlebar_client_detach(c);
1103 ewmh_update_net_client_list(c->phys_screen);
1105 /* set client as invalid */
1106 c->invalid = true;
1108 client_unref(globalconf.L, c);
1111 /** Kill a client via a WM_DELETE_WINDOW request or KillClient if not
1112 * supported.
1113 * \param c The client to kill.
1115 void
1116 client_kill(client_t *c)
1118 if(client_hasproto(c, WM_DELETE_WINDOW))
1120 xcb_client_message_event_t ev;
1122 /* Initialize all of event's fields first */
1123 p_clear(&ev, 1);
1125 ev.response_type = XCB_CLIENT_MESSAGE;
1126 ev.window = c->win;
1127 ev.format = 32;
1128 ev.data.data32[1] = XCB_CURRENT_TIME;
1129 ev.type = WM_PROTOCOLS;
1130 ev.data.data32[0] = WM_DELETE_WINDOW;
1132 xcb_send_event(globalconf.connection, false, c->win,
1133 XCB_EVENT_MASK_NO_EVENT, (char *) &ev);
1135 else
1136 xcb_kill_client(globalconf.connection, c->win);
1139 /** Get all clients into a table.
1140 * \param L The Lua VM state.
1141 * \return The number of elements pushed on stack.
1142 * \luastack
1143 * \lparam An optional screen nunmber.
1144 * \lreturn A table with all clients.
1146 static int
1147 luaA_client_get(lua_State *L)
1149 int i = 1, screen;
1151 screen = luaL_optnumber(L, 1, 0) - 1;
1153 lua_newtable(L);
1155 if(screen == -1)
1156 foreach(c, globalconf.clients)
1158 client_push(L, *c);
1159 lua_rawseti(L, -2, i++);
1161 else
1163 luaA_checkscreen(screen);
1164 foreach(c, globalconf.clients)
1165 if((*c)->screen == &globalconf.screens.tab[screen])
1167 client_push(L, *c);
1168 lua_rawseti(L, -2, i++);
1172 return 1;
1175 /** Check if a client is visible on its screen.
1176 * \param L The Lua VM state.
1177 * \return The number of elements pushed on stack.
1178 * \luastack
1179 * \lvalue A client.
1180 * \lreturn A boolean value, true if the client is visible, false otherwise.
1182 static int
1183 luaA_client_isvisible(lua_State *L)
1185 client_t *c = luaA_client_checkudata(L, 1);
1186 lua_pushboolean(L, client_isvisible(c, c->screen));
1187 return 1;
1190 /** Set client border width.
1191 * \param c The client.
1192 * \param width The border width.
1194 void
1195 client_setborder(client_t *c, int width)
1197 uint32_t w = width;
1199 if(width > 0 && (c->type == WINDOW_TYPE_DOCK
1200 || c->type == WINDOW_TYPE_SPLASH
1201 || c->type == WINDOW_TYPE_DESKTOP
1202 || c->isfullscreen))
1203 return;
1205 if(width == c->border || width < 0)
1206 return;
1208 /* Update geometry with the new border. */
1209 c->geometry.width -= 2 * c->border;
1210 c->geometry.height -= 2 * c->border;
1212 c->border = width;
1213 xcb_configure_window(globalconf.connection, c->win,
1214 XCB_CONFIG_WINDOW_BORDER_WIDTH, &w);
1216 c->geometry.width += 2 * c->border;
1217 c->geometry.height += 2 * c->border;
1219 /* Changing border size also affects the size of the titlebar. */
1220 titlebar_update_geometry(c);
1222 hook_property(client, c, "border_width");
1225 /** Kill a client.
1226 * \param L The Lua VM state.
1228 * \luastack
1229 * \lvalue A client.
1231 static int
1232 luaA_client_kill(lua_State *L)
1234 client_t *c = luaA_client_checkudata(L, 1);
1235 client_kill(c);
1236 return 0;
1239 /** Swap a client with another one.
1240 * \param L The Lua VM state.
1241 * \luastack
1242 * \lvalue A client.
1243 * \lparam A client to swap with.
1245 static int
1246 luaA_client_swap(lua_State *L)
1248 client_t *c = luaA_client_checkudata(L, 1);
1249 client_t *swap = luaA_client_checkudata(L, 2);
1251 if(c != swap)
1253 client_t **ref_c = NULL, **ref_swap = NULL;
1254 foreach(item, globalconf.clients)
1256 if(*item == c)
1257 ref_c = item;
1258 else if(*item == swap)
1259 ref_swap = item;
1260 if(ref_c && ref_swap)
1261 break;
1263 /* swap ! */
1264 *ref_c = swap;
1265 *ref_swap = c;
1267 /* Call hook to notify list change */
1268 if(globalconf.hooks.clients != LUA_REFNIL)
1269 luaA_dofunction_from_registry(L, globalconf.hooks.clients, 0, 0);
1272 return 0;
1275 /** Access or set the client tags.
1276 * \param L The Lua VM state.
1277 * \return The number of elements pushed on stack.
1278 * \lparam A table with tags to set, or none to get the current tags table.
1279 * \return The clients tag.
1281 static int
1282 luaA_client_tags(lua_State *L)
1284 client_t *c = luaA_client_checkudata(L, 1);
1285 tag_array_t *tags = &c->screen->tags;
1286 int j = 0;
1288 if(lua_gettop(L) == 2)
1290 luaA_checktable(L, 2);
1291 for(int i = 0; i < tags->len; i++)
1292 untag_client(c, tags->tab[i]);
1293 lua_pushnil(L);
1294 while(lua_next(L, 2))
1295 tag_client(c);
1296 lua_pop(L, 1);
1299 lua_newtable(L);
1300 foreach(tag, *tags)
1301 if(is_client_tagged(c, *tag))
1303 tag_push(L, *tag);
1304 lua_rawseti(L, -2, ++j);
1307 return 1;
1310 /** Raise a client on top of others which are on the same layer.
1311 * \param L The Lua VM state.
1312 * \luastack
1313 * \lvalue A client.
1315 static int
1316 luaA_client_raise(lua_State *L)
1318 client_t *c = luaA_client_checkudata(L, 1);
1319 client_raise(c);
1320 return 0;
1323 /** Lower a client on bottom of others which are on the same layer.
1324 * \param L The Lua VM state.
1325 * \luastack
1326 * \lvalue A client.
1328 static int
1329 luaA_client_lower(lua_State *L)
1331 client_t *c = luaA_client_checkudata(L, 1);
1332 client_lower(c);
1333 return 0;
1336 /** Redraw a client by unmapping and mapping it quickly.
1337 * \param L The Lua VM state.
1339 * \luastack
1340 * \lvalue A client.
1342 static int
1343 luaA_client_redraw(lua_State *L)
1345 client_t *c = luaA_client_checkudata(L, 1);
1347 xcb_unmap_window(globalconf.connection, c->win);
1348 xcb_map_window(globalconf.connection, c->win);
1350 /* Set the focus on the current window if the redraw has been
1351 performed on the window where the pointer is currently on
1352 because after the unmapping/mapping, the focus is lost */
1353 if(globalconf.screen_focus->client_focus == c)
1355 client_unfocus(c);
1356 client_focus(c);
1359 return 0;
1362 /** Stop managing a client.
1363 * \param L The Lua VM state.
1364 * \return The number of elements pushed on stack.
1365 * \luastack
1366 * \lvalue A client.
1368 static int
1369 luaA_client_unmanage(lua_State *L)
1371 client_t *c = luaA_client_checkudata(L, 1);
1372 client_unmanage(c);
1373 return 0;
1376 /** Return client geometry.
1377 * \param L The Lua VM state.
1378 * \return The number of elements pushed on stack.
1379 * \luastack
1380 * \lparam A table with new coordinates, or none.
1381 * \lreturn A table with client coordinates.
1383 static int
1384 luaA_client_geometry(lua_State *L)
1386 client_t *c = luaA_client_checkudata(L, 1);
1388 if(lua_gettop(L) == 2)
1390 area_t geometry;
1392 luaA_checktable(L, 2);
1393 geometry.x = luaA_getopt_number(L, 2, "x", c->geometry.x);
1394 geometry.y = luaA_getopt_number(L, 2, "y", c->geometry.y);
1395 if(client_isfixed(c))
1397 geometry.width = c->geometry.width;
1398 geometry.height = c->geometry.height;
1400 else
1402 geometry.width = luaA_getopt_number(L, 2, "width", c->geometry.width);
1403 geometry.height = luaA_getopt_number(L, 2, "height", c->geometry.height);
1406 client_resize(c, geometry, c->size_hints_honor);
1409 return luaA_pusharea(L, c->geometry);
1412 /** Push a strut type to a table on stack.
1413 * \param L The Lua VM state.
1414 * \param struts The struts to push.
1415 * \return The number of elements pushed on stack.
1417 static inline int
1418 luaA_pushstruts(lua_State *L, strut_t struts)
1420 lua_createtable(L, 4, 0);
1421 lua_pushnumber(L, struts.left);
1422 lua_setfield(L, -2, "left");
1423 lua_pushnumber(L, struts.right);
1424 lua_setfield(L, -2, "right");
1425 lua_pushnumber(L, struts.top);
1426 lua_setfield(L, -2, "top");
1427 lua_pushnumber(L, struts.bottom);
1428 lua_setfield(L, -2, "bottom");
1429 return 1;
1432 /** Return client struts (reserved space at the edge of the screen).
1433 * \param L The Lua VM state.
1434 * \return The number of elements pushed on stack.
1435 * \luastack
1436 * \lparam A table with new strut values, or none.
1437 * \lreturn A table with strut values.
1439 static int
1440 luaA_client_struts(lua_State *L)
1442 client_t *c = luaA_client_checkudata(L, 1);
1444 if(lua_gettop(L) == 2)
1446 strut_t struts;
1447 area_t screen_area = display_area_get(c->phys_screen);
1449 struts.left = luaA_getopt_number(L, 2, "left", c->strut.left);
1450 struts.right = luaA_getopt_number(L, 2, "right", c->strut.right);
1451 struts.top = luaA_getopt_number(L, 2, "top", c->strut.top);
1452 struts.bottom = luaA_getopt_number(L, 2, "bottom", c->strut.bottom);
1454 if(struts.left != c->strut.left || struts.right != c->strut.right ||
1455 struts.top != c->strut.top || struts.bottom != c->strut.bottom) {
1456 /* Struts are not so well defined in the context of xinerama. So we just
1457 * give the entire root window and let the window manager decide. */
1458 struts.left_start_y = 0;
1459 struts.left_end_y = !struts.left ? 0 : screen_area.height;
1460 struts.right_start_y = 0;
1461 struts.right_end_y = !struts.right ? 0 : screen_area.height;
1462 struts.top_start_x = 0;
1463 struts.top_end_x = !struts.top ? 0 : screen_area.width;
1464 struts.bottom_start_x = 0;
1465 struts.bottom_end_x = !struts.bottom ? 0 : screen_area.width;
1467 c->strut = struts;
1469 ewmh_update_client_strut(c);
1471 hook_property(client, c, "struts");
1475 return luaA_pushstruts(L, c->strut);
1478 /** Client newindex.
1479 * \param L The Lua VM state.
1480 * \return The number of elements pushed on stack.
1482 static int
1483 luaA_client_newindex(lua_State *L)
1485 size_t len;
1486 client_t *c = luaA_client_checkudata(L, 1);
1487 const char *buf = luaL_checklstring(L, 2, &len);
1488 bool b;
1489 double d;
1490 int i;
1492 switch(a_tokenize(buf, len))
1494 case A_TK_SCREEN:
1495 if(globalconf.xinerama_is_active)
1497 i = luaL_checknumber(L, 3) - 1;
1498 luaA_checkscreen(i);
1499 screen_client_moveto(c, &globalconf.screens.tab[i], true, true);
1501 break;
1502 case A_TK_HIDE:
1503 b = luaA_checkboolean(L, 3);
1504 if(b != c->ishidden)
1506 client_need_reban(c);
1507 c->ishidden = b;
1508 client_need_reban(c);
1509 hook_property(client, c, "hide");
1511 break;
1512 case A_TK_MINIMIZED:
1513 client_setminimized(c, luaA_checkboolean(L, 3));
1514 break;
1515 case A_TK_FULLSCREEN:
1516 client_setfullscreen(c, luaA_checkboolean(L, 3));
1517 break;
1518 case A_TK_MAXIMIZED_HORIZONTAL:
1519 client_setmaxhoriz(c, luaA_checkboolean(L, 3));
1520 break;
1521 case A_TK_MAXIMIZED_VERTICAL:
1522 client_setmaxvert(c, luaA_checkboolean(L, 3));
1523 break;
1524 case A_TK_ICON:
1525 luaA_object_unref_item(L, 1, c->icon);
1526 c->icon = NULL;
1527 c->icon = luaA_object_ref_item(L, 1, 3);
1528 /* execute hook */
1529 hook_property(client, c, "icon");
1530 break;
1531 case A_TK_OPACITY:
1532 if(lua_isnil(L, 3))
1533 window_opacity_set(c->win, -1);
1534 else
1536 d = luaL_checknumber(L, 3);
1537 if(d >= 0 && d <= 1)
1538 window_opacity_set(c->win, d);
1540 break;
1541 case A_TK_STICKY:
1542 client_setsticky(c, luaA_checkboolean(L, 3));
1543 break;
1544 case A_TK_SIZE_HINTS_HONOR:
1545 c->size_hints_honor = luaA_checkboolean(L, 3);
1546 hook_property(client, c, "size_hints_honor");
1547 break;
1548 case A_TK_BORDER_WIDTH:
1549 client_setborder(c, luaL_checknumber(L, 3));
1550 break;
1551 case A_TK_ONTOP:
1552 client_setontop(c, luaA_checkboolean(L, 3));
1553 break;
1554 case A_TK_ABOVE:
1555 client_setabove(c, luaA_checkboolean(L, 3));
1556 break;
1557 case A_TK_BELOW:
1558 client_setbelow(c, luaA_checkboolean(L, 3));
1559 break;
1560 case A_TK_URGENT:
1561 client_seturgent(c, luaA_checkboolean(L, 3));
1562 break;
1563 case A_TK_BORDER_COLOR:
1564 if((buf = luaL_checklstring(L, 3, &len))
1565 && xcolor_init_reply(xcolor_init_unchecked(&c->border_color, buf, len)))
1566 xcb_change_window_attributes(globalconf.connection, c->win,
1567 XCB_CW_BORDER_PIXEL, &c->border_color.pixel);
1568 break;
1569 case A_TK_TITLEBAR:
1570 if(lua_isnil(L, 3))
1571 titlebar_client_detach(c);
1572 else
1573 titlebar_client_attach(c);
1574 break;
1575 case A_TK_SKIP_TASKBAR:
1576 c->skiptb = luaA_checkboolean(L, 3);
1577 hook_property(client, c, "skip_taskbar");
1578 break;
1579 case A_TK_KEYS:
1580 luaA_object_unref_item(L, 1, c->keys);
1581 c->keys = luaA_object_ref_item(L, 1, 3);
1582 break;
1583 case A_TK_BUTTONS:
1584 luaA_object_unref_item(L, 1, c->buttons);
1585 c->buttons = luaA_object_ref_item(L, 1, 3);
1586 break;
1587 default:
1588 return 0;
1591 return 0;
1594 /** Client object.
1595 * \param L The Lua VM state.
1596 * \return The number of elements pushed on stack.
1597 * \luastack
1598 * \lfield id The window X id.
1599 * \lfield name The client title.
1600 * \lfield skip_taskbar If true the client won't be shown in the tasklist.
1601 * \lfield type The window type (desktop, normal, dock, …).
1602 * \lfield class The client class.
1603 * \lfield instance The client instance.
1604 * \lfield pid The client PID, if available.
1605 * \lfield role The window role, if available.
1606 * \lfield machine The machine client is running on.
1607 * \lfield icon_name The client name when iconified.
1608 * \lfield screen Client screen number.
1609 * \lfield hide Define if the client must be hidden, i.e. never mapped,
1610 * invisible in taskbar.
1611 * \lfield minimized Define it the client must be iconify, i.e. only visible in
1612 * taskbar.
1613 * \lfield size_hints_honor Honor size hints, i.e. respect size ratio.
1614 * \lfield border_width The client border width.
1615 * \lfield border_color The client border color.
1616 * \lfield titlebar The client titlebar.
1617 * \lfield urgent The client urgent state.
1618 * \lfield content An image representing the client window content (screenshot).
1619 * \lfield focus The focused client.
1620 * \lfield opacity The client opacity between 0 and 1.
1621 * \lfield ontop The client is on top of every other windows.
1622 * \lfield above The client is above normal windows.
1623 * \lfield below The client is below normal windows.
1624 * \lfield fullscreen The client is fullscreen or not.
1625 * \lfield maximized_horizontal The client is maximized horizontally or not.
1626 * \lfield maximized_vertical The client is maximized vertically or not.
1627 * \lfield transient_for Return the client the window is transient for.
1628 * \lfield group_id Identification unique to a group of windows.
1629 * \lfield leader_id Identification unique to windows spawned by the same command.
1630 * \lfield size_hints A table with size hints of the client: user_position,
1631 * user_size, program_position, program_size, etc.
1633 static int
1634 luaA_client_index(lua_State *L)
1636 size_t len;
1637 ssize_t slen;
1638 client_t *c = luaA_client_checkudata(L, 1);
1639 const char *buf = luaL_checklstring(L, 2, &len);
1640 char *value;
1641 void *data;
1642 xcb_get_property_cookie_t prop_c;
1643 xcb_get_property_reply_t *prop_r = NULL;
1644 double d;
1646 if(luaA_usemetatable(L, 1, 2))
1647 return 1;
1649 switch(a_tokenize(buf, len))
1651 case A_TK_NAME:
1652 lua_pushstring(L, c->name);
1653 break;
1654 case A_TK_TRANSIENT_FOR:
1655 return client_push(globalconf.L, c->transient_for);
1656 case A_TK_SKIP_TASKBAR:
1657 lua_pushboolean(L, c->skiptb);
1658 break;
1659 case A_TK_CONTENT:
1660 return client_getcontent(c);
1661 case A_TK_TYPE:
1662 switch(c->type)
1664 case WINDOW_TYPE_DESKTOP:
1665 lua_pushliteral(L, "desktop");
1666 break;
1667 case WINDOW_TYPE_DOCK:
1668 lua_pushliteral(L, "dock");
1669 break;
1670 case WINDOW_TYPE_SPLASH:
1671 lua_pushliteral(L, "splash");
1672 break;
1673 case WINDOW_TYPE_DIALOG:
1674 lua_pushliteral(L, "dialog");
1675 break;
1676 case WINDOW_TYPE_MENU:
1677 lua_pushliteral(L, "menu");
1678 break;
1679 case WINDOW_TYPE_TOOLBAR:
1680 lua_pushliteral(L, "toolbar");
1681 break;
1682 case WINDOW_TYPE_UTILITY:
1683 lua_pushliteral(L, "utility");
1684 break;
1685 case WINDOW_TYPE_DROPDOWN_MENU:
1686 lua_pushliteral(L, "dropdown_menu");
1687 break;
1688 case WINDOW_TYPE_POPUP_MENU:
1689 lua_pushliteral(L, "popup_menu");
1690 break;
1691 case WINDOW_TYPE_TOOLTIP:
1692 lua_pushliteral(L, "tooltip");
1693 break;
1694 case WINDOW_TYPE_NOTIFICATION:
1695 lua_pushliteral(L, "notification");
1696 break;
1697 case WINDOW_TYPE_COMBO:
1698 lua_pushliteral(L, "combo");
1699 break;
1700 case WINDOW_TYPE_DND:
1701 lua_pushliteral(L, "dnd");
1702 break;
1703 case WINDOW_TYPE_NORMAL:
1704 lua_pushliteral(L, "normal");
1705 break;
1707 break;
1708 case A_TK_CLASS:
1709 lua_pushstring(L, c->class);
1710 break;
1711 case A_TK_INSTANCE:
1712 lua_pushstring(L, c->instance);
1713 break;
1714 case A_TK_ROLE:
1715 if(!xutil_text_prop_get(globalconf.connection, c->win,
1716 WM_WINDOW_ROLE, &value, &slen))
1717 return 0;
1718 lua_pushlstring(L, value, slen);
1719 p_delete(&value);
1720 break;
1721 case A_TK_PID:
1722 prop_c = xcb_get_property_unchecked(globalconf.connection, false, c->win, _NET_WM_PID, CARDINAL, 0L, 1L);
1723 prop_r = xcb_get_property_reply(globalconf.connection, prop_c, NULL);
1725 if(prop_r && prop_r->value_len && (data = xcb_get_property_value(prop_r)))
1726 lua_pushnumber(L, *(uint32_t *)data);
1727 else
1729 p_delete(&prop_r);
1730 return 0;
1732 break;
1733 case A_TK_ID:
1734 lua_pushnumber(L, c->win);
1735 break;
1736 case A_TK_LEADER_ID:
1737 lua_pushnumber(L, c->leader_win);
1738 break;
1739 case A_TK_MACHINE:
1740 if(!xutil_text_prop_get(globalconf.connection, c->win,
1741 WM_CLIENT_MACHINE, &value, &slen))
1742 return 0;
1743 lua_pushlstring(L, value, slen);
1744 p_delete(&value);
1745 break;
1746 case A_TK_ICON_NAME:
1747 lua_pushstring(L, c->icon_name);
1748 break;
1749 case A_TK_SCREEN:
1750 lua_pushnumber(L, 1 + screen_array_indexof(&globalconf.screens, c->screen));
1751 break;
1752 case A_TK_HIDE:
1753 lua_pushboolean(L, c->ishidden);
1754 break;
1755 case A_TK_MINIMIZED:
1756 lua_pushboolean(L, c->isminimized);
1757 break;
1758 case A_TK_FULLSCREEN:
1759 lua_pushboolean(L, c->isfullscreen);
1760 break;
1761 case A_TK_GROUP_ID:
1762 if(c->group_win)
1763 lua_pushnumber(L, c->group_win);
1764 else
1765 return 0;
1766 break;
1767 case A_TK_MAXIMIZED_HORIZONTAL:
1768 lua_pushboolean(L, c->ismaxhoriz);
1769 break;
1770 case A_TK_MAXIMIZED_VERTICAL:
1771 lua_pushboolean(L, c->ismaxvert);
1772 break;
1773 case A_TK_ICON:
1774 luaA_object_push_item(L, 1, c->icon);
1775 break;
1776 case A_TK_OPACITY:
1777 if((d = window_opacity_get(c->win)) >= 0)
1778 lua_pushnumber(L, d);
1779 else
1780 return 0;
1781 break;
1782 case A_TK_ONTOP:
1783 lua_pushboolean(L, c->isontop);
1784 break;
1785 case A_TK_ABOVE:
1786 lua_pushboolean(L, c->isabove);
1787 break;
1788 case A_TK_BELOW:
1789 lua_pushboolean(L, c->isbelow);
1790 break;
1791 case A_TK_STICKY:
1792 lua_pushboolean(L, c->issticky);
1793 break;
1794 case A_TK_SIZE_HINTS_HONOR:
1795 lua_pushboolean(L, c->size_hints_honor);
1796 break;
1797 case A_TK_BORDER_WIDTH:
1798 lua_pushnumber(L, c->border);
1799 break;
1800 case A_TK_BORDER_COLOR:
1801 luaA_pushxcolor(L, &c->border_color);
1802 break;
1803 case A_TK_TITLEBAR:
1804 return wibox_push(L, c->titlebar);
1805 case A_TK_URGENT:
1806 lua_pushboolean(L, c->isurgent);
1807 break;
1808 case A_TK_SIZE_HINTS:
1810 const char *u_or_p = NULL;
1812 lua_createtable(L, 0, 1);
1814 if(c->size_hints.flags & XCB_SIZE_HINT_US_POSITION)
1815 u_or_p = "user_position";
1816 else if(c->size_hints.flags & XCB_SIZE_HINT_P_POSITION)
1817 u_or_p = "program_position";
1819 if(u_or_p)
1821 lua_createtable(L, 0, 2);
1822 lua_pushnumber(L, c->size_hints.x);
1823 lua_setfield(L, -2, "x");
1824 lua_pushnumber(L, c->size_hints.y);
1825 lua_setfield(L, -2, "y");
1826 lua_setfield(L, -2, u_or_p);
1827 u_or_p = NULL;
1830 if(c->size_hints.flags & XCB_SIZE_HINT_US_SIZE)
1831 u_or_p = "user_size";
1832 else if(c->size_hints.flags & XCB_SIZE_HINT_P_SIZE)
1833 u_or_p = "program_size";
1835 if(u_or_p)
1837 lua_createtable(L, 0, 2);
1838 lua_pushnumber(L, c->size_hints.width);
1839 lua_setfield(L, -2, "width");
1840 lua_pushnumber(L, c->size_hints.height);
1841 lua_setfield(L, -2, "height");
1842 lua_setfield(L, -2, u_or_p);
1845 if(c->size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE)
1847 lua_pushnumber(L, c->size_hints.min_width);
1848 lua_setfield(L, -2, "min_width");
1849 lua_pushnumber(L, c->size_hints.min_height);
1850 lua_setfield(L, -2, "min_height");
1853 if(c->size_hints.flags & XCB_SIZE_HINT_P_MAX_SIZE)
1855 lua_pushnumber(L, c->size_hints.max_width);
1856 lua_setfield(L, -2, "max_width");
1857 lua_pushnumber(L, c->size_hints.max_height);
1858 lua_setfield(L, -2, "max_height");
1861 if(c->size_hints.flags & XCB_SIZE_HINT_P_RESIZE_INC)
1863 lua_pushnumber(L, c->size_hints.width_inc);
1864 lua_setfield(L, -2, "width_inc");
1865 lua_pushnumber(L, c->size_hints.height_inc);
1866 lua_setfield(L, -2, "height_inc");
1869 if(c->size_hints.flags & XCB_SIZE_HINT_P_ASPECT)
1871 lua_pushnumber(L, c->size_hints.min_aspect_num);
1872 lua_setfield(L, -2, "min_aspect_num");
1873 lua_pushnumber(L, c->size_hints.min_aspect_den);
1874 lua_setfield(L, -2, "min_aspect_den");
1875 lua_pushnumber(L, c->size_hints.max_aspect_num);
1876 lua_setfield(L, -2, "max_aspect_num");
1877 lua_pushnumber(L, c->size_hints.max_aspect_den);
1878 lua_setfield(L, -2, "max_aspect_den");
1881 if(c->size_hints.flags & XCB_SIZE_HINT_BASE_SIZE)
1883 lua_pushnumber(L, c->size_hints.base_width);
1884 lua_setfield(L, -2, "base_width");
1885 lua_pushnumber(L, c->size_hints.base_height);
1886 lua_setfield(L, -2, "base_height");
1889 if(c->size_hints.flags & XCB_SIZE_HINT_P_WIN_GRAVITY)
1891 switch(c->size_hints.win_gravity)
1893 default:
1894 lua_pushliteral(L, "north_west");
1895 break;
1896 case XCB_GRAVITY_NORTH:
1897 lua_pushliteral(L, "north");
1898 break;
1899 case XCB_GRAVITY_NORTH_EAST:
1900 lua_pushliteral(L, "north_east");
1901 break;
1902 case XCB_GRAVITY_WEST:
1903 lua_pushliteral(L, "west");
1904 break;
1905 case XCB_GRAVITY_CENTER:
1906 lua_pushliteral(L, "center");
1907 break;
1908 case XCB_GRAVITY_EAST:
1909 lua_pushliteral(L, "east");
1910 break;
1911 case XCB_GRAVITY_SOUTH_WEST:
1912 lua_pushliteral(L, "south_west");
1913 break;
1914 case XCB_GRAVITY_SOUTH:
1915 lua_pushliteral(L, "south");
1916 break;
1917 case XCB_GRAVITY_SOUTH_EAST:
1918 lua_pushliteral(L, "south_east");
1919 break;
1920 case XCB_GRAVITY_STATIC:
1921 lua_pushliteral(L, "static");
1922 break;
1924 lua_setfield(L, -2, "win_gravity");
1927 break;
1928 case A_TK_KEYS:
1929 return luaA_object_push_item(L, 1, c->keys);
1930 case A_TK_BUTTONS:
1931 return luaA_object_push_item(L, 1, c->buttons);
1932 default:
1933 return 0;
1936 return 1;
1939 /* Client module.
1940 * \param L The Lua VM state.
1941 * \return The number of pushed elements.
1943 static int
1944 luaA_client_module_index(lua_State *L)
1946 size_t len;
1947 const char *buf = luaL_checklstring(L, 2, &len);
1949 switch(a_tokenize(buf, len))
1951 case A_TK_FOCUS:
1952 return client_push(globalconf.L, globalconf.screen_focus->client_focus);
1953 break;
1954 default:
1955 return 0;
1959 /* Client module new index.
1960 * \param L The Lua VM state.
1961 * \return The number of pushed elements.
1963 static int
1964 luaA_client_module_newindex(lua_State *L)
1966 size_t len;
1967 const char *buf = luaL_checklstring(L, 2, &len);
1968 client_t *c;
1970 switch(a_tokenize(buf, len))
1972 case A_TK_FOCUS:
1973 c = luaA_client_checkudata(L, 3);
1974 client_focus(c);
1975 break;
1976 default:
1977 break;
1980 return 0;
1983 const struct luaL_reg awesome_client_methods[] =
1985 LUA_CLASS_METHODS(client)
1986 { "get", luaA_client_get },
1987 { "__index", luaA_client_module_index },
1988 { "__newindex", luaA_client_module_newindex },
1989 { NULL, NULL }
1991 const struct luaL_reg awesome_client_meta[] =
1993 LUA_OBJECT_META(client)
1994 { "isvisible", luaA_client_isvisible },
1995 { "geometry", luaA_client_geometry },
1996 { "struts", luaA_client_struts },
1997 { "tags", luaA_client_tags },
1998 { "kill", luaA_client_kill },
1999 { "swap", luaA_client_swap },
2000 { "raise", luaA_client_raise },
2001 { "lower", luaA_client_lower },
2002 { "redraw", luaA_client_redraw },
2003 { "unmanage", luaA_client_unmanage },
2004 { "__index", luaA_client_index },
2005 { "__newindex", luaA_client_newindex },
2006 { "__gc", luaA_client_gc },
2007 { NULL, NULL }
2010 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80