change codename
[awesome.git] / root.c
blobf3df891a44d7cbb1fd9f869cd40c393292ea900c
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 void
36 root_set_wallpaper_pixmap(xcb_connection_t *c, xcb_pixmap_t p)
38 xcb_get_property_cookie_t prop_c;
39 xcb_get_property_reply_t *prop_r;
40 const xcb_screen_t *screen = globalconf.screen;
42 /* We now have the pattern painted to the pixmap p. Now turn p into the root
43 * window's background pixmap.
45 xcb_change_window_attributes(c, screen->root, XCB_CW_BACK_PIXMAP, &p);
46 xcb_clear_area(c, 0, screen->root, 0, 0, 0, 0);
48 prop_c = xcb_get_property_unchecked(c, false,
49 screen->root, ESETROOT_PMAP_ID, XCB_ATOM_PIXMAP, 0, 1);
51 /* Theoretically, this should be enough to set the wallpaper. However, to
52 * make pseudo-transparency work, clients need a way to get the wallpaper.
53 * You can't query a window's back pixmap, so properties are (ab)used.
55 xcb_change_property(c, XCB_PROP_MODE_REPLACE, screen->root, _XROOTPMAP_ID, XCB_ATOM_PIXMAP, 32, 1, &p);
56 xcb_change_property(c, XCB_PROP_MODE_REPLACE, screen->root, ESETROOT_PMAP_ID, XCB_ATOM_PIXMAP, 32, 1, &p);
58 /* Now make sure that the old wallpaper is freed (but only do this for ESETROOT_PMAP_ID) */
59 prop_r = xcb_get_property_reply(c, prop_c, NULL);
60 if (prop_r && prop_r->value_len)
62 xcb_pixmap_t *rootpix = xcb_get_property_value(prop_r);
63 if (rootpix)
64 xcb_kill_client(c, *rootpix);
66 free(prop_r);
69 static bool
70 root_set_wallpaper(cairo_pattern_t *pattern)
72 xcb_connection_t *c = xcb_connect(NULL, NULL);
73 xcb_pixmap_t p = xcb_generate_id(c);
74 /* globalconf.connection should be connected to the same X11 server, so we
75 * can just use the info from that other connection.
77 const xcb_screen_t *screen = globalconf.screen;
78 uint16_t width = screen->width_in_pixels;
79 uint16_t height = screen->height_in_pixels;
80 bool result = false;
81 cairo_surface_t *surface;
82 cairo_device_t *device;
83 cairo_t *cr;
85 if (xcb_connection_has_error(c))
86 goto disconnect;
88 xcb_create_pixmap(c, screen->root_depth, p, screen->root, width, height);
89 surface = cairo_xcb_surface_create(c, p, draw_default_visual(screen), width, height);
90 device = cairo_device_reference(cairo_surface_get_device(surface));
91 cr = cairo_create(surface);
92 /* Paint the pattern to the surface */
93 cairo_set_source(cr, pattern);
94 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
95 cairo_paint(cr);
96 cairo_destroy(cr);
97 cairo_surface_finish(surface);
98 cairo_surface_destroy(surface);
99 /* Make sure that cairo doesn't try to use the connection later again */
100 assert(device != NULL);
101 cairo_device_finish(device);
102 cairo_device_destroy(device);
104 root_set_wallpaper_pixmap(c, p);
106 /* Make sure our pixmap is not destroyed when we disconnect. */
107 xcb_set_close_down_mode(c, XCB_CLOSE_DOWN_RETAIN_PERMANENT);
109 result = true;
110 disconnect:
111 xcb_flush(c);
112 xcb_disconnect(c);
113 return result;
116 static xcb_keycode_t
117 _string_to_key_code(const char *s)
119 xcb_keysym_t keysym;
120 xcb_keycode_t *keycodes;
122 keysym = XStringToKeysym(s);
123 keycodes = xcb_key_symbols_get_keycode(globalconf.keysyms, keysym);
125 if(keycodes) {
126 return keycodes[0]; /* XXX only returning the first is probably not
127 * the best */
128 } else {
129 return 0;
133 /** Send fake events. Usually the current focused client will get it.
134 * \param L The Lua VM state.
135 * \return The number of element pushed on stack.
136 * \luastack
137 * \lparam The event type: key_press, key_release, button_press, button_release
138 * or motion_notify.
139 * \lparam The detail: in case of a key event, this is the keycode to send, in
140 * case of a button event this is the number of the button. In case of a motion
141 * event, this is a boolean value which if true make the coordinates relatives.
142 * \lparam In case of a motion event, this is the X coordinate.
143 * \lparam In case of a motion event, this is the Y coordinate.
144 * If not specified, the current one is used.
146 static int
147 luaA_root_fake_input(lua_State *L)
149 if(!globalconf.have_xtest)
151 luaA_warn(L, "XTest extension is not available, cannot fake input.");
152 return 0;
155 const char *stype = luaL_checkstring(L, 1);
156 uint8_t type, detail;
157 int x = 0, y = 0;
159 if (A_STREQ(stype, "key_press"))
161 type = XCB_KEY_PRESS;
162 if(lua_type(L, 2) == LUA_TSTRING) {
163 detail = _string_to_key_code(lua_tostring(L, 2)); /* keysym */
164 } else {
165 detail = luaL_checknumber(L, 2); /* keycode */
168 else if(A_STREQ(stype, "key_release"))
170 type = XCB_KEY_RELEASE;
171 if(lua_type(L, 2) == LUA_TSTRING) {
172 detail = _string_to_key_code(lua_tostring(L, 2)); /* keysym */
173 } else {
174 detail = luaL_checknumber(L, 2); /* keycode */
177 else if(A_STREQ(stype, "button_press"))
179 type = XCB_BUTTON_PRESS;
180 detail = luaL_checknumber(L, 2); /* button number */
182 else if(A_STREQ(stype, "button_release"))
184 type = XCB_BUTTON_RELEASE;
185 detail = luaL_checknumber(L, 2); /* button number */
187 else if(A_STREQ(stype, "motion_notify"))
189 type = XCB_MOTION_NOTIFY;
190 detail = luaA_checkboolean(L, 2); /* relative to the current position or not */
191 x = luaL_checknumber(L, 3);
192 y = luaL_checknumber(L, 4);
194 else
195 return 0;
197 xcb_test_fake_input(globalconf.connection,
198 type,
199 detail,
200 XCB_CURRENT_TIME,
201 XCB_NONE,
202 x, y,
204 return 0;
207 /** Get or set global key bindings.
208 * This binding will be available when you'll press keys on root window.
209 * \param L The Lua VM state.
210 * \return The number of element pushed on stack.
211 * \luastack
212 * \lparam An array of key bindings objects, or nothing.
213 * \lreturn The array of key bindings objects of this client.
215 static int
216 luaA_root_keys(lua_State *L)
218 if(lua_gettop(L) == 1)
220 luaA_checktable(L, 1);
222 foreach(key, globalconf.keys)
223 luaA_object_unref(globalconf.L, *key);
225 key_array_wipe(&globalconf.keys);
226 key_array_init(&globalconf.keys);
228 lua_pushnil(L);
229 while(lua_next(L, 1))
230 key_array_append(&globalconf.keys, luaA_object_ref_class(L, -1, &key_class));
232 xcb_screen_t *s = globalconf.screen;
233 xcb_ungrab_key(globalconf.connection, XCB_GRAB_ANY, s->root, XCB_BUTTON_MASK_ANY);
234 xwindow_grabkeys(s->root, &globalconf.keys);
236 return 1;
239 lua_createtable(L, globalconf.keys.len, 0);
240 for(int i = 0; i < globalconf.keys.len; i++)
242 luaA_object_push(L, globalconf.keys.tab[i]);
243 lua_rawseti(L, -2, i + 1);
246 return 1;
249 /** Get or set global mouse bindings.
250 * This binding will be available when you'll click on root window.
251 * \param L The Lua VM state.
252 * \return The number of element pushed on stack.
253 * \luastack
254 * \lparam An array of mouse button bindings objects, or nothing.
255 * \lreturn The array of mouse button bindings objects.
257 static int
258 luaA_root_buttons(lua_State *L)
260 if(lua_gettop(L) == 1)
262 luaA_checktable(L, 1);
264 foreach(button, globalconf.buttons)
265 luaA_object_unref(globalconf.L, *button);
267 button_array_wipe(&globalconf.buttons);
268 button_array_init(&globalconf.buttons);
270 lua_pushnil(L);
271 while(lua_next(L, 1))
272 button_array_append(&globalconf.buttons, luaA_object_ref(L, -1));
274 return 1;
277 lua_createtable(L, globalconf.buttons.len, 0);
278 for(int i = 0; i < globalconf.buttons.len; i++)
280 luaA_object_push(L, globalconf.buttons.tab[i]);
281 lua_rawseti(L, -2, i + 1);
284 return 1;
287 /** Set the root cursor.
288 * \param L The Lua VM state.
289 * \return The number of element pushed on stack.
290 * \luastack
291 * \lparam A X cursor name.
293 static int
294 luaA_root_cursor(lua_State *L)
296 const char *cursor_name = luaL_checkstring(L, 1);
297 uint16_t cursor_font = xcursor_font_fromstr(cursor_name);
299 if(cursor_font)
301 uint32_t change_win_vals[] = { xcursor_new(globalconf.display, cursor_font) };
303 xcb_change_window_attributes(globalconf.connection,
304 globalconf.screen->root,
305 XCB_CW_CURSOR,
306 change_win_vals);
308 else
309 luaA_warn(L, "invalid cursor %s", cursor_name);
311 return 0;
314 /** Get the drawins attached to a screen.
315 * \param L The Lua VM state.
316 * \return The number of element pushed on stack.
317 * \luastack
318 * \lreturn A table with all drawins.
320 static int
321 luaA_root_drawins(lua_State *L)
323 lua_createtable(L, globalconf.drawins.len, 0);
325 for(int i = 0; i < globalconf.drawins.len; i++)
327 luaA_object_push(L, globalconf.drawins.tab[i]);
328 lua_rawseti(L, -2, i + 1);
331 return 1;
334 /** Get the screen's wallpaper
335 * \param L The Lua VM state.
336 * \return The number of element pushed on stack.
337 * \luastack
338 * \lreturn A cairo surface for the wallpaper.
340 static int
341 luaA_root_wallpaper(lua_State *L)
343 xcb_get_property_cookie_t prop_c;
344 xcb_get_property_reply_t *prop_r;
345 xcb_pixmap_t *rootpix;
346 cairo_surface_t *surface;
348 if(lua_gettop(L) == 1)
350 cairo_pattern_t *pattern = (cairo_pattern_t *)lua_touserdata(L, -1);
351 lua_pushboolean(L, root_set_wallpaper(pattern));
352 /* Don't return the wallpaper, it's too easy to get memleaks */
353 return 1;
356 prop_c = xcb_get_property_unchecked(globalconf.connection, false,
357 globalconf.screen->root, _XROOTPMAP_ID, XCB_ATOM_PIXMAP, 0, 1);
358 prop_r = xcb_get_property_reply(globalconf.connection, prop_c, NULL);
360 if (!prop_r || !prop_r->value_len)
362 free(prop_r);
363 return 0;
366 rootpix = xcb_get_property_value(prop_r);
367 if (!rootpix)
369 free(prop_r);
370 return 0;
373 /* We can't query the pixmap's values (or even if that pixmap exists at
374 * all), so let's just assume that it uses the default visual and is as
375 * large as the root window. Everything else wouldn't make sense.
377 surface = cairo_xcb_surface_create(globalconf.connection, *rootpix, globalconf.default_visual,
378 globalconf.screen->width_in_pixels, globalconf.screen->height_in_pixels);
380 /* lua has to make sure this surface gets destroyed */
381 lua_pushlightuserdata(L, surface);
382 free(prop_r);
383 return 1;
386 /** Get the screen's wallpaper
387 * \param L The Lua VM state.
388 * \return The number of element pushed on stack.
389 * \luastack
390 * \lreturn A cairo surface for the wallpaper.
392 static int
393 luaA_root_tags(lua_State *L)
395 lua_createtable(L, globalconf.tags.len, 0);
396 for(int i = 0; i < globalconf.tags.len; i++)
398 luaA_object_push(L, globalconf.tags.tab[i]);
399 lua_rawseti(L, -2, i + 1);
402 return 1;
405 const struct luaL_Reg awesome_root_lib[] =
407 { "buttons", luaA_root_buttons },
408 { "keys", luaA_root_keys },
409 { "cursor", luaA_root_cursor },
410 { "fake_input", luaA_root_fake_input },
411 { "drawins", luaA_root_drawins },
412 { "wallpaper", luaA_root_wallpaper },
413 { "tags", luaA_root_tags },
414 { NULL, NULL }
417 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80