2010-07-08 22:26:58 +00:00
|
|
|
//
|
|
|
|
// TermWindowController.m
|
|
|
|
// 2Term
|
|
|
|
//
|
|
|
|
// Created by Kelvin Sherlock on 7/2/2010.
|
|
|
|
// Copyright 2010 __MyCompanyName__. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "TermWindowController.h"
|
|
|
|
#import "EmulatorView.h"
|
2010-12-18 17:40:58 +00:00
|
|
|
#import "CurveView.h"
|
2010-10-05 19:05:42 +00:00
|
|
|
|
2010-12-18 17:40:58 +00:00
|
|
|
#import "VT52.h"
|
|
|
|
#import "PTSE.h"
|
2010-07-08 22:26:58 +00:00
|
|
|
|
2010-12-20 23:37:02 +00:00
|
|
|
#import "Defaults.h"
|
|
|
|
|
|
|
|
|
2010-07-08 22:26:58 +00:00
|
|
|
#define TTYDEFCHARS
|
|
|
|
|
|
|
|
#include <util.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/ttydefaults.h>
|
|
|
|
|
2010-10-05 18:56:45 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2010-07-08 22:26:58 +00:00
|
|
|
@implementation TermWindowController
|
|
|
|
|
2010-10-05 18:56:45 +00:00
|
|
|
@synthesize emulator = _emulator;
|
2010-12-18 17:40:58 +00:00
|
|
|
@synthesize emulatorView = _emulatorView;
|
|
|
|
@synthesize curveView = _curveView;
|
2010-10-05 18:56:45 +00:00
|
|
|
|
2010-12-20 23:37:02 +00:00
|
|
|
@synthesize parameters = _parameters;
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-07-08 22:26:58 +00:00
|
|
|
+(id)new
|
|
|
|
{
|
|
|
|
return [[self alloc] initWithWindowNibName: @"TermWindow"];
|
|
|
|
}
|
|
|
|
|
2010-10-05 18:56:45 +00:00
|
|
|
-(void)dealloc
|
|
|
|
{
|
|
|
|
[_emulator release];
|
2010-12-18 17:40:58 +00:00
|
|
|
[_emulatorView release];
|
|
|
|
[_curveView release];
|
|
|
|
|
2010-12-20 23:37:02 +00:00
|
|
|
[_parameters release];
|
2010-10-05 18:56:45 +00:00
|
|
|
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2010-07-08 22:26:58 +00:00
|
|
|
-(void)awakeFromNib
|
|
|
|
{
|
|
|
|
[self initPTY];
|
|
|
|
}
|
2010-10-05 18:56:45 +00:00
|
|
|
*/
|
2010-07-08 22:26:58 +00:00
|
|
|
|
|
|
|
-(void)initPTY
|
|
|
|
{
|
|
|
|
int pid;
|
|
|
|
int fd;
|
|
|
|
struct termios term;
|
2010-10-06 00:39:09 +00:00
|
|
|
struct winsize ws = [_emulator defaultSize];
|
2010-07-08 22:26:58 +00:00
|
|
|
//int flags;
|
|
|
|
|
|
|
|
memset(&term, 0, sizeof(term));
|
|
|
|
|
|
|
|
//term.c_oflag = OPOST | ONLCR;
|
|
|
|
//term.c_lflag = ECHO;
|
|
|
|
//term.c_iflag = ICRNL; // | ICANON | ECHOE | ISIG;
|
|
|
|
|
|
|
|
term.c_oflag = TTYDEF_OFLAG;
|
|
|
|
term.c_lflag = TTYDEF_LFLAG;
|
|
|
|
term.c_iflag = TTYDEF_IFLAG;
|
|
|
|
term.c_cflag = TTYDEF_CFLAG;
|
|
|
|
|
|
|
|
term.c_ispeed = term.c_ospeed = TTYDEF_SPEED;
|
|
|
|
|
|
|
|
memcpy(term.c_cc, ttydefchars, sizeof(ttydefchars));
|
|
|
|
|
2011-01-11 00:29:59 +00:00
|
|
|
if ([_emulator respondsToSelector: @selector(initTerm:)])
|
|
|
|
[_emulator initTerm: &term];
|
2010-07-08 22:26:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
pid = forkpty(&fd, NULL, &term, &ws);
|
|
|
|
|
|
|
|
if (pid < 0)
|
|
|
|
{
|
2010-10-06 00:39:09 +00:00
|
|
|
fprintf(stderr, "forkpty failed\n");
|
|
|
|
fflush(stderr);
|
|
|
|
|
2010-07-08 22:26:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (pid == 0)
|
|
|
|
{
|
2010-10-05 18:56:45 +00:00
|
|
|
|
|
|
|
std::vector<const char *> environ;
|
|
|
|
std::string s;
|
2010-10-06 00:39:09 +00:00
|
|
|
|
2010-10-05 18:56:45 +00:00
|
|
|
|
|
|
|
s.append("TERM_PROGRAM=2Term");
|
|
|
|
s.append(1, (char)0);
|
|
|
|
|
|
|
|
s.append("LANG=C");
|
|
|
|
s.append(1, (char)0);
|
|
|
|
|
|
|
|
s.append("TERM=");
|
|
|
|
s.append([_emulator termName]);
|
|
|
|
|
|
|
|
s.append(1, (char)0);
|
|
|
|
s.append(1, (char )0);
|
|
|
|
|
|
|
|
for (std::string::size_type index = 0;;)
|
|
|
|
{
|
|
|
|
environ.push_back(&s[index]);
|
|
|
|
|
|
|
|
index = s.find((char)0, index);
|
|
|
|
if (index == std::string::npos) break;
|
|
|
|
|
|
|
|
if (s[++index] == 0) break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
environ.push_back(NULL);
|
|
|
|
|
|
|
|
|
2010-07-08 22:26:58 +00:00
|
|
|
// call login -f [username]
|
2010-10-05 18:56:45 +00:00
|
|
|
// -p -- do NOT ignore environment.
|
2010-07-08 22:26:58 +00:00
|
|
|
// export TERM=...
|
|
|
|
|
|
|
|
|
2010-10-05 18:56:45 +00:00
|
|
|
// TODO -- option for localhost, telnet, ssh, etc.
|
|
|
|
execle("/usr/bin/login", "login", "-pf", getlogin(), NULL, &environ[0]);
|
2010-10-06 00:39:09 +00:00
|
|
|
|
2010-07-08 22:26:58 +00:00
|
|
|
fprintf(stderr, "execle failed\n");
|
|
|
|
fflush(stderr);
|
|
|
|
|
2010-12-21 05:17:15 +00:00
|
|
|
// should not call exit.
|
|
|
|
_exit(-1);
|
2010-07-08 22:26:58 +00:00
|
|
|
// child
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
if (fcntl(fd, F_GETFL, &flags) < 0) flags = 0;
|
|
|
|
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
|
|
|
|
*/
|
2010-12-20 23:37:02 +00:00
|
|
|
|
2011-01-11 04:35:45 +00:00
|
|
|
[_emulatorView resizeTo: iSize(ws.ws_col, ws.ws_row) animated: NO];
|
2010-07-08 22:26:58 +00:00
|
|
|
|
2011-01-17 02:34:48 +00:00
|
|
|
|
2010-10-06 00:39:09 +00:00
|
|
|
if (![_emulator resizable])
|
|
|
|
{
|
|
|
|
|
|
|
|
NSWindow *window = [self window];
|
|
|
|
NSUInteger mask = [window styleMask];
|
|
|
|
|
|
|
|
|
|
|
|
[window setShowsResizeIndicator: NO];
|
|
|
|
|
|
|
|
[window setStyleMask: mask & ~NSResizableWindowMask];
|
|
|
|
}
|
|
|
|
|
2010-07-08 22:26:58 +00:00
|
|
|
_child = pid;
|
|
|
|
|
|
|
|
[_emulatorView setFd: fd];
|
|
|
|
[_emulatorView startBackgroundReader];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-05 18:56:45 +00:00
|
|
|
#pragma mark -
|
|
|
|
#pragma mark NSWindowDelegate
|
|
|
|
|
|
|
|
- (void)windowDidLoad
|
|
|
|
{
|
2010-12-20 23:37:02 +00:00
|
|
|
|
|
|
|
BOOL scanLines;
|
|
|
|
NSColor *foregroundColor;
|
|
|
|
NSColor *backgroundColor;
|
|
|
|
Class klass;
|
|
|
|
id o;
|
|
|
|
|
2011-01-17 02:34:48 +00:00
|
|
|
NSWindow *window;
|
|
|
|
|
2010-10-05 18:56:45 +00:00
|
|
|
[super windowDidLoad];
|
|
|
|
|
2011-01-17 02:34:48 +00:00
|
|
|
window = [self window];
|
|
|
|
|
|
|
|
// resize in 2.0 he ight increments to prevent jittering the scan lines.
|
|
|
|
[window setResizeIncrements: NSMakeSize(1.0, 2.0)];
|
|
|
|
|
|
|
|
|
2010-12-20 23:37:02 +00:00
|
|
|
klass = [_parameters objectForKey: kClass];
|
|
|
|
if (!klass || ![klass conformsToProtocol: @protocol(Emulator)])
|
2010-12-18 17:40:58 +00:00
|
|
|
{
|
2010-12-20 23:37:02 +00:00
|
|
|
klass = [VT52 class];
|
2010-12-18 17:40:58 +00:00
|
|
|
}
|
2010-10-05 18:56:45 +00:00
|
|
|
|
2010-12-20 23:37:02 +00:00
|
|
|
o = [_parameters objectForKey: kScanLines];
|
|
|
|
scanLines = o ? [(NSNumber *)o boolValue] : YES;
|
|
|
|
|
|
|
|
o = [_parameters objectForKey: kForegroundColor];
|
|
|
|
foregroundColor = o ? (NSColor *)o : [NSColor greenColor];
|
|
|
|
|
|
|
|
o = [_parameters objectForKey: kBackgroundColor];
|
|
|
|
backgroundColor = o ? (NSColor *)o : [NSColor blackColor];
|
|
|
|
|
|
|
|
|
|
|
|
[self willChangeValueForKey: @"emulator"];
|
|
|
|
_emulator = [klass new];
|
|
|
|
[self didChangeValueForKey: @"emulator"];
|
|
|
|
|
|
|
|
|
2010-10-05 18:56:45 +00:00
|
|
|
[_emulatorView setEmulator: _emulator];
|
2010-12-20 23:37:02 +00:00
|
|
|
[_emulatorView setForegroundColor: foregroundColor];
|
|
|
|
[_emulatorView setBackgroundColor: backgroundColor];
|
|
|
|
[_emulatorView setScanLines: scanLines];
|
|
|
|
|
2010-10-05 18:56:45 +00:00
|
|
|
|
2010-12-20 23:37:02 +00:00
|
|
|
//[_curveView initScanLines];
|
|
|
|
//[_curveView setColor: [NSColor blueColor]];
|
2010-12-18 17:40:58 +00:00
|
|
|
|
2010-10-05 18:56:45 +00:00
|
|
|
[self initPTY];
|
2011-01-17 02:34:48 +00:00
|
|
|
|
|
|
|
[window setMinSize: [window frame].size];
|
2010-10-05 18:56:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
-(void)windowWillClose:(NSNotification *)notification
|
|
|
|
{
|
|
|
|
[self autorelease];
|
|
|
|
}
|
|
|
|
|
2010-07-08 22:26:58 +00:00
|
|
|
@end
|