1 /*****************************************************************************
2 * eyetv.m: small class to control the notification parts of the EyeTV plugin
3 *****************************************************************************
4 * Copyright (C) 2006-2007 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 msg_Dbg( VLCIntf, "notification received in VLC with name %s and object %s",
61 [[theNotification name] UTF8String], [[theNotification object] UTF8String] );
63 /* update our info on the used device */
64 if( [[theNotification name] isEqualToString: @"DeviceAdded"] )
65 b_deviceConnected = YES;
66 if( [[theNotification name] isEqualToString: @"DeviceRemoved"] )
67 b_deviceConnected = NO;
69 /* is eyetv running? */
70 if( [[theNotification name] isEqualToString: @"PluginInit"] )
72 if( [[theNotification name] isEqualToString: @"PluginQuit"] )
76 - (BOOL)isEyeTVrunning
81 - (BOOL)isDeviceConnected
83 return b_deviceConnected;
89 NSAppleScript *script = [[NSAppleScript alloc] initWithSource:
90 @"tell application \"EyeTV\"\n"
91 "launch with server mode\n"
93 NSDictionary *errorDict;
94 NSAppleEventDescriptor *descriptor = [script executeAndReturnError:&errorDict];
95 if( nil == descriptor )
97 NSString *errorString = [errorDict objectForKey:NSAppleScriptErrorMessage];
98 msg_Err( VLCIntf, "opening EyeTV failed with error status '%s'", [errorString UTF8String] );
103 - (int)currentChannel
105 int currentChannel = 0;
106 NSAppleScript *script = [[NSAppleScript alloc] initWithSource:
107 @"tell application \"EyeTV\" to get current channel"];
108 NSDictionary *errorDict;
109 NSAppleEventDescriptor *descriptor = [script executeAndReturnError:&errorDict];
110 if( nil == descriptor )
112 NSString *errorString = [errorDict objectForKey:NSAppleScriptErrorMessage];
113 msg_Err( VLCIntf, "EyeTV channel inventory failed with error status '%s'", [errorString UTF8String] );
117 currentChannel = (int)[descriptor int32Value];
120 return currentChannel;
123 - (int)switchChannelUp:(BOOL)b_yesOrNo
125 int currentChannel = 0;
126 NSAppleScript *script;
127 NSDictionary *errorDict;
128 NSAppleEventDescriptor *descriptor;
130 if( b_yesOrNo == YES )
132 script = [[NSAppleScript alloc] initWithSource:
133 @"tell application \"EyeTV\"\n"
135 "get current channel\n"
137 msg_Dbg( VLCIntf, "telling eyetv to switch 1 channel up" );
141 script = [[NSAppleScript alloc] initWithSource:
142 @"tell application \"EyeTV\"\n"
144 "get current channel\n"
146 msg_Dbg( VLCIntf, "telling eyetv to switch 1 channel down" );
149 descriptor = [script executeAndReturnError:&errorDict];
150 if( nil == descriptor )
152 NSString *errorString = [errorDict objectForKey:NSAppleScriptErrorMessage];
153 msg_Err( VLCIntf, "EyeTV channel change failed with error status '%s'", [errorString UTF8String] );
157 currentChannel = (int)[descriptor int32Value];
160 return currentChannel;
163 - (void)selectChannel: (int)theChannelNum
165 NSAppleScript *script;
166 switch( theChannelNum )
168 case -2: // Composite
169 script = [[NSAppleScript alloc] initWithSource:
170 @"tell application \"EyeTV\"\n"
171 " input_change input source composite video input\n"
172 " show player_window\n"
176 script = [[NSAppleScript alloc] initWithSource:
177 @"tell application \"EyeTV\"\n"
178 " input_change input source S video input\n"
179 " show player_window\n"
183 script = [[NSAppleScript alloc] initWithSource:
184 @"tell application \"EyeTV\"\n"
185 " show player_window\n"
189 if( theChannelNum > 0 )
191 NSString *channel_change = [NSString stringWithFormat:
192 @"tell application \"EyeTV\"\n"
193 " channel_change channel number %d\n"
194 " show player_window\n"
195 "end tell", theChannelNum];
196 script = [[NSAppleScript alloc] initWithSource:channel_change];
201 NSDictionary *errorDict;
202 NSAppleEventDescriptor *descriptor = [script executeAndReturnError:&errorDict];
203 if( nil == descriptor )
205 NSString *errorString = [errorDict objectForKey:NSAppleScriptErrorMessage];
206 msg_Err( VLCIntf, "EyeTV source change failed with error status '%s'", [errorString UTF8String] );
211 - (NSEnumerator *)allChannels
213 NSEnumerator *channels = nil;
214 NSAppleScript *script = [[NSAppleScript alloc] initWithSource:
215 @"tell application \"EyeTV\" to get name of every channel"];
216 NSDictionary *errorDict;
217 NSAppleEventDescriptor *descriptor = [script executeAndReturnError:&errorDict];
218 if( nil == descriptor )
220 NSString *errorString = [errorDict objectForKey:NSAppleScriptErrorMessage];
221 msg_Err( VLCIntf, "EyeTV channel inventory failed with error status '%s'", [errorString UTF8String] );
225 int count = [descriptor numberOfItems];
227 NSMutableArray *channelArray = [NSMutableArray arrayWithCapacity:count];
228 while( x++ < count ) {
229 [channelArray addObject:[[descriptor descriptorAtIndex:x] stringValue]];
231 channels = [channelArray objectEnumerator];