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.
24 #include "common/tokenize.h"
26 DO_LUA_TOSTRING(button_t
, button
, "button")
27 LUA_OBJECT_FUNCS(button_t
, button
, "button")
30 button_unref_simplified(button_t
**b
)
32 button_unref(globalconf
.L
, *b
);
36 * \param L The Lua VM state.
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
);
49 /** Create a new mouse button bindings.
50 * \param L The Lua VM state.
51 * \return The number of elements pushed on stack.
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.
60 luaA_button_new(lua_State
*L
)
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... */
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
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
);
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.
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
);
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);
127 * \param L The Lua VM state.
128 * \return The number of elements pushed on stack.
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
137 luaA_button_index(lua_State
*L
)
139 if(luaA_usemetatable(L
, 1, 2))
143 button_t
*button
= luaL_checkudata(L
, 1, "button");
144 const char *attr
= luaL_checklstring(L
, 2, &len
);
146 switch(a_tokenize(attr
, len
))
149 if(button
->press
!= LUA_REFNIL
)
150 lua_rawgeti(L
, LUA_REGISTRYINDEX
, button
->press
);
155 if(button
->release
!= LUA_REFNIL
)
156 lua_rawgeti(L
, LUA_REGISTRYINDEX
, button
->release
);
161 lua_pushnumber(L
, button
->button
);
164 luaA_pushmodifiers(L
, button
->mod
);
174 * \param L The Lua VM state.
175 * \return The number of elements pushed on stack.
179 luaA_button_newindex(lua_State
*L
)
181 if(luaA_usemetatable(L
, 1, 2))
185 button_t
*button
= luaL_checkudata(L
, 1, "button");
186 const char *attr
= luaL_checklstring(L
, 2, &len
);
188 switch(a_tokenize(attr
, len
))
191 luaA_registerfct(L
, 3, &button
->press
);
194 luaA_registerfct(L
, 3, &button
->release
);
197 button
->button
= luaL_checknumber(L
, 3);
200 luaA_setmodifiers(L
, 3, &button
->mod
);
208 const struct luaL_reg awesome_button_methods
[] =
210 { "__call", luaA_button_new
},
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
},
222 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80