Remove "encoding=utf-8" from Vim modelines
[awesome.git] / root.c
blobe33eb70684a9ed9992c039d070afc670934f2721
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 "button.h"
28 #include "wibox.h"
29 #include "luaa.h"
30 #include "window.h"
31 #include "common/xcursor.h"
32 #include "common/tokenize.h"
33 #include "common/xutil.h"
35 static xcb_keycode_t
36 _string_to_key_code(const char *s)
38 xcb_keysym_t keysym;
39 xcb_keycode_t *keycodes;
41 keysym = XStringToKeysym(s);
42 keycodes = xcb_key_symbols_get_keycode(globalconf.keysyms, keysym);
44 if(keycodes) {
45 return keycodes[0]; /* XXX only returning the first is probably not
46 * the best */
47 } else {
48 return 0;
52 /** Send fake events. Usually the current focused client will get it.
53 * \param L The Lua VM state.
54 * \return The number of element pushed on stack.
55 * \luastack
56 * \lparam The event type: key_press, key_release, button_press, button_release
57 * or motion_notify.
58 * \lparam The detail: in case of a key event, this is the keycode to send, in
59 * case of a button event this is the number of the button. In case of a motion
60 * event, this is a boolean value which if true make the coordinates relatives.
61 * \lparam In case of a motion event, this is the X coordinate.
62 * \lparam In case of a motion event, this is the Y coordinate.
63 * \lparam In case of a motion event, this is the screen number to move on.
64 * If not specified, the current one is used.
66 static int
67 luaA_root_fake_input(lua_State *L)
69 if(!globalconf.have_xtest)
71 luaA_warn(L, "XTest extension is not available, cannot fake input.");
72 return 0;
75 size_t tlen;
76 const char *stype = luaL_checklstring(L, 1, &tlen);
77 uint8_t type, detail;
78 int x = 0, y = 0;
79 xcb_window_t root = XCB_NONE;
81 switch(a_tokenize(stype, tlen))
83 case A_TK_KEY_PRESS:
84 type = XCB_KEY_PRESS;
85 if(lua_type(L, 2) == LUA_TSTRING) {
86 detail = _string_to_key_code(lua_tostring(L, 2));
87 } else {
88 detail = luaL_checknumber(L, 2); /* keycode */
90 break;
91 case A_TK_KEY_RELEASE:
92 type = XCB_KEY_RELEASE;
93 if(lua_type(L, 2) == LUA_TSTRING) {
94 detail = _string_to_key_code(lua_tostring(L, 2));
95 } else {
96 detail = luaL_checknumber(L, 2); /* keycode */
98 break;
99 case A_TK_BUTTON_PRESS:
100 type = XCB_BUTTON_PRESS;
101 detail = luaL_checknumber(L, 2); /* button number */
102 break;
103 case A_TK_BUTTON_RELEASE:
104 type = XCB_BUTTON_RELEASE;
105 detail = luaL_checknumber(L, 2); /* button number */
106 break;
107 case A_TK_MOTION_NOTIFY:
108 type = XCB_MOTION_NOTIFY;
109 detail = luaA_checkboolean(L, 2); /* relative to the current position or not */
110 x = luaL_checknumber(L, 3);
111 y = luaL_checknumber(L, 4);
112 if(lua_gettop(L) == 5 && !globalconf.xinerama_is_active)
114 int screen = luaL_checknumber(L, 5) - 1;
115 luaA_checkscreen(screen);
116 root = xutil_screen_get(globalconf.connection, screen)->root;
118 break;
119 default:
120 return 0;
123 xcb_test_fake_input(globalconf.connection,
124 type,
125 detail,
126 XCB_CURRENT_TIME,
127 root,
128 x, y,
130 return 0;
133 /** Get or set global key bindings.
134 * This binding will be available when you'll press keys on root window.
135 * \param L The Lua VM state.
136 * \return The number of element pushed on stack.
137 * \luastack
138 * \lparam An array of key bindings objects, or nothing.
139 * \lreturn The array of key bindings objects of this client.
141 static int
142 luaA_root_keys(lua_State *L)
144 if(lua_gettop(L) == 1)
146 luaA_checktable(L, 1);
148 foreach(key, globalconf.keys)
149 luaA_object_unref(globalconf.L, *key);
151 key_array_wipe(&globalconf.keys);
152 key_array_init(&globalconf.keys);
154 lua_pushnil(L);
155 while(lua_next(L, 1))
156 key_array_append(&globalconf.keys, luaA_object_ref_class(L, -1, &key_class));
158 int nscreen = xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
160 for(int phys_screen = 0; phys_screen < nscreen; phys_screen++)
162 xcb_screen_t *s = xutil_screen_get(globalconf.connection, phys_screen);
163 xcb_ungrab_key(globalconf.connection, XCB_GRAB_ANY, s->root, XCB_BUTTON_MASK_ANY);
164 window_grabkeys(s->root, &globalconf.keys);
167 return 1;
170 lua_createtable(L, globalconf.keys.len, 0);
171 for(int i = 0; i < globalconf.keys.len; i++)
173 luaA_object_push(L, globalconf.keys.tab[i]);
174 lua_rawseti(L, -2, i + 1);
177 return 1;
180 /** Get or set global mouse bindings.
181 * This binding will be available when you'll click on root window.
182 * \param L The Lua VM state.
183 * \return The number of element pushed on stack.
184 * \luastack
185 * \lparam An array of mouse button bindings objects, or nothing.
186 * \lreturn The array of mouse button bindings objects.
188 static int
189 luaA_root_buttons(lua_State *L)
191 if(lua_gettop(L) == 1)
193 luaA_checktable(L, 1);
195 foreach(button, globalconf.buttons)
196 luaA_object_unref(globalconf.L, *button);
198 button_array_wipe(&globalconf.buttons);
199 button_array_init(&globalconf.buttons);
201 lua_pushnil(L);
202 while(lua_next(L, 1))
203 button_array_append(&globalconf.buttons, luaA_object_ref(L, -1));
205 return 1;
208 lua_createtable(L, globalconf.buttons.len, 0);
209 for(int i = 0; i < globalconf.buttons.len; i++)
211 luaA_object_push(L, globalconf.buttons.tab[i]);
212 lua_rawseti(L, -2, i + 1);
215 return 1;
218 /** Set the root cursor.
219 * \param L The Lua VM state.
220 * \return The number of element pushed on stack.
221 * \luastack
222 * \lparam A X cursor name.
224 static int
225 luaA_root_cursor(lua_State *L)
227 const char *cursor_name = luaL_checkstring(L, 1);
228 uint16_t cursor_font = xcursor_font_fromstr(cursor_name);
230 if(cursor_font)
232 uint32_t change_win_vals[] = { xcursor_new(globalconf.connection, cursor_font) };
234 for(int screen_nbr = 0;
235 screen_nbr < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
236 screen_nbr++)
237 xcb_change_window_attributes(globalconf.connection,
238 xutil_screen_get(globalconf.connection, screen_nbr)->root,
239 XCB_CW_CURSOR,
240 change_win_vals);
242 else
243 luaA_warn(L, "invalid cursor %s", cursor_name);
245 return 0;
248 /** Get the wiboxes attached to a screen.
249 * \param L The Lua VM state.
250 * \return The number of element pushed on stack.
251 * \luastack
252 * \lreturn A table with all wiboxes.
254 static int
255 luaA_root_wiboxes(lua_State *L)
257 lua_createtable(L, globalconf.wiboxes.len, 0);
259 for(int i = 0; i < globalconf.wiboxes.len; i++)
261 luaA_object_push(L, globalconf.wiboxes.tab[i]);
262 lua_rawseti(L, -2, i + 1);
265 return 1;
268 const struct luaL_reg awesome_root_lib[] =
270 { "buttons", luaA_root_buttons },
271 { "keys", luaA_root_keys },
272 { "cursor", luaA_root_cursor },
273 { "fake_input", luaA_root_fake_input },
274 { "wiboxes", luaA_root_wiboxes },
275 { NULL, NULL }
278 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80