awful.widget: button can take a string as image argument
[awesome.git] / client.c
blob589aa9eeae581f4b790f6d5f8da7e400b77ec9ce
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++)
70 if(prop[i] == '1')
71 tag_client(c, tags->tab[i]);
72 else
73 untag_client(c, tags->tab[i]);
75 p_delete(&prop);
77 /* check for fullscreen */
78 reply = xcb_get_property_reply(globalconf.connection, fullscreen_q, NULL);
80 if(reply && reply->value_len && (data = xcb_get_property_value(reply)))
81 client_setfullscreen(c, *(bool *) data);
82 p_delete(&reply);
84 return true;
87 /** Check if client supports protocol a protocole in WM_PROTOCOL.
88 * \param win The window.
89 * \return True if client has the atom in protocol, false otherwise.
91 static bool
92 window_hasproto(xcb_window_t win, xcb_atom_t atom)
94 uint32_t i;
95 xcb_get_wm_protocols_reply_t protocols;
96 bool ret = false;
98 if(xcb_get_wm_protocols_reply(globalconf.connection,
99 xcb_get_wm_protocols_unchecked(globalconf.connection,
100 win, WM_PROTOCOLS),
101 &protocols, NULL))
103 for(i = 0; !ret && i < protocols.atoms_len; i++)
104 if(protocols.atoms[i] == atom)
105 ret = true;
106 xcb_get_wm_protocols_reply_wipe(&protocols);
108 return ret;
111 /** Returns true if a client is tagged
112 * with one of the tags of the specified screen.
113 * \param c The client to check.
114 * \param screen Virtual screen number.
115 * \return true if the client is visible, false otherwise.
117 bool
118 client_maybevisible(client_t *c, int screen)
120 if(c->screen == screen)
122 if(c->issticky || c->type == WINDOW_TYPE_DESKTOP)
123 return true;
125 tag_array_t *tags = &globalconf.screens[screen].tags;
127 for(int i = 0; i < tags->len; i++)
128 if(tags->tab[i]->selected && is_client_tagged(c, tags->tab[i]))
129 return true;
131 return false;
134 /** Return the content of a client as an image.
135 * That's just take a screenshot.
136 * \param c The client.
137 * \return An image.
139 static image_t *
140 client_getcontent(client_t *c)
142 xcb_image_t *ximage = xcb_image_get(globalconf.connection,
143 c->win,
144 0, 0,
145 c->geometries.internal.width,
146 c->geometries.internal.height,
147 ~0, XCB_IMAGE_FORMAT_Z_PIXMAP);
149 if(!ximage || ximage->bpp < 24)
150 return NULL;
152 uint32_t *data = p_alloca(uint32_t, ximage->width * ximage->height);
154 for(int y = 0; y < ximage->height; y++)
155 for(int x = 0; x < ximage->width; x++)
157 data[y * ximage->width + x] = xcb_image_get_pixel(ximage, x, y);
158 data[y * ximage->width + x] |= 0xff000000; /* set alpha to 0xff */
161 image_t *image = image_new_from_argb32(ximage->width, ximage->height, data);
163 xcb_image_destroy(ximage);
165 return image;
168 /** Get a client by its window.
169 * \param w The client window to find.
170 * \return A client pointer if found, NULL otherwise.
172 client_t *
173 client_getbywin(xcb_window_t w)
175 client_t *c;
176 for(c = globalconf.clients; c && c->win != w; c = c->next);
177 return c;
180 /** Unfocus a client.
181 * \param c The client.
183 static void
184 client_unfocus(client_t *c)
186 xcb_window_t root_win = xutil_screen_get(globalconf.connection, c->phys_screen)->root;
187 globalconf.screens[c->phys_screen].client_focus = NULL;
189 /* Set focus on root window, so no events leak to the current window. */
190 xcb_set_input_focus(globalconf.connection, XCB_INPUT_FOCUS_POINTER_ROOT,
191 root_win, XCB_CURRENT_TIME);
193 /* Call hook */
194 if(globalconf.hooks.unfocus != LUA_REFNIL)
196 luaA_client_userdata_new(globalconf.L, c);
197 luaA_dofunction(globalconf.L, globalconf.hooks.unfocus, 1, 0);
200 ewmh_update_net_active_window(c->phys_screen);
203 /** Ban client and move it out of the viewport.
204 * \param c The client.
206 void
207 client_ban(client_t *c)
209 if(!c->isbanned)
211 /* Move all clients out of the physical viewport into negative coordinate space. */
212 /* They will all be put on top of each other. */
213 uint32_t request[2] = { - (c->geometries.internal.width + 2 * c->border),
214 - (c->geometries.internal.height + 2 * c->border) };
216 xcb_configure_window(globalconf.connection, c->win,
217 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
218 request);
220 /* Do it manually because client geometry remains unchanged. */
221 if (c->titlebar)
223 simple_window_t *sw = &c->titlebar->sw;
225 if (sw->window)
227 request[0] = - (sw->geometry.width);
228 request[1] = - (sw->geometry.height);
229 /* Move the titlebar to the same place as the window. */
230 xcb_configure_window(globalconf.connection, sw->window,
231 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
232 request);
236 c->isbanned = true;
238 /* All the wiboxes (may) need to be repositioned. */
239 if(client_hasstrut(c))
240 wibox_update_positions();
243 /* Wait until the last moment to take away the focus from the window. */
244 if (globalconf.screens[c->phys_screen].client_focus == c)
245 client_unfocus(c);
248 /** Give focus to client, or to first client if client is NULL.
249 * \param c The client or NULL.
250 * \return True if a window (even root) has received focus, false otherwise.
252 void
253 client_focus(client_t *c)
255 if(!client_maybevisible(c, c->screen) || c->nofocus)
256 return;
258 /* unfocus current selected client */
259 if(globalconf.screen_focus->client_focus
260 && c != globalconf.screen_focus->client_focus)
261 client_unfocus(globalconf.screen_focus->client_focus);
263 /* stop hiding c */
264 c->ishidden = false;
265 client_setminimized(c, false);
267 /* unban the client before focusing or it will fail */
268 client_unban(c);
270 globalconf.screen_focus = &globalconf.screens[c->phys_screen];
271 globalconf.screen_focus->client_focus = c;
273 xcb_set_input_focus(globalconf.connection, XCB_INPUT_FOCUS_POINTER_ROOT,
274 c->win, XCB_CURRENT_TIME);
276 /* Some layouts use focused client differently, so call them back.
277 * And anyway, we have maybe unhidden */
278 client_need_arrange(c);
280 /* execute hook */
281 if(globalconf.hooks.focus != LUA_REFNIL)
283 luaA_client_userdata_new(globalconf.L, globalconf.screen_focus->client_focus);
284 luaA_dofunction(globalconf.L, globalconf.hooks.focus, 1, 0);
287 ewmh_update_net_active_window(c->phys_screen);
290 /** Stack a window below.
291 * \param c The client.
292 * \param previous The previous window on the stack.
293 * \param return The next-previous!
295 static xcb_window_t
296 client_stack_above(client_t *c, xcb_window_t previous)
298 uint32_t config_win_vals[2];
300 config_win_vals[0] = previous;
301 config_win_vals[1] = XCB_STACK_MODE_ABOVE;
303 xcb_configure_window(globalconf.connection, c->win,
304 XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE,
305 config_win_vals);
307 config_win_vals[0] = c->win;
309 if(c->titlebar)
311 xcb_configure_window(globalconf.connection,
312 c->titlebar->sw.window,
313 XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE,
314 config_win_vals);
315 previous = c->titlebar->sw.window;
317 else
318 previous = c->win;
320 /* stack transient window on top of their parents */
321 for(client_node_t *node = *client_node_list_last(&globalconf.stack);
322 node; node = node->prev)
323 if(node->client->transient_for == c)
325 client_stack_above(node->client,
326 previous);
327 previous = node->client->win;
330 return previous;
333 /** Stacking layout layers */
334 typedef enum
336 /** This one is a special layer */
337 LAYER_IGNORE,
338 LAYER_DESKTOP,
339 LAYER_BELOW,
340 LAYER_NORMAL,
341 LAYER_ABOVE,
342 LAYER_FULLSCREEN,
343 LAYER_ONTOP,
344 LAYER_OUTOFSPACE
345 } layer_t;
347 /** Get the real layer of a client according to its attribute (fullscreen, …)
348 * \param c The client.
349 * \return The real layer.
351 static layer_t
352 client_layer_translator(client_t *c)
354 /* first deal with user set attributes */
355 if(c->isontop)
356 return LAYER_ONTOP;
357 else if(c->isfullscreen)
358 return LAYER_FULLSCREEN;
359 else if(c->isabove)
360 return LAYER_ABOVE;
361 else if(c->isbelow)
362 return LAYER_BELOW;
364 /* check for transient attr */
365 if(c->transient_for)
366 return LAYER_IGNORE;
368 /* then deal with windows type */
369 switch(c->type)
371 case WINDOW_TYPE_DOCK:
372 return LAYER_ABOVE;
373 case WINDOW_TYPE_DESKTOP:
374 return LAYER_DESKTOP;
375 case WINDOW_TYPE_DIALOG:
376 case WINDOW_TYPE_MENU:
377 case WINDOW_TYPE_TOOLBAR:
378 case WINDOW_TYPE_UTILITY:
379 return LAYER_ABOVE;
380 default:
381 break;
384 return LAYER_NORMAL;
387 /** Restack clients.
388 * \todo It might be worth stopping to restack everyone and only stack `c'
389 * relatively to the first matching in the list.
391 void
392 client_stack()
394 uint32_t config_win_vals[2];
395 client_node_t *node, *last = *client_node_list_last(&globalconf.stack);
396 layer_t layer;
397 int screen;
399 config_win_vals[0] = XCB_NONE;
400 config_win_vals[1] = XCB_STACK_MODE_ABOVE;
402 /* stack desktop windows */
403 for(layer = LAYER_DESKTOP; layer < LAYER_BELOW; layer++)
404 for(node = last; node; node = node->prev)
405 if(client_layer_translator(node->client) == layer)
406 config_win_vals[0] = client_stack_above(node->client,
407 config_win_vals[0]);
409 /* first stack not ontop wibox window */
410 for(screen = 0; screen < globalconf.nscreen; screen++)
411 for(int i = 0; i < globalconf.screens[screen].wiboxes.len; i++)
413 wibox_t *sb = globalconf.screens[screen].wiboxes.tab[i];
414 if(!sb->ontop)
416 xcb_configure_window(globalconf.connection,
417 sb->sw.window,
418 XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE,
419 config_win_vals);
420 config_win_vals[0] = sb->sw.window;
424 /* stack bottom layers */
425 for(layer = LAYER_BELOW; layer < LAYER_FULLSCREEN; layer++)
426 for(node = last; node; node = node->prev)
427 if(client_layer_translator(node->client) == layer)
428 config_win_vals[0] = client_stack_above(node->client,
429 config_win_vals[0]);
431 /* then stack ontop wibox window */
432 for(screen = 0; screen < globalconf.nscreen; screen++)
433 for(int i = 0; i < globalconf.screens[screen].wiboxes.len; i++)
435 wibox_t *sb = globalconf.screens[screen].wiboxes.tab[i];
436 if(sb->ontop)
438 xcb_configure_window(globalconf.connection,
439 sb->sw.window,
440 XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE,
441 config_win_vals);
442 config_win_vals[0] = sb->sw.window;
446 /* finally stack ontop and fullscreen windows */
447 for(layer = LAYER_FULLSCREEN; layer < LAYER_OUTOFSPACE; layer++)
448 for(node = last; node; node = node->prev)
449 if(client_layer_translator(node->client) == layer)
450 config_win_vals[0] = client_stack_above(node->client,
451 config_win_vals[0]);
454 /** Copy tags from one client to another client on the same screen.
455 * Be careful: this does not untag!
456 * \param src_c The source client.
457 * \param dst_c The destination client.
459 static void
460 client_duplicate_tags(client_t *src_c, client_t *dst_c)
462 int i;
463 tag_array_t *tags = &globalconf.screens[src_c->screen].tags;
465 /* Avoid doing dangerous things. */
466 if (src_c->screen != dst_c->screen)
467 return;
469 for(i = 0; i < tags->len; i++)
470 if(is_client_tagged(src_c, tags->tab[i]))
471 tag_client(dst_c, tags->tab[i]);
474 /** Manage a new client.
475 * \param w The window.
476 * \param wgeom Window geometry.
477 * \param phys_screen Physical screen number.
478 * \param startup True if we are managing at startup time.
480 void
481 client_manage(xcb_window_t w, xcb_get_geometry_reply_t *wgeom, int phys_screen, bool startup)
483 xcb_get_property_cookie_t ewmh_icon_cookie;
484 client_t *c, *tc = NULL, *group = NULL;
485 image_t *icon;
486 int screen;
487 const uint32_t select_input_val[] =
489 XCB_EVENT_MASK_STRUCTURE_NOTIFY
490 | XCB_EVENT_MASK_PROPERTY_CHANGE
491 | XCB_EVENT_MASK_ENTER_WINDOW
492 | XCB_EVENT_MASK_LEAVE_WINDOW
496 if(systray_iskdedockapp(w))
498 systray_request_handle(w, phys_screen, NULL);
499 return;
502 /* Send request to get NET_WM_ICON property as soon as possible... */
503 ewmh_icon_cookie = ewmh_window_icon_get_unchecked(w);
504 xcb_change_window_attributes(globalconf.connection, w, XCB_CW_EVENT_MASK, select_input_val);
505 c = p_new(client_t, 1);
507 screen = c->screen = screen_getbycoord(phys_screen, wgeom->x, wgeom->y);
509 c->phys_screen = phys_screen;
511 /* Initial values */
512 c->win = w;
513 c->geometry.x = wgeom->x;
514 c->geometry.y = wgeom->y;
515 c->geometry.width = wgeom->width + 2 * wgeom->border_width;
516 c->geometry.height = wgeom->height + 2 * wgeom->border_width;
517 /* Also set internal geometry (client_ban() needs it). */
518 c->geometries.internal.x = wgeom->x;
519 c->geometries.internal.y = wgeom->y;
520 c->geometries.internal.width = wgeom->width;
521 c->geometries.internal.height = wgeom->height;
522 client_setborder(c, wgeom->border_width);
523 if((icon = ewmh_window_icon_get_reply(ewmh_icon_cookie)))
524 c->icon = image_ref(&icon);
526 /* we honor size hints by default */
527 c->size_hints_honor = true;
529 /* update hints */
530 property_update_wm_normal_hints(c, NULL);
531 property_update_wm_hints(c, NULL);
532 property_update_wm_transient_for(c, NULL);
533 property_update_wm_client_leader(c, NULL);
535 /* Recursively find the parent. */
536 for(tc = c; tc->transient_for; tc = tc->transient_for);
537 if(tc != c)
538 screen = tc->screen;
540 /* Try to load props, if it fails check for group windows.
541 * transient_for windows are excluded, because they inherit the parent tags. */
542 if(!client_loadprops(c, &globalconf.screens[screen])
543 && c->group_win
544 && !c->transient_for)
545 /* Try to find a group of windows for better initial placement. */
546 for(group = globalconf.clients; group; group = group->next)
547 if(group->group_win == c->group_win)
548 break;
550 /* Move to the right screen.
551 * Assumption: Window groups do not span multiple logical screens. */
552 if(group)
553 screen = group->screen;
555 /* Then check clients hints */
556 ewmh_client_check_hints(c);
558 /* move client to screen, but do not tag it for now */
559 screen_client_moveto(c, screen, false, true);
561 /* Duplicate the tags of either the parent or a group window.
562 * Otherwise check for existing tags, if that fails tag it with the current tag.
563 * This could be done lua side, but it's sane behaviour. */
564 if (!c->issticky)
566 if(c->transient_for)
567 client_duplicate_tags(tc, c);
568 else if (group)
569 client_duplicate_tags(group, c);
570 else
572 int i;
573 tag_array_t *tags = &globalconf.screens[screen].tags;
574 for(i = 0; i < tags->len; i++)
575 if(is_client_tagged(c, tags->tab[i]))
576 break;
578 /* if no tag, set current selected */
579 if(i == tags->len)
580 for(i = 0; i < tags->len; i++)
581 if(tags->tab[i]->selected)
582 tag_client(c, tags->tab[i]);
586 /* Push client in client list */
587 client_list_push(&globalconf.clients, client_ref(&c));
589 /* Push client in stack */
590 client_raise(c);
592 /* update window title */
593 property_update_wm_name(c);
594 property_update_wm_icon_name(c);
596 /* update strut */
597 ewmh_client_strut_update(c, NULL);
599 ewmh_update_net_client_list(c->phys_screen);
601 /* Always stay in NORMAL_STATE. Even though iconified seems more
602 * appropriate sometimes. The only possible loss is that clients not using
603 * visibility events may continue to proces data (when banned).
604 * Without any exposes or other events the cost should be fairly limited though.
606 * Some clients may expect the window to be unmapped when STATE_ICONIFIED.
607 * Two conflicting parts of the ICCCM v2.0 (section 4.1.4):
609 * "Normal -> Iconic - The client should send a ClientMessage event as described later in this section."
610 * (note no explicit mention of unmapping, while Normal->Widthdrawn does mention that)
612 * "Once a client's window has left the Withdrawn state, the window will be mapped
613 * if it is in the Normal state and the window will be unmapped if it is in the Iconic state."
615 * At this stage it's just safer to keep it in normal state and avoid confusion.
617 window_state_set(c->win, XCB_WM_STATE_NORMAL);
619 /* Move window outside the viewport before mapping it. */
620 client_ban(c);
621 xcb_map_window(globalconf.connection, c->win);
623 /* Call hook to notify list change */
624 if(globalconf.hooks.clients != LUA_REFNIL)
625 luaA_dofunction(globalconf.L, globalconf.hooks.clients, 0, 0);
627 /* call hook */
628 if(globalconf.hooks.manage != LUA_REFNIL)
630 luaA_client_userdata_new(globalconf.L, c);
631 lua_pushboolean(globalconf.L, startup);
632 luaA_dofunction(globalconf.L, globalconf.hooks.manage, 2, 0);
636 /** Compute client geometry with respect to its geometry hints.
637 * \param c The client.
638 * \param geometry The geometry that the client might receive.
639 * \return The geometry the client must take respecting its hints.
641 area_t
642 client_geometry_hints(client_t *c, area_t geometry)
644 int32_t basew, baseh, minw, minh;
646 /* base size is substituted with min size if not specified */
647 if(c->size_hints.flags & XCB_SIZE_HINT_P_SIZE)
649 basew = c->size_hints.base_width;
650 baseh = c->size_hints.base_height;
652 else if(c->size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE)
654 basew = c->size_hints.min_width;
655 baseh = c->size_hints.min_height;
657 else
658 basew = baseh = 0;
660 /* min size is substituted with base size if not specified */
661 if(c->size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE)
663 minw = c->size_hints.min_width;
664 minh = c->size_hints.min_height;
666 else if(c->size_hints.flags & XCB_SIZE_HINT_P_SIZE)
668 minw = c->size_hints.base_width;
669 minh = c->size_hints.base_height;
671 else
672 minw = minh = 0;
674 if(c->size_hints.flags & XCB_SIZE_HINT_P_ASPECT
675 && c->size_hints.min_aspect_num > 0
676 && c->size_hints.min_aspect_den > 0
677 && geometry.height - baseh > 0
678 && geometry.width - basew > 0)
680 double dx = (double) (geometry.width - basew);
681 double dy = (double) (geometry.height - baseh);
682 double min = (double) c->size_hints.min_aspect_num / (double) c->size_hints.min_aspect_den;
683 double max = (double) c->size_hints.max_aspect_num / (double) c->size_hints.min_aspect_den;
684 double ratio = dx / dy;
685 if(max > 0 && min > 0 && ratio > 0)
687 if(ratio < min)
689 dy = (dx * min + dy) / (min * min + 1);
690 dx = dy * min;
691 geometry.width = (int) dx + basew;
692 geometry.height = (int) dy + baseh;
694 else if(ratio > max)
696 dy = (dx * min + dy) / (max * max + 1);
697 dx = dy * min;
698 geometry.width = (int) dx + basew;
699 geometry.height = (int) dy + baseh;
704 if(minw)
705 geometry.width = MAX(geometry.width, minw);
706 if(minh)
707 geometry.height = MAX(geometry.height, minh);
709 if(c->size_hints.flags & XCB_SIZE_HINT_P_MAX_SIZE)
711 if(c->size_hints.max_width)
712 geometry.width = MIN(geometry.width, c->size_hints.max_width);
713 if(c->size_hints.max_height)
714 geometry.height = MIN(geometry.height, c->size_hints.max_height);
717 if(c->size_hints.flags & (XCB_SIZE_HINT_P_RESIZE_INC | XCB_SIZE_HINT_BASE_SIZE)
718 && c->size_hints.width_inc && c->size_hints.height_inc)
720 uint16_t t1 = geometry.width, t2 = geometry.height;
721 unsigned_subtract(t1, basew);
722 unsigned_subtract(t2, baseh);
723 geometry.width -= t1 % c->size_hints.width_inc;
724 geometry.height -= t2 % c->size_hints.height_inc;
727 return geometry;
730 /** Resize client window.
731 * \param c Client to resize.
732 * \param geometry New window geometry.
733 * \param hints Use size hints.
735 void
736 client_resize(client_t *c, area_t geometry, bool hints)
738 int new_screen;
739 area_t geometry_internal;
740 area_t area;
742 /* offscreen appearance fixes */
743 area = display_area_get(c->phys_screen, NULL,
744 &globalconf.screens[c->screen].padding);
746 if(geometry.x > area.width)
747 geometry.x = area.width - geometry.width;
748 if(geometry.y > area.height)
749 geometry.y = area.height - geometry.height;
750 if(geometry.x + geometry.width < 0)
751 geometry.x = 0;
752 if(geometry.y + geometry.height < 0)
753 geometry.y = 0;
755 /* Real client geometry, please keep it contained to C code at the very least. */
756 geometry_internal = titlebar_geometry_remove(c->titlebar, c->border, geometry);
758 if(hints)
759 geometry_internal = client_geometry_hints(c, geometry_internal);
761 if(geometry_internal.width == 0 || geometry_internal.height == 0)
762 return;
764 /* Also let client hints propegate to the "official" geometry. */
765 geometry = titlebar_geometry_add(c->titlebar, c->border, geometry_internal);
767 if(c->geometries.internal.x != geometry_internal.x
768 || c->geometries.internal.y != geometry_internal.y
769 || c->geometries.internal.width != geometry_internal.width
770 || c->geometries.internal.height != geometry_internal.height)
772 new_screen = screen_getbycoord(c->screen, geometry_internal.x, geometry_internal.y);
774 /* Values to configure a window is an array where values are
775 * stored according to 'value_mask' */
776 uint32_t values[4];
778 c->geometries.internal.x = values[0] = geometry_internal.x;
779 c->geometries.internal.y = values[1] = geometry_internal.y;
780 c->geometries.internal.width = values[2] = geometry_internal.width;
781 c->geometries.internal.height = values[3] = geometry_internal.height;
783 /* Also store geometry including border and titlebar. */
784 c->geometry = geometry;
786 titlebar_update_geometry(c);
788 /* The idea is to give a client a resize even when banned. */
789 /* We just have to move the (x,y) to keep it out of the viewport. */
790 /* This at least doesn't break expectations about events. */
791 if (c->isbanned)
793 geometry.x = values[0] = - (geometry_internal.width + 2 * c->border);
794 geometry.y = values[1] = - (geometry_internal.height + 2 * c->border);
797 xcb_configure_window(globalconf.connection, c->win,
798 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y
799 | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
800 values);
801 window_configure(c->win, geometry_internal, c->border);
803 screen_client_moveto(c, new_screen, true, false);
805 /* execute hook */
806 hooks_property(c, "geometry");
810 /** Set a client minimized, or not.
811 * \param c The client.
812 * \param s Set or not the client minimized.
814 void
815 client_setminimized(client_t *c, bool s)
817 if(c->isminimized != s)
819 client_need_arrange(c);
820 c->isminimized = s;
821 client_need_arrange(c);
822 ewmh_client_update_hints(c);
823 /* execute hook */
824 hooks_property(c, "minimized");
828 /** Set a client sticky, or not.
829 * \param c The client.
830 * \param s Set or not the client sticky.
832 void
833 client_setsticky(client_t *c, bool s)
835 if(c->issticky != s)
837 client_need_arrange(c);
838 c->issticky = s;
839 client_need_arrange(c);
840 ewmh_client_update_hints(c);
841 hooks_property(c, "sticky");
845 /** Set a client fullscreen, or not.
846 * \param c The client.
847 * \param s Set or not the client fullscreen.
849 void
850 client_setfullscreen(client_t *c, bool s)
852 if(c->isfullscreen != s)
854 area_t geometry;
856 /* become fullscreen! */
857 if((c->isfullscreen = s))
859 /* remove any max state */
860 client_setmaxhoriz(c, false);
861 client_setmaxvert(c, false);
863 geometry = screen_area_get(c->screen, NULL, NULL, false);
864 c->geometries.fullscreen = c->geometry;
865 c->border_fs = c->border;
866 client_setborder(c, 0);
868 else
870 geometry = c->geometries.fullscreen;
871 client_setborder(c, c->border_fs);
873 client_resize(c, geometry, false);
874 client_need_arrange(c);
875 client_stack();
876 xcb_change_property(globalconf.connection,
877 XCB_PROP_MODE_REPLACE,
878 c->win, _AWESOME_FULLSCREEN, CARDINAL, 8, 1,
879 &c->isfullscreen);
880 ewmh_client_update_hints(c);
881 hooks_property(c, "fullscreen");
885 /** Set a client horizontally maximized.
886 * \param c The client.
887 * \param s The maximized status.
889 void
890 client_setmaxhoriz(client_t *c, bool s)
892 if(c->ismaxhoriz != s)
894 area_t geometry;
896 if((c->ismaxhoriz = s))
898 /* remove fullscreen mode */
899 client_setfullscreen(c, false);
901 geometry = screen_area_get(c->screen,
902 &globalconf.screens[c->screen].wiboxes,
903 &globalconf.screens[c->screen].padding,
904 true);
905 geometry.y = c->geometry.y;
906 geometry.height = c->geometry.height;
907 c->geometries.max.x = c->geometry.x;
908 c->geometries.max.width = c->geometry.width;
910 else
912 geometry = c->geometry;
913 geometry.x = c->geometries.max.x;
914 geometry.width = c->geometries.max.width;
917 client_resize(c, geometry, c->size_hints_honor);
918 client_need_arrange(c);
919 client_stack();
920 ewmh_client_update_hints(c);
921 hooks_property(c, "maximized_horizontal");
925 /** Set a client vertically maximized.
926 * \param c The client.
927 * \param s The maximized status.
929 void
930 client_setmaxvert(client_t *c, bool s)
932 if(c->ismaxvert != s)
934 area_t geometry;
936 if((c->ismaxvert = s))
938 /* remove fullscreen mode */
939 client_setfullscreen(c, false);
941 geometry = screen_area_get(c->screen,
942 &globalconf.screens[c->screen].wiboxes,
943 &globalconf.screens[c->screen].padding,
944 true);
945 geometry.x = c->geometry.x;
946 geometry.width = c->geometry.width;
947 c->geometries.max.y = c->geometry.y;
948 c->geometries.max.height = c->geometry.height;
950 else
952 geometry = c->geometry;
953 geometry.y = c->geometries.max.y;
954 geometry.height = c->geometries.max.height;
957 client_resize(c, geometry, c->size_hints_honor);
958 client_need_arrange(c);
959 client_stack();
960 ewmh_client_update_hints(c);
961 hooks_property(c, "maximized_vertical");
965 /** Set a client above, or not.
966 * \param c The client.
967 * \param s Set or not the client above.
969 void
970 client_setabove(client_t *c, bool s)
972 if(c->isabove != s)
974 c->isabove = s;
975 client_stack();
976 ewmh_client_update_hints(c);
977 /* execute hook */
978 hooks_property(c, "above");
982 /** Set a client below, or not.
983 * \param c The client.
984 * \param s Set or not the client below.
986 void
987 client_setbelow(client_t *c, bool s)
989 if(c->isbelow != s)
991 c->isbelow = s;
992 client_stack();
993 ewmh_client_update_hints(c);
994 /* execute hook */
995 hooks_property(c, "below");
999 /** Set a client modal, or not.
1000 * \param c The client.
1001 * \param s Set or not the client moda.
1003 void
1004 client_setmodal(client_t *c, bool s)
1006 if(c->ismodal != s)
1008 c->ismodal = s;
1009 client_stack();
1010 ewmh_client_update_hints(c);
1011 /* execute hook */
1012 hooks_property(c, "modal");
1016 /** Set a client ontop, or not.
1017 * \param c The client.
1018 * \param s Set or not the client moda.
1020 void
1021 client_setontop(client_t *c, bool s)
1023 if(c->isontop != s)
1025 c->isontop = s;
1026 client_stack();
1027 /* execute hook */
1028 hooks_property(c, "ontop");
1032 /** Save client properties as an X property.
1033 * \param c The client.
1035 void
1036 client_saveprops_tags(client_t *c)
1038 tag_array_t *tags = &globalconf.screens[c->screen].tags;
1039 unsigned char *prop = p_alloca(unsigned char, tags->len + 1);
1040 int i;
1042 for(i = 0; i < tags->len; i++)
1043 prop[i] = is_client_tagged(c, tags->tab[i]) ? '1' : '0';
1045 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE, c->win, _AWESOME_TAGS, STRING, 8, i, prop);
1048 /** Unban a client and move it back into the viewport.
1049 * \param c The client.
1051 void
1052 client_unban(client_t *c)
1054 if(c->isbanned)
1056 /* Move the client back where it belongs. */
1057 uint32_t request[2] = { c->geometries.internal.x, c->geometries.internal.y };
1059 xcb_configure_window(globalconf.connection, c->win,
1060 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
1061 request);
1062 window_configure(c->win, c->geometries.internal, c->border);
1064 /* Do this manually because the system doesn't know we moved the toolbar.
1065 * Note that !isvisible titlebars are unmapped and for fullscreen it'll
1066 * end up offscreen anyway. */
1067 if(c->titlebar)
1069 simple_window_t *sw = &c->titlebar->sw;
1070 /* All resizing is done, so only move now. */
1071 request[0] = sw->geometry.x;
1072 request[1] = sw->geometry.y;
1074 xcb_configure_window(globalconf.connection, sw->window,
1075 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
1076 request);
1079 c->isbanned = false;
1081 /* All the wiboxes (may) need to be repositioned. */
1082 if(client_hasstrut(c))
1083 wibox_update_positions();
1087 /** Unmanage a client.
1088 * \param c The client.
1090 void
1091 client_unmanage(client_t *c)
1093 tag_array_t *tags = &globalconf.screens[c->screen].tags;
1095 if(globalconf.screens[c->phys_screen].client_focus == c)
1096 client_unfocus(c);
1098 /* remove client everywhere */
1099 client_list_detach(&globalconf.clients, c);
1100 stack_client_delete(c);
1101 for(int i = 0; i < tags->len; i++)
1102 untag_client(c, tags->tab[i]);
1104 /* call hook */
1105 if(globalconf.hooks.unmanage != LUA_REFNIL)
1107 luaA_client_userdata_new(globalconf.L, c);
1108 luaA_dofunction(globalconf.L, globalconf.hooks.unmanage, 1, 0);
1111 /* Call hook to notify list change */
1112 if(globalconf.hooks.clients != LUA_REFNIL)
1113 luaA_dofunction(globalconf.L, globalconf.hooks.clients, 0, 0);
1115 /* The server grab construct avoids race conditions. */
1116 xcb_grab_server(globalconf.connection);
1118 xcb_ungrab_button(globalconf.connection, XCB_BUTTON_INDEX_ANY, c->win,
1119 XCB_BUTTON_MASK_ANY);
1120 window_state_set(c->win, XCB_WM_STATE_WITHDRAWN);
1122 xcb_flush(globalconf.connection);
1123 xcb_ungrab_server(globalconf.connection);
1125 titlebar_client_detach(c);
1127 ewmh_update_net_client_list(c->phys_screen);
1129 /* delete properties */
1130 xcb_delete_property(globalconf.connection, c->win, _AWESOME_TAGS);
1131 xcb_delete_property(globalconf.connection, c->win, _AWESOME_FLOATING);
1133 /* All the wiboxes (may) need to be repositioned. */
1134 if(client_hasstrut(c))
1135 wibox_update_positions();
1137 /* set client as invalid */
1138 c->invalid = true;
1140 client_unref(&c);
1143 /** Kill a client via a WM_DELETE_WINDOW request or KillClient if not
1144 * supported.
1145 * \param c The client to kill.
1147 void
1148 client_kill(client_t *c)
1150 if(window_hasproto(c->win, WM_DELETE_WINDOW))
1152 xcb_client_message_event_t ev;
1154 /* Initialize all of event's fields first */
1155 p_clear(&ev, 1);
1157 ev.response_type = XCB_CLIENT_MESSAGE;
1158 ev.window = c->win;
1159 ev.format = 32;
1160 ev.data.data32[1] = XCB_CURRENT_TIME;
1161 ev.type = WM_PROTOCOLS;
1162 ev.data.data32[0] = WM_DELETE_WINDOW;
1164 xcb_send_event(globalconf.connection, false, c->win,
1165 XCB_EVENT_MASK_NO_EVENT, (char *) &ev);
1167 else
1168 xcb_kill_client(globalconf.connection, c->win);
1171 /** Get all clients into a table.
1172 * \param L The Lua VM state.
1173 * \return The number of elements pushed on stack.
1174 * \luastack
1175 * \lparam An optional screen nunmber.
1176 * \lreturn A table with all clients.
1178 static int
1179 luaA_client_get(lua_State *L)
1181 int i = 1, screen;
1182 client_t *c;
1184 screen = luaL_optnumber(L, 1, 0) - 1;
1186 lua_newtable(L);
1188 if(screen == SCREEN_UNDEF)
1189 for(c = globalconf.clients; c; c = c->next)
1191 luaA_client_userdata_new(globalconf.L, c);
1192 lua_rawseti(L, -2, i++);
1194 else
1196 luaA_checkscreen(screen);
1197 for(c = globalconf.clients; c; c = c->next)
1198 if(c->screen == screen)
1200 luaA_client_userdata_new(globalconf.L, c);
1201 lua_rawseti(L, -2, i++);
1205 return 1;
1208 /** Check if a client is visible on its screen.
1209 * \param L The Lua VM state.
1210 * \return The number of elements pushed on stack.
1211 * \luastack
1212 * \lvalue A client.
1213 * \lreturn A boolean value, true if the client is visible, false otherwise.
1215 static int
1216 luaA_client_isvisible(lua_State *L)
1218 client_t **c = luaA_checkudata(L, 1, "client");
1219 lua_pushboolean(L, client_isvisible(*c, (*c)->screen));
1220 return 1;
1223 /** Set client border width.
1224 * \param c The client.
1225 * \param width The border width.
1227 void
1228 client_setborder(client_t *c, int width)
1230 uint32_t w = width;
1232 if(width > 0 && (c->type == WINDOW_TYPE_DOCK
1233 || c->type == WINDOW_TYPE_SPLASH
1234 || c->type == WINDOW_TYPE_DESKTOP
1235 || c->isfullscreen))
1236 return;
1238 if(width == c->border || width < 0)
1239 return;
1241 c->border = width;
1242 xcb_configure_window(globalconf.connection, c->win,
1243 XCB_CONFIG_WINDOW_BORDER_WIDTH, &w);
1245 client_need_arrange(c);
1247 hooks_property(c, "border_width");
1250 /** Kill a client.
1251 * \param L The Lua VM state.
1253 * \luastack
1254 * \lvalue A client.
1256 static int
1257 luaA_client_kill(lua_State *L)
1259 client_t **c = luaA_checkudata(L, 1, "client");
1260 client_kill(*c);
1261 return 0;
1264 /** Swap a client with another one.
1265 * \param L The Lua VM state.
1266 * \luastack
1267 * \lvalue A client.
1268 * \lparam A client to swap with.
1270 static int
1271 luaA_client_swap(lua_State *L)
1273 client_t **c = luaA_checkudata(L, 1, "client");
1274 client_t **swap = luaA_checkudata(L, 2, "client");
1276 if(*c != *swap)
1278 client_list_swap(&globalconf.clients, *swap, *c);
1279 client_need_arrange(*c);
1280 client_need_arrange(*swap);
1282 /* Call hook to notify list change */
1283 if(globalconf.hooks.clients != LUA_REFNIL)
1284 luaA_dofunction(L, globalconf.hooks.clients, 0, 0);
1287 return 0;
1290 /** Access or set the client tags.
1291 * \param L The Lua VM state.
1292 * \return The number of elements pushed on stack.
1293 * \lparam A table with tags to set, or none to get the current tags table.
1294 * \return The clients tag.
1296 static int
1297 luaA_client_tags(lua_State *L)
1299 tag_array_t *tags;
1300 tag_t **tag;
1301 client_t **c = luaA_checkudata(L, 1, "client");
1302 int j = 0;
1304 if(lua_gettop(L) == 2)
1306 luaA_checktable(L, 2);
1307 tags = &globalconf.screens[(*c)->screen].tags;
1308 for(int i = 0; i < tags->len; i++)
1309 untag_client(*c, tags->tab[i]);
1310 lua_pushnil(L);
1311 while(lua_next(L, 2))
1313 tag = luaA_checkudata(L, -1, "tag");
1314 tag_client(*c, *tag);
1315 lua_pop(L, 1);
1317 lua_pop(L, 1);
1320 tags = &globalconf.screens[(*c)->screen].tags;
1321 luaA_otable_new(L);
1322 for(int i = 0; i < tags->len; i++)
1323 if(is_client_tagged(*c, tags->tab[i]))
1325 luaA_tag_userdata_new(L, tags->tab[i]);
1326 lua_rawseti(L, -2, ++j);
1329 return 1;
1332 /** Raise a client on top of others which are on the same layer.
1333 * \param L The Lua VM state.
1334 * \luastack
1335 * \lvalue A client.
1337 static int
1338 luaA_client_raise(lua_State *L)
1340 client_t **c = luaA_checkudata(L, 1, "client");
1341 client_raise(*c);
1342 return 0;
1345 /** Lower a client on bottom of others which are on the same layer.
1346 * \param L The Lua VM state.
1347 * \luastack
1348 * \lvalue A client.
1350 static int
1351 luaA_client_lower(lua_State *L)
1353 client_t **c = luaA_checkudata(L, 1, "client");
1354 client_lower(*c);
1355 return 0;
1358 /** Redraw a client by unmapping and mapping it quickly.
1359 * \param L The Lua VM state.
1361 * \luastack
1362 * \lvalue A client.
1364 static int
1365 luaA_client_redraw(lua_State *L)
1367 client_t **c = luaA_checkudata(L, 1, "client");
1369 xcb_unmap_window(globalconf.connection, (*c)->win);
1370 xcb_map_window(globalconf.connection, (*c)->win);
1372 /* Set the focus on the current window if the redraw has been
1373 performed on the window where the pointer is currently on
1374 because after the unmapping/mapping, the focus is lost */
1375 if(globalconf.screen_focus->client_focus == *c)
1376 xcb_set_input_focus(globalconf.connection, XCB_INPUT_FOCUS_POINTER_ROOT,
1377 (*c)->win, XCB_CURRENT_TIME);
1379 return 0;
1382 /** Stop managing a client.
1383 * \param L The Lua VM state.
1384 * \return The number of elements pushed on stack.
1385 * \luastack
1386 * \lvalue A client.
1388 static int
1389 luaA_client_unmanage(lua_State *L)
1391 client_t **c = luaA_checkudata(L, 1, "client");
1392 client_unmanage(*c);
1393 return 0;
1396 /** Return client geometry.
1397 * \param L The Lua VM state.
1398 * \return The number of elements pushed on stack.
1399 * \luastack
1400 * \lparam A table with new coordinates, or none.
1401 * \lreturn A table with client coordinates.
1403 static int
1404 luaA_client_geometry(lua_State *L)
1406 client_t **c = luaA_checkudata(L, 1, "client");
1408 if(lua_gettop(L) == 2)
1410 area_t geometry;
1412 luaA_checktable(L, 2);
1413 geometry.x = luaA_getopt_number(L, 2, "x", (*c)->geometry.x);
1414 geometry.y = luaA_getopt_number(L, 2, "y", (*c)->geometry.y);
1415 if(client_isfixed(*c))
1417 geometry.width = (*c)->geometry.width;
1418 geometry.height = (*c)->geometry.height;
1420 else
1422 geometry.width = luaA_getopt_number(L, 2, "width", (*c)->geometry.width);
1423 geometry.height = luaA_getopt_number(L, 2, "height", (*c)->geometry.height);
1426 client_resize(*c, geometry, (*c)->size_hints_honor);
1429 return luaA_pusharea(L, (*c)->geometry);
1432 /** Client newindex.
1433 * \param L The Lua VM state.
1434 * \return The number of elements pushed on stack.
1437 luaA_client_newindex(lua_State *L)
1439 size_t len;
1440 client_t **c = luaA_checkudata(L, 1, "client");
1441 const char *buf = luaL_checklstring(L, 2, &len);
1442 bool b;
1443 double d;
1444 int i;
1445 wibox_t **t = NULL;
1446 image_t **image;
1448 if((*c)->invalid)
1449 luaL_error(L, "client is invalid\n");
1451 switch(a_tokenize(buf, len))
1453 case A_TK_SCREEN:
1454 if(globalconf.xinerama_is_active)
1456 i = luaL_checknumber(L, 3) - 1;
1457 luaA_checkscreen(i);
1458 screen_client_moveto(*c, i, true, true);
1460 break;
1461 case A_TK_HIDE:
1462 b = luaA_checkboolean(L, 3);
1463 if(b != (*c)->ishidden)
1465 client_need_arrange(*c);
1466 (*c)->ishidden = b;
1467 client_need_arrange(*c);
1469 break;
1470 case A_TK_MINIMIZE:
1471 luaA_deprecate(L, "client.minimized");
1472 case A_TK_MINIMIZED:
1473 client_setminimized(*c, luaA_checkboolean(L, 3));
1474 break;
1475 case A_TK_FULLSCREEN:
1476 client_setfullscreen(*c, luaA_checkboolean(L, 3));
1477 break;
1478 case A_TK_MAXIMIZED_HORIZONTAL:
1479 client_setmaxhoriz(*c, luaA_checkboolean(L, 3));
1480 break;
1481 case A_TK_MAXIMIZED_VERTICAL:
1482 client_setmaxvert(*c, luaA_checkboolean(L, 3));
1483 break;
1484 case A_TK_ICON:
1485 image = luaA_checkudata(L, 3, "image");
1486 image_unref(&(*c)->icon);
1487 image_ref(image);
1488 (*c)->icon = *image;
1489 /* execute hook */
1490 hooks_property(*c, "icon");
1491 break;
1492 case A_TK_OPACITY:
1493 if(lua_isnil(L, 3))
1494 window_opacity_set((*c)->win, -1);
1495 else
1497 d = luaL_checknumber(L, 3);
1498 if(d >= 0 && d <= 1)
1499 window_opacity_set((*c)->win, d);
1501 break;
1502 case A_TK_STICKY:
1503 client_setsticky(*c, luaA_checkboolean(L, 3));
1504 break;
1505 case A_TK_HONORSIZEHINTS:
1506 (*c)->size_hints_honor = luaA_checkboolean(L, 3);
1507 client_need_arrange(*c);
1508 break;
1509 case A_TK_BORDER_WIDTH:
1510 client_setborder(*c, luaL_checknumber(L, 3));
1511 break;
1512 case A_TK_ONTOP:
1513 client_setontop(*c, luaA_checkboolean(L, 3));
1514 break;
1515 case A_TK_BORDER_COLOR:
1516 if((buf = luaL_checklstring(L, 3, &len))
1517 && xcolor_init_reply(xcolor_init_unchecked(&(*c)->border_color, buf, len)))
1518 xcb_change_window_attributes(globalconf.connection, (*c)->win,
1519 XCB_CW_BORDER_PIXEL, &(*c)->border_color.pixel);
1520 break;
1521 case A_TK_TITLEBAR:
1522 if(lua_isnil(L, 3))
1523 titlebar_client_detach(*c);
1524 else
1526 t = luaA_checkudata(L, 3, "wibox");
1527 titlebar_client_attach(*c, *t);
1529 break;
1530 default:
1531 return 0;
1534 return 0;
1537 /** Client object.
1538 * \param L The Lua VM state.
1539 * \return The number of elements pushed on stack.
1540 * \luastack
1541 * \lfield name The client title.
1542 * \lfield skip_taskbar True if the client does not want to be in taskbar.
1543 * \lfield type The window type (desktop, normal, dock, …).
1544 * \lfield class The client class.
1545 * \lfield instance The client instance.
1546 * \lfield pid The client PID, if available.
1547 * \lfield role The window role, if available.
1548 * \lfield machine The machine client is running on.
1549 * \lfield icon_name The client name when iconified.
1550 * \lfield screen Client screen number.
1551 * \lfield hide Define if the client must be hidden, i.e. never mapped,
1552 * invisible in taskbar.
1553 * \lfield minimize Define it the client must be iconify, i.e. only visible in
1554 * taskbar.
1555 * \lfield icon_path Path to the icon used to identify.
1556 * \lfield size_hints_honor Honor size hints, i.e. respect size ratio.
1557 * \lfield border_width The client border width.
1558 * \lfield border_color The client border color.
1559 * \lfield titlebar The client titlebar.
1560 * \lfield urgent The client urgent state.
1561 * \lfield content An image representing the client window content (screenshot).
1562 * \lfield focus The focused client.
1563 * \lfield opacity The client opacity between 0 and 1.
1564 * \lfield ontop The client is on top of every other windows.
1565 * \lfield fullscreen The client is fullscreen or not.
1566 * \lfield maximized_horizontal The client is maximized horizontally or not.
1567 * \lfield maximized_vertical The client is maximized vertically or not.
1568 * \lfield transient_for Return the client the window is transient for.
1569 * \lfield group_id Identification unique to a group of windows.
1570 * \lfield leader_id Identification unique to windows spawned by the same command.
1571 * \lfield size_hints A table with size hints of the client: user_position,
1572 * user_size, program_position, program_size, etc.
1574 static int
1575 luaA_client_index(lua_State *L)
1577 size_t len;
1578 ssize_t slen;
1579 client_t **c = luaA_checkudata(L, 1, "client");
1580 const char *buf = luaL_checklstring(L, 2, &len);
1581 char *value;
1582 void *data;
1583 xcb_get_wm_class_reply_t hint;
1584 xcb_get_property_cookie_t prop_c;
1585 xcb_get_property_reply_t *prop_r = NULL;
1586 double d;
1588 if((*c)->invalid)
1589 luaL_error(L, "client is invalid\n");
1591 if(luaA_usemetatable(L, 1, 2))
1592 return 1;
1594 switch(a_tokenize(buf, len))
1596 image_t *image;
1598 case A_TK_NAME:
1599 lua_pushstring(L, (*c)->name);
1600 break;
1601 case A_TK_TRANSIENT_FOR:
1602 if((*c)->transient_for)
1603 return luaA_client_userdata_new(L, (*c)->transient_for);
1604 return 0;
1605 case A_TK_SKIP_TASKBAR:
1606 lua_pushboolean(L, (*c)->skiptb);
1607 break;
1608 case A_TK_CONTENT:
1609 if((image = client_getcontent(*c)))
1610 return luaA_image_userdata_new(L, image);
1611 return 0;
1612 case A_TK_TYPE:
1613 switch((*c)->type)
1615 case WINDOW_TYPE_DESKTOP:
1616 lua_pushliteral(L, "desktop");
1617 break;
1618 case WINDOW_TYPE_DOCK:
1619 lua_pushliteral(L, "dock");
1620 break;
1621 case WINDOW_TYPE_SPLASH:
1622 lua_pushliteral(L, "splash");
1623 break;
1624 case WINDOW_TYPE_DIALOG:
1625 lua_pushliteral(L, "dialog");
1626 break;
1627 case WINDOW_TYPE_MENU:
1628 lua_pushliteral(L, "menu");
1629 break;
1630 case WINDOW_TYPE_TOOLBAR:
1631 lua_pushliteral(L, "toolbar");
1632 break;
1633 case WINDOW_TYPE_UTILITY:
1634 lua_pushliteral(L, "utility");
1635 break;
1636 default:
1637 lua_pushliteral(L, "normal");
1638 break;
1640 break;
1641 case A_TK_CLASS:
1642 if(!xcb_get_wm_class_reply(globalconf.connection,
1643 xcb_get_wm_class_unchecked(globalconf.connection, (*c)->win),
1644 &hint, NULL))
1645 return 0;
1646 lua_pushstring(L, hint.class);
1647 xcb_get_wm_class_reply_wipe(&hint);
1648 break;
1649 case A_TK_INSTANCE:
1650 if(!xcb_get_wm_class_reply(globalconf.connection,
1651 xcb_get_wm_class_unchecked(globalconf.connection, (*c)->win),
1652 &hint, NULL))
1653 return 0;
1654 lua_pushstring(L, hint.name);
1655 xcb_get_wm_class_reply_wipe(&hint);
1656 break;
1657 case A_TK_ROLE:
1658 if(!xutil_text_prop_get(globalconf.connection, (*c)->win,
1659 WM_WINDOW_ROLE, &value, &slen))
1660 return 0;
1661 lua_pushlstring(L, value, slen);
1662 p_delete(&value);
1663 break;
1664 case A_TK_PID:
1665 prop_c = xcb_get_property_unchecked(globalconf.connection, false, (*c)->win, _NET_WM_PID, CARDINAL, 0L, 1L);
1666 prop_r = xcb_get_property_reply(globalconf.connection, prop_c, NULL);
1668 if(prop_r && prop_r->value_len && (data = xcb_get_property_value(prop_r)))
1669 lua_pushnumber(L, *(uint32_t *)data);
1670 else
1672 p_delete(&prop_r);
1673 return 0;
1675 break;
1676 case A_TK_LEADER_ID:
1677 lua_pushnumber(L, (*c)->leader_win);
1678 break;
1679 case A_TK_MACHINE:
1680 if(!xutil_text_prop_get(globalconf.connection, (*c)->win,
1681 WM_CLIENT_MACHINE, &value, &slen))
1682 return 0;
1683 lua_pushlstring(L, value, slen);
1684 p_delete(&value);
1685 break;
1686 case A_TK_ICON_NAME:
1687 lua_pushstring(L, (*c)->icon_name);
1688 break;
1689 case A_TK_SCREEN:
1690 lua_pushnumber(L, 1 + (*c)->screen);
1691 break;
1692 case A_TK_HIDE:
1693 lua_pushboolean(L, (*c)->ishidden);
1694 break;
1695 case A_TK_MINIMIZE:
1696 luaA_deprecate(L, "client.minimized");
1697 case A_TK_MINIMIZED:
1698 lua_pushboolean(L, (*c)->isminimized);
1699 break;
1700 case A_TK_FULLSCREEN:
1701 lua_pushboolean(L, (*c)->isfullscreen);
1702 break;
1703 case A_TK_GROUP_ID:
1704 if((*c)->group_win)
1705 lua_pushnumber(L, (*c)->group_win);
1706 else
1707 return 0;
1708 break;
1709 case A_TK_MAXIMIZED_HORIZONTAL:
1710 lua_pushboolean(L, (*c)->ismaxhoriz);
1711 break;
1712 case A_TK_MAXIMIZED_VERTICAL:
1713 lua_pushboolean(L, (*c)->ismaxvert);
1714 break;
1715 case A_TK_ICON:
1716 if((*c)->icon)
1717 luaA_image_userdata_new(L, (*c)->icon);
1718 else
1719 return 0;
1720 break;
1721 case A_TK_OPACITY:
1722 if((d = window_opacity_get((*c)->win)) >= 0)
1723 lua_pushnumber(L, d);
1724 else
1725 return 0;
1726 break;
1727 case A_TK_ONTOP:
1728 lua_pushboolean(L, (*c)->isontop);
1729 break;
1730 case A_TK_STICKY:
1731 lua_pushboolean(L, (*c)->issticky);
1732 break;
1733 case A_TK_HONORSIZEHINTS:
1734 luaA_deprecate(L, "size_hints_honor");
1735 case A_TK_SIZE_HINTS_HONOR:
1736 lua_pushboolean(L, (*c)->size_hints_honor);
1737 break;
1738 case A_TK_BORDER_WIDTH:
1739 lua_pushnumber(L, (*c)->border);
1740 break;
1741 case A_TK_BORDER_COLOR:
1742 luaA_pushcolor(L, &(*c)->border_color);
1743 break;
1744 case A_TK_TITLEBAR:
1745 if((*c)->titlebar)
1746 return luaA_wibox_userdata_new(L, (*c)->titlebar);
1747 return 0;
1748 case A_TK_URGENT:
1749 lua_pushboolean(L, (*c)->isurgent);
1750 break;
1751 case A_TK_SIZE_HINTS:
1753 const char *u_or_p = NULL;
1755 lua_newtable(L);
1757 if((*c)->size_hints.flags & XCB_SIZE_HINT_US_POSITION)
1758 u_or_p = "user_position";
1759 else if((*c)->size_hints.flags & XCB_SIZE_HINT_P_POSITION)
1760 u_or_p = "program_position";
1762 if(u_or_p)
1764 lua_newtable(L);
1765 lua_pushnumber(L, (*c)->size_hints.x);
1766 lua_setfield(L, -2, "x");
1767 lua_pushnumber(L, (*c)->size_hints.y);
1768 lua_setfield(L, -2, "y");
1769 lua_setfield(L, -2, u_or_p);
1770 u_or_p = NULL;
1773 if((*c)->size_hints.flags & XCB_SIZE_HINT_US_SIZE)
1774 u_or_p = "user_size";
1775 else if((*c)->size_hints.flags & XCB_SIZE_HINT_P_SIZE)
1776 u_or_p = "program_size";
1778 if(u_or_p)
1780 lua_newtable(L);
1781 lua_pushnumber(L, (*c)->size_hints.width);
1782 lua_setfield(L, -2, "width");
1783 lua_pushnumber(L, (*c)->size_hints.height);
1784 lua_setfield(L, -2, "height");
1785 lua_setfield(L, -2, u_or_p);
1788 if((*c)->size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE)
1790 lua_pushnumber(L, (*c)->size_hints.min_width);
1791 lua_setfield(L, -2, "min_width");
1792 lua_pushnumber(L, (*c)->size_hints.min_height);
1793 lua_setfield(L, -2, "min_height");
1796 if((*c)->size_hints.flags & XCB_SIZE_HINT_P_MAX_SIZE)
1798 lua_pushnumber(L, (*c)->size_hints.max_width);
1799 lua_setfield(L, -2, "max_width");
1800 lua_pushnumber(L, (*c)->size_hints.max_height);
1801 lua_setfield(L, -2, "max_height");
1804 if((*c)->size_hints.flags & XCB_SIZE_HINT_P_RESIZE_INC)
1806 lua_pushnumber(L, (*c)->size_hints.width_inc);
1807 lua_setfield(L, -2, "width_inc");
1808 lua_pushnumber(L, (*c)->size_hints.height_inc);
1809 lua_setfield(L, -2, "height_inc");
1812 if((*c)->size_hints.flags & XCB_SIZE_HINT_P_ASPECT)
1814 lua_pushnumber(L, (*c)->size_hints.min_aspect_num);
1815 lua_setfield(L, -2, "min_aspect_num");
1816 lua_pushnumber(L, (*c)->size_hints.min_aspect_den);
1817 lua_setfield(L, -2, "min_aspect_den");
1818 lua_pushnumber(L, (*c)->size_hints.max_aspect_num);
1819 lua_setfield(L, -2, "max_aspect_num");
1820 lua_pushnumber(L, (*c)->size_hints.max_aspect_den);
1821 lua_setfield(L, -2, "max_aspect_den");
1824 if((*c)->size_hints.flags & XCB_SIZE_HINT_BASE_SIZE)
1826 lua_pushnumber(L, (*c)->size_hints.base_width);
1827 lua_setfield(L, -2, "base_width");
1828 lua_pushnumber(L, (*c)->size_hints.base_height);
1829 lua_setfield(L, -2, "base_height");
1832 if((*c)->size_hints.flags & XCB_SIZE_HINT_P_WIN_GRAVITY)
1834 switch((*c)->size_hints.win_gravity)
1836 default:
1837 lua_pushliteral(L, "north_west");
1838 break;
1839 case XCB_GRAVITY_NORTH:
1840 lua_pushliteral(L, "north");
1841 break;
1842 case XCB_GRAVITY_NORTH_EAST:
1843 lua_pushliteral(L, "north_east");
1844 break;
1845 case XCB_GRAVITY_WEST:
1846 lua_pushliteral(L, "west");
1847 break;
1848 case XCB_GRAVITY_CENTER:
1849 lua_pushliteral(L, "center");
1850 break;
1851 case XCB_GRAVITY_EAST:
1852 lua_pushliteral(L, "east");
1853 break;
1854 case XCB_GRAVITY_SOUTH_WEST:
1855 lua_pushliteral(L, "south_west");
1856 break;
1857 case XCB_GRAVITY_SOUTH:
1858 lua_pushliteral(L, "south");
1859 break;
1860 case XCB_GRAVITY_SOUTH_EAST:
1861 lua_pushliteral(L, "south_east");
1862 break;
1863 case XCB_GRAVITY_STATIC:
1864 lua_pushliteral(L, "static");
1865 break;
1867 lua_setfield(L, -2, "win_gravity");
1870 break;
1871 default:
1872 return 0;
1875 return 1;
1878 /** Get or set mouse buttons bindings for a client.
1879 * \param L The Lua VM state.
1880 * \return The number of element pushed on stack.
1881 * \luastack
1882 * \lvalue A client.
1883 * \lparam An array of mouse button bindings objects, or nothing.
1884 * \return The array of mouse button bindings objects of this client.
1886 static int
1887 luaA_client_buttons(lua_State *L)
1889 client_t **client = luaA_checkudata(L, 1, "client");
1890 button_array_t *buttons = &(*client)->buttons;
1892 if(lua_gettop(L) == 2)
1893 luaA_button_array_set(L, 2, buttons);
1895 return luaA_button_array_get(L, buttons);
1898 /** Send fake events to a client.
1899 * \param L The Lua VM state.
1900 * \return The number of element pushed on stack.
1901 * \luastack
1902 * \lvalue A client.
1903 * \param The event type: key_press, key_release, button_press, button_release
1904 * or motion_notify.
1905 * \param The detail: in case of a key event, this is the keycode to send, in
1906 * case of a button event this is the number of the button. In case of a motion
1907 * event, this is a boolean value which if true make the coordinates relatives.
1908 * \param In case of a motion event, this is the X coordinate.
1909 * \param In case of a motion event, this is the Y coordinate.
1911 static int
1912 luaA_client_fake_input(lua_State *L)
1914 if(!globalconf.have_xtest)
1916 luaA_warn(L, "XTest extension is not available, cannot fake input.");
1917 return 0;
1920 client_t **c = luaA_checkudata(L, 1, "client");
1921 size_t tlen;
1922 const char *stype = luaL_checklstring(L, 2, &tlen);
1923 uint8_t type, detail;
1924 int x = 0, y = 0;
1926 switch(a_tokenize(stype, tlen))
1928 case A_TK_KEY_PRESS:
1929 type = XCB_KEY_PRESS;
1930 detail = luaL_checknumber(L, 3); /* keycode */
1931 break;
1932 case A_TK_KEY_RELEASE:
1933 type = XCB_KEY_RELEASE;
1934 detail = luaL_checknumber(L, 3); /* keycode */
1935 break;
1936 case A_TK_BUTTON_PRESS:
1937 type = XCB_BUTTON_PRESS;
1938 detail = luaL_checknumber(L, 3); /* button number */
1939 break;
1940 case A_TK_BUTTON_RELEASE:
1941 type = XCB_BUTTON_RELEASE;
1942 detail = luaL_checknumber(L, 3); /* button number */
1943 break;
1944 case A_TK_MOTION_NOTIFY:
1945 type = XCB_MOTION_NOTIFY;
1946 detail = luaA_checkboolean(L, 3); /* relative to the current position or not */
1947 x = luaL_checknumber(L, 4);
1948 y = luaL_checknumber(L, 5);
1949 break;
1950 default:
1951 return 0;
1954 xcb_test_fake_input(globalconf.connection,
1955 type,
1956 detail,
1957 XCB_CURRENT_TIME,
1958 (*c)->win,
1959 x, y,
1961 return 0;
1964 /* Client module.
1965 * \param L The Lua VM state.
1966 * \return The number of pushed elements.
1968 static int
1969 luaA_client_module_index(lua_State *L)
1971 size_t len;
1972 const char *buf = luaL_checklstring(L, 2, &len);
1974 switch(a_tokenize(buf, len))
1976 case A_TK_FOCUS:
1977 if(globalconf.screen_focus->client_focus)
1978 luaA_client_userdata_new(L, globalconf.screen_focus->client_focus);
1979 else
1980 return 0;
1981 break;
1982 default:
1983 return 0;
1986 return 1;
1989 /* Client module new index.
1990 * \param L The Lua VM state.
1991 * \return The number of pushed elements.
1993 static int
1994 luaA_client_module_newindex(lua_State *L)
1996 size_t len;
1997 const char *buf = luaL_checklstring(L, 2, &len);
1998 client_t **c;
2000 switch(a_tokenize(buf, len))
2002 case A_TK_FOCUS:
2003 c = luaA_checkudata(L, 3, "client");
2004 client_focus(*c);
2005 break;
2006 default:
2007 break;
2010 return 0;
2013 /** Move a client with mouse (DEPRECATED).
2014 * \param L The Lua VM state.
2016 static int
2017 luaA_client_mouse_move(lua_State *L)
2019 luaA_deprecate(L, "awful.mouse.client.move()");
2020 return 0;
2023 /** Resize a client with mouse (DEPRECATED).
2024 * \param L The Lua VM state.
2025 * \return The number of pushed elements.
2027 static int
2028 luaA_client_mouse_resize(lua_State *L)
2030 luaA_deprecate(L, "awful.mouse.client.resize()");
2031 return 0;
2034 const struct luaL_reg awesome_client_methods[] =
2036 { "get", luaA_client_get },
2037 { "__index", luaA_client_module_index },
2038 { "__newindex", luaA_client_module_newindex },
2039 { NULL, NULL }
2041 const struct luaL_reg awesome_client_meta[] =
2043 { "isvisible", luaA_client_isvisible },
2044 { "geometry", luaA_client_geometry },
2045 { "buttons", luaA_client_buttons },
2046 { "tags", luaA_client_tags },
2047 { "kill", luaA_client_kill },
2048 { "swap", luaA_client_swap },
2049 { "raise", luaA_client_raise },
2050 { "lower", luaA_client_lower },
2051 { "redraw", luaA_client_redraw },
2052 { "mouse_resize", luaA_client_mouse_resize },
2053 { "mouse_move", luaA_client_mouse_move },
2054 { "unmanage", luaA_client_unmanage },
2055 { "fake_input", luaA_client_fake_input },
2056 { "__index", luaA_client_index },
2057 { "__newindex", luaA_client_newindex },
2058 { "__eq", luaA_client_eq },
2059 { "__gc", luaA_client_gc },
2060 { "__tostring", luaA_client_tostring },
2061 { NULL, NULL }
2064 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80