awesomerc: remove marking
[awesome.git] / client.c
blob5a8df3f7684edab68c8c5e2672b61c3a38bb2a7f
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 button_array_wipe(&c->buttons);
53 key_array_wipe(&c->keys);
54 xcb_get_wm_protocols_reply_wipe(&c->protocols);
55 p_delete(&c->class);
56 p_delete(&c->startup_id);
57 p_delete(&c->instance);
58 p_delete(&c->icon_name);
59 p_delete(&c->name);
60 return luaA_object_gc(L);
63 /** Change the clients urgency flag.
64 * \param c The client
65 * \param urgent The new flag state
67 void
68 client_seturgent(client_t *c, bool urgent)
70 if(c->isurgent != urgent)
72 xcb_get_property_cookie_t hints =
73 xcb_get_wm_hints_unchecked(globalconf.connection, c->win);
75 c->isurgent = urgent;
76 ewmh_client_update_hints(c);
78 /* update ICCCM hints */
79 xcb_wm_hints_t wmh;
80 xcb_get_wm_hints_reply(globalconf.connection, hints, &wmh, NULL);
82 if(urgent)
83 wmh.flags |= XCB_WM_HINT_X_URGENCY;
84 else
85 wmh.flags &= ~XCB_WM_HINT_X_URGENCY;
87 xcb_set_wm_hints(globalconf.connection, c->win, &wmh);
89 hook_property(client, c, "urgent");
93 /** Returns true if a client is tagged
94 * with one of the tags of the specified screen.
95 * \param c The client to check.
96 * \param screen Virtual screen.
97 * \return true if the client is visible, false otherwise.
99 bool
100 client_maybevisible(client_t *c, screen_t *screen)
102 if(c->screen == screen)
104 if(c->issticky || c->type == WINDOW_TYPE_DESKTOP)
105 return true;
107 foreach(tag, screen->tags)
108 if((*tag)->selected && is_client_tagged(c, *tag))
109 return true;
111 return false;
114 /** Return the content of a client as an image.
115 * That's just take a screenshot.
116 * \param c The client.
117 * \return 1 if the image has been pushed on stack, false otherwise.
119 static int
120 client_getcontent(client_t *c)
122 xcb_image_t *ximage = xcb_image_get(globalconf.connection,
123 c->win,
124 0, 0,
125 c->geometries.internal.width,
126 c->geometries.internal.height,
127 ~0, XCB_IMAGE_FORMAT_Z_PIXMAP);
128 int retval = 0;
130 if(ximage)
132 if(ximage->bpp >= 24)
134 uint32_t *data = p_alloca(uint32_t, ximage->width * ximage->height);
136 for(int y = 0; y < ximage->height; y++)
137 for(int x = 0; x < ximage->width; x++)
139 data[y * ximage->width + x] = xcb_image_get_pixel(ximage, x, y);
140 data[y * ximage->width + x] |= 0xff000000; /* set alpha to 0xff */
143 retval = image_new_from_argb32(ximage->width, ximage->height, data);
145 xcb_image_destroy(ximage);
148 return retval;
151 /** Get a client by its window.
152 * \param w The client window to find.
153 * \return A client pointer if found, NULL otherwise.
155 client_t *
156 client_getbywin(xcb_window_t w)
158 foreach(c, globalconf.clients)
159 if((*c)->win == w)
160 return *c;
162 return NULL;
165 /** Record that a client lost focus.
166 * \param c Client being unfocused
168 void
169 client_unfocus_update(client_t *c)
171 globalconf.screens.tab[c->phys_screen].client_focus = NULL;
172 ewmh_update_net_active_window(c->phys_screen);
174 /* Call hook */
175 if(globalconf.hooks.unfocus != LUA_REFNIL)
177 client_push(globalconf.L, c);
178 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.unfocus, 1, 0);
183 /** Unfocus a client.
184 * \param c The client.
186 void
187 client_unfocus(client_t *c)
190 xcb_window_t root_win = xutil_screen_get(globalconf.connection, c->phys_screen)->root;
191 /* Set focus on root window, so no events leak to the current window.
192 * This kind of inlines client_setfocus(), but a root window will never have
193 * the WM_TAKE_FOCUS protocol.
195 xcb_set_input_focus(globalconf.connection, XCB_INPUT_FOCUS_PARENT,
196 root_win, XCB_CURRENT_TIME);
198 client_unfocus_update(c);
201 /** Check if client supports protocol a protocole in WM_PROTOCOL.
202 * \param c The client.
203 * \param atom The protocol atom to check for.
204 * \return True if client has the atom in protocol, false otherwise.
206 bool
207 client_hasproto(client_t *c, xcb_atom_t atom)
209 uint32_t i;
211 for(i = 0; i < c->protocols.atoms_len; i++)
212 if(c->protocols.atoms[i] == atom)
213 return true;
214 return false;
217 /** Sets focus on window - using xcb_set_input_focus or WM_TAKE_FOCUS
218 * \param c Client that should get focus
219 * \param set_input_focus Should we call xcb_set_input_focus
221 void
222 client_setfocus(client_t *c, bool set_input_focus)
224 bool takefocus = client_hasproto(c, WM_TAKE_FOCUS);
225 if(set_input_focus)
226 xcb_set_input_focus(globalconf.connection, XCB_INPUT_FOCUS_PARENT,
227 c->win, XCB_CURRENT_TIME);
228 if(takefocus)
229 window_takefocus(c->win);
232 /** Ban client and move it out of the viewport.
233 * \param c The client.
235 void
236 client_ban(client_t *c)
238 if(!c->isbanned)
240 xcb_unmap_window(globalconf.connection, c->win);
242 c->isbanned = true;
244 if(globalconf.screens.tab[c->phys_screen].prev_client_focus == c)
245 globalconf.screens.tab[c->phys_screen].prev_client_focus = NULL;
247 /* Wait until the last moment to take away the focus from the window. */
248 if(globalconf.screens.tab[c->phys_screen].client_focus == c)
249 client_unfocus(c);
253 /** This is part of The Bob Marley Algorithmm: we ignore enter and leave window
254 * in certain cases, like map/unmap or move, so we don't get spurious events.
256 void
257 client_ignore_enterleave_events(void)
259 foreach(c, globalconf.clients)
260 xcb_change_window_attributes(globalconf.connection,
261 (*c)->win,
262 XCB_CW_EVENT_MASK,
263 (const uint32_t []) { CLIENT_SELECT_INPUT_EVENT_MASK & ~(XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_LEAVE_WINDOW) });
266 void
267 client_restore_enterleave_events(void)
269 foreach(c, globalconf.clients)
270 xcb_change_window_attributes(globalconf.connection,
271 (*c)->win,
272 XCB_CW_EVENT_MASK,
273 (const uint32_t []) { CLIENT_SELECT_INPUT_EVENT_MASK });
276 /** Record that a client got focus.
277 * \param c Client being focused.
279 void
280 client_focus_update(client_t *c)
282 if(!client_maybevisible(c, c->screen))
284 /* Focus previously focused client */
285 client_focus(globalconf.screen_focus->prev_client_focus);
286 return;
289 if(globalconf.screen_focus
290 && globalconf.screen_focus->client_focus)
292 if (globalconf.screen_focus->client_focus != c)
293 client_unfocus_update(globalconf.screen_focus->client_focus);
294 else
295 /* Already focused */
296 return;
298 /* stop hiding client */
299 c->ishidden = false;
300 client_setminimized(c, false);
302 /* unban the client before focusing for consistency */
303 client_unban(c);
305 globalconf.screen_focus = &globalconf.screens.tab[c->phys_screen];
306 globalconf.screen_focus->prev_client_focus = c;
307 globalconf.screen_focus->client_focus = c;
309 /* according to EWMH, we have to remove the urgent state from a client */
310 client_seturgent(c, false);
312 ewmh_update_net_active_window(c->phys_screen);
314 /* execute hook */
315 if(globalconf.hooks.focus != LUA_REFNIL)
317 client_push(globalconf.L, c);
318 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.focus, 1, 0);
323 /** Give focus to client, or to first client if client is NULL.
324 * \param c The client or NULL.
326 void
327 client_focus(client_t *c)
329 /* We have to set focus on first client */
330 if(!c && globalconf.clients.len && !(c = globalconf.clients.tab[0]))
331 return;
333 if(!client_maybevisible(c, c->screen))
334 return;
336 if (!c->nofocus)
337 client_focus_update(c);
339 client_setfocus(c, !c->nofocus);
342 /** Stack a window below.
343 * \param c The client.
344 * \param previous The previous window on the stack.
345 * \return The next-previous!
347 static xcb_window_t
348 client_stack_above(client_t *c, xcb_window_t previous)
350 uint32_t config_win_vals[2];
352 config_win_vals[0] = previous;
353 config_win_vals[1] = XCB_STACK_MODE_ABOVE;
355 xcb_configure_window(globalconf.connection, c->win,
356 XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE,
357 config_win_vals);
359 config_win_vals[0] = c->win;
361 if(c->titlebar)
363 xcb_configure_window(globalconf.connection,
364 c->titlebar->window,
365 XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE,
366 config_win_vals);
367 previous = c->titlebar->window;
369 else
370 previous = c->win;
372 /* stack transient window on top of their parents */
373 foreach(node, globalconf.stack)
374 if((*node)->transient_for == c)
375 previous = client_stack_above(*node, previous);
377 return previous;
380 /** Stacking layout layers */
381 typedef enum
383 /** This one is a special layer */
384 LAYER_IGNORE,
385 LAYER_DESKTOP,
386 LAYER_BELOW,
387 LAYER_NORMAL,
388 LAYER_ABOVE,
389 LAYER_FULLSCREEN,
390 LAYER_ONTOP,
391 /** This one only used for counting and is not a real layer */
392 LAYER_COUNT
393 } layer_t;
395 /** Get the real layer of a client according to its attribute (fullscreen, …)
396 * \param c The client.
397 * \return The real layer.
399 static layer_t
400 client_layer_translator(client_t *c)
402 /* first deal with user set attributes */
403 if(c->isontop)
404 return LAYER_ONTOP;
405 else if(c->isfullscreen)
406 return LAYER_FULLSCREEN;
407 else if(c->isabove)
408 return LAYER_ABOVE;
409 else if(c->isbelow)
410 return LAYER_BELOW;
412 /* check for transient attr */
413 if(c->transient_for)
414 return LAYER_IGNORE;
416 /* then deal with windows type */
417 switch(c->type)
419 case WINDOW_TYPE_DESKTOP:
420 return LAYER_DESKTOP;
421 default:
422 break;
425 return LAYER_NORMAL;
428 /** Restack clients.
429 * \todo It might be worth stopping to restack everyone and only stack `c'
430 * relatively to the first matching in the list.
432 void
433 client_stack_refresh()
435 uint32_t config_win_vals[2];
436 layer_t layer;
438 if (!globalconf.client_need_stack_refresh)
439 return;
440 globalconf.client_need_stack_refresh = false;
442 config_win_vals[0] = XCB_NONE;
443 config_win_vals[1] = XCB_STACK_MODE_ABOVE;
445 /* stack desktop windows */
446 for(layer = LAYER_DESKTOP; layer < LAYER_BELOW; 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 /* first stack not 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->window,
460 XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE,
461 config_win_vals);
462 config_win_vals[0] = sb->window;
466 /* then stack clients */
467 for(layer = LAYER_BELOW; layer < LAYER_COUNT; layer++)
468 foreach(node, globalconf.stack)
469 if(client_layer_translator(*node) == layer)
470 config_win_vals[0] = client_stack_above(*node,
471 config_win_vals[0]);
473 /* then stack ontop wibox window */
474 foreach(_sb, globalconf.wiboxes)
476 wibox_t *sb = *_sb;
477 if(sb->ontop)
479 xcb_configure_window(globalconf.connection,
480 sb->window,
481 XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE,
482 config_win_vals);
483 config_win_vals[0] = sb->window;
488 /** Manage a new client.
489 * \param w The window.
490 * \param wgeom Window geometry.
491 * \param phys_screen Physical screen number.
492 * \param startup True if we are managing at startup time.
494 void
495 client_manage(xcb_window_t w, xcb_get_geometry_reply_t *wgeom, int phys_screen, bool startup)
497 xcb_get_property_cookie_t ewmh_icon_cookie;
498 screen_t *screen;
499 const uint32_t select_input_val[] = { CLIENT_SELECT_INPUT_EVENT_MASK };
501 if(systray_iskdedockapp(w))
503 systray_request_handle(w, phys_screen, NULL);
504 return;
507 /* Send request to get NET_WM_ICON property as soon as possible... */
508 ewmh_icon_cookie = ewmh_window_icon_get_unchecked(w);
509 xcb_change_window_attributes(globalconf.connection, w, XCB_CW_EVENT_MASK, select_input_val);
511 client_t *c = client_new(globalconf.L);
512 /* Push client in client list */
513 client_array_push(&globalconf.clients, client_ref(globalconf.L, -1));
516 screen = c->screen = screen_getbycoord(&globalconf.screens.tab[phys_screen],
517 wgeom->x, wgeom->y);
519 c->phys_screen = phys_screen;
521 /* consider the window banned */
522 c->isbanned = true;
524 /* Initial values */
525 c->win = w;
526 c->geometry.x = wgeom->x;
527 c->geometry.y = wgeom->y;
528 /* Border will be added later. */
529 c->geometry.width = wgeom->width;
530 c->geometry.height = wgeom->height;
531 /* Also set internal geometry (client_ban() needs it). */
532 c->geometries.internal.x = wgeom->x;
533 c->geometries.internal.y = wgeom->y;
534 c->geometries.internal.width = wgeom->width;
535 c->geometries.internal.height = wgeom->height;
536 client_setborder(c, wgeom->border_width);
538 if(ewmh_window_icon_get_reply(ewmh_icon_cookie))
540 client_push(globalconf.L, c);
541 c->icon = luaA_object_ref_item(globalconf.L, -1, -2);
542 lua_pop(globalconf.L, 1);
545 /* we honor size hints by default */
546 c->size_hints_honor = true;
548 /* update hints */
549 property_update_wm_normal_hints(c, NULL);
550 property_update_wm_hints(c, NULL);
551 property_update_wm_transient_for(c, NULL);
552 property_update_wm_client_leader(c, NULL);
553 property_update_wm_client_machine(c);
554 property_update_wm_window_role(c);
555 property_update_net_wm_pid(c, NULL);
557 /* get opacity */
558 c->opacity = window_opacity_get(c->win);
560 /* Then check clients hints */
561 ewmh_client_check_hints(c);
563 /* move client to screen, but do not tag it */
564 screen_client_moveto(c, screen, false, true);
566 /* Push client in stack */
567 client_raise(c);
569 /* update window title */
570 property_update_wm_name(c);
571 property_update_wm_icon_name(c);
572 property_update_wm_class(c, NULL);
573 property_update_wm_protocols(c);
575 xutil_text_prop_get(globalconf.connection, c->win, _NET_STARTUP_ID, &c->startup_id, NULL);
577 /* update strut */
578 ewmh_process_client_strut(c, NULL);
580 ewmh_update_net_client_list(c->phys_screen);
582 /* Always stay in NORMAL_STATE. Even though iconified seems more
583 * appropriate sometimes. The only possible loss is that clients not using
584 * visibility events may continue to proces data (when banned).
585 * Without any exposes or other events the cost should be fairly limited though.
587 * Some clients may expect the window to be unmapped when STATE_ICONIFIED.
588 * Two conflicting parts of the ICCCM v2.0 (section 4.1.4):
590 * "Normal -> Iconic - The client should send a ClientMessage event as described later in this section."
591 * (note no explicit mention of unmapping, while Normal->Widthdrawn does mention that)
593 * "Once a client's window has left the Withdrawn state, the window will be mapped
594 * if it is in the Normal state and the window will be unmapped if it is in the Iconic state."
596 * At this stage it's just safer to keep it in normal state and avoid confusion.
598 window_state_set(c->win, XCB_WM_STATE_NORMAL);
600 if(!startup)
601 spawn_start_notify(c);
603 /* Call hook to notify list change */
604 if(globalconf.hooks.clients != LUA_REFNIL)
605 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.clients, 0, 0);
607 /* call hook */
608 if(globalconf.hooks.manage != LUA_REFNIL)
610 client_push(globalconf.L, c);
611 lua_pushboolean(globalconf.L, startup);
612 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.manage, 2, 0);
616 /** Compute client geometry with respect to its geometry hints.
617 * \param c The client.
618 * \param geometry The geometry that the client might receive.
619 * \return The geometry the client must take respecting its hints.
621 area_t
622 client_geometry_hints(client_t *c, area_t geometry)
624 int32_t basew, baseh, minw, minh;
626 /* base size is substituted with min size if not specified */
627 if(c->size_hints.flags & XCB_SIZE_HINT_P_SIZE)
629 basew = c->size_hints.base_width;
630 baseh = c->size_hints.base_height;
632 else if(c->size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE)
634 basew = c->size_hints.min_width;
635 baseh = c->size_hints.min_height;
637 else
638 basew = baseh = 0;
640 /* min size is substituted with base size if not specified */
641 if(c->size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE)
643 minw = c->size_hints.min_width;
644 minh = c->size_hints.min_height;
646 else if(c->size_hints.flags & XCB_SIZE_HINT_P_SIZE)
648 minw = c->size_hints.base_width;
649 minh = c->size_hints.base_height;
651 else
652 minw = minh = 0;
654 if(c->size_hints.flags & XCB_SIZE_HINT_P_ASPECT
655 && c->size_hints.min_aspect_num > 0
656 && c->size_hints.min_aspect_den > 0
657 && geometry.height - baseh > 0
658 && geometry.width - basew > 0)
660 double dx = (double) (geometry.width - basew);
661 double dy = (double) (geometry.height - baseh);
662 double min = (double) c->size_hints.min_aspect_num / (double) c->size_hints.min_aspect_den;
663 double max = (double) c->size_hints.max_aspect_num / (double) c->size_hints.min_aspect_den;
664 double ratio = dx / dy;
665 if(max > 0 && min > 0 && ratio > 0)
667 if(ratio < min)
669 dy = (dx * min + dy) / (min * min + 1);
670 dx = dy * min;
671 geometry.width = (int) dx + basew;
672 geometry.height = (int) dy + baseh;
674 else if(ratio > max)
676 dy = (dx * min + dy) / (max * max + 1);
677 dx = dy * min;
678 geometry.width = (int) dx + basew;
679 geometry.height = (int) dy + baseh;
684 if(minw)
685 geometry.width = MAX(geometry.width, minw);
686 if(minh)
687 geometry.height = MAX(geometry.height, minh);
689 if(c->size_hints.flags & XCB_SIZE_HINT_P_MAX_SIZE)
691 if(c->size_hints.max_width)
692 geometry.width = MIN(geometry.width, c->size_hints.max_width);
693 if(c->size_hints.max_height)
694 geometry.height = MIN(geometry.height, c->size_hints.max_height);
697 if(c->size_hints.flags & (XCB_SIZE_HINT_P_RESIZE_INC | XCB_SIZE_HINT_BASE_SIZE)
698 && c->size_hints.width_inc && c->size_hints.height_inc)
700 uint16_t t1 = geometry.width, t2 = geometry.height;
701 unsigned_subtract(t1, basew);
702 unsigned_subtract(t2, baseh);
703 geometry.width -= t1 % c->size_hints.width_inc;
704 geometry.height -= t2 % c->size_hints.height_inc;
707 return geometry;
710 /** Resize client window.
711 * The sizse given as parameters are with titlebar and borders!
712 * \param c Client to resize.
713 * \param geometry New window geometry.
714 * \param hints Use size hints.
715 * \return true if an actual resize occurred.
717 bool
718 client_resize(client_t *c, area_t geometry, bool hints)
720 area_t geometry_internal;
721 area_t area;
723 /* offscreen appearance fixes */
724 area = display_area_get(c->phys_screen);
726 if(geometry.x > area.width)
727 geometry.x = area.width - geometry.width;
728 if(geometry.y > area.height)
729 geometry.y = area.height - geometry.height;
730 if(geometry.x + geometry.width < 0)
731 geometry.x = 0;
732 if(geometry.y + geometry.height < 0)
733 geometry.y = 0;
735 /* Real client geometry, please keep it contained to C code at the very least. */
736 geometry_internal = titlebar_geometry_remove(c->titlebar, c->border, geometry);
738 if(hints)
739 geometry_internal = client_geometry_hints(c, geometry_internal);
741 if(geometry_internal.width == 0 || geometry_internal.height == 0)
742 return false;
744 /* Also let client hints propegate to the "official" geometry. */
745 geometry = titlebar_geometry_add(c->titlebar, c->border, geometry_internal);
747 if(c->geometries.internal.x != geometry_internal.x
748 || c->geometries.internal.y != geometry_internal.y
749 || c->geometries.internal.width != geometry_internal.width
750 || c->geometries.internal.height != geometry_internal.height)
752 screen_t *new_screen = screen_getbycoord(c->screen,
753 geometry_internal.x, geometry_internal.y);
755 /* Values to configure a window is an array where values are
756 * stored according to 'value_mask' */
757 uint32_t values[4];
759 c->geometries.internal.x = values[0] = geometry_internal.x;
760 c->geometries.internal.y = values[1] = geometry_internal.y;
761 c->geometries.internal.width = values[2] = geometry_internal.width;
762 c->geometries.internal.height = values[3] = geometry_internal.height;
764 /* Also store geometry including border and titlebar. */
765 c->geometry = geometry;
767 titlebar_update_geometry(c);
769 /* Ignore all spurious enter/leave notify events */
770 client_ignore_enterleave_events();
772 xcb_configure_window(globalconf.connection, c->win,
773 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y
774 | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
775 values);
777 client_restore_enterleave_events();
779 screen_client_moveto(c, new_screen, true, false);
781 /* execute hook */
782 hook_property(client, c, "geometry");
784 return true;
787 return false;
790 /** Set a client minimized, or not.
791 * \param c The client.
792 * \param s Set or not the client minimized.
794 void
795 client_setminimized(client_t *c, bool s)
797 if(c->isminimized != s)
799 client_need_reban(c);
800 c->isminimized = s;
801 client_need_reban(c);
802 if(s)
803 window_state_set(c->win, XCB_WM_STATE_ICONIC);
804 else
805 window_state_set(c->win, XCB_WM_STATE_NORMAL);
806 ewmh_client_update_hints(c);
807 /* execute hook */
808 hook_property(client, c, "minimized");
812 /** Set a client sticky, or not.
813 * \param c The client.
814 * \param s Set or not the client sticky.
816 void
817 client_setsticky(client_t *c, bool s)
819 if(c->issticky != s)
821 client_need_reban(c);
822 c->issticky = s;
823 client_need_reban(c);
824 ewmh_client_update_hints(c);
825 hook_property(client, c, "sticky");
829 /** Set a client fullscreen, or not.
830 * \param c The client.
831 * \param s Set or not the client fullscreen.
833 void
834 client_setfullscreen(client_t *c, bool s)
836 if(c->isfullscreen != s)
838 area_t geometry;
840 /* Make sure the current geometry is stored without titlebar. */
841 if(s)
842 titlebar_ban(c->titlebar);
844 /* become fullscreen! */
845 if((c->isfullscreen = s))
847 /* remove any max state */
848 client_setmaxhoriz(c, false);
849 client_setmaxvert(c, false);
850 /* You can only be part of one of the special layers. */
851 client_setbelow(c, false);
852 client_setabove(c, false);
853 client_setontop(c, false);
855 geometry = screen_area_get(c->screen, false);
856 c->geometries.fullscreen = c->geometry;
857 c->border_fs = c->border;
858 client_setborder(c, 0);
860 else
862 geometry = c->geometries.fullscreen;
863 client_setborder(c, c->border_fs);
865 client_resize(c, geometry, false);
866 client_stack();
867 ewmh_client_update_hints(c);
868 hook_property(client, c, "fullscreen");
872 /** Set a client horizontally maximized.
873 * \param c The client.
874 * \param s The maximized status.
876 void
877 client_setmaxhoriz(client_t *c, bool s)
879 if(c->ismaxhoriz != s)
881 area_t geometry;
883 if((c->ismaxhoriz = s))
885 /* remove fullscreen mode */
886 client_setfullscreen(c, false);
888 geometry = screen_area_get(c->screen, true);
889 geometry.y = c->geometry.y;
890 geometry.height = c->geometry.height;
891 c->geometries.max.x = c->geometry.x;
892 c->geometries.max.width = c->geometry.width;
894 else
896 geometry = c->geometry;
897 geometry.x = c->geometries.max.x;
898 geometry.width = c->geometries.max.width;
901 client_resize(c, geometry, c->size_hints_honor);
902 client_stack();
903 ewmh_client_update_hints(c);
904 hook_property(client, c, "maximized_horizontal");
908 /** Set a client vertically maximized.
909 * \param c The client.
910 * \param s The maximized status.
912 void
913 client_setmaxvert(client_t *c, bool s)
915 if(c->ismaxvert != s)
917 area_t geometry;
919 if((c->ismaxvert = s))
921 /* remove fullscreen mode */
922 client_setfullscreen(c, false);
924 geometry = screen_area_get(c->screen, true);
925 geometry.x = c->geometry.x;
926 geometry.width = c->geometry.width;
927 c->geometries.max.y = c->geometry.y;
928 c->geometries.max.height = c->geometry.height;
930 else
932 geometry = c->geometry;
933 geometry.y = c->geometries.max.y;
934 geometry.height = c->geometries.max.height;
937 client_resize(c, geometry, c->size_hints_honor);
938 client_stack();
939 ewmh_client_update_hints(c);
940 hook_property(client, c, "maximized_vertical");
944 /** Set a client above, or not.
945 * \param c The client.
946 * \param s Set or not the client above.
948 void
949 client_setabove(client_t *c, bool s)
951 if(c->isabove != s)
953 /* You can only be part of one of the special layers. */
954 if(s)
956 client_setbelow(c, false);
957 client_setontop(c, false);
958 client_setfullscreen(c, false);
960 c->isabove = s;
961 client_stack();
962 ewmh_client_update_hints(c);
963 /* execute hook */
964 hook_property(client, c, "above");
968 /** Set a client below, or not.
969 * \param c The client.
970 * \param s Set or not the client below.
972 void
973 client_setbelow(client_t *c, bool s)
975 if(c->isbelow != s)
977 /* You can only be part of one of the special layers. */
978 if(s)
980 client_setabove(c, false);
981 client_setontop(c, false);
982 client_setfullscreen(c, false);
984 c->isbelow = s;
985 client_stack();
986 ewmh_client_update_hints(c);
987 /* execute hook */
988 hook_property(client, c, "below");
992 /** Set a client modal, or not.
993 * \param c The client.
994 * \param s Set or not the client moda.
996 void
997 client_setmodal(client_t *c, bool s)
999 if(c->ismodal != s)
1001 c->ismodal = s;
1002 client_stack();
1003 ewmh_client_update_hints(c);
1004 /* execute hook */
1005 hook_property(client, c, "modal");
1009 /** Set a client ontop, or not.
1010 * \param c The client.
1011 * \param s Set or not the client moda.
1013 void
1014 client_setontop(client_t *c, bool s)
1016 if(c->isontop != s)
1018 /* You can only be part of one of the special layers. */
1019 if(s)
1021 client_setabove(c, false);
1022 client_setbelow(c, false);
1023 client_setfullscreen(c, false);
1025 c->isontop = s;
1026 client_stack();
1027 /* execute hook */
1028 hook_property(client, c, "ontop");
1032 /** Unban a client and move it back into the viewport.
1033 * \param c The client.
1035 void
1036 client_unban(client_t *c)
1038 if(c->isbanned)
1040 xcb_map_window(globalconf.connection, c->win);
1042 c->isbanned = false;
1046 /** Unmanage a client.
1047 * \param c The client.
1049 void
1050 client_unmanage(client_t *c)
1052 tag_array_t *tags = &c->screen->tags;
1054 /* Reset transient_for attributes of widows that maybe refering to us */
1055 foreach(_tc, globalconf.clients)
1057 client_t *tc = *_tc;
1058 if(tc->transient_for == c)
1059 tc->transient_for = NULL;
1062 if(globalconf.screens.tab[c->phys_screen].prev_client_focus == c)
1063 globalconf.screens.tab[c->phys_screen].prev_client_focus = NULL;
1065 if(globalconf.screens.tab[c->phys_screen].client_focus == c)
1066 client_unfocus(c);
1068 /* remove client from global list and everywhere else */
1069 foreach(elem, globalconf.clients)
1070 if(*elem == c)
1072 client_array_remove(&globalconf.clients, elem);
1073 break;
1075 stack_client_remove(c);
1076 for(int i = 0; i < tags->len; i++)
1077 untag_client(c, tags->tab[i]);
1079 /* call hook */
1080 if(globalconf.hooks.unmanage != LUA_REFNIL)
1082 client_push(globalconf.L, c);
1083 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.unmanage, 1, 0);
1086 /* Call hook to notify list change */
1087 if(globalconf.hooks.clients != LUA_REFNIL)
1088 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.clients, 0, 0);
1090 window_state_set(c->win, XCB_WM_STATE_WITHDRAWN);
1092 titlebar_client_detach(c);
1094 ewmh_update_net_client_list(c->phys_screen);
1096 /* set client as invalid */
1097 c->invalid = true;
1099 client_unref(globalconf.L, c);
1102 /** Kill a client via a WM_DELETE_WINDOW request or KillClient if not
1103 * supported.
1104 * \param c The client to kill.
1106 void
1107 client_kill(client_t *c)
1109 if(client_hasproto(c, WM_DELETE_WINDOW))
1111 xcb_client_message_event_t ev;
1113 /* Initialize all of event's fields first */
1114 p_clear(&ev, 1);
1116 ev.response_type = XCB_CLIENT_MESSAGE;
1117 ev.window = c->win;
1118 ev.format = 32;
1119 ev.data.data32[1] = XCB_CURRENT_TIME;
1120 ev.type = WM_PROTOCOLS;
1121 ev.data.data32[0] = WM_DELETE_WINDOW;
1123 xcb_send_event(globalconf.connection, false, c->win,
1124 XCB_EVENT_MASK_NO_EVENT, (char *) &ev);
1126 else
1127 xcb_kill_client(globalconf.connection, c->win);
1130 /** Get all clients into a table.
1131 * \param L The Lua VM state.
1132 * \return The number of elements pushed on stack.
1133 * \luastack
1134 * \lparam An optional screen nunmber.
1135 * \lreturn A table with all clients.
1137 static int
1138 luaA_client_get(lua_State *L)
1140 int i = 1, screen;
1142 screen = luaL_optnumber(L, 1, 0) - 1;
1144 lua_newtable(L);
1146 if(screen == -1)
1147 foreach(c, globalconf.clients)
1149 client_push(L, *c);
1150 lua_rawseti(L, -2, i++);
1152 else
1154 luaA_checkscreen(screen);
1155 foreach(c, globalconf.clients)
1156 if((*c)->screen == &globalconf.screens.tab[screen])
1158 client_push(L, *c);
1159 lua_rawseti(L, -2, i++);
1163 return 1;
1166 /** Check if a client is visible on its screen.
1167 * \param L The Lua VM state.
1168 * \return The number of elements pushed on stack.
1169 * \luastack
1170 * \lvalue A client.
1171 * \lreturn A boolean value, true if the client is visible, false otherwise.
1173 static int
1174 luaA_client_isvisible(lua_State *L)
1176 client_t *c = luaA_client_checkudata(L, 1);
1177 lua_pushboolean(L, client_isvisible(c, c->screen));
1178 return 1;
1181 /** Set client border width.
1182 * \param c The client.
1183 * \param width The border width.
1185 void
1186 client_setborder(client_t *c, int width)
1188 uint32_t w = width;
1190 if(width > 0 && (c->type == WINDOW_TYPE_DOCK
1191 || c->type == WINDOW_TYPE_SPLASH
1192 || c->type == WINDOW_TYPE_DESKTOP
1193 || c->isfullscreen))
1194 return;
1196 if(width == c->border || width < 0)
1197 return;
1199 /* Update geometry with the new border. */
1200 c->geometry.width -= 2 * c->border;
1201 c->geometry.height -= 2 * c->border;
1203 c->border = width;
1204 xcb_configure_window(globalconf.connection, c->win,
1205 XCB_CONFIG_WINDOW_BORDER_WIDTH, &w);
1207 c->geometry.width += 2 * c->border;
1208 c->geometry.height += 2 * c->border;
1210 /* Changing border size also affects the size of the titlebar. */
1211 titlebar_update_geometry(c);
1213 hook_property(client, c, "border_width");
1216 /** Kill a client.
1217 * \param L The Lua VM state.
1219 * \luastack
1220 * \lvalue A client.
1222 static int
1223 luaA_client_kill(lua_State *L)
1225 client_t *c = luaA_client_checkudata(L, 1);
1226 client_kill(c);
1227 return 0;
1230 /** Swap a client with another one.
1231 * \param L The Lua VM state.
1232 * \luastack
1233 * \lvalue A client.
1234 * \lparam A client to swap with.
1236 static int
1237 luaA_client_swap(lua_State *L)
1239 client_t *c = luaA_client_checkudata(L, 1);
1240 client_t *swap = luaA_client_checkudata(L, 2);
1242 if(c != swap)
1244 client_t **ref_c = NULL, **ref_swap = NULL;
1245 foreach(item, globalconf.clients)
1247 if(*item == c)
1248 ref_c = item;
1249 else if(*item == swap)
1250 ref_swap = item;
1251 if(ref_c && ref_swap)
1252 break;
1254 /* swap ! */
1255 *ref_c = swap;
1256 *ref_swap = c;
1258 /* Call hook to notify list change */
1259 if(globalconf.hooks.clients != LUA_REFNIL)
1260 luaA_dofunction_from_registry(L, globalconf.hooks.clients, 0, 0);
1263 return 0;
1266 /** Access or set the client tags.
1267 * \param L The Lua VM state.
1268 * \return The number of elements pushed on stack.
1269 * \lparam A table with tags to set, or none to get the current tags table.
1270 * \return The clients tag.
1272 static int
1273 luaA_client_tags(lua_State *L)
1275 client_t *c = luaA_client_checkudata(L, 1);
1276 tag_array_t *tags = &c->screen->tags;
1277 int j = 0;
1279 if(lua_gettop(L) == 2)
1281 luaA_checktable(L, 2);
1282 for(int i = 0; i < tags->len; i++)
1283 untag_client(c, tags->tab[i]);
1284 lua_pushnil(L);
1285 while(lua_next(L, 2))
1286 tag_client(c);
1287 lua_pop(L, 1);
1290 lua_newtable(L);
1291 foreach(tag, *tags)
1292 if(is_client_tagged(c, *tag))
1294 tag_push(L, *tag);
1295 lua_rawseti(L, -2, ++j);
1298 return 1;
1301 /** Raise a client on top of others which are on the same layer.
1302 * \param L The Lua VM state.
1303 * \luastack
1304 * \lvalue A client.
1306 static int
1307 luaA_client_raise(lua_State *L)
1309 client_t *c = luaA_client_checkudata(L, 1);
1310 client_raise(c);
1311 return 0;
1314 /** Lower a client on bottom of others which are on the same layer.
1315 * \param L The Lua VM state.
1316 * \luastack
1317 * \lvalue A client.
1319 static int
1320 luaA_client_lower(lua_State *L)
1322 client_t *c = luaA_client_checkudata(L, 1);
1323 client_lower(c);
1324 return 0;
1327 /** Redraw a client by unmapping and mapping it quickly.
1328 * \param L The Lua VM state.
1330 * \luastack
1331 * \lvalue A client.
1333 static int
1334 luaA_client_redraw(lua_State *L)
1336 client_t *c = luaA_client_checkudata(L, 1);
1338 xcb_unmap_window(globalconf.connection, c->win);
1339 xcb_map_window(globalconf.connection, c->win);
1341 /* Set the focus on the current window if the redraw has been
1342 performed on the window where the pointer is currently on
1343 because after the unmapping/mapping, the focus is lost */
1344 if(globalconf.screen_focus->client_focus == c)
1346 client_unfocus(c);
1347 client_focus(c);
1350 return 0;
1353 /** Stop managing a client.
1354 * \param L The Lua VM state.
1355 * \return The number of elements pushed on stack.
1356 * \luastack
1357 * \lvalue A client.
1359 static int
1360 luaA_client_unmanage(lua_State *L)
1362 client_t *c = luaA_client_checkudata(L, 1);
1363 client_unmanage(c);
1364 return 0;
1367 /** Return client geometry.
1368 * \param L The Lua VM state.
1369 * \return The number of elements pushed on stack.
1370 * \luastack
1371 * \lparam A table with new coordinates, or none.
1372 * \lreturn A table with client coordinates.
1374 static int
1375 luaA_client_geometry(lua_State *L)
1377 client_t *c = luaA_client_checkudata(L, 1);
1379 if(lua_gettop(L) == 2)
1381 area_t geometry;
1383 luaA_checktable(L, 2);
1384 geometry.x = luaA_getopt_number(L, 2, "x", c->geometry.x);
1385 geometry.y = luaA_getopt_number(L, 2, "y", c->geometry.y);
1386 if(client_isfixed(c))
1388 geometry.width = c->geometry.width;
1389 geometry.height = c->geometry.height;
1391 else
1393 geometry.width = luaA_getopt_number(L, 2, "width", c->geometry.width);
1394 geometry.height = luaA_getopt_number(L, 2, "height", c->geometry.height);
1397 client_resize(c, geometry, c->size_hints_honor);
1400 return luaA_pusharea(L, c->geometry);
1403 /** Push a strut type to a table on stack.
1404 * \param L The Lua VM state.
1405 * \param struts The struts to push.
1406 * \return The number of elements pushed on stack.
1408 static inline int
1409 luaA_pushstruts(lua_State *L, strut_t struts)
1411 lua_createtable(L, 4, 0);
1412 lua_pushnumber(L, struts.left);
1413 lua_setfield(L, -2, "left");
1414 lua_pushnumber(L, struts.right);
1415 lua_setfield(L, -2, "right");
1416 lua_pushnumber(L, struts.top);
1417 lua_setfield(L, -2, "top");
1418 lua_pushnumber(L, struts.bottom);
1419 lua_setfield(L, -2, "bottom");
1420 return 1;
1423 /** Return client struts (reserved space at the edge of the screen).
1424 * \param L The Lua VM state.
1425 * \return The number of elements pushed on stack.
1426 * \luastack
1427 * \lparam A table with new strut values, or none.
1428 * \lreturn A table with strut values.
1430 static int
1431 luaA_client_struts(lua_State *L)
1433 client_t *c = luaA_client_checkudata(L, 1);
1435 if(lua_gettop(L) == 2)
1437 strut_t struts;
1438 area_t screen_area = display_area_get(c->phys_screen);
1440 struts.left = luaA_getopt_number(L, 2, "left", c->strut.left);
1441 struts.right = luaA_getopt_number(L, 2, "right", c->strut.right);
1442 struts.top = luaA_getopt_number(L, 2, "top", c->strut.top);
1443 struts.bottom = luaA_getopt_number(L, 2, "bottom", c->strut.bottom);
1445 if(struts.left != c->strut.left || struts.right != c->strut.right ||
1446 struts.top != c->strut.top || struts.bottom != c->strut.bottom) {
1447 /* Struts are not so well defined in the context of xinerama. So we just
1448 * give the entire root window and let the window manager decide. */
1449 struts.left_start_y = 0;
1450 struts.left_end_y = !struts.left ? 0 : screen_area.height;
1451 struts.right_start_y = 0;
1452 struts.right_end_y = !struts.right ? 0 : screen_area.height;
1453 struts.top_start_x = 0;
1454 struts.top_end_x = !struts.top ? 0 : screen_area.width;
1455 struts.bottom_start_x = 0;
1456 struts.bottom_end_x = !struts.bottom ? 0 : screen_area.width;
1458 c->strut = struts;
1460 ewmh_update_client_strut(c);
1462 hook_property(client, c, "struts");
1466 return luaA_pushstruts(L, c->strut);
1469 /** Client newindex.
1470 * \param L The Lua VM state.
1471 * \return The number of elements pushed on stack.
1473 static int
1474 luaA_client_newindex(lua_State *L)
1476 size_t len;
1477 client_t *c = luaA_client_checkudata(L, 1);
1478 const char *buf = luaL_checklstring(L, 2, &len);
1479 bool b;
1480 double d;
1481 int i;
1483 switch(a_tokenize(buf, len))
1485 case A_TK_SCREEN:
1486 if(globalconf.xinerama_is_active)
1488 i = luaL_checknumber(L, 3) - 1;
1489 luaA_checkscreen(i);
1490 screen_client_moveto(c, &globalconf.screens.tab[i], true, true);
1492 break;
1493 case A_TK_HIDE:
1494 b = luaA_checkboolean(L, 3);
1495 if(b != c->ishidden)
1497 client_need_reban(c);
1498 c->ishidden = b;
1499 client_need_reban(c);
1500 hook_property(client, c, "hide");
1502 break;
1503 case A_TK_MINIMIZED:
1504 client_setminimized(c, luaA_checkboolean(L, 3));
1505 break;
1506 case A_TK_FULLSCREEN:
1507 client_setfullscreen(c, luaA_checkboolean(L, 3));
1508 break;
1509 case A_TK_MAXIMIZED_HORIZONTAL:
1510 client_setmaxhoriz(c, luaA_checkboolean(L, 3));
1511 break;
1512 case A_TK_MAXIMIZED_VERTICAL:
1513 client_setmaxvert(c, luaA_checkboolean(L, 3));
1514 break;
1515 case A_TK_ICON:
1516 luaA_object_unref_item(L, 1, c->icon);
1517 c->icon = NULL;
1518 c->icon = luaA_object_ref_item(L, 1, 3);
1519 /* execute hook */
1520 hook_property(client, c, "icon");
1521 break;
1522 case A_TK_OPACITY:
1523 if(lua_isnil(L, 3))
1525 window_opacity_set(c->win, -1);
1526 c->opacity = -1;
1528 else
1530 d = luaL_checknumber(L, 3);
1531 if(d >= 0 && d <= 1)
1533 window_opacity_set(c->win, d);
1534 c->opacity = d;
1537 break;
1538 case A_TK_STICKY:
1539 client_setsticky(c, luaA_checkboolean(L, 3));
1540 break;
1541 case A_TK_SIZE_HINTS_HONOR:
1542 c->size_hints_honor = luaA_checkboolean(L, 3);
1543 hook_property(client, c, "size_hints_honor");
1544 break;
1545 case A_TK_BORDER_WIDTH:
1546 client_setborder(c, luaL_checknumber(L, 3));
1547 break;
1548 case A_TK_ONTOP:
1549 client_setontop(c, luaA_checkboolean(L, 3));
1550 break;
1551 case A_TK_ABOVE:
1552 client_setabove(c, luaA_checkboolean(L, 3));
1553 break;
1554 case A_TK_BELOW:
1555 client_setbelow(c, luaA_checkboolean(L, 3));
1556 break;
1557 case A_TK_URGENT:
1558 client_seturgent(c, luaA_checkboolean(L, 3));
1559 break;
1560 case A_TK_BORDER_COLOR:
1561 if((buf = luaL_checklstring(L, 3, &len))
1562 && xcolor_init_reply(xcolor_init_unchecked(&c->border_color, buf, len)))
1563 xcb_change_window_attributes(globalconf.connection, c->win,
1564 XCB_CW_BORDER_PIXEL, &c->border_color.pixel);
1565 break;
1566 case A_TK_TITLEBAR:
1567 if(lua_isnil(L, 3))
1568 titlebar_client_detach(c);
1569 else
1570 titlebar_client_attach(c);
1571 break;
1572 case A_TK_SKIP_TASKBAR:
1573 c->skiptb = luaA_checkboolean(L, 3);
1574 hook_property(client, c, "skip_taskbar");
1575 break;
1576 default:
1577 return 0;
1580 return 0;
1583 /** Client object.
1584 * \param L The Lua VM state.
1585 * \return The number of elements pushed on stack.
1586 * \luastack
1587 * \lfield id The window X id.
1588 * \lfield name The client title.
1589 * \lfield skip_taskbar If true the client won't be shown in the tasklist.
1590 * \lfield type The window type (desktop, normal, dock, …).
1591 * \lfield class The client class.
1592 * \lfield instance The client instance.
1593 * \lfield pid The client PID, if available.
1594 * \lfield role The window role, if available.
1595 * \lfield machine The machine client is running on.
1596 * \lfield icon_name The client name when iconified.
1597 * \lfield screen Client screen number.
1598 * \lfield hide Define if the client must be hidden, i.e. never mapped,
1599 * invisible in taskbar.
1600 * \lfield minimized Define it the client must be iconify, i.e. only visible in
1601 * taskbar.
1602 * \lfield size_hints_honor Honor size hints, i.e. respect size ratio.
1603 * \lfield border_width The client border width.
1604 * \lfield border_color The client border color.
1605 * \lfield titlebar The client titlebar.
1606 * \lfield urgent The client urgent state.
1607 * \lfield content An image representing the client window content (screenshot).
1608 * \lfield focus The focused client.
1609 * \lfield opacity The client opacity between 0 and 1.
1610 * \lfield ontop The client is on top of every other windows.
1611 * \lfield above The client is above normal windows.
1612 * \lfield below The client is below normal windows.
1613 * \lfield fullscreen The client is fullscreen or not.
1614 * \lfield maximized_horizontal The client is maximized horizontally or not.
1615 * \lfield maximized_vertical The client is maximized vertically or not.
1616 * \lfield transient_for Return the client the window is transient for.
1617 * \lfield group_id Identification unique to a group of windows.
1618 * \lfield leader_id Identification unique to windows spawned by the same command.
1619 * \lfield size_hints A table with size hints of the client: user_position,
1620 * user_size, program_position, program_size, etc.
1622 static int
1623 luaA_client_index(lua_State *L)
1625 size_t len;
1626 client_t *c = luaA_client_checkudata(L, 1);
1627 const char *buf = luaL_checklstring(L, 2, &len);
1629 if(luaA_usemetatable(L, 1, 2))
1630 return 1;
1632 switch(a_tokenize(buf, len))
1634 case A_TK_NAME:
1635 lua_pushstring(L, c->name);
1636 break;
1637 case A_TK_TRANSIENT_FOR:
1638 return client_push(globalconf.L, c->transient_for);
1639 case A_TK_SKIP_TASKBAR:
1640 lua_pushboolean(L, c->skiptb);
1641 break;
1642 case A_TK_CONTENT:
1643 return client_getcontent(c);
1644 case A_TK_TYPE:
1645 switch(c->type)
1647 case WINDOW_TYPE_DESKTOP:
1648 lua_pushliteral(L, "desktop");
1649 break;
1650 case WINDOW_TYPE_DOCK:
1651 lua_pushliteral(L, "dock");
1652 break;
1653 case WINDOW_TYPE_SPLASH:
1654 lua_pushliteral(L, "splash");
1655 break;
1656 case WINDOW_TYPE_DIALOG:
1657 lua_pushliteral(L, "dialog");
1658 break;
1659 case WINDOW_TYPE_MENU:
1660 lua_pushliteral(L, "menu");
1661 break;
1662 case WINDOW_TYPE_TOOLBAR:
1663 lua_pushliteral(L, "toolbar");
1664 break;
1665 case WINDOW_TYPE_UTILITY:
1666 lua_pushliteral(L, "utility");
1667 break;
1668 case WINDOW_TYPE_DROPDOWN_MENU:
1669 lua_pushliteral(L, "dropdown_menu");
1670 break;
1671 case WINDOW_TYPE_POPUP_MENU:
1672 lua_pushliteral(L, "popup_menu");
1673 break;
1674 case WINDOW_TYPE_TOOLTIP:
1675 lua_pushliteral(L, "tooltip");
1676 break;
1677 case WINDOW_TYPE_NOTIFICATION:
1678 lua_pushliteral(L, "notification");
1679 break;
1680 case WINDOW_TYPE_COMBO:
1681 lua_pushliteral(L, "combo");
1682 break;
1683 case WINDOW_TYPE_DND:
1684 lua_pushliteral(L, "dnd");
1685 break;
1686 case WINDOW_TYPE_NORMAL:
1687 lua_pushliteral(L, "normal");
1688 break;
1690 break;
1691 case A_TK_CLASS:
1692 lua_pushstring(L, c->class);
1693 break;
1694 case A_TK_INSTANCE:
1695 lua_pushstring(L, c->instance);
1696 break;
1697 case A_TK_ROLE:
1698 lua_pushstring(L, c->role);
1699 break;
1700 case A_TK_PID:
1701 lua_pushnumber(L, c->pid);
1702 break;
1703 case A_TK_ID:
1704 lua_pushnumber(L, c->win);
1705 break;
1706 case A_TK_LEADER_ID:
1707 lua_pushnumber(L, c->leader_win);
1708 break;
1709 case A_TK_MACHINE:
1710 lua_pushstring(L, c->machine);
1711 break;
1712 case A_TK_ICON_NAME:
1713 lua_pushstring(L, c->icon_name);
1714 break;
1715 case A_TK_SCREEN:
1716 lua_pushnumber(L, 1 + screen_array_indexof(&globalconf.screens, c->screen));
1717 break;
1718 case A_TK_HIDE:
1719 lua_pushboolean(L, c->ishidden);
1720 break;
1721 case A_TK_MINIMIZED:
1722 lua_pushboolean(L, c->isminimized);
1723 break;
1724 case A_TK_FULLSCREEN:
1725 lua_pushboolean(L, c->isfullscreen);
1726 break;
1727 case A_TK_GROUP_ID:
1728 if(c->group_win)
1729 lua_pushnumber(L, c->group_win);
1730 else
1731 return 0;
1732 break;
1733 case A_TK_MAXIMIZED_HORIZONTAL:
1734 lua_pushboolean(L, c->ismaxhoriz);
1735 break;
1736 case A_TK_MAXIMIZED_VERTICAL:
1737 lua_pushboolean(L, c->ismaxvert);
1738 break;
1739 case A_TK_ICON:
1740 luaA_object_push_item(L, 1, c->icon);
1741 break;
1742 case A_TK_OPACITY:
1743 lua_pushnumber(L, c->opacity);
1744 break;
1745 case A_TK_ONTOP:
1746 lua_pushboolean(L, c->isontop);
1747 break;
1748 case A_TK_ABOVE:
1749 lua_pushboolean(L, c->isabove);
1750 break;
1751 case A_TK_BELOW:
1752 lua_pushboolean(L, c->isbelow);
1753 break;
1754 case A_TK_STICKY:
1755 lua_pushboolean(L, c->issticky);
1756 break;
1757 case A_TK_SIZE_HINTS_HONOR:
1758 lua_pushboolean(L, c->size_hints_honor);
1759 break;
1760 case A_TK_BORDER_WIDTH:
1761 lua_pushnumber(L, c->border);
1762 break;
1763 case A_TK_BORDER_COLOR:
1764 luaA_pushxcolor(L, &c->border_color);
1765 break;
1766 case A_TK_TITLEBAR:
1767 return wibox_push(L, c->titlebar);
1768 case A_TK_URGENT:
1769 lua_pushboolean(L, c->isurgent);
1770 break;
1771 case A_TK_SIZE_HINTS:
1773 const char *u_or_p = NULL;
1775 lua_createtable(L, 0, 1);
1777 if(c->size_hints.flags & XCB_SIZE_HINT_US_POSITION)
1778 u_or_p = "user_position";
1779 else if(c->size_hints.flags & XCB_SIZE_HINT_P_POSITION)
1780 u_or_p = "program_position";
1782 if(u_or_p)
1784 lua_createtable(L, 0, 2);
1785 lua_pushnumber(L, c->size_hints.x);
1786 lua_setfield(L, -2, "x");
1787 lua_pushnumber(L, c->size_hints.y);
1788 lua_setfield(L, -2, "y");
1789 lua_setfield(L, -2, u_or_p);
1790 u_or_p = NULL;
1793 if(c->size_hints.flags & XCB_SIZE_HINT_US_SIZE)
1794 u_or_p = "user_size";
1795 else if(c->size_hints.flags & XCB_SIZE_HINT_P_SIZE)
1796 u_or_p = "program_size";
1798 if(u_or_p)
1800 lua_createtable(L, 0, 2);
1801 lua_pushnumber(L, c->size_hints.width);
1802 lua_setfield(L, -2, "width");
1803 lua_pushnumber(L, c->size_hints.height);
1804 lua_setfield(L, -2, "height");
1805 lua_setfield(L, -2, u_or_p);
1808 if(c->size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE)
1810 lua_pushnumber(L, c->size_hints.min_width);
1811 lua_setfield(L, -2, "min_width");
1812 lua_pushnumber(L, c->size_hints.min_height);
1813 lua_setfield(L, -2, "min_height");
1816 if(c->size_hints.flags & XCB_SIZE_HINT_P_MAX_SIZE)
1818 lua_pushnumber(L, c->size_hints.max_width);
1819 lua_setfield(L, -2, "max_width");
1820 lua_pushnumber(L, c->size_hints.max_height);
1821 lua_setfield(L, -2, "max_height");
1824 if(c->size_hints.flags & XCB_SIZE_HINT_P_RESIZE_INC)
1826 lua_pushnumber(L, c->size_hints.width_inc);
1827 lua_setfield(L, -2, "width_inc");
1828 lua_pushnumber(L, c->size_hints.height_inc);
1829 lua_setfield(L, -2, "height_inc");
1832 if(c->size_hints.flags & XCB_SIZE_HINT_P_ASPECT)
1834 lua_pushnumber(L, c->size_hints.min_aspect_num);
1835 lua_setfield(L, -2, "min_aspect_num");
1836 lua_pushnumber(L, c->size_hints.min_aspect_den);
1837 lua_setfield(L, -2, "min_aspect_den");
1838 lua_pushnumber(L, c->size_hints.max_aspect_num);
1839 lua_setfield(L, -2, "max_aspect_num");
1840 lua_pushnumber(L, c->size_hints.max_aspect_den);
1841 lua_setfield(L, -2, "max_aspect_den");
1844 if(c->size_hints.flags & XCB_SIZE_HINT_BASE_SIZE)
1846 lua_pushnumber(L, c->size_hints.base_width);
1847 lua_setfield(L, -2, "base_width");
1848 lua_pushnumber(L, c->size_hints.base_height);
1849 lua_setfield(L, -2, "base_height");
1852 if(c->size_hints.flags & XCB_SIZE_HINT_P_WIN_GRAVITY)
1854 switch(c->size_hints.win_gravity)
1856 default:
1857 lua_pushliteral(L, "north_west");
1858 break;
1859 case XCB_GRAVITY_NORTH:
1860 lua_pushliteral(L, "north");
1861 break;
1862 case XCB_GRAVITY_NORTH_EAST:
1863 lua_pushliteral(L, "north_east");
1864 break;
1865 case XCB_GRAVITY_WEST:
1866 lua_pushliteral(L, "west");
1867 break;
1868 case XCB_GRAVITY_CENTER:
1869 lua_pushliteral(L, "center");
1870 break;
1871 case XCB_GRAVITY_EAST:
1872 lua_pushliteral(L, "east");
1873 break;
1874 case XCB_GRAVITY_SOUTH_WEST:
1875 lua_pushliteral(L, "south_west");
1876 break;
1877 case XCB_GRAVITY_SOUTH:
1878 lua_pushliteral(L, "south");
1879 break;
1880 case XCB_GRAVITY_SOUTH_EAST:
1881 lua_pushliteral(L, "south_east");
1882 break;
1883 case XCB_GRAVITY_STATIC:
1884 lua_pushliteral(L, "static");
1885 break;
1887 lua_setfield(L, -2, "win_gravity");
1890 break;
1891 default:
1892 return 0;
1895 return 1;
1898 /** Get or set mouse buttons bindings for a client.
1899 * \param L The Lua VM state.
1900 * \return The number of element pushed on stack.
1901 * \luastack
1902 * \lvalue A client.
1903 * \lparam An array of mouse button bindings objects, or nothing.
1904 * \return The array of mouse button bindings objects of this client.
1906 static int
1907 luaA_client_buttons(lua_State *L)
1909 client_t *client = luaA_client_checkudata(L, 1);
1910 button_array_t *buttons = &client->buttons;
1912 if(lua_gettop(L) == 2)
1913 luaA_button_array_set(L, 1, 2, buttons);
1915 window_buttons_grab(client->win, &client->buttons);
1917 return luaA_button_array_get(L, 1, buttons);
1920 /** Get or set keys bindings for a client.
1921 * \param L The Lua VM state.
1922 * \return The number of element pushed on stack.
1923 * \luastack
1924 * \lvalue A client.
1925 * \lparam An array of key bindings objects, or nothing.
1926 * \return The array of key bindings objects of this client.
1928 static int
1929 luaA_client_keys(lua_State *L)
1931 client_t *c = luaA_client_checkudata(L, 1);
1932 key_array_t *keys = &c->keys;
1934 if(lua_gettop(L) == 2)
1936 luaA_key_array_set(L, 1, 2, keys);
1937 xcb_ungrab_key(globalconf.connection, XCB_GRAB_ANY, c->win, XCB_BUTTON_MASK_ANY);
1938 window_grabkeys(c->win, keys);
1941 return luaA_key_array_get(L, 1, keys);
1944 /* Client module.
1945 * \param L The Lua VM state.
1946 * \return The number of pushed elements.
1948 static int
1949 luaA_client_module_index(lua_State *L)
1951 size_t len;
1952 const char *buf = luaL_checklstring(L, 2, &len);
1954 switch(a_tokenize(buf, len))
1956 case A_TK_FOCUS:
1957 return client_push(globalconf.L, globalconf.screen_focus->client_focus);
1958 break;
1959 default:
1960 return 0;
1964 /* Client module new index.
1965 * \param L The Lua VM state.
1966 * \return The number of pushed elements.
1968 static int
1969 luaA_client_module_newindex(lua_State *L)
1971 size_t len;
1972 const char *buf = luaL_checklstring(L, 2, &len);
1973 client_t *c;
1975 switch(a_tokenize(buf, len))
1977 case A_TK_FOCUS:
1978 c = luaA_client_checkudata(L, 3);
1979 client_focus(c);
1980 break;
1981 default:
1982 break;
1985 return 0;
1988 const struct luaL_reg awesome_client_methods[] =
1990 LUA_CLASS_METHODS(client)
1991 { "get", luaA_client_get },
1992 { "__index", luaA_client_module_index },
1993 { "__newindex", luaA_client_module_newindex },
1994 { NULL, NULL }
1996 const struct luaL_reg awesome_client_meta[] =
1998 LUA_OBJECT_META(client)
1999 { "isvisible", luaA_client_isvisible },
2000 { "geometry", luaA_client_geometry },
2001 { "struts", luaA_client_struts },
2002 { "buttons", luaA_client_buttons },
2003 { "keys", luaA_client_keys },
2004 { "tags", luaA_client_tags },
2005 { "kill", luaA_client_kill },
2006 { "swap", luaA_client_swap },
2007 { "raise", luaA_client_raise },
2008 { "lower", luaA_client_lower },
2009 { "redraw", luaA_client_redraw },
2010 { "unmanage", luaA_client_unmanage },
2011 { "__index", luaA_client_index },
2012 { "__newindex", luaA_client_newindex },
2013 { "__gc", luaA_client_gc },
2014 { NULL, NULL }
2017 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80