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