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/path.h"
27 #include "common/queue.h"
28 #include "common/rect.h"
29 #include "common/noncopyable.h"
30 
31 #include "common/list.h"
32 #include "common/singleton.h"
33 
34 namespace Common {
35 
49 enum EventType {
50  EVENT_INVALID = 0,
57  EVENT_LBUTTONDOWN = 4,
58  EVENT_LBUTTONUP = 5,
59  EVENT_RBUTTONDOWN = 6,
60  EVENT_RBUTTONUP = 7,
61  EVENT_WHEELUP = 8,
62  EVENT_WHEELDOWN = 9,
63  EVENT_MBUTTONDOWN = 13,
64  EVENT_MBUTTONUP = 14,
65 
66  EVENT_MAINMENU = 15,
67  EVENT_RETURN_TO_LAUNCHER = 16,
68  EVENT_MUTE = 17,
69 
70  EVENT_QUIT = 10,
71  EVENT_SCREEN_CHANGED = 11,
72 
75 
85 
86  EVENT_CUSTOM_BACKEND_ACTION_START = 18,
87  EVENT_CUSTOM_BACKEND_ACTION_END = 19,
88  EVENT_CUSTOM_BACKEND_ACTION_AXIS = 34,
89  EVENT_CUSTOM_ENGINE_ACTION_START = 20,
90  EVENT_CUSTOM_ENGINE_ACTION_END = 21,
91 
92  EVENT_VIRTUAL_KEYBOARD = 22,
93 
94  EVENT_DROP_FILE = 23,
95 
96  EVENT_JOYAXIS_MOTION = 24,
97  EVENT_JOYBUTTON_DOWN = 25,
98  EVENT_JOYBUTTON_UP = 26,
99 
100  EVENT_CLIPBOARD_UPDATE = 27,
101 
102  EVENT_CUSTOM_BACKEND_HARDWARE = 28,
103  EVENT_DEBUGGER = 29,
104 
112  EVENT_X1BUTTONUP = 31,
113  EVENT_X2BUTTONDOWN = 32,
114  EVENT_X2BUTTONUP = 33,
115 
118  EVENT_FOCUS_LOST = 37
119 };
120 
121 const int16 JOYAXIS_MIN = -32768;
122 const int16 JOYAXIS_MAX = 32767;
123 
129  byte axis;
131  int16 position;
138  uint8 button;
139 
140  JoystickState() : axis(0), position(0), button(0) {}
141 };
142 
147  JOYSTICK_BUTTON_INVALID,
148  JOYSTICK_BUTTON_A,
149  JOYSTICK_BUTTON_B,
150  JOYSTICK_BUTTON_X,
151  JOYSTICK_BUTTON_Y,
152  JOYSTICK_BUTTON_BACK,
153  JOYSTICK_BUTTON_GUIDE,
154  JOYSTICK_BUTTON_START,
155  JOYSTICK_BUTTON_LEFT_STICK,
156  JOYSTICK_BUTTON_RIGHT_STICK,
157  JOYSTICK_BUTTON_LEFT_SHOULDER,
158  JOYSTICK_BUTTON_RIGHT_SHOULDER,
159  JOYSTICK_BUTTON_DPAD_UP,
160  JOYSTICK_BUTTON_DPAD_DOWN,
161  JOYSTICK_BUTTON_DPAD_LEFT,
162  JOYSTICK_BUTTON_DPAD_RIGHT,
163  JOYSTICK_BUTTON_DPAD_CENTER
164 };
165 
170  JOYSTICK_AXIS_LEFT_STICK_X,
171  JOYSTICK_AXIS_LEFT_STICK_Y,
172  JOYSTICK_AXIS_RIGHT_STICK_X,
173  JOYSTICK_AXIS_RIGHT_STICK_Y,
174  JOYSTICK_AXIS_LEFT_TRIGGER,
175  JOYSTICK_AXIS_RIGHT_TRIGGER,
176  JOYSTICK_AXIS_HAT_X,
177  JOYSTICK_AXIS_HAT_Y
178 };
179 
184  MOUSE_BUTTON_LEFT = 0,
185  MOUSE_BUTTON_RIGHT = 1,
186  MOUSE_BUTTON_MIDDLE = 2,
187  MOUSE_WHEEL_UP = 3,
188  MOUSE_WHEEL_DOWN = 4,
189  MOUSE_BUTTON_X1 = 5,
190  MOUSE_BUTTON_X2 = 6
191 };
193 typedef uint32 CustomEventType;
194 
199 struct Event {
200 
203 
209  bool kbdRepeat;
210 
216 
224 
226  CustomEventType customType;
227 
230 
235 
241 
242  Event() : type(EVENT_INVALID), kbdRepeat(false), customType(0) {
243  }
244 };
245 
251 bool isMouseEvent(const Event &event);
252 
259 class EventSource {
260 public:
261  virtual ~EventSource();
262 
269  virtual bool pollEvent(Event &event) = 0;
270 
279  virtual bool allowMapping() const { return true; }
280 };
281 
289 protected:
290  Queue<Event> _artificialEventQueue;
291 public:
293  void addEvent(const Event &ev) {
294  _artificialEventQueue.push(ev);
295  }
296 
297  bool pollEvent(Event &ev) {
298  if (!_artificialEventQueue.empty()) {
299  ev = _artificialEventQueue.pop();
300  return true;
301  } else {
302  return false;
303  }
304  }
305 
310  virtual bool allowMapping() const { return false; }
311 };
312 
319 public:
320  virtual ~EventObserver();
321 
335  virtual bool notifyEvent(const Event &event) = 0;
336 
340  virtual void notifyPoll() { }
341 };
342 
348 class EventMapper {
349 public:
350  virtual ~EventMapper();
351 
355  virtual bool mapEvent(const Event &ev, List<Event> &mappedEvents) = 0;
356 };
357 
372 public:
373  EventDispatcher();
374  ~EventDispatcher();
375 
382  void dispatch();
383 
388  void clearEvents();
389 
393  void registerMapper(EventMapper *mapper, bool autoFree);
394 
400  void unregisterMapper(EventMapper *mapper);
401 
405  void registerSource(EventSource *source, bool autoFree);
406 
412  void unregisterSource(EventSource *source);
413 
418  void ignoreSources(bool ignore);
419 
426  void registerObserver(EventObserver *obs, uint priority, bool autoFree, bool listenPolls = false);
427 
433  void unregisterObserver(EventObserver *obs);
434 private:
435  struct Entry {
436  bool autoFree;
437  bool ignore;
438  };
439 
440  struct MapperEntry : public Entry {
441  EventMapper *mapper;
442  };
443 
444  List<MapperEntry> _mappers;
445 
446  struct SourceEntry : public Entry {
447  EventSource *source;
448  };
449 
450  List<SourceEntry> _sources;
451 
452  struct ObserverEntry : public Entry {
453  uint priority;
454  EventObserver *observer;
455  bool poll;
456  };
457 
458  List<ObserverEntry> _observers;
459 
460  void dispatchEvent(const Event &event);
461  void dispatchPoll();
462 };
463 
464 class Keymap;
465 class Keymapper;
466 
473 public:
474  virtual ~EventManager();
475 
476  enum {
477  LBUTTON = 1 << MOUSE_BUTTON_LEFT,
478  RBUTTON = 1 << MOUSE_BUTTON_RIGHT
479  };
480 
481 
486  virtual void init() {}
487 
493  virtual bool pollEvent(Event &event) = 0;
494 
498  virtual void pushEvent(const Event &event) = 0;
499 
503  virtual void purgeMouseEvents() = 0;
504 
508  virtual void purgeKeyboardEvents() = 0;
509 
511  virtual Point getMousePos() const = 0;
512 
518  virtual int getButtonState() const = 0;
519 
521  virtual int getModifierState() const = 0;
522 
526  virtual int shouldQuit() const = 0;
527 
531  virtual int shouldReturnToLauncher() const = 0;
532 
537  virtual void resetReturnToLauncher() = 0;
538  virtual void resetQuit() = 0;
539  // Optional: check whether a given key is currently pressed ????
540  //virtual bool isKeyPressed(int keycode) = 0;
541 
542  // TODO: Keyboard repeat support?
543 
544  // TODO: Consider removing OSystem::getScreenChangeID and
545  // replacing it by a generic getScreenChangeID method here
547  virtual Keymapper *getKeymapper() = 0;
549  virtual Keymap *getGlobalKeymap() = 0;
550 
551  enum {
556  kEventManPriority = 0,
561  kEventRecorderPriority = 1,
566  kEventRemapperPriority = 999
567  };
568 
572  EventDispatcher *getEventDispatcher() { return &_dispatcher; }
573 
574 protected:
575  EventDispatcher _dispatcher;
576 };
577 
585 
588 } // End of namespace Common
589 
590 #endif
Definition: keymap.h:66
bool isMouseEvent(const Event &event)
Common::Path path
Definition: events.h:229
void addEvent(const Event &ev)
Definition: events.h:293
Definition: events.h:52
uint8 button
Definition: events.h:138
bool kbdRepeat
Definition: events.h:209
Common::Point relMouse
Definition: events.h:234
CustomEventType customType
Definition: events.h:226
EventType
Definition: events.h:49
Definition: list.h:44
Definition: events.h:371
Definition: events.h:84
EventSource * makeKeyboardRepeatingEventSource(EventSource *eventSource)
Definition: path.h:52
uint32 CustomEventType
Definition: events.h:193
Definition: events.h:127
int16 position
Definition: events.h:131
EventDispatcher * getEventDispatcher()
Definition: events.h:572
Point mouse
Definition: events.h:223
Definition: queue.h:42
Definition: events.h:348
Definition: keymapper.h:44
JoystickAxis
Definition: events.h:169
Definition: noncopyable.h:39
MouseButton
Definition: events.h:183
Definition: events.h:111
Definition: events.h:117
Definition: events.h:318
KeyState kbd
Definition: events.h:215
bool pollEvent(Event &ev)
Definition: events.h:297
JoystickState joystick
Definition: events.h:240
Definition: events.h:199
Definition: algorithm.h:29
Definition: rect.h:45
Definition: events.h:54
virtual bool allowMapping() const
Definition: events.h:279
Definition: events.h:288
Definition: keyboard.h:294
EventType type
Definition: events.h:202
Definition: events.h:259
Definition: events.h:74
JoystickButton
Definition: events.h:146
Definition: events.h:472
virtual void notifyPoll()
Definition: events.h:340
Definition: events.h:56
virtual void init()
Definition: events.h:486
byte axis
Definition: events.h:129
virtual bool allowMapping() const
Definition: events.h:310