openMSX
InputEvents.cc
Go to the documentation of this file.
1 #include "InputEvents.hh"
2 #include "Keys.hh"
3 #include "TclObject.hh"
4 #include "Timer.hh"
5 #include "checked_cast.hh"
6 #include "strCat.hh"
7 #include "stl.hh"
8 #include <string>
9 #include <tuple>
10 #include <SDL.h>
11 
12 using std::string;
13 
14 namespace openmsx {
15 
16 // class TimedEvent
17 
19  : Event(type_)
20  , realtime(Timer::getTime()) // TODO use SDL2 event timestamp
21 {
22 }
23 
24 
25 // class KeyEvent
26 
27 KeyEvent::KeyEvent(EventType type_, Keys::KeyCode keyCode_, uint32_t unicode_)
28  : TimedEvent(type_), keyCode(keyCode_), unicode(unicode_)
29 {
30 }
31 
32 TclObject KeyEvent::toTclList() const
33 {
34  auto result = makeTclList("keyb", Keys::getName(getKeyCode()));
35  if (getUnicode() != 0) {
36  result.addListElement(strCat("unicode", getUnicode()));
37  }
38  return result;
39 }
40 
41 bool KeyEvent::lessImpl(const Event& other) const
42 {
43  // note: don't compare unicode
44  auto& o = checked_cast<const KeyEvent&>(other);
45  return getKeyCode() < o.getKeyCode();
46 }
47 
48 
49 // class KeyUpEvent
50 
52  : KeyEvent(OPENMSX_KEY_UP_EVENT, keyCode_, 0)
53 {
54 }
55 
56 
57 // class KeyDownEvent
58 
59 KeyDownEvent::KeyDownEvent(Keys::KeyCode keyCode_, uint32_t unicode_)
60  : KeyEvent(OPENMSX_KEY_DOWN_EVENT, keyCode_, unicode_)
61 {
62 }
63 
64 
65 // class MouseButtonEvent
66 
68  : TimedEvent(type_), button(button_)
69 {
70 }
71 
72 TclObject MouseButtonEvent::toTclHelper(std::string_view direction) const
73 {
74  return makeTclList("mouse", strCat("button", getButton()), direction);
75 }
76 
77 bool MouseButtonEvent::lessImpl(const Event& other) const
78 {
79  auto& o = checked_cast<const MouseButtonEvent&>(other);
80  return getButton() < o.getButton();
81 }
82 
83 
84 // class MouseButtonUpEvent
85 
88 {
89 }
90 
92 {
93  return toTclHelper("up");
94 }
95 
96 
97 // class MouseButtonDownEvent
98 
101 {
102 }
103 
105 {
106  return toTclHelper("down");
107 }
108 
109 
110 // class MouseWheelEvent
111 
114  , x(x_), y(y_)
115 {
116 }
117 
119 {
120  return makeTclList("mouse", "wheel", getX(), getY());
121 }
122 
123 bool MouseWheelEvent::lessImpl(const Event& other) const
124 {
125  auto& o = checked_cast<const MouseWheelEvent&>(other);
126  return std::tuple( getX(), getY()) <
127  std::tuple(o.getX(), o.getY());
128 }
129 
130 
131 // class MouseMotionEvent
132 
133 MouseMotionEvent::MouseMotionEvent(int xrel_, int yrel_, int xabs_, int yabs_)
135  , xrel(xrel_), yrel(yrel_)
136  , xabs(xabs_), yabs(yabs_)
137 {
138 }
139 
141 {
142  return makeTclList("mouse", "motion", getX(), getY(), getAbsX(), getAbsY());
143 }
144 
145 bool MouseMotionEvent::lessImpl(const Event& other) const
146 {
147  auto& o = checked_cast<const MouseMotionEvent&>(other);
148  return std::tuple( getX(), getY(), getAbsX(), getAbsY()) <
149  std::tuple(o.getX(), o.getY(), o.getAbsX(), o.getAbsY());
150 }
151 
152 
153 // class JoystickEvent
154 
155 JoystickEvent::JoystickEvent(EventType type_, unsigned joystick_)
156  : TimedEvent(type_), joystick(joystick_)
157 {
158 }
159 
161 {
162  return makeTclList(strCat("joy", getJoystick() + 1));
163 }
164 
165 bool JoystickEvent::lessImpl(const Event& other) const
166 {
167  auto& o = checked_cast<const JoystickEvent&>(other);
168  return (getJoystick() != o.getJoystick())
169  ? (getJoystick() < o.getJoystick())
170  : lessImpl(o);
171 }
172 
173 
174 // class JoystickButtonEvent
175 
177  EventType type_, unsigned joystick_, unsigned button_)
178  : JoystickEvent(type_, joystick_), button(button_)
179 {
180 }
181 
182 TclObject JoystickButtonEvent::toTclHelper(std::string_view direction) const
183 {
184  auto result = JoystickEvent::toTclHelper();
185  result.addListElement(strCat("button", getButton()), direction);
186  return result;
187 }
188 
189 bool JoystickButtonEvent::lessImpl(const JoystickEvent& other) const
190 {
191  auto& o = checked_cast<const JoystickButtonEvent&>(other);
192  return getButton() < o.getButton();
193 }
194 
195 
196 // class JoystickButtonUpEvent
197 
198 JoystickButtonUpEvent::JoystickButtonUpEvent(unsigned joystick_, unsigned button_)
199  : JoystickButtonEvent(OPENMSX_JOY_BUTTON_UP_EVENT, joystick_, button_)
200 {
201 }
202 
204 {
205  return toTclHelper("up");
206 }
207 
208 
209 // class JoystickButtonDownEvent
210 
211 JoystickButtonDownEvent::JoystickButtonDownEvent(unsigned joystick_, unsigned button_)
213 {
214 }
215 
217 {
218  return toTclHelper("down");
219 }
220 
221 
222 // class JoystickAxisMotionEvent
223 
225  unsigned joystick_, unsigned axis_, int value_)
227  , axis(axis_), value(value_)
228 {
229 }
230 
232 {
233  auto result = JoystickEvent::toTclHelper();
234  result.addListElement(strCat("axis", getAxis()), getValue());
235  return result;
236 }
237 
238 bool JoystickAxisMotionEvent::lessImpl(const JoystickEvent& other) const
239 {
240  auto& o = checked_cast<const JoystickAxisMotionEvent&>(other);
241  return std::tuple( getAxis(), getValue()) <
242  std::tuple(o.getAxis(), o.getValue());
243 }
244 
245 
246 // class JoystickHatEvent
247 
248 JoystickHatEvent::JoystickHatEvent(unsigned joystick_, unsigned hat_, unsigned value_)
250  , hat(hat_), value(value_)
251 {
252 }
253 
255 {
256  auto result = JoystickEvent::toTclHelper();
257  const char* str;
258  switch (getValue()) {
259  case SDL_HAT_UP: str = "up"; break;
260  case SDL_HAT_RIGHT: str = "right"; break;
261  case SDL_HAT_DOWN: str = "down"; break;
262  case SDL_HAT_LEFT: str = "left"; break;
263  case SDL_HAT_RIGHTUP: str = "rightup"; break;
264  case SDL_HAT_RIGHTDOWN: str = "rightdown"; break;
265  case SDL_HAT_LEFTUP: str = "leftup"; break;
266  case SDL_HAT_LEFTDOWN: str = "leftdown"; break;
267  default: str = "center"; break;
268  }
269  result.addListElement(strCat("hat", getHat()), str);
270  return result;
271 }
272 
273 bool JoystickHatEvent::lessImpl(const JoystickEvent& other) const
274 {
275  auto& o = checked_cast<const JoystickHatEvent&>(other);
276  return std::tuple( getHat(), getValue()) <
277  std::tuple(o.getHat(), o.getValue());
278 }
279 
280 
281 // class FocusEvent
282 
284  : Event(OPENMSX_FOCUS_EVENT), gain(gain_)
285 {
286 }
287 
289 {
290  return makeTclList("focus", getGain());
291 }
292 
293 bool FocusEvent::lessImpl(const Event& other) const
294 {
295  auto& o = checked_cast<const FocusEvent&>(other);
296  return getGain() < o.getGain();
297 }
298 
299 
300 // class ResizeEvent
301 
302 ResizeEvent::ResizeEvent(unsigned x_, unsigned y_)
303  : Event(OPENMSX_RESIZE_EVENT), x(x_), y(y_)
304 {
305 }
306 
308 {
309  return makeTclList("resize", int(getX()), int(getY()));
310 }
311 
312 bool ResizeEvent::lessImpl(const Event& other) const
313 {
314  auto& o = checked_cast<const ResizeEvent&>(other);
315  return std::tuple( getX(), getY()) <
316  std::tuple(o.getX(), o.getY());
317 }
318 
319 
320 // class QuitEvent
321 
323 {
324 }
325 
327 {
328  return makeTclList("quit");
329 }
330 
331 bool QuitEvent::lessImpl(const Event& /*other*/) const
332 {
333  return false;
334 }
335 
336 // class OsdControlEvent
337 
339  EventType type_, unsigned button_,
340  std::shared_ptr<const Event> origEvent_)
341  : TimedEvent(type_), origEvent(std::move(origEvent_)), button(button_)
342 {
343 }
344 
345 bool OsdControlEvent::isRepeatStopper(const Event& other) const
346 {
347  // If this OsdControlEvent was geneated by the other event, then
348  // repeat should not be stopped.
349  if (origEvent.get() == &other) return false;
350 
351  // If this OsdControlEvent event was generated by a joystick motion
352  // event and the new event is also a joystick motion event then don't
353  // stop repeat. We don't need to check the actual values of the events
354  // (it also isn't trivial), because when the values differ by enough,
355  // a new OsdControlEvent will be generated and that one will stop
356  // repeat.
357  return !dynamic_cast<const JoystickAxisMotionEvent*>(origEvent.get()) ||
358  !dynamic_cast<const JoystickAxisMotionEvent*>(&other);
359 }
360 
361 TclObject OsdControlEvent::toTclHelper(std::string_view direction) const
362 {
363  static constexpr const char* const names[] = {
364  "LEFT", "RIGHT", "UP", "DOWN", "A", "B"
365  };
366  return makeTclList("OSDcontrol", names[getButton()], direction);
367 }
368 
369 bool OsdControlEvent::lessImpl(const Event& other) const
370 {
371  auto& o = checked_cast<const OsdControlEvent&>(other);
372  return getButton() < o.getButton();
373 }
374 
375 
376 // class OsdControlReleaseEvent
377 
379  unsigned button_, const std::shared_ptr<const Event>& origEvent_)
380  : OsdControlEvent(OPENMSX_OSD_CONTROL_RELEASE_EVENT, button_, origEvent_)
381 {
382 }
383 
385 {
386  return toTclHelper("RELEASE");
387 }
388 
389 
390 // class OsdControlPressEvent
391 
393  unsigned button_, const std::shared_ptr<const Event>& origEvent_)
394  : OsdControlEvent(OPENMSX_OSD_CONTROL_PRESS_EVENT, button_, origEvent_)
395 {
396 }
397 
399 {
400  return toTclHelper("PRESS");
401 }
402 
403 
404 // class GroupEvent
405 
406 GroupEvent::GroupEvent(EventType type_, std::vector<EventType> typesToMatch_, const TclObject& tclListComponents_)
407  : Event(type_)
408  , typesToMatch(std::move(typesToMatch_))
409  , tclListComponents(tclListComponents_)
410 {
411 }
412 
414 {
415  return tclListComponents;
416 }
417 
418 bool GroupEvent::lessImpl(const Event& /*other*/) const
419 {
420  // All Group events are equivalent
421  return false;
422 }
423 
424 bool GroupEvent::matches(const Event& other) const
425 {
426  return contains(typesToMatch, other.getType());
427 }
428 
429 
430 } // namespace openmsx
TclObject toTclHelper() const
Definition: InputEvents.cc:160
TclObject toTclList() const override
Similar to toString(), but retains the structure of the event.
Definition: InputEvents.cc:254
TclObject toTclList() const override
Similar to toString(), but retains the structure of the event.
Definition: InputEvents.cc:307
JoystickAxisMotionEvent(unsigned joystick, unsigned axis, int value)
Definition: InputEvents.cc:224
TclObject toTclList() const override
Similar to toString(), but retains the structure of the event.
Definition: InputEvents.cc:413
TclObject toTclList() const override
Similar to toString(), but retains the structure of the event.
Definition: InputEvents.cc:91
TclObject toTclHelper(std::string_view direction) const
Definition: InputEvents.cc:361
unsigned getButton() const
Definition: InputEvents.hh:66
MouseWheelEvent(int x, int y)
Definition: InputEvents.cc:112
unsigned getButton() const
Definition: InputEvents.hh:143
TimedEvent(EventType type)
Definition: InputEvents.cc:18
bool contains(ITER first, ITER last, const VAL &val)
Check if a range contains a given value, using linear search.
Definition: stl.hh:92
MouseButtonUpEvent(unsigned button)
Definition: InputEvents.cc:86
STL namespace.
bool getGain() const
Definition: InputEvents.hh:205
TclObject toTclList() const override
Similar to toString(), but retains the structure of the event.
Definition: InputEvents.cc:140
TclObject toTclList() const override
Similar to toString(), but retains the structure of the event.
Definition: InputEvents.cc:216
KeyCode
Constants that identify keys and key modifiers.
Definition: Keys.hh:25
FocusEvent(bool gain)
Definition: InputEvents.cc:283
TclObject toTclList() const override
Similar to toString(), but retains the structure of the event.
Definition: InputEvents.cc:384
TclObject toTclList() const override
Similar to toString(), but retains the structure of the event.
Definition: InputEvents.cc:398
MouseButtonEvent(EventType type, unsigned button_)
Definition: InputEvents.cc:67
unsigned getHat() const
Definition: InputEvents.hh:190
JoystickButtonEvent(EventType type, unsigned joystick, unsigned button)
Definition: InputEvents.cc:176
OsdControlEvent(EventType type, unsigned button_, std::shared_ptr< const Event > origEvent)
Definition: InputEvents.cc:338
KeyEvent(EventType type, Keys::KeyCode keyCode, uint32_t unicode)
Definition: InputEvents.cc:27
OSD events are triggered by other events.
Definition: InputEvents.hh:242
MouseMotionEvent(int xrel, int yrel, int xabs, int yabs)
Definition: InputEvents.cc:133
unsigned getJoystick() const
Definition: InputEvents.hh:127
MouseButtonDownEvent(unsigned button)
Definition: InputEvents.cc:99
JoystickEvent(EventType type, unsigned joystick)
Definition: InputEvents.cc:155
TclObject toTclList() const override
Similar to toString(), but retains the structure of the event.
Definition: InputEvents.cc:118
TclObject toTclList() const override
Similar to toString(), but retains the structure of the event.
Definition: InputEvents.cc:288
uint32_t getUnicode() const
Definition: InputEvents.hh:30
bool isRepeatStopper(const Event &other) const final override
Get the event that actually triggered the creation of this event.
Definition: InputEvents.cc:345
JoystickButtonUpEvent(unsigned joystick, unsigned button)
Definition: InputEvents.cc:198
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
EventType
Definition: Event.hh:10
JoystickHatEvent(unsigned joystick, unsigned hat, unsigned value)
Definition: InputEvents.cc:248
KeyDownEvent(Keys::KeyCode keyCode_)
Definition: InputEvents.hh:52
Keys::KeyCode getKeyCode() const
Definition: InputEvents.hh:29
unsigned getButton() const
Definition: InputEvents.hh:248
unsigned getX() const
Definition: InputEvents.hh:218
OsdControlPressEvent(unsigned button, const std::shared_ptr< const Event > &origEvent)
Definition: InputEvents.cc:392
GroupEvent(EventType type, std::vector< EventType > typesToMatch, const TclObject &tclListComponents)
Definition: InputEvents.cc:406
string getName(KeyCode keyCode)
Translate key code to key name.
Definition: Keys.cc:589
TclObject toTclList() const override
Similar to toString(), but retains the structure of the event.
Definition: InputEvents.cc:203
unsigned getY() const
Definition: InputEvents.hh:219
TclObject toTclHelper(std::string_view direction) const
Definition: InputEvents.cc:72
TclObject toTclList() const override
Similar to toString(), but retains the structure of the event.
Definition: InputEvents.cc:326
EventType getType() const
Definition: Event.hh:87
constexpr KeyMatrixPosition x
Keyboard bindings.
Definition: Keyboard.cc:1377
KeyUpEvent(Keys::KeyCode keyCode)
Definition: InputEvents.cc:51
std::string strCat(Ts &&...ts)
Definition: strCat.hh:573
JoystickButtonDownEvent(unsigned joystick, unsigned button)
Definition: InputEvents.cc:211
OsdControlReleaseEvent(unsigned button, const std::shared_ptr< const Event > &origEvent)
Definition: InputEvents.cc:378
ResizeEvent(unsigned x, unsigned y)
Definition: InputEvents.cc:302
TclObject toTclList() const override
Similar to toString(), but retains the structure of the event.
Definition: InputEvents.cc:231
TclObject makeTclList(Args &&... args)
Definition: TclObject.hh:280
TclObject toTclList() const override
Similar to toString(), but retains the structure of the event.
Definition: InputEvents.cc:104
unsigned getValue() const
Definition: InputEvents.hh:191