From d2cff86c974717e0c5e4ed8319f1085a022efce2 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Sat, 23 Mar 2019 14:15:55 -0700 Subject: [PATCH] Refactor SdlGamepadKeyNavigation to avoid multiple reinitializations of SDL --- app/gui/AppView.qml | 7 ------- app/gui/PcView.qml | 8 -------- app/gui/SettingsView.qml | 9 ++------- app/gui/StreamSegue.qml | 19 +++++++++++-------- app/gui/main.qml | 9 +++++++-- app/gui/sdlgamepadkeynavigation.cpp | 13 +------------ app/main.cpp | 7 ++++++- 7 files changed, 27 insertions(+), 45 deletions(-) diff --git a/app/gui/AppView.qml b/app/gui/AppView.qml index 6ead3208..2d5d35a9 100644 --- a/app/gui/AppView.qml +++ b/app/gui/AppView.qml @@ -4,7 +4,6 @@ import QtQuick.Controls 2.2 import AppModel 1.0 import ComputerManager 1.0 -import SdlGamepadKeyNavigation 1.0 GridView { property int computerIndex @@ -26,10 +25,6 @@ GridView { stackView.pop() } - SdlGamepadKeyNavigation { - id: gamepadKeyNav - } - Component.onCompleted: { // Don't show any highlighted item until interacting with them. // We do this here instead of onActivated to avoid losing the user's @@ -39,12 +34,10 @@ GridView { StackView.onActivated: { appModel.computerLost.connect(computerLost) - gamepadKeyNav.enable() } StackView.onDeactivating: { appModel.computerLost.disconnect(computerLost) - gamepadKeyNav.disable() } function createModel() diff --git a/app/gui/PcView.qml b/app/gui/PcView.qml index c544a7bc..0b990b5d 100644 --- a/app/gui/PcView.qml +++ b/app/gui/PcView.qml @@ -7,7 +7,6 @@ import QtQuick.Window 2.2 import ComputerModel 1.0 import ComputerManager 1.0 -import SdlGamepadKeyNavigation 1.0 GridView { property ComputerModel computerModel : createModel() @@ -23,10 +22,6 @@ GridView { cellWidth: 350; cellHeight: 350; objectName: "Computers" - SdlGamepadKeyNavigation { - id: gamepadKeyNav - } - Component.onCompleted: { // Don't show any highlighted item until interacting with them. // We do this here instead of onActivated to avoid losing the user's @@ -35,14 +30,11 @@ GridView { } StackView.onActivated: { - gamepadKeyNav.enable() - // Setup signals on CM ComputerManager.computerAddCompleted.connect(addComplete) } StackView.onDeactivating: { - gamepadKeyNav.disable() ComputerManager.computerAddCompleted.disconnect(addComplete) } diff --git a/app/gui/SettingsView.qml b/app/gui/SettingsView.qml index 72991607..8217c0e4 100644 --- a/app/gui/SettingsView.qml +++ b/app/gui/SettingsView.qml @@ -29,17 +29,12 @@ Flickable { id: prefs } - SdlGamepadKeyNavigation { - id: gamepadKeyNav - } - StackView.onActivated: { - gamepadKeyNav.setSettingsMode(true) - gamepadKeyNav.enable() + SdlGamepadKeyNavigation.setSettingsMode(true) } StackView.onDeactivating: { - gamepadKeyNav.disable() + SdlGamepadKeyNavigation.setSettingsMode(false) prefs.save() } diff --git a/app/gui/StreamSegue.qml b/app/gui/StreamSegue.qml index e8ecb29c..4e776205 100644 --- a/app/gui/StreamSegue.qml +++ b/app/gui/StreamSegue.qml @@ -94,16 +94,12 @@ Item { } } - // It's important that we don't call enable() here - // or it may interfere with the Session instance - // getting notified of initial connected gamepads. - SdlGamepadKeyNavigation { - id: gamepadKeyNav - } - StackView.onDeactivating: { // Show the toolbar again when popped off the stack toolBar.visible = true + + // Enable GUI gamepad usage now + SdlGamepadKeyNavigation.enable() } StackView.onActivated: { @@ -133,10 +129,13 @@ Item { // in the hintText control itself to synchronize // with Session.exec() which requires no concurrent // gamepad usage. - hintText.text = gamepadKeyNav.getConnectedGamepads() > 0 ? + hintText.text = SdlGamepadKeyNavigation.getConnectedGamepads() > 0 ? "Tip: Press Start+Select+L1+R1 to disconnect your session" : "Tip: Press Ctrl+Alt+Shift+Q to disconnect your session" + // Stop GUI gamepad usage now + SdlGamepadKeyNavigation.disable() + // Run the streaming session to completion session.exec(Screen.virtualX, Screen.virtualY) } @@ -181,6 +180,10 @@ Item { if (!visible) { Qt.quit() } + else { + // Enable GUI gamepad usage now + SdlGamepadKeyNavigation.enable() + } } onHelp: { diff --git a/app/gui/main.qml b/app/gui/main.qml index 43122940..cf4b7e55 100644 --- a/app/gui/main.qml +++ b/app/gui/main.qml @@ -8,6 +8,7 @@ import ComputerManager 1.0 import AutoUpdateChecker 1.0 import StreamingPreferences 1.0 import SystemProperties 1.0 +import SdlGamepadKeyNavigation 1.0 ApplicationWindow { property bool pollingActive: false @@ -17,12 +18,16 @@ ApplicationWindow { width: 1280 height: 600 - visibility: prefs.startWindowed ? "Windowed" : "Maximized" - StreamingPreferences { id: prefs } + visibility: prefs.startWindowed ? "Windowed" : "Maximized" + + Component.onCompleted: { + SdlGamepadKeyNavigation.enable() + } + StackView { id: stackView initialItem: initialView diff --git a/app/gui/sdlgamepadkeynavigation.cpp b/app/gui/sdlgamepadkeynavigation.cpp index 9f48b001..a06b3f46 100644 --- a/app/gui/sdlgamepadkeynavigation.cpp +++ b/app/gui/sdlgamepadkeynavigation.cpp @@ -247,17 +247,7 @@ void SdlGamepadKeyNavigation::setSettingsMode(bool settingsMode) int SdlGamepadKeyNavigation::getConnectedGamepads() { - if (SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER) != 0) { - SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, - "SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER) failed: %s", - SDL_GetError()); - return 0; - } - - // Applying mappings is necessary to ensure gamepad without - // a built-in mapping are properly counted. - MappingManager mappingManager; - mappingManager.applyMappings(); + Q_ASSERT(m_Enabled); int count = 0; for (int i = 0; i < SDL_NumJoysticks(); i++) { @@ -266,6 +256,5 @@ int SdlGamepadKeyNavigation::getConnectedGamepads() } } - SDL_QuitSubSystem(SDL_INIT_GAMECONTROLLER); return count; } diff --git a/app/main.cpp b/app/main.cpp index 187834bd..12b2a25a 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -350,7 +350,6 @@ int main(int argc, char *argv[]) qmlRegisterType("ComputerModel", 1, 0, "ComputerModel"); qmlRegisterType("AppModel", 1, 0, "AppModel"); qmlRegisterType("StreamingPreferences", 1, 0, "StreamingPreferences"); - qmlRegisterType("SdlGamepadKeyNavigation", 1, 0, "SdlGamepadKeyNavigation"); qmlRegisterUncreatableType("Session", 1, 0, "Session", "Session cannot be created from QML"); qmlRegisterSingletonType("ComputerManager", 1, 0, "ComputerManager", @@ -367,6 +366,12 @@ int main(int argc, char *argv[]) [](QQmlEngine*, QJSEngine*) -> QObject* { return new SystemProperties(); }); + qmlRegisterSingletonType("SdlGamepadKeyNavigation", 1, 0, + "SdlGamepadKeyNavigation", + [](QQmlEngine*, QJSEngine*) -> QObject* { + return new SdlGamepadKeyNavigation(); + }); + #ifndef Q_OS_WINRT // Use the dense material dark theme by default