forked from natinusala/borealis
-
Notifications
You must be signed in to change notification settings - Fork 9
/
switch_input.cpp
461 lines (379 loc) · 16.6 KB
/
switch_input.cpp
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
451
452
453
454
455
456
457
458
459
460
461
/*
Copyright 2021 natinusala
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <borealis/core/application.hpp>
#include <borealis/platforms/switch/switch_input.hpp>
namespace brls
{
static const uint64_t HidNpadButton_None = ((uint64_t)1 << (63));
static const uint64_t SWITCH_BUTTONS_FULL_MAPPING[_BUTTON_MAX] = {
HidNpadButton_ZL, // BUTTON_LT
HidNpadButton_L, // BUTTON_LB
HidNpadButton_StickL, // BUTTON_LSB
HidNpadButton_Up, // BUTTON_UP
HidNpadButton_Right, // BUTTON_RIGHT
HidNpadButton_Down, // BUTTON_DOWN
HidNpadButton_Left, // BUTTON_LEFT
HidNpadButton_Minus, // BUTTON_BACK
HidNpadButton_None, // BUTTON_GUIDE
HidNpadButton_Plus, // BUTTON_START
HidNpadButton_StickR, // BUTTON_RSB
HidNpadButton_Y, // BUTTON_Y
HidNpadButton_B, // BUTTON_B
HidNpadButton_A, // BUTTON_A
HidNpadButton_X, // BUTTON_X
HidNpadButton_R, // BUTTON_RB
HidNpadButton_ZR, // BUTTON_RT
HidNpadButton_AnyUp, // BUTTON_NAV_UP
HidNpadButton_AnyRight, // BUTTON_NAV_RIGHT
HidNpadButton_AnyDown, // BUTTON_NAV_DOWN
HidNpadButton_AnyLeft, // BUTTON_NAV_LEFT
};
static const uint64_t SWITCH_BUTTONS_HALF_MAPPING[_BUTTON_MAX] = {
HidNpadButton_None, // BUTTON_LT
HidNpadButton_AnySL, // BUTTON_LB
HidNpadButton_StickL | HidNpadButton_StickR, // BUTTON_LSB
// HidNpadButton_StickLRight | HidNpadButton_StickRLeft, // BUTTON_UP
// HidNpadButton_StickLDown | HidNpadButton_StickRUp, // BUTTON_RIGHT
// HidNpadButton_StickLLeft | HidNpadButton_StickRRight, // BUTTON_DOWN
// HidNpadButton_StickLUp | HidNpadButton_StickRDown, // BUTTON_LEFT
HidNpadButton_None, // BUTTON_UP
HidNpadButton_None, // BUTTON_RIGHT
HidNpadButton_None, // BUTTON_DOWN
HidNpadButton_None, // BUTTON_LEFT
HidNpadButton_None, // BUTTON_BACK
HidNpadButton_None, // BUTTON_GUIDE
HidNpadButton_Plus | HidNpadButton_Minus, // BUTTON_START
HidNpadButton_None, // BUTTON_RSB
HidNpadButton_B | HidNpadButton_Up, // BUTTON_Y
HidNpadButton_A | HidNpadButton_Left, // BUTTON_B
HidNpadButton_X | HidNpadButton_Down, // BUTTON_A
HidNpadButton_Y | HidNpadButton_Right, // BUTTON_X
HidNpadButton_AnySR, // BUTTON_RB
HidNpadButton_None, // BUTTON_RT
HidNpadButton_StickLRight | HidNpadButton_StickRLeft, // BUTTON_NAV_UP
HidNpadButton_StickLDown | HidNpadButton_StickRUp, // BUTTON_NAV_RIGHT
HidNpadButton_StickLLeft | HidNpadButton_StickRRight, // BUTTON_NAV_DOWN
HidNpadButton_StickLUp | HidNpadButton_StickRDown, // BUTTON_NAV_LEFT
};
static const size_t SWITCH_AXIS_MAPPING[_AXES_MAX] = {
LEFT_X,
LEFT_Y,
RIGHT_X,
RIGHT_Y,
};
SwitchInputManager::SwitchInputManager()
{
padConfigureInput(GAMEPADS_MAX, HidNpadStyleSet_NpadStandard);
hidSetNpadJoyHoldType(HidNpadJoyHoldType_Horizontal);
padInitialize(&this->padStateHendheld, HidNpadIdType_Handheld);
hidInitializeVibrationDevices(m_vibration_device_hendheld, 2, HidNpadIdType_Handheld, HidNpadStyleTag_NpadHandheld);
padUpdate(&this->padStateHendheld);
for (int i = 0; i < GAMEPADS_MAX; i++) {
padInitialize(&this->padsState[i], (HidNpadIdType)i);
padUpdate(&this->padsState[i]);
padsStyleSet[i] = this->padsState[i].style_set;
reinitVibration(i);
}
hidInitializeMouse();
hidInitializeKeyboard();
m_hid_keyboard_state.assign(256, false);
}
SwitchInputManager::~SwitchInputManager()
{
NVGcontext* vg = Application::getNVGContext();
if (this->cursorTexture != 0)
nvgDeleteImage(vg, this->cursorTexture);
}
void SwitchInputManager::reinitVibration(int controller)
{
Logger::debug("Vibration reinit #{}", controller);
hidInitializeVibrationDevices(m_vibration_device_handles[controller], 2, (HidNpadIdType)controller, HidNpadStyleTag_NpadJoyDual);
sendRumbleInternal(m_vibration_device_handles[controller], m_vibration_values[controller], 0, 0);
}
void SwitchInputManager::updateUnifiedControllerState(ControllerState* state)
{
for (size_t i = 0; i < _BUTTON_MAX; i++)
state->buttons[i] = false;
for (size_t i = 0; i < _AXES_MAX; i++)
state->axes[i] = 0;
for (int i = 0; i < GAMEPADS_MAX; i++) {
ControllerState localState;
updateControllerState(&localState, i);
for (size_t i = 0; i < _BUTTON_MAX; i++)
state->buttons[i] |= localState.buttons[i];
for (size_t i = 0; i < _AXES_MAX; i++) {
state->axes[i] += localState.axes[i];
if (state->axes[i] < -1) state->axes[i] = -1;
else if (state->axes[i] > 1) state->axes[i] = 1;
}
}
}
short SwitchInputManager::getControllersConnectedCount()
{
padUpdate(&this->padStateHendheld);
int extra = padStateHendheld.active_handheld ? 1 : 0;
int controllers = extra;
for (int i = 0; i < GAMEPADS_MAX - extra; i++) {
if (padsState[i].style_set == 0)
break;
controllers++;
}
return controllers;
}
void SwitchInputManager::updateControllerState(ControllerState* state, int controller)
{
padUpdate(&this->padStateHendheld);
if (controller == 0 && padStateHendheld.active_handheld) {
updateControllerStateInner(state, &padStateHendheld);
return;
}
int localController = padStateHendheld.active_handheld ? controller - 1: controller;
PadState* pad = &this->padsState[localController];
updateControllerStateInner(state, pad);
if (padsStyleSet[localController] != pad->style_set) {
padsStyleSet[localController] = pad->style_set;
reinitVibration(localController);
}
}
void SwitchInputManager::updateControllerStateInner(ControllerState* state, PadState* pad)
{
padUpdate(pad);
uint64_t keysDown = padGetButtons(pad);
bool full = pad->style_set & HidNpadStyleSet_NpadFullCtrl;
for (size_t i = 0; i < _BUTTON_MAX; i++)
{
uint64_t switchKey = full ? SWITCH_BUTTONS_FULL_MAPPING[i] : SWITCH_BUTTONS_HALF_MAPPING[i];
state->buttons[i] = keysDown & switchKey;
}
HidAnalogStickState analog_stick_l = padGetStickPos(pad, 0);
HidAnalogStickState analog_stick_r = padGetStickPos(pad, 1);
if (full) {
state->axes[LEFT_X] = (float)analog_stick_l.x / (float)0x7FFF;
state->axes[LEFT_Y] = (float)analog_stick_l.y / (float)0x7FFF * -1.0f;
state->axes[RIGHT_X] = (float)analog_stick_r.x / (float)0x7FFF;
state->axes[RIGHT_Y] = (float)analog_stick_r.y / (float)0x7FFF * -1.0f;
} else {
state->axes[LEFT_X] = (float)analog_stick_l.y / (float)0x7FFF * -1.0f + (float)analog_stick_r.y / (float)0x7FFF;
state->axes[LEFT_Y] = (float)analog_stick_l.x / (float)0x7FFF * -1.0f + (float)analog_stick_r.x / (float)0x7FFF;
state->axes[RIGHT_X] = 0;
state->axes[RIGHT_Y] = 0;
}
state->buttons[BUTTON_NAV_UP] |= getKeyboardKeyState(BRLS_KBD_KEY_UP);
state->buttons[BUTTON_NAV_RIGHT] |= getKeyboardKeyState(BRLS_KBD_KEY_RIGHT);
state->buttons[BUTTON_NAV_DOWN] |= getKeyboardKeyState(BRLS_KBD_KEY_DOWN);
state->buttons[BUTTON_NAV_LEFT] |= getKeyboardKeyState(BRLS_KBD_KEY_LEFT);
}
bool SwitchInputManager::getKeyboardKeyState(BrlsKeyboardScancode key)
{
for (int i = 0; i < 256; ++i) {
if (key == switchKeyToGlfwKey(i))
return m_hid_keyboard_state[i];
}
return false;
}
void SwitchInputManager::updateTouchStates(std::vector<RawTouchState>* states)
{
// Get touchscreen state
static HidTouchScreenState hidState;
if (hidGetTouchScreenStates(&hidState, 1))
{
for (int i = 0; i < hidState.count; i++)
{
RawTouchState state;
state.pressed = true;
state.fingerId = hidState.touches[i].finger_id;
state.position.x = hidState.touches[i].x / Application::windowScale;
state.position.y = hidState.touches[i].y / Application::windowScale;
states->push_back(state);
}
}
}
void SwitchInputManager::sendRumbleInternal(HidVibrationDeviceHandle vibration_device[2], HidVibrationValue vibration_values[2], unsigned short lowFreqMotor, unsigned short highFreqMotor)
{
float low = (float)lowFreqMotor / 0xFFFF;
float high = (float)highFreqMotor / 0xFFFF;
memset(vibration_values, 0, sizeof(vibration_values));
vibration_values[0].amp_low = low;
vibration_values[0].freq_low = low * 50;
vibration_values[0].amp_high = high;
vibration_values[0].freq_high = high * 100;
vibration_values[1].amp_low = low;
vibration_values[1].freq_low = low * 50;
vibration_values[1].amp_high = high;
vibration_values[1].freq_high = high * 100;
hidSendVibrationValues(vibration_device, vibration_values, 2);
}
void SwitchInputManager::sendRumble(unsigned short controller, unsigned short lowFreqMotor, unsigned short highFreqMotor)
{
float low = (float)lowFreqMotor / 0xFFFF;
float high = (float)highFreqMotor / 0xFFFF;
padUpdate(&this->padStateHendheld);
if (controller == 0 && padStateHendheld.active_handheld) {
sendRumbleInternal(m_vibration_device_hendheld, m_vibration_values_hendheld, lowFreqMotor, highFreqMotor);
return;
}
int localController = padStateHendheld.active_handheld ? controller - 1: controller;
sendRumbleInternal(m_vibration_device_handles[localController], m_vibration_values[localController], lowFreqMotor, highFreqMotor);
}
void SwitchInputManager::updateMouseStates(RawMouseState* state)
{
if (currentMouseState.attributes & HidMouseAttribute_IsConnected) {
state->position = Point(currentMouseState.x, currentMouseState.y);
state->offset = Point(currentMouseState.delta_x, currentMouseState.delta_y);
state->scroll = Point(0, currentMouseState.wheel_delta_x);
state->leftButton = currentMouseState.buttons & HidMouseButton_Left;
state->middleButton = currentMouseState.buttons & HidMouseButton_Middle;
state->rightButton = currentMouseState.buttons & HidMouseButton_Right;
lastCoursorPosition = state->position;
}
}
void SwitchInputManager::runloopStart()
{
upToDateMouseState();
handleMouse();
handleKeyboard();
}
void SwitchInputManager::upToDateMouseState()
{
hidGetMouseStates(¤tMouseState, 1);
}
void SwitchInputManager::handleMouse()
{
if (currentMouseState.attributes & HidMouseAttribute_IsConnected) {
getMouseCusorOffsetChanged()->fire(Point(currentMouseState.delta_x, currentMouseState.delta_y));
getMouseScrollOffsetChanged()->fire(Point(currentMouseState.wheel_delta_y, currentMouseState.wheel_delta_x));
}
}
void SwitchInputManager::handleKeyboard()
{
HidKeyboardState state;
if (hidGetKeyboardStates(&state, 1)) {
for (int i = 0; i < 256; ++i) {
auto is_pressed = (state.keys[i / 64] & (1ul << (i % 64))) != 0;
if (m_hid_keyboard_state[i] != is_pressed)
{
m_hid_keyboard_state[i] = is_pressed;
BrlsKeyboardScancode glfwKey = switchKeyToGlfwKey(i);
KeyState keyState;
keyState.key = glfwKey;
keyState.pressed = is_pressed;
if (state.modifiers & HidKeyboardModifier_LeftAlt)
keyState.mods |= BRLS_KBD_MODIFIER_ALT;
if (state.modifiers & HidKeyboardModifier_Control)
keyState.mods |= BRLS_KBD_MODIFIER_CTRL;
if (state.modifiers & HidKeyboardModifier_Shift)
keyState.mods |= BRLS_KBD_MODIFIER_SHIFT;
if (state.modifiers & HidKeyboardModifier_Gui)
keyState.mods |= BRLS_KBD_MODIFIER_META;
getKeyboardKeyStateChanged()->fire(keyState);
}
}
} else {
Logger::debug("Keyboard failed!");
}
}
void SwitchInputManager::setPointerLock(bool lock)
{
pointerLocked = lock;
}
void SwitchInputManager::drawCoursor(NVGcontext* vg)
{
initCursor(vg);
if (!pointerLocked) {
this->paint.xform[4] = lastCoursorPosition.x;
this->paint.xform[5] = lastCoursorPosition.y;
nvgBeginPath(vg);
nvgRect(vg, lastCoursorPosition.x, lastCoursorPosition.y, this->cursorWidth, this->cursorHeight);
nvgFillPaint(vg, this->paint);
nvgFill(vg);
}
}
void SwitchInputManager::initCursor(NVGcontext* vg)
{
if (cursorInited) return;
if (vg) {
this->pointerIcon = std::string(BRLS_RESOURCES) + "img/sys/cursor.png";
this->cursorTexture = nvgCreateImage(vg, pointerIcon.c_str(), NVG_IMAGE_NEAREST);
int width, height;
nvgImageSize(vg, cursorTexture, &width, &height);
float aspect = (float)height / (float)width;
this->cursorWidth = 18;
this->cursorHeight = 18 * aspect;
this->paint = nvgImagePattern(vg, 0, 0, this->cursorWidth, this->cursorHeight, 0, this->cursorTexture, 1.0f);
this->cursorInited = true;
}
}
BrlsKeyboardScancode SwitchInputManager::switchKeyToGlfwKey(int key)
{
if (KBD_A <= key && key <= KBD_Z) {
return (BrlsKeyboardScancode)(key - KBD_A + BRLS_KBD_KEY_A);
} else if (KBD_1 <= key && key <= KBD_9) {
return (BrlsKeyboardScancode)(key - KBD_1 + BRLS_KBD_KEY_1);
} else if (KBD_F1 <= key && key <= KBD_F12) {
return (BrlsKeyboardScancode)(key - KBD_F1 + BRLS_KBD_KEY_F1);
} else if (KBD_KP1 <= key && key <= KBD_KP9) {
return (BrlsKeyboardScancode)(key - KBD_KP1 + BRLS_KBD_KEY_KP_1);
}
switch (key) {
case KBD_0: return BRLS_KBD_KEY_0;
case KBD_SPACE: return BRLS_KBD_KEY_SPACE;
case KBD_APOSTROPHE: return BRLS_KBD_KEY_APOSTROPHE;
case KBD_COMMA: return BRLS_KBD_KEY_COMMA;
case KBD_MINUS: return BRLS_KBD_KEY_MINUS;
case KBD_DOT: return BRLS_KBD_KEY_PERIOD;
case KBD_SLASH: return BRLS_KBD_KEY_SLASH;
case KBD_SEMICOLON: return BRLS_KBD_KEY_SEMICOLON;
case KBD_EQUAL: return BRLS_KBD_KEY_EQUAL;
case KBD_LEFTBRACE: return BRLS_KBD_KEY_LEFT_BRACKET;
case KBD_RIGHTBRACE: return BRLS_KBD_KEY_RIGHT_BRACKET;
case KBD_BACKSLASH: return BRLS_KBD_KEY_BACKSLASH;
case KBD_GRAVE: return BRLS_KBD_KEY_GRAVE_ACCENT;
case KBD_ESC: return BRLS_KBD_KEY_ESCAPE;
case KBD_ENTER: return BRLS_KBD_KEY_ENTER;
case KBD_TAB: return BRLS_KBD_KEY_TAB;
case KBD_BACKSPACE: return BRLS_KBD_KEY_BACKSPACE;
case KBD_CAPSLOCK: return BRLS_KBD_KEY_CAPS_LOCK;
case KBD_LEFTSHIFT: return BRLS_KBD_KEY_LEFT_SHIFT;
case KBD_LEFTCTRL: return BRLS_KBD_KEY_LEFT_CONTROL;
case KBD_LEFTALT: return BRLS_KBD_KEY_LEFT_ALT;
case KBD_LEFTMETA: return BRLS_KBD_KEY_LEFT_SUPER;
case KBD_RIGHTSHIFT: return BRLS_KBD_KEY_RIGHT_SHIFT;
case KBD_RIGHTCTRL: return BRLS_KBD_KEY_RIGHT_CONTROL;
case KBD_RIGHTALT: return BRLS_KBD_KEY_RIGHT_ALT;
case KBD_RIGHTMETA: return BRLS_KBD_KEY_RIGHT_SUPER;
case KBD_LEFT: return BRLS_KBD_KEY_LEFT;
case KBD_RIGHT: return BRLS_KBD_KEY_RIGHT;
case KBD_UP: return BRLS_KBD_KEY_UP;
case KBD_DOWN: return BRLS_KBD_KEY_DOWN;
case KBD_SYSRQ: return BRLS_KBD_KEY_PRINT_SCREEN;
case KBD_SCROLLLOCK: return BRLS_KBD_KEY_SCROLL_LOCK;
case KBD_PAUSE: return BRLS_KBD_KEY_PAUSE;
case KBD_INSERT: return BRLS_KBD_KEY_INSERT;
case KBD_HOME: return BRLS_KBD_KEY_HOME;
case KBD_PAGEUP: return BRLS_KBD_KEY_PAGE_UP;
case KBD_DELETE: return BRLS_KBD_KEY_DELETE;
case KBD_END: return BRLS_KBD_KEY_END;
case KBD_PAGEDOWN: return BRLS_KBD_KEY_PAGE_DOWN;
case KBD_NUMLOCK: return BRLS_KBD_KEY_NUM_LOCK;
case KBD_KPSLASH: return BRLS_KBD_KEY_KP_DIVIDE;
case KBD_KPASTERISK: return BRLS_KBD_KEY_KP_MULTIPLY;
case KBD_KPMINUS: return BRLS_KBD_KEY_KP_SUBTRACT;
case KBD_KPPLUS: return BRLS_KBD_KEY_KP_ADD;
case KBD_KPENTER: return BRLS_KBD_KEY_KP_ENTER;
case KBD_KPDOT: return BRLS_KBD_KEY_KP_DECIMAL;
case KBD_KP0: return BRLS_KBD_KEY_KP_0;
// case KBD_HASHTILDE: return GLFW_HASHTILDE;
default: return BRLS_KBD_KEY_UNKNOWN;
}
}
} // namespace brls