Blame view

battery.c 2.53 KB
ce4638ed   Miguel Barão   Initial commit
1
2
#include <stdio.h>
#include <stdlib.h>
140b270e   Miguel Barão   add support for l...
3
#include <string.h>
ce4638ed   Miguel Barão   Initial commit
4
#include <sys/types.h>
407581b9   Miguel Barão   use a different s...
5
6
7

const char *icon[] = {
    "\uf58d",                                         /* 0% */
ce4638ed   Miguel Barão   Initial commit
8
    "\uf579", "\uf57a", "\uf57b", "\uf57c", "\uf57d", /* 50% */
fbcc53ca   Miguel Barão   refactor
9
10
    "\uf57e", "\uf57f", "\uf580", "\uf581", "\uf578", /* 100% */
};
ce4638ed   Miguel Barão   Initial commit
11

407581b9   Miguel Barão   use a different s...
12
typedef enum {
cb150c6a   Miguel Barão   fix icons
13
  CHARGED = 0,
407581b9   Miguel Barão   use a different s...
14
15
16
  DISCHARGING = 1,
  CHARGING = 2,
  DISCONNECTED = 7,
cb150c6a   Miguel Barão   fix icons
17
  UNKNOWN = -1
ae8c717f   Miguel Barão   fix icons on linux
18
} State;
cb150c6a   Miguel Barão   fix icons
19

407581b9   Miguel Barão   use a different s...
20
#if defined(__linux__)
140b270e   Miguel Barão   add support for l...
21
22
23
24
25
void battery_state(int *nbats, int bat[], State state[]) {
  FILE *fd;
  int i = 0;
  do {
    char filename[50], status[50];
6e56c017   Miguel Barão   fix freebsd version
26

140b270e   Miguel Barão   add support for l...
27
28
    // get current capacity
    sprintf(filename, "/sys/class/power_supply/BAT%d/capacity", i);
cb150c6a   Miguel Barão   fix icons
29
    fd = fopen(filename, "rb");
fbcc53ca   Miguel Barão   refactor
30
    if (fd == NULL)
140b270e   Miguel Barão   add support for l...
31
      break;
fbcc53ca   Miguel Barão   refactor
32
    fscanf(fd, "%d", &bat[i]);
140b270e   Miguel Barão   add support for l...
33
    fclose(fd);
fbcc53ca   Miguel Barão   refactor
34
35
36
    // battery status (charging...)
    sprintf(filename, "/sys/class/power_supply/BAT%d/status", i);
    fd = fopen(filename, "rb");
140b270e   Miguel Barão   add support for l...
37
    fgets(status, 50, fd);
6e56c017   Miguel Barão   fix freebsd version
38
    // puts(status);
140b270e   Miguel Barão   add support for l...
39
40
41
    fclose(fd);
    if (!strcmp(status, "Not charging\n"))
      state[i] = CHARGED;
6e56c017   Miguel Barão   fix freebsd version
42
    else if (!strcmp(status, "Discharging\n"))
140b270e   Miguel Barão   add support for l...
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
      state[i] = DISCHARGING;
    else if (!strcmp(status, "Charging\n"))
      state[i] = CHARGING;

    // next
    i++;
  } while (1);
  *nbats = i;
}
#elif defined(__FreeBSD__)
#include <sys/sysctl.h>
void battery_state(int *nbats, int bat[], State state[]) {
  size_t len = sizeof(int);
  int status, life = 0;

7ad151d0   Miguel Barão   add UNKNOWN to li...
58
59
  *nbats = 1; // FIXME: deal with multiple batteries

fbcc53ca   Miguel Barão   refactor
60
  if (sysctlbyname("hw.acpi.battery.state", &status, &len, NULL, 0) ||
140b270e   Miguel Barão   add support for l...
61
      sysctlbyname("hw.acpi.battery.life", &life, &len, NULL, 0)) {
6e56c017   Miguel Barão   fix freebsd version
62
    state[0] = UNKNOWN;
ce4638ed   Miguel Barão   Initial commit
63
    bat[0] = 0;
6e56c017   Miguel Barão   fix freebsd version
64
  } else {
ce4638ed   Miguel Barão   Initial commit
65
    bat[0] = life;
6e56c017   Miguel Barão   fix freebsd version
66
    state[0] = status;
140b270e   Miguel Barão   add support for l...
67
  }
6e56c017   Miguel Barão   fix freebsd version
68
69
70
71
72
73
74
75
}
#endif

int main() {
  char *color;
  int life[2], nbats;
  State state[2];

fbcc53ca   Miguel Barão   refactor
76
77
  battery_state(&nbats, life, state);
  for (int i = 0; i < nbats; i++) {
140b270e   Miguel Barão   add support for l...
78

fbcc53ca   Miguel Barão   refactor
79
    if (life[i] >= 50)
140b270e   Miguel Barão   add support for l...
80
81
82
83
84
85
86
      color = "colour028"; /* green */
    else if (life[i] >= 25)
      color = "colour172"; /* yellow */
    else
      color = "colour088"; /* red */

    switch (state[i]) {
fbcc53ca   Miguel Barão   refactor
87
    case CHARGED: /* fully charged */
6e56c017   Miguel Barão   fix freebsd version
88
      printf("%s %d%%  ", icon[life[i] / 10], life[i]);
fbcc53ca   Miguel Barão   refactor
89
90
      break;
    case DISCHARGING: /* discharging */
140b270e   Miguel Barão   add support for l...
91
      printf("#[fg=%s]%s#[fg=default] %d%%  ", color, icon[life[i] / 10],
140b270e   Miguel Barão   add support for l...
92
             life[i]);
fbcc53ca   Miguel Barão   refactor
93
94
      break;
    case CHARGING: /* charging */
140b270e   Miguel Barão   add support for l...
95
96
      printf("#[fg=yellow]\uf583#[fg=default] %d%%\n  ", life[i]);
      break;
c53116a8   Miguel Barão   fix charging icon...
97
98
99
100
101
    case DISCONNECTED: /* disconnected */
      printf("\uf492  ");
      break;
    default: /* unknown code */
      printf("\uf590  ");
8e1ef252   Miguel Barão   update to light r...
102
    }
c53116a8   Miguel Barão   fix charging icon...
103
  }
fbcc53ca   Miguel Barão   refactor
104

6e56c017   Miguel Barão   fix freebsd version
105
  return EXIT_SUCCESS;
140b270e   Miguel Barão   add support for l...
106
}
cb150c6a   Miguel Barão   fix icons

140b270e   Miguel Barão   add support for l...

cb150c6a   Miguel Barão   fix icons

140b270e   Miguel Barão   add support for l...

cb150c6a   Miguel Barão   fix icons

140b270e   Miguel Barão   add support for l...

ce4638ed   Miguel Barão   Initial commit

7887053e   Miguel Barão   Unknown and plugg...

ce4638ed   Miguel Barão   Initial commit