Commit 8ee2582d authored by Yavor Doganov's avatar Yavor Doganov

Import Upstream version 0.8

parent d1d5705e
/*
Project: batmon
Copyright (C) 2005-2010 GNUstep Application Project
Copyright (C) 2005-2014 GNUstep Application Project
Author: Riccardo Mottola
FreeBSD support by Chris B. Vetter
Created: 2005-06-25 21:06:19 +0200 by multix
......@@ -47,8 +46,6 @@
[[NSUserDefaults standardUserDefaults] registerDefaults:defaults];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (id)init
......@@ -146,6 +143,8 @@
chargePercentToDraw = [batModel chargePercent];
if (chargePercentToDraw > 100)
chargePercentToDraw = 100;
else if (chargePercentToDraw < 0 || chargePercentToDraw == NAN)
chargePercentToDraw = 0;
[[NSColor darkGrayColor] set];
/* main body */
......@@ -160,7 +159,9 @@
[NSBezierPath fillRect: NSMakeRect((WIDTH/2)-2, HEIGHT+1+1, 4, 2)];
/* draw the charge status */
if ([batModel isCritical] == YES)
if ([batModel isWarning] == YES)
[[NSColor orangeColor] set];
else if ([batModel isCritical] == YES)
[[NSColor redColor] set];
else
[[NSColor greenColor] set];
......@@ -224,7 +225,7 @@
/* info window */
lifeVal = [batModel lastCapacity]/[batModel designCapacity];
[lifeGauge setDoubleValue:lifeVal*100];
[lifeGaugePercent setStringValue:[NSString stringWithFormat:@"%3.1f%", lifeVal*100]];
[lifeGaugePercent setStringValue:[NSString stringWithFormat:@"%3.1f%%", lifeVal*100]];
[battType setStringValue:[batModel batteryType]];
[manufacturer setStringValue:[batModel manufacturer]];
......
/*
Project: batmon
Copyright (C) 2006-2013 GNUstep Application Project
Author: Riccardo Mottola
FreeBSD support by Chris B. Vetter (initial version)
Created: 2013-07-23 Riccardo Mottola
This application 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; either
version 2 of the License, or (at your option) any later version.
This application 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
Library General Public License for more details.
You should have received a copy of the GNU General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
*/
#include <unistd.h>
#include <stdint.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <dev/acpica/acpiio.h>
#define ACPIDEV "/dev/acpi"
#import "BatteryModel.h"
@implementation BatteryModel (PlatformSpecific)
- (void)initPlatformSpecific
{
}
- (void)updatePlatformSpecific
{
union acpi_battery_ioctl_arg battio;
int acpifd;
battio.unit = 0;
acpifd = open(ACPIDEV, O_RDWR);
if (acpifd == -1) acpifd = open(ACPIDEV, O_RDONLY);
if (acpifd == -1) return;
if( -1 == ioctl(acpifd, ACPIIO_CMBAT_GET_BIF, &battio) ) return;
desCap = (float)battio.bif.dcap / 1000; // design capacity
lastCap = (float)battio.bif.lfcap / 1000; // last full capacity
if( -1 == ioctl(acpifd, ACPIIO_CMBAT_GET_BST, &battio) ) return;
close(acpifd);
currCap = (float)battio.bst.cap / 1000; // remaining capacity
volts = (float)battio.bst.volt / 1000; // present voltage
watts = (float)battio.bst.rate / 1000; // present rate
amps = 0;
if (volts)
amps = watts / volts;
batteryType = @"";
if( ACPI_BATT_STAT_NOT_PRESENT != battio.bst.state )
{
isCritical = NO;
if( battio.bst.state & ACPI_BATT_STAT_CRITICAL )
isCritical = YES;
if( battio.bst.state == 0 )
batteryState = BMBStateHigh;
else if( battio.bst.state & ACPI_BATT_STAT_CHARGING )
batteryState = BMBStateCharging;
else if( battio.bst.state & ACPI_BATT_STAT_DISCHARG )
batteryState = BMBStateDischarging;
else if (battio.bst.state & ACPI_BATT_STAT_INVALID )
batteryState = BMBStateUnknown;
else
batteryState = BMBStateUnknown;
batteryType = [NSString stringWithFormat: @"%s", battio.bif.type];
}
else
{
batteryState = BMBStateMissing;
batteryType = @"Missing";
}
if( batteryState == BMBStateHigh )
{
timeRemaining = 0;
chargePercent = currCap/lastCap*100;
isCharging = YES;
}
else if( batteryState == BMBStateCharging )
{
timeRemaining = (lastCap-currCap) / watts;
chargePercent = currCap/lastCap*100;
isCharging = YES;
}
else if( batteryState == BMBStateDischarging )
{
timeRemaining = currCap / watts;
chargePercent = currCap/lastCap*100;
isCharging = NO;
}
}
@end
This diff is collapsed.
/*
Project: batmon
Copyright (C) 2006-2013 GNUstep Application Project
Author: Riccardo Mottola
Created: 2013-07-23 Riccardo Mottola
This application 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; either
version 2 of the License, or (at your option) any later version.
This application 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
Library General Public License for more details.
You should have received a copy of the GNU General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
*/
#include <paths.h> /* path for the system devices */
#include <fcntl.h> /* open */
#include <unistd.h>
#include <sys/envsys.h>
#include <prop/proplib.h> /* psd property dictionaries */
#import "BatteryModel.h"
@implementation BatteryModel (PlatformSpecific)
- (void)initPlatformSpecific
{
}
- (void)updatePlatformSpecific
{
int sysmonfd; /* fd of /dev/sysmon */
int rval;
prop_dictionary_t bsd_dict;
NSString *string;
NSDictionary *dict;
char *cStr;
NSEnumerator *enum1;
id obj1;
NSDictionary *acpibat0;
NSMutableDictionary *batDict;
NSDictionary *chargeDict;
NSString *valueStr;
float chargeRate;
float dischargeRate;
/* Open the device in ro mode */
if ((sysmonfd = open(_PATH_SYSMON, O_RDONLY)) == -1)
NSLog(@"Error opening device: %s", _PATH_SYSMON);
rval = prop_dictionary_recv_ioctl(sysmonfd,
ENVSYS_GETDICTIONARY,
&bsd_dict);
if (rval)
{
NSLog(@"Error: %s", strerror(rval));
return;
}
cStr = prop_dictionary_externalize(bsd_dict);
string = [NSString stringWithCString: cStr];
dict = [string propertyList];
if (dict == nil)
{
NSLog(@"Could not parse dictionary");
return;
}
acpibat0 = [dict objectForKey: @"acpibat0"];
//NSLog(@"acpibat0: %@", acpibat0);
batDict = [NSMutableDictionary dictionaryWithCapacity: 3];
enum1 = [acpibat0 objectEnumerator];
while ((obj1 = [enum1 nextObject]))
{
// NSLog(@"--->%@", obj1);
if ([obj1 isKindOfClass: [NSDictionary class]])
{
NSString *descriptionKey;
descriptionKey = [obj1 objectForKey:@"description"];
// NSLog(@"key-----> %@", descriptionKey);
if (descriptionKey)
[batDict setObject: obj1 forKey: descriptionKey];
}
else
NSLog(@"not a dict");
}
//NSLog(@"battery dictionary: %@", batDict);
(void)close(sysmonfd);
valueStr = [[batDict objectForKey: @"voltage"] objectForKey: @"cur-value"];
if (valueStr)
volts = [valueStr floatValue] / 1000000;
NSLog(@"voltage: %@ %f", valueStr, volts);
valueStr = [[batDict objectForKey: @"design cap"] objectForKey: @"cur-value"];
if (valueStr)
{
if ([[[batDict objectForKey: @"design cap"] objectForKey: @"type"] isEqualToString: @"Ampere hour"])
{
useWattHours = NO;
}
else if ([[[batDict objectForKey: @"design cap"] objectForKey: @"type"] isEqualToString: @"Watt hour"])
{
useWattHours = YES;
}
desCap = [valueStr floatValue] / 1000000;
}
NSLog(@"design cap: %@ %f", valueStr, desCap);
chargeDict = [batDict objectForKey: @"charge"];
valueStr = [chargeDict objectForKey: @"critical-capacity"];
if (valueStr)
{
critCap = [valueStr floatValue] / 1000000;
}
NSLog(@"crit cap: %@ %f", valueStr, critCap);
valueStr = [chargeDict objectForKey: @"warning-capacity"];
if (valueStr)
{
warnCap = [valueStr floatValue] / 1000000;
}
NSLog(@"warn cap: %@ %f", valueStr, warnCap);
valueStr = [[batDict objectForKey: @"last full cap"] objectForKey: @"cur-value"];
if (valueStr)
lastCap = [valueStr floatValue] / 1000000;
NSLog(@"last full cap: %@, %f", valueStr, lastCap);
valueStr = [[batDict objectForKey: @"charge rate"] objectForKey: @"cur-value"];
chargeRate = 0;
if (valueStr)
chargeRate = [valueStr floatValue] / 1000000;
NSLog(@"charge rate: %@ %f", valueStr, chargeRate);
valueStr = [[batDict objectForKey: @"discharge rate"] objectForKey: @"cur-value"];
dischargeRate = 0;
if (valueStr)
dischargeRate = [valueStr floatValue] / 1000000;
NSLog(@"discharge rate: %@ %f", valueStr, dischargeRate);
valueStr = [[batDict objectForKey: @"charge"] objectForKey: @"cur-value"];
if (valueStr)
currCap = [valueStr floatValue] / 1000000;
NSLog(@"charge: %@, %f", valueStr, currCap);
valueStr = [[batDict objectForKey: @"charging"] objectForKey: @"cur-value"];
NSLog(@"charging: %@", valueStr);
NSLog(@"currCap %f, lastCap %f, amps %f", currCap, lastCap,amps);
if ([valueStr intValue] == 0)
{
amps = dischargeRate;
isCharging = NO;
if (amps > 0)
timeRemaining = currCap / amps;
else
timeRemaining = -1;
if (lastCap)
chargePercent = currCap/lastCap*100;
batteryState = BMBStateDischarging;
}
else
{
amps = chargeRate;
isCharging = YES;
if (amps > 0)
timeRemaining = (lastCap-currCap) / amps;
else
timeRemaining = -1;
chargePercent = 0;
if (lastCap)
chargePercent = currCap/lastCap*100;
batteryState = BMBStateCharging;
}
watts = amps * volts;
/* sanitize */
if (critCap > warnCap)
critCap = warnCap;
if (critCap == 0)
{
if (warnCap > 0)
critCap = warnCap;
else
critCap = desCap / 100;
}
isCritical = NO;
if (currCap <= critCap)
isCritical = YES;
}
@end
/*
Project: batmon
Copyright (C) 2006-2014 GNUstep Application Project
Author: Riccardo Mottola
Created: 2013-07-23 Riccardo Mottola
This application 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; either
version 2 of the License, or (at your option) any later version.
This application 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
Library General Public License for more details.
You should have received a copy of the GNU General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
*/
#if defined(openbsd) || defined(__OpenBSD__)
#include <unistd.h>
#include <fcntl.h> /* open */
#include <sys/ioctl.h>
#include <machine/apmvar.h>
#define APMDEV "/dev/apm"
#endif
#import "BatteryModel.h"
@implementation BatteryModel (PlatformSpecific)
- (void)initPlatformSpecific
{
}
- (void)updatePlatformSpecific
{
int apmfd;
struct apm_power_info apmPwInfo;
BOOL validBattery;
apmfd = open(APMDEV, O_RDONLY);
if (apmfd == -1)
return;
if( -1 == ioctl(apmfd, APM_IOC_GETPOWER, &apmPwInfo) )
return;
isCharging = NO;
isCritical = NO;
validBattery = YES;
if (APM_BATT_HIGH == apmPwInfo.battery_state)
batteryState = BMBStateHigh;
else if (APM_BATT_LOW == apmPwInfo.battery_state)
{
batteryState = BMBStateLow;
}
else if (APM_BATT_CRITICAL == apmPwInfo.battery_state)
{
batteryState = BMBStateCritical;
isCritical = YES;
}
else if (APM_BATT_CHARGING == apmPwInfo.battery_state)
{
batteryState = BMBStateCharging;
isCharging = YES;
}
else if (APM_BATTERY_ABSENT == apmPwInfo.battery_state)
{
batteryState = BMBStateMissing;
validBattery = NO;
}
else
{
batteryState = BMBStateUnknown;;
validBattery = NO;
}
if (APM_AC_ON == apmPwInfo.ac_state)
isCharging = YES;
/* we expect time in hours */
if (validBattery)
{
timeRemaining = (float)apmPwInfo.minutes_left / 60;
chargePercent = (float)(int)apmPwInfo.battery_life;
/* sanity checks */
if (isCharging && timeRemaining > 100)
timeRemaining = 0;
if (chargePercent > 100)
chargePercent = 100;
else if (chargePercent < 0)
chargePercent = 0;
if (timeRemaining < 0)
timeRemaining = 0;
}
else
{
chargePercent = 0;
timeRemaining= 0;
}
close(apmfd);
}
@end
/*
Project: batmon
Copyright (C) 2006-2013 GNUstep Application Project
Copyright (C) 2006-2014 GNUstep Application Project
Author: Riccardo Mottola
......@@ -22,6 +22,13 @@
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
*/
#if defined (linux)
#define DEV_SYS_POWERSUPPLY @"/sys/class/power_supply"
#define DEV_PROC_PMU @"/proc/pmu"
#endif
typedef enum
{
BMBStateUnknown = 0,
......@@ -70,6 +77,8 @@ typedef enum
@private float timeRemaining;
@private BOOL isCharging;
@private BOOL isCritical;
@private float critPercent;
@private float warnPercent;
@private BMBatteryStates batteryState;
@private NSString *batteryType;
......@@ -89,6 +98,7 @@ typedef enum
- (float)chargePercent;
- (BOOL)isCritical;
- (BOOL)isWarning;
- (BOOL)isCharging;
- (BOOL)isUsingWattHours;
......@@ -100,3 +110,9 @@ typedef enum
@end
@interface BatteryModel (PlatformSpecific)
- (void)initPlatformSpecific;
- (void)updatePlatformSpecific;
@end
This diff is collapsed.
2014-05-25 Riccardo Mottola <rm@gnu.org>
* AppController.m
* BatteryModel.h
* BatteryModel.m
Calculate warning capacity and draw orange bar in case.
2014-05-25 Riccardo Mottola <rm@gnu.org>
* BatteryModel.m
Calculate critical status on capacity if !isCritical.
2014-05-04 Riccardo Mottola <rm@gnu.org>
* BatteryModel-Linux.m
* BatteryModel-OpenBSD.m
Init isCritical, so that once condition reverts it returns to NO.
2014-01-19 Riccardo Mottola <rm@gnu.org>
* BatteryModel.h
* BatteryModel.m
Fix ACPI/APM/PMU detection fallback on Linux.
2013-10-15 Riccardo Mottola <rm@gnu.org>
* AppController.m
Fix formatting string.
2013-07-24 Riccardo Mottola <rm@gnu.org>
* BatteryModel-NetBSD.m
* BatteryModel-FreeBSD.m
* BatteryModel.m
FreeBSD and NetBSD refactor.
2013-07-24 Riccardo Mottola <rm@gnu.org>
* BatteryModel-Linux.m
* BatteryModel-OpenBSD.m
* BatteryModel.h
* BatteryModel.m
Provide initPlatformSpecific initialization code (used only for linux)
2013-07-24 Riccardo Mottola <rm@gnu.org>
* BatteryModel.m
* BatteryModel-Linux.m
Separate update method into OpenBSD Platform Specific file.
2013-07-24 Riccardo Mottola <rm@gnu.org>
* BatteryModel.h
* BatteryModel.m
* BatteryModel-Linux.m
Separate update method into linux Platform Specific file.
2013-04-12 Riccardo Mottola <rm@gnu.org>
* BatteryModel.m (update)
Fix leak of file descriptor on linux.
......@@ -13,7 +13,7 @@ include $(GNUSTEP_MAKEFILES)/common.make
#
# Application
#
VERSION = 0.7
VERSION = 0.8
PACKAGE_NAME = batmon
APP_NAME = batmon
batmon_APPLICATION_ICON = BatMon_icon.tif
......
......@@ -21,7 +21,9 @@ ADDITIONAL_INCLUDE_DIRS +=
ADDITIONAL_LIB_DIRS +=
# Additional GUI libraries to link
ifeq ($(findstring netbsd, $(GNUSTEP_TARGET_OS)), netbsd)
ADDITIONAL_GUI_LIBS += -lprop
ADDITIONAL_GUI_LIBS +=
ifeq ($(findstring netbsd, $(GNUSTEP_TARGET_OS)), netbsd)
ADDITIONAL_GUI_LIBS += -lprop
endif
{
APPLICATIONICON = "BatMon_icon.tif";
"APP_DOCUMENT_BASED" = NO;
"APP_TYPE" = GORM;
"BUILDER_TARGETS" = (
all,
install,
uninstall,
clean,
distclean,
dist
);
"CLASS_FILES" = (
"AppController.m",
"BatteryModel.m"
);
COMPILEROPTIONS = "";
CPPOPTIONS = "";
"CREATION_DATE" = "2005-06-25 21:06:19 +0200";
"DOCU_FILES" = (
LICENSE,
README
);
FRAMEWORKS = (
);
"HEADER_FILES" = (
"AppController.h",
"BatteryModel.h"
);
IMAGES = (
"BatMon_icon.tif",
"small_battery.tif",
"small_plug.tif"
);
INSTALLDIR = "$(HOME)/GNUstep";
INTERFACES = (
"batmon.gorm"
);
LANGUAGE = English;
LIBRARIES = (
"gnustep-base",
"gnustep-gui"
);
LINKEROPTIONS = "";
"LOCALIZED_RESOURCES" = (
);
MAININTERFACE = "batmon.gorm";
MAKEFILEDIR = "$(GNUSTEP_MAKEFILES)";
"OBJC_COMPILEROPTIONS" = "";
"OTHER_RESOURCES" = (
);
"OTHER_SOURCES" = (
"main.m"
);
"PRINCIPAL_CLASS" = NSApplication;
"PROJECT_AUTHORS" = (
"Riccardo Mottola (rmottola@users.sf.net)"
);
"PROJECT_COPYRIGHT" = "Copyright (C) 2005-2014";
"PROJECT_COPYRIGHT_DESC" = "Released under GPL";
"PROJECT_CREATOR" = "";
"PROJECT_DESCRIPTION" = "Battery Monitor";
"PROJECT_DOCUMENTTYPES" = (
);
"PROJECT_GROUP" = "No group avaliable!";
"PROJECT_MAINTAINER" = "";
"PROJECT_NAME" = batmon;
"PROJECT_RELEASE" = "0.8";
"PROJECT_SUMMARY" = "No summary avaliable!";
"PROJECT_TYPE" = Application;
"PROJECT_URL" = "http://gap.nongnu.org/batmon/";
"SEARCH_HEADER_DIRS" = (
);
"SEARCH_LIB_DIRS" = (
);
SUBPROJECTS = (
);
"SUPPORTING_FILES" = (
"batmonInfo.plist"
);
"USER_LANGUAGES" = (
English
);
}
\ No newline at end of file
{"LAST_EDITING" = "2014-08-29 03:17:56 +0200"; "PC_WINDOWS" = {ProjectBrowser = "{x = 0; y = 0; width = 544; height = 166}"; ProjectWindow = "107 290 562 501 0 0 1400 1050 "; ShowToolbar = YES; }; }
\ No newline at end of file
......@@ -56,7 +56,7 @@
"PROJECT_AUTHORS" = (
"Riccardo Mottola (rmottola@users.sf.net)"
);
"PROJECT_COPYRIGHT" = "Copyright (C) 2005-2013";
"PROJECT_COPYRIGHT" = "Copyright (C) 2005-2014";
"PROJECT_COPYRIGHT_DESC" = "Released under GPL";
"PROJECT_CREATOR" = "";
"PROJECT_DESCRIPTION" = "Battery Monitor";
......@@ -65,7 +65,7 @@
"PROJECT_GROUP" = "No group avaliable!";
"PROJECT_MAINTAINER" = "";
"PROJECT_NAME" = batmon;
"PROJECT_RELEASE" = "0.6";
"PROJECT_RELEASE" = "0.8";
"PROJECT_SUMMARY" = "No summary avaliable!";
"PROJECT_TYPE" = Application;
"PROJECT_URL" = "http://gap.nongnu.org/batmon/";
......@@ -81,4 +81,4 @@
"USER_LANGUAGES" = (
English
);
}
}
\ No newline at end of file
{"LAST_EDITING" = "2014-09-04 00:50:40 +0200"; "PC_WINDOWS" = {ProjectBrowser = "{x = 0; y = 0; width = 544; height = 166}"; ProjectBuild = "0 0 0 0 4 0 3 0 "; ProjectLaunch = "510 109 482 355 0 0 1400 1050 "; ProjectWindow = "107 290 562 501 0 0 1400 1050 "; ShowToolbar = YES; }; }
\ No newline at end of file
......@@ -3,17 +3,17 @@
ApplicationDescription = "Battery Monitor";
ApplicationIcon = "BatMon_icon.tif";
ApplicationName = batmon;
ApplicationRelease = "0.7";
ApplicationRelease = "0.8";
Authors = (
"Riccardo Mottola (rmottola@users.sf.net)"
);
Copyright = "Copyright (C) 2005-2013";
Copyright = "Copyright (C) 2005-2014";
CopyrightDescription = "Released under GPL";
FullVersionID = "0.7";
FullVersionID = "0.8";
NSExecutable = batmon;
NSIcon = "BatMon_icon.tif";
NSMainNibFile = "batmon.gorm";
NSPrincipalClass = NSApplication;
NSRole = Application;
URL = "http://gap.nongnu.org/batmon/";
}
}
\ No newline at end of file