change codename
[awesome.git] / client.c
blob612627bb44c290a5ecb7e0c61bad2812992d6ddf
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 /** Change the clients urgency flag.
114 * \param c The client
115 * \param urgent The new flag state
117 void
118 client_seturgent(client_t *c, bool urgent)
120 if(c->isurgent != urgent)
122 xcb_get_property_cookie_t hints =
123 xcb_get_wm_hints_unchecked(globalconf.connection, c->win);
125 c->isurgent = urgent;
126 ewmh_client_update_hints(c);
128 /* update ICCCM hints */
129 xcb_wm_hints_t wmh;
130 xcb_get_wm_hints_reply(globalconf.connection, hints, &wmh, NULL);
132 if(urgent)
133 wmh.flags |= XCB_WM_HINT_X_URGENCY;
134 else
135 wmh.flags &= ~XCB_WM_HINT_X_URGENCY;
137 xcb_set_wm_hints(globalconf.connection, c->win, &wmh);
139 hooks_property(c, "urgent");
143 /** Returns true if a client is tagged
144 * with one of the tags of the specified screen.
145 * \param c The client to check.
146 * \param screen Virtual screen number.
147 * \return true if the client is visible, false otherwise.
149 bool
150 client_maybevisible(client_t *c, int screen)
152 if(c->screen == screen)
154 if(c->issticky || c->type == WINDOW_TYPE_DESKTOP)
155 return true;
157 tag_array_t *tags = &globalconf.screens[screen].tags;
159 for(int i = 0; i < tags->len; i++)
160 if(tags->tab[i]->selected && is_client_tagged(c, tags->tab[i]))
161 return true;
163 return false;
166 /** Return the content of a client as an image.
167 * That's just take a screenshot.
168 * \param c The client.
169 * \return An image.
171 static image_t *
172 client_getcontent(client_t *c)
174 xcb_image_t *ximage = xcb_image_get(globalconf.connection,
175 c->win,
176 0, 0,
177 c->geometries.internal.width,
178 c->geometries.internal.height,
179 ~0, XCB_IMAGE_FORMAT_Z_PIXMAP);
181 if(!ximage || ximage->bpp < 24)
182 return NULL;
184 uint32_t *data = p_alloca(uint32_t, ximage->width * ximage->height);
186 for(int y = 0; y < ximage->height; y++)
187 for(int x = 0; x < ximage->width; x++)
189 data[y * ximage->width + x] = xcb_image_get_pixel(ximage, x, y);
190 data[y * ximage->width + x] |= 0xff000000; /* set alpha to 0xff */
193 image_t *image = image_new_from_argb32(ximage->width, ximage->height, data);
195 xcb_image_destroy(ximage);
197 return image;
200 /** Get a client by its window.
201 * \param w The client window to find.
202 * \return A client pointer if found, NULL otherwise.
204 client_t *
205 client_getbywin(xcb_window_t w)
207 client_t *c;
208 for(c = globalconf.clients; c && c->win != w; c = c->next);
209 return c;
212 /** Call unfocus hook.
213 * \param c Client being unfocused
215 static void
216 client_unfocus_hook(client_t *c)
218 /* Call hook */
219 if(globalconf.hooks.unfocus != LUA_REFNIL)
221 luaA_client_userdata_new(globalconf.L, c);
222 luaA_dofunction(globalconf.L, globalconf.hooks.unfocus, 1, 0);
226 /** Unfocus a client.
227 * \param c The client.
229 static void
230 client_unfocus(client_t *c)
232 xcb_window_t root_win = xutil_screen_get(globalconf.connection, c->phys_screen)->root;
233 globalconf.screens[c->phys_screen].client_focus = NULL;
235 /* Set focus on root window, so no events leak to the current window. */
236 xcb_set_input_focus(globalconf.connection, XCB_INPUT_FOCUS_POINTER_ROOT,
237 root_win, XCB_CURRENT_TIME);
239 client_unfocus_hook(c);
241 ewmh_update_net_active_window(c->phys_screen);
244 /** Ban client and move it out of the viewport.
245 * \param c The client.
247 void
248 client_ban(client_t *c)
250 if(!c->isbanned)
252 /* Move all clients out of the physical viewport into negative coordinate space. */
253 /* They will all be put on top of each other. */
254 uint32_t request[2] = { - (c->geometries.internal.width + 2 * c->border),
255 - (c->geometries.internal.height + 2 * c->border) };
257 xcb_configure_window(globalconf.connection, c->win,
258 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
259 request);
261 titlebar_ban(c->titlebar);
263 c->isbanned = true;
265 /* All the wiboxes (may) need to be repositioned. */
266 if(client_hasstrut(c))
267 wibox_update_positions();
270 /* Wait until the last moment to take away the focus from the window. */
271 if (globalconf.screens[c->phys_screen].client_focus == c)
272 client_unfocus(c);
275 /** Give focus to client, or to first client if client is NULL.
276 * \param c The client or NULL.
277 * \return True if a window (even root) has received focus, false otherwise.
279 void
280 client_focus(client_t *c)
282 if(!client_maybevisible(c, c->screen))
283 return;
285 /* Input Model: No Input */
286 if (!window_hasproto(c->win, WM_TAKE_FOCUS) && c->nofocus)
287 return;
289 /* unfocus current selected client
290 * We don't really need to unfocus here,
291 * because client already received FocusOut event.
292 * What we need to do is call unfocus hook, to
293 * inform lua script, about this event.
295 if(globalconf.screen_focus->client_focus
296 && c != globalconf.screen_focus->client_focus)
297 client_unfocus_hook(globalconf.screen_focus->client_focus);
299 /* stop hiding c */
300 c->ishidden = false;
301 client_setminimized(c, false);
303 /* unban the client before focusing or it will fail */
304 client_unban(c);
306 globalconf.screen_focus = &globalconf.screens[c->phys_screen];
307 globalconf.screen_focus->client_focus = c;
309 /* Input Models: Passive, Locally Active */
310 xcb_set_input_focus(globalconf.connection, XCB_INPUT_FOCUS_POINTER_ROOT,
311 c->win, XCB_CURRENT_TIME);
313 /* TODO: Currently we don't handle correctly globally active input model
314 * One fix I know of, is we should handle FocusIn and FocusOut events for windows,
315 * and use WM_TAKE_FOCUS client message.
318 /* Some layouts use focused client differently, so call them back.
319 * And anyway, we have maybe unhidden */
320 client_need_arrange(c);
322 /* execute hook */
323 if(globalconf.hooks.focus != LUA_REFNIL)
325 luaA_client_userdata_new(globalconf.L, globalconf.screen_focus->client_focus);
326 luaA_dofunction(globalconf.L, globalconf.hooks.focus, 1, 0);
329 /* according to EWMH, we have to remove the urgent state from a client */
330 client_seturgent(c, false);
332 ewmh_update_net_active_window(c->phys_screen);
335 /** Stack a window below.
336 * \param c The client.
337 * \param previous The previous window on the stack.
338 * \param return The next-previous!
340 static xcb_window_t
341 client_stack_above(client_t *c, xcb_window_t previous)
343 uint32_t config_win_vals[2];
345 config_win_vals[0] = previous;
346 config_win_vals[1] = XCB_STACK_MODE_ABOVE;
348 xcb_configure_window(globalconf.connection, c->win,
349 XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE,
350 config_win_vals);
352 config_win_vals[0] = c->win;
354 if(c->titlebar)
356 xcb_configure_window(globalconf.connection,
357 c->titlebar->sw.window,
358 XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE,
359 config_win_vals);
360 previous = c->titlebar->sw.window;
362 else
363 previous = c->win;
365 /* stack transient window on top of their parents */
366 for(client_node_t *node = *client_node_list_last(&globalconf.stack);
367 node; node = node->prev)
368 if(node->client->transient_for == c)
369 previous = client_stack_above(node->client, previous);
371 return previous;
374 /** Stacking layout layers */
375 typedef enum
377 /** This one is a special layer */
378 LAYER_IGNORE,
379 LAYER_DESKTOP,
380 LAYER_BELOW,
381 LAYER_NORMAL,
382 LAYER_ABOVE,
383 LAYER_FULLSCREEN,
384 LAYER_ONTOP,
385 LAYER_OUTOFSPACE
386 } layer_t;
388 /** Get the real layer of a client according to its attribute (fullscreen, …)
389 * \param c The client.
390 * \return The real layer.
392 static layer_t
393 client_layer_translator(client_t *c)
395 /* first deal with user set attributes */
396 if(c->isontop)
397 return LAYER_ONTOP;
398 else if(c->isfullscreen)
399 return LAYER_FULLSCREEN;
400 else if(c->isabove)
401 return LAYER_ABOVE;
402 else if(c->isbelow)
403 return LAYER_BELOW;
405 /* check for transient attr */
406 if(c->transient_for)
407 return LAYER_IGNORE;
409 /* then deal with windows type */
410 switch(c->type)
412 case WINDOW_TYPE_DOCK:
413 return LAYER_ABOVE;
414 case WINDOW_TYPE_DESKTOP:
415 return LAYER_DESKTOP;
416 case WINDOW_TYPE_DIALOG:
417 case WINDOW_TYPE_MENU:
418 case WINDOW_TYPE_TOOLBAR:
419 case WINDOW_TYPE_UTILITY:
420 return LAYER_ABOVE;
421 default:
422 break;
425 return LAYER_NORMAL;
428 /** Restack clients.
429 * \todo It might be worth stopping to restack everyone and only stack `c'
430 * relatively to the first matching in the list.
432 void
433 client_stack()
435 uint32_t config_win_vals[2];
436 client_node_t *node, *last = *client_node_list_last(&globalconf.stack);
437 layer_t layer;
438 int screen;
440 config_win_vals[0] = XCB_NONE;
441 config_win_vals[1] = XCB_STACK_MODE_ABOVE;
443 /* stack desktop windows */
444 for(layer = LAYER_DESKTOP; layer < LAYER_BELOW; layer++)
445 for(node = last; node; node = node->prev)
446 if(client_layer_translator(node->client) == layer)
447 config_win_vals[0] = client_stack_above(node->client,
448 config_win_vals[0]);
450 /* first stack not ontop wibox window */
451 for(screen = 0; screen < globalconf.nscreen; screen++)
452 for(int i = 0; i < globalconf.screens[screen].wiboxes.len; i++)
454 wibox_t *sb = globalconf.screens[screen].wiboxes.tab[i];
455 if(!sb->ontop)
457 xcb_configure_window(globalconf.connection,
458 sb->sw.window,
459 XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE,
460 config_win_vals);
461 config_win_vals[0] = sb->sw.window;
465 /* then stack clients */
466 for(layer = LAYER_BELOW; layer < LAYER_OUTOFSPACE; layer++)
467 for(node = last; node; node = node->prev)
468 if(client_layer_translator(node->client) == layer)
469 config_win_vals[0] = client_stack_above(node->client,
470 config_win_vals[0]);
472 /* then stack ontop wibox window */
473 for(screen = 0; screen < globalconf.nscreen; screen++)
474 for(int i = 0; i < globalconf.screens[screen].wiboxes.len; i++)
476 wibox_t *sb = globalconf.screens[screen].wiboxes.tab[i];
477 if(sb->ontop)
479 xcb_configure_window(globalconf.connection,
480 sb->sw.window,
481 XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE,
482 config_win_vals);
483 config_win_vals[0] = sb->sw.window;
488 /** Manage a new client.
489 * \param w The window.
490 * \param wgeom Window geometry.
491 * \param phys_screen Physical screen number.
492 * \param startup True if we are managing at startup time.
494 void
495 client_manage(xcb_window_t w, xcb_get_geometry_reply_t *wgeom, int phys_screen, bool startup)
497 xcb_get_property_cookie_t ewmh_icon_cookie;
498 client_t *c, *tc = NULL;
499 image_t *icon;
500 int screen;
501 const uint32_t select_input_val[] =
503 XCB_EVENT_MASK_STRUCTURE_NOTIFY
504 | XCB_EVENT_MASK_PROPERTY_CHANGE
505 | XCB_EVENT_MASK_ENTER_WINDOW
506 | XCB_EVENT_MASK_LEAVE_WINDOW
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 /* Try to load props */
556 client_loadprops(c, &globalconf.screens[screen]);
558 /* Then check clients hints */
559 ewmh_client_check_hints(c);
561 /* move client to screen, but do not tag it */
562 screen_client_moveto(c, screen, false, true);
564 /* Push client in client list */
565 client_list_push(&globalconf.clients, client_ref(&c));
567 /* Push client in stack */
568 client_raise(c);
570 /* update window title */
571 property_update_wm_name(c);
572 property_update_wm_icon_name(c);
574 /* update strut */
575 ewmh_process_client_strut(c, NULL);
577 ewmh_update_net_client_list(c->phys_screen);
579 /* Always stay in NORMAL_STATE. Even though iconified seems more
580 * appropriate sometimes. The only possible loss is that clients not using
581 * visibility events may continue to proces data (when banned).
582 * Without any exposes or other events the cost should be fairly limited though.
584 * Some clients may expect the window to be unmapped when STATE_ICONIFIED.
585 * Two conflicting parts of the ICCCM v2.0 (section 4.1.4):
587 * "Normal -> Iconic - The client should send a ClientMessage event as described later in this section."
588 * (note no explicit mention of unmapping, while Normal->Widthdrawn does mention that)
590 * "Once a client's window has left the Withdrawn state, the window will be mapped
591 * if it is in the Normal state and the window will be unmapped if it is in the Iconic state."
593 * At this stage it's just safer to keep it in normal state and avoid confusion.
595 window_state_set(c->win, XCB_WM_STATE_NORMAL);
597 /* Move window outside the viewport before mapping it. */
598 client_ban(c);
599 xcb_map_window(globalconf.connection, c->win);
601 /* Call hook to notify list change */
602 if(globalconf.hooks.clients != LUA_REFNIL)
603 luaA_dofunction(globalconf.L, globalconf.hooks.clients, 0, 0);
605 /* call hook */
606 if(globalconf.hooks.manage != LUA_REFNIL)
608 luaA_client_userdata_new(globalconf.L, c);
609 lua_pushboolean(globalconf.L, startup);
610 luaA_dofunction(globalconf.L, globalconf.hooks.manage, 2, 0);
614 /** Compute client geometry with respect to its geometry hints.
615 * \param c The client.
616 * \param geometry The geometry that the client might receive.
617 * \return The geometry the client must take respecting its hints.
619 area_t
620 client_geometry_hints(client_t *c, area_t geometry)
622 int32_t basew, baseh, minw, minh;
624 /* base size is substituted with min size if not specified */
625 if(c->size_hints.flags & XCB_SIZE_HINT_P_SIZE)
627 basew = c->size_hints.base_width;
628 baseh = c->size_hints.base_height;
630 else if(c->size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE)
632 basew = c->size_hints.min_width;
633 baseh = c->size_hints.min_height;
635 else
636 basew = baseh = 0;
638 /* min size is substituted with base size if not specified */
639 if(c->size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE)
641 minw = c->size_hints.min_width;
642 minh = c->size_hints.min_height;
644 else if(c->size_hints.flags & XCB_SIZE_HINT_P_SIZE)
646 minw = c->size_hints.base_width;
647 minh = c->size_hints.base_height;
649 else
650 minw = minh = 0;
652 if(c->size_hints.flags & XCB_SIZE_HINT_P_ASPECT
653 && c->size_hints.min_aspect_num > 0
654 && c->size_hints.min_aspect_den > 0
655 && geometry.height - baseh > 0
656 && geometry.width - basew > 0)
658 double dx = (double) (geometry.width - basew);
659 double dy = (double) (geometry.height - baseh);
660 double min = (double) c->size_hints.min_aspect_num / (double) c->size_hints.min_aspect_den;
661 double max = (double) c->size_hints.max_aspect_num / (double) c->size_hints.min_aspect_den;
662 double ratio = dx / dy;
663 if(max > 0 && min > 0 && ratio > 0)
665 if(ratio < min)
667 dy = (dx * min + dy) / (min * min + 1);
668 dx = dy * min;
669 geometry.width = (int) dx + basew;
670 geometry.height = (int) dy + baseh;
672 else if(ratio > max)
674 dy = (dx * min + dy) / (max * max + 1);
675 dx = dy * min;
676 geometry.width = (int) dx + basew;
677 geometry.height = (int) dy + baseh;
682 if(minw)
683 geometry.width = MAX(geometry.width, minw);
684 if(minh)
685 geometry.height = MAX(geometry.height, minh);
687 if(c->size_hints.flags & XCB_SIZE_HINT_P_MAX_SIZE)
689 if(c->size_hints.max_width)
690 geometry.width = MIN(geometry.width, c->size_hints.max_width);
691 if(c->size_hints.max_height)
692 geometry.height = MIN(geometry.height, c->size_hints.max_height);
695 if(c->size_hints.flags & (XCB_SIZE_HINT_P_RESIZE_INC | XCB_SIZE_HINT_BASE_SIZE)
696 && c->size_hints.width_inc && c->size_hints.height_inc)
698 uint16_t t1 = geometry.width, t2 = geometry.height;
699 unsigned_subtract(t1, basew);
700 unsigned_subtract(t2, baseh);
701 geometry.width -= t1 % c->size_hints.width_inc;
702 geometry.height -= t2 % c->size_hints.height_inc;
705 return geometry;
708 /** Resize client window.
709 * The sizse given as parameters are with titlebar and borders!
710 * \param c Client to resize.
711 * \param geometry New window geometry.
712 * \param hints Use size hints.
713 * \return true if an actual resize occurred.
715 bool
716 client_resize(client_t *c, area_t geometry, bool hints)
718 int new_screen;
719 area_t geometry_internal;
720 area_t area;
722 /* offscreen appearance fixes */
723 area = display_area_get(c->phys_screen, NULL,
724 &globalconf.screens[c->screen].padding);
726 if(geometry.x > area.width)
727 geometry.x = area.width - geometry.width;
728 if(geometry.y > area.height)
729 geometry.y = area.height - geometry.height;
730 if(geometry.x + geometry.width < 0)
731 geometry.x = 0;
732 if(geometry.y + geometry.height < 0)
733 geometry.y = 0;
735 /* Real client geometry, please keep it contained to C code at the very least. */
736 geometry_internal = titlebar_geometry_remove(c->titlebar, c->border, geometry);
738 if(hints)
739 geometry_internal = client_geometry_hints(c, geometry_internal);
741 if(geometry_internal.width == 0 || geometry_internal.height == 0)
742 return false;
744 /* Also let client hints propegate to the "official" geometry. */
745 geometry = titlebar_geometry_add(c->titlebar, c->border, geometry_internal);
747 if(c->geometries.internal.x != geometry_internal.x
748 || c->geometries.internal.y != geometry_internal.y
749 || c->geometries.internal.width != geometry_internal.width
750 || c->geometries.internal.height != geometry_internal.height)
752 new_screen = screen_getbycoord(c->screen, geometry_internal.x, geometry_internal.y);
754 /* Values to configure a window is an array where values are
755 * stored according to 'value_mask' */
756 uint32_t values[4];
758 c->geometries.internal.x = values[0] = geometry_internal.x;
759 c->geometries.internal.y = values[1] = geometry_internal.y;
760 c->geometries.internal.width = values[2] = geometry_internal.width;
761 c->geometries.internal.height = values[3] = geometry_internal.height;
763 /* Also store geometry including border and titlebar. */
764 c->geometry = geometry;
766 titlebar_update_geometry(c);
768 /* The idea is to give a client a resize even when banned. */
769 /* We just have to move the (x,y) to keep it out of the viewport. */
770 /* This at least doesn't break expectations about events. */
771 if (c->isbanned)
773 geometry_internal.x = values[0] = - (geometry_internal.width + 2 * c->border);
774 geometry_internal.y = values[1] = - (geometry_internal.height + 2 * c->border);
777 xcb_configure_window(globalconf.connection, c->win,
778 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y
779 | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
780 values);
781 window_configure(c->win, geometry_internal, c->border);
783 screen_client_moveto(c, new_screen, true, false);
785 /* execute hook */
786 hooks_property(c, "geometry");
788 return true;
791 return false;
794 /** Set a client minimized, or not.
795 * \param c The client.
796 * \param s Set or not the client minimized.
798 void
799 client_setminimized(client_t *c, bool s)
801 if(c->isminimized != s)
803 client_need_arrange(c);
804 c->isminimized = s;
805 client_need_arrange(c);
806 ewmh_client_update_hints(c);
807 /* execute hook */
808 hooks_property(c, "minimized");
812 /** Set a client sticky, or not.
813 * \param c The client.
814 * \param s Set or not the client sticky.
816 void
817 client_setsticky(client_t *c, bool s)
819 if(c->issticky != s)
821 client_need_arrange(c);
822 c->issticky = s;
823 client_need_arrange(c);
824 ewmh_client_update_hints(c);
825 hooks_property(c, "sticky");
829 /** Set a client fullscreen, or not.
830 * \param c The client.
831 * \param s Set or not the client fullscreen.
833 void
834 client_setfullscreen(client_t *c, bool s)
836 if(c->isfullscreen != s)
838 area_t geometry;
840 /* become fullscreen! */
841 if((c->isfullscreen = s))
843 /* remove any max state */
844 client_setmaxhoriz(c, false);
845 client_setmaxvert(c, false);
846 /* You can only be part of one of the special layers. */
847 client_setbelow(c, false);
848 client_setabove(c, false);
849 client_setontop(c, false);
851 geometry = screen_area_get(c->screen, NULL, NULL, false);
852 c->geometries.fullscreen = c->geometry;
853 c->border_fs = c->border;
854 client_setborder(c, 0);
856 else
858 geometry = c->geometries.fullscreen;
859 client_setborder(c, c->border_fs);
861 client_resize(c, geometry, false);
862 client_need_arrange(c);
863 client_stack();
864 xcb_change_property(globalconf.connection,
865 XCB_PROP_MODE_REPLACE,
866 c->win, _AWESOME_FULLSCREEN, CARDINAL, 8, 1,
867 &c->isfullscreen);
868 ewmh_client_update_hints(c);
869 hooks_property(c, "fullscreen");
873 /** Set a client horizontally maximized.
874 * \param c The client.
875 * \param s The maximized status.
877 void
878 client_setmaxhoriz(client_t *c, bool s)
880 if(c->ismaxhoriz != s)
882 area_t geometry;
884 if((c->ismaxhoriz = s))
886 /* remove fullscreen mode */
887 client_setfullscreen(c, false);
889 geometry = screen_area_get(c->screen,
890 &globalconf.screens[c->screen].wiboxes,
891 &globalconf.screens[c->screen].padding,
892 true);
893 geometry.y = c->geometry.y;
894 geometry.height = c->geometry.height;
895 c->geometries.max.x = c->geometry.x;
896 c->geometries.max.width = c->geometry.width;
898 else
900 geometry = c->geometry;
901 geometry.x = c->geometries.max.x;
902 geometry.width = c->geometries.max.width;
905 client_resize(c, geometry, c->size_hints_honor);
906 client_need_arrange(c);
907 client_stack();
908 ewmh_client_update_hints(c);
909 hooks_property(c, "maximized_horizontal");
913 /** Set a client vertically maximized.
914 * \param c The client.
915 * \param s The maximized status.
917 void
918 client_setmaxvert(client_t *c, bool s)
920 if(c->ismaxvert != s)
922 area_t geometry;
924 if((c->ismaxvert = s))
926 /* remove fullscreen mode */
927 client_setfullscreen(c, false);
929 geometry = screen_area_get(c->screen,
930 &globalconf.screens[c->screen].wiboxes,
931 &globalconf.screens[c->screen].padding,
932 true);
933 geometry.x = c->geometry.x;
934 geometry.width = c->geometry.width;
935 c->geometries.max.y = c->geometry.y;
936 c->geometries.max.height = c->geometry.height;
938 else
940 geometry = c->geometry;
941 geometry.y = c->geometries.max.y;
942 geometry.height = c->geometries.max.height;
945 client_resize(c, geometry, c->size_hints_honor);
946 client_need_arrange(c);
947 client_stack();
948 ewmh_client_update_hints(c);
949 hooks_property(c, "maximized_vertical");
953 /** Set a client above, or not.
954 * \param c The client.
955 * \param s Set or not the client above.
957 void
958 client_setabove(client_t *c, bool s)
960 if(c->isabove != s)
962 /* You can only be part of one of the special layers. */
963 if(s)
965 client_setbelow(c, false);
966 client_setontop(c, false);
967 client_setfullscreen(c, false);
969 c->isabove = s;
970 client_stack();
971 ewmh_client_update_hints(c);
972 /* execute hook */
973 hooks_property(c, "above");
977 /** Set a client below, or not.
978 * \param c The client.
979 * \param s Set or not the client below.
981 void
982 client_setbelow(client_t *c, bool s)
984 if(c->isbelow != s)
986 /* You can only be part of one of the special layers. */
987 if(s)
989 client_setabove(c, false);
990 client_setontop(c, false);
991 client_setfullscreen(c, false);
993 c->isbelow = s;
994 client_stack();
995 ewmh_client_update_hints(c);
996 /* execute hook */
997 hooks_property(c, "below");
1001 /** Set a client modal, or not.
1002 * \param c The client.
1003 * \param s Set or not the client moda.
1005 void
1006 client_setmodal(client_t *c, bool s)
1008 if(c->ismodal != s)
1010 c->ismodal = s;
1011 client_stack();
1012 ewmh_client_update_hints(c);
1013 /* execute hook */
1014 hooks_property(c, "modal");
1018 /** Set a client ontop, or not.
1019 * \param c The client.
1020 * \param s Set or not the client moda.
1022 void
1023 client_setontop(client_t *c, bool s)
1025 if(c->isontop != s)
1027 /* You can only be part of one of the special layers. */
1028 if(s)
1030 client_setabove(c, false);
1031 client_setbelow(c, false);
1032 client_setfullscreen(c, false);
1034 c->isontop = s;
1035 client_stack();
1036 /* execute hook */
1037 hooks_property(c, "ontop");
1041 /** Save client properties as an X property.
1042 * \param c The client.
1044 void
1045 client_saveprops_tags(client_t *c)
1047 tag_array_t *tags = &globalconf.screens[c->screen].tags;
1048 unsigned char *prop = p_alloca(unsigned char, tags->len + 1);
1049 int i;
1051 for(i = 0; i < tags->len; i++)
1052 prop[i] = is_client_tagged(c, tags->tab[i]) ? '1' : '0';
1054 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE, c->win, _AWESOME_TAGS, STRING, 8, i, prop);
1057 /** Unban a client and move it back into the viewport.
1058 * \param c The client.
1060 void
1061 client_unban(client_t *c)
1063 if(c->isbanned)
1065 /* Move the client back where it belongs. */
1066 uint32_t request[] = { c->geometries.internal.x,
1067 c->geometries.internal.y,
1068 c->geometries.internal.width,
1069 c->geometries.internal.height };
1071 xcb_configure_window(globalconf.connection, c->win,
1072 XCB_CONFIG_WINDOW_X
1073 | XCB_CONFIG_WINDOW_Y
1074 | XCB_CONFIG_WINDOW_WIDTH
1075 | XCB_CONFIG_WINDOW_HEIGHT,
1076 request);
1077 window_configure(c->win, c->geometries.internal, c->border);
1079 /* Do this manually because the system doesn't know we moved the toolbar.
1080 * Note that !isvisible titlebars are unmapped and for fullscreen it'll
1081 * end up offscreen anyway. */
1082 if(c->titlebar)
1084 simple_window_t *sw = &c->titlebar->sw;
1085 /* All resizing is done, so only move now. */
1086 request[0] = sw->geometry.x;
1087 request[1] = sw->geometry.y;
1089 xcb_configure_window(globalconf.connection, sw->window,
1090 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
1091 request);
1094 c->isbanned = false;
1096 /* All the wiboxes (may) need to be repositioned. */
1097 if(client_hasstrut(c))
1098 wibox_update_positions();
1102 /** Unmanage a client.
1103 * \param c The client.
1105 void
1106 client_unmanage(client_t *c)
1108 tag_array_t *tags = &globalconf.screens[c->screen].tags;
1110 /* Reset transient_for attributes of widows that maybe refering to us */
1111 for(client_t *tc = globalconf.clients; tc; tc = tc->next)
1112 if(tc->transient_for == c)
1113 tc->transient_for = NULL;
1115 if(globalconf.screens[c->phys_screen].client_focus == c)
1116 client_unfocus(c);
1118 /* remove client everywhere */
1119 client_list_detach(&globalconf.clients, c);
1120 stack_client_delete(c);
1121 for(int i = 0; i < tags->len; i++)
1122 untag_client(c, tags->tab[i]);
1124 /* call hook */
1125 if(globalconf.hooks.unmanage != LUA_REFNIL)
1127 luaA_client_userdata_new(globalconf.L, c);
1128 luaA_dofunction(globalconf.L, globalconf.hooks.unmanage, 1, 0);
1131 /* Call hook to notify list change */
1132 if(globalconf.hooks.clients != LUA_REFNIL)
1133 luaA_dofunction(globalconf.L, globalconf.hooks.clients, 0, 0);
1135 /* The server grab construct avoids race conditions. */
1136 xcb_grab_server(globalconf.connection);
1138 xcb_ungrab_button(globalconf.connection, XCB_BUTTON_INDEX_ANY, c->win,
1139 XCB_BUTTON_MASK_ANY);
1140 window_state_set(c->win, XCB_WM_STATE_WITHDRAWN);
1142 xcb_flush(globalconf.connection);
1143 xcb_ungrab_server(globalconf.connection);
1145 titlebar_client_detach(c);
1147 ewmh_update_net_client_list(c->phys_screen);
1149 /* delete properties */
1150 xcb_delete_property(globalconf.connection, c->win, _AWESOME_TAGS);
1151 xcb_delete_property(globalconf.connection, c->win, _AWESOME_FLOATING);
1153 /* All the wiboxes (may) need to be repositioned. */
1154 if(client_hasstrut(c))
1155 wibox_update_positions();
1157 /* set client as invalid */
1158 c->invalid = true;
1160 client_unref(&c);
1163 /** Kill a client via a WM_DELETE_WINDOW request or KillClient if not
1164 * supported.
1165 * \param c The client to kill.
1167 void
1168 client_kill(client_t *c)
1170 if(window_hasproto(c->win, WM_DELETE_WINDOW))
1172 xcb_client_message_event_t ev;
1174 /* Initialize all of event's fields first */
1175 p_clear(&ev, 1);
1177 ev.response_type = XCB_CLIENT_MESSAGE;
1178 ev.window = c->win;
1179 ev.format = 32;
1180 ev.data.data32[1] = XCB_CURRENT_TIME;
1181 ev.type = WM_PROTOCOLS;
1182 ev.data.data32[0] = WM_DELETE_WINDOW;
1184 xcb_send_event(globalconf.connection, false, c->win,
1185 XCB_EVENT_MASK_NO_EVENT, (char *) &ev);
1187 else
1188 xcb_kill_client(globalconf.connection, c->win);
1191 /** Get all clients into a table.
1192 * \param L The Lua VM state.
1193 * \return The number of elements pushed on stack.
1194 * \luastack
1195 * \lparam An optional screen nunmber.
1196 * \lreturn A table with all clients.
1198 static int
1199 luaA_client_get(lua_State *L)
1201 int i = 1, screen;
1202 client_t *c;
1204 screen = luaL_optnumber(L, 1, 0) - 1;
1206 lua_newtable(L);
1208 if(screen == SCREEN_UNDEF)
1209 for(c = globalconf.clients; c; c = c->next)
1211 luaA_client_userdata_new(globalconf.L, c);
1212 lua_rawseti(L, -2, i++);
1214 else
1216 luaA_checkscreen(screen);
1217 for(c = globalconf.clients; c; c = c->next)
1218 if(c->screen == screen)
1220 luaA_client_userdata_new(globalconf.L, c);
1221 lua_rawseti(L, -2, i++);
1225 return 1;
1228 /** Check if a client is visible on its screen.
1229 * \param L The Lua VM state.
1230 * \return The number of elements pushed on stack.
1231 * \luastack
1232 * \lvalue A client.
1233 * \lreturn A boolean value, true if the client is visible, false otherwise.
1235 static int
1236 luaA_client_isvisible(lua_State *L)
1238 client_t **c = luaA_checkudata(L, 1, "client");
1239 lua_pushboolean(L, client_isvisible(*c, (*c)->screen));
1240 return 1;
1243 /** Set client border width.
1244 * \param c The client.
1245 * \param width The border width.
1247 void
1248 client_setborder(client_t *c, int width)
1250 uint32_t w = width;
1252 if(width > 0 && (c->type == WINDOW_TYPE_DOCK
1253 || c->type == WINDOW_TYPE_SPLASH
1254 || c->type == WINDOW_TYPE_DESKTOP
1255 || c->isfullscreen))
1256 return;
1258 if(width == c->border || width < 0)
1259 return;
1261 /* Update geometry with the new border. */
1262 c->geometry.width -= c->border;
1263 c->geometry.height -= c->border;
1265 c->border = width;
1266 xcb_configure_window(globalconf.connection, c->win,
1267 XCB_CONFIG_WINDOW_BORDER_WIDTH, &w);
1269 c->geometry.width += c->border;
1270 c->geometry.height += c->border;
1271 /* Tiled clients will be resized by the layout functions. */
1272 client_need_arrange(c);
1274 /* Changing border size also affects the size of the titlebar. */
1275 if (c->titlebar)
1276 titlebar_update_geometry(c);
1278 hooks_property(c, "border_width");
1281 /** Kill a client.
1282 * \param L The Lua VM state.
1284 * \luastack
1285 * \lvalue A client.
1287 static int
1288 luaA_client_kill(lua_State *L)
1290 client_t **c = luaA_checkudata(L, 1, "client");
1291 client_kill(*c);
1292 return 0;
1295 /** Swap a client with another one.
1296 * \param L The Lua VM state.
1297 * \luastack
1298 * \lvalue A client.
1299 * \lparam A client to swap with.
1301 static int
1302 luaA_client_swap(lua_State *L)
1304 client_t **c = luaA_checkudata(L, 1, "client");
1305 client_t **swap = luaA_checkudata(L, 2, "client");
1307 if(*c != *swap)
1309 client_list_swap(&globalconf.clients, *swap, *c);
1310 client_need_arrange(*c);
1311 client_need_arrange(*swap);
1313 /* Call hook to notify list change */
1314 if(globalconf.hooks.clients != LUA_REFNIL)
1315 luaA_dofunction(L, globalconf.hooks.clients, 0, 0);
1318 return 0;
1321 /** Access or set the client tags.
1322 * \param L The Lua VM state.
1323 * \return The number of elements pushed on stack.
1324 * \lparam A table with tags to set, or none to get the current tags table.
1325 * \return The clients tag.
1327 static int
1328 luaA_client_tags(lua_State *L)
1330 tag_array_t *tags;
1331 tag_t **tag;
1332 client_t **c = luaA_checkudata(L, 1, "client");
1333 int j = 0;
1335 if(lua_gettop(L) == 2)
1337 luaA_checktable(L, 2);
1338 tags = &globalconf.screens[(*c)->screen].tags;
1339 for(int i = 0; i < tags->len; i++)
1340 untag_client(*c, tags->tab[i]);
1341 lua_pushnil(L);
1342 while(lua_next(L, 2))
1344 tag = luaA_checkudata(L, -1, "tag");
1345 tag_client(*c, *tag);
1346 lua_pop(L, 1);
1348 lua_pop(L, 1);
1351 tags = &globalconf.screens[(*c)->screen].tags;
1352 luaA_otable_new(L);
1353 for(int i = 0; i < tags->len; i++)
1354 if(is_client_tagged(*c, tags->tab[i]))
1356 luaA_tag_userdata_new(L, tags->tab[i]);
1357 lua_rawseti(L, -2, ++j);
1360 return 1;
1363 /** Raise a client on top of others which are on the same layer.
1364 * \param L The Lua VM state.
1365 * \luastack
1366 * \lvalue A client.
1368 static int
1369 luaA_client_raise(lua_State *L)
1371 client_t **c = luaA_checkudata(L, 1, "client");
1372 client_raise(*c);
1373 return 0;
1376 /** Lower a client on bottom of others which are on the same layer.
1377 * \param L The Lua VM state.
1378 * \luastack
1379 * \lvalue A client.
1381 static int
1382 luaA_client_lower(lua_State *L)
1384 client_t **c = luaA_checkudata(L, 1, "client");
1385 client_lower(*c);
1386 return 0;
1389 /** Redraw a client by unmapping and mapping it quickly.
1390 * \param L The Lua VM state.
1392 * \luastack
1393 * \lvalue A client.
1395 static int
1396 luaA_client_redraw(lua_State *L)
1398 client_t **c = luaA_checkudata(L, 1, "client");
1400 xcb_unmap_window(globalconf.connection, (*c)->win);
1401 xcb_map_window(globalconf.connection, (*c)->win);
1403 /* Set the focus on the current window if the redraw has been
1404 performed on the window where the pointer is currently on
1405 because after the unmapping/mapping, the focus is lost */
1406 if(globalconf.screen_focus->client_focus == *c)
1407 client_focus(*c);
1409 return 0;
1412 /** Stop managing a client.
1413 * \param L The Lua VM state.
1414 * \return The number of elements pushed on stack.
1415 * \luastack
1416 * \lvalue A client.
1418 static int
1419 luaA_client_unmanage(lua_State *L)
1421 client_t **c = luaA_checkudata(L, 1, "client");
1422 client_unmanage(*c);
1423 return 0;
1426 /** Return client geometry.
1427 * \param L The Lua VM state.
1428 * \return The number of elements pushed on stack.
1429 * \luastack
1430 * \lparam A table with new coordinates, or none.
1431 * \lreturn A table with client coordinates.
1433 static int
1434 luaA_client_geometry(lua_State *L)
1436 client_t **c = luaA_checkudata(L, 1, "client");
1438 if(lua_gettop(L) == 2)
1440 area_t geometry;
1442 luaA_checktable(L, 2);
1443 geometry.x = luaA_getopt_number(L, 2, "x", (*c)->geometry.x);
1444 geometry.y = luaA_getopt_number(L, 2, "y", (*c)->geometry.y);
1445 if(client_isfixed(*c))
1447 geometry.width = (*c)->geometry.width;
1448 geometry.height = (*c)->geometry.height;
1450 else
1452 geometry.width = luaA_getopt_number(L, 2, "width", (*c)->geometry.width);
1453 geometry.height = luaA_getopt_number(L, 2, "height", (*c)->geometry.height);
1456 client_resize(*c, geometry, (*c)->size_hints_honor);
1459 return luaA_pusharea(L, (*c)->geometry);
1462 /** Push a strut type to a table on stack.
1463 * \param L The Lua VM state.
1464 * \param struts The struts to push.
1465 * \return The number of elements pushed on stack.
1467 static inline int
1468 luaA_pushstruts(lua_State *L, strut_t struts)
1470 lua_newtable(L);
1471 lua_pushnumber(L, struts.left);
1472 lua_setfield(L, -2, "left");
1473 lua_pushnumber(L, struts.right);
1474 lua_setfield(L, -2, "right");
1475 lua_pushnumber(L, struts.top);
1476 lua_setfield(L, -2, "top");
1477 lua_pushnumber(L, struts.bottom);
1478 lua_setfield(L, -2, "bottom");
1479 return 1;
1482 /** Return client struts (reserved space at the edge of the screen).
1483 * \param L The Lua VM state.
1484 * \return The number of elements pushed on stack.
1485 * \luastack
1486 * \lparam A table with new strut values, or none.
1487 * \lreturn A table with strut values.
1489 static int
1490 luaA_client_struts(lua_State *L)
1492 client_t **c = luaA_checkudata(L, 1, "client");
1494 if(lua_gettop(L) == 2)
1496 strut_t struts;
1497 area_t screen_area = display_area_get((*c)->phys_screen, NULL, NULL);
1499 struts.left = luaA_getopt_number(L, 2, "left", (*c)->strut.left);
1500 struts.right = luaA_getopt_number(L, 2, "right", (*c)->strut.right);
1501 struts.top = luaA_getopt_number(L, 2, "top", (*c)->strut.top);
1502 struts.bottom = luaA_getopt_number(L, 2, "bottom", (*c)->strut.bottom);
1504 if (struts.left != (*c)->strut.left || struts.right != (*c)->strut.right ||
1505 struts.top != (*c)->strut.top || struts.bottom != (*c)->strut.bottom) {
1506 /* Struts are not so well defined in the context of xinerama. So we just
1507 * give the entire root window and let the window manager decide. */
1508 struts.left_start_y = 0;
1509 struts.left_end_y = !struts.left ? 0 : screen_area.height;
1510 struts.right_start_y = 0;
1511 struts.right_end_y = !struts.right ? 0 : screen_area.height;
1512 struts.top_start_x = 0;
1513 struts.top_end_x = !struts.top ? 0 : screen_area.width;
1514 struts.bottom_start_x = 0;
1515 struts.bottom_end_x = !struts.bottom ? 0 : screen_area.width;
1517 (*c)->strut = struts;
1519 ewmh_update_client_strut((*c));
1521 client_need_arrange((*c));
1522 /* All the wiboxes (may) need to be repositioned. */
1523 wibox_update_positions();
1527 return luaA_pushstruts(L, (*c)->strut);
1530 /** Client newindex.
1531 * \param L The Lua VM state.
1532 * \return The number of elements pushed on stack.
1535 luaA_client_newindex(lua_State *L)
1537 size_t len;
1538 client_t **c = luaA_checkudata(L, 1, "client");
1539 const char *buf = luaL_checklstring(L, 2, &len);
1540 bool b;
1541 double d;
1542 int i;
1543 wibox_t **t = NULL;
1544 image_t **image;
1546 if((*c)->invalid)
1547 luaL_error(L, "client is invalid\n");
1549 switch(a_tokenize(buf, len))
1551 case A_TK_SCREEN:
1552 if(globalconf.xinerama_is_active)
1554 i = luaL_checknumber(L, 3) - 1;
1555 luaA_checkscreen(i);
1556 screen_client_moveto(*c, i, true, true);
1558 break;
1559 case A_TK_HIDE:
1560 b = luaA_checkboolean(L, 3);
1561 if(b != (*c)->ishidden)
1563 client_need_arrange(*c);
1564 (*c)->ishidden = b;
1565 client_need_arrange(*c);
1567 break;
1568 case A_TK_MINIMIZE:
1569 luaA_deprecate(L, "client.minimized");
1570 case A_TK_MINIMIZED:
1571 client_setminimized(*c, luaA_checkboolean(L, 3));
1572 break;
1573 case A_TK_FULLSCREEN:
1574 client_setfullscreen(*c, luaA_checkboolean(L, 3));
1575 break;
1576 case A_TK_MAXIMIZED_HORIZONTAL:
1577 client_setmaxhoriz(*c, luaA_checkboolean(L, 3));
1578 break;
1579 case A_TK_MAXIMIZED_VERTICAL:
1580 client_setmaxvert(*c, luaA_checkboolean(L, 3));
1581 break;
1582 case A_TK_ICON:
1583 image = luaA_checkudata(L, 3, "image");
1584 image_unref(&(*c)->icon);
1585 image_ref(image);
1586 (*c)->icon = *image;
1587 /* execute hook */
1588 hooks_property(*c, "icon");
1589 break;
1590 case A_TK_OPACITY:
1591 if(lua_isnil(L, 3))
1592 window_opacity_set((*c)->win, -1);
1593 else
1595 d = luaL_checknumber(L, 3);
1596 if(d >= 0 && d <= 1)
1597 window_opacity_set((*c)->win, d);
1599 break;
1600 case A_TK_STICKY:
1601 client_setsticky(*c, luaA_checkboolean(L, 3));
1602 break;
1603 case A_TK_HONORSIZEHINTS:
1604 luaA_deprecate(L, "size_hints_honor");
1605 case A_TK_SIZE_HINTS_HONOR:
1606 (*c)->size_hints_honor = luaA_checkboolean(L, 3);
1607 client_need_arrange(*c);
1608 break;
1609 case A_TK_BORDER_WIDTH:
1610 client_setborder(*c, luaL_checknumber(L, 3));
1611 break;
1612 case A_TK_ONTOP:
1613 client_setontop(*c, luaA_checkboolean(L, 3));
1614 break;
1615 case A_TK_ABOVE:
1616 client_setabove(*c, luaA_checkboolean(L, 3));
1617 break;
1618 case A_TK_BELOW:
1619 client_setbelow(*c, luaA_checkboolean(L, 3));
1620 break;
1621 case A_TK_BORDER_COLOR:
1622 if((buf = luaL_checklstring(L, 3, &len))
1623 && xcolor_init_reply(xcolor_init_unchecked(&(*c)->border_color, buf, len)))
1624 xcb_change_window_attributes(globalconf.connection, (*c)->win,
1625 XCB_CW_BORDER_PIXEL, &(*c)->border_color.pixel);
1626 break;
1627 case A_TK_TITLEBAR:
1628 if(lua_isnil(L, 3))
1629 titlebar_client_detach(*c);
1630 else
1632 t = luaA_checkudata(L, 3, "wibox");
1633 titlebar_client_attach(*c, *t);
1635 break;
1636 default:
1637 return 0;
1640 return 0;
1643 /** Client object.
1644 * \param L The Lua VM state.
1645 * \return The number of elements pushed on stack.
1646 * \luastack
1647 * \lfield name The client title.
1648 * \lfield skip_taskbar True if the client does not want to be in taskbar.
1649 * \lfield type The window type (desktop, normal, dock, …).
1650 * \lfield class The client class.
1651 * \lfield instance The client instance.
1652 * \lfield pid The client PID, if available.
1653 * \lfield role The window role, if available.
1654 * \lfield machine The machine client is running on.
1655 * \lfield icon_name The client name when iconified.
1656 * \lfield screen Client screen number.
1657 * \lfield hide Define if the client must be hidden, i.e. never mapped,
1658 * invisible in taskbar.
1659 * \lfield minimize Define it the client must be iconify, i.e. only visible in
1660 * taskbar.
1661 * \lfield icon_path Path to the icon used to identify.
1662 * \lfield size_hints_honor Honor size hints, i.e. respect size ratio.
1663 * \lfield border_width The client border width.
1664 * \lfield border_color The client border color.
1665 * \lfield titlebar The client titlebar.
1666 * \lfield urgent The client urgent state.
1667 * \lfield content An image representing the client window content (screenshot).
1668 * \lfield focus The focused client.
1669 * \lfield opacity The client opacity between 0 and 1.
1670 * \lfield ontop The client is on top of every other windows.
1671 * \lfield above The client is above normal windows.
1672 * \lfield below The client is below normal windows.
1673 * \lfield fullscreen The client is fullscreen or not.
1674 * \lfield maximized_horizontal The client is maximized horizontally or not.
1675 * \lfield maximized_vertical The client is maximized vertically or not.
1676 * \lfield transient_for Return the client the window is transient for.
1677 * \lfield group_id Identification unique to a group of windows.
1678 * \lfield leader_id Identification unique to windows spawned by the same command.
1679 * \lfield size_hints A table with size hints of the client: user_position,
1680 * user_size, program_position, program_size, etc.
1682 static int
1683 luaA_client_index(lua_State *L)
1685 size_t len;
1686 ssize_t slen;
1687 client_t **c = luaA_checkudata(L, 1, "client");
1688 const char *buf = luaL_checklstring(L, 2, &len);
1689 char *value;
1690 void *data;
1691 xcb_get_wm_class_reply_t hint;
1692 xcb_get_property_cookie_t prop_c;
1693 xcb_get_property_reply_t *prop_r = NULL;
1694 double d;
1696 if((*c)->invalid)
1697 luaL_error(L, "client is invalid\n");
1699 if(luaA_usemetatable(L, 1, 2))
1700 return 1;
1702 switch(a_tokenize(buf, len))
1704 image_t *image;
1706 case A_TK_NAME:
1707 lua_pushstring(L, (*c)->name);
1708 break;
1709 case A_TK_TRANSIENT_FOR:
1710 if((*c)->transient_for)
1711 return luaA_client_userdata_new(L, (*c)->transient_for);
1712 return 0;
1713 case A_TK_SKIP_TASKBAR:
1714 lua_pushboolean(L, (*c)->skiptb);
1715 break;
1716 case A_TK_CONTENT:
1717 if((image = client_getcontent(*c)))
1718 return luaA_image_userdata_new(L, image);
1719 return 0;
1720 case A_TK_TYPE:
1721 switch((*c)->type)
1723 case WINDOW_TYPE_DESKTOP:
1724 lua_pushliteral(L, "desktop");
1725 break;
1726 case WINDOW_TYPE_DOCK:
1727 lua_pushliteral(L, "dock");
1728 break;
1729 case WINDOW_TYPE_SPLASH:
1730 lua_pushliteral(L, "splash");
1731 break;
1732 case WINDOW_TYPE_DIALOG:
1733 lua_pushliteral(L, "dialog");
1734 break;
1735 case WINDOW_TYPE_MENU:
1736 lua_pushliteral(L, "menu");
1737 break;
1738 case WINDOW_TYPE_TOOLBAR:
1739 lua_pushliteral(L, "toolbar");
1740 break;
1741 case WINDOW_TYPE_UTILITY:
1742 lua_pushliteral(L, "utility");
1743 break;
1744 case WINDOW_TYPE_DROPDOWN_MENU:
1745 lua_pushliteral(L, "dropdown_menu");
1746 break;
1747 case WINDOW_TYPE_POPUP_MENU:
1748 lua_pushliteral(L, "popup_menu");
1749 break;
1750 case WINDOW_TYPE_TOOLTIP:
1751 lua_pushliteral(L, "tooltip");
1752 break;
1753 case WINDOW_TYPE_NOTIFICATION:
1754 lua_pushliteral(L, "notification");
1755 break;
1756 case WINDOW_TYPE_COMBO:
1757 lua_pushliteral(L, "combo");
1758 break;
1759 case WINDOW_TYPE_DND:
1760 lua_pushliteral(L, "dnd");
1761 break;
1762 case WINDOW_TYPE_NORMAL:
1763 lua_pushliteral(L, "normal");
1764 break;
1766 break;
1767 case A_TK_CLASS:
1768 if(!xcb_get_wm_class_reply(globalconf.connection,
1769 xcb_get_wm_class_unchecked(globalconf.connection, (*c)->win),
1770 &hint, NULL))
1771 return 0;
1772 lua_pushstring(L, hint.class_name);
1773 xcb_get_wm_class_reply_wipe(&hint);
1774 break;
1775 case A_TK_INSTANCE:
1776 if(!xcb_get_wm_class_reply(globalconf.connection,
1777 xcb_get_wm_class_unchecked(globalconf.connection, (*c)->win),
1778 &hint, NULL))
1779 return 0;
1780 lua_pushstring(L, hint.instance_name);
1781 xcb_get_wm_class_reply_wipe(&hint);
1782 break;
1783 case A_TK_ROLE:
1784 if(!xutil_text_prop_get(globalconf.connection, (*c)->win,
1785 WM_WINDOW_ROLE, &value, &slen))
1786 return 0;
1787 lua_pushlstring(L, value, slen);
1788 p_delete(&value);
1789 break;
1790 case A_TK_PID:
1791 prop_c = xcb_get_property_unchecked(globalconf.connection, false, (*c)->win, _NET_WM_PID, CARDINAL, 0L, 1L);
1792 prop_r = xcb_get_property_reply(globalconf.connection, prop_c, NULL);
1794 if(prop_r && prop_r->value_len && (data = xcb_get_property_value(prop_r)))
1795 lua_pushnumber(L, *(uint32_t *)data);
1796 else
1798 p_delete(&prop_r);
1799 return 0;
1801 break;
1802 case A_TK_LEADER_ID:
1803 lua_pushnumber(L, (*c)->leader_win);
1804 break;
1805 case A_TK_MACHINE:
1806 if(!xutil_text_prop_get(globalconf.connection, (*c)->win,
1807 WM_CLIENT_MACHINE, &value, &slen))
1808 return 0;
1809 lua_pushlstring(L, value, slen);
1810 p_delete(&value);
1811 break;
1812 case A_TK_ICON_NAME:
1813 lua_pushstring(L, (*c)->icon_name);
1814 break;
1815 case A_TK_SCREEN:
1816 lua_pushnumber(L, 1 + (*c)->screen);
1817 break;
1818 case A_TK_HIDE:
1819 lua_pushboolean(L, (*c)->ishidden);
1820 break;
1821 case A_TK_MINIMIZE:
1822 luaA_deprecate(L, "client.minimized");
1823 case A_TK_MINIMIZED:
1824 lua_pushboolean(L, (*c)->isminimized);
1825 break;
1826 case A_TK_FULLSCREEN:
1827 lua_pushboolean(L, (*c)->isfullscreen);
1828 break;
1829 case A_TK_GROUP_ID:
1830 if((*c)->group_win)
1831 lua_pushnumber(L, (*c)->group_win);
1832 else
1833 return 0;
1834 break;
1835 case A_TK_MAXIMIZED_HORIZONTAL:
1836 lua_pushboolean(L, (*c)->ismaxhoriz);
1837 break;
1838 case A_TK_MAXIMIZED_VERTICAL:
1839 lua_pushboolean(L, (*c)->ismaxvert);
1840 break;
1841 case A_TK_ICON:
1842 if((*c)->icon)
1843 luaA_image_userdata_new(L, (*c)->icon);
1844 else
1845 return 0;
1846 break;
1847 case A_TK_OPACITY:
1848 if((d = window_opacity_get((*c)->win)) >= 0)
1849 lua_pushnumber(L, d);
1850 else
1851 return 0;
1852 break;
1853 case A_TK_ONTOP:
1854 lua_pushboolean(L, (*c)->isontop);
1855 break;
1856 case A_TK_ABOVE:
1857 lua_pushboolean(L, (*c)->isabove);
1858 break;
1859 case A_TK_BELOW:
1860 lua_pushboolean(L, (*c)->isbelow);
1861 break;
1862 case A_TK_STICKY:
1863 lua_pushboolean(L, (*c)->issticky);
1864 break;
1865 case A_TK_HONORSIZEHINTS:
1866 luaA_deprecate(L, "size_hints_honor");
1867 case A_TK_SIZE_HINTS_HONOR:
1868 lua_pushboolean(L, (*c)->size_hints_honor);
1869 break;
1870 case A_TK_BORDER_WIDTH:
1871 lua_pushnumber(L, (*c)->border);
1872 break;
1873 case A_TK_BORDER_COLOR:
1874 luaA_pushcolor(L, &(*c)->border_color);
1875 break;
1876 case A_TK_TITLEBAR:
1877 if((*c)->titlebar)
1878 return luaA_wibox_userdata_new(L, (*c)->titlebar);
1879 return 0;
1880 case A_TK_URGENT:
1881 lua_pushboolean(L, (*c)->isurgent);
1882 break;
1883 case A_TK_SIZE_HINTS:
1885 const char *u_or_p = NULL;
1887 lua_newtable(L);
1889 if((*c)->size_hints.flags & XCB_SIZE_HINT_US_POSITION)
1890 u_or_p = "user_position";
1891 else if((*c)->size_hints.flags & XCB_SIZE_HINT_P_POSITION)
1892 u_or_p = "program_position";
1894 if(u_or_p)
1896 lua_newtable(L);
1897 lua_pushnumber(L, (*c)->size_hints.x);
1898 lua_setfield(L, -2, "x");
1899 lua_pushnumber(L, (*c)->size_hints.y);
1900 lua_setfield(L, -2, "y");
1901 lua_setfield(L, -2, u_or_p);
1902 u_or_p = NULL;
1905 if((*c)->size_hints.flags & XCB_SIZE_HINT_US_SIZE)
1906 u_or_p = "user_size";
1907 else if((*c)->size_hints.flags & XCB_SIZE_HINT_P_SIZE)
1908 u_or_p = "program_size";
1910 if(u_or_p)
1912 lua_newtable(L);
1913 lua_pushnumber(L, (*c)->size_hints.width);
1914 lua_setfield(L, -2, "width");
1915 lua_pushnumber(L, (*c)->size_hints.height);
1916 lua_setfield(L, -2, "height");
1917 lua_setfield(L, -2, u_or_p);
1920 if((*c)->size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE)
1922 lua_pushnumber(L, (*c)->size_hints.min_width);
1923 lua_setfield(L, -2, "min_width");
1924 lua_pushnumber(L, (*c)->size_hints.min_height);
1925 lua_setfield(L, -2, "min_height");
1928 if((*c)->size_hints.flags & XCB_SIZE_HINT_P_MAX_SIZE)
1930 lua_pushnumber(L, (*c)->size_hints.max_width);
1931 lua_setfield(L, -2, "max_width");
1932 lua_pushnumber(L, (*c)->size_hints.max_height);
1933 lua_setfield(L, -2, "max_height");
1936 if((*c)->size_hints.flags & XCB_SIZE_HINT_P_RESIZE_INC)
1938 lua_pushnumber(L, (*c)->size_hints.width_inc);
1939 lua_setfield(L, -2, "width_inc");
1940 lua_pushnumber(L, (*c)->size_hints.height_inc);
1941 lua_setfield(L, -2, "height_inc");
1944 if((*c)->size_hints.flags & XCB_SIZE_HINT_P_ASPECT)
1946 lua_pushnumber(L, (*c)->size_hints.min_aspect_num);
1947 lua_setfield(L, -2, "min_aspect_num");
1948 lua_pushnumber(L, (*c)->size_hints.min_aspect_den);
1949 lua_setfield(L, -2, "min_aspect_den");
1950 lua_pushnumber(L, (*c)->size_hints.max_aspect_num);
1951 lua_setfield(L, -2, "max_aspect_num");
1952 lua_pushnumber(L, (*c)->size_hints.max_aspect_den);
1953 lua_setfield(L, -2, "max_aspect_den");
1956 if((*c)->size_hints.flags & XCB_SIZE_HINT_BASE_SIZE)
1958 lua_pushnumber(L, (*c)->size_hints.base_width);
1959 lua_setfield(L, -2, "base_width");
1960 lua_pushnumber(L, (*c)->size_hints.base_height);
1961 lua_setfield(L, -2, "base_height");
1964 if((*c)->size_hints.flags & XCB_SIZE_HINT_P_WIN_GRAVITY)
1966 switch((*c)->size_hints.win_gravity)
1968 default:
1969 lua_pushliteral(L, "north_west");
1970 break;
1971 case XCB_GRAVITY_NORTH:
1972 lua_pushliteral(L, "north");
1973 break;
1974 case XCB_GRAVITY_NORTH_EAST:
1975 lua_pushliteral(L, "north_east");
1976 break;
1977 case XCB_GRAVITY_WEST:
1978 lua_pushliteral(L, "west");
1979 break;
1980 case XCB_GRAVITY_CENTER:
1981 lua_pushliteral(L, "center");
1982 break;
1983 case XCB_GRAVITY_EAST:
1984 lua_pushliteral(L, "east");
1985 break;
1986 case XCB_GRAVITY_SOUTH_WEST:
1987 lua_pushliteral(L, "south_west");
1988 break;
1989 case XCB_GRAVITY_SOUTH:
1990 lua_pushliteral(L, "south");
1991 break;
1992 case XCB_GRAVITY_SOUTH_EAST:
1993 lua_pushliteral(L, "south_east");
1994 break;
1995 case XCB_GRAVITY_STATIC:
1996 lua_pushliteral(L, "static");
1997 break;
1999 lua_setfield(L, -2, "win_gravity");
2002 break;
2003 default:
2004 return 0;
2007 return 1;
2010 /** Get or set mouse buttons bindings for a client.
2011 * \param L The Lua VM state.
2012 * \return The number of element pushed on stack.
2013 * \luastack
2014 * \lvalue A client.
2015 * \lparam An array of mouse button bindings objects, or nothing.
2016 * \return The array of mouse button bindings objects of this client.
2018 static int
2019 luaA_client_buttons(lua_State *L)
2021 client_t **client = luaA_checkudata(L, 1, "client");
2022 button_array_t *buttons = &(*client)->buttons;
2024 if(lua_gettop(L) == 2)
2025 luaA_button_array_set(L, 2, buttons);
2027 return luaA_button_array_get(L, buttons);
2030 /** Get or set keys bindings for a client.
2031 * \param L The Lua VM state.
2032 * \return The number of element pushed on stack.
2033 * \luastack
2034 * \lvalue A client.
2035 * \lparam An array of key bindings objects, or nothing.
2036 * \return The array of key bindings objects of this client.
2038 static int
2039 luaA_client_keys(lua_State *L)
2041 client_t **c = luaA_checkudata(L, 1, "client");
2042 keybindings_t *keys = &(*c)->keys;
2044 if(lua_gettop(L) == 2)
2046 luaA_key_array_set(L, 2, keys);
2047 xcb_ungrab_key(globalconf.connection, XCB_GRAB_ANY, (*c)->win, XCB_BUTTON_MASK_ANY);
2048 window_grabkeys((*c)->win, keys);
2051 return luaA_key_array_get(L, keys);
2054 /** Send fake events to a client.
2055 * \param L The Lua VM state.
2056 * \return The number of element pushed on stack.
2057 * \luastack
2058 * \lvalue A client.
2059 * \param The event type: key_press, key_release, button_press, button_release
2060 * or motion_notify.
2061 * \param The detail: in case of a key event, this is the keycode to send, in
2062 * case of a button event this is the number of the button. In case of a motion
2063 * event, this is a boolean value which if true make the coordinates relatives.
2064 * \param In case of a motion event, this is the X coordinate.
2065 * \param In case of a motion event, this is the Y coordinate.
2067 static int
2068 luaA_client_fake_input(lua_State *L)
2070 if(!globalconf.have_xtest)
2072 luaA_warn(L, "XTest extension is not available, cannot fake input.");
2073 return 0;
2076 client_t **c = luaA_checkudata(L, 1, "client");
2077 size_t tlen;
2078 const char *stype = luaL_checklstring(L, 2, &tlen);
2079 uint8_t type, detail;
2080 int x = 0, y = 0;
2082 switch(a_tokenize(stype, tlen))
2084 case A_TK_KEY_PRESS:
2085 type = XCB_KEY_PRESS;
2086 detail = luaL_checknumber(L, 3); /* keycode */
2087 break;
2088 case A_TK_KEY_RELEASE:
2089 type = XCB_KEY_RELEASE;
2090 detail = luaL_checknumber(L, 3); /* keycode */
2091 break;
2092 case A_TK_BUTTON_PRESS:
2093 type = XCB_BUTTON_PRESS;
2094 detail = luaL_checknumber(L, 3); /* button number */
2095 break;
2096 case A_TK_BUTTON_RELEASE:
2097 type = XCB_BUTTON_RELEASE;
2098 detail = luaL_checknumber(L, 3); /* button number */
2099 break;
2100 case A_TK_MOTION_NOTIFY:
2101 type = XCB_MOTION_NOTIFY;
2102 detail = luaA_checkboolean(L, 3); /* relative to the current position or not */
2103 x = luaL_checknumber(L, 4);
2104 y = luaL_checknumber(L, 5);
2105 break;
2106 default:
2107 return 0;
2110 xcb_test_fake_input(globalconf.connection,
2111 type,
2112 detail,
2113 XCB_CURRENT_TIME,
2114 (*c)->win,
2115 x, y,
2117 return 0;
2120 /* Client module.
2121 * \param L The Lua VM state.
2122 * \return The number of pushed elements.
2124 static int
2125 luaA_client_module_index(lua_State *L)
2127 size_t len;
2128 const char *buf = luaL_checklstring(L, 2, &len);
2130 switch(a_tokenize(buf, len))
2132 case A_TK_FOCUS:
2133 if(globalconf.screen_focus->client_focus)
2134 luaA_client_userdata_new(L, globalconf.screen_focus->client_focus);
2135 else
2136 return 0;
2137 break;
2138 default:
2139 return 0;
2142 return 1;
2145 /* Client module new index.
2146 * \param L The Lua VM state.
2147 * \return The number of pushed elements.
2149 static int
2150 luaA_client_module_newindex(lua_State *L)
2152 size_t len;
2153 const char *buf = luaL_checklstring(L, 2, &len);
2154 client_t **c;
2156 switch(a_tokenize(buf, len))
2158 case A_TK_FOCUS:
2159 c = luaA_checkudata(L, 3, "client");
2160 client_focus(*c);
2161 break;
2162 default:
2163 break;
2166 return 0;
2169 /** Move a client with mouse (DEPRECATED).
2170 * \param L The Lua VM state.
2172 static int
2173 luaA_client_mouse_move(lua_State *L)
2175 luaA_deprecate(L, "awful.mouse.client.move()");
2176 return 0;
2179 /** Resize a client with mouse (DEPRECATED).
2180 * \param L The Lua VM state.
2181 * \return The number of pushed elements.
2183 static int
2184 luaA_client_mouse_resize(lua_State *L)
2186 luaA_deprecate(L, "awful.mouse.client.resize()");
2187 return 0;
2190 const struct luaL_reg awesome_client_methods[] =
2192 { "get", luaA_client_get },
2193 { "__index", luaA_client_module_index },
2194 { "__newindex", luaA_client_module_newindex },
2195 { NULL, NULL }
2197 const struct luaL_reg awesome_client_meta[] =
2199 { "isvisible", luaA_client_isvisible },
2200 { "geometry", luaA_client_geometry },
2201 { "struts", luaA_client_struts },
2202 { "buttons", luaA_client_buttons },
2203 { "keys", luaA_client_keys },
2204 { "tags", luaA_client_tags },
2205 { "kill", luaA_client_kill },
2206 { "swap", luaA_client_swap },
2207 { "raise", luaA_client_raise },
2208 { "lower", luaA_client_lower },
2209 { "redraw", luaA_client_redraw },
2210 { "mouse_resize", luaA_client_mouse_resize },
2211 { "mouse_move", luaA_client_mouse_move },
2212 { "unmanage", luaA_client_unmanage },
2213 { "fake_input", luaA_client_fake_input },
2214 { "__index", luaA_client_index },
2215 { "__newindex", luaA_client_newindex },
2216 { "__eq", luaA_client_eq },
2217 { "__gc", luaA_client_gc },
2218 { "__tostring", luaA_client_tostring },
2219 { NULL, NULL }
2222 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80