Commit 140b270eec0705018b5e5a2f628f5e5c7a3108e0
1 parent
d8824a52
Exists in
master
add support for linux with 2 batteries
Showing
1 changed file
with
81 additions
and
32 deletions
Show diff stats
battery.c
| 1 | -#ifndef __FreeBSD__ | ||
| 2 | - #error Only works on FreeBSD | ||
| 3 | -#endif | ||
| 4 | - | ||
| 5 | #include <stdio.h> | 1 | #include <stdio.h> |
| 6 | #include <stdlib.h> | 2 | #include <stdlib.h> |
| 3 | +#include <string.h> | ||
| 7 | #include <sys/types.h> | 4 | #include <sys/types.h> |
| 8 | -#include <sys/sysctl.h> | ||
| 9 | 5 | ||
| 10 | -const char *bat_icons[] = { | 6 | +const char *icon[] = { |
| 11 | "\uf58d", /* 0% */ | 7 | "\uf58d", /* 0% */ |
| 12 | "\uf579", | 8 | "\uf579", |
| 13 | "\uf57a", | 9 | "\uf57a", |
| @@ -21,36 +17,89 @@ const char *bat_icons[] = { | @@ -21,36 +17,89 @@ const char *bat_icons[] = { | ||
| 21 | "\uf578", /* 100% */ | 17 | "\uf578", /* 100% */ |
| 22 | }; | 18 | }; |
| 23 | 19 | ||
| 24 | -int main() { | ||
| 25 | - int life, state; | 20 | +typedef enum { |
| 21 | + CHARGED = 0, | ||
| 22 | + DISCHARGING = 1, | ||
| 23 | + CHARGING = 2, | ||
| 24 | + DISCONNECTED = 7, | ||
| 25 | +} State; | ||
| 26 | + | ||
| 27 | +#if defined (__linux__) | ||
| 28 | +void battery_state(int *nbats, int bat[], State state[]) { | ||
| 29 | + FILE *fd; | ||
| 30 | + int i=0; | ||
| 31 | + do { | ||
| 32 | + char filename[50], status[50]; | ||
| 33 | + | ||
| 34 | + // get current capacity | ||
| 35 | + sprintf(filename, "/sys/class/power_supply/BAT%d/capacity", i); | ||
| 36 | + fd = fopen(filename, "rb"); | ||
| 37 | + if (fd == NULL) | ||
| 38 | + break; | ||
| 39 | + fscanf(fd, "%d", &bat[i]); | ||
| 40 | + fclose(fd); | ||
| 41 | + // battery status (charging...) | ||
| 42 | + sprintf(filename, "/sys/class/power_supply/BAT%d/status", i); | ||
| 43 | + fd = fopen(filename, "rb"); | ||
| 44 | + fgets(status, 50, fd); | ||
| 45 | + // puts(status); | ||
| 46 | + fclose(fd); | ||
| 47 | + if (!strcmp(status, "Not charging\n")) | ||
| 48 | + state[i] = CHARGED; | ||
| 49 | + else if (!strcmp(status, "Discharging\n")) | ||
| 50 | + state[i] = DISCHARGING; | ||
| 51 | + else if (!strcmp(status, "Charging\n")) | ||
| 52 | + state[i] = CHARGING; | ||
| 53 | + | ||
| 54 | + // next | ||
| 55 | + i++; | ||
| 56 | + } while (1); | ||
| 57 | + *nbats = i; | ||
| 58 | +} | ||
| 59 | +#elif defined (__FreeBSD__) | ||
| 60 | +#include <sys/sysctl.h> | ||
| 61 | +void battery_state(int *nbats, int bat[], State state[]) { | ||
| 26 | size_t len = sizeof(int); | 62 | size_t len = sizeof(int); |
| 63 | + int state = -1, life = 0; | ||
| 27 | 64 | ||
| 28 | if (sysctlbyname("hw.acpi.battery.state", &state, &len, NULL, 0) || | 65 | if (sysctlbyname("hw.acpi.battery.state", &state, &len, NULL, 0) || |
| 29 | - sysctlbyname("hw.acpi.battery.life", &life, &len, NULL, 0)) { | ||
| 30 | - puts("(no battery)"); | ||
| 31 | - exit(EXIT_FAILURE); | ||
| 32 | - } | 66 | + sysctlbyname("hw.acpi.battery.life", &life, &len, NULL, 0)) |
| 67 | + state[0] = UNKNOWN; | ||
| 68 | + | ||
| 69 | + *nbats = 1; | ||
| 70 | + bat[0] = life; | ||
| 71 | + state[0] = state; | ||
| 72 | +} | ||
| 73 | +#endif | ||
| 74 | + | ||
| 75 | +int main() { | ||
| 76 | + char *color; | ||
| 77 | + int life[2], nbats; | ||
| 78 | + State state[2]; | ||
| 79 | + | ||
| 80 | + battery_state(&nbats, life, state); | ||
| 81 | + for (int i=0; i < nbats; i++) { | ||
| 82 | + | ||
| 83 | + if (life[i] >= 50) color = "colour028"; /* green */ | ||
| 84 | + else if (life[i] >= 25) color = "colour172"; /* yellow */ | ||
| 85 | + else color = "colour088"; /* red */ | ||
| 33 | 86 | ||
| 34 | - if (life > 100) | ||
| 35 | - life = 100; | ||
| 36 | - else if (life < 0) | ||
| 37 | - life = 0; | ||
| 38 | - | ||
| 39 | - switch (state) { | ||
| 40 | - case 0: /* fully charged */ | ||
| 41 | - printf("\uf583 Full"); | ||
| 42 | - break; | ||
| 43 | - case 1: /* discharging */ | ||
| 44 | - printf("%s %d%%\n", bat_icons[life / 10], life); | ||
| 45 | - break; | ||
| 46 | - case 2: /* charging */ | ||
| 47 | - printf("\uf583 %d%%\n", life); | ||
| 48 | - break; | ||
| 49 | - case 7: /* disconnected */ | ||
| 50 | - printf("\uf492"); | ||
| 51 | - break; | ||
| 52 | - default: /* unknown code */ | ||
| 53 | - printf("\uf590"); | 87 | + switch (state[i]) { |
| 88 | + case CHARGED: /* fully charged */ | ||
| 89 | + printf("%s %d%% ", icon[life[i]/10], life[i]); | ||
| 90 | + break; | ||
| 91 | + case DISCHARGING: /* discharging */ | ||
| 92 | + printf("#[fg=%s]%s#[fg=default] %d%% ", color, icon[life[i]/10], life[i]); | ||
| 93 | + break; | ||
| 94 | + case CHARGING: /* charging */ | ||
| 95 | + printf("#[fg=yellow]\uf583#[fg=default] %d%%\n ", life[i]); | ||
| 96 | + break; | ||
| 97 | + case DISCONNECTED: /* disconnected */ | ||
| 98 | + printf("\uf492 "); | ||
| 99 | + break; | ||
| 100 | + default: /* unknown code */ | ||
| 101 | + printf("\uf590 "); | ||
| 102 | + } | ||
| 54 | } | 103 | } |
| 55 | 104 | ||
| 56 | return EXIT_SUCCESS; | 105 | return EXIT_SUCCESS; |