1

I'm looking for a way to determine if the Mac running my game has more than one mouse attached. The typical real world example is a MacBook (with built in trackpad) with an external mouse connected.

My game has different control set ups for common configurations like keyboard+mouse, just keyboard (e.g., a MacBook with just the trackpad, no mouse) and gamepad. Ideally I'll be able to detect this in the game and set the controls accordingly.

I'm planning to support Mac OS 10.7+.

Is there a Cocoa (or non-Cocoa) API I can use to get this information?

For reference, I've ask (and got an answer) for a similar question for Windows-based computers.

Community
  • 1
  • 1
NoobsArePeople2
  • 1,986
  • 14
  • 21

2 Answers2

1

you should deal with IOKit... the following sample enumerates all the USB devices connected to the system, you can use that to see if there's a pointing device attached:

#include <IOKit/IOKitLib.h>
#include <IOKit/usb/IOUSBLib.h>

int main(int argc, const char *argv[])
{
    CFMutableDictionaryRef matchingDict;
    io_iterator_t iter;
    kern_return_t kr;
    io_service device;

    /* set up a matching dictionary for the class */
    matchingDict = IOServiceMatching(kIOUSBDeviceClassName);
    if (matchingDict == NULL)
    {
        return -1; // fail
    }

    /* Now we have a dictionary, get an iterator.*/
    kr = IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDict, &iter);
    if (kr != KERN_SUCCESS)
    {
        return -1;
    }

    /* iterate */
    while ((device = IOIteratorNext(iter)))
    {
        /* do something with device, eg. check properties */
        /* ... */
        /* And free the reference taken before continuing to the next item */
        IOObjectRelease(device);
    }

   /* Done, release the iterator */
   IOObjectRelase(iter);
   return 0;
}

The internal trackpad should be seen as an attached USB device but I'm not sure about that...

Leonardo Bernardini
  • 1,076
  • 13
  • 23
  • Stumbled on [ManyMouse](http://icculus.org/manymouse/) after asking my question. Need to do some testing but I think it will cover my needs. It's using [HIDManager](http://hg.icculus.org/icculus/manymouse/file/2e0dbebdbd28/macosx_hidmanager.c) behind the scenes. – NoobsArePeople2 Apr 11 '14 at 20:29
1

I ended up using ManyMouse, a cross-platform library for detecting the number of mice attached to a computer. On OSX it uses HIDManager to detect mice. Once it's built getting the number of mice attached to the system is a one-liner:

const int available_mice = ManyMouse_Init();
NoobsArePeople2
  • 1,986
  • 14
  • 21