awesomerc: remove marking
[awesome.git] / button.c
blob6d5cc1a59b4ca33c0d63712a413b60dcb23160f6
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 /** Create a new mouse button bindings.
27 * \param L The Lua VM state.
28 * \return The number of elements pushed on stack.
29 * \luastack
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.
36 static int
37 luaA_button_new(lua_State *L)
39 xcb_button_t xbutton;
40 button_t *button;
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);
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);
61 return 1;
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.
70 void
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);
81 lua_pushnil(L);
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));
85 else
86 lua_pop(L, 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.
95 int
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);
104 return 1;
107 /** Button object.
108 * \param L The Lua VM state.
109 * \return The number of elements pushed on stack.
110 * \luastack
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
115 * button is pressed.
117 static int
118 luaA_button_index(lua_State *L)
120 if(luaA_usemetatable(L, 1, 2))
121 return 1;
123 size_t len;
124 button_t *button = luaL_checkudata(L, 1, "button");
125 const char *attr = luaL_checklstring(L, 2, &len);
127 switch(a_tokenize(attr, len))
129 case A_TK_PRESS:
130 return luaA_object_push_item(L, 1, button->press);
131 case A_TK_RELEASE:
132 return luaA_object_push_item(L, 1, button->release);
133 case A_TK_BUTTON:
134 lua_pushnumber(L, button->button);
135 break;
136 case A_TK_MODIFIERS:
137 luaA_pushmodifiers(L, button->mod);
138 break;
139 default:
140 return 0;
143 return 1;
146 /** Button object.
147 * \param L The Lua VM state.
148 * \return The number of elements pushed on stack.
149 * \luastack
151 static int
152 luaA_button_newindex(lua_State *L)
154 if(luaA_usemetatable(L, 1, 2))
155 return 1;
157 size_t len;
158 button_t *button = luaL_checkudata(L, 1, "button");
159 const char *attr = luaL_checklstring(L, 2, &len);
161 switch(a_tokenize(attr, len))
163 case A_TK_PRESS:
164 luaA_checkfunction(L, 3);
165 luaA_object_unref_item(L, 1, button->press);
166 button->press = luaA_object_ref_item(L, 1, 3);
167 break;
168 case A_TK_RELEASE:
169 luaA_checkfunction(L, 3);
170 luaA_object_unref_item(L, 1, button->release);
171 button->release = luaA_object_ref_item(L, 1, 3);
172 break;
173 case A_TK_BUTTON:
174 button->button = luaL_checknumber(L, 3);
175 break;
176 case A_TK_MODIFIERS:
177 button->mod = luaA_tomodifiers(L, 3);
178 break;
179 default:
180 break;
183 return 0;
186 const struct luaL_reg awesome_button_methods[] =
188 LUA_CLASS_METHODS(button)
189 { "__call", luaA_button_new },
190 { NULL, NULL }
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 },
198 { NULL, NULL }
201 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80