Use lazy loading for keyboard scancodes on macos (backport)

This commit is contained in:
Lorenzooone 2024-09-16 22:50:53 +02:00 committed by Chris Thrasher
parent 54c7edb2a0
commit 5545df7290
3 changed files with 19 additions and 6 deletions

View File

@ -22,6 +22,7 @@
- [Windows] Close the clipboard if we fail to empty it (#3043) - [Windows] Close the clipboard if we fail to empty it (#3043)
- [Android] Remove use of deprecated `ALooper_pollAll` (#3181, #3189) - [Android] Remove use of deprecated `ALooper_pollAll` (#3181, #3189)
- [macOS] Fix how macOS fullscreen video modes are detected (#2300, #3151) - [macOS] Fix how macOS fullscreen video modes are detected (#2300, #3151)
- [macOS] Prevent unnecessary macOS input monitoring permission prompts (#2843, #3235)
### Graphics ### Graphics

View File

@ -281,6 +281,7 @@ private:
// Member data // Member data
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
IOHIDManagerRef m_manager; ///< Underlying HID Manager 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 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::Scancode m_keyToScancodeMapping[Keyboard::KeyCount]; ///< Mapping from Key to Scancode
Keyboard::Key m_scancodeToKeyMapping[Keyboard::Scan::ScancodeCount]; ///< Mapping from Scancode to Key Keyboard::Key m_scancodeToKeyMapping[Keyboard::Scan::ScancodeCount]; ///< Mapping from Scancode to Key

View File

@ -555,6 +555,13 @@ bool HIDInputManager::isKeyPressed(Keyboard::Key key)
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
bool HIDInputManager::isKeyPressed(Keyboard::Scancode code) 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]); return (code != Keyboard::Scan::Unknown) && isPressed(m_keys[code]);
} }
@ -694,7 +701,8 @@ String HIDInputManager::getDescription(Keyboard::Scancode code)
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
HIDInputManager::HIDInputManager() : HIDInputManager::HIDInputManager() :
m_manager(0) m_manager(0),
m_keysInitialized(false)
{ {
// Create an HID Manager reference // Create an HID Manager reference
m_manager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone); m_manager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
@ -708,7 +716,6 @@ m_manager(0)
} }
// Build up our knownledge of the hardware // Build up our knownledge of the hardware
initializeKeyboard();
buildMappings(); buildMappings();
// Register for notification on keyboard layout changes // Register for notification on keyboard layout changes
@ -908,13 +915,17 @@ void HIDInputManager::freeUp()
CFRelease(m_manager); CFRelease(m_manager);
m_manager = 0; 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) for (unsigned int i = 0; i < Keyboard::KeyCount; ++i)
CFRelease(*it); {
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;
} }