Many changes! Snapshots now working properly. Programmatic environment interface...
[RExecServer.git] / TerminalDelegate.m
blobd43a03400d4f4e5c139332f45eee44ade77771bd
1 //
2 //  TerminalDelegate.m
3 //  RExecServer
4 //
5 //  Created by Byron Ellis on 7/3/07.
6 //  Copyright 2007 __MyCompanyName__. All rights reserved.
7 //
9 #import "TerminalDelegate.h"
10 #import "RInterpreter.h"
11 #import "RDevice.h"
12 #import "DeviceWindowController.h"
13 #include <R_ext/eventloop.h>
15 void Terminal_ProcessEvents(void) {
16         NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
17         [NSApp postEvent:[NSEvent otherEventWithType:NSApplicationDefined 
18                                                                         location:NSMakePoint(0,0)
19                                                                          modifierFlags:0
20                                                                          timestamp:0 
21                                                                          windowNumber:0 
22                                                                          context:nil 
23                                                                          subtype:1337 
24                                                                          data1:0 
25                                                                          data2:0] atStart:NO];
26         [NSApp run];
27         [pool release];
30 @implementation TerminalDelegate
31 - (id)init {
32         if(nil == [super init]) return nil;
33         
34         readerFn = NULL;
35         writerFn = NULL;
36         flushFn  = NULL;
37         
38         readerCurMax = 0;
39         readerBuffer = NULL;
40         
41         return self;
44 - (void)setReaderFunction:(R_READER_FN)fn {
45         readerFn = fn;
47 - (void)setWriterFunction:(R_WRITER_FN)fn {
48         writerFn = fn;
50 - (void)setFlushFunction:(R_FLUSH_FN)fn {
51         flushFn = fn;
54 - (void)didOpenDevice:(id)aDevice forInterpreter:(id)anInterpreter {
55         if(YES == [aDevice canDrawInView]) {
56                 DeviceWindowController *devCtrl = [[DeviceWindowController alloc] initWithWindowNibName:@"Device"];
57                 [devCtrl setDevice:aDevice];
58                 [[devCtrl window] makeKeyAndOrderFront:self];
59         }
62 - (void)didCloseDevice:(id)aDevice forInterpreter:(id)anInterpreter { }
65 - (BOOL)shouldBufferOutputForInterpreter:(id)anInterpreter { return NO; }
66 - (void)appendString:(NSString*)outputString ofType:(int)aType forInterpreter:(id)anInterpreter {
67         writerFn((char*)[outputString UTF8String],[outputString length]);
70 - (void)didFinishEvaluationForInterpreter:(id)anInterpreter {
72 - (void)didBeginEvaluationForInterpreter:(id)anInterpreter {
74 - (void)didBeginWaitingForInputWithMaximumLength:(int)bufferLength addToHistory:(BOOL)shouldAdd forInterpreter:(id)anInterpreter {
75         if(readerCurMax < bufferLength) {
76                 if(NULL != readerBuffer) free(readerBuffer);
77                 readerBuffer = calloc(sizeof(unsigned char),bufferLength);
78                 readerCurMax = bufferLength;
79         }
80         R_PolledEvents = Terminal_ProcessEvents;
81         R_wait_usec    = 10000;
82         memset(readerBuffer,0,sizeof(unsigned char)*readerCurMax);
83         readerUsed     = readerFn("",readerBuffer,bufferLength,shouldAdd == YES ? 1 : 0);
84         [anInterpreter evaluateInput:[NSString stringWithUTF8String:(char*)readerBuffer]];
86 - (void)didGetInputForInterpreter:(id)anInterpreter {
88 - (void)flushOutputForInterpreter:(id)anInterpreter {
90 - (void)didCloseInterpreter:(id)anInterpreter {
93 @end