aboutsummaryrefslogtreecommitdiff
path: root/engines/glk/events.h
blob: 99a25f6ad1177ddf1122c586397ca90033192489 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#ifndef GLK_EVENTS_H
#define GLK_EVENTS_H

#include "common/events.h"
#include "graphics/surface.h"
#include "glk/utils.h"

namespace Glk {

#define GAME_FRAME_RATE 100
#define GAME_FRAME_TIME (1000 / GAME_FRAME_RATE)

class Window;

/**
 * Event types
 */
enum EvType {
	evtype_None         = 0,
	evtype_Timer        = 1,
	evtype_CharInput    = 2,
	evtype_LineInput    = 3,
	evtype_MouseInput   = 4,
	evtype_Arrange      = 5,
	evtype_Redraw       = 6,
	evtype_SoundNotify  = 7,
	evtype_Hyperlink    = 8,
	evtype_VolumeNotify = 9,

	// ScummVM custom events
	evtype_Quit         = 99
};

/**
 * Keycodes
 */
enum Keycode {
	keycode_Unknown  = 0xffffffffU,
	keycode_Left     = 0xfffffffeU,
	keycode_Right    = 0xfffffffdU,
	keycode_Up       = 0xfffffffcU,
	keycode_Down     = 0xfffffffbU,
	keycode_Return   = 0xfffffffaU,
	keycode_Delete   = 0xfffffff9U,
	keycode_Escape   = 0xfffffff8U,
	keycode_Tab      = 0xfffffff7U,
	keycode_PageUp   = 0xfffffff6U,
	keycode_PageDown = 0xfffffff5U,
	keycode_Home     = 0xfffffff4U,
	keycode_End      = 0xfffffff3U,
	keycode_Func1    = 0xffffffefU,
	keycode_Func2    = 0xffffffeeU,
	keycode_Func3    = 0xffffffedU,
	keycode_Func4    = 0xffffffecU,
	keycode_Func5    = 0xffffffebU,
	keycode_Func6    = 0xffffffeaU,
	keycode_Func7    = 0xffffffe9U,
	keycode_Func8    = 0xffffffe8U,
	keycode_Func9    = 0xffffffe7U,
	keycode_Func10   = 0xffffffe6U,
	keycode_Func11   = 0xffffffe5U,
	keycode_Func12   = 0xffffffe4U,

	// non standard keycodes
	keycode_Erase          = 0xffffef7fU,
	keycode_MouseWheelUp   = 0xffffeffeU,
	keycode_MouseWheelDown = 0xffffefffU,
	keycode_SkipWordLeft   = 0xfffff000U,
	keycode_SkipWordRight  = 0xfffff001U,

	// The last keycode is always = 0x100000000 - keycode_MAXVAL)
	keycode_MAXVAL = 28U
};

/**
 * List of cursors
 */
enum CursorId {
	CURSOR_NONE = 0,
	CURSOR_ARROW = 1,
	CURSOR_IBEAM = 2,
	CURSOR_HAND = 3
};

/**
 * Event structure
 */
struct Event {
	EvType type;
	Window *window;
	uint val1, val2;

	/**
	 * Constructor
	 */
	Event() {
		clear();
	}

	/**
	 * Constructor
	 */
	Event(EvType evType, Window *evWindow, uint evVal1, uint evVal2) {
		type = evType;
		window = evWindow;
		val1 = evVal1;
		val2 = evVal2;
	}

	/**
	 * Clear
	 */
	void clear() {
		type = evtype_None;
		window = nullptr;
		val1 = val2 = 0;
	}

	/**
	 * Boolean cast to allow checking whether event is filled out
	 */
	operator bool() const {
		return type != evtype_None;
	}
};
typedef Event event_t;

class EventQueue : public Common::Queue<Event> {
public:
	/**
	 * Retrieve a pending event, if any
	 */
	Event retrieve() {
		return empty() ? Event() : pop();
	}
};

/**
 * Events manager
 */
class Events {
	struct Surface : public Graphics::Surface {
		Common::Point _hotspot;
	};
private:
	EventQueue _eventsPolled;       ///< User generated events
	EventQueue _eventsLogged;       ///< Custom events generated by game code
	Event *_currentEvent;           ///< Event pointer passed during event retrieval
	uint32 _priorFrameTime;         ///< Time of prior game frame
	uint32 _frameCounter;           ///< Frame counter
	bool _redraw;                   ///< Screen needed redrawing
	CursorId _cursorId;             ///< Current cursor Id
	Surface _cursors[4];            ///< Cursor pixel data
	uint _timerMilli;               ///< Time in milliseconds between timer events
	uint _timerTimeExpiry;          ///< When to trigger next timer event
private:
	/**
	 * Initialize the cursor graphics
	 */
	void initializeCursors();

	/**
	 * Checks for whether it's time for the next game frame
	 */
	void checkForNextFrameCounter();

	/**
	 * Dispatches an event
	 */
	void dispatchEvent(Event &ev, bool polled);

	/**
	 * Poll for user events
	 */
	void pollEvents();

	/**
	 * Handle a key down event
	 */
	void handleKeyDown(const Common::KeyState &ks);

	/**
	 * Handle scroll events
	 */
	void handleScroll(bool wheelUp);

	/**
	 * Handle mouse move events
	 */
	void handleMouseMove(const Point &pos);

	/**
	 * Handle mouse down events
	 */
	void handleButtonDown(bool isLeft, const Point &pos);

	/**
	 * Handle mouse up events
	 */
	void handleButtonUp(bool isLeft, const Point &pos);

	/**
	 * Returns true if the passed keycode is for the Ctrl or Alt keys
	 */
	bool isModifierKey(const Common::KeyCode &keycode) const;
public:
	bool _forceClick;
public:
	/**
	 * Constructor
	 */
	Events();

	/**
	 * Destructor
	 */
	~Events();

	/**
	  * Get any pending event
	  */
	void getEvent(event_t *event, bool polled);

	/**
	 * Store an event for retrieval
	 */
	void store(EvType type, Window *win, uint val1 = 0, uint val2 = 0);

	/**
	 * Wait for a keypress
	 */
	uint getKeypress();

	/**
	 * Wait for a keyboard or mouse press
	 */
	void waitForPress();

	/**
	 * Get the total number of frames played
	 */
	uint32 getTotalPlayTicks() const {
		return _frameCounter;
	}

	/**
	 * Set the total number of frames played
	 */
	void setTotalPlayTicks(uint frames) {
		_frameCounter = frames;
	}

	/**
	 * Flags the screen for redrawing
	 */
	void redraw() {
		_redraw = true;
	}

	/**
	 * Sets the current cursor
	 */
	void setCursor(CursorId cursorId);

	/**
	 * Sets whether the mouse cursor is visible
	 * @remarks Normally the cursor is visible for all games, even for those that didn't have mouse originally,
	 * so as to allow for common Glk functionality for selecting ranges of text
	 */
	void showMouseCursor(bool visible);

	/**
	 * Set a timer interval
	 * @param   milli       Time in millieseconds for intervals, or 0 for off
	 */
	void setTimerInterval(uint milli);

	/**
	 * Returns true if it's time for a timer event
	 */
	bool isTimerExpired() const;
};

} // End of namespace Glk

#endif