MythTV  master
AppleRemote.h
Go to the documentation of this file.
1 #ifndef APPLEREMOTE
2 #define APPLEREMOTE
3 
4 // C++ headers
5 #include <string>
6 #include <vector>
7 #include <map>
8 
9 // MythTV headers
10 #include "mthread.h"
11 
12 #include <QTimer>
13 
14 #include <IOKit/IOKitLib.h>
15 #include <IOKit/IOCFPlugIn.h>
16 #include <IOKit/hid/IOHIDLib.h>
17 #include <IOKit/hid/IOHIDKeys.h>
18 #include <CoreFoundation/CoreFoundation.h>
19 
20 class AppleRemote : public QObject, public MThread
21 {
22  Q_OBJECT
23 public:
24  enum Event
25  { // label/meaning on White ... and Aluminium remote
26  Up = 0, // VolumePlus Up
27  Down, // VolumeMinus Down
29  Select, // Play Select
35  PlayHold, // was PlaySleep
37  PlayPause, // Play or Pause
38  Undefined // Used to handle the Apple TV > v2.3
39  };
40 
41  class Listener
42  {
43  public:
44  virtual ~Listener() = default;
45  virtual void appleRemoteButton(Event button, bool pressedDown) = 0;
46  };
47 
48  static AppleRemote * Get();
49  ~AppleRemote();
50 
51  bool isListeningToRemote();
53  Listener* listener() { return _listener; }
56  void startListening();
57  void stopListening();
58  void run() override; // MThread
59 
60 protected:
61  AppleRemote(); // will be a singleton class
62 
64 
65 
66 private:
67  bool openInExclusiveMode {true};
68  IOHIDDeviceInterface** hidDeviceInterface {nullptr};
69  IOHIDQueueInterface** queue {nullptr};
70  std::vector<int> cookies;
71  std::map< std::string, Event > cookieToButtonMapping;
72  int remoteId {0};
73  Listener* _listener {nullptr};
74 
75  bool mUsingNewAtv {false};
77  int mEventCount {0};
78  bool mKeyIsDown {false};
79  QTimer* mCallbackTimer {nullptr};
80 
81  void _initCookieMap();
82  bool _initCookies();
83  bool _createDeviceInterface(io_object_t hidDevice);
84  bool _openDevice();
85 
86  static void QueueCallbackFunction(void* target, IOReturn result,
87  void* refcon, void* sender);
88  void _queueCallbackFunction(IOReturn result,
89  void* refcon, void* sender);
90  void _queueCallbackATV23(IOReturn result);
91  void _handleEventWithCookieString(std::string cookieString,
92  SInt32 sumOfValues);
93  void _handleEventATV23(std::string cookieString, SInt32 sumOfValues);
94 
95 private slots:
96  // Key up event handling on the ATV v2.3 and above
97  void TimeoutHandler();
98 };
99 
100 #endif // APPLEREMOTE
bool mKeyIsDown
Definition: AppleRemote.h:78
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:46
unsigned int slots[4]
Definition: element.c:38
bool isListeningToRemote()
Definition: AppleRemote.cpp:72
Listener * _listener
Definition: AppleRemote.h:73
void _handleEventWithCookieString(std::string cookieString, SInt32 sumOfValues)
static AppleRemote * Get()
Definition: AppleRemote.cpp:47
void _initCookieMap()
Apple keeps changing the "interface" between the remote and the OS.
static void QueueCallbackFunction(void *target, IOReturn result, void *refcon, void *sender)
void run() override
Runs the Qt event loop unless we have a QRunnable, in which case we run the runnable run instead.
AppleRemote::Event mLastEvent
Definition: AppleRemote.h:76
bool _openDevice()
QTimer * mCallbackTimer
Definition: AppleRemote.h:79
void _handleEventATV23(std::string cookieString, SInt32 sumOfValues)
void setOpenInExclusiveMode(bool in)
Definition: AppleRemote.h:54
void _queueCallbackFunction(IOReturn result, void *refcon, void *sender)
virtual void appleRemoteButton(Event button, bool pressedDown)=0
bool mUsingNewAtv
Definition: AppleRemote.h:75
void startListening()
Definition: AppleRemote.cpp:82
static AppleRemote * _instance
Definition: AppleRemote.h:63
int mEventCount
Definition: AppleRemote.h:77
std::vector< int > cookies
Definition: AppleRemote.h:70
IOHIDDeviceInterface ** hidDeviceInterface
Definition: AppleRemote.h:68
void TimeoutHandler()
Listener * listener()
Definition: AppleRemote.h:53
bool openInExclusiveMode
Definition: AppleRemote.h:67
void stopListening()
bool _createDeviceInterface(io_object_t hidDevice)
bool isOpenInExclusiveMode()
Definition: AppleRemote.h:55
std::map< std::string, Event > cookieToButtonMapping
Definition: AppleRemote.h:71
Event details.
Definition: zmdefines.h:25
void setListener(Listener *listener)
Definition: AppleRemote.cpp:77
IOHIDQueueInterface ** queue
Definition: AppleRemote.h:69
void _queueCallbackATV23(IOReturn result)
bool _initCookies()
virtual ~Listener()=default