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/directory.hpp"
10 #include "library/globalwrap.hpp"
11 #include "library/string.hpp"
12 #include "library/zip.hpp"
13 #include "lua/lua.hpp"
23 keyboard::invbind_set lsnes_invbinds
;
24 gamepad::set lsnes_gamepads
;
28 globalwrap
<std::map
<std::pair
<unsigned, unsigned>, keyboard::key
*>> buttons
;
29 globalwrap
<std::map
<std::pair
<unsigned, unsigned>, keyboard::key
*>> axes
;
30 globalwrap
<std::map
<std::pair
<unsigned, unsigned>, keyboard::key
*>> hats
;
33 void lsnes_gamepads_init()
35 lsnes_gamepads
.set_button_cb([](unsigned jnum
, unsigned num
, bool val
) {
36 if(!buttons().count(std::make_pair(jnum
, num
)))
38 lsnes_instance
.iqueue
->queue(keypress_info(keyboard::modifier_set(), *buttons()[std::make_pair(jnum
,
41 lsnes_gamepads
.set_hat_cb([](unsigned jnum
, unsigned num
, unsigned val
) {
42 if(!hats().count(std::make_pair(jnum
, num
)))
44 lsnes_instance
.iqueue
->queue(keypress_info(keyboard::modifier_set(), *hats()[std::make_pair(
47 lsnes_gamepads
.set_axis_cb([](unsigned jnum
, unsigned num
, int16_t val
) {
48 if(!axes().count(std::make_pair(jnum
, num
)))
50 lsnes_instance
.iqueue
->queue(keypress_info(keyboard::modifier_set(), *axes()[std::make_pair(jnum
,
53 lsnes_gamepads
.set_axismode_cb([](unsigned jnum
, unsigned num
, int mode
, double tolerance
) {
54 if(!axes().count(std::make_pair(jnum
, num
)))
56 axes()[std::make_pair(jnum
, num
)]->cast_axis()->set_mode(mode
, tolerance
);
58 lsnes_gamepads
.set_newitem_cb([](unsigned jnum
, unsigned num
, int type
) {
60 std::string name
= (stringfmt() << "joystick" << jnum
<< "axis" << num
).str();
61 int mode
= lsnes_gamepads
[jnum
].get_mode(num
);
62 axes()[std::make_pair(jnum
, num
)] = new keyboard::key_axis(*lsnes_instance
.keyboard
, name
,
65 } else if(type
== 1) {
66 std::string name
= (stringfmt() << "joystick" << jnum
<< "button" << num
).str();
67 buttons()[std::make_pair(jnum
, num
)] = new keyboard::key_key(*lsnes_instance
.keyboard
, name
,
70 } else if(type
== 2) {
71 std::string name
= (stringfmt() << "joystick" << jnum
<< "hat" << num
).str();
72 hats()[std::make_pair(jnum
, num
)] = new keyboard::key_hat(*lsnes_instance
.keyboard
, name
,
78 auto cfg
= zip::readrel(get_config_path() + "/gamepads.json", "");
79 std::string
_cfg(cfg
.begin(), cfg
.end());
80 JSON::node
config(_cfg
);
81 lsnes_gamepads
.load(config
);
86 void lsnes_gamepads_deinit()
88 std::string tmpname
= get_config_path() + "/gamepads.json.tmp";
89 std::string finalname
= get_config_path() + "/gamepads.json";
90 std::ofstream
cfg(get_config_path() + "/gamepads.json.tmp");
91 JSON::printer_indenting printer
;
93 cfg
<< lsnes_gamepads
.save().serialize(&printer
);
97 directory::rename_overwrite(tmpname
.c_str(), finalname
.c_str());
101 void cleanup_keymapper()
103 for(auto i
: buttons()) delete i
.second
;
104 for(auto i
: axes()) delete i
.second
;
105 for(auto i
: hats()) delete i
.second
;
110 command::fnptr
<> CMD_show_joysticks(lsnes_cmds
, CKEYMAPPER::show
,
111 []() throw(std::bad_alloc
, std::runtime_error
) {
112 messages
<< "--------------------------------------------" << std::endl
;
113 messages
<< lsnes_gamepads
.get_summary() << std::endl
;
114 messages
<< "--------------------------------------------" << std::endl
;
117 command::fnptr
<> reset_joysticks(lsnes_cmds
, CKEYMAPPER::reset
,
118 []() throw(std::bad_alloc
, std::runtime_error
) {
119 joystick_driver_quit(true);
120 lsnes_gamepads
.offline_all(); //Not supposed to have online gamepads when entering reset.
121 joystick_driver_init(true);
122 messages
<< "Reset gamepads" << std::endl
;