Skip to content

Composite hid device that's also a keyboard on Mac OS X #344

Closed

Description

@dnaq

I have an issue with implementing a HID-based protocol for a BLE device that is also a keyboard an getting it to work under Mac OS X. My guess is that the operating system refuses to give us access to the hid device due to the fact of it also being a keyboard. When using USB the solution is pretty simple, just use a different endpoint for the custom protocol, but for BLE we can only have a single HOG endpoint. I'm wondering if there is, or if it would be possible to implement, a way for hidapi to not try to grab the whole device, but instead just grab the reports of the usages we're interested in.

Activity

  1. added
    question
    on Oct 13, 2021
  2. Youw commented on Oct 13, 2021

    @Youw
    Member

    the operating system refuses to give us access to the hid device due to the fact of it also being a keyboard

    That is true. No workaround is possible on SW/HIDAPI side. The way HID driver is implemented on macOS, a device with multiple usage_pages/usages is represented as a single undividable device.

    When using USB the solution is pretty simple, just use a different endpoint for the custom protocol

    Agree, that's what I'd do.

    but for BLE we can only have a single HOG endpoint

    This is a bit suprising, I've imagined that something similar should be possible.
    But I have never worked with BLE devices so I cannot be sure or give any hints.

  3. dnaq commented on Oct 14, 2021

    @dnaq
    Author

    Looking into the issue a bit more it seems like we can grab the device if we run the application under sudo and if we allow "Input Monitoring" for the application under Privacy/Input Monitoring in the security & privacy settings.

    The next issue is that this causes the keypresses of the device to not be handled by the operating system. I think this might be because of the kIOHIDOptionsTypeSeizeDevice flag when opening the device. I haven't been able to investigate that further for now, but I'm planning to see what happens without that flag set.

  4. dnaq commented on Oct 14, 2021

    @dnaq
    Author

    And this seems to work beautifully and not need root permissions anymore. So just changing that flag to zero makes us able to open those devices from normal user applications if the Input Monitoring setting is enabled for our application.

  5. added
    macOS
    on Oct 14, 2021
  6. Youw commented on Oct 14, 2021

    @Youw
    Member

    That is something that has been on our place for some time. If we remove that flag by default with a new update - that will break backward compatibility for programs that may rely on it. I'm currently thinking on the alternative approach.

  7. dnaq commented on Oct 14, 2021

    @dnaq
    Author

    Would it break backwards compatibility though? The hidraw backend on Linux doesn't allow exclusive access afaik. I don't know how the situation is on windows, but shouldn't hidapi cater to the "lowest common denominator"?.

    If that is an issue another option is to expose an open_nonexclusive() function, but I'm not sure that would be needed.

  8. Youw commented on Oct 14, 2021

    @Youw
    Member

    It is not about what it should or shouldn't do. HIDAPI has been opening the device on macOS exclusively since the very first version. And even if it is beeter not to do this way by default, by changing the default, we break the backward compativility of the programs that are already using HIDAPI on macOS and rely on such behavior explicitly or implicitly.

  9. dnaq commented on Oct 14, 2021

    @dnaq
    Author

    I understand what you mean, I’m just saying that since other platforms hidapi support do not open the devices with exclusive access the probability of a Mac OS X application using hidapi depending on that behaviour would be pretty low. The reason being that the main reason of using hidapi on Mac OS X would be portability.

    That being said I’m not advocating for changing the default, you surely know a lot more about hidapis users than I do, but I would be grateful for being able to open the device in non-exclusive access mode (without having to resort to patching hidapi).

  10. Youw commented on Oct 15, 2021

    @Youw
    Member

    but I would be grateful for being able to open the device in non-exclusive access mode

    You're not the only one:

    I'm currently thinking on the alternative approach.

  11. mcuee commented on Oct 19, 2021

    @mcuee
    Member

    Is it possible to add a new API (or new API option) to open in non-exclusive access mode?

  12. Youw commented on Oct 20, 2021

    @Youw
    Member

    @mcuee please check my suggestion from some time ago: https://github.com/orgs/libusb/teams/hidapi/discussions/4 (this link is available only for HIDAPI team members).

  13. added a commit that references this issue on Apr 23, 2022
    05f0588
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    macOSquestion

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions