eyetv.m 7.69 KB
Newer Older
1 2 3
/*****************************************************************************
* eyetv.m: small class to control the notification parts of the EyeTV plugin
*****************************************************************************
Felix Paul Kühne's avatar
Felix Paul Kühne committed
4
* Copyright (C) 2006-2011 VLC authors and VideoLAN
5 6 7
* $Id$
*
* Authors: Felix Kühne <fkuehne at videolan dot org>
8
*          Damien Fouilleul <damienf at videolan dot org>
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/

25
#import "eyetv.h"
26 27
/* for apple event interaction [carbon] */
//#import <Foundation/NSAppleScript>
28
/* for various VLC core related calls */
29 30 31 32 33 34 35 36 37 38 39
#import "intf.h"

@implementation VLCEyeTVController

static VLCEyeTVController *_o_sharedInstance = nil;

+ (VLCEyeTVController *)sharedInstance
{
    return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
}

40
- (id)init
41 42 43 44 45 46
{
    if (_o_sharedInstance) {
        [self dealloc];
    } else {
        _o_sharedInstance = [super init];

47
        [[NSDistributedNotificationCenter defaultCenter]
48 49 50 51 52 53
                    addObserver: self
                       selector: @selector(globalNotificationReceived:)
                           name: NULL
                         object: @"VLCEyeTVSupport"
             suspensionBehavior: NSNotificationSuspensionBehaviorDeliverImmediately];
    }
54

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    return _o_sharedInstance;
}

- (void)globalNotificationReceived: (NSNotification *)theNotification
{
    /* update our info on the used device */
    if( [[theNotification name] isEqualToString: @"DeviceAdded"] )
        b_deviceConnected = YES;
    if( [[theNotification name] isEqualToString: @"DeviceRemoved"] )
        b_deviceConnected = NO;

    /* is eyetv running? */
    if( [[theNotification name] isEqualToString: @"PluginInit"] )
        b_eyeTVactive = YES;
    if( [[theNotification name] isEqualToString: @"PluginQuit"] )
        b_eyeTVactive = NO;
}

- (BOOL)isEyeTVrunning
{
    return b_eyeTVactive;
}

- (BOOL)isDeviceConnected
{
    return b_deviceConnected;
}


- (void)launchEyeTV
{
    NSAppleScript *script = [[NSAppleScript alloc] initWithSource:
87 88 89
                @"tell application \"EyeTV\"\n"
                   "launch with server mode\n"
                 "end tell"];
90 91
    NSDictionary *errorDict;
    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 98 99
    }
    [script release];
}

100
- (int)currentChannel
101
{
102 103 104 105 106
    int currentChannel = 0;
    NSAppleScript *script = [[NSAppleScript alloc] initWithSource:
            @"tell application \"EyeTV\" to get current channel"];
    NSDictionary *errorDict;
    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 113 114 115 116 117 118 119 120 121 122
    }
    else
    {
        currentChannel = (int)[descriptor int32Value];
    }
    [script release];
    return currentChannel;
}

- (int)switchChannelUp:(BOOL)b_yesOrNo
{
    int currentChannel = 0;
123 124 125 126 127 128 129
    NSAppleScript *script;
    NSDictionary *errorDict;
    NSAppleEventDescriptor *descriptor;

    if( b_yesOrNo == YES )
    {
        script = [[NSAppleScript alloc] initWithSource:
130 131 132 133
                    @"tell application \"EyeTV\"\n"
                       "channel_up\n"
                       "get current channel\n"
                     "end tell"];
134
        NSLog( @"telling eyetv to switch 1 channel up" );
135 136 137
    }
    else
    {
138 139 140 141 142
        script = [[NSAppleScript alloc] initWithSource:
                    @"tell application \"EyeTV\"\n"
                       "channel_down\n"
                       "get current channel\n"
                     "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 153 154
    }
    else
    {
        currentChannel = (int)[descriptor int32Value];
155 156
    }
    [script release];
157
    return currentChannel;
158 159 160 161 162 163 164 165 166
}

- (void)selectChannel: (int)theChannelNum
{
    NSAppleScript *script;
    switch( theChannelNum )
    {
        case -2: // Composite
            script = [[NSAppleScript alloc] initWithSource:
167
                        @"tell application \"EyeTV\"\n"
168 169
                         "  input_change input source composite video input\n"
                         "  show player_window\n"
170
                         "end tell"];
171 172 173
            break;
        case -1: // S-Video
            script = [[NSAppleScript alloc] initWithSource:
174
                        @"tell application \"EyeTV\"\n"
175 176
                         "  input_change input source S video input\n"
                         "  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 185 186 187 188
            break;
        default:
            if( theChannelNum > 0 )
            {
                NSString *channel_change = [NSString stringWithFormat:
189
                    @"tell application \"EyeTV\"\n"
190 191
                     "  channel_change channel number %d\n"
                     "  show player_window\n"
192
                     "end tell", theChannelNum];
193 194 195 196 197 198 199
                script = [[NSAppleScript alloc] initWithSource:channel_change];
            }
            else
                return;
    }
    NSDictionary *errorDict;
    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 206 207
    }
    [script release];
}

208
- (NSEnumerator *)allChannels
209 210 211 212 213 214
{
    NSEnumerator *channels = nil;
    NSAppleScript *script = [[NSAppleScript alloc] initWithSource:
            @"tell application \"EyeTV\" to get name of every channel"];
    NSDictionary *errorDict;
    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 221 222
    }
    else
    {
        int count = [descriptor numberOfItems];
223
        int x=0;
224 225 226 227 228 229 230 231 232 233 234
        NSMutableArray *channelArray = [NSMutableArray arrayWithCapacity:count];
        while( x++ < count ) {
            [channelArray addObject:[[descriptor descriptorAtIndex:x] stringValue]];
        }
        channels = [channelArray objectEnumerator];
    }
    [script release];
    return channels;
}

@end