change codename
[awesome.git] / button.c
blobf6fbf018a224fff4701b202682012e1593bf84ab
1 /*
2 * button.c - button managing
4 * Copyright © 2007-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 "button.h"
24 #include "common/tokenize.h"
26 DO_LUA_TOSTRING(button_t, button, "button")
27 LUA_OBJECT_FUNCS(button_t, button, "button")
29 void
30 button_unref_simplified(button_t **b)
32 button_unref(globalconf.L, *b);
35 /** Collect a button.
36 * \param L The Lua VM state.
37 * \return 0.
39 static int
40 luaA_button_gc(lua_State *L)
42 button_t *button = luaL_checkudata(L, 1, "button");
43 luaA_ref_array_wipe(&button->refs);
44 luaL_unref(globalconf.L, LUA_REGISTRYINDEX, button->press);
45 luaL_unref(globalconf.L, LUA_REGISTRYINDEX, button->release);
46 return 0;
49 /** Create a new mouse button bindings.
50 * \param L The Lua VM state.
51 * \return The number of elements pushed on stack.
52 * \luastack
53 * \lparam A table with modifiers keys, or a button to clone.
54 * \lparam A mouse button number, or 0 to match any button.
55 * \lparam A function to execute on click events.
56 * \lparam A function to execute on release events.
57 * \lreturn A mouse button binding.
59 static int
60 luaA_button_new(lua_State *L)
62 xcb_button_t xbutton;
63 button_t *button;
64 luaA_ref press = LUA_REFNIL, release = LUA_REFNIL;
66 luaA_checktable(L, 2);
67 /* arg 3 is mouse button */
68 xbutton = luaL_checknumber(L, 3);
70 /* arg 4 and 5 are callback functions, check they are functions... */
71 if(!lua_isnil(L, 4))
72 luaA_checkfunction(L, 4);
73 if(lua_gettop(L) == 5 && !lua_isnil(L, 5))
74 luaA_checkfunction(L, 5);
76 /* ... then register (can't register before since 5 maybe not nil but not a
77 * function */
78 if(!lua_isnil(L, 4))
79 luaA_registerfct(L, 4, &press);
80 if(lua_gettop(L) == 5 && !lua_isnil(L, 5))
81 luaA_registerfct(L, 5, &release);
83 button = button_new(L);
84 button->press = press;
85 button->release = release;
86 button->button = xbutton;
88 luaA_setmodifiers(L, 2, &button->mod);
90 return 1;
93 /** Set a button array with a Lua table.
94 * \param L The Lua VM state.
95 * \param idx The index of the Lua table.
96 * \param buttons The array button to fill.
98 void
99 luaA_button_array_set(lua_State *L, int idx, button_array_t *buttons)
101 luaA_checktable(L, idx);
102 button_array_wipe(buttons);
103 button_array_init(buttons);
104 lua_pushnil(L);
105 while(lua_next(L, idx))
106 button_array_append(buttons, button_ref(L));
109 /** Push an array of button as an Lua table onto the stack.
110 * \param L The Lua VM state.
111 * \param buttons The button array to push.
112 * \return The number of elements pushed on stack.
115 luaA_button_array_get(lua_State *L, button_array_t *buttons)
117 lua_createtable(L, buttons->len, 0);
118 for(int i = 0; i < buttons->len; i++)
120 button_push(L, buttons->tab[i]);
121 lua_rawseti(L, -2, i + 1);
123 return 1;
126 /** Button object.
127 * \param L The Lua VM state.
128 * \return The number of elements pushed on stack.
129 * \luastack
130 * \lfield press The function called when button press event is received.
131 * \lfield release The function called when button release event is received.
132 * \lfield button The mouse button number, or 0 for any button.
133 * \lfield modifiers The modifier key table that should be pressed while the
134 * button is pressed.
136 static int
137 luaA_button_index(lua_State *L)
139 if(luaA_usemetatable(L, 1, 2))
140 return 1;
142 size_t len;
143 button_t *button = luaL_checkudata(L, 1, "button");
144 const char *attr = luaL_checklstring(L, 2, &len);
146 switch(a_tokenize(attr, len))
148 case A_TK_PRESS:
149 if(button->press != LUA_REFNIL)
150 lua_rawgeti(L, LUA_REGISTRYINDEX, button->press);
151 else
152 lua_pushnil(L);
153 break;
154 case A_TK_RELEASE:
155 if(button->release != LUA_REFNIL)
156 lua_rawgeti(L, LUA_REGISTRYINDEX, button->release);
157 else
158 lua_pushnil(L);
159 break;
160 case A_TK_BUTTON:
161 lua_pushnumber(L, button->button);
162 break;
163 case A_TK_MODIFIERS:
164 luaA_pushmodifiers(L, button->mod);
165 break;
166 default:
167 return 0;
170 return 1;
173 /** Button object.
174 * \param L The Lua VM state.
175 * \return The number of elements pushed on stack.
176 * \luastack
178 static int
179 luaA_button_newindex(lua_State *L)
181 if(luaA_usemetatable(L, 1, 2))
182 return 1;
184 size_t len;
185 button_t *button = luaL_checkudata(L, 1, "button");
186 const char *attr = luaL_checklstring(L, 2, &len);
188 switch(a_tokenize(attr, len))
190 case A_TK_PRESS:
191 luaA_registerfct(L, 3, &button->press);
192 break;
193 case A_TK_RELEASE:
194 luaA_registerfct(L, 3, &button->release);
195 break;
196 case A_TK_BUTTON:
197 button->button = luaL_checknumber(L, 3);
198 break;
199 case A_TK_MODIFIERS:
200 luaA_setmodifiers(L, 3, &button->mod);
201 default:
202 break;
205 return 0;
208 const struct luaL_reg awesome_button_methods[] =
210 { "__call", luaA_button_new },
211 { NULL, NULL }
213 const struct luaL_reg awesome_button_meta[] =
215 { "__index", luaA_button_index },
216 { "__newindex", luaA_button_newindex },
217 { "__gc", luaA_button_gc },
218 { "__tostring", luaA_button_tostring },
219 { NULL, NULL }
222 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80