titlebar: fix modkey for buttons
[awesome.git] / button.c
blobd96f3c0aa4381e0e7649e0c171a64fa5b633e4c8
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 /** Button object.
65 * \param L The Lua VM state.
66 * \return The number of elements pushed on stack.
67 * \luastack
68 * \lfield press The function called when button press event is received.
69 * \lfield release The function called when button release event is received.
70 * \lfield button The mouse button number, or 0 for any button.
71 * \lfield modifiers The modifier key table that should be pressed while the
72 * button is pressed.
74 static int
75 luaA_button_index(lua_State *L)
77 if(luaA_usemetatable(L, 1, 2))
78 return 1;
80 size_t len;
81 button_t *button = luaL_checkudata(L, 1, "button");
82 const char *attr = luaL_checklstring(L, 2, &len);
84 switch(a_tokenize(attr, len))
86 case A_TK_PRESS:
87 return luaA_object_push_item(L, 1, button->press);
88 case A_TK_RELEASE:
89 return luaA_object_push_item(L, 1, button->release);
90 case A_TK_BUTTON:
91 lua_pushnumber(L, button->button);
92 break;
93 case A_TK_MODIFIERS:
94 luaA_pushmodifiers(L, button->mod);
95 break;
96 default:
97 return 0;
100 return 1;
103 /** Button object.
104 * \param L The Lua VM state.
105 * \return The number of elements pushed on stack.
106 * \luastack
108 static int
109 luaA_button_newindex(lua_State *L)
111 if(luaA_usemetatable(L, 1, 2))
112 return 1;
114 size_t len;
115 button_t *button = luaL_checkudata(L, 1, "button");
116 const char *attr = luaL_checklstring(L, 2, &len);
118 switch(a_tokenize(attr, len))
120 case A_TK_PRESS:
121 luaA_checkfunction(L, 3);
122 luaA_object_unref_item(L, 1, button->press);
123 button->press = luaA_object_ref_item(L, 1, 3);
124 break;
125 case A_TK_RELEASE:
126 luaA_checkfunction(L, 3);
127 luaA_object_unref_item(L, 1, button->release);
128 button->release = luaA_object_ref_item(L, 1, 3);
129 break;
130 case A_TK_BUTTON:
131 button->button = luaL_checknumber(L, 3);
132 break;
133 case A_TK_MODIFIERS:
134 button->mod = luaA_tomodifiers(L, 3);
135 break;
136 default:
137 break;
140 return 0;
143 const struct luaL_reg awesome_button_methods[] =
145 LUA_CLASS_METHODS(button)
146 { "__call", luaA_button_new },
147 { NULL, NULL }
149 const struct luaL_reg awesome_button_meta[] =
151 LUA_OBJECT_META(button)
152 { "__index", luaA_button_index },
153 { "__newindex", luaA_button_newindex },
154 { "__gc", luaA_object_gc },
155 { NULL, NULL }
158 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80