Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/ui/ui-drv/cocoa/VideatorProxy.m
blob: 506ac48816083ac5278cde75fc7d5c28c3057afb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
 *     XaoS, a fast portable realtime fractal zoomer 
 *                  Copyright (C) 1996 by
 *
 *      Jan Hubicka          (hubicka@paru.cas.cz)
 *      Thomas Marsh         (tmarsh@austin.ibm.com)
 *
 *    Cocoa Driver by J.B. Langston III (jb-langston@austin.rr.com)
 *    Videator Support by Andrew Stone (Stone Design)
 *    For information about Videator, visit http://www.stone.com/Videator
 *
 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
#include <aconfig.h>
#ifdef VIDEATOR_SUPPORT
#import "VideatorProxy.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <libc.h>

NSString *cheapHostName() {
    NSString *host = @"localhost";
    char s[_POSIX_HOST_NAME_MAX+1];
    s[0] = '\0';
    if (gethostname(s, _POSIX_HOST_NAME_MAX) == 0) {
        if (strlen(s) > 0)
            host = [[[NSString alloc] initWithUTF8String:s]autorelease];
    }
    return host;
}

#define VideatorServer	([NSString stringWithFormat:@"VideatorServer-%@",cheapHostName()])
#define FRAME_REFRESH_THRESHOLD  0.04

@protocol VideatorVendedProtocol
// to notify in main thread
- (void)runUpdateAlert:(NSString *)latestVersionNumber;
// XaoS
- (BOOL)wantsXaoSImage;
- (void)setXaosImageData:(NSData *)bmData;
// automator mode to check its not hung
- (BOOL)heartBeat;
@end


@implementation VideatorProxy

+ (void)setupDefaults
{
    NSString *userDefaultsValuesPath;
    NSDictionary *userDefaultsValuesDict;
    NSDictionary *initialValuesDict;
    NSArray *resettableUserDefaultsKeys;
    
    userDefaultsValuesPath=[[NSBundle mainBundle] pathForResource:@"UserDefaults" 
                                                           ofType:@"plist"];
    userDefaultsValuesDict=[NSDictionary dictionaryWithContentsOfFile:userDefaultsValuesPath];
    
    [[NSUserDefaults standardUserDefaults] registerDefaults:userDefaultsValuesDict];
    
    resettableUserDefaultsKeys=[NSArray arrayWithObjects:@"EnableVideator",nil];
    initialValuesDict=[userDefaultsValuesDict dictionaryWithValuesForKeys:resettableUserDefaultsKeys];
    
    [[NSUserDefaultsController sharedUserDefaultsController] setInitialValues:initialValuesDict];
}

+ (void)initialize {
    [self setupDefaults];
}

- (id)init {
    self = [super init];
    if (self) {
        _videatorEnabled = [[NSUserDefaults standardUserDefaults] boolForKey:@"EnableVideator"];
    }
    return self;
}

- (void)toggleVideator:(id)sender {
    _videatorEnabled ^= 1;
    [[NSUserDefaults standardUserDefaults] setBool:_videatorEnabled forKey:@"EnableVideator"];
}

- (BOOL)videatorEnabled {
    return _videatorEnabled;
}

- (void)connectionDidDie:(NSNotification *)n {
    _videatorProxy = nil;
    _killDate = [[NSCalendarDate date] retain];
    NSLog(@"Videator is dead... ...Long Live Videator!");
}

- (void)getProxy {
    
    // do not try and reconnect to an application that is terminating:
    if (_killDate && [_killDate timeIntervalSinceNow] > -10.0) return;
    else _killDate = nil;
    
    _videatorProxy = [[NSConnection rootProxyForConnectionWithRegisteredName:VideatorServer host:nil] retain];
    // if we can't find it, no big deal:
    if (_videatorProxy != nil) {
        [_videatorProxy setProtocolForProxy:@protocol(VideatorVendedProtocol)];
        [[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(connectionDidDie:) name:@"VideatorWillTerminate" object:nil];
    }
}

- (void)sendImageRep:(NSBitmapImageRep *)imageRep {
    // simply return if user does not want this
    if (!_videatorEnabled) return;
    
    // Andrew's Videator hook - costs almost nothing since the view maintains the bitmapImageRep in hand -
    // We call it here because other mechanisms might cause a redraw in the view but we don't want that overhead
    // unless we've been notified that there really was a change:
    // HOWEVER we can only shove so much stuff down the pipe - let's try 30 frames per second threshold
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSDate *now = [NSDate date];
    
    
    if (_wantsThrottle) {
        static float _FRAME_REFRESH_THRESHOLD = 0.0;
        if (_FRAME_REFRESH_THRESHOLD == 0.0) {
            _FRAME_REFRESH_THRESHOLD = [[NSUserDefaults standardUserDefaults] floatForKey:@"RefreshThreshold"];
            if (_FRAME_REFRESH_THRESHOLD == 0.0) _FRAME_REFRESH_THRESHOLD = FRAME_REFRESH_THRESHOLD;
        }
        if (_lastFrameCreatedDate && [now timeIntervalSinceDate:_lastFrameCreatedDate] < _FRAME_REFRESH_THRESHOLD) return;
        [_lastFrameCreatedDate release];
        _lastFrameCreatedDate = [now retain];
    }
    
    if (!_videatorProxy) [self getProxy];
    NS_DURING
    if (_videatorProxy!=nil /* DO NOT WAIT FOR THE ROUNDTRIP && [_videatorProxy wantsXaoSImage] */)
        [_videatorProxy setXaosImageData:[imageRep TIFFRepresentationUsingCompression:NSTIFFCompressionLZW factor:0]];
    NS_HANDLER
    
    NS_ENDHANDLER
    
    [pool release];
}

@end
#endif