5 // Created by Byron Ellis on 7/3/07.
6 // Copyright 2007 __MyCompanyName__. All rights reserved.
9 #import "TerminalDelegate.h"
10 #import "RInterpreter.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)
30 @implementation TerminalDelegate
32 if(nil == [super init]) return nil;
44 - (void)setReaderFunction:(R_READER_FN)fn {
47 - (void)setWriterFunction:(R_WRITER_FN)fn {
50 - (void)setFlushFunction:(R_FLUSH_FN)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];
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;
80 R_PolledEvents = Terminal_ProcessEvents;
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 {