Ticket #9038: events.patch
File events.patch, 16.2 KB (added by , 15 years ago) |
---|
-
backends/base-backend.h
diff --git a/backends/base-backend.h b/backends/base-backend.h index 697577c..3fcca9c 100644
a b 29 29 #include "common/system.h" 30 30 #include "backends/events/default/default-events.h" 31 31 32 class BaseBackend : public OSystem, EventProvider{32 class BaseBackend : public OSystem, Common::EventSource { 33 33 public: 34 34 virtual Common::EventManager *getEventManager(); 35 35 virtual void displayMessageOnOSD(const char *msg); -
backends/events/default/default-events.cpp
diff --git a/backends/events/default/default-events.cpp b/backends/events/default/default-events.cpp index 4fdf96e..0e32ff9 100644
a b void writeRecord(Common::OutSaveFile *outFile, uint32 diff, Common::Event &event 92 92 } 93 93 } 94 94 95 DefaultEventManager::DefaultEventManager(EventProvider *boss) : 96 _boss(boss), 95 DefaultEventManager::DefaultEventManager(Common::EventSource *boss) : 97 96 _buttonState(0), 98 97 _modifierState(0), 99 98 _shouldQuit(false), 100 99 _shouldRTL(false), 101 100 _confirmExitDialogActive(false) { 102 101 103 assert(_boss); 102 assert(boss); 103 104 _dispatcher.registerSource(boss, false); 105 _dispatcher.registerSource(&_artificialEventSource, false); 106 107 _dispatcher.registerObserver(this, kEventManPriority, false); 104 108 105 109 _recordFile = NULL; 106 110 _recordTimeFile = NULL; … … DefaultEventManager::DefaultEventManager(EventProvider *boss) : 202 206 #endif 203 207 #ifdef ENABLE_KEYMAPPER 204 208 _keymapper = new Common::Keymapper(this); 209 // EventDispatcher will automatically free the keymapper 210 _dispatcher.registerMapper(_keymapper); 205 211 _remap = false; 206 212 #endif 207 213 } 208 214 209 215 DefaultEventManager::~DefaultEventManager() { 210 #ifdef ENABLE_KEYMAPPER211 delete _keymapper;212 #endif213 216 #ifdef ENABLE_VKEYBD 214 217 delete _vk; 215 218 #endif … … DefaultEventManager::~DefaultEventManager() { 219 222 g_system->unlockMutex(_timeMutex); 220 223 g_system->unlockMutex(_recorderMutex); 221 224 222 if (!artificialEventQueue.empty())223 artificialEventQueue.clear();224 225 225 if (_playbackFile != NULL) { 226 226 delete _playbackFile; 227 227 } … … void DefaultEventManager::processMillis(uint32 &millis) { 381 381 382 382 bool DefaultEventManager::pollEvent(Common::Event &event) { 383 383 uint32 time = g_system->getMillis(); 384 bool result ;384 bool result = false; 385 385 386 if (!artificialEventQueue.empty()) { 387 event = artificialEventQueue.pop(); 386 _dispatcher.dispatch(); 387 if (!_eventQueue.empty()) { 388 event = _eventQueue.pop(); 388 389 result = true; 389 } else {390 result = _boss->pollEvent(event);391 392 #ifdef ENABLE_KEYMAPPER393 if (result) {394 // send key press events to keymapper395 if (event.type == Common::EVENT_KEYDOWN) {396 if (_keymapper->mapKeyDown(event.kbd)) {397 result = false;398 }399 } else if (event.type == Common::EVENT_KEYUP) {400 if (_keymapper->mapKeyUp(event.kbd)) {401 result = false;402 }403 }404 }405 #endif406 390 } 407 391 408 392 if (_recordMode != kPassthrough) { … … bool DefaultEventManager::pollEvent(Common::Event &event) { 598 582 } 599 583 600 584 void DefaultEventManager::pushEvent(const Common::Event &event) { 601 602 585 // If already received an EVENT_QUIT, don't add another one 603 586 if (event.type == Common::EVENT_QUIT) { 604 587 if (!_shouldQuit) 605 artificialEventQueue.push(event);588 _artificialEventSource.addEvent(event); 606 589 } else 607 artificialEventQueue.push(event);590 _artificialEventSource.addEvent(event); 608 591 } 609 592 610 593 #endif // !defined(DISABLE_DEFAULT_EVENTMANAGER) -
backends/events/default/default-events.h
diff --git a/backends/events/default/default-events.h b/backends/events/default/default-events.h index 5841318..963706a 100644
a b namespace Common { 41 41 } 42 42 43 43 44 class EventProvider { 45 public: 46 virtual ~EventProvider() {} 47 /** 48 * Get the next event in the event queue. 49 * @param event point to an Common::Event struct, which will be filled with the event data. 50 * @return true if an event was retrieved. 51 */ 52 virtual bool pollEvent(Common::Event &event) = 0; 53 }; 54 55 56 class DefaultEventManager : public Common::EventManager { 57 EventProvider *_boss; 58 44 class DefaultEventManager : public Common::EventManager, Common::EventObserver { 59 45 #ifdef ENABLE_VKEYBD 60 46 Common::VirtualKeyboard *_vk; 61 47 #endif … … class DefaultEventManager : public Common::EventManager { 65 51 bool _remap; 66 52 #endif 67 53 68 Common::Queue<Common::Event> _artificialEventQueue; 54 Common::ArtificialEventSource _artificialEventSource; 55 56 Common::Queue<Common::Event> _eventQueue; 57 bool notifyEvent(const Common::Event &ev) { 58 _eventQueue.push(ev); 59 return true; 60 } 69 61 70 62 Common::Point _mousePos; 71 63 int _buttonState; … … class DefaultEventManager : public Common::EventManager { 128 120 void record(Common::Event &event); 129 121 bool playback(Common::Event &event); 130 122 public: 131 DefaultEventManager( EventProvider*boss);123 DefaultEventManager(Common::EventSource *boss); 132 124 ~DefaultEventManager(); 133 125 134 126 virtual void init(); -
backends/keymapper/keymapper.cpp
diff --git a/backends/keymapper/keymapper.cpp b/backends/keymapper/keymapper.cpp index 7d88882..c0c4541 100644
a b void Keymapper::popKeymap() { 168 168 _activeMaps.pop(); 169 169 } 170 170 171 bool Keymapper::notifyEvent(const Common::Event &ev) { 172 if (ev.type == Common::EVENT_KEYDOWN) 173 return mapKeyDown(ev.kbd); 174 else if (ev.type == Common::EVENT_KEYUP) 175 return mapKeyUp(ev.kbd); 176 else 177 return false; 178 } 179 171 180 bool Keymapper::mapKeyDown(const KeyState& key) { 172 181 return mapKey(key, true); 173 182 } … … void Keymapper::executeAction(const Action *action, bool keyDown) { 255 264 } 256 265 257 266 evt.mouse = _eventMan->getMousePos(); 258 _eventMan->pushEvent(evt);267 addEvent(evt); 259 268 } 260 269 } 261 270 -
backends/keymapper/keymapper.h
diff --git a/backends/keymapper/keymapper.h b/backends/keymapper/keymapper.h index c82f64b..f492882 100644
a b 39 39 40 40 namespace Common { 41 41 42 class Keymapper {42 class Keymapper : public Common::EventMapper, private Common::ArtificialEventSource { 43 43 public: 44 44 45 45 struct MapRecord { … … public: 134 134 */ 135 135 void popKeymap(); 136 136 137 // Implementation of the EventMapper interface 138 bool notifyEvent(const Common::Event &ev); 139 bool pollEvent(Common::Event &ev) { return Common::ArtificialEventSource::pollEvent(ev); } 140 137 141 /** 138 142 * @brief Map a key press event. 139 143 * If the active keymap contains a Action mapped to the given key, then -
new file common/events.cpp
diff --git a/common/events.cpp b/common/events.cpp new file mode 100644 index 0000000..0fab55c
- + 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 8 * modify it under the terms of the GNU General Public License 9 * as published by the Free Software Foundation; either version 2 10 * of the License, or (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, write to the Free Software 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 20 * 21 * $URL$ 22 * $Id$ 23 * 24 */ 25 26 #include "common/events.h" 27 28 namespace Common { 29 30 EventDispatcher::EventDispatcher() : _mapper(0) { 31 } 32 33 EventDispatcher::~EventDispatcher() { 34 for (Common::List<SourceEntry>::iterator i = _sources.begin(); i != _sources.end(); ++i) { 35 if (i->autoFree) 36 delete i->source; 37 } 38 39 for (Common::List<ObserverEntry>::iterator i = _observers.begin(); i != _observers.end(); ++i) { 40 if (i->autoFree) 41 delete i->observer; 42 } 43 44 delete _mapper; 45 _mapper = 0; 46 } 47 48 void EventDispatcher::dispatch() { 49 Common::Event event; 50 51 for (Common::List<SourceEntry>::iterator i = _sources.begin(); i != _sources.end(); ++i) { 52 const bool allowMapping = i->source->allowMapping(); 53 54 while (i->source->pollEvent(event)) { 55 // We only try to process the events via the setup event mapper, when 56 // we have a setup mapper and when the event source allows mapping. 57 if (_mapper && allowMapping) { 58 if (_mapper->notifyEvent(event)) { 59 // We allow the event mapper to create multiple events, when 60 // eating an event. 61 while (_mapper->pollEvent(event)) 62 dispatchEvent(event); 63 64 // Try getting another event from the current EventSource. 65 continue; 66 } 67 } 68 69 dispatchEvent(event); 70 } 71 } 72 } 73 74 void EventDispatcher::registerMapper(EventMapper *mapper) { 75 if (_mapper) 76 delete _mapper; 77 _mapper = mapper; 78 } 79 80 void EventDispatcher::registerSource(EventSource *source, bool autoFree) { 81 SourceEntry newEntry; 82 83 newEntry.source = source; 84 newEntry.autoFree = autoFree; 85 86 _sources.push_back(newEntry); 87 } 88 89 void EventDispatcher::unregisterSource(EventSource *source) { 90 for (Common::List<SourceEntry>::iterator i = _sources.begin(); i != _sources.end(); ++i) { 91 if (i->source == source) { 92 if (i->autoFree) 93 delete source; 94 95 _sources.erase(i); 96 return; 97 } 98 } 99 } 100 101 void EventDispatcher::registerObserver(EventObserver *obs, uint priority, bool autoFree) { 102 ObserverEntry newEntry; 103 104 newEntry.observer = obs; 105 newEntry.priority = priority; 106 newEntry.autoFree = autoFree; 107 108 for (Common::List<ObserverEntry>::iterator i = _observers.begin(); i != _observers.end(); ++i) { 109 if (i->priority < priority) { 110 _observers.insert(i, newEntry); 111 return; 112 } 113 } 114 115 _observers.push_back(newEntry); 116 } 117 118 void EventDispatcher::unregisterObserver(EventObserver *obs) { 119 for (Common::List<ObserverEntry>::iterator i = _observers.begin(); i != _observers.end(); ++i) { 120 if (i->observer == obs) { 121 if (i->autoFree) 122 delete obs; 123 124 _observers.erase(i); 125 return; 126 } 127 } 128 } 129 130 void EventDispatcher::dispatchEvent(const Event &event) { 131 for (Common::List<ObserverEntry>::iterator i = _observers.begin(); i != _observers.end(); ++i) { 132 if (i->observer->notifyEvent(event)) 133 break; 134 } 135 } 136 137 } // end of namespace Common 138 -
common/events.h
diff --git a/common/events.h b/common/events.h index e13d95c..f4f5ffd 100644
a b 31 31 #include "common/rect.h" 32 32 #include "common/noncopyable.h" 33 33 34 #include "common/list.h" 35 #include "common/singleton.h" 36 34 37 namespace Common { 35 38 36 39 /** … … struct Event { 126 129 Event() : type(EVENT_INVALID), synthetic(false) {} 127 130 }; 128 131 132 /** 133 * A source of Events. 134 * 135 * An example for this is OSystem, it provides events created by the system 136 * and or user. 137 */ 138 class EventSource { 139 public: 140 virtual ~EventSource() {} 141 142 /** 143 * Queries a event from the source. 144 * 145 * @param event a reference to the event struct, where the event should be stored. 146 * @return true if an event was polled, false otherwise. 147 */ 148 virtual bool pollEvent(Event &event) = 0; 149 150 /** 151 * Checks whether events from this source are allowed to be mapped. 152 * 153 * Possible event sources not allowing mapping are: the event recorder/player and/or 154 * the EventManager, which allows user events to be pushed. 155 * 156 * By default we allow mapping for every event source. 157 */ 158 virtual bool allowMapping() const { return true; } 159 }; 160 161 /** 162 * An artificial event source. This is class is used as an event source, which is 163 * made up by client specific events. 164 * 165 * Example usage cases for this are the Keymapper or the DefaultEventManager. 166 */ 167 class ArtificialEventSource : public EventSource { 168 protected: 169 Common::Queue<Common::Event> _artificialEventQueue; 170 public: 171 void addEvent(const Common::Event &ev) { 172 _artificialEventQueue.push(ev); 173 } 174 175 bool pollEvent(Common::Event &ev) { 176 if (!_artificialEventQueue.empty()) { 177 ev = _artificialEventQueue.pop(); 178 return true; 179 } else { 180 return false; 181 } 182 } 183 184 /** 185 * By default an artificial event source prevents its events 186 * from being mapped. 187 */ 188 virtual bool allowMapping() const { return false; } 189 }; 190 191 /** 192 * Object which catches and processes Events. 193 * 194 * An example for this is the Engine object, it is catching events and processing them. 195 */ 196 class EventObserver { 197 public: 198 virtual ~EventObserver() {} 199 200 /** 201 * Notifies the source of an incoming event. 202 * 203 * An obeser is supposed to eat the event, with returning true, when 204 * it might want prevent other observers from preventing to receive 205 * the event. An usage example here is the keymapper: 206 * If it processes an Event, it should 'eat' it and create a new 207 * event, which the EventDispatcher will then catch. 208 * 209 * @param event the event, which is incoming. 210 * @return true if this observer uses this event, false otherwise. 211 */ 212 virtual bool notifyEvent(const Event &event) = 0; 213 }; 214 215 /** 216 * A event mapper, which will map events to others. 217 * 218 * An example for this is the Keymapper. 219 */ 220 class EventMapper : public EventSource, public EventObserver { 221 public: 222 /** For event mappers resulting events should never be mapped */ 223 bool allowMapping() const { return false; } 224 }; 225 226 /** 227 * Dispatches events from various sources to various observers. 228 * 229 * EventDispatcher is using a priority based approach. Observers 230 * with higher priority will be notified before observers with 231 * lower priority. Because of the possibility that oberservers 232 * might 'eat' events, not all observers might be notified. 233 * 234 * Another speciality is the support for a event mapper, which 235 * will catch events and create new events out of them. This 236 * mapper will be processed before an event is sent to the 237 * observers. 238 */ 239 class EventDispatcher { 240 public: 241 EventDispatcher(); 242 ~EventDispatcher(); 243 244 /** 245 * Tries to catch events from the registered event 246 * sources and dispatch them to the observers. 247 * 248 * This dispatches *all* events the sources offer. 249 */ 250 void dispatch(); 251 252 /** 253 * Registers an event mapper with the dispatcher. 254 * 255 * The ownership of the "mapper" variable will pass 256 * to the EventDispatcher, thus it will be deleted 257 * with "delete", when EventDispatcher is destroyed. 258 * 259 * Note there is only one mapper per EventDispatcher 260 * possible, thus when this method is called twice, 261 * the former mapper will be destroied. 262 */ 263 void registerMapper(EventMapper *mapper); 264 265 /** 266 * Queries the setup event mapper. 267 */ 268 EventMapper *queryMapper() const { return _mapper; } 269 270 /** 271 * Registers a new EventSource with the Dispatcher. 272 */ 273 void registerSource(EventSource *source, bool autoFree); 274 275 /** 276 * Unregisters a EventSource. 277 * 278 * This takes the "autoFree" flag passed to registerSource into account. 279 */ 280 void unregisterSource(EventSource *source); 281 282 /** 283 * Registers a new EventObserver with the Dispatcher. 284 */ 285 void registerObserver(EventObserver *obs, uint priority, bool autoFree); 286 287 /** 288 * Unregisters a EventObserver. 289 * 290 * This takes the "autoFree" flag passed to registerObserver into account. 291 */ 292 void unregisterObserver(EventObserver *obs); 293 private: 294 EventMapper *_mapper; 295 296 struct Entry { 297 bool autoFree; 298 }; 299 300 struct SourceEntry : public Entry { 301 EventSource *source; 302 }; 303 304 Common::List<SourceEntry> _sources; 305 306 struct ObserverEntry : public Entry { 307 uint priority; 308 EventObserver *observer; 309 }; 310 311 Common::List<ObserverEntry> _observers; 312 313 void dispatchEvent(const Event &event); 314 }; 315 129 316 class Keymapper; 130 317 131 318 /** … … public: 207 394 virtual Common::Keymapper *getKeymapper() = 0; 208 395 #endif 209 396 210 protected: 397 enum { 398 /** 399 * Priority of the event manager, for now it's lowest since it eats 400 * *all* events, we might to change that in the future though. 401 */ 402 kEventManPriority = 0 403 }; 211 404 212 Common::Queue<Common::Event> artificialEventQueue; 405 /** 406 * Returns the underlying EventDispatcher. 407 */ 408 EventDispatcher *getEventDispatcher() { return &_dispatcher; } 409 410 protected: 411 EventDispatcher _dispatcher; 213 412 }; 214 413 215 414 } // End of namespace Common -
common/module.mk
diff --git a/common/module.mk b/common/module.mk index bdf9590..798fe4f 100644
a b MODULE_OBJS := \ 5 5 config-file.o \ 6 6 config-manager.o \ 7 7 debug.o \ 8 events.o \ 8 9 file.o \ 9 10 fs.o \ 10 11 hashmap.o \