Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P1] Keep the media source on the CXX compile rule

With LIBVIRTUALHID_BUILD_MACOS_BROKER=ON (the macOS default), the root CMakeLists.txt enables OBJCXX after add_subdirectory(src). CMake then classifies this new .mm source as Objective-C++, but the already-processed src directory has no Objective-C++ compile rule. A clean configure at this head fails during generation with Missing variable is: CMAKE_OBJCXX_COMPILE_OBJECT; the same broker-enabled configuration succeeds on target commit bef56ce. This blocks normal macOS builds and the Universal macOS broker CI job.

To preserve the intended CXX-only consumer integration, explicitly retain CMake's CXX rule while telling Clang to parse this file as Objective-C++:

Suggested change
PROPERTIES COMPILE_OPTIONS "-fobjc-arc")
PROPERTIES LANGUAGE CXX COMPILE_OPTIONS "-x;objective-c++;-fobjc-arc")

I tested this exact correction on macOS 27.2.1 with AppleClang 21 and CMake 4.4.3: the broker, license CLI, and library build successfully, and both CXX-only and OBJCXX-enabled add_subdirectory consumers build and run.

Review completed by Codex | Model: gpt-6.1-sol | Thinking: xhigh

target_link_libraries(${target_name}
PRIVATE
"-framework AppKit"
"-framework ApplicationServices"
"-framework Carbon"
"-framework CoreFoundation"
Expand Down
41 changes: 41 additions & 0 deletions src/platform/macos/macos_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
#include <ApplicationServices/ApplicationServices.h>
#include <Carbon/Carbon.h>
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/hidsystem/ev_keymap.h>
#include <IOKit/hidsystem/IOLLEvent.h>

// 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 {
Expand Down Expand Up @@ -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<int> 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.
*
Expand Down Expand Up @@ -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();
Expand Down
39 changes: 39 additions & 0 deletions src/platform/macos/macos_media_key.hpp
Original file line number Diff line number Diff line change
@@ -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 <cstdint>

// platform includes
#include <ApplicationServices/ApplicationServices.h>

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<std::int64_t>(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
38 changes: 38 additions & 0 deletions src/platform/macos/macos_media_key.mm
Original file line number Diff line number Diff line change
@@ -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 <AppKit/AppKit.h>

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<NSEventModifierFlags>(key_state << 8)
timestamp:0
windowNumber:0
context:nil
subtype:aux_control_buttons_subtype
data1:static_cast<NSInteger>(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
25 changes: 25 additions & 0 deletions tests/fixtures/include/fixtures/macos_backend_test_hooks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,31 @@ namespace lvh::detail::test {
*/
std::optional<std::uint16_t> 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<int> 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<std::uint32_t> 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<KeyboardEvent> &events);

/**
* @brief Check whether a portable key code maps to a macOS modifier key.
*
Expand Down
29 changes: 29 additions & 0 deletions tests/fixtures/macos_backend_test_hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <ApplicationServices/ApplicationServices.h>
#include <Carbon/Carbon.h>
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/hidsystem/ev_keymap.h>
#include <IOKit/hidsystem/IOLLEvent.h>

namespace {
Expand Down Expand Up @@ -249,6 +251,33 @@ namespace lvh::detail::test {
return static_cast<std::uint16_t>(*mapped);
}

std::optional<int> macos_backend_media_key_type(KeyboardKeyCode key_code) {
return macos::macos_media_key_type(key_code);
}

MacosMouseEventsResult macos_backend_keyboard_events(const std::vector<KeyboardEvent> &events) {
MacosMouseEventsResult captured;
MouseEnvironment environment {{.origin_x = 0, .origin_y = 0, .width = 400, .height = 200}};
environment.events = &captured;

macos::MacosKeyboard keyboard {std::make_shared<macos::MacosInputState>()};
for (const auto &event : events) {
captured.statuses.push_back(keyboard.submit(event));
}
return captured;
}

std::optional<std::uint32_t> 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<std::uint32_t>(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) {
Expand Down
47 changes: 47 additions & 0 deletions tests/unit/test_macos_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

// platform includes
#include <Carbon/Carbon.h>
#include <IOKit/hidsystem/ev_keymap.h>

// local includes
#include "platform/macos/macos_media_key.hpp"

/**
* @brief Test fixture for macOS backend internals.
Expand Down Expand Up @@ -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<std::uint32_t>(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));
Expand Down