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"
29 #include <xcb/xtest.h>
30 #include <xcb/xcb_aux.h>
31 #include <cairo-xcb.h>
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
);
62 xcb_kill_client(c
, *rootpix
);
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
;
79 cairo_surface_t
*surface
;
80 cairo_device_t
*device
;
83 if (xcb_connection_has_error(c
))
86 /* Create a pixmap and make sure it is already created, because we are going
87 * to use it from the other X11 connection (Juggling with X11 connections
88 * is a really, really bad idea).
90 xcb_create_pixmap(c
, screen
->root_depth
, p
, screen
->root
, width
, height
);
93 /* Now paint to the picture from the main connection so that cairo sees that
94 * it can tell the X server to copy between the (possible) old pixmap and
95 * the new one directly and doesn't need GetImage and PutImage.
97 surface
= cairo_xcb_surface_create(globalconf
.connection
, p
, draw_default_visual(screen
), width
, height
);
98 device
= cairo_device_reference(cairo_surface_get_device(surface
));
99 cr
= cairo_create(surface
);
100 /* Paint the pattern to the surface */
101 cairo_set_source(cr
, pattern
);
102 cairo_set_operator(cr
, CAIRO_OPERATOR_SOURCE
);
105 cairo_surface_finish(surface
);
106 cairo_surface_destroy(surface
);
107 /* Finish the device so we safely call xcb_disconnect() */
108 cairo_device_finish(device
);
109 cairo_device_destroy(device
);
110 xcb_aux_sync(globalconf
.connection
);
112 root_set_wallpaper_pixmap(c
, p
);
114 /* Make sure our pixmap is not destroyed when we disconnect. */
115 xcb_set_close_down_mode(c
, XCB_CLOSE_DOWN_RETAIN_PERMANENT
);
125 _string_to_key_code(const char *s
)
128 xcb_keycode_t
*keycodes
;
130 keysym
= XStringToKeysym(s
);
131 keycodes
= xcb_key_symbols_get_keycode(globalconf
.keysyms
, keysym
);
134 return keycodes
[0]; /* XXX only returning the first is probably not
141 /** Send fake events. Usually the current focused client will get it.
142 * \param L The Lua VM state.
143 * \return The number of element pushed on stack.
145 * \lparam The event type: key_press, key_release, button_press, button_release
147 * \lparam The detail: in case of a key event, this is the keycode to send, in
148 * case of a button event this is the number of the button. In case of a motion
149 * event, this is a boolean value which if true make the coordinates relatives.
150 * \lparam In case of a motion event, this is the X coordinate.
151 * \lparam In case of a motion event, this is the Y coordinate.
152 * If not specified, the current one is used.
155 luaA_root_fake_input(lua_State
*L
)
157 if(!globalconf
.have_xtest
)
159 luaA_warn(L
, "XTest extension is not available, cannot fake input.");
163 const char *stype
= luaL_checkstring(L
, 1);
164 uint8_t type
, detail
;
167 if (A_STREQ(stype
, "key_press"))
169 type
= XCB_KEY_PRESS
;
170 if(lua_type(L
, 2) == LUA_TSTRING
) {
171 detail
= _string_to_key_code(lua_tostring(L
, 2)); /* keysym */
173 detail
= luaL_checknumber(L
, 2); /* keycode */
176 else if(A_STREQ(stype
, "key_release"))
178 type
= XCB_KEY_RELEASE
;
179 if(lua_type(L
, 2) == LUA_TSTRING
) {
180 detail
= _string_to_key_code(lua_tostring(L
, 2)); /* keysym */
182 detail
= luaL_checknumber(L
, 2); /* keycode */
185 else if(A_STREQ(stype
, "button_press"))
187 type
= XCB_BUTTON_PRESS
;
188 detail
= luaL_checknumber(L
, 2); /* button number */
190 else if(A_STREQ(stype
, "button_release"))
192 type
= XCB_BUTTON_RELEASE
;
193 detail
= luaL_checknumber(L
, 2); /* button number */
195 else if(A_STREQ(stype
, "motion_notify"))
197 type
= XCB_MOTION_NOTIFY
;
198 detail
= luaA_checkboolean(L
, 2); /* relative to the current position or not */
199 x
= luaL_checknumber(L
, 3);
200 y
= luaL_checknumber(L
, 4);
205 xcb_test_fake_input(globalconf
.connection
,
215 /** Get or set global key bindings.
216 * This binding will be available when you'll press keys on root window.
217 * \param L The Lua VM state.
218 * \return The number of element pushed on stack.
220 * \lparam An array of key bindings objects, or nothing.
221 * \lreturn The array of key bindings objects of this client.
224 luaA_root_keys(lua_State
*L
)
226 if(lua_gettop(L
) == 1)
228 luaA_checktable(L
, 1);
230 foreach(key
, globalconf
.keys
)
231 luaA_object_unref(globalconf
.L
, *key
);
233 key_array_wipe(&globalconf
.keys
);
234 key_array_init(&globalconf
.keys
);
237 while(lua_next(L
, 1))
238 key_array_append(&globalconf
.keys
, luaA_object_ref_class(L
, -1, &key_class
));
240 xcb_screen_t
*s
= globalconf
.screen
;
241 xcb_ungrab_key(globalconf
.connection
, XCB_GRAB_ANY
, s
->root
, XCB_BUTTON_MASK_ANY
);
242 xwindow_grabkeys(s
->root
, &globalconf
.keys
);
247 lua_createtable(L
, globalconf
.keys
.len
, 0);
248 for(int i
= 0; i
< globalconf
.keys
.len
; i
++)
250 luaA_object_push(L
, globalconf
.keys
.tab
[i
]);
251 lua_rawseti(L
, -2, i
+ 1);
257 /** Get or set global mouse bindings.
258 * This binding will be available when you'll click on root window.
259 * \param L The Lua VM state.
260 * \return The number of element pushed on stack.
262 * \lparam An array of mouse button bindings objects, or nothing.
263 * \lreturn The array of mouse button bindings objects.
266 luaA_root_buttons(lua_State
*L
)
268 if(lua_gettop(L
) == 1)
270 luaA_checktable(L
, 1);
272 foreach(button
, globalconf
.buttons
)
273 luaA_object_unref(globalconf
.L
, *button
);
275 button_array_wipe(&globalconf
.buttons
);
276 button_array_init(&globalconf
.buttons
);
279 while(lua_next(L
, 1))
280 button_array_append(&globalconf
.buttons
, luaA_object_ref(L
, -1));
285 lua_createtable(L
, globalconf
.buttons
.len
, 0);
286 for(int i
= 0; i
< globalconf
.buttons
.len
; i
++)
288 luaA_object_push(L
, globalconf
.buttons
.tab
[i
]);
289 lua_rawseti(L
, -2, i
+ 1);
295 /** Set the root cursor.
296 * \param L The Lua VM state.
297 * \return The number of element pushed on stack.
299 * \lparam A X cursor name.
302 luaA_root_cursor(lua_State
*L
)
304 const char *cursor_name
= luaL_checkstring(L
, 1);
305 uint16_t cursor_font
= xcursor_font_fromstr(cursor_name
);
309 uint32_t change_win_vals
[] = { xcursor_new(globalconf
.cursor_ctx
, cursor_font
) };
311 xcb_change_window_attributes(globalconf
.connection
,
312 globalconf
.screen
->root
,
317 luaA_warn(L
, "invalid cursor %s", cursor_name
);
322 /** Get the drawins attached to a screen.
323 * \param L The Lua VM state.
324 * \return The number of element pushed on stack.
326 * \lreturn A table with all drawins.
329 luaA_root_drawins(lua_State
*L
)
331 lua_createtable(L
, globalconf
.drawins
.len
, 0);
333 for(int i
= 0; i
< globalconf
.drawins
.len
; i
++)
335 luaA_object_push(L
, globalconf
.drawins
.tab
[i
]);
336 lua_rawseti(L
, -2, i
+ 1);
342 /** Get the screen's wallpaper
343 * \param L The Lua VM state.
344 * \return The number of element pushed on stack.
346 * \lreturn A cairo surface for the wallpaper.
349 luaA_root_wallpaper(lua_State
*L
)
351 xcb_get_property_cookie_t prop_c
;
352 xcb_get_property_reply_t
*prop_r
;
353 xcb_get_geometry_cookie_t geom_c
;
354 xcb_get_geometry_reply_t
*geom_r
;
355 xcb_pixmap_t
*rootpix
;
356 cairo_surface_t
*surface
;
358 if(lua_gettop(L
) == 1)
360 cairo_pattern_t
*pattern
= (cairo_pattern_t
*)lua_touserdata(L
, -1);
361 lua_pushboolean(L
, root_set_wallpaper(pattern
));
362 /* Don't return the wallpaper, it's too easy to get memleaks */
366 prop_c
= xcb_get_property_unchecked(globalconf
.connection
, false,
367 globalconf
.screen
->root
, _XROOTPMAP_ID
, XCB_ATOM_PIXMAP
, 0, 1);
368 prop_r
= xcb_get_property_reply(globalconf
.connection
, prop_c
, NULL
);
370 if (!prop_r
|| !prop_r
->value_len
)
376 rootpix
= xcb_get_property_value(prop_r
);
383 geom_c
= xcb_get_geometry_unchecked(globalconf
.connection
, *rootpix
);
384 geom_r
= xcb_get_geometry_reply(globalconf
.connection
, geom_c
, NULL
);
391 /* Only the default visual makes sense, so just the default depth */
392 if (geom_r
->depth
!= draw_visual_depth(globalconf
.screen
, globalconf
.default_visual
->visual_id
))
393 warn("Got a pixmap with depth %d, but the default depth is %d, continuing anyway",
394 geom_r
->depth
, draw_visual_depth(globalconf
.screen
, globalconf
.default_visual
->visual_id
));
396 surface
= cairo_xcb_surface_create(globalconf
.connection
, *rootpix
, globalconf
.default_visual
,
397 geom_r
->width
, geom_r
->height
);
399 /* lua has to make sure this surface gets destroyed */
400 lua_pushlightuserdata(L
, surface
);
406 /** Get the screen's wallpaper
407 * \param L The Lua VM state.
408 * \return The number of element pushed on stack.
410 * \lreturn A cairo surface for the wallpaper.
413 luaA_root_tags(lua_State
*L
)
415 lua_createtable(L
, globalconf
.tags
.len
, 0);
416 for(int i
= 0; i
< globalconf
.tags
.len
; i
++)
418 luaA_object_push(L
, globalconf
.tags
.tab
[i
]);
419 lua_rawseti(L
, -2, i
+ 1);
425 const struct luaL_Reg awesome_root_lib
[] =
427 { "buttons", luaA_root_buttons
},
428 { "keys", luaA_root_keys
},
429 { "cursor", luaA_root_cursor
},
430 { "fake_input", luaA_root_fake_input
},
431 { "drawins", luaA_root_drawins
},
432 { "wallpaper", luaA_root_wallpaper
},
433 { "tags", luaA_root_tags
},
434 { "__index", luaA_default_index
},
435 { "__newindex", luaA_default_newindex
},
439 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80