client: make `ontop' windows under fullscreen
[awesome.git] / client.c
blobf234d65cf406e8da4854a2bc1262b39ab7886d5f
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 /** Load windows properties, restoring client's tag
44 * and floating state before awesome was restarted if any.
45 * \param c A client pointer.
46 * \param screen A virtual screen.
47 * \return True if client had property, false otherwise.
49 static bool
50 client_loadprops(client_t * c, screen_t *screen)
52 ssize_t len;
53 tag_array_t *tags = &screen->tags;
54 char *prop = NULL;
55 xcb_get_property_cookie_t fullscreen_q;
56 xcb_get_property_reply_t *reply;
57 void *data;
59 if(!xutil_text_prop_get(globalconf.connection, c->win, _AWESOME_TAGS,
60 &prop, &len))
61 return false;
63 /* Send the GetProperty requests which will be processed later */
64 fullscreen_q = xcb_get_property_unchecked(globalconf.connection, false, c->win,
65 _AWESOME_FULLSCREEN, CARDINAL, 0, 1);
67 /* ignore property if the tag count isn't matching */
68 if(len == tags->len)
69 for(int i = 0; i < tags->len; i++)
71 if(prop[i] == '1')
72 tag_client(c, tags->tab[i]);
73 else
74 untag_client(c, tags->tab[i]);
77 p_delete(&prop);
79 /* check for fullscreen */
80 reply = xcb_get_property_reply(globalconf.connection, fullscreen_q, NULL);
82 if(reply && reply->value_len && (data = xcb_get_property_value(reply)))
83 client_setfullscreen(c, *(bool *) data);
84 p_delete(&reply);
86 return true;
89 /** Check if client supports protocol a protocole in WM_PROTOCOL.
90 * \param win The window.
91 * \return True if client has the atom in protocol, false otherwise.
93 static bool
94 window_hasproto(xcb_window_t win, xcb_atom_t atom)
96 uint32_t i;
97 xcb_get_wm_protocols_reply_t protocols;
98 bool ret = false;
100 if(xcb_get_wm_protocols_reply(globalconf.connection,
101 xcb_get_wm_protocols_unchecked(globalconf.connection,
102 win, WM_PROTOCOLS),
103 &protocols, NULL))
105 for(i = 0; !ret && i < protocols.atoms_len; i++)
106 if(protocols.atoms[i] == atom)
107 ret = true;
108 xcb_get_wm_protocols_reply_wipe(&protocols);
110 return ret;
113 /** Returns true if a client is tagged
114 * with one of the tags of the specified screen.
115 * \param c The client to check.
116 * \param screen Virtual screen number.
117 * \return true if the client is visible, false otherwise.
119 bool
120 client_maybevisible(client_t *c, int screen)
122 if(c->screen == screen)
124 if(c->issticky || c->type == WINDOW_TYPE_DESKTOP)
125 return true;
127 tag_array_t *tags = &globalconf.screens[screen].tags;
129 for(int i = 0; i < tags->len; i++)
130 if(tags->tab[i]->selected && is_client_tagged(c, tags->tab[i]))
131 return true;
133 return false;
136 /** Return the content of a client as an image.
137 * That's just take a screenshot.
138 * \param c The client.
139 * \return An image.
141 static image_t *
142 client_getcontent(client_t *c)
144 xcb_image_t *ximage = xcb_image_get(globalconf.connection,
145 c->win,
146 0, 0,
147 c->geometries.internal.width,
148 c->geometries.internal.height,
149 ~0, XCB_IMAGE_FORMAT_Z_PIXMAP);
151 if(!ximage || ximage->bpp < 24)
152 return NULL;
154 uint32_t *data = p_alloca(uint32_t, ximage->width * ximage->height);
156 for(int y = 0; y < ximage->height; y++)
157 for(int x = 0; x < ximage->width; x++)
159 data[y * ximage->width + x] = xcb_image_get_pixel(ximage, x, y);
160 data[y * ximage->width + x] |= 0xff000000; /* set alpha to 0xff */
163 image_t *image = image_new_from_argb32(ximage->width, ximage->height, data);
165 xcb_image_destroy(ximage);
167 return image;
170 /** Get a client by its window.
171 * \param w The client window to find.
172 * \return A client pointer if found, NULL otherwise.
174 client_t *
175 client_getbywin(xcb_window_t w)
177 client_t *c;
178 for(c = globalconf.clients; c && c->win != w; c = c->next);
179 return c;
182 /** Unfocus a client.
183 * \param c The client.
185 static void
186 client_unfocus(client_t *c)
188 xcb_window_t root_win = xutil_screen_get(globalconf.connection, c->phys_screen)->root;
189 globalconf.screens[c->phys_screen].client_focus = NULL;
191 /* Set focus on root window, so no events leak to the current window. */
192 if (!c->nofocus)
193 xcb_set_input_focus(globalconf.connection, XCB_INPUT_FOCUS_POINTER_ROOT,
194 root_win, XCB_CURRENT_TIME);
196 /* Call hook */
197 if(globalconf.hooks.unfocus != LUA_REFNIL)
199 luaA_client_userdata_new(globalconf.L, c);
200 luaA_dofunction(globalconf.L, globalconf.hooks.unfocus, 1, 0);
203 ewmh_update_net_active_window(c->phys_screen);
206 /** Ban client and move it out of the viewport.
207 * \param c The client.
209 void
210 client_ban(client_t *c)
212 if(!c->isbanned)
214 /* Move all clients out of the physical viewport into negative coordinate space. */
215 /* They will all be put on top of each other. */
216 uint32_t request[2] = { - (c->geometries.internal.width + 2 * c->border),
217 - (c->geometries.internal.height + 2 * c->border) };
219 xcb_configure_window(globalconf.connection, c->win,
220 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
221 request);
223 /* Do it manually because client geometry remains unchanged. */
224 if (c->titlebar)
226 simple_window_t *sw = &c->titlebar->sw;
228 if (sw->window)
230 request[0] = - (sw->geometry.width);
231 request[1] = - (sw->geometry.height);
232 /* Move the titlebar to the same place as the window. */
233 xcb_configure_window(globalconf.connection, sw->window,
234 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
235 request);
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 /** Give focus to client, or to first client if client is NULL.
252 * \param c The client or NULL.
253 * \return True if a window (even root) has received focus, false otherwise.
255 void
256 client_focus(client_t *c)
258 if(!client_maybevisible(c, c->screen))
259 return;
261 /* unfocus current selected client */
262 if(globalconf.screen_focus->client_focus
263 && c != globalconf.screen_focus->client_focus)
264 client_unfocus(globalconf.screen_focus->client_focus);
266 /* stop hiding c */
267 c->ishidden = false;
268 client_setminimized(c, false);
270 /* unban the client before focusing or it will fail */
271 client_unban(c);
273 globalconf.screen_focus = &globalconf.screens[c->phys_screen];
274 globalconf.screen_focus->client_focus = c;
276 if (!c->nofocus)
277 xcb_set_input_focus(globalconf.connection, XCB_INPUT_FOCUS_POINTER_ROOT,
278 c->win, XCB_CURRENT_TIME);
280 /* Some layouts use focused client differently, so call them back.
281 * And anyway, we have maybe unhidden */
282 client_need_arrange(c);
284 /* execute hook */
285 if(globalconf.hooks.focus != LUA_REFNIL)
287 luaA_client_userdata_new(globalconf.L, globalconf.screen_focus->client_focus);
288 luaA_dofunction(globalconf.L, globalconf.hooks.focus, 1, 0);
291 ewmh_update_net_active_window(c->phys_screen);
294 /** Stack a window below.
295 * \param c The client.
296 * \param previous The previous window on the stack.
297 * \param return The next-previous!
299 static xcb_window_t
300 client_stack_above(client_t *c, xcb_window_t previous)
302 uint32_t config_win_vals[2];
304 config_win_vals[0] = previous;
305 config_win_vals[1] = XCB_STACK_MODE_ABOVE;
307 xcb_configure_window(globalconf.connection, c->win,
308 XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE,
309 config_win_vals);
311 config_win_vals[0] = c->win;
313 if(c->titlebar)
315 xcb_configure_window(globalconf.connection,
316 c->titlebar->sw.window,
317 XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE,
318 config_win_vals);
319 previous = c->titlebar->sw.window;
321 else
322 previous = c->win;
324 /* stack transient window on top of their parents */
325 for(client_node_t *node = *client_node_list_last(&globalconf.stack);
326 node; node = node->prev)
327 if(node->client->transient_for == c)
329 client_stack_above(node->client,
330 previous);
331 previous = node->client->win;
334 return previous;
337 /** Stacking layout layers */
338 typedef enum
340 /** This one is a special layer */
341 LAYER_IGNORE,
342 LAYER_DESKTOP,
343 LAYER_BELOW,
344 LAYER_NORMAL,
345 LAYER_ABOVE,
346 LAYER_ONTOP,
347 LAYER_FULLSCREEN,
348 LAYER_OUTOFSPACE
349 } layer_t;
351 /** Get the real layer of a client according to its attribute (fullscreen, …)
352 * \param c The client.
353 * \return The real layer.
355 static layer_t
356 client_layer_translator(client_t *c)
358 /* first deal with user set attributes */
359 if(c->isontop)
360 return LAYER_ONTOP;
361 else if(c->isfullscreen)
362 return LAYER_FULLSCREEN;
363 else if(c->isabove)
364 return LAYER_ABOVE;
365 else if(c->isbelow)
366 return LAYER_BELOW;
368 /* check for transient attr */
369 if(c->transient_for)
370 return LAYER_IGNORE;
372 /* then deal with windows type */
373 switch(c->type)
375 case WINDOW_TYPE_DOCK:
376 return LAYER_ABOVE;
377 case WINDOW_TYPE_DESKTOP:
378 return LAYER_DESKTOP;
379 case WINDOW_TYPE_DIALOG:
380 case WINDOW_TYPE_MENU:
381 case WINDOW_TYPE_TOOLBAR:
382 case WINDOW_TYPE_UTILITY:
383 return LAYER_ABOVE;
384 default:
385 break;
388 return LAYER_NORMAL;
391 /** Restack clients.
392 * \todo It might be worth stopping to restack everyone and only stack `c'
393 * relatively to the first matching in the list.
395 void
396 client_stack()
398 uint32_t config_win_vals[2];
399 client_node_t *node, *last = *client_node_list_last(&globalconf.stack);
400 layer_t layer;
401 int screen;
403 config_win_vals[0] = XCB_NONE;
404 config_win_vals[1] = XCB_STACK_MODE_ABOVE;
406 /* stack desktop windows */
407 for(layer = LAYER_DESKTOP; layer < LAYER_BELOW; layer++)
408 for(node = last; node; node = node->prev)
409 if(client_layer_translator(node->client) == layer)
410 config_win_vals[0] = client_stack_above(node->client,
411 config_win_vals[0]);
413 /* first stack not ontop wibox window */
414 for(screen = 0; screen < globalconf.nscreen; screen++)
415 for(int i = 0; i < globalconf.screens[screen].wiboxes.len; i++)
417 wibox_t *sb = globalconf.screens[screen].wiboxes.tab[i];
418 if(!sb->ontop)
420 xcb_configure_window(globalconf.connection,
421 sb->sw.window,
422 XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE,
423 config_win_vals);
424 config_win_vals[0] = sb->sw.window;
428 /* stack bottom layers */
429 for(layer = LAYER_BELOW; layer < LAYER_FULLSCREEN; layer++)
430 for(node = last; node; node = node->prev)
431 if(client_layer_translator(node->client) == layer)
432 config_win_vals[0] = client_stack_above(node->client,
433 config_win_vals[0]);
435 /* then stack ontop wibox window */
436 for(screen = 0; screen < globalconf.nscreen; screen++)
437 for(int i = 0; i < globalconf.screens[screen].wiboxes.len; i++)
439 wibox_t *sb = globalconf.screens[screen].wiboxes.tab[i];
440 if(sb->ontop)
442 xcb_configure_window(globalconf.connection,
443 sb->sw.window,
444 XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE,
445 config_win_vals);
446 config_win_vals[0] = sb->sw.window;
450 /* finally stack fullscreen windows */
451 for(layer = LAYER_FULLSCREEN; layer < LAYER_OUTOFSPACE; 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]);
458 /** Copy tags from one client to another client on the same screen.
459 * Be careful: this does not untag!
460 * \param src_c The source client.
461 * \param dst_c The destination client.
463 static void
464 client_duplicate_tags(client_t *src_c, client_t *dst_c)
466 int i;
467 tag_array_t *tags = &globalconf.screens[src_c->screen].tags;
469 /* Avoid doing dangerous things. */
470 if (src_c->screen != dst_c->screen)
471 return;
473 for(i = 0; i < tags->len; i++)
474 if(is_client_tagged(src_c, tags->tab[i]))
475 tag_client(dst_c, tags->tab[i]);
478 /** Manage a new client.
479 * \param w The window.
480 * \param wgeom Window geometry.
481 * \param phys_screen Physical screen number.
482 * \param startup True if we are managing at startup time.
484 void
485 client_manage(xcb_window_t w, xcb_get_geometry_reply_t *wgeom, int phys_screen, bool startup)
487 xcb_get_property_cookie_t ewmh_icon_cookie;
488 client_t *c, *tc = NULL, *group = NULL;
489 image_t *icon;
490 int screen;
491 const uint32_t select_input_val[] =
493 XCB_EVENT_MASK_STRUCTURE_NOTIFY
494 | XCB_EVENT_MASK_PROPERTY_CHANGE
495 | XCB_EVENT_MASK_ENTER_WINDOW
496 | XCB_EVENT_MASK_LEAVE_WINDOW
500 if(systray_iskdedockapp(w))
502 systray_request_handle(w, phys_screen, NULL);
503 return;
506 /* Send request to get NET_WM_ICON property as soon as possible... */
507 ewmh_icon_cookie = ewmh_window_icon_get_unchecked(w);
508 xcb_change_window_attributes(globalconf.connection, w, XCB_CW_EVENT_MASK, select_input_val);
509 c = p_new(client_t, 1);
511 screen = c->screen = screen_getbycoord(phys_screen, wgeom->x, wgeom->y);
513 c->phys_screen = phys_screen;
515 /* Initial values */
516 c->win = w;
517 c->geometry.x = wgeom->x;
518 c->geometry.y = wgeom->y;
519 c->geometry.width = wgeom->width + 2 * wgeom->border_width;
520 c->geometry.height = wgeom->height + 2 * wgeom->border_width;
521 /* Also set internal geometry (client_ban() needs it). */
522 c->geometries.internal.x = wgeom->x;
523 c->geometries.internal.y = wgeom->y;
524 c->geometries.internal.width = wgeom->width;
525 c->geometries.internal.height = wgeom->height;
526 client_setborder(c, wgeom->border_width);
527 if((icon = ewmh_window_icon_get_reply(ewmh_icon_cookie)))
528 c->icon = image_ref(&icon);
530 /* we honor size hints by default */
531 c->size_hints_honor = true;
533 /* update hints */
534 property_update_wm_normal_hints(c, NULL);
535 property_update_wm_hints(c, NULL);
536 property_update_wm_transient_for(c, NULL);
537 property_update_wm_client_leader(c, NULL);
539 /* Recursively find the parent. */
540 for(tc = c; tc->transient_for; tc = tc->transient_for);
541 if(tc != c)
542 screen = tc->screen;
544 /* Try to load props, if it fails check for group windows.
545 * transient_for windows are excluded, because they inherit the parent tags. */
546 if(!client_loadprops(c, &globalconf.screens[screen])
547 && c->group_win
548 && !c->transient_for)
549 /* Try to find a group of windows for better initial placement. */
550 for(group = globalconf.clients; group; group = group->next)
551 if(group->group_win == c->group_win)
552 break;
554 /* Move to the right screen.
555 * Assumption: Window groups do not span multiple logical screens. */
556 if(group && group->phys_screen == c->phys_screen)
557 screen_client_moveto(c, group->screen, false, true);
559 /* Then check clients hints */
560 ewmh_client_check_hints(c);
562 /* move client to screen, but do not tag it for now */
563 screen_client_moveto(c, screen, false, true);
565 /* Duplicate the tags of either the parent or a group window.
566 * Otherwise check for existing tags, if that fails tag it with the current tag.
567 * This could be done lua side, but it's sane behaviour. */
568 if (!c->issticky)
570 if(c->transient_for)
571 client_duplicate_tags(tc, c);
572 else if (group)
573 client_duplicate_tags(group, c);
574 else
576 int i;
577 tag_array_t *tags = &globalconf.screens[screen].tags;
578 for(i = 0; i < tags->len; i++)
579 if(is_client_tagged(c, tags->tab[i]))
580 break;
582 /* if no tag, set current selected */
583 if(i == tags->len)
584 for(i = 0; i < tags->len; i++)
585 if(tags->tab[i]->selected)
586 tag_client(c, tags->tab[i]);
590 /* Push client in client list */
591 client_list_push(&globalconf.clients, client_ref(&c));
593 /* Push client in stack */
594 client_raise(c);
596 /* update window title */
597 property_update_wm_name(c);
598 property_update_wm_icon_name(c);
600 /* update strut */
601 ewmh_client_strut_update(c, NULL);
603 ewmh_update_net_client_list(c->phys_screen);
605 /* Always stay in NORMAL_STATE. Even though iconified seems more
606 * appropriate sometimes. The only possible loss is that clients not using
607 * visibility events may continue to proces data (when banned).
608 * Without any exposes or other events the cost should be fairly limited though.
610 * Some clients may expect the window to be unmapped when STATE_ICONIFIED.
611 * Two conflicting parts of the ICCCM v2.0 (section 4.1.4):
613 * "Normal -> Iconic - The client should send a ClientMessage event as described later in this section."
614 * (note no explicit mention of unmapping, while Normal->Widthdrawn does mention that)
616 * "Once a client's window has left the Withdrawn state, the window will be mapped
617 * if it is in the Normal state and the window will be unmapped if it is in the Iconic state."
619 * At this stage it's just safer to keep it in normal state and avoid confusion.
621 window_state_set(c->win, XCB_WM_STATE_NORMAL);
623 /* Move window outside the viewport before mapping it. */
624 client_ban(c);
625 xcb_map_window(globalconf.connection, c->win);
627 /* Call hook to notify list change */
628 if(globalconf.hooks.clients != LUA_REFNIL)
629 luaA_dofunction(globalconf.L, globalconf.hooks.clients, 0, 0);
631 /* call hook */
632 if(globalconf.hooks.manage != LUA_REFNIL)
634 luaA_client_userdata_new(globalconf.L, c);
635 lua_pushboolean(globalconf.L, startup);
636 luaA_dofunction(globalconf.L, globalconf.hooks.manage, 2, 0);
640 /** Compute client geometry with respect to its geometry hints.
641 * \param c The client.
642 * \param geometry The geometry that the client might receive.
643 * \return The geometry the client must take respecting its hints.
645 area_t
646 client_geometry_hints(client_t *c, area_t geometry)
648 int32_t basew, baseh, minw, minh;
650 /* base size is substituted with min size if not specified */
651 if(c->size_hints.flags & XCB_SIZE_HINT_P_SIZE)
653 basew = c->size_hints.base_width;
654 baseh = c->size_hints.base_height;
656 else if(c->size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE)
658 basew = c->size_hints.min_width;
659 baseh = c->size_hints.min_height;
661 else
662 basew = baseh = 0;
664 /* min size is substituted with base size if not specified */
665 if(c->size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE)
667 minw = c->size_hints.min_width;
668 minh = c->size_hints.min_height;
670 else if(c->size_hints.flags & XCB_SIZE_HINT_P_SIZE)
672 minw = c->size_hints.base_width;
673 minh = c->size_hints.base_height;
675 else
676 minw = minh = 0;
678 if(c->size_hints.flags & XCB_SIZE_HINT_P_ASPECT
679 && c->size_hints.min_aspect_num > 0
680 && c->size_hints.min_aspect_den > 0
681 && geometry.height - baseh > 0
682 && geometry.width - basew > 0)
684 double dx = (double) (geometry.width - basew);
685 double dy = (double) (geometry.height - baseh);
686 double min = (double) c->size_hints.min_aspect_num / (double) c->size_hints.min_aspect_den;
687 double max = (double) c->size_hints.max_aspect_num / (double) c->size_hints.min_aspect_den;
688 double ratio = dx / dy;
689 if(max > 0 && min > 0 && ratio > 0)
691 if(ratio < min)
693 dy = (dx * min + dy) / (min * min + 1);
694 dx = dy * min;
695 geometry.width = (int) dx + basew;
696 geometry.height = (int) dy + baseh;
698 else if(ratio > max)
700 dy = (dx * min + dy) / (max * max + 1);
701 dx = dy * min;
702 geometry.width = (int) dx + basew;
703 geometry.height = (int) dy + baseh;
708 if(minw)
709 geometry.width = MAX(geometry.width, minw);
710 if(minh)
711 geometry.height = MAX(geometry.height, minh);
713 if(c->size_hints.flags & XCB_SIZE_HINT_P_MAX_SIZE)
715 if(c->size_hints.max_width)
716 geometry.width = MIN(geometry.width, c->size_hints.max_width);
717 if(c->size_hints.max_height)
718 geometry.height = MIN(geometry.height, c->size_hints.max_height);
721 if(c->size_hints.flags & (XCB_SIZE_HINT_P_RESIZE_INC | XCB_SIZE_HINT_BASE_SIZE)
722 && c->size_hints.width_inc && c->size_hints.height_inc)
724 uint16_t t1 = geometry.width, t2 = geometry.height;
725 unsigned_subtract(t1, basew);
726 unsigned_subtract(t2, baseh);
727 geometry.width -= t1 % c->size_hints.width_inc;
728 geometry.height -= t2 % c->size_hints.height_inc;
731 return geometry;
734 /** Resize client window.
735 * The sizse given as parameters are with titlebar and borders!
736 * \param c Client to resize.
737 * \param geometry New window geometry.
738 * \param hints Use size hints.
740 void
741 client_resize(client_t *c, area_t geometry, bool hints)
743 int new_screen;
744 area_t geometry_internal;
745 area_t area;
747 /* offscreen appearance fixes */
748 area = display_area_get(c->phys_screen, NULL,
749 &globalconf.screens[c->screen].padding);
751 if(geometry.x > area.width)
752 geometry.x = area.width - geometry.width;
753 if(geometry.y > area.height)
754 geometry.y = area.height - geometry.height;
755 if(geometry.x + geometry.width < 0)
756 geometry.x = 0;
757 if(geometry.y + geometry.height < 0)
758 geometry.y = 0;
760 /* Real client geometry, please keep it contained to C code at the very least. */
761 geometry_internal = titlebar_geometry_remove(c->titlebar, c->border, geometry);
763 if(hints)
764 geometry_internal = client_geometry_hints(c, geometry_internal);
766 if(geometry_internal.width == 0 || geometry_internal.height == 0)
767 return;
769 /* Also let client hints propegate to the "official" geometry. */
770 geometry = titlebar_geometry_add(c->titlebar, c->border, geometry_internal);
772 if(c->geometries.internal.x != geometry_internal.x
773 || c->geometries.internal.y != geometry_internal.y
774 || c->geometries.internal.width != geometry_internal.width
775 || c->geometries.internal.height != geometry_internal.height)
777 new_screen = screen_getbycoord(c->screen, geometry_internal.x, geometry_internal.y);
779 /* Values to configure a window is an array where values are
780 * stored according to 'value_mask' */
781 uint32_t values[4];
783 c->geometries.internal.x = values[0] = geometry_internal.x;
784 c->geometries.internal.y = values[1] = geometry_internal.y;
785 c->geometries.internal.width = values[2] = geometry_internal.width;
786 c->geometries.internal.height = values[3] = geometry_internal.height;
788 /* Also store geometry including border and titlebar. */
789 c->geometry = geometry;
791 titlebar_update_geometry(c);
793 /* The idea is to give a client a resize even when banned. */
794 /* We just have to move the (x,y) to keep it out of the viewport. */
795 /* This at least doesn't break expectations about events. */
796 if (c->isbanned)
798 geometry.x = values[0] = - (geometry_internal.width + 2 * c->border);
799 geometry.y = values[1] = - (geometry_internal.height + 2 * c->border);
802 xcb_configure_window(globalconf.connection, c->win,
803 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y
804 | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
805 values);
806 window_configure(c->win, geometry_internal, c->border);
808 screen_client_moveto(c, new_screen, true, false);
810 /* execute hook */
811 hooks_property(c, "geometry");
815 /** Set a client minimized, or not.
816 * \param c The client.
817 * \param s Set or not the client minimized.
819 void
820 client_setminimized(client_t *c, bool s)
822 if(c->isminimized != s)
824 client_need_arrange(c);
825 c->isminimized = s;
826 client_need_arrange(c);
827 ewmh_client_update_hints(c);
828 /* execute hook */
829 hooks_property(c, "minimized");
833 /** Set a client sticky, or not.
834 * \param c The client.
835 * \param s Set or not the client sticky.
837 void
838 client_setsticky(client_t *c, bool s)
840 if(c->issticky != s)
842 client_need_arrange(c);
843 c->issticky = s;
844 client_need_arrange(c);
845 ewmh_client_update_hints(c);
846 hooks_property(c, "sticky");
850 /** Set a client fullscreen, or not.
851 * \param c The client.
852 * \param s Set or not the client fullscreen.
854 void
855 client_setfullscreen(client_t *c, bool s)
857 if(c->isfullscreen != s)
859 area_t geometry;
861 /* become fullscreen! */
862 if((c->isfullscreen = s))
864 /* remove any max state */
865 client_setmaxhoriz(c, false);
866 client_setmaxvert(c, false);
868 geometry = screen_area_get(c->screen, NULL, NULL, false);
869 c->geometries.fullscreen = c->geometry;
870 c->border_fs = c->border;
871 client_setborder(c, 0);
873 else
875 geometry = c->geometries.fullscreen;
876 client_setborder(c, c->border_fs);
878 client_resize(c, geometry, false);
879 client_need_arrange(c);
880 client_stack();
881 xcb_change_property(globalconf.connection,
882 XCB_PROP_MODE_REPLACE,
883 c->win, _AWESOME_FULLSCREEN, CARDINAL, 8, 1,
884 &c->isfullscreen);
885 ewmh_client_update_hints(c);
886 hooks_property(c, "fullscreen");
890 /** Set a client horizontally maximized.
891 * \param c The client.
892 * \param s The maximized status.
894 void
895 client_setmaxhoriz(client_t *c, bool s)
897 if(c->ismaxhoriz != s)
899 area_t geometry;
901 if((c->ismaxhoriz = s))
903 /* remove fullscreen mode */
904 client_setfullscreen(c, false);
906 geometry = screen_area_get(c->screen,
907 &globalconf.screens[c->screen].wiboxes,
908 &globalconf.screens[c->screen].padding,
909 true);
910 geometry.y = c->geometry.y;
911 geometry.height = c->geometry.height;
912 c->geometries.max.x = c->geometry.x;
913 c->geometries.max.width = c->geometry.width;
915 else
917 geometry = c->geometry;
918 geometry.x = c->geometries.max.x;
919 geometry.width = c->geometries.max.width;
922 client_resize(c, geometry, c->size_hints_honor);
923 client_need_arrange(c);
924 client_stack();
925 ewmh_client_update_hints(c);
926 hooks_property(c, "maximized_horizontal");
930 /** Set a client vertically maximized.
931 * \param c The client.
932 * \param s The maximized status.
934 void
935 client_setmaxvert(client_t *c, bool s)
937 if(c->ismaxvert != s)
939 area_t geometry;
941 if((c->ismaxvert = s))
943 /* remove fullscreen mode */
944 client_setfullscreen(c, false);
946 geometry = screen_area_get(c->screen,
947 &globalconf.screens[c->screen].wiboxes,
948 &globalconf.screens[c->screen].padding,
949 true);
950 geometry.x = c->geometry.x;
951 geometry.width = c->geometry.width;
952 c->geometries.max.y = c->geometry.y;
953 c->geometries.max.height = c->geometry.height;
955 else
957 geometry = c->geometry;
958 geometry.y = c->geometries.max.y;
959 geometry.height = c->geometries.max.height;
962 client_resize(c, geometry, c->size_hints_honor);
963 client_need_arrange(c);
964 client_stack();
965 ewmh_client_update_hints(c);
966 hooks_property(c, "maximized_vertical");
970 /** Set a client above, or not.
971 * \param c The client.
972 * \param s Set or not the client above.
974 void
975 client_setabove(client_t *c, bool s)
977 if(c->isabove != s)
979 c->isabove = s;
980 client_stack();
981 ewmh_client_update_hints(c);
982 /* execute hook */
983 hooks_property(c, "above");
987 /** Set a client below, or not.
988 * \param c The client.
989 * \param s Set or not the client below.
991 void
992 client_setbelow(client_t *c, bool s)
994 if(c->isbelow != s)
996 c->isbelow = s;
997 client_stack();
998 ewmh_client_update_hints(c);
999 /* execute hook */
1000 hooks_property(c, "below");
1004 /** Set a client modal, or not.
1005 * \param c The client.
1006 * \param s Set or not the client moda.
1008 void
1009 client_setmodal(client_t *c, bool s)
1011 if(c->ismodal != s)
1013 c->ismodal = s;
1014 client_stack();
1015 ewmh_client_update_hints(c);
1016 /* execute hook */
1017 hooks_property(c, "modal");
1021 /** Set a client ontop, or not.
1022 * \param c The client.
1023 * \param s Set or not the client moda.
1025 void
1026 client_setontop(client_t *c, bool s)
1028 if(c->isontop != s)
1030 c->isontop = s;
1031 client_stack();
1032 /* execute hook */
1033 hooks_property(c, "ontop");
1037 /** Save client properties as an X property.
1038 * \param c The client.
1040 void
1041 client_saveprops_tags(client_t *c)
1043 tag_array_t *tags = &globalconf.screens[c->screen].tags;
1044 unsigned char *prop = p_alloca(unsigned char, tags->len + 1);
1045 int i;
1047 for(i = 0; i < tags->len; i++)
1048 prop[i] = is_client_tagged(c, tags->tab[i]) ? '1' : '0';
1050 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE, c->win, _AWESOME_TAGS, STRING, 8, i, prop);
1053 /** Unban a client and move it back into the viewport.
1054 * \param c The client.
1056 void
1057 client_unban(client_t *c)
1059 if(c->isbanned)
1061 /* Move the client back where it belongs. */
1062 uint32_t request[2] = { c->geometries.internal.x, c->geometries.internal.y };
1064 xcb_configure_window(globalconf.connection, c->win,
1065 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
1066 request);
1067 window_configure(c->win, c->geometries.internal, c->border);
1069 /* Do this manually because the system doesn't know we moved the toolbar.
1070 * Note that !isvisible titlebars are unmapped and for fullscreen it'll
1071 * end up offscreen anyway. */
1072 if(c->titlebar)
1074 simple_window_t *sw = &c->titlebar->sw;
1075 /* All resizing is done, so only move now. */
1076 request[0] = sw->geometry.x;
1077 request[1] = sw->geometry.y;
1079 xcb_configure_window(globalconf.connection, sw->window,
1080 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
1081 request);
1084 c->isbanned = false;
1086 /* All the wiboxes (may) need to be repositioned. */
1087 if(client_hasstrut(c))
1088 wibox_update_positions();
1092 /** Unmanage a client.
1093 * \param c The client.
1095 void
1096 client_unmanage(client_t *c)
1098 tag_array_t *tags = &globalconf.screens[c->screen].tags;
1100 if(globalconf.screens[c->phys_screen].client_focus == c)
1101 client_unfocus(c);
1103 /* remove client everywhere */
1104 client_list_detach(&globalconf.clients, c);
1105 stack_client_delete(c);
1106 for(int i = 0; i < tags->len; i++)
1107 untag_client(c, tags->tab[i]);
1109 /* call hook */
1110 if(globalconf.hooks.unmanage != LUA_REFNIL)
1112 luaA_client_userdata_new(globalconf.L, c);
1113 luaA_dofunction(globalconf.L, globalconf.hooks.unmanage, 1, 0);
1116 /* Call hook to notify list change */
1117 if(globalconf.hooks.clients != LUA_REFNIL)
1118 luaA_dofunction(globalconf.L, globalconf.hooks.clients, 0, 0);
1120 /* The server grab construct avoids race conditions. */
1121 xcb_grab_server(globalconf.connection);
1123 xcb_ungrab_button(globalconf.connection, XCB_BUTTON_INDEX_ANY, c->win,
1124 XCB_BUTTON_MASK_ANY);
1125 window_state_set(c->win, XCB_WM_STATE_WITHDRAWN);
1127 xcb_flush(globalconf.connection);
1128 xcb_ungrab_server(globalconf.connection);
1130 titlebar_client_detach(c);
1132 ewmh_update_net_client_list(c->phys_screen);
1134 /* delete properties */
1135 xcb_delete_property(globalconf.connection, c->win, _AWESOME_TAGS);
1136 xcb_delete_property(globalconf.connection, c->win, _AWESOME_FLOATING);
1138 /* All the wiboxes (may) need to be repositioned. */
1139 if(client_hasstrut(c))
1140 wibox_update_positions();
1142 /* set client as invalid */
1143 c->invalid = true;
1145 client_unref(&c);
1148 /** Kill a client via a WM_DELETE_WINDOW request or KillClient if not
1149 * supported.
1150 * \param c The client to kill.
1152 void
1153 client_kill(client_t *c)
1155 if(window_hasproto(c->win, WM_DELETE_WINDOW))
1157 xcb_client_message_event_t ev;
1159 /* Initialize all of event's fields first */
1160 p_clear(&ev, 1);
1162 ev.response_type = XCB_CLIENT_MESSAGE;
1163 ev.window = c->win;
1164 ev.format = 32;
1165 ev.data.data32[1] = XCB_CURRENT_TIME;
1166 ev.type = WM_PROTOCOLS;
1167 ev.data.data32[0] = WM_DELETE_WINDOW;
1169 xcb_send_event(globalconf.connection, false, c->win,
1170 XCB_EVENT_MASK_NO_EVENT, (char *) &ev);
1172 else
1173 xcb_kill_client(globalconf.connection, c->win);
1176 /** Get all clients into a table.
1177 * \param L The Lua VM state.
1178 * \return The number of elements pushed on stack.
1179 * \luastack
1180 * \lparam An optional screen nunmber.
1181 * \lreturn A table with all clients.
1183 static int
1184 luaA_client_get(lua_State *L)
1186 int i = 1, screen;
1187 client_t *c;
1189 screen = luaL_optnumber(L, 1, 0) - 1;
1191 lua_newtable(L);
1193 if(screen == SCREEN_UNDEF)
1194 for(c = globalconf.clients; c; c = c->next)
1196 luaA_client_userdata_new(globalconf.L, c);
1197 lua_rawseti(L, -2, i++);
1199 else
1201 luaA_checkscreen(screen);
1202 for(c = globalconf.clients; c; c = c->next)
1203 if(c->screen == screen)
1205 luaA_client_userdata_new(globalconf.L, c);
1206 lua_rawseti(L, -2, i++);
1210 return 1;
1213 /** Check if a client is visible on its screen.
1214 * \param L The Lua VM state.
1215 * \return The number of elements pushed on stack.
1216 * \luastack
1217 * \lvalue A client.
1218 * \lreturn A boolean value, true if the client is visible, false otherwise.
1220 static int
1221 luaA_client_isvisible(lua_State *L)
1223 client_t **c = luaA_checkudata(L, 1, "client");
1224 lua_pushboolean(L, client_isvisible(*c, (*c)->screen));
1225 return 1;
1228 /** Set client border width.
1229 * \param c The client.
1230 * \param width The border width.
1232 void
1233 client_setborder(client_t *c, int width)
1235 uint32_t w = width;
1237 if(width > 0 && (c->type == WINDOW_TYPE_DOCK
1238 || c->type == WINDOW_TYPE_SPLASH
1239 || c->type == WINDOW_TYPE_DESKTOP
1240 || c->isfullscreen))
1241 return;
1243 if(width == c->border || width < 0)
1244 return;
1246 c->border = width;
1247 xcb_configure_window(globalconf.connection, c->win,
1248 XCB_CONFIG_WINDOW_BORDER_WIDTH, &w);
1250 client_need_arrange(c);
1252 hooks_property(c, "border_width");
1255 /** Kill a client.
1256 * \param L The Lua VM state.
1258 * \luastack
1259 * \lvalue A client.
1261 static int
1262 luaA_client_kill(lua_State *L)
1264 client_t **c = luaA_checkudata(L, 1, "client");
1265 client_kill(*c);
1266 return 0;
1269 /** Swap a client with another one.
1270 * \param L The Lua VM state.
1271 * \luastack
1272 * \lvalue A client.
1273 * \lparam A client to swap with.
1275 static int
1276 luaA_client_swap(lua_State *L)
1278 client_t **c = luaA_checkudata(L, 1, "client");
1279 client_t **swap = luaA_checkudata(L, 2, "client");
1281 if(*c != *swap)
1283 client_list_swap(&globalconf.clients, *swap, *c);
1284 client_need_arrange(*c);
1285 client_need_arrange(*swap);
1287 /* Call hook to notify list change */
1288 if(globalconf.hooks.clients != LUA_REFNIL)
1289 luaA_dofunction(L, globalconf.hooks.clients, 0, 0);
1292 return 0;
1295 /** Access or set the client tags.
1296 * \param L The Lua VM state.
1297 * \return The number of elements pushed on stack.
1298 * \lparam A table with tags to set, or none to get the current tags table.
1299 * \return The clients tag.
1301 static int
1302 luaA_client_tags(lua_State *L)
1304 tag_array_t *tags;
1305 tag_t **tag;
1306 client_t **c = luaA_checkudata(L, 1, "client");
1307 int j = 0;
1309 if(lua_gettop(L) == 2)
1311 luaA_checktable(L, 2);
1312 tags = &globalconf.screens[(*c)->screen].tags;
1313 for(int i = 0; i < tags->len; i++)
1314 untag_client(*c, tags->tab[i]);
1315 lua_pushnil(L);
1316 while(lua_next(L, 2))
1318 tag = luaA_checkudata(L, -1, "tag");
1319 tag_client(*c, *tag);
1320 lua_pop(L, 1);
1322 lua_pop(L, 1);
1325 tags = &globalconf.screens[(*c)->screen].tags;
1326 luaA_otable_new(L);
1327 for(int i = 0; i < tags->len; i++)
1328 if(is_client_tagged(*c, tags->tab[i]))
1330 luaA_tag_userdata_new(L, tags->tab[i]);
1331 lua_rawseti(L, -2, ++j);
1334 return 1;
1337 /** Raise a client on top of others which are on the same layer.
1338 * \param L The Lua VM state.
1339 * \luastack
1340 * \lvalue A client.
1342 static int
1343 luaA_client_raise(lua_State *L)
1345 client_t **c = luaA_checkudata(L, 1, "client");
1346 client_raise(*c);
1347 return 0;
1350 /** Lower a client on bottom of others which are on the same layer.
1351 * \param L The Lua VM state.
1352 * \luastack
1353 * \lvalue A client.
1355 static int
1356 luaA_client_lower(lua_State *L)
1358 client_t **c = luaA_checkudata(L, 1, "client");
1359 client_lower(*c);
1360 return 0;
1363 /** Redraw a client by unmapping and mapping it quickly.
1364 * \param L The Lua VM state.
1366 * \luastack
1367 * \lvalue A client.
1369 static int
1370 luaA_client_redraw(lua_State *L)
1372 client_t **c = luaA_checkudata(L, 1, "client");
1374 xcb_unmap_window(globalconf.connection, (*c)->win);
1375 xcb_map_window(globalconf.connection, (*c)->win);
1377 /* Set the focus on the current window if the redraw has been
1378 performed on the window where the pointer is currently on
1379 because after the unmapping/mapping, the focus is lost */
1380 if(globalconf.screen_focus->client_focus == *c)
1381 xcb_set_input_focus(globalconf.connection, XCB_INPUT_FOCUS_POINTER_ROOT,
1382 (*c)->win, XCB_CURRENT_TIME);
1384 return 0;
1387 /** Stop managing a client.
1388 * \param L The Lua VM state.
1389 * \return The number of elements pushed on stack.
1390 * \luastack
1391 * \lvalue A client.
1393 static int
1394 luaA_client_unmanage(lua_State *L)
1396 client_t **c = luaA_checkudata(L, 1, "client");
1397 client_unmanage(*c);
1398 return 0;
1401 /** Return client geometry.
1402 * \param L The Lua VM state.
1403 * \return The number of elements pushed on stack.
1404 * \luastack
1405 * \lparam A table with new coordinates, or none.
1406 * \lreturn A table with client coordinates.
1408 static int
1409 luaA_client_geometry(lua_State *L)
1411 client_t **c = luaA_checkudata(L, 1, "client");
1413 if(lua_gettop(L) == 2)
1415 area_t geometry;
1417 luaA_checktable(L, 2);
1418 geometry.x = luaA_getopt_number(L, 2, "x", (*c)->geometry.x);
1419 geometry.y = luaA_getopt_number(L, 2, "y", (*c)->geometry.y);
1420 if(client_isfixed(*c))
1422 geometry.width = (*c)->geometry.width;
1423 geometry.height = (*c)->geometry.height;
1425 else
1427 geometry.width = luaA_getopt_number(L, 2, "width", (*c)->geometry.width);
1428 geometry.height = luaA_getopt_number(L, 2, "height", (*c)->geometry.height);
1431 client_resize(*c, geometry, (*c)->size_hints_honor);
1434 return luaA_pusharea(L, (*c)->geometry);
1437 /** Client newindex.
1438 * \param L The Lua VM state.
1439 * \return The number of elements pushed on stack.
1442 luaA_client_newindex(lua_State *L)
1444 size_t len;
1445 client_t **c = luaA_checkudata(L, 1, "client");
1446 const char *buf = luaL_checklstring(L, 2, &len);
1447 bool b;
1448 double d;
1449 int i;
1450 wibox_t **t = NULL;
1451 image_t **image;
1453 if((*c)->invalid)
1454 luaL_error(L, "client is invalid\n");
1456 switch(a_tokenize(buf, len))
1458 case A_TK_SCREEN:
1459 if(globalconf.xinerama_is_active)
1461 i = luaL_checknumber(L, 3) - 1;
1462 luaA_checkscreen(i);
1463 screen_client_moveto(*c, i, true, true);
1465 break;
1466 case A_TK_HIDE:
1467 b = luaA_checkboolean(L, 3);
1468 if(b != (*c)->ishidden)
1470 client_need_arrange(*c);
1471 (*c)->ishidden = b;
1472 client_need_arrange(*c);
1474 break;
1475 case A_TK_MINIMIZE:
1476 luaA_deprecate(L, "client.minimized");
1477 case A_TK_MINIMIZED:
1478 client_setminimized(*c, luaA_checkboolean(L, 3));
1479 break;
1480 case A_TK_FULLSCREEN:
1481 client_setfullscreen(*c, luaA_checkboolean(L, 3));
1482 break;
1483 case A_TK_MAXIMIZED_HORIZONTAL:
1484 client_setmaxhoriz(*c, luaA_checkboolean(L, 3));
1485 break;
1486 case A_TK_MAXIMIZED_VERTICAL:
1487 client_setmaxvert(*c, luaA_checkboolean(L, 3));
1488 break;
1489 case A_TK_ICON:
1490 image = luaA_checkudata(L, 3, "image");
1491 image_unref(&(*c)->icon);
1492 image_ref(image);
1493 (*c)->icon = *image;
1494 /* execute hook */
1495 hooks_property(*c, "icon");
1496 break;
1497 case A_TK_OPACITY:
1498 if(lua_isnil(L, 3))
1499 window_opacity_set((*c)->win, -1);
1500 else
1502 d = luaL_checknumber(L, 3);
1503 if(d >= 0 && d <= 1)
1504 window_opacity_set((*c)->win, d);
1506 break;
1507 case A_TK_STICKY:
1508 client_setsticky(*c, luaA_checkboolean(L, 3));
1509 break;
1510 case A_TK_HONORSIZEHINTS:
1511 luaA_deprecate(L, "size_hints_honor");
1512 case A_TK_SIZE_HINTS_HONOR:
1513 (*c)->size_hints_honor = luaA_checkboolean(L, 3);
1514 client_need_arrange(*c);
1515 break;
1516 case A_TK_BORDER_WIDTH:
1517 client_setborder(*c, luaL_checknumber(L, 3));
1518 break;
1519 case A_TK_ONTOP:
1520 client_setontop(*c, luaA_checkboolean(L, 3));
1521 break;
1522 case A_TK_BORDER_COLOR:
1523 if((buf = luaL_checklstring(L, 3, &len))
1524 && xcolor_init_reply(xcolor_init_unchecked(&(*c)->border_color, buf, len)))
1525 xcb_change_window_attributes(globalconf.connection, (*c)->win,
1526 XCB_CW_BORDER_PIXEL, &(*c)->border_color.pixel);
1527 break;
1528 case A_TK_TITLEBAR:
1529 if(lua_isnil(L, 3))
1530 titlebar_client_detach(*c);
1531 else
1533 t = luaA_checkudata(L, 3, "wibox");
1534 titlebar_client_attach(*c, *t);
1536 break;
1537 default:
1538 return 0;
1541 return 0;
1544 /** Client object.
1545 * \param L The Lua VM state.
1546 * \return The number of elements pushed on stack.
1547 * \luastack
1548 * \lfield name The client title.
1549 * \lfield skip_taskbar True if the client does not want to be in taskbar.
1550 * \lfield type The window type (desktop, normal, dock, …).
1551 * \lfield class The client class.
1552 * \lfield instance The client instance.
1553 * \lfield pid The client PID, if available.
1554 * \lfield role The window role, if available.
1555 * \lfield machine The machine client is running on.
1556 * \lfield icon_name The client name when iconified.
1557 * \lfield screen Client screen number.
1558 * \lfield hide Define if the client must be hidden, i.e. never mapped,
1559 * invisible in taskbar.
1560 * \lfield minimize Define it the client must be iconify, i.e. only visible in
1561 * taskbar.
1562 * \lfield icon_path Path to the icon used to identify.
1563 * \lfield size_hints_honor Honor size hints, i.e. respect size ratio.
1564 * \lfield border_width The client border width.
1565 * \lfield border_color The client border color.
1566 * \lfield titlebar The client titlebar.
1567 * \lfield urgent The client urgent state.
1568 * \lfield content An image representing the client window content (screenshot).
1569 * \lfield focus The focused client.
1570 * \lfield opacity The client opacity between 0 and 1.
1571 * \lfield ontop The client is on top of every other windows.
1572 * \lfield fullscreen The client is fullscreen or not.
1573 * \lfield maximized_horizontal The client is maximized horizontally or not.
1574 * \lfield maximized_vertical The client is maximized vertically or not.
1575 * \lfield transient_for Return the client the window is transient for.
1576 * \lfield group_id Identification unique to a group of windows.
1577 * \lfield leader_id Identification unique to windows spawned by the same command.
1578 * \lfield size_hints A table with size hints of the client: user_position,
1579 * user_size, program_position, program_size, etc.
1581 static int
1582 luaA_client_index(lua_State *L)
1584 size_t len;
1585 ssize_t slen;
1586 client_t **c = luaA_checkudata(L, 1, "client");
1587 const char *buf = luaL_checklstring(L, 2, &len);
1588 char *value;
1589 void *data;
1590 xcb_get_wm_class_reply_t hint;
1591 xcb_get_property_cookie_t prop_c;
1592 xcb_get_property_reply_t *prop_r = NULL;
1593 double d;
1595 if((*c)->invalid)
1596 luaL_error(L, "client is invalid\n");
1598 if(luaA_usemetatable(L, 1, 2))
1599 return 1;
1601 switch(a_tokenize(buf, len))
1603 image_t *image;
1605 case A_TK_NAME:
1606 lua_pushstring(L, (*c)->name);
1607 break;
1608 case A_TK_TRANSIENT_FOR:
1609 if((*c)->transient_for)
1610 return luaA_client_userdata_new(L, (*c)->transient_for);
1611 return 0;
1612 case A_TK_SKIP_TASKBAR:
1613 lua_pushboolean(L, (*c)->skiptb);
1614 break;
1615 case A_TK_CONTENT:
1616 if((image = client_getcontent(*c)))
1617 return luaA_image_userdata_new(L, image);
1618 return 0;
1619 case A_TK_TYPE:
1620 switch((*c)->type)
1622 case WINDOW_TYPE_DESKTOP:
1623 lua_pushliteral(L, "desktop");
1624 break;
1625 case WINDOW_TYPE_DOCK:
1626 lua_pushliteral(L, "dock");
1627 break;
1628 case WINDOW_TYPE_SPLASH:
1629 lua_pushliteral(L, "splash");
1630 break;
1631 case WINDOW_TYPE_DIALOG:
1632 lua_pushliteral(L, "dialog");
1633 break;
1634 case WINDOW_TYPE_MENU:
1635 lua_pushliteral(L, "menu");
1636 break;
1637 case WINDOW_TYPE_TOOLBAR:
1638 lua_pushliteral(L, "toolbar");
1639 break;
1640 case WINDOW_TYPE_UTILITY:
1641 lua_pushliteral(L, "utility");
1642 break;
1643 default:
1644 lua_pushliteral(L, "normal");
1645 break;
1647 break;
1648 case A_TK_CLASS:
1649 if(!xcb_get_wm_class_reply(globalconf.connection,
1650 xcb_get_wm_class_unchecked(globalconf.connection, (*c)->win),
1651 &hint, NULL))
1652 return 0;
1653 lua_pushstring(L, hint.class);
1654 xcb_get_wm_class_reply_wipe(&hint);
1655 break;
1656 case A_TK_INSTANCE:
1657 if(!xcb_get_wm_class_reply(globalconf.connection,
1658 xcb_get_wm_class_unchecked(globalconf.connection, (*c)->win),
1659 &hint, NULL))
1660 return 0;
1661 lua_pushstring(L, hint.name);
1662 xcb_get_wm_class_reply_wipe(&hint);
1663 break;
1664 case A_TK_ROLE:
1665 if(!xutil_text_prop_get(globalconf.connection, (*c)->win,
1666 WM_WINDOW_ROLE, &value, &slen))
1667 return 0;
1668 lua_pushlstring(L, value, slen);
1669 p_delete(&value);
1670 break;
1671 case A_TK_PID:
1672 prop_c = xcb_get_property_unchecked(globalconf.connection, false, (*c)->win, _NET_WM_PID, CARDINAL, 0L, 1L);
1673 prop_r = xcb_get_property_reply(globalconf.connection, prop_c, NULL);
1675 if(prop_r && prop_r->value_len && (data = xcb_get_property_value(prop_r)))
1676 lua_pushnumber(L, *(uint32_t *)data);
1677 else
1679 p_delete(&prop_r);
1680 return 0;
1682 break;
1683 case A_TK_LEADER_ID:
1684 lua_pushnumber(L, (*c)->leader_win);
1685 break;
1686 case A_TK_MACHINE:
1687 if(!xutil_text_prop_get(globalconf.connection, (*c)->win,
1688 WM_CLIENT_MACHINE, &value, &slen))
1689 return 0;
1690 lua_pushlstring(L, value, slen);
1691 p_delete(&value);
1692 break;
1693 case A_TK_ICON_NAME:
1694 lua_pushstring(L, (*c)->icon_name);
1695 break;
1696 case A_TK_SCREEN:
1697 lua_pushnumber(L, 1 + (*c)->screen);
1698 break;
1699 case A_TK_HIDE:
1700 lua_pushboolean(L, (*c)->ishidden);
1701 break;
1702 case A_TK_MINIMIZE:
1703 luaA_deprecate(L, "client.minimized");
1704 case A_TK_MINIMIZED:
1705 lua_pushboolean(L, (*c)->isminimized);
1706 break;
1707 case A_TK_FULLSCREEN:
1708 lua_pushboolean(L, (*c)->isfullscreen);
1709 break;
1710 case A_TK_GROUP_ID:
1711 if((*c)->group_win)
1712 lua_pushnumber(L, (*c)->group_win);
1713 else
1714 return 0;
1715 break;
1716 case A_TK_MAXIMIZED_HORIZONTAL:
1717 lua_pushboolean(L, (*c)->ismaxhoriz);
1718 break;
1719 case A_TK_MAXIMIZED_VERTICAL:
1720 lua_pushboolean(L, (*c)->ismaxvert);
1721 break;
1722 case A_TK_ICON:
1723 if((*c)->icon)
1724 luaA_image_userdata_new(L, (*c)->icon);
1725 else
1726 return 0;
1727 break;
1728 case A_TK_OPACITY:
1729 if((d = window_opacity_get((*c)->win)) >= 0)
1730 lua_pushnumber(L, d);
1731 else
1732 return 0;
1733 break;
1734 case A_TK_ONTOP:
1735 lua_pushboolean(L, (*c)->isontop);
1736 break;
1737 case A_TK_STICKY:
1738 lua_pushboolean(L, (*c)->issticky);
1739 break;
1740 case A_TK_HONORSIZEHINTS:
1741 luaA_deprecate(L, "size_hints_honor");
1742 case A_TK_SIZE_HINTS_HONOR:
1743 lua_pushboolean(L, (*c)->size_hints_honor);
1744 break;
1745 case A_TK_BORDER_WIDTH:
1746 lua_pushnumber(L, (*c)->border);
1747 break;
1748 case A_TK_BORDER_COLOR:
1749 luaA_pushcolor(L, &(*c)->border_color);
1750 break;
1751 case A_TK_TITLEBAR:
1752 if((*c)->titlebar)
1753 return luaA_wibox_userdata_new(L, (*c)->titlebar);
1754 return 0;
1755 case A_TK_URGENT:
1756 lua_pushboolean(L, (*c)->isurgent);
1757 break;
1758 case A_TK_SIZE_HINTS:
1760 const char *u_or_p = NULL;
1762 lua_newtable(L);
1764 if((*c)->size_hints.flags & XCB_SIZE_HINT_US_POSITION)
1765 u_or_p = "user_position";
1766 else if((*c)->size_hints.flags & XCB_SIZE_HINT_P_POSITION)
1767 u_or_p = "program_position";
1769 if(u_or_p)
1771 lua_newtable(L);
1772 lua_pushnumber(L, (*c)->size_hints.x);
1773 lua_setfield(L, -2, "x");
1774 lua_pushnumber(L, (*c)->size_hints.y);
1775 lua_setfield(L, -2, "y");
1776 lua_setfield(L, -2, u_or_p);
1777 u_or_p = NULL;
1780 if((*c)->size_hints.flags & XCB_SIZE_HINT_US_SIZE)
1781 u_or_p = "user_size";
1782 else if((*c)->size_hints.flags & XCB_SIZE_HINT_P_SIZE)
1783 u_or_p = "program_size";
1785 if(u_or_p)
1787 lua_newtable(L);
1788 lua_pushnumber(L, (*c)->size_hints.width);
1789 lua_setfield(L, -2, "width");
1790 lua_pushnumber(L, (*c)->size_hints.height);
1791 lua_setfield(L, -2, "height");
1792 lua_setfield(L, -2, u_or_p);
1795 if((*c)->size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE)
1797 lua_pushnumber(L, (*c)->size_hints.min_width);
1798 lua_setfield(L, -2, "min_width");
1799 lua_pushnumber(L, (*c)->size_hints.min_height);
1800 lua_setfield(L, -2, "min_height");
1803 if((*c)->size_hints.flags & XCB_SIZE_HINT_P_MAX_SIZE)
1805 lua_pushnumber(L, (*c)->size_hints.max_width);
1806 lua_setfield(L, -2, "max_width");
1807 lua_pushnumber(L, (*c)->size_hints.max_height);
1808 lua_setfield(L, -2, "max_height");
1811 if((*c)->size_hints.flags & XCB_SIZE_HINT_P_RESIZE_INC)
1813 lua_pushnumber(L, (*c)->size_hints.width_inc);
1814 lua_setfield(L, -2, "width_inc");
1815 lua_pushnumber(L, (*c)->size_hints.height_inc);
1816 lua_setfield(L, -2, "height_inc");
1819 if((*c)->size_hints.flags & XCB_SIZE_HINT_P_ASPECT)
1821 lua_pushnumber(L, (*c)->size_hints.min_aspect_num);
1822 lua_setfield(L, -2, "min_aspect_num");
1823 lua_pushnumber(L, (*c)->size_hints.min_aspect_den);
1824 lua_setfield(L, -2, "min_aspect_den");
1825 lua_pushnumber(L, (*c)->size_hints.max_aspect_num);
1826 lua_setfield(L, -2, "max_aspect_num");
1827 lua_pushnumber(L, (*c)->size_hints.max_aspect_den);
1828 lua_setfield(L, -2, "max_aspect_den");
1831 if((*c)->size_hints.flags & XCB_SIZE_HINT_BASE_SIZE)
1833 lua_pushnumber(L, (*c)->size_hints.base_width);
1834 lua_setfield(L, -2, "base_width");
1835 lua_pushnumber(L, (*c)->size_hints.base_height);
1836 lua_setfield(L, -2, "base_height");
1839 if((*c)->size_hints.flags & XCB_SIZE_HINT_P_WIN_GRAVITY)
1841 switch((*c)->size_hints.win_gravity)
1843 default:
1844 lua_pushliteral(L, "north_west");
1845 break;
1846 case XCB_GRAVITY_NORTH:
1847 lua_pushliteral(L, "north");
1848 break;
1849 case XCB_GRAVITY_NORTH_EAST:
1850 lua_pushliteral(L, "north_east");
1851 break;
1852 case XCB_GRAVITY_WEST:
1853 lua_pushliteral(L, "west");
1854 break;
1855 case XCB_GRAVITY_CENTER:
1856 lua_pushliteral(L, "center");
1857 break;
1858 case XCB_GRAVITY_EAST:
1859 lua_pushliteral(L, "east");
1860 break;
1861 case XCB_GRAVITY_SOUTH_WEST:
1862 lua_pushliteral(L, "south_west");
1863 break;
1864 case XCB_GRAVITY_SOUTH:
1865 lua_pushliteral(L, "south");
1866 break;
1867 case XCB_GRAVITY_SOUTH_EAST:
1868 lua_pushliteral(L, "south_east");
1869 break;
1870 case XCB_GRAVITY_STATIC:
1871 lua_pushliteral(L, "static");
1872 break;
1874 lua_setfield(L, -2, "win_gravity");
1877 break;
1878 default:
1879 return 0;
1882 return 1;
1885 /** Get or set mouse buttons bindings for a client.
1886 * \param L The Lua VM state.
1887 * \return The number of element pushed on stack.
1888 * \luastack
1889 * \lvalue A client.
1890 * \lparam An array of mouse button bindings objects, or nothing.
1891 * \return The array of mouse button bindings objects of this client.
1893 static int
1894 luaA_client_buttons(lua_State *L)
1896 client_t **client = luaA_checkudata(L, 1, "client");
1897 button_array_t *buttons = &(*client)->buttons;
1899 if(lua_gettop(L) == 2)
1900 luaA_button_array_set(L, 2, buttons);
1902 return luaA_button_array_get(L, buttons);
1905 /** Get or set keys bindings for a client.
1906 * \param L The Lua VM state.
1907 * \return The number of element pushed on stack.
1908 * \luastack
1909 * \lvalue A client.
1910 * \lparam An array of key bindings objects, or nothing.
1911 * \return The array of key bindings objects of this client.
1913 static int
1914 luaA_client_keys(lua_State *L)
1916 client_t **c = luaA_checkudata(L, 1, "client");
1917 keybindings_t *keys = &(*c)->keys;
1919 if(lua_gettop(L) == 2)
1921 luaA_key_array_set(L, 2, keys);
1922 xcb_ungrab_key(globalconf.connection, XCB_GRAB_ANY, (*c)->win, XCB_BUTTON_MASK_ANY);
1923 window_grabkeys((*c)->win, keys);
1926 return luaA_key_array_get(L, keys);
1929 /** Send fake events to a client.
1930 * \param L The Lua VM state.
1931 * \return The number of element pushed on stack.
1932 * \luastack
1933 * \lvalue A client.
1934 * \param The event type: key_press, key_release, button_press, button_release
1935 * or motion_notify.
1936 * \param The detail: in case of a key event, this is the keycode to send, in
1937 * case of a button event this is the number of the button. In case of a motion
1938 * event, this is a boolean value which if true make the coordinates relatives.
1939 * \param In case of a motion event, this is the X coordinate.
1940 * \param In case of a motion event, this is the Y coordinate.
1942 static int
1943 luaA_client_fake_input(lua_State *L)
1945 if(!globalconf.have_xtest)
1947 luaA_warn(L, "XTest extension is not available, cannot fake input.");
1948 return 0;
1951 client_t **c = luaA_checkudata(L, 1, "client");
1952 size_t tlen;
1953 const char *stype = luaL_checklstring(L, 2, &tlen);
1954 uint8_t type, detail;
1955 int x = 0, y = 0;
1957 switch(a_tokenize(stype, tlen))
1959 case A_TK_KEY_PRESS:
1960 type = XCB_KEY_PRESS;
1961 detail = luaL_checknumber(L, 3); /* keycode */
1962 break;
1963 case A_TK_KEY_RELEASE:
1964 type = XCB_KEY_RELEASE;
1965 detail = luaL_checknumber(L, 3); /* keycode */
1966 break;
1967 case A_TK_BUTTON_PRESS:
1968 type = XCB_BUTTON_PRESS;
1969 detail = luaL_checknumber(L, 3); /* button number */
1970 break;
1971 case A_TK_BUTTON_RELEASE:
1972 type = XCB_BUTTON_RELEASE;
1973 detail = luaL_checknumber(L, 3); /* button number */
1974 break;
1975 case A_TK_MOTION_NOTIFY:
1976 type = XCB_MOTION_NOTIFY;
1977 detail = luaA_checkboolean(L, 3); /* relative to the current position or not */
1978 x = luaL_checknumber(L, 4);
1979 y = luaL_checknumber(L, 5);
1980 break;
1981 default:
1982 return 0;
1985 xcb_test_fake_input(globalconf.connection,
1986 type,
1987 detail,
1988 XCB_CURRENT_TIME,
1989 (*c)->win,
1990 x, y,
1992 return 0;
1995 /* Client module.
1996 * \param L The Lua VM state.
1997 * \return The number of pushed elements.
1999 static int
2000 luaA_client_module_index(lua_State *L)
2002 size_t len;
2003 const char *buf = luaL_checklstring(L, 2, &len);
2005 switch(a_tokenize(buf, len))
2007 case A_TK_FOCUS:
2008 if(globalconf.screen_focus->client_focus)
2009 luaA_client_userdata_new(L, globalconf.screen_focus->client_focus);
2010 else
2011 return 0;
2012 break;
2013 default:
2014 return 0;
2017 return 1;
2020 /* Client module new index.
2021 * \param L The Lua VM state.
2022 * \return The number of pushed elements.
2024 static int
2025 luaA_client_module_newindex(lua_State *L)
2027 size_t len;
2028 const char *buf = luaL_checklstring(L, 2, &len);
2029 client_t **c;
2031 switch(a_tokenize(buf, len))
2033 case A_TK_FOCUS:
2034 c = luaA_checkudata(L, 3, "client");
2035 client_focus(*c);
2036 break;
2037 default:
2038 break;
2041 return 0;
2044 /** Move a client with mouse (DEPRECATED).
2045 * \param L The Lua VM state.
2047 static int
2048 luaA_client_mouse_move(lua_State *L)
2050 luaA_deprecate(L, "awful.mouse.client.move()");
2051 return 0;
2054 /** Resize a client with mouse (DEPRECATED).
2055 * \param L The Lua VM state.
2056 * \return The number of pushed elements.
2058 static int
2059 luaA_client_mouse_resize(lua_State *L)
2061 luaA_deprecate(L, "awful.mouse.client.resize()");
2062 return 0;
2065 const struct luaL_reg awesome_client_methods[] =
2067 { "get", luaA_client_get },
2068 { "__index", luaA_client_module_index },
2069 { "__newindex", luaA_client_module_newindex },
2070 { NULL, NULL }
2072 const struct luaL_reg awesome_client_meta[] =
2074 { "isvisible", luaA_client_isvisible },
2075 { "geometry", luaA_client_geometry },
2076 { "buttons", luaA_client_buttons },
2077 { "keys", luaA_client_keys },
2078 { "tags", luaA_client_tags },
2079 { "kill", luaA_client_kill },
2080 { "swap", luaA_client_swap },
2081 { "raise", luaA_client_raise },
2082 { "lower", luaA_client_lower },
2083 { "redraw", luaA_client_redraw },
2084 { "mouse_resize", luaA_client_mouse_resize },
2085 { "mouse_move", luaA_client_mouse_move },
2086 { "unmanage", luaA_client_unmanage },
2087 { "fake_input", luaA_client_fake_input },
2088 { "__index", luaA_client_index },
2089 { "__newindex", luaA_client_newindex },
2090 { "__eq", luaA_client_eq },
2091 { "__gc", luaA_client_gc },
2092 { "__tostring", luaA_client_tostring },
2093 { NULL, NULL }
2096 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80