55
66#include <IOKit/IOKitLib.h>
77
8- static double detectBatteryTemp (void )
9- {
10- double result = 0 ;
11-
12- if (ffDetectCoreTemps (FF_TEMP_BATTERY , & result ))
13- return FF_BATTERY_TEMP_UNSET ;
14-
15- return result ;
16- }
17-
188const char * ffDetectBattery (FFBatteryOptions * options , FFlist * results )
199{
2010 io_iterator_t iterator ;
@@ -24,7 +14,7 @@ const char* ffDetectBattery(FFBatteryOptions* options, FFlist* results)
2414 io_registry_entry_t registryEntry ;
2515 while ((registryEntry = IOIteratorNext (iterator )) != 0 )
2616 {
27- CFMutableDictionaryRef properties ;
17+ FF_CFTYPE_AUTO_RELEASE CFMutableDictionaryRef properties = NULL ;
2818 if (IORegistryEntryCreateCFProperties (registryEntry , & properties , kCFAllocatorDefault , kNilOptions ) != kIOReturnSuccess )
2919 {
3020 IOObjectRelease (registryEntry );
@@ -35,7 +25,13 @@ const char* ffDetectBattery(FFBatteryOptions* options, FFlist* results)
3525 const char * error ;
3626
3727 FFBatteryResult * battery = ffListAdd (results );
28+ battery -> temperature = FF_BATTERY_TEMP_UNSET ;
29+ ffStrbufInit (& battery -> manufacturer );
30+ ffStrbufInit (& battery -> modelName );
31+ ffStrbufInit (& battery -> technology );
32+ ffStrbufInit (& battery -> status );
3833 battery -> capacity = 0.0 /0.0 ;
34+
3935 int currentCapacity , maxCapacity ;
4036
4137 if ((error = ffCfDictGetInt (properties , CFSTR ("MaxCapacity" ), & maxCapacity )))
@@ -50,33 +46,36 @@ const char* ffDetectBattery(FFBatteryOptions* options, FFlist* results)
5046
5147 battery -> capacity = currentCapacity * 100.0 / maxCapacity ;
5248
53- ffStrbufInit (& battery -> manufacturer );
54- ffStrbufInit (& battery -> modelName );
55- ffStrbufInit (& battery -> technology );
49+ ffCfDictGetString (properties , CFSTR ("DeviceName" ), & battery -> modelName );
50+
5651 if (!ffCfDictGetBool (properties , CFSTR ("built-in" ), & boolValue ) && boolValue )
5752 {
5853 ffStrbufAppendS (& battery -> manufacturer , "Apple Inc." );
59- ffStrbufAppendS (& battery -> modelName , "Builtin" );
6054 ffStrbufAppendS (& battery -> technology , "Lithium" );
61- }
62- else
63- {
64- ffStrbufAppendS (& battery -> manufacturer , "Unknown" );
65- ffStrbufAppendS (& battery -> modelName , "Unknown" );
66- ffStrbufAppendS (& battery -> technology , "Unknown" );
55+ if (!battery -> modelName .length )
56+ ffStrbufAppendS (& battery -> modelName , "Built-in" );
6757 }
6858
69- ffStrbufInit (& battery -> status );
70- if (!ffCfDictGetBool (properties , CFSTR ("FullyCharged" ), & boolValue ) && boolValue )
71- ffStrbufAppendS (& battery -> status , "Fully charged" );
72- else if (!ffCfDictGetBool (properties , CFSTR ("IsCharging" ), & boolValue ) && boolValue )
73- ffStrbufAppendS (& battery -> status , "Charging" );
74- else
75- ffStrbufAppendS (& battery -> status , "" );
59+ ffCfDictGetInt (properties , CFSTR ("CycleCount" ), & battery -> cycleCount );
60+
61+ if (!ffCfDictGetBool (properties , CFSTR ("ExternalConnected" ), & boolValue ) && boolValue )
62+ ffStrbufAppendS (& battery -> status , "AC connected, " );
63+ if (!ffCfDictGetBool (properties , CFSTR ("IsCharging" ), & boolValue ) && boolValue )
64+ ffStrbufAppendS (& battery -> status , "Charging, " );
65+ if (!ffCfDictGetBool (properties , CFSTR ("AtCriticalLevel" ), & boolValue ) && boolValue )
66+ ffStrbufAppendS (& battery -> status , "Critical, " );
67+ ffStrbufTrimRight (& battery -> status , ' ' );
68+ ffStrbufTrimRight (& battery -> status , ',' );
7669
77- battery -> temperature = options -> temp ? detectBatteryTemp () : FF_BATTERY_TEMP_UNSET ;
70+ if (options -> temp )
71+ {
72+ int64_t temp ;
73+ if (!ffCfDictGetInt64 (properties , CFSTR ("Temperature" ), & temp ))
74+ battery -> temperature = (double ) temp / 10 - 273.15 ;
75+ else
76+ ffDetectCoreTemps (FF_TEMP_BATTERY , & battery -> temperature );
77+ }
7878
79- CFRelease (properties );
8079 IOObjectRelease (registryEntry );
8180 }
8281
0 commit comments