openMSX
InputEventGenerator.hh
Go to the documentation of this file.
1 #ifndef INPUTEVENTGENERATOR_HH
2 #define INPUTEVENTGENERATOR_HH
3 
4 #include "Observer.hh"
5 #include "BooleanSetting.hh"
6 #include "EventListener.hh"
7 #include "Command.hh"
8 #include "Keys.hh"
9 #include <SDL.h>
10 #include <memory>
11 
12 namespace openmsx {
13 
14 class CommandController;
15 class EventDistributor;
16 class GlobalSettings;
17 
18 class InputEventGenerator final : private Observer<Setting>
19  , private EventListener
20 {
21 public:
24 
25  InputEventGenerator(CommandController& commandController,
26  EventDistributor& eventDistributor,
27  GlobalSettings& globalSettings);
29 
33  void wait();
34 
38  void setKeyRepeat(bool enable);
39 
41  BooleanSetting& getGrabInput() { return grabInput; }
42 
47  static int joystickNumButtons(SDL_Joystick* joystick);
48  static bool joystickGetButton(SDL_Joystick* joystick, int button);
49 
50  void poll();
51 
52 private:
53  using EventPtr = std::shared_ptr<const Event>;
54 
55  void handle(const SDL_Event& evt);
56  void handleKeyDown(const SDL_KeyboardEvent& key, uint32_t unicode);
57  void handleText(const char* utf8);
58  void setGrabInput(bool grab);
59 
60  // Observer<Setting>
61  void update(const Setting& setting) override;
62 
63  // EventListener
64  int signalEvent(const std::shared_ptr<const Event>& event) override;
65 
66  EventDistributor& eventDistributor;
67  GlobalSettings& globalSettings;
68  BooleanSetting grabInput;
69 
70  struct EscapeGrabCmd final : Command {
71  explicit EscapeGrabCmd(CommandController& commandController);
72  void execute(span<const TclObject> tokens, TclObject& result) override;
73  std::string help(const std::vector<std::string>& tokens) const override;
74  } escapeGrabCmd;
75 
76  enum EscapeGrabState {
77  ESCAPE_GRAB_WAIT_CMD,
78  ESCAPE_GRAB_WAIT_LOST,
79  ESCAPE_GRAB_WAIT_GAIN
80  } escapeGrabState;
81 
82  // OsdControl
83  void setNewOsdControlButtonState(
84  unsigned newState, const EventPtr& origEvent);
85  void triggerOsdControlEventsFromJoystickAxisMotion(
86  unsigned axis, int value, const EventPtr& origEvent);
87  void triggerOsdControlEventsFromJoystickHat(
88  int value, const EventPtr& origEvent);
89  void osdControlChangeButton(
90  bool up, unsigned changedButtonMask, const EventPtr& origEvent);
91  void triggerOsdControlEventsFromJoystickButtonEvent(
92  unsigned button, bool up, const EventPtr& origEvent);
93  void triggerOsdControlEventsFromKeyEvent(
94  Keys::KeyCode keyCode, bool up, const EventPtr& origEvent);
95 
96 
97  bool keyRepeat;
98  unsigned osdControlButtonsState; // 0 is pressed, 1 is released
99 
100  // only for Android
101  static bool androidButtonA;
102  static bool androidButtonB;
103 };
104 
105 } // namespace openmsx
106 
107 #endif
void wait()
Wait for event(s) and handle it.
Definition: span.hh:34
InputEventGenerator & operator=(const InputEventGenerator &)=delete
KeyCode
Constants that identify keys and key modifiers.
Definition: Keys.hh:26
static int joystickNumButtons(SDL_Joystick *joystick)
Normally the following two functions simply delegate to SDL_JoystickNumButtons() and SDL_JoystickGetB...
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
void setKeyRepeat(bool enable)
Enable or disable keyboard event repeats.
BooleanSetting & getGrabInput()
Input Grab on or off.
static bool joystickGetButton(SDL_Joystick *joystick, int button)
Generic Gang-of-Four Observer class, templatized edition.
Definition: Observer.hh:9
This class contains settings that are used by several other class (including some singletons)...
InputEventGenerator(const InputEventGenerator &)=delete