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>
28 #include "common/xcursor.h"
29 #include "common/tokenize.h"
30 #include "common/xutil.h"
32 /** Send fake events. Usually the current focused client will get it.
33 * \param L The Lua VM state.
34 * \return The number of element pushed on stack.
36 * \lparam The event type: key_press, key_release, button_press, button_release
38 * \lparam The detail: in case of a key event, this is the keycode to send, in
39 * case of a button event this is the number of the button. In case of a motion
40 * event, this is a boolean value which if true make the coordinates relatives.
41 * \lparam In case of a motion event, this is the X coordinate.
42 * \lparam In case of a motion event, this is the Y coordinate.
43 * \lparam In case of a motion event, this is the screen number to move on.
44 * If not specified, the current one is used.
47 luaA_root_fake_input(lua_State
*L
)
49 if(!globalconf
.have_xtest
)
51 luaA_warn(L
, "XTest extension is not available, cannot fake input.");
56 const char *stype
= luaL_checklstring(L
, 1, &tlen
);
59 xcb_window_t root
= XCB_NONE
;
61 switch(a_tokenize(stype
, tlen
))
65 detail
= luaL_checknumber(L
, 2); /* keycode */
67 case A_TK_KEY_RELEASE
:
68 type
= XCB_KEY_RELEASE
;
69 detail
= luaL_checknumber(L
, 2); /* keycode */
71 case A_TK_BUTTON_PRESS
:
72 type
= XCB_BUTTON_PRESS
;
73 detail
= luaL_checknumber(L
, 2); /* button number */
75 case A_TK_BUTTON_RELEASE
:
76 type
= XCB_BUTTON_RELEASE
;
77 detail
= luaL_checknumber(L
, 2); /* button number */
79 case A_TK_MOTION_NOTIFY
:
80 type
= XCB_MOTION_NOTIFY
;
81 detail
= luaA_checkboolean(L
, 2); /* relative to the current position or not */
82 x
= luaL_checknumber(L
, 3);
83 y
= luaL_checknumber(L
, 4);
84 if(lua_gettop(L
) == 5 && !globalconf
.xinerama_is_active
)
86 int screen
= luaL_checknumber(L
, 5) - 1;
87 luaA_checkscreen(screen
);
88 root
= xutil_screen_get(globalconf
.connection
, screen
)->root
;
95 xcb_test_fake_input(globalconf
.connection
,
105 /** Get or set global key bindings.
106 * This binding will be available when you'll press keys on root window.
107 * \param L The Lua VM state.
108 * \return The number of element pushed on stack.
110 * \lparam An array of key bindings objects, or nothing.
111 * \lreturn The array of key bindings objects of this client.
114 luaA_root_keys(lua_State
*L
)
116 if(lua_gettop(L
) == 1)
118 luaA_checktable(L
, 1);
120 foreach(key
, globalconf
.keys
)
121 luaA_object_unref(globalconf
.L
, *key
);
123 key_array_wipe(&globalconf
.keys
);
124 key_array_init(&globalconf
.keys
);
127 while(lua_next(L
, 1))
128 key_array_append(&globalconf
.keys
, luaA_object_ref(L
, -1));
130 int nscreen
= xcb_setup_roots_length(xcb_get_setup(globalconf
.connection
));
132 for(int phys_screen
= 0; phys_screen
< nscreen
; phys_screen
++)
134 xcb_screen_t
*s
= xutil_screen_get(globalconf
.connection
, phys_screen
);
135 xcb_ungrab_key(globalconf
.connection
, XCB_GRAB_ANY
, s
->root
, XCB_BUTTON_MASK_ANY
);
136 window_grabkeys(s
->root
, &globalconf
.keys
);
142 lua_createtable(L
, globalconf
.keys
.len
, 0);
143 for(int i
= 0; i
< globalconf
.keys
.len
; i
++)
145 luaA_object_push(L
, globalconf
.keys
.tab
[i
]);
146 lua_rawseti(L
, -2, i
+ 1);
152 /** Get or set global mouse bindings.
153 * This binding will be available when you'll click on root window.
154 * \param L The Lua VM state.
155 * \return The number of element pushed on stack.
157 * \lparam An array of mouse button bindings objects, or nothing.
158 * \lreturn The array of mouse button bindings objects.
161 luaA_root_buttons(lua_State
*L
)
163 if(lua_gettop(L
) == 1)
165 luaA_checktable(L
, 1);
167 foreach(button
, globalconf
.buttons
)
168 luaA_object_unref(globalconf
.L
, *button
);
170 button_array_wipe(&globalconf
.buttons
);
171 button_array_init(&globalconf
.buttons
);
174 while(lua_next(L
, 1))
175 button_array_append(&globalconf
.buttons
, luaA_object_ref(L
, -1));
180 lua_createtable(L
, globalconf
.buttons
.len
, 0);
181 for(int i
= 0; i
< globalconf
.buttons
.len
; i
++)
183 luaA_object_push(L
, globalconf
.buttons
.tab
[i
]);
184 lua_rawseti(L
, -2, i
+ 1);
190 /** Set the root cursor.
191 * \param L The Lua VM state.
192 * \return The number of element pushed on stack.
194 * \lparam A X cursor name.
197 luaA_root_cursor(lua_State
*L
)
199 const char *cursor_name
= luaL_checkstring(L
, 1);
200 uint16_t cursor_font
= xcursor_font_fromstr(cursor_name
);
204 uint32_t change_win_vals
[] = { xcursor_new(globalconf
.connection
, cursor_font
) };
206 for(int screen_nbr
= 0;
207 screen_nbr
< xcb_setup_roots_length(xcb_get_setup(globalconf
.connection
));
209 xcb_change_window_attributes(globalconf
.connection
,
210 xutil_screen_get(globalconf
.connection
, screen_nbr
)->root
,
215 luaA_warn(L
, "invalid cursor %s", cursor_name
);
220 /** Get the wiboxes attached to a screen.
221 * \param L The Lua VM state.
222 * \return The number of element pushed on stack.
224 * \lreturn A table with all wiboxes.
227 luaA_root_wiboxes(lua_State
*L
)
229 lua_createtable(L
, globalconf
.wiboxes
.len
, 0);
231 for(int i
= 0; i
< globalconf
.wiboxes
.len
; i
++)
233 luaA_object_push(L
, globalconf
.wiboxes
.tab
[i
]);
234 lua_rawseti(L
, -2, i
+ 1);
240 const struct luaL_reg awesome_root_lib
[] =
242 { "buttons", luaA_root_buttons
},
243 { "keys", luaA_root_keys
},
244 { "cursor", luaA_root_cursor
},
245 { "fake_input", luaA_root_fake_input
},
246 { "wiboxes", luaA_root_wiboxes
},
250 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80