awful.layout: also arrange on client tag change
[awesome.git] / client.c
blobae63f9072155482c02e559b545dc510e89a9d4d5
1 /*
2 * client.c - client management
4 * Copyright © 2007-2009 Julien Danjou <julien@danjou.info>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <xcb/xcb_atom.h>
23 #include <xcb/xcb_image.h>
25 #include "tag.h"
26 #include "ewmh.h"
27 #include "screen.h"
28 #include "titlebar.h"
29 #include "systray.h"
30 #include "property.h"
31 #include "spawn.h"
32 #include "common/atoms.h"
33 #include "common/xutil.h"
35 DO_LUA_TOSTRING(client_t, client, "client")
37 client_t *
38 luaA_client_checkudata(lua_State *L, int ud)
40 client_t *c = luaL_checkudata(L, ud, "client");
41 if(c->invalid)
42 luaL_error(L, "client is invalid\n");
43 return c;
46 /** Collect a client.
47 * \param L The Lua VM state.
48 * \return The number of element pushed on stack.
50 static int
51 luaA_client_gc(lua_State *L)
53 client_t *c = luaL_checkudata(L, 1, "client");
54 button_array_wipe(&c->buttons);
55 key_array_wipe(&c->keys);
56 image_unref(L, c->icon);
57 xcb_get_wm_protocols_reply_wipe(&c->protocols);
58 p_delete(&c->class);
59 p_delete(&c->startup_id);
60 p_delete(&c->instance);
61 p_delete(&c->icon_name);
62 p_delete(&c->name);
63 return luaA_object_gc(L);
66 /** Change the clients urgency flag.
67 * \param c The client
68 * \param urgent The new flag state
70 void
71 client_seturgent(client_t *c, bool urgent)
73 if(c->isurgent != urgent)
75 xcb_get_property_cookie_t hints =
76 xcb_get_wm_hints_unchecked(globalconf.connection, c->win);
78 c->isurgent = urgent;
79 ewmh_client_update_hints(c);
81 /* update ICCCM hints */
82 xcb_wm_hints_t wmh;
83 xcb_get_wm_hints_reply(globalconf.connection, hints, &wmh, NULL);
85 if(urgent)
86 wmh.flags |= XCB_WM_HINT_X_URGENCY;
87 else
88 wmh.flags &= ~XCB_WM_HINT_X_URGENCY;
90 xcb_set_wm_hints(globalconf.connection, c->win, &wmh);
92 hook_property(client, c, "urgent");
96 /** Returns true if a client is tagged
97 * with one of the tags of the specified screen.
98 * \param c The client to check.
99 * \param screen Virtual screen.
100 * \return true if the client is visible, false otherwise.
102 bool
103 client_maybevisible(client_t *c, screen_t *screen)
105 if(c->screen == screen)
107 if(c->issticky || c->type == WINDOW_TYPE_DESKTOP)
108 return true;
110 foreach(tag, screen->tags)
111 if((*tag)->selected && is_client_tagged(c, *tag))
112 return true;
114 return false;
117 /** Return the content of a client as an image.
118 * That's just take a screenshot.
119 * \param c The client.
120 * \return 1 if the image has been pushed on stack, false otherwise.
122 static int
123 client_getcontent(client_t *c)
125 xcb_image_t *ximage = xcb_image_get(globalconf.connection,
126 c->win,
127 0, 0,
128 c->geometries.internal.width,
129 c->geometries.internal.height,
130 ~0, XCB_IMAGE_FORMAT_Z_PIXMAP);
131 int retval = 0;
133 if(ximage)
135 if(ximage->bpp >= 24)
137 uint32_t *data = p_alloca(uint32_t, ximage->width * ximage->height);
139 for(int y = 0; y < ximage->height; y++)
140 for(int x = 0; x < ximage->width; x++)
142 data[y * ximage->width + x] = xcb_image_get_pixel(ximage, x, y);
143 data[y * ximage->width + x] |= 0xff000000; /* set alpha to 0xff */
146 retval = image_new_from_argb32(ximage->width, ximage->height, data);
148 xcb_image_destroy(ximage);
151 return retval;
154 /** Get a client by its window.
155 * \param w The client window to find.
156 * \return A client pointer if found, NULL otherwise.
158 client_t *
159 client_getbywin(xcb_window_t w)
161 foreach(c, globalconf.clients)
162 if((*c)->win == w)
163 return *c;
165 return NULL;
168 /** Record that a client lost focus.
169 * \param c Client being unfocused
171 void
172 client_unfocus_update(client_t *c)
174 globalconf.screens.tab[c->phys_screen].client_focus = NULL;
175 ewmh_update_net_active_window(c->phys_screen);
177 /* Call hook */
178 if(globalconf.hooks.unfocus != LUA_REFNIL)
180 client_push(globalconf.L, c);
181 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.unfocus, 1, 0);
186 /** Unfocus a client.
187 * \param c The client.
189 void
190 client_unfocus(client_t *c)
193 xcb_window_t root_win = xutil_screen_get(globalconf.connection, c->phys_screen)->root;
194 /* Set focus on root window, so no events leak to the current window.
195 * This kind of inlines client_setfocus(), but a root window will never have
196 * the WM_TAKE_FOCUS protocol.
198 xcb_set_input_focus(globalconf.connection, XCB_INPUT_FOCUS_PARENT,
199 root_win, XCB_CURRENT_TIME);
201 client_unfocus_update(c);
204 /** Check if client supports protocol a protocole in WM_PROTOCOL.
205 * \param c The client.
206 * \return True if client has the atom in protocol, false otherwise.
208 bool
209 client_hasproto(client_t *c, xcb_atom_t atom)
211 uint32_t i;
213 for(i = 0; i < c->protocols.atoms_len; i++)
214 if(c->protocols.atoms[i] == atom)
215 return true;
216 return false;
219 /** Sets focus on window - using xcb_set_input_focus or WM_TAKE_FOCUS
220 * \param c Client that should get focus
221 * \param set_input_focus Should we call xcb_set_input_focus
223 void
224 client_setfocus(client_t *c, bool set_input_focus)
226 bool takefocus = client_hasproto(c, WM_TAKE_FOCUS);
227 if(set_input_focus)
228 xcb_set_input_focus(globalconf.connection, XCB_INPUT_FOCUS_PARENT,
229 c->win, XCB_CURRENT_TIME);
230 if(takefocus)
231 window_takefocus(c->win);
234 /** Ban client and move it out of the viewport.
235 * \param c The client.
237 void
238 client_ban(client_t *c)
240 if(!c->isbanned)
242 xcb_unmap_window(globalconf.connection, c->win);
244 c->isbanned = true;
246 if(globalconf.screens.tab[c->phys_screen].prev_client_focus == c)
247 globalconf.screens.tab[c->phys_screen].prev_client_focus = NULL;
249 /* Wait until the last moment to take away the focus from the window. */
250 if(globalconf.screens.tab[c->phys_screen].client_focus == c)
251 client_unfocus(c);
255 /** Record that a client got focus.
256 * \param c Client being focused.
258 void
259 client_focus_update(client_t *c)
261 if(!client_maybevisible(c, c->screen))
263 /* Focus previously focused client */
264 client_focus(globalconf.screen_focus->prev_client_focus);
265 return;
268 if(globalconf.screen_focus
269 && globalconf.screen_focus->client_focus)
271 if (globalconf.screen_focus->client_focus != c)
272 client_unfocus_update(globalconf.screen_focus->client_focus);
273 else
274 /* Already focused */
275 return;
277 /* stop hiding client */
278 c->ishidden = false;
279 client_setminimized(c, false);
281 /* unban the client before focusing for consistency */
282 client_unban(c);
284 globalconf.screen_focus = &globalconf.screens.tab[c->phys_screen];
285 globalconf.screen_focus->prev_client_focus = c;
286 globalconf.screen_focus->client_focus = c;
288 /* according to EWMH, we have to remove the urgent state from a client */
289 client_seturgent(c, false);
291 ewmh_update_net_active_window(c->phys_screen);
293 /* execute hook */
294 if(globalconf.hooks.focus != LUA_REFNIL)
296 client_push(globalconf.L, c);
297 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.focus, 1, 0);
302 /** Give focus to client, or to first client if client is NULL.
303 * \param c The client or NULL.
305 void
306 client_focus(client_t *c)
308 /* We have to set focus on first client */
309 if(!c && globalconf.clients.len && !(c = globalconf.clients.tab[0]))
310 return;
312 if(!client_maybevisible(c, c->screen))
313 return;
315 if (!c->nofocus)
316 client_focus_update(c);
318 client_setfocus(c, !c->nofocus);
321 /** Stack a window below.
322 * \param c The client.
323 * \param previous The previous window on the stack.
324 * \param return The next-previous!
326 static xcb_window_t
327 client_stack_above(client_t *c, xcb_window_t previous)
329 uint32_t config_win_vals[2];
331 config_win_vals[0] = previous;
332 config_win_vals[1] = XCB_STACK_MODE_ABOVE;
334 xcb_configure_window(globalconf.connection, c->win,
335 XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE,
336 config_win_vals);
338 config_win_vals[0] = c->win;
340 if(c->titlebar)
342 xcb_configure_window(globalconf.connection,
343 c->titlebar->sw.window,
344 XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE,
345 config_win_vals);
346 previous = c->titlebar->sw.window;
348 else
349 previous = c->win;
351 /* stack transient window on top of their parents */
352 foreach(node, globalconf.stack)
353 if((*node)->transient_for == c)
354 previous = client_stack_above(*node, previous);
356 return previous;
359 /** Stacking layout layers */
360 typedef enum
362 /** This one is a special layer */
363 LAYER_IGNORE,
364 LAYER_DESKTOP,
365 LAYER_BELOW,
366 LAYER_NORMAL,
367 LAYER_ABOVE,
368 LAYER_FULLSCREEN,
369 LAYER_ONTOP,
370 /** This one only used for counting and is not a real layer */
371 LAYER_COUNT
372 } layer_t;
374 /** Get the real layer of a client according to its attribute (fullscreen, …)
375 * \param c The client.
376 * \return The real layer.
378 static layer_t
379 client_layer_translator(client_t *c)
381 /* first deal with user set attributes */
382 if(c->isontop)
383 return LAYER_ONTOP;
384 else if(c->isfullscreen)
385 return LAYER_FULLSCREEN;
386 else if(c->isabove)
387 return LAYER_ABOVE;
388 else if(c->isbelow)
389 return LAYER_BELOW;
391 /* check for transient attr */
392 if(c->transient_for)
393 return LAYER_IGNORE;
395 /* then deal with windows type */
396 switch(c->type)
398 case WINDOW_TYPE_DESKTOP:
399 return LAYER_DESKTOP;
400 default:
401 break;
404 return LAYER_NORMAL;
407 /** Restack clients.
408 * \todo It might be worth stopping to restack everyone and only stack `c'
409 * relatively to the first matching in the list.
411 void
412 client_stack_refresh()
414 uint32_t config_win_vals[2];
415 layer_t layer;
417 if (!globalconf.client_need_stack_refresh)
418 return;
419 globalconf.client_need_stack_refresh = false;
421 config_win_vals[0] = XCB_NONE;
422 config_win_vals[1] = XCB_STACK_MODE_ABOVE;
424 /* stack desktop windows */
425 for(layer = LAYER_DESKTOP; layer < LAYER_BELOW; layer++)
426 foreach(node, globalconf.stack)
427 if(client_layer_translator(*node) == layer)
428 config_win_vals[0] = client_stack_above(*node,
429 config_win_vals[0]);
431 /* first stack not ontop wibox window */
432 foreach(_sb, globalconf.wiboxes)
434 wibox_t *sb = *_sb;
435 if(!sb->ontop)
437 xcb_configure_window(globalconf.connection,
438 sb->sw.window,
439 XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE,
440 config_win_vals);
441 config_win_vals[0] = sb->sw.window;
445 /* then stack clients */
446 for(layer = LAYER_BELOW; layer < LAYER_COUNT; layer++)
447 foreach(node, globalconf.stack)
448 if(client_layer_translator(*node) == layer)
449 config_win_vals[0] = client_stack_above(*node,
450 config_win_vals[0]);
452 /* then stack ontop wibox window */
453 foreach(_sb, globalconf.wiboxes)
455 wibox_t *sb = *_sb;
456 if(sb->ontop)
458 xcb_configure_window(globalconf.connection,
459 sb->sw.window,
460 XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE,
461 config_win_vals);
462 config_win_vals[0] = sb->sw.window;
467 /** Manage a new client.
468 * \param w The window.
469 * \param wgeom Window geometry.
470 * \param phys_screen Physical screen number.
471 * \param startup True if we are managing at startup time.
473 void
474 client_manage(xcb_window_t w, xcb_get_geometry_reply_t *wgeom, int phys_screen, bool startup)
476 xcb_get_property_cookie_t ewmh_icon_cookie;
477 client_t *c, *tc = NULL;
478 screen_t *screen;
479 const uint32_t select_input_val[] = { CLIENT_SELECT_INPUT_EVENT_MASK };
481 if(systray_iskdedockapp(w))
483 systray_request_handle(w, phys_screen, NULL);
484 return;
487 /* Send request to get NET_WM_ICON property as soon as possible... */
488 ewmh_icon_cookie = ewmh_window_icon_get_unchecked(w);
489 xcb_change_window_attributes(globalconf.connection, w, XCB_CW_EVENT_MASK, select_input_val);
491 c = client_new(globalconf.L);
492 /* Push client in client list */
493 client_array_push(&globalconf.clients, client_ref(globalconf.L, -1));
496 screen = c->screen = screen_getbycoord(&globalconf.screens.tab[phys_screen],
497 wgeom->x, wgeom->y);
499 c->phys_screen = phys_screen;
501 /* consider the window banned */
502 c->isbanned = true;
504 /* Initial values */
505 c->win = w;
506 c->geometry.x = wgeom->x;
507 c->geometry.y = wgeom->y;
508 /* Border will be added later. */
509 c->geometry.width = wgeom->width;
510 c->geometry.height = wgeom->height;
511 /* Also set internal geometry (client_ban() needs it). */
512 c->geometries.internal.x = wgeom->x;
513 c->geometries.internal.y = wgeom->y;
514 c->geometries.internal.width = wgeom->width;
515 c->geometries.internal.height = wgeom->height;
516 client_setborder(c, wgeom->border_width);
518 if(ewmh_window_icon_get_reply(ewmh_icon_cookie))
519 c->icon = image_ref(globalconf.L, -1);
521 /* we honor size hints by default */
522 c->size_hints_honor = true;
524 /* update hints */
525 property_update_wm_normal_hints(c, NULL);
526 property_update_wm_hints(c, NULL);
527 property_update_wm_transient_for(c, NULL);
528 property_update_wm_client_leader(c, NULL);
530 /* Recursively find the parent. */
531 for(tc = c; tc->transient_for; tc = tc->transient_for);
532 if(tc != c && tc->phys_screen == c->phys_screen)
533 screen = tc->screen;
535 /* Then check clients hints */
536 ewmh_client_check_hints(c);
538 /* move client to screen, but do not tag it */
539 screen_client_moveto(c, screen, false, true);
541 /* Push client in stack */
542 client_raise(c);
544 /* update window title */
545 property_update_wm_name(c);
546 property_update_wm_icon_name(c);
547 property_update_wm_class(c, NULL);
548 property_update_wm_protocols(c);
550 xutil_text_prop_get(globalconf.connection, c->win, _NET_STARTUP_ID, &c->startup_id, NULL);
552 /* update strut */
553 ewmh_process_client_strut(c, NULL);
555 ewmh_update_net_client_list(c->phys_screen);
557 /* Always stay in NORMAL_STATE. Even though iconified seems more
558 * appropriate sometimes. The only possible loss is that clients not using
559 * visibility events may continue to proces data (when banned).
560 * Without any exposes or other events the cost should be fairly limited though.
562 * Some clients may expect the window to be unmapped when STATE_ICONIFIED.
563 * Two conflicting parts of the ICCCM v2.0 (section 4.1.4):
565 * "Normal -> Iconic - The client should send a ClientMessage event as described later in this section."
566 * (note no explicit mention of unmapping, while Normal->Widthdrawn does mention that)
568 * "Once a client's window has left the Withdrawn state, the window will be mapped
569 * if it is in the Normal state and the window will be unmapped if it is in the Iconic state."
571 * At this stage it's just safer to keep it in normal state and avoid confusion.
573 window_state_set(c->win, XCB_WM_STATE_NORMAL);
575 if(!startup)
576 spawn_start_notify(c);
578 /* Call hook to notify list change */
579 if(globalconf.hooks.clients != LUA_REFNIL)
580 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.clients, 0, 0);
582 /* call hook */
583 if(globalconf.hooks.manage != LUA_REFNIL)
585 client_push(globalconf.L, c);
586 lua_pushboolean(globalconf.L, startup);
587 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.manage, 2, 0);
591 /** Compute client geometry with respect to its geometry hints.
592 * \param c The client.
593 * \param geometry The geometry that the client might receive.
594 * \return The geometry the client must take respecting its hints.
596 area_t
597 client_geometry_hints(client_t *c, area_t geometry)
599 int32_t basew, baseh, minw, minh;
601 /* base size is substituted with min size if not specified */
602 if(c->size_hints.flags & XCB_SIZE_HINT_P_SIZE)
604 basew = c->size_hints.base_width;
605 baseh = c->size_hints.base_height;
607 else if(c->size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE)
609 basew = c->size_hints.min_width;
610 baseh = c->size_hints.min_height;
612 else
613 basew = baseh = 0;
615 /* min size is substituted with base size if not specified */
616 if(c->size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE)
618 minw = c->size_hints.min_width;
619 minh = c->size_hints.min_height;
621 else if(c->size_hints.flags & XCB_SIZE_HINT_P_SIZE)
623 minw = c->size_hints.base_width;
624 minh = c->size_hints.base_height;
626 else
627 minw = minh = 0;
629 if(c->size_hints.flags & XCB_SIZE_HINT_P_ASPECT
630 && c->size_hints.min_aspect_num > 0
631 && c->size_hints.min_aspect_den > 0
632 && geometry.height - baseh > 0
633 && geometry.width - basew > 0)
635 double dx = (double) (geometry.width - basew);
636 double dy = (double) (geometry.height - baseh);
637 double min = (double) c->size_hints.min_aspect_num / (double) c->size_hints.min_aspect_den;
638 double max = (double) c->size_hints.max_aspect_num / (double) c->size_hints.min_aspect_den;
639 double ratio = dx / dy;
640 if(max > 0 && min > 0 && ratio > 0)
642 if(ratio < min)
644 dy = (dx * min + dy) / (min * min + 1);
645 dx = dy * min;
646 geometry.width = (int) dx + basew;
647 geometry.height = (int) dy + baseh;
649 else if(ratio > max)
651 dy = (dx * min + dy) / (max * max + 1);
652 dx = dy * min;
653 geometry.width = (int) dx + basew;
654 geometry.height = (int) dy + baseh;
659 if(minw)
660 geometry.width = MAX(geometry.width, minw);
661 if(minh)
662 geometry.height = MAX(geometry.height, minh);
664 if(c->size_hints.flags & XCB_SIZE_HINT_P_MAX_SIZE)
666 if(c->size_hints.max_width)
667 geometry.width = MIN(geometry.width, c->size_hints.max_width);
668 if(c->size_hints.max_height)
669 geometry.height = MIN(geometry.height, c->size_hints.max_height);
672 if(c->size_hints.flags & (XCB_SIZE_HINT_P_RESIZE_INC | XCB_SIZE_HINT_BASE_SIZE)
673 && c->size_hints.width_inc && c->size_hints.height_inc)
675 uint16_t t1 = geometry.width, t2 = geometry.height;
676 unsigned_subtract(t1, basew);
677 unsigned_subtract(t2, baseh);
678 geometry.width -= t1 % c->size_hints.width_inc;
679 geometry.height -= t2 % c->size_hints.height_inc;
682 return geometry;
685 /** Resize client window.
686 * The sizse given as parameters are with titlebar and borders!
687 * \param c Client to resize.
688 * \param geometry New window geometry.
689 * \param hints Use size hints.
690 * \return true if an actual resize occurred.
692 bool
693 client_resize(client_t *c, area_t geometry, bool hints)
695 area_t geometry_internal;
696 area_t area;
698 /* offscreen appearance fixes */
699 area = display_area_get(c->phys_screen);
701 if(geometry.x > area.width)
702 geometry.x = area.width - geometry.width;
703 if(geometry.y > area.height)
704 geometry.y = area.height - geometry.height;
705 if(geometry.x + geometry.width < 0)
706 geometry.x = 0;
707 if(geometry.y + geometry.height < 0)
708 geometry.y = 0;
710 /* Real client geometry, please keep it contained to C code at the very least. */
711 geometry_internal = titlebar_geometry_remove(c->titlebar, c->border, geometry);
713 if(hints)
714 geometry_internal = client_geometry_hints(c, geometry_internal);
716 if(geometry_internal.width == 0 || geometry_internal.height == 0)
717 return false;
719 /* Also let client hints propegate to the "official" geometry. */
720 geometry = titlebar_geometry_add(c->titlebar, c->border, geometry_internal);
722 if(c->geometries.internal.x != geometry_internal.x
723 || c->geometries.internal.y != geometry_internal.y
724 || c->geometries.internal.width != geometry_internal.width
725 || c->geometries.internal.height != geometry_internal.height)
727 screen_t *new_screen = screen_getbycoord(c->screen,
728 geometry_internal.x, geometry_internal.y);
730 /* Values to configure a window is an array where values are
731 * stored according to 'value_mask' */
732 uint32_t values[4];
734 c->geometries.internal.x = values[0] = geometry_internal.x;
735 c->geometries.internal.y = values[1] = geometry_internal.y;
736 c->geometries.internal.width = values[2] = geometry_internal.width;
737 c->geometries.internal.height = values[3] = geometry_internal.height;
739 /* Also store geometry including border and titlebar. */
740 c->geometry = geometry;
742 titlebar_update_geometry(c);
744 xcb_configure_window(globalconf.connection, c->win,
745 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y
746 | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
747 values);
749 screen_client_moveto(c, new_screen, true, false);
751 /* execute hook */
752 hook_property(client, c, "geometry");
754 return true;
757 return false;
760 /** Set a client minimized, or not.
761 * \param c The client.
762 * \param s Set or not the client minimized.
764 void
765 client_setminimized(client_t *c, bool s)
767 if(c->isminimized != s)
769 client_need_reban(c);
770 c->isminimized = s;
771 client_need_reban(c);
772 if(s)
773 window_state_set(c->win, XCB_WM_STATE_ICONIC);
774 else
775 window_state_set(c->win, XCB_WM_STATE_NORMAL);
776 ewmh_client_update_hints(c);
777 /* execute hook */
778 hook_property(client, c, "minimized");
782 /** Set a client sticky, or not.
783 * \param c The client.
784 * \param s Set or not the client sticky.
786 void
787 client_setsticky(client_t *c, bool s)
789 if(c->issticky != s)
791 client_need_reban(c);
792 c->issticky = s;
793 client_need_reban(c);
794 ewmh_client_update_hints(c);
795 hook_property(client, c, "sticky");
799 /** Set a client fullscreen, or not.
800 * \param c The client.
801 * \param s Set or not the client fullscreen.
803 void
804 client_setfullscreen(client_t *c, bool s)
806 if(c->isfullscreen != s)
808 area_t geometry;
810 /* Make sure the current geometry is stored without titlebar. */
811 if(s)
812 titlebar_ban(c->titlebar);
814 /* become fullscreen! */
815 if((c->isfullscreen = s))
817 /* remove any max state */
818 client_setmaxhoriz(c, false);
819 client_setmaxvert(c, false);
820 /* You can only be part of one of the special layers. */
821 client_setbelow(c, false);
822 client_setabove(c, false);
823 client_setontop(c, false);
825 geometry = screen_area_get(c->screen, false);
826 c->geometries.fullscreen = c->geometry;
827 c->border_fs = c->border;
828 client_setborder(c, 0);
830 else
832 geometry = c->geometries.fullscreen;
833 client_setborder(c, c->border_fs);
835 client_resize(c, geometry, false);
836 client_stack();
837 ewmh_client_update_hints(c);
838 hook_property(client, c, "fullscreen");
842 /** Set a client horizontally maximized.
843 * \param c The client.
844 * \param s The maximized status.
846 void
847 client_setmaxhoriz(client_t *c, bool s)
849 if(c->ismaxhoriz != s)
851 area_t geometry;
853 if((c->ismaxhoriz = s))
855 /* remove fullscreen mode */
856 client_setfullscreen(c, false);
858 geometry = screen_area_get(c->screen, true);
859 geometry.y = c->geometry.y;
860 geometry.height = c->geometry.height;
861 c->geometries.max.x = c->geometry.x;
862 c->geometries.max.width = c->geometry.width;
864 else
866 geometry = c->geometry;
867 geometry.x = c->geometries.max.x;
868 geometry.width = c->geometries.max.width;
871 client_resize(c, geometry, c->size_hints_honor);
872 client_stack();
873 ewmh_client_update_hints(c);
874 hook_property(client, c, "maximized_horizontal");
878 /** Set a client vertically maximized.
879 * \param c The client.
880 * \param s The maximized status.
882 void
883 client_setmaxvert(client_t *c, bool s)
885 if(c->ismaxvert != s)
887 area_t geometry;
889 if((c->ismaxvert = s))
891 /* remove fullscreen mode */
892 client_setfullscreen(c, false);
894 geometry = screen_area_get(c->screen, true);
895 geometry.x = c->geometry.x;
896 geometry.width = c->geometry.width;
897 c->geometries.max.y = c->geometry.y;
898 c->geometries.max.height = c->geometry.height;
900 else
902 geometry = c->geometry;
903 geometry.y = c->geometries.max.y;
904 geometry.height = c->geometries.max.height;
907 client_resize(c, geometry, c->size_hints_honor);
908 client_stack();
909 ewmh_client_update_hints(c);
910 hook_property(client, c, "maximized_vertical");
914 /** Set a client above, or not.
915 * \param c The client.
916 * \param s Set or not the client above.
918 void
919 client_setabove(client_t *c, bool s)
921 if(c->isabove != s)
923 /* You can only be part of one of the special layers. */
924 if(s)
926 client_setbelow(c, false);
927 client_setontop(c, false);
928 client_setfullscreen(c, false);
930 c->isabove = s;
931 client_stack();
932 ewmh_client_update_hints(c);
933 /* execute hook */
934 hook_property(client, c, "above");
938 /** Set a client below, or not.
939 * \param c The client.
940 * \param s Set or not the client below.
942 void
943 client_setbelow(client_t *c, bool s)
945 if(c->isbelow != s)
947 /* You can only be part of one of the special layers. */
948 if(s)
950 client_setabove(c, false);
951 client_setontop(c, false);
952 client_setfullscreen(c, false);
954 c->isbelow = s;
955 client_stack();
956 ewmh_client_update_hints(c);
957 /* execute hook */
958 hook_property(client, c, "below");
962 /** Set a client modal, or not.
963 * \param c The client.
964 * \param s Set or not the client moda.
966 void
967 client_setmodal(client_t *c, bool s)
969 if(c->ismodal != s)
971 c->ismodal = s;
972 client_stack();
973 ewmh_client_update_hints(c);
974 /* execute hook */
975 hook_property(client, c, "modal");
979 /** Set a client ontop, or not.
980 * \param c The client.
981 * \param s Set or not the client moda.
983 void
984 client_setontop(client_t *c, bool s)
986 if(c->isontop != s)
988 /* You can only be part of one of the special layers. */
989 if(s)
991 client_setabove(c, false);
992 client_setbelow(c, false);
993 client_setfullscreen(c, false);
995 c->isontop = s;
996 client_stack();
997 /* execute hook */
998 hook_property(client, c, "ontop");
1002 /** Unban a client and move it back into the viewport.
1003 * \param c The client.
1005 void
1006 client_unban(client_t *c)
1008 if(c->isbanned)
1010 xcb_map_window(globalconf.connection, c->win);
1012 c->isbanned = false;
1016 /** Unmanage a client.
1017 * \param c The client.
1019 void
1020 client_unmanage(client_t *c)
1022 tag_array_t *tags = &c->screen->tags;
1024 /* Reset transient_for attributes of widows that maybe refering to us */
1025 foreach(_tc, globalconf.clients)
1027 client_t *tc = *_tc;
1028 if(tc->transient_for == c)
1029 tc->transient_for = NULL;
1032 if(globalconf.screens.tab[c->phys_screen].prev_client_focus == c)
1033 globalconf.screens.tab[c->phys_screen].prev_client_focus = NULL;
1035 if(globalconf.screens.tab[c->phys_screen].client_focus == c)
1036 client_unfocus(c);
1038 /* remove client from global list and everywhere else */
1039 foreach(elem, globalconf.clients)
1040 if(*elem == c)
1042 client_array_remove(&globalconf.clients, elem);
1043 break;
1045 stack_client_remove(c);
1046 for(int i = 0; i < tags->len; i++)
1047 untag_client(c, tags->tab[i]);
1049 /* call hook */
1050 if(globalconf.hooks.unmanage != LUA_REFNIL)
1052 client_push(globalconf.L, c);
1053 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.unmanage, 1, 0);
1056 /* Call hook to notify list change */
1057 if(globalconf.hooks.clients != LUA_REFNIL)
1058 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.clients, 0, 0);
1060 /* The server grab construct avoids race conditions. */
1061 xcb_grab_server(globalconf.connection);
1063 xcb_ungrab_button(globalconf.connection, XCB_BUTTON_INDEX_ANY, c->win,
1064 XCB_BUTTON_MASK_ANY);
1065 window_state_set(c->win, XCB_WM_STATE_WITHDRAWN);
1067 xcb_flush(globalconf.connection);
1068 xcb_ungrab_server(globalconf.connection);
1070 titlebar_client_detach(c);
1072 ewmh_update_net_client_list(c->phys_screen);
1074 /* set client as invalid */
1075 c->invalid = true;
1077 client_unref(globalconf.L, c);
1080 /** Kill a client via a WM_DELETE_WINDOW request or KillClient if not
1081 * supported.
1082 * \param c The client to kill.
1084 void
1085 client_kill(client_t *c)
1087 if(client_hasproto(c, WM_DELETE_WINDOW))
1089 xcb_client_message_event_t ev;
1091 /* Initialize all of event's fields first */
1092 p_clear(&ev, 1);
1094 ev.response_type = XCB_CLIENT_MESSAGE;
1095 ev.window = c->win;
1096 ev.format = 32;
1097 ev.data.data32[1] = XCB_CURRENT_TIME;
1098 ev.type = WM_PROTOCOLS;
1099 ev.data.data32[0] = WM_DELETE_WINDOW;
1101 xcb_send_event(globalconf.connection, false, c->win,
1102 XCB_EVENT_MASK_NO_EVENT, (char *) &ev);
1104 else
1105 xcb_kill_client(globalconf.connection, c->win);
1108 /** Get all clients into a table.
1109 * \param L The Lua VM state.
1110 * \return The number of elements pushed on stack.
1111 * \luastack
1112 * \lparam An optional screen nunmber.
1113 * \lreturn A table with all clients.
1115 static int
1116 luaA_client_get(lua_State *L)
1118 int i = 1, screen;
1120 screen = luaL_optnumber(L, 1, 0) - 1;
1122 lua_newtable(L);
1124 if(screen == -1)
1125 foreach(c, globalconf.clients)
1127 client_push(L, *c);
1128 lua_rawseti(L, -2, i++);
1130 else
1132 luaA_checkscreen(screen);
1133 foreach(c, globalconf.clients)
1134 if((*c)->screen == &globalconf.screens.tab[screen])
1136 client_push(L, *c);
1137 lua_rawseti(L, -2, i++);
1141 return 1;
1144 /** Check if a client is visible on its screen.
1145 * \param L The Lua VM state.
1146 * \return The number of elements pushed on stack.
1147 * \luastack
1148 * \lvalue A client.
1149 * \lreturn A boolean value, true if the client is visible, false otherwise.
1151 static int
1152 luaA_client_isvisible(lua_State *L)
1154 client_t *c = luaA_client_checkudata(L, 1);
1155 lua_pushboolean(L, client_isvisible(c, c->screen));
1156 return 1;
1159 /** Set client border width.
1160 * \param c The client.
1161 * \param width The border width.
1163 void
1164 client_setborder(client_t *c, int width)
1166 uint32_t w = width;
1168 if(width > 0 && (c->type == WINDOW_TYPE_DOCK
1169 || c->type == WINDOW_TYPE_SPLASH
1170 || c->type == WINDOW_TYPE_DESKTOP
1171 || c->isfullscreen))
1172 return;
1174 if(width == c->border || width < 0)
1175 return;
1177 /* Update geometry with the new border. */
1178 c->geometry.width -= 2 * c->border;
1179 c->geometry.height -= 2 * c->border;
1181 c->border = width;
1182 xcb_configure_window(globalconf.connection, c->win,
1183 XCB_CONFIG_WINDOW_BORDER_WIDTH, &w);
1185 c->geometry.width += 2 * c->border;
1186 c->geometry.height += 2 * c->border;
1188 /* Changing border size also affects the size of the titlebar. */
1189 titlebar_update_geometry(c);
1191 hook_property(client, c, "border_width");
1194 /** Kill a client.
1195 * \param L The Lua VM state.
1197 * \luastack
1198 * \lvalue A client.
1200 static int
1201 luaA_client_kill(lua_State *L)
1203 client_t *c = luaA_client_checkudata(L, 1);
1204 client_kill(c);
1205 return 0;
1208 /** Swap a client with another one.
1209 * \param L The Lua VM state.
1210 * \luastack
1211 * \lvalue A client.
1212 * \lparam A client to swap with.
1214 static int
1215 luaA_client_swap(lua_State *L)
1217 client_t *c = luaA_client_checkudata(L, 1);
1218 client_t *swap = luaA_client_checkudata(L, 2);
1220 if(c != swap)
1222 client_t **ref_c = NULL, **ref_swap = NULL;
1223 foreach(item, globalconf.clients)
1225 if(*item == c)
1226 ref_c = item;
1227 else if(*item == swap)
1228 ref_swap = item;
1229 if(ref_c && ref_swap)
1230 break;
1232 /* swap ! */
1233 *ref_c = swap;
1234 *ref_swap = c;
1236 /* Call hook to notify list change */
1237 if(globalconf.hooks.clients != LUA_REFNIL)
1238 luaA_dofunction_from_registry(L, globalconf.hooks.clients, 0, 0);
1241 return 0;
1244 /** Access or set the client tags.
1245 * \param L The Lua VM state.
1246 * \return The number of elements pushed on stack.
1247 * \lparam A table with tags to set, or none to get the current tags table.
1248 * \return The clients tag.
1250 static int
1251 luaA_client_tags(lua_State *L)
1253 client_t *c = luaA_client_checkudata(L, 1);
1254 tag_array_t *tags = &c->screen->tags;
1255 int j = 0;
1257 if(lua_gettop(L) == 2)
1259 luaA_checktable(L, 2);
1260 for(int i = 0; i < tags->len; i++)
1261 untag_client(c, tags->tab[i]);
1262 lua_pushnil(L);
1263 while(lua_next(L, 2))
1264 tag_client(c);
1265 lua_pop(L, 1);
1268 lua_newtable(L);
1269 foreach(tag, *tags)
1270 if(is_client_tagged(c, *tag))
1272 tag_push(L, *tag);
1273 lua_rawseti(L, -2, ++j);
1276 return 1;
1279 /** Raise a client on top of others which are on the same layer.
1280 * \param L The Lua VM state.
1281 * \luastack
1282 * \lvalue A client.
1284 static int
1285 luaA_client_raise(lua_State *L)
1287 client_t *c = luaA_client_checkudata(L, 1);
1288 client_raise(c);
1289 return 0;
1292 /** Lower a client on bottom of others which are on the same layer.
1293 * \param L The Lua VM state.
1294 * \luastack
1295 * \lvalue A client.
1297 static int
1298 luaA_client_lower(lua_State *L)
1300 client_t *c = luaA_client_checkudata(L, 1);
1301 client_lower(c);
1302 return 0;
1305 /** Redraw a client by unmapping and mapping it quickly.
1306 * \param L The Lua VM state.
1308 * \luastack
1309 * \lvalue A client.
1311 static int
1312 luaA_client_redraw(lua_State *L)
1314 client_t *c = luaA_client_checkudata(L, 1);
1316 xcb_unmap_window(globalconf.connection, c->win);
1317 xcb_map_window(globalconf.connection, c->win);
1319 /* Set the focus on the current window if the redraw has been
1320 performed on the window where the pointer is currently on
1321 because after the unmapping/mapping, the focus is lost */
1322 if(globalconf.screen_focus->client_focus == c)
1324 client_unfocus(c);
1325 client_focus(c);
1328 return 0;
1331 /** Stop managing a client.
1332 * \param L The Lua VM state.
1333 * \return The number of elements pushed on stack.
1334 * \luastack
1335 * \lvalue A client.
1337 static int
1338 luaA_client_unmanage(lua_State *L)
1340 client_t *c = luaA_client_checkudata(L, 1);
1341 client_unmanage(c);
1342 return 0;
1345 /** Return client geometry.
1346 * \param L The Lua VM state.
1347 * \return The number of elements pushed on stack.
1348 * \luastack
1349 * \lparam A table with new coordinates, or none.
1350 * \lreturn A table with client coordinates.
1352 static int
1353 luaA_client_geometry(lua_State *L)
1355 client_t *c = luaA_client_checkudata(L, 1);
1357 if(lua_gettop(L) == 2)
1359 area_t geometry;
1361 luaA_checktable(L, 2);
1362 geometry.x = luaA_getopt_number(L, 2, "x", c->geometry.x);
1363 geometry.y = luaA_getopt_number(L, 2, "y", c->geometry.y);
1364 if(client_isfixed(c))
1366 geometry.width = c->geometry.width;
1367 geometry.height = c->geometry.height;
1369 else
1371 geometry.width = luaA_getopt_number(L, 2, "width", c->geometry.width);
1372 geometry.height = luaA_getopt_number(L, 2, "height", c->geometry.height);
1375 client_resize(c, geometry, c->size_hints_honor);
1378 return luaA_pusharea(L, c->geometry);
1381 /** Push a strut type to a table on stack.
1382 * \param L The Lua VM state.
1383 * \param struts The struts to push.
1384 * \return The number of elements pushed on stack.
1386 static inline int
1387 luaA_pushstruts(lua_State *L, strut_t struts)
1389 lua_createtable(L, 4, 0);
1390 lua_pushnumber(L, struts.left);
1391 lua_setfield(L, -2, "left");
1392 lua_pushnumber(L, struts.right);
1393 lua_setfield(L, -2, "right");
1394 lua_pushnumber(L, struts.top);
1395 lua_setfield(L, -2, "top");
1396 lua_pushnumber(L, struts.bottom);
1397 lua_setfield(L, -2, "bottom");
1398 return 1;
1401 /** Return client struts (reserved space at the edge of the screen).
1402 * \param L The Lua VM state.
1403 * \return The number of elements pushed on stack.
1404 * \luastack
1405 * \lparam A table with new strut values, or none.
1406 * \lreturn A table with strut values.
1408 static int
1409 luaA_client_struts(lua_State *L)
1411 client_t *c = luaA_client_checkudata(L, 1);
1413 if(lua_gettop(L) == 2)
1415 strut_t struts;
1416 area_t screen_area = display_area_get(c->phys_screen);
1418 struts.left = luaA_getopt_number(L, 2, "left", c->strut.left);
1419 struts.right = luaA_getopt_number(L, 2, "right", c->strut.right);
1420 struts.top = luaA_getopt_number(L, 2, "top", c->strut.top);
1421 struts.bottom = luaA_getopt_number(L, 2, "bottom", c->strut.bottom);
1423 if(struts.left != c->strut.left || struts.right != c->strut.right ||
1424 struts.top != c->strut.top || struts.bottom != c->strut.bottom) {
1425 /* Struts are not so well defined in the context of xinerama. So we just
1426 * give the entire root window and let the window manager decide. */
1427 struts.left_start_y = 0;
1428 struts.left_end_y = !struts.left ? 0 : screen_area.height;
1429 struts.right_start_y = 0;
1430 struts.right_end_y = !struts.right ? 0 : screen_area.height;
1431 struts.top_start_x = 0;
1432 struts.top_end_x = !struts.top ? 0 : screen_area.width;
1433 struts.bottom_start_x = 0;
1434 struts.bottom_end_x = !struts.bottom ? 0 : screen_area.width;
1436 c->strut = struts;
1438 ewmh_update_client_strut(c);
1440 hook_property(client, c, "struts");
1444 return luaA_pushstruts(L, c->strut);
1447 /** Client newindex.
1448 * \param L The Lua VM state.
1449 * \return The number of elements pushed on stack.
1451 static int
1452 luaA_client_newindex(lua_State *L)
1454 size_t len;
1455 client_t *c = luaA_client_checkudata(L, 1);
1456 const char *buf = luaL_checklstring(L, 2, &len);
1457 bool b;
1458 double d;
1459 int i;
1461 switch(a_tokenize(buf, len))
1463 case A_TK_SCREEN:
1464 if(globalconf.xinerama_is_active)
1466 i = luaL_checknumber(L, 3) - 1;
1467 luaA_checkscreen(i);
1468 screen_client_moveto(c, &globalconf.screens.tab[i], true, true);
1470 break;
1471 case A_TK_HIDE:
1472 b = luaA_checkboolean(L, 3);
1473 if(b != c->ishidden)
1475 client_need_reban(c);
1476 c->ishidden = b;
1477 client_need_reban(c);
1478 hook_property(client, c, "hide");
1480 break;
1481 case A_TK_MINIMIZED:
1482 client_setminimized(c, luaA_checkboolean(L, 3));
1483 break;
1484 case A_TK_FULLSCREEN:
1485 client_setfullscreen(c, luaA_checkboolean(L, 3));
1486 break;
1487 case A_TK_MAXIMIZED_HORIZONTAL:
1488 client_setmaxhoriz(c, luaA_checkboolean(L, 3));
1489 break;
1490 case A_TK_MAXIMIZED_VERTICAL:
1491 client_setmaxvert(c, luaA_checkboolean(L, 3));
1492 break;
1493 case A_TK_ICON:
1494 image_unref(L, c->icon);
1495 c->icon = NULL;
1496 c->icon = image_ref(L, 3);
1497 /* execute hook */
1498 hook_property(client, c, "icon");
1499 break;
1500 case A_TK_OPACITY:
1501 if(lua_isnil(L, 3))
1502 window_opacity_set(c->win, -1);
1503 else
1505 d = luaL_checknumber(L, 3);
1506 if(d >= 0 && d <= 1)
1507 window_opacity_set(c->win, d);
1509 break;
1510 case A_TK_STICKY:
1511 client_setsticky(c, luaA_checkboolean(L, 3));
1512 break;
1513 case A_TK_SIZE_HINTS_HONOR:
1514 c->size_hints_honor = luaA_checkboolean(L, 3);
1515 hook_property(client, c, "size_hints_honor");
1516 break;
1517 case A_TK_BORDER_WIDTH:
1518 client_setborder(c, luaL_checknumber(L, 3));
1519 break;
1520 case A_TK_ONTOP:
1521 client_setontop(c, luaA_checkboolean(L, 3));
1522 break;
1523 case A_TK_ABOVE:
1524 client_setabove(c, luaA_checkboolean(L, 3));
1525 break;
1526 case A_TK_BELOW:
1527 client_setbelow(c, luaA_checkboolean(L, 3));
1528 break;
1529 case A_TK_URGENT:
1530 client_seturgent(c, luaA_checkboolean(L, 3));
1531 break;
1532 case A_TK_BORDER_COLOR:
1533 if((buf = luaL_checklstring(L, 3, &len))
1534 && xcolor_init_reply(xcolor_init_unchecked(&c->border_color, buf, len)))
1535 xcb_change_window_attributes(globalconf.connection, c->win,
1536 XCB_CW_BORDER_PIXEL, &c->border_color.pixel);
1537 break;
1538 case A_TK_TITLEBAR:
1539 if(lua_isnil(L, 3))
1540 titlebar_client_detach(c);
1541 else
1542 titlebar_client_attach(c);
1543 break;
1544 case A_TK_SKIP_TASKBAR:
1545 c->skiptb = luaA_checkboolean(L, 3);
1546 hook_property(client, c, "skip_taskbar");
1547 break;
1548 default:
1549 return 0;
1552 return 0;
1555 /** Client object.
1556 * \param L The Lua VM state.
1557 * \return The number of elements pushed on stack.
1558 * \luastack
1559 * \lfield id The window X id.
1560 * \lfield name The client title.
1561 * \lfield skip_taskbar If true the client won't be shown in the tasklist.
1562 * \lfield type The window type (desktop, normal, dock, …).
1563 * \lfield class The client class.
1564 * \lfield instance The client instance.
1565 * \lfield pid The client PID, if available.
1566 * \lfield role The window role, if available.
1567 * \lfield machine The machine client is running on.
1568 * \lfield icon_name The client name when iconified.
1569 * \lfield screen Client screen number.
1570 * \lfield hide Define if the client must be hidden, i.e. never mapped,
1571 * invisible in taskbar.
1572 * \lfield minimized Define it the client must be iconify, i.e. only visible in
1573 * taskbar.
1574 * \lfield size_hints_honor Honor size hints, i.e. respect size ratio.
1575 * \lfield border_width The client border width.
1576 * \lfield border_color The client border color.
1577 * \lfield titlebar The client titlebar.
1578 * \lfield urgent The client urgent state.
1579 * \lfield content An image representing the client window content (screenshot).
1580 * \lfield focus The focused client.
1581 * \lfield opacity The client opacity between 0 and 1.
1582 * \lfield ontop The client is on top of every other windows.
1583 * \lfield above The client is above normal windows.
1584 * \lfield below The client is below normal windows.
1585 * \lfield fullscreen The client is fullscreen or not.
1586 * \lfield maximized_horizontal The client is maximized horizontally or not.
1587 * \lfield maximized_vertical The client is maximized vertically or not.
1588 * \lfield transient_for Return the client the window is transient for.
1589 * \lfield group_id Identification unique to a group of windows.
1590 * \lfield leader_id Identification unique to windows spawned by the same command.
1591 * \lfield size_hints A table with size hints of the client: user_position,
1592 * user_size, program_position, program_size, etc.
1594 static int
1595 luaA_client_index(lua_State *L)
1597 size_t len;
1598 ssize_t slen;
1599 client_t *c = luaA_client_checkudata(L, 1);
1600 const char *buf = luaL_checklstring(L, 2, &len);
1601 char *value;
1602 void *data;
1603 xcb_get_property_cookie_t prop_c;
1604 xcb_get_property_reply_t *prop_r = NULL;
1605 double d;
1607 if(luaA_usemetatable(L, 1, 2))
1608 return 1;
1610 switch(a_tokenize(buf, len))
1612 case A_TK_NAME:
1613 lua_pushstring(L, c->name);
1614 break;
1615 case A_TK_TRANSIENT_FOR:
1616 return client_push(globalconf.L, c->transient_for);
1617 case A_TK_SKIP_TASKBAR:
1618 lua_pushboolean(L, c->skiptb);
1619 break;
1620 case A_TK_CONTENT:
1621 return client_getcontent(c);
1622 case A_TK_TYPE:
1623 switch(c->type)
1625 case WINDOW_TYPE_DESKTOP:
1626 lua_pushliteral(L, "desktop");
1627 break;
1628 case WINDOW_TYPE_DOCK:
1629 lua_pushliteral(L, "dock");
1630 break;
1631 case WINDOW_TYPE_SPLASH:
1632 lua_pushliteral(L, "splash");
1633 break;
1634 case WINDOW_TYPE_DIALOG:
1635 lua_pushliteral(L, "dialog");
1636 break;
1637 case WINDOW_TYPE_MENU:
1638 lua_pushliteral(L, "menu");
1639 break;
1640 case WINDOW_TYPE_TOOLBAR:
1641 lua_pushliteral(L, "toolbar");
1642 break;
1643 case WINDOW_TYPE_UTILITY:
1644 lua_pushliteral(L, "utility");
1645 break;
1646 case WINDOW_TYPE_DROPDOWN_MENU:
1647 lua_pushliteral(L, "dropdown_menu");
1648 break;
1649 case WINDOW_TYPE_POPUP_MENU:
1650 lua_pushliteral(L, "popup_menu");
1651 break;
1652 case WINDOW_TYPE_TOOLTIP:
1653 lua_pushliteral(L, "tooltip");
1654 break;
1655 case WINDOW_TYPE_NOTIFICATION:
1656 lua_pushliteral(L, "notification");
1657 break;
1658 case WINDOW_TYPE_COMBO:
1659 lua_pushliteral(L, "combo");
1660 break;
1661 case WINDOW_TYPE_DND:
1662 lua_pushliteral(L, "dnd");
1663 break;
1664 case WINDOW_TYPE_NORMAL:
1665 lua_pushliteral(L, "normal");
1666 break;
1668 break;
1669 case A_TK_CLASS:
1670 lua_pushstring(L, c->class);
1671 break;
1672 case A_TK_INSTANCE:
1673 lua_pushstring(L, c->instance);
1674 break;
1675 case A_TK_ROLE:
1676 if(!xutil_text_prop_get(globalconf.connection, c->win,
1677 WM_WINDOW_ROLE, &value, &slen))
1678 return 0;
1679 lua_pushlstring(L, value, slen);
1680 p_delete(&value);
1681 break;
1682 case A_TK_PID:
1683 prop_c = xcb_get_property_unchecked(globalconf.connection, false, c->win, _NET_WM_PID, CARDINAL, 0L, 1L);
1684 prop_r = xcb_get_property_reply(globalconf.connection, prop_c, NULL);
1686 if(prop_r && prop_r->value_len && (data = xcb_get_property_value(prop_r)))
1687 lua_pushnumber(L, *(uint32_t *)data);
1688 else
1690 p_delete(&prop_r);
1691 return 0;
1693 break;
1694 case A_TK_ID:
1695 lua_pushnumber(L, c->win);
1696 break;
1697 case A_TK_LEADER_ID:
1698 lua_pushnumber(L, c->leader_win);
1699 break;
1700 case A_TK_MACHINE:
1701 if(!xutil_text_prop_get(globalconf.connection, c->win,
1702 WM_CLIENT_MACHINE, &value, &slen))
1703 return 0;
1704 lua_pushlstring(L, value, slen);
1705 p_delete(&value);
1706 break;
1707 case A_TK_ICON_NAME:
1708 lua_pushstring(L, c->icon_name);
1709 break;
1710 case A_TK_SCREEN:
1711 lua_pushnumber(L, 1 + screen_array_indexof(&globalconf.screens, c->screen));
1712 break;
1713 case A_TK_HIDE:
1714 lua_pushboolean(L, c->ishidden);
1715 break;
1716 case A_TK_MINIMIZED:
1717 lua_pushboolean(L, c->isminimized);
1718 break;
1719 case A_TK_FULLSCREEN:
1720 lua_pushboolean(L, c->isfullscreen);
1721 break;
1722 case A_TK_GROUP_ID:
1723 if(c->group_win)
1724 lua_pushnumber(L, c->group_win);
1725 else
1726 return 0;
1727 break;
1728 case A_TK_MAXIMIZED_HORIZONTAL:
1729 lua_pushboolean(L, c->ismaxhoriz);
1730 break;
1731 case A_TK_MAXIMIZED_VERTICAL:
1732 lua_pushboolean(L, c->ismaxvert);
1733 break;
1734 case A_TK_ICON:
1735 image_push(L, c->icon);
1736 break;
1737 case A_TK_OPACITY:
1738 if((d = window_opacity_get(c->win)) >= 0)
1739 lua_pushnumber(L, d);
1740 else
1741 return 0;
1742 break;
1743 case A_TK_ONTOP:
1744 lua_pushboolean(L, c->isontop);
1745 break;
1746 case A_TK_ABOVE:
1747 lua_pushboolean(L, c->isabove);
1748 break;
1749 case A_TK_BELOW:
1750 lua_pushboolean(L, c->isbelow);
1751 break;
1752 case A_TK_STICKY:
1753 lua_pushboolean(L, c->issticky);
1754 break;
1755 case A_TK_SIZE_HINTS_HONOR:
1756 lua_pushboolean(L, c->size_hints_honor);
1757 break;
1758 case A_TK_BORDER_WIDTH:
1759 lua_pushnumber(L, c->border);
1760 break;
1761 case A_TK_BORDER_COLOR:
1762 luaA_pushxcolor(L, &c->border_color);
1763 break;
1764 case A_TK_TITLEBAR:
1765 return wibox_push(L, c->titlebar);
1766 case A_TK_URGENT:
1767 lua_pushboolean(L, c->isurgent);
1768 break;
1769 case A_TK_SIZE_HINTS:
1771 const char *u_or_p = NULL;
1773 lua_createtable(L, 0, 1);
1775 if(c->size_hints.flags & XCB_SIZE_HINT_US_POSITION)
1776 u_or_p = "user_position";
1777 else if(c->size_hints.flags & XCB_SIZE_HINT_P_POSITION)
1778 u_or_p = "program_position";
1780 if(u_or_p)
1782 lua_createtable(L, 0, 2);
1783 lua_pushnumber(L, c->size_hints.x);
1784 lua_setfield(L, -2, "x");
1785 lua_pushnumber(L, c->size_hints.y);
1786 lua_setfield(L, -2, "y");
1787 lua_setfield(L, -2, u_or_p);
1788 u_or_p = NULL;
1791 if(c->size_hints.flags & XCB_SIZE_HINT_US_SIZE)
1792 u_or_p = "user_size";
1793 else if(c->size_hints.flags & XCB_SIZE_HINT_P_SIZE)
1794 u_or_p = "program_size";
1796 if(u_or_p)
1798 lua_createtable(L, 0, 2);
1799 lua_pushnumber(L, c->size_hints.width);
1800 lua_setfield(L, -2, "width");
1801 lua_pushnumber(L, c->size_hints.height);
1802 lua_setfield(L, -2, "height");
1803 lua_setfield(L, -2, u_or_p);
1806 if(c->size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE)
1808 lua_pushnumber(L, c->size_hints.min_width);
1809 lua_setfield(L, -2, "min_width");
1810 lua_pushnumber(L, c->size_hints.min_height);
1811 lua_setfield(L, -2, "min_height");
1814 if(c->size_hints.flags & XCB_SIZE_HINT_P_MAX_SIZE)
1816 lua_pushnumber(L, c->size_hints.max_width);
1817 lua_setfield(L, -2, "max_width");
1818 lua_pushnumber(L, c->size_hints.max_height);
1819 lua_setfield(L, -2, "max_height");
1822 if(c->size_hints.flags & XCB_SIZE_HINT_P_RESIZE_INC)
1824 lua_pushnumber(L, c->size_hints.width_inc);
1825 lua_setfield(L, -2, "width_inc");
1826 lua_pushnumber(L, c->size_hints.height_inc);
1827 lua_setfield(L, -2, "height_inc");
1830 if(c->size_hints.flags & XCB_SIZE_HINT_P_ASPECT)
1832 lua_pushnumber(L, c->size_hints.min_aspect_num);
1833 lua_setfield(L, -2, "min_aspect_num");
1834 lua_pushnumber(L, c->size_hints.min_aspect_den);
1835 lua_setfield(L, -2, "min_aspect_den");
1836 lua_pushnumber(L, c->size_hints.max_aspect_num);
1837 lua_setfield(L, -2, "max_aspect_num");
1838 lua_pushnumber(L, c->size_hints.max_aspect_den);
1839 lua_setfield(L, -2, "max_aspect_den");
1842 if(c->size_hints.flags & XCB_SIZE_HINT_BASE_SIZE)
1844 lua_pushnumber(L, c->size_hints.base_width);
1845 lua_setfield(L, -2, "base_width");
1846 lua_pushnumber(L, c->size_hints.base_height);
1847 lua_setfield(L, -2, "base_height");
1850 if(c->size_hints.flags & XCB_SIZE_HINT_P_WIN_GRAVITY)
1852 switch(c->size_hints.win_gravity)
1854 default:
1855 lua_pushliteral(L, "north_west");
1856 break;
1857 case XCB_GRAVITY_NORTH:
1858 lua_pushliteral(L, "north");
1859 break;
1860 case XCB_GRAVITY_NORTH_EAST:
1861 lua_pushliteral(L, "north_east");
1862 break;
1863 case XCB_GRAVITY_WEST:
1864 lua_pushliteral(L, "west");
1865 break;
1866 case XCB_GRAVITY_CENTER:
1867 lua_pushliteral(L, "center");
1868 break;
1869 case XCB_GRAVITY_EAST:
1870 lua_pushliteral(L, "east");
1871 break;
1872 case XCB_GRAVITY_SOUTH_WEST:
1873 lua_pushliteral(L, "south_west");
1874 break;
1875 case XCB_GRAVITY_SOUTH:
1876 lua_pushliteral(L, "south");
1877 break;
1878 case XCB_GRAVITY_SOUTH_EAST:
1879 lua_pushliteral(L, "south_east");
1880 break;
1881 case XCB_GRAVITY_STATIC:
1882 lua_pushliteral(L, "static");
1883 break;
1885 lua_setfield(L, -2, "win_gravity");
1888 break;
1889 default:
1890 return 0;
1893 return 1;
1896 /** Get or set mouse buttons bindings for a client.
1897 * \param L The Lua VM state.
1898 * \return The number of element pushed on stack.
1899 * \luastack
1900 * \lvalue A client.
1901 * \lparam An array of mouse button bindings objects, or nothing.
1902 * \return The array of mouse button bindings objects of this client.
1904 static int
1905 luaA_client_buttons(lua_State *L)
1907 client_t *client = luaA_client_checkudata(L, 1);
1908 button_array_t *buttons = &client->buttons;
1910 if(lua_gettop(L) == 2)
1911 luaA_button_array_set(L, 2, buttons);
1913 window_buttons_grab(client->win, &client->buttons);
1915 return luaA_button_array_get(L, buttons);
1918 /** Get or set keys bindings for a client.
1919 * \param L The Lua VM state.
1920 * \return The number of element pushed on stack.
1921 * \luastack
1922 * \lvalue A client.
1923 * \lparam An array of key bindings objects, or nothing.
1924 * \return The array of key bindings objects of this client.
1926 static int
1927 luaA_client_keys(lua_State *L)
1929 client_t *c = luaA_client_checkudata(L, 1);
1930 key_array_t *keys = &c->keys;
1932 if(lua_gettop(L) == 2)
1934 luaA_key_array_set(L, 2, keys);
1935 xcb_ungrab_key(globalconf.connection, XCB_GRAB_ANY, c->win, XCB_BUTTON_MASK_ANY);
1936 window_grabkeys(c->win, keys);
1939 return luaA_key_array_get(L, keys);
1942 /* Client module.
1943 * \param L The Lua VM state.
1944 * \return The number of pushed elements.
1946 static int
1947 luaA_client_module_index(lua_State *L)
1949 size_t len;
1950 const char *buf = luaL_checklstring(L, 2, &len);
1952 switch(a_tokenize(buf, len))
1954 case A_TK_FOCUS:
1955 return client_push(globalconf.L, globalconf.screen_focus->client_focus);
1956 break;
1957 default:
1958 return 0;
1962 /* Client module new index.
1963 * \param L The Lua VM state.
1964 * \return The number of pushed elements.
1966 static int
1967 luaA_client_module_newindex(lua_State *L)
1969 size_t len;
1970 const char *buf = luaL_checklstring(L, 2, &len);
1971 client_t *c;
1973 switch(a_tokenize(buf, len))
1975 case A_TK_FOCUS:
1976 c = luaA_client_checkudata(L, 3);
1977 client_focus(c);
1978 break;
1979 default:
1980 break;
1983 return 0;
1986 const struct luaL_reg awesome_client_methods[] =
1988 { "get", luaA_client_get },
1989 { "__index", luaA_client_module_index },
1990 { "__newindex", luaA_client_module_newindex },
1991 { NULL, NULL }
1993 const struct luaL_reg awesome_client_meta[] =
1995 { "isvisible", luaA_client_isvisible },
1996 { "geometry", luaA_client_geometry },
1997 { "struts", luaA_client_struts },
1998 { "buttons", luaA_client_buttons },
1999 { "keys", luaA_client_keys },
2000 { "tags", luaA_client_tags },
2001 { "kill", luaA_client_kill },
2002 { "swap", luaA_client_swap },
2003 { "raise", luaA_client_raise },
2004 { "lower", luaA_client_lower },
2005 { "redraw", luaA_client_redraw },
2006 { "unmanage", luaA_client_unmanage },
2007 { "__index", luaA_client_index },
2008 { "__newindex", luaA_client_newindex },
2009 { "__gc", luaA_client_gc },
2010 { "__tostring", luaA_client_tostring },
2011 { NULL, NULL }
2014 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80