draw: do not delete image on text rendering
[awesome.git] / client.c
blob3db6c17b627615aea105d71ad03428c3199b65f7
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 <stdio.h>
24 #include <xcb/xcb.h>
25 #include <xcb/xcb_atom.h>
26 #include <xcb/xcb_icccm.h>
28 #include "client.h"
29 #include "tag.h"
30 #include "window.h"
31 #include "ewmh.h"
32 #include "widget.h"
33 #include "screen.h"
34 #include "titlebar.h"
35 #include "lua.h"
36 #include "stack.h"
37 #include "mouse.h"
38 #include "systray.h"
39 #include "layouts/floating.h"
40 #include "common/markup.h"
41 #include "common/atoms.h"
43 extern awesome_t globalconf;
44 extern const name_func_link_t FloatingPlacementList[];
46 /** Create a new client userdata.
47 * \param L The Lua VM state.
48 * \param p A client pointer.
49 * \param The number of elements pushed on the stack.
51 int
52 luaA_client_userdata_new(lua_State *L, client_t *p)
54 client_t **pp = lua_newuserdata(L, sizeof(client_t *));
55 *pp = p;
56 client_ref(pp);
57 return luaA_settype(L, "client");
60 DO_LUA_EQ(client_t, client, "client")
61 DO_LUA_GC(client_t, client, "client", client_unref)
63 /** Load windows properties, restoring client's tag
64 * and floating state before awesome was restarted if any.
65 * \param c A client pointer.
66 * \param screen A virtual screen.
67 * \return True if client had property, false otherwise.
69 static bool
70 client_loadprops(client_t * c, screen_t *screen)
72 ssize_t len;
73 tag_array_t *tags = &screen->tags;
74 char *prop = NULL;
76 if(!xutil_text_prop_get(globalconf.connection, c->win, _AWESOME_PROPERTIES,
77 &prop, &len))
78 return false;
80 if(len != tags->len + 2)
82 /* ignore property if the tag count isn't matching */
83 p_delete(&prop);
84 return false;
87 for(int i = 0; i < tags->len; i++)
88 if(prop[i] == '1')
89 tag_client(c, tags->tab[i]);
90 else
91 untag_client(c, tags->tab[i]);
93 client_setlayer(c, prop[tags->len + 1] - '0');
94 client_setfloating(c, prop[tags->len] == '1');
95 p_delete(&prop);
96 return true;
99 /** Check if client supports protocol WM_DELETE_WINDOW.
100 * \param win The window.
101 * \return True if client has WM_DELETE_WINDOW, false otherwise.
103 static bool
104 window_isprotodel(xcb_window_t win)
106 uint32_t i, n;
107 xcb_atom_t *protocols;
108 bool ret = false;
110 if(xcb_get_wm_protocols(globalconf.connection, win, &n, &protocols))
112 for(i = 0; !ret && i < n; i++)
113 if(protocols[i] == WM_DELETE_WINDOW)
114 ret = true;
115 p_delete(&protocols);
117 return ret;
120 /** Returns true if a client is tagged
121 * with one of the tags of the specified screen.
122 * \param c The client to check.
123 * \param screen Virtual screen number.
124 * \return true if the client is visible, false otherwise.
126 bool
127 client_maybevisible(client_t *c, int screen)
129 if(c->screen == screen)
131 tag_array_t *tags = &globalconf.screens[screen].tags;
132 for(int i = 0; i < tags->len; i++)
133 if(tags->tab[i]->selected && is_client_tagged(c, tags->tab[i]))
134 return true;
136 return false;
139 /** Returns true if a client is tagged
140 * with one of the tags of the specified screen and is not hidden.
141 * \param c The client to check.
142 * \param screen Virtual screen number.
143 * \return true if the client is visible, false otherwise.
145 bool
146 client_isvisible(client_t *c, int screen)
148 return (!c->ishidden && client_maybevisible(c, screen));
151 /** Get a client by its window.
152 * \param w The client window to find.
153 * \return A client pointer if found, NULL otherwise.
155 client_t *
156 client_getbywin(xcb_window_t w)
158 client_t *c;
159 for(c = globalconf.clients; c && c->win != w; c = c->next);
160 return c;
163 /** Update client name attribute with its new title.
164 * \param c The client.
165 * \param Return true if it has been updated.
167 bool
168 client_updatetitle(client_t *c)
170 char *name, *utf8;
171 ssize_t len;
173 if(!xutil_text_prop_get(globalconf.connection, c->win, _NET_WM_NAME, &name, &len))
174 if(!xutil_text_prop_get(globalconf.connection, c->win, WM_NAME, &name, &len))
175 return false;
177 p_delete(&c->name);
179 if((utf8 = draw_iso2utf8(name, len)))
180 c->name = utf8;
181 else
182 c->name = name;
184 /* call hook */
185 luaA_client_userdata_new(globalconf.L, c);
186 luaA_dofunction(globalconf.L, globalconf.hooks.titleupdate, 1, 0);
188 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
190 return true;
193 /** Unfocus a client.
194 * \param c The client.
196 static void
197 client_unfocus(client_t *c)
199 globalconf.screens[c->screen].client_focus = NULL;
201 /* Call hook */
202 luaA_client_userdata_new(globalconf.L, c);
203 luaA_dofunction(globalconf.L, globalconf.hooks.unfocus, 1, 0);
205 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
206 ewmh_update_net_active_window(c->phys_screen);
209 /** Ban client and unmap it.
210 * \param c The client.
212 void
213 client_ban(client_t *c)
215 if(globalconf.screen_focus->client_focus == c)
216 client_unfocus(c);
217 xcb_unmap_window(globalconf.connection, c->win);
218 if(c->ishidden)
219 window_state_set(c->win, XCB_WM_ICONIC_STATE);
220 else
221 window_state_set(c->win, XCB_WM_WITHDRAWN_STATE);
222 if(c->titlebar && c->titlebar->position && c->titlebar->sw)
223 xcb_unmap_window(globalconf.connection, c->titlebar->sw->window);
226 /** Give focus to client, or to first client if client is NULL.
227 * \param c The client or NULL.
228 * \return True if a window (even root) has received focus, false otherwise.
230 static void
231 client_focus(client_t *c)
233 if(!client_maybevisible(c, c->screen))
234 return;
236 /* unfocus current selected client */
237 if(globalconf.screen_focus->client_focus
238 && c != globalconf.screen_focus->client_focus)
239 client_unfocus(globalconf.screen_focus->client_focus);
241 /* stop hiding c */
242 c->ishidden = false;
244 /* unban the client before focusing or it will fail */
245 client_unban(c);
247 globalconf.screen_focus = &globalconf.screens[c->screen];
248 globalconf.screen_focus->client_focus = c;
250 xcb_set_input_focus(globalconf.connection, XCB_INPUT_FOCUS_POINTER_ROOT,
251 c->win, XCB_CURRENT_TIME);
253 /* Some layouts use focused client differently, so call them back.
254 * And anyway, we have maybe unhidden */
255 globalconf.screens[c->screen].need_arrange = true;
257 /* execute hook */
258 luaA_client_userdata_new(globalconf.L, globalconf.screen_focus->client_focus);
259 luaA_dofunction(globalconf.L, globalconf.hooks.focus, 1, 0);
261 ewmh_update_net_active_window(c->phys_screen);
262 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
265 /** Restack clients.
266 * \todo It might be worth stopping to restack everyone and only stack `c'
267 * relatively to the first matching in the list.
269 void
270 client_stack(void)
272 uint32_t config_win_vals[2];
273 client_node_t *node;
274 layer_t layer;
275 statusbar_t *sb;
276 int screen;
277 xembed_window_t *emwin;
279 config_win_vals[0] = XCB_NONE;
280 config_win_vals[1] = XCB_STACK_MODE_BELOW;
282 /* first stack fullscreen and modal windows */
283 for(layer = LAYER_OUTOFSPACE - 1; layer >= LAYER_FULLSCREEN; layer--)
284 for(node = globalconf.stack; node; node = node->next)
285 if(node->client->layer == layer)
287 if(node->client->titlebar
288 && node->client->titlebar->sw
289 && node->client->titlebar->position)
291 xcb_configure_window(globalconf.connection,
292 node->client->titlebar->sw->window,
293 XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE,
294 config_win_vals);
295 config_win_vals[0] = node->client->titlebar->sw->window;
297 xcb_configure_window(globalconf.connection, node->client->win,
298 XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE,
299 config_win_vals);
300 config_win_vals[0] = node->client->win;
303 /* then stack systray windows */
304 for(emwin = globalconf.embedded; emwin; emwin = emwin->next)
306 xcb_configure_window(globalconf.connection,
307 emwin->win,
308 XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE,
309 config_win_vals);
310 config_win_vals[0] = emwin->win;
313 /* then stack statusbar window */
314 for(screen = 0; screen < globalconf.screens_info->nscreen; screen++)
315 for(sb = globalconf.screens[screen].statusbar; sb; sb = sb->next)
316 if(sb->sw)
318 xcb_configure_window(globalconf.connection,
319 sb->sw->window,
320 XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE,
321 config_win_vals);
322 config_win_vals[0] = sb->sw->window;
325 /* finally stack everything else */
326 for(layer = LAYER_FULLSCREEN - 1; layer >= LAYER_DESKTOP; layer--)
327 for(node = globalconf.stack; node; node = node->next)
328 if(node->client->layer == layer)
330 if(node->client->titlebar
331 && node->client->titlebar->sw
332 && node->client->titlebar->position)
334 xcb_configure_window(globalconf.connection,
335 node->client->titlebar->sw->window,
336 XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE,
337 config_win_vals);
338 config_win_vals[0] = node->client->titlebar->sw->window;
340 xcb_configure_window(globalconf.connection, node->client->win,
341 XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE,
342 config_win_vals);
343 config_win_vals[0] = node->client->win;
347 /** Put client on top of the stack
348 * \param c The client to raise.
350 void
351 client_raise(client_t *c)
353 /* Push c on top of the stack. */
354 stack_client_push(c);
355 client_stack();
358 /** Manage a new client.
359 * \param w The window.
360 * \param wgeom Window geometry.
361 * \param screen Virtual screen number where to manage client.
363 void
364 client_manage(xcb_window_t w, xcb_get_geometry_reply_t *wgeom, int screen)
366 xcb_get_property_cookie_t ewmh_icon_cookie;
367 client_t *c, *t = NULL;
368 xcb_window_t trans;
369 bool rettrans, retloadprops;
370 xcb_size_hints_t *u_size_hints;
371 const uint32_t select_input_val[] =
373 XCB_EVENT_MASK_STRUCTURE_NOTIFY
374 | XCB_EVENT_MASK_PROPERTY_CHANGE
375 | XCB_EVENT_MASK_ENTER_WINDOW
378 /* Send request to get NET_WM_ICON property as soon as possible... */
379 ewmh_icon_cookie = ewmh_window_icon_get_unchecked(w);
380 xcb_change_window_attributes(globalconf.connection, w, XCB_CW_EVENT_MASK, select_input_val);
382 if(systray_iskdedockapp(w))
384 systray_request_handle(w, screen_virttophys(screen), NULL);
385 return;
388 c = p_new(client_t, 1);
390 c->screen = screen_get_bycoord(globalconf.screens_info, screen, wgeom->x, wgeom->y);
392 if(globalconf.screens_info->xinerama_is_active)
393 c->phys_screen = globalconf.default_screen;
394 else
395 c->phys_screen = c->screen;
397 /* Initial values */
398 c->win = w;
399 c->geometry.x = c->f_geometry.x = c->m_geometry.x = wgeom->x;
400 c->geometry.y = c->f_geometry.y = c->m_geometry.y = wgeom->y;
401 c->geometry.width = c->f_geometry.width = c->m_geometry.width = wgeom->width;
402 c->geometry.height = c->f_geometry.height = c->m_geometry.height = wgeom->height;
403 c->layer = c->oldlayer = LAYER_TILE;
404 client_setborder(c, wgeom->border_width);
405 c->icon = ewmh_window_icon_get_reply(ewmh_icon_cookie);
407 /* update hints */
408 u_size_hints = client_updatesizehints(c);
409 client_updatewmhints(c);
411 /* Try to load props if any */
412 if(!(retloadprops = client_loadprops(c, &globalconf.screens[screen])))
413 screen_client_moveto(c, screen, true);
415 /* Then check clients hints */
416 ewmh_check_client_hints(c);
418 /* check for transient and set tags like its parent */
419 if((rettrans = xcb_get_wm_transient_for(globalconf.connection, w, &trans))
420 && (t = client_getbywin(trans)))
422 tag_array_t *tags = &globalconf.screens[c->screen].tags;
423 for(int i = 0; i < tags->len; i++)
424 if(is_client_tagged(t, tags->tab[i]))
425 tag_client(c, tags->tab[i]);
428 /* should be floating if transsient or fixed */
429 if(rettrans || c->isfixed)
430 client_setfloating(c, true);
432 /* Push client in client list */
433 client_list_push(&globalconf.clients, c);
434 client_ref(&c);
435 /* Push client in stack */
436 client_raise(c);
438 /* update window title */
439 client_updatetitle(c);
441 ewmh_update_net_client_list(c->phys_screen);
442 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
444 /* call hook */
445 luaA_client_userdata_new(globalconf.L, c);
446 luaA_dofunction(globalconf.L, globalconf.hooks.manage, 1, 0);
448 if(c->floating_placement
449 && !retloadprops
450 && u_size_hints
451 && !(xcb_size_hints_get_flags(u_size_hints) & (XCB_SIZE_US_POSITION_HINT
452 | XCB_SIZE_P_POSITION_HINT)))
454 if(c->isfloating)
455 client_resize(c, c->floating_placement(c), false);
456 else
457 c->f_geometry = c->floating_placement(c);
460 if(u_size_hints)
461 xcb_free_size_hints(u_size_hints);
464 /** Compute client geometry with respect to its geometry hints.
465 * \param c The client.
466 * \param geometry The geometry that the client might receive.
467 * \return The geometry the client must take respecting its hints.
469 area_t
470 client_geometry_hints(client_t *c, area_t geometry)
472 double dx, dy, max, min, ratio;
474 if(c->minay > 0 && c->maxay > 0 && (geometry.height - c->baseh) > 0
475 && (geometry.width - c->basew) > 0)
477 dx = (double) (geometry.width - c->basew);
478 dy = (double) (geometry.height - c->baseh);
479 min = (double) (c->minax) / (double) (c->minay);
480 max = (double) (c->maxax) / (double) (c->maxay);
481 ratio = dx / dy;
482 if(max > 0 && min > 0 && ratio > 0)
484 if(ratio < min)
486 dy = (dx * min + dy) / (min * min + 1);
487 dx = dy * min;
488 geometry.width = (int) dx + c->basew;
489 geometry.height = (int) dy + c->baseh;
491 else if(ratio > max)
493 dy = (dx * min + dy) / (max * max + 1);
494 dx = dy * min;
495 geometry.width = (int) dx + c->basew;
496 geometry.height = (int) dy + c->baseh;
500 if(c->minw && geometry.width < c->minw)
501 geometry.width = c->minw;
502 if(c->minh && geometry.height < c->minh)
503 geometry.height = c->minh;
504 if(c->maxw && geometry.width > c->maxw)
505 geometry.width = c->maxw;
506 if(c->maxh && geometry.height > c->maxh)
507 geometry.height = c->maxh;
508 if(c->incw)
509 geometry.width -= (geometry.width - c->basew) % c->incw;
510 if(c->inch)
511 geometry.height -= (geometry.height - c->baseh) % c->inch;
513 return geometry;
516 /** Resize client window.
517 * \param c Client to resize.
518 * \param geometry New window geometry.
519 * \param hints Use size hints.
520 * \return True if the client has been resized.
522 bool
523 client_resize(client_t *c, area_t geometry, bool hints)
525 int new_screen;
526 area_t area;
527 layout_t *layout = layout_get_current(c->screen);
528 bool resized = false;
529 /* Values to configure a window is an array where values are
530 * stored according to 'value_mask' */
531 uint32_t values[5];
533 if(c->titlebar && !c->ismoving && !c->isfloating && layout != layout_floating)
534 geometry = titlebar_geometry_remove(c->titlebar, c->border, geometry);
536 if(hints)
537 geometry = client_geometry_hints(c, geometry);
539 if(geometry.width <= 0 || geometry.height <= 0)
540 return false;
542 /* offscreen appearance fixes */
543 area = display_area_get(c->phys_screen, NULL,
544 &globalconf.screens[c->screen].padding);
546 if(geometry.x > area.width)
547 geometry.x = area.width - geometry.width - 2 * c->border;
548 if(geometry.y > area.height)
549 geometry.y = area.height - geometry.height - 2 * c->border;
550 if(geometry.x + geometry.width + 2 * c->border < 0)
551 geometry.x = 0;
552 if(geometry.y + geometry.height + 2 * c->border < 0)
553 geometry.y = 0;
555 if(c->geometry.x != geometry.x || c->geometry.y != geometry.y
556 || c->geometry.width != geometry.width || c->geometry.height != geometry.height)
558 new_screen =
559 screen_get_bycoord(globalconf.screens_info, c->screen, geometry.x, geometry.y);
561 c->geometry.x = values[0] = geometry.x;
562 c->geometry.width = values[2] = geometry.width;
563 c->geometry.y = values[1] = geometry.y;
564 c->geometry.height = values[3] = geometry.height;
565 values[4] = c->border;
567 /* save the floating geometry if the window is floating but not
568 * maximized */
569 if(c->ismoving || c->isfloating
570 || layout_get_current(new_screen) == layout_floating)
572 titlebar_update_geometry_floating(c);
573 if(!c->ismax)
574 c->f_geometry = geometry;
577 xcb_configure_window(globalconf.connection, c->win,
578 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
579 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
580 XCB_CONFIG_WINDOW_BORDER_WIDTH,
581 values);
582 window_configure(c->win, geometry, c->border);
584 if(c->screen != new_screen)
585 screen_client_moveto(c, new_screen, false);
587 resized = true;
590 /* call it again like it was floating,
591 * we want it to be sticked to the window */
592 if(!c->ismoving && !c->isfloating && layout != layout_floating)
593 titlebar_update_geometry_floating(c);
595 return resized;
598 /* Set the client layer.
599 * \param c The client.
600 * \param layer The layer.
602 void
603 client_setlayer(client_t *c, layer_t layer)
605 c->layer = layer;
606 client_raise(c);
607 client_saveprops(c);
610 /** Set a clinet floating.
611 * \param c The client.
612 * \param floating Set floating, or not.
613 * \param layer Layer to put the floating window onto.
615 void
616 client_setfloating(client_t *c, bool floating)
618 if(c->isfloating != floating)
620 if((c->isfloating = floating))
622 client_setlayer(c, MAX(c->layer, LAYER_FLOAT));
623 client_resize(c, c->f_geometry, false);
624 titlebar_update_geometry_floating(c);
626 else if(c->ismax)
628 c->ismax = false;
629 client_setlayer(c, c->oldlayer);
630 client_resize(c, c->m_geometry, false);
632 else
633 client_setlayer(c, c->oldlayer);
634 if(client_isvisible(c, c->screen))
635 globalconf.screens[c->screen].need_arrange = true;
636 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
637 client_saveprops(c);
641 /** Save client properties as an X property.
642 * \param c The client.
644 void
645 client_saveprops(client_t *c)
647 tag_array_t *tags = &globalconf.screens[c->screen].tags;
648 unsigned char *prop = p_alloca(unsigned char, tags->len + 3);
649 int i;
651 for(i = 0; i < tags->len; i++)
652 prop[i] = is_client_tagged(c, tags->tab[i]) ? '1' : '0';
654 prop[i++] = c->isfloating ? '1' : '0';
655 prop[i++] = '0' + c->layer;
657 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE, c->win, _AWESOME_PROPERTIES, STRING, 8, i, prop);
660 /** Unban a client.
661 * \param c The client.
663 void
664 client_unban(client_t *c)
666 xcb_map_window(globalconf.connection, c->win);
667 window_state_set(c->win, XCB_WM_NORMAL_STATE);
668 if(c->titlebar && c->titlebar->sw && c->titlebar->position)
669 xcb_map_window(globalconf.connection, c->titlebar->sw->window);
672 /** Unmanage a client.
673 * \param c The client.
675 void
676 client_unmanage(client_t *c)
678 tag_array_t *tags = &globalconf.screens[c->screen].tags;
680 if(globalconf.screens[c->screen].client_focus == c)
681 client_unfocus(c);
683 /* call hook */
684 luaA_client_userdata_new(globalconf.L, c);
685 luaA_dofunction(globalconf.L, globalconf.hooks.unmanage, 1, 0);
687 /* The server grab construct avoids race conditions. */
688 xcb_grab_server(globalconf.connection);
690 xcb_configure_window(globalconf.connection, c->win,
691 XCB_CONFIG_WINDOW_BORDER_WIDTH,
692 (uint32_t *) &c->oldborder);
694 xcb_ungrab_button(globalconf.connection, XCB_BUTTON_INDEX_ANY, c->win,
695 XUTIL_ANY_MODIFIER);
696 window_state_set(c->win, XCB_WM_WITHDRAWN_STATE);
698 xcb_aux_sync(globalconf.connection);
699 xcb_ungrab_server(globalconf.connection);
701 /* remove client everywhere */
702 client_list_detach(&globalconf.clients, c);
703 stack_client_delete(c);
704 for(int i = 0; i < tags->len; i++)
705 untag_client(c, tags->tab[i]);
707 if(c->titlebar)
709 simplewindow_delete(&c->titlebar->sw);
710 titlebar_unref(&c->titlebar);
713 ewmh_update_net_client_list(c->phys_screen);
715 /* delete properties */
716 xcb_delete_property(globalconf.connection, c->win, _AWESOME_PROPERTIES);
718 /* set client as invalid */
719 c->invalid = true;
721 client_unref(&c);
724 /** Update the WM hints of a client.
725 * \param c The client.
727 void
728 client_updatewmhints(client_t *c)
730 xcb_wm_hints_t *wmh;
731 uint32_t wm_hints_flags;
733 if((wmh = xcb_get_wm_hints(globalconf.connection, c->win)))
735 wm_hints_flags = xcb_wm_hints_get_flags(wmh);
736 if((c->isurgent = xcb_wm_hints_get_urgency(wmh)))
738 /* execute hook */
739 luaA_client_userdata_new(globalconf.L, c);
740 luaA_dofunction(globalconf.L, globalconf.hooks.urgent, 1, 0);
742 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
744 if((wm_hints_flags & XCB_WM_STATE_HINT) &&
745 (xcb_wm_hints_get_initial_state(wmh) == XCB_WM_WITHDRAWN_STATE))
747 client_setborder(c, 0);
748 c->skip = true;
750 xcb_free_wm_hints(wmh);
754 /** Update the size hints of a client.
755 * \param c The client.
756 * \return A pointer to a xcb_size_hints_t.
758 xcb_size_hints_t *
759 client_updatesizehints(client_t *c)
761 long msize;
762 xcb_size_hints_t *size;
763 uint32_t size_flags;
765 if(!(size = xcb_get_wm_normal_hints(globalconf.connection, c->win, &msize)))
766 return NULL;
768 size_flags = xcb_size_hints_get_flags(size);
770 if((size_flags & XCB_SIZE_P_SIZE_HINT))
771 xcb_size_hints_get_base_size(size, &c->basew, &c->baseh);
772 else if((size_flags & XCB_SIZE_P_MIN_SIZE_HINT))
773 xcb_size_hints_get_min_size(size, &c->basew, &c->baseh);
774 else
775 c->basew = c->baseh = 0;
776 if((size_flags & XCB_SIZE_P_RESIZE_INC_HINT))
777 xcb_size_hints_get_increase(size, &c->incw, &c->inch);
778 else
779 c->incw = c->inch = 0;
781 if((size_flags & XCB_SIZE_P_MAX_SIZE_HINT))
782 xcb_size_hints_get_max_size(size, &c->maxw, &c->maxh);
783 else
784 c->maxw = c->maxh = 0;
786 if((size_flags & XCB_SIZE_P_MIN_SIZE_HINT))
787 xcb_size_hints_get_min_size(size, &c->minw, &c->minh);
788 else if((size_flags & XCB_SIZE_BASE_SIZE_HINT))
789 xcb_size_hints_get_base_size(size, &c->minw, &c->minh);
790 else
791 c->minw = c->minh = 0;
793 if((size_flags & XCB_SIZE_P_ASPECT_HINT))
795 xcb_size_hints_get_min_aspect(size, &c->minax, &c->minay);
796 xcb_size_hints_get_max_aspect(size, &c->maxax, &c->maxay);
798 else
799 c->minax = c->maxax = c->minay = c->maxay = 0;
801 if(c->maxw && c->minw && c->maxh && c->minh
802 && c->maxw == c->minw && c->maxh == c->minh)
803 c->isfixed = true;
805 c->hassizehints = !(!c->basew && !c->baseh && !c->incw && !c->inch
806 && !c->maxw && !c->maxh && !c->minw && !c->minh
807 && !c->minax && !c->maxax && !c->minax && !c->minay);
808 return size;
811 /** Kill a client via a WM_DELETE_WINDOW request or XKillClient if not
812 * supported.
813 * \param c The client to kill.
815 void
816 client_kill(client_t *c)
818 xcb_client_message_event_t ev;
820 if(window_isprotodel(c->win))
822 /* Initialize all of event's fields first */
823 p_clear(&ev, 1);
825 ev.response_type = XCB_CLIENT_MESSAGE;
826 ev.window = c->win;
827 ev.format = 32;
828 ev.data.data32[1] = XCB_CURRENT_TIME;
829 ev.type = WM_PROTOCOLS;
830 ev.data.data32[0] = WM_DELETE_WINDOW;
832 xcb_send_event(globalconf.connection, false, c->win,
833 XCB_EVENT_MASK_NO_EVENT, (char *) &ev);
835 else
836 xcb_kill_client(globalconf.connection, c->win);
839 /** Get all clients into a table.
840 * \param L The Lua VM state.
841 * \luastack
842 * \lreturn A table with all clients.
844 static int
845 luaA_client_get(lua_State *L)
847 int i = 1;
848 client_t *c;
850 lua_newtable(L);
852 for(c = globalconf.clients; c; c = c->next)
854 luaA_client_userdata_new(globalconf.L, c);
855 lua_rawseti(L, -2, i++);
858 return 1;
861 /** Add mouse bindings over clients's window.
862 * \param L The Lua VM state.
863 * \luastack
864 * \lvalue A client.
865 * \lparam A button binding.
867 static int
868 luaA_client_mouse_add(lua_State *L)
870 client_t **c = luaA_checkudata(L, 1, "client");
871 button_t **b = luaA_checkudata(L, 2, "mouse");
872 button_list_push(&(*c)->buttons, *b);
873 button_ref(b);
874 return 0;
877 /** Remove mouse bindings over clients's window.
878 * \param L The Lua VM state.
879 * \luastack
880 * \lvalue A client.
881 * \lparam A button binding.
883 static int
884 luaA_client_mouse_remove(lua_State *L)
886 client_t **c = luaA_checkudata(L, 1, "client");
887 button_t **b = luaA_checkudata(L, 1, "mouse");
888 button_list_detach(&(*c)->buttons, *b);
889 button_unref(b);
890 return 0;
893 /** Get only visible clients for a screen.
894 * \param L The Lua VM state.
895 * \luastack
896 * \lparam A screen number.
897 * \lreturn A table with all visible clients for this screen.
899 static int
900 luaA_client_visible_get(lua_State *L)
902 int i = 1;
903 client_t *c;
904 int screen = luaL_checknumber(L, 1) - 1;
906 luaA_checkscreen(screen);
908 lua_newtable(L);
910 for(c = globalconf.clients; c; c = c->next)
911 if(!c->skip && !c->ishidden && client_isvisible(c, screen))
913 luaA_client_userdata_new(globalconf.L, c);
914 lua_rawseti(L, -2, i++);
917 return 1;
920 /** Get the currently focused client (DEPRECATED).
921 * \param L The Lua VM state.
922 * \luastack
923 * \lreturn The currently focused client.
925 static int
926 luaA_client_focus_get(lua_State *L)
928 if(globalconf.screen_focus->client_focus)
929 return luaA_client_userdata_new(L, globalconf.screen_focus->client_focus);
930 deprecate();
931 return 0;
934 /** Set client border width.
935 * \param c The client.
936 * \param width The border width.
938 void
939 client_setborder(client_t *c, int width)
941 uint32_t w = width;
943 if((c->noborder && width > 0) || width == c->border || width < 0)
944 return;
946 c->border = width;
947 xcb_configure_window(globalconf.connection, c->win,
948 XCB_CONFIG_WINDOW_BORDER_WIDTH, &w);
950 if(c->isfloating || layout_get_current(c->screen) == layout_floating)
951 titlebar_update_geometry_floating(c);
952 else
953 globalconf.screens[c->screen].need_arrange = true;
956 /** Kill a client.
957 * \param L The Lua VM state.
959 * \luastack
960 * \lvalue A client.
962 static int
963 luaA_client_kill(lua_State *L)
965 client_t **c = luaA_checkudata(L, 1, "client");
966 client_kill(*c);
967 return 0;
970 /** Swap a client with another one.
971 * \param L The Lua VM state.
972 * \luastack
973 * \lvalue A client.
974 * \lparam A client to swap with.
976 static int
977 luaA_client_swap(lua_State *L)
979 client_t **c = luaA_checkudata(L, 1, "client");
980 client_t **swap = luaA_checkudata(L, 2, "client");
981 client_list_swap(&globalconf.clients, *swap, *c);
982 globalconf.screens[(*c)->screen].need_arrange = true;
983 globalconf.screens[(*swap)->screen].need_arrange = true;
984 widget_invalidate_cache((*c)->screen, WIDGET_CACHE_CLIENTS);
985 widget_invalidate_cache((*swap)->screen, WIDGET_CACHE_CLIENTS);
986 return 0;
989 /** Access or set the client tags.
990 * \param L The Lua VM state.
991 * \return The number of elements pushed on stack.
992 * \lparam A table with tags to set, or none to get the current tags table.
993 * \return The clients tag.
995 static int
996 luaA_client_tags(lua_State *L)
998 tag_array_t *tags;
999 tag_t **tag;
1000 client_t **c = luaA_checkudata(L, 1, "client");
1002 if(lua_gettop(L) == 2)
1004 luaA_checktable(L, 2);
1005 tags = &globalconf.screens[(*c)->screen].tags;
1006 for(int i = 0; i < tags->len; i++)
1007 untag_client(*c, tags->tab[i]);
1008 lua_pushnil(L);
1009 while(lua_next(L, 2))
1011 tag = luaA_checkudata(L, -1, "tag");
1012 tag_client(*c, *tag);
1013 lua_pop(L, 1);
1016 else
1018 tags = &globalconf.screens[(*c)->screen].tags;
1019 luaA_otable_new(L);
1020 for(int i = 0; i < tags->len; i++)
1021 if(is_client_tagged(*c, tags->tab[i]))
1023 luaA_tag_userdata_new(L, tags->tab[i]);
1024 luaA_tag_userdata_new(L, tags->tab[i]);
1025 lua_rawset(L, -3);
1029 return 1;
1032 /** Focus a client (DEPRECATED).
1033 * \param L The Lua VM state.
1035 * \luastack
1036 * \lvalue A client.
1038 static int
1039 luaA_client_focus_set(lua_State *L)
1041 client_t **c = luaA_checkudata(L, 1, "client");
1042 client_focus(*c);
1043 deprecate();
1044 return 0;
1047 /** Raise a client on top of others which are on the same layer.
1048 * \param L The Lua VM state.
1050 * \luastack
1051 * \lvalue A client.
1053 static int
1054 luaA_client_raise(lua_State *L)
1056 client_t **c = luaA_checkudata(L, 1, "client");
1057 client_raise(*c);
1058 return 0;
1061 /** Redraw a client by unmapping and mapping it quickly.
1062 * \param L The Lua VM state.
1064 * \luastack
1065 * \lvalue A client.
1067 static int
1068 luaA_client_redraw(lua_State *L)
1070 client_t **c = luaA_checkudata(L, 1, "client");
1071 xcb_unmap_window(globalconf.connection, (*c)->win);
1072 xcb_map_window(globalconf.connection, (*c)->win);
1073 return 0;
1076 /** Return a formated string for a client.
1077 * \param L The Lua VM state.
1078 * \luastack
1079 * \lvalue A client.
1080 * \lreturn A string.
1082 static int
1083 luaA_client_tostring(lua_State *L)
1085 client_t **p = luaA_checkudata(L, 1, "client");
1086 lua_pushfstring(L, "[client udata(%p) name(%s)]", *p, (*p)->name);
1087 return 1;
1090 /** Stop managing a client.
1091 * \param L The Lua VM state.
1092 * \luastack
1093 * \lvalue A client.
1095 static int
1096 luaA_client_unmanage(lua_State *L)
1098 client_t **c = luaA_checkudata(L, 1, "client");
1099 client_unmanage(*c);
1100 return 0;
1103 /** Client newindex.
1104 * \param L The Lua VM state.
1105 * \return The number of elements pushed on stack.
1108 luaA_client_newindex(lua_State *L)
1110 size_t len;
1111 client_t **c = luaA_checkudata(L, 1, "client");
1112 const char *buf = luaL_checklstring(L, 2, &len);
1113 bool b;
1114 double d;
1115 int i;
1116 titlebar_t **t = NULL;
1118 if((*c)->invalid)
1119 luaL_error(L, "client is invalid\n");
1121 switch(a_tokenize(buf, len))
1123 case A_TK_NAME:
1124 buf = luaL_checklstring(L, 3, &len);
1125 p_delete(&(*c)->name);
1126 a_iso2utf8(&(*c)->name, buf, len);
1127 widget_invalidate_cache((*c)->screen, WIDGET_CACHE_CLIENTS);
1128 break;
1129 case A_TK_FLOATING_PLACEMENT:
1130 (*c)->floating_placement = name_func_lookup(luaL_checkstring(L, 3),
1131 FloatingPlacementList);
1132 break;
1133 case A_TK_SCREEN:
1134 if(globalconf.screens_info->xinerama_is_active)
1136 i = luaL_checknumber(L, 3) - 1;
1137 luaA_checkscreen(i);
1138 if(i != (*c)->screen)
1139 screen_client_moveto(*c, i, true);
1141 break;
1142 case A_TK_HIDE:
1143 b = luaA_checkboolean(L, 3);
1144 if(b != (*c)->ishidden)
1146 (*c)->ishidden = b;
1147 if(client_isvisible(*c, (*c)->screen))
1148 globalconf.screens[(*c)->screen].need_arrange = true;
1150 break;
1151 case A_TK_ICON_PATH:
1152 buf = luaL_checkstring(L, 3);
1153 p_delete(&(*c)->icon_path);
1154 (*c)->icon_path = a_strdup(buf);
1155 widget_invalidate_cache((*c)->screen, WIDGET_CACHE_CLIENTS);
1156 break;
1157 case A_TK_OPACITY:
1158 d = luaL_checknumber(L, 3);
1159 if(d == -1 || (d >= 0 && d <= 1))
1160 window_trans_set((*c)->win, d);
1161 break;
1162 case A_TK_FLOATING:
1163 client_setfloating(*c, luaA_checkboolean(L, 3));
1164 break;
1165 case A_TK_HONORSIZEHINTS:
1166 (*c)->honorsizehints = luaA_checkboolean(L, 3);
1167 if(client_isvisible(*c, (*c)->screen))
1168 globalconf.screens[(*c)->screen].need_arrange = true;
1169 break;
1170 case A_TK_BORDER_WIDTH:
1171 client_setborder(*c, luaL_checknumber(L, 3));
1172 break;
1173 case A_TK_BORDER_COLOR:
1174 if((buf = luaL_checklstring(L, 3, &len))
1175 && xcolor_init_reply(globalconf.connection,
1176 xcolor_init_unchecked(globalconf.connection,
1177 &(*c)->border_color,
1178 (*c)->phys_screen, buf,
1179 len)))
1180 xcb_change_window_attributes(globalconf.connection, (*c)->win,
1181 XCB_CW_BORDER_PIXEL, &(*c)->border_color.pixel);
1182 break;
1183 case A_TK_COORDS:
1184 if((*c)->isfloating || layout_get_current((*c)->screen) == layout_floating)
1186 area_t geometry;
1187 luaA_checktable(L, 3);
1188 geometry.x = luaA_getopt_number(L, 3, "x", (*c)->geometry.x);
1189 geometry.y = luaA_getopt_number(L, 3, "y", (*c)->geometry.y);
1190 geometry.width = luaA_getopt_number(L, 3, "width", (*c)->geometry.width);
1191 geometry.height = luaA_getopt_number(L, 3, "height", (*c)->geometry.height);
1192 client_resize(*c, geometry, false);
1194 break;
1195 case A_TK_TITLEBAR:
1196 if(!lua_isnil(L, 3))
1198 t = luaA_checkudata(L, 3, "titlebar");
1199 if(client_getbytitlebar(*t))
1200 luaL_error(L, "titlebar is already used by another client");
1203 /* If client had a titlebar, unref it */
1204 if((*c)->titlebar)
1206 simplewindow_delete(&(*c)->titlebar->sw);
1207 titlebar_unref(&(*c)->titlebar);
1208 globalconf.screens[(*c)->screen].need_arrange = true;
1211 if(t)
1213 /* Attach titlebar to client */
1214 (*c)->titlebar = *t;
1215 titlebar_ref(t);
1216 titlebar_init(*c);
1218 break;
1219 default:
1220 return 0;
1223 return 0;
1226 /** Client object.
1227 * \param L The Lua VM state.
1228 * \return The number of elements pushed on stack.
1229 * \luastack
1230 * \lfield name The client title.
1231 * \lfield class The client class.
1232 * \lfield instance The client instance.
1233 * \lfield pid The client PID, if available.
1234 * \lfield machine The machine client is running on.
1235 * \lfield icon_name The client name when iconified.
1236 * \lfield floating_placement The floating placement used for this client.
1237 * \lfield screen Client screen number.
1238 * \lfield hide Define if the client must be hidden, i.e. never mapped.
1239 * \lfield icon_path Path to the icon used to identify.
1240 * \lfield floating True always floating.
1241 * \lfield honorsizehints Honor size hints, i.e. respect size ratio.
1242 * \lfield border_width The client border width.
1243 * \lfield border_color The client border color.
1244 * \lfield coords The client coordinates.
1245 * \lfield titlebar The client titlebar.
1246 * \lfield urgent The client urgent state.
1247 * \lfield focus The focused client.
1249 static int
1250 luaA_client_index(lua_State *L)
1252 size_t len;
1253 ssize_t slen;
1254 client_t **c = luaA_checkudata(L, 1, "client");
1255 const char *buf = luaL_checklstring(L, 2, &len);
1256 char *value;
1257 void *data;
1258 xutil_class_hint_t hint;
1259 xcb_get_property_cookie_t prop_c;
1260 xcb_get_property_reply_t *prop_r = NULL;
1262 if((*c)->invalid)
1263 luaL_error(L, "client is invalid\n");
1265 if(luaA_usemetatable(L, 1, 2))
1266 return 1;
1268 switch(a_tokenize(buf, len))
1270 case A_TK_NAME:
1271 lua_pushstring(L, (*c)->name);
1272 break;
1273 case A_TK_CLASS:
1274 if(!xutil_class_hint_get(globalconf.connection, (*c)->win, &hint))
1275 return 0;
1276 lua_pushstring(L, hint.res_class);
1277 break;
1278 case A_TK_INSTANCE:
1279 if(!xutil_class_hint_get(globalconf.connection, (*c)->win, &hint))
1280 return 0;
1281 lua_pushstring(L, hint.res_name);
1282 break;
1283 case A_TK_PID:
1284 prop_c = xcb_get_property_unchecked(globalconf.connection, false, (*c)->win, _NET_WM_PID, CARDINAL, 0L, 1L);
1285 prop_r = xcb_get_property_reply(globalconf.connection, prop_c, NULL);
1287 if(prop_r && prop_r->value_len && (data = xcb_get_property_value(prop_r)))
1288 lua_pushnumber(L, *(uint32_t *)data);
1289 else
1291 p_delete(&prop_r);
1292 return 0;
1294 break;
1295 case A_TK_MACHINE:
1296 if(!xutil_text_prop_get(globalconf.connection, (*c)->win,
1297 WM_CLIENT_MACHINE, &value, &slen))
1298 return 0;
1299 lua_pushlstring(L, value, slen);
1300 p_delete(&value);
1301 break;
1302 case A_TK_ICON_NAME:
1303 if(!xutil_text_prop_get(globalconf.connection, (*c)->win,
1304 _NET_WM_ICON_NAME, &value, &slen))
1305 if(!xutil_text_prop_get(globalconf.connection, (*c)->win,
1306 WM_ICON_NAME, &value, &slen))
1307 return 0;
1308 lua_pushlstring(L, value, slen);
1309 p_delete(&value);
1310 break;
1311 case A_TK_FLOATING_PLACEMENT:
1312 lua_pushstring(L, name_func_rlookup((*c)->floating_placement,
1313 FloatingPlacementList));
1314 break;
1315 case A_TK_SCREEN:
1316 lua_pushnumber(L, 1 + (*c)->screen);
1317 break;
1318 case A_TK_HIDE:
1319 lua_pushboolean(L, (*c)->ishidden);
1320 break;
1321 case A_TK_ICON_PATH:
1322 lua_pushstring(L, (*c)->icon_path);
1323 break;
1324 case A_TK_FLOATING:
1325 lua_pushboolean(L, (*c)->isfloating);
1326 break;
1327 case A_TK_HONORSIZEHINTS:
1328 lua_pushboolean(L, (*c)->honorsizehints);
1329 break;
1330 case A_TK_BORDER_WIDTH:
1331 lua_pushnumber(L, (*c)->border);
1332 break;
1333 case A_TK_BORDER_COLOR:
1334 luaA_pushcolor(L, &(*c)->border_color);
1335 break;
1336 case A_TK_COORDS:
1337 lua_newtable(L);
1338 lua_pushnumber(L, (*c)->geometry.width);
1339 lua_setfield(L, -2, "width");
1340 lua_pushnumber(L, (*c)->geometry.height);
1341 lua_setfield(L, -2, "height");
1342 lua_pushnumber(L, (*c)->geometry.x);
1343 lua_setfield(L, -2, "x");
1344 lua_pushnumber(L, (*c)->geometry.y);
1345 lua_setfield(L, -2, "y");
1346 break;
1347 case A_TK_TITLEBAR:
1348 if((*c)->titlebar)
1349 return luaA_titlebar_userdata_new(globalconf.L, (*c)->titlebar);
1350 return 0;
1351 case A_TK_URGENT:
1352 lua_pushboolean(L, (*c)->isurgent);
1353 break;
1354 default:
1355 return 0;
1358 return 1;
1361 /* Client module.
1362 * \param L The Lua VM state.
1363 * \return The number of pushed elements.
1365 static int
1366 luaA_client_module_index(lua_State *L)
1368 size_t len;
1369 const char *buf = luaL_checklstring(L, 2, &len);
1371 switch(a_tokenize(buf, len))
1373 case A_TK_FOCUS:
1374 if(globalconf.screen_focus->client_focus)
1375 luaA_client_userdata_new(L, globalconf.screen_focus->client_focus);
1376 else
1377 return 0;
1378 break;
1379 default:
1380 return 0;
1383 return 1;
1386 /* Client module new index.
1387 * \param L The Lua VM state.
1388 * \return The number of pushed elements.
1390 static int
1391 luaA_client_module_newindex(lua_State *L)
1393 size_t len;
1394 const char *buf = luaL_checklstring(L, 2, &len);
1395 client_t **c;
1397 switch(a_tokenize(buf, len))
1399 case A_TK_FOCUS:
1400 c = luaA_checkudata(L, 3, "client");
1401 client_focus(*c);
1402 break;
1403 default:
1404 break;
1407 return 0;
1410 const struct luaL_reg awesome_client_methods[] =
1412 { "get", luaA_client_get },
1413 { "focus_get", luaA_client_focus_get },
1414 { "visible_get", luaA_client_visible_get },
1415 { "__index", luaA_client_module_index },
1416 { "__newindex", luaA_client_module_newindex },
1417 { NULL, NULL }
1419 const struct luaL_reg awesome_client_meta[] =
1421 { "tags", luaA_client_tags },
1422 { "kill", luaA_client_kill },
1423 { "swap", luaA_client_swap },
1424 { "focus_set", luaA_client_focus_set },
1425 { "raise", luaA_client_raise },
1426 { "redraw", luaA_client_redraw },
1427 { "mouse_resize", luaA_client_mouse_resize },
1428 { "mouse_move", luaA_client_mouse_move },
1429 { "unmanage", luaA_client_unmanage },
1430 { "mouse_add", luaA_client_mouse_add },
1431 { "mouse_remove", luaA_client_mouse_remove },
1432 { "__index", luaA_client_index },
1433 { "__newindex", luaA_client_newindex },
1434 { "__eq", luaA_client_eq },
1435 { "__gc", luaA_client_gc },
1436 { "__tostring", luaA_client_tostring },
1437 { NULL, NULL }
1440 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80