File: events.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (250 lines) | stat: -rw-r--r-- 5,807 bytes parent folder | download | duplicates (2)
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
/* 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 3 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, see <http://www.gnu.org/licenses/>.
 *
 */

#include "common/system.h"
#include "m4/platform/events.h"
#include "m4/vars.h"
#include "m4/m4.h"

namespace M4 {

/*
 *  Define call mask bit fields
 */

enum {
	CursorPositionChanged = 0,
	LeftButtonPressed, LeftButtonReleased, RightButtonPressed,
	RightButtonReleased
};

/*
 *  Define call mask values
 */
#define CPC				((uint16)(1 << CursorPositionChanged))
#define LBD				((uint16)(1 << LeftButtonPressed))
#define LBU				((uint16)(1 << LeftButtonReleased))
#define LBH				((uint16)(1 << LeftButtonHold))
#define RBD				((uint16)(1 << RightButtonPressed))
#define RBU				((uint16)(1 << RightButtonReleased))
#define RBH				((uint16)(1 << RightButtonHold))

#define LBC				(LBD + LBU)
#define RBC				(RBD + RBU)
#define MBC				(MBD + MBU)
#define MSA				(LBC + RBC + CPC)
#define OEMA			(LBC + RBC + MBC + CPC)

#define _MLD (_mouseStateEvent & LBD)
#define _ClearMLD _mouseStateEvent &= ~LBD
#define _MLU (_mouseStateEvent & LBU)
#define _ClearMLU _mouseStateEvent &= ~LBU

#define _MRD (_mouseStateEvent & RBD)
#define _ClearMRD _mouseStateEvent &= ~RBD
#define _MRU (_mouseStateEvent & RBU)
#define _ClearMRU _mouseStateEvent &= ~RBU

#define _MMOVE ((_mouseX != _oldX) || (_mouseY != _oldY))
#define _MSAVE _oldX = _mouseX; _oldY = _mouseY

Events *g_events;

Events::Events() {
	g_events = this;
}

Events::~Events() {
	g_events = nullptr;
}

void Events::process() {
	pollEvents();
}

void Events::pollEvents() {
	Common::Event ev;
	while (g_system->getEventManager()->pollEvent(ev)) {
		if (ev.type == Common::EVENT_QUIT || ev.type == Common::EVENT_RETURN_TO_LAUNCHER) {
			_G(kernel).going = false;
		} else if (ev.type >= Common::EVENT_MOUSEMOVE && ev.type <= Common::EVENT_MBUTTONUP) {
			handleMouseEvent(ev);
		} else if (ev.type == Common::EVENT_KEYDOWN || ev.type == Common::EVENT_KEYUP) {
			handleKeyboardEvent(ev);
		}
	}
}

void Events::handleMouseEvent(const Common::Event &ev) {
	_mouseX = ev.mouse.x;
	_mouseY = ev.mouse.y;

	switch (ev.type) {
	case Common::EVENT_MOUSEMOVE:
		_mouseStateEvent |= CPC;
		break;
	case Common::EVENT_LBUTTONDOWN:
		_mouseStateEvent |= LBD;
		ButtonState = 1;
		break;
	case Common::EVENT_LBUTTONUP:
		_mouseStateEvent |= LBU;
		ButtonState = 0;
		break;
	case Common::EVENT_RBUTTONDOWN:
		_mouseStateEvent |= RBD;
		ButtonState = 2;
		break;
	case Common::EVENT_RBUTTONUP:
		_mouseStateEvent |= RBU;
		ButtonState = 0;
		break;

	case Common::EVENT_WHEELDOWN:
		_G(toggle_cursor) = CURSCHANGE_NEXT;
		break;
	case Common::EVENT_WHEELUP:
		_G(toggle_cursor) = CURSCHANGE_PREVIOUS;
		break;
	case Common::EVENT_MBUTTONDOWN:
		_G(toggle_cursor) = CURSCHANGE_TOGGLE;
		break;

	default:
		break;
	}
}

void Events::handleKeyboardEvent(const Common::Event &ev) {
	if (ev.type == Common::EVENT_KEYDOWN && _pendingKeys.size() < 16)
		_pendingKeys.push(ev.kbd);
}

MouseEvent Events::mouse_get_event() {
	process();

	switch (_mouse_state) {
	case _MS_no_event:
		if (_MLD) {
			_ClearMLD;
			if (_dclickTime && (timer_read_60() < _dclickTime)) {
				_mouse_state = _MS_doubleclick_Down;
				_dclickTime = 0;
				return _ME_doubleclick;
			}
			_dclickTime = 0;
			_mouse_state = _MS_L_clickDown;
			return _ME_L_click;
		}
		if (_MRD) {
			_ClearMRD;
			_mouse_state = _MS_R_clickDown;
			return _ME_R_click;
		}
		if (_MMOVE) {
			_MSAVE;
			return _ME_move;
		}
		return _ME_no_event;

	case _MS_L_clickDown:
		if (_MLU || !ButtonState) {
			_dclickTime = timer_read_60() + 15;
			_ClearMLU;
			_mouse_state = _MS_no_event;
			return _ME_L_release;
		}
		if (_MMOVE) {
			_MSAVE;
			return _ME_L_drag;
		}
		return _ME_L_hold;

	case _MS_R_clickDown:
		if (_MRU) {
			_ClearMRU;
			_mouse_state = _MS_no_event;
			_G(toggle_cursor) = CURSCHANGE_NEXT;
			return _ME_R_release;
		}
		if (_MMOVE) {
			_MSAVE;
			return _ME_R_drag;
		}
		return _ME_R_hold;

	case _MS_doubleclick_Down:
		if (_MLU) {
			_ClearMLU; _ClearMLD;
			_mouse_state = _MS_no_event;
			return _ME_doubleclick_release;
		}
		if (_MMOVE) {
			_MSAVE;
			return _ME_doubleclick_drag;
		}
		return _ME_doubleclick_hold;

	default:
		return _ME_no_event;
	}

	return _ME_no_event;
}

bool Events::util_kbd_check(int32 *parm1) {
	process();

	if (!parm1 || _pendingKeys.empty())
		return false;

	Common::KeyState ks = _pendingKeys.pop();
	if (is_mod_key(ks))
		return false;

	int flags = ks.flags & (Common::KBD_CTRL | Common::KBD_ALT);
	int key = (ks.ascii >= 32 && ks.ascii <= 127 && !flags) ? ks.ascii : (int)ks.keycode;
	*parm1 = key | (flags << 16);

	return true;
}

void Events::delay(uint amount) {
	uint32 beginTime = g_system->getMillis(), newTime;

	do {
		krn_pal_game_task();
		g_system->delayMillis(5);
		process();

		newTime = g_system->getMillis();
	} while (!g_engine->shouldQuit() && newTime < (beginTime + amount));
}

MouseEvent mouse_get_event() {
	return g_events->mouse_get_event();
}

bool util_kbd_check(int32 *parm1) {
	return g_events->util_kbd_check(parm1);
}

} // namespace M4