]> git.sesse.net Git - vlc/blob - modules/gui/macosx/eyetv.m
macosx: removed eyetv debug, which just floods the console
[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     /* 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;
65
66     /* is eyetv running? */
67     if( [[theNotification name] isEqualToString: @"PluginInit"] )
68         b_eyeTVactive = YES;
69     if( [[theNotification name] isEqualToString: @"PluginQuit"] )
70         b_eyeTVactive = NO;
71 }
72
73 - (BOOL)isEyeTVrunning
74 {
75     return b_eyeTVactive;
76 }
77
78 - (BOOL)isDeviceConnected
79 {
80     return b_deviceConnected;
81 }
82
83
84 - (void)launchEyeTV
85 {
86     NSAppleScript *script = [[NSAppleScript alloc] initWithSource:
87                 @"tell application \"EyeTV\"\n"
88                    "launch with server mode\n"
89                  "end tell"];
90     NSDictionary *errorDict;
91     NSAppleEventDescriptor *descriptor = [script executeAndReturnError:&errorDict];
92     if( nil == descriptor ) 
93     {
94         NSString *errorString = [errorDict objectForKey:NSAppleScriptErrorMessage];
95         NSLog( @"opening EyeTV failed with error status '%@'", errorString );
96     }
97     [script release];
98 }
99
100 - (int)currentChannel
101 {
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 ) 
108     {
109         NSString *errorString = [errorDict objectForKey:NSAppleScriptErrorMessage];
110         NSLog( @"EyeTV channel inventory failed with error status '%@'", errorString );
111     }
112     else
113     {
114         currentChannel = (int)[descriptor int32Value];
115     }
116     [script release];
117     return currentChannel;
118 }
119
120 - (int)switchChannelUp:(BOOL)b_yesOrNo
121 {
122     int currentChannel = 0;
123     NSAppleScript *script;
124     NSDictionary *errorDict;
125     NSAppleEventDescriptor *descriptor;
126
127     if( b_yesOrNo == YES )
128     {
129         script = [[NSAppleScript alloc] initWithSource:
130                     @"tell application \"EyeTV\"\n"
131                        "channel_up\n"
132                        "get current channel\n"
133                      "end tell"];
134         NSLog( @"telling eyetv to switch 1 channel up" );
135     }
136     else
137     {
138         script = [[NSAppleScript alloc] initWithSource:
139                     @"tell application \"EyeTV\"\n"
140                        "channel_down\n"
141                        "get current channel\n"
142                      "end tell"];
143         NSLog( @"telling eyetv to switch 1 channel down" );
144     }
145     
146     descriptor = [script executeAndReturnError:&errorDict];
147     if( nil == descriptor ) 
148     {
149         NSString *errorString = [errorDict objectForKey:NSAppleScriptErrorMessage];
150         NSLog( @"EyeTV channel change failed with error status '%@'", errorString );
151     }
152     else
153     {
154         currentChannel = (int)[descriptor int32Value];
155     }
156     [script release];
157     return currentChannel;
158 }
159
160 - (void)selectChannel: (int)theChannelNum
161 {
162     NSAppleScript *script;
163     switch( theChannelNum )
164     {
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"
170                          "end tell"];
171             break;
172         case -1: // S-Video
173             script = [[NSAppleScript alloc] initWithSource:
174                         @"tell application \"EyeTV\"\n"
175                          "  input_change input source S video input\n"
176                          "  show player_window\n"
177                          "end tell"];
178             break;
179         case 0: // Last
180             script = [[NSAppleScript alloc] initWithSource:
181                         @"tell application \"EyeTV\"\n"
182                          "  show player_window\n"
183                          "end tell"];
184             break;
185         default:
186             if( theChannelNum > 0 )
187             {
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];
194             }
195             else
196                 return;
197     }
198     NSDictionary *errorDict;
199     NSAppleEventDescriptor *descriptor = [script executeAndReturnError:&errorDict];
200     if( nil == descriptor ) 
201     {
202         NSString *errorString = [errorDict objectForKey:NSAppleScriptErrorMessage];
203         NSLog( @"EyeTV source change failed with error status '%@'", errorString );
204     }
205     [script release];
206 }
207
208 - (NSEnumerator *)allChannels
209 {
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 ) 
216     {
217         NSString *errorString = [errorDict objectForKey:NSAppleScriptErrorMessage];
218         NSLog( @"EyeTV channel inventory failed with error status '%@'", errorString );
219     }
220     else
221     {
222         int count = [descriptor numberOfItems];
223         int x=0; 
224         NSMutableArray *channelArray = [NSMutableArray arrayWithCapacity:count];
225         while( x++ < count ) {
226             [channelArray addObject:[[descriptor descriptorAtIndex:x] stringValue]];
227         }
228         channels = [channelArray objectEnumerator];
229     }
230     [script release];
231     return channels;
232 }
233
234 @end