/*
 *    Filename:	ScreenDim.m 
 *    Created :	Tue Sep 24 22:20:57 1991 
 *    Author  :	Vince DeMarco
 *		<vince@whatnxt.cuc.ab.ca>
 */


/* Generated by Interface Builder */

#import <stdio.h>
#import <stdlib.h>
#import <libc.h>
#import <cthreads.h>

#import "ScreenDim.h"
#import "EyeView.h"
#import "Passwd.h"
#import "nppow.h"
#import "spotlight.h"
#import "black.h"

#import <appkit/Application.h>
#import <appkit/Control.h>
#import <appkit/Menu.h>
#import <appkit/defaults.h>

#import <nextdev/evsio.h>

static void idleTimerProc(DPSTimedEntry teNumber, double now, char *userData)
{
    [(id) userData idle];
    return;
}

@implementation ScreenDim

- init
{
    self = [super init];
    dimtime  = atoi(NXGetDefaultValue([NXApp appName],"DimTime"));
    savetype = atoi(NXGetDefaultValue([NXApp appName],"DimType"));
    passwdlck= atoi(NXGetDefaultValue([NXApp appName],"PasswdLock"));

    passwd = [[Passwd alloc] init];

    evsfd = open ( "/dev/evs0", O_RDONLY );
    timedEntry = DPSAddTimedEntry(60.0, (DPSTimedEntryProc)idleTimerProc,(void *)self,
				   NX_MODALRESPTHRESHOLD);
    PS_spotlight_init();	
    return(self);
}

- free
{
    close(evsfd);
    DPSRemoveTimedEntry(timedEntry);
    [passwd free];
    return [super free];
}

- setEyes:anObject
{
    eyes = anObject;
    return self;
}

- setDimTime:(int)dimTime
{
    dimtime = dimTime;
    idleTime = 0;
    return self;
}

- (int)dimTime
{
    return dimtime;
}

- setSaveType:(int)value
{
    savetype = value;
    return self;
}

- (int)saveType
{
    return savetype;
}

- setpasswdOn:(int)value
{
    passwdlck = value;
    return self;
}

- (int)passwd
{
    return passwdlck;
}

- dimScreen:sender
{
    if (isLocked == YES){
	return self;
    }
    idleTime = 0;
    isLocked = YES;
    [eyes stopTimer];
    DPSRemoveTimedEntry(timedEntry);
    close(evsfd);
    idleTime = 0 ;

    [self disableMainMenu];

    if (savetype == BLACKOUT ){
	PS_black();
    }else{
	PS_spotlight();
    }

    if (passwdlck){
	[passwd requestPasswd:sender];
    }

    evsfd = open("/dev/evs0", O_RDONLY );
    [eyes startTimer];
    timedEntry = DPSAddTimedEntry(60.0,(DPSTimedEntryProc)idleTimerProc,(void *)self,
				   NX_MODALRESPTHRESHOLD ) ;
    isLocked = NO;

    if (!passwdlck){
	[self enableMainMenu];
    }

    return self;
}

- idle
{
    int     currentDimTime;
    
    if (isLocked == YES){
	return self;
    }
    
    ioctl(evsfd, EVSIOGADT, &currentDimTime ); /* Get current autodim time */
    if ( currentDimTime == lastDimTime ){       /* If this value changes, you know that a
						 * user event has been generated since the last time
						 * EVSIOGADT was called
						 */
	idleTime++;
    }else{
	idleTime = 0;
    }
    if ( idleTime >= dimtime){
	isLocked = YES;
	[eyes stopTimer];
	DPSRemoveTimedEntry(timedEntry);
	close(evsfd);
	idleTime = 0;

	[NXApp unhide:self];
	[self disableMainMenu];

	if (passwdlck){                 /* It would be more secure not to do this */
	    [passwd orderWindowOut];    /* But this is being done for looks only  */
	    [NXApp unhide:self];        /* This is just incase the user decides to try to 
					 * Switch to another application while the cross
					 * over is occuring
					 */
	}


	if (savetype == BLACKOUT ){
	    PS_black();
	}else{
	    PS_spotlight();
	}

	if (passwdlck){
	    [passwd requestPasswd:self];
	}

	evsfd = open ("/dev/evs0", O_RDONLY );
	[eyes startTimer];
	timedEntry = DPSAddTimedEntry(60.0,(DPSTimedEntryProc)idleTimerProc,(void *)self,
				       NX_MODALRESPTHRESHOLD ) ;

	isLocked = NO;

	if (!passwdlck){
	    [self enableMainMenu];
	    [NXApp hide:self];
	}
    }
    lastDimTime = currentDimTime;
    return self;
}

- disableMainMenu
{
    id matrix = [[NXApp mainMenu] itemList];
    [matrix setEnabled:NO];
    return self;
}

- enableMainMenu
{
    id matrix = [[NXApp mainMenu] itemList];
    [matrix setEnabled:YES];
    return self;
}

- printerOff:sender
{
    cthread_detach(cthread_fork(nppoweroff,(any_t)0));
    return self;
}

@end
