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

View File

@ -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()

View File

@ -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)
}

View File

@ -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()
}

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: {
// 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: {

View File

@ -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

View File

@ -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;
}

View File

@ -350,7 +350,6 @@ int main(int argc, char *argv[])
qmlRegisterType<ComputerModel>("ComputerModel", 1, 0, "ComputerModel");
qmlRegisterType<AppModel>("AppModel", 1, 0, "AppModel");
qmlRegisterType<StreamingPreferences>("StreamingPreferences", 1, 0, "StreamingPreferences");
qmlRegisterType<SdlGamepadKeyNavigation>("SdlGamepadKeyNavigation", 1, 0, "SdlGamepadKeyNavigation");
qmlRegisterUncreatableType<Session>("Session", 1, 0, "Session", "Session cannot be created from QML");
qmlRegisterSingletonType<ComputerManager>("ComputerManager", 1, 0,
"ComputerManager",
@ -367,6 +366,12 @@ int main(int argc, char *argv[])
[](QQmlEngine*, QJSEngine*) -> QObject* {
return new SystemProperties();
});
qmlRegisterSingletonType<SdlGamepadKeyNavigation>("SdlGamepadKeyNavigation", 1, 0,
"SdlGamepadKeyNavigation",
[](QQmlEngine*, QJSEngine*) -> QObject* {
return new SdlGamepadKeyNavigation();
});
#ifndef Q_OS_WINRT
// Use the dense material dark theme by default