1 #include "lua/internal.hpp"
2 #include "core/keymapper.hpp"
3 #include "core/command.hpp"
9 lua_inverse_bind(lua::state
& L
, const std::string
& name
, const std::string
& cmd
);
12 return ikey
.getname();
14 static int create(lua::state
& L
, lua::parameters
& P
);
16 keyboard::invbind ikey
;
19 class lua_command_binding
: public command::base
22 lua_command_binding(lua::state
& _L
, const std::string
& cmd
, int idx
)
23 : command::base(lsnes_cmd
, cmd
), L(_L
)
25 L
.pushlightuserdata(this);
27 L
.rawset(LUA_REGISTRYINDEX
);
29 void invoke(const std::string
& arguments
) throw(std::bad_alloc
, std::runtime_error
)
31 L
.pushlightuserdata(this);
32 L
.rawget(LUA_REGISTRYINDEX
);
33 L
.pushstring(arguments
.c_str());
34 int r
= L
.pcall(1, 0, 0);
37 err
= L
.get_string(-1, "Lua command callback");
38 else if(r
== LUA_ERRMEM
)
39 err
= "Out of memory";
40 else if(r
== LUA_ERRERR
)
43 err
= "Unknown error";
45 messages
<< "Error running lua command hook: " << err
<< std::endl
;
52 class lua_command_bind
55 lua_command_bind(lua::state
& L
, const std::string
& cmd
, int idx1
, int idx2
);
60 return a
->get_name() + "," + b
->get_name();
64 static int create(lua::state
& L
, lua::parameters
& P
);
66 lua_command_binding
* a
;
67 lua_command_binding
* b
;
70 lua_inverse_bind::lua_inverse_bind(lua::state
& L
, const std::string
& name
, const std::string
& cmd
)
71 : ikey(lsnes_mapper
, cmd
, "Lua‣" + name
)
75 lua_command_bind::lua_command_bind(lua::state
& L
, const std::string
& cmd
, int idx1
, int idx2
)
77 if(L
.type(idx2
) == LUA_TFUNCTION
) {
78 a
= new lua_command_binding(L
, "+" + cmd
, idx1
);
79 b
= new lua_command_binding(L
, "-" + cmd
, idx2
);
81 a
= new lua_command_binding(L
, cmd
, idx1
);
86 lua_command_bind::~lua_command_bind()
94 int list_bindings(lua::state
& L
, lua::parameters
& P
)
98 P(P
.optional(target
, ""));
101 for(auto key
: lsnes_mapper
.get_bindings()) {
102 std::string _key
= key
;
103 std::string cmd
= lsnes_mapper
.get(key
);
104 if(target
!= "" && cmd
!= target
)
106 L
.pushlstring(_key
.c_str(), _key
.length());
107 L
.pushlstring(cmd
.c_str(), cmd
.length());
110 for(auto key
: lsnes_mapper
.get_controller_keys()) {
111 for(unsigned i
= 0;; i
++) {
112 std::string _key
= key
->get_string(i
);
115 std::string cmd
= key
->get_command();
117 if(target
!= "" && cmd
!= target
)
119 L
.pushlstring(_key
.c_str(), _key
.length());
120 L
.pushlstring(cmd
.c_str(), cmd
.length());
127 int get_alias(lua::state
& L
, lua::parameters
& P
)
133 std::string a
= lsnes_cmd
.get_alias_for(name
);
141 int set_alias(lua::state
& L
, lua::parameters
& P
)
143 std::string name
, value
;
145 P(name
, P
.optional(value
, ""));
147 lsnes_cmd
.set_alias_for(name
, value
);
148 refresh_alias_binds();
152 lua::functions
alias_fns(lua_func_misc
, "", {
153 {"list_bindings", list_bindings
},
154 {"get_alias", get_alias
},
155 {"set_alias", set_alias
},
158 lua::_class
<lua_inverse_bind
> class_inverse_bind(lua_class_bind
, "INVERSEBIND", {
159 {"new", lua_inverse_bind::create
},
160 }, {}, &lua_inverse_bind::print
);
161 lua::_class
<lua_command_bind
> class_command_bind(lua_class_bind
, "COMMANDBIND", {
162 {"new", lua_command_bind::create
},
163 }, {}, &lua_command_bind::print
);
166 int lua_inverse_bind::create(lua::state
& L
, lua::parameters
& P
)
168 std::string name
, command
;
172 lua_inverse_bind
* b
= lua::_class
<lua_inverse_bind
>::create(L
, name
, command
);
176 int lua_command_bind::create(lua::state
& L
, lua::parameters
& P
)
179 int lfn1
= 0, lfn2
= 0;
181 P(name
, P
.function(lfn1
));
182 if(P
.is_function() || P
.is_novalue())
185 P
.expected("function or nil");
186 lua_command_bind
* b
= lua::_class
<lua_command_bind
>::create(L
.get_master(), name
, lfn1
, lfn2
);