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 /** Create a new mouse button bindings.
27 * \param L The Lua VM state.
28 * \return The number of elements pushed on stack.
30 * \lparam A table with modifiers keys, or a button to clone.
31 * \lparam A mouse button number, or 0 to match any button.
32 * \lparam A function to execute on click events.
33 * \lparam A function to execute on release events.
34 * \lreturn A mouse button binding.
37 luaA_button_new(lua_State
*L
)
44 luaA_checktable(L
, 2);
45 /* arg 3 is mouse button */
46 xbutton
= luaL_checknumber(L
, 3);
48 /* arg 4 and 5 are callback functions, check they are functions... */
50 luaA_checkfunction(L
, 4);
52 luaA_checkfunction(L
, 5);
54 button
= button_new(L
);
56 button
->press
= luaA_object_ref_item(L
, -1, 4);
57 button
->release
= luaA_object_ref_item(L
, -1, 4);
58 button
->button
= xbutton
;
59 button
->mod
= luaA_tomodifiers(L
, 2);
64 /** Set a button array with a Lua table.
65 * \param L The Lua VM state.
66 * \param oidx The index of the object to store items into.
67 * \param idx The index of the Lua table.
68 * \param buttons The array button to fill.
71 luaA_button_array_set(lua_State
*L
, int oidx
, int idx
, button_array_t
*buttons
)
73 luaA_checktable(L
, idx
);
75 foreach(button
, *buttons
)
76 luaA_object_unref_item(L
, oidx
, *button
);
78 button_array_wipe(buttons
);
79 button_array_init(buttons
);
82 while(lua_next(L
, idx
))
83 if(luaA_toudata(L
, -1, "button"))
84 button_array_append(buttons
, luaA_object_ref_item(L
, oidx
, -1));
89 /** Push an array of button as an Lua table onto the stack.
90 * \param L The Lua VM state.
91 * \param oidx The index of the object to get items from.
92 * \param buttons The button array to push.
93 * \return The number of elements pushed on stack.
96 luaA_button_array_get(lua_State
*L
, int oidx
, button_array_t
*buttons
)
98 lua_createtable(L
, buttons
->len
, 0);
99 for(int i
= 0; i
< buttons
->len
; i
++)
101 luaA_object_push_item(L
, oidx
, buttons
->tab
[i
]);
102 lua_rawseti(L
, -2, i
+ 1);
108 * \param L The Lua VM state.
109 * \return The number of elements pushed on stack.
111 * \lfield press The function called when button press event is received.
112 * \lfield release The function called when button release event is received.
113 * \lfield button The mouse button number, or 0 for any button.
114 * \lfield modifiers The modifier key table that should be pressed while the
118 luaA_button_index(lua_State
*L
)
120 if(luaA_usemetatable(L
, 1, 2))
124 button_t
*button
= luaL_checkudata(L
, 1, "button");
125 const char *attr
= luaL_checklstring(L
, 2, &len
);
127 switch(a_tokenize(attr
, len
))
130 return luaA_object_push_item(L
, 1, button
->press
);
132 return luaA_object_push_item(L
, 1, button
->release
);
134 lua_pushnumber(L
, button
->button
);
137 luaA_pushmodifiers(L
, button
->mod
);
147 * \param L The Lua VM state.
148 * \return The number of elements pushed on stack.
152 luaA_button_newindex(lua_State
*L
)
154 if(luaA_usemetatable(L
, 1, 2))
158 button_t
*button
= luaL_checkudata(L
, 1, "button");
159 const char *attr
= luaL_checklstring(L
, 2, &len
);
161 switch(a_tokenize(attr
, len
))
164 luaA_checkfunction(L
, 3);
165 luaA_object_unref_item(L
, 1, button
->press
);
166 button
->press
= luaA_object_ref_item(L
, 1, 3);
169 luaA_checkfunction(L
, 3);
170 luaA_object_unref_item(L
, 1, button
->release
);
171 button
->release
= luaA_object_ref_item(L
, 1, 3);
174 button
->button
= luaL_checknumber(L
, 3);
177 button
->mod
= luaA_tomodifiers(L
, 3);
186 const struct luaL_reg awesome_button_methods
[] =
188 LUA_CLASS_METHODS(button
)
189 { "__call", luaA_button_new
},
192 const struct luaL_reg awesome_button_meta
[] =
194 LUA_OBJECT_META(button
)
195 { "__index", luaA_button_index
},
196 { "__newindex", luaA_button_newindex
},
197 { "__gc", luaA_object_gc
},
201 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80