//
// Copyright 1993 (c) Todd Anthony Nathan.  All Rights Reserved.
//

#import "ESD.h"
#import "ESDConstants.h"
#import <appkit/Application.h>

#import <drivers/event_status_driver.h>


/*
 * Define the list of commands and functions this XModule implements
 */

const char *handlerList[] = {

	// Keyboard Functions
	"setkeyrepeatinterval",
	"KEYREPEATINTERVAL",
	"setkeyrepeatthreshold",
	"KEYREPEATTHRESHOLD",
	"setkeymapping",
	"KEYMAPPINGLENGTH",
	"GETKEYMAPPING",
	"resetkeyboard",

	// Mouse Functions
	"setclicktime",
	"CLICKTIME",
	"setclickspace",
	"GETCLICKSPACE",
	"setmousescaling",
	"GETMOUSESCALING",
	"enablemousebutton",
	"MOUSEBUTTONENABLED",
	"resetmouse",

	// Screen Functions
	"setautodimthreshold",
	"AUTODIMTHRESHOLD",
	"AUTODIMTIME",
	"setautodimstate",
	"AUTODIMSTATE",
	"setscreenbrightness",
	"SCREENBRIGHTNESS",
	"setautodimbrightness",
	"AUTODIMBRIGHTNESS",

	// Volume Functions
	"setcurrentvolume",
	"CURRENTVOLUME",

	// Wait Cursor Functions
	"setwaitcursorthreshold",
	"WAITCURSORTHRESHOLD",
	"setwaitcursorsustain",
	"WAITCURSORSUSTAIN",
	"setwaitcursorframeinterval",
	"WAITCURSORFRAMEINTERVAL",

	"ESD_VERSION",

	NULL
};



/*
 * Keep track of the mach header where we get loaded
 * so we can use it to load other stuff later (such as
 * nib files)
 * Note: only needed in XModules that load nib files, etc.
 */
 
static struct mach_header	*loadHeader = NULL;
static NXEventHandle		ourEventStatusHandle;



@implementation EventStatusDriver : XModule



// ---------------------------------------------------------------
//   Creating and Initializing the XModule instance
// ---------------------------------------------------------------


- init
{
	[super init];
	
	// Do your stuff here...
	ourEventStatusHandle = NXOpenEventStatus();	

	return ( self );
}


- free
{
	// Do your stuff here...

	return ( [super free] );
}



// ---------------------------------------------------------------
//   Standard XModule methods
// ---------------------------------------------------------------

+ (const char *)moduleName
{
	return ( "EventStatusDriver" );
}


+ (const char **)xCmdsAndFcns
{
	return ( handlerList );
}


- prepareToExecuteHandlers
{
	// [self loadInterfaceData];
	return ( self );
}


- executeHandler:(NXAtom)handlerName
{
	// Keyboard Functions

	if (!strcmp(handlerName, "setkeyrepeatinterval")) {
		return ( [ self ESD_setkeyrepeatinterval ] );
	} else if (!strcmp(handlerName, "KEYREPEATINTERVAL")) {
		return ( [ self ESD_KEYREPEATINTERVAL ] );
	} else if (!strcmp(handlerName, "setkeyrepeatthreshold")) {
		return ( [ self ESD_setkeyrepeatthreshold ] );
	} else if (!strcmp(handlerName, "KEYREPEATTHRESHOLD")) {
		return ( [ self ESD_KEYREPEATTHRESHOLD ] );
	} else if (!strcmp(handlerName, "setkeymapping")) {
		return ( [ self ESD_setkeymapping ] );
	} else if (!strcmp(handlerName, "KEYMAPPINGLENGTH")) {
		return ( [ self ESD_KEYMAPPINGLENGTH ] );
	} else if (!strcmp(handlerName, "GETKEYMAPPING")) {
		return ( [ self ESD_GETKEYMAPPING ] );
	} else if (!strcmp(handlerName, "resetkeyboard")) {
		return ( [ self ESD_resetkeyboard ] );

	// Mouse Functions

	} else if (!strcmp(handlerName, "setclicktime")) {
		return ( [ self ESD_setclicktime ] );
	} else if (!strcmp(handlerName, "CLICKTIME")) {
		return ( [ self ESD_CLICKTIME ] );
	} else if (!strcmp(handlerName, "setclickspace")) {
		return ( [ self ESD_setclickspace ] );
	} else if (!strcmp(handlerName, "GETCLICKSPACE")) {
		return ( [ self ESD_GETCLICKSPACE ] );
	} else if (!strcmp(handlerName, "setmousescaling")) {
		return ( [ self ESD_setmousescaling ] );
	} else if (!strcmp(handlerName, "GETMOUSESCALING")) {
		return ( [ self ESD_GETMOUSESCALING ] );
	} else if (!strcmp(handlerName, "enablemousebutton")) {
		return ( [ self ESD_enablemousebutton ] );
	} else if (!strcmp(handlerName, "MOUSEBUTTONENABLED")) {
		return ( [ self ESD_MOUSEBUTTONENABLED ] );
	} else if (!strcmp(handlerName, "resetmouse")) {
		return ( [ self ESD_resetmouse ] );

	// Screen Functions

	} else if (!strcmp(handlerName, "setautodimthreshold")) {
		return ( [ self ESD_setautodimthreshold ] );
	} else if (!strcmp(handlerName, "AUTODIMTHRESHOLD")) {
		return ( [ self ESD_AUTODIMTHRESHOLD ] );
	} else if (!strcmp(handlerName, "AUTODIMTIME")) {
		return ( [ self ESD_AUTODIMTIME ] );
	} else if (!strcmp(handlerName, "setautodimstate")) {
		return ( [ self ESD_setautodimstate ] );
	} else if (!strcmp(handlerName, "AUTODIMSTATE")) {
		return ( [ self ESD_AUTODIMSTATE ] );
	} else if (!strcmp(handlerName, "setscreenbrightness")) {
		return ( [ self ESD_setscreenbrightness ] );
	} else if (!strcmp(handlerName, "SCREENBRIGHTNESS")) {
		return ( [ self ESD_SCREENBRIGHTNESS ] );
	} else if (!strcmp(handlerName, "setautodimbrightness")) {
		return ( [ self ESD_setautodimbrightness ] );
	} else if (!strcmp(handlerName, "AUTODIMBRIGHTNESS")) {
		return ( [ self ESD_AUTODIMBRIGHTNESS ] );

	// Volume Functions
	
	} else if (!strcmp(handlerName, "setcurrentvolume")) {
		return ( [ self ESD_setcurrentvolume ] );
	} else if (!strcmp(handlerName, "CURRENTVOLUME")) {
		return ( [ self ESD_CURRENTVOLUME ] );

	// Wait Cursor Functions

	} else if (!strcmp(handlerName, "setwaitcursorthreshold")) {
		return ( [ self ESD_setwaitcursorthreshold ] );
	} else if (!strcmp(handlerName, "WAITCURSORTHRESHOLD")) {
		return ( [ self ESD_WAITCURSORTHRESHOLD ] );
	} else if (!strcmp(handlerName, "setwaitcursorsustain")) {
		return ( [ self ESD_setwaitcursorsustain ] );
	} else if (!strcmp(handlerName, "WAITCURSORSUSTAIN")) {
		return ( [ self ESD_WAITCURSORSUSTAIN ] );
	} else if (!strcmp(handlerName, "setwaitcursorframeinterval")) {
		return ( [ self ESD_setwaitcursorframeinterval ] );
	} else if (!strcmp(handlerName, "WAITCURSORFRAMEINTERVAL")) {
		return ( [ self ESD_WAITCURSORFRAMEINTERVAL ] );
	} else if (!strcmp(handlerName, "ESD_VERSION")) {
		return ( [ self ESD_version ] );
	} else {
		return ( nil ); // handlerName not recognized!
	}
}




// ---------------------------------------------------------------
//   Useful methods for loading interface data
// ---------------------------------------------------------------


+ finishLoading:(struct mach_header *)header
{
	loadHeader = header; // Save this for later use
	return ( self );
}


- loadInterfaceData
{
	//[NXApp loadNibSection:"MyXModule.nib" owner:self
	//    withNames:NO fromHeader:loadHeader] )
		
	return ( self );
}



///////////////////////
// Keyboard Functions
///////////////////////
- ESD_setkeyrepeatinterval
{
	int		pCount = [ self paramCount ];

	if (( pCount != 1 ) || ( !([[ self getParam:1 ] validAsNumber ]) )) {
		return ( [[ self getEmptyContainer ]
			setString:"Usage:  setKeyRepeatInterval  <aFloat>" ] );
	}

	NXSetKeyRepeatInterval(ourEventStatusHandle, [[ self getParam:1 ] floatVal ]);

	return ( self );
}


- ESD_KEYREPEATINTERVAL
{
	return ( [[ self getEmptyContainer ] setFloat:NXKeyRepeatInterval(ourEventStatusHandle) ] );
}


- ESD_setkeyrepeatthreshold
{
	int		pCount = [ self paramCount ];

	if (( pCount != 1 ) || ( !([[ self getParam:1 ] validAsNumber ]) )) {
		return ( [[ self getEmptyContainer ]
			setString:"Usage:  setKeyRepeatThreshold  <aFloat>" ] );
	}

	NXSetKeyRepeatThreshold(ourEventStatusHandle, [[ self getParam:1 ] floatVal ]);

	return ( self );
}


- ESD_KEYREPEATTHRESHOLD
{
	return ( [[ self getEmptyContainer ] setFloat:NXKeyRepeatThreshold(ourEventStatusHandle) ] );
}


- ESD_setkeymapping
{
	int		pCount = [ self paramCount ];

	if ( pCount != 1 ) {
		return ( [[ self getEmptyContainer ]
			setString:"Usage:  setKeyMapping  <keyMapping>" ] );
	}

	return ( self );
}


- ESD_KEYMAPPINGLENGTH
{
	return ( [[ self getEmptyContainer ] setInt:NXKeyMappingLength(ourEventStatusHandle) ] );
}


- ESD_GETKEYMAPPING
{
	return ( self );
}


- ESD_resetkeyboard
{
	NXResetKeyboard(ourEventStatusHandle);

	return ( self );
}




////////////////////
// Mouse Functions
////////////////////
- ESD_setclicktime
{
	int		pCount = [ self paramCount ];

	if (( pCount != 1 ) || ( !([[ self getParam:1 ] validAsNumber ]) )) {
		return ( [[ self getEmptyContainer ]
			setString:"Usage:  setClickTime  <aFloat>" ] );
	}

	NXSetClickTime(ourEventStatusHandle, [[ self getParam:1 ] floatVal ]);

	return ( self );
}


- ESD_CLICKTIME
{
	return ( [[ self getEmptyContainer ] setFloat:NXClickTime(ourEventStatusHandle) ] );
}


- ESD_setclickspace
{
	int		pCount = [ self paramCount ];
	NXSize		newClickSpace;
	const char	*clickSpaceParam;

	if (( pCount != 1 ) || ( !([[ self getParam:1 ] validAsPoint ]) )) {
		return ( [[ self getEmptyContainer ]
			setString:"Usage:  setClickSpace  ªwidth,heightº" ] );
	}

	clickSpaceParam = [[ self getParam:1 ] stringVal ];
	sscanf(clickSpaceParam, " %f , %f ", &newClickSpace.width, &newClickSpace.height );
	NXSetClickSpace(ourEventStatusHandle, &newClickSpace);

	return ( self );
}


- ESD_GETCLICKSPACE
{
	NXSize		currentClickSpace;

	NXGetClickSpace(ourEventStatusHandle, &currentClickSpace);

	return ( [[ self getEmptyContainer ] setPoint:currentClickSpace.width :currentClickSpace.height ] );
}


- ESD_setmousescaling
{
	int		pCount = [ self paramCount ];

	if ( pCount != 1 ) {
		return ( [[ self getEmptyContainer ]
			setString:"Usage:  setMouseScaling  <aFloat>" ] );
	}

	return ( self );
}


- ESD_GETMOUSESCALING
{
	return ( self );
}


- ESD_enablemousebutton
{
	return ( self );
}


- ESD_MOUSEBUTTONENABLED
{
	return ( self );
}


- ESD_resetmouse
{
	NXResetMouse(ourEventStatusHandle);

	return ( self );
}




/////////////////////
// Screen Functions
/////////////////////
- ESD_setautodimthreshold
{
	int		pCount = [ self paramCount ];
	float		newAutoDimThreshold;

 	if (( pCount != 1 ) || ( !([[ self getParam:1 ] validAsNumber ]) )) {
		return ( [[ self getEmptyContainer ]
			setString:"Usage:  setAutoDimThreshold  <aFloat>" ] );
	}

	newAutoDimThreshold = [[ self getParam:1 ] floatVal ];
	if (( newAutoDimThreshold < 0.0 ) || ( newAutoDimThreshold > 1.0 )) {
		return ( [[ self getEmptyContainer ]
			setString:"Usage:  setAutoDimBrightness  <0.0 - 1.0>" ] );
	}

	NXSetAutoDimThreshold(ourEventStatusHandle, newAutoDimThreshold);

	return ( self );
}


- ESD_AUTODIMTHRESHOLD
{
	return ( [[ self getEmptyContainer ] setFloat:NXAutoDimThreshold(ourEventStatusHandle) ] );
}


- ESD_AUTODIMTIME
{
	return ( [[ self getEmptyContainer ] setFloat:NXAutoDimTime(ourEventStatusHandle) ] );
}


- ESD_setautodimstate
{
	int		pCount = [ self paramCount ];
	BOOL		autoDimState;

	if ( pCount != 1 ) {
		return ( [[ self getEmptyContainer ]
			setString:"Usage:  setAutoDimState  <boolVal>" ] );
	} else {
		autoDimState = [[ self getParam:1 ] boolVal ];
		if (( autoDimState == YES ) && ( autoDimState == NO )) {
			return ( [[ self getEmptyContainer ]
				setString:"Usage:  setAutoDimState  <boolVal>" ] );
		}
	}

	NXSetAutoDimState(ourEventStatusHandle, autoDimState);

	return ( self );
}


- ESD_AUTODIMSTATE
{
	return ( [[ self getEmptyContainer ] setBool:NXAutoDimState(ourEventStatusHandle) ] );
}


- ESD_setscreenbrightness
{
	int		pCount = [ self paramCount ];
	float		newScreenBrightness;

	if (( pCount != 1 ) || ( !([[ self getParam:1 ] validAsNumber ]) )) {
		return ( [[ self getEmptyContainer ]
			setString:"Usage:  setScreenBrightness  <aFloat>" ] );
	}

	newScreenBrightness = [[ self getParam:1 ] floatVal ];
	if (( newScreenBrightness < 0.0 ) || ( newScreenBrightness > 1.0 )) {
		return ( [[ self getEmptyContainer ]
			setString:"Usage:  setAutoDimBrightness  <0.0 - 1.0>" ] );
	}

	NXSetScreenBrightness(ourEventStatusHandle, newScreenBrightness);

	return ( self );
}


- ESD_SCREENBRIGHTNESS
{
	return ( [[ self getEmptyContainer ] setFloat:NXScreenBrightness(ourEventStatusHandle) ] );
}


- ESD_setautodimbrightness
{
	int		pCount = [ self paramCount ];
	float		newAutoDimBrightness;

	if (( pCount != 1 ) || ( !([[ self getParam:1 ] validAsNumber ]) )) {
		return ( [[ self getEmptyContainer ]
			setString:"Usage:  setAutoDimBrightness  <0.0 - 1.0>" ] );
	}

	newAutoDimBrightness = [[ self getParam:1 ] floatVal ];
	if (( newAutoDimBrightness < 0.0 ) || ( newAutoDimBrightness > 1.0 )) {
		return ( [[ self getEmptyContainer ]
			setString:"Usage:  setAutoDimBrightness  <0.0 - 1.0>" ] );
	}

	NXSetAutoDimBrightness(ourEventStatusHandle, newAutoDimBrightness);

	return ( self );
}


- ESD_AUTODIMBRIGHTNESS
{
	return ( [[ self getEmptyContainer ] setFloat:NXAutoDimBrightness(ourEventStatusHandle) ] );
}




/////////////////////
// Volume Functions
/////////////////////
- ESD_setcurrentvolume
{
	int		pCount = [ self paramCount ];
	float		newVolume;

	if (( pCount != 1 ) || ( !([[ self getParam:1 ] validAsNumber ]) )) {
		return ( [[ self getEmptyContainer ]
			setString:"Usage:  setCurrentVolume  <0.0 - 1.0>" ] );
	}

	newVolume = [[ self getParam:1 ] floatVal ];
	if (( newVolume < 0.0 ) || ( newVolume > 1.0 )) {
		return ( [[ self getEmptyContainer ]
			setString:"Usage:  setCurrentVolume  <0.0 - 1.0>" ] );
	}

	NXSetCurrentVolume(ourEventStatusHandle, newVolume);

	return ( self );
}


- ESD_CURRENTVOLUME
{
	return ( [[ self getEmptyContainer ] setFloat:NXCurrentVolume(ourEventStatusHandle) ] );
}




//////////////////////////
// Wait Cursor Functions
//////////////////////////
- ESD_setwaitcursorthreshold
{
	int		pCount = [ self paramCount ];

	if (( pCount != 1 ) || ( !([[ self getParam:1 ] validAsNumber ]) )) {
		return ( [[ self getEmptyContainer ]
			setString:"Usage:  setWaitCursorThreshold  <aFloat>" ] );
	}

	NXSetWaitCursorThreshold(ourEventStatusHandle, [[ self getParam:1 ] floatVal ]);

	return ( self );
}


- ESD_WAITCURSORTHRESHOLD
{
	return ( [[ self getEmptyContainer ] setFloat:NXWaitCursorThreshold(ourEventStatusHandle) ] );
}


- ESD_setwaitcursorsustain
{
	int		pCount = [ self paramCount ];

	if (( pCount != 1 ) || ( !([[ self getParam:1 ] validAsNumber ]) )) {
		return ( [[ self getEmptyContainer ]
			setString:"Usage:  setWaitCursorSustain  <aFloat>" ] );
	}

	NXSetWaitCursorSustain(ourEventStatusHandle, [[ self getParam:1 ] floatVal ]);

	return ( self );
}


- ESD_WAITCURSORSUSTAIN
{
	return ( [[ self getEmptyContainer ] setFloat:NXWaitCursorSustain(ourEventStatusHandle) ] );
}


- ESD_setwaitcursorframeinterval
{
	int		pCount = [ self paramCount ];

	if (( pCount != 1 ) || ( !([[ self getParam:1 ] validAsNumber ]) )) {
		return ( [[ self getEmptyContainer ]
			setString:"Usage:  setWaitCursorFrameInterval  <aFloat>" ] );
	}

	NXSetWaitCursorFrameInterval(ourEventStatusHandle, [[ self getParam:1 ] floatVal ]);

	return ( self );
}


- ESD_WAITCURSORFRAMEINTERVAL
{
	return ( [[ self getEmptyContainer ] setFloat:NXWaitCursorFrameInterval(ourEventStatusHandle) ] );
}



- ESD_version
{
	id		version;

	version = [[ self getEmptyContainer ] setString:"r" ];
	[ version appendString:P_RELEASE ];
	[ version appendString:" v" ];
	[ version appendString:P_VERSION ];
	[ version appendString:"    " ];
	[ version appendString:P_RELTIME ];
	[ version appendString:"\n" ];
	[ version appendString:P_COPYRIGHT ];

	return ( version );
}



@end

