BatteryMeter.c 1.69 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 "Platform.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
13
14
15
16
#include "ProcessList.h"
#include "CRT.h"
#include "String.h"

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

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

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

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

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

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

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

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

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

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

   return;
}

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