awful.util: table.join ignore nil arguments
[awesome.git] / root.c
blob0d7d408c9cb5e35198a48f2fadeab707df97fb6f
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 "mouse.h"
25 #include "common/xcursor.h"
26 #include "common/tokenize.h"
27 #include "common/xutil.h"
29 /** Send fake events. Usually the current focused client will get it.
30 * \param L The Lua VM state.
31 * \return The number of element pushed on stack.
32 * \luastack
33 * \lvalue A client.
34 * \lparam The event type: key_press, key_release, button_press, button_release
35 * or motion_notify.
36 * \lparam The detail: in case of a key event, this is the keycode to send, in
37 * case of a button event this is the number of the button. In case of a motion
38 * event, this is a boolean value which if true make the coordinates relatives.
39 * \lparam In case of a motion event, this is the X coordinate.
40 * \lparam In case of a motion event, this is the Y coordinate.
41 * \lparam In case of a motion event, this is the screen number to move on.
42 * If not specified, the current one is used.
44 static int
45 luaA_root_fake_input(lua_State *L)
47 if(!globalconf.have_xtest)
49 luaA_warn(L, "XTest extension is not available, cannot fake input.");
50 return 0;
53 size_t tlen;
54 const char *stype = luaL_checklstring(L, 2, &tlen);
55 uint8_t type, detail;
56 int x = 0, y = 0;
57 xcb_window_t root = XCB_NONE;
59 switch(a_tokenize(stype, tlen))
61 case A_TK_KEY_PRESS:
62 type = XCB_KEY_PRESS;
63 detail = luaL_checknumber(L, 3); /* keycode */
64 break;
65 case A_TK_KEY_RELEASE:
66 type = XCB_KEY_RELEASE;
67 detail = luaL_checknumber(L, 3); /* keycode */
68 break;
69 case A_TK_BUTTON_PRESS:
70 type = XCB_BUTTON_PRESS;
71 detail = luaL_checknumber(L, 3); /* button number */
72 break;
73 case A_TK_BUTTON_RELEASE:
74 type = XCB_BUTTON_RELEASE;
75 detail = luaL_checknumber(L, 3); /* button number */
76 break;
77 case A_TK_MOTION_NOTIFY:
78 type = XCB_MOTION_NOTIFY;
79 detail = luaA_checkboolean(L, 3); /* relative to the current position or not */
80 x = luaL_checknumber(L, 4);
81 y = luaL_checknumber(L, 5);
82 if(lua_gettop(L) == 6 && !globalconf.xinerama_is_active)
84 int screen = luaL_checknumber(L, 6);
85 luaA_checkscreen(screen);
86 root = xutil_screen_get(globalconf.connection, screen)->root;
88 break;
89 default:
90 return 0;
93 xcb_test_fake_input(globalconf.connection,
94 type,
95 detail,
96 XCB_CURRENT_TIME,
97 root,
98 x, y,
99 0);
100 return 0;
103 /** Get or set global key bindings.
104 * This binding will be available when you'll press keys on root window.
105 * \param L The Lua VM state.
106 * \return The number of element pushed on stack.
107 * \luastack
108 * \lparam An array of key bindings objects, or nothing.
109 * \lreturn The array of key bindings objects of this client.
111 static int
112 luaA_root_keys(lua_State *L)
114 if(lua_gettop(L) == 1)
116 luaA_key_array_set(L, 1, &globalconf.keys);
118 int nscreen = xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
120 for(int phys_screen = 0; phys_screen < nscreen; phys_screen++)
122 xcb_screen_t *s = xutil_screen_get(globalconf.connection, phys_screen);
123 xcb_ungrab_key(globalconf.connection, XCB_GRAB_ANY, s->root, XCB_BUTTON_MASK_ANY);
124 window_grabkeys(s->root, &globalconf.keys);
128 return luaA_key_array_get(L, &globalconf.keys);
131 /** Get or set global mouse bindings.
132 * This binding will be available when you'll click on root window.
133 * \param L The Lua VM state.
134 * \return The number of element pushed on stack.
135 * \luastack
136 * \lparam An array of mouse button bindings objects, or nothing.
137 * \lreturn The array of mouse button bindings objects.
139 static int
140 luaA_root_buttons(lua_State *L)
142 if(lua_gettop(L) == 1)
143 luaA_button_array_set(L, 1, &globalconf.buttons);
145 return luaA_button_array_get(L, &globalconf.buttons);
148 /** Set the root cursor.
149 * \param L The Lua VM state.
150 * \return The number of element pushed on stack.
151 * \luastack
152 * \lparam A X cursor name.
154 static int
155 luaA_root_cursor(lua_State *L)
157 const char *cursor_name = luaL_checkstring(L, 1);
158 uint16_t cursor_font = xcursor_font_fromstr(cursor_name);
160 if(cursor_font)
162 uint32_t change_win_vals[] = { xcursor_new(globalconf.connection, cursor_font) };
164 for(int screen_nbr = 0;
165 screen_nbr < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
166 screen_nbr++)
167 xcb_change_window_attributes(globalconf.connection,
168 xutil_screen_get(globalconf.connection, screen_nbr)->root,
169 XCB_CW_CURSOR,
170 change_win_vals);
172 else
173 luaA_warn(L, "invalid cursor %s", cursor_name);
175 return 0;
178 const struct luaL_reg awesome_root_lib[] =
180 { "buttons", luaA_root_buttons },
181 { "keys", luaA_root_keys },
182 { "cursor", luaA_root_cursor },
183 { "fake_input", luaA_root_fake_input },
184 { NULL, NULL }
187 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80