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 "volume_change level 0\n"
136 "tell application \"System Events\" to set visible of process \"EyeTV\" to false\n"
137 "get current channel\n"
139 msg_Dbg( VLCIntf, "telling eyetv to switch 1 channel up" );
143 script = [[NSAppleScript alloc] initWithSource:
144 @"tell application \"EyeTV\"\n"
146 "volume_change level 0\n"
147 "tell application \"System Events\" to set visible of process \"EyeTV\" to false\n"
148 "get current channel\n"
150 msg_Dbg( VLCIntf, "telling eyetv to switch 1 channel down" );
153 descriptor = [script executeAndReturnError:&errorDict];
154 if( nil == descriptor )
156 NSString *errorString = [errorDict objectForKey:NSAppleScriptErrorMessage];
157 msg_Err( VLCIntf, "EyeTV channel change failed with error status '%s'", [errorString UTF8String] );
161 currentChannel = (int)[descriptor int32Value];
164 return currentChannel;
167 - (void)selectChannel: (int)theChannelNum
169 NSAppleScript *script;
170 switch( theChannelNum )
172 case -2: // Composite
173 script = [[NSAppleScript alloc] initWithSource:
174 @"tell application \"EyeTV\"\n"
175 " input_change input source composite video input"
176 " volume_change level 0\n"
177 " tell application \"System Events\" to set visible of process \"EyeTV\" to false\n"
181 script = [[NSAppleScript alloc] initWithSource:
182 @"tell application \"EyeTV\"\n"
183 " input_change input source S video input"
184 " volume_change level 0\n"
185 " tell application \"System Events\" to set visible of process \"EyeTV\" to false\n"
189 script = [[NSAppleScript alloc] initWithSource:
190 @"tell application \"EyeTV\"\n"
191 " input_change input source tuner input"
192 " volume_change level 0\n"
193 " tell application \"System Events\" to set visible of process \"EyeTV\" to false\n"
197 if( theChannelNum > 0 )
199 NSString *channel_change = [NSString stringWithFormat:
200 @"tell application \"EyeTV\"\n"
201 @" channel_change channel number %d\n"
202 " volume_change level 0\n"
203 " tell application \"System Events\" to set visible of process \"EyeTV\" to false\n"
204 "end tell", theChannelNum];
205 script = [[NSAppleScript alloc] initWithSource:channel_change];
210 NSDictionary *errorDict;
211 NSAppleEventDescriptor *descriptor = [script executeAndReturnError:&errorDict];
212 if( nil == descriptor )
214 NSString *errorString = [errorDict objectForKey:NSAppleScriptErrorMessage];
215 msg_Err( VLCIntf, "EyeTV source change failed with error status '%s'", [errorString UTF8String] );
220 - (NSEnumerator *)allChannels
222 NSEnumerator *channels = nil;
223 NSAppleScript *script = [[NSAppleScript alloc] initWithSource:
224 @"tell application \"EyeTV\" to get name of every channel"];
225 NSDictionary *errorDict;
226 NSAppleEventDescriptor *descriptor = [script executeAndReturnError:&errorDict];
227 if( nil == descriptor )
229 NSString *errorString = [errorDict objectForKey:NSAppleScriptErrorMessage];
230 msg_Err( VLCIntf, "EyeTV channel inventory failed with error status '%s'", [errorString UTF8String] );
234 int count = [descriptor numberOfItems];
236 NSMutableArray *channelArray = [NSMutableArray arrayWithCapacity:count];
237 while( x++ < count ) {
238 [channelArray addObject:[[descriptor descriptorAtIndex:x] stringValue]];
240 channels = [channelArray objectEnumerator];