OXYGENHUB
◆ main openmsx / src / input / JoystickManager.hh
2.3 KiB · C++ · last changed chore(): initial commit 1 month ago source blame history raw
e8555c89 Andy McCall 1
#ifndef JOYSTICK_MANAGER_HH
2
#define JOYSTICK_MANAGER_HH
3
4
#include "InfoTopic.hh"
5
#include "JoystickId.hh"
6
7
#include <cstdint>
8
#include <memory>
9
#include <optional>
10
#include <vector>
11
12
#include <SDL.h>
13
14
namespace openmsx {
15
16
class CommandController;
17
class JoystickAxisMotionEvent;
18
class FloatSetting;
19
class Reactor;
20
21
class JoystickManager {
22
public:
23
	struct Info {
24
		SDL_Joystick* joystick = nullptr;
25
		int instanceId = -1;
26
		std::unique_ptr<FloatSetting> deadZoneSetting;
27
		std::unique_ptr<FloatSetting> midPointSetting;
28
		std::unique_ptr<FloatSetting> saturationSetting;
29
	};
30
public:
31
32
	JoystickManager(const JoystickManager&) = delete;
33
	JoystickManager(JoystickManager&&) = delete;
34
	JoystickManager& operator=(const JoystickManager&) = delete;
35
	JoystickManager& operator=(JoystickManager&&) = delete;
36
37
	explicit JoystickManager(Reactor& reactor);
38
	~JoystickManager();
39
40
	// Handle SDL joystick added/removed events
41
	void add(int deviceIndex);
42
	void remove(int instanceId);
43
44
	// return the range of JoystickId's for which there is a corresponding SDL_Joystick
45
	[[nodiscard]] std::vector<JoystickId> getConnectedJoysticks() const;
46
47
	[[nodiscard]] int getJoyDeadThreshold(JoystickId joyId) const;
48
	[[nodiscard]] float getJoyValue(JoystickId joyId, const JoystickAxisMotionEvent& e) const;
49
	[[nodiscard]] Info* getSettings(JoystickId joyId);
50
	[[nodiscard]] std::string getDisplayName(JoystickId joyId) const;
51
	[[nodiscard]] std::optional<unsigned> getNumAxes(JoystickId joyId) const;
52
	[[nodiscard]] std::optional<unsigned> getNumBalls(JoystickId joyId) const;
53
	[[nodiscard]] std::optional<unsigned> getNumButtons(JoystickId joyId) const;
54
	[[nodiscard]] std::optional<unsigned> getNumHats(JoystickId joyId) const;
55
	[[nodiscard]] std::optional<int16_t> getAxis(JoystickId joyId, int axis) const;
56
57
	[[nodiscard]] std::optional<JoystickId> translateSdlInstanceId(SDL_Event& evt) const;
58
59
private:
60
	[[nodiscard]] size_t getFreeSlot();
61
62
private:
63
	CommandController& commandController;
64
65
	struct JoystickInfo final : InfoTopic {
66
		explicit JoystickInfo(InfoCommand& openMsxInfoCommand);
67
		void execute(std::span<const TclObject> tokens,
68
			     TclObject& result) const override;
69
		[[nodiscard]] std::string help(std::span<const TclObject> tokens) const override;
70
	} joystickInfo;
71
72
	std::vector<Info> infos;
73
};
74
75
} // namespace openmsx
76
77
#endif