Blame view

battery.c 2.41 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",
fbcc53ca   Miguel Barão   refactor
9
10
    "\uf57a",
    "\uf57b",
ce4638ed   Miguel Barão   Initial commit
11
    "\uf57c",
407581b9   Miguel Barão   use a different s...
12
    "\uf57d",  /* 50% */
cb150c6a   Miguel Barão   fix icons
13
    "\uf57e",
407581b9   Miguel Barão   use a different s...
14
15
16
    "\uf57f",
    "\uf580",
    "\uf581",
cb150c6a   Miguel Barão   fix icons
17
    "\uf578",  /* 100% */
ae8c717f   Miguel Barão   fix icons on linux
18
};
cb150c6a   Miguel Barão   fix icons
19

407581b9   Miguel Barão   use a different s...
20
typedef enum {
140b270e   Miguel Barão   add support for l...
21
22
23
24
25
  CHARGED = 0,
  DISCHARGING = 1,
  CHARGING = 2,
  DISCONNECTED = 7,
} State;
6e56c017   Miguel Barão   fix freebsd version
26

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

    // next
    i++;
  } while (1);
  *nbats = i;
7ad151d0   Miguel Barão   add UNKNOWN to li...
58
59
}
#elif defined (__FreeBSD__)
fbcc53ca   Miguel Barão   refactor
60
#include <sys/sysctl.h>
140b270e   Miguel Barão   add support for l...
61
void battery_state(int *nbats, int bat[], State state[]) {
6e56c017   Miguel Barão   fix freebsd version
62
  size_t len = sizeof(int);
ce4638ed   Miguel Barão   Initial commit
63
  int state = -1, life = 0;
6e56c017   Miguel Barão   fix freebsd version
64

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

  *nbats = 1;
  bat[0] = life;
  state[0] = state;
}
#endif

int main() {
fbcc53ca   Miguel Barão   refactor
76
77
  char *color;
  int life[2], nbats;
140b270e   Miguel Barão   add support for l...
78
  State state[2];
fbcc53ca   Miguel Barão   refactor
79

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

    if (life[i] >= 50) color = "colour028"; /* green */
    else if (life[i] >= 25) color = "colour172"; /* yellow */
    else color = "colour088"; /* red */

fbcc53ca   Miguel Barão   refactor
87
    switch (state[i]) {
6e56c017   Miguel Barão   fix freebsd version
88
    case CHARGED: /* fully charged */
fbcc53ca   Miguel Barão   refactor
89
90
      printf("%s %d%%  ", icon[life[i]/10], life[i]);
      break;
140b270e   Miguel Barão   add support for l...
91
    case DISCHARGING: /* discharging */
140b270e   Miguel Barão   add support for l...
92
      printf("#[fg=%s]%s#[fg=default] %d%%  ", color, icon[life[i]/10], 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