From 5545df7290250cbe767a2705c8401d8f3c4f23a0 Mon Sep 17 00:00:00 2001 From: Lorenzooone Date: Mon, 16 Sep 2024 22:50:53 +0200 Subject: [PATCH] Use lazy loading for keyboard scancodes on macos (backport) --- changelog.md | 1 + src/SFML/Window/OSX/HIDInputManager.hpp | 1 + src/SFML/Window/OSX/HIDInputManager.mm | 23 +++++++++++++++++------ 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/changelog.md b/changelog.md index 22c11eb20..0e59b0e23 100644 --- a/changelog.md +++ b/changelog.md @@ -22,6 +22,7 @@ - [Windows] Close the clipboard if we fail to empty it (#3043) - [Android] Remove use of deprecated `ALooper_pollAll` (#3181, #3189) - [macOS] Fix how macOS fullscreen video modes are detected (#2300, #3151) +- [macOS] Prevent unnecessary macOS input monitoring permission prompts (#2843, #3235) ### Graphics diff --git a/src/SFML/Window/OSX/HIDInputManager.hpp b/src/SFML/Window/OSX/HIDInputManager.hpp index b17d566b5..75376b335 100644 --- a/src/SFML/Window/OSX/HIDInputManager.hpp +++ b/src/SFML/Window/OSX/HIDInputManager.hpp @@ -281,6 +281,7 @@ private: // Member data //////////////////////////////////////////////////////////// IOHIDManagerRef m_manager; ///< Underlying HID Manager + bool m_keysInitialized; ///< Has initializeKeyboard been called at least once? IOHIDElements m_keys[Keyboard::Scan::ScancodeCount]; ///< All the keys on any connected keyboard Keyboard::Scancode m_keyToScancodeMapping[Keyboard::KeyCount]; ///< Mapping from Key to Scancode Keyboard::Key m_scancodeToKeyMapping[Keyboard::Scan::ScancodeCount]; ///< Mapping from Scancode to Key diff --git a/src/SFML/Window/OSX/HIDInputManager.mm b/src/SFML/Window/OSX/HIDInputManager.mm index 6d31ae6b7..f3f7b3ab2 100644 --- a/src/SFML/Window/OSX/HIDInputManager.mm +++ b/src/SFML/Window/OSX/HIDInputManager.mm @@ -555,6 +555,13 @@ bool HIDInputManager::isKeyPressed(Keyboard::Key key) //////////////////////////////////////////////////////////// bool HIDInputManager::isKeyPressed(Keyboard::Scancode code) { + // Lazy load m_keys to prevent unnecessary macOS input monitoring permission requests + if (!m_keysInitialized) + { + initializeKeyboard(); + m_keysInitialized = true; + } + return (code != Keyboard::Scan::Unknown) && isPressed(m_keys[code]); } @@ -694,7 +701,8 @@ String HIDInputManager::getDescription(Keyboard::Scancode code) //////////////////////////////////////////////////////////// HIDInputManager::HIDInputManager() : -m_manager(0) +m_manager(0), +m_keysInitialized(false) { // Create an HID Manager reference m_manager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone); @@ -708,7 +716,6 @@ m_manager(0) } // Build up our knownledge of the hardware - initializeKeyboard(); buildMappings(); // Register for notification on keyboard layout changes @@ -908,13 +915,17 @@ void HIDInputManager::freeUp() CFRelease(m_manager); m_manager = 0; - for (unsigned int i = 0; i < Keyboard::KeyCount; ++i) + if (m_keysInitialized) { - for (IOHIDElements::iterator it = m_keys[i].begin(); it != m_keys[i].end(); ++it) - CFRelease(*it); + for (unsigned int i = 0; i < Keyboard::KeyCount; ++i) + { + for (IOHIDElements::iterator it = m_keys[i].begin(); it != m_keys[i].end(); ++it) + CFRelease(*it); - m_keys[i].clear(); + m_keys[i].clear(); + } } + m_keysInitialized = false; }