#import "Thinker.h"
#import "BackWindow.h"
#import "psfuncts.h"
#import <appkit/Window.h>
#import <objc/List.h>
#import <nextdev/evsio.h>
#import <libc.h>


// these defines allow this to compile on a 2.0 system, though
// BackSpace can't prevent the screen from dimming on a 2.0
// system.  You need 2.1 for that.  Other than this, BackSpace
// works fine on a 2.0 system.
#ifndef EVSIOSADB
#define EVSIOSADB _IOW('e', 104, int)	/* Set AutoDim Brightness */
#define EVSIOCADB _IOR('e', 105, int)	/* Current AutoDim Brightness */
#endif

@implementation Thinker(ioctls)

// You really should restore the old dim value every time you
// are not acting as the screen saver.  Unfortunately, setting
// a bright screen saver value has no effect when the screen is
// dim!
- normalMode
{
	[self _setDimBrightness : &oldDimBrightness];
	return self;
}

- screenSaverMode
{
	// this method prevents the screen from dimming so you can see the
	// screen saver.  Just return self right here if you want the screen
	// to go dim while the screensaver is doing its thing.
	
	
	[self getNormalBrightness :&normalBrightness];
	[self _setDimBrightness :&normalBrightness];

	return self;
}


// In the multi-headed case, I gotta throw a black window over all
// the screens so they don't burn in while I do animation on one.

- blackOutAllScreens
{
	int i;
	NXRect r;
		
	if (screenCount <= 1) return self;

	if (!screenList) screenList = [[List alloc] init];

	for (i=0; i < screenCount; i++)
	{
		id theWindow;
		r = screens[i].screenBounds;
			
		theWindow = [[BackWindow alloc]
				initContent:&r style:NX_TOKENSTYLE
				backing:NX_NONRETAINED buttonMask:0 defer:NO];

		[screenList addObject:theWindow];

		[theWindow removeFromEventMask:(NX_LMOUSEDOWNMASK | NX_LMOUSEUPMASK
  			   | NX_MOUSEMOVEDMASK | NX_LMOUSEDRAGGEDMASK
			   | NX_MOUSEENTEREDMASK | NX_MOUSEEXITEDMASK
			   | NX_KEYDOWNMASK | NX_KEYUPMASK
			   | NX_CURSORUPDATEMASK)];
		[theWindow setBackgroundGray:NX_BLACK];

		tweakWindow([theWindow windowNum], SAVERTIER-1);
		[theWindow placeWindowAndDisplay:&r];
		[theWindow orderFront:self];

	}

	return self;
}

- unBlackOutAllScreens
{
	if (screenCount <= 1) return self;
	[screenList makeObjectsPerform:@selector(orderOut:) with:self];
	[screenList freeObjects];
	return self;
}

- getDimBrightness:(int *)b
{
	ioctl(evs, EVSIOCADB, b);
	return self;
}

- _setDimBrightness :(int *)b
{
	ioctl(evs, EVSIOSADB, b);
	return self;
}


- getNormalBrightness :(int *)b
{
	ioctl(evs, EVSIOCB, b);		//get current brighness
	return self;
}


- getDimTime :(int *)t
{
	ioctl(evs, EVSIOGADT, t);			//get dim time
	return self;
}

- getDimInterval :(int *)i
{
	ioctl(evs, EVSIOCADT, i);		//get dim interval
	return self;
}

- setDimInterval :(int *)i
{
	ioctl(evs, EVSIOSADT, i);	//set dim interval
	return self;
}


- getDimStatus :(int *)s
{
	ioctl(evs, EVSIOCADS, s);			//get dim status
	return self;
}

@end

