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"
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.
37 * \lparam The event type: key_press, key_release, button_press, button_release
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.
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.");
57 const char *stype
= luaL_checklstring(L
, 1, &tlen
);
60 xcb_window_t root
= XCB_NONE
;
62 switch(a_tokenize(stype
, tlen
))
66 detail
= luaL_checknumber(L
, 2); /* keycode */
68 case A_TK_KEY_RELEASE
:
69 type
= XCB_KEY_RELEASE
;
70 detail
= luaL_checknumber(L
, 2); /* keycode */
72 case A_TK_BUTTON_PRESS
:
73 type
= XCB_BUTTON_PRESS
;
74 detail
= luaL_checknumber(L
, 2); /* button number */
76 case A_TK_BUTTON_RELEASE
:
77 type
= XCB_BUTTON_RELEASE
;
78 detail
= luaL_checknumber(L
, 2); /* button number */
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
;
96 xcb_test_fake_input(globalconf
.connection
,
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.
111 * \lparam An array of key bindings objects, or nothing.
112 * \lreturn The array of key bindings objects of this client.
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
);
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
);
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);
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.
158 * \lparam An array of mouse button bindings objects, or nothing.
159 * \lreturn The array of mouse button bindings objects.
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
);
175 while(lua_next(L
, 1))
176 button_array_append(&globalconf
.buttons
, luaA_object_ref(L
, -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);
191 /** Set the root cursor.
192 * \param L The Lua VM state.
193 * \return The number of element pushed on stack.
195 * \lparam A X cursor name.
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
);
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
));
210 xcb_change_window_attributes(globalconf
.connection
,
211 xutil_screen_get(globalconf
.connection
, screen_nbr
)->root
,
216 luaA_warn(L
, "invalid cursor %s", cursor_name
);
221 /** Get the wiboxes attached to a screen.
222 * \param L The Lua VM state.
223 * \return The number of element pushed on stack.
225 * \lreturn A table with all wiboxes.
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);
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
},
251 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80