1 /*****************************************************************************
2 * eyetv.m: small class to control the notification parts of the EyeTV plugin
3 *****************************************************************************
4 * Copyright (C) 2006-2011 VLC authors and VideoLAN
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 /* update our info on the used device */
61 if( [[theNotification name] isEqualToString: @"DeviceAdded"] )
62 b_deviceConnected = YES;
63 if( [[theNotification name] isEqualToString: @"DeviceRemoved"] )
64 b_deviceConnected = NO;
66 /* is eyetv running? */
67 if( [[theNotification name] isEqualToString: @"PluginInit"] )
69 if( [[theNotification name] isEqualToString: @"PluginQuit"] )
73 - (BOOL)isEyeTVrunning
78 - (BOOL)isDeviceConnected
80 return b_deviceConnected;
86 NSAppleScript *script = [[NSAppleScript alloc] initWithSource:
87 @"tell application \"EyeTV\"\n"
88 "launch with server mode\n"
90 NSDictionary *errorDict;
91 NSAppleEventDescriptor *descriptor = [script executeAndReturnError:&errorDict];
92 if( nil == descriptor )
94 NSString *errorString = [errorDict objectForKey:NSAppleScriptErrorMessage];
95 NSLog( @"opening EyeTV failed with error status '%@'", errorString );
100 - (int)currentChannel
102 int currentChannel = 0;
103 NSAppleScript *script = [[NSAppleScript alloc] initWithSource:
104 @"tell application \"EyeTV\" to get current channel"];
105 NSDictionary *errorDict;
106 NSAppleEventDescriptor *descriptor = [script executeAndReturnError:&errorDict];
107 if( nil == descriptor )
109 NSString *errorString = [errorDict objectForKey:NSAppleScriptErrorMessage];
110 NSLog( @"EyeTV channel inventory failed with error status '%@'", errorString );
114 currentChannel = (int)[descriptor int32Value];
117 return currentChannel;
120 - (int)switchChannelUp:(BOOL)b_yesOrNo
122 int currentChannel = 0;
123 NSAppleScript *script;
124 NSDictionary *errorDict;
125 NSAppleEventDescriptor *descriptor;
127 if( b_yesOrNo == YES )
129 script = [[NSAppleScript alloc] initWithSource:
130 @"tell application \"EyeTV\"\n"
132 "get current channel\n"
134 NSLog( @"telling eyetv to switch 1 channel up" );
138 script = [[NSAppleScript alloc] initWithSource:
139 @"tell application \"EyeTV\"\n"
141 "get current channel\n"
143 NSLog( @"telling eyetv to switch 1 channel down" );
146 descriptor = [script executeAndReturnError:&errorDict];
147 if( nil == descriptor )
149 NSString *errorString = [errorDict objectForKey:NSAppleScriptErrorMessage];
150 NSLog( @"EyeTV channel change failed with error status '%@'", errorString );
154 currentChannel = (int)[descriptor int32Value];
157 return currentChannel;
160 - (void)selectChannel: (int)theChannelNum
162 NSAppleScript *script;
163 switch( theChannelNum )
165 case -2: // Composite
166 script = [[NSAppleScript alloc] initWithSource:
167 @"tell application \"EyeTV\"\n"
168 " input_change input source composite video input\n"
169 " show player_window\n"
173 script = [[NSAppleScript alloc] initWithSource:
174 @"tell application \"EyeTV\"\n"
175 " input_change input source S video input\n"
176 " show player_window\n"
180 script = [[NSAppleScript alloc] initWithSource:
181 @"tell application \"EyeTV\"\n"
182 " show player_window\n"
186 if( theChannelNum > 0 )
188 NSString *channel_change = [NSString stringWithFormat:
189 @"tell application \"EyeTV\"\n"
190 " channel_change channel number %d\n"
191 " show player_window\n"
192 "end tell", theChannelNum];
193 script = [[NSAppleScript alloc] initWithSource:channel_change];
198 NSDictionary *errorDict;
199 NSAppleEventDescriptor *descriptor = [script executeAndReturnError:&errorDict];
200 if( nil == descriptor )
202 NSString *errorString = [errorDict objectForKey:NSAppleScriptErrorMessage];
203 NSLog( @"EyeTV source change failed with error status '%@'", errorString );
208 - (NSEnumerator *)allChannels
210 NSEnumerator *channels = nil;
211 NSAppleScript *script = [[NSAppleScript alloc] initWithSource:
212 @"tell application \"EyeTV\" to get name of every channel"];
213 NSDictionary *errorDict;
214 NSAppleEventDescriptor *descriptor = [script executeAndReturnError:&errorDict];
215 if( nil == descriptor )
217 NSString *errorString = [errorDict objectForKey:NSAppleScriptErrorMessage];
218 NSLog( @"EyeTV channel inventory failed with error status '%@'", errorString );
222 int count = [descriptor numberOfItems];
224 NSMutableArray *channelArray = [NSMutableArray arrayWithCapacity:count];
225 while( x++ < count ) {
226 [channelArray addObject:[[descriptor descriptorAtIndex:x] stringValue]];
228 channels = [channelArray objectEnumerator];