BatteryMeter.c 1.74 KB
Newer Older
Hisham Muhammad's avatar
Hisham Muhammad committed
1
/*
Hisham Muhammad's avatar
Hisham Muhammad committed
2
3
4
5
htop - BatteryMeter.c
(C) 2004-2011 Hisham H. Muhammad
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
Hisham Muhammad's avatar
Hisham Muhammad committed
6

Hisham Muhammad's avatar
Hisham Muhammad committed
7
This meter written by Ian P. Hands (iphands@gmail.com, ihands@redhat.com).
Hisham Muhammad's avatar
Hisham Muhammad committed
8
9
10
*/

#include "BatteryMeter.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
11

12
#include "Battery.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
13
14
#include "ProcessList.h"
#include "CRT.h"
David Hunt's avatar
David Hunt committed
15
#include "StringUtils.h"
16
#include "Platform.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
17

Hisham Muhammad's avatar
Hisham Muhammad committed
18
19
20
#include <string.h>
#include <stdlib.h>

21
/*{
Hisham Muhammad's avatar
Hisham Muhammad committed
22
#include "Meter.h"
23
24
25
26
27
28
29
30

typedef enum ACPresence_ {
   AC_ABSENT,
   AC_PRESENT,
   AC_ERROR
} ACPresence;
}*/

Hisham Muhammad's avatar
Hisham Muhammad committed
31
32
33
34
int BatteryMeter_attributes[] = {
   BATTERY
};

35
static void BatteryMeter_updateValues(Meter * this, char *buffer, int len) {
36
37
38
   ACPresence isOnAC;
   double percent;
   
39
   Battery_getData(&percent, &isOnAC);
40
41
42
43
44

   if (percent == -1) {
      this->values[0] = 0;
      snprintf(buffer, len, "n/a");
      return;
45
46
   }

Hisham Muhammad's avatar
Hisham Muhammad committed
47
48
   this->values[0] = percent;

Hisham Muhammad's avatar
Hisham Muhammad committed
49
   const char *onAcText, *onBatteryText, *unknownText;
50
51

   unknownText = "%.1f%%";
Hisham Muhammad's avatar
Hisham Muhammad committed
52
53
54
55
56
57
58
59
   if (this->mode == TEXT_METERMODE) {
      onAcText = "%.1f%% (Running on A/C)";
      onBatteryText = "%.1f%% (Running on battery)";
   } else {
      onAcText = "%.1f%%(A/C)";
      onBatteryText = "%.1f%%(bat)";
   }

60
   if (isOnAC == AC_PRESENT) {
Hisham Muhammad's avatar
Hisham Muhammad committed
61
      snprintf(buffer, len, onAcText, percent);
62
   } else if (isOnAC == AC_ABSENT) {
63
      snprintf(buffer, len, onBatteryText, percent);
Hisham Muhammad's avatar
Hisham Muhammad committed
64
   } else {
65
      snprintf(buffer, len, unknownText, percent);
Hisham Muhammad's avatar
Hisham Muhammad committed
66
67
68
69
70
   }

   return;
}

71
72
73
74
75
MeterClass BatteryMeter_class = {
   .super = {
      .extends = Class(Meter),
      .delete = Meter_delete
   },
76
   .updateValues = BatteryMeter_updateValues,
77
   .defaultMode = TEXT_METERMODE,
78
   .maxItems = 1,
Hisham Muhammad's avatar
Hisham Muhammad committed
79
80
81
82
83
84
   .total = 100.0,
   .attributes = BatteryMeter_attributes,
   .name = "Battery",
   .uiName = "Battery",
   .caption = "Battery: "
};