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"
32 #include "common/xcursor.h"
33 #include "common/xutil.h"
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
);
64 xcb_kill_client(c
, *rootpix
);
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
;
81 cairo_surface_t
*surface
;
82 cairo_device_t
*device
;
85 if (xcb_connection_has_error(c
))
88 /* Create a pixmap and make sure it is already created, because we are going
89 * to use it from the other X11 connection (Juggling with X11 connections
90 * is a really, really bad idea).
92 xcb_create_pixmap(c
, screen
->root_depth
, p
, screen
->root
, width
, height
);
95 /* Now paint to the picture from the main connection so that cairo sees that
96 * it can tell the X server to copy between the (possible) old pixmap and
97 * the new one directly and doesn't need GetImage and PutImage.
99 surface
= cairo_xcb_surface_create(globalconf
.connection
, p
, draw_default_visual(screen
), width
, height
);
100 device
= cairo_device_reference(cairo_surface_get_device(surface
));
101 cr
= cairo_create(surface
);
102 /* Paint the pattern to the surface */
103 cairo_set_source(cr
, pattern
);
104 cairo_set_operator(cr
, CAIRO_OPERATOR_SOURCE
);
107 cairo_surface_finish(surface
);
108 cairo_surface_destroy(surface
);
109 /* Finish the device so we safely call xcb_disconnect() */
110 cairo_device_finish(device
);
111 cairo_device_destroy(device
);
112 xcb_aux_sync(globalconf
.connection
);
114 root_set_wallpaper_pixmap(c
, p
);
116 /* Make sure our pixmap is not destroyed when we disconnect. */
117 xcb_set_close_down_mode(c
, XCB_CLOSE_DOWN_RETAIN_PERMANENT
);
127 _string_to_key_code(const char *s
)
130 xcb_keycode_t
*keycodes
;
132 keysym
= XStringToKeysym(s
);
133 keycodes
= xcb_key_symbols_get_keycode(globalconf
.keysyms
, keysym
);
136 return keycodes
[0]; /* XXX only returning the first is probably not
143 /** Send fake events. Usually the current focused client will get it.
144 * \param L The Lua VM state.
145 * \return The number of element pushed on stack.
147 * \lparam The event type: key_press, key_release, button_press, button_release
149 * \lparam The detail: in case of a key event, this is the keycode to send, in
150 * case of a button event this is the number of the button. In case of a motion
151 * event, this is a boolean value which if true make the coordinates relatives.
152 * \lparam In case of a motion event, this is the X coordinate.
153 * \lparam In case of a motion event, this is the Y coordinate.
154 * If not specified, the current one is used.
157 luaA_root_fake_input(lua_State
*L
)
159 if(!globalconf
.have_xtest
)
161 luaA_warn(L
, "XTest extension is not available, cannot fake input.");
165 const char *stype
= luaL_checkstring(L
, 1);
166 uint8_t type
, detail
;
169 if (A_STREQ(stype
, "key_press"))
171 type
= XCB_KEY_PRESS
;
172 if(lua_type(L
, 2) == LUA_TSTRING
) {
173 detail
= _string_to_key_code(lua_tostring(L
, 2)); /* keysym */
175 detail
= luaL_checknumber(L
, 2); /* keycode */
178 else if(A_STREQ(stype
, "key_release"))
180 type
= XCB_KEY_RELEASE
;
181 if(lua_type(L
, 2) == LUA_TSTRING
) {
182 detail
= _string_to_key_code(lua_tostring(L
, 2)); /* keysym */
184 detail
= luaL_checknumber(L
, 2); /* keycode */
187 else if(A_STREQ(stype
, "button_press"))
189 type
= XCB_BUTTON_PRESS
;
190 detail
= luaL_checknumber(L
, 2); /* button number */
192 else if(A_STREQ(stype
, "button_release"))
194 type
= XCB_BUTTON_RELEASE
;
195 detail
= luaL_checknumber(L
, 2); /* button number */
197 else if(A_STREQ(stype
, "motion_notify"))
199 type
= XCB_MOTION_NOTIFY
;
200 detail
= luaA_checkboolean(L
, 2); /* relative to the current position or not */
201 x
= luaL_checknumber(L
, 3);
202 y
= luaL_checknumber(L
, 4);
207 xcb_test_fake_input(globalconf
.connection
,
217 /** Get or set global key bindings.
218 * This binding will be available when you'll press keys on root window.
219 * \param L The Lua VM state.
220 * \return The number of element pushed on stack.
222 * \lparam An array of key bindings objects, or nothing.
223 * \lreturn The array of key bindings objects of this client.
226 luaA_root_keys(lua_State
*L
)
228 if(lua_gettop(L
) == 1)
230 luaA_checktable(L
, 1);
232 foreach(key
, globalconf
.keys
)
233 luaA_object_unref(globalconf
.L
, *key
);
235 key_array_wipe(&globalconf
.keys
);
236 key_array_init(&globalconf
.keys
);
239 while(lua_next(L
, 1))
240 key_array_append(&globalconf
.keys
, luaA_object_ref_class(L
, -1, &key_class
));
242 xcb_screen_t
*s
= globalconf
.screen
;
243 xcb_ungrab_key(globalconf
.connection
, XCB_GRAB_ANY
, s
->root
, XCB_BUTTON_MASK_ANY
);
244 xwindow_grabkeys(s
->root
, &globalconf
.keys
);
249 lua_createtable(L
, globalconf
.keys
.len
, 0);
250 for(int i
= 0; i
< globalconf
.keys
.len
; i
++)
252 luaA_object_push(L
, globalconf
.keys
.tab
[i
]);
253 lua_rawseti(L
, -2, i
+ 1);
259 /** Get or set global mouse bindings.
260 * This binding will be available when you'll click on root window.
261 * \param L The Lua VM state.
262 * \return The number of element pushed on stack.
264 * \lparam An array of mouse button bindings objects, or nothing.
265 * \lreturn The array of mouse button bindings objects.
268 luaA_root_buttons(lua_State
*L
)
270 if(lua_gettop(L
) == 1)
272 luaA_checktable(L
, 1);
274 foreach(button
, globalconf
.buttons
)
275 luaA_object_unref(globalconf
.L
, *button
);
277 button_array_wipe(&globalconf
.buttons
);
278 button_array_init(&globalconf
.buttons
);
281 while(lua_next(L
, 1))
282 button_array_append(&globalconf
.buttons
, luaA_object_ref(L
, -1));
287 lua_createtable(L
, globalconf
.buttons
.len
, 0);
288 for(int i
= 0; i
< globalconf
.buttons
.len
; i
++)
290 luaA_object_push(L
, globalconf
.buttons
.tab
[i
]);
291 lua_rawseti(L
, -2, i
+ 1);
297 /** Set the root cursor.
298 * \param L The Lua VM state.
299 * \return The number of element pushed on stack.
301 * \lparam A X cursor name.
304 luaA_root_cursor(lua_State
*L
)
306 const char *cursor_name
= luaL_checkstring(L
, 1);
307 uint16_t cursor_font
= xcursor_font_fromstr(cursor_name
);
311 uint32_t change_win_vals
[] = { xcursor_new(globalconf
.cursor_ctx
, cursor_font
) };
313 xcb_change_window_attributes(globalconf
.connection
,
314 globalconf
.screen
->root
,
319 luaA_warn(L
, "invalid cursor %s", cursor_name
);
324 /** Get the drawins attached to a screen.
325 * \param L The Lua VM state.
326 * \return The number of element pushed on stack.
328 * \lreturn A table with all drawins.
331 luaA_root_drawins(lua_State
*L
)
333 lua_createtable(L
, globalconf
.drawins
.len
, 0);
335 for(int i
= 0; i
< globalconf
.drawins
.len
; i
++)
337 luaA_object_push(L
, globalconf
.drawins
.tab
[i
]);
338 lua_rawseti(L
, -2, i
+ 1);
344 /** Get the screen's wallpaper
345 * \param L The Lua VM state.
346 * \return The number of element pushed on stack.
348 * \lreturn A cairo surface for the wallpaper.
351 luaA_root_wallpaper(lua_State
*L
)
353 xcb_get_property_cookie_t prop_c
;
354 xcb_get_property_reply_t
*prop_r
;
355 xcb_get_geometry_cookie_t geom_c
;
356 xcb_get_geometry_reply_t
*geom_r
;
357 xcb_pixmap_t
*rootpix
;
358 cairo_surface_t
*surface
;
360 if(lua_gettop(L
) == 1)
362 cairo_pattern_t
*pattern
= (cairo_pattern_t
*)lua_touserdata(L
, -1);
363 lua_pushboolean(L
, root_set_wallpaper(pattern
));
364 /* Don't return the wallpaper, it's too easy to get memleaks */
368 prop_c
= xcb_get_property_unchecked(globalconf
.connection
, false,
369 globalconf
.screen
->root
, _XROOTPMAP_ID
, XCB_ATOM_PIXMAP
, 0, 1);
370 prop_r
= xcb_get_property_reply(globalconf
.connection
, prop_c
, NULL
);
372 if (!prop_r
|| !prop_r
->value_len
)
378 rootpix
= xcb_get_property_value(prop_r
);
385 geom_c
= xcb_get_geometry_unchecked(globalconf
.connection
, *rootpix
);
386 geom_r
= xcb_get_geometry_reply(globalconf
.connection
, geom_c
, NULL
);
393 /* Only the default visual makes sense, so just the default depth */
394 if (geom_r
->depth
!= draw_visual_depth(globalconf
.screen
, globalconf
.default_visual
->visual_id
))
395 warn("Got a pixmap with depth %d, but the default depth is %d, continuing anyway",
396 geom_r
->depth
, draw_visual_depth(globalconf
.screen
, globalconf
.default_visual
->visual_id
));
398 surface
= cairo_xcb_surface_create(globalconf
.connection
, *rootpix
, globalconf
.default_visual
,
399 geom_r
->width
, geom_r
->height
);
401 /* lua has to make sure this surface gets destroyed */
402 lua_pushlightuserdata(L
, surface
);
408 /** Get the screen's wallpaper
409 * \param L The Lua VM state.
410 * \return The number of element pushed on stack.
412 * \lreturn A cairo surface for the wallpaper.
415 luaA_root_tags(lua_State
*L
)
417 lua_createtable(L
, globalconf
.tags
.len
, 0);
418 for(int i
= 0; i
< globalconf
.tags
.len
; i
++)
420 luaA_object_push(L
, globalconf
.tags
.tab
[i
]);
421 lua_rawseti(L
, -2, i
+ 1);
427 const struct luaL_Reg awesome_root_lib
[] =
429 { "buttons", luaA_root_buttons
},
430 { "keys", luaA_root_keys
},
431 { "cursor", luaA_root_cursor
},
432 { "fake_input", luaA_root_fake_input
},
433 { "drawins", luaA_root_drawins
},
434 { "wallpaper", luaA_root_wallpaper
},
435 { "tags", luaA_root_tags
},
436 { "__index", luaA_default_index
},
437 { "__newindex", luaA_default_newindex
},
441 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80