ScummVM API documentation
keymapper.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_KEYMAPPER_H
23 #define COMMON_KEYMAPPER_H
24 
25 #include "common/scummsys.h"
26 
27 #include "backends/keymapper/keymap.h"
28 
29 #include "common/array.h"
30 #include "common/config-manager.h"
31 #include "common/events.h"
32 
33 namespace Common {
34 
35 const char *const kGuiKeymapName = "gui";
36 const char *const kGlobalKeymapName = "global";
37 
38 struct Action;
39 class DelayedEventSource;
40 struct HardwareInput;
41 class HardwareInputSet;
42 class KeymapperDefaultBindings;
43 
45 public:
46 
47  Keymapper(EventManager *eventMan);
48  ~Keymapper();
49 
50  // EventMapper interface
51  virtual bool mapEvent(const Event &ev, List<Event> &mappedEvents);
52 
58  void registerHardwareInputSet(HardwareInputSet *inputs, KeymapperDefaultBindings *backendDefaultBindings);
59 
67  void addGlobalKeymap(Keymap *keymap);
68 
77  void addGameKeymap(Keymap *keymap);
78 
83  void cleanupGameKeymaps();
84 
90  void setGameKeymapState(const String &id, bool enable);
91 
95  void disableAllGameKeymaps();
96 
102  Keymap *getKeymap(const String &id) const;
103 
107  const KeymapArray &getKeymaps() const { return _keymaps; }
108 
112  void reloadAllMappings();
113 
119  void setEnabledKeymapType(Keymap::KeymapType type);
120  Keymap::KeymapType enabledKeymapType() const { return _enabledKeymapType; }
121 
125  void setEnabled(bool enabled) { _enabled = enabled; }
126 
130  bool isEnabled() const { return _enabled; }
131 
135  void clear();
136 
140  HardwareInput findHardwareInput(const Event &event);
141 
142  void initKeymap(Keymap *keymap, ConfigManager::Domain *domain);
143  void reloadKeymapMappings(Keymap *keymap);
144 
145 private:
146  EventManager *_eventMan;
147  HardwareInputSet *_hardwareInputs;
148  KeymapperDefaultBindings *_backendDefaultBindings;
149  DelayedEventSource *_delayedEventSource;
150 
151  enum IncomingEventType {
152  kIncomingEventIgnored,
153  kIncomingEventStart,
154  kIncomingEventEnd,
155  kIncomingEventInstant
156  };
157 
158  enum {
159  kJoyAxisPressedTreshold = Common::JOYAXIS_MAX / 2,
160  kJoyAxisUnpressedTreshold = Common::JOYAXIS_MAX / 4
161  };
162 
163  bool _enabled;
164  Keymap::KeymapType _enabledKeymapType;
165 
166  KeymapArray _keymaps;
167 
168  bool _joystickAxisPreviouslyPressed[8]; // size should match the number of valid axis entries of defaultJoystickAxes (in hardware-input.cpp)
169 
170  Keymap::KeymapMatch getMappedActions(const Event &event, Keymap::ActionArray &actions, Keymap::KeymapType keymapType) const;
171  Event executeAction(const Action *act, const Event &incomingEvent);
172  EventType convertStartToEnd(EventType eventType);
173  IncomingEventType convertToIncomingEventType(const Event &ev) const;
174 
175  void hardcodedEventMapping(Event ev);
176  void resetInputState();
177 };
178 
183 public:
184  KeymapTypeEnabler(Keymapper *keymapper, Keymap::KeymapType keymapType) :
185  _keymapper(keymapper) {
186  assert(keymapper);
187  _previousKeymapType = keymapper->enabledKeymapType();
188  keymapper->setEnabledKeymapType(keymapType);
189  }
190 
191  ~KeymapTypeEnabler() {
192  _keymapper->setEnabledKeymapType(_previousKeymapType);
193  }
194 
195 private:
196  Keymapper *_keymapper;
197  Keymap::KeymapType _previousKeymapType;
198 };
199 
201 public:
202  // EventSource API
203  bool pollEvent(Event &event) override;
204  bool allowMapping() const override;
205 
209  void scheduleEvent(const Event &ev, uint32 delayMillis);
210 
211 private:
212  struct DelayedEventsEntry {
213  const uint32 timerOffset;
214  const Event event;
215  DelayedEventsEntry(const uint32 offset, const Event ev) : timerOffset(offset), event(ev) { }
216  };
217 
218  Queue<DelayedEventsEntry> _delayedEvents;
219  uint32 _delayedEffectiveTime;
220 };
221 
222 } // End of namespace Common
223 
224 #endif // #ifndef COMMON_KEYMAPPER_H
Definition: keymap.h:66
Definition: str.h:59
Definition: action.h:41
void cleanupGameKeymaps()
Definition: array.h:52
EventType
Definition: events.h:48
Definition: hardware-input.h:54
Definition: list.h:44
void setEnabledKeymapType(Keymap::KeymapType type)
void disableAllGameKeymaps()
Keymap * getKeymap(const String &id) const
Definition: events.h:347
Definition: keymapper.h:44
HardwareInput findHardwareInput(const Event &event)
void setGameKeymapState(const String &id, bool enable)
void addGlobalKeymap(Keymap *keymap)
Definition: keymapper.h:200
const KeymapArray & getKeymaps() const
Definition: keymapper.h:107
void addGameKeymap(Keymap *keymap)
Definition: events.h:198
Definition: hardware-input.h:199
Definition: algorithm.h:29
Definition: keymapper-defaults.h:32
Definition: config-manager.h:59
virtual bool mapEvent(const Event &ev, List< Event > &mappedEvents)
bool isEnabled() const
Definition: keymapper.h:130
Definition: keymapper.h:182
Definition: events.h:258
Definition: events.h:471
void registerHardwareInputSet(HardwareInputSet *inputs, KeymapperDefaultBindings *backendDefaultBindings)
void reloadAllMappings()
void setEnabled(bool enabled)
Definition: keymapper.h:125