forked from mklein-de/htop-osx
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathBatteryMeter.c
145 lines (118 loc) · 3.86 KB
/
BatteryMeter.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
htop
(C) 2004-2006 Hisham H. Muhammad
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
This "Meter" written by Ian P. Hands ([email protected], [email protected]).
Adapted for OSX by Jonas Due Vesterheden ([email protected])
*/
#include <CoreFoundation/CoreFoundation.h>
#include <CoreFoundation/CFString.h>
#include <IOKit/ps/IOPowerSources.h>
#include <IOKit/ps/IOPSKeys.h>
#include "BatteryMeter.h"
#include "Meter.h"
#include "ProcessList.h"
#include "CRT.h"
#include "String.h"
#include "debug.h"
/*{
typedef enum ACPresence_ {
AC_ABSENT,
AC_PRESENT,
AC_ERROR
} ACPresence;
}*/
int BatteryMeter_attributes[] = {
BATTERY
};
static ACPresence chkIsOnline() {
CFTypeRef sourceInfo = IOPSCopyPowerSourcesInfo();
CFArrayRef sourceList = IOPSCopyPowerSourcesList(sourceInfo);
// Loop through sources, find the first battery
int count = CFArrayGetCount(sourceList);
CFDictionaryRef source = NULL;
for(int i=0; i < count; i++) {
source = IOPSGetPowerSourceDescription(sourceInfo, CFArrayGetValueAtIndex(sourceList, i));
// Is this a battery?
CFStringRef type = (CFStringRef)CFDictionaryGetValue(source, CFSTR(kIOPSTransportTypeKey));
if(kCFCompareEqualTo == CFStringCompare(type, CFSTR(kIOPSInternalType), 0)) {
break;
}
}
ACPresence isOn = AC_ERROR;
if(source != NULL) {
CFStringRef state = CFDictionaryGetValue(source, CFSTR(kIOPSPowerSourceStateKey));
if(kCFCompareEqualTo == CFStringCompare(state, CFSTR(kIOPSACPowerValue), 0)) {
isOn = AC_PRESENT;
} else {
isOn = AC_ABSENT;
}
}
CFRelease(sourceInfo);
CFRelease(sourceList);
return isOn;
}
static double getBatData() {
CFTypeRef sourceInfo = IOPSCopyPowerSourcesInfo();
CFArrayRef sourceList = IOPSCopyPowerSourcesList(sourceInfo);
// Loop through sources, find the first battery
int count = CFArrayGetCount(sourceList);
CFDictionaryRef source = NULL;
for(int i=0; i < count; i++) {
source = IOPSGetPowerSourceDescription(sourceInfo, CFArrayGetValueAtIndex(sourceList, i));
// Is this a battery?
CFStringRef type = (CFStringRef)CFDictionaryGetValue(source, CFSTR(kIOPSTransportTypeKey));
if(kCFCompareEqualTo == CFStringCompare(type, CFSTR(kIOPSInternalType), 0)) {
break;
}
}
float percent = 0;
if(source != NULL) {
int curCapacity;
CFNumberGetValue(CFDictionaryGetValue(source, CFSTR(kIOPSCurrentCapacityKey)), kCFNumberIntType, &curCapacity);
int maxCapacity;
CFNumberGetValue(CFDictionaryGetValue(source, CFSTR(kIOPSMaxCapacityKey)), kCFNumberIntType, &maxCapacity);
percent = curCapacity / (float)maxCapacity*100.f;
}
CFRelease(sourceInfo);
CFRelease(sourceList);
return percent;
}
static void BatteryMeter_setValues(Meter * this, char *buffer, int len) {
double percent = getBatData();
if (percent == 0) {
snprintf(buffer, len, "n/a");
return;
}
this->values[0] = percent;
char *onAcText, *onBatteryText, *unknownText;
unknownText = "%.1f%%";
if (this->mode == TEXT_METERMODE) {
onAcText = "%.1f%% (Running on A/C)";
onBatteryText = "%.1f%% (Running on battery)";
} else {
onAcText = "%.1f%%(A/C)";
onBatteryText = "%.1f%%(bat)";
}
ACPresence isOnLine = chkIsOnline();
if (isOnLine == AC_PRESENT) {
snprintf(buffer, len, onAcText, percent);
} else if (isOnLine == AC_ABSENT) {
snprintf(buffer, len, onBatteryText, percent);
} else {
snprintf(buffer, len, unknownText, percent);
}
return;
}
MeterType BatteryMeter = {
.setValues = BatteryMeter_setValues,
.display = NULL,
.mode = TEXT_METERMODE,
.items = 1,
.total = 100.0,
.attributes = BatteryMeter_attributes,
.name = "Battery",
.uiName = "Battery",
.caption = "Battery: "
};