Search code examples
appkithiddriverkitusb-hidmacos-app-extension

macOS app and extension: How to send x,y data from HIDStylusDriver to StylusApp to Pen/Draw?


I am using HIDStylusDriver to handle stylus input:

Screenshot of HIDStylusDriver project in XCode

I have a touch screen and I want to:

  1. Control the screen. I can enable the extension and control the touch screen successfully.
  2. Draw on this screen. To do this, I need to send X,Y coordinates to StylusApp (AppKit), but I am unable to do this in Swift.

I am able to log the X,Y coordinates with the default code:

static void printStylus(IOHIDDigitizerStylusData *data)
{
    os_log(OS_LOG_DEFAULT, "dispatch stylus: id: %d x: %d y: %d range: %d tip: %d barrel: %d invert: %d erase: %d tp: %d tx: %d ty: %d tw: %d tc: %d pc: %d rc: %d",
           data->identifier, data->x, data->y, data->inRange, data->tip, data->barrelSwitch, data->invert, data->eraser, data->tipPressure,
           data->tiltX, data->tiltY, data->twist, data->tipChanged, data->positionChanged, data->rangeChanged);
}

But I don't know how to send it to AppKit to Pen.

What I have tried: I have researched communication between the driver and the client (source), but it seems the HID driver doesn't support communication to the client app—and thus I can't send X,Y coordinates to the Stylus App (AppKit) to Pen/Draw—because I can't override the HIDStylusDriver.

What I expecting: I want to send X,Y coordinates from HIDStylusDriver to Stylus App (AppKit) to Pen/Draw.


Solution

  • I have receive a solution for this, I just install HIDDriver, then it will dispatchDigitizerStylusEvent: dispatchDigitizerStylusEvent(timestamp, stylusData); Then I can get x, y coordinates from DragGesture. But one more thing that is I also need get Width of touch to send to client app, that would be perfect. But I also feel very satisfy with this, thank everyone.

    One remaining issue is: I am going to use Comunicate between Client and App(https://developer.apple.com/documentation/driverkit/communicating_between_a_driverkit_extension_and_a_client_app) to send width of touch (Pen/Pencil) to App. But this plist not satisfy for user client connect (use IOServicePen):

    <key>IOKitPersonalities</key>
    <dict>
        <key>HIDStylusDriver</key>
        <dict>
            <key>UserClientProperties</key>
            <dict>
                <key>IOUserClass</key>
                <string>NullDriverUserClient</string>
                <key>IOClass</key>
                <string>IOUserUserClient</string>
            </dict>
            <key>CFBundleIdentifier</key>
            <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
            <key>IOClass</key>
            <string>AppleUserHIDEventService</string>
            <key>IOProviderClass</key>
            <string>IOHIDInterface</string>
            <key>IOResourceMatch</key>
            <string>IOKit</string>
            <key>IOUserClass</key>
            <string>HIDStylusDriver</string>
            <key>IOUserServerName</key>
            <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
      <key>ProductID</key>
      <integer>AAAA</integer>
      <key>VendorID</key>
      <integer>BBBB</integer>
    </dict>
    

    How can I edit this plist to satisfy with IOServiceOpen, and also can use AppleUserHIDEventService?

    NOTE: I am not request entitlement yet, I will request later for development also of distribution.