xref: /aoo41x/main/apple_remote/RemoteControl.m (revision 53e04a5d)
1cdf0e10cSrcweir/*****************************************************************************
2cdf0e10cSrcweir * RemoteControl.m
3cdf0e10cSrcweir * RemoteControlWrapper
4cdf0e10cSrcweir *
5cdf0e10cSrcweir * Created by Martin Kahr on 11.03.06 under a MIT-style license.
6cdf0e10cSrcweir * Copyright (c) 2006 martinkahr.com. All rights reserved.
7cdf0e10cSrcweir *
8cdf0e10cSrcweir * Code modified and adapted to OpenOffice.org
9cdf0e10cSrcweir * by Eric Bachard on 11.08.2008 under the same License
10cdf0e10cSrcweir *
11cdf0e10cSrcweir * Permission is hereby granted, free of charge, to any person obtaining a
12cdf0e10cSrcweir * copy of this software and associated documentation files (the "Software"),
13cdf0e10cSrcweir * to deal in the Software without restriction, including without limitation
14cdf0e10cSrcweir * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15cdf0e10cSrcweir * and/or sell copies of the Software, and to permit persons to whom the
16cdf0e10cSrcweir * Software is furnished to do so, subject to the following conditions:
17cdf0e10cSrcweir *
18cdf0e10cSrcweir * The above copyright notice and this permission notice shall be included
19cdf0e10cSrcweir * in all copies or substantial portions of the Software.
20cdf0e10cSrcweir *
21cdf0e10cSrcweir * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22cdf0e10cSrcweir * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23cdf0e10cSrcweir * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24cdf0e10cSrcweir * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25cdf0e10cSrcweir * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26cdf0e10cSrcweir * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27cdf0e10cSrcweir * THE SOFTWARE.
28cdf0e10cSrcweir *
29cdf0e10cSrcweir *****************************************************************************/
30cdf0e10cSrcweir
31cdf0e10cSrcweir#import "RemoteControl.h"
32cdf0e10cSrcweir
33cdf0e10cSrcweir// notifaction names that are being used to signal that an application wants to
34cdf0e10cSrcweir// have access to the remote control device or if the application has finished
35cdf0e10cSrcweir// using the remote control device
36cdf0e10cSrcweirNSString* REQUEST_FOR_REMOTE_CONTROL_NOTIFCATION     = @"mac.remotecontrols.RequestForRemoteControl";
37cdf0e10cSrcweirNSString* FINISHED_USING_REMOTE_CONTROL_NOTIFICATION = @"mac.remotecontrols.FinishedUsingRemoteControl";
38cdf0e10cSrcweir
39cdf0e10cSrcweir// keys used in user objects for distributed notifications
40cdf0e10cSrcweirNSString* kRemoteControlDeviceName = @"RemoteControlDeviceName";
41cdf0e10cSrcweirNSString* kApplicationIdentifier   = @"CFBundleIdentifier";
42cdf0e10cSrcweir// bundle identifier of the application that should get access to the remote control
43cdf0e10cSrcweir// this key is being used in the FINISHED notification only
44cdf0e10cSrcweirNSString* kTargetApplicationIdentifier = @"TargetBundleIdentifier";
45cdf0e10cSrcweir
46cdf0e10cSrcweir
47cdf0e10cSrcweir@implementation RemoteControl
48cdf0e10cSrcweir
49cdf0e10cSrcweir// returns nil if the remote control device is not available
50cdf0e10cSrcweir- (id) initWithDelegate: (id) _remoteControlDelegate {
51cdf0e10cSrcweir	if ( (self = [super init]) ) {
52cdf0e10cSrcweir		delegate = [_remoteControlDelegate retain];
53cdf0e10cSrcweir#ifdef DEBUG
54*53e04a5dSHerbert Dürr        NSLog( @"Apple RemoteControl initWithDelegate ok");
55cdf0e10cSrcweir#endif
56cdf0e10cSrcweir    }
57cdf0e10cSrcweir	return self;
58cdf0e10cSrcweir}
59cdf0e10cSrcweir
60cdf0e10cSrcweir- (void) dealloc {
61cdf0e10cSrcweir	[delegate release];
62cdf0e10cSrcweir	[super dealloc];
63cdf0e10cSrcweir}
64cdf0e10cSrcweir
65cdf0e10cSrcweir- (void) setListeningToRemote: (BOOL) value {
66cdf0e10cSrcweir#ifdef DEBUG
67*53e04a5dSHerbert Dürr        NSLog( @"Apple RemoteControl setListeningToRemote ok");
68cdf0e10cSrcweir#endif
69cdf0e10cSrcweir}
70cdf0e10cSrcweir- (BOOL) isListeningToRemote {
71cdf0e10cSrcweir	return NO;
72cdf0e10cSrcweir}
73cdf0e10cSrcweir
74cdf0e10cSrcweir- (void) startListening: (id) sender {
75cdf0e10cSrcweir#ifdef DEBUG
76*53e04a5dSHerbert Dürr            NSLog( @"Apple RemoteControl startListening ok");
77cdf0e10cSrcweir#endif
78cdf0e10cSrcweir}
79cdf0e10cSrcweir- (void) stopListening: (id) sender {
80cdf0e10cSrcweir#ifdef DEBUG
81*53e04a5dSHerbert Dürr            NSLog( @"Apple RemoteControl stopListening ok");
82cdf0e10cSrcweir#endif
83cdf0e10cSrcweir}
84cdf0e10cSrcweir
85cdf0e10cSrcweir- (BOOL) isOpenInExclusiveMode {
86cdf0e10cSrcweir	return YES;
87cdf0e10cSrcweir}
88cdf0e10cSrcweir- (void) setOpenInExclusiveMode: (BOOL) value {
89cdf0e10cSrcweir}
90cdf0e10cSrcweir
91cdf0e10cSrcweir- (BOOL) sendsEventForButtonIdentifier: (RemoteControlEventIdentifier) identifier {
92cdf0e10cSrcweir#ifdef DEBUG
93*53e04a5dSHerbert Dürr   NSLog( @"Apple RemoteControl: sending event for button identifier\n");
94cdf0e10cSrcweir#endif
95cdf0e10cSrcweir	return YES;
96cdf0e10cSrcweir}
97cdf0e10cSrcweir
98cdf0e10cSrcweir+ (void) sendDistributedNotification: (NSString*) notificationName targetBundleIdentifier: (NSString*) targetIdentifier
99cdf0e10cSrcweir{
100cdf0e10cSrcweir    NSDictionary* userInfo = [NSDictionary dictionaryWithObjectsAndKeys: [NSString stringWithCString:[self remoteControlDeviceName] encoding:NSASCIIStringEncoding],
101cdf0e10cSrcweir                            kRemoteControlDeviceName /* key = RemoteControlDeviceName  -> OK */,
102cdf0e10cSrcweir                            [[NSBundle mainBundle] bundleIdentifier] /* value = org.openoffice.script -> OK */,
103cdf0e10cSrcweir                            kApplicationIdentifier/* key = CFBundleIdentifier -> OK */,
104cdf0e10cSrcweir                            targetIdentifier /*value = AppleIRController -> OK */,
105cdf0e10cSrcweir                            kTargetApplicationIdentifier /*targetBundleIdentifier -> does not appear, since the peer is nil*/,
106cdf0e10cSrcweir                            nil];
107cdf0e10cSrcweir#ifdef DEBUG
108*53e04a5dSHerbert Dürr    NSLog( @"Apple Remote: sendDistributedNotification ...");
109cdf0e10cSrcweir    // Debug purpose: returns all the existing dictionary keys.
110*53e04a5dSHerbert Dürr    NSEnumerator* itKey = [userInfo keyEnumerator];
111*53e04a5dSHerbert Dürr    NSEnumerator* itVal = [userInfo objectEnumerator];
112*53e04a5dSHerbert Dürr    for(;;) {
113*53e04a5dSHerbert Dürr        NSString* sKey = [itKey nextObject];
114*53e04a5dSHerbert Dürr        NSString* sVal = [itVal nextObject];
115*53e04a5dSHerbert Dürr        if( !sKey && !sVal)
116*53e04a5dSHerbert Dürr            break;
117*53e04a5dSHerbert Dürr        if( !sKey) sKey = @"nil";
118*53e04a5dSHerbert Dürr	if( !sVal) sVal = @"nil";
119*53e04a5dSHerbert Dürr        NSLog( @"\tARdict[\"%@\"] = \"%@\"",sKey,sVal);
120cdf0e10cSrcweir    }
121cdf0e10cSrcweir#endif
122cdf0e10cSrcweir
123cdf0e10cSrcweir	[[NSDistributedNotificationCenter defaultCenter] postNotificationName:notificationName
124cdf0e10cSrcweir																   object:nil
125cdf0e10cSrcweir																 userInfo:userInfo
126cdf0e10cSrcweir													   deliverImmediately:YES];
127cdf0e10cSrcweir}
128cdf0e10cSrcweir
129cdf0e10cSrcweir+ (void) sendFinishedNotifcationForAppIdentifier: (NSString*) identifier {
130cdf0e10cSrcweir    [self sendDistributedNotification:FINISHED_USING_REMOTE_CONTROL_NOTIFICATION targetBundleIdentifier:identifier];
131cdf0e10cSrcweir#ifdef DEBUG
132*53e04a5dSHerbert Dürr    NSLog( @"Apple RemoteControl: sendFinishedNotifcationForAppIdentifier ...");
133cdf0e10cSrcweir#endif
134cdf0e10cSrcweir}
135cdf0e10cSrcweir+ (void) sendRequestForRemoteControlNotification {
136cdf0e10cSrcweir    [self sendDistributedNotification:REQUEST_FOR_REMOTE_CONTROL_NOTIFCATION targetBundleIdentifier:nil];
137cdf0e10cSrcweir#ifdef DEBUG
138*53e04a5dSHerbert Dürr    NSLog( @"Apple RemoteControl: sendRequestForRemoteControlNotification ...");
139cdf0e10cSrcweir#endif
140cdf0e10cSrcweir}
141cdf0e10cSrcweir
142cdf0e10cSrcweir+ (const char*) remoteControlDeviceName {
143cdf0e10cSrcweir	return NULL;
144cdf0e10cSrcweir}
145cdf0e10cSrcweir
146cdf0e10cSrcweir@end
147