swindow: fix draw_context_update for north and south orientation
[awesome.git] / client.c
blob111d4562e8d32fc75b1e4b316e6a128988fc71d0
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 /** Update the position of all window with struts on a specific screen.
758 * \param screen The screen that should be processed.
760 void
761 client_update_strut_positions(screen_t *screen)
763 area_t allowed_area, geom;
765 /* Ignore all struts for starters. */
766 foreach(_c, globalconf.clients)
768 client_t *c = *_c;
769 if(c->screen == screen && client_hasstrut(c))
770 c->ignore_strut = true;
773 /* Rationale:
774 * Top and bottom panels are common, so they take precendence.
775 * WINDOW_TYPE_DOCK really wants to be at the side, so choose them first.
778 /* WINDOW_TYPE_DOCK: top + bottom. */
779 foreach(_c, globalconf.clients)
781 client_t *c = *_c;
782 if(c->screen != screen || !client_hasstrut(c) || c->type != WINDOW_TYPE_DOCK)
783 continue;
785 /* Screen area, minus padding, wibox'es and already processed struts. */
786 allowed_area = screen_area_get(c->screen,
787 &c->screen->wiboxes,
788 &c->screen->padding,
789 true);
791 geom = c->geometry;
793 if(c->strut.top || c->strut.top_start_x || c->strut.top_end_x)
795 geom.y = allowed_area.y;
796 if(geom.x < allowed_area.x
797 || geom.x + geom.width > allowed_area.x + allowed_area.width)
799 geom.x = allowed_area.x;
800 if(geom.width > allowed_area.width)
801 geom.width = allowed_area.width;
803 c->ignore_strut = false;
804 client_resize(c, geom, false);
806 else if(c->strut.bottom || c->strut.bottom_start_x || c->strut.bottom_end_x)
808 geom.y = allowed_area.y + allowed_area.height - geom.height;
809 if(geom.x < allowed_area.x
810 || geom.x + geom.width > allowed_area.x + allowed_area.width)
812 geom.x = allowed_area.x;
813 if(geom.width > allowed_area.width)
814 geom.width = allowed_area.width;
816 c->ignore_strut = false;
817 client_resize(c, geom, false);
821 /* WINDOW_TYPE_DOCK: left + right. */
822 foreach(_c, globalconf.clients)
824 client_t *c = *_c;
825 if(c->screen != screen || !client_hasstrut(c) || c->type != WINDOW_TYPE_DOCK)
826 continue;
828 /* Screen area, minus padding, wibox'es and already processed struts. */
829 allowed_area = screen_area_get(c->screen,
830 &c->screen->wiboxes,
831 &c->screen->padding,
832 true);
834 geom = c->geometry;
836 if(c->strut.left || c->strut.left_start_y || c->strut.left_end_y)
838 geom.x = allowed_area.x;
839 if(geom.y < allowed_area.y
840 || geom.y + geom.height > allowed_area.y + allowed_area.height)
842 geom.y = allowed_area.y;
843 if (geom.height > allowed_area.height)
844 geom.height = allowed_area.height;
846 c->ignore_strut = false;
847 client_resize(c, geom, false);
849 else if(c->strut.right || c->strut.right_start_y || c->strut.right_end_y)
851 geom.x = allowed_area.x + allowed_area.width - geom.width;
852 if(geom.y < allowed_area.y
853 || geom.y + geom.height > allowed_area.y + allowed_area.height)
855 geom.y = allowed_area.y;
856 if (geom.height > allowed_area.height)
857 geom.height = allowed_area.height;
859 c->ignore_strut = false;
860 client_resize(c, geom, false);
864 /* not WINDOW_TYPE_DOCK: top + bottom. */
865 foreach(_c, globalconf.clients)
867 client_t *c = *_c;
868 if(c->screen != screen || !client_hasstrut(c) || c->type == WINDOW_TYPE_DOCK)
869 continue;
871 /* Screen area, minus padding, wibox'es and already processed struts. */
872 allowed_area = screen_area_get(c->screen,
873 &c->screen->wiboxes,
874 &c->screen->padding,
875 true);
877 geom = c->geometry;
879 if(c->strut.top || c->strut.top_start_x || c->strut.top_end_x)
881 geom.y = allowed_area.y;
882 if(geom.x < allowed_area.x
883 || geom.x + geom.width > allowed_area.x + allowed_area.width)
885 geom.x = allowed_area.x;
886 if(geom.width > allowed_area.width)
887 geom.width = allowed_area.width;
889 c->ignore_strut = false;
890 client_resize(c, geom, false);
892 else if(c->strut.bottom || c->strut.bottom_start_x || c->strut.bottom_end_x)
894 geom.y = allowed_area.y + allowed_area.height - geom.height;
895 if(geom.x < allowed_area.x
896 || geom.x + geom.width > allowed_area.x + allowed_area.width)
898 geom.x = allowed_area.x;
899 if(geom.width > allowed_area.width)
900 geom.width = allowed_area.width;
902 c->ignore_strut = false;
903 client_resize(c, geom, false);
907 /* not WINDOW_TYPE_DOCK: left + right. */
908 foreach(_c, globalconf.clients)
910 client_t *c = *_c;
911 if(c->screen != screen || !client_hasstrut(c) || c->type == WINDOW_TYPE_DOCK)
912 continue;
914 /* Screen area, minus padding, wibox'es and already processed struts. */
915 allowed_area = screen_area_get(c->screen,
916 &c->screen->wiboxes,
917 &c->screen->padding,
918 true);
920 geom = c->geometry;
922 if(c->strut.left || c->strut.left_start_y || c->strut.left_end_y)
924 geom.x = allowed_area.x;
925 if(geom.y < allowed_area.y
926 || geom.y + geom.height > allowed_area.y + allowed_area.height)
928 geom.y = allowed_area.y;
929 if (geom.height > allowed_area.height)
930 geom.height = allowed_area.height;
932 c->ignore_strut = false;
933 client_resize(c, geom, false);
935 else if(c->strut.right || c->strut.right_start_y || c->strut.right_end_y)
937 geom.x = allowed_area.x + allowed_area.width - geom.width;
938 if(geom.y < allowed_area.y
939 || geom.y + geom.height > allowed_area.y + allowed_area.height)
941 geom.y = allowed_area.y;
942 if (geom.height > allowed_area.height)
943 geom.height = allowed_area.height;
945 c->ignore_strut = false;
946 client_resize(c, geom, false);
952 /** Set a client minimized, or not.
953 * \param c The client.
954 * \param s Set or not the client minimized.
956 void
957 client_setminimized(client_t *c, bool s)
959 if(c->isminimized != s)
961 client_need_arrange(c);
962 c->isminimized = s;
963 client_need_arrange(c);
964 ewmh_client_update_hints(c);
965 /* execute hook */
966 hooks_property(c, "minimized");
970 /** Set a client sticky, or not.
971 * \param c The client.
972 * \param s Set or not the client sticky.
974 void
975 client_setsticky(client_t *c, bool s)
977 if(c->issticky != s)
979 client_need_arrange(c);
980 c->issticky = s;
981 client_need_arrange(c);
982 ewmh_client_update_hints(c);
983 hooks_property(c, "sticky");
987 /** Set a client fullscreen, or not.
988 * \param c The client.
989 * \param s Set or not the client fullscreen.
991 void
992 client_setfullscreen(client_t *c, bool s)
994 if(c->isfullscreen != s)
996 area_t geometry;
998 /* Make sure the current geometry is stored without titlebar. */
999 if(s)
1000 titlebar_ban(c->titlebar);
1002 /* become fullscreen! */
1003 if((c->isfullscreen = s))
1005 /* remove any max state */
1006 client_setmaxhoriz(c, false);
1007 client_setmaxvert(c, false);
1008 /* You can only be part of one of the special layers. */
1009 client_setbelow(c, false);
1010 client_setabove(c, false);
1011 client_setontop(c, false);
1013 geometry = screen_area_get(c->screen, NULL, NULL, false);
1014 c->geometries.fullscreen = c->geometry;
1015 c->border_fs = c->border;
1016 client_setborder(c, 0);
1018 else
1020 geometry = c->geometries.fullscreen;
1021 client_setborder(c, c->border_fs);
1023 client_resize(c, geometry, false);
1024 client_need_arrange(c);
1025 client_stack();
1026 ewmh_client_update_hints(c);
1027 hooks_property(c, "fullscreen");
1031 /** Set a client horizontally maximized.
1032 * \param c The client.
1033 * \param s The maximized status.
1035 void
1036 client_setmaxhoriz(client_t *c, bool s)
1038 if(c->ismaxhoriz != s)
1040 area_t geometry;
1042 if((c->ismaxhoriz = s))
1044 /* remove fullscreen mode */
1045 client_setfullscreen(c, false);
1047 geometry = screen_area_get(c->screen,
1048 &c->screen->wiboxes,
1049 &c->screen->padding,
1050 true);
1051 geometry.y = c->geometry.y;
1052 geometry.height = c->geometry.height;
1053 c->geometries.max.x = c->geometry.x;
1054 c->geometries.max.width = c->geometry.width;
1056 else
1058 geometry = c->geometry;
1059 geometry.x = c->geometries.max.x;
1060 geometry.width = c->geometries.max.width;
1063 client_resize(c, geometry, c->size_hints_honor);
1064 client_need_arrange(c);
1065 client_stack();
1066 ewmh_client_update_hints(c);
1067 hooks_property(c, "maximized_horizontal");
1071 /** Set a client vertically maximized.
1072 * \param c The client.
1073 * \param s The maximized status.
1075 void
1076 client_setmaxvert(client_t *c, bool s)
1078 if(c->ismaxvert != s)
1080 area_t geometry;
1082 if((c->ismaxvert = s))
1084 /* remove fullscreen mode */
1085 client_setfullscreen(c, false);
1087 geometry = screen_area_get(c->screen,
1088 &c->screen->wiboxes,
1089 &c->screen->padding,
1090 true);
1091 geometry.x = c->geometry.x;
1092 geometry.width = c->geometry.width;
1093 c->geometries.max.y = c->geometry.y;
1094 c->geometries.max.height = c->geometry.height;
1096 else
1098 geometry = c->geometry;
1099 geometry.y = c->geometries.max.y;
1100 geometry.height = c->geometries.max.height;
1103 client_resize(c, geometry, c->size_hints_honor);
1104 client_need_arrange(c);
1105 client_stack();
1106 ewmh_client_update_hints(c);
1107 hooks_property(c, "maximized_vertical");
1111 /** Set a client above, or not.
1112 * \param c The client.
1113 * \param s Set or not the client above.
1115 void
1116 client_setabove(client_t *c, bool s)
1118 if(c->isabove != s)
1120 /* You can only be part of one of the special layers. */
1121 if(s)
1123 client_setbelow(c, false);
1124 client_setontop(c, false);
1125 client_setfullscreen(c, false);
1127 c->isabove = s;
1128 client_stack();
1129 ewmh_client_update_hints(c);
1130 /* execute hook */
1131 hooks_property(c, "above");
1135 /** Set a client below, or not.
1136 * \param c The client.
1137 * \param s Set or not the client below.
1139 void
1140 client_setbelow(client_t *c, bool s)
1142 if(c->isbelow != s)
1144 /* You can only be part of one of the special layers. */
1145 if(s)
1147 client_setabove(c, false);
1148 client_setontop(c, false);
1149 client_setfullscreen(c, false);
1151 c->isbelow = s;
1152 client_stack();
1153 ewmh_client_update_hints(c);
1154 /* execute hook */
1155 hooks_property(c, "below");
1159 /** Set a client modal, or not.
1160 * \param c The client.
1161 * \param s Set or not the client moda.
1163 void
1164 client_setmodal(client_t *c, bool s)
1166 if(c->ismodal != s)
1168 c->ismodal = s;
1169 client_stack();
1170 ewmh_client_update_hints(c);
1171 /* execute hook */
1172 hooks_property(c, "modal");
1176 /** Set a client ontop, or not.
1177 * \param c The client.
1178 * \param s Set or not the client moda.
1180 void
1181 client_setontop(client_t *c, bool s)
1183 if(c->isontop != s)
1185 /* You can only be part of one of the special layers. */
1186 if(s)
1188 client_setabove(c, false);
1189 client_setbelow(c, false);
1190 client_setfullscreen(c, false);
1192 c->isontop = s;
1193 client_stack();
1194 /* execute hook */
1195 hooks_property(c, "ontop");
1199 /** Unban a client and move it back into the viewport.
1200 * \param c The client.
1202 void
1203 client_unban(client_t *c)
1205 if(c->isbanned)
1207 /* Move the client back where it belongs. */
1208 uint32_t request[] = { c->geometries.internal.x,
1209 c->geometries.internal.y,
1210 c->geometries.internal.width,
1211 c->geometries.internal.height };
1213 xcb_configure_window(globalconf.connection, c->win,
1214 XCB_CONFIG_WINDOW_X
1215 | XCB_CONFIG_WINDOW_Y
1216 | XCB_CONFIG_WINDOW_WIDTH
1217 | XCB_CONFIG_WINDOW_HEIGHT,
1218 request);
1220 c->isbanned = false;
1222 /* All the wiboxes (may) need to be repositioned. */
1223 if(client_hasstrut(c))
1224 wibox_update_positions();
1228 /** Unmanage a client.
1229 * \param c The client.
1231 void
1232 client_unmanage(client_t *c)
1234 tag_array_t *tags = &c->screen->tags;
1236 /* Reset transient_for attributes of widows that maybe refering to us */
1237 foreach(_tc, globalconf.clients)
1239 client_t *tc = *_tc;
1240 if(tc->transient_for == c)
1241 tc->transient_for = NULL;
1244 if(globalconf.screens.tab[c->phys_screen].prev_client_focus == c)
1245 globalconf.screens.tab[c->phys_screen].prev_client_focus = NULL;
1247 if(globalconf.screens.tab[c->phys_screen].client_focus == c)
1248 client_unfocus(c);
1250 /* remove client from global list and everywhere else */
1251 foreach(elem, globalconf.clients)
1252 if(*elem == c)
1254 client_array_remove(&globalconf.clients, elem);
1255 break;
1257 stack_client_remove(c);
1258 for(int i = 0; i < tags->len; i++)
1259 untag_client(c, tags->tab[i]);
1261 /* call hook */
1262 if(globalconf.hooks.unmanage != LUA_REFNIL)
1264 client_push(globalconf.L, c);
1265 luaA_dofunction(globalconf.L, globalconf.hooks.unmanage, 1, 0);
1268 /* Call hook to notify list change */
1269 if(globalconf.hooks.clients != LUA_REFNIL)
1270 luaA_dofunction(globalconf.L, globalconf.hooks.clients, 0, 0);
1272 /* The server grab construct avoids race conditions. */
1273 xcb_grab_server(globalconf.connection);
1275 xcb_ungrab_button(globalconf.connection, XCB_BUTTON_INDEX_ANY, c->win,
1276 XCB_BUTTON_MASK_ANY);
1277 window_state_set(c->win, XCB_WM_STATE_WITHDRAWN);
1279 xcb_flush(globalconf.connection);
1280 xcb_ungrab_server(globalconf.connection);
1282 titlebar_client_detach(c);
1284 ewmh_update_net_client_list(c->phys_screen);
1286 /* All the wiboxes (may) need to be repositioned. */
1287 if(client_hasstrut(c))
1288 wibox_update_positions();
1290 /* set client as invalid */
1291 c->invalid = true;
1293 client_unref(globalconf.L, c);
1296 /** Kill a client via a WM_DELETE_WINDOW request or KillClient if not
1297 * supported.
1298 * \param c The client to kill.
1300 void
1301 client_kill(client_t *c)
1303 if(window_hasproto(c->win, WM_DELETE_WINDOW))
1305 xcb_client_message_event_t ev;
1307 /* Initialize all of event's fields first */
1308 p_clear(&ev, 1);
1310 ev.response_type = XCB_CLIENT_MESSAGE;
1311 ev.window = c->win;
1312 ev.format = 32;
1313 ev.data.data32[1] = XCB_CURRENT_TIME;
1314 ev.type = WM_PROTOCOLS;
1315 ev.data.data32[0] = WM_DELETE_WINDOW;
1317 xcb_send_event(globalconf.connection, false, c->win,
1318 XCB_EVENT_MASK_NO_EVENT, (char *) &ev);
1320 else
1321 xcb_kill_client(globalconf.connection, c->win);
1324 /** Get all clients into a table.
1325 * \param L The Lua VM state.
1326 * \return The number of elements pushed on stack.
1327 * \luastack
1328 * \lparam An optional screen nunmber.
1329 * \lreturn A table with all clients.
1331 static int
1332 luaA_client_get(lua_State *L)
1334 int i = 1, screen;
1336 screen = luaL_optnumber(L, 1, 0) - 1;
1338 lua_newtable(L);
1340 if(screen == -1)
1341 foreach(c, globalconf.clients)
1343 client_push(L, *c);
1344 lua_rawseti(L, -2, i++);
1346 else
1348 luaA_checkscreen(screen);
1349 foreach(c, globalconf.clients)
1350 if((*c)->screen == &globalconf.screens.tab[screen])
1352 client_push(L, *c);
1353 lua_rawseti(L, -2, i++);
1357 return 1;
1360 /** Check if a client is visible on its screen.
1361 * \param L The Lua VM state.
1362 * \return The number of elements pushed on stack.
1363 * \luastack
1364 * \lvalue A client.
1365 * \lreturn A boolean value, true if the client is visible, false otherwise.
1367 static int
1368 luaA_client_isvisible(lua_State *L)
1370 client_t *c = luaA_client_checkudata(L, 1);
1371 lua_pushboolean(L, client_isvisible(c, c->screen));
1372 return 1;
1375 /** Set client border width.
1376 * \param c The client.
1377 * \param width The border width.
1379 void
1380 client_setborder(client_t *c, int width)
1382 uint32_t w = width;
1384 if(width > 0 && (c->type == WINDOW_TYPE_DOCK
1385 || c->type == WINDOW_TYPE_SPLASH
1386 || c->type == WINDOW_TYPE_DESKTOP
1387 || c->isfullscreen))
1388 return;
1390 if(width == c->border || width < 0)
1391 return;
1393 /* Update geometry with the new border. */
1394 c->geometry.width -= 2 * c->border;
1395 c->geometry.height -= 2 * c->border;
1397 c->border = width;
1398 xcb_configure_window(globalconf.connection, c->win,
1399 XCB_CONFIG_WINDOW_BORDER_WIDTH, &w);
1401 c->geometry.width += 2 * c->border;
1402 c->geometry.height += 2 * c->border;
1403 /* Tiled clients will be resized by the layout functions. */
1404 client_need_arrange(c);
1406 /* Changing border size also affects the size of the titlebar. */
1407 titlebar_update_geometry(c);
1409 hooks_property(c, "border_width");
1412 /** Kill a client.
1413 * \param L The Lua VM state.
1415 * \luastack
1416 * \lvalue A client.
1418 static int
1419 luaA_client_kill(lua_State *L)
1421 client_t *c = luaA_client_checkudata(L, 1);
1422 client_kill(c);
1423 return 0;
1426 /** Swap a client with another one.
1427 * \param L The Lua VM state.
1428 * \luastack
1429 * \lvalue A client.
1430 * \lparam A client to swap with.
1432 static int
1433 luaA_client_swap(lua_State *L)
1435 client_t *c = luaA_client_checkudata(L, 1);
1436 client_t *swap = luaA_client_checkudata(L, 2);
1438 if(c != swap)
1440 client_t **ref_c = NULL, **ref_swap = NULL;
1441 foreach(item, globalconf.clients)
1443 if(*item == c)
1444 ref_c = item;
1445 else if(*item == swap)
1446 ref_swap = item;
1447 if(ref_c && ref_swap)
1448 break;
1450 /* swap ! */
1451 *ref_c = swap;
1452 *ref_swap = c;
1454 client_need_arrange(c);
1455 client_need_arrange(swap);
1457 /* Call hook to notify list change */
1458 if(globalconf.hooks.clients != LUA_REFNIL)
1459 luaA_dofunction(L, globalconf.hooks.clients, 0, 0);
1462 return 0;
1465 /** Access or set the client tags.
1466 * \param L The Lua VM state.
1467 * \return The number of elements pushed on stack.
1468 * \lparam A table with tags to set, or none to get the current tags table.
1469 * \return The clients tag.
1471 static int
1472 luaA_client_tags(lua_State *L)
1474 client_t *c = luaA_client_checkudata(L, 1);
1475 tag_array_t *tags = &c->screen->tags;
1476 int j = 0;
1478 if(lua_gettop(L) == 2)
1480 luaA_checktable(L, 2);
1481 for(int i = 0; i < tags->len; i++)
1482 untag_client(c, tags->tab[i]);
1483 lua_pushnil(L);
1484 while(lua_next(L, 2))
1485 tag_client(c);
1486 lua_pop(L, 1);
1489 lua_newtable(L);
1490 foreach(tag, *tags)
1491 if(is_client_tagged(c, *tag))
1493 tag_push(L, *tag);
1494 lua_rawseti(L, -2, ++j);
1497 return 1;
1500 /** Raise a client on top of others which are on the same layer.
1501 * \param L The Lua VM state.
1502 * \luastack
1503 * \lvalue A client.
1505 static int
1506 luaA_client_raise(lua_State *L)
1508 client_t *c = luaA_client_checkudata(L, 1);
1509 client_raise(c);
1510 return 0;
1513 /** Lower a client on bottom of others which are on the same layer.
1514 * \param L The Lua VM state.
1515 * \luastack
1516 * \lvalue A client.
1518 static int
1519 luaA_client_lower(lua_State *L)
1521 client_t *c = luaA_client_checkudata(L, 1);
1522 client_lower(c);
1523 return 0;
1526 /** Redraw a client by unmapping and mapping it quickly.
1527 * \param L The Lua VM state.
1529 * \luastack
1530 * \lvalue A client.
1532 static int
1533 luaA_client_redraw(lua_State *L)
1535 client_t *c = luaA_client_checkudata(L, 1);
1537 xcb_unmap_window(globalconf.connection, c->win);
1538 xcb_map_window(globalconf.connection, c->win);
1540 /* Set the focus on the current window if the redraw has been
1541 performed on the window where the pointer is currently on
1542 because after the unmapping/mapping, the focus is lost */
1543 if(globalconf.screen_focus->client_focus == c)
1545 client_unfocus(c);
1546 client_focus(c);
1549 return 0;
1552 /** Stop managing a client.
1553 * \param L The Lua VM state.
1554 * \return The number of elements pushed on stack.
1555 * \luastack
1556 * \lvalue A client.
1558 static int
1559 luaA_client_unmanage(lua_State *L)
1561 client_t *c = luaA_client_checkudata(L, 1);
1562 client_unmanage(c);
1563 return 0;
1566 /** Return client geometry.
1567 * \param L The Lua VM state.
1568 * \return The number of elements pushed on stack.
1569 * \luastack
1570 * \lparam A table with new coordinates, or none.
1571 * \lreturn A table with client coordinates.
1573 static int
1574 luaA_client_geometry(lua_State *L)
1576 client_t *c = luaA_client_checkudata(L, 1);
1578 if(lua_gettop(L) == 2)
1580 area_t geometry;
1582 luaA_checktable(L, 2);
1583 geometry.x = luaA_getopt_number(L, 2, "x", c->geometry.x);
1584 geometry.y = luaA_getopt_number(L, 2, "y", c->geometry.y);
1585 if(client_isfixed(c))
1587 geometry.width = c->geometry.width;
1588 geometry.height = c->geometry.height;
1590 else
1592 geometry.width = luaA_getopt_number(L, 2, "width", c->geometry.width);
1593 geometry.height = luaA_getopt_number(L, 2, "height", c->geometry.height);
1596 client_resize(c, geometry, c->size_hints_honor);
1599 return luaA_pusharea(L, c->geometry);
1602 /** Push a strut type to a table on stack.
1603 * \param L The Lua VM state.
1604 * \param struts The struts to push.
1605 * \return The number of elements pushed on stack.
1607 static inline int
1608 luaA_pushstruts(lua_State *L, strut_t struts)
1610 lua_createtable(L, 4, 0);
1611 lua_pushnumber(L, struts.left);
1612 lua_setfield(L, -2, "left");
1613 lua_pushnumber(L, struts.right);
1614 lua_setfield(L, -2, "right");
1615 lua_pushnumber(L, struts.top);
1616 lua_setfield(L, -2, "top");
1617 lua_pushnumber(L, struts.bottom);
1618 lua_setfield(L, -2, "bottom");
1619 return 1;
1622 /** Return client struts (reserved space at the edge of the screen).
1623 * \param L The Lua VM state.
1624 * \return The number of elements pushed on stack.
1625 * \luastack
1626 * \lparam A table with new strut values, or none.
1627 * \lreturn A table with strut values.
1629 static int
1630 luaA_client_struts(lua_State *L)
1632 client_t *c = luaA_client_checkudata(L, 1);
1634 if(lua_gettop(L) == 2)
1636 strut_t struts;
1637 area_t screen_area = display_area_get(c->phys_screen, NULL, NULL);
1639 struts.left = luaA_getopt_number(L, 2, "left", c->strut.left);
1640 struts.right = luaA_getopt_number(L, 2, "right", c->strut.right);
1641 struts.top = luaA_getopt_number(L, 2, "top", c->strut.top);
1642 struts.bottom = luaA_getopt_number(L, 2, "bottom", c->strut.bottom);
1644 if(struts.left != c->strut.left || struts.right != c->strut.right ||
1645 struts.top != c->strut.top || struts.bottom != c->strut.bottom) {
1646 /* Struts are not so well defined in the context of xinerama. So we just
1647 * give the entire root window and let the window manager decide. */
1648 struts.left_start_y = 0;
1649 struts.left_end_y = !struts.left ? 0 : screen_area.height;
1650 struts.right_start_y = 0;
1651 struts.right_end_y = !struts.right ? 0 : screen_area.height;
1652 struts.top_start_x = 0;
1653 struts.top_end_x = !struts.top ? 0 : screen_area.width;
1654 struts.bottom_start_x = 0;
1655 struts.bottom_end_x = !struts.bottom ? 0 : screen_area.width;
1657 c->strut = struts;
1659 ewmh_update_client_strut(c);
1661 client_need_arrange(c);
1662 /* All the wiboxes (may) need to be repositioned. */
1663 wibox_update_positions();
1665 hooks_property(c, "struts");
1669 return luaA_pushstruts(L, c->strut);
1672 /** Client newindex.
1673 * \param L The Lua VM state.
1674 * \return The number of elements pushed on stack.
1676 static int
1677 luaA_client_newindex(lua_State *L)
1679 size_t len;
1680 client_t *c = luaA_client_checkudata(L, 1);
1681 const char *buf = luaL_checklstring(L, 2, &len);
1682 bool b;
1683 double d;
1684 int i;
1686 switch(a_tokenize(buf, len))
1688 case A_TK_SCREEN:
1689 if(globalconf.xinerama_is_active)
1691 i = luaL_checknumber(L, 3) - 1;
1692 luaA_checkscreen(i);
1693 screen_client_moveto(c, &globalconf.screens.tab[i], true, true);
1695 break;
1696 case A_TK_HIDE:
1697 b = luaA_checkboolean(L, 3);
1698 if(b != c->ishidden)
1700 client_need_arrange(c);
1701 c->ishidden = b;
1702 client_need_arrange(c);
1704 break;
1705 case A_TK_MINIMIZED:
1706 client_setminimized(c, luaA_checkboolean(L, 3));
1707 break;
1708 case A_TK_FULLSCREEN:
1709 client_setfullscreen(c, luaA_checkboolean(L, 3));
1710 break;
1711 case A_TK_MAXIMIZED_HORIZONTAL:
1712 client_setmaxhoriz(c, luaA_checkboolean(L, 3));
1713 break;
1714 case A_TK_MAXIMIZED_VERTICAL:
1715 client_setmaxvert(c, luaA_checkboolean(L, 3));
1716 break;
1717 case A_TK_ICON:
1718 image_unref(L, c->icon);
1719 c->icon = image_ref(L);
1720 /* execute hook */
1721 hooks_property(c, "icon");
1722 break;
1723 case A_TK_OPACITY:
1724 if(lua_isnil(L, 3))
1725 window_opacity_set(c->win, -1);
1726 else
1728 d = luaL_checknumber(L, 3);
1729 if(d >= 0 && d <= 1)
1730 window_opacity_set(c->win, d);
1732 break;
1733 case A_TK_STICKY:
1734 client_setsticky(c, luaA_checkboolean(L, 3));
1735 break;
1736 case A_TK_SIZE_HINTS_HONOR:
1737 c->size_hints_honor = luaA_checkboolean(L, 3);
1738 hooks_property(c, "size_hints_honor");
1739 break;
1740 case A_TK_BORDER_WIDTH:
1741 client_setborder(c, luaL_checknumber(L, 3));
1742 break;
1743 case A_TK_ONTOP:
1744 client_setontop(c, luaA_checkboolean(L, 3));
1745 break;
1746 case A_TK_ABOVE:
1747 client_setabove(c, luaA_checkboolean(L, 3));
1748 break;
1749 case A_TK_BELOW:
1750 client_setbelow(c, luaA_checkboolean(L, 3));
1751 break;
1752 case A_TK_URGENT:
1753 client_seturgent(c, luaA_checkboolean(L, 3));
1754 break;
1755 case A_TK_BORDER_COLOR:
1756 if((buf = luaL_checklstring(L, 3, &len))
1757 && xcolor_init_reply(xcolor_init_unchecked(&c->border_color, buf, len)))
1758 xcb_change_window_attributes(globalconf.connection, c->win,
1759 XCB_CW_BORDER_PIXEL, &c->border_color.pixel);
1760 break;
1761 case A_TK_TITLEBAR:
1762 if(lua_isnil(L, 3))
1763 titlebar_client_detach(c);
1764 else
1765 titlebar_client_attach(c);
1766 break;
1767 default:
1768 return 0;
1771 return 0;
1774 /** Client object.
1775 * \param L The Lua VM state.
1776 * \return The number of elements pushed on stack.
1777 * \luastack
1778 * \lfield id The window X id.
1779 * \lfield name The client title.
1780 * \lfield skip_taskbar True if the client does not want to be in taskbar.
1781 * \lfield type The window type (desktop, normal, dock, …).
1782 * \lfield class The client class.
1783 * \lfield instance The client instance.
1784 * \lfield pid The client PID, if available.
1785 * \lfield role The window role, if available.
1786 * \lfield machine The machine client is running on.
1787 * \lfield icon_name The client name when iconified.
1788 * \lfield screen Client screen number.
1789 * \lfield hide Define if the client must be hidden, i.e. never mapped,
1790 * invisible in taskbar.
1791 * \lfield minimized Define it the client must be iconify, i.e. only visible in
1792 * taskbar.
1793 * \lfield size_hints_honor Honor size hints, i.e. respect size ratio.
1794 * \lfield border_width The client border width.
1795 * \lfield border_color The client border color.
1796 * \lfield titlebar The client titlebar.
1797 * \lfield urgent The client urgent state.
1798 * \lfield content An image representing the client window content (screenshot).
1799 * \lfield focus The focused client.
1800 * \lfield opacity The client opacity between 0 and 1.
1801 * \lfield ontop The client is on top of every other windows.
1802 * \lfield above The client is above normal windows.
1803 * \lfield below The client is below normal windows.
1804 * \lfield fullscreen The client is fullscreen or not.
1805 * \lfield maximized_horizontal The client is maximized horizontally or not.
1806 * \lfield maximized_vertical The client is maximized vertically or not.
1807 * \lfield transient_for Return the client the window is transient for.
1808 * \lfield group_id Identification unique to a group of windows.
1809 * \lfield leader_id Identification unique to windows spawned by the same command.
1810 * \lfield size_hints A table with size hints of the client: user_position,
1811 * user_size, program_position, program_size, etc.
1813 static int
1814 luaA_client_index(lua_State *L)
1816 size_t len;
1817 ssize_t slen;
1818 client_t *c = luaA_client_checkudata(L, 1);
1819 const char *buf = luaL_checklstring(L, 2, &len);
1820 char *value;
1821 void *data;
1822 xcb_get_property_cookie_t prop_c;
1823 xcb_get_property_reply_t *prop_r = NULL;
1824 double d;
1826 if(luaA_usemetatable(L, 1, 2))
1827 return 1;
1829 switch(a_tokenize(buf, len))
1831 case A_TK_NAME:
1832 lua_pushstring(L, c->name);
1833 break;
1834 case A_TK_TRANSIENT_FOR:
1835 return client_push(globalconf.L, c->transient_for);
1836 case A_TK_SKIP_TASKBAR:
1837 lua_pushboolean(L, c->skiptb);
1838 break;
1839 case A_TK_CONTENT:
1840 return client_getcontent(c);
1841 case A_TK_TYPE:
1842 switch(c->type)
1844 case WINDOW_TYPE_DESKTOP:
1845 lua_pushliteral(L, "desktop");
1846 break;
1847 case WINDOW_TYPE_DOCK:
1848 lua_pushliteral(L, "dock");
1849 break;
1850 case WINDOW_TYPE_SPLASH:
1851 lua_pushliteral(L, "splash");
1852 break;
1853 case WINDOW_TYPE_DIALOG:
1854 lua_pushliteral(L, "dialog");
1855 break;
1856 case WINDOW_TYPE_MENU:
1857 lua_pushliteral(L, "menu");
1858 break;
1859 case WINDOW_TYPE_TOOLBAR:
1860 lua_pushliteral(L, "toolbar");
1861 break;
1862 case WINDOW_TYPE_UTILITY:
1863 lua_pushliteral(L, "utility");
1864 break;
1865 case WINDOW_TYPE_DROPDOWN_MENU:
1866 lua_pushliteral(L, "dropdown_menu");
1867 break;
1868 case WINDOW_TYPE_POPUP_MENU:
1869 lua_pushliteral(L, "popup_menu");
1870 break;
1871 case WINDOW_TYPE_TOOLTIP:
1872 lua_pushliteral(L, "tooltip");
1873 break;
1874 case WINDOW_TYPE_NOTIFICATION:
1875 lua_pushliteral(L, "notification");
1876 break;
1877 case WINDOW_TYPE_COMBO:
1878 lua_pushliteral(L, "combo");
1879 break;
1880 case WINDOW_TYPE_DND:
1881 lua_pushliteral(L, "dnd");
1882 break;
1883 case WINDOW_TYPE_NORMAL:
1884 lua_pushliteral(L, "normal");
1885 break;
1887 break;
1888 case A_TK_CLASS:
1889 lua_pushstring(L, c->class);
1890 break;
1891 case A_TK_INSTANCE:
1892 lua_pushstring(L, c->instance);
1893 break;
1894 case A_TK_ROLE:
1895 if(!xutil_text_prop_get(globalconf.connection, c->win,
1896 WM_WINDOW_ROLE, &value, &slen))
1897 return 0;
1898 lua_pushlstring(L, value, slen);
1899 p_delete(&value);
1900 break;
1901 case A_TK_PID:
1902 prop_c = xcb_get_property_unchecked(globalconf.connection, false, c->win, _NET_WM_PID, CARDINAL, 0L, 1L);
1903 prop_r = xcb_get_property_reply(globalconf.connection, prop_c, NULL);
1905 if(prop_r && prop_r->value_len && (data = xcb_get_property_value(prop_r)))
1906 lua_pushnumber(L, *(uint32_t *)data);
1907 else
1909 p_delete(&prop_r);
1910 return 0;
1912 break;
1913 case A_TK_ID:
1914 lua_pushnumber(L, c->win);
1915 break;
1916 case A_TK_LEADER_ID:
1917 lua_pushnumber(L, c->leader_win);
1918 break;
1919 case A_TK_MACHINE:
1920 if(!xutil_text_prop_get(globalconf.connection, c->win,
1921 WM_CLIENT_MACHINE, &value, &slen))
1922 return 0;
1923 lua_pushlstring(L, value, slen);
1924 p_delete(&value);
1925 break;
1926 case A_TK_ICON_NAME:
1927 lua_pushstring(L, c->icon_name);
1928 break;
1929 case A_TK_SCREEN:
1930 lua_pushnumber(L, 1 + screen_array_indexof(&globalconf.screens, c->screen));
1931 break;
1932 case A_TK_HIDE:
1933 lua_pushboolean(L, c->ishidden);
1934 break;
1935 case A_TK_MINIMIZED:
1936 lua_pushboolean(L, c->isminimized);
1937 break;
1938 case A_TK_FULLSCREEN:
1939 lua_pushboolean(L, c->isfullscreen);
1940 break;
1941 case A_TK_GROUP_ID:
1942 if(c->group_win)
1943 lua_pushnumber(L, c->group_win);
1944 else
1945 return 0;
1946 break;
1947 case A_TK_MAXIMIZED_HORIZONTAL:
1948 lua_pushboolean(L, c->ismaxhoriz);
1949 break;
1950 case A_TK_MAXIMIZED_VERTICAL:
1951 lua_pushboolean(L, c->ismaxvert);
1952 break;
1953 case A_TK_ICON:
1954 image_push(L, c->icon);
1955 break;
1956 case A_TK_OPACITY:
1957 if((d = window_opacity_get(c->win)) >= 0)
1958 lua_pushnumber(L, d);
1959 else
1960 return 0;
1961 break;
1962 case A_TK_ONTOP:
1963 lua_pushboolean(L, c->isontop);
1964 break;
1965 case A_TK_ABOVE:
1966 lua_pushboolean(L, c->isabove);
1967 break;
1968 case A_TK_BELOW:
1969 lua_pushboolean(L, c->isbelow);
1970 break;
1971 case A_TK_STICKY:
1972 lua_pushboolean(L, c->issticky);
1973 break;
1974 case A_TK_SIZE_HINTS_HONOR:
1975 lua_pushboolean(L, c->size_hints_honor);
1976 break;
1977 case A_TK_BORDER_WIDTH:
1978 lua_pushnumber(L, c->border);
1979 break;
1980 case A_TK_BORDER_COLOR:
1981 luaA_pushxcolor(L, &c->border_color);
1982 break;
1983 case A_TK_TITLEBAR:
1984 return wibox_push(L, c->titlebar);
1985 case A_TK_URGENT:
1986 lua_pushboolean(L, c->isurgent);
1987 break;
1988 case A_TK_SIZE_HINTS:
1990 const char *u_or_p = NULL;
1992 lua_createtable(L, 0, 1);
1994 if(c->size_hints.flags & XCB_SIZE_HINT_US_POSITION)
1995 u_or_p = "user_position";
1996 else if(c->size_hints.flags & XCB_SIZE_HINT_P_POSITION)
1997 u_or_p = "program_position";
1999 if(u_or_p)
2001 lua_createtable(L, 0, 2);
2002 lua_pushnumber(L, c->size_hints.x);
2003 lua_setfield(L, -2, "x");
2004 lua_pushnumber(L, c->size_hints.y);
2005 lua_setfield(L, -2, "y");
2006 lua_setfield(L, -2, u_or_p);
2007 u_or_p = NULL;
2010 if(c->size_hints.flags & XCB_SIZE_HINT_US_SIZE)
2011 u_or_p = "user_size";
2012 else if(c->size_hints.flags & XCB_SIZE_HINT_P_SIZE)
2013 u_or_p = "program_size";
2015 if(u_or_p)
2017 lua_createtable(L, 0, 2);
2018 lua_pushnumber(L, c->size_hints.width);
2019 lua_setfield(L, -2, "width");
2020 lua_pushnumber(L, c->size_hints.height);
2021 lua_setfield(L, -2, "height");
2022 lua_setfield(L, -2, u_or_p);
2025 if(c->size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE)
2027 lua_pushnumber(L, c->size_hints.min_width);
2028 lua_setfield(L, -2, "min_width");
2029 lua_pushnumber(L, c->size_hints.min_height);
2030 lua_setfield(L, -2, "min_height");
2033 if(c->size_hints.flags & XCB_SIZE_HINT_P_MAX_SIZE)
2035 lua_pushnumber(L, c->size_hints.max_width);
2036 lua_setfield(L, -2, "max_width");
2037 lua_pushnumber(L, c->size_hints.max_height);
2038 lua_setfield(L, -2, "max_height");
2041 if(c->size_hints.flags & XCB_SIZE_HINT_P_RESIZE_INC)
2043 lua_pushnumber(L, c->size_hints.width_inc);
2044 lua_setfield(L, -2, "width_inc");
2045 lua_pushnumber(L, c->size_hints.height_inc);
2046 lua_setfield(L, -2, "height_inc");
2049 if(c->size_hints.flags & XCB_SIZE_HINT_P_ASPECT)
2051 lua_pushnumber(L, c->size_hints.min_aspect_num);
2052 lua_setfield(L, -2, "min_aspect_num");
2053 lua_pushnumber(L, c->size_hints.min_aspect_den);
2054 lua_setfield(L, -2, "min_aspect_den");
2055 lua_pushnumber(L, c->size_hints.max_aspect_num);
2056 lua_setfield(L, -2, "max_aspect_num");
2057 lua_pushnumber(L, c->size_hints.max_aspect_den);
2058 lua_setfield(L, -2, "max_aspect_den");
2061 if(c->size_hints.flags & XCB_SIZE_HINT_BASE_SIZE)
2063 lua_pushnumber(L, c->size_hints.base_width);
2064 lua_setfield(L, -2, "base_width");
2065 lua_pushnumber(L, c->size_hints.base_height);
2066 lua_setfield(L, -2, "base_height");
2069 if(c->size_hints.flags & XCB_SIZE_HINT_P_WIN_GRAVITY)
2071 switch(c->size_hints.win_gravity)
2073 default:
2074 lua_pushliteral(L, "north_west");
2075 break;
2076 case XCB_GRAVITY_NORTH:
2077 lua_pushliteral(L, "north");
2078 break;
2079 case XCB_GRAVITY_NORTH_EAST:
2080 lua_pushliteral(L, "north_east");
2081 break;
2082 case XCB_GRAVITY_WEST:
2083 lua_pushliteral(L, "west");
2084 break;
2085 case XCB_GRAVITY_CENTER:
2086 lua_pushliteral(L, "center");
2087 break;
2088 case XCB_GRAVITY_EAST:
2089 lua_pushliteral(L, "east");
2090 break;
2091 case XCB_GRAVITY_SOUTH_WEST:
2092 lua_pushliteral(L, "south_west");
2093 break;
2094 case XCB_GRAVITY_SOUTH:
2095 lua_pushliteral(L, "south");
2096 break;
2097 case XCB_GRAVITY_SOUTH_EAST:
2098 lua_pushliteral(L, "south_east");
2099 break;
2100 case XCB_GRAVITY_STATIC:
2101 lua_pushliteral(L, "static");
2102 break;
2104 lua_setfield(L, -2, "win_gravity");
2107 break;
2108 default:
2109 return 0;
2112 return 1;
2115 /** Get or set mouse buttons bindings for a client.
2116 * \param L The Lua VM state.
2117 * \return The number of element pushed on stack.
2118 * \luastack
2119 * \lvalue A client.
2120 * \lparam An array of mouse button bindings objects, or nothing.
2121 * \return The array of mouse button bindings objects of this client.
2123 static int
2124 luaA_client_buttons(lua_State *L)
2126 client_t *client = luaA_client_checkudata(L, 1);
2127 button_array_t *buttons = &client->buttons;
2129 if(lua_gettop(L) == 2)
2130 luaA_button_array_set(L, 2, buttons);
2132 window_buttons_grab(client->win, &client->buttons);
2134 return luaA_button_array_get(L, buttons);
2137 /** Get or set keys bindings for a client.
2138 * \param L The Lua VM state.
2139 * \return The number of element pushed on stack.
2140 * \luastack
2141 * \lvalue A client.
2142 * \lparam An array of key bindings objects, or nothing.
2143 * \return The array of key bindings objects of this client.
2145 static int
2146 luaA_client_keys(lua_State *L)
2148 client_t *c = luaA_client_checkudata(L, 1);
2149 key_array_t *keys = &c->keys;
2151 if(lua_gettop(L) == 2)
2153 luaA_key_array_set(L, 2, keys);
2154 xcb_ungrab_key(globalconf.connection, XCB_GRAB_ANY, c->win, XCB_BUTTON_MASK_ANY);
2155 window_grabkeys(c->win, keys);
2158 return luaA_key_array_get(L, keys);
2161 /* Client module.
2162 * \param L The Lua VM state.
2163 * \return The number of pushed elements.
2165 static int
2166 luaA_client_module_index(lua_State *L)
2168 size_t len;
2169 const char *buf = luaL_checklstring(L, 2, &len);
2171 switch(a_tokenize(buf, len))
2173 case A_TK_FOCUS:
2174 return client_push(globalconf.L, globalconf.screen_focus->client_focus);
2175 break;
2176 default:
2177 return 0;
2181 /* Client module new index.
2182 * \param L The Lua VM state.
2183 * \return The number of pushed elements.
2185 static int
2186 luaA_client_module_newindex(lua_State *L)
2188 size_t len;
2189 const char *buf = luaL_checklstring(L, 2, &len);
2190 client_t *c;
2192 switch(a_tokenize(buf, len))
2194 case A_TK_FOCUS:
2195 c = luaA_client_checkudata(L, 3);
2196 client_focus(c);
2197 break;
2198 default:
2199 break;
2202 return 0;
2205 const struct luaL_reg awesome_client_methods[] =
2207 { "get", luaA_client_get },
2208 { "__index", luaA_client_module_index },
2209 { "__newindex", luaA_client_module_newindex },
2210 { NULL, NULL }
2212 const struct luaL_reg awesome_client_meta[] =
2214 { "isvisible", luaA_client_isvisible },
2215 { "geometry", luaA_client_geometry },
2216 { "struts", luaA_client_struts },
2217 { "buttons", luaA_client_buttons },
2218 { "keys", luaA_client_keys },
2219 { "tags", luaA_client_tags },
2220 { "kill", luaA_client_kill },
2221 { "swap", luaA_client_swap },
2222 { "raise", luaA_client_raise },
2223 { "lower", luaA_client_lower },
2224 { "redraw", luaA_client_redraw },
2225 { "unmanage", luaA_client_unmanage },
2226 { "__index", luaA_client_index },
2227 { "__newindex", luaA_client_newindex },
2228 { "__gc", luaA_client_gc },
2229 { "__tostring", luaA_client_tostring },
2230 { NULL, NULL }
2233 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80