Lua: input.keyhook

This commit is contained in:
Ilari Liusvaara 2012-01-17 21:35:49 +02:00
parent af655f14f3
commit dfe264f629
8 changed files with 116 additions and 35 deletions

View file

@ -323,6 +323,10 @@ public:
* Get all key parameters.
*/
static std::map<std::string, struct parameters> get_all_parameters();
/**
* Set callback requests on/off
*/
void request_hook_callback(bool state);
private:
unsigned state;
enum type ktype;
@ -335,6 +339,7 @@ private:
double compensate2(double value);
void run_listeners(const modifier_set& modifiers, unsigned subkey, bool polarity, bool really, double x);
std::string keyname;
bool requests_hook;
};
/**

View file

@ -11,10 +11,10 @@ extern "C"
std::string get_string_argument(lua_State* LS, unsigned argindex, const char* fname);
bool get_boolean_argument(lua_State* LS, unsigned argindex, const char* fname);
void push_keygroup_parameters(lua_State* LS, const struct keygroup::parameters& p);
extern lua_render_context* lua_render_ctx;
extern controller_frame* lua_input_controllerdata;
template<typename T>
T get_numeric_argument(lua_State* LS, unsigned argindex, const char* fname)
{

View file

@ -1,8 +1,9 @@
#ifndef _lua__hpp__included__
#define _lua__hpp__included__
#include "render.hpp"
#include "controllerframe.hpp"
#include "core/controllerframe.hpp"
#include "core/keymapper.hpp"
#include "core/render.hpp"
struct lua_State;
@ -82,6 +83,7 @@ void lua_callback_err_save(const std::string& name) throw();
void lua_callback_post_save(const std::string& name, bool is_state) throw();
void lua_callback_snoop_input(uint32_t port, uint32_t controller, uint32_t index, short value) throw();
void lua_callback_quit() throw();
void lua_callback_keyhook(const std::string& key, const struct keygroup::parameters& p) throw();
extern bool lua_supported;
extern bool lua_requests_repaint;

View file

@ -2471,6 +2471,15 @@ cal_tolerance: Dead zone tolerance.
Only meaningful with axis and pressure types.
\end_layout
\begin_layout Subsubsection
input.keyhook(key, state)
\end_layout
\begin_layout Standard
Requests that keyhook events to be sent for key (state=true) or not sent
(state=false).
\end_layout
\begin_layout Subsection
Table hostmemory
\end_layout
@ -3064,6 +3073,16 @@ Called each time bsnes asks for input.
Note: There is no way to modify the value to be sent.
\end_layout
\begin_layout Subsubsection
Callback: on_keyhook(string keyname, table state)
\end_layout
\begin_layout Standard
Sent when key that has keyhook events requested changes state.
Keyname is name of the key (group) and state is the state (same kind as
table values in input.raw).
\end_layout
\begin_layout Section
Modifier and key names:
\end_layout

View file

@ -1214,6 +1214,11 @@ table has the following fields:
• cal_tolerance: Dead zone tolerance. Only meaningful with axis
and pressure types.
8.4.5 input.keyhook(key, state)
Requests that keyhook events to be sent for key (state=true) or
not sent (state=false).
8.5 Table hostmemory
Host memory handling (extra memory saved to savestates). Host
@ -1542,6 +1547,12 @@ autofire have been taken into account). Might be useful when
translating movies to format suitable for console verification.
Note: There is no way to modify the value to be sent.
8.10.16 Callback: on_keyhook(string keyname, table state)
Sent when key that has keyhook events requested changes state.
Keyname is name of the key (group) and state is the state (same
kind as table values in input.raw).
9 Modifier and key names:
9.1 SDL Platform

View file

@ -6,6 +6,7 @@
#include "core/memorymanip.hpp"
#include "core/misc.hpp"
#include "core/window.hpp"
#include "core/lua.hpp"
#include <stdexcept>
#include <stdexcept>
@ -307,6 +308,7 @@ keygroup::keygroup(const std::string& name, enum type t) throw(std::bad_alloc)
cal_center = 0;
cal_right = 32767;
cal_tolerance = 0.5;
requests_hook = false;
}
keygroup::~keygroup() throw()
@ -318,8 +320,16 @@ void keygroup::change_type(enum type t) throw()
{
ktype = t;
state = 0;
if(requests_hook)
lua_callback_keyhook(keyname, get_parameters());
}
void keygroup::request_hook_callback(bool state)
{
requests_hook = state;
}
std::pair<keygroup*, unsigned> keygroup::lookup(const std::string& name) throw(std::bad_alloc,
std::runtime_error)
{
@ -353,6 +363,8 @@ void keygroup::change_calibration(short left, short center, short right, double
cal_center = center;
cal_right = right;
cal_tolerance = tolerance;
if(requests_hook)
lua_callback_keyhook(keyname, get_parameters());
}
double keygroup::compensate(short value)
@ -402,6 +414,8 @@ double keygroup::compensate2(double value)
void keygroup::set_position(short pos, const modifier_set& modifiers) throw()
{
last_rawval = pos;
if(requests_hook)
lua_callback_keyhook(keyname, get_parameters());
double x = compensate2(compensate(pos));
unsigned tmp;
bool left, right, up, down;

View file

@ -19,6 +19,7 @@ void lua_callback_err_save(const std::string& name) throw() {}
void lua_callback_post_save(const std::string& name, bool is_state) throw() {}
void lua_callback_snoop_input(uint32_t port, uint32_t controller, uint32_t index, short value) throw() {}
void lua_callback_quit() throw() {}
void lua_callback_keyhook(const std::string& key, const struct keygroup::parameters& p) throw() {}
void init_lua() throw() {}
void quit_lua() throw() {}
bool lua_requests_repaint = false;
@ -150,6 +151,42 @@ bool get_boolean_argument(lua_State* LS, unsigned argindex, const char* fname)
return (lua_toboolean(LS, argindex) != 0);
}
void push_keygroup_parameters(lua_State* LS, const struct keygroup::parameters& p)
{
lua_newtable(LS);
lua_pushstring(LS, "last_rawval");
lua_pushnumber(LS, p.last_rawval);
lua_settable(LS, -3);
lua_pushstring(LS, "cal_left");
lua_pushnumber(LS, p.cal_left);
lua_settable(LS, -3);
lua_pushstring(LS, "cal_center");
lua_pushnumber(LS, p.cal_center);
lua_settable(LS, -3);
lua_pushstring(LS, "cal_right");
lua_pushnumber(LS, p.cal_right);
lua_settable(LS, -3);
lua_pushstring(LS, "cal_tolerance");
lua_pushnumber(LS, p.cal_tolerance);
lua_settable(LS, -3);
lua_pushstring(LS, "ktype");
switch(p.ktype) {
case keygroup::KT_DISABLED: lua_pushstring(LS, "disabled"); break;
case keygroup::KT_KEY: lua_pushstring(LS, "key"); break;
case keygroup::KT_AXIS_PAIR: lua_pushstring(LS, "axis"); break;
case keygroup::KT_AXIS_PAIR_INVERSE: lua_pushstring(LS, "axis-inverse"); break;
case keygroup::KT_HAT: lua_pushstring(LS, "hat"); break;
case keygroup::KT_MOUSE: lua_pushstring(LS, "mouse"); break;
case keygroup::KT_PRESSURE_PM: lua_pushstring(LS, "pressure-pm"); break;
case keygroup::KT_PRESSURE_P0: lua_pushstring(LS, "pressure-p0"); break;
case keygroup::KT_PRESSURE_0M: lua_pushstring(LS, "pressure-0m"); break;
case keygroup::KT_PRESSURE_0P: lua_pushstring(LS, "pressure-0p"); break;
case keygroup::KT_PRESSURE_M0: lua_pushstring(LS, "pressure-m0"); break;
case keygroup::KT_PRESSURE_MP: lua_pushstring(LS, "pressure-mp"); break;
};
lua_settable(LS, -3);
}
lua_render_context* lua_render_ctx = NULL;
controller_frame* lua_input_controllerdata = NULL;
@ -463,6 +500,15 @@ void lua_callback_quit() throw()
run_lua_cb(0);
}
void lua_callback_keyhook(const std::string& key, const struct keygroup::parameters& p) throw()
{
if(!callback_exists("on_keyhook"))
return;
lua_pushstring(L, key.c_str());
push_keygroup_parameters(L, p);
run_lua_cb(2);
}
void init_lua() throw()
{
L = lua_newstate(alloc, NULL);

View file

@ -45,40 +45,24 @@ namespace
lua_newtable(LS);
for(auto i : s) {
lua_pushstring(LS, i.first.c_str());
lua_newtable(LS);
lua_pushstring(LS, "last_rawval");
lua_pushnumber(LS, i.second.last_rawval);
lua_settable(LS, -3);
lua_pushstring(LS, "cal_left");
lua_pushnumber(LS, i.second.cal_left);
lua_settable(LS, -3);
lua_pushstring(LS, "cal_center");
lua_pushnumber(LS, i.second.cal_center);
lua_settable(LS, -3);
lua_pushstring(LS, "cal_right");
lua_pushnumber(LS, i.second.cal_right);
lua_settable(LS, -3);
lua_pushstring(LS, "cal_tolerance");
lua_pushnumber(LS, i.second.cal_tolerance);
lua_settable(LS, -3);
lua_pushstring(LS, "ktype");
switch(i.second.ktype) {
case keygroup::KT_DISABLED: lua_pushstring(LS, "disabled"); break;
case keygroup::KT_KEY: lua_pushstring(LS, "key"); break;
case keygroup::KT_AXIS_PAIR: lua_pushstring(LS, "axis"); break;
case keygroup::KT_AXIS_PAIR_INVERSE: lua_pushstring(LS, "axis-inverse"); break;
case keygroup::KT_HAT: lua_pushstring(LS, "hat"); break;
case keygroup::KT_MOUSE: lua_pushstring(LS, "mouse"); break;
case keygroup::KT_PRESSURE_PM: lua_pushstring(LS, "pressure-pm"); break;
case keygroup::KT_PRESSURE_P0: lua_pushstring(LS, "pressure-p0"); break;
case keygroup::KT_PRESSURE_0M: lua_pushstring(LS, "pressure-0m"); break;
case keygroup::KT_PRESSURE_0P: lua_pushstring(LS, "pressure-0p"); break;
case keygroup::KT_PRESSURE_M0: lua_pushstring(LS, "pressure-m0"); break;
case keygroup::KT_PRESSURE_MP: lua_pushstring(LS, "pressure-mp"); break;
};
lua_settable(LS, -3);
push_keygroup_parameters(LS, i.second);
lua_settable(LS, -3);
}
return 1;
});
function_ptr_luafun ireq("input.keyhook", [](lua_State* LS, const std::string& fname) -> int {
struct keygroup* k;
bool state;
std::string x = get_string_argument(LS, 1, fname.c_str());
state = get_boolean_argument(LS, 2, fname.c_str());
k = keygroup::lookup_by_name(x);
if(!k) {
lua_pushstring(LS, "Invalid key name");
lua_error(LS);
return 0;
}
k->request_hook_callback(state);
return 0;
});
}