1 #include "cmdhelp/keymapper.hpp"
2 #include "core/command.hpp"
3 #include "core/instance.hpp"
4 #include "core/joystickapi.hpp"
5 #include "core/keymapper.hpp"
6 #include "core/memorymanip.hpp"
7 #include "core/messages.hpp"
8 #include "core/queue.hpp"
9 #include "library/globalwrap.hpp"
10 #include "library/string.hpp"
11 #include "library/zip.hpp"
12 #include "lua/lua.hpp"
22 keyboard::invbind_set lsnes_invbinds
;
23 gamepad::set lsnes_gamepads
;
27 globalwrap
<std::map
<std::pair
<unsigned, unsigned>, keyboard::key
*>> buttons
;
28 globalwrap
<std::map
<std::pair
<unsigned, unsigned>, keyboard::key
*>> axes
;
29 globalwrap
<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 lsnes_instance
.iqueue
->queue(keypress_info(keyboard::modifier_set(), *buttons()[std::make_pair(jnum
,
40 lsnes_gamepads
.set_hat_cb([](unsigned jnum
, unsigned num
, unsigned val
) {
41 if(!hats().count(std::make_pair(jnum
, num
)))
43 lsnes_instance
.iqueue
->queue(keypress_info(keyboard::modifier_set(), *hats()[std::make_pair(
46 lsnes_gamepads
.set_axis_cb([](unsigned jnum
, unsigned num
, int16_t val
) {
47 if(!axes().count(std::make_pair(jnum
, num
)))
49 lsnes_instance
.iqueue
->queue(keypress_info(keyboard::modifier_set(), *axes()[std::make_pair(jnum
,
52 lsnes_gamepads
.set_axismode_cb([](unsigned jnum
, unsigned num
, int mode
, double tolerance
) {
53 if(!axes().count(std::make_pair(jnum
, num
)))
55 axes()[std::make_pair(jnum
, num
)]->cast_axis()->set_mode(mode
, tolerance
);
57 lsnes_gamepads
.set_newitem_cb([](unsigned jnum
, unsigned num
, int type
) {
59 std::string name
= (stringfmt() << "joystick" << jnum
<< "axis" << num
).str();
60 int mode
= lsnes_gamepads
[jnum
].get_mode(num
);
61 axes()[std::make_pair(jnum
, num
)] = new keyboard::key_axis(*lsnes_instance
.keyboard
, name
,
64 } else if(type
== 1) {
65 std::string name
= (stringfmt() << "joystick" << jnum
<< "button" << num
).str();
66 buttons()[std::make_pair(jnum
, num
)] = new keyboard::key_key(*lsnes_instance
.keyboard
, name
,
69 } else if(type
== 2) {
70 std::string name
= (stringfmt() << "joystick" << jnum
<< "hat" << num
).str();
71 hats()[std::make_pair(jnum
, num
)] = new keyboard::key_hat(*lsnes_instance
.keyboard
, name
,
77 auto cfg
= zip::readrel(get_config_path() + "/gamepads.json", "");
78 std::string
_cfg(cfg
.begin(), cfg
.end());
79 JSON::node
config(_cfg
);
80 lsnes_gamepads
.load(config
);
85 void lsnes_gamepads_deinit()
87 std::ofstream
cfg(get_config_path() + "/gamepads.json");
88 JSON::printer_indenting printer
;
90 cfg
<< lsnes_gamepads
.save().serialize(&printer
);
93 void cleanup_keymapper()
95 for(auto i
: buttons()) delete i
.second
;
96 for(auto i
: axes()) delete i
.second
;
97 for(auto i
: hats()) delete i
.second
;
102 command::fnptr
<> CMD_show_joysticks(lsnes_cmds
, CKEYMAPPER::show
,
103 []() throw(std::bad_alloc
, std::runtime_error
) {
104 messages
<< "--------------------------------------------" << std::endl
;
105 messages
<< lsnes_gamepads
.get_summary() << std::endl
;
106 messages
<< "--------------------------------------------" << std::endl
;
109 command::fnptr
<> reset_joysticks(lsnes_cmds
, CKEYMAPPER::reset
,
110 []() throw(std::bad_alloc
, std::runtime_error
) {
111 joystick_driver_quit();
112 lsnes_gamepads
.offline_all(); //Not supposed to have online gamepads.
113 joystick_driver_init();
114 messages
<< "Reset gamepads" << std::endl
;