1 /*****************************************************************************
6 * Created by Martin Kahr on 11.03.06 under a MIT-style license.
7 * Copyright (c) 2006 martinkahr.com. All rights reserved.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 *****************************************************************************
29 * Note that changes made by any members or contributors of the VideoLAN team
30 * (i.e. changes that were checked in exclusively into one of VideoLAN's source code
31 * repositories) are licensed under the GNU General Public License version 2,
32 * or (at your option) any later version.
33 * Thus, the following statements apply to our changes:
35 * Copyright (C) 2006-2007 VLC authors and VideoLAN
36 * Authors: Eric Petit <titer@m0k.org>
37 * Felix Kühne <fkuehne at videolan dot org>
39 * This program is free software; you can redistribute it and/or modify
40 * it under the terms of the GNU General Public License as published by
41 * the Free Software Foundation; either version 2 of the License, or
42 * (at your option) any later version.
44 * This program is distributed in the hope that it will be useful,
45 * but WITHOUT ANY WARRANTY; without even the implied warranty of
46 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
47 * GNU General Public License for more details.
49 * You should have received a copy of the GNU General Public License
50 * along with this program; if not, write to the Free Software
51 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
52 *****************************************************************************/
54 #import <Cocoa/Cocoa.h>
56 #import <mach/mach_error.h>
57 #import <IOKit/IOKitLib.h>
58 #import <IOKit/IOCFPlugIn.h>
59 #import <IOKit/hid/IOHIDLib.h>
60 #import <IOKit/hid/IOHIDKeys.h>
62 enum AppleRemoteEventIdentifier
64 kRemoteButtonVolume_Plus =1<<1,
65 kRemoteButtonVolume_Minus =1<<2,
66 kRemoteButtonMenu =1<<3,
67 kRemoteButtonPlay =1<<4,
68 kRemoteButtonRight =1<<5,
69 kRemoteButtonLeft =1<<6,
70 kRemoteButtonRight_Hold =1<<7,
71 kRemoteButtonLeft_Hold =1<<8,
72 kRemoteButtonMenu_Hold =1<<9,
73 kRemoteButtonPlay_Sleep =1<<10,
74 kRemoteControl_Switched =1<<11,
75 kRemoteButtonVolume_Plus_Hold =1<<12,
76 kRemoteButtonVolume_Minus_Hold =1<<13,
80 k2009RemoteButtonFullscreen
83 typedef enum AppleRemoteEventIdentifier AppleRemoteEventIdentifier;
85 /* Encapsulates usage of the apple remote control
86 This class is implemented as a singleton as there is exactly one remote per machine (until now)
87 The class is not thread safe
89 @interface AppleRemote : NSObject {
90 IOHIDDeviceInterface** hidDeviceInterface;
91 IOHIDQueueInterface** queue;
93 NSDictionary* _cookieToButtonMapping;
94 CFRunLoopSourceRef eventSource;
96 BOOL _openInExclusiveMode;
97 BOOL _simulatePlusMinusHold;
98 BOOL _processesBacklog;
100 /* state for simulating plus/minus hold */
101 BOOL lastEventSimulatedHold;
102 AppleRemoteEventIdentifier lastPlusMinusEvent;
103 NSTimeInterval lastPlusMinusEventTime;
106 unsigned int _clickCountEnabledButtons;
107 NSTimeInterval _maxClickTimeDifference;
108 NSTimeInterval lastClickCountEventTime;
109 AppleRemoteEventIdentifier lastClickCountEvent;
110 unsigned int eventClickCount;
114 + (AppleRemote *)sharedInstance;
116 @property (readonly) int remoteId;
117 @property (readonly) BOOL remoteAvailable;
118 @property (readwrite) BOOL listeningToRemote;
119 @property (readwrite) BOOL openInExclusiveMode;
121 /* click counting makes it possible to recognize if the user has pressed a button repeatedly
122 * click counting does delay each event as it has to wait if there is another event (second click)
123 * therefore there is a slight time difference (maximumClickCountTimeDifference) between a single click
124 * of the user and the call of your delegate method
125 * click counting can be enabled individually for specific buttons. Use the property clickCountEnableButtons
126 * to set the buttons for which click counting shall be enabled */
127 @property (readwrite) BOOL clickCountingEnabled;
129 @property (readwrite) unsigned int clickCountEnabledButtons;
131 /* the maximum time difference till which clicks are recognized as multi clicks */
132 @property (readwrite) NSTimeInterval maximumClickCountTimeDifference;
134 /* When your application needs to much time on the main thread when processing an event other events
135 * may already be received which are put on a backlog. As soon as your main thread
136 * has some spare time this backlog is processed and may flood your delegate with calls.
137 * Backlog processing is turned off by default. */
138 @property (readwrite) BOOL processesBacklog;
140 /* Sets an NSApplication delegate which starts listening when application is becoming active
141 * and stops listening when application resigns being active.
142 * If an NSApplication delegate has been already set all method calls will be forwarded to this delegate, too. */
143 @property (readwrite) BOOL listeningOnAppActivate;
145 /* Simulating plus/minus hold does deactivate sending of individual requests for plus/minus pressed down/released.
146 * Instead special hold events are being triggered when the user is pressing and holding plus/minus for a small period.
147 * With simulating enabled the plus/minus buttons do behave as the left/right buttons */
148 @property (readwrite) BOOL simulatesPlusMinusHold;
150 /* Delegates are not retained */
151 @property (readwrite, assign) id delegate;
153 - (IBAction) startListening: (id) sender;
154 - (IBAction) stopListening: (id) sender;
157 @interface AppleRemote (Singleton)
159 + (AppleRemote*) sharedRemote;
163 /* Method definitions for the delegate of the AppleRemote class */
164 @interface NSObject(NSAppleRemoteDelegate)
166 - (void) appleRemoteButton: (AppleRemoteEventIdentifier)buttonIdentifier pressedDown: (BOOL) pressedDown clickCount: (unsigned int) count;
170 @interface AppleRemote (PrivateMethods)
171 @property (readonly) NSDictionary * cookieToButtonMapping;
173 - (void) setRemoteId: (int) aValue;
174 - (IOHIDQueueInterface**) queue;
175 - (IOHIDDeviceInterface**) hidDeviceInterface;
176 - (void) handleEventWithCookieString: (NSString*) cookieString sumOfValues: (SInt32) sumOfValues;
179 @interface AppleRemote (IOKitMethods)
180 - (io_object_t) findAppleRemoteDevice;
181 - (IOHIDDeviceInterface**) createInterfaceForDevice: (io_object_t) hidDevice;
182 - (BOOL) initializeCookies;
186 /* A NSApplication delegate which is used to activate and deactivate listening to the remote control
187 * dependent on the activation state of your application.
188 * All events are delegated to the original NSApplication delegate if necessary */
189 @interface AppleRemoteApplicationDelegate : NSObject {
190 id applicationDelegate;
193 - (id) initWithApplicationDelegate: (id) delegate;
194 - (id) applicationDelegate;