xoreos  0.0.5
events.h
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos 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 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos 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 xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #ifndef EVENTS_EVENTS_H
26 #define EVENTS_EVENTS_H
27 
28 #include <list>
29 #include <vector>
30 
31 #include "src/common/types.h"
32 #include "src/common/ptrvector.h"
33 #include "src/common/singleton.h"
34 #include "src/common/mutex.h"
35 
36 #include "src/events/types.h"
37 #include "src/events/joystick.h"
38 
39 namespace Common {
40  class UString;
41 }
42 
43 namespace Events {
44 
45 class Request;
46 
48 class EventsManager : public Common::Singleton<EventsManager> {
49 public:
50  EventsManager();
51 
52  // Initialization
53 
55  void init();
57  void deinit();
58 
60  void reset();
61 
63  bool ready() const;
64 
65 
66  // Quitting
67 
69  bool quitRequested() const;
71  void requestQuit();
72 
74  void doQuit();
75 
76  // For the game thread to signal the main thread that the engine code threw
77 
79  bool fatalErrorRaised() const;
81  void raiseFatalError();
82 
83 
84  // Timing
85 
87  void delay(uint32 ms);
89  uint32 getTimestamp() const;
90 
91 
92  // Events
93 
95  void flushEvents();
96 
102  bool pollEvent(Event &event);
103 
109  bool pushEvent(Event &event);
110 
111 
112  // Keyboard input
113 
115  void enableKeyRepeat(bool repeat = true);
116 
125  void enableTextInput(bool textInput = true);
126 
132  Common::UString getTextInput(const Event &event);
133 
134 
135  // Joystick input
136 
138  size_t getJoystickCount() const;
139 
141  Joystick *getJoystickByIndex(size_t index) const;
143  Joystick *getJoystickByName(const Common::UString &name) const;
144 
145 
147  bool isQueueFull() const;
148 
150  void runMainLoop();
151 
152 
153 private:
155 
156  typedef std::list<Event> EventQueue;
157  typedef void (EventsManager::*RequestHandler)(Request &);
158 
161 
162  bool _ready;
163 
165  bool _doQuit;
166 
168 
170 
173 
174  size_t _queueSize;
175 
178 
179  bool _repeat;
181 
183 
184 
186  void initJoysticks();
188  void deinitJoysticks();
189 
191  bool parseEventQuit(const Event &event);
193  bool parseEventGraphics(const Event &event);
195  bool parseITC(const Event &event);
196 
197  // Request handler
198  void requestCallInMainThread(Request &request);
199  void requestRebuildGLContainer(Request &request);
200  void requestDestroyGLContainer(Request &request);
201 
202  void processEvents();
203 
204  friend class RequestManager;
205 };
206 
207 } // End of namespace Events
208 
210 #define EventMan Events::EventsManager::instance()
211 
212 #endif // EVENTS_EVENTS_H
bool pushEvent(Event &event)
Push an event onto the events queue.
Definition: events.cpp:260
Basic event types.
Class and macro for implementing singletons.
bool parseITC(const Event &event)
Look for inter-thread communication.
Definition: events.cpp:186
bool ready() const
Was the events subsystem successfully initialized?
Definition: events.cpp:123
void runMainLoop()
Run the main loop.
Definition: events.cpp:341
Definition: 2dafile.h:39
A class holding an UTF-8 string.
Definition: ustring.h:48
The request manager, handling all requests.
Definition: requests.h:65
void requestRebuildGLContainer(Request &request)
Definition: events.cpp:396
Joystick/Gamepad handling.
void init()
Initialize the events subsystem.
Definition: events.cpp:66
void enableTextInput(bool textInput=true)
Enable/Disable the text input.
Definition: events.cpp:285
bool quitRequested() const
Was an engine quit requested?
Definition: events.cpp:319
void requestDestroyGLContainer(Request &request)
Definition: events.cpp:400
bool isQueueFull() const
Is the event queue full?
Definition: events.cpp:127
Common::Mutex _eventQueueMutex
Definition: events.h:172
EventQueue _eventQueue
Definition: events.h:171
bool _ready
Was the events subsystem successfully initialized?
Definition: events.h:162
Common::UString getTextInput(const Event &event)
Return the text that was input with keyboard, in UTF-8 encoding.
Definition: events.cpp:300
SDL_Event Event
Definition: types.h:42
A condition.
Definition: mutex.h:83
Joysticks _joysticks
Definition: events.h:169
uint32 getTimestamp() const
Return the number of milliseconds the application is running.
Definition: events.cpp:136
Generic template base class for implementing the singleton design pattern.
Definition: singleton.h:61
A mutex.
Definition: mutex.h:40
void raiseFatalError()
Raise a fatal engine error.
Definition: events.cpp:335
Common::Condition _queueProcessed
Definition: events.h:177
void(EventsManager::* RequestHandler)(Request &)
Definition: events.h:157
Joystick * getJoystickByIndex(size_t index) const
Return the joystick with that index.
Definition: events.cpp:377
void requestQuit()
Request an engine quit.
Definition: events.cpp:323
void requestCallInMainThread(Request &request)
Definition: events.cpp:392
void reset()
Completely reset the events manager.
Definition: events.cpp:103
Low-level type definitions to handle fixed width types portably.
For range checks.
Definition: types.h:75
static const RequestHandler _requestHandler[kITCEventMAX]
Pointer to the request handler.
Definition: events.h:160
void deinitJoysticks()
Deinitialize the available joysticks/gamepads.
Definition: events.cpp:367
The events manager.
Definition: events.h:48
A vector storing pointer to objects, with automatic deletion.
void doQuit()
Initiate the actual quitting process.
Definition: events.cpp:327
Thread mutex classes.
bool pollEvent(Event &event)
Get an event from the events queue.
Definition: events.cpp:247
void delay(uint32 ms)
Sleep that number of milliseconds.
Definition: events.cpp:131
void initJoysticks()
Initialize the available joysticks/gamepads.
Definition: events.cpp:353
Common::PtrVector< Joystick > Joysticks
Definition: events.h:154
void deinit()
Deinitialize the events subsystem.
Definition: events.cpp:92
Joystick * getJoystickByName(const Common::UString &name) const
Return the first joystick with that name.
Definition: events.cpp:384
uint32_t uint32
Definition: types.h:204
bool parseEventGraphics(const Event &event)
Look for graphics events.
Definition: events.cpp:161
bool parseEventQuit(const Event &event)
Look for quit events.
Definition: events.cpp:140
size_t getJoystickCount() const
Return the number of available joysticks.
Definition: events.cpp:373
A request, carrying inter-thread communication.
Definition: requesttypes.h:52
bool _quitRequested
Was an engine quit requested?
Definition: events.h:164
bool fatalErrorRaised() const
Was a fatal engine error raised?
Definition: events.cpp:331
bool _doQuit
Are we currently in the process of quitting?
Definition: events.h:165
void enableKeyRepeat(bool repeat=true)
Enable/Disable repeated key events.
Definition: events.cpp:273
void flushEvents()
Clear the event queue, ignore all unhandled events.
Definition: events.cpp:241
std::list< Event > EventQueue
Definition: events.h:156
unsigned int uint
Definition: types.h:211