src/platform/qt/InputController.cpp (view raw)
1/* Copyright (c) 2013-2014 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "InputController.h"
7
8#include "ConfigController.h"
9#include "GamepadAxisEvent.h"
10#include "GamepadButtonEvent.h"
11#include "InputProfile.h"
12#include "LogController.h"
13
14#include <QApplication>
15#include <QTimer>
16#include <QWidget>
17#ifdef BUILD_QT_MULTIMEDIA
18#include <QCameraInfo>
19#include <QVideoSurfaceFormat>
20#endif
21
22#include <mgba/core/interface.h>
23#include <mgba-util/configuration.h>
24
25using namespace QGBA;
26
27#ifdef BUILD_SDL
28int InputController::s_sdlInited = 0;
29mSDLEvents InputController::s_sdlEvents;
30#endif
31
32InputController::InputController(int playerId, QWidget* topLevel, QObject* parent)
33 : QObject(parent)
34 , m_playerId(playerId)
35 , m_topLevel(topLevel)
36 , m_focusParent(topLevel)
37{
38 mInputMapInit(&m_inputMap, &GBAInputInfo);
39
40#ifdef BUILD_SDL
41 if (s_sdlInited == 0) {
42 mSDLInitEvents(&s_sdlEvents);
43 }
44 ++s_sdlInited;
45 m_sdlPlayer.bindings = &m_inputMap;
46 mSDLInitBindingsGBA(&m_inputMap);
47 updateJoysticks();
48#endif
49
50#ifdef BUILD_SDL
51 connect(&m_gamepadTimer, &QTimer::timeout, [this]() {
52 testGamepad(SDL_BINDING_BUTTON);
53 if (m_playerId == 0) {
54 updateJoysticks();
55 }
56 });
57#endif
58 m_gamepadTimer.setInterval(50);
59 m_gamepadTimer.start();
60
61#ifdef BUILD_QT_MULTIMEDIA
62 connect(&m_videoDumper, &VideoDumper::imageAvailable, this, &InputController::setCamImage);
63#endif
64
65 mInputBindKey(&m_inputMap, KEYBOARD, Qt::Key_X, GBA_KEY_A);
66 mInputBindKey(&m_inputMap, KEYBOARD, Qt::Key_Z, GBA_KEY_B);
67 mInputBindKey(&m_inputMap, KEYBOARD, Qt::Key_A, GBA_KEY_L);
68 mInputBindKey(&m_inputMap, KEYBOARD, Qt::Key_S, GBA_KEY_R);
69 mInputBindKey(&m_inputMap, KEYBOARD, Qt::Key_Return, GBA_KEY_START);
70 mInputBindKey(&m_inputMap, KEYBOARD, Qt::Key_Backspace, GBA_KEY_SELECT);
71 mInputBindKey(&m_inputMap, KEYBOARD, Qt::Key_Up, GBA_KEY_UP);
72 mInputBindKey(&m_inputMap, KEYBOARD, Qt::Key_Down, GBA_KEY_DOWN);
73 mInputBindKey(&m_inputMap, KEYBOARD, Qt::Key_Left, GBA_KEY_LEFT);
74 mInputBindKey(&m_inputMap, KEYBOARD, Qt::Key_Right, GBA_KEY_RIGHT);
75
76
77#ifdef M_CORE_GBA
78 m_lux.p = this;
79 m_lux.sample = [](GBALuminanceSource* context) {
80 InputControllerLux* lux = static_cast<InputControllerLux*>(context);
81 lux->value = 0xFF - lux->p->m_luxValue;
82 };
83
84 m_lux.readLuminance = [](GBALuminanceSource* context) {
85 InputControllerLux* lux = static_cast<InputControllerLux*>(context);
86 return lux->value;
87 };
88 setLuminanceLevel(0);
89#endif
90
91 m_image.p = this;
92 m_image.startRequestImage = [](mImageSource* context, unsigned w, unsigned h, int) {
93 InputControllerImage* image = static_cast<InputControllerImage*>(context);
94 image->w = w;
95 image->h = h;
96 if (image->image.isNull()) {
97 image->image.load(":/res/no-cam.png");
98 }
99#ifdef BUILD_QT_MULTIMEDIA
100 if (image->p->m_config->getQtOption("cameraDriver").toInt() == static_cast<int>(CameraDriver::QT_MULTIMEDIA)) {
101 QByteArray camera = image->p->m_config->getQtOption("camera").toByteArray();
102 if (!camera.isNull()) {
103 QMetaObject::invokeMethod(image->p, "setCamera", Q_ARG(QByteArray, camera));
104 }
105 QMetaObject::invokeMethod(image->p, "setupCam");
106 }
107#endif
108 };
109
110 m_image.stopRequestImage = [](mImageSource* context) {
111 InputControllerImage* image = static_cast<InputControllerImage*>(context);
112#ifdef BUILD_QT_MULTIMEDIA
113 QMetaObject::invokeMethod(image->p, "teardownCam");
114#endif
115 };
116
117 m_image.requestImage = [](mImageSource* context, const void** buffer, size_t* stride, mColorFormat* format) {
118 InputControllerImage* image = static_cast<InputControllerImage*>(context);
119 QSize size;
120 {
121 QMutexLocker locker(&image->mutex);
122 if (image->outOfDate) {
123 image->resizedImage = image->image.scaled(image->w, image->h, Qt::KeepAspectRatioByExpanding);
124 image->resizedImage = image->resizedImage.convertToFormat(QImage::Format_RGB16);
125 image->outOfDate = false;
126 }
127 }
128 size = image->resizedImage.size();
129 const uint16_t* bits = reinterpret_cast<const uint16_t*>(image->resizedImage.constBits());
130 if (size.width() > image->w) {
131 bits += (size.width() - image->w) / 2;
132 }
133 if (size.height() > image->h) {
134 bits += ((size.height() - image->h) / 2) * size.width();
135 }
136 *buffer = bits;
137 *stride = image->resizedImage.bytesPerLine() / sizeof(*bits);
138 *format = mCOLOR_RGB565;
139 };
140}
141
142InputController::~InputController() {
143 mInputMapDeinit(&m_inputMap);
144
145#ifdef BUILD_SDL
146 if (m_playerAttached) {
147 mSDLDetachPlayer(&s_sdlEvents, &m_sdlPlayer);
148 }
149
150 --s_sdlInited;
151 if (s_sdlInited == 0) {
152 mSDLDeinitEvents(&s_sdlEvents);
153 }
154#endif
155}
156
157void InputController::setConfiguration(ConfigController* config) {
158 m_config = config;
159 loadConfiguration(KEYBOARD);
160#ifdef BUILD_SDL
161 mSDLEventsLoadConfig(&s_sdlEvents, config->input());
162 if (!m_playerAttached) {
163 m_playerAttached = mSDLAttachPlayer(&s_sdlEvents, &m_sdlPlayer);
164 }
165 loadConfiguration(SDL_BINDING_BUTTON);
166 loadProfile(SDL_BINDING_BUTTON, profileForType(SDL_BINDING_BUTTON));
167#endif
168}
169
170void InputController::loadConfiguration(uint32_t type) {
171 mInputMapLoad(&m_inputMap, type, m_config->input());
172#ifdef BUILD_SDL
173 if (m_playerAttached) {
174 mSDLPlayerLoadConfig(&m_sdlPlayer, m_config->input());
175 }
176#endif
177}
178
179void InputController::loadProfile(uint32_t type, const QString& profile) {
180 bool loaded = mInputProfileLoad(&m_inputMap, type, m_config->input(), profile.toUtf8().constData());
181 recalibrateAxes();
182 if (!loaded) {
183 const InputProfile* ip = InputProfile::findProfile(profile);
184 if (ip) {
185 ip->apply(this);
186 }
187 }
188 emit profileLoaded(profile);
189}
190
191void InputController::saveConfiguration() {
192 saveConfiguration(KEYBOARD);
193#ifdef BUILD_SDL
194 saveConfiguration(SDL_BINDING_BUTTON);
195 saveProfile(SDL_BINDING_BUTTON, profileForType(SDL_BINDING_BUTTON));
196 if (m_playerAttached) {
197 mSDLPlayerSaveConfig(&m_sdlPlayer, m_config->input());
198 }
199#endif
200 m_config->write();
201}
202
203void InputController::saveConfiguration(uint32_t type) {
204 mInputMapSave(&m_inputMap, type, m_config->input());
205 m_config->write();
206}
207
208void InputController::saveProfile(uint32_t type, const QString& profile) {
209 mInputProfileSave(&m_inputMap, type, m_config->input(), profile.toUtf8().constData());
210 m_config->write();
211}
212
213const char* InputController::profileForType(uint32_t type) {
214 UNUSED(type);
215#ifdef BUILD_SDL
216 if (type == SDL_BINDING_BUTTON && m_sdlPlayer.joystick) {
217#if SDL_VERSION_ATLEAST(2, 0, 0)
218 return SDL_JoystickName(m_sdlPlayer.joystick->joystick);
219#else
220 return SDL_JoystickName(SDL_JoystickIndex(m_sdlPlayer.joystick->joystick));
221#endif
222 }
223#endif
224 return 0;
225}
226
227QStringList InputController::connectedGamepads(uint32_t type) const {
228 UNUSED(type);
229
230#ifdef BUILD_SDL
231 if (type == SDL_BINDING_BUTTON) {
232 QStringList pads;
233 for (size_t i = 0; i < SDL_JoystickListSize(&s_sdlEvents.joysticks); ++i) {
234 const char* name;
235#if SDL_VERSION_ATLEAST(2, 0, 0)
236 name = SDL_JoystickName(SDL_JoystickListGetPointer(&s_sdlEvents.joysticks, i)->joystick);
237#else
238 name = SDL_JoystickName(SDL_JoystickIndex(SDL_JoystickListGetPointer(&s_sdlEvents.joysticks, i)->joystick));
239#endif
240 if (name) {
241 pads.append(QString(name));
242 } else {
243 pads.append(QString());
244 }
245 }
246 return pads;
247 }
248#endif
249
250 return QStringList();
251}
252
253int InputController::gamepad(uint32_t type) const {
254#ifdef BUILD_SDL
255 if (type == SDL_BINDING_BUTTON) {
256 return m_sdlPlayer.joystick ? m_sdlPlayer.joystick->index : 0;
257 }
258#endif
259 return 0;
260}
261
262void InputController::setGamepad(uint32_t type, int index) {
263#ifdef BUILD_SDL
264 if (type == SDL_BINDING_BUTTON) {
265 mSDLPlayerChangeJoystick(&s_sdlEvents, &m_sdlPlayer, index);
266 }
267#endif
268}
269
270void InputController::setPreferredGamepad(uint32_t type, int index) {
271 if (!m_config) {
272 return;
273 }
274 char name[34] = {0};
275 SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(SDL_JoystickListGetPointer(&s_sdlEvents.joysticks, index)->joystick), name, sizeof(name));
276 mInputSetPreferredDevice(m_config->input(), "gba", type, m_playerId, name);
277}
278
279mRumble* InputController::rumble() {
280#ifdef BUILD_SDL
281#if SDL_VERSION_ATLEAST(2, 0, 0)
282 if (m_playerAttached) {
283 return &m_sdlPlayer.rumble.d;
284 }
285#endif
286#endif
287 return nullptr;
288}
289
290mRotationSource* InputController::rotationSource() {
291#ifdef BUILD_SDL
292 if (m_playerAttached) {
293 return &m_sdlPlayer.rotation.d;
294 }
295#endif
296 return nullptr;
297}
298
299void InputController::registerTiltAxisX(int axis) {
300#ifdef BUILD_SDL
301 if (m_playerAttached) {
302 m_sdlPlayer.rotation.axisX = axis;
303 }
304#endif
305}
306
307void InputController::registerTiltAxisY(int axis) {
308#ifdef BUILD_SDL
309 if (m_playerAttached) {
310 m_sdlPlayer.rotation.axisY = axis;
311 }
312#endif
313}
314
315void InputController::registerGyroAxisX(int axis) {
316#ifdef BUILD_SDL
317 if (m_playerAttached) {
318 m_sdlPlayer.rotation.gyroX = axis;
319 }
320#endif
321}
322
323void InputController::registerGyroAxisY(int axis) {
324#ifdef BUILD_SDL
325 if (m_playerAttached) {
326 m_sdlPlayer.rotation.gyroY = axis;
327 }
328#endif
329}
330
331float InputController::gyroSensitivity() const {
332#ifdef BUILD_SDL
333 if (m_playerAttached) {
334 return m_sdlPlayer.rotation.gyroSensitivity;
335 }
336#endif
337 return 0;
338}
339
340void InputController::setGyroSensitivity(float sensitivity) {
341#ifdef BUILD_SDL
342 if (m_playerAttached) {
343 m_sdlPlayer.rotation.gyroSensitivity = sensitivity;
344 }
345#endif
346}
347
348GBAKey InputController::mapKeyboard(int key) const {
349 return static_cast<GBAKey>(mInputMapKey(&m_inputMap, KEYBOARD, key));
350}
351
352void InputController::bindKey(uint32_t type, int key, GBAKey gbaKey) {
353 return mInputBindKey(&m_inputMap, type, key, gbaKey);
354}
355
356void InputController::updateJoysticks() {
357#ifdef BUILD_SDL
358 QString profile = profileForType(SDL_BINDING_BUTTON);
359 mSDLUpdateJoysticks(&s_sdlEvents, m_config->input());
360 QString newProfile = profileForType(SDL_BINDING_BUTTON);
361 if (profile != newProfile) {
362 loadProfile(SDL_BINDING_BUTTON, newProfile);
363 }
364#endif
365}
366
367int InputController::pollEvents() {
368 int activeButtons = 0;
369#ifdef BUILD_SDL
370 if (m_playerAttached && m_sdlPlayer.joystick) {
371 SDL_Joystick* joystick = m_sdlPlayer.joystick->joystick;
372 SDL_JoystickUpdate();
373 int numButtons = SDL_JoystickNumButtons(joystick);
374 int i;
375 for (i = 0; i < numButtons; ++i) {
376 GBAKey key = static_cast<GBAKey>(mInputMapKey(&m_inputMap, SDL_BINDING_BUTTON, i));
377 if (key == GBA_KEY_NONE) {
378 continue;
379 }
380 if (hasPendingEvent(key)) {
381 continue;
382 }
383 if (SDL_JoystickGetButton(joystick, i)) {
384 activeButtons |= 1 << key;
385 }
386 }
387 int numHats = SDL_JoystickNumHats(joystick);
388 for (i = 0; i < numHats; ++i) {
389 int hat = SDL_JoystickGetHat(joystick, i);
390 activeButtons |= mInputMapHat(&m_inputMap, SDL_BINDING_BUTTON, i, hat);
391 }
392
393 int numAxes = SDL_JoystickNumAxes(joystick);
394 for (i = 0; i < numAxes; ++i) {
395 int value = SDL_JoystickGetAxis(joystick, i);
396
397 enum GBAKey key = static_cast<GBAKey>(mInputMapAxis(&m_inputMap, SDL_BINDING_BUTTON, i, value));
398 if (key != GBA_KEY_NONE) {
399 activeButtons |= 1 << key;
400 }
401 }
402 }
403#endif
404 return activeButtons;
405}
406
407QSet<int> InputController::activeGamepadButtons(int type) {
408 QSet<int> activeButtons;
409#ifdef BUILD_SDL
410 if (m_playerAttached && type == SDL_BINDING_BUTTON && m_sdlPlayer.joystick) {
411 SDL_Joystick* joystick = m_sdlPlayer.joystick->joystick;
412 SDL_JoystickUpdate();
413 int numButtons = SDL_JoystickNumButtons(joystick);
414 int i;
415 for (i = 0; i < numButtons; ++i) {
416 if (SDL_JoystickGetButton(joystick, i)) {
417 activeButtons.insert(i);
418 }
419 }
420 }
421#endif
422 return activeButtons;
423}
424
425void InputController::recalibrateAxes() {
426#ifdef BUILD_SDL
427 if (m_playerAttached && m_sdlPlayer.joystick) {
428 SDL_Joystick* joystick = m_sdlPlayer.joystick->joystick;
429 SDL_JoystickUpdate();
430 int numAxes = SDL_JoystickNumAxes(joystick);
431 if (numAxes < 1) {
432 return;
433 }
434 m_deadzones.resize(numAxes);
435 int i;
436 for (i = 0; i < numAxes; ++i) {
437 m_deadzones[i] = SDL_JoystickGetAxis(joystick, i);
438 }
439 }
440#endif
441}
442
443QSet<QPair<int, GamepadAxisEvent::Direction>> InputController::activeGamepadAxes(int type) {
444 QSet<QPair<int, GamepadAxisEvent::Direction>> activeAxes;
445#ifdef BUILD_SDL
446 if (m_playerAttached && type == SDL_BINDING_BUTTON && m_sdlPlayer.joystick) {
447 SDL_Joystick* joystick = m_sdlPlayer.joystick->joystick;
448 SDL_JoystickUpdate();
449 int numAxes = SDL_JoystickNumAxes(joystick);
450 if (numAxes < 1) {
451 return activeAxes;
452 }
453 m_deadzones.resize(numAxes);
454 int i;
455 for (i = 0; i < numAxes; ++i) {
456 int32_t axis = SDL_JoystickGetAxis(joystick, i);
457 axis -= m_deadzones[i];
458 if (axis >= AXIS_THRESHOLD || axis <= -AXIS_THRESHOLD) {
459 activeAxes.insert(qMakePair(i, axis > 0 ? GamepadAxisEvent::POSITIVE : GamepadAxisEvent::NEGATIVE));
460 }
461 }
462 }
463#endif
464 return activeAxes;
465}
466
467void InputController::bindAxis(uint32_t type, int axis, GamepadAxisEvent::Direction direction, GBAKey key) {
468 const mInputAxis* old = mInputQueryAxis(&m_inputMap, type, axis);
469 mInputAxis description = { GBA_KEY_NONE, GBA_KEY_NONE, -AXIS_THRESHOLD, AXIS_THRESHOLD };
470 if (old) {
471 description = *old;
472 }
473 int deadzone = 0;
474 if (axis > 0 && m_deadzones.size() > axis) {
475 deadzone = m_deadzones[axis];
476 }
477 switch (direction) {
478 case GamepadAxisEvent::NEGATIVE:
479 description.lowDirection = key;
480
481 description.deadLow = deadzone - AXIS_THRESHOLD;
482 break;
483 case GamepadAxisEvent::POSITIVE:
484 description.highDirection = key;
485 description.deadHigh = deadzone + AXIS_THRESHOLD;
486 break;
487 default:
488 return;
489 }
490 mInputBindAxis(&m_inputMap, type, axis, &description);
491}
492
493void InputController::unbindAllAxes(uint32_t type) {
494 mInputUnbindAllAxes(&m_inputMap, type);
495}
496
497QSet<QPair<int, GamepadHatEvent::Direction>> InputController::activeGamepadHats(int type) {
498 QSet<QPair<int, GamepadHatEvent::Direction>> activeHats;
499#ifdef BUILD_SDL
500 if (m_playerAttached && type == SDL_BINDING_BUTTON && m_sdlPlayer.joystick) {
501 SDL_Joystick* joystick = m_sdlPlayer.joystick->joystick;
502 SDL_JoystickUpdate();
503 int numHats = SDL_JoystickNumHats(joystick);
504 if (numHats < 1) {
505 return activeHats;
506 }
507
508 int i;
509 for (i = 0; i < numHats; ++i) {
510 int hat = SDL_JoystickGetHat(joystick, i);
511 if (hat & GamepadHatEvent::UP) {
512 activeHats.insert(qMakePair(i, GamepadHatEvent::UP));
513 }
514 if (hat & GamepadHatEvent::RIGHT) {
515 activeHats.insert(qMakePair(i, GamepadHatEvent::RIGHT));
516 }
517 if (hat & GamepadHatEvent::DOWN) {
518 activeHats.insert(qMakePair(i, GamepadHatEvent::DOWN));
519 }
520 if (hat & GamepadHatEvent::LEFT) {
521 activeHats.insert(qMakePair(i, GamepadHatEvent::LEFT));
522 }
523 }
524 }
525#endif
526 return activeHats;
527}
528
529void InputController::bindHat(uint32_t type, int hat, GamepadHatEvent::Direction direction, GBAKey gbaKey) {
530 mInputHatBindings bindings{ -1, -1, -1, -1 };
531 mInputQueryHat(&m_inputMap, type, hat, &bindings);
532 switch (direction) {
533 case GamepadHatEvent::UP:
534 bindings.up = gbaKey;
535 break;
536 case GamepadHatEvent::RIGHT:
537 bindings.right = gbaKey;
538 break;
539 case GamepadHatEvent::DOWN:
540 bindings.down = gbaKey;
541 break;
542 case GamepadHatEvent::LEFT:
543 bindings.left = gbaKey;
544 break;
545 default:
546 return;
547 }
548 mInputBindHat(&m_inputMap, type, hat, &bindings);
549}
550
551void InputController::testGamepad(int type) {
552 auto activeAxes = activeGamepadAxes(type);
553 auto oldAxes = m_activeAxes;
554 m_activeAxes = activeAxes;
555
556 auto activeButtons = activeGamepadButtons(type);
557 auto oldButtons = m_activeButtons;
558 m_activeButtons = activeButtons;
559
560 auto activeHats = activeGamepadHats(type);
561 auto oldHats = m_activeHats;
562 m_activeHats = activeHats;
563
564 if (!QApplication::focusWidget()) {
565 return;
566 }
567
568 activeAxes.subtract(oldAxes);
569 oldAxes.subtract(m_activeAxes);
570
571 for (auto& axis : m_activeAxes) {
572 bool newlyAboveThreshold = activeAxes.contains(axis);
573 if (newlyAboveThreshold) {
574 GamepadAxisEvent* event = new GamepadAxisEvent(axis.first, axis.second, newlyAboveThreshold, type, this);
575 postPendingEvent(event->gbaKey());
576 sendGamepadEvent(event);
577 if (!event->isAccepted()) {
578 clearPendingEvent(event->gbaKey());
579 }
580 }
581 }
582 for (auto axis : oldAxes) {
583 GamepadAxisEvent* event = new GamepadAxisEvent(axis.first, axis.second, false, type, this);
584 clearPendingEvent(event->gbaKey());
585 sendGamepadEvent(event);
586 }
587
588 if (!QApplication::focusWidget()) {
589 return;
590 }
591
592 activeButtons.subtract(oldButtons);
593 oldButtons.subtract(m_activeButtons);
594
595 for (int button : activeButtons) {
596 GamepadButtonEvent* event = new GamepadButtonEvent(GamepadButtonEvent::Down(), button, type, this);
597 postPendingEvent(event->gbaKey());
598 sendGamepadEvent(event);
599 if (!event->isAccepted()) {
600 clearPendingEvent(event->gbaKey());
601 }
602 }
603 for (int button : oldButtons) {
604 GamepadButtonEvent* event = new GamepadButtonEvent(GamepadButtonEvent::Up(), button, type, this);
605 clearPendingEvent(event->gbaKey());
606 sendGamepadEvent(event);
607 }
608
609 activeHats.subtract(oldHats);
610 oldHats.subtract(m_activeHats);
611
612 for (auto& hat : activeHats) {
613 GamepadHatEvent* event = new GamepadHatEvent(GamepadHatEvent::Down(), hat.first, hat.second, type, this);
614 postPendingEvent(event->gbaKey());
615 sendGamepadEvent(event);
616 if (!event->isAccepted()) {
617 clearPendingEvent(event->gbaKey());
618 }
619 }
620 for (auto& hat : oldHats) {
621 GamepadHatEvent* event = new GamepadHatEvent(GamepadHatEvent::Up(), hat.first, hat.second, type, this);
622 clearPendingEvent(event->gbaKey());
623 sendGamepadEvent(event);
624 }
625}
626
627void InputController::sendGamepadEvent(QEvent* event) {
628 QWidget* focusWidget = nullptr;
629 if (m_focusParent) {
630 focusWidget = m_focusParent->focusWidget();
631 if (!focusWidget) {
632 focusWidget = m_focusParent;
633 }
634 } else {
635 focusWidget = QApplication::focusWidget();
636 }
637 QApplication::sendEvent(focusWidget, event);
638}
639
640void InputController::postPendingEvent(GBAKey key) {
641 m_pendingEvents.insert(key);
642}
643
644void InputController::clearPendingEvent(GBAKey key) {
645 m_pendingEvents.remove(key);
646}
647
648bool InputController::hasPendingEvent(GBAKey key) const {
649 return m_pendingEvents.contains(key);
650}
651
652void InputController::suspendScreensaver() {
653#ifdef BUILD_SDL
654#if SDL_VERSION_ATLEAST(2, 0, 0)
655 mSDLSuspendScreensaver(&s_sdlEvents);
656#endif
657#endif
658}
659
660void InputController::resumeScreensaver() {
661#ifdef BUILD_SDL
662#if SDL_VERSION_ATLEAST(2, 0, 0)
663 mSDLResumeScreensaver(&s_sdlEvents);
664#endif
665#endif
666}
667
668void InputController::setScreensaverSuspendable(bool suspendable) {
669#ifdef BUILD_SDL
670#if SDL_VERSION_ATLEAST(2, 0, 0)
671 mSDLSetScreensaverSuspendable(&s_sdlEvents, suspendable);
672#endif
673#endif
674}
675
676void InputController::stealFocus(QWidget* focus) {
677 m_focusParent = focus;
678}
679
680void InputController::releaseFocus(QWidget* focus) {
681 if (focus == m_focusParent) {
682 m_focusParent = m_topLevel;
683 }
684}
685
686void InputController::loadCamImage(const QString& path) {
687 setCamImage(QImage(path));
688}
689
690void InputController::setCamImage(const QImage& image) {
691 if (image.isNull()) {
692 return;
693 }
694 QMutexLocker locker(&m_image.mutex);
695 m_image.image = image;
696 m_image.resizedImage = QImage();
697 m_image.outOfDate = true;
698}
699
700QList<QPair<QByteArray, QString>> InputController::listCameras() const {
701 QList<QPair<QByteArray, QString>> out;
702#ifdef BUILD_QT_MULTIMEDIA
703 QList<QCameraInfo> cams = QCameraInfo::availableCameras();
704 for (const auto& cam : cams) {
705 out.append(qMakePair(cam.deviceName().toLatin1(), cam.description()));
706 }
707#endif
708 return out;
709}
710
711void InputController::increaseLuminanceLevel() {
712 setLuminanceLevel(m_luxLevel + 1);
713}
714
715void InputController::decreaseLuminanceLevel() {
716 setLuminanceLevel(m_luxLevel - 1);
717}
718
719void InputController::setLuminanceLevel(int level) {
720 int value = 0x16;
721 level = std::max(0, std::min(10, level));
722 if (level > 0) {
723 value += GBA_LUX_LEVELS[level - 1];
724 }
725 setLuminanceValue(value);
726}
727
728void InputController::setLuminanceValue(uint8_t value) {
729 m_luxValue = value;
730 value = std::max<int>(value - 0x16, 0);
731 m_luxLevel = 10;
732 for (int i = 0; i < 10; ++i) {
733 if (value < GBA_LUX_LEVELS[i]) {
734 m_luxLevel = i;
735 break;
736 }
737 }
738 emit luminanceValueChanged(m_luxValue);
739}
740
741void InputController::setupCam() {
742#ifdef BUILD_QT_MULTIMEDIA
743 if (!m_camera) {
744 m_camera = std::make_unique<QCamera>();
745 connect(m_camera.get(), &QCamera::statusChanged, this, &InputController::prepareCamSettings, Qt::QueuedConnection);
746 }
747 m_camera->setCaptureMode(QCamera::CaptureVideo);
748 m_camera->setViewfinder(&m_videoDumper);
749 m_camera->load();
750#endif
751}
752
753#ifdef BUILD_QT_MULTIMEDIA
754void InputController::prepareCamSettings(QCamera::Status status) {
755 if (status != QCamera::LoadedStatus || m_camera->state() == QCamera::ActiveState) {
756 return;
757 }
758#if (QT_VERSION >= QT_VERSION_CHECK(5, 5, 0))
759 QVideoFrame::PixelFormat format(QVideoFrame::Format_RGB32);
760 QCameraViewfinderSettings settings;
761 QSize size(1280, 720);
762 auto cameraRes = m_camera->supportedViewfinderResolutions(settings);
763 for (auto& cameraSize : cameraRes) {
764 if (cameraSize.width() < m_image.w || cameraSize.height() < m_image.h) {
765 continue;
766 }
767 if (cameraSize.width() <= size.width() && cameraSize.height() <= size.height()) {
768 size = cameraSize;
769 }
770 }
771 settings.setResolution(size);
772
773 auto cameraFormats = m_camera->supportedViewfinderPixelFormats(settings);
774 auto goodFormats = m_videoDumper.supportedPixelFormats();
775 bool goodFormatFound = false;
776 for (const auto& goodFormat : goodFormats) {
777 if (cameraFormats.contains(goodFormat)) {
778 settings.setPixelFormat(goodFormat);
779 format = goodFormat;
780 goodFormatFound = true;
781 break;
782 }
783 }
784 if (!goodFormatFound) {
785 LOG(QT, WARN) << "Could not find a valid camera format!";
786 for (const auto& format : cameraFormats) {
787 LOG(QT, WARN) << "Camera supported format: " << QString::number(format);
788 }
789 }
790 m_camera->setViewfinderSettings(settings);
791#endif
792 m_camera->start();
793}
794#endif
795
796void InputController::teardownCam() {
797#ifdef BUILD_QT_MULTIMEDIA
798 if (m_camera) {
799 m_camera->stop();
800 }
801#endif
802}
803
804void InputController::setCamera(const QByteArray& name) {
805#ifdef BUILD_QT_MULTIMEDIA
806 bool needsRestart = false;
807 if (m_camera) {
808 needsRestart = m_camera->state() == QCamera::ActiveState;
809 }
810 m_camera = std::make_unique<QCamera>(name);
811 connect(m_camera.get(), &QCamera::statusChanged, this, &InputController::prepareCamSettings, Qt::QueuedConnection);
812 if (needsRestart) {
813 setupCam();
814 }
815#endif
816}