array: check that we are still in the tab
[awesome.git] / root.c
blob1c4962b1882f084144b11a22e4b1f06f991b1239
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 "structs.h"
25 #include "button.h"
26 #include "common/xcursor.h"
27 #include "common/tokenize.h"
28 #include "common/xutil.h"
30 /** Send fake events. Usually the current focused client will get it.
31 * \param L The Lua VM state.
32 * \return The number of element pushed on stack.
33 * \luastack
34 * \lvalue A client.
35 * \lparam The event type: key_press, key_release, button_press, button_release
36 * or motion_notify.
37 * \lparam The detail: in case of a key event, this is the keycode to send, in
38 * case of a button event this is the number of the button. In case of a motion
39 * event, this is a boolean value which if true make the coordinates relatives.
40 * \lparam In case of a motion event, this is the X coordinate.
41 * \lparam In case of a motion event, this is the Y coordinate.
42 * \lparam In case of a motion event, this is the screen number to move on.
43 * If not specified, the current one is used.
45 static int
46 luaA_root_fake_input(lua_State *L)
48 if(!globalconf.have_xtest)
50 luaA_warn(L, "XTest extension is not available, cannot fake input.");
51 return 0;
54 size_t tlen;
55 const char *stype = luaL_checklstring(L, 2, &tlen);
56 uint8_t type, detail;
57 int x = 0, y = 0;
58 xcb_window_t root = XCB_NONE;
60 switch(a_tokenize(stype, tlen))
62 case A_TK_KEY_PRESS:
63 type = XCB_KEY_PRESS;
64 detail = luaL_checknumber(L, 3); /* keycode */
65 break;
66 case A_TK_KEY_RELEASE:
67 type = XCB_KEY_RELEASE;
68 detail = luaL_checknumber(L, 3); /* keycode */
69 break;
70 case A_TK_BUTTON_PRESS:
71 type = XCB_BUTTON_PRESS;
72 detail = luaL_checknumber(L, 3); /* button number */
73 break;
74 case A_TK_BUTTON_RELEASE:
75 type = XCB_BUTTON_RELEASE;
76 detail = luaL_checknumber(L, 3); /* button number */
77 break;
78 case A_TK_MOTION_NOTIFY:
79 type = XCB_MOTION_NOTIFY;
80 detail = luaA_checkboolean(L, 3); /* relative to the current position or not */
81 x = luaL_checknumber(L, 4);
82 y = luaL_checknumber(L, 5);
83 if(lua_gettop(L) == 6 && !globalconf.xinerama_is_active)
85 int screen = luaL_checknumber(L, 6);
86 luaA_checkscreen(screen);
87 root = xutil_screen_get(globalconf.connection, screen)->root;
89 break;
90 default:
91 return 0;
94 xcb_test_fake_input(globalconf.connection,
95 type,
96 detail,
97 XCB_CURRENT_TIME,
98 root,
99 x, y,
101 return 0;
104 /** Get or set global key bindings.
105 * This binding will be available when you'll press keys on root window.
106 * \param L The Lua VM state.
107 * \return The number of element pushed on stack.
108 * \luastack
109 * \lparam An array of key bindings objects, or nothing.
110 * \lreturn The array of key bindings objects of this client.
112 static int
113 luaA_root_keys(lua_State *L)
115 if(lua_gettop(L) == 1)
117 luaA_key_array_set(L, 1, &globalconf.keys);
119 int nscreen = xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
121 for(int phys_screen = 0; phys_screen < nscreen; phys_screen++)
123 xcb_screen_t *s = xutil_screen_get(globalconf.connection, phys_screen);
124 xcb_ungrab_key(globalconf.connection, XCB_GRAB_ANY, s->root, XCB_BUTTON_MASK_ANY);
125 window_grabkeys(s->root, &globalconf.keys);
129 return luaA_key_array_get(L, &globalconf.keys);
132 /** Get or set global mouse bindings.
133 * This binding will be available when you'll click on root window.
134 * \param L The Lua VM state.
135 * \return The number of element pushed on stack.
136 * \luastack
137 * \lparam An array of mouse button bindings objects, or nothing.
138 * \lreturn The array of mouse button bindings objects.
140 static int
141 luaA_root_buttons(lua_State *L)
143 if(lua_gettop(L) == 1)
144 luaA_button_array_set(L, 1, &globalconf.buttons);
146 return luaA_button_array_get(L, &globalconf.buttons);
149 /** Set the root cursor.
150 * \param L The Lua VM state.
151 * \return The number of element pushed on stack.
152 * \luastack
153 * \lparam A X cursor name.
155 static int
156 luaA_root_cursor(lua_State *L)
158 const char *cursor_name = luaL_checkstring(L, 1);
159 uint16_t cursor_font = xcursor_font_fromstr(cursor_name);
161 if(cursor_font)
163 uint32_t change_win_vals[] = { xcursor_new(globalconf.connection, cursor_font) };
165 for(int screen_nbr = 0;
166 screen_nbr < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
167 screen_nbr++)
168 xcb_change_window_attributes(globalconf.connection,
169 xutil_screen_get(globalconf.connection, screen_nbr)->root,
170 XCB_CW_CURSOR,
171 change_win_vals);
173 else
174 luaA_warn(L, "invalid cursor %s", cursor_name);
176 return 0;
179 const struct luaL_reg awesome_root_lib[] =
181 { "buttons", luaA_root_buttons },
182 { "keys", luaA_root_keys },
183 { "cursor", luaA_root_cursor },
184 { "fake_input", luaA_root_fake_input },
185 { NULL, NULL }
188 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80