Blame view

battery.c 2.58 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;
    else
      state[i] = UNKNOWN;

    // 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);
7ad151d0   Miguel Barão   add UNKNOWN to li...
58
59
  int status, life = 0;

fbcc53ca   Miguel Barão   refactor
60
  *nbats = 1; // FIXME: deal with multiple batteries
140b270e   Miguel Barão   add support for l...
61

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

int main() {
  char *color;
  int life[2], nbats;
fbcc53ca   Miguel Barão   refactor
76
77
  State state[2];

140b270e   Miguel Barão   add support for l...
78
  battery_state(&nbats, life, state);
fbcc53ca   Miguel Barão   refactor
79
  for (int i = 0; i < nbats; i++) {
140b270e   Miguel Barão   add support for l...
80
81
82
83
84
85
86

    switch (state[i]) {
    case CHARGED: /* fully charged */
      printf("%s %d%%  ", icon[life[i] / 10], life[i]);
      break;
    case DISCHARGING: /* discharging */
      if (life[i] >= 50)
fbcc53ca   Miguel Barão   refactor
87
        color = "colour028"; /* green */
6e56c017   Miguel Barão   fix freebsd version
88
      else if (life[i] >= 25)
fbcc53ca   Miguel Barão   refactor
89
90
        color = "colour172"; /* yellow */
      else
140b270e   Miguel Barão   add support for l...
91
        color = "colour196"; /* red */
140b270e   Miguel Barão   add support for l...
92

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

  return EXIT_SUCCESS;
cb150c6a   Miguel Barão   fix icons
108
}
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