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 @synthesize eyeTVRunning = b_eyeTVactive, deviceConnected = b_deviceConnected;
35 static VLCEyeTVController *_o_sharedInstance = nil;
37 + (VLCEyeTVController *)sharedInstance
39 return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
44 if (_o_sharedInstance)
47 _o_sharedInstance = [super init];
49 [[NSDistributedNotificationCenter defaultCenter]
51 selector: @selector(globalNotificationReceived:)
53 object: @"VLCEyeTVSupport"
54 suspensionBehavior: NSNotificationSuspensionBehaviorDeliverImmediately];
57 return _o_sharedInstance;
60 - (void)globalNotificationReceived: (NSNotification *)theNotification
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"])
77 NSAppleScript *script = [[NSAppleScript alloc] initWithSource:
78 @"tell application \"EyeTV\"\n"
79 "launch with server mode\n"
81 NSDictionary *errorDict;
82 NSAppleEventDescriptor *descriptor = [script executeAndReturnError:&errorDict];
83 if (nil == descriptor) {
84 NSString *errorString = [errorDict objectForKey:NSAppleScriptErrorMessage];
85 NSLog(@"opening EyeTV failed with error status '%@'", errorString);
92 int currentChannel = 0;
93 NSAppleScript *script = [[NSAppleScript alloc] initWithSource:
94 @"tell application \"EyeTV\" to get current channel"];
95 NSDictionary *errorDict;
96 NSAppleEventDescriptor *descriptor = [script executeAndReturnError:&errorDict];
97 if (nil == descriptor) {
98 NSString *errorString = [errorDict objectForKey:NSAppleScriptErrorMessage];
99 NSLog(@"EyeTV channel inventory failed with error status '%@'", errorString);
101 currentChannel = (int)[descriptor int32Value];
103 return currentChannel;
106 - (int)switchChannelUp:(BOOL)b_yesOrNo
108 int currentChannel = 0;
109 NSAppleScript *script;
110 NSDictionary *errorDict;
111 NSAppleEventDescriptor *descriptor;
113 if (b_yesOrNo == YES) {
114 script = [[NSAppleScript alloc] initWithSource:
115 @"tell application \"EyeTV\"\n"
117 "get current channel\n"
119 NSLog(@"telling eyetv to switch 1 channel up");
121 script = [[NSAppleScript alloc] initWithSource:
122 @"tell application \"EyeTV\"\n"
124 "get current channel\n"
126 NSLog(@"telling eyetv to switch 1 channel down");
129 descriptor = [script executeAndReturnError:&errorDict];
130 if (nil == descriptor) {
131 NSString *errorString = [errorDict objectForKey:NSAppleScriptErrorMessage];
132 NSLog(@"EyeTV channel change failed with error status '%@'", errorString);
134 currentChannel = (int)[descriptor int32Value];
137 return currentChannel;
140 - (void)setChannel: (int)theChannelNum
142 NSAppleScript *script;
143 switch(theChannelNum) {
144 case -2: // Composite
145 script = [[NSAppleScript alloc] initWithSource:
146 @"tell application \"EyeTV\"\n"
147 " input_change input source composite video input\n"
148 " show player_window\n"
152 script = [[NSAppleScript alloc] initWithSource:
153 @"tell application \"EyeTV\"\n"
154 " input_change input source S video input\n"
155 " show player_window\n"
159 script = [[NSAppleScript alloc] initWithSource:
160 @"tell application \"EyeTV\"\n"
161 " show player_window\n"
165 if (theChannelNum > 0) {
166 NSString *channel_change = [NSString stringWithFormat:
167 @"tell application \"EyeTV\"\n"
168 " channel_change channel number %d\n"
169 " show player_window\n"
170 "end tell", theChannelNum];
171 script = [[NSAppleScript alloc] initWithSource:channel_change];
176 NSDictionary *errorDict;
177 NSAppleEventDescriptor *descriptor = [script executeAndReturnError:&errorDict];
178 if (nil == descriptor) {
179 NSString *errorString = [errorDict objectForKey:NSAppleScriptErrorMessage];
180 NSLog(@"EyeTV source change failed with error status '%@'", errorString);
185 - (NSEnumerator *)allChannels
187 NSEnumerator *channels = nil;
188 NSAppleScript *script = [[NSAppleScript alloc] initWithSource:
189 @"tell application \"EyeTV\" to get name of every channel"];
190 NSDictionary *errorDict;
191 NSAppleEventDescriptor *descriptor = [script executeAndReturnError:&errorDict];
192 if (nil == descriptor) {
193 NSString *errorString = [errorDict objectForKey:NSAppleScriptErrorMessage];
194 NSLog(@"EyeTV channel inventory failed with error status '%@'", errorString);
196 int count = [descriptor numberOfItems];
198 NSMutableArray *channelArray = [NSMutableArray arrayWithCapacity:count];
200 [channelArray addObject:[[descriptor descriptorAtIndex:x] stringValue]];
202 channels = [channelArray objectEnumerator];