1 /*****************************************************************************
2 * eyetv.m: small class to control the notification parts of the EyeTV plugin
3 *****************************************************************************
4 * Copyright (C) 2006-2011 the VideoLAN team
7 * Authors: Felix Kühne <fkuehne at videolan dot org>
8 * Damien Fouilleul <damienf at videolan dot org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
26 /* for apple event interaction [carbon] */
27 //#import <Foundation/NSAppleScript>
28 /* for various VLC core related calls */
31 @implementation VLCEyeTVController
33 static VLCEyeTVController *_o_sharedInstance = nil;
35 + (VLCEyeTVController *)sharedInstance
37 return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
42 if (_o_sharedInstance) {
45 _o_sharedInstance = [super init];
47 [[NSDistributedNotificationCenter defaultCenter]
49 selector: @selector(globalNotificationReceived:)
51 object: @"VLCEyeTVSupport"
52 suspensionBehavior: NSNotificationSuspensionBehaviorDeliverImmediately];
55 return _o_sharedInstance;
58 - (void)globalNotificationReceived: (NSNotification *)theNotification
60 NSLog( @"notification received in VLC with name %@ and object %@", [theNotification name], [theNotification object] );
62 /* update our info on the used device */
63 if( [[theNotification name] isEqualToString: @"DeviceAdded"] )
64 b_deviceConnected = YES;
65 if( [[theNotification name] isEqualToString: @"DeviceRemoved"] )
66 b_deviceConnected = NO;
68 /* is eyetv running? */
69 if( [[theNotification name] isEqualToString: @"PluginInit"] )
71 if( [[theNotification name] isEqualToString: @"PluginQuit"] )
75 - (BOOL)isEyeTVrunning
80 - (BOOL)isDeviceConnected
82 return b_deviceConnected;
88 NSAppleScript *script = [[NSAppleScript alloc] initWithSource:
89 @"tell application \"EyeTV\"\n"
90 "launch with server mode\n"
92 NSDictionary *errorDict;
93 NSAppleEventDescriptor *descriptor = [script executeAndReturnError:&errorDict];
94 if( nil == descriptor )
96 NSString *errorString = [errorDict objectForKey:NSAppleScriptErrorMessage];
97 NSLog( @"opening EyeTV failed with error status '%@'", errorString );
102 - (int)currentChannel
104 int currentChannel = 0;
105 NSAppleScript *script = [[NSAppleScript alloc] initWithSource:
106 @"tell application \"EyeTV\" to get current channel"];
107 NSDictionary *errorDict;
108 NSAppleEventDescriptor *descriptor = [script executeAndReturnError:&errorDict];
109 if( nil == descriptor )
111 NSString *errorString = [errorDict objectForKey:NSAppleScriptErrorMessage];
112 NSLog( @"EyeTV channel inventory failed with error status '%@'", errorString );
116 currentChannel = (int)[descriptor int32Value];
119 return currentChannel;
122 - (int)switchChannelUp:(BOOL)b_yesOrNo
124 int currentChannel = 0;
125 NSAppleScript *script;
126 NSDictionary *errorDict;
127 NSAppleEventDescriptor *descriptor;
129 if( b_yesOrNo == YES )
131 script = [[NSAppleScript alloc] initWithSource:
132 @"tell application \"EyeTV\"\n"
134 "get current channel\n"
136 NSLog( @"telling eyetv to switch 1 channel up" );
140 script = [[NSAppleScript alloc] initWithSource:
141 @"tell application \"EyeTV\"\n"
143 "get current channel\n"
145 NSLog( @"telling eyetv to switch 1 channel down" );
148 descriptor = [script executeAndReturnError:&errorDict];
149 if( nil == descriptor )
151 NSString *errorString = [errorDict objectForKey:NSAppleScriptErrorMessage];
152 NSLog( @"EyeTV channel change failed with error status '%@'", errorString );
156 currentChannel = (int)[descriptor int32Value];
159 return currentChannel;
162 - (void)selectChannel: (int)theChannelNum
164 NSAppleScript *script;
165 switch( theChannelNum )
167 case -2: // Composite
168 script = [[NSAppleScript alloc] initWithSource:
169 @"tell application \"EyeTV\"\n"
170 " input_change input source composite video input\n"
171 " show player_window\n"
175 script = [[NSAppleScript alloc] initWithSource:
176 @"tell application \"EyeTV\"\n"
177 " input_change input source S video input\n"
178 " show player_window\n"
182 script = [[NSAppleScript alloc] initWithSource:
183 @"tell application \"EyeTV\"\n"
184 " show player_window\n"
188 if( theChannelNum > 0 )
190 NSString *channel_change = [NSString stringWithFormat:
191 @"tell application \"EyeTV\"\n"
192 " channel_change channel number %d\n"
193 " show player_window\n"
194 "end tell", theChannelNum];
195 script = [[NSAppleScript alloc] initWithSource:channel_change];
200 NSDictionary *errorDict;
201 NSAppleEventDescriptor *descriptor = [script executeAndReturnError:&errorDict];
202 if( nil == descriptor )
204 NSString *errorString = [errorDict objectForKey:NSAppleScriptErrorMessage];
205 NSLog( @"EyeTV source change failed with error status '%@'", errorString );
210 - (NSEnumerator *)allChannels
212 NSEnumerator *channels = nil;
213 NSAppleScript *script = [[NSAppleScript alloc] initWithSource:
214 @"tell application \"EyeTV\" to get name of every channel"];
215 NSDictionary *errorDict;
216 NSAppleEventDescriptor *descriptor = [script executeAndReturnError:&errorDict];
217 if( nil == descriptor )
219 NSString *errorString = [errorDict objectForKey:NSAppleScriptErrorMessage];
220 NSLog( @"EyeTV channel inventory failed with error status '%@'", errorString );
224 int count = [descriptor numberOfItems];
226 NSMutableArray *channelArray = [NSMutableArray arrayWithCapacity:count];
227 while( x++ < count ) {
228 [channelArray addObject:[[descriptor descriptorAtIndex:x] stringValue]];
230 channels = [channelArray objectEnumerator];