AppleRemoteDelegate.mm 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #import "AppleRemoteDelegate.h"
  2. #import "InputAppleRemote.h"
  3. #include "QsLog.h"
  4. @implementation AppleRemoteDelegate
  5. ///////////////////////////////////////////////////////////////////////////////////////////////////
  6. - (instancetype)initWithRemoteHandler: (InputAppleRemote*)remoteHandler
  7. {
  8. self = [super init];
  9. if (self) {
  10. m_remoteHandler = remoteHandler;
  11. }
  12. return self;
  13. }
  14. ///////////////////////////////////////////////////////////////////////////////////////////////////
  15. - (bool)setupRemote
  16. {
  17. [[HIDRemote sharedHIDRemote] setDelegate:self];
  18. [[HIDRemote sharedHIDRemote] setSimulateHoldEvents:NO];
  19. [[HIDRemote sharedHIDRemote] setExclusiveLockLendingEnabled:YES];
  20. if (![[HIDRemote sharedHIDRemote] startRemoteControl:kHIDRemoteModeExclusive])
  21. {
  22. QLOG_ERROR() << "Failed to init AppleRemote";
  23. return false;
  24. }
  25. return true;
  26. }
  27. ///////////////////////////////////////////////////////////////////////////////////////////////////
  28. - (QString)remoteNameFromAttributes:(NSMutableDictionary*)attributes
  29. {
  30. NSString* product = [attributes objectForKey:kHIDRemoteProduct];
  31. NSString* manufacturer = [attributes objectForKey:kHIDRemoteManufacturer];
  32. return QString("%1-%2").arg([manufacturer UTF8String]).arg([product UTF8String]);
  33. }
  34. ///////////////////////////////////////////////////////////////////////////////////////////////////
  35. - (void)hidRemote:(HIDRemote *)hidRemote
  36. eventWithButton:(HIDRemoteButtonCode)buttonCode
  37. isPressed:(BOOL)isPressed
  38. fromHardwareWithAttributes:(NSMutableDictionary *)attributes
  39. {
  40. m_remoteHandler->remoteButtonEvent(buttonCode, isPressed, [self remoteNameFromAttributes:attributes]);
  41. }
  42. ///////////////////////////////////////////////////////////////////////////////////////////////////
  43. - (void)hidRemote:(HIDRemote *)hidRemote foundNewHardwareWithAttributes:(NSMutableDictionary *)attributes
  44. {
  45. m_remoteHandler->addRemote([self remoteNameFromAttributes:attributes]);
  46. }
  47. ///////////////////////////////////////////////////////////////////////////////////////////////////
  48. - (void)hidRemote:(HIDRemote *)hidRemote releasedHardwareWithAttributes:(NSMutableDictionary *)attributes
  49. {
  50. m_remoteHandler->removeRemote([self remoteNameFromAttributes:attributes]);
  51. }
  52. ///////////////////////////////////////////////////////////////////////////////////////////////////
  53. - (void)hidRemote:(HIDRemote *)hidRemote failedNewHardwareWithError:(NSError *)error
  54. {
  55. m_remoteHandler->addRemoteFailed(QString([[error localizedDescription] UTF8String]));
  56. }
  57. /////////////////////////////////////////////////////////////////////////////////////////
  58. - (void)hidRemote:(HIDRemote *)hidRemote remoteIDChangedOldID:(SInt32)old newID:(SInt32)newID
  59. forHardwareWithAttributes:(NSMutableDictionary *)attributes
  60. {
  61. m_remoteHandler->changeRemoteID(newID);
  62. }
  63. @end