aboutsummaryrefslogtreecommitdiff
path: root/tools/EventClients/Clients/OSXRemote/XBMCHelper.m
blob: d3f0d38bcbab3a417779d9e334d04b8d3238adce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
//
//  XBMCHelper.m
//  xbmchelper
//
//  Created by Stephan Diederich on 11/12/08.
//  Copyright 2008 University Heidelberg. All rights reserved.
//

#import "XBMCHelper.h"
#import "XBMCDebugHelpers.h"

//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
@interface XBMCHelper (private)

- (NSString *)buttonNameForButtonCode:(HIDRemoteButtonCode)buttonCode;
- (void) checkAndLaunchApp;

@end

@implementation XBMCHelper
- (id) init{
  if( (self = [super init]) ){
    if ((remote = [HIDRemote sharedHIDRemote]))
    {
      [remote setDelegate:self];
      [remote setSimulateHoldEvents:NO];
      //for now, we're using lending of exclusive lock
      //kHIDRemoteModeExclusiveAuto isn't working, as we're a background daemon
      //one possibility would be to know when XBMC is running. Once we know that,
      //we could acquire exclusive lock when it's running, and release _exclusive_
      //access once done
      [remote setExclusiveLockLendingEnabled:YES];

      if ([HIDRemote isCandelairInstallationRequiredForRemoteMode:kHIDRemoteModeExclusive])
      {
        //setup failed. user needs to install CandelaIR driver
        NSLog(@"Error! Candelair driver installation necessary. XBMCHelper won't function properly!");
        NSLog(@"Due to an issue in the OS version you are running, an additional driver needs to be installed before XBMC(Helper) can reliably access the remote.");
        NSLog(@"See http://www.candelair.com/download/ for details");
        [super dealloc];
        return nil;
      }
      else
      {
        if ([remote startRemoteControl:kHIDRemoteModeExclusive])
        {
          DLOG(@"Driver has started successfully.");
          if ([remote activeRemoteControlCount])
            DLOG(@"Driver has found %d remotes.", [remote activeRemoteControlCount]);
          else
            ELOG(@"Driver has not found any remotes it could use. Will use remotes as they become available.");
        }
        else
        {
          ELOG(@"Failed to start remote control.");
          //setup failed, cleanup
          [remote setDelegate:nil];
          [super dealloc];
          return nil;
        }
      }
    }
  }
  return self;
}

//----------------------------------------------------------------------------
- (void) dealloc{
  [remote stopRemoteControl];
  if( [remote delegate] == self)
    [remote setDelegate:nil];
  [mp_wrapper release];
  [mp_app_path release];
  [mp_home_path release];

  [super dealloc];
}

//----------------------------------------------------------------------------
- (void) connectToServer:(NSString*) fp_server onPort:(int) f_port withMode:(eRemoteMode) f_mode withTimeout:(double) f_timeout{
  if(mp_wrapper)
    [self disconnect];
  mp_wrapper = [[XBMCClientWrapper alloc] initWithMode:f_mode serverAddress:fp_server port:f_port verbose:m_verbose];
  [mp_wrapper setUniversalModeTimeout:f_timeout];
}

//----------------------------------------------------------------------------
- (void) disconnect{
  [mp_wrapper release];
  mp_wrapper = nil;
}

//----------------------------------------------------------------------------
- (void) enableVerboseMode:(bool) f_really{
  m_verbose = f_really;
  [mp_wrapper enableVerboseMode:f_really];
}

//----------------------------------------------------------------------------
- (void) setApplicationPath:(NSString*) fp_app_path{
  if (mp_app_path != fp_app_path) {
    [mp_app_path release];
    mp_app_path = [[fp_app_path stringByStandardizingPath] retain];
  }
}

//----------------------------------------------------------------------------
- (void) setApplicationHome:(NSString*) fp_home_path{
  if (mp_home_path != fp_home_path) {
    [mp_home_path release];
    mp_home_path = [[fp_home_path stringByStandardizingPath] retain];
  }
}

#pragma mark -
#pragma mark HIDRemote delegate methods

// Notification of button events
- (void)hidRemote:(HIDRemote *)hidRemote eventWithButton:(HIDRemoteButtonCode)buttonCode
        isPressed:(BOOL)isPressed fromHardwareWithAttributes:(NSMutableDictionary *)attributes
{
  if(m_verbose){
    NSLog(@"Received button '%@' %@ event", [self buttonNameForButtonCode:buttonCode], (isPressed)?@"press":@"release");
  }
  switch(buttonCode)
  {
    case kHIDRemoteButtonCodeUp:
      if(isPressed)
        [mp_wrapper handleEvent:ATV_BUTTON_UP];
      else
        [mp_wrapper handleEvent:ATV_BUTTON_UP_RELEASE];
      break;
    case kHIDRemoteButtonCodeDown:
      if(isPressed)
        [mp_wrapper handleEvent:ATV_BUTTON_DOWN];
      else
        [mp_wrapper handleEvent:ATV_BUTTON_DOWN_RELEASE];
      break;
    case kHIDRemoteButtonCodeLeft:
      if(isPressed)
        [mp_wrapper handleEvent:ATV_BUTTON_LEFT];
      else
        [mp_wrapper handleEvent:ATV_BUTTON_LEFT_RELEASE];
      break;
    case kHIDRemoteButtonCodeRight:
      if(isPressed)
        [mp_wrapper handleEvent:ATV_BUTTON_RIGHT];
      else
        [mp_wrapper handleEvent:ATV_BUTTON_RIGHT_RELEASE];
      break;      
    case kHIDRemoteButtonCodeCenter:
      if(isPressed) [mp_wrapper handleEvent:ATV_BUTTON_CENTER];
      break;
    case kHIDRemoteButtonCodeMenu:
      if(isPressed){
        [self checkAndLaunchApp]; //launch mp_app_path if it's not running
        [mp_wrapper handleEvent:ATV_BUTTON_MENU];
      }
      break;
    case kHIDRemoteButtonCodePlay: //aluminium remote
      if(isPressed) {
        [mp_wrapper handleEvent:ATV_BUTTON_PLAY];
      }
      break;
//    case kHIDRemoteButtonCodeUpHold:
//      //TODO
//      break;
//    case kHIDRemoteButtonCodeDownHold:
//      //TODO
      break;
    case kHIDRemoteButtonCodeLeftHold:
      if(isPressed)
        [mp_wrapper handleEvent:ATV_BUTTON_LEFT_H];
      else
        [mp_wrapper handleEvent:ATV_BUTTON_LEFT_H_RELEASE];
      break;
    case kHIDRemoteButtonCodeRightHold:
      if(isPressed)
        [mp_wrapper handleEvent:ATV_BUTTON_RIGHT_H];
      else
        [mp_wrapper handleEvent:ATV_BUTTON_RIGHT_H_RELEASE];
      break;
    case kHIDRemoteButtonCodeCenterHold:
      if(isPressed) [mp_wrapper handleEvent:ATV_BUTTON_CENTER_H];
      break;      
    case kHIDRemoteButtonCodeMenuHold:
      if(isPressed) {
        [self checkAndLaunchApp]; //launch mp_app_path if it's not running
        [mp_wrapper handleEvent:ATV_BUTTON_MENU_H];
      }
      break;
    case kHIDRemoteButtonCodePlayHold: //aluminium remote
      if(isPressed) {
        [mp_wrapper handleEvent:ATV_BUTTON_PLAY_H];
      }
      break;
    default:
      NSLog(@"Oha, remote button not recognized %i pressed/released %i", buttonCode, isPressed);
  }
}


// Notification of ID changes
- (void)hidRemote:(HIDRemote *)hidRemote remoteIDChangedOldID:(SInt32)old 
            newID:(SInt32)newID forHardwareWithAttributes:(NSMutableDictionary *)attributes
{
  if(m_verbose)
    NSLog(@"Change of remote ID from %d to %d", old, newID);
  [mp_wrapper switchRemote: newID];
  
}

#pragma mark -
#pragma mark Helper methods

- (NSString *)buttonNameForButtonCode:(HIDRemoteButtonCode)buttonCode
{
	switch (buttonCode)
	{
		case kHIDRemoteButtonCodePlus:
			return (@"Plus");
      break;
		case kHIDRemoteButtonCodeMinus:
			return (@"Minus");
      break;
		case kHIDRemoteButtonCodeLeft:
			return (@"Left");
      break;
		case kHIDRemoteButtonCodeRight:
			return (@"Right");
      break;
		case kHIDRemoteButtonCodePlayPause:
			return (@"Play/Pause");
      break;
		case kHIDRemoteButtonCodeMenu:
			return (@"Menu");
      break;
		case kHIDRemoteButtonCodePlusHold:
			return (@"Plus (hold)");
      break;
		case kHIDRemoteButtonCodeMinusHold:
			return (@"Minus (hold)");
      break;
		case kHIDRemoteButtonCodeLeftHold:
			return (@"Left (hold)");
      break;
		case kHIDRemoteButtonCodeRightHold:
			return (@"Right (hold)");
      break;
		case kHIDRemoteButtonCodePlayPauseHold:
			return (@"Play/Pause (hold)");
      break;
		case kHIDRemoteButtonCodeMenuHold:
			return (@"Menu (hold)");
      break;
	}
	return ([NSString stringWithFormat:@"Button %x", (int)buttonCode]);
}

//----------------------------------------------------------------------------
- (void) checkAndLaunchApp
{
  if(!mp_app_path || ![mp_app_path length]){
    ELOG(@"No executable set. Nothing to launch");
    return;
  }
  NSFileManager *fileManager = [NSFileManager defaultManager];
  if([fileManager fileExistsAtPath:mp_app_path]){
    if(mp_home_path && [mp_home_path length])
      setenv("KODI_HOME", [mp_home_path UTF8String], 1);
    //launch or activate xbmc
    if(![[NSWorkspace sharedWorkspace] launchApplication:mp_app_path])
      ELOG(@"Error launching %@", mp_app_path);
  } else
    ELOG(@"Path does not exist: %@. Cannot launch executable", mp_app_path);
}


#pragma mark -
#pragma mark Other (unused) HIDRemoteDelegate methods
//- (BOOL)hidRemote:(HIDRemote *)aHidRemote
//lendExclusiveLockToApplicationWithInfo:(NSDictionary *)applicationInfo
//{
//	NSLog(@"Lending exclusive lock to %@ (pid %@)", [applicationInfo objectForKey:(id)kCFBundleIdentifierKey], [applicationInfo objectForKey:kHIDRemoteDNStatusPIDKey]);
//	return (YES);
//}
//
//- (void)hidRemote:(HIDRemote *)aHidRemote
//exclusiveLockReleasedByApplicationWithInfo:(NSDictionary *)applicationInfo
//{
//  NSLog(@"Exclusive lock released by %@ (pid %@)", [applicationInfo objectForKey:(id)kCFBundleIdentifierKey], [applicationInfo objectForKey:kHIDRemoteDNStatusPIDKey]);
//	[aHidRemote startRemoteControl:kHIDRemoteModeExclusive];
//}
//
//- (BOOL)hidRemote:(HIDRemote *)aHidRemote
//shouldRetryExclusiveLockWithInfo:(NSDictionary *)applicationInfo
//{
//  NSLog(@"%@ (pid %@) says I should retry to acquire exclusive locks", [applicationInfo objectForKey:(id)kCFBundleIdentifierKey], [applicationInfo objectForKey:kHIDRemoteDNStatusPIDKey]);
//	return (YES);
//}
//
//
//// Notification about hardware additions/removals
//- (void)hidRemote:(HIDRemote *)aHidRemote foundNewHardwareWithAttributes:(NSMutableDictionary *)attributes
//{
//	NSLog(@"Found hardware: %@ by %@ (Transport: %@)", [attributes objectForKey:kHIDRemoteProduct], [attributes objectForKey:kHIDRemoteManufacturer], [attributes objectForKey:kHIDRemoteTransport]);
//}
//
//- (void)hidRemote:(HIDRemote *)aHidRemote failedNewHardwareWithError:(NSError *)error
//{
//	NSLog(@"Initialization of hardware failed with error %@ (%@)", [error localizedDescription], [[error userInfo] objectForKey:@"InternalErrorCode"]);
//}
//
//- (void)hidRemote:(HIDRemote *)aHidRemote releasedHardwareWithAttributes:(NSMutableDictionary *)attributes
//{
//	NSLog(@"Released hardware: %@ by %@ (Transport: %@)", [attributes objectForKey:kHIDRemoteProduct], [attributes objectForKey:kHIDRemoteManufacturer], [attributes objectForKey:kHIDRemoteTransport]);
//}

@end