screen: Add screen.by_coords()
[awesome.git] / root.c
blob828d1ee599d2f509ea7edf31a024e0455d01df20
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>
26 #include "globalconf.h"
27 #include "objects/button.h"
28 #include "objects/drawin.h"
29 #include "luaa.h"
30 #include "xwindow.h"
31 #include "common/xcursor.h"
32 #include "common/xutil.h"
34 static xcb_keycode_t
35 _string_to_key_code(const char *s)
37 xcb_keysym_t keysym;
38 xcb_keycode_t *keycodes;
40 keysym = XStringToKeysym(s);
41 keycodes = xcb_key_symbols_get_keycode(globalconf.keysyms, keysym);
43 if(keycodes) {
44 return keycodes[0]; /* XXX only returning the first is probably not
45 * the best */
46 } else {
47 return 0;
51 /** Send fake events. Usually the current focused client will get it.
52 * \param L The Lua VM state.
53 * \return The number of element pushed on stack.
54 * \luastack
55 * \lparam The event type: key_press, key_release, button_press, button_release
56 * or motion_notify.
57 * \lparam The detail: in case of a key event, this is the keycode to send, in
58 * case of a button event this is the number of the button. In case of a motion
59 * event, this is a boolean value which if true make the coordinates relatives.
60 * \lparam In case of a motion event, this is the X coordinate.
61 * \lparam In case of a motion event, this is the Y coordinate.
62 * If not specified, the current one is used.
64 static int
65 luaA_root_fake_input(lua_State *L)
67 if(!globalconf.have_xtest)
69 luaA_warn(L, "XTest extension is not available, cannot fake input.");
70 return 0;
73 const char *stype = luaL_checkstring(L, 1);
74 uint8_t type, detail;
75 int x = 0, y = 0;
77 if (A_STREQ(stype, "key_press"))
79 type = XCB_KEY_PRESS;
80 if(lua_type(L, 2) == LUA_TSTRING) {
81 detail = _string_to_key_code(lua_tostring(L, 2)); /* keysym */
82 } else {
83 detail = luaL_checknumber(L, 2); /* keycode */
86 else if(A_STREQ(stype, "key_release"))
88 type = XCB_KEY_RELEASE;
89 if(lua_type(L, 2) == LUA_TSTRING) {
90 detail = _string_to_key_code(lua_tostring(L, 2)); /* keysym */
91 } else {
92 detail = luaL_checknumber(L, 2); /* keycode */
95 else if(A_STREQ(stype, "button_press"))
97 type = XCB_BUTTON_PRESS;
98 detail = luaL_checknumber(L, 2); /* button number */
100 else if(A_STREQ(stype, "button_release"))
102 type = XCB_BUTTON_RELEASE;
103 detail = luaL_checknumber(L, 2); /* button number */
105 else if(A_STREQ(stype, "motion_notify"))
107 type = XCB_MOTION_NOTIFY;
108 detail = luaA_checkboolean(L, 2); /* relative to the current position or not */
109 x = luaL_checknumber(L, 3);
110 y = luaL_checknumber(L, 4);
112 else
113 return 0;
115 xcb_test_fake_input(globalconf.connection,
116 type,
117 detail,
118 XCB_CURRENT_TIME,
119 XCB_NONE,
120 x, y,
122 return 0;
125 /** Get or set global key bindings.
126 * This binding will be available when you'll press keys on root window.
127 * \param L The Lua VM state.
128 * \return The number of element pushed on stack.
129 * \luastack
130 * \lparam An array of key bindings objects, or nothing.
131 * \lreturn The array of key bindings objects of this client.
133 static int
134 luaA_root_keys(lua_State *L)
136 if(lua_gettop(L) == 1)
138 luaA_checktable(L, 1);
140 foreach(key, globalconf.keys)
141 luaA_object_unref(globalconf.L, *key);
143 key_array_wipe(&globalconf.keys);
144 key_array_init(&globalconf.keys);
146 lua_pushnil(L);
147 while(lua_next(L, 1))
148 key_array_append(&globalconf.keys, luaA_object_ref_class(L, -1, &key_class));
150 xcb_screen_t *s = globalconf.screen;
151 xcb_ungrab_key(globalconf.connection, XCB_GRAB_ANY, s->root, XCB_BUTTON_MASK_ANY);
152 xwindow_grabkeys(s->root, &globalconf.keys);
154 return 1;
157 lua_createtable(L, globalconf.keys.len, 0);
158 for(int i = 0; i < globalconf.keys.len; i++)
160 luaA_object_push(L, globalconf.keys.tab[i]);
161 lua_rawseti(L, -2, i + 1);
164 return 1;
167 /** Get or set global mouse bindings.
168 * This binding will be available when you'll click on root window.
169 * \param L The Lua VM state.
170 * \return The number of element pushed on stack.
171 * \luastack
172 * \lparam An array of mouse button bindings objects, or nothing.
173 * \lreturn The array of mouse button bindings objects.
175 static int
176 luaA_root_buttons(lua_State *L)
178 if(lua_gettop(L) == 1)
180 luaA_checktable(L, 1);
182 foreach(button, globalconf.buttons)
183 luaA_object_unref(globalconf.L, *button);
185 button_array_wipe(&globalconf.buttons);
186 button_array_init(&globalconf.buttons);
188 lua_pushnil(L);
189 while(lua_next(L, 1))
190 button_array_append(&globalconf.buttons, luaA_object_ref(L, -1));
192 return 1;
195 lua_createtable(L, globalconf.buttons.len, 0);
196 for(int i = 0; i < globalconf.buttons.len; i++)
198 luaA_object_push(L, globalconf.buttons.tab[i]);
199 lua_rawseti(L, -2, i + 1);
202 return 1;
205 /** Set the root cursor.
206 * \param L The Lua VM state.
207 * \return The number of element pushed on stack.
208 * \luastack
209 * \lparam A X cursor name.
211 static int
212 luaA_root_cursor(lua_State *L)
214 const char *cursor_name = luaL_checkstring(L, 1);
215 uint16_t cursor_font = xcursor_font_fromstr(cursor_name);
217 if(cursor_font)
219 uint32_t change_win_vals[] = { xcursor_new(globalconf.connection, cursor_font) };
221 xcb_change_window_attributes(globalconf.connection,
222 globalconf.screen->root,
223 XCB_CW_CURSOR,
224 change_win_vals);
226 else
227 luaA_warn(L, "invalid cursor %s", cursor_name);
229 return 0;
232 /** Get the drawins attached to a screen.
233 * \param L The Lua VM state.
234 * \return The number of element pushed on stack.
235 * \luastack
236 * \lreturn A table with all drawins.
238 static int
239 luaA_root_drawins(lua_State *L)
241 lua_createtable(L, globalconf.drawins.len, 0);
243 for(int i = 0; i < globalconf.drawins.len; i++)
245 luaA_object_push(L, globalconf.drawins.tab[i]);
246 lua_rawseti(L, -2, i + 1);
249 return 1;
252 const struct luaL_reg awesome_root_lib[] =
254 { "buttons", luaA_root_buttons },
255 { "keys", luaA_root_keys },
256 { "cursor", luaA_root_cursor },
257 { "fake_input", luaA_root_fake_input },
258 { "drawins", luaA_root_drawins },
259 { NULL, NULL }
262 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80