/*****************************************************************************/
/*
	AutodimApp.m
	Copyright 1991 by Victor Barger.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

    Author: Victor Barger
    email:  barger@cs.wisc.edu
    mail:	16 N. Allen St., Madison, WI  53705-3924, U.S.A.
*/
/*****************************************************************************/

#import <appkit/Application.h>
#import <appkit/Panel.h>
#import <appkit/Slider.h>
#import <appkit/Form.h>
#import <defaults.h>

#include <libc.h>
#include <nextdev/evsio.h>

#import "AutodimApp.h"

/*****************************************************************************/

#define APPNAME				"Autodim"
#define SCREEN_BRIGHTNESS	"ScreenBrightness"
#define AUTOLAUNCHED		"NXAutoLaunch"

static int get_autodim_brightness();
static void set_autodim_brightness(int level);

/*****************************************************************************/

@implementation AutodimApp

/*****************************************************************************/
- appDidInit:sender
{
	const char *autolaunch;
	static NXDefaultsVector AutodimDefaults = {
		{SCREEN_BRIGHTNESS,"16"},
		{NULL}
	};

	NXRegisterDefaults(APPNAME,AutodimDefaults);
	def_level = atoi(NXGetDefaultValue(APPNAME,SCREEN_BRIGHTNESS));
	cur_level = get_autodim_brightness();

	if ((def_level != cur_level) && (def_level>=0) && (def_level<=15)) {
		cur_level = def_level;
		set_autodim_brightness(cur_level);
	}

	autolaunch = NXGetDefaultValue(APPNAME,AUTOLAUNCHED);
	if (strcmp(autolaunch,"YES")==0) {
		[NXApp terminate:sender];
	}
	else {
		[self revert:NULL];
		[autodimPanel makeKeyAndOrderFront:self];
	}
	return self;
}

/*****************************************************************************/
- appWillTerminate:sender
{
	char buf[4];

	if (cur_level != def_level) {
		sprintf(buf,"%d",cur_level);
		NXWriteDefault(APPNAME,SCREEN_BRIGHTNESS,buf);
	}
	return self;
}

/*****************************************************************************/
- revert:sender
{
	[slider setIntValue:cur_level];
	[editField setIntValue:cur_level];
    return self;
}

/*****************************************************************************/
- set:sender
{
	cur_level = [editField intValue];
	set_autodim_brightness(cur_level);

	if ([slider intValue] != cur_level)
		[slider setIntValue:cur_level];
    return self;
}

/*****************************************************************************/
- info:sender
{
	if (!infoPanel) {
		infoPanel = [self loadNibSection:"Info.nib"
						owner:self withNames:NO];
	}
	[infoPanel makeKeyAndOrderFront:self];
	return self;
}

/*****************************************************************************/

@end

/*****************************************************************************/
static int get_autodim_brightness()
{
	int evs, level;
	
	evs = open("/dev/evs0",O_RDONLY);
	if (evs < 0) {
		NXRunAlertPanel("Autodim","Error opening /dev/evs0.",
			"OK",NULL,NULL);
		[NXApp terminate:nil];
		exit(1);
	}
	ioctl(evs,EVSIOCADB,&level);
	close(evs);
	return(level);
}

/*****************************************************************************/
static void set_autodim_brightness(int level)
{
	int evs;

	evs = open("/dev/evs0",O_WRONLY);
	if (evs < 0) {
		NXRunAlertPanel("Autodim",
			"Error opening /dev/evs0; cannot set brightness.","OK",NULL,NULL);
		return;
	}
	ioctl(evs,EVSIOSADB,&level);
	close(evs);
}

/*****************************************************************************/

