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"
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
;
29 void joystick_create(uint64_t id
, const std::string
& xname
)
31 joynumbers
[id
] = joystick_count
++;
32 joysticks
[id
].name(xname
);
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
))
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
))
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
))
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
))
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
))
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
))
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
))
111 joysticks
[jid
].report_pov(id
, angle
);
114 void joystick_message(uint64_t jid
)
116 if(!joysticks
.count(jid
))
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
)
130 void joystick_flush()
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
));
137 if(joysticks
[i
.first
.first
].axis(i
.first
.second
, x
))
138 platform::queue(keypress(modifier_set(), *i
.second
, x
));
140 if(joysticks
[i
.first
.first
].hat(i
.first
.second
, x
))
141 platform::queue(keypress(modifier_set(), *i
.second
, x
));