awful.client: store floating geometry of clients in awful
[awesome.git] / client.c
blobbb4350a180b4d45e3a48417b725ba581cca87b55
1 /*
2 * client.c - client management
4 * Copyright © 2007-2008 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/xtest.h>
23 #include <xcb/xcb_atom.h>
24 #include <xcb/xcb_image.h>
26 #include "cnode.h"
27 #include "tag.h"
28 #include "window.h"
29 #include "ewmh.h"
30 #include "screen.h"
31 #include "titlebar.h"
32 #include "systray.h"
33 #include "property.h"
34 #include "wibox.h"
35 #include "common/atoms.h"
37 extern awesome_t globalconf;
39 DO_LUA_NEW(extern, client_t, client, "client", client_ref)
40 DO_LUA_EQ(client_t, client, "client")
41 DO_LUA_GC(client_t, client, "client", client_unref)
43 /** Check if client supports protocol a protocole in WM_PROTOCOL.
44 * \param win The window.
45 * \return True if client has the atom in protocol, false otherwise.
47 static bool
48 window_hasproto(xcb_window_t win, xcb_atom_t atom)
50 uint32_t i;
51 xcb_get_wm_protocols_reply_t protocols;
52 bool ret = false;
54 if(xcb_get_wm_protocols_reply(globalconf.connection,
55 xcb_get_wm_protocols_unchecked(globalconf.connection,
56 win, WM_PROTOCOLS),
57 &protocols, NULL))
59 for(i = 0; !ret && i < protocols.atoms_len; i++)
60 if(protocols.atoms[i] == atom)
61 ret = true;
62 xcb_get_wm_protocols_reply_wipe(&protocols);
64 return ret;
67 /** Send WM_TAKE_FOCUS client message to window
68 * \param win destination window
70 static void
71 window_takefocus(xcb_window_t win)
73 xcb_client_message_event_t ev;
75 /* Initialize all of event's fields first */
76 p_clear(&ev, 1);
78 ev.response_type = XCB_CLIENT_MESSAGE;
79 ev.window = win;
80 ev.format = 32;
81 ev.data.data32[1] = XCB_CURRENT_TIME;
82 ev.type = WM_PROTOCOLS;
83 ev.data.data32[0] = WM_TAKE_FOCUS;
85 xcb_send_event(globalconf.connection, false, win,
86 XCB_EVENT_MASK_NO_EVENT, (char *) &ev);
89 /** Change the clients urgency flag.
90 * \param c The client
91 * \param urgent The new flag state
93 void
94 client_seturgent(client_t *c, bool urgent)
96 if(c->isurgent != urgent)
98 xcb_get_property_cookie_t hints =
99 xcb_get_wm_hints_unchecked(globalconf.connection, c->win);
101 c->isurgent = urgent;
102 ewmh_client_update_hints(c);
104 /* update ICCCM hints */
105 xcb_wm_hints_t wmh;
106 xcb_get_wm_hints_reply(globalconf.connection, hints, &wmh, NULL);
108 if(urgent)
109 wmh.flags |= XCB_WM_HINT_X_URGENCY;
110 else
111 wmh.flags &= ~XCB_WM_HINT_X_URGENCY;
113 xcb_set_wm_hints(globalconf.connection, c->win, &wmh);
115 hooks_property(c, "urgent");
119 /** Returns true if a client is tagged
120 * with one of the tags of the specified screen.
121 * \param c The client to check.
122 * \param screen Virtual screen number.
123 * \return true if the client is visible, false otherwise.
125 bool
126 client_maybevisible(client_t *c, int screen)
128 if(c->screen == screen)
130 if(c->issticky || c->type == WINDOW_TYPE_DESKTOP)
131 return true;
133 tag_array_t *tags = &globalconf.screens[screen].tags;
135 for(int i = 0; i < tags->len; i++)
136 if(tags->tab[i]->selected && is_client_tagged(c, tags->tab[i]))
137 return true;
139 return false;
142 /** Return the content of a client as an image.
143 * That's just take a screenshot.
144 * \param c The client.
145 * \return An image.
147 static image_t *
148 client_getcontent(client_t *c)
150 xcb_image_t *ximage = xcb_image_get(globalconf.connection,
151 c->win,
152 0, 0,
153 c->geometries.internal.width,
154 c->geometries.internal.height,
155 ~0, XCB_IMAGE_FORMAT_Z_PIXMAP);
157 if(!ximage || ximage->bpp < 24)
158 return NULL;
160 uint32_t *data = p_alloca(uint32_t, ximage->width * ximage->height);
162 for(int y = 0; y < ximage->height; y++)
163 for(int x = 0; x < ximage->width; x++)
165 data[y * ximage->width + x] = xcb_image_get_pixel(ximage, x, y);
166 data[y * ximage->width + x] |= 0xff000000; /* set alpha to 0xff */
169 image_t *image = image_new_from_argb32(ximage->width, ximage->height, data);
171 xcb_image_destroy(ximage);
173 return image;
176 /** Get a client by its window.
177 * \param w The client window to find.
178 * \return A client pointer if found, NULL otherwise.
180 client_t *
181 client_getbywin(xcb_window_t w)
183 client_t *c;
184 for(c = globalconf.clients; c && c->win != w; c = c->next);
185 return c;
188 /** Call unfocus hook.
189 * \param c Client being unfocused
191 static void
192 client_unfocus_hook(client_t *c)
194 /* Call hook */
195 if(globalconf.hooks.unfocus != LUA_REFNIL)
197 luaA_client_userdata_new(globalconf.L, c);
198 luaA_dofunction(globalconf.L, globalconf.hooks.unfocus, 1, 0);
202 /** Unfocus a client.
203 * \param c The client.
205 static void
206 client_unfocus(client_t *c)
208 xcb_window_t root_win = xutil_screen_get(globalconf.connection, c->phys_screen)->root;
209 globalconf.screens[c->phys_screen].client_focus = NULL;
211 /* Set focus on root window, so no events leak to the current window. */
212 xcb_set_input_focus(globalconf.connection, XCB_INPUT_FOCUS_POINTER_ROOT,
213 root_win, XCB_CURRENT_TIME);
215 client_unfocus_hook(c);
217 ewmh_update_net_active_window(c->phys_screen);
220 /** Ban client and move it out of the viewport.
221 * \param c The client.
223 void
224 client_ban(client_t *c)
226 if(!c->isbanned)
228 /* Move all clients out of the physical viewport into negative coordinate space. */
229 /* They will all be put on top of each other. */
230 uint32_t request[2] = { - (c->geometries.internal.width + 2 * c->border),
231 - (c->geometries.internal.height + 2 * c->border) };
233 xcb_configure_window(globalconf.connection, c->win,
234 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
235 request);
237 titlebar_ban(c->titlebar);
239 c->isbanned = true;
241 /* All the wiboxes (may) need to be repositioned. */
242 if(client_hasstrut(c))
243 wibox_update_positions();
246 /* Wait until the last moment to take away the focus from the window. */
247 if (globalconf.screens[c->phys_screen].client_focus == c)
248 client_unfocus(c);
251 /** Call focus hook.
252 * \param c Client being focused.
254 static void
255 client_focus_hook(client_t *c)
257 /* execute hook */
258 if(globalconf.hooks.focus != LUA_REFNIL)
260 luaA_client_userdata_new(globalconf.L, c);
261 luaA_dofunction(globalconf.L, globalconf.hooks.focus, 1, 0);
265 /** Give focus to client, or to first client if client is NULL.
266 * \param c The client or NULL.
267 * \param sendmessage true, if we should send message.
269 void
270 client_focus(client_t *c, bool sendmessage)
272 /* Handle c == NULL case */
273 if(!c)
275 if(sendmessage)
276 c = globalconf.clients;
278 if(!c)
279 return;
282 if(!client_maybevisible(c, c->screen))
283 return;
285 /* Does client window support WM_TAKE_FOCUS protocol ? */
286 bool takefocus = window_hasproto(c->win, WM_TAKE_FOCUS);
288 /* Disallow setting focus on client with No Input Model */
289 if(sendmessage && !takefocus && c->nofocus)
290 return;
292 /* Save current focused client */
293 client_t *focused_before = globalconf.screen_focus->client_focus;
295 if(c == focused_before)
296 return;
298 /* stop hiding c */
299 c->ishidden = false;
300 client_setminimized(c, false);
302 /* unban the client before focusing or it will fail */
303 client_unban(c);
305 globalconf.screen_focus = &globalconf.screens[c->phys_screen];
306 globalconf.screen_focus->client_focus = c;
308 if(sendmessage)
310 if(!c->nofocus)
311 /* Input models: Passive, Locally Active */
312 xcb_set_input_focus(globalconf.connection, XCB_INPUT_FOCUS_POINTER_ROOT,
313 c->win, XCB_CURRENT_TIME);
315 if(takefocus)
316 /* Input models: No Input, Globally Active */
317 window_takefocus(c->win);
320 /* Some layouts use focused client differently, so call them back.
321 * And anyway, we have maybe unhidden */
322 client_need_arrange(c);
324 /* unfocus current selected client
325 * We don't really need to unfocus here,
326 * because client already received FocusOut event.
327 * What we need to do is call unfocus hook, to
328 * inform lua script, about this event.
330 if(focused_before)
331 client_unfocus_hook(focused_before);
333 client_focus_hook(c);
335 /* according to EWMH, we have to remove the urgent state from a client */
336 client_seturgent(c, false);
338 ewmh_update_net_active_window(c->phys_screen);
342 /** Stack a window below.
343 * \param c The client.
344 * \param previous The previous window on the stack.
345 * \param return The next-previous!
347 static xcb_window_t
348 client_stack_above(client_t *c, xcb_window_t previous)
350 uint32_t config_win_vals[2];
352 config_win_vals[0] = previous;
353 config_win_vals[1] = XCB_STACK_MODE_ABOVE;
355 xcb_configure_window(globalconf.connection, c->win,
356 XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE,
357 config_win_vals);
359 config_win_vals[0] = c->win;
361 if(c->titlebar)
363 xcb_configure_window(globalconf.connection,
364 c->titlebar->sw.window,
365 XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE,
366 config_win_vals);
367 previous = c->titlebar->sw.window;
369 else
370 previous = c->win;
372 /* stack transient window on top of their parents */
373 for(client_node_t *node = *client_node_list_last(&globalconf.stack);
374 node; node = node->prev)
375 if(node->client->transient_for == c)
376 previous = client_stack_above(node->client, previous);
378 return previous;
381 /** Stacking layout layers */
382 typedef enum
384 /** This one is a special layer */
385 LAYER_IGNORE,
386 LAYER_DESKTOP,
387 LAYER_BELOW,
388 LAYER_NORMAL,
389 LAYER_ABOVE,
390 LAYER_FULLSCREEN,
391 LAYER_ONTOP,
392 LAYER_OUTOFSPACE
393 } layer_t;
395 /** Get the real layer of a client according to its attribute (fullscreen, …)
396 * \param c The client.
397 * \return The real layer.
399 static layer_t
400 client_layer_translator(client_t *c)
402 /* first deal with user set attributes */
403 if(c->isontop)
404 return LAYER_ONTOP;
405 else if(c->isfullscreen)
406 return LAYER_FULLSCREEN;
407 else if(c->isabove)
408 return LAYER_ABOVE;
409 else if(c->isbelow)
410 return LAYER_BELOW;
412 /* check for transient attr */
413 if(c->transient_for)
414 return LAYER_IGNORE;
416 /* then deal with windows type */
417 switch(c->type)
419 case WINDOW_TYPE_DOCK:
420 return LAYER_ABOVE;
421 case WINDOW_TYPE_DESKTOP:
422 return LAYER_DESKTOP;
423 case WINDOW_TYPE_DIALOG:
424 case WINDOW_TYPE_MENU:
425 case WINDOW_TYPE_TOOLBAR:
426 case WINDOW_TYPE_UTILITY:
427 return LAYER_ABOVE;
428 default:
429 break;
432 return LAYER_NORMAL;
435 /** Restack clients.
436 * \todo It might be worth stopping to restack everyone and only stack `c'
437 * relatively to the first matching in the list.
439 void
440 client_stack()
442 uint32_t config_win_vals[2];
443 client_node_t *node, *last = *client_node_list_last(&globalconf.stack);
444 layer_t layer;
445 int screen;
447 config_win_vals[0] = XCB_NONE;
448 config_win_vals[1] = XCB_STACK_MODE_ABOVE;
450 /* stack desktop windows */
451 for(layer = LAYER_DESKTOP; layer < LAYER_BELOW; layer++)
452 for(node = last; node; node = node->prev)
453 if(client_layer_translator(node->client) == layer)
454 config_win_vals[0] = client_stack_above(node->client,
455 config_win_vals[0]);
457 /* first stack not ontop wibox window */
458 for(screen = 0; screen < globalconf.nscreen; screen++)
459 for(int i = 0; i < globalconf.screens[screen].wiboxes.len; i++)
461 wibox_t *sb = globalconf.screens[screen].wiboxes.tab[i];
462 if(!sb->ontop)
464 xcb_configure_window(globalconf.connection,
465 sb->sw.window,
466 XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE,
467 config_win_vals);
468 config_win_vals[0] = sb->sw.window;
472 /* then stack clients */
473 for(layer = LAYER_BELOW; layer < LAYER_OUTOFSPACE; layer++)
474 for(node = last; node; node = node->prev)
475 if(client_layer_translator(node->client) == layer)
476 config_win_vals[0] = client_stack_above(node->client,
477 config_win_vals[0]);
479 /* then stack ontop wibox window */
480 for(screen = 0; screen < globalconf.nscreen; screen++)
481 for(int i = 0; i < globalconf.screens[screen].wiboxes.len; i++)
483 wibox_t *sb = globalconf.screens[screen].wiboxes.tab[i];
484 if(sb->ontop)
486 xcb_configure_window(globalconf.connection,
487 sb->sw.window,
488 XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE,
489 config_win_vals);
490 config_win_vals[0] = sb->sw.window;
495 /** Manage a new client.
496 * \param w The window.
497 * \param wgeom Window geometry.
498 * \param phys_screen Physical screen number.
499 * \param startup True if we are managing at startup time.
501 void
502 client_manage(xcb_window_t w, xcb_get_geometry_reply_t *wgeom, int phys_screen, bool startup)
504 xcb_get_property_cookie_t ewmh_icon_cookie;
505 client_t *c, *tc = NULL;
506 image_t *icon;
507 int screen;
508 const uint32_t select_input_val[] = { CLIENT_SELECT_INPUT_EVENT_MASK };
510 if(systray_iskdedockapp(w))
512 systray_request_handle(w, phys_screen, NULL);
513 return;
516 /* Send request to get NET_WM_ICON property as soon as possible... */
517 ewmh_icon_cookie = ewmh_window_icon_get_unchecked(w);
518 xcb_change_window_attributes(globalconf.connection, w, XCB_CW_EVENT_MASK, select_input_val);
519 c = p_new(client_t, 1);
521 screen = c->screen = screen_getbycoord(phys_screen, wgeom->x, wgeom->y);
523 c->phys_screen = phys_screen;
525 /* Initial values */
526 c->win = w;
527 c->geometry.x = wgeom->x;
528 c->geometry.y = wgeom->y;
529 /* Border will be added later. */
530 c->geometry.width = wgeom->width;
531 c->geometry.height = wgeom->height;
532 /* Also set internal geometry (client_ban() needs it). */
533 c->geometries.internal.x = wgeom->x;
534 c->geometries.internal.y = wgeom->y;
535 c->geometries.internal.width = wgeom->width;
536 c->geometries.internal.height = wgeom->height;
537 client_setborder(c, wgeom->border_width);
538 if((icon = ewmh_window_icon_get_reply(ewmh_icon_cookie)))
539 c->icon = image_ref(&icon);
541 /* we honor size hints by default */
542 c->size_hints_honor = true;
544 /* update hints */
545 property_update_wm_normal_hints(c, NULL);
546 property_update_wm_hints(c, NULL);
547 property_update_wm_transient_for(c, NULL);
548 property_update_wm_client_leader(c, NULL);
550 /* Recursively find the parent. */
551 for(tc = c; tc->transient_for; tc = tc->transient_for);
552 if(tc != c && tc->phys_screen == c->phys_screen)
553 screen = tc->screen;
555 /* Then check clients hints */
556 ewmh_client_check_hints(c);
558 /* move client to screen, but do not tag it */
559 screen_client_moveto(c, screen, false, true);
561 /* Push client in client list */
562 client_list_push(&globalconf.clients, client_ref(&c));
564 /* Push client in stack */
565 client_raise(c);
567 /* update window title */
568 property_update_wm_name(c);
569 property_update_wm_icon_name(c);
571 /* update strut */
572 ewmh_process_client_strut(c, NULL);
574 ewmh_update_net_client_list(c->phys_screen);
576 /* Always stay in NORMAL_STATE. Even though iconified seems more
577 * appropriate sometimes. The only possible loss is that clients not using
578 * visibility events may continue to proces data (when banned).
579 * Without any exposes or other events the cost should be fairly limited though.
581 * Some clients may expect the window to be unmapped when STATE_ICONIFIED.
582 * Two conflicting parts of the ICCCM v2.0 (section 4.1.4):
584 * "Normal -> Iconic - The client should send a ClientMessage event as described later in this section."
585 * (note no explicit mention of unmapping, while Normal->Widthdrawn does mention that)
587 * "Once a client's window has left the Withdrawn state, the window will be mapped
588 * if it is in the Normal state and the window will be unmapped if it is in the Iconic state."
590 * At this stage it's just safer to keep it in normal state and avoid confusion.
592 window_state_set(c->win, XCB_WM_STATE_NORMAL);
594 /* Move window outside the viewport before mapping it. */
595 client_ban(c);
596 xcb_map_window(globalconf.connection, c->win);
598 /* Call hook to notify list change */
599 if(globalconf.hooks.clients != LUA_REFNIL)
600 luaA_dofunction(globalconf.L, globalconf.hooks.clients, 0, 0);
602 /* call hook */
603 if(globalconf.hooks.manage != LUA_REFNIL)
605 luaA_client_userdata_new(globalconf.L, c);
606 lua_pushboolean(globalconf.L, startup);
607 luaA_dofunction(globalconf.L, globalconf.hooks.manage, 2, 0);
611 /** Compute client geometry with respect to its geometry hints.
612 * \param c The client.
613 * \param geometry The geometry that the client might receive.
614 * \return The geometry the client must take respecting its hints.
616 area_t
617 client_geometry_hints(client_t *c, area_t geometry)
619 int32_t basew, baseh, minw, minh;
621 /* base size is substituted with min size if not specified */
622 if(c->size_hints.flags & XCB_SIZE_HINT_P_SIZE)
624 basew = c->size_hints.base_width;
625 baseh = c->size_hints.base_height;
627 else if(c->size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE)
629 basew = c->size_hints.min_width;
630 baseh = c->size_hints.min_height;
632 else
633 basew = baseh = 0;
635 /* min size is substituted with base size if not specified */
636 if(c->size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE)
638 minw = c->size_hints.min_width;
639 minh = c->size_hints.min_height;
641 else if(c->size_hints.flags & XCB_SIZE_HINT_P_SIZE)
643 minw = c->size_hints.base_width;
644 minh = c->size_hints.base_height;
646 else
647 minw = minh = 0;
649 if(c->size_hints.flags & XCB_SIZE_HINT_P_ASPECT
650 && c->size_hints.min_aspect_num > 0
651 && c->size_hints.min_aspect_den > 0
652 && geometry.height - baseh > 0
653 && geometry.width - basew > 0)
655 double dx = (double) (geometry.width - basew);
656 double dy = (double) (geometry.height - baseh);
657 double min = (double) c->size_hints.min_aspect_num / (double) c->size_hints.min_aspect_den;
658 double max = (double) c->size_hints.max_aspect_num / (double) c->size_hints.min_aspect_den;
659 double ratio = dx / dy;
660 if(max > 0 && min > 0 && ratio > 0)
662 if(ratio < min)
664 dy = (dx * min + dy) / (min * min + 1);
665 dx = dy * min;
666 geometry.width = (int) dx + basew;
667 geometry.height = (int) dy + baseh;
669 else if(ratio > max)
671 dy = (dx * min + dy) / (max * max + 1);
672 dx = dy * min;
673 geometry.width = (int) dx + basew;
674 geometry.height = (int) dy + baseh;
679 if(minw)
680 geometry.width = MAX(geometry.width, minw);
681 if(minh)
682 geometry.height = MAX(geometry.height, minh);
684 if(c->size_hints.flags & XCB_SIZE_HINT_P_MAX_SIZE)
686 if(c->size_hints.max_width)
687 geometry.width = MIN(geometry.width, c->size_hints.max_width);
688 if(c->size_hints.max_height)
689 geometry.height = MIN(geometry.height, c->size_hints.max_height);
692 if(c->size_hints.flags & (XCB_SIZE_HINT_P_RESIZE_INC | XCB_SIZE_HINT_BASE_SIZE)
693 && c->size_hints.width_inc && c->size_hints.height_inc)
695 uint16_t t1 = geometry.width, t2 = geometry.height;
696 unsigned_subtract(t1, basew);
697 unsigned_subtract(t2, baseh);
698 geometry.width -= t1 % c->size_hints.width_inc;
699 geometry.height -= t2 % c->size_hints.height_inc;
702 return geometry;
705 /** Resize client window.
706 * The sizse given as parameters are with titlebar and borders!
707 * \param c Client to resize.
708 * \param geometry New window geometry.
709 * \param hints Use size hints.
710 * \return true if an actual resize occurred.
712 bool
713 client_resize(client_t *c, area_t geometry, bool hints)
715 int new_screen;
716 area_t geometry_internal;
717 area_t area;
719 /* offscreen appearance fixes */
720 area = display_area_get(c->phys_screen, NULL,
721 &globalconf.screens[c->screen].padding);
723 if(geometry.x > area.width)
724 geometry.x = area.width - geometry.width;
725 if(geometry.y > area.height)
726 geometry.y = area.height - geometry.height;
727 if(geometry.x + geometry.width < 0)
728 geometry.x = 0;
729 if(geometry.y + geometry.height < 0)
730 geometry.y = 0;
732 /* Real client geometry, please keep it contained to C code at the very least. */
733 geometry_internal = titlebar_geometry_remove(c->titlebar, c->border, geometry);
735 if(hints)
736 geometry_internal = client_geometry_hints(c, geometry_internal);
738 if(geometry_internal.width == 0 || geometry_internal.height == 0)
739 return false;
741 /* Also let client hints propegate to the "official" geometry. */
742 geometry = titlebar_geometry_add(c->titlebar, c->border, geometry_internal);
744 if(c->geometries.internal.x != geometry_internal.x
745 || c->geometries.internal.y != geometry_internal.y
746 || c->geometries.internal.width != geometry_internal.width
747 || c->geometries.internal.height != geometry_internal.height)
749 new_screen = screen_getbycoord(c->screen, geometry_internal.x, geometry_internal.y);
751 /* Values to configure a window is an array where values are
752 * stored according to 'value_mask' */
753 uint32_t values[4];
755 c->geometries.internal.x = values[0] = geometry_internal.x;
756 c->geometries.internal.y = values[1] = geometry_internal.y;
757 c->geometries.internal.width = values[2] = geometry_internal.width;
758 c->geometries.internal.height = values[3] = geometry_internal.height;
760 /* Also store geometry including border and titlebar. */
761 c->geometry = geometry;
763 titlebar_update_geometry(c);
765 /* The idea is to give a client a resize even when banned. */
766 /* We just have to move the (x,y) to keep it out of the viewport. */
767 /* This at least doesn't break expectations about events. */
768 if (c->isbanned)
770 geometry_internal.x = values[0] = - (geometry_internal.width + 2 * c->border);
771 geometry_internal.y = values[1] = - (geometry_internal.height + 2 * c->border);
774 xcb_configure_window(globalconf.connection, c->win,
775 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y
776 | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
777 values);
778 window_configure(c->win, geometry_internal, c->border);
780 screen_client_moveto(c, new_screen, true, false);
782 /* execute hook */
783 hooks_property(c, "geometry");
785 return true;
788 return false;
791 /** Set a client minimized, or not.
792 * \param c The client.
793 * \param s Set or not the client minimized.
795 void
796 client_setminimized(client_t *c, bool s)
798 if(c->isminimized != s)
800 client_need_arrange(c);
801 c->isminimized = s;
802 client_need_arrange(c);
803 ewmh_client_update_hints(c);
804 /* execute hook */
805 hooks_property(c, "minimized");
809 /** Set a client sticky, or not.
810 * \param c The client.
811 * \param s Set or not the client sticky.
813 void
814 client_setsticky(client_t *c, bool s)
816 if(c->issticky != s)
818 client_need_arrange(c);
819 c->issticky = s;
820 client_need_arrange(c);
821 ewmh_client_update_hints(c);
822 hooks_property(c, "sticky");
826 /** Set a client fullscreen, or not.
827 * \param c The client.
828 * \param s Set or not the client fullscreen.
830 void
831 client_setfullscreen(client_t *c, bool s)
833 if(c->isfullscreen != s)
835 area_t geometry;
837 /* become fullscreen! */
838 if((c->isfullscreen = s))
840 /* remove any max state */
841 client_setmaxhoriz(c, false);
842 client_setmaxvert(c, false);
843 /* You can only be part of one of the special layers. */
844 client_setbelow(c, false);
845 client_setabove(c, false);
846 client_setontop(c, false);
848 geometry = screen_area_get(c->screen, NULL, NULL, false);
849 c->geometries.fullscreen = c->geometry;
850 c->border_fs = c->border;
851 client_setborder(c, 0);
853 else
855 geometry = c->geometries.fullscreen;
856 client_setborder(c, c->border_fs);
858 client_resize(c, geometry, false);
859 client_need_arrange(c);
860 client_stack();
861 ewmh_client_update_hints(c);
862 hooks_property(c, "fullscreen");
866 /** Set a client horizontally maximized.
867 * \param c The client.
868 * \param s The maximized status.
870 void
871 client_setmaxhoriz(client_t *c, bool s)
873 if(c->ismaxhoriz != s)
875 area_t geometry;
877 if((c->ismaxhoriz = s))
879 /* remove fullscreen mode */
880 client_setfullscreen(c, false);
882 geometry = screen_area_get(c->screen,
883 &globalconf.screens[c->screen].wiboxes,
884 &globalconf.screens[c->screen].padding,
885 true);
886 geometry.y = c->geometry.y;
887 geometry.height = c->geometry.height;
888 c->geometries.max.x = c->geometry.x;
889 c->geometries.max.width = c->geometry.width;
891 else
893 geometry = c->geometry;
894 geometry.x = c->geometries.max.x;
895 geometry.width = c->geometries.max.width;
898 client_resize(c, geometry, c->size_hints_honor);
899 client_need_arrange(c);
900 client_stack();
901 ewmh_client_update_hints(c);
902 hooks_property(c, "maximized_horizontal");
906 /** Set a client vertically maximized.
907 * \param c The client.
908 * \param s The maximized status.
910 void
911 client_setmaxvert(client_t *c, bool s)
913 if(c->ismaxvert != s)
915 area_t geometry;
917 if((c->ismaxvert = s))
919 /* remove fullscreen mode */
920 client_setfullscreen(c, false);
922 geometry = screen_area_get(c->screen,
923 &globalconf.screens[c->screen].wiboxes,
924 &globalconf.screens[c->screen].padding,
925 true);
926 geometry.x = c->geometry.x;
927 geometry.width = c->geometry.width;
928 c->geometries.max.y = c->geometry.y;
929 c->geometries.max.height = c->geometry.height;
931 else
933 geometry = c->geometry;
934 geometry.y = c->geometries.max.y;
935 geometry.height = c->geometries.max.height;
938 client_resize(c, geometry, c->size_hints_honor);
939 client_need_arrange(c);
940 client_stack();
941 ewmh_client_update_hints(c);
942 hooks_property(c, "maximized_vertical");
946 /** Set a client above, or not.
947 * \param c The client.
948 * \param s Set or not the client above.
950 void
951 client_setabove(client_t *c, bool s)
953 if(c->isabove != s)
955 /* You can only be part of one of the special layers. */
956 if(s)
958 client_setbelow(c, false);
959 client_setontop(c, false);
960 client_setfullscreen(c, false);
962 c->isabove = s;
963 client_stack();
964 ewmh_client_update_hints(c);
965 /* execute hook */
966 hooks_property(c, "above");
970 /** Set a client below, or not.
971 * \param c The client.
972 * \param s Set or not the client below.
974 void
975 client_setbelow(client_t *c, bool s)
977 if(c->isbelow != s)
979 /* You can only be part of one of the special layers. */
980 if(s)
982 client_setabove(c, false);
983 client_setontop(c, false);
984 client_setfullscreen(c, false);
986 c->isbelow = s;
987 client_stack();
988 ewmh_client_update_hints(c);
989 /* execute hook */
990 hooks_property(c, "below");
994 /** Set a client modal, or not.
995 * \param c The client.
996 * \param s Set or not the client moda.
998 void
999 client_setmodal(client_t *c, bool s)
1001 if(c->ismodal != s)
1003 c->ismodal = s;
1004 client_stack();
1005 ewmh_client_update_hints(c);
1006 /* execute hook */
1007 hooks_property(c, "modal");
1011 /** Set a client ontop, or not.
1012 * \param c The client.
1013 * \param s Set or not the client moda.
1015 void
1016 client_setontop(client_t *c, bool s)
1018 if(c->isontop != s)
1020 /* You can only be part of one of the special layers. */
1021 if(s)
1023 client_setabove(c, false);
1024 client_setbelow(c, false);
1025 client_setfullscreen(c, false);
1027 c->isontop = s;
1028 client_stack();
1029 /* execute hook */
1030 hooks_property(c, "ontop");
1034 /** Unban a client and move it back into the viewport.
1035 * \param c The client.
1037 void
1038 client_unban(client_t *c)
1040 if(c->isbanned)
1042 /* Move the client back where it belongs. */
1043 uint32_t request[] = { c->geometries.internal.x,
1044 c->geometries.internal.y,
1045 c->geometries.internal.width,
1046 c->geometries.internal.height };
1048 xcb_configure_window(globalconf.connection, c->win,
1049 XCB_CONFIG_WINDOW_X
1050 | XCB_CONFIG_WINDOW_Y
1051 | XCB_CONFIG_WINDOW_WIDTH
1052 | XCB_CONFIG_WINDOW_HEIGHT,
1053 request);
1054 window_configure(c->win, c->geometries.internal, c->border);
1056 /* Do this manually because the system doesn't know we moved the toolbar.
1057 * Note that !isvisible titlebars are unmapped and for fullscreen it'll
1058 * end up offscreen anyway. */
1059 if(c->titlebar)
1061 simple_window_t *sw = &c->titlebar->sw;
1062 /* All resizing is done, so only move now. */
1063 request[0] = sw->geometry.x;
1064 request[1] = sw->geometry.y;
1066 xcb_configure_window(globalconf.connection, sw->window,
1067 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
1068 request);
1071 c->isbanned = false;
1073 /* All the wiboxes (may) need to be repositioned. */
1074 if(client_hasstrut(c))
1075 wibox_update_positions();
1079 /** Unmanage a client.
1080 * \param c The client.
1082 void
1083 client_unmanage(client_t *c)
1085 tag_array_t *tags = &globalconf.screens[c->screen].tags;
1087 /* Reset transient_for attributes of widows that maybe refering to us */
1088 for(client_t *tc = globalconf.clients; tc; tc = tc->next)
1089 if(tc->transient_for == c)
1090 tc->transient_for = NULL;
1092 if(globalconf.screens[c->phys_screen].client_focus == c)
1093 client_unfocus(c);
1095 /* remove client everywhere */
1096 client_list_detach(&globalconf.clients, c);
1097 stack_client_delete(c);
1098 for(int i = 0; i < tags->len; i++)
1099 untag_client(c, tags->tab[i]);
1101 /* call hook */
1102 if(globalconf.hooks.unmanage != LUA_REFNIL)
1104 luaA_client_userdata_new(globalconf.L, c);
1105 luaA_dofunction(globalconf.L, globalconf.hooks.unmanage, 1, 0);
1108 /* Call hook to notify list change */
1109 if(globalconf.hooks.clients != LUA_REFNIL)
1110 luaA_dofunction(globalconf.L, globalconf.hooks.clients, 0, 0);
1112 /* The server grab construct avoids race conditions. */
1113 xcb_grab_server(globalconf.connection);
1115 xcb_ungrab_button(globalconf.connection, XCB_BUTTON_INDEX_ANY, c->win,
1116 XCB_BUTTON_MASK_ANY);
1117 window_state_set(c->win, XCB_WM_STATE_WITHDRAWN);
1119 xcb_flush(globalconf.connection);
1120 xcb_ungrab_server(globalconf.connection);
1122 titlebar_client_detach(c);
1124 ewmh_update_net_client_list(c->phys_screen);
1126 /* All the wiboxes (may) need to be repositioned. */
1127 if(client_hasstrut(c))
1128 wibox_update_positions();
1130 /* set client as invalid */
1131 c->invalid = true;
1133 client_unref(&c);
1136 /** Kill a client via a WM_DELETE_WINDOW request or KillClient if not
1137 * supported.
1138 * \param c The client to kill.
1140 void
1141 client_kill(client_t *c)
1143 if(window_hasproto(c->win, WM_DELETE_WINDOW))
1145 xcb_client_message_event_t ev;
1147 /* Initialize all of event's fields first */
1148 p_clear(&ev, 1);
1150 ev.response_type = XCB_CLIENT_MESSAGE;
1151 ev.window = c->win;
1152 ev.format = 32;
1153 ev.data.data32[1] = XCB_CURRENT_TIME;
1154 ev.type = WM_PROTOCOLS;
1155 ev.data.data32[0] = WM_DELETE_WINDOW;
1157 xcb_send_event(globalconf.connection, false, c->win,
1158 XCB_EVENT_MASK_NO_EVENT, (char *) &ev);
1160 else
1161 xcb_kill_client(globalconf.connection, c->win);
1164 /** Get all clients into a table.
1165 * \param L The Lua VM state.
1166 * \return The number of elements pushed on stack.
1167 * \luastack
1168 * \lparam An optional screen nunmber.
1169 * \lreturn A table with all clients.
1171 static int
1172 luaA_client_get(lua_State *L)
1174 int i = 1, screen;
1175 client_t *c;
1177 screen = luaL_optnumber(L, 1, 0) - 1;
1179 lua_newtable(L);
1181 if(screen == SCREEN_UNDEF)
1182 for(c = globalconf.clients; c; c = c->next)
1184 luaA_client_userdata_new(globalconf.L, c);
1185 lua_rawseti(L, -2, i++);
1187 else
1189 luaA_checkscreen(screen);
1190 for(c = globalconf.clients; c; c = c->next)
1191 if(c->screen == screen)
1193 luaA_client_userdata_new(globalconf.L, c);
1194 lua_rawseti(L, -2, i++);
1198 return 1;
1201 /** Check if a client is visible on its screen.
1202 * \param L The Lua VM state.
1203 * \return The number of elements pushed on stack.
1204 * \luastack
1205 * \lvalue A client.
1206 * \lreturn A boolean value, true if the client is visible, false otherwise.
1208 static int
1209 luaA_client_isvisible(lua_State *L)
1211 client_t **c = luaA_checkudata(L, 1, "client");
1212 lua_pushboolean(L, client_isvisible(*c, (*c)->screen));
1213 return 1;
1216 /** Set client border width.
1217 * \param c The client.
1218 * \param width The border width.
1220 void
1221 client_setborder(client_t *c, int width)
1223 uint32_t w = width;
1225 if(width > 0 && (c->type == WINDOW_TYPE_DOCK
1226 || c->type == WINDOW_TYPE_SPLASH
1227 || c->type == WINDOW_TYPE_DESKTOP
1228 || c->isfullscreen))
1229 return;
1231 if(width == c->border || width < 0)
1232 return;
1234 /* Update geometry with the new border. */
1235 c->geometry.width -= 2 * c->border;
1236 c->geometry.height -= 2 * c->border;
1238 c->border = width;
1239 xcb_configure_window(globalconf.connection, c->win,
1240 XCB_CONFIG_WINDOW_BORDER_WIDTH, &w);
1242 c->geometry.width += 2 * c->border;
1243 c->geometry.height += 2 * c->border;
1244 /* Tiled clients will be resized by the layout functions. */
1245 client_need_arrange(c);
1247 /* Changing border size also affects the size of the titlebar. */
1248 if (c->titlebar)
1249 titlebar_update_geometry(c);
1251 hooks_property(c, "border_width");
1254 /** Kill a client.
1255 * \param L The Lua VM state.
1257 * \luastack
1258 * \lvalue A client.
1260 static int
1261 luaA_client_kill(lua_State *L)
1263 client_t **c = luaA_checkudata(L, 1, "client");
1264 client_kill(*c);
1265 return 0;
1268 /** Swap a client with another one.
1269 * \param L The Lua VM state.
1270 * \luastack
1271 * \lvalue A client.
1272 * \lparam A client to swap with.
1274 static int
1275 luaA_client_swap(lua_State *L)
1277 client_t **c = luaA_checkudata(L, 1, "client");
1278 client_t **swap = luaA_checkudata(L, 2, "client");
1280 if(*c != *swap)
1282 client_list_swap(&globalconf.clients, *swap, *c);
1283 client_need_arrange(*c);
1284 client_need_arrange(*swap);
1286 /* Call hook to notify list change */
1287 if(globalconf.hooks.clients != LUA_REFNIL)
1288 luaA_dofunction(L, globalconf.hooks.clients, 0, 0);
1291 return 0;
1294 /** Access or set the client tags.
1295 * \param L The Lua VM state.
1296 * \return The number of elements pushed on stack.
1297 * \lparam A table with tags to set, or none to get the current tags table.
1298 * \return The clients tag.
1300 static int
1301 luaA_client_tags(lua_State *L)
1303 tag_array_t *tags;
1304 tag_t **tag;
1305 client_t **c = luaA_checkudata(L, 1, "client");
1306 int j = 0;
1308 if(lua_gettop(L) == 2)
1310 luaA_checktable(L, 2);
1311 tags = &globalconf.screens[(*c)->screen].tags;
1312 for(int i = 0; i < tags->len; i++)
1313 untag_client(*c, tags->tab[i]);
1314 lua_pushnil(L);
1315 while(lua_next(L, 2))
1317 tag = luaA_checkudata(L, -1, "tag");
1318 tag_client(*c, *tag);
1319 lua_pop(L, 1);
1321 lua_pop(L, 1);
1324 tags = &globalconf.screens[(*c)->screen].tags;
1325 luaA_otable_new(L);
1326 for(int i = 0; i < tags->len; i++)
1327 if(is_client_tagged(*c, tags->tab[i]))
1329 luaA_tag_userdata_new(L, tags->tab[i]);
1330 lua_rawseti(L, -2, ++j);
1333 return 1;
1336 /** Raise a client on top of others which are on the same layer.
1337 * \param L The Lua VM state.
1338 * \luastack
1339 * \lvalue A client.
1341 static int
1342 luaA_client_raise(lua_State *L)
1344 client_t **c = luaA_checkudata(L, 1, "client");
1345 client_raise(*c);
1346 return 0;
1349 /** Lower a client on bottom of others which are on the same layer.
1350 * \param L The Lua VM state.
1351 * \luastack
1352 * \lvalue A client.
1354 static int
1355 luaA_client_lower(lua_State *L)
1357 client_t **c = luaA_checkudata(L, 1, "client");
1358 client_lower(*c);
1359 return 0;
1362 /** Redraw a client by unmapping and mapping it quickly.
1363 * \param L The Lua VM state.
1365 * \luastack
1366 * \lvalue A client.
1368 static int
1369 luaA_client_redraw(lua_State *L)
1371 client_t **c = luaA_checkudata(L, 1, "client");
1373 xcb_unmap_window(globalconf.connection, (*c)->win);
1374 xcb_map_window(globalconf.connection, (*c)->win);
1376 /* Set the focus on the current window if the redraw has been
1377 performed on the window where the pointer is currently on
1378 because after the unmapping/mapping, the focus is lost */
1379 if(globalconf.screen_focus->client_focus == *c)
1381 client_unfocus(*c);
1382 client_focus(*c, true);
1385 return 0;
1388 /** Stop managing a client.
1389 * \param L The Lua VM state.
1390 * \return The number of elements pushed on stack.
1391 * \luastack
1392 * \lvalue A client.
1394 static int
1395 luaA_client_unmanage(lua_State *L)
1397 client_t **c = luaA_checkudata(L, 1, "client");
1398 client_unmanage(*c);
1399 return 0;
1402 /** Return client geometry.
1403 * \param L The Lua VM state.
1404 * \return The number of elements pushed on stack.
1405 * \luastack
1406 * \lparam A table with new coordinates, or none.
1407 * \lreturn A table with client coordinates.
1409 static int
1410 luaA_client_geometry(lua_State *L)
1412 client_t **c = luaA_checkudata(L, 1, "client");
1414 if(lua_gettop(L) == 2)
1416 area_t geometry;
1418 luaA_checktable(L, 2);
1419 geometry.x = luaA_getopt_number(L, 2, "x", (*c)->geometry.x);
1420 geometry.y = luaA_getopt_number(L, 2, "y", (*c)->geometry.y);
1421 if(client_isfixed(*c))
1423 geometry.width = (*c)->geometry.width;
1424 geometry.height = (*c)->geometry.height;
1426 else
1428 geometry.width = luaA_getopt_number(L, 2, "width", (*c)->geometry.width);
1429 geometry.height = luaA_getopt_number(L, 2, "height", (*c)->geometry.height);
1432 client_resize(*c, geometry, (*c)->size_hints_honor);
1435 return luaA_pusharea(L, (*c)->geometry);
1438 /** Push a strut type to a table on stack.
1439 * \param L The Lua VM state.
1440 * \param struts The struts to push.
1441 * \return The number of elements pushed on stack.
1443 static inline int
1444 luaA_pushstruts(lua_State *L, strut_t struts)
1446 lua_newtable(L);
1447 lua_pushnumber(L, struts.left);
1448 lua_setfield(L, -2, "left");
1449 lua_pushnumber(L, struts.right);
1450 lua_setfield(L, -2, "right");
1451 lua_pushnumber(L, struts.top);
1452 lua_setfield(L, -2, "top");
1453 lua_pushnumber(L, struts.bottom);
1454 lua_setfield(L, -2, "bottom");
1455 return 1;
1458 /** Return client struts (reserved space at the edge of the screen).
1459 * \param L The Lua VM state.
1460 * \return The number of elements pushed on stack.
1461 * \luastack
1462 * \lparam A table with new strut values, or none.
1463 * \lreturn A table with strut values.
1465 static int
1466 luaA_client_struts(lua_State *L)
1468 client_t **c = luaA_checkudata(L, 1, "client");
1470 if(lua_gettop(L) == 2)
1472 strut_t struts;
1473 area_t screen_area = display_area_get((*c)->phys_screen, NULL, NULL);
1475 struts.left = luaA_getopt_number(L, 2, "left", (*c)->strut.left);
1476 struts.right = luaA_getopt_number(L, 2, "right", (*c)->strut.right);
1477 struts.top = luaA_getopt_number(L, 2, "top", (*c)->strut.top);
1478 struts.bottom = luaA_getopt_number(L, 2, "bottom", (*c)->strut.bottom);
1480 if (struts.left != (*c)->strut.left || struts.right != (*c)->strut.right ||
1481 struts.top != (*c)->strut.top || struts.bottom != (*c)->strut.bottom) {
1482 /* Struts are not so well defined in the context of xinerama. So we just
1483 * give the entire root window and let the window manager decide. */
1484 struts.left_start_y = 0;
1485 struts.left_end_y = !struts.left ? 0 : screen_area.height;
1486 struts.right_start_y = 0;
1487 struts.right_end_y = !struts.right ? 0 : screen_area.height;
1488 struts.top_start_x = 0;
1489 struts.top_end_x = !struts.top ? 0 : screen_area.width;
1490 struts.bottom_start_x = 0;
1491 struts.bottom_end_x = !struts.bottom ? 0 : screen_area.width;
1493 (*c)->strut = struts;
1495 ewmh_update_client_strut((*c));
1497 client_need_arrange((*c));
1498 /* All the wiboxes (may) need to be repositioned. */
1499 wibox_update_positions();
1503 return luaA_pushstruts(L, (*c)->strut);
1506 /** Client newindex.
1507 * \param L The Lua VM state.
1508 * \return The number of elements pushed on stack.
1511 luaA_client_newindex(lua_State *L)
1513 size_t len;
1514 client_t **c = luaA_checkudata(L, 1, "client");
1515 const char *buf = luaL_checklstring(L, 2, &len);
1516 bool b;
1517 double d;
1518 int i;
1519 wibox_t **t = NULL;
1520 image_t **image;
1522 if((*c)->invalid)
1523 luaL_error(L, "client is invalid\n");
1525 switch(a_tokenize(buf, len))
1527 case A_TK_SCREEN:
1528 if(globalconf.xinerama_is_active)
1530 i = luaL_checknumber(L, 3) - 1;
1531 luaA_checkscreen(i);
1532 screen_client_moveto(*c, i, true, true);
1534 break;
1535 case A_TK_HIDE:
1536 b = luaA_checkboolean(L, 3);
1537 if(b != (*c)->ishidden)
1539 client_need_arrange(*c);
1540 (*c)->ishidden = b;
1541 client_need_arrange(*c);
1543 break;
1544 case A_TK_MINIMIZED:
1545 client_setminimized(*c, luaA_checkboolean(L, 3));
1546 break;
1547 case A_TK_FULLSCREEN:
1548 client_setfullscreen(*c, luaA_checkboolean(L, 3));
1549 break;
1550 case A_TK_MAXIMIZED_HORIZONTAL:
1551 client_setmaxhoriz(*c, luaA_checkboolean(L, 3));
1552 break;
1553 case A_TK_MAXIMIZED_VERTICAL:
1554 client_setmaxvert(*c, luaA_checkboolean(L, 3));
1555 break;
1556 case A_TK_ICON:
1557 image = luaA_checkudata(L, 3, "image");
1558 image_unref(&(*c)->icon);
1559 image_ref(image);
1560 (*c)->icon = *image;
1561 /* execute hook */
1562 hooks_property(*c, "icon");
1563 break;
1564 case A_TK_OPACITY:
1565 if(lua_isnil(L, 3))
1566 window_opacity_set((*c)->win, -1);
1567 else
1569 d = luaL_checknumber(L, 3);
1570 if(d >= 0 && d <= 1)
1571 window_opacity_set((*c)->win, d);
1573 break;
1574 case A_TK_STICKY:
1575 client_setsticky(*c, luaA_checkboolean(L, 3));
1576 break;
1577 case A_TK_SIZE_HINTS_HONOR:
1578 (*c)->size_hints_honor = luaA_checkboolean(L, 3);
1579 client_need_arrange(*c);
1580 break;
1581 case A_TK_BORDER_WIDTH:
1582 client_setborder(*c, luaL_checknumber(L, 3));
1583 break;
1584 case A_TK_ONTOP:
1585 client_setontop(*c, luaA_checkboolean(L, 3));
1586 break;
1587 case A_TK_ABOVE:
1588 client_setabove(*c, luaA_checkboolean(L, 3));
1589 break;
1590 case A_TK_BELOW:
1591 client_setbelow(*c, luaA_checkboolean(L, 3));
1592 break;
1593 case A_TK_BORDER_COLOR:
1594 if((buf = luaL_checklstring(L, 3, &len))
1595 && xcolor_init_reply(xcolor_init_unchecked(&(*c)->border_color, buf, len)))
1596 xcb_change_window_attributes(globalconf.connection, (*c)->win,
1597 XCB_CW_BORDER_PIXEL, &(*c)->border_color.pixel);
1598 break;
1599 case A_TK_TITLEBAR:
1600 if(lua_isnil(L, 3))
1601 titlebar_client_detach(*c);
1602 else
1604 t = luaA_checkudata(L, 3, "wibox");
1605 titlebar_client_attach(*c, *t);
1607 break;
1608 default:
1609 return 0;
1612 return 0;
1615 /** Client object.
1616 * \param L The Lua VM state.
1617 * \return The number of elements pushed on stack.
1618 * \luastack
1619 * \lfield id The window X id.
1620 * \lfield name The client title.
1621 * \lfield skip_taskbar True if the client does not want to be in taskbar.
1622 * \lfield type The window type (desktop, normal, dock, …).
1623 * \lfield class The client class.
1624 * \lfield instance The client instance.
1625 * \lfield pid The client PID, if available.
1626 * \lfield role The window role, if available.
1627 * \lfield machine The machine client is running on.
1628 * \lfield icon_name The client name when iconified.
1629 * \lfield screen Client screen number.
1630 * \lfield hide Define if the client must be hidden, i.e. never mapped,
1631 * invisible in taskbar.
1632 * \lfield minimize Define it the client must be iconify, i.e. only visible in
1633 * taskbar.
1634 * \lfield icon_path Path to the icon used to identify.
1635 * \lfield size_hints_honor Honor size hints, i.e. respect size ratio.
1636 * \lfield border_width The client border width.
1637 * \lfield border_color The client border color.
1638 * \lfield titlebar The client titlebar.
1639 * \lfield urgent The client urgent state.
1640 * \lfield content An image representing the client window content (screenshot).
1641 * \lfield focus The focused client.
1642 * \lfield opacity The client opacity between 0 and 1.
1643 * \lfield ontop The client is on top of every other windows.
1644 * \lfield above The client is above normal windows.
1645 * \lfield below The client is below normal windows.
1646 * \lfield fullscreen The client is fullscreen or not.
1647 * \lfield maximized_horizontal The client is maximized horizontally or not.
1648 * \lfield maximized_vertical The client is maximized vertically or not.
1649 * \lfield transient_for Return the client the window is transient for.
1650 * \lfield group_id Identification unique to a group of windows.
1651 * \lfield leader_id Identification unique to windows spawned by the same command.
1652 * \lfield size_hints A table with size hints of the client: user_position,
1653 * user_size, program_position, program_size, etc.
1655 static int
1656 luaA_client_index(lua_State *L)
1658 size_t len;
1659 ssize_t slen;
1660 client_t **c = luaA_checkudata(L, 1, "client");
1661 const char *buf = luaL_checklstring(L, 2, &len);
1662 char *value;
1663 void *data;
1664 xcb_get_wm_class_reply_t hint;
1665 xcb_get_property_cookie_t prop_c;
1666 xcb_get_property_reply_t *prop_r = NULL;
1667 double d;
1669 if((*c)->invalid)
1670 luaL_error(L, "client is invalid\n");
1672 if(luaA_usemetatable(L, 1, 2))
1673 return 1;
1675 switch(a_tokenize(buf, len))
1677 image_t *image;
1679 case A_TK_NAME:
1680 lua_pushstring(L, (*c)->name);
1681 break;
1682 case A_TK_TRANSIENT_FOR:
1683 if((*c)->transient_for)
1684 return luaA_client_userdata_new(L, (*c)->transient_for);
1685 return 0;
1686 case A_TK_SKIP_TASKBAR:
1687 lua_pushboolean(L, (*c)->skiptb);
1688 break;
1689 case A_TK_CONTENT:
1690 if((image = client_getcontent(*c)))
1691 return luaA_image_userdata_new(L, image);
1692 return 0;
1693 case A_TK_TYPE:
1694 switch((*c)->type)
1696 case WINDOW_TYPE_DESKTOP:
1697 lua_pushliteral(L, "desktop");
1698 break;
1699 case WINDOW_TYPE_DOCK:
1700 lua_pushliteral(L, "dock");
1701 break;
1702 case WINDOW_TYPE_SPLASH:
1703 lua_pushliteral(L, "splash");
1704 break;
1705 case WINDOW_TYPE_DIALOG:
1706 lua_pushliteral(L, "dialog");
1707 break;
1708 case WINDOW_TYPE_MENU:
1709 lua_pushliteral(L, "menu");
1710 break;
1711 case WINDOW_TYPE_TOOLBAR:
1712 lua_pushliteral(L, "toolbar");
1713 break;
1714 case WINDOW_TYPE_UTILITY:
1715 lua_pushliteral(L, "utility");
1716 break;
1717 case WINDOW_TYPE_DROPDOWN_MENU:
1718 lua_pushliteral(L, "dropdown_menu");
1719 break;
1720 case WINDOW_TYPE_POPUP_MENU:
1721 lua_pushliteral(L, "popup_menu");
1722 break;
1723 case WINDOW_TYPE_TOOLTIP:
1724 lua_pushliteral(L, "tooltip");
1725 break;
1726 case WINDOW_TYPE_NOTIFICATION:
1727 lua_pushliteral(L, "notification");
1728 break;
1729 case WINDOW_TYPE_COMBO:
1730 lua_pushliteral(L, "combo");
1731 break;
1732 case WINDOW_TYPE_DND:
1733 lua_pushliteral(L, "dnd");
1734 break;
1735 case WINDOW_TYPE_NORMAL:
1736 lua_pushliteral(L, "normal");
1737 break;
1739 break;
1740 case A_TK_CLASS:
1741 if(!xcb_get_wm_class_reply(globalconf.connection,
1742 xcb_get_wm_class_unchecked(globalconf.connection, (*c)->win),
1743 &hint, NULL))
1744 return 0;
1745 lua_pushstring(L, hint.class_name);
1746 xcb_get_wm_class_reply_wipe(&hint);
1747 break;
1748 case A_TK_INSTANCE:
1749 if(!xcb_get_wm_class_reply(globalconf.connection,
1750 xcb_get_wm_class_unchecked(globalconf.connection, (*c)->win),
1751 &hint, NULL))
1752 return 0;
1753 lua_pushstring(L, hint.instance_name);
1754 xcb_get_wm_class_reply_wipe(&hint);
1755 break;
1756 case A_TK_ROLE:
1757 if(!xutil_text_prop_get(globalconf.connection, (*c)->win,
1758 WM_WINDOW_ROLE, &value, &slen))
1759 return 0;
1760 lua_pushlstring(L, value, slen);
1761 p_delete(&value);
1762 break;
1763 case A_TK_PID:
1764 prop_c = xcb_get_property_unchecked(globalconf.connection, false, (*c)->win, _NET_WM_PID, CARDINAL, 0L, 1L);
1765 prop_r = xcb_get_property_reply(globalconf.connection, prop_c, NULL);
1767 if(prop_r && prop_r->value_len && (data = xcb_get_property_value(prop_r)))
1768 lua_pushnumber(L, *(uint32_t *)data);
1769 else
1771 p_delete(&prop_r);
1772 return 0;
1774 break;
1775 case A_TK_ID:
1776 lua_pushnumber(L, (*c)->win);
1777 break;
1778 case A_TK_LEADER_ID:
1779 lua_pushnumber(L, (*c)->leader_win);
1780 break;
1781 case A_TK_MACHINE:
1782 if(!xutil_text_prop_get(globalconf.connection, (*c)->win,
1783 WM_CLIENT_MACHINE, &value, &slen))
1784 return 0;
1785 lua_pushlstring(L, value, slen);
1786 p_delete(&value);
1787 break;
1788 case A_TK_ICON_NAME:
1789 lua_pushstring(L, (*c)->icon_name);
1790 break;
1791 case A_TK_SCREEN:
1792 lua_pushnumber(L, 1 + (*c)->screen);
1793 break;
1794 case A_TK_HIDE:
1795 lua_pushboolean(L, (*c)->ishidden);
1796 break;
1797 case A_TK_MINIMIZED:
1798 lua_pushboolean(L, (*c)->isminimized);
1799 break;
1800 case A_TK_FULLSCREEN:
1801 lua_pushboolean(L, (*c)->isfullscreen);
1802 break;
1803 case A_TK_GROUP_ID:
1804 if((*c)->group_win)
1805 lua_pushnumber(L, (*c)->group_win);
1806 else
1807 return 0;
1808 break;
1809 case A_TK_MAXIMIZED_HORIZONTAL:
1810 lua_pushboolean(L, (*c)->ismaxhoriz);
1811 break;
1812 case A_TK_MAXIMIZED_VERTICAL:
1813 lua_pushboolean(L, (*c)->ismaxvert);
1814 break;
1815 case A_TK_ICON:
1816 if((*c)->icon)
1817 luaA_image_userdata_new(L, (*c)->icon);
1818 else
1819 return 0;
1820 break;
1821 case A_TK_OPACITY:
1822 if((d = window_opacity_get((*c)->win)) >= 0)
1823 lua_pushnumber(L, d);
1824 else
1825 return 0;
1826 break;
1827 case A_TK_ONTOP:
1828 lua_pushboolean(L, (*c)->isontop);
1829 break;
1830 case A_TK_ABOVE:
1831 lua_pushboolean(L, (*c)->isabove);
1832 break;
1833 case A_TK_BELOW:
1834 lua_pushboolean(L, (*c)->isbelow);
1835 break;
1836 case A_TK_STICKY:
1837 lua_pushboolean(L, (*c)->issticky);
1838 break;
1839 case A_TK_SIZE_HINTS_HONOR:
1840 lua_pushboolean(L, (*c)->size_hints_honor);
1841 break;
1842 case A_TK_BORDER_WIDTH:
1843 lua_pushnumber(L, (*c)->border);
1844 break;
1845 case A_TK_BORDER_COLOR:
1846 luaA_pushcolor(L, &(*c)->border_color);
1847 break;
1848 case A_TK_TITLEBAR:
1849 if((*c)->titlebar)
1850 return luaA_wibox_userdata_new(L, (*c)->titlebar);
1851 return 0;
1852 case A_TK_URGENT:
1853 lua_pushboolean(L, (*c)->isurgent);
1854 break;
1855 case A_TK_SIZE_HINTS:
1857 const char *u_or_p = NULL;
1859 lua_newtable(L);
1861 if((*c)->size_hints.flags & XCB_SIZE_HINT_US_POSITION)
1862 u_or_p = "user_position";
1863 else if((*c)->size_hints.flags & XCB_SIZE_HINT_P_POSITION)
1864 u_or_p = "program_position";
1866 if(u_or_p)
1868 lua_newtable(L);
1869 lua_pushnumber(L, (*c)->size_hints.x);
1870 lua_setfield(L, -2, "x");
1871 lua_pushnumber(L, (*c)->size_hints.y);
1872 lua_setfield(L, -2, "y");
1873 lua_setfield(L, -2, u_or_p);
1874 u_or_p = NULL;
1877 if((*c)->size_hints.flags & XCB_SIZE_HINT_US_SIZE)
1878 u_or_p = "user_size";
1879 else if((*c)->size_hints.flags & XCB_SIZE_HINT_P_SIZE)
1880 u_or_p = "program_size";
1882 if(u_or_p)
1884 lua_newtable(L);
1885 lua_pushnumber(L, (*c)->size_hints.width);
1886 lua_setfield(L, -2, "width");
1887 lua_pushnumber(L, (*c)->size_hints.height);
1888 lua_setfield(L, -2, "height");
1889 lua_setfield(L, -2, u_or_p);
1892 if((*c)->size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE)
1894 lua_pushnumber(L, (*c)->size_hints.min_width);
1895 lua_setfield(L, -2, "min_width");
1896 lua_pushnumber(L, (*c)->size_hints.min_height);
1897 lua_setfield(L, -2, "min_height");
1900 if((*c)->size_hints.flags & XCB_SIZE_HINT_P_MAX_SIZE)
1902 lua_pushnumber(L, (*c)->size_hints.max_width);
1903 lua_setfield(L, -2, "max_width");
1904 lua_pushnumber(L, (*c)->size_hints.max_height);
1905 lua_setfield(L, -2, "max_height");
1908 if((*c)->size_hints.flags & XCB_SIZE_HINT_P_RESIZE_INC)
1910 lua_pushnumber(L, (*c)->size_hints.width_inc);
1911 lua_setfield(L, -2, "width_inc");
1912 lua_pushnumber(L, (*c)->size_hints.height_inc);
1913 lua_setfield(L, -2, "height_inc");
1916 if((*c)->size_hints.flags & XCB_SIZE_HINT_P_ASPECT)
1918 lua_pushnumber(L, (*c)->size_hints.min_aspect_num);
1919 lua_setfield(L, -2, "min_aspect_num");
1920 lua_pushnumber(L, (*c)->size_hints.min_aspect_den);
1921 lua_setfield(L, -2, "min_aspect_den");
1922 lua_pushnumber(L, (*c)->size_hints.max_aspect_num);
1923 lua_setfield(L, -2, "max_aspect_num");
1924 lua_pushnumber(L, (*c)->size_hints.max_aspect_den);
1925 lua_setfield(L, -2, "max_aspect_den");
1928 if((*c)->size_hints.flags & XCB_SIZE_HINT_BASE_SIZE)
1930 lua_pushnumber(L, (*c)->size_hints.base_width);
1931 lua_setfield(L, -2, "base_width");
1932 lua_pushnumber(L, (*c)->size_hints.base_height);
1933 lua_setfield(L, -2, "base_height");
1936 if((*c)->size_hints.flags & XCB_SIZE_HINT_P_WIN_GRAVITY)
1938 switch((*c)->size_hints.win_gravity)
1940 default:
1941 lua_pushliteral(L, "north_west");
1942 break;
1943 case XCB_GRAVITY_NORTH:
1944 lua_pushliteral(L, "north");
1945 break;
1946 case XCB_GRAVITY_NORTH_EAST:
1947 lua_pushliteral(L, "north_east");
1948 break;
1949 case XCB_GRAVITY_WEST:
1950 lua_pushliteral(L, "west");
1951 break;
1952 case XCB_GRAVITY_CENTER:
1953 lua_pushliteral(L, "center");
1954 break;
1955 case XCB_GRAVITY_EAST:
1956 lua_pushliteral(L, "east");
1957 break;
1958 case XCB_GRAVITY_SOUTH_WEST:
1959 lua_pushliteral(L, "south_west");
1960 break;
1961 case XCB_GRAVITY_SOUTH:
1962 lua_pushliteral(L, "south");
1963 break;
1964 case XCB_GRAVITY_SOUTH_EAST:
1965 lua_pushliteral(L, "south_east");
1966 break;
1967 case XCB_GRAVITY_STATIC:
1968 lua_pushliteral(L, "static");
1969 break;
1971 lua_setfield(L, -2, "win_gravity");
1974 break;
1975 default:
1976 return 0;
1979 return 1;
1982 /** Get or set mouse buttons bindings for a client.
1983 * \param L The Lua VM state.
1984 * \return The number of element pushed on stack.
1985 * \luastack
1986 * \lvalue A client.
1987 * \lparam An array of mouse button bindings objects, or nothing.
1988 * \return The array of mouse button bindings objects of this client.
1990 static int
1991 luaA_client_buttons(lua_State *L)
1993 client_t **client = luaA_checkudata(L, 1, "client");
1994 button_array_t *buttons = &(*client)->buttons;
1996 if(lua_gettop(L) == 2)
1997 luaA_button_array_set(L, 2, buttons);
1999 window_buttons_grab((*client)->win, &(*client)->buttons);
2001 return luaA_button_array_get(L, buttons);
2004 /** Get or set keys bindings for a client.
2005 * \param L The Lua VM state.
2006 * \return The number of element pushed on stack.
2007 * \luastack
2008 * \lvalue A client.
2009 * \lparam An array of key bindings objects, or nothing.
2010 * \return The array of key bindings objects of this client.
2012 static int
2013 luaA_client_keys(lua_State *L)
2015 client_t **c = luaA_checkudata(L, 1, "client");
2016 keybindings_t *keys = &(*c)->keys;
2018 if(lua_gettop(L) == 2)
2020 luaA_key_array_set(L, 2, keys);
2021 xcb_ungrab_key(globalconf.connection, XCB_GRAB_ANY, (*c)->win, XCB_BUTTON_MASK_ANY);
2022 window_grabkeys((*c)->win, keys);
2025 return luaA_key_array_get(L, keys);
2028 /** Send fake events to a client.
2029 * \param L The Lua VM state.
2030 * \return The number of element pushed on stack.
2031 * \luastack
2032 * \lvalue A client.
2033 * \param The event type: key_press, key_release, button_press, button_release
2034 * or motion_notify.
2035 * \param The detail: in case of a key event, this is the keycode to send, in
2036 * case of a button event this is the number of the button. In case of a motion
2037 * event, this is a boolean value which if true make the coordinates relatives.
2038 * \param In case of a motion event, this is the X coordinate.
2039 * \param In case of a motion event, this is the Y coordinate.
2041 static int
2042 luaA_client_fake_input(lua_State *L)
2044 if(!globalconf.have_xtest)
2046 luaA_warn(L, "XTest extension is not available, cannot fake input.");
2047 return 0;
2050 client_t **c = luaA_checkudata(L, 1, "client");
2051 size_t tlen;
2052 const char *stype = luaL_checklstring(L, 2, &tlen);
2053 uint8_t type, detail;
2054 int x = 0, y = 0;
2056 switch(a_tokenize(stype, tlen))
2058 case A_TK_KEY_PRESS:
2059 type = XCB_KEY_PRESS;
2060 detail = luaL_checknumber(L, 3); /* keycode */
2061 break;
2062 case A_TK_KEY_RELEASE:
2063 type = XCB_KEY_RELEASE;
2064 detail = luaL_checknumber(L, 3); /* keycode */
2065 break;
2066 case A_TK_BUTTON_PRESS:
2067 type = XCB_BUTTON_PRESS;
2068 detail = luaL_checknumber(L, 3); /* button number */
2069 break;
2070 case A_TK_BUTTON_RELEASE:
2071 type = XCB_BUTTON_RELEASE;
2072 detail = luaL_checknumber(L, 3); /* button number */
2073 break;
2074 case A_TK_MOTION_NOTIFY:
2075 type = XCB_MOTION_NOTIFY;
2076 detail = luaA_checkboolean(L, 3); /* relative to the current position or not */
2077 x = luaL_checknumber(L, 4);
2078 y = luaL_checknumber(L, 5);
2079 break;
2080 default:
2081 return 0;
2084 xcb_test_fake_input(globalconf.connection,
2085 type,
2086 detail,
2087 XCB_CURRENT_TIME,
2088 (*c)->win,
2089 x, y,
2091 return 0;
2094 /* Client module.
2095 * \param L The Lua VM state.
2096 * \return The number of pushed elements.
2098 static int
2099 luaA_client_module_index(lua_State *L)
2101 size_t len;
2102 const char *buf = luaL_checklstring(L, 2, &len);
2104 switch(a_tokenize(buf, len))
2106 case A_TK_FOCUS:
2107 if(globalconf.screen_focus->client_focus)
2108 luaA_client_userdata_new(L, globalconf.screen_focus->client_focus);
2109 else
2110 return 0;
2111 break;
2112 default:
2113 return 0;
2116 return 1;
2119 /* Client module new index.
2120 * \param L The Lua VM state.
2121 * \return The number of pushed elements.
2123 static int
2124 luaA_client_module_newindex(lua_State *L)
2126 size_t len;
2127 const char *buf = luaL_checklstring(L, 2, &len);
2128 client_t **c;
2130 switch(a_tokenize(buf, len))
2132 case A_TK_FOCUS:
2133 c = luaA_checkudata(L, 3, "client");
2134 client_focus(*c, true);
2135 break;
2136 default:
2137 break;
2140 return 0;
2143 const struct luaL_reg awesome_client_methods[] =
2145 { "get", luaA_client_get },
2146 { "__index", luaA_client_module_index },
2147 { "__newindex", luaA_client_module_newindex },
2148 { NULL, NULL }
2150 const struct luaL_reg awesome_client_meta[] =
2152 { "isvisible", luaA_client_isvisible },
2153 { "geometry", luaA_client_geometry },
2154 { "struts", luaA_client_struts },
2155 { "buttons", luaA_client_buttons },
2156 { "keys", luaA_client_keys },
2157 { "tags", luaA_client_tags },
2158 { "kill", luaA_client_kill },
2159 { "swap", luaA_client_swap },
2160 { "raise", luaA_client_raise },
2161 { "lower", luaA_client_lower },
2162 { "redraw", luaA_client_redraw },
2163 { "unmanage", luaA_client_unmanage },
2164 { "fake_input", luaA_client_fake_input },
2165 { "__index", luaA_client_index },
2166 { "__newindex", luaA_client_newindex },
2167 { "__eq", luaA_client_eq },
2168 { "__gc", luaA_client_gc },
2169 { "__tostring", luaA_client_tostring },
2170 { NULL, NULL }
2173 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80