himbeles / mac-device-connect-daemon

Run shell script or executable triggered by device detection on a Mac.
MIT License
61 stars 12 forks source link

What about all usb devices #3

Open rsanjuan87 opened 3 years ago

rsanjuan87 commented 3 years ago

Hi, nice work I love it I have done with my cell phone to start 'scrcpy' app but I want to do it with all usb devices? like on Windows conecting usb devices, plays a sound I have tried with no vendor and/or product section, with a empty values but it's like it need those values to work

applebit924 commented 2 years ago

I would like this function as well. I can write a script that loops until change, but this is far more efficient.

applebit924 commented 2 years ago

The more I read the other issues thread the more I am thinking my method would be more ideal: I want to detect changes as a whole: connects and disconnects and take action. I simply wish there was an action based event (such as this) rather than a timed based method (my loop). It would be gold!

My method at the moment for comparing is to execute (shell):

ioreg -p IOUSB -w0; ioreg -p IOPCI -w0

Either to set the above command as a variable or write to a temporary file, then compare to last. If there is a difference, then device changes have occured on PCI (Thunderbolt) and/or USB.

FYI: More verbose info on devices:

ioreg -p IOUSB -l -w0 ioreg -p IOPCI -l -w0

sv-vv commented 1 month ago

The key com.apple.device-attach looks like a standard name required by the functionality to work but it is not. It is just an identifier that the author of the original example chose. You can use whatever value you want there; it is returned by the call xpc_dictionary_get_string(object, XPC_EVENT_KEY_NAME) on line 32.

It is possible to listen for event com.apple.iokit.matching for multiple USB devices, like this:

    <key>LaunchEvents</key>
    <dict>
        <key>com.apple.iokit.matching</key>
        <dict>
            <key>device-1</key>
            <dict>
                <key>idVendor</key>
                <integer>32902</integer>
                <key>idProduct</key>
                <integer>5427</integer>
                ...
            </dict>
            <key>device-2</key>
            <dict>
                <key>idVendor</key>
                <integer>123</integer>
                <key>idProduct</key>
                <integer>456</integer>
                ...
            </dict>
        ...

Unfortunately, apart for being logged (on line 34), neither the identifier set this way, nor the vendor Id and product Id are available to the program that you launch using this code. :-( You have to implement your own detection in the launched program to find out what device has been connected.