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")
28 /** Create a new mouse button bindings.
29 * \param L The Lua VM state.
30 * \return The number of elements pushed on stack.
32 * \lparam A table with modifiers keys, or a button to clone.
33 * \lparam A mouse button number, or 0 to match any button.
34 * \lparam A function to execute on click events.
35 * \lparam A function to execute on release events.
36 * \lreturn A mouse button binding.
39 luaA_button_new(lua_State
*L
)
46 luaA_checktable(L
, 2);
47 /* arg 3 is mouse button */
48 xbutton
= luaL_checknumber(L
, 3);
50 /* arg 4 and 5 are callback functions, check they are functions... */
52 luaA_checkfunction(L
, 4);
54 luaA_checkfunction(L
, 5);
56 button
= button_new(L
);
58 button
->press
= luaA_object_ref_item(L
, -1, 4);
59 button
->release
= luaA_object_ref_item(L
, -1, 4);
60 button
->button
= xbutton
;
61 button
->mod
= luaA_tomodifiers(L
, 2);
67 * \param L The Lua VM state.
68 * \return The number of elements pushed on stack.
70 * \lfield press The function called when button press event is received.
71 * \lfield release The function called when button release event is received.
72 * \lfield button The mouse button number, or 0 for any button.
73 * \lfield modifiers The modifier key table that should be pressed while the
77 luaA_button_index(lua_State
*L
)
79 if(luaA_usemetatable(L
, 1, 2))
83 button_t
*button
= luaL_checkudata(L
, 1, "button");
84 const char *attr
= luaL_checklstring(L
, 2, &len
);
86 switch(a_tokenize(attr
, len
))
89 return luaA_object_push_item(L
, 1, button
->press
);
91 return luaA_object_push_item(L
, 1, button
->release
);
93 lua_pushnumber(L
, button
->button
);
96 luaA_pushmodifiers(L
, button
->mod
);
106 * \param L The Lua VM state.
107 * \return The number of elements pushed on stack.
111 luaA_button_newindex(lua_State
*L
)
113 if(luaA_usemetatable(L
, 1, 2))
117 button_t
*button
= luaL_checkudata(L
, 1, "button");
118 const char *attr
= luaL_checklstring(L
, 2, &len
);
120 switch(a_tokenize(attr
, len
))
123 luaA_checkfunction(L
, 3);
124 luaA_object_unref_item(L
, 1, button
->press
);
125 button
->press
= luaA_object_ref_item(L
, 1, 3);
128 luaA_checkfunction(L
, 3);
129 luaA_object_unref_item(L
, 1, button
->release
);
130 button
->release
= luaA_object_ref_item(L
, 1, 3);
133 button
->button
= luaL_checknumber(L
, 3);
136 button
->mod
= luaA_tomodifiers(L
, 3);
145 const struct luaL_reg awesome_button_methods
[] =
147 { "__call", luaA_button_new
},
150 const struct luaL_reg awesome_button_meta
[] =
152 { "__index", luaA_button_index
},
153 { "__newindex", luaA_button_newindex
},
154 { "__gc", luaA_object_gc
},
155 { "__tostring", luaA_button_tostring
},
159 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80