From f672b8534f93165efa49757d92c5c443a57a0e02 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Sun, 28 Oct 2018 17:59:07 -0700 Subject: [PATCH] Change quit tip based on whether gamepads are attached --- app/gui/StreamSegue.qml | 18 +++++++++++++++++- app/gui/sdlgamepadkeynavigation.cpp | 20 ++++++++++++++++++++ app/gui/sdlgamepadkeynavigation.h | 2 ++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/app/gui/StreamSegue.qml b/app/gui/StreamSegue.qml index 013a6832..09a40a5f 100644 --- a/app/gui/StreamSegue.qml +++ b/app/gui/StreamSegue.qml @@ -3,6 +3,7 @@ import QtQuick.Controls 2.2 import QtQuick.Dialogs 1.2 import QtQuick.Window 2.2 +import SdlGamepadKeyNavigation 1.0 import Session 1.0 Item { @@ -58,11 +59,26 @@ Item { toast.visible = true } + // 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 + } + onVisibleChanged: { if (visible) { // Hide the toolbar before we start loading toolBar.visible = false + // Set the hint text. We do this here rather than + // in the hintText control itself to synchronize + // with Session.exec() which requires no concurrent + // gamepad usage. + hintText.text = gamepadKeyNav.getConnectedGamepads() > 0 ? + "Tip: Press Start+Select+L1+R1 to disconnect your session" : + "Tip: Press Ctrl+Alt+Shift+Q to disconnect your session" + // Hook up our signals session.stageStarting.connect(stageStarting) session.stageFailed.connect(stageFailed) @@ -118,7 +134,7 @@ Item { } Label { - text: "Tip: Press Ctrl+Alt+Shift+Q to disconnect your session" + id: hintText anchors.bottom: parent.bottom anchors.bottomMargin: 50 anchors.horizontalCenter: parent.horizontalCenter diff --git a/app/gui/sdlgamepadkeynavigation.cpp b/app/gui/sdlgamepadkeynavigation.cpp index 65d45dd2..3f1f7047 100644 --- a/app/gui/sdlgamepadkeynavigation.cpp +++ b/app/gui/sdlgamepadkeynavigation.cpp @@ -244,3 +244,23 @@ void SdlGamepadKeyNavigation::setSettingsMode(bool settingsMode) { m_SettingsMode = 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; + } + + int count = 0; + for (int i = 0; i < SDL_NumJoysticks(); i++) { + if (SDL_IsGameController(i)) { + count++; + } + } + + SDL_QuitSubSystem(SDL_INIT_GAMECONTROLLER); + return count; +} diff --git a/app/gui/sdlgamepadkeynavigation.h b/app/gui/sdlgamepadkeynavigation.h index b516a901..900b7edf 100644 --- a/app/gui/sdlgamepadkeynavigation.h +++ b/app/gui/sdlgamepadkeynavigation.h @@ -20,6 +20,8 @@ public: Q_INVOKABLE void setSettingsMode(bool settingsMode); + Q_INVOKABLE int getConnectedGamepads(); + private: void sendKey(QEvent::Type type, Qt::Key key, Qt::KeyboardModifiers modifiers = Qt::NoModifier);