diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d03952b..97ce3fb 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -107,9 +107,14 @@ function(libvirtualhid_add_library target_name) target_sources(${target_name} PRIVATE "${CMAKE_CURRENT_FUNCTION_LIST_DIR}/platform/macos/macos_broker_client.cpp" - "${CMAKE_CURRENT_FUNCTION_LIST_DIR}/platform/macos/macos_backend.cpp") + "${CMAKE_CURRENT_FUNCTION_LIST_DIR}/platform/macos/macos_backend.cpp" + "${CMAKE_CURRENT_FUNCTION_LIST_DIR}/platform/macos/macos_media_key.mm") + set_source_files_properties( + "${CMAKE_CURRENT_FUNCTION_LIST_DIR}/platform/macos/macos_media_key.mm" + PROPERTIES COMPILE_OPTIONS "-fobjc-arc") target_link_libraries(${target_name} PRIVATE + "-framework AppKit" "-framework ApplicationServices" "-framework Carbon" "-framework CoreFoundation" diff --git a/src/platform/macos/macos_backend.cpp b/src/platform/macos/macos_backend.cpp index 5ac0d8a..32038d9 100644 --- a/src/platform/macos/macos_backend.cpp +++ b/src/platform/macos/macos_backend.cpp @@ -18,11 +18,13 @@ #include #include #include +#include #include // local includes #include "core/backend.hpp" #include "platform/macos/macos_broker_client.hpp" +#include "platform/macos/macos_media_key.hpp" namespace lvh::detail { namespace macos { @@ -222,6 +224,35 @@ namespace lvh::detail { */ constexpr auto key_code_map = make_key_code_map(); + /** + * @brief Map a portable media or volume key code to a macOS auxiliary key type. + * + * The system acts on these keys when they arrive as NX_SYSDEFINED + * auxiliary-control events, the way a physical media key is delivered. + * VK_MEDIA_STOP has no NX_KEYTYPE_* value and stays unmapped. + * + * @param key_code Portable Windows-style key code. + * @return NX_KEYTYPE_* value, or no value when the key is not a media key. + */ + constexpr std::optional macos_media_key_type(KeyboardKeyCode key_code) { + switch (key_code) { + case 0xAD: // VKEY_VOLUME_MUTE + return NX_KEYTYPE_MUTE; + case 0xAE: // VKEY_VOLUME_DOWN + return NX_KEYTYPE_SOUND_DOWN; + case 0xAF: // VKEY_VOLUME_UP + return NX_KEYTYPE_SOUND_UP; + case 0xB0: // VKEY_MEDIA_NEXT_TRACK + return NX_KEYTYPE_NEXT; + case 0xB1: // VKEY_MEDIA_PREV_TRACK + return NX_KEYTYPE_PREVIOUS; + case 0xB3: // VKEY_MEDIA_PLAY_PAUSE + return NX_KEYTYPE_PLAY; + default: + return std::nullopt; + } + } + /** * @brief Resolve a portable key code to a macOS virtual key code. * @@ -639,6 +670,16 @@ namespace lvh::detail { return OperationStatus::failure(device_closed, "macOS keyboard is closed"); } + if (const auto media_key = macos_media_key_type(event.key_code); media_key.has_value()) { + const auto media_event = macos_media_key::create_event(*media_key, event.pressed); + if (!media_event) { + return OperationStatus::failure(backend_failure, "create macOS media key event"); + } + CGEventPost(kCGHIDEventTap, media_event); + CFRelease(media_event); + return OperationStatus::success(); + } + const auto key = macos_key_code(event.key_code); if (!key) { return OperationStatus::success(); diff --git a/src/platform/macos/macos_media_key.hpp b/src/platform/macos/macos_media_key.hpp new file mode 100644 index 0000000..928583d --- /dev/null +++ b/src/platform/macos/macos_media_key.hpp @@ -0,0 +1,39 @@ +/** + * @file src/platform/macos/macos_media_key.hpp + * @brief macOS media and volume key event creation. + */ +#pragma once + +// standard includes +#include + +// platform includes +#include + +namespace lvh::detail::macos_media_key { + + /** + * @brief Pack the data1 field of an auxiliary-control event. + * + * @param key_type NX_KEYTYPE_* value. + * @param pressed Whether the key is pressed or released. + * @return Key type in the upper 16 bits and the key state (0xA down, 0xB up) in bits 8-15. + */ + constexpr std::int64_t event_data(int key_type, bool pressed) { + const std::int64_t key_state = pressed ? 0xA : 0xB; + return (static_cast(key_type) << 16) | (key_state << 8); + } + + /** + * @brief Create an NX_SYSDEFINED auxiliary-control event for a media key. + * + * A physical media or volume key arrives as a system-defined event, not as a key + * code event, and this builds the same event. + * + * @param key_type NX_KEYTYPE_* value. + * @param pressed Whether the key is pressed or released. + * @return Retained CoreGraphics event, or null when the event could not be created. + */ + CGEventRef create_event(int key_type, bool pressed); + +} // namespace lvh::detail::macos_media_key diff --git a/src/platform/macos/macos_media_key.mm b/src/platform/macos/macos_media_key.mm new file mode 100644 index 0000000..19c0fe0 --- /dev/null +++ b/src/platform/macos/macos_media_key.mm @@ -0,0 +1,38 @@ +/** + * @file src/platform/macos/macos_media_key.mm + * @brief macOS media and volume key event creation. + */ + +// local includes +#include "platform/macos/macos_media_key.hpp" + +// platform includes +#import + +namespace lvh::detail::macos_media_key { + + CGEventRef create_event(int key_type, bool pressed) { + @autoreleasepool { + constexpr short aux_control_buttons_subtype = 8; // NX_SUBTYPE_AUX_CONTROL_BUTTONS + const NSInteger key_state = pressed ? 0xA : 0xB; + + NSEvent *event = [NSEvent otherEventWithType:NSEventTypeSystemDefined + location:NSZeroPoint + modifierFlags:static_cast(key_state << 8) + timestamp:0 + windowNumber:0 + context:nil + subtype:aux_control_buttons_subtype + data1:static_cast(event_data(key_type, pressed)) + data2:-1]; + const CGEventRef cg_event = event ? event.CGEvent : nullptr; + if (!cg_event) { + return nullptr; + } + + CFRetain(cg_event); + return cg_event; + } + } + +} // namespace lvh::detail::macos_media_key diff --git a/tests/fixtures/include/fixtures/macos_backend_test_hooks.hpp b/tests/fixtures/include/fixtures/macos_backend_test_hooks.hpp index f82f286..4e55a21 100644 --- a/tests/fixtures/include/fixtures/macos_backend_test_hooks.hpp +++ b/tests/fixtures/include/fixtures/macos_backend_test_hooks.hpp @@ -144,6 +144,31 @@ namespace lvh::detail::test { */ std::optional macos_backend_key_code(KeyboardKeyCode key_code); + /** + * @brief Translate a portable media or volume key code to a macOS auxiliary key type. + * + * @param key_code Portable key code. + * @return NX_KEYTYPE_* value when the key is a media key. + */ + std::optional macos_backend_media_key_type(KeyboardKeyCode key_code); + + /** + * @brief Build the macOS media key event and report its CoreGraphics event type. + * + * @param key_type NX_KEYTYPE_* value. + * @param pressed Whether the key is pressed. + * @return CoreGraphics event type, or no value when the event could not be created. + */ + std::optional macos_backend_media_key_event_type(int key_type, bool pressed); + + /** + * @brief Submit keyboard events with posting intercepted. + * + * @param events Keyboard events to submit. + * @return Submission statuses and the events captured before posting. + */ + MacosMouseEventsResult macos_backend_keyboard_events(const std::vector &events); + /** * @brief Check whether a portable key code maps to a macOS modifier key. * diff --git a/tests/fixtures/macos_backend_test_hooks.cpp b/tests/fixtures/macos_backend_test_hooks.cpp index 20626f8..7c3bd07 100644 --- a/tests/fixtures/macos_backend_test_hooks.cpp +++ b/tests/fixtures/macos_backend_test_hooks.cpp @@ -18,11 +18,13 @@ #include "core/backend.hpp" #include "fixtures/macos_backend_test_hooks.hpp" #include "platform/macos/macos_broker_client.hpp" +#include "platform/macos/macos_media_key.hpp" // platform includes #include #include #include +#include #include namespace { @@ -249,6 +251,33 @@ namespace lvh::detail::test { return static_cast(*mapped); } + std::optional macos_backend_media_key_type(KeyboardKeyCode key_code) { + return macos::macos_media_key_type(key_code); + } + + MacosMouseEventsResult macos_backend_keyboard_events(const std::vector &events) { + MacosMouseEventsResult captured; + MouseEnvironment environment {{.origin_x = 0, .origin_y = 0, .width = 400, .height = 200}}; + environment.events = &captured; + + macos::MacosKeyboard keyboard {std::make_shared()}; + for (const auto &event : events) { + captured.statuses.push_back(keyboard.submit(event)); + } + return captured; + } + + std::optional macos_backend_media_key_event_type(int key_type, bool pressed) { + const auto event = macos_media_key::create_event(key_type, pressed); + if (!event) { + return std::nullopt; + } + + const auto type = static_cast(CGEventGetType(event)); + CFRelease(event); + return type; + } + bool macos_backend_is_modifier_key(KeyboardKeyCode key_code) { const auto mapped = macos::macos_key_code(key_code); if (!mapped) { diff --git a/tests/unit/test_macos_backend.cpp b/tests/unit/test_macos_backend.cpp index 112cc4c..efaf93d 100644 --- a/tests/unit/test_macos_backend.cpp +++ b/tests/unit/test_macos_backend.cpp @@ -16,6 +16,10 @@ // platform includes #include +#include + +// local includes +#include "platform/macos/macos_media_key.hpp" /** * @brief Test fixture for macOS backend internals. @@ -57,6 +61,49 @@ TEST_F(MacosBackendTest, TranslatesKeyboardKeys) { EXPECT_FALSE(lvh::detail::test::macos_backend_key_code(0xFFFF).has_value()); } +TEST_F(MacosBackendTest, TranslatesMediaKeysToAuxiliaryKeyTypes) { + EXPECT_EQ(lvh::detail::test::macos_backend_media_key_type(0xAD), NX_KEYTYPE_MUTE); + EXPECT_EQ(lvh::detail::test::macos_backend_media_key_type(0xAE), NX_KEYTYPE_SOUND_DOWN); + EXPECT_EQ(lvh::detail::test::macos_backend_media_key_type(0xAF), NX_KEYTYPE_SOUND_UP); + EXPECT_EQ(lvh::detail::test::macos_backend_media_key_type(0xB0), NX_KEYTYPE_NEXT); + EXPECT_EQ(lvh::detail::test::macos_backend_media_key_type(0xB1), NX_KEYTYPE_PREVIOUS); + EXPECT_EQ(lvh::detail::test::macos_backend_media_key_type(0xB3), NX_KEYTYPE_PLAY); + EXPECT_FALSE(lvh::detail::test::macos_backend_media_key_type(0xB2).has_value()); + EXPECT_FALSE(lvh::detail::test::macos_backend_media_key_type(0x41).has_value()); +} + +TEST_F(MacosBackendTest, BuildsSystemDefinedEventsForMediaKeys) { + constexpr std::uint32_t system_defined_event_type = 14; // NX_SYSDEFINED + EXPECT_EQ(lvh::detail::test::macos_backend_media_key_event_type(NX_KEYTYPE_PLAY, true), system_defined_event_type); + EXPECT_EQ(lvh::detail::test::macos_backend_media_key_event_type(NX_KEYTYPE_PLAY, false), system_defined_event_type); +} + +TEST_F(MacosBackendTest, PacksMediaKeyTypeAndStateIntoEventData) { + EXPECT_EQ(lvh::detail::macos_media_key::event_data(NX_KEYTYPE_PLAY, true), 0x100A00); + EXPECT_EQ(lvh::detail::macos_media_key::event_data(NX_KEYTYPE_PLAY, false), 0x100B00); + EXPECT_EQ(lvh::detail::macos_media_key::event_data(NX_KEYTYPE_SOUND_UP, true), 0x000A00); +} + +TEST_F(MacosBackendTest, SubmitPostsSystemDefinedEventsForMediaKeys) { + constexpr std::uint32_t system_defined_event_type = 14; // NX_SYSDEFINED + const auto result = lvh::detail::test::macos_backend_keyboard_events({ + {.key_code = 0xB3, .pressed = true}, + {.key_code = 0xB3, .pressed = false}, + {.key_code = 0xAF, .pressed = true}, + {.key_code = 0xB2, .pressed = true}, + }); + + ASSERT_EQ(result.statuses.size(), 4U); + for (const auto &status : result.statuses) { + EXPECT_TRUE(status.ok()) << status.message(); + } + ASSERT_EQ(result.events.size(), 3U); // VK_MEDIA_STOP has no macOS key type and posts nothing + for (const auto &event : result.events) { + EXPECT_EQ(event.event_type, system_defined_event_type); + EXPECT_EQ(event.tap_location, static_cast(kCGHIDEventTap)); + } +} + TEST_F(MacosBackendTest, IdentifiesModifierKeys) { EXPECT_TRUE(lvh::detail::test::macos_backend_is_modifier_key(0x10)); EXPECT_TRUE(lvh::detail::test::macos_backend_is_modifier_key(0x11));