OXYGENHUB
◆ main openmsx / src / events / InputEventGenerator.hh
3.0 KiB · C++ · last changed chore(): initial commit 1 month ago source blame history raw
1
#ifndef INPUTEVENTGENERATOR_HH
2
#define INPUTEVENTGENERATOR_HH
3
4
#include "BooleanSetting.hh"
5
#include "Command.hh"
6
#include "EventListener.hh"
7
#include "JoystickManager.hh"
8
9
#include "SDLKey.hh"
10
#include <SDL.h>
11
12
#include <cstdint>
13
#include <optional>
14
15
namespace openmsx {
16
17
class CommandController;
18
class EventDistributor;
19
20
class InputEventGenerator final : private EventListener
21
{
22
public:
23
	InputEventGenerator(CommandController& commandController,
24
	                    EventDistributor& eventDistributor);
25
	InputEventGenerator(const InputEventGenerator&) = delete;
26
	InputEventGenerator(InputEventGenerator&&) = delete;
27
	InputEventGenerator& operator=(const InputEventGenerator&) = delete;
28
	InputEventGenerator& operator=(InputEventGenerator&&) = delete;
29
	~InputEventGenerator();
30
31
	/** Wait for event(s) and handle it.
32
	  * This method should be called from the main thread.
33
	  */
34
	void wait();
35
36
	/** Input Grab on or off */
37
	[[nodiscard]] BooleanSetting& getGrabInput() { return grabInput; }
38
	/** Must be called when 'grabinput' or 'fullscreen' setting changes. */
39
	void updateGrab(bool grab);
40
41
	/** Poll for SDL events and dispatch them.
42
	  * @param timeoutMs If provided, wait up to this many milliseconds for
43
	  *        the first event using SDL_WaitEventTimeout(). Subsequent
44
	  *        events in the same batch use SDL_PollEvent(). If nullopt,
45
	  *        uses SDL_PollEvent() for all events (non-blocking).
46
	  */
47
	void poll(std::optional<int> timeoutMs = {});
48
49
	[[nodiscard]] JoystickManager& getJoystickManager() { return joystickManager; }
50
51
	void initializeGrab() const;
52
53
private:
54
	void handle(SDL_Event& evt);
55
	void handleKeyDown(const SDL_KeyboardEvent& key, uint32_t unicode);
56
	void splitText(uint32_t timestamp, const char* utf8);
57
	void setGrabInput(bool grab) const;
58
59
	// EventListener
60
	bool signalEvent(const Event& event) override;
61
62
	EventDistributor& eventDistributor;
63
	JoystickManager joystickManager;
64
	BooleanSetting grabInput;
65
66
	struct EscapeGrabCmd final : Command {
67
		explicit EscapeGrabCmd(CommandController& commandController);
68
		void execute(std::span<const TclObject> tokens, TclObject& result) override;
69
		[[nodiscard]] std::string help(std::span<const TclObject> tokens) const override;
70
	} escapeGrabCmd;
71
72
	enum EscapeGrabState : uint8_t {
73
		ESCAPE_GRAB_WAIT_CMD,
74
		ESCAPE_GRAB_WAIT_LOST,
75
		ESCAPE_GRAB_WAIT_GAIN
76
	} escapeGrabState = ESCAPE_GRAB_WAIT_CMD;
77
78
	// OsdControl
79
	void setNewOsdControlButtonState(unsigned newState);
80
	void triggerOsdControlEventsFromJoystickAxisMotion(unsigned axis, int value);
81
	void triggerOsdControlEventsFromJoystickHat(int value);
82
	void osdControlChangeButton(bool down, unsigned changedButtonMask);
83
	void triggerOsdControlEventsFromJoystickButtonEvent(unsigned button, bool down);
84
	void triggerOsdControlEventsFromKeyEvent(SDLKey key, bool repeat);
85
86
private:
87
	unsigned osdControlButtonsState = unsigned(~0); // 0 is pressed, 1 is released
88
	bool sendQuit = false; // only send QuitEvent once
89
90
	// only for Android
91
	static inline bool androidButtonA = false;
92
	static inline bool androidButtonB = false;
93
};
94
95
} // namespace openmsx
96
97
#endif