ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

common/events.h

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #ifndef COMMON_EVENTS_H
00024 #define COMMON_EVENTS_H
00025 
00026 #include "common/keyboard.h"
00027 #include "common/queue.h"
00028 #include "common/rect.h"
00029 #include "common/noncopyable.h"
00030 
00031 #include "common/list.h"
00032 #include "common/singleton.h"
00033 
00034 namespace Common {
00035 
00045 enum EventType {
00046     EVENT_INVALID = 0,
00048     EVENT_KEYDOWN = 1,
00050     EVENT_KEYUP = 2,
00052     EVENT_MOUSEMOVE = 3,
00053     EVENT_LBUTTONDOWN = 4,
00054     EVENT_LBUTTONUP = 5,
00055     EVENT_RBUTTONDOWN = 6,
00056     EVENT_RBUTTONUP = 7,
00057     EVENT_WHEELUP = 8,
00058     EVENT_WHEELDOWN = 9,
00059     EVENT_MBUTTONDOWN = 13,
00060     EVENT_MBUTTONUP = 14,
00061 
00062     EVENT_MAINMENU = 15,
00063     EVENT_RTL = 16,
00064     EVENT_MUTE = 17,
00065 
00066     EVENT_QUIT = 10,
00067     EVENT_SCREEN_CHANGED = 11,
00075     EVENT_PREDICTIVE_DIALOG = 12
00076 
00077 #ifdef ENABLE_KEYMAPPER
00078     ,
00079     // IMPORTANT NOTE: This is part of the WIP Keymapper. If you plan to use
00080     // this, please talk to tsoliman and/or LordHoto.
00081     EVENT_CUSTOM_BACKEND_ACTION = 18,
00082     EVENT_CUSTOM_BACKEND_HARDWARE = 21,
00083     EVENT_GUI_REMAP_COMPLETE_ACTION = 22,
00084     EVENT_KEYMAPPER_REMAP = 19
00085 #endif
00086 #ifdef ENABLE_VKEYBD
00087     ,
00088     EVENT_VIRTUAL_KEYBOARD = 20
00089 #endif
00090 /* START of ResidualVM-specific code */
00091     ,
00092     EVENT_JOYAXIS_MOTION = 23,
00093     EVENT_JOYBUTTON_DOWN = 24,
00094     EVENT_JOYBUTTON_UP = 25
00095 };
00096 
00097 const int16 JOYAXIS_MIN = -32768;
00098 const int16 JOYAXIS_MAX = 32767;
00099 
00103 struct JoystickState {
00105     byte axis;
00107     int16 position;
00109     byte button;
00110 };
00111 /* END of ResidualVM-specific code */
00112 
00113 typedef uint32 CustomEventType;
00114 
00119 struct Event {
00120 
00122     EventType type;
00123 
00129     bool kbdRepeat;
00130 
00135     KeyState kbd;
00136 
00143     Point mouse;
00144 
00145 #ifdef ENABLE_KEYMAPPER
00146     // IMPORTANT NOTE: This is part of the WIP Keymapper. If you plan to use
00147     // this, please talk to tsoliman and/or LordHoto.
00148     CustomEventType customType;
00149 #endif
00150 
00156     Common::Point relMouse;
00157 
00164     JoystickState joystick;
00165 
00166     Event() : type(EVENT_INVALID), kbdRepeat(false) {
00167 #ifdef ENABLE_KEYMAPPER
00168         customType = 0;
00169 #endif
00170     }
00171 };
00172 
00179 class EventSource {
00180 public:
00181     virtual ~EventSource() {}
00182 
00189     virtual bool pollEvent(Event &event) = 0;
00190 
00199     virtual bool allowMapping() const { return true; }
00200 };
00201 
00208 class ArtificialEventSource : public EventSource {
00209 protected:
00210     Queue<Event> _artificialEventQueue;
00211 public:
00212     void addEvent(const Event &ev) {
00213         _artificialEventQueue.push(ev);
00214     }
00215 
00216     bool pollEvent(Event &ev) {
00217     if (!_artificialEventQueue.empty()) {
00218             ev = _artificialEventQueue.pop();
00219             return true;
00220         } else {
00221             return false;
00222         }
00223     }
00224 
00229     virtual bool allowMapping() const { return false; }
00230 };
00231 
00237 class EventObserver {
00238 public:
00239     virtual ~EventObserver() {}
00240 
00254     virtual bool notifyEvent(const Event &event) = 0;
00255 
00262     virtual bool notifyPoll() { return false; }
00263 };
00264 
00270 class EventMapper {
00271 public:
00272     virtual ~EventMapper() {}
00273 
00277     virtual List<Event> mapEvent(const Event &ev, EventSource *source) = 0;
00278 
00279     virtual List<Event> getDelayedEvents() = 0;
00280 };
00281 
00282 class DefaultEventMapper : public EventMapper {
00283 public:
00284     DefaultEventMapper() : _delayedEvents(), _delayedEffectiveTime(0) {}
00285     // EventMapper interface
00286     virtual List<Event> mapEvent(const Event &ev, EventSource *source);
00287     virtual List<Event> getDelayedEvents();
00288 protected:
00289     virtual void addDelayedEvent(uint32 millis, Event ev);
00290 
00291     struct DelayedEventsEntry {
00292         const uint32 timerOffset;
00293         const Event event;
00294         DelayedEventsEntry(const uint32 offset, const Event ev) : timerOffset(offset), event(ev) { }
00295     };
00296 
00297     Queue<DelayedEventsEntry> _delayedEvents;
00298     uint32 _delayedEffectiveTime;
00299 };
00300 
00314 class EventDispatcher {
00315 public:
00316     EventDispatcher();
00317     ~EventDispatcher();
00318 
00325     void dispatch();
00326 
00331     void clearEvents();
00332 
00347     void registerMapper(EventMapper *mapper, bool autoFree = true);
00348 
00352     EventMapper *queryMapper() const { return _mapper; }
00353 
00357     void registerSource(EventSource *source, bool autoFree);
00358 
00364     void unregisterSource(EventSource *source);
00365 
00372     void registerObserver(EventObserver *obs, uint priority, bool autoFree, bool listenPolls = false);
00373 
00379     void unregisterObserver(EventObserver *obs);
00380 private:
00381     bool _autoFreeMapper;
00382     EventMapper *_mapper;
00383 
00384     struct Entry {
00385         bool autoFree;
00386     };
00387 
00388     struct SourceEntry : public Entry {
00389         EventSource *source;
00390     };
00391 
00392     List<SourceEntry> _sources;
00393 
00394     struct ObserverEntry : public Entry {
00395         uint priority;
00396         EventObserver *observer;
00397         bool poll;
00398     };
00399 
00400     List<ObserverEntry> _observers;
00401 
00402     void dispatchEvent(const Event &event);
00403     void dispatchPoll();
00404 };
00405 
00406 class Keymapper;
00407 
00413 class EventManager : NonCopyable {
00414 public:
00415     EventManager() {}
00416     virtual ~EventManager() {}
00417 
00418     enum {
00419         LBUTTON = 1 << 0,
00420         RBUTTON = 1 << 1
00421     };
00422 
00423 
00428     virtual void init() {}
00429 
00435     virtual bool pollEvent(Event &event) = 0;
00436 
00440     virtual void pushEvent(const Event &event) = 0;
00441 
00445     virtual void purgeMouseEvents() = 0;
00446 
00448     virtual Point getMousePos() const = 0;
00449 
00455     virtual int getButtonState() const = 0;
00456 
00458     virtual int getModifierState() const = 0;
00459 
00464     virtual int shouldQuit() const = 0;
00465 
00469     virtual int shouldRTL() const = 0;
00470 
00475     virtual void resetRTL() = 0;
00476 #ifdef FORCE_RTL
00477     virtual void resetQuit() = 0;
00478 #endif
00479     // Optional: check whether a given key is currently pressed ????
00480     //virtual bool isKeyPressed(int keycode) = 0;
00481 
00482     // TODO: Keyboard repeat support?
00483 
00484     // TODO: Consider removing OSystem::getScreenChangeID and
00485     // replacing it by a generic getScreenChangeID method here
00486 #ifdef ENABLE_KEYMAPPER
00487     virtual Keymapper *getKeymapper() = 0;
00488 #endif
00489 
00490     enum {
00495         kEventManPriority = 0,
00500         kEventRecorderPriority = 1
00501     };
00502 
00506     EventDispatcher *getEventDispatcher() { return &_dispatcher; }
00507 
00508 protected:
00509     EventDispatcher _dispatcher;
00510 };
00511 
00512 } // End of namespace Common
00513 
00514 #endif


Generated on Sat Feb 23 2019 05:01:02 for ResidualVM by doxygen 1.7.1
curved edge   curved edge