Refactor SdlGamepadKeyNavigation to avoid multiple reinitializations of SDL

This commit is contained in:
Cameron Gutman
2019-03-23 14:15:55 -07:00
parent 03c0be0720
commit d2cff86c97
7 changed files with 27 additions and 45 deletions
-7
View File
@@ -4,7 +4,6 @@ import QtQuick.Controls 2.2
import AppModel 1.0 import AppModel 1.0
import ComputerManager 1.0 import ComputerManager 1.0
import SdlGamepadKeyNavigation 1.0
GridView { GridView {
property int computerIndex property int computerIndex
@@ -26,10 +25,6 @@ GridView {
stackView.pop() stackView.pop()
} }
SdlGamepadKeyNavigation {
id: gamepadKeyNav
}
Component.onCompleted: { Component.onCompleted: {
// Don't show any highlighted item until interacting with them. // Don't show any highlighted item until interacting with them.
// We do this here instead of onActivated to avoid losing the user's // We do this here instead of onActivated to avoid losing the user's
@@ -39,12 +34,10 @@ GridView {
StackView.onActivated: { StackView.onActivated: {
appModel.computerLost.connect(computerLost) appModel.computerLost.connect(computerLost)
gamepadKeyNav.enable()
} }
StackView.onDeactivating: { StackView.onDeactivating: {
appModel.computerLost.disconnect(computerLost) appModel.computerLost.disconnect(computerLost)
gamepadKeyNav.disable()
} }
function createModel() function createModel()
-8
View File
@@ -7,7 +7,6 @@ import QtQuick.Window 2.2
import ComputerModel 1.0 import ComputerModel 1.0
import ComputerManager 1.0 import ComputerManager 1.0
import SdlGamepadKeyNavigation 1.0
GridView { GridView {
property ComputerModel computerModel : createModel() property ComputerModel computerModel : createModel()
@@ -23,10 +22,6 @@ GridView {
cellWidth: 350; cellHeight: 350; cellWidth: 350; cellHeight: 350;
objectName: "Computers" objectName: "Computers"
SdlGamepadKeyNavigation {
id: gamepadKeyNav
}
Component.onCompleted: { Component.onCompleted: {
// Don't show any highlighted item until interacting with them. // Don't show any highlighted item until interacting with them.
// We do this here instead of onActivated to avoid losing the user's // We do this here instead of onActivated to avoid losing the user's
@@ -35,14 +30,11 @@ GridView {
} }
StackView.onActivated: { StackView.onActivated: {
gamepadKeyNav.enable()
// Setup signals on CM // Setup signals on CM
ComputerManager.computerAddCompleted.connect(addComplete) ComputerManager.computerAddCompleted.connect(addComplete)
} }
StackView.onDeactivating: { StackView.onDeactivating: {
gamepadKeyNav.disable()
ComputerManager.computerAddCompleted.disconnect(addComplete) ComputerManager.computerAddCompleted.disconnect(addComplete)
} }
+2 -7
View File
@@ -29,17 +29,12 @@ Flickable {
id: prefs id: prefs
} }
SdlGamepadKeyNavigation {
id: gamepadKeyNav
}
StackView.onActivated: { StackView.onActivated: {
gamepadKeyNav.setSettingsMode(true) SdlGamepadKeyNavigation.setSettingsMode(true)
gamepadKeyNav.enable()
} }
StackView.onDeactivating: { StackView.onDeactivating: {
gamepadKeyNav.disable() SdlGamepadKeyNavigation.setSettingsMode(false)
prefs.save() prefs.save()
} }
+11 -8
View File
@@ -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: { StackView.onDeactivating: {
// Show the toolbar again when popped off the stack // Show the toolbar again when popped off the stack
toolBar.visible = true toolBar.visible = true
// Enable GUI gamepad usage now
SdlGamepadKeyNavigation.enable()
} }
StackView.onActivated: { StackView.onActivated: {
@@ -133,10 +129,13 @@ Item {
// in the hintText control itself to synchronize // in the hintText control itself to synchronize
// with Session.exec() which requires no concurrent // with Session.exec() which requires no concurrent
// gamepad usage. // gamepad usage.
hintText.text = gamepadKeyNav.getConnectedGamepads() > 0 ? hintText.text = SdlGamepadKeyNavigation.getConnectedGamepads() > 0 ?
"Tip: Press Start+Select+L1+R1 to disconnect your session" : "Tip: Press Start+Select+L1+R1 to disconnect your session" :
"Tip: Press Ctrl+Alt+Shift+Q 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 // Run the streaming session to completion
session.exec(Screen.virtualX, Screen.virtualY) session.exec(Screen.virtualX, Screen.virtualY)
} }
@@ -181,6 +180,10 @@ Item {
if (!visible) { if (!visible) {
Qt.quit() Qt.quit()
} }
else {
// Enable GUI gamepad usage now
SdlGamepadKeyNavigation.enable()
}
} }
onHelp: { onHelp: {
+7 -2
View File
@@ -8,6 +8,7 @@ import ComputerManager 1.0
import AutoUpdateChecker 1.0 import AutoUpdateChecker 1.0
import StreamingPreferences 1.0 import StreamingPreferences 1.0
import SystemProperties 1.0 import SystemProperties 1.0
import SdlGamepadKeyNavigation 1.0
ApplicationWindow { ApplicationWindow {
property bool pollingActive: false property bool pollingActive: false
@@ -17,12 +18,16 @@ ApplicationWindow {
width: 1280 width: 1280
height: 600 height: 600
visibility: prefs.startWindowed ? "Windowed" : "Maximized"
StreamingPreferences { StreamingPreferences {
id: prefs id: prefs
} }
visibility: prefs.startWindowed ? "Windowed" : "Maximized"
Component.onCompleted: {
SdlGamepadKeyNavigation.enable()
}
StackView { StackView {
id: stackView id: stackView
initialItem: initialView initialItem: initialView
+1 -12
View File
@@ -247,17 +247,7 @@ void SdlGamepadKeyNavigation::setSettingsMode(bool settingsMode)
int SdlGamepadKeyNavigation::getConnectedGamepads() int SdlGamepadKeyNavigation::getConnectedGamepads()
{ {
if (SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER) != 0) { Q_ASSERT(m_Enabled);
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();
int count = 0; int count = 0;
for (int i = 0; i < SDL_NumJoysticks(); i++) { for (int i = 0; i < SDL_NumJoysticks(); i++) {
@@ -266,6 +256,5 @@ int SdlGamepadKeyNavigation::getConnectedGamepads()
} }
} }
SDL_QuitSubSystem(SDL_INIT_GAMECONTROLLER);
return count; return count;
} }
+6 -1
View File
@@ -350,7 +350,6 @@ int main(int argc, char *argv[])
qmlRegisterType<ComputerModel>("ComputerModel", 1, 0, "ComputerModel"); qmlRegisterType<ComputerModel>("ComputerModel", 1, 0, "ComputerModel");
qmlRegisterType<AppModel>("AppModel", 1, 0, "AppModel"); qmlRegisterType<AppModel>("AppModel", 1, 0, "AppModel");
qmlRegisterType<StreamingPreferences>("StreamingPreferences", 1, 0, "StreamingPreferences"); qmlRegisterType<StreamingPreferences>("StreamingPreferences", 1, 0, "StreamingPreferences");
qmlRegisterType<SdlGamepadKeyNavigation>("SdlGamepadKeyNavigation", 1, 0, "SdlGamepadKeyNavigation");
qmlRegisterUncreatableType<Session>("Session", 1, 0, "Session", "Session cannot be created from QML"); qmlRegisterUncreatableType<Session>("Session", 1, 0, "Session", "Session cannot be created from QML");
qmlRegisterSingletonType<ComputerManager>("ComputerManager", 1, 0, qmlRegisterSingletonType<ComputerManager>("ComputerManager", 1, 0,
"ComputerManager", "ComputerManager",
@@ -367,6 +366,12 @@ int main(int argc, char *argv[])
[](QQmlEngine*, QJSEngine*) -> QObject* { [](QQmlEngine*, QJSEngine*) -> QObject* {
return new SystemProperties(); return new SystemProperties();
}); });
qmlRegisterSingletonType<SdlGamepadKeyNavigation>("SdlGamepadKeyNavigation", 1, 0,
"SdlGamepadKeyNavigation",
[](QQmlEngine*, QJSEngine*) -> QObject* {
return new SdlGamepadKeyNavigation();
});
#ifndef Q_OS_WINRT #ifndef Q_OS_WINRT
// Use the dense material dark theme by default // Use the dense material dark theme by default