Exemplo n.º 1
0
  osxHIDInputDevice::osxHIDInputDevice(URI uri,
				       const char *device_description,
				       const char *elements_description):parser(0) {
    theDevice = 0 ;
    inputreport_callback = 0 ;
    inputreport_context = 0 ;
    value_callback = 0 ;
    value_context = 0 ;    
    queue_callback = 0 ;
    queue_context = 0 ;
    debugLevel = OSX_DEFAULT_DEBUGLEVEL ;
    seizeDevice = OSX_DEFAULT_SEIZEDEVICE ;

    this->uri = uri ;
    this->uri.generalize() ;
    URI::getQueryArg(uri.query, "debugLevel", &debugLevel) ;
    URI::getQueryArg(uri.query, "seize", &seizeDevice) ;
    parser = new HIDReportParser(NULL, 0, debugLevel);

    manager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone) ;
    if (!manager) 
      throw std::runtime_error("IOHIDManagerCreate failed") ;

    device_match = 0 ;
    if (device_description) {
      if (!strncmp(device_description, "<?xml", 5)) {
	device_match = (CFMutableDictionaryRef)getPropertyListFromXML(device_description) ;
	if (debugLevel>1) 
	  std::cerr << "Filtering devices based on XML description: " << device_description << std::endl ;
      } else {
	device_match = (CFMutableDictionaryRef)getPropertyListFromFile(device_description) ;
	if (debugLevel>1) 
	  std::cerr << "Filtering devices based on file " << device_description << std::endl ;
      }
    }
    IOHIDManagerSetDeviceMatching(manager, device_match) ;

    elements_match = 0 ;
    if (elements_description) {
      if (!strncmp(elements_description, "<?xml", 5)) {
	elements_match = (CFArrayRef)getPropertyListFromXML(elements_description) ;
	if (debugLevel>1) 
	  std::cerr << "Filtering elements based on XML description: " << elements_description << std::endl ;
      } else {
	elements_match = (CFArrayRef)getPropertyListFromFile(elements_description) ;
	if (debugLevel>1) 
	  std::cerr << "Filtering elements based on file " << elements_description << std::endl ;
      }
    }
  
    IOHIDManagerRegisterDeviceMatchingCallback(manager, AddDevice, (void*)this) ;
    IOHIDManagerRegisterDeviceRemovalCallback(manager, RemoveDevice, (void*)this) ;
    IOHIDManagerScheduleWithRunLoop(manager, CFRunLoopGetMain(), kCFRunLoopDefaultMode) ;

    IOOptionBits inOptions = seizeDevice ? kIOHIDOptionsTypeSeizeDevice : kIOHIDOptionsTypeNone ;
    if (IOHIDManagerOpen(manager, inOptions)!=kIOReturnSuccess) 
      throw std::runtime_error("IOHIDManagerOpen failed") ;
  }
  osxPointingDeviceManager::osxPointingDeviceManager()
  {
    manager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
    if (!manager)
      throw std::runtime_error("IOHIDManagerCreate failed");

    const char *plist = hidDeviceFromVendorProductUsagePageUsage(0, 0, kHIDPage_GenericDesktop, kHIDUsage_GD_Mouse).c_str();
    CFMutableDictionaryRef device_match = (CFMutableDictionaryRef)getPropertyListFromXML(plist);
    IOHIDManagerSetDeviceMatching(manager, device_match);

    IOHIDManagerRegisterDeviceMatchingCallback(manager, AddDevice, (void*)this);
    IOHIDManagerRegisterDeviceRemovalCallback(manager, RemoveDevice, (void*)this);

#if USE_CURRENT_RUNLOOP
    CFRunLoopRef runLoop = CFRunLoopGetCurrent();
    CFStringRef runLoopMode = kCFRunLoopDefaultMode;
#else
    CFRunLoopRef runLoop = CFRunLoopGetMain();
    CFStringRef runLoopMode = kCFRunLoopCommonModes;
#endif
    IOHIDManagerScheduleWithRunLoop(manager, runLoop, runLoopMode);
  }