ScummVM API documentation
sdl-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 BACKEND_EVENTS_SDL_H
23 #define BACKEND_EVENTS_SDL_H
24 
25 #include "backends/platform/sdl/sdl-sys.h"
26 #include "backends/graphics/sdl/sdl-graphics.h"
27 
28 #include "common/events.h"
29 
30 // Type names which changed between SDL 1.2 and SDL 2.
31 #if !SDL_VERSION_ATLEAST(2, 0, 0)
32 typedef SDLKey SDL_Keycode;
33 typedef SDLMod SDL_Keymod;
34 typedef SDL_keysym SDL_Keysym;
35 #endif
36 
41 public:
43  virtual ~SdlEventSource();
44 
45  void setGraphicsManager(SdlGraphicsManager *gMan) { _graphicsManager = gMan; }
46 
50  virtual bool pollEvent(Common::Event &event);
51 
56  void fakeWarpMouse(const int x, const int y);
57 
59  bool isJoystickConnected() const;
60 
62  void setEngineRunning(bool value);
63 
64 protected:
67 
68  bool _engineRunning;
69 
70  int _mouseX;
71  int _mouseY;
72 
74  SDL_Joystick *_joystick;
75 
76 #if SDL_VERSION_ATLEAST(3, 0, 0)
77 
78  SDL_Gamepad *_controller;
79 #elif SDL_VERSION_ATLEAST(2, 0, 0)
80 
81  SDL_GameController *_controller;
82 #endif
83 
86 
91 
96 
105  void openJoystick(int joystickIndex);
106 
110  void closeJoystick();
111 
115  virtual void preprocessEvents(SDL_Event *event) {}
116 
120  virtual bool dispatchSDLEvent(SDL_Event &ev, Common::Event &event);
121 
122 
130 
131  virtual bool handleKeyDown(SDL_Event &ev, Common::Event &event);
132  virtual bool handleKeyUp(SDL_Event &ev, Common::Event &event);
133  virtual bool handleMouseMotion(SDL_Event &ev, Common::Event &event);
134  virtual bool handleMouseButtonDown(SDL_Event &ev, Common::Event &event);
135  virtual bool handleMouseButtonUp(SDL_Event &ev, Common::Event &event);
136  virtual bool handleSysWMEvent(SDL_Event &ev, Common::Event &event);
137  virtual int mapSDLJoystickButtonToOSystem(Uint8 sdlButton);
138  virtual bool handleJoyButtonDown(SDL_Event &ev, Common::Event &event);
139  virtual bool handleJoyButtonUp(SDL_Event &ev, Common::Event &event);
140  virtual bool handleJoyAxisMotion(SDL_Event &ev, Common::Event &event);
141  virtual bool handleJoyHatMotion(SDL_Event &ev, Common::Event &event);
142 
143 #if SDL_VERSION_ATLEAST(2, 0, 0)
144  virtual bool handleJoystickAdded(const SDL_JoyDeviceEvent &device, Common::Event &event);
145  virtual bool handleJoystickRemoved(const SDL_JoyDeviceEvent &device, Common::Event &event);
146  virtual int mapSDLControllerButtonToOSystem(Uint8 sdlButton);
147  virtual bool handleControllerButton(const SDL_Event &ev, Common::Event &event, bool buttonUp);
148  virtual bool handleControllerAxisMotion(const SDL_Event &ev, Common::Event &event);
149 
150  virtual bool isTouchPortTouchpadMode(SDL_TouchID port);
151  virtual bool isTouchPortActive(SDL_TouchID port);
152  virtual Common::Point getTouchscreenSize();
153  virtual void convertTouchXYToGameXY(float touchX, float touchY, int *gameX, int *gameY);
154 #endif
155 
157 
163  virtual bool processMouseEvent(Common::Event &event, int x, int y, int relx = 0, int rely = 0);
164 
169  virtual bool remapKey(SDL_Event &ev, Common::Event &event);
170 
174  virtual int mapKey(SDL_Keycode key, SDL_Keymod mod, Uint16 unicode);
175 
179  virtual void SDLModToOSystemKeyFlags(SDL_Keymod mod, Common::Event &event);
180 
184  Common::KeyCode SDLToOSystemKeycode(const SDL_Keycode key);
185 
189  bool handleResizeEvent(Common::Event &event, int w, int h);
190 
195  uint32 obtainUnicode(const SDL_KeyboardEvent &key);
196 
197 #if !SDL_VERSION_ATLEAST(3, 0, 0)
198 
201  SDL_Keycode obtainKeycode(const SDL_Keysym keySym);
202 #endif
203 
208 
215 
216  uint8 _lastHatPosition;
217 
218 #if SDL_VERSION_ATLEAST(2, 0, 0)
219 
222  bool _queuedFakeKeyUp;
223 
228  Common::Event _fakeKeyUp;
229 
230  enum {
231  MAX_NUM_FINGERS = 3, // number of fingers to track per panel
232  MAX_TAP_TIME = 250, // taps longer than this will not result in mouse click events
233  MAX_TAP_MOTION_DISTANCE = 10, // max distance finger motion in Vita screen pixels to be considered a tap
234  SIMULATED_CLICK_DURATION = 50, // time in ms how long simulated mouse clicks should be
235  FINGER_SUBPIXEL_MULTIPLIER = 16 // multiplier for sub-pixel resolution
236  };
237 
238  struct TouchFinger {
239 #if SDL_VERSION_ATLEAST(3, 0, 0)
240  SDL_FingerID id = 0; // 0: no touch
241 #else
242  int id = -1; // -1: no touch
243 #endif
244  uint32 timeLastDown = 0;
245  int lastX = 0; // last known screen coordinates
246  int lastY = 0; // last known screen coordinates
247  float lastDownX = 0; // SDL touch coordinates when last pressed down
248  float lastDownY = 0; // SDL touch coordinates when last pressed down
249  };
250 
251  enum DraggingType {
252  DRAG_NONE = 0,
253  DRAG_TWO_FINGER,
254  DRAG_THREE_FINGER,
255  };
256 
257  struct TouchPanelState {
258  TouchFinger _finger[MAX_NUM_FINGERS]; // keep track of finger status
259  DraggingType _multiFingerDragging = DRAG_NONE; // keep track whether we are currently drag-and-dropping
260  unsigned int _simulatedClickStartTime[2] = {0, 0}; // initiation time of last simulated left or right click (zero if no click)
261  int _hiresDX = 0; // keep track of slow, sub-pixel, finger motion across multiple frames
262  int _hiresDY = 0;
263  bool _tapMade = false;
264  };
265 
267 
268 private:
269  void preprocessFingerDown(SDL_Event *event);
270  bool preprocessFingerUp(SDL_Event *event, Common::Event *ev);
271  void preprocessFingerMotion(SDL_Event *event);
272  void finishSimulatedMouseClicks(void);
273 #endif
274 };
275 
276 #endif
void setEngineRunning(bool value)
virtual bool dispatchSDLEvent(SDL_Event &ev, Common::Event &event)
void closeJoystick()
uint32 obtainUnicode(const SDL_KeyboardEvent &key)
SdlGraphicsManager * _graphicsManager
Definition: sdl-events.h:90
Definition: sdl-events.h:40
bool _queuedFakeMouseMove
Definition: sdl-events.h:207
Definition: sdl-graphics.h:38
bool _scrollLock
Definition: sdl-events.h:66
void fakeWarpMouse(const int x, const int y)
bool handleResizeEvent(Common::Event &event, int w, int h)
void loadGameControllerMappingFile()
Definition: hashmap.h:85
virtual int mapKey(SDL_Keycode key, SDL_Keymod mod, Uint16 unicode)
void openJoystick(int joystickIndex)
virtual bool pollEvent(Common::Event &event)
bool isJoystickConnected() const
virtual void SDLModToOSystemKeyFlags(SDL_Keymod mod, Common::Event &event)
Definition: events.h:210
virtual void preprocessEvents(SDL_Event *event)
Definition: sdl-events.h:115
Definition: rect.h:144
int _lastScreenID
Definition: sdl-events.h:85
Common::Event _fakeMouseMove
Definition: sdl-events.h:214
virtual bool processMouseEvent(Common::Event &event, int x, int y, int relx=0, int rely=0)
Common::KeyCode SDLToOSystemKeycode(const SDL_Keycode key)
Definition: events.h:270
SDL_Joystick * _joystick
Definition: sdl-events.h:74
virtual bool remapKey(SDL_Event &ev, Common::Event &event)
SDL_Keycode obtainKeycode(const SDL_Keysym keySym)