Naughty: Remove some code duplication
[awesome.git] / button.c
blobe06b8895b50e9b49daf2864a55669c827f2b2643
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 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.
31 * \luastack
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.
38 static int
39 luaA_button_new(lua_State *L)
41 xcb_button_t xbutton;
42 button_t *button;
44 lua_settop(L, 5);
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... */
51 if(!lua_isnil(L, 4))
52 luaA_checkfunction(L, 4);
53 if(!lua_isnil(L, 5))
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);
63 return 1;
66 /** Button object.
67 * \param L The Lua VM state.
68 * \return The number of elements pushed on stack.
69 * \luastack
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
74 * button is pressed.
76 static int
77 luaA_button_index(lua_State *L)
79 if(luaA_usemetatable(L, 1, 2))
80 return 1;
82 size_t len;
83 button_t *button = luaL_checkudata(L, 1, "button");
84 const char *attr = luaL_checklstring(L, 2, &len);
86 switch(a_tokenize(attr, len))
88 case A_TK_PRESS:
89 return luaA_object_push_item(L, 1, button->press);
90 case A_TK_RELEASE:
91 return luaA_object_push_item(L, 1, button->release);
92 case A_TK_BUTTON:
93 lua_pushnumber(L, button->button);
94 break;
95 case A_TK_MODIFIERS:
96 luaA_pushmodifiers(L, button->mod);
97 break;
98 default:
99 return 0;
102 return 1;
105 /** Button object.
106 * \param L The Lua VM state.
107 * \return The number of elements pushed on stack.
108 * \luastack
110 static int
111 luaA_button_newindex(lua_State *L)
113 if(luaA_usemetatable(L, 1, 2))
114 return 1;
116 size_t len;
117 button_t *button = luaL_checkudata(L, 1, "button");
118 const char *attr = luaL_checklstring(L, 2, &len);
120 switch(a_tokenize(attr, len))
122 case A_TK_PRESS:
123 luaA_checkfunction(L, 3);
124 luaA_object_unref_item(L, 1, button->press);
125 button->press = luaA_object_ref_item(L, 1, 3);
126 break;
127 case A_TK_RELEASE:
128 luaA_checkfunction(L, 3);
129 luaA_object_unref_item(L, 1, button->release);
130 button->release = luaA_object_ref_item(L, 1, 3);
131 break;
132 case A_TK_BUTTON:
133 button->button = luaL_checknumber(L, 3);
134 break;
135 case A_TK_MODIFIERS:
136 button->mod = luaA_tomodifiers(L, 3);
137 break;
138 default:
139 break;
142 return 0;
145 const struct luaL_reg awesome_button_methods[] =
147 { "__call", luaA_button_new },
148 { NULL, NULL }
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 },
156 { NULL, NULL }
159 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80