change codename
[awesome.git] / button.c
blobac3d38cd258091676bc62ff12170c61672efe79f
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"
23 #include "luaa.h"
24 #include "key.h"
25 #include "common/luaobject.h"
27 /** Create a new mouse button bindings.
28 * \param L The Lua VM state.
29 * \return The number of elements pushed on stack.
31 static int
32 luaA_button_new(lua_State *L)
34 xcb_button_t xbutton;
35 button_t *button;
37 /* compat code */
38 if(lua_istable(L, 2) && lua_isnumber(L, 3))
40 luaA_deprecate(L, "new syntax");
42 lua_settop(L, 5);
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... */
49 if(!lua_isnil(L, 4))
50 luaA_checkfunction(L, 4);
51 if(!lua_isnil(L, 5))
52 luaA_checkfunction(L, 5);
54 button = button_new(L);
55 button->button = xbutton;
56 button->modifiers = luaA_tomodifiers(L, 2);
58 if(!lua_isnil(L, 4))
60 lua_pushvalue(L, 4);
61 luaA_object_add_signal(L, -2, "press", -1);
64 if(!lua_isnil(L, 5))
66 lua_pushvalue(L, 5);
67 luaA_object_add_signal(L, -2, "release", -1);
70 return 1;
73 return luaA_class_new(L, &button_class);
76 /** Set a button array with a Lua table.
77 * \param L The Lua VM state.
78 * \param oidx The index of the object to store items into.
79 * \param idx The index of the Lua table.
80 * \param buttons The array button to fill.
82 void
83 luaA_button_array_set(lua_State *L, int oidx, int idx, button_array_t *buttons)
85 luaA_checktable(L, idx);
87 foreach(button, *buttons)
88 luaA_object_unref_item(L, oidx, *button);
90 button_array_wipe(buttons);
91 button_array_init(buttons);
93 lua_pushnil(L);
94 while(lua_next(L, idx))
95 if(luaA_toudata(L, -1, &button_class))
96 button_array_append(buttons, luaA_object_ref_item(L, oidx, -1));
97 else
98 lua_pop(L, 1);
101 /** Push an array of button as an Lua table onto the stack.
102 * \param L The Lua VM state.
103 * \param oidx The index of the object to get items from.
104 * \param buttons The button array to push.
105 * \return The number of elements pushed on stack.
108 luaA_button_array_get(lua_State *L, int oidx, button_array_t *buttons)
110 lua_createtable(L, buttons->len, 0);
111 for(int i = 0; i < buttons->len; i++)
113 luaA_object_push_item(L, oidx, buttons->tab[i]);
114 lua_rawseti(L, -2, i + 1);
116 return 1;
119 LUA_OBJECT_EXPORT_PROPERTY(button, button_t, button, lua_pushnumber);
120 LUA_OBJECT_EXPORT_PROPERTY(button, button_t, modifiers, luaA_pushmodifiers);
122 static int
123 luaA_button_set_modifiers(lua_State *L, button_t *b)
125 b->modifiers = luaA_tomodifiers(L, -1);
126 luaA_object_emit_signal(L, -3, "property::modifiers", 0);
127 return 0;
130 static int
131 luaA_button_set_button(lua_State *L, button_t *b)
133 b->button = luaL_checknumber(L, -1);
134 luaA_object_emit_signal(L, -3, "property::button", 0);
135 return 0;
138 void
139 button_class_setup(lua_State *L)
141 static const struct luaL_reg button_methods[] =
143 LUA_CLASS_METHODS(button)
144 { "__call", luaA_button_new },
145 { NULL, NULL }
148 static const struct luaL_reg button_meta[] =
150 LUA_OBJECT_META(button)
151 LUA_CLASS_META
152 { "__gc", luaA_object_gc },
153 { NULL, NULL }
156 luaA_class_setup(L, &button_class, "button", (lua_class_allocator_t) button_new,
157 luaA_class_index_miss_property, luaA_class_newindex_miss_property,
158 button_methods, button_meta);
159 luaA_class_add_property(&button_class, A_TK_BUTTON,
160 (lua_class_propfunc_t) luaA_button_set_button,
161 (lua_class_propfunc_t) luaA_button_get_button,
162 (lua_class_propfunc_t) luaA_button_set_button);
163 luaA_class_add_property(&button_class, A_TK_MODIFIERS,
164 (lua_class_propfunc_t) luaA_button_set_modifiers,
165 (lua_class_propfunc_t) luaA_button_get_modifiers,
166 (lua_class_propfunc_t) luaA_button_set_modifiers);
169 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80