change codename
[awesome.git] / client.c
blob5385de497bb7ab2c96b0975f245994c42d41f8f2
1 /*
2 * client.c - client management
4 * Copyright © 2007-2009 Julien Danjou <julien@danjou.info>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <xcb/xcb_atom.h>
23 #include <xcb/xcb_image.h>
25 #include "tag.h"
26 #include "ewmh.h"
27 #include "screen.h"
28 #include "titlebar.h"
29 #include "systray.h"
30 #include "property.h"
31 #include "spawn.h"
32 #include "common/atoms.h"
33 #include "common/xutil.h"
35 DO_LUA_TOSTRING(client_t, client, "client")
37 client_t *
38 luaA_client_checkudata(lua_State *L, int ud)
40 client_t *c = luaL_checkudata(L, ud, "client");
41 if(c->invalid)
42 luaL_error(L, "client is invalid\n");
43 return c;
46 /** Collect a client.
47 * \param L The Lua VM state.
48 * \return The number of element pushed on stack.
50 static int
51 luaA_client_gc(lua_State *L)
53 client_t *c = luaL_checkudata(L, 1, "client");
54 luaA_ref_array_wipe(&c->refs);
55 button_array_wipe(&c->buttons);
56 image_unref(L, c->icon);
57 p_delete(&c->class);
58 p_delete(&c->startup_id);
59 p_delete(&c->instance);
60 p_delete(&c->icon_name);
61 p_delete(&c->name);
62 return 0;
65 /** Change the clients urgency flag.
66 * \param c The client
67 * \param urgent The new flag state
69 void
70 client_seturgent(client_t *c, bool urgent)
72 if(c->isurgent != urgent)
74 xcb_get_property_cookie_t hints =
75 xcb_get_wm_hints_unchecked(globalconf.connection, c->win);
77 c->isurgent = urgent;
78 ewmh_client_update_hints(c);
80 /* update ICCCM hints */
81 xcb_wm_hints_t wmh;
82 xcb_get_wm_hints_reply(globalconf.connection, hints, &wmh, NULL);
84 if(urgent)
85 wmh.flags |= XCB_WM_HINT_X_URGENCY;
86 else
87 wmh.flags &= ~XCB_WM_HINT_X_URGENCY;
89 xcb_set_wm_hints(globalconf.connection, c->win, &wmh);
91 hooks_property(c, "urgent");
95 /** Returns true if a client is tagged
96 * with one of the tags of the specified screen.
97 * \param c The client to check.
98 * \param screen Virtual screen.
99 * \return true if the client is visible, false otherwise.
101 bool
102 client_maybevisible(client_t *c, screen_t *screen)
104 if(c->screen == screen)
106 if(c->issticky || c->type == WINDOW_TYPE_DESKTOP)
107 return true;
109 foreach(tag, screen->tags)
110 if((*tag)->selected && is_client_tagged(c, *tag))
111 return true;
113 return false;
116 /** Return the content of a client as an image.
117 * That's just take a screenshot.
118 * \param c The client.
119 * \return 1 if the image has been pushed on stack, false otherwise.
121 static int
122 client_getcontent(client_t *c)
124 xcb_image_t *ximage = xcb_image_get(globalconf.connection,
125 c->win,
126 0, 0,
127 c->geometries.internal.width,
128 c->geometries.internal.height,
129 ~0, XCB_IMAGE_FORMAT_Z_PIXMAP);
130 int retval = 0;
132 if(ximage)
134 if(ximage->bpp >= 24)
136 uint32_t *data = p_alloca(uint32_t, ximage->width * ximage->height);
138 for(int y = 0; y < ximage->height; y++)
139 for(int x = 0; x < ximage->width; x++)
141 data[y * ximage->width + x] = xcb_image_get_pixel(ximage, x, y);
142 data[y * ximage->width + x] |= 0xff000000; /* set alpha to 0xff */
145 retval = image_new_from_argb32(ximage->width, ximage->height, data);
147 xcb_image_destroy(ximage);
150 return retval;
153 /** Get a client by its window.
154 * \param w The client window to find.
155 * \return A client pointer if found, NULL otherwise.
157 client_t *
158 client_getbywin(xcb_window_t w)
160 foreach(c, globalconf.clients)
161 if((*c)->win == w)
162 return *c;
164 return NULL;
167 /** Record that a client lost focus.
168 * \param c Client being unfocused
170 void
171 client_unfocus_update(client_t *c)
173 globalconf.screens.tab[c->phys_screen].client_focus = NULL;
174 ewmh_update_net_active_window(c->phys_screen);
176 /* Call hook */
177 if(globalconf.hooks.unfocus != LUA_REFNIL)
179 client_push(globalconf.L, c);
180 luaA_dofunction(globalconf.L, globalconf.hooks.unfocus, 1, 0);
185 /** Unfocus a client.
186 * \param c The client.
188 void
189 client_unfocus(client_t *c)
192 xcb_window_t root_win = xutil_screen_get(globalconf.connection, c->phys_screen)->root;
193 /* Set focus on root window, so no events leak to the current window. */
194 window_setfocus(root_win, true);
196 client_unfocus_update(c);
199 /** Ban client and move it out of the viewport.
200 * \param c The client.
202 void
203 client_ban(client_t *c)
205 if(!c->isbanned)
207 /* Move all clients out of the physical viewport into negative coordinate space. */
208 /* They will all be put on top of each other. */
209 uint32_t request[2] = { - (c->geometries.internal.width + 2 * c->border),
210 - (c->geometries.internal.height + 2 * c->border) };
212 xcb_configure_window(globalconf.connection, c->win,
213 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
214 request);
216 c->isbanned = true;
218 /* All the wiboxes (may) need to be repositioned. */
219 if(client_hasstrut(c))
220 wibox_update_positions();
222 if(globalconf.screens.tab[c->phys_screen].prev_client_focus == c)
223 globalconf.screens.tab[c->phys_screen].prev_client_focus = NULL;
225 /* Wait until the last moment to take away the focus from the window. */
226 if(globalconf.screens.tab[c->phys_screen].client_focus == c)
227 client_unfocus(c);
231 /** Record that a client got focus.
232 * \param c Client being focused.
234 void
235 client_focus_update(client_t *c)
237 if(!client_maybevisible(c, c->screen))
239 /* Focus previously focused client */
240 client_focus(globalconf.screen_focus->prev_client_focus);
241 return;
244 if(globalconf.screen_focus
245 && globalconf.screen_focus->client_focus)
247 if (globalconf.screen_focus->client_focus != c)
248 client_unfocus_update(globalconf.screen_focus->client_focus);
249 else
250 /* Already focused */
251 return;
253 /* stop hiding client */
254 c->ishidden = false;
255 client_setminimized(c, false);
257 /* unban the client before focusing for consistency */
258 client_unban(c);
260 globalconf.screen_focus = &globalconf.screens.tab[c->phys_screen];
261 globalconf.screen_focus->prev_client_focus = c;
262 globalconf.screen_focus->client_focus = c;
264 /* Some layouts use focused client differently, so call them back.
265 * And anyway, we have maybe unhidden */
266 client_need_arrange(c);
268 /* according to EWMH, we have to remove the urgent state from a client */
269 client_seturgent(c, false);
271 ewmh_update_net_active_window(c->phys_screen);
273 /* execute hook */
274 if(globalconf.hooks.focus != LUA_REFNIL)
276 client_push(globalconf.L, c);
277 luaA_dofunction(globalconf.L, globalconf.hooks.focus, 1, 0);
282 /** Give focus to client, or to first client if client is NULL.
283 * \param c The client or NULL.
285 void
286 client_focus(client_t *c)
288 /* We have to set focus on first client */
289 if(!c && globalconf.clients.len && !(c = globalconf.clients.tab[0]))
290 return;
292 if(!client_maybevisible(c, c->screen))
293 return;
295 if (!c->nofocus)
296 client_focus_update(c);
298 window_setfocus(c->win, !c->nofocus);
301 /** Stack a window below.
302 * \param c The client.
303 * \param previous The previous window on the stack.
304 * \param return The next-previous!
306 static xcb_window_t
307 client_stack_above(client_t *c, xcb_window_t previous)
309 uint32_t config_win_vals[2];
311 config_win_vals[0] = previous;
312 config_win_vals[1] = XCB_STACK_MODE_ABOVE;
314 xcb_configure_window(globalconf.connection, c->win,
315 XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE,
316 config_win_vals);
318 config_win_vals[0] = c->win;
320 if(c->titlebar)
322 xcb_configure_window(globalconf.connection,
323 c->titlebar->sw.window,
324 XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE,
325 config_win_vals);
326 previous = c->titlebar->sw.window;
328 else
329 previous = c->win;
331 /* stack transient window on top of their parents */
332 foreach(node, globalconf.stack)
333 if((*node)->transient_for == c)
334 previous = client_stack_above(*node, previous);
336 return previous;
339 /** Stacking layout layers */
340 typedef enum
342 /** This one is a special layer */
343 LAYER_IGNORE,
344 LAYER_DESKTOP,
345 LAYER_BELOW,
346 LAYER_NORMAL,
347 LAYER_ABOVE,
348 LAYER_FULLSCREEN,
349 LAYER_ONTOP,
350 /** This one only used for counting and is not a real layer */
351 LAYER_COUNT
352 } layer_t;
354 /** Get the real layer of a client according to its attribute (fullscreen, …)
355 * \param c The client.
356 * \return The real layer.
358 static layer_t
359 client_layer_translator(client_t *c)
361 /* first deal with user set attributes */
362 if(c->isontop)
363 return LAYER_ONTOP;
364 else if(c->isfullscreen)
365 return LAYER_FULLSCREEN;
366 else if(c->isabove)
367 return LAYER_ABOVE;
368 else if(c->isbelow)
369 return LAYER_BELOW;
371 /* check for transient attr */
372 if(c->transient_for)
373 return LAYER_IGNORE;
375 /* then deal with windows type */
376 switch(c->type)
378 case WINDOW_TYPE_DESKTOP:
379 return LAYER_DESKTOP;
380 default:
381 break;
384 return LAYER_NORMAL;
387 /** Restack clients.
388 * \todo It might be worth stopping to restack everyone and only stack `c'
389 * relatively to the first matching in the list.
391 static void
392 client_real_stack(void)
394 uint32_t config_win_vals[2];
395 layer_t layer;
397 config_win_vals[0] = XCB_NONE;
398 config_win_vals[1] = XCB_STACK_MODE_ABOVE;
400 /* stack desktop windows */
401 for(layer = LAYER_DESKTOP; layer < LAYER_BELOW; layer++)
402 foreach(node, globalconf.stack)
403 if(client_layer_translator(*node) == layer)
404 config_win_vals[0] = client_stack_above(*node,
405 config_win_vals[0]);
407 /* first stack not ontop wibox window */
408 foreach(s, globalconf.screens)
409 foreach(_sb, s->wiboxes)
411 wibox_t *sb = *_sb;
412 if(!sb->ontop)
414 xcb_configure_window(globalconf.connection,
415 sb->sw.window,
416 XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE,
417 config_win_vals);
418 config_win_vals[0] = sb->sw.window;
422 /* then stack clients */
423 for(layer = LAYER_BELOW; layer < LAYER_COUNT; layer++)
424 foreach(node, globalconf.stack)
425 if(client_layer_translator(*node) == layer)
426 config_win_vals[0] = client_stack_above(*node,
427 config_win_vals[0]);
429 /* then stack ontop wibox window */
430 foreach(s, globalconf.screens)
431 foreach(_sb, s->wiboxes)
433 wibox_t *sb = *_sb;
434 if(sb->ontop)
436 xcb_configure_window(globalconf.connection,
437 sb->sw.window,
438 XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE,
439 config_win_vals);
440 config_win_vals[0] = sb->sw.window;
445 void
446 client_stack_refresh()
448 if (!globalconf.client_need_stack_refresh)
449 return;
450 globalconf.client_need_stack_refresh = false;
451 client_real_stack();
454 /** Manage a new client.
455 * \param w The window.
456 * \param wgeom Window geometry.
457 * \param phys_screen Physical screen number.
458 * \param startup True if we are managing at startup time.
460 void
461 client_manage(xcb_window_t w, xcb_get_geometry_reply_t *wgeom, int phys_screen, bool startup)
463 xcb_get_property_cookie_t ewmh_icon_cookie;
464 client_t *c, *tc = NULL;
465 screen_t *screen;
466 const uint32_t select_input_val[] = { CLIENT_SELECT_INPUT_EVENT_MASK };
468 if(systray_iskdedockapp(w))
470 systray_request_handle(w, phys_screen, NULL);
471 return;
474 /* Send request to get NET_WM_ICON property as soon as possible... */
475 ewmh_icon_cookie = ewmh_window_icon_get_unchecked(w);
476 xcb_change_window_attributes(globalconf.connection, w, XCB_CW_EVENT_MASK, select_input_val);
478 c = client_new(globalconf.L);
479 /* Push client in client list */
480 client_array_push(&globalconf.clients, client_ref(globalconf.L));
483 screen = c->screen = screen_getbycoord(&globalconf.screens.tab[phys_screen],
484 wgeom->x, wgeom->y);
486 c->phys_screen = phys_screen;
488 /* Initial values */
489 c->win = w;
490 c->geometry.x = wgeom->x;
491 c->geometry.y = wgeom->y;
492 /* Border will be added later. */
493 c->geometry.width = wgeom->width;
494 c->geometry.height = wgeom->height;
495 /* Also set internal geometry (client_ban() needs it). */
496 c->geometries.internal.x = wgeom->x;
497 c->geometries.internal.y = wgeom->y;
498 c->geometries.internal.width = wgeom->width;
499 c->geometries.internal.height = wgeom->height;
500 client_setborder(c, wgeom->border_width);
502 if(ewmh_window_icon_get_reply(ewmh_icon_cookie))
503 c->icon = image_ref(globalconf.L);
505 /* we honor size hints by default */
506 c->size_hints_honor = true;
508 /* update hints */
509 property_update_wm_normal_hints(c, NULL);
510 property_update_wm_hints(c, NULL);
511 property_update_wm_transient_for(c, NULL);
512 property_update_wm_client_leader(c, NULL);
514 /* Recursively find the parent. */
515 for(tc = c; tc->transient_for; tc = tc->transient_for);
516 if(tc != c && tc->phys_screen == c->phys_screen)
517 screen = tc->screen;
519 /* Then check clients hints */
520 ewmh_client_check_hints(c);
522 /* move client to screen, but do not tag it */
523 screen_client_moveto(c, screen, false, true);
525 /* Push client in stack */
526 client_raise(c);
528 /* update window title */
529 property_update_wm_name(c);
530 property_update_wm_icon_name(c);
531 property_update_wm_class(c, NULL);
533 xutil_text_prop_get(globalconf.connection, c->win, _NET_STARTUP_ID, &c->startup_id, NULL);
535 /* update strut */
536 ewmh_process_client_strut(c, NULL);
538 ewmh_update_net_client_list(c->phys_screen);
540 /* Always stay in NORMAL_STATE. Even though iconified seems more
541 * appropriate sometimes. The only possible loss is that clients not using
542 * visibility events may continue to proces data (when banned).
543 * Without any exposes or other events the cost should be fairly limited though.
545 * Some clients may expect the window to be unmapped when STATE_ICONIFIED.
546 * Two conflicting parts of the ICCCM v2.0 (section 4.1.4):
548 * "Normal -> Iconic - The client should send a ClientMessage event as described later in this section."
549 * (note no explicit mention of unmapping, while Normal->Widthdrawn does mention that)
551 * "Once a client's window has left the Withdrawn state, the window will be mapped
552 * if it is in the Normal state and the window will be unmapped if it is in the Iconic state."
554 * At this stage it's just safer to keep it in normal state and avoid confusion.
556 window_state_set(c->win, XCB_WM_STATE_NORMAL);
558 /* Move window outside the viewport before mapping it. */
559 client_ban(c);
560 xcb_map_window(globalconf.connection, c->win);
562 if(!startup)
563 spawn_start_notify(c);
565 /* Call hook to notify list change */
566 if(globalconf.hooks.clients != LUA_REFNIL)
567 luaA_dofunction(globalconf.L, globalconf.hooks.clients, 0, 0);
569 /* call hook */
570 if(globalconf.hooks.manage != LUA_REFNIL)
572 client_push(globalconf.L, c);
573 lua_pushboolean(globalconf.L, startup);
574 luaA_dofunction(globalconf.L, globalconf.hooks.manage, 2, 0);
578 /** Compute client geometry with respect to its geometry hints.
579 * \param c The client.
580 * \param geometry The geometry that the client might receive.
581 * \return The geometry the client must take respecting its hints.
583 area_t
584 client_geometry_hints(client_t *c, area_t geometry)
586 int32_t basew, baseh, minw, minh;
588 /* base size is substituted with min size if not specified */
589 if(c->size_hints.flags & XCB_SIZE_HINT_P_SIZE)
591 basew = c->size_hints.base_width;
592 baseh = c->size_hints.base_height;
594 else if(c->size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE)
596 basew = c->size_hints.min_width;
597 baseh = c->size_hints.min_height;
599 else
600 basew = baseh = 0;
602 /* min size is substituted with base size if not specified */
603 if(c->size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE)
605 minw = c->size_hints.min_width;
606 minh = c->size_hints.min_height;
608 else if(c->size_hints.flags & XCB_SIZE_HINT_P_SIZE)
610 minw = c->size_hints.base_width;
611 minh = c->size_hints.base_height;
613 else
614 minw = minh = 0;
616 if(c->size_hints.flags & XCB_SIZE_HINT_P_ASPECT
617 && c->size_hints.min_aspect_num > 0
618 && c->size_hints.min_aspect_den > 0
619 && geometry.height - baseh > 0
620 && geometry.width - basew > 0)
622 double dx = (double) (geometry.width - basew);
623 double dy = (double) (geometry.height - baseh);
624 double min = (double) c->size_hints.min_aspect_num / (double) c->size_hints.min_aspect_den;
625 double max = (double) c->size_hints.max_aspect_num / (double) c->size_hints.min_aspect_den;
626 double ratio = dx / dy;
627 if(max > 0 && min > 0 && ratio > 0)
629 if(ratio < min)
631 dy = (dx * min + dy) / (min * min + 1);
632 dx = dy * min;
633 geometry.width = (int) dx + basew;
634 geometry.height = (int) dy + baseh;
636 else if(ratio > max)
638 dy = (dx * min + dy) / (max * max + 1);
639 dx = dy * min;
640 geometry.width = (int) dx + basew;
641 geometry.height = (int) dy + baseh;
646 if(minw)
647 geometry.width = MAX(geometry.width, minw);
648 if(minh)
649 geometry.height = MAX(geometry.height, minh);
651 if(c->size_hints.flags & XCB_SIZE_HINT_P_MAX_SIZE)
653 if(c->size_hints.max_width)
654 geometry.width = MIN(geometry.width, c->size_hints.max_width);
655 if(c->size_hints.max_height)
656 geometry.height = MIN(geometry.height, c->size_hints.max_height);
659 if(c->size_hints.flags & (XCB_SIZE_HINT_P_RESIZE_INC | XCB_SIZE_HINT_BASE_SIZE)
660 && c->size_hints.width_inc && c->size_hints.height_inc)
662 uint16_t t1 = geometry.width, t2 = geometry.height;
663 unsigned_subtract(t1, basew);
664 unsigned_subtract(t2, baseh);
665 geometry.width -= t1 % c->size_hints.width_inc;
666 geometry.height -= t2 % c->size_hints.height_inc;
669 return geometry;
672 /** Resize client window.
673 * The sizse given as parameters are with titlebar and borders!
674 * \param c Client to resize.
675 * \param geometry New window geometry.
676 * \param hints Use size hints.
677 * \return true if an actual resize occurred.
679 bool
680 client_resize(client_t *c, area_t geometry, bool hints)
682 area_t geometry_internal;
683 area_t area;
685 /* offscreen appearance fixes */
686 area = display_area_get(c->phys_screen, NULL,
687 &c->screen->padding);
689 if(geometry.x > area.width)
690 geometry.x = area.width - geometry.width;
691 if(geometry.y > area.height)
692 geometry.y = area.height - geometry.height;
693 if(geometry.x + geometry.width < 0)
694 geometry.x = 0;
695 if(geometry.y + geometry.height < 0)
696 geometry.y = 0;
698 /* Real client geometry, please keep it contained to C code at the very least. */
699 geometry_internal = titlebar_geometry_remove(c->titlebar, c->border, geometry);
701 if(hints)
702 geometry_internal = client_geometry_hints(c, geometry_internal);
704 if(geometry_internal.width == 0 || geometry_internal.height == 0)
705 return false;
707 /* Also let client hints propegate to the "official" geometry. */
708 geometry = titlebar_geometry_add(c->titlebar, c->border, geometry_internal);
710 if(c->geometries.internal.x != geometry_internal.x
711 || c->geometries.internal.y != geometry_internal.y
712 || c->geometries.internal.width != geometry_internal.width
713 || c->geometries.internal.height != geometry_internal.height)
715 screen_t *new_screen = screen_getbycoord(c->screen,
716 geometry_internal.x, geometry_internal.y);
718 /* Values to configure a window is an array where values are
719 * stored according to 'value_mask' */
720 uint32_t values[4];
722 c->geometries.internal.x = values[0] = geometry_internal.x;
723 c->geometries.internal.y = values[1] = geometry_internal.y;
724 c->geometries.internal.width = values[2] = geometry_internal.width;
725 c->geometries.internal.height = values[3] = geometry_internal.height;
727 /* Also store geometry including border and titlebar. */
728 c->geometry = geometry;
730 titlebar_update_geometry(c);
732 /* The idea is to give a client a resize even when banned. */
733 /* We just have to move the (x,y) to keep it out of the viewport. */
734 /* This at least doesn't break expectations about events. */
735 if(c->isbanned)
737 geometry_internal.x = values[0] = - (geometry_internal.width + 2 * c->border);
738 geometry_internal.y = values[1] = - (geometry_internal.height + 2 * c->border);
741 xcb_configure_window(globalconf.connection, c->win,
742 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y
743 | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
744 values);
746 screen_client_moveto(c, new_screen, true, false);
748 /* execute hook */
749 hooks_property(c, "geometry");
751 return true;
754 return false;
757 /** Set a client minimized, or not.
758 * \param c The client.
759 * \param s Set or not the client minimized.
761 void
762 client_setminimized(client_t *c, bool s)
764 if(c->isminimized != s)
766 client_need_arrange(c);
767 c->isminimized = s;
768 client_need_arrange(c);
769 ewmh_client_update_hints(c);
770 /* execute hook */
771 hooks_property(c, "minimized");
775 /** Set a client sticky, or not.
776 * \param c The client.
777 * \param s Set or not the client sticky.
779 void
780 client_setsticky(client_t *c, bool s)
782 if(c->issticky != s)
784 client_need_arrange(c);
785 c->issticky = s;
786 client_need_arrange(c);
787 ewmh_client_update_hints(c);
788 hooks_property(c, "sticky");
792 /** Set a client fullscreen, or not.
793 * \param c The client.
794 * \param s Set or not the client fullscreen.
796 void
797 client_setfullscreen(client_t *c, bool s)
799 if(c->isfullscreen != s)
801 area_t geometry;
803 /* Make sure the current geometry is stored without titlebar. */
804 if(s)
805 titlebar_ban(c->titlebar);
807 /* become fullscreen! */
808 if((c->isfullscreen = s))
810 /* remove any max state */
811 client_setmaxhoriz(c, false);
812 client_setmaxvert(c, false);
813 /* You can only be part of one of the special layers. */
814 client_setbelow(c, false);
815 client_setabove(c, false);
816 client_setontop(c, false);
818 geometry = screen_area_get(c->screen, NULL, NULL, false);
819 c->geometries.fullscreen = c->geometry;
820 c->border_fs = c->border;
821 client_setborder(c, 0);
823 else
825 geometry = c->geometries.fullscreen;
826 client_setborder(c, c->border_fs);
828 client_resize(c, geometry, false);
829 client_need_arrange(c);
830 client_stack();
831 ewmh_client_update_hints(c);
832 hooks_property(c, "fullscreen");
836 /** Set a client horizontally maximized.
837 * \param c The client.
838 * \param s The maximized status.
840 void
841 client_setmaxhoriz(client_t *c, bool s)
843 if(c->ismaxhoriz != s)
845 area_t geometry;
847 if((c->ismaxhoriz = s))
849 /* remove fullscreen mode */
850 client_setfullscreen(c, false);
852 geometry = screen_area_get(c->screen,
853 &c->screen->wiboxes,
854 &c->screen->padding,
855 true);
856 geometry.y = c->geometry.y;
857 geometry.height = c->geometry.height;
858 c->geometries.max.x = c->geometry.x;
859 c->geometries.max.width = c->geometry.width;
861 else
863 geometry = c->geometry;
864 geometry.x = c->geometries.max.x;
865 geometry.width = c->geometries.max.width;
868 client_resize(c, geometry, c->size_hints_honor);
869 client_need_arrange(c);
870 client_stack();
871 ewmh_client_update_hints(c);
872 hooks_property(c, "maximized_horizontal");
876 /** Set a client vertically maximized.
877 * \param c The client.
878 * \param s The maximized status.
880 void
881 client_setmaxvert(client_t *c, bool s)
883 if(c->ismaxvert != s)
885 area_t geometry;
887 if((c->ismaxvert = s))
889 /* remove fullscreen mode */
890 client_setfullscreen(c, false);
892 geometry = screen_area_get(c->screen,
893 &c->screen->wiboxes,
894 &c->screen->padding,
895 true);
896 geometry.x = c->geometry.x;
897 geometry.width = c->geometry.width;
898 c->geometries.max.y = c->geometry.y;
899 c->geometries.max.height = c->geometry.height;
901 else
903 geometry = c->geometry;
904 geometry.y = c->geometries.max.y;
905 geometry.height = c->geometries.max.height;
908 client_resize(c, geometry, c->size_hints_honor);
909 client_need_arrange(c);
910 client_stack();
911 ewmh_client_update_hints(c);
912 hooks_property(c, "maximized_vertical");
916 /** Set a client above, or not.
917 * \param c The client.
918 * \param s Set or not the client above.
920 void
921 client_setabove(client_t *c, bool s)
923 if(c->isabove != s)
925 /* You can only be part of one of the special layers. */
926 if(s)
928 client_setbelow(c, false);
929 client_setontop(c, false);
930 client_setfullscreen(c, false);
932 c->isabove = s;
933 client_stack();
934 ewmh_client_update_hints(c);
935 /* execute hook */
936 hooks_property(c, "above");
940 /** Set a client below, or not.
941 * \param c The client.
942 * \param s Set or not the client below.
944 void
945 client_setbelow(client_t *c, bool s)
947 if(c->isbelow != s)
949 /* You can only be part of one of the special layers. */
950 if(s)
952 client_setabove(c, false);
953 client_setontop(c, false);
954 client_setfullscreen(c, false);
956 c->isbelow = s;
957 client_stack();
958 ewmh_client_update_hints(c);
959 /* execute hook */
960 hooks_property(c, "below");
964 /** Set a client modal, or not.
965 * \param c The client.
966 * \param s Set or not the client moda.
968 void
969 client_setmodal(client_t *c, bool s)
971 if(c->ismodal != s)
973 c->ismodal = s;
974 client_stack();
975 ewmh_client_update_hints(c);
976 /* execute hook */
977 hooks_property(c, "modal");
981 /** Set a client ontop, or not.
982 * \param c The client.
983 * \param s Set or not the client moda.
985 void
986 client_setontop(client_t *c, bool s)
988 if(c->isontop != s)
990 /* You can only be part of one of the special layers. */
991 if(s)
993 client_setabove(c, false);
994 client_setbelow(c, false);
995 client_setfullscreen(c, false);
997 c->isontop = s;
998 client_stack();
999 /* execute hook */
1000 hooks_property(c, "ontop");
1004 /** Unban a client and move it back into the viewport.
1005 * \param c The client.
1007 void
1008 client_unban(client_t *c)
1010 if(c->isbanned)
1012 /* Move the client back where it belongs. */
1013 uint32_t request[] = { c->geometries.internal.x,
1014 c->geometries.internal.y,
1015 c->geometries.internal.width,
1016 c->geometries.internal.height };
1018 xcb_configure_window(globalconf.connection, c->win,
1019 XCB_CONFIG_WINDOW_X
1020 | XCB_CONFIG_WINDOW_Y
1021 | XCB_CONFIG_WINDOW_WIDTH
1022 | XCB_CONFIG_WINDOW_HEIGHT,
1023 request);
1025 c->isbanned = false;
1027 /* All the wiboxes (may) need to be repositioned. */
1028 if(client_hasstrut(c))
1029 wibox_update_positions();
1033 /** Unmanage a client.
1034 * \param c The client.
1036 void
1037 client_unmanage(client_t *c)
1039 tag_array_t *tags = &c->screen->tags;
1041 /* Reset transient_for attributes of widows that maybe refering to us */
1042 foreach(_tc, globalconf.clients)
1044 client_t *tc = *_tc;
1045 if(tc->transient_for == c)
1046 tc->transient_for = NULL;
1049 if(globalconf.screens.tab[c->phys_screen].prev_client_focus == c)
1050 globalconf.screens.tab[c->phys_screen].prev_client_focus = NULL;
1052 if(globalconf.screens.tab[c->phys_screen].client_focus == c)
1053 client_unfocus(c);
1055 /* remove client from global list and everywhere else */
1056 foreach(elem, globalconf.clients)
1057 if(*elem == c)
1059 client_array_remove(&globalconf.clients, elem);
1060 break;
1062 stack_client_remove(c);
1063 for(int i = 0; i < tags->len; i++)
1064 untag_client(c, tags->tab[i]);
1066 /* call hook */
1067 if(globalconf.hooks.unmanage != LUA_REFNIL)
1069 client_push(globalconf.L, c);
1070 luaA_dofunction(globalconf.L, globalconf.hooks.unmanage, 1, 0);
1073 /* Call hook to notify list change */
1074 if(globalconf.hooks.clients != LUA_REFNIL)
1075 luaA_dofunction(globalconf.L, globalconf.hooks.clients, 0, 0);
1077 /* The server grab construct avoids race conditions. */
1078 xcb_grab_server(globalconf.connection);
1080 xcb_ungrab_button(globalconf.connection, XCB_BUTTON_INDEX_ANY, c->win,
1081 XCB_BUTTON_MASK_ANY);
1082 window_state_set(c->win, XCB_WM_STATE_WITHDRAWN);
1084 xcb_flush(globalconf.connection);
1085 xcb_ungrab_server(globalconf.connection);
1087 titlebar_client_detach(c);
1089 ewmh_update_net_client_list(c->phys_screen);
1091 /* All the wiboxes (may) need to be repositioned. */
1092 if(client_hasstrut(c))
1093 wibox_update_positions();
1095 /* set client as invalid */
1096 c->invalid = true;
1098 client_unref(globalconf.L, c);
1101 /** Kill a client via a WM_DELETE_WINDOW request or KillClient if not
1102 * supported.
1103 * \param c The client to kill.
1105 void
1106 client_kill(client_t *c)
1108 if(window_hasproto(c->win, WM_DELETE_WINDOW))
1110 xcb_client_message_event_t ev;
1112 /* Initialize all of event's fields first */
1113 p_clear(&ev, 1);
1115 ev.response_type = XCB_CLIENT_MESSAGE;
1116 ev.window = c->win;
1117 ev.format = 32;
1118 ev.data.data32[1] = XCB_CURRENT_TIME;
1119 ev.type = WM_PROTOCOLS;
1120 ev.data.data32[0] = WM_DELETE_WINDOW;
1122 xcb_send_event(globalconf.connection, false, c->win,
1123 XCB_EVENT_MASK_NO_EVENT, (char *) &ev);
1125 else
1126 xcb_kill_client(globalconf.connection, c->win);
1129 /** Get all clients into a table.
1130 * \param L The Lua VM state.
1131 * \return The number of elements pushed on stack.
1132 * \luastack
1133 * \lparam An optional screen nunmber.
1134 * \lreturn A table with all clients.
1136 static int
1137 luaA_client_get(lua_State *L)
1139 int i = 1, screen;
1141 screen = luaL_optnumber(L, 1, 0) - 1;
1143 lua_newtable(L);
1145 if(screen == -1)
1146 foreach(c, globalconf.clients)
1148 client_push(L, *c);
1149 lua_rawseti(L, -2, i++);
1151 else
1153 luaA_checkscreen(screen);
1154 foreach(c, globalconf.clients)
1155 if((*c)->screen == &globalconf.screens.tab[screen])
1157 client_push(L, *c);
1158 lua_rawseti(L, -2, i++);
1162 return 1;
1165 /** Check if a client is visible on its screen.
1166 * \param L The Lua VM state.
1167 * \return The number of elements pushed on stack.
1168 * \luastack
1169 * \lvalue A client.
1170 * \lreturn A boolean value, true if the client is visible, false otherwise.
1172 static int
1173 luaA_client_isvisible(lua_State *L)
1175 client_t *c = luaA_client_checkudata(L, 1);
1176 lua_pushboolean(L, client_isvisible(c, c->screen));
1177 return 1;
1180 /** Set client border width.
1181 * \param c The client.
1182 * \param width The border width.
1184 void
1185 client_setborder(client_t *c, int width)
1187 uint32_t w = width;
1189 if(width > 0 && (c->type == WINDOW_TYPE_DOCK
1190 || c->type == WINDOW_TYPE_SPLASH
1191 || c->type == WINDOW_TYPE_DESKTOP
1192 || c->isfullscreen))
1193 return;
1195 if(width == c->border || width < 0)
1196 return;
1198 /* Update geometry with the new border. */
1199 c->geometry.width -= 2 * c->border;
1200 c->geometry.height -= 2 * c->border;
1202 c->border = width;
1203 xcb_configure_window(globalconf.connection, c->win,
1204 XCB_CONFIG_WINDOW_BORDER_WIDTH, &w);
1206 c->geometry.width += 2 * c->border;
1207 c->geometry.height += 2 * c->border;
1208 /* Tiled clients will be resized by the layout functions. */
1209 client_need_arrange(c);
1211 /* Changing border size also affects the size of the titlebar. */
1212 titlebar_update_geometry(c);
1214 hooks_property(c, "border_width");
1217 /** Kill a client.
1218 * \param L The Lua VM state.
1220 * \luastack
1221 * \lvalue A client.
1223 static int
1224 luaA_client_kill(lua_State *L)
1226 client_t *c = luaA_client_checkudata(L, 1);
1227 client_kill(c);
1228 return 0;
1231 /** Swap a client with another one.
1232 * \param L The Lua VM state.
1233 * \luastack
1234 * \lvalue A client.
1235 * \lparam A client to swap with.
1237 static int
1238 luaA_client_swap(lua_State *L)
1240 client_t *c = luaA_client_checkudata(L, 1);
1241 client_t *swap = luaA_client_checkudata(L, 2);
1243 if(c != swap)
1245 client_t **ref_c = NULL, **ref_swap = NULL;
1246 foreach(item, globalconf.clients)
1248 if(*item == c)
1249 ref_c = item;
1250 else if(*item == swap)
1251 ref_swap = item;
1252 if(ref_c && ref_swap)
1253 break;
1255 /* swap ! */
1256 *ref_c = swap;
1257 *ref_swap = c;
1259 client_need_arrange(c);
1260 client_need_arrange(swap);
1262 /* Call hook to notify list change */
1263 if(globalconf.hooks.clients != LUA_REFNIL)
1264 luaA_dofunction(L, globalconf.hooks.clients, 0, 0);
1267 return 0;
1270 /** Access or set the client tags.
1271 * \param L The Lua VM state.
1272 * \return The number of elements pushed on stack.
1273 * \lparam A table with tags to set, or none to get the current tags table.
1274 * \return The clients tag.
1276 static int
1277 luaA_client_tags(lua_State *L)
1279 client_t *c = luaA_client_checkudata(L, 1);
1280 tag_array_t *tags = &c->screen->tags;
1281 int j = 0;
1283 if(lua_gettop(L) == 2)
1285 luaA_checktable(L, 2);
1286 for(int i = 0; i < tags->len; i++)
1287 untag_client(c, tags->tab[i]);
1288 lua_pushnil(L);
1289 while(lua_next(L, 2))
1290 tag_client(c);
1291 lua_pop(L, 1);
1294 lua_newtable(L);
1295 foreach(tag, *tags)
1296 if(is_client_tagged(c, *tag))
1298 tag_push(L, *tag);
1299 lua_rawseti(L, -2, ++j);
1302 return 1;
1305 /** Raise a client on top of others which are on the same layer.
1306 * \param L The Lua VM state.
1307 * \luastack
1308 * \lvalue A client.
1310 static int
1311 luaA_client_raise(lua_State *L)
1313 client_t *c = luaA_client_checkudata(L, 1);
1314 client_raise(c);
1315 return 0;
1318 /** Lower a client on bottom of others which are on the same layer.
1319 * \param L The Lua VM state.
1320 * \luastack
1321 * \lvalue A client.
1323 static int
1324 luaA_client_lower(lua_State *L)
1326 client_t *c = luaA_client_checkudata(L, 1);
1327 client_lower(c);
1328 return 0;
1331 /** Redraw a client by unmapping and mapping it quickly.
1332 * \param L The Lua VM state.
1334 * \luastack
1335 * \lvalue A client.
1337 static int
1338 luaA_client_redraw(lua_State *L)
1340 client_t *c = luaA_client_checkudata(L, 1);
1342 xcb_unmap_window(globalconf.connection, c->win);
1343 xcb_map_window(globalconf.connection, c->win);
1345 /* Set the focus on the current window if the redraw has been
1346 performed on the window where the pointer is currently on
1347 because after the unmapping/mapping, the focus is lost */
1348 if(globalconf.screen_focus->client_focus == c)
1350 client_unfocus(c);
1351 client_focus(c);
1354 return 0;
1357 /** Stop managing a client.
1358 * \param L The Lua VM state.
1359 * \return The number of elements pushed on stack.
1360 * \luastack
1361 * \lvalue A client.
1363 static int
1364 luaA_client_unmanage(lua_State *L)
1366 client_t *c = luaA_client_checkudata(L, 1);
1367 client_unmanage(c);
1368 return 0;
1371 /** Return client geometry.
1372 * \param L The Lua VM state.
1373 * \return The number of elements pushed on stack.
1374 * \luastack
1375 * \lparam A table with new coordinates, or none.
1376 * \lreturn A table with client coordinates.
1378 static int
1379 luaA_client_geometry(lua_State *L)
1381 client_t *c = luaA_client_checkudata(L, 1);
1383 if(lua_gettop(L) == 2)
1385 area_t geometry;
1387 luaA_checktable(L, 2);
1388 geometry.x = luaA_getopt_number(L, 2, "x", c->geometry.x);
1389 geometry.y = luaA_getopt_number(L, 2, "y", c->geometry.y);
1390 if(client_isfixed(c))
1392 geometry.width = c->geometry.width;
1393 geometry.height = c->geometry.height;
1395 else
1397 geometry.width = luaA_getopt_number(L, 2, "width", c->geometry.width);
1398 geometry.height = luaA_getopt_number(L, 2, "height", c->geometry.height);
1401 client_resize(c, geometry, c->size_hints_honor);
1404 return luaA_pusharea(L, c->geometry);
1407 /** Push a strut type to a table on stack.
1408 * \param L The Lua VM state.
1409 * \param struts The struts to push.
1410 * \return The number of elements pushed on stack.
1412 static inline int
1413 luaA_pushstruts(lua_State *L, strut_t struts)
1415 lua_createtable(L, 4, 0);
1416 lua_pushnumber(L, struts.left);
1417 lua_setfield(L, -2, "left");
1418 lua_pushnumber(L, struts.right);
1419 lua_setfield(L, -2, "right");
1420 lua_pushnumber(L, struts.top);
1421 lua_setfield(L, -2, "top");
1422 lua_pushnumber(L, struts.bottom);
1423 lua_setfield(L, -2, "bottom");
1424 return 1;
1427 /** Return client struts (reserved space at the edge of the screen).
1428 * \param L The Lua VM state.
1429 * \return The number of elements pushed on stack.
1430 * \luastack
1431 * \lparam A table with new strut values, or none.
1432 * \lreturn A table with strut values.
1434 static int
1435 luaA_client_struts(lua_State *L)
1437 client_t *c = luaA_client_checkudata(L, 1);
1439 if(lua_gettop(L) == 2)
1441 strut_t struts;
1442 area_t screen_area = display_area_get(c->phys_screen, NULL, NULL);
1444 struts.left = luaA_getopt_number(L, 2, "left", c->strut.left);
1445 struts.right = luaA_getopt_number(L, 2, "right", c->strut.right);
1446 struts.top = luaA_getopt_number(L, 2, "top", c->strut.top);
1447 struts.bottom = luaA_getopt_number(L, 2, "bottom", c->strut.bottom);
1449 if(struts.left != c->strut.left || struts.right != c->strut.right ||
1450 struts.top != c->strut.top || struts.bottom != c->strut.bottom) {
1451 /* Struts are not so well defined in the context of xinerama. So we just
1452 * give the entire root window and let the window manager decide. */
1453 struts.left_start_y = 0;
1454 struts.left_end_y = !struts.left ? 0 : screen_area.height;
1455 struts.right_start_y = 0;
1456 struts.right_end_y = !struts.right ? 0 : screen_area.height;
1457 struts.top_start_x = 0;
1458 struts.top_end_x = !struts.top ? 0 : screen_area.width;
1459 struts.bottom_start_x = 0;
1460 struts.bottom_end_x = !struts.bottom ? 0 : screen_area.width;
1462 c->strut = struts;
1464 ewmh_update_client_strut(c);
1466 client_need_arrange(c);
1467 /* All the wiboxes (may) need to be repositioned. */
1468 wibox_update_positions();
1470 hooks_property(c, "struts");
1474 return luaA_pushstruts(L, c->strut);
1477 /** Client newindex.
1478 * \param L The Lua VM state.
1479 * \return The number of elements pushed on stack.
1481 static int
1482 luaA_client_newindex(lua_State *L)
1484 size_t len;
1485 client_t *c = luaA_client_checkudata(L, 1);
1486 const char *buf = luaL_checklstring(L, 2, &len);
1487 bool b;
1488 double d;
1489 int i;
1491 switch(a_tokenize(buf, len))
1493 case A_TK_SCREEN:
1494 if(globalconf.xinerama_is_active)
1496 i = luaL_checknumber(L, 3) - 1;
1497 luaA_checkscreen(i);
1498 screen_client_moveto(c, &globalconf.screens.tab[i], true, true);
1500 break;
1501 case A_TK_HIDE:
1502 b = luaA_checkboolean(L, 3);
1503 if(b != c->ishidden)
1505 client_need_arrange(c);
1506 c->ishidden = b;
1507 client_need_arrange(c);
1509 break;
1510 case A_TK_MINIMIZED:
1511 client_setminimized(c, luaA_checkboolean(L, 3));
1512 break;
1513 case A_TK_FULLSCREEN:
1514 client_setfullscreen(c, luaA_checkboolean(L, 3));
1515 break;
1516 case A_TK_MAXIMIZED_HORIZONTAL:
1517 client_setmaxhoriz(c, luaA_checkboolean(L, 3));
1518 break;
1519 case A_TK_MAXIMIZED_VERTICAL:
1520 client_setmaxvert(c, luaA_checkboolean(L, 3));
1521 break;
1522 case A_TK_ICON:
1523 image_unref(L, c->icon);
1524 c->icon = image_ref(L);
1525 /* execute hook */
1526 hooks_property(c, "icon");
1527 break;
1528 case A_TK_OPACITY:
1529 if(lua_isnil(L, 3))
1530 window_opacity_set(c->win, -1);
1531 else
1533 d = luaL_checknumber(L, 3);
1534 if(d >= 0 && d <= 1)
1535 window_opacity_set(c->win, 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 hooks_property(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 default:
1573 return 0;
1576 return 0;
1579 /** Client object.
1580 * \param L The Lua VM state.
1581 * \return The number of elements pushed on stack.
1582 * \luastack
1583 * \lfield id The window X id.
1584 * \lfield name The client title.
1585 * \lfield skip_taskbar True if the client does not want to be in taskbar.
1586 * \lfield type The window type (desktop, normal, dock, …).
1587 * \lfield class The client class.
1588 * \lfield instance The client instance.
1589 * \lfield pid The client PID, if available.
1590 * \lfield role The window role, if available.
1591 * \lfield machine The machine client is running on.
1592 * \lfield icon_name The client name when iconified.
1593 * \lfield screen Client screen number.
1594 * \lfield hide Define if the client must be hidden, i.e. never mapped,
1595 * invisible in taskbar.
1596 * \lfield minimized Define it the client must be iconify, i.e. only visible in
1597 * taskbar.
1598 * \lfield size_hints_honor Honor size hints, i.e. respect size ratio.
1599 * \lfield border_width The client border width.
1600 * \lfield border_color The client border color.
1601 * \lfield titlebar The client titlebar.
1602 * \lfield urgent The client urgent state.
1603 * \lfield content An image representing the client window content (screenshot).
1604 * \lfield focus The focused client.
1605 * \lfield opacity The client opacity between 0 and 1.
1606 * \lfield ontop The client is on top of every other windows.
1607 * \lfield above The client is above normal windows.
1608 * \lfield below The client is below normal windows.
1609 * \lfield fullscreen The client is fullscreen or not.
1610 * \lfield maximized_horizontal The client is maximized horizontally or not.
1611 * \lfield maximized_vertical The client is maximized vertically or not.
1612 * \lfield transient_for Return the client the window is transient for.
1613 * \lfield group_id Identification unique to a group of windows.
1614 * \lfield leader_id Identification unique to windows spawned by the same command.
1615 * \lfield size_hints A table with size hints of the client: user_position,
1616 * user_size, program_position, program_size, etc.
1618 static int
1619 luaA_client_index(lua_State *L)
1621 size_t len;
1622 ssize_t slen;
1623 client_t *c = luaA_client_checkudata(L, 1);
1624 const char *buf = luaL_checklstring(L, 2, &len);
1625 char *value;
1626 void *data;
1627 xcb_get_property_cookie_t prop_c;
1628 xcb_get_property_reply_t *prop_r = NULL;
1629 double d;
1631 if(luaA_usemetatable(L, 1, 2))
1632 return 1;
1634 switch(a_tokenize(buf, len))
1636 case A_TK_NAME:
1637 lua_pushstring(L, c->name);
1638 break;
1639 case A_TK_TRANSIENT_FOR:
1640 return client_push(globalconf.L, c->transient_for);
1641 case A_TK_SKIP_TASKBAR:
1642 lua_pushboolean(L, c->skiptb);
1643 break;
1644 case A_TK_CONTENT:
1645 return client_getcontent(c);
1646 case A_TK_TYPE:
1647 switch(c->type)
1649 case WINDOW_TYPE_DESKTOP:
1650 lua_pushliteral(L, "desktop");
1651 break;
1652 case WINDOW_TYPE_DOCK:
1653 lua_pushliteral(L, "dock");
1654 break;
1655 case WINDOW_TYPE_SPLASH:
1656 lua_pushliteral(L, "splash");
1657 break;
1658 case WINDOW_TYPE_DIALOG:
1659 lua_pushliteral(L, "dialog");
1660 break;
1661 case WINDOW_TYPE_MENU:
1662 lua_pushliteral(L, "menu");
1663 break;
1664 case WINDOW_TYPE_TOOLBAR:
1665 lua_pushliteral(L, "toolbar");
1666 break;
1667 case WINDOW_TYPE_UTILITY:
1668 lua_pushliteral(L, "utility");
1669 break;
1670 case WINDOW_TYPE_DROPDOWN_MENU:
1671 lua_pushliteral(L, "dropdown_menu");
1672 break;
1673 case WINDOW_TYPE_POPUP_MENU:
1674 lua_pushliteral(L, "popup_menu");
1675 break;
1676 case WINDOW_TYPE_TOOLTIP:
1677 lua_pushliteral(L, "tooltip");
1678 break;
1679 case WINDOW_TYPE_NOTIFICATION:
1680 lua_pushliteral(L, "notification");
1681 break;
1682 case WINDOW_TYPE_COMBO:
1683 lua_pushliteral(L, "combo");
1684 break;
1685 case WINDOW_TYPE_DND:
1686 lua_pushliteral(L, "dnd");
1687 break;
1688 case WINDOW_TYPE_NORMAL:
1689 lua_pushliteral(L, "normal");
1690 break;
1692 break;
1693 case A_TK_CLASS:
1694 lua_pushstring(L, c->class);
1695 break;
1696 case A_TK_INSTANCE:
1697 lua_pushstring(L, c->instance);
1698 break;
1699 case A_TK_ROLE:
1700 if(!xutil_text_prop_get(globalconf.connection, c->win,
1701 WM_WINDOW_ROLE, &value, &slen))
1702 return 0;
1703 lua_pushlstring(L, value, slen);
1704 p_delete(&value);
1705 break;
1706 case A_TK_PID:
1707 prop_c = xcb_get_property_unchecked(globalconf.connection, false, c->win, _NET_WM_PID, CARDINAL, 0L, 1L);
1708 prop_r = xcb_get_property_reply(globalconf.connection, prop_c, NULL);
1710 if(prop_r && prop_r->value_len && (data = xcb_get_property_value(prop_r)))
1711 lua_pushnumber(L, *(uint32_t *)data);
1712 else
1714 p_delete(&prop_r);
1715 return 0;
1717 break;
1718 case A_TK_ID:
1719 lua_pushnumber(L, c->win);
1720 break;
1721 case A_TK_LEADER_ID:
1722 lua_pushnumber(L, c->leader_win);
1723 break;
1724 case A_TK_MACHINE:
1725 if(!xutil_text_prop_get(globalconf.connection, c->win,
1726 WM_CLIENT_MACHINE, &value, &slen))
1727 return 0;
1728 lua_pushlstring(L, value, slen);
1729 p_delete(&value);
1730 break;
1731 case A_TK_ICON_NAME:
1732 lua_pushstring(L, c->icon_name);
1733 break;
1734 case A_TK_SCREEN:
1735 lua_pushnumber(L, 1 + screen_array_indexof(&globalconf.screens, c->screen));
1736 break;
1737 case A_TK_HIDE:
1738 lua_pushboolean(L, c->ishidden);
1739 break;
1740 case A_TK_MINIMIZED:
1741 lua_pushboolean(L, c->isminimized);
1742 break;
1743 case A_TK_FULLSCREEN:
1744 lua_pushboolean(L, c->isfullscreen);
1745 break;
1746 case A_TK_GROUP_ID:
1747 if(c->group_win)
1748 lua_pushnumber(L, c->group_win);
1749 else
1750 return 0;
1751 break;
1752 case A_TK_MAXIMIZED_HORIZONTAL:
1753 lua_pushboolean(L, c->ismaxhoriz);
1754 break;
1755 case A_TK_MAXIMIZED_VERTICAL:
1756 lua_pushboolean(L, c->ismaxvert);
1757 break;
1758 case A_TK_ICON:
1759 image_push(L, c->icon);
1760 break;
1761 case A_TK_OPACITY:
1762 if((d = window_opacity_get(c->win)) >= 0)
1763 lua_pushnumber(L, d);
1764 else
1765 return 0;
1766 break;
1767 case A_TK_ONTOP:
1768 lua_pushboolean(L, c->isontop);
1769 break;
1770 case A_TK_ABOVE:
1771 lua_pushboolean(L, c->isabove);
1772 break;
1773 case A_TK_BELOW:
1774 lua_pushboolean(L, c->isbelow);
1775 break;
1776 case A_TK_STICKY:
1777 lua_pushboolean(L, c->issticky);
1778 break;
1779 case A_TK_SIZE_HINTS_HONOR:
1780 lua_pushboolean(L, c->size_hints_honor);
1781 break;
1782 case A_TK_BORDER_WIDTH:
1783 lua_pushnumber(L, c->border);
1784 break;
1785 case A_TK_BORDER_COLOR:
1786 luaA_pushxcolor(L, &c->border_color);
1787 break;
1788 case A_TK_TITLEBAR:
1789 return wibox_push(L, c->titlebar);
1790 case A_TK_URGENT:
1791 lua_pushboolean(L, c->isurgent);
1792 break;
1793 case A_TK_SIZE_HINTS:
1795 const char *u_or_p = NULL;
1797 lua_createtable(L, 0, 1);
1799 if(c->size_hints.flags & XCB_SIZE_HINT_US_POSITION)
1800 u_or_p = "user_position";
1801 else if(c->size_hints.flags & XCB_SIZE_HINT_P_POSITION)
1802 u_or_p = "program_position";
1804 if(u_or_p)
1806 lua_createtable(L, 0, 2);
1807 lua_pushnumber(L, c->size_hints.x);
1808 lua_setfield(L, -2, "x");
1809 lua_pushnumber(L, c->size_hints.y);
1810 lua_setfield(L, -2, "y");
1811 lua_setfield(L, -2, u_or_p);
1812 u_or_p = NULL;
1815 if(c->size_hints.flags & XCB_SIZE_HINT_US_SIZE)
1816 u_or_p = "user_size";
1817 else if(c->size_hints.flags & XCB_SIZE_HINT_P_SIZE)
1818 u_or_p = "program_size";
1820 if(u_or_p)
1822 lua_createtable(L, 0, 2);
1823 lua_pushnumber(L, c->size_hints.width);
1824 lua_setfield(L, -2, "width");
1825 lua_pushnumber(L, c->size_hints.height);
1826 lua_setfield(L, -2, "height");
1827 lua_setfield(L, -2, u_or_p);
1830 if(c->size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE)
1832 lua_pushnumber(L, c->size_hints.min_width);
1833 lua_setfield(L, -2, "min_width");
1834 lua_pushnumber(L, c->size_hints.min_height);
1835 lua_setfield(L, -2, "min_height");
1838 if(c->size_hints.flags & XCB_SIZE_HINT_P_MAX_SIZE)
1840 lua_pushnumber(L, c->size_hints.max_width);
1841 lua_setfield(L, -2, "max_width");
1842 lua_pushnumber(L, c->size_hints.max_height);
1843 lua_setfield(L, -2, "max_height");
1846 if(c->size_hints.flags & XCB_SIZE_HINT_P_RESIZE_INC)
1848 lua_pushnumber(L, c->size_hints.width_inc);
1849 lua_setfield(L, -2, "width_inc");
1850 lua_pushnumber(L, c->size_hints.height_inc);
1851 lua_setfield(L, -2, "height_inc");
1854 if(c->size_hints.flags & XCB_SIZE_HINT_P_ASPECT)
1856 lua_pushnumber(L, c->size_hints.min_aspect_num);
1857 lua_setfield(L, -2, "min_aspect_num");
1858 lua_pushnumber(L, c->size_hints.min_aspect_den);
1859 lua_setfield(L, -2, "min_aspect_den");
1860 lua_pushnumber(L, c->size_hints.max_aspect_num);
1861 lua_setfield(L, -2, "max_aspect_num");
1862 lua_pushnumber(L, c->size_hints.max_aspect_den);
1863 lua_setfield(L, -2, "max_aspect_den");
1866 if(c->size_hints.flags & XCB_SIZE_HINT_BASE_SIZE)
1868 lua_pushnumber(L, c->size_hints.base_width);
1869 lua_setfield(L, -2, "base_width");
1870 lua_pushnumber(L, c->size_hints.base_height);
1871 lua_setfield(L, -2, "base_height");
1874 if(c->size_hints.flags & XCB_SIZE_HINT_P_WIN_GRAVITY)
1876 switch(c->size_hints.win_gravity)
1878 default:
1879 lua_pushliteral(L, "north_west");
1880 break;
1881 case XCB_GRAVITY_NORTH:
1882 lua_pushliteral(L, "north");
1883 break;
1884 case XCB_GRAVITY_NORTH_EAST:
1885 lua_pushliteral(L, "north_east");
1886 break;
1887 case XCB_GRAVITY_WEST:
1888 lua_pushliteral(L, "west");
1889 break;
1890 case XCB_GRAVITY_CENTER:
1891 lua_pushliteral(L, "center");
1892 break;
1893 case XCB_GRAVITY_EAST:
1894 lua_pushliteral(L, "east");
1895 break;
1896 case XCB_GRAVITY_SOUTH_WEST:
1897 lua_pushliteral(L, "south_west");
1898 break;
1899 case XCB_GRAVITY_SOUTH:
1900 lua_pushliteral(L, "south");
1901 break;
1902 case XCB_GRAVITY_SOUTH_EAST:
1903 lua_pushliteral(L, "south_east");
1904 break;
1905 case XCB_GRAVITY_STATIC:
1906 lua_pushliteral(L, "static");
1907 break;
1909 lua_setfield(L, -2, "win_gravity");
1912 break;
1913 default:
1914 return 0;
1917 return 1;
1920 /** Get or set mouse buttons 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 mouse button bindings objects, or nothing.
1926 * \return The array of mouse button bindings objects of this client.
1928 static int
1929 luaA_client_buttons(lua_State *L)
1931 client_t *client = luaA_client_checkudata(L, 1);
1932 button_array_t *buttons = &client->buttons;
1934 if(lua_gettop(L) == 2)
1935 luaA_button_array_set(L, 2, buttons);
1937 window_buttons_grab(client->win, &client->buttons);
1939 return luaA_button_array_get(L, buttons);
1942 /** Get or set keys bindings for a client.
1943 * \param L The Lua VM state.
1944 * \return The number of element pushed on stack.
1945 * \luastack
1946 * \lvalue A client.
1947 * \lparam An array of key bindings objects, or nothing.
1948 * \return The array of key bindings objects of this client.
1950 static int
1951 luaA_client_keys(lua_State *L)
1953 client_t *c = luaA_client_checkudata(L, 1);
1954 key_array_t *keys = &c->keys;
1956 if(lua_gettop(L) == 2)
1958 luaA_key_array_set(L, 2, keys);
1959 xcb_ungrab_key(globalconf.connection, XCB_GRAB_ANY, c->win, XCB_BUTTON_MASK_ANY);
1960 window_grabkeys(c->win, keys);
1963 return luaA_key_array_get(L, keys);
1966 /* Client module.
1967 * \param L The Lua VM state.
1968 * \return The number of pushed elements.
1970 static int
1971 luaA_client_module_index(lua_State *L)
1973 size_t len;
1974 const char *buf = luaL_checklstring(L, 2, &len);
1976 switch(a_tokenize(buf, len))
1978 case A_TK_FOCUS:
1979 return client_push(globalconf.L, globalconf.screen_focus->client_focus);
1980 break;
1981 default:
1982 return 0;
1986 /* Client module new index.
1987 * \param L The Lua VM state.
1988 * \return The number of pushed elements.
1990 static int
1991 luaA_client_module_newindex(lua_State *L)
1993 size_t len;
1994 const char *buf = luaL_checklstring(L, 2, &len);
1995 client_t *c;
1997 switch(a_tokenize(buf, len))
1999 case A_TK_FOCUS:
2000 c = luaA_client_checkudata(L, 3);
2001 client_focus(c);
2002 break;
2003 default:
2004 break;
2007 return 0;
2010 const struct luaL_reg awesome_client_methods[] =
2012 { "get", luaA_client_get },
2013 { "__index", luaA_client_module_index },
2014 { "__newindex", luaA_client_module_newindex },
2015 { NULL, NULL }
2017 const struct luaL_reg awesome_client_meta[] =
2019 { "isvisible", luaA_client_isvisible },
2020 { "geometry", luaA_client_geometry },
2021 { "struts", luaA_client_struts },
2022 { "buttons", luaA_client_buttons },
2023 { "keys", luaA_client_keys },
2024 { "tags", luaA_client_tags },
2025 { "kill", luaA_client_kill },
2026 { "swap", luaA_client_swap },
2027 { "raise", luaA_client_raise },
2028 { "lower", luaA_client_lower },
2029 { "redraw", luaA_client_redraw },
2030 { "unmanage", luaA_client_unmanage },
2031 { "__index", luaA_client_index },
2032 { "__newindex", luaA_client_newindex },
2033 { "__gc", luaA_client_gc },
2034 { "__tostring", luaA_client_tostring },
2035 { NULL, NULL }
2038 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80