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"
|
|
|
|
|
|
|
|
DO_LUA_TOSTRING(button_t, button, "button")
|
|
|
|
|
|
|
|
/** 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** 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[] =
|
|
|
|
{
|
|
|
|
{ "__call", luaA_button_new },
|
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
|
|
|
const struct luaL_reg awesome_button_meta[] =
|
|
|
|
{
|
|
|
|
{ "__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
|
|
|
{ "__tostring", luaA_button_tostring },
|
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80
|