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 "libmythbase/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 
76  int mEventCount {0};
77  bool mKeyIsDown {false};
78  QTimer* mCallbackTimer {nullptr};
79 
80  void _initCookieMap();
81  bool _initCookies();
82  bool _createDeviceInterface(io_object_t hidDevice);
83  bool _openDevice();
84 
85  static void QueueCallbackFunction(void* target, IOReturn result,
86  void* refcon, void* sender);
87  void _queueCallbackFunction(IOReturn result,
88  void* refcon, void* sender);
89  void _handleEventWithCookieString(std::string cookieString,
90  SInt32 sumOfValues);
91 };
92 
93 #endif // APPLEREMOTE
AppleRemote::Get
static AppleRemote * Get()
Definition: AppleRemote.cpp:41
AppleRemote::run
void run() override
Runs the Qt event loop unless we have a QRunnable, in which case we run the runnable run instead.
Definition: AppleRemote.cpp:116
AppleRemote::Listener::~Listener
virtual ~Listener()=default
AppleRemote::mKeyIsDown
bool mKeyIsDown
Definition: AppleRemote.h:77
AppleRemote::Select
@ Select
Definition: AppleRemote.h:29
AppleRemote::mLastEvent
AppleRemote::Event mLastEvent
Definition: AppleRemote.h:75
AppleRemote::RightHold
@ RightHold
Definition: AppleRemote.h:32
AppleRemote::Down
@ Down
Definition: AppleRemote.h:27
AppleRemote::_initCookieMap
void _initCookieMap()
Apple keeps changing the "interface" between the remote and the OS.
Definition: AppleRemote.cpp:139
AppleRemote::Event
Event
Definition: AppleRemote.h:24
AppleRemote::MenuHold
@ MenuHold
Definition: AppleRemote.h:34
AppleRemote::_handleEventWithCookieString
void _handleEventWithCookieString(std::string cookieString, SInt32 sumOfValues)
Definition: AppleRemote.cpp:379
AppleRemote::Menu
@ Menu
Definition: AppleRemote.h:28
AppleRemote::QueueCallbackFunction
static void QueueCallbackFunction(void *target, IOReturn result, void *refcon, void *sender)
Definition: AppleRemote.cpp:341
AppleRemote::_openDevice
bool _openDevice()
Definition: AppleRemote.cpp:281
AppleRemote::Listener::appleRemoteButton
virtual void appleRemoteButton(Event button, bool pressedDown)=0
AppleRemote::Listener
Definition: AppleRemote.h:41
AppleRemote::AppleRemote
AppleRemote()
Definition: AppleRemote.cpp:125
AppleRemote::remoteId
int remoteId
Definition: AppleRemote.h:72
AppleRemote::Right
@ Right
Definition: AppleRemote.h:30
AppleRemote::mCallbackTimer
QTimer * mCallbackTimer
Definition: AppleRemote.h:78
Event
Event details.
Definition: zmdefines.h:26
AppleRemote
Definition: AppleRemote.h:20
AppleRemote::_queueCallbackFunction
void _queueCallbackFunction(IOReturn result, void *refcon, void *sender)
Definition: AppleRemote.cpp:349
AppleRemote::listener
Listener * listener()
Definition: AppleRemote.h:53
AppleRemote::openInExclusiveMode
bool openInExclusiveMode
Definition: AppleRemote.h:67
AppleRemote::setOpenInExclusiveMode
void setOpenInExclusiveMode(bool in)
Definition: AppleRemote.h:54
AppleRemote::PlayHold
@ PlayHold
Definition: AppleRemote.h:35
AppleRemote::~AppleRemote
~AppleRemote()
Definition: AppleRemote.cpp:49
AppleRemote::cookies
std::vector< int > cookies
Definition: AppleRemote.h:70
AppleRemote::hidDeviceInterface
IOHIDDeviceInterface ** hidDeviceInterface
Definition: AppleRemote.h:68
AppleRemote::Undefined
@ Undefined
Definition: AppleRemote.h:38
AppleRemote::ControlSwitched
@ ControlSwitched
Definition: AppleRemote.h:36
AppleRemote::_createDeviceInterface
bool _createDeviceInterface(io_object_t hidDevice)
Definition: AppleRemote.cpp:252
AppleRemote::startListening
void startListening()
Definition: AppleRemote.cpp:73
AppleRemote::isOpenInExclusiveMode
bool isOpenInExclusiveMode()
Definition: AppleRemote.h:55
AppleRemote::cookieToButtonMapping
std::map< std::string, Event > cookieToButtonMapping
Definition: AppleRemote.h:71
AppleRemote::mEventCount
int mEventCount
Definition: AppleRemote.h:76
AppleRemote::Up
@ Up
Definition: AppleRemote.h:26
AppleRemote::Left
@ Left
Definition: AppleRemote.h:31
AppleRemote::LeftHold
@ LeftHold
Definition: AppleRemote.h:33
AppleRemote::_instance
static AppleRemote * _instance
Definition: AppleRemote.h:63
MThread
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:48
AppleRemote::_initCookies
bool _initCookies()
Definition: AppleRemote.cpp:212
mthread.h
AppleRemote::stopListening
void stopListening()
Definition: AppleRemote.cpp:95
AppleRemote::queue
IOHIDQueueInterface ** queue
Definition: AppleRemote.h:69
AppleRemote::isListeningToRemote
bool isListeningToRemote()
Definition: AppleRemote.cpp:63
AppleRemote::_listener
Listener * _listener
Definition: AppleRemote.h:73
AppleRemote::setListener
void setListener(Listener *listener)
Definition: AppleRemote.cpp:68
AppleRemote::PlayPause
@ PlayPause
Definition: AppleRemote.h:37