]> git.sesse.net Git - vlc/blob - modules/gui/macosx/eyetv.m
2ba6f90a6901c25d3b5c73872a0aeac9b64280c8
[vlc] / modules / gui / macosx / eyetv.m
1 /*****************************************************************************
2 * eyetv.m: small class to control the notification parts of the EyeTV plugin
3 *****************************************************************************
4 * Copyright (C) 2006-2011 the VideoLAN team
5 * $Id$
6 *
7 * Authors: Felix Kühne <fkuehne at videolan dot org>
8 *          Damien Fouilleul <damienf at videolan dot org>
9 *
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.
14 *
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.
19 *
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 *****************************************************************************/
24
25 #import "eyetv.h"
26 /* for apple event interaction [carbon] */
27 //#import <Foundation/NSAppleScript>
28 /* for various VLC core related calls */
29 #import "intf.h"
30
31 @implementation VLCEyeTVController
32
33 static VLCEyeTVController *_o_sharedInstance = nil;
34
35 + (VLCEyeTVController *)sharedInstance
36 {
37     return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
38 }
39
40 - (id)init 
41 {
42     if (_o_sharedInstance) {
43         [self dealloc];
44     } else {
45         _o_sharedInstance = [super init];
46
47         [[NSDistributedNotificationCenter defaultCenter]
48                     addObserver: self
49                        selector: @selector(globalNotificationReceived:)
50                            name: NULL
51                          object: @"VLCEyeTVSupport"
52              suspensionBehavior: NSNotificationSuspensionBehaviorDeliverImmediately];
53     }
54     
55     return _o_sharedInstance;
56 }
57
58 - (void)globalNotificationReceived: (NSNotification *)theNotification
59 {
60     NSLog( @"notification received in VLC with name %@ and object %@", [theNotification name], [theNotification object] );
61
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;
67
68     /* is eyetv running? */
69     if( [[theNotification name] isEqualToString: @"PluginInit"] )
70         b_eyeTVactive = YES;
71     if( [[theNotification name] isEqualToString: @"PluginQuit"] )
72         b_eyeTVactive = NO;
73 }
74
75 - (BOOL)isEyeTVrunning
76 {
77     return b_eyeTVactive;
78 }
79
80 - (BOOL)isDeviceConnected
81 {
82     return b_deviceConnected;
83 }
84
85
86 - (void)launchEyeTV
87 {
88     NSAppleScript *script = [[NSAppleScript alloc] initWithSource:
89                 @"tell application \"EyeTV\"\n"
90                    "launch with server mode\n"
91                  "end tell"];
92     NSDictionary *errorDict;
93     NSAppleEventDescriptor *descriptor = [script executeAndReturnError:&errorDict];
94     if( nil == descriptor ) 
95     {
96         NSString *errorString = [errorDict objectForKey:NSAppleScriptErrorMessage];
97         NSLog( @"opening EyeTV failed with error status '%@'", errorString );
98     }
99     [script release];
100 }
101
102 - (int)currentChannel
103 {
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 ) 
110     {
111         NSString *errorString = [errorDict objectForKey:NSAppleScriptErrorMessage];
112         NSLog( @"EyeTV channel inventory failed with error status '%@'", errorString );
113     }
114     else
115     {
116         currentChannel = (int)[descriptor int32Value];
117     }
118     [script release];
119     return currentChannel;
120 }
121
122 - (int)switchChannelUp:(BOOL)b_yesOrNo
123 {
124     int currentChannel = 0;
125     NSAppleScript *script;
126     NSDictionary *errorDict;
127     NSAppleEventDescriptor *descriptor;
128
129     if( b_yesOrNo == YES )
130     {
131         script = [[NSAppleScript alloc] initWithSource:
132                     @"tell application \"EyeTV\"\n"
133                        "channel_up\n"
134                        "get current channel\n"
135                      "end tell"];
136         NSLog( @"telling eyetv to switch 1 channel up" );
137     }
138     else
139     {
140         script = [[NSAppleScript alloc] initWithSource:
141                     @"tell application \"EyeTV\"\n"
142                        "channel_down\n"
143                        "get current channel\n"
144                      "end tell"];
145         NSLog( @"telling eyetv to switch 1 channel down" );
146     }
147     
148     descriptor = [script executeAndReturnError:&errorDict];
149     if( nil == descriptor ) 
150     {
151         NSString *errorString = [errorDict objectForKey:NSAppleScriptErrorMessage];
152         NSLog( @"EyeTV channel change failed with error status '%@'", errorString );
153     }
154     else
155     {
156         currentChannel = (int)[descriptor int32Value];
157     }
158     [script release];
159     return currentChannel;
160 }
161
162 - (void)selectChannel: (int)theChannelNum
163 {
164     NSAppleScript *script;
165     switch( theChannelNum )
166     {
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"
172                          "end tell"];
173             break;
174         case -1: // S-Video
175             script = [[NSAppleScript alloc] initWithSource:
176                         @"tell application \"EyeTV\"\n"
177                          "  input_change input source S video input\n"
178                          "  show player_window\n"
179                          "end tell"];
180             break;
181         case 0: // Last
182             script = [[NSAppleScript alloc] initWithSource:
183                         @"tell application \"EyeTV\"\n"
184                          "  show player_window\n"
185                          "end tell"];
186             break;
187         default:
188             if( theChannelNum > 0 )
189             {
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];
196             }
197             else
198                 return;
199     }
200     NSDictionary *errorDict;
201     NSAppleEventDescriptor *descriptor = [script executeAndReturnError:&errorDict];
202     if( nil == descriptor ) 
203     {
204         NSString *errorString = [errorDict objectForKey:NSAppleScriptErrorMessage];
205         NSLog( @"EyeTV source change failed with error status '%@'", errorString );
206     }
207     [script release];
208 }
209
210 - (NSEnumerator *)allChannels
211 {
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 ) 
218     {
219         NSString *errorString = [errorDict objectForKey:NSAppleScriptErrorMessage];
220         NSLog( @"EyeTV channel inventory failed with error status '%@'", errorString );
221     }
222     else
223     {
224         int count = [descriptor numberOfItems];
225         int x=0; 
226         NSMutableArray *channelArray = [NSMutableArray arrayWithCapacity:count];
227         while( x++ < count ) {
228             [channelArray addObject:[[descriptor descriptorAtIndex:x] stringValue]];
229         }
230         channels = [channelArray objectEnumerator];
231     }
232     [script release];
233     return channels;
234 }
235
236 @end