1 #include "core/command.hpp"
2 #include "core/dispatch.hpp"
3 #include "library/globalwrap.hpp"
4 #include "core/keymapper.hpp"
5 #include "core/memorymanip.hpp"
6 #include "core/misc.hpp"
7 #include "core/window.hpp"
9 #include "library/string.hpp"
10 #include "library/zip.hpp"
21 keyboard_mapper
lsnes_mapper(lsnes_kbd
, lsnes_cmd
);
23 hw_gamepad_set lsnes_gamepads
;
27 std::map
<std::pair
<unsigned, unsigned>, keyboard_key
*> buttons
;
28 std::map
<std::pair
<unsigned, unsigned>, keyboard_key
*> axes
;
29 std::map
<std::pair
<unsigned, unsigned>, keyboard_key
*> hats
;
32 void lsnes_gamepads_init()
34 lsnes_gamepads
.set_button_cb([](unsigned jnum
, unsigned num
, bool val
) {
35 if(!buttons
.count(std::make_pair(jnum
, num
)))
37 platform::queue(keypress(keyboard_modifier_set(), *buttons
[std::make_pair(jnum
, num
)], val
));
39 lsnes_gamepads
.set_hat_cb([](unsigned jnum
, unsigned num
, unsigned val
) {
40 if(!hats
.count(std::make_pair(jnum
, num
)))
42 platform::queue(keypress(keyboard_modifier_set(), *hats
[std::make_pair(jnum
, num
)], val
));
44 lsnes_gamepads
.set_axis_cb([](unsigned jnum
, unsigned num
, int16_t val
) {
45 if(!axes
.count(std::make_pair(jnum
, num
)))
47 platform::queue(keypress(keyboard_modifier_set(), *axes
[std::make_pair(jnum
, num
)], val
));
49 lsnes_gamepads
.set_axismode_cb([](unsigned jnum
, unsigned num
, int mode
, double tolerance
) {
50 if(!axes
.count(std::make_pair(jnum
, num
)))
52 axes
[std::make_pair(jnum
, num
)]->cast_axis()->set_mode(mode
, tolerance
);
54 lsnes_gamepads
.set_newitem_cb([](unsigned jnum
, unsigned num
, int type
) {
56 std::string name
= (stringfmt() << "joystick" << jnum
<< "axis" << num
).str();
57 int mode
= lsnes_gamepads
[jnum
].get_mode(num
);
58 axes
[std::make_pair(jnum
, num
)] = new keyboard_key_axis(lsnes_kbd
, name
, "joystick", mode
);
60 } else if(type
== 1) {
61 std::string name
= (stringfmt() << "joystick" << jnum
<< "button" << num
).str();
62 buttons
[std::make_pair(jnum
, num
)] = new keyboard_key_key(lsnes_kbd
, name
, "joystick");
64 } else if(type
== 2) {
65 std::string name
= (stringfmt() << "joystick" << jnum
<< "hat" << num
).str();
66 hats
[std::make_pair(jnum
, num
)] = new keyboard_key_hat(lsnes_kbd
, name
, "joystick");
71 auto cfg
= read_file_relative(get_config_path() + "/gamepads.json", "");
72 std::string
_cfg(cfg
.begin(), cfg
.end());
73 JSON::node
config(_cfg
);
74 lsnes_gamepads
.load(config
);
79 void lsnes_gamepads_deinit()
81 std::ofstream
cfg(get_config_path() + "/gamepads.json");
83 cfg
<< lsnes_gamepads
.save().serialize() << std::endl
;
86 void cleanup_keymapper()
88 for(auto i
: buttons
) delete i
.second
;
89 for(auto i
: axes
) delete i
.second
;
90 for(auto i
: hats
) delete i
.second
;
95 function_ptr_command
<> show_joysticks(lsnes_cmd
, "show-joysticks", "Show joystick info",
96 "Syntax: show-joysticks\nShow joystick info.\n",
97 []() throw(std::bad_alloc
, std::runtime_error
) {
98 messages
<< "--------------------------------------------" << std::endl
;
99 messages
<< lsnes_gamepads
.get_summary() << std::endl
;
100 messages
<< "--------------------------------------------" << std::endl
;