Refactor command handling to library/
[lsnes.git] / src / core / joystick.cpp
blobc92726d9cefad3ef6c39c23ed387d1ee2065fd6c
1 #include "core/command.hpp"
2 #include "core/joystick.hpp"
3 #include "core/window.hpp"
4 #include "library/string.hpp"
5 #include "library/joyfun.hpp"
6 #include <map>
7 #include <string>
9 namespace
11 std::map<uint64_t, joystick_model> joysticks;
12 std::map<uint64_t, unsigned> joynumbers;
13 std::map<std::pair<uint64_t, unsigned>, keygroup*> axes;
14 std::map<std::pair<uint64_t, unsigned>, keygroup*> buttons;
15 std::map<std::pair<uint64_t, unsigned>, keygroup*> hats;
16 unsigned joystick_count = 0;
18 function_ptr_command<> show_joysticks(lsnes_cmd, "show-joysticks", "Show joysticks",
19 "Syntax: show-joysticks\nShow joystick data.\n",
20 []() throw(std::bad_alloc, std::runtime_error) {
21 messages << "Driver: " << joystick_plugin::name << std::endl;
22 messages << "--------------------------------------" << std::endl;
23 for(auto i : joynumbers)
24 messages << joysticks[i.first].compose_report(i.second) << std::endl;
25 messages << "--------------------------------------" << std::endl;
26 });
29 void joystick_create(uint64_t id, const std::string& xname)
31 joynumbers[id] = joystick_count++;
32 joysticks[id].name(xname);
35 void joystick_quit()
37 for(auto i : axes)
38 delete i.second;
39 for(auto i : buttons)
40 delete i.second;
41 for(auto i : hats)
42 delete i.second;
43 joysticks.clear();
44 joynumbers.clear();
45 axes.clear();
46 buttons.clear();
47 hats.clear();
48 joystick_count = 0;
51 void joystick_new_axis(uint64_t jid, uint64_t id, int64_t minv, int64_t maxv, const std::string& xname,
52 enum keygroup::type atype)
54 if(!joysticks.count(jid))
55 return;
56 unsigned jnum = joynumbers[jid];
57 unsigned n = joysticks[jid].new_axis(id, minv, maxv, xname);
58 std::string name = (stringfmt() << "joystick" << jnum << "axis" << n).str();
59 axes[std::make_pair(jid, n)] = new keygroup(name, "joystick", atype);
62 void joystick_new_button(uint64_t jid, uint64_t id, const std::string& xname)
64 if(!joysticks.count(jid))
65 return;
66 unsigned jnum = joynumbers[jid];
67 unsigned n = joysticks[jid].new_button(id, xname);
68 std::string name = (stringfmt() << "joystick" << jnum << "button" << n).str();
69 buttons[std::make_pair(jid, n)] = new keygroup(name, "joystick", keygroup::KT_KEY);
72 void joystick_new_hat(uint64_t jid, uint64_t id_x, uint64_t id_y, int64_t min_dev, const std::string& xname_x,
73 const std::string& xname_y)
75 if(!joysticks.count(jid))
76 return;
77 unsigned jnum = joynumbers[jid];
78 unsigned n = joysticks[jid].new_hat(id_x, id_y, min_dev, xname_x, xname_y);
79 std::string name = (stringfmt() << "joystick" << jnum << "hat" << n).str();
80 hats[std::make_pair(jid, n)] = new keygroup(name, "joystick", keygroup::KT_HAT);
83 void joystick_new_hat(uint64_t jid, uint64_t id, const std::string& xname)
85 if(!joysticks.count(jid))
86 return;
87 unsigned jnum = joynumbers[jid];
88 unsigned n = joysticks[jid].new_hat(id, xname);
89 std::string name = (stringfmt() << "joystick" << jnum << "hat" << n).str();
90 hats[std::make_pair(jid, n)] = new keygroup(name, "joystick", keygroup::KT_HAT);
93 void joystick_report_axis(uint64_t jid, uint64_t id, int64_t value)
95 if(!joysticks.count(jid))
96 return;
97 joysticks[jid].report_axis(id, value);
100 void joystick_report_button(uint64_t jid, uint64_t id, bool value)
102 if(!joysticks.count(jid))
103 return;
104 joysticks[jid].report_button(id, value);
107 void joystick_report_pov(uint64_t jid, uint64_t id, int angle)
109 if(!joysticks.count(jid))
110 return;
111 joysticks[jid].report_pov(id, angle);
114 void joystick_message(uint64_t jid)
116 if(!joysticks.count(jid))
117 return;
118 messages << "Found '" << joysticks[jid].name() << "' (" << joysticks[jid].buttons() << " buttons, "
119 << joysticks[jid].axes() << " axes, " << joysticks[jid].hats() << " hats)" << std::endl;
122 std::set<uint64_t> joystick_set()
124 std::set<uint64_t> x;
125 for(auto i : joynumbers)
126 x.insert(i.first);
127 return x;
130 void joystick_flush()
132 short x;
133 for(auto i : buttons)
134 if(joysticks[i.first.first].button(i.first.second, x))
135 platform::queue(keypress(modifier_set(), *i.second, x));
136 for(auto i : axes)
137 if(joysticks[i.first.first].axis(i.first.second, x))
138 platform::queue(keypress(modifier_set(), *i.second, x));
139 for(auto i : hats)
140 if(joysticks[i.first.first].hat(i.first.second, x))
141 platform::queue(keypress(modifier_set(), *i.second, x));