1 #include "lua/internal.hpp"
2 #include "core/instance.hpp"
3 #include "core/keymapper.hpp"
4 #include "core/messages.hpp"
5 #include "core/command.hpp"
11 lua_inverse_bind(lua::state
& L
, const std::string
& name
, const std::string
& cmd
);
12 static size_t overcommit(const std::string
& name
, const std::string
& cmd
) { return 0; }
15 return ikey
.getname();
17 static int create(lua::state
& L
, lua::parameters
& P
);
19 keyboard::invbind ikey
;
22 class lua_command_binding
: public command::base
25 lua_command_binding(lua::state
& _L
, const std::string
& cmd
, int idx
)
26 : command::base(*CORE().command
, cmd
, false), L(_L
)
28 L
.pushlightuserdata(this);
30 L
.rawset(LUA_REGISTRYINDEX
);
32 void invoke(const std::string
& arguments
) throw(std::bad_alloc
, std::runtime_error
)
34 L
.pushlightuserdata(this);
35 L
.rawget(LUA_REGISTRYINDEX
);
36 L
.pushstring(arguments
.c_str());
37 int r
= L
.pcall(1, 0, 0);
40 err
= L
.get_string(-1, "Lua command callback");
41 else if(r
== LUA_ERRMEM
)
42 err
= "Out of memory";
43 else if(r
== LUA_ERRERR
)
46 err
= "Unknown error";
48 messages
<< "Error running lua command hook: " << err
<< std::endl
;
55 class lua_command_bind
58 lua_command_bind(lua::state
& L
, const std::string
& cmd
, int idx1
, int idx2
);
59 static size_t overcommit(const std::string
& cmd
, int idx1
, int idx2
) { return 0; }
64 return a
->get_name() + "," + b
->get_name();
68 static int create(lua::state
& L
, lua::parameters
& P
);
70 lua_command_binding
* a
;
71 lua_command_binding
* b
;
74 lua_inverse_bind::lua_inverse_bind(lua::state
& L
, const std::string
& name
, const std::string
& cmd
)
75 : ikey(*CORE().mapper
, cmd
, "Lua‣" + name
)
79 lua_command_bind::lua_command_bind(lua::state
& L
, const std::string
& cmd
, int idx1
, int idx2
)
81 if(L
.type(idx2
) == LUA_TFUNCTION
) {
82 a
= new lua_command_binding(L
, "+" + cmd
, idx1
);
83 b
= new lua_command_binding(L
, "-" + cmd
, idx2
);
85 a
= new lua_command_binding(L
, cmd
, idx1
);
90 lua_command_bind::~lua_command_bind()
98 int list_bindings(lua::state
& L
, lua::parameters
& P
)
103 P(P
.optional(target
, ""));
106 for(auto key
: core
.mapper
->get_bindings()) {
107 std::string _key
= key
;
108 std::string cmd
= core
.mapper
->get(key
);
109 if(target
!= "" && cmd
!= target
)
111 L
.pushlstring(_key
.c_str(), _key
.length());
112 L
.pushlstring(cmd
.c_str(), cmd
.length());
115 for(auto key
: core
.mapper
->get_controller_keys()) {
116 for(unsigned i
= 0;; i
++) {
117 std::string _key
= key
->get_string(i
);
120 std::string cmd
= key
->get_command();
122 if(target
!= "" && cmd
!= target
)
124 L
.pushlstring(_key
.c_str(), _key
.length());
125 L
.pushlstring(cmd
.c_str(), cmd
.length());
132 int get_alias(lua::state
& L
, lua::parameters
& P
)
138 std::string a
= CORE().command
->get_alias_for(name
);
146 int set_alias(lua::state
& L
, lua::parameters
& P
)
149 std::string name
, value
;
151 P(name
, P
.optional(value
, ""));
153 core
.command
->set_alias_for(name
, value
);
154 (*core
.abindmanager
)();
158 lua::functions
LUA_alias_fns(lua_func_misc
, "", {
159 {"list_bindings", list_bindings
},
160 {"get_alias", get_alias
},
161 {"set_alias", set_alias
},
164 lua::_class
<lua_inverse_bind
> LUA_class_inverse_bind(lua_class_bind
, "INVERSEBIND", {
165 {"new", lua_inverse_bind::create
},
166 }, {}, &lua_inverse_bind::print
);
167 lua::_class
<lua_command_bind
> LUA_class_command_bind(lua_class_bind
, "COMMANDBIND", {
168 {"new", lua_command_bind::create
},
169 }, {}, &lua_command_bind::print
);
172 int lua_inverse_bind::create(lua::state
& L
, lua::parameters
& P
)
174 std::string name
, command
;
178 lua::_class
<lua_inverse_bind
>::create(L
, name
, command
);
182 int lua_command_bind::create(lua::state
& L
, lua::parameters
& P
)
185 int lfn1
= 0, lfn2
= 0;
187 P(name
, P
.function(lfn1
));
188 if(P
.is_function() || P
.is_novalue())
191 P
.expected("function or nil");
192 lua::_class
<lua_command_bind
>::create(L
.get_master(), name
, lfn1
, lfn2
);