image: load image without cache (FS#651)
[awesome.git] / root.c
blobcf9e8f978a3b284e8d4178459e67f7b329e702f8
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 <xcb/xtest.h>
24 #include "globalconf.h"
25 #include "button.h"
26 #include "wibox.h"
27 #include "luaa.h"
28 #include "window.h"
29 #include "common/xcursor.h"
30 #include "common/tokenize.h"
31 #include "common/xutil.h"
33 /** Send fake events. Usually the current focused client will get it.
34 * \param L The Lua VM state.
35 * \return The number of element pushed on stack.
36 * \luastack
37 * \lparam The event type: key_press, key_release, button_press, button_release
38 * or motion_notify.
39 * \lparam The detail: in case of a key event, this is the keycode to send, in
40 * case of a button event this is the number of the button. In case of a motion
41 * event, this is a boolean value which if true make the coordinates relatives.
42 * \lparam In case of a motion event, this is the X coordinate.
43 * \lparam In case of a motion event, this is the Y coordinate.
44 * \lparam In case of a motion event, this is the screen number to move on.
45 * If not specified, the current one is used.
47 static int
48 luaA_root_fake_input(lua_State *L)
50 if(!globalconf.have_xtest)
52 luaA_warn(L, "XTest extension is not available, cannot fake input.");
53 return 0;
56 size_t tlen;
57 const char *stype = luaL_checklstring(L, 1, &tlen);
58 uint8_t type, detail;
59 int x = 0, y = 0;
60 xcb_window_t root = XCB_NONE;
62 switch(a_tokenize(stype, tlen))
64 case A_TK_KEY_PRESS:
65 type = XCB_KEY_PRESS;
66 detail = luaL_checknumber(L, 2); /* keycode */
67 break;
68 case A_TK_KEY_RELEASE:
69 type = XCB_KEY_RELEASE;
70 detail = luaL_checknumber(L, 2); /* keycode */
71 break;
72 case A_TK_BUTTON_PRESS:
73 type = XCB_BUTTON_PRESS;
74 detail = luaL_checknumber(L, 2); /* button number */
75 break;
76 case A_TK_BUTTON_RELEASE:
77 type = XCB_BUTTON_RELEASE;
78 detail = luaL_checknumber(L, 2); /* button number */
79 break;
80 case A_TK_MOTION_NOTIFY:
81 type = XCB_MOTION_NOTIFY;
82 detail = luaA_checkboolean(L, 2); /* relative to the current position or not */
83 x = luaL_checknumber(L, 3);
84 y = luaL_checknumber(L, 4);
85 if(lua_gettop(L) == 5 && !globalconf.xinerama_is_active)
87 int screen = luaL_checknumber(L, 5) - 1;
88 luaA_checkscreen(screen);
89 root = xutil_screen_get(globalconf.connection, screen)->root;
91 break;
92 default:
93 return 0;
96 xcb_test_fake_input(globalconf.connection,
97 type,
98 detail,
99 XCB_CURRENT_TIME,
100 root,
101 x, y,
103 return 0;
106 /** Get or set global key bindings.
107 * This binding will be available when you'll press keys on root window.
108 * \param L The Lua VM state.
109 * \return The number of element pushed on stack.
110 * \luastack
111 * \lparam An array of key bindings objects, or nothing.
112 * \lreturn The array of key bindings objects of this client.
114 static int
115 luaA_root_keys(lua_State *L)
117 if(lua_gettop(L) == 1)
119 luaA_checktable(L, 1);
121 foreach(key, globalconf.keys)
122 luaA_object_unref(globalconf.L, *key);
124 key_array_wipe(&globalconf.keys);
125 key_array_init(&globalconf.keys);
127 lua_pushnil(L);
128 while(lua_next(L, 1))
129 key_array_append(&globalconf.keys, luaA_object_ref_class(L, -1, &key_class));
131 int nscreen = xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
133 for(int phys_screen = 0; phys_screen < nscreen; phys_screen++)
135 xcb_screen_t *s = xutil_screen_get(globalconf.connection, phys_screen);
136 xcb_ungrab_key(globalconf.connection, XCB_GRAB_ANY, s->root, XCB_BUTTON_MASK_ANY);
137 window_grabkeys(s->root, &globalconf.keys);
140 return 1;
143 lua_createtable(L, globalconf.keys.len, 0);
144 for(int i = 0; i < globalconf.keys.len; i++)
146 luaA_object_push(L, globalconf.keys.tab[i]);
147 lua_rawseti(L, -2, i + 1);
150 return 1;
153 /** Get or set global mouse bindings.
154 * This binding will be available when you'll click on root window.
155 * \param L The Lua VM state.
156 * \return The number of element pushed on stack.
157 * \luastack
158 * \lparam An array of mouse button bindings objects, or nothing.
159 * \lreturn The array of mouse button bindings objects.
161 static int
162 luaA_root_buttons(lua_State *L)
164 if(lua_gettop(L) == 1)
166 luaA_checktable(L, 1);
168 foreach(button, globalconf.buttons)
169 luaA_object_unref(globalconf.L, *button);
171 button_array_wipe(&globalconf.buttons);
172 button_array_init(&globalconf.buttons);
174 lua_pushnil(L);
175 while(lua_next(L, 1))
176 button_array_append(&globalconf.buttons, luaA_object_ref(L, -1));
178 return 1;
181 lua_createtable(L, globalconf.buttons.len, 0);
182 for(int i = 0; i < globalconf.buttons.len; i++)
184 luaA_object_push(L, globalconf.buttons.tab[i]);
185 lua_rawseti(L, -2, i + 1);
188 return 1;
191 /** Set the root cursor.
192 * \param L The Lua VM state.
193 * \return The number of element pushed on stack.
194 * \luastack
195 * \lparam A X cursor name.
197 static int
198 luaA_root_cursor(lua_State *L)
200 const char *cursor_name = luaL_checkstring(L, 1);
201 uint16_t cursor_font = xcursor_font_fromstr(cursor_name);
203 if(cursor_font)
205 uint32_t change_win_vals[] = { xcursor_new(globalconf.connection, cursor_font) };
207 for(int screen_nbr = 0;
208 screen_nbr < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
209 screen_nbr++)
210 xcb_change_window_attributes(globalconf.connection,
211 xutil_screen_get(globalconf.connection, screen_nbr)->root,
212 XCB_CW_CURSOR,
213 change_win_vals);
215 else
216 luaA_warn(L, "invalid cursor %s", cursor_name);
218 return 0;
221 /** Get the wiboxes attached to a screen.
222 * \param L The Lua VM state.
223 * \return The number of element pushed on stack.
224 * \luastack
225 * \lreturn A table with all wiboxes.
227 static int
228 luaA_root_wiboxes(lua_State *L)
230 lua_createtable(L, globalconf.wiboxes.len, 0);
232 for(int i = 0; i < globalconf.wiboxes.len; i++)
234 luaA_object_push(L, globalconf.wiboxes.tab[i]);
235 lua_rawseti(L, -2, i + 1);
238 return 1;
241 const struct luaL_reg awesome_root_lib[] =
243 { "buttons", luaA_root_buttons },
244 { "keys", luaA_root_keys },
245 { "cursor", luaA_root_cursor },
246 { "fake_input", luaA_root_fake_input },
247 { "wiboxes", luaA_root_wiboxes },
248 { NULL, NULL }
251 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80