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
|
/* 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 "backends/timer/default/default-timer.h"
#include "engines/engine.h"
#include "gui.h"
#include "options-dialog.h"
#include "config.h"
#include "osystem.h"
namespace _3DS {
static Common::Mutex *eventMutex;
static InputMode inputMode = MODE_DRAG;
static aptHookCookie cookie;
static bool optionMenuOpening = false;
static Common::String messageOSD;
static bool showMessageOSD = false;
static void pushEventQueue(Common::Queue<Common::Event> *queue, Common::Event &event) {
Common::StackLock lock(*eventMutex);
queue->push(event);
}
static void eventThreadFunc(void *arg) {
OSystem_3DS *osys = (OSystem_3DS *)g_system;
auto eventQueue = (Common::Queue<Common::Event> *)arg;
uint32 touchStartTime = osys->getMillis();
touchPosition lastTouch = {0, 0};
bool isRightClick = false;
float cursorX = 0;
float cursorY = 0;
float cursorDeltaX = 0;
float cursorDeltaY = 0;
int circleDeadzone = 20;
int borderSnapZone = 6;
Common::Event event;
while (!osys->exiting) {
do {
osys->delayMillis(10);
} while (osys->sleeping && !osys->exiting);
hidScanInput();
touchPosition touch;
circlePosition circle;
u32 held = hidKeysHeld();
u32 keysPressed = hidKeysDown();
u32 keysReleased = hidKeysUp();
// C-Pad used to control the cursor
hidCircleRead(&circle);
if (circle.dx < circleDeadzone && circle.dx > -circleDeadzone)
circle.dx = 0;
if (circle.dy < circleDeadzone && circle.dy > -circleDeadzone)
circle.dy = 0;
cursorDeltaX = (0.0002f + config.sensitivity / 100000.f) * circle.dx * abs(circle.dx);
cursorDeltaY = (0.0002f + config.sensitivity / 100000.f) * circle.dy * abs(circle.dy);
// Touch screen events
if (held & KEY_TOUCH) {
hidTouchRead(&touch);
if (config.snapToBorder) {
if (touch.px < borderSnapZone)
touch.px = 0;
if (touch.px > 319 - borderSnapZone)
touch.px = 319;
if (touch.py < borderSnapZone)
touch.py = 0;
if (touch.py > 239 - borderSnapZone)
touch.py = 239;
}
cursorX = touch.px;
cursorY = touch.py;
osys->transformPoint(touch);
osys->warpMouse(touch.px, touch.py);
event.mouse.x = touch.px;
event.mouse.y = touch.py;
if (keysPressed & KEY_TOUCH) {
touchStartTime = osys->getMillis();
isRightClick = (held & KEY_X || held & KEY_DUP);
if (inputMode == MODE_DRAG) {
event.type = isRightClick ? Common::EVENT_RBUTTONDOWN : Common::EVENT_LBUTTONDOWN;
pushEventQueue(eventQueue, event);
}
} else if (touch.px != lastTouch.px || touch.py != lastTouch.py) {
event.type = Common::EVENT_MOUSEMOVE;
pushEventQueue(eventQueue, event);
}
lastTouch = touch;
} else if (keysReleased & KEY_TOUCH) {
event.mouse.x = lastTouch.px;
event.mouse.y = lastTouch.py;
if (inputMode == MODE_DRAG) {
event.type = isRightClick ? Common::EVENT_RBUTTONUP : Common::EVENT_LBUTTONUP;
pushEventQueue(eventQueue, event);
} else if (osys->getMillis() - touchStartTime < 200) {
// Process click in MODE_HOVER
event.type = Common::EVENT_MOUSEMOVE;
pushEventQueue(eventQueue, event);
event.type = isRightClick ? Common::EVENT_RBUTTONDOWN : Common::EVENT_LBUTTONDOWN;
pushEventQueue(eventQueue, event);
event.type = isRightClick ? Common::EVENT_RBUTTONUP : Common::EVENT_LBUTTONUP;
pushEventQueue(eventQueue, event);
}
} else if (cursorDeltaX != 0 || cursorDeltaY != 0) {
cursorX += cursorDeltaX;
cursorY -= cursorDeltaY;
if (cursorX < 0) cursorX = 0;
if (cursorY < 0) cursorY = 0;
if (cursorX > 320) cursorX = 320;
if (cursorY > 240) cursorY = 240;
lastTouch.px = cursorX;
lastTouch.py = cursorY;
osys->transformPoint(lastTouch);
osys->warpMouse(lastTouch.px, lastTouch.py);
event.mouse.x = lastTouch.px;
event.mouse.y = lastTouch.py;
event.type = Common::EVENT_MOUSEMOVE;
pushEventQueue(eventQueue, event);
}
// Button events
if (keysPressed & KEY_R) {
if (inputMode == MODE_DRAG) {
inputMode = MODE_HOVER;
osys->displayMessageOnOSD("Hover Mode");
} else {
inputMode = MODE_DRAG;
osys->displayMessageOnOSD("Drag Mode");
}
}
if (keysPressed & KEY_A || keysPressed & KEY_DLEFT || keysReleased & KEY_A || keysReleased & KEY_DLEFT) {
// SIMULATE LEFT CLICK
event.mouse.x = lastTouch.px;
event.mouse.y = lastTouch.py;
if (keysPressed & KEY_A || keysPressed & KEY_DLEFT)
event.type = Common::EVENT_LBUTTONDOWN;
else
event.type = Common::EVENT_LBUTTONUP;
pushEventQueue(eventQueue, event);
}
if (keysPressed & KEY_X || keysPressed & KEY_DUP || keysReleased & KEY_X || keysReleased & KEY_DUP) {
// SIMULATE RIGHT CLICK
event.mouse.x = lastTouch.px;
event.mouse.y = lastTouch.py;
if (keysPressed & KEY_X || keysPressed & KEY_DUP)
event.type = Common::EVENT_RBUTTONDOWN;
else
event.type = Common::EVENT_RBUTTONUP;
pushEventQueue(eventQueue, event);
}
if (keysPressed & KEY_L) {
event.type = Common::EVENT_VIRTUAL_KEYBOARD;
pushEventQueue(eventQueue, event);
}
if (keysPressed & KEY_START) {
event.type = Common::EVENT_MAINMENU;
pushEventQueue(eventQueue, event);
}
if (keysPressed & KEY_SELECT) {
if (!optionMenuOpened)
optionMenuOpening = true;
}
if (keysPressed & KEY_B || keysReleased & KEY_B || keysPressed & KEY_DDOWN || keysReleased & KEY_DDOWN) {
if (keysPressed & KEY_B || keysPressed & KEY_DDOWN)
event.type = Common::EVENT_KEYDOWN;
else
event.type = Common::EVENT_KEYUP;
event.kbd.keycode = Common::KEYCODE_ESCAPE;
event.kbd.ascii = Common::ASCII_ESCAPE;
event.kbd.flags = 0;
pushEventQueue(eventQueue, event);
}
// TODO: EVENT_PREDICTIVE_DIALOG
// EVENT_SCREEN_CHANGED
}
}
static void aptHookFunc(APT_HookType hookType, void *param) {
OSystem_3DS *osys = (OSystem_3DS *)g_system;
switch (hookType) {
case APTHOOK_ONSUSPEND:
case APTHOOK_ONSLEEP:
if (g_engine)
g_engine->pauseEngine(true);
osys->sleeping = true;
if (R_SUCCEEDED(gspLcdInit())) {
GSPLCD_PowerOnBacklight(GSPLCD_SCREEN_BOTH);
gspLcdExit();
}
break;
case APTHOOK_ONRESTORE:
case APTHOOK_ONWAKEUP:
if (g_engine)
g_engine->pauseEngine(false);
osys->sleeping = false;
loadConfig();
break;
default: {
Common::StackLock lock(*eventMutex);
Common::Event event;
event.type = Common::EVENT_QUIT;
g_system->getEventManager()->pushEvent(event);
}
}
}
static void timerThreadFunc(void *arg) {
OSystem_3DS *osys = (OSystem_3DS *)arg;
DefaultTimerManager *tm = (DefaultTimerManager *)osys->getTimerManager();
while (!osys->exiting) {
g_system->delayMillis(10);
tm->handler();
}
}
void OSystem_3DS::initEvents() {
eventMutex = new Common::Mutex();
s32 prio = 0;
svcGetThreadPriority(&prio, CUR_THREAD_HANDLE);
_timerThread = threadCreate(&timerThreadFunc, this, 32 * 1024, prio - 1, -2, false);
_eventThread = threadCreate(&eventThreadFunc, &_eventQueue, 32 * 1024, prio - 1, -2, false);
aptHook(&cookie, aptHookFunc, this);
}
void OSystem_3DS::destroyEvents() {
threadJoin(_timerThread, U64_MAX);
threadFree(_timerThread);
threadJoin(_eventThread, U64_MAX);
threadFree(_eventThread);
delete eventMutex;
}
void OSystem_3DS::transformPoint(touchPosition &point) {
if (!_overlayVisible) {
point.px = static_cast<float>(point.px) / _gameBottomTexture.getScaleX() - _gameBottomX;
point.py = static_cast<float>(point.py) / _gameBottomTexture.getScaleY() - _gameBottomY;
}
}
void OSystem_3DS::displayMessageOnOSD(const char *msg) {
messageOSD = msg;
showMessageOSD = true;
}
bool OSystem_3DS::pollEvent(Common::Event &event) {
if (showMessageOSD) {
showMessageOSD = false;
StatusMessageDialog dialog(messageOSD, 800);
dialog.runModal();
}
aptMainLoop(); // Call apt hook when necessary
if (optionMenuOpening) {
optionMenuOpening = false;
OptionsDialog dialog;
if (g_engine)
g_engine->pauseEngine(true);
dialog.runModal();
if (g_engine)
g_engine->pauseEngine(false);
}
Common::StackLock lock(*eventMutex);
if (_eventQueue.empty())
return false;
event = _eventQueue.pop();
return true;
}
} // namespace _3DS
|