2009-04-26 10:46:09 +02:00
|
|
|
/*
|
|
|
|
* button.c - button managing
|
|
|
|
*
|
|
|
|
* Copyright © 2007-2009 Julien Danjou <julien@danjou.info>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "button.h"
|
|
|
|
|
|
|
|
#include "common/tokenize.h"
|
|
|
|
|
|
|
|
/** Create a new mouse button bindings.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \return The number of elements pushed on stack.
|
|
|
|
* \luastack
|
|
|
|
* \lparam A table with modifiers keys, or a button to clone.
|
2009-04-26 12:34:30 +02:00
|
|
|
* \lparam A mouse button number, or 0 to match any button.
|
2009-04-26 10:46:09 +02:00
|
|
|
* \lparam A function to execute on click events.
|
|
|
|
* \lparam A function to execute on release events.
|
|
|
|
* \lreturn A mouse button binding.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
luaA_button_new(lua_State *L)
|
|
|
|
{
|
2009-04-26 12:34:13 +02:00
|
|
|
xcb_button_t xbutton;
|
2009-05-08 14:00:43 +02:00
|
|
|
button_t *button;
|
2009-07-28 10:29:30 +02:00
|
|
|
|
|
|
|
lua_settop(L, 5);
|
2009-04-26 10:46:09 +02:00
|
|
|
|
|
|
|
luaA_checktable(L, 2);
|
|
|
|
/* arg 3 is mouse button */
|
2009-04-26 12:34:13 +02:00
|
|
|
xbutton = luaL_checknumber(L, 3);
|
2009-04-26 10:46:09 +02:00
|
|
|
|
|
|
|
/* arg 4 and 5 are callback functions, check they are functions... */
|
|
|
|
if(!lua_isnil(L, 4))
|
|
|
|
luaA_checkfunction(L, 4);
|
2009-07-28 10:29:30 +02:00
|
|
|
if(!lua_isnil(L, 5))
|
2009-04-26 10:46:09 +02:00
|
|
|
luaA_checkfunction(L, 5);
|
|
|
|
|
|
|
|
button = button_new(L);
|
2009-07-28 10:29:30 +02:00
|
|
|
|
|
|
|
button->press = luaA_object_ref_item(L, -1, 4);
|
|
|
|
button->release = luaA_object_ref_item(L, -1, 4);
|
2009-04-26 12:34:13 +02:00
|
|
|
button->button = xbutton;
|
2009-06-17 17:05:47 +02:00
|
|
|
button->mod = luaA_tomodifiers(L, 2);
|
2009-04-26 10:46:09 +02:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-08-14 16:46:35 +02:00
|
|
|
/** Set a button array with a Lua table.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \param oidx The index of the object to store items into.
|
|
|
|
* \param idx The index of the Lua table.
|
|
|
|
* \param buttons The array button to fill.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
luaA_button_array_set(lua_State *L, int oidx, int idx, button_array_t *buttons)
|
|
|
|
{
|
|
|
|
luaA_checktable(L, idx);
|
|
|
|
|
|
|
|
foreach(button, *buttons)
|
|
|
|
luaA_object_unref_item(L, oidx, *button);
|
|
|
|
|
|
|
|
button_array_wipe(buttons);
|
|
|
|
button_array_init(buttons);
|
|
|
|
|
|
|
|
lua_pushnil(L);
|
|
|
|
while(lua_next(L, idx))
|
|
|
|
if(luaA_toudata(L, -1, "button"))
|
|
|
|
button_array_append(buttons, luaA_object_ref_item(L, oidx, -1));
|
|
|
|
else
|
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Push an array of button as an Lua table onto the stack.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \param oidx The index of the object to get items from.
|
|
|
|
* \param buttons The button array to push.
|
|
|
|
* \return The number of elements pushed on stack.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
luaA_button_array_get(lua_State *L, int oidx, button_array_t *buttons)
|
|
|
|
{
|
|
|
|
lua_createtable(L, buttons->len, 0);
|
|
|
|
for(int i = 0; i < buttons->len; i++)
|
|
|
|
{
|
|
|
|
luaA_object_push_item(L, oidx, buttons->tab[i]);
|
|
|
|
lua_rawseti(L, -2, i + 1);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-04-26 10:46:09 +02:00
|
|
|
/** Button object.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \return The number of elements pushed on stack.
|
|
|
|
* \luastack
|
|
|
|
* \lfield press The function called when button press event is received.
|
|
|
|
* \lfield release The function called when button release event is received.
|
2009-04-26 19:43:23 +02:00
|
|
|
* \lfield button The mouse button number, or 0 for any button.
|
2009-04-26 19:44:25 +02:00
|
|
|
* \lfield modifiers The modifier key table that should be pressed while the
|
|
|
|
* button is pressed.
|
2009-04-26 10:46:09 +02:00
|
|
|
*/
|
|
|
|
static int
|
|
|
|
luaA_button_index(lua_State *L)
|
|
|
|
{
|
|
|
|
if(luaA_usemetatable(L, 1, 2))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
size_t len;
|
|
|
|
button_t *button = luaL_checkudata(L, 1, "button");
|
|
|
|
const char *attr = luaL_checklstring(L, 2, &len);
|
|
|
|
|
|
|
|
switch(a_tokenize(attr, len))
|
|
|
|
{
|
|
|
|
case A_TK_PRESS:
|
2009-07-28 10:29:30 +02:00
|
|
|
return luaA_object_push_item(L, 1, button->press);
|
2009-04-26 10:46:09 +02:00
|
|
|
case A_TK_RELEASE:
|
2009-07-28 10:29:30 +02:00
|
|
|
return luaA_object_push_item(L, 1, button->release);
|
2009-04-26 10:46:09 +02:00
|
|
|
case A_TK_BUTTON:
|
|
|
|
lua_pushnumber(L, button->button);
|
|
|
|
break;
|
2009-04-26 19:44:25 +02:00
|
|
|
case A_TK_MODIFIERS:
|
|
|
|
luaA_pushmodifiers(L, button->mod);
|
|
|
|
break;
|
2009-04-26 10:46:09 +02:00
|
|
|
default:
|
2009-04-26 19:43:54 +02:00
|
|
|
return 0;
|
2009-04-26 10:46:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Button object.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \return The number of elements pushed on stack.
|
|
|
|
* \luastack
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
luaA_button_newindex(lua_State *L)
|
|
|
|
{
|
|
|
|
if(luaA_usemetatable(L, 1, 2))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
size_t len;
|
|
|
|
button_t *button = luaL_checkudata(L, 1, "button");
|
|
|
|
const char *attr = luaL_checklstring(L, 2, &len);
|
|
|
|
|
|
|
|
switch(a_tokenize(attr, len))
|
|
|
|
{
|
|
|
|
case A_TK_PRESS:
|
2009-07-28 10:29:30 +02:00
|
|
|
luaA_checkfunction(L, 3);
|
|
|
|
luaA_object_unref_item(L, 1, button->press);
|
|
|
|
button->press = luaA_object_ref_item(L, 1, 3);
|
2009-04-26 10:46:09 +02:00
|
|
|
break;
|
|
|
|
case A_TK_RELEASE:
|
2009-07-28 10:29:30 +02:00
|
|
|
luaA_checkfunction(L, 3);
|
|
|
|
luaA_object_unref_item(L, 1, button->release);
|
|
|
|
button->release = luaA_object_ref_item(L, 1, 3);
|
2009-04-26 10:46:09 +02:00
|
|
|
break;
|
|
|
|
case A_TK_BUTTON:
|
2009-04-26 12:34:13 +02:00
|
|
|
button->button = luaL_checknumber(L, 3);
|
2009-04-26 10:46:09 +02:00
|
|
|
break;
|
2009-04-27 14:32:39 +02:00
|
|
|
case A_TK_MODIFIERS:
|
2009-06-17 17:05:47 +02:00
|
|
|
button->mod = luaA_tomodifiers(L, 3);
|
|
|
|
break;
|
2009-04-26 10:46:09 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct luaL_reg awesome_button_methods[] =
|
|
|
|
{
|
2009-06-09 12:51:25 +02:00
|
|
|
LUA_CLASS_METHODS(button)
|
2009-04-26 10:46:09 +02:00
|
|
|
{ "__call", luaA_button_new },
|
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
|
|
|
const struct luaL_reg awesome_button_meta[] =
|
|
|
|
{
|
2009-07-29 15:32:55 +02:00
|
|
|
LUA_OBJECT_META(button)
|
2009-04-26 10:46:09 +02:00
|
|
|
{ "__index", luaA_button_index },
|
|
|
|
{ "__newindex", luaA_button_newindex },
|
2009-07-28 10:29:30 +02:00
|
|
|
{ "__gc", luaA_object_gc },
|
2009-04-26 10:46:09 +02:00
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80
|