Repository navigation
Ensure Switch controller paradigm exists and recover Mac controllers after sleep #87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
actuallyitsnathaniel
wants to merge
2
commits into
ThatGuyMcd:main
Choose a base branch
from
actuallyitsnathaniel:controller-map-etiquette-and-mac-wake
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25,478 changes: 12,739 additions & 12,739 deletions
25,478
patches/release-prep/v1.0.7/source-capsule-r2/manifest.json
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| #pragma once | ||
|
|
||
| #include <algorithm> | ||
| #include <array> | ||
| #include <cctype> | ||
| #include <cstddef> | ||
| #include <cstdint> | ||
| #include <string_view> | ||
|
|
||
| namespace dkr::runtime::controllers { | ||
|
|
||
| // Face-button reporting for Nintendo diamond pads. Xbox/PlayStation keep SDL | ||
| // position numbering. Switch Pro / Joy-Con are normalised to printed letters | ||
| // (physical A reports as SDL A) so stock bindings match Nintendo etiquette. | ||
|
|
||
| enum class ControllerFamily : std::uint8_t { | ||
| Xbox = 0, | ||
| PlayStation = 1, | ||
| Switch = 2, | ||
| N64 = 3, | ||
| GameCube = 4, | ||
| }; | ||
|
|
||
| inline constexpr int kSdlControllerTypeUnknown = 0; | ||
| inline constexpr int kSdlControllerTypeXbox360 = 1; | ||
| inline constexpr int kSdlControllerTypeXboxOne = 2; | ||
| inline constexpr int kSdlControllerTypePs3 = 3; | ||
| inline constexpr int kSdlControllerTypePs4 = 4; | ||
| inline constexpr int kSdlControllerTypeNintendoSwitchPro = 5; | ||
| inline constexpr int kSdlControllerTypeVirtual = 6; | ||
| inline constexpr int kSdlControllerTypePs5 = 7; | ||
| inline constexpr int kSdlControllerTypeAmazonLuna = 8; | ||
| inline constexpr int kSdlControllerTypeGoogleStadia = 9; | ||
| inline constexpr int kSdlControllerTypeNvidiaShield = 10; | ||
| inline constexpr int kSdlControllerTypeJoyConLeft = 11; | ||
| inline constexpr int kSdlControllerTypeJoyConRight = 12; | ||
| inline constexpr int kSdlControllerTypeJoyConPair = 13; | ||
|
|
||
| inline bool contains_ci(std::string_view haystack, std::string_view needle) { | ||
| if (needle.empty() || haystack.size() < needle.size()) { | ||
| return false; | ||
| } | ||
| const auto match_at = [&](std::size_t offset) { | ||
| for (std::size_t index = 0; index < needle.size(); ++index) { | ||
| const auto left = static_cast<unsigned char>(haystack[offset + index]); | ||
| const auto right = static_cast<unsigned char>(needle[index]); | ||
| if (std::tolower(left) != std::tolower(right)) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| }; | ||
| for (std::size_t offset = 0; offset + needle.size() <= haystack.size(); | ||
| ++offset) { | ||
| if (match_at(offset)) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| inline bool name_looks_like_n64(std::string_view name) { | ||
| return contains_ci(name, "n64") || contains_ci(name, "nintendo 64"); | ||
| } | ||
|
|
||
| inline bool name_looks_like_gamecube(std::string_view name) { | ||
| return contains_ci(name, "gamecube") || contains_ci(name, "game cube") || | ||
| contains_ci(name, "nso gc") || contains_ci(name, "raphnet gc") || | ||
| contains_ci(name, "gc adapter") || | ||
| contains_ci(name, "gc and n64") || | ||
| contains_ci(name, "gamecube controller"); | ||
| } | ||
|
|
||
| inline bool name_looks_like_switch(std::string_view name) { | ||
| if (name_looks_like_n64(name) || name_looks_like_gamecube(name)) { | ||
| return false; | ||
| } | ||
| return contains_ci(name, "switch pro") || contains_ci(name, "joy-con") || | ||
| contains_ci(name, "joycon") || | ||
| contains_ci(name, "nintendo switch") || | ||
| contains_ci(name, "nso snes"); | ||
| } | ||
|
|
||
| inline ControllerFamily family_from_sdl_type(int sdl_type) { | ||
| switch (sdl_type) { | ||
| case kSdlControllerTypePs3: | ||
| case kSdlControllerTypePs4: | ||
| case kSdlControllerTypePs5: | ||
| return ControllerFamily::PlayStation; | ||
| case kSdlControllerTypeNintendoSwitchPro: | ||
| case kSdlControllerTypeJoyConLeft: | ||
| case kSdlControllerTypeJoyConRight: | ||
| case kSdlControllerTypeJoyConPair: | ||
| return ControllerFamily::Switch; | ||
| default: | ||
| return ControllerFamily::Xbox; | ||
| } | ||
| } | ||
|
|
||
| // Prefer N64/GameCube name hits over SDL's Switch-Pro classification: NSO and | ||
| // third-party retro pads often report as a generic Nintendo device. | ||
| inline ControllerFamily detect_controller_family(std::string_view name, | ||
| int sdl_type = | ||
| kSdlControllerTypeUnknown) { | ||
| if (name_looks_like_n64(name)) { | ||
| return ControllerFamily::N64; | ||
| } | ||
| if (name_looks_like_gamecube(name)) { | ||
| return ControllerFamily::GameCube; | ||
| } | ||
| if (sdl_type != kSdlControllerTypeUnknown) { | ||
| if (sdl_type == kSdlControllerTypeVirtual && | ||
| name_looks_like_switch(name)) { | ||
| return ControllerFamily::Switch; | ||
| } | ||
| return family_from_sdl_type(sdl_type); | ||
| } | ||
| if (name_looks_like_switch(name)) { | ||
| return ControllerFamily::Switch; | ||
| } | ||
| return ControllerFamily::Xbox; | ||
| } | ||
|
|
||
| constexpr bool uses_nintendo_face_etiquette(ControllerFamily family) { | ||
| return family == ControllerFamily::Switch; | ||
| } | ||
|
|
||
| constexpr std::size_t nintendo_label_face_button(std::size_t button) { | ||
| switch (button) { | ||
| case 0U: | ||
| return 1U; | ||
| case 1U: | ||
| return 0U; | ||
| case 2U: | ||
| return 3U; | ||
| case 3U: | ||
| return 2U; | ||
| default: | ||
| return button; | ||
| } | ||
| } | ||
|
|
||
| template <typename ButtonArray> | ||
| inline void apply_nintendo_face_etiquette(ButtonArray& buttons) { | ||
| const auto south = buttons[0]; | ||
| const auto east = buttons[1]; | ||
| const auto west = buttons[2]; | ||
| const auto north = buttons[3]; | ||
| buttons[0] = east; | ||
| buttons[1] = south; | ||
| buttons[2] = north; | ||
| buttons[3] = west; | ||
| } | ||
|
|
||
| } // namespace dkr::runtime::controllers |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,10 @@ | |
| #include <SDL_metal.h> | ||
| extern "C" bool dkr_show_metal_layer(void* ns_window_ptr, void* layer_ptr); | ||
| extern "C" bool dkr_handle_fullscreen_event(const SDL_Event* event); | ||
| #if !defined(DKR_IOS) | ||
| extern "C" bool dkr_macos_app_is_active(void); | ||
| extern "C" bool dkr_macos_take_input_recovery(void); | ||
| #endif | ||
| #endif | ||
| #if defined(DKR_IOS) | ||
| #include "../ios/ios_platform.hpp" | ||
|
|
@@ -706,6 +710,42 @@ void RefreshControllers(bool force = true) { | |
| ReconcileControllers(); | ||
| } | ||
|
|
||
| void RefreshSdl3Controllers(); | ||
|
|
||
| bool WindowAcceptsGameplayInput() { | ||
| if (g_window == nullptr) return false; | ||
| const Uint32 flags = SDL_GetWindowFlags(g_window); | ||
| if ((flags & SDL_WINDOW_INPUT_FOCUS) != 0U) return true; | ||
| #if defined(__APPLE__) && !defined(DKR_IOS) | ||
| // After Mac lid sleep SDL's INPUT_FOCUS bit often stays clear even while | ||
| // the app is frontmost again. Trust NSApp so pads and keys resume. | ||
| if ((flags & (SDL_WINDOW_MINIMIZED | SDL_WINDOW_HIDDEN)) != 0U) { | ||
| return false; | ||
| } | ||
| return dkr_macos_app_is_active(); | ||
| #else | ||
| return false; | ||
| #endif | ||
| } | ||
|
|
||
| void RecoverControllersAfterHostActivation() { | ||
| g_controller_inventory_dirty.store(true, std::memory_order_relaxed); | ||
| if (g_active_input_backend.load(std::memory_order_acquire) == | ||
| dkr::runtime::platform::InputBackend::SDL3Native) { | ||
| RefreshSdl3Controllers(); | ||
| return; | ||
| } | ||
| // Close and reopen: after sleep GetAttached can stay true for dead HID | ||
| // handles, so a dirty-only refresh would keep the zombie pads. | ||
| for (ControllerRecord& record : g_controller_devices) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This drops and reopens every pad on each wake, even when they're fine. Do player slot assignments survive that? |
||
| CloseControllerRecord(record); | ||
| } | ||
| g_controller_devices.clear(); | ||
| g_controllers.fill(nullptr); | ||
| g_gyro_controllers.fill(nullptr); | ||
| RefreshControllers(true); | ||
| } | ||
|
|
||
| const dkr::runtime::sdl3_input::Device* Sdl3DeviceForInstance(int instance) { | ||
| const auto match = std::find_if( | ||
| g_sdl3_input_state.devices.begin(), g_sdl3_input_state.devices.end(), | ||
|
|
@@ -1431,6 +1471,9 @@ bool dkr::runtime::platform::initialise() { | |
| // allowing newer Linux/SteamOS SDL builds to expose the built-in Deck pad. | ||
| SDL_SetHint("SDL_JOYSTICK_HIDAPI", "1"); | ||
| SDL_SetHint("SDL_JOYSTICK_HIDAPI_STEAMDECK", "1"); | ||
| // Nintendo diamond pads: report by printed letter (A is A). SDL's default | ||
| // is already "1"; pin it so Switch Pro / Joy-Con defaults match etiquette. | ||
| SDL_SetHint("SDL_GAMECONTROLLER_USE_BUTTON_LABELS", "1"); | ||
| // SDL may continue sampling connected game controllers while the native | ||
| // window is unfocused. DKR-R still gates those samples independently for | ||
| // every N64 port in poll_input, so this hint grants capability rather than | ||
|
|
@@ -1802,6 +1845,16 @@ void dkr::runtime::platform::pump_window_events(void*) { | |
| return; | ||
| } | ||
|
|
||
| #if defined(__APPLE__) && !defined(DKR_IOS) | ||
| // After lid sleep, pads often need a forced re-open even when SDL still | ||
| // reports the old instance as attached. | ||
| if (event.type == SDL_WINDOWEVENT && | ||
| (event.window.event == SDL_WINDOWEVENT_FOCUS_GAINED || | ||
| event.window.event == SDL_WINDOWEVENT_RESTORED)) { | ||
| g_controller_inventory_dirty.store(true, std::memory_order_relaxed); | ||
| } | ||
| #endif | ||
|
|
||
| // Overlay lifecycle commands are host-global. Process them before | ||
| // forwarding the event to ImGui so a focused widget cannot swallow | ||
| // Escape, F1 or Back/View and leave the player trapped in the UI. | ||
|
|
@@ -1828,6 +1881,12 @@ void dkr::runtime::platform::pump_window_events(void*) { | |
|
|
||
| dkr::runtime::ui::handle_runtime_event(&event); | ||
| } | ||
| #if defined(__APPLE__) && !defined(DKR_IOS) | ||
| if (dkr_macos_take_input_recovery()) { | ||
| std::scoped_lock lock(g_platform_mutex); | ||
| RecoverControllersAfterHostActivation(); | ||
| } | ||
| #endif | ||
| if (dkr::runtime::input::consume_shortcut_request( | ||
| dkr::runtime::input::ShortcutAction::ToggleOverlay)) { | ||
| dkr::runtime::ui::toggle_overlay(); | ||
|
|
@@ -2439,8 +2498,7 @@ void dkr::runtime::platform::poll_input() { | |
| RefreshControllers(false); | ||
| } | ||
| const bool blocked = dkr::runtime::ui::overlay_visible(); | ||
| const bool window_focused = g_window != nullptr && | ||
| (SDL_GetWindowFlags(g_window) & SDL_WINDOW_INPUT_FOCUS) != 0U; | ||
| const bool window_focused = WindowAcceptsGameplayInput(); | ||
| const bool online_routing = | ||
| g_online_input_routing.load(std::memory_order_acquire); | ||
| const int configured_keyboard_player = | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[NSApp isActive]is true while a native file picker or other modal panel has key focus, so gameplay input leaks through while typing in it. This also runs frompoll_inputoff the main thread.