
/* Generated by Interface Builder */

#import "NeXTICS.h"
#import "spawn.h"
#import "ics_filter.h"
#import <appkit/appkit.h>
#import <sys/types.h>
#import <sys/time.h>

@implementation NeXTICS

void ioActivity(int fd, id self)
{
  struct timeval timeout;
  
  timeout.tv_sec = 1;
  timeout.tv_usec = 0;
  RunSelect( &timeout );
  if (childPID() <= 0) {
     fprintf( stderr,  "CHILD DIED!\n" );
     [self terminate:self];
  }
}

+ initialize
{
    static NXDefaultsVector NeXTICSDefaults = {
        {"ECHO", "1"},
        {NULL}
    };

    NXRegisterDefaults("NeXTICS", NeXTICSDefaults);
    return self;
}

+ new
{
  self = [super new];
  Spawn( NXArgv[1], &(NXArgv[1]), filterChars );
  DPSAddFD(fileno(stdin), (DPSFDProc)ioActivity, self, NX_MODALRESPTHRESHOLD);
  DPSAddFD(childsOutput(),(DPSFDProc)ioActivity, self, NX_MODALRESPTHRESHOLD);
  echo = (NXGetDefaultValue("NeXTICS","ECHO")[0] == '1');
  if (echo) {
    turnOnEcho();
  } else {
    turnOffEcho();
  }
  return self;
}

- appWillTerminate:sender
{
  cleanUp();
  return self;
}

- toggleEcho:sender
{
  if (echo) {
    fprintf( stderr, "ECHO OFF\n" );
    turnOffEcho();
  } else {
    fprintf( stderr, "ECHO ON\n" );
    turnOnEcho();
  }
  echo = !echo;
  if (echo)
    NXWriteDefault("NeXTICS", "ECHO", "1");
  else
    NXWriteDefault("NeXTICS", "ECHO", "0");
  return self;
}

@end
