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 
129  EVENT_USER_LAST_AVAILABLE = 9999
130 };
131 
132 const int16 JOYAXIS_MIN = -32768;
133 const int16 JOYAXIS_MAX = 32767;
134 
140  byte axis;
142  int16 position;
149  uint8 button;
150 
151  JoystickState() : axis(0), position(0), button(0) {}
152 };
153 
158  JOYSTICK_BUTTON_INVALID,
159  JOYSTICK_BUTTON_A,
160  JOYSTICK_BUTTON_B,
161  JOYSTICK_BUTTON_X,
162  JOYSTICK_BUTTON_Y,
163  JOYSTICK_BUTTON_BACK,
164  JOYSTICK_BUTTON_GUIDE,
165  JOYSTICK_BUTTON_START,
166  JOYSTICK_BUTTON_LEFT_STICK,
167  JOYSTICK_BUTTON_RIGHT_STICK,
168  JOYSTICK_BUTTON_LEFT_SHOULDER,
169  JOYSTICK_BUTTON_RIGHT_SHOULDER,
170  JOYSTICK_BUTTON_DPAD_UP,
171  JOYSTICK_BUTTON_DPAD_DOWN,
172  JOYSTICK_BUTTON_DPAD_LEFT,
173  JOYSTICK_BUTTON_DPAD_RIGHT,
174  JOYSTICK_BUTTON_DPAD_CENTER
175 };
176 
181  JOYSTICK_AXIS_LEFT_STICK_X,
182  JOYSTICK_AXIS_LEFT_STICK_Y,
183  JOYSTICK_AXIS_RIGHT_STICK_X,
184  JOYSTICK_AXIS_RIGHT_STICK_Y,
185  JOYSTICK_AXIS_LEFT_TRIGGER,
186  JOYSTICK_AXIS_RIGHT_TRIGGER,
187  JOYSTICK_AXIS_HAT_X,
188  JOYSTICK_AXIS_HAT_Y
189 };
190 
195  MOUSE_BUTTON_LEFT = 0,
196  MOUSE_BUTTON_RIGHT = 1,
197  MOUSE_BUTTON_MIDDLE = 2,
198  MOUSE_WHEEL_UP = 3,
199  MOUSE_WHEEL_DOWN = 4,
200  MOUSE_BUTTON_X1 = 5,
201  MOUSE_BUTTON_X2 = 6
202 };
204 typedef uint32 CustomEventType;
205 
210 struct Event {
211 
214 
220  bool kbdRepeat;
221 
227 
235 
237  CustomEventType customType;
238 
241 
246 
252 
253  Event() : type(EVENT_INVALID), kbdRepeat(false), customType(0) {
254  }
255 };
256 
262 bool isMouseEvent(const Event &event);
263 
270 class EventSource {
271 public:
272  virtual ~EventSource();
273 
280  virtual bool pollEvent(Event &event) = 0;
281 
290  virtual bool allowMapping() const { return true; }
291 };
292 
300 protected:
301  Queue<Event> _artificialEventQueue;
302 public:
304  void addEvent(const Event &ev) {
305  _artificialEventQueue.push(ev);
306  }
307 
308  bool pollEvent(Event &ev) {
309  if (!_artificialEventQueue.empty()) {
310  ev = _artificialEventQueue.pop();
311  return true;
312  } else {
313  return false;
314  }
315  }
316 
321  virtual bool allowMapping() const { return false; }
322 };
323 
330 public:
331  virtual ~EventObserver();
332 
346  virtual bool notifyEvent(const Event &event) = 0;
347 
351  virtual void notifyPoll() { }
352 };
353 
359 class EventMapper {
360 public:
361  virtual ~EventMapper();
362 
366  virtual bool mapEvent(const Event &ev, List<Event> &mappedEvents) = 0;
367 };
368 
383 public:
384  EventDispatcher();
385  ~EventDispatcher();
386 
393  void dispatch();
394 
399  void clearEvents();
400 
404  void registerMapper(EventMapper *mapper, bool autoFree);
405 
411  void unregisterMapper(EventMapper *mapper);
412 
416  void registerSource(EventSource *source, bool autoFree);
417 
423  void unregisterSource(EventSource *source);
424 
429  void ignoreSources(bool ignore);
430 
437  void registerObserver(EventObserver *obs, uint priority, bool autoFree, bool listenPolls = false);
438 
444  void unregisterObserver(EventObserver *obs);
445 private:
446  struct Entry {
447  bool autoFree;
448  bool ignore;
449  };
450 
451  struct MapperEntry : public Entry {
452  EventMapper *mapper;
453  };
454 
455  List<MapperEntry> _mappers;
456 
457  struct SourceEntry : public Entry {
458  EventSource *source;
459  };
460 
461  List<SourceEntry> _sources;
462 
463  struct ObserverEntry : public Entry {
464  uint priority;
465  EventObserver *observer;
466  bool poll;
467  };
468 
469  List<ObserverEntry> _observers;
470 
471  void dispatchEvent(const Event &event);
472  void dispatchPoll();
473 };
474 
475 class Keymap;
476 class Keymapper;
477 
484 public:
485  virtual ~EventManager();
486 
487  enum {
488  LBUTTON = 1 << MOUSE_BUTTON_LEFT,
489  RBUTTON = 1 << MOUSE_BUTTON_RIGHT
490  };
491 
492 
497  virtual void init() {}
498 
504  virtual bool pollEvent(Event &event) = 0;
505 
509  virtual void pushEvent(const Event &event) = 0;
510 
514  virtual void purgeMouseEvents() = 0;
515 
519  virtual void purgeKeyboardEvents() = 0;
520 
522  virtual Point getMousePos() const = 0;
523 
529  virtual int getButtonState() const = 0;
530 
532  virtual int getModifierState() const = 0;
533 
537  virtual int shouldQuit() const = 0;
538 
542  virtual int shouldReturnToLauncher() const = 0;
543 
548  virtual void resetReturnToLauncher() = 0;
549  virtual void resetQuit() = 0;
550  // Optional: check whether a given key is currently pressed ????
551  //virtual bool isKeyPressed(int keycode) = 0;
552 
553  // TODO: Keyboard repeat support?
554 
555  // TODO: Consider removing OSystem::getScreenChangeID and
556  // replacing it by a generic getScreenChangeID method here
558  virtual Keymapper *getKeymapper() = 0;
560  virtual Keymap *getGlobalKeymap() = 0;
561 
562  enum {
567  kEventManPriority = 0,
572  kEventRecorderPriority = 1,
577  kEventRemapperPriority = 999
578  };
579 
583  EventDispatcher *getEventDispatcher() { return &_dispatcher; }
584 
585 protected:
586  EventDispatcher _dispatcher;
587 };
588 
596 
599 } // End of namespace Common
600 
601 #endif
Definition: keymap.h:66
bool isMouseEvent(const Event &event)
Common::Path path
Definition: events.h:240
void addEvent(const Event &ev)
Definition: events.h:304
Definition: events.h:52
uint8 button
Definition: events.h:149
bool kbdRepeat
Definition: events.h:220
Common::Point relMouse
Definition: events.h:245
CustomEventType customType
Definition: events.h:237
EventType
Definition: events.h:49
Definition: list.h:44
Definition: events.h:382
Definition: events.h:84
EventSource * makeKeyboardRepeatingEventSource(EventSource *eventSource)
Definition: path.h:52
uint32 CustomEventType
Definition: events.h:204
Definition: events.h:138
int16 position
Definition: events.h:142
EventDispatcher * getEventDispatcher()
Definition: events.h:583
Point mouse
Definition: events.h:234
Definition: queue.h:42
Definition: events.h:359
Definition: keymapper.h:44
JoystickAxis
Definition: events.h:180
Definition: noncopyable.h:39
MouseButton
Definition: events.h:194
Definition: events.h:111
Definition: events.h:117
Definition: events.h:329
KeyState kbd
Definition: events.h:226
bool pollEvent(Event &ev)
Definition: events.h:308
JoystickState joystick
Definition: events.h:251
Definition: events.h:210
Definition: algorithm.h:29
Definition: rect.h:144
Definition: events.h:54
virtual bool allowMapping() const
Definition: events.h:290
Definition: events.h:299
Definition: keyboard.h:294
EventType type
Definition: events.h:213
Definition: events.h:270
Definition: events.h:74
JoystickButton
Definition: events.h:157
Definition: events.h:483
virtual void notifyPoll()
Definition: events.h:351
Definition: events.h:56
virtual void init()
Definition: events.h:497
Definition: events.h:128
byte axis
Definition: events.h:140
virtual bool allowMapping() const
Definition: events.h:321