Merge branch 'rr1-maint'
[lsnes.git] / src / lua / input.cpp
blobe39251fa837a5d3c276bd62ed84ceb83e5de093a
1 #include "core/keymapper.hpp"
2 #include "lua/internal.hpp"
3 #include "core/movie.hpp"
4 #include "core/moviedata.hpp"
6 namespace
8 function_ptr_luafun iset("input.set", [](lua_State* LS, const std::string& fname) -> int {
9 if(!lua_input_controllerdata)
10 return 0;
11 unsigned controller = get_numeric_argument<unsigned>(LS, 1, fname.c_str());
12 unsigned index = get_numeric_argument<unsigned>(LS, 2, fname.c_str());
13 short value = get_numeric_argument<short>(LS, 3, fname.c_str());
14 if(controller >= MAX_PORTS * MAX_CONTROLLERS_PER_PORT || index > MAX_CONTROLS_PER_CONTROLLER)
15 return 0;
16 lua_input_controllerdata->axis(controller, index, value);
17 return 0;
18 });
20 function_ptr_luafun iget("input.get", [](lua_State* LS, const std::string& fname) -> int {
21 if(!lua_input_controllerdata)
22 return 0;
23 unsigned controller = get_numeric_argument<unsigned>(LS, 1, fname.c_str());
24 unsigned index = get_numeric_argument<unsigned>(LS, 2, fname.c_str());
25 if(controller >= MAX_PORTS * MAX_CONTROLLERS_PER_PORT || index > MAX_CONTROLS_PER_CONTROLLER)
26 return 0;
27 lua_pushnumber(LS, lua_input_controllerdata->axis(controller, index));
28 return 1;
29 });
31 function_ptr_luafun iseta("input.seta", [](lua_State* LS, const std::string& fname) -> int {
32 if(!lua_input_controllerdata)
33 return 0;
34 short val;
35 unsigned controller = get_numeric_argument<unsigned>(LS, 1, fname.c_str());
36 if(controller >= MAX_PORTS * MAX_CONTROLLERS_PER_PORT)
37 return 0;
38 uint64_t base = get_numeric_argument<uint64_t>(LS, 2, fname.c_str());
39 for(unsigned i = 0; i < MAX_CONTROLS_PER_CONTROLLER; i++) {
40 val = (base >> i) & 1;
41 get_numeric_argument<short>(LS, i + 3, val, fname.c_str());
42 lua_input_controllerdata->axis(controller, i, val);
44 return 0;
45 });
47 function_ptr_luafun igeta("input.geta", [](lua_State* LS, const std::string& fname) -> int {
48 if(!lua_input_controllerdata)
49 return 0;
50 unsigned controller = get_numeric_argument<unsigned>(LS, 1, fname.c_str());
51 if(controller >= MAX_PORTS * MAX_CONTROLLERS_PER_PORT)
52 return 0;
53 uint64_t fret = 0;
54 for(unsigned i = 0; i < MAX_CONTROLS_PER_CONTROLLER; i++)
55 if(lua_input_controllerdata->axis(controller, i))
56 fret |= (1ULL << i);
57 lua_pushnumber(LS, fret);
58 for(unsigned i = 0; i < MAX_CONTROLS_PER_CONTROLLER; i++)
59 lua_pushnumber(LS, lua_input_controllerdata->axis(controller, i));
60 return MAX_CONTROLS_PER_CONTROLLER + 1;
61 });
63 function_ptr_luafun igett("input.controllertype", [](lua_State* LS, const std::string& fname) -> int {
64 unsigned controller = get_numeric_argument<unsigned>(LS, 1, fname.c_str());
65 auto& m = get_movie();
66 controller_frame f = m.read_subframe(m.get_current_frame(), 0);
67 porttype_t p = f.get_port_type(controller / MAX_CONTROLLERS_PER_PORT);
68 const porttype_info& i = porttype_info::lookup(p);
69 if(i.controllers <= controller % MAX_CONTROLLERS_PER_PORT)
70 lua_pushnil(LS);
71 else if(p == PT_NONE)
72 lua_pushnil(LS);
73 else if(p == PT_GAMEPAD)
74 lua_pushstring(LS, "gamepad");
75 else if(p == PT_MULTITAP)
76 lua_pushstring(LS, "gamepad");
77 else if(p == PT_MOUSE)
78 lua_pushstring(LS, "mouse");
79 else if(p == PT_SUPERSCOPE)
80 lua_pushstring(LS, "superscope");
81 else if(p == PT_JUSTIFIER)
82 lua_pushstring(LS, "justifier");
83 else if(p == PT_JUSTIFIERS)
84 lua_pushstring(LS, "justifier");
85 else
86 lua_pushstring(LS, "unknown");
87 return 1;
88 });
90 function_ptr_luafun ireset("input.reset", [](lua_State* LS, const std::string& fname) -> int {
91 if(!lua_input_controllerdata)
92 return 0;
93 long cycles = 0;
94 get_numeric_argument(LS, 1, cycles, fname.c_str());
95 if(cycles < 0)
96 return 0;
97 short lo = cycles % 10000;
98 short hi = cycles / 10000;
99 lua_input_controllerdata->reset(true);
100 lua_input_controllerdata->delay(std::make_pair(hi, lo));
101 return 0;
104 function_ptr_luafun iraw("input.raw", [](lua_State* LS, const std::string& fname) -> int {
105 auto s = keygroup::get_all_parameters();
106 lua_newtable(LS);
107 for(auto i : s) {
108 lua_pushstring(LS, i.first.c_str());
109 push_keygroup_parameters(LS, i.second);
110 lua_settable(LS, -3);
112 return 1;
115 function_ptr_luafun ireq("input.keyhook", [](lua_State* LS, const std::string& fname) -> int {
116 struct keygroup* k;
117 bool state;
118 std::string x = get_string_argument(LS, 1, fname.c_str());
119 state = get_boolean_argument(LS, 2, fname.c_str());
120 k = keygroup::lookup_by_name(x);
121 if(!k) {
122 lua_pushstring(LS, "Invalid key name");
123 lua_error(LS);
124 return 0;
126 k->request_hook_callback(state);
127 return 0;