Create function pointer to lua function adapter and migrate what we can
[lsnes.git] / controller.cpp
blob5c412b5fa7a0e36d7f5dfadd6d0b854cbb665eee
1 #include "controller.hpp"
2 #include "lsnes.hpp"
3 #include <snes/snes.hpp>
4 #include <ui-libsnes/libsnes.hpp>
6 namespace
8 porttype_t porttypes[2];
9 int analog_indices[3] = {-1, -1, -1};
10 bool analog_is_mouse[3];
12 void update_analog_indices() throw()
14 int i = 0;
15 for(unsigned j = 0; j < sizeof(analog_indices) / sizeof(analog_indices[0]); j++)
16 analog_indices[j] = -1;
17 for(unsigned j = 0; j < 8; j++) {
18 devicetype_t d = controller_type_by_logical(j);
19 switch(d) {
20 case DT_NONE:
21 case DT_GAMEPAD:
22 break;
23 case DT_MOUSE:
24 analog_is_mouse[i] = true;
25 analog_indices[i++] = j;
26 break;
27 case DT_SUPERSCOPE:
28 case DT_JUSTIFIER:
29 analog_is_mouse[i] = false;
30 analog_indices[i++] = j;
31 break;
37 int controller_index_by_logical(unsigned lid) throw()
39 bool p1multitap = (porttypes[0] == PT_MULTITAP);
40 unsigned p1devs = port_types[porttypes[0]].devices;
41 unsigned p2devs = port_types[porttypes[1]].devices;
42 if(lid >= p1devs + p2devs)
43 return -1;
44 if(!p1multitap)
45 if(lid < p1devs)
46 return lid;
47 else
48 return 4 + lid - p1devs;
49 else
50 if(lid == 0)
51 return 0;
52 else if(lid < 5)
53 return lid + 3;
54 else
55 return lid - 4;
58 int controller_index_by_analog(unsigned aid) throw()
60 if(aid > 2)
61 return -1;
62 return analog_indices[aid];
65 bool controller_ismouse_by_analog(unsigned aid) throw()
67 if(aid > 2)
68 return false;
69 return analog_is_mouse[aid];
72 devicetype_t controller_type_by_logical(unsigned lid) throw()
74 int x = controller_index_by_logical(lid);
75 if(x < 0)
76 return DT_NONE;
77 enum porttype_t rawtype = porttypes[x >> 2];
78 if((x & 3) < port_types[rawtype].devices)
79 return port_types[rawtype].dtype;
80 else
81 return DT_NONE;
84 void controller_set_port_type(unsigned port, porttype_t ptype, bool set_core) throw()
86 if(set_core && ptype != PT_INVALID)
87 snes_set_controller_port_device(port != 0, port_types[ptype].bsnes_type);
88 porttypes[port] = ptype;
89 update_analog_indices();