ScummVM API documentation
events.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef COMMON_EVENTS_H
23 #define COMMON_EVENTS_H
24 
25 #include "common/keyboard.h"
26 #include "common/queue.h"
27 #include "common/rect.h"
28 #include "common/noncopyable.h"
29 
30 #include "common/list.h"
31 #include "common/singleton.h"
32 
33 namespace Common {
34 
48 enum EventType {
49  EVENT_INVALID = 0,
56  EVENT_LBUTTONDOWN = 4,
57  EVENT_LBUTTONUP = 5,
58  EVENT_RBUTTONDOWN = 6,
59  EVENT_RBUTTONUP = 7,
60  EVENT_WHEELUP = 8,
61  EVENT_WHEELDOWN = 9,
62  EVENT_MBUTTONDOWN = 13,
63  EVENT_MBUTTONUP = 14,
64 
65  EVENT_MAINMENU = 15,
66  EVENT_RETURN_TO_LAUNCHER = 16,
67  EVENT_MUTE = 17,
68 
69  EVENT_QUIT = 10,
70  EVENT_SCREEN_CHANGED = 11,
71 
74 
84 
85  EVENT_CUSTOM_BACKEND_ACTION_START = 18,
86  EVENT_CUSTOM_BACKEND_ACTION_END = 19,
87  EVENT_CUSTOM_BACKEND_ACTION_AXIS = 34,
88  EVENT_CUSTOM_ENGINE_ACTION_START = 20,
89  EVENT_CUSTOM_ENGINE_ACTION_END = 21,
90 
91  EVENT_VIRTUAL_KEYBOARD = 22,
92 
93  EVENT_DROP_FILE = 23,
94 
95  EVENT_JOYAXIS_MOTION = 24,
96  EVENT_JOYBUTTON_DOWN = 25,
97  EVENT_JOYBUTTON_UP = 26,
98 
99  EVENT_CLIPBOARD_UPDATE = 27,
100 
101  EVENT_CUSTOM_BACKEND_HARDWARE = 28,
102  EVENT_DEBUGGER = 29,
103 
111  EVENT_X1BUTTONUP = 31,
112  EVENT_X2BUTTONDOWN = 32,
113  EVENT_X2BUTTONUP = 33,
114 
117  EVENT_FOCUS_LOST = 37
118 };
119 
120 const int16 JOYAXIS_MIN = -32768;
121 const int16 JOYAXIS_MAX = 32767;
122 
128  byte axis;
130  int16 position;
137  uint8 button;
138 
139  JoystickState() : axis(0), position(0), button(0) {}
140 };
141 
146  JOYSTICK_BUTTON_INVALID,
147  JOYSTICK_BUTTON_A,
148  JOYSTICK_BUTTON_B,
149  JOYSTICK_BUTTON_X,
150  JOYSTICK_BUTTON_Y,
151  JOYSTICK_BUTTON_BACK,
152  JOYSTICK_BUTTON_GUIDE,
153  JOYSTICK_BUTTON_START,
154  JOYSTICK_BUTTON_LEFT_STICK,
155  JOYSTICK_BUTTON_RIGHT_STICK,
156  JOYSTICK_BUTTON_LEFT_SHOULDER,
157  JOYSTICK_BUTTON_RIGHT_SHOULDER,
158  JOYSTICK_BUTTON_DPAD_UP,
159  JOYSTICK_BUTTON_DPAD_DOWN,
160  JOYSTICK_BUTTON_DPAD_LEFT,
161  JOYSTICK_BUTTON_DPAD_RIGHT,
162  JOYSTICK_BUTTON_DPAD_CENTER
163 };
164 
169  JOYSTICK_AXIS_LEFT_STICK_X,
170  JOYSTICK_AXIS_LEFT_STICK_Y,
171  JOYSTICK_AXIS_RIGHT_STICK_X,
172  JOYSTICK_AXIS_RIGHT_STICK_Y,
173  JOYSTICK_AXIS_LEFT_TRIGGER,
174  JOYSTICK_AXIS_RIGHT_TRIGGER,
175  JOYSTICK_AXIS_HAT_X,
176  JOYSTICK_AXIS_HAT_Y
177 };
178 
183  MOUSE_BUTTON_LEFT = 0,
184  MOUSE_BUTTON_RIGHT = 1,
185  MOUSE_BUTTON_MIDDLE = 2,
186  MOUSE_WHEEL_UP = 3,
187  MOUSE_WHEEL_DOWN = 4,
188  MOUSE_BUTTON_X1 = 5,
189  MOUSE_BUTTON_X2 = 6
190 };
192 typedef uint32 CustomEventType;
193 
198 struct Event {
199 
202 
208  bool kbdRepeat;
209 
215 
223 
225  CustomEventType customType;
226 
229 
234 
240 
241  Event() : type(EVENT_INVALID), kbdRepeat(false), customType(0) {
242  }
243 };
244 
250 bool isMouseEvent(const Event &event);
251 
258 class EventSource {
259 public:
260  virtual ~EventSource();
261 
268  virtual bool pollEvent(Event &event) = 0;
269 
278  virtual bool allowMapping() const { return true; }
279 };
280 
288 protected:
289  Queue<Event> _artificialEventQueue;
290 public:
292  void addEvent(const Event &ev) {
293  _artificialEventQueue.push(ev);
294  }
295 
296  bool pollEvent(Event &ev) {
297  if (!_artificialEventQueue.empty()) {
298  ev = _artificialEventQueue.pop();
299  return true;
300  } else {
301  return false;
302  }
303  }
304 
309  virtual bool allowMapping() const { return false; }
310 };
311 
318 public:
319  virtual ~EventObserver();
320 
334  virtual bool notifyEvent(const Event &event) = 0;
335 
339  virtual void notifyPoll() { }
340 };
341 
347 class EventMapper {
348 public:
349  virtual ~EventMapper();
350 
354  virtual bool mapEvent(const Event &ev, List<Event> &mappedEvents) = 0;
355 };
356 
371 public:
372  EventDispatcher();
373  ~EventDispatcher();
374 
381  void dispatch();
382 
387  void clearEvents();
388 
392  void registerMapper(EventMapper *mapper, bool autoFree);
393 
399  void unregisterMapper(EventMapper *mapper);
400 
404  void registerSource(EventSource *source, bool autoFree);
405 
411  void unregisterSource(EventSource *source);
412 
417  void ignoreSources(bool ignore);
418 
425  void registerObserver(EventObserver *obs, uint priority, bool autoFree, bool listenPolls = false);
426 
432  void unregisterObserver(EventObserver *obs);
433 private:
434  struct Entry {
435  bool autoFree;
436  bool ignore;
437  };
438 
439  struct MapperEntry : public Entry {
440  EventMapper *mapper;
441  };
442 
443  List<MapperEntry> _mappers;
444 
445  struct SourceEntry : public Entry {
446  EventSource *source;
447  };
448 
449  List<SourceEntry> _sources;
450 
451  struct ObserverEntry : public Entry {
452  uint priority;
453  EventObserver *observer;
454  bool poll;
455  };
456 
457  List<ObserverEntry> _observers;
458 
459  void dispatchEvent(const Event &event);
460  void dispatchPoll();
461 };
462 
463 class Keymap;
464 class Keymapper;
465 
472 public:
473  virtual ~EventManager();
474 
475  enum {
476  LBUTTON = 1 << MOUSE_BUTTON_LEFT,
477  RBUTTON = 1 << MOUSE_BUTTON_RIGHT
478  };
479 
480 
485  virtual void init() {}
486 
492  virtual bool pollEvent(Event &event) = 0;
493 
497  virtual void pushEvent(const Event &event) = 0;
498 
502  virtual void purgeMouseEvents() = 0;
503 
507  virtual void purgeKeyboardEvents() = 0;
508 
510  virtual Point getMousePos() const = 0;
511 
517  virtual int getButtonState() const = 0;
518 
520  virtual int getModifierState() const = 0;
521 
525  virtual int shouldQuit() const = 0;
526 
530  virtual int shouldReturnToLauncher() const = 0;
531 
536  virtual void resetReturnToLauncher() = 0;
537  virtual void resetQuit() = 0;
538  // Optional: check whether a given key is currently pressed ????
539  //virtual bool isKeyPressed(int keycode) = 0;
540 
541  // TODO: Keyboard repeat support?
542 
543  // TODO: Consider removing OSystem::getScreenChangeID and
544  // replacing it by a generic getScreenChangeID method here
546  virtual Keymapper *getKeymapper() = 0;
548  virtual Keymap *getGlobalKeymap() = 0;
549 
550  enum {
555  kEventManPriority = 0,
560  kEventRecorderPriority = 1,
565  kEventRemapperPriority = 999
566  };
567 
571  EventDispatcher *getEventDispatcher() { return &_dispatcher; }
572 
573 protected:
574  EventDispatcher _dispatcher;
575 };
576 
584 
587 } // End of namespace Common
588 
589 #endif
Definition: keymap.h:66
bool isMouseEvent(const Event &event)
Common::Path path
Definition: events.h:228
void addEvent(const Event &ev)
Definition: events.h:292
Definition: events.h:51
uint8 button
Definition: events.h:137
bool kbdRepeat
Definition: events.h:208
Common::Point relMouse
Definition: events.h:233
CustomEventType customType
Definition: events.h:225
EventType
Definition: events.h:48
Definition: list.h:44
Definition: events.h:370
Definition: events.h:83
EventSource * makeKeyboardRepeatingEventSource(EventSource *eventSource)
Definition: path.h:52
uint32 CustomEventType
Definition: events.h:192
Definition: events.h:126
int16 position
Definition: events.h:130
EventDispatcher * getEventDispatcher()
Definition: events.h:571
Point mouse
Definition: events.h:222
Definition: queue.h:42
Definition: events.h:347
Definition: keymapper.h:44
JoystickAxis
Definition: events.h:168
Definition: noncopyable.h:39
MouseButton
Definition: events.h:182
Definition: events.h:110
Definition: events.h:116
Definition: events.h:317
KeyState kbd
Definition: events.h:214
bool pollEvent(Event &ev)
Definition: events.h:296
JoystickState joystick
Definition: events.h:239
Definition: events.h:198
Definition: algorithm.h:29
Definition: rect.h:45
Definition: events.h:53
virtual bool allowMapping() const
Definition: events.h:278
Definition: events.h:287
Definition: keyboard.h:294
EventType type
Definition: events.h:201
Definition: events.h:258
Definition: events.h:73
JoystickButton
Definition: events.h:145
Definition: events.h:471
virtual void notifyPoll()
Definition: events.h:339
Definition: events.h:55
virtual void init()
Definition: events.h:485
byte axis
Definition: events.h:128
virtual bool allowMapping() const
Definition: events.h:309