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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
|
/* 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.
*
*/
#include "common/system.h"
#include "common/events.h"
#include "common/file.h"
#include "sci/sci.h"
#include "sci/event.h"
#include "sci/console.h"
#include "sci/engine/state.h"
#include "sci/engine/kernel.h"
#ifdef ENABLE_SCI32
#include "sci/graphics/cursor32.h"
#include "sci/graphics/frameout.h"
#endif
#include "sci/graphics/screen.h"
namespace Sci {
struct ScancodeRow {
int offset;
const char *keys;
};
static const ScancodeRow scancodeAltifyRows[] = {
{ 0x10, "QWERTYUIOP[]" },
{ 0x1e, "ASDFGHJKL;'\\" },
{ 0x2c, "ZXCVBNM,./" }
};
static const byte codePageMap88591ToDOS[0x80] = {
'?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', // 0x8x
'?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', // 0x9x
'?', 0xad, 0x9b, 0x9c, '?', 0x9d, '?', 0x9e, '?', '?', 0xa6, 0xae, 0xaa, '?', '?', '?', // 0xAx
'?', '?', '?', '?', '?', '?', '?', '?', '?', '?', 0xa7, 0xaf, 0xac, 0xab, '?', 0xa8, // 0xBx
'?', '?', '?', '?', 0x8e, 0x8f, 0x92, 0x80, '?', 0x90, '?', '?', '?', '?', '?', '?', // 0xCx
'?', 0xa5, '?', '?', '?', '?', 0x99, '?', '?', '?', '?', '?', 0x9a, '?', '?', 0xe1, // 0xDx
0x85, 0xa0, 0x83, '?', 0x84, 0x86, 0x91, 0x87, 0x8a, 0x82, 0x88, 0x89, 0x8d, 0xa1, 0x8c, 0x8b, // 0xEx
'?', 0xa4, 0x95, 0xa2, 0x93, '?', 0x94, '?', '?', 0x97, 0xa3, 0x96, 0x81, '?', '?', 0x98 // 0xFx
};
struct SciKeyConversion {
Common::KeyCode scummVMKey;
int sciKeyNumlockOff;
int sciKeyNumlockOn;
};
static const SciKeyConversion keyMappings[] = {
{ Common::KEYCODE_UP , kSciKeyUp , kSciKeyUp },
{ Common::KEYCODE_DOWN , kSciKeyDown , kSciKeyDown },
{ Common::KEYCODE_RIGHT , kSciKeyRight , kSciKeyRight },
{ Common::KEYCODE_LEFT , kSciKeyLeft , kSciKeyLeft },
{ Common::KEYCODE_INSERT , kSciKeyInsert , kSciKeyInsert },
{ Common::KEYCODE_HOME , kSciKeyHome , kSciKeyHome },
{ Common::KEYCODE_END , kSciKeyEnd , kSciKeyEnd },
{ Common::KEYCODE_PAGEUP , kSciKeyPageUp , kSciKeyPageUp },
{ Common::KEYCODE_PAGEDOWN , kSciKeyPageDown , kSciKeyPageDown },
{ Common::KEYCODE_DELETE , kSciKeyDelete , kSciKeyDelete },
{ Common::KEYCODE_KP0 , kSciKeyInsert , '0' },
{ Common::KEYCODE_KP1 , kSciKeyEnd , '1' },
{ Common::KEYCODE_KP2 , kSciKeyDown , '2' },
{ Common::KEYCODE_KP3 , kSciKeyPageDown , '3' },
{ Common::KEYCODE_KP4 , kSciKeyLeft , '4' },
{ Common::KEYCODE_KP5 , kSciKeyCenter , '5' },
{ Common::KEYCODE_KP6 , kSciKeyRight , '6' },
{ Common::KEYCODE_KP7 , kSciKeyHome , '7' },
{ Common::KEYCODE_KP8 , kSciKeyUp , '8' },
{ Common::KEYCODE_KP9 , kSciKeyPageUp , '9' },
{ Common::KEYCODE_KP_PERIOD , kSciKeyDelete , '.' },
{ Common::KEYCODE_KP_ENTER , kSciKeyEnter , kSciKeyEnter },
{ Common::KEYCODE_KP_PLUS , '+' , '+' },
{ Common::KEYCODE_KP_MINUS , '-' , '-' },
{ Common::KEYCODE_KP_MULTIPLY , '*' , '*' },
{ Common::KEYCODE_KP_DIVIDE , '/' , '/' }
};
struct MouseEventConversion {
Common::EventType commonType;
SciEventType sciType;
};
static const MouseEventConversion mouseEventMappings[] = {
{ Common::EVENT_LBUTTONDOWN , kSciEventMousePress },
{ Common::EVENT_RBUTTONDOWN , kSciEventMousePress },
{ Common::EVENT_MBUTTONDOWN , kSciEventMousePress },
{ Common::EVENT_LBUTTONUP , kSciEventMouseRelease },
{ Common::EVENT_RBUTTONUP , kSciEventMouseRelease },
{ Common::EVENT_MBUTTONUP , kSciEventMouseRelease }
};
EventManager::EventManager(bool fontIsExtended) :
_fontIsExtended(fontIsExtended)
#ifdef ENABLE_SCI32
, _hotRectanglesActive(false)
#endif
{}
EventManager::~EventManager() {
}
/**
* Calculates the IBM keyboard alt-key scancode of a printable character.
*/
static int altify(char ch) {
const char c = toupper(ch);
for (int row = 0; row < ARRAYSIZE(scancodeAltifyRows); ++row) {
const char *keys = scancodeAltifyRows[row].keys;
int offset = scancodeAltifyRows[row].offset;
while (*keys) {
if (*keys == c)
return offset << 8;
++offset;
++keys;
}
}
return ch;
}
SciEvent EventManager::getScummVMEvent() {
#ifdef ENABLE_SCI32
SciEvent input = { kSciEventNone, kSciKeyModNone, 0, Common::Point(), Common::Point(), -1 };
SciEvent noEvent = { kSciEventNone, kSciKeyModNone, 0, Common::Point(), Common::Point(), -1 };
#else
SciEvent input = { kSciEventNone, kSciKeyModNone, 0, Common::Point() };
SciEvent noEvent = { kSciEventNone, kSciKeyModNone, 0, Common::Point() };
#endif
Common::EventManager *em = g_system->getEventManager();
Common::Event ev;
// SCI does not generate separate events for mouse movement (it puts the
// current mouse position on every event, including non-mouse events), so
// skip past all mousemove events in the event queue
bool found;
do {
found = em->pollEvent(ev);
} while (found && ev.type == Common::EVENT_MOUSEMOVE);
Common::Point mousePos = em->getMousePos();
#if ENABLE_SCI32
if (getSciVersion() >= SCI_VERSION_2) {
const Buffer &screen = g_sci->_gfxFrameout->getCurrentBuffer();
// This will clamp `mousePos` according to the restricted zone,
// so any cursor or screen item associated with the mouse position
// does not bounce when it hits the edge (or ignore the edge)
g_sci->_gfxCursor32->deviceMoved(mousePos);
Common::Point mousePosSci = mousePos;
mulru(mousePosSci, Ratio(screen.scriptWidth, screen.screenWidth), Ratio(screen.scriptHeight, screen.screenHeight));
noEvent.mousePosSci = input.mousePosSci = mousePosSci;
if (_hotRectanglesActive) {
checkHotRectangles(mousePosSci);
}
} else {
#endif
g_sci->_gfxScreen->adjustBackUpscaledCoordinates(mousePos.y, mousePos.x);
#if ENABLE_SCI32
}
#endif
noEvent.mousePos = input.mousePos = mousePos;
if (!found || ev.type == Common::EVENT_MOUSEMOVE) {
int modifiers = em->getModifierState();
if (modifiers & Common::KBD_ALT)
noEvent.modifiers |= kSciKeyModAlt;
if (modifiers & Common::KBD_CTRL)
noEvent.modifiers |= kSciKeyModCtrl;
if (modifiers & Common::KBD_SHIFT)
noEvent.modifiers |= kSciKeyModShift;
return noEvent;
}
if (ev.type == Common::EVENT_QUIT || ev.type == Common::EVENT_RTL) {
input.type = kSciEventQuit;
return input;
}
int scummVMKeyFlags;
switch (ev.type) {
case Common::EVENT_KEYDOWN:
case Common::EVENT_KEYUP:
// Use keyboard modifiers directly in case this is a keyboard event
scummVMKeyFlags = ev.kbd.flags;
break;
default:
// Otherwise get them from EventManager
scummVMKeyFlags = em->getModifierState();
break;
}
// Caps lock and scroll lock are not handled here because we already
// handle upper case keys elsewhere, and scroll lock doesn't seem to
// ever be used
input.modifiers = kSciKeyModNone;
if (scummVMKeyFlags & Common::KBD_ALT)
input.modifiers |= kSciKeyModAlt;
if (scummVMKeyFlags & Common::KBD_CTRL)
input.modifiers |= kSciKeyModCtrl;
if (scummVMKeyFlags & Common::KBD_SHIFT)
input.modifiers |= kSciKeyModShift;
// Handle mouse events
for (int i = 0; i < ARRAYSIZE(mouseEventMappings); i++) {
if (mouseEventMappings[i].commonType == ev.type) {
input.type = mouseEventMappings[i].sciType;
// Sierra passed keyboard modifiers for mouse events, too.
// Sierra also set certain modifiers within their mouse interrupt handler
// This whole thing was probably meant for people using a mouse, that only featured 1 button
// So the user was able to press Ctrl and click the mouse button to create a right click.
switch (ev.type) {
case Common::EVENT_RBUTTONDOWN: // right button
case Common::EVENT_RBUTTONUP:
input.modifiers |= kSciKeyModShift; // this value was hardcoded in the mouse interrupt handler
break;
case Common::EVENT_MBUTTONDOWN: // middle button
case Common::EVENT_MBUTTONUP:
input.modifiers |= kSciKeyModCtrl; // this value was hardcoded in the mouse interrupt handler
break;
default:
break;
}
return input;
}
}
// If we reached here, make sure that it's a keydown event
if (ev.type != Common::EVENT_KEYDOWN)
return noEvent;
// Check for Control-Shift-D (debug console)
if (ev.type == Common::EVENT_KEYDOWN && ev.kbd.hasFlags(Common::KBD_CTRL | Common::KBD_SHIFT) && ev.kbd.keycode == Common::KEYCODE_d) {
// Open debug console
Console *con = g_sci->getSciDebugger();
con->attach();
return noEvent;
}
const Common::KeyCode scummVMKeycode = ev.kbd.keycode;
input.character = ev.kbd.ascii;
input.type = kSciEventKeyDown;
if (scummVMKeycode >= Common::KEYCODE_KP0 && scummVMKeycode <= Common::KEYCODE_KP9) {
if (!(scummVMKeyFlags & Common::KBD_NUM)) {
// HACK: Num-Lock not enabled
// We shouldn't get a valid ascii code in these cases. We fix it here, so that cursor keys
// on the numpad work properly.
input.character = 0;
}
}
if (input.character && input.character <= 0xFF) {
// Extended characters need to be converted to the old to DOS CP850/437
// character sets for multilingual games
if (input.character >= 0x80 && input.character <= 0xFF) {
// SSCI accepted all input scan codes, regardless of locale, and
// just didn't display any characters that were missing from fonts
// used by text input controls. We intentionally filter them out
// entirely for non-multilingual games here instead, so we can have
// better error detection for bugs in the text controls
if (!_fontIsExtended) {
return noEvent;
}
input.character = codePageMap88591ToDOS[input.character & 0x7f];
}
if (scummVMKeycode == Common::KEYCODE_TAB) {
input.character = kSciKeyTab;
if (scummVMKeyFlags & Common::KBD_SHIFT)
input.character = kSciKeyShiftTab;
}
if (scummVMKeycode == Common::KEYCODE_DELETE)
input.character = kSciKeyDelete;
} else if (scummVMKeycode >= Common::KEYCODE_F1 && scummVMKeycode <= Common::KEYCODE_F10) {
if (scummVMKeyFlags & Common::KBD_SHIFT)
input.character = kSciKeyShiftF1 + ((scummVMKeycode - Common::KEYCODE_F1) << 8);
else
input.character = kSciKeyF1 + ((scummVMKeycode - Common::KEYCODE_F1) << 8);
} else {
// Arrow keys, numpad keys, etc.
for (int i = 0; i < ARRAYSIZE(keyMappings); i++) {
if (keyMappings[i].scummVMKey == scummVMKeycode) {
const bool numlockOn = (ev.kbd.flags & Common::KBD_NUM);
input.character = numlockOn ? keyMappings[i].sciKeyNumlockOn : keyMappings[i].sciKeyNumlockOff;
break;
}
}
}
// TODO: Leaky abstractions from SDL should not be handled in game engines!
// When Ctrl and Alt are pressed together with a printable key, SDL1 on
// Linux will give us a control character instead of the printable
// character we need to convert to an alt scancode
if ((scummVMKeyFlags & Common::KBD_ALT) && input.character > 0 && input.character < 27)
input.character += 96; // 0x01 -> 'a'
if (scummVMKeyFlags & Common::KBD_ALT) {
input.character = altify(input.character & 0xFF);
} else if ((scummVMKeyFlags & Common::KBD_NON_STICKY) == Common::KBD_CTRL && input.character >= 'a' && input.character <= 'z') {
// In SSCI, Ctrl+<key> generates ASCII control characters, but the
// backends usually give us a printable character + Ctrl flag, so
// convert this combo back into what is expected by game scripts
input.character -= 96;
}
// If no actual key was pressed (e.g. if only a modifier key was pressed),
// ignore the event
if (!input.character)
return noEvent;
return input;
}
void EventManager::updateScreen() {
// Update the screen here, since it's called very often.
// Throttle the screen update rate to 60fps.
EngineState *s = g_sci->getEngineState();
if (g_system->getMillis() - s->_screenUpdateTime >= 1000 / 60) {
g_system->updateScreen();
s->_screenUpdateTime = g_system->getMillis();
// Throttle the checking of shouldQuit() to 60fps as well, since
// Engine::shouldQuit() invokes 2 virtual functions
// (EventManager::shouldQuit() and EventManager::shouldRTL()),
// which is very expensive to invoke constantly without any
// throttling at all.
if (g_engine->shouldQuit())
s->abortScriptProcessing = kAbortQuitGame;
}
}
SciEvent EventManager::getSciEvent(SciEventType mask) {
#ifdef ENABLE_SCI32
SciEvent event = { kSciEventNone, kSciKeyModNone, 0, Common::Point(), Common::Point(), -1 };
#else
SciEvent event = { kSciEventNone, kSciKeyModNone, 0, Common::Point() };
#endif
if (getSciVersion() < SCI_VERSION_2) {
updateScreen();
}
// Get all queued events from graphics driver
do {
event = getScummVMEvent();
if (event.type != kSciEventNone)
_events.push_back(event);
} while (event.type != kSciEventNone);
// Search for matching event in queue
Common::List<SciEvent>::iterator iter = _events.begin();
while (iter != _events.end() && !(iter->type & mask))
++iter;
if (iter != _events.end()) {
// Event found
event = *iter;
// If not peeking at the queue, remove the event
if (!(mask & kSciEventPeek))
_events.erase(iter);
} else {
// No event found: we must return a kSciEventNone event.
// Because event.type is kSciEventNone already here,
// there is no need to change it.
}
return event;
}
void EventManager::flushEvents() {
Common::EventManager *em = g_system->getEventManager();
Common::Event event;
while (em->pollEvent(event));
_events.clear();
}
#ifdef ENABLE_SCI32
void EventManager::setHotRectanglesActive(const bool active) {
_hotRectanglesActive = active;
}
void EventManager::setHotRectangles(const Common::Array<Common::Rect> &rects) {
_hotRects = rects;
_activeRectIndex = -1;
}
void EventManager::checkHotRectangles(const Common::Point &mousePosition) {
int lastActiveRectIndex = _activeRectIndex;
_activeRectIndex = -1;
for (int16 i = 0; i < (int16)_hotRects.size(); ++i) {
if (_hotRects[i].contains(mousePosition)) {
_activeRectIndex = i;
if (i != lastActiveRectIndex) {
SciEvent hotRectEvent;
hotRectEvent.type = kSciEventHotRectangle;
hotRectEvent.hotRectangleIndex = i;
_events.push_front(hotRectEvent);
break;
}
lastActiveRectIndex = _activeRectIndex;
}
}
if (lastActiveRectIndex != _activeRectIndex && lastActiveRectIndex != -1) {
_activeRectIndex = -1;
SciEvent hotRectEvent;
hotRectEvent.type = kSciEventHotRectangle;
hotRectEvent.hotRectangleIndex = -1;
_events.push_front(hotRectEvent);
}
}
#endif
} // End of namespace Sci
|