00001 #ifndef APPLEREMOTE
00002 #define APPLEREMOTE
00003
00004 #include <string>
00005 #include <vector>
00006 #include <map>
00007
00008 #include <IOKit/IOKitLib.h>
00009 #include <IOKit/IOCFPlugIn.h>
00010 #include <IOKit/hid/IOHIDLib.h>
00011 #include <IOKit/hid/IOHIDKeys.h>
00012 #include <CoreFoundation/CoreFoundation.h>
00013
00014 class AppleRemote
00015 {
00016 public:
00017 enum Event {
00018 VolumePlus = 0,
00019 VolumeMinus,
00020 Menu,
00021 Play,
00022 Right,
00023 Left,
00024 RightHold,
00025 LeftHold,
00026 MenuHold,
00027 PlaySleep,
00028 ControlSwitched
00029 };
00030
00031 class Listener {
00032 public:
00033 virtual ~Listener();
00034 virtual void appleRemoteButton(Event button, bool pressedDown) = 0;
00035 };
00036
00037 static AppleRemote& instance();
00038 ~AppleRemote();
00039
00040 bool isListeningToRemote();
00041 void setListener(Listener* listener);
00042 Listener* listener() { return _listener; }
00043 void setOpenInExclusiveMode(bool in) { openInExclusiveMode = in; };
00044 bool isOpenInExclusiveMode() { return openInExclusiveMode; };
00045 void startListening();
00046 void stopListening();
00047 void runLoop();
00048
00049 protected:
00050 AppleRemote();
00051
00052 static AppleRemote* _instance;
00053 static const char* const AppleRemoteDeviceName;
00054 static const int REMOTE_SWITCH_COOKIE;
00055
00056
00057 private:
00058 bool openInExclusiveMode;
00059 IOHIDDeviceInterface** hidDeviceInterface;
00060 IOHIDQueueInterface** queue;
00061 std::vector<int> cookies;
00062 std::map< std::string, Event > cookieToButtonMapping;
00063 int remoteId;
00064 Listener* _listener;
00065
00066 void _initCookieMap();
00067 io_object_t _findAppleRemoteDevice();
00068 bool _initCookies();
00069 bool _createDeviceInterface(io_object_t hidDevice);
00070 bool _openDevice();
00071
00072 static void QueueCallbackFunction(void* target, IOReturn result,
00073 void* refcon, void* sender);
00074 void _queueCallbackFunction(IOReturn result,
00075 void* refcon, void* sender);
00076 void _handleEventWithCookieString(std::string cookieString,
00077 SInt32 sumOfValues);
00078 };
00079
00080 #endif // APPLEREMOTE