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"
25 #include "objects/button.h"
26 #include "objects/drawin.h"
29 #include "common/xcursor.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 * If not specified, the current one is used.
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.");
54 const char *stype
= luaL_checkstring(L
, 1);
58 if(a_strcmp(stype
, "key_press") == 0)
61 detail
= luaL_checknumber(L
, 2); /* keycode */
63 else if(a_strcmp(stype
, "key_release") == 0)
65 type
= XCB_KEY_RELEASE
;
66 detail
= luaL_checknumber(L
, 2); /* keycode */
68 else if(a_strcmp(stype
, "button_press") == 0)
70 type
= XCB_BUTTON_PRESS
;
71 detail
= luaL_checknumber(L
, 2); /* button number */
73 else if(a_strcmp(stype
, "button_release") == 0)
75 type
= XCB_BUTTON_RELEASE
;
76 detail
= luaL_checknumber(L
, 2); /* button number */
78 else if(a_strcmp(stype
, "motion_notify") == 0)
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);
88 xcb_test_fake_input(globalconf
.connection
,
98 /** Get or set global key bindings.
99 * This binding will be available when you'll press keys on root window.
100 * \param L The Lua VM state.
101 * \return The number of element pushed on stack.
103 * \lparam An array of key bindings objects, or nothing.
104 * \lreturn The array of key bindings objects of this client.
107 luaA_root_keys(lua_State
*L
)
109 if(lua_gettop(L
) == 1)
111 luaA_checktable(L
, 1);
113 foreach(key
, globalconf
.keys
)
114 luaA_object_unref(globalconf
.L
, *key
);
116 key_array_wipe(&globalconf
.keys
);
117 key_array_init(&globalconf
.keys
);
120 while(lua_next(L
, 1))
121 key_array_append(&globalconf
.keys
, luaA_object_ref_class(L
, -1, &key_class
));
123 xcb_screen_t
*s
= globalconf
.screen
;
124 xcb_ungrab_key(globalconf
.connection
, XCB_GRAB_ANY
, s
->root
, XCB_BUTTON_MASK_ANY
);
125 xwindow_grabkeys(s
->root
, &globalconf
.keys
);
130 lua_createtable(L
, globalconf
.keys
.len
, 0);
131 for(int i
= 0; i
< globalconf
.keys
.len
; i
++)
133 luaA_object_push(L
, globalconf
.keys
.tab
[i
]);
134 lua_rawseti(L
, -2, i
+ 1);
140 /** Get or set global mouse bindings.
141 * This binding will be available when you'll click on root window.
142 * \param L The Lua VM state.
143 * \return The number of element pushed on stack.
145 * \lparam An array of mouse button bindings objects, or nothing.
146 * \lreturn The array of mouse button bindings objects.
149 luaA_root_buttons(lua_State
*L
)
151 if(lua_gettop(L
) == 1)
153 luaA_checktable(L
, 1);
155 foreach(button
, globalconf
.buttons
)
156 luaA_object_unref(globalconf
.L
, *button
);
158 button_array_wipe(&globalconf
.buttons
);
159 button_array_init(&globalconf
.buttons
);
162 while(lua_next(L
, 1))
163 button_array_append(&globalconf
.buttons
, luaA_object_ref(L
, -1));
168 lua_createtable(L
, globalconf
.buttons
.len
, 0);
169 for(int i
= 0; i
< globalconf
.buttons
.len
; i
++)
171 luaA_object_push(L
, globalconf
.buttons
.tab
[i
]);
172 lua_rawseti(L
, -2, i
+ 1);
178 /** Set the root cursor.
179 * \param L The Lua VM state.
180 * \return The number of element pushed on stack.
182 * \lparam A X cursor name.
185 luaA_root_cursor(lua_State
*L
)
187 const char *cursor_name
= luaL_checkstring(L
, 1);
188 uint16_t cursor_font
= xcursor_font_fromstr(cursor_name
);
192 uint32_t change_win_vals
[] = { xcursor_new(globalconf
.connection
, cursor_font
) };
194 xcb_change_window_attributes(globalconf
.connection
,
195 globalconf
.screen
->root
,
200 luaA_warn(L
, "invalid cursor %s", cursor_name
);
205 /** Get the drawins attached to a screen.
206 * \param L The Lua VM state.
207 * \return The number of element pushed on stack.
209 * \lreturn A table with all drawins.
212 luaA_root_drawins(lua_State
*L
)
214 lua_createtable(L
, globalconf
.drawins
.len
, 0);
216 for(int i
= 0; i
< globalconf
.drawins
.len
; i
++)
218 luaA_object_push(L
, globalconf
.drawins
.tab
[i
]);
219 lua_rawseti(L
, -2, i
+ 1);
225 const struct luaL_reg awesome_root_lib
[] =
227 { "buttons", luaA_root_buttons
},
228 { "keys", luaA_root_keys
},
229 { "cursor", luaA_root_cursor
},
230 { "fake_input", luaA_root_fake_input
},
231 { "drawins", luaA_root_drawins
},
235 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80