12 : commandController(commandController_)
19 SDL_JoystickEventState(SDL_ENABLE);
24 for (
auto& info : infos) {
25 if (info.joystick) SDL_JoystickClose(info.joystick);
29size_t JoystickManager::getFreeSlot()
31 if (
auto it =
ranges::find(infos,
nullptr, &Info::joystick); it != infos.end()) {
32 return std::distance(infos.begin(), it);
34 auto idx = infos.size();
36 infos[idx].deadZoneSetting = std::make_unique<IntegerSetting>(
38 tmpStrCat(
"joystick", idx + 1,
"_deadzone"),
39 "size (as a percentage) of the dead center zone",
46 auto idx = getFreeSlot();
47 auto& info = infos[idx];
48 info.joystick = SDL_JoystickOpen(deviceIndex);
49 info.instanceId = info.joystick ? SDL_JoystickInstanceID(info.joystick) : -1;
54 auto it =
ranges::find(infos, instanceId, &Info::instanceId);
55 if (it == infos.end())
return;
57 SDL_JoystickClose(it->joystick);
58 it->joystick =
nullptr;
65 std::vector<JoystickId> result;
66 for (
auto i :
xrange(infos.size())) {
67 if (!infos[i].joystick)
continue;
75 unsigned id = joyId.
raw();
76 return (
id < infos.size()) ? infos[
id].deadZoneSetting.get() :
nullptr;
81 unsigned id = joyId.
raw();
82 auto* joystick = (
id < infos.size()) ? infos[
id].joystick :
nullptr;
83 return joystick ? SDL_JoystickName(joystick) :
"[Not plugged in]";
88 unsigned id = joyId.
raw();
89 auto* joystick = (
id < infos.size()) ? infos[
id].joystick :
nullptr;
90 if (!joystick)
return {};
91 return SDL_JoystickNumButtons(joystick);
96 int instanceId = evt.jbutton.which;
97 auto it =
ranges::find(infos, instanceId, &Info::instanceId);
98 if (it == infos.end())
return {};
99 evt.jbutton.which = narrow<int>(std::distance(infos.begin(), it));
A Setting with an integer value.
void add(int deviceIndex)
IntegerSetting * getJoyDeadZoneSetting(JoystickId joyId) const
std::optional< unsigned > getNumButtons(JoystickId joyId) const
void remove(int instanceId)
std::vector< JoystickId > getConnectedJoysticks() const
std::optional< JoystickId > translateSdlInstanceId(SDL_Event &evt) const
std::string getDisplayName(JoystickId joyId) const
This file implemented 3 utility functions:
auto find(InputRange &&range, const T &value)
TemporaryString tmpStrCat(Ts &&... ts)
constexpr auto xrange(T e)