Fix wibox.layout.rotate
[awesome.git] / objects / window.c
blob937dac259fc0eab7f2d2fe91ed7d5bf232c1b483
1 /*
2 * window.c - window object
4 * Copyright © 2009 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 "luaa.h"
23 #include "xwindow.h"
24 #include "ewmh.h"
25 #include "screen.h"
26 #include "objects/window.h"
27 #include "common/atoms.h"
28 #include "common/luaobject.h"
30 LUA_CLASS_FUNCS(window, window_class)
32 static xcb_window_t
33 window_get(window_t *window)
35 if (window->frame_window != XCB_NONE)
36 return window->frame_window;
37 return window->window;
40 static void
41 window_wipe(window_t *window)
43 button_array_wipe(&window->buttons);
46 /** Get or set mouse buttons bindings on a window.
47 * \param L The Lua VM state.
48 * \return The number of elements pushed on the stack.
50 static int
51 luaA_window_buttons(lua_State *L)
53 window_t *window = luaA_checkudata(L, 1, &window_class);
55 if(lua_gettop(L) == 2)
57 luaA_button_array_set(L, 1, 2, &window->buttons);
58 luaA_object_emit_signal(L, 1, "property::buttons", 0);
59 xwindow_buttons_grab(window_get(window), &window->buttons);
62 return luaA_button_array_get(L, 1, &window->buttons);
65 /** Return window struts (reserved space at the edge of the screen).
66 * \param L The Lua VM state.
67 * \return The number of elements pushed on stack.
69 static int
70 luaA_window_struts(lua_State *L)
72 window_t *window = luaA_checkudata(L, 1, &window_class);
74 if(lua_gettop(L) == 2)
76 luaA_tostrut(L, 2, &window->strut);
77 ewmh_update_strut(window->window, &window->strut);
78 luaA_object_emit_signal(L, 1, "property::struts", 0);
79 /* FIXME: Only emit if the workarea actually changed
80 * (= window is visible, only on the right screen)? */
81 foreach(s, globalconf.screens)
82 screen_emit_signal(L, s, "property::workarea", 0);
85 return luaA_pushstrut(L, window->strut);
88 /** Set a window opacity.
89 * \param L The Lua VM state.
90 * \param idx The index of the window on the stack.
91 * \param opacity The opacity value.
93 void
94 window_set_opacity(lua_State *L, int idx, double opacity)
96 window_t *window = luaA_checkudata(L, idx, &window_class);
98 if(window->opacity != opacity)
100 window->opacity = opacity;
101 xwindow_set_opacity(window_get(window), opacity);
102 luaA_object_emit_signal(L, idx, "property::opacity", 0);
106 /** Set a window opacity.
107 * \param L The Lua VM state.
108 * \param window The window object.
109 * \return The number of elements pushed on stack.
111 static int
112 luaA_window_set_opacity(lua_State *L, window_t *window)
114 if(lua_isnil(L, -1))
115 window_set_opacity(L, -3, -1);
116 else
118 double d = luaL_checknumber(L, -1);
119 if(d >= 0 && d <= 1)
120 window_set_opacity(L, -3, d);
122 return 0;
125 /** Get the window opacity.
126 * \param L The Lua VM state.
127 * \param window The window object.
128 * \return The number of elements pushed on stack.
130 static int
131 luaA_window_get_opacity(lua_State *L, window_t *window)
133 if(window->opacity >= 0)
134 lua_pushnumber(L, window->opacity);
135 else
136 /* Let's always return some "good" value */
137 lua_pushnumber(L, 1);
138 return 1;
141 /** Set the window border color.
142 * \param L The Lua VM state.
143 * \param window The window object.
144 * \return The number of elements pushed on stack.
146 static int
147 luaA_window_set_border_color(lua_State *L, window_t *window)
149 size_t len;
150 const char *color_name = luaL_checklstring(L, -1, &len);
152 if(color_name &&
153 color_init_reply(color_init_unchecked(&window->border_color, color_name, len)))
155 xwindow_set_border_color(window_get(window), &window->border_color);
156 luaA_object_emit_signal(L, -3, "property::border_color", 0);
159 return 0;
162 /** Set a window border width.
163 * \param L The Lua VM state.
164 * \param idx The window index.
165 * \param width The border width.
167 void
168 window_set_border_width(lua_State *L, int idx, int width)
170 window_t *window = luaA_checkudata(L, idx, &window_class);
172 if(width == window->border_width || width < 0)
173 return;
175 if(window->window)
176 xcb_configure_window(globalconf.connection, window_get(window),
177 XCB_CONFIG_WINDOW_BORDER_WIDTH,
178 (uint32_t[]) { width });
180 window->border_width = width;
182 luaA_object_emit_signal(L, idx, "property::border_width", 0);
185 /** Get the window type.
186 * \param L The Lua VM state.
187 * \param window The window object.
188 * \return The number of elements pushed on stack.
191 luaA_window_get_type(lua_State *L, window_t *w)
193 switch(w->type)
195 case WINDOW_TYPE_DESKTOP:
196 lua_pushliteral(L, "desktop");
197 break;
198 case WINDOW_TYPE_DOCK:
199 lua_pushliteral(L, "dock");
200 break;
201 case WINDOW_TYPE_SPLASH:
202 lua_pushliteral(L, "splash");
203 break;
204 case WINDOW_TYPE_DIALOG:
205 lua_pushliteral(L, "dialog");
206 break;
207 case WINDOW_TYPE_MENU:
208 lua_pushliteral(L, "menu");
209 break;
210 case WINDOW_TYPE_TOOLBAR:
211 lua_pushliteral(L, "toolbar");
212 break;
213 case WINDOW_TYPE_UTILITY:
214 lua_pushliteral(L, "utility");
215 break;
216 case WINDOW_TYPE_DROPDOWN_MENU:
217 lua_pushliteral(L, "dropdown_menu");
218 break;
219 case WINDOW_TYPE_POPUP_MENU:
220 lua_pushliteral(L, "popup_menu");
221 break;
222 case WINDOW_TYPE_TOOLTIP:
223 lua_pushliteral(L, "tooltip");
224 break;
225 case WINDOW_TYPE_NOTIFICATION:
226 lua_pushliteral(L, "notification");
227 break;
228 case WINDOW_TYPE_COMBO:
229 lua_pushliteral(L, "combo");
230 break;
231 case WINDOW_TYPE_DND:
232 lua_pushliteral(L, "dnd");
233 break;
234 case WINDOW_TYPE_NORMAL:
235 lua_pushliteral(L, "normal");
236 break;
237 default:
238 return 0;
240 return 1;
243 /** Set the window type.
244 * \param L The Lua VM state.
245 * \param window The window object.
246 * \return The number of elements pushed on stack.
249 luaA_window_set_type(lua_State *L, window_t *w)
251 window_type_t type;
252 const char *buf = luaL_checkstring(L, -1);
254 if (A_STREQ(buf, "desktop"))
255 type = WINDOW_TYPE_DESKTOP;
256 else if(A_STREQ(buf, "dock"))
257 type = WINDOW_TYPE_DOCK;
258 else if(A_STREQ(buf, "splash"))
259 type = WINDOW_TYPE_SPLASH;
260 else if(A_STREQ(buf, "dialog"))
261 type = WINDOW_TYPE_DIALOG;
262 else if(A_STREQ(buf, "menu"))
263 type = WINDOW_TYPE_MENU;
264 else if(A_STREQ(buf, "toolbar"))
265 type = WINDOW_TYPE_TOOLBAR;
266 else if(A_STREQ(buf, "utility"))
267 type = WINDOW_TYPE_UTILITY;
268 else if(A_STREQ(buf, "dropdown_menu"))
269 type = WINDOW_TYPE_DROPDOWN_MENU;
270 else if(A_STREQ(buf, "popup_menu"))
271 type = WINDOW_TYPE_POPUP_MENU;
272 else if(A_STREQ(buf, "tooltip"))
273 type = WINDOW_TYPE_TOOLTIP;
274 else if(A_STREQ(buf, "notification"))
275 type = WINDOW_TYPE_NOTIFICATION;
276 else if(A_STREQ(buf, "combo"))
277 type = WINDOW_TYPE_COMBO;
278 else if(A_STREQ(buf, "dnd"))
279 type = WINDOW_TYPE_DND;
280 else if(A_STREQ(buf, "normal"))
281 type = WINDOW_TYPE_NORMAL;
282 else
284 warn("Unknown window type '%s'", buf);
285 return 0;
288 if(w->type != type)
290 w->type = type;
291 if(w->window != XCB_WINDOW_NONE)
292 ewmh_update_window_type(w->window, window_translate_type(w->type));
293 luaA_object_emit_signal(globalconf.L, -3, "property::type", 0);
296 return 0;
299 /** Translate a window_type_t into the corresponding EWMH atom.
300 * @param type The type to return.
301 * @return The EWMH atom for this type.
303 uint32_t
304 window_translate_type(window_type_t type)
306 switch(type)
308 case WINDOW_TYPE_NORMAL:
309 return _NET_WM_WINDOW_TYPE_NORMAL;
310 case WINDOW_TYPE_DESKTOP:
311 return _NET_WM_WINDOW_TYPE_DESKTOP;
312 case WINDOW_TYPE_DOCK:
313 return _NET_WM_WINDOW_TYPE_DOCK;
314 case WINDOW_TYPE_SPLASH:
315 return _NET_WM_WINDOW_TYPE_SPLASH;
316 case WINDOW_TYPE_DIALOG:
317 return _NET_WM_WINDOW_TYPE_DIALOG;
318 case WINDOW_TYPE_MENU:
319 return _NET_WM_WINDOW_TYPE_MENU;
320 case WINDOW_TYPE_TOOLBAR:
321 return _NET_WM_WINDOW_TYPE_TOOLBAR;
322 case WINDOW_TYPE_UTILITY:
323 return _NET_WM_WINDOW_TYPE_UTILITY;
324 case WINDOW_TYPE_DROPDOWN_MENU:
325 return _NET_WM_WINDOW_TYPE_DROPDOWN_MENU;
326 case WINDOW_TYPE_POPUP_MENU:
327 return _NET_WM_WINDOW_TYPE_POPUP_MENU;
328 case WINDOW_TYPE_TOOLTIP:
329 return _NET_WM_WINDOW_TYPE_TOOLTIP;
330 case WINDOW_TYPE_NOTIFICATION:
331 return _NET_WM_WINDOW_TYPE_NOTIFICATION;
332 case WINDOW_TYPE_COMBO:
333 return _NET_WM_WINDOW_TYPE_COMBO;
334 case WINDOW_TYPE_DND:
335 return _NET_WM_WINDOW_TYPE_DND;
337 return _NET_WM_WINDOW_TYPE_NORMAL;
340 static int
341 luaA_window_set_border_width(lua_State *L, window_t *c)
343 window_set_border_width(L, -3, luaL_checknumber(L, -1));
344 return 0;
347 LUA_OBJECT_EXPORT_PROPERTY(window, window_t, window, lua_pushnumber)
348 LUA_OBJECT_EXPORT_PROPERTY(window, window_t, border_color, luaA_pushcolor)
349 LUA_OBJECT_EXPORT_PROPERTY(window, window_t, border_width, lua_pushnumber)
351 void
352 window_class_setup(lua_State *L)
354 static const struct luaL_Reg window_methods[] =
356 { NULL, NULL }
359 static const struct luaL_Reg window_meta[] =
361 { "struts", luaA_window_struts },
362 { "buttons", luaA_window_buttons },
363 { NULL, NULL }
366 luaA_class_setup(L, &window_class, "window", NULL,
367 NULL, (lua_class_collector_t) window_wipe, NULL,
368 luaA_class_index_miss_property, luaA_class_newindex_miss_property,
369 window_methods, window_meta);
371 luaA_class_add_property(&window_class, "window",
372 NULL,
373 (lua_class_propfunc_t) luaA_window_get_window,
374 NULL);
375 luaA_class_add_property(&window_class, "opacity",
376 (lua_class_propfunc_t) luaA_window_set_opacity,
377 (lua_class_propfunc_t) luaA_window_get_opacity,
378 (lua_class_propfunc_t) luaA_window_set_opacity);
379 luaA_class_add_property(&window_class, "border_color",
380 (lua_class_propfunc_t) luaA_window_set_border_color,
381 (lua_class_propfunc_t) luaA_window_get_border_color,
382 (lua_class_propfunc_t) luaA_window_set_border_color);
383 luaA_class_add_property(&window_class, "border_width",
384 (lua_class_propfunc_t) luaA_window_set_border_width,
385 (lua_class_propfunc_t) luaA_window_get_border_width,
386 (lua_class_propfunc_t) luaA_window_set_border_width);
388 signal_add(&window_class.signals, "property::border_color");
389 signal_add(&window_class.signals, "property::border_width");
390 signal_add(&window_class.signals, "property::buttons");
391 signal_add(&window_class.signals, "property::opacity");
392 signal_add(&window_class.signals, "property::struts");
393 signal_add(&window_class.signals, "property::type");
396 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80