Merge branch 'fix-ldoc-set-spacing' of git://github.com/actionless/awesome
[awesome.git] / root.c
blob9e6fde54cc16ae4e3494e604e004a5643300fd4e
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 "globalconf.h"
24 #include "common/atoms.h"
25 #include "common/xcursor.h"
26 #include "objects/button.h"
27 #include "xwindow.h"
29 #include <xcb/xtest.h>
30 #include <xcb/xcb_aux.h>
31 #include <cairo-xcb.h>
33 static void
34 root_set_wallpaper_pixmap(xcb_connection_t *c, xcb_pixmap_t p)
36 xcb_get_property_cookie_t prop_c;
37 xcb_get_property_reply_t *prop_r;
38 const xcb_screen_t *screen = globalconf.screen;
40 /* We now have the pattern painted to the pixmap p. Now turn p into the root
41 * window's background pixmap.
43 xcb_change_window_attributes(c, screen->root, XCB_CW_BACK_PIXMAP, &p);
44 xcb_clear_area(c, 0, screen->root, 0, 0, 0, 0);
46 prop_c = xcb_get_property_unchecked(c, false,
47 screen->root, ESETROOT_PMAP_ID, XCB_ATOM_PIXMAP, 0, 1);
49 /* Theoretically, this should be enough to set the wallpaper. However, to
50 * make pseudo-transparency work, clients need a way to get the wallpaper.
51 * You can't query a window's back pixmap, so properties are (ab)used.
53 xcb_change_property(c, XCB_PROP_MODE_REPLACE, screen->root, _XROOTPMAP_ID, XCB_ATOM_PIXMAP, 32, 1, &p);
54 xcb_change_property(c, XCB_PROP_MODE_REPLACE, screen->root, ESETROOT_PMAP_ID, XCB_ATOM_PIXMAP, 32, 1, &p);
56 /* Now make sure that the old wallpaper is freed (but only do this for ESETROOT_PMAP_ID) */
57 prop_r = xcb_get_property_reply(c, prop_c, NULL);
58 if (prop_r && prop_r->value_len)
60 xcb_pixmap_t *rootpix = xcb_get_property_value(prop_r);
61 if (rootpix)
62 xcb_kill_client(c, *rootpix);
64 p_delete(&prop_r);
67 static bool
68 root_set_wallpaper(cairo_pattern_t *pattern)
70 xcb_connection_t *c = xcb_connect(NULL, NULL);
71 xcb_pixmap_t p = xcb_generate_id(c);
72 /* globalconf.connection should be connected to the same X11 server, so we
73 * can just use the info from that other connection.
75 const xcb_screen_t *screen = globalconf.screen;
76 uint16_t width = screen->width_in_pixels;
77 uint16_t height = screen->height_in_pixels;
78 bool result = false;
79 cairo_surface_t *surface;
80 cairo_t *cr;
82 if (xcb_connection_has_error(c))
83 goto disconnect;
85 /* Create a pixmap and make sure it is already created, because we are going
86 * to use it from the other X11 connection (Juggling with X11 connections
87 * is a really, really bad idea).
89 xcb_create_pixmap(c, screen->root_depth, p, screen->root, width, height);
90 xcb_aux_sync(c);
92 /* Now paint to the picture from the main connection so that cairo sees that
93 * it can tell the X server to copy between the (possible) old pixmap and
94 * the new one directly and doesn't need GetImage and PutImage.
96 surface = cairo_xcb_surface_create(globalconf.connection, p, draw_default_visual(screen), width, height);
97 cr = cairo_create(surface);
98 /* Paint the pattern to the surface */
99 cairo_set_source(cr, pattern);
100 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
101 cairo_paint(cr);
102 cairo_destroy(cr);
103 cairo_surface_finish(surface);
104 cairo_surface_destroy(surface);
105 xcb_aux_sync(globalconf.connection);
107 root_set_wallpaper_pixmap(c, p);
109 /* Make sure our pixmap is not destroyed when we disconnect. */
110 xcb_set_close_down_mode(c, XCB_CLOSE_DOWN_RETAIN_PERMANENT);
112 result = true;
113 disconnect:
114 xcb_flush(c);
115 xcb_disconnect(c);
116 return result;
119 static xcb_keycode_t
120 _string_to_key_code(const char *s)
122 xcb_keysym_t keysym;
123 xcb_keycode_t *keycodes;
125 keysym = XStringToKeysym(s);
126 keycodes = xcb_key_symbols_get_keycode(globalconf.keysyms, keysym);
128 if(keycodes) {
129 return keycodes[0]; /* XXX only returning the first is probably not
130 * the best */
131 } else {
132 return 0;
136 /** Send fake events. Usually the current focused client will get it.
137 * \param L The Lua VM state.
138 * \return The number of element pushed on stack.
139 * \luastack
140 * \lparam The event type: key_press, key_release, button_press, button_release
141 * or motion_notify.
142 * \lparam The detail: in case of a key event, this is the keycode to send, in
143 * case of a button event this is the number of the button. In case of a motion
144 * event, this is a boolean value which if true make the coordinates relatives.
145 * \lparam In case of a motion event, this is the X coordinate.
146 * \lparam In case of a motion event, this is the Y coordinate.
147 * If not specified, the current one is used.
149 static int
150 luaA_root_fake_input(lua_State *L)
152 if(!globalconf.have_xtest)
154 luaA_warn(L, "XTest extension is not available, cannot fake input.");
155 return 0;
158 const char *stype = luaL_checkstring(L, 1);
159 uint8_t type, detail;
160 int x = 0, y = 0;
162 if (A_STREQ(stype, "key_press"))
164 type = XCB_KEY_PRESS;
165 if(lua_type(L, 2) == LUA_TSTRING) {
166 detail = _string_to_key_code(lua_tostring(L, 2)); /* keysym */
167 } else {
168 detail = luaL_checknumber(L, 2); /* keycode */
171 else if(A_STREQ(stype, "key_release"))
173 type = XCB_KEY_RELEASE;
174 if(lua_type(L, 2) == LUA_TSTRING) {
175 detail = _string_to_key_code(lua_tostring(L, 2)); /* keysym */
176 } else {
177 detail = luaL_checknumber(L, 2); /* keycode */
180 else if(A_STREQ(stype, "button_press"))
182 type = XCB_BUTTON_PRESS;
183 detail = luaL_checknumber(L, 2); /* button number */
185 else if(A_STREQ(stype, "button_release"))
187 type = XCB_BUTTON_RELEASE;
188 detail = luaL_checknumber(L, 2); /* button number */
190 else if(A_STREQ(stype, "motion_notify"))
192 type = XCB_MOTION_NOTIFY;
193 detail = luaA_checkboolean(L, 2); /* relative to the current position or not */
194 x = luaL_checknumber(L, 3);
195 y = luaL_checknumber(L, 4);
197 else
198 return 0;
200 xcb_test_fake_input(globalconf.connection,
201 type,
202 detail,
203 XCB_CURRENT_TIME,
204 XCB_NONE,
205 x, y,
207 return 0;
210 /** Get or set global key bindings.
211 * This binding will be available when you'll press keys on root window.
212 * \param L The Lua VM state.
213 * \return The number of element pushed on stack.
214 * \luastack
215 * \lparam An array of key bindings objects, or nothing.
216 * \lreturn The array of key bindings objects of this client.
218 static int
219 luaA_root_keys(lua_State *L)
221 if(lua_gettop(L) == 1)
223 luaA_checktable(L, 1);
225 foreach(key, globalconf.keys)
226 luaA_object_unref(globalconf.L, *key);
228 key_array_wipe(&globalconf.keys);
229 key_array_init(&globalconf.keys);
231 lua_pushnil(L);
232 while(lua_next(L, 1))
233 key_array_append(&globalconf.keys, luaA_object_ref_class(L, -1, &key_class));
235 xcb_screen_t *s = globalconf.screen;
236 xcb_ungrab_key(globalconf.connection, XCB_GRAB_ANY, s->root, XCB_BUTTON_MASK_ANY);
237 xwindow_grabkeys(s->root, &globalconf.keys);
239 return 1;
242 lua_createtable(L, globalconf.keys.len, 0);
243 for(int i = 0; i < globalconf.keys.len; i++)
245 luaA_object_push(L, globalconf.keys.tab[i]);
246 lua_rawseti(L, -2, i + 1);
249 return 1;
252 /** Get or set global mouse bindings.
253 * This binding will be available when you'll click on root window.
254 * \param L The Lua VM state.
255 * \return The number of element pushed on stack.
256 * \luastack
257 * \lparam An array of mouse button bindings objects, or nothing.
258 * \lreturn The array of mouse button bindings objects.
260 static int
261 luaA_root_buttons(lua_State *L)
263 if(lua_gettop(L) == 1)
265 luaA_checktable(L, 1);
267 foreach(button, globalconf.buttons)
268 luaA_object_unref(globalconf.L, *button);
270 button_array_wipe(&globalconf.buttons);
271 button_array_init(&globalconf.buttons);
273 lua_pushnil(L);
274 while(lua_next(L, 1))
275 button_array_append(&globalconf.buttons, luaA_object_ref(L, -1));
277 return 1;
280 lua_createtable(L, globalconf.buttons.len, 0);
281 for(int i = 0; i < globalconf.buttons.len; i++)
283 luaA_object_push(L, globalconf.buttons.tab[i]);
284 lua_rawseti(L, -2, i + 1);
287 return 1;
290 /** Set the root cursor.
291 * \param L The Lua VM state.
292 * \return The number of element pushed on stack.
293 * \luastack
294 * \lparam A X cursor name.
296 static int
297 luaA_root_cursor(lua_State *L)
299 const char *cursor_name = luaL_checkstring(L, 1);
300 uint16_t cursor_font = xcursor_font_fromstr(cursor_name);
302 if(cursor_font)
304 uint32_t change_win_vals[] = { xcursor_new(globalconf.cursor_ctx, cursor_font) };
306 xcb_change_window_attributes(globalconf.connection,
307 globalconf.screen->root,
308 XCB_CW_CURSOR,
309 change_win_vals);
311 else
312 luaA_warn(L, "invalid cursor %s", cursor_name);
314 return 0;
317 /** Get the drawins attached to a screen.
318 * \param L The Lua VM state.
319 * \return The number of element pushed on stack.
320 * \luastack
321 * \lreturn A table with all drawins.
323 static int
324 luaA_root_drawins(lua_State *L)
326 lua_createtable(L, globalconf.drawins.len, 0);
328 for(int i = 0; i < globalconf.drawins.len; i++)
330 luaA_object_push(L, globalconf.drawins.tab[i]);
331 lua_rawseti(L, -2, i + 1);
334 return 1;
337 /** Get the screen's wallpaper
338 * \param L The Lua VM state.
339 * \return The number of element pushed on stack.
340 * \luastack
341 * \lreturn A cairo surface for the wallpaper.
343 static int
344 luaA_root_wallpaper(lua_State *L)
346 xcb_get_property_cookie_t prop_c;
347 xcb_get_property_reply_t *prop_r;
348 xcb_get_geometry_cookie_t geom_c;
349 xcb_get_geometry_reply_t *geom_r;
350 xcb_pixmap_t *rootpix;
351 cairo_surface_t *surface;
353 if(lua_gettop(L) == 1)
355 cairo_pattern_t *pattern = (cairo_pattern_t *)lua_touserdata(L, -1);
356 lua_pushboolean(L, root_set_wallpaper(pattern));
357 /* Don't return the wallpaper, it's too easy to get memleaks */
358 return 1;
361 prop_c = xcb_get_property_unchecked(globalconf.connection, false,
362 globalconf.screen->root, _XROOTPMAP_ID, XCB_ATOM_PIXMAP, 0, 1);
363 prop_r = xcb_get_property_reply(globalconf.connection, prop_c, NULL);
365 if (!prop_r || !prop_r->value_len)
367 p_delete(&prop_r);
368 return 0;
371 rootpix = xcb_get_property_value(prop_r);
372 if (!rootpix)
374 p_delete(&prop_r);
375 return 0;
378 geom_c = xcb_get_geometry_unchecked(globalconf.connection, *rootpix);
379 geom_r = xcb_get_geometry_reply(globalconf.connection, geom_c, NULL);
380 if (!geom_r)
382 p_delete(&prop_r);
383 return 0;
386 /* Only the default visual makes sense, so just the default depth */
387 if (geom_r->depth != draw_visual_depth(globalconf.screen, globalconf.default_visual->visual_id))
388 warn("Got a pixmap with depth %d, but the default depth is %d, continuing anyway",
389 geom_r->depth, draw_visual_depth(globalconf.screen, globalconf.default_visual->visual_id));
391 surface = cairo_xcb_surface_create(globalconf.connection, *rootpix, globalconf.default_visual,
392 geom_r->width, geom_r->height);
394 /* lua has to make sure this surface gets destroyed */
395 lua_pushlightuserdata(L, surface);
396 p_delete(&prop_r);
397 p_delete(&geom_r);
398 return 1;
401 /** Get the screen's wallpaper
402 * \param L The Lua VM state.
403 * \return The number of element pushed on stack.
404 * \luastack
405 * \lreturn A cairo surface for the wallpaper.
407 static int
408 luaA_root_tags(lua_State *L)
410 lua_createtable(L, globalconf.tags.len, 0);
411 for(int i = 0; i < globalconf.tags.len; i++)
413 luaA_object_push(L, globalconf.tags.tab[i]);
414 lua_rawseti(L, -2, i + 1);
417 return 1;
420 const struct luaL_Reg awesome_root_lib[] =
422 { "buttons", luaA_root_buttons },
423 { "keys", luaA_root_keys },
424 { "cursor", luaA_root_cursor },
425 { "fake_input", luaA_root_fake_input },
426 { "drawins", luaA_root_drawins },
427 { "wallpaper", luaA_root_wallpaper },
428 { "tags", luaA_root_tags },
429 { "__index", luaA_default_index },
430 { "__newindex", luaA_default_newindex },
431 { NULL, NULL }
434 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80