/*
 * BAT : バッテリー残量表示アプリケーション：メインルーチン
 * Copyright(C) 1997-1999 Eiichiro Ito
 *
 * 1997/11/24:Eiichiro Ito
 *  (1) 作成開始
 * 修正履歴
 *
 */
#include	"conslib.h"
#include	<tchar.h>

TCHAR	AppName[] = TEXT("BAT") ;	/* 必須項目 */
TCHAR	MessageBuf[ 256 ] ;
DWORD	g_dwRet = 0 ;

void
sec2hms( DWORD sec, LPDWORD h, LPDWORD m, LPDWORD s )
{
	*s = sec % 60, sec /= 60 ;
	*m = sec % 60, sec /= 60 ;
	*h = sec % 100 ;
}

BOOL
info( LPTSTR ptr )
{
#ifdef	_WIN32_WCE
	DWORD		h, m, s ;
	SYSTEM_POWER_STATUS_EX	st ;

	if ( !GetSystemPowerStatusEx( &st, TRUE ) ) {
		return FALSE ;
	}
	_tcscpy( ptr, TEXT("AC=") ),	ptr += _tcslen( ptr ) ;
	switch ( st.ACLineStatus ) {
	case 0:
		g_dwRet = 1 ;
		_tcscpy( ptr, TEXT("OFF") ) ;
		break ;
	case 1:
		g_dwRet = 0 ;
		_tcscpy( ptr, TEXT("ON ") ) ;
		break ;
	default:
		g_dwRet = 2 ;
		_tcscpy( ptr, TEXT("???") ) ;
		break ;
	}
	ptr += _tcslen( ptr ) ;
	*ptr++ = TEXT('\r'), *ptr++ = TEXT('\n') ;
	_tcscpy( ptr, TEXT("Main=") ),	ptr += _tcslen( ptr ) ;
	if ( st.BatteryFlag == 255 ) {
		_tcscpy( ptr, TEXT("???") ),ptr += _tcslen( ptr ) ;
	} else {
		if ( st.BatteryFlag & 1 ) {
			_tcscpy( ptr, TEXT("High") ), ptr += 4 ;
		} else if ( st.BatteryFlag & 2 ) {
			_tcscpy( ptr, TEXT("Low ") ), ptr += 4 ;
		} else if ( st.BatteryFlag & 4 ) {
			_tcscpy( ptr, TEXT("XXXX") ), ptr += 4 ;
		}
		if ( st.BatteryFlag & 8 ) {
			_tcscpy( ptr, TEXT("(Charge)") ), ptr += 8 ;
		}
		if ( st.BatteryFlag & 128 ) {
			_tcscpy( ptr, TEXT("[NoBattery]") ), ptr += 11 ;
		}
	}
	*ptr++ = TEXT(' ') ;
	if ( st.BatteryLifePercent == 255 ) {
		_tcscpy( ptr, TEXT("???") ) ;
	} else {
		wsprintf( ptr, TEXT("%3d%%"), st.BatteryLifePercent ) ;
	}
	ptr += _tcslen( ptr ) ;
	*ptr++ = TEXT('(') ;
	if ( st.BatteryLifeTime == 0xFFFFFFFF ) {
		_tcscpy( ptr, TEXT("???") ) ;
	} else {
		sec2hms( st.BatteryLifeTime, &h, &m, &s ) ;
		wsprintf( ptr, TEXT("%02d:%02d:%02d"), h, m, s ) ;
	}
	ptr += _tcslen( ptr ) ;
	*ptr++ = TEXT('/') ;
	if ( st.BatteryFullLifeTime == 0xFFFFFFFF ) {
		_tcscpy( ptr, TEXT("???") ) ;
	} else {
		sec2hms( st.BatteryFullLifeTime, &h, &m, &s ) ;
		wsprintf( ptr, TEXT("%02d:%02d:%02d"), h, m, s ) ;
	}
	ptr += _tcslen( ptr ) ;
	*ptr++ = TEXT(')') ;
	*ptr++ = TEXT('\r'), *ptr++ = TEXT('\n') ;
	_tcscpy( ptr, TEXT("Bkup=") ), ptr += 5 ;
	if ( st.BackupBatteryFlag == 255 ) {
		_tcscpy( ptr, TEXT("???") ), ptr += 3 ;
	} else {
		if ( st.BackupBatteryFlag & 1 ) {
			_tcscpy( ptr, TEXT("High") ), ptr += 4 ;
		} else if ( st.BackupBatteryFlag & 2 ) {
			_tcscpy( ptr, TEXT("Low ") ), ptr += 4 ;
		} else if ( st.BackupBatteryFlag & 4 ) {
			_tcscpy( ptr, TEXT("XXXX") ), ptr += 4 ;
		}
		if ( st.BackupBatteryFlag & 8 ) {
			_tcscpy( ptr, TEXT("(Charge)") ), ptr += 8 ;
		}
		if ( st.BackupBatteryFlag & 128 ) {
			_tcscpy( ptr, TEXT("[NoBattery]") ), ptr += 11 ;
		}
	}
	*ptr++ = TEXT(' ') ;
	if ( st.BackupBatteryLifePercent == 255 ) {
		_tcscpy( ptr, TEXT("???") ) ;
	} else {
		wsprintf( ptr, TEXT("%3d%%"), st.BackupBatteryLifePercent ) ;
	}
	ptr += _tcslen( ptr ) ;
	*ptr++ = TEXT('(') ;
	if ( st.BackupBatteryLifeTime == 0xFFFFFFFF ) {
		_tcscpy( ptr, TEXT("???") ) ;
	} else {
		sec2hms( st.BackupBatteryLifeTime, &h, &m, &s ) ;
		wsprintf( ptr, TEXT("%02d:%02d:%02d"), h, m, s ) ;
	}
	ptr += _tcslen( ptr ) ;
	*ptr++ = TEXT('/') ;
	if ( st.BackupBatteryFullLifeTime == 0xFFFFFFFF ) {
		_tcscpy( ptr, TEXT("???") ) ;
	} else {
		sec2hms( st.BackupBatteryFullLifeTime, &h, &m, &s ) ;
		wsprintf( ptr, TEXT("%02d:%02d:%02d"), h, m, s ) ;
	}
	ptr += _tcslen( ptr ) ;
	*ptr++ = TEXT(')') ;
#endif	/* _WIN32_WCE */
	*ptr = 0 ;
	return TRUE ;
}

int
Main( DWORD argc, LPTSTR argv[] )
{
	if ( !info( MessageBuf ) ) {
		Cputs( TEXT("Bat: can't get battery information\n") ) ;
		return 9 ;
	}
	Cputs( MessageBuf ) ;
	Putch( TEXT('\n') ) ;
	return g_dwRet ;
}
