openMSX
InputEvents.hh
Go to the documentation of this file.
1 #ifndef INPUTEVENTS_HH
2 #define INPUTEVENTS_HH
3 
4 #include "Event.hh"
5 #include "Keys.hh"
6 #include "TclObject.hh"
7 #include <cstdint>
8 #include <memory>
9 #include <vector>
10 
11 namespace openmsx {
12 
13 class TimedEvent : public Event
14 {
15 public:
17  uint64_t getRealTime() const { return realtime; }
18 protected:
19  explicit TimedEvent(EventType type);
20  ~TimedEvent() = default;
21 private:
22  const uint64_t realtime;
23 };
24 
25 
26 class KeyEvent : public TimedEvent
27 {
28 public:
29  Keys::KeyCode getKeyCode() const { return keyCode; }
30  Keys::KeyCode getScanCode() const { return scanCode; }
31  uint32_t getUnicode() const { return unicode; }
32 
33 protected:
34  KeyEvent(EventType type, Keys::KeyCode keyCode, Keys::KeyCode scanCode, uint32_t unicode);
35  ~KeyEvent() = default;
36 
37 private:
38  TclObject toTclList() const override;
39  bool lessImpl(const Event& other) const override;
40  const Keys::KeyCode keyCode;
41  const Keys::KeyCode scanCode; // 2nd class support, see comments in toTclList()
42  const uint32_t unicode;
43 };
44 
45 class KeyUpEvent final : public KeyEvent
46 {
47 public:
48  explicit KeyUpEvent(Keys::KeyCode keyCode_)
49  : KeyUpEvent(keyCode_, keyCode_) {}
50 
51  explicit KeyUpEvent(Keys::KeyCode keyCode_, Keys::KeyCode scanCode_)
52  : KeyEvent(OPENMSX_KEY_UP_EVENT, keyCode_, scanCode_, 0) {}
53 };
54 
55 class KeyDownEvent final : public KeyEvent
56 {
57 public:
58  explicit KeyDownEvent(Keys::KeyCode keyCode_)
59  : KeyDownEvent(keyCode_, keyCode_, 0) {}
60 
61  explicit KeyDownEvent(Keys::KeyCode keyCode_, Keys::KeyCode scanCode_)
62  : KeyDownEvent(keyCode_, scanCode_, 0) {}
63 
64  explicit KeyDownEvent(Keys::KeyCode keyCode_, uint32_t unicode_)
65  : KeyDownEvent(keyCode_, keyCode_, unicode_) {}
66 
67  explicit KeyDownEvent(Keys::KeyCode keyCode_, Keys::KeyCode scanCode_, uint32_t unicode_)
68  : KeyEvent(OPENMSX_KEY_DOWN_EVENT, keyCode_, scanCode_, unicode_) {}
69 };
70 
71 
73 {
74 public:
75  static constexpr unsigned LEFT = 1;
76  static constexpr unsigned MIDDLE = 2;
77  static constexpr unsigned RIGHT = 3;
78 
79  unsigned getButton() const { return button; }
80 
81 protected:
82  MouseButtonEvent(EventType type, unsigned button_);
83  ~MouseButtonEvent() = default;
84  TclObject toTclHelper(std::string_view direction) const;
85 
86 private:
87  bool lessImpl(const Event& other) const override;
88  const unsigned button;
89 };
90 
92 {
93 public:
94  explicit MouseButtonUpEvent(unsigned button);
95  TclObject toTclList() const override;
96 };
97 
99 {
100 public:
101  explicit MouseButtonDownEvent(unsigned button);
102  TclObject toTclList() const override;
103 };
104 
105 class MouseWheelEvent final : public TimedEvent
106 {
107 public:
108  MouseWheelEvent(int x, int y);
109  int getX() const { return x; }
110  int getY() const { return y; }
111  TclObject toTclList() const override;
112 
113 private:
114  bool lessImpl(const Event& other) const override;
115  const int x;
116  const int y;
117 };
118 
119 class MouseMotionEvent final : public TimedEvent
120 {
121 public:
122  MouseMotionEvent(int xrel, int yrel, int xabs, int yabs);
123  int getX() const { return xrel; }
124  int getY() const { return yrel; }
125  int getAbsX() const { return xabs; }
126  int getAbsY() const { return yabs; }
127  TclObject toTclList() const override;
128 
129 private:
130  bool lessImpl(const Event& other) const override;
131  const int xrel;
132  const int yrel;
133  const int xabs;
134  const int yabs;
135 };
136 
137 class JoystickEvent : public TimedEvent
138 {
139 public:
140  unsigned getJoystick() const { return joystick; }
141 
142 protected:
143  JoystickEvent(EventType type, unsigned joystick);
144  ~JoystickEvent() = default;
145  TclObject toTclHelper() const;
146 
147 private:
148  bool lessImpl(const Event& other) const override;
149  virtual bool lessImpl(const JoystickEvent& other) const = 0;
150  const unsigned joystick;
151 };
152 
154 {
155 public:
156  unsigned getButton() const { return button; }
157 
158 protected:
159  JoystickButtonEvent(EventType type, unsigned joystick, unsigned button);
160  ~JoystickButtonEvent() = default;
161  TclObject toTclHelper(std::string_view direction) const;
162 
163 private:
164  bool lessImpl(const JoystickEvent& other) const override;
165  const unsigned button;
166 };
167 
169 {
170 public:
171  JoystickButtonUpEvent(unsigned joystick, unsigned button);
172  TclObject toTclList() const override;
173 };
174 
176 {
177 public:
178  JoystickButtonDownEvent(unsigned joystick, unsigned button);
179  TclObject toTclList() const override;
180 };
181 
183 {
184 public:
185  static constexpr unsigned X_AXIS = 0;
186  static constexpr unsigned Y_AXIS = 1;
187 
188  JoystickAxisMotionEvent(unsigned joystick, unsigned axis, int value);
189  unsigned getAxis() const { return axis; }
190  int getValue() const { return value; }
191  TclObject toTclList() const override;
192 
193 private:
194  bool lessImpl(const JoystickEvent& other) const override;
195  const unsigned axis;
196  const int value;
197 };
198 
199 class JoystickHatEvent final : public JoystickEvent
200 {
201 public:
202  JoystickHatEvent(unsigned joystick, unsigned hat, unsigned value);
203  unsigned getHat() const { return hat; }
204  unsigned getValue() const { return value; }
205  TclObject toTclList() const override;
206 
207 private:
208  bool lessImpl(const JoystickEvent& other) const override;
209  const unsigned hat;
210  const unsigned value;
211 };
212 
213 
214 class FocusEvent final : public Event
215 {
216 public:
217  explicit FocusEvent(bool gain);
218  bool getGain() const { return gain; }
219  TclObject toTclList() const override;
220 
221 private:
222  bool lessImpl(const Event& other) const override;
223  const bool gain;
224 };
225 
226 
227 class ResizeEvent final : public Event
228 {
229 public:
230  ResizeEvent(unsigned x, unsigned y);
231  unsigned getX() const { return x; }
232  unsigned getY() const { return y; }
233  TclObject toTclList() const override;
234 
235 private:
236  bool lessImpl(const Event& other) const override;
237  const unsigned x;
238  const unsigned y;
239 };
240 
241 
242 class FileDropEvent final : public Event
243 {
244 public:
245  explicit FileDropEvent(std::string fileName);
246  const std::string& getFileName() const { return fileName; }
247  TclObject toTclList() const override;
248 
249 private:
250  bool lessImpl(const Event& other) const override;
251  const std::string fileName;
252 };
253 
254 
255 class QuitEvent final : public Event
256 {
257 public:
258  QuitEvent();
259  TclObject toTclList() const override;
260 private:
261  bool lessImpl(const Event& other) const override;
262 };
263 
269 {
270 public:
273 
274  unsigned getButton() const { return button; }
275 
287  bool isRepeatStopper(const Event& other) const final override;
288 
289 protected:
290  OsdControlEvent(EventType type, unsigned button_,
291  std::shared_ptr<const Event> origEvent);
292  ~OsdControlEvent() = default;
293  TclObject toTclHelper(std::string_view direction) const;
294 
295 private:
296  bool lessImpl(const Event& other) const final override;
297  const std::shared_ptr<const Event> origEvent;
298  const unsigned button;
299 };
300 
302 {
303 public:
304  OsdControlReleaseEvent(unsigned button,
305  const std::shared_ptr<const Event>& origEvent);
306  TclObject toTclList() const override;
307 };
308 
310 {
311 public:
312  OsdControlPressEvent(unsigned button,
313  const std::shared_ptr<const Event>& origEvent);
314  TclObject toTclList() const override;
315 };
316 
317 
318 class GroupEvent final : public Event
319 {
320 public:
321  GroupEvent(EventType type, std::vector<EventType> typesToMatch, const TclObject& tclListComponents);
322  TclObject toTclList() const override;
323 
324 private:
325  bool lessImpl(const Event& other) const override;
326  bool matches(const Event& other) const override;
327  const std::vector<EventType> typesToMatch;
328  const TclObject tclListComponents;
329 };
330 
331 
332 } // namespace openmsx
333 
334 #endif
openmsx::KeyEvent
Definition: InputEvents.hh:27
openmsx::KeyDownEvent::KeyDownEvent
KeyDownEvent(Keys::KeyCode keyCode_, uint32_t unicode_)
Definition: InputEvents.hh:64
openmsx::JoystickButtonUpEvent::JoystickButtonUpEvent
JoystickButtonUpEvent(unsigned joystick, unsigned button)
Definition: InputEvents.cc:193
openmsx::OsdControlEvent::DOWN_BUTTON
@ DOWN_BUTTON
Definition: InputEvents.hh:271
openmsx::OsdControlEvent::RIGHT_BUTTON
@ RIGHT_BUTTON
Definition: InputEvents.hh:271
openmsx::ResizeEvent::toTclList
TclObject toTclList() const override
Similar to toString(), but retains the structure of the event.
Definition: InputEvents.cc:302
openmsx::MouseButtonEvent::MouseButtonEvent
MouseButtonEvent(EventType type, unsigned button_)
Definition: InputEvents.cc:62
openmsx::KeyEvent::KeyEvent
KeyEvent(EventType type, Keys::KeyCode keyCode, Keys::KeyCode scanCode, uint32_t unicode)
Definition: InputEvents.cc:27
openmsx::OsdControlReleaseEvent::toTclList
TclObject toTclList() const override
Similar to toString(), but retains the structure of the event.
Definition: InputEvents.cc:398
openmsx::Event
Definition: Event.hh:84
openmsx::JoystickHatEvent::toTclList
TclObject toTclList() const override
Similar to toString(), but retains the structure of the event.
Definition: InputEvents.cc:249
openmsx::KeyUpEvent
Definition: InputEvents.hh:46
TclObject.hh
openmsx::MouseButtonEvent::RIGHT
static constexpr unsigned RIGHT
Definition: InputEvents.hh:77
openmsx::MouseMotionEvent::toTclList
TclObject toTclList() const override
Similar to toString(), but retains the structure of the event.
Definition: InputEvents.cc:135
openmsx::JoystickButtonUpEvent
Definition: InputEvents.hh:169
openmsx::MouseButtonDownEvent
Definition: InputEvents.hh:99
openmsx::KeyEvent::getScanCode
Keys::KeyCode getScanCode() const
Definition: InputEvents.hh:30
openmsx::MouseButtonEvent::toTclHelper
TclObject toTclHelper(std::string_view direction) const
Definition: InputEvents.cc:67
openmsx::OsdControlPressEvent::toTclList
TclObject toTclList() const override
Similar to toString(), but retains the structure of the event.
Definition: InputEvents.cc:412
openmsx::JoystickButtonEvent
Definition: InputEvents.hh:154
openmsx::TimedEvent::~TimedEvent
~TimedEvent()=default
openmsx::KeyDownEvent::KeyDownEvent
KeyDownEvent(Keys::KeyCode keyCode_, Keys::KeyCode scanCode_)
Definition: InputEvents.hh:61
openmsx::OsdControlReleaseEvent
Definition: InputEvents.hh:302
openmsx::MouseButtonEvent::getButton
unsigned getButton() const
Definition: InputEvents.hh:79
openmsx::KeyEvent::getUnicode
uint32_t getUnicode() const
Definition: InputEvents.hh:31
openmsx::OsdControlEvent::toTclHelper
TclObject toTclHelper(std::string_view direction) const
Definition: InputEvents.cc:375
openmsx::KeyDownEvent::KeyDownEvent
KeyDownEvent(Keys::KeyCode keyCode_, Keys::KeyCode scanCode_, uint32_t unicode_)
Definition: InputEvents.hh:67
openmsx::TimedEvent
Definition: InputEvents.hh:14
openmsx::OPENMSX_KEY_DOWN_EVENT
@ OPENMSX_KEY_DOWN_EVENT
Definition: Event.hh:13
openmsx::JoystickButtonDownEvent::toTclList
TclObject toTclList() const override
Similar to toString(), but retains the structure of the event.
Definition: InputEvents.cc:211
openmsx::JoystickEvent::getJoystick
unsigned getJoystick() const
Definition: InputEvents.hh:140
openmsx::MouseButtonDownEvent::MouseButtonDownEvent
MouseButtonDownEvent(unsigned button)
Definition: InputEvents.cc:94
openmsx::OsdControlEvent::getButton
unsigned getButton() const
Definition: InputEvents.hh:274
openmsx::JoystickButtonEvent::~JoystickButtonEvent
~JoystickButtonEvent()=default
openmsx::ResizeEvent::getY
unsigned getY() const
Definition: InputEvents.hh:232
openmsx::JoystickAxisMotionEvent::JoystickAxisMotionEvent
JoystickAxisMotionEvent(unsigned joystick, unsigned axis, int value)
Definition: InputEvents.cc:219
openmsx::KeyUpEvent::KeyUpEvent
KeyUpEvent(Keys::KeyCode keyCode_, Keys::KeyCode scanCode_)
Definition: InputEvents.hh:51
openmsx::MouseWheelEvent::MouseWheelEvent
MouseWheelEvent(int x, int y)
Definition: InputEvents.cc:107
openmsx::ResizeEvent::getX
unsigned getX() const
Definition: InputEvents.hh:231
openmsx::JoystickAxisMotionEvent::getValue
int getValue() const
Definition: InputEvents.hh:190
openmsx::JoystickButtonDownEvent
Definition: InputEvents.hh:176
openmsx::FileDropEvent
Definition: InputEvents.hh:243
openmsx::JoystickAxisMotionEvent::getAxis
unsigned getAxis() const
Definition: InputEvents.hh:189
openmsx::FocusEvent
Definition: InputEvents.hh:215
openmsx::OsdControlEvent::isRepeatStopper
bool isRepeatStopper(const Event &other) const final override
Get the event that actually triggered the creation of this event.
Definition: InputEvents.cc:359
openmsx::JoystickAxisMotionEvent::toTclList
TclObject toTclList() const override
Similar to toString(), but retains the structure of the event.
Definition: InputEvents.cc:226
openmsx::ResizeEvent::ResizeEvent
ResizeEvent(unsigned x, unsigned y)
Definition: InputEvents.cc:297
openmsx::MouseMotionEvent::MouseMotionEvent
MouseMotionEvent(int xrel, int yrel, int xabs, int yabs)
Definition: InputEvents.cc:128
openmsx::MouseWheelEvent::getX
int getX() const
Definition: InputEvents.hh:109
openmsx::FocusEvent::getGain
bool getGain() const
Definition: InputEvents.hh:218
openmsx::OsdControlPressEvent::OsdControlPressEvent
OsdControlPressEvent(unsigned button, const std::shared_ptr< const Event > &origEvent)
Definition: InputEvents.cc:406
openmsx::FileDropEvent::toTclList
TclObject toTclList() const override
Similar to toString(), but retains the structure of the event.
Definition: InputEvents.cc:322
openmsx::JoystickEvent::JoystickEvent
JoystickEvent(EventType type, unsigned joystick)
Definition: InputEvents.cc:150
openmsx::JoystickHatEvent::getValue
unsigned getValue() const
Definition: InputEvents.hh:204
openmsx::JoystickHatEvent::JoystickHatEvent
JoystickHatEvent(unsigned joystick, unsigned hat, unsigned value)
Definition: InputEvents.cc:243
openmsx::MouseMotionEvent::getY
int getY() const
Definition: InputEvents.hh:124
openmsx::JoystickButtonEvent::getButton
unsigned getButton() const
Definition: InputEvents.hh:156
openmsx::MouseButtonUpEvent::toTclList
TclObject toTclList() const override
Similar to toString(), but retains the structure of the event.
Definition: InputEvents.cc:86
openmsx::GroupEvent::toTclList
TclObject toTclList() const override
Similar to toString(), but retains the structure of the event.
Definition: InputEvents.cc:427
Keys.hh
openmsx::JoystickButtonUpEvent::toTclList
TclObject toTclList() const override
Similar to toString(), but retains the structure of the event.
Definition: InputEvents.cc:198
openmsx::FileDropEvent::FileDropEvent
FileDropEvent(std::string fileName)
Definition: InputEvents.cc:317
openmsx::JoystickHatEvent
Definition: InputEvents.hh:200
openmsx::JoystickEvent
Definition: InputEvents.hh:138
openmsx::GroupEvent
Definition: InputEvents.hh:319
openmsx::MouseWheelEvent::toTclList
TclObject toTclList() const override
Similar to toString(), but retains the structure of the event.
Definition: InputEvents.cc:113
openmsx::MouseButtonEvent::MIDDLE
static constexpr unsigned MIDDLE
Definition: InputEvents.hh:76
openmsx::OsdControlReleaseEvent::OsdControlReleaseEvent
OsdControlReleaseEvent(unsigned button, const std::shared_ptr< const Event > &origEvent)
Definition: InputEvents.cc:392
openmsx::x
constexpr KeyMatrixPosition x
Keyboard bindings.
Definition: Keyboard.cc:1419
openmsx::MouseMotionEvent::getX
int getX() const
Definition: InputEvents.hh:123
openmsx::TimedEvent::TimedEvent
TimedEvent(EventType type)
Definition: InputEvents.cc:18
openmsx::QuitEvent
Definition: InputEvents.hh:256
openmsx::MouseMotionEvent::getAbsX
int getAbsX() const
Definition: InputEvents.hh:125
openmsx::MouseButtonUpEvent::MouseButtonUpEvent
MouseButtonUpEvent(unsigned button)
Definition: InputEvents.cc:81
openmsx::OsdControlEvent::UP_BUTTON
@ UP_BUTTON
Definition: InputEvents.hh:271
openmsx::TimedEvent::getRealTime
uint64_t getRealTime() const
Query creation time.
Definition: InputEvents.hh:17
openmsx::JoystickEvent::toTclHelper
TclObject toTclHelper() const
Definition: InputEvents.cc:155
openmsx::JoystickAxisMotionEvent::Y_AXIS
static constexpr unsigned Y_AXIS
Definition: InputEvents.hh:186
openmsx::OsdControlEvent::LEFT_BUTTON
@ LEFT_BUTTON
Definition: InputEvents.hh:271
openmsx::FocusEvent::toTclList
TclObject toTclList() const override
Similar to toString(), but retains the structure of the event.
Definition: InputEvents.cc:283
openmsx::MouseButtonUpEvent
Definition: InputEvents.hh:92
openmsx::EventType
EventType
Definition: Event.hh:11
openmsx::QuitEvent::toTclList
TclObject toTclList() const override
Similar to toString(), but retains the structure of the event.
Definition: InputEvents.cc:340
Event.hh
openmsx::MouseMotionEvent::getAbsY
int getAbsY() const
Definition: InputEvents.hh:126
openmsx::OsdControlEvent::~OsdControlEvent
~OsdControlEvent()=default
openmsx::MouseButtonEvent::~MouseButtonEvent
~MouseButtonEvent()=default
openmsx::MouseMotionEvent
Definition: InputEvents.hh:120
openmsx::OsdControlEvent::A_BUTTON
@ A_BUTTON
Definition: InputEvents.hh:272
openmsx::TclObject
Definition: TclObject.hh:22
openmsx::GroupEvent::GroupEvent
GroupEvent(EventType type, std::vector< EventType > typesToMatch, const TclObject &tclListComponents)
Definition: InputEvents.cc:420
openmsx::OsdControlEvent::B_BUTTON
@ B_BUTTON
Definition: InputEvents.hh:272
openmsx::Keys::KeyCode
KeyCode
Constants that identify keys and key modifiers.
Definition: Keys.hh:26
openmsx::OPENMSX_KEY_UP_EVENT
@ OPENMSX_KEY_UP_EVENT
Definition: Event.hh:12
openmsx::JoystickAxisMotionEvent::X_AXIS
static constexpr unsigned X_AXIS
Definition: InputEvents.hh:185
openmsx::KeyUpEvent::KeyUpEvent
KeyUpEvent(Keys::KeyCode keyCode_)
Definition: InputEvents.hh:48
openmsx::MouseButtonEvent::LEFT
static constexpr unsigned LEFT
Definition: InputEvents.hh:75
openmsx::JoystickAxisMotionEvent
Definition: InputEvents.hh:183
openmsx::MouseButtonDownEvent::toTclList
TclObject toTclList() const override
Similar to toString(), but retains the structure of the event.
Definition: InputEvents.cc:99
openmsx::ResizeEvent
Definition: InputEvents.hh:228
openmsx::OsdControlEvent
OSD events are triggered by other events.
Definition: InputEvents.hh:269
openmsx::OsdControlPressEvent
Definition: InputEvents.hh:310
openmsx::KeyDownEvent
Definition: InputEvents.hh:56
openmsx::KeyEvent::getKeyCode
Keys::KeyCode getKeyCode() const
Definition: InputEvents.hh:29
openmsx
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
openmsx::KeyDownEvent::KeyDownEvent
KeyDownEvent(Keys::KeyCode keyCode_)
Definition: InputEvents.hh:58
openmsx::KeyEvent::~KeyEvent
~KeyEvent()=default
openmsx::FileDropEvent::getFileName
const std::string & getFileName() const
Definition: InputEvents.hh:246
openmsx::MouseWheelEvent
Definition: InputEvents.hh:106
openmsx::JoystickButtonDownEvent::JoystickButtonDownEvent
JoystickButtonDownEvent(unsigned joystick, unsigned button)
Definition: InputEvents.cc:206
openmsx::FocusEvent::FocusEvent
FocusEvent(bool gain)
Definition: InputEvents.cc:278
openmsx::QuitEvent::QuitEvent
QuitEvent()
Definition: InputEvents.cc:336
openmsx::JoystickHatEvent::getHat
unsigned getHat() const
Definition: InputEvents.hh:203
openmsx::OsdControlEvent::OsdControlEvent
OsdControlEvent(EventType type, unsigned button_, std::shared_ptr< const Event > origEvent)
Definition: InputEvents.cc:352
openmsx::MouseWheelEvent::getY
int getY() const
Definition: InputEvents.hh:110
openmsx::MouseButtonEvent
Definition: InputEvents.hh:73
openmsx::JoystickEvent::~JoystickEvent
~JoystickEvent()=default
openmsx::JoystickButtonEvent::JoystickButtonEvent
JoystickButtonEvent(EventType type, unsigned joystick, unsigned button)
Definition: InputEvents.cc:171