FS#996: awful.ewmh: Connect to property::border_width signal
[awesome.git] / root.c
blobc6f570411c8793059da8ff04a3f28d8472ebbf21
1 /*
2 * root.c - root window management
4 * Copyright © 2008-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 <X11/keysym.h>
23 #include <X11/XF86keysym.h>
24 #include <xcb/xtest.h>
25 #include <cairo-xcb.h>
27 #include "globalconf.h"
28 #include "objects/button.h"
29 #include "objects/drawin.h"
30 #include "luaa.h"
31 #include "xwindow.h"
32 #include "common/xcursor.h"
33 #include "common/xutil.h"
35 static xcb_keycode_t
36 _string_to_key_code(const char *s)
38 xcb_keysym_t keysym;
39 xcb_keycode_t *keycodes;
41 keysym = XStringToKeysym(s);
42 keycodes = xcb_key_symbols_get_keycode(globalconf.keysyms, keysym);
44 if(keycodes) {
45 return keycodes[0]; /* XXX only returning the first is probably not
46 * the best */
47 } else {
48 return 0;
52 /** Send fake events. Usually the current focused client will get it.
53 * \param L The Lua VM state.
54 * \return The number of element pushed on stack.
55 * \luastack
56 * \lparam The event type: key_press, key_release, button_press, button_release
57 * or motion_notify.
58 * \lparam The detail: in case of a key event, this is the keycode to send, in
59 * case of a button event this is the number of the button. In case of a motion
60 * event, this is a boolean value which if true make the coordinates relatives.
61 * \lparam In case of a motion event, this is the X coordinate.
62 * \lparam In case of a motion event, this is the Y coordinate.
63 * If not specified, the current one is used.
65 static int
66 luaA_root_fake_input(lua_State *L)
68 if(!globalconf.have_xtest)
70 luaA_warn(L, "XTest extension is not available, cannot fake input.");
71 return 0;
74 const char *stype = luaL_checkstring(L, 1);
75 uint8_t type, detail;
76 int x = 0, y = 0;
78 if (A_STREQ(stype, "key_press"))
80 type = XCB_KEY_PRESS;
81 if(lua_type(L, 2) == LUA_TSTRING) {
82 detail = _string_to_key_code(lua_tostring(L, 2)); /* keysym */
83 } else {
84 detail = luaL_checknumber(L, 2); /* keycode */
87 else if(A_STREQ(stype, "key_release"))
89 type = XCB_KEY_RELEASE;
90 if(lua_type(L, 2) == LUA_TSTRING) {
91 detail = _string_to_key_code(lua_tostring(L, 2)); /* keysym */
92 } else {
93 detail = luaL_checknumber(L, 2); /* keycode */
96 else if(A_STREQ(stype, "button_press"))
98 type = XCB_BUTTON_PRESS;
99 detail = luaL_checknumber(L, 2); /* button number */
101 else if(A_STREQ(stype, "button_release"))
103 type = XCB_BUTTON_RELEASE;
104 detail = luaL_checknumber(L, 2); /* button number */
106 else if(A_STREQ(stype, "motion_notify"))
108 type = XCB_MOTION_NOTIFY;
109 detail = luaA_checkboolean(L, 2); /* relative to the current position or not */
110 x = luaL_checknumber(L, 3);
111 y = luaL_checknumber(L, 4);
113 else
114 return 0;
116 xcb_test_fake_input(globalconf.connection,
117 type,
118 detail,
119 XCB_CURRENT_TIME,
120 XCB_NONE,
121 x, y,
123 return 0;
126 /** Get or set global key bindings.
127 * This binding will be available when you'll press keys on root window.
128 * \param L The Lua VM state.
129 * \return The number of element pushed on stack.
130 * \luastack
131 * \lparam An array of key bindings objects, or nothing.
132 * \lreturn The array of key bindings objects of this client.
134 static int
135 luaA_root_keys(lua_State *L)
137 if(lua_gettop(L) == 1)
139 luaA_checktable(L, 1);
141 foreach(key, globalconf.keys)
142 luaA_object_unref(globalconf.L, *key);
144 key_array_wipe(&globalconf.keys);
145 key_array_init(&globalconf.keys);
147 lua_pushnil(L);
148 while(lua_next(L, 1))
149 key_array_append(&globalconf.keys, luaA_object_ref_class(L, -1, &key_class));
151 xcb_screen_t *s = globalconf.screen;
152 xcb_ungrab_key(globalconf.connection, XCB_GRAB_ANY, s->root, XCB_BUTTON_MASK_ANY);
153 xwindow_grabkeys(s->root, &globalconf.keys);
155 return 1;
158 lua_createtable(L, globalconf.keys.len, 0);
159 for(int i = 0; i < globalconf.keys.len; i++)
161 luaA_object_push(L, globalconf.keys.tab[i]);
162 lua_rawseti(L, -2, i + 1);
165 return 1;
168 /** Get or set global mouse bindings.
169 * This binding will be available when you'll click on root window.
170 * \param L The Lua VM state.
171 * \return The number of element pushed on stack.
172 * \luastack
173 * \lparam An array of mouse button bindings objects, or nothing.
174 * \lreturn The array of mouse button bindings objects.
176 static int
177 luaA_root_buttons(lua_State *L)
179 if(lua_gettop(L) == 1)
181 luaA_checktable(L, 1);
183 foreach(button, globalconf.buttons)
184 luaA_object_unref(globalconf.L, *button);
186 button_array_wipe(&globalconf.buttons);
187 button_array_init(&globalconf.buttons);
189 lua_pushnil(L);
190 while(lua_next(L, 1))
191 button_array_append(&globalconf.buttons, luaA_object_ref(L, -1));
193 return 1;
196 lua_createtable(L, globalconf.buttons.len, 0);
197 for(int i = 0; i < globalconf.buttons.len; i++)
199 luaA_object_push(L, globalconf.buttons.tab[i]);
200 lua_rawseti(L, -2, i + 1);
203 return 1;
206 /** Set the root cursor.
207 * \param L The Lua VM state.
208 * \return The number of element pushed on stack.
209 * \luastack
210 * \lparam A X cursor name.
212 static int
213 luaA_root_cursor(lua_State *L)
215 const char *cursor_name = luaL_checkstring(L, 1);
216 uint16_t cursor_font = xcursor_font_fromstr(cursor_name);
218 if(cursor_font)
220 uint32_t change_win_vals[] = { xcursor_new(globalconf.connection, cursor_font) };
222 xcb_change_window_attributes(globalconf.connection,
223 globalconf.screen->root,
224 XCB_CW_CURSOR,
225 change_win_vals);
227 else
228 luaA_warn(L, "invalid cursor %s", cursor_name);
230 return 0;
233 /** Get the drawins attached to a screen.
234 * \param L The Lua VM state.
235 * \return The number of element pushed on stack.
236 * \luastack
237 * \lreturn A table with all drawins.
239 static int
240 luaA_root_drawins(lua_State *L)
242 lua_createtable(L, globalconf.drawins.len, 0);
244 for(int i = 0; i < globalconf.drawins.len; i++)
246 luaA_object_push(L, globalconf.drawins.tab[i]);
247 lua_rawseti(L, -2, i + 1);
250 return 1;
253 /** Get the screen's wallpaper
254 * \param L The Lua VM state.
255 * \return The number of element pushed on stack.
256 * \luastack
257 * \lreturn A cairo surface for the wallpaper.
259 static int
260 luaA_root_wallpaper(lua_State *L)
262 xcb_get_property_cookie_t prop_c;
263 xcb_get_property_reply_t *prop_r;
264 xcb_pixmap_t *rootpix;
265 cairo_surface_t *surface;
266 int ret;
268 prop_c = xcb_get_property_unchecked(globalconf.connection, false,
269 globalconf.screen->root, _XROOTPMAP_ID, XCB_ATOM_PIXMAP, 0, 1);
270 prop_r = xcb_get_property_reply(globalconf.connection, prop_c, NULL);
272 if (!prop_r || !prop_r->value_len)
273 return 0;
275 rootpix = xcb_get_property_value(prop_r);
276 if (!rootpix)
277 return 0;
279 /* We can't query the pixmap's values (or even if that pixmap exists at
280 * all), so let's just assume that it uses the default visual and is as
281 * large as the root window. Everything else wouldn't make sense.
283 surface = cairo_xcb_surface_create(globalconf.connection, *rootpix, globalconf.default_visual,
284 globalconf.screen->width_in_pixels, globalconf.screen->height_in_pixels);
285 ret = oocairo_surface_push(globalconf.L, surface);
286 cairo_surface_destroy(surface);
287 return ret;
290 const struct luaL_reg awesome_root_lib[] =
292 { "buttons", luaA_root_buttons },
293 { "keys", luaA_root_keys },
294 { "cursor", luaA_root_cursor },
295 { "fake_input", luaA_root_fake_input },
296 { "drawins", luaA_root_drawins },
297 { "wallpaper", luaA_root_wallpaper },
298 { NULL, NULL }
301 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80