Ticket #9038: events.patch

File events.patch, 16.2 KB (added by lordhoto, 15 years ago)

Patch against r42180. (Includes disptacher API, DefaultEventManager cleanup and Keymapper adaption).

  • backends/base-backend.h

    diff --git a/backends/base-backend.h b/backends/base-backend.h
    index 697577c..3fcca9c 100644
    a b  
    2929#include "common/system.h"
    3030#include "backends/events/default/default-events.h"
    3131
    32 class BaseBackend : public OSystem, EventProvider {
     32class BaseBackend : public OSystem, Common::EventSource {
    3333public:
    3434        virtual Common::EventManager *getEventManager();
    3535        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  
    9292        }
    9393}
    9494
    95 DefaultEventManager::DefaultEventManager(EventProvider *boss) :
    96         _boss(boss),
     95DefaultEventManager::DefaultEventManager(Common::EventSource *boss) :
    9796        _buttonState(0),
    9897        _modifierState(0),
    9998        _shouldQuit(false),
    10099        _shouldRTL(false),
    101100        _confirmExitDialogActive(false) {
    102101
    103         assert(_boss);
     102        assert(boss);
     103
     104        _dispatcher.registerSource(boss, false);
     105        _dispatcher.registerSource(&_artificialEventSource, false);
     106
     107        _dispatcher.registerObserver(this, kEventManPriority, false);
    104108
    105109        _recordFile = NULL;
    106110        _recordTimeFile = NULL;
    DefaultEventManager::DefaultEventManager(EventProvider *boss) :  
    202206#endif
    203207#ifdef ENABLE_KEYMAPPER
    204208        _keymapper = new Common::Keymapper(this);
     209        // EventDispatcher will automatically free the keymapper
     210        _dispatcher.registerMapper(_keymapper);
    205211        _remap = false;
    206212#endif
    207213}
    208214
    209215DefaultEventManager::~DefaultEventManager() {
    210 #ifdef ENABLE_KEYMAPPER
    211         delete _keymapper;
    212 #endif
    213216#ifdef ENABLE_VKEYBD
    214217        delete _vk;
    215218#endif
    DefaultEventManager::~DefaultEventManager() {  
    219222        g_system->unlockMutex(_timeMutex);
    220223        g_system->unlockMutex(_recorderMutex);
    221224
    222         if (!artificialEventQueue.empty())
    223                 artificialEventQueue.clear();
    224 
    225225        if (_playbackFile != NULL) {
    226226                delete _playbackFile;
    227227        }
    void DefaultEventManager::processMillis(uint32 &millis) {  
    381381
    382382bool DefaultEventManager::pollEvent(Common::Event &event) {
    383383        uint32 time = g_system->getMillis();
    384         bool result;
     384        bool result = false;
    385385
    386         if (!artificialEventQueue.empty()) {
    387                 event = artificialEventQueue.pop();
     386        _dispatcher.dispatch();
     387        if (!_eventQueue.empty()) {
     388                event = _eventQueue.pop();
    388389                result = true;
    389         } else {
    390                 result = _boss->pollEvent(event);
    391 
    392 #ifdef ENABLE_KEYMAPPER
    393                 if (result) {
    394                         // send key press events to keymapper
    395                         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 #endif
    406390        }
    407391
    408392        if (_recordMode != kPassthrough)  {
    bool DefaultEventManager::pollEvent(Common::Event &event) {  
    598582}
    599583
    600584void DefaultEventManager::pushEvent(const Common::Event &event) {
    601 
    602585        // If already received an EVENT_QUIT, don't add another one
    603586        if (event.type == Common::EVENT_QUIT) {
    604587                if (!_shouldQuit)
    605                         artificialEventQueue.push(event);
     588                        _artificialEventSource.addEvent(event);
    606589        } else
    607                 artificialEventQueue.push(event);
     590                _artificialEventSource.addEvent(event);
    608591}
    609592
    610593#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 {  
    4141}
    4242
    4343
    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 
     44class DefaultEventManager : public Common::EventManager, Common::EventObserver {
    5945#ifdef ENABLE_VKEYBD
    6046        Common::VirtualKeyboard *_vk;
    6147#endif
    class DefaultEventManager : public Common::EventManager {  
    6551        bool _remap;
    6652#endif
    6753
    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        }
    6961
    7062        Common::Point _mousePos;
    7163        int _buttonState;
    class DefaultEventManager : public Common::EventManager {  
    128120        void record(Common::Event &event);
    129121        bool playback(Common::Event &event);
    130122public:
    131         DefaultEventManager(EventProvider *boss);
     123        DefaultEventManager(Common::EventSource *boss);
    132124        ~DefaultEventManager();
    133125
    134126        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() {  
    168168                _activeMaps.pop();
    169169}
    170170
     171bool 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
    171180bool Keymapper::mapKeyDown(const KeyState& key) {
    172181        return mapKey(key, true);
    173182}
    void Keymapper::executeAction(const Action *action, bool keyDown) {  
    255264                }
    256265
    257266                evt.mouse = _eventMan->getMousePos();
    258                 _eventMan->pushEvent(evt);
     267                addEvent(evt);
    259268        }
    260269}
    261270
  • backends/keymapper/keymapper.h

    diff --git a/backends/keymapper/keymapper.h b/backends/keymapper/keymapper.h
    index c82f64b..f492882 100644
    a b  
    3939
    4040namespace Common {
    4141
    42 class Keymapper {
     42class Keymapper : public Common::EventMapper, private Common::ArtificialEventSource {
    4343public:
    4444
    4545        struct MapRecord {
    public:  
    134134         */
    135135        void popKeymap();
    136136
     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
    137141        /**
    138142         * @brief Map a key press event.
    139143         * 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
     28namespace Common {
     29
     30EventDispatcher::EventDispatcher() : _mapper(0) {
     31}
     32
     33EventDispatcher::~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
     48void 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
     74void EventDispatcher::registerMapper(EventMapper *mapper) {
     75        if (_mapper)
     76                delete _mapper;
     77        _mapper = mapper;
     78}
     79
     80void 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
     89void 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
     101void 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
     118void 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
     130void 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  
    3131#include "common/rect.h"
    3232#include "common/noncopyable.h"
    3333
     34#include "common/list.h"
     35#include "common/singleton.h"
     36
    3437namespace Common {
    3538
    3639/**
    struct Event {  
    126129        Event() : type(EVENT_INVALID), synthetic(false) {}
    127130};
    128131
     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 */
     138class EventSource {
     139public:
     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 */
     167class ArtificialEventSource : public EventSource {
     168protected:
     169        Common::Queue<Common::Event> _artificialEventQueue;
     170public:
     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 */
     196class EventObserver {
     197public:
     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 */
     220class EventMapper : public EventSource, public EventObserver {
     221public:
     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 */
     239class EventDispatcher {
     240public:
     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);
     293private:
     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
    129316class Keymapper;
    130317
    131318/**
    public:  
    207394        virtual Common::Keymapper *getKeymapper() = 0;
    208395#endif
    209396
    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        };
    211404
    212         Common::Queue<Common::Event> artificialEventQueue;
     405        /**
     406         * Returns the underlying EventDispatcher.
     407         */
     408        EventDispatcher *getEventDispatcher() { return &_dispatcher; }
     409
     410protected:
     411        EventDispatcher _dispatcher;
    213412};
    214413
    215414} // 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 := \  
    55        config-file.o \
    66        config-manager.o \
    77        debug.o \
     8        events.o \
    89        file.o \
    910        fs.o \
    1011        hashmap.o \