dotfiles

personal configuration files and scripts
git clone https://tongong.net/git/dotfiles.git
Log | Files | Refs | README

battery.c (1154B)


      1 #include <stdio.h>
      2 #include <string.h>
      3 
      4 #include "../util.h"
      5 #include "battery.h"
      6 
      7 #define ICON         " "
      8 #define ICON_CHARGE  " "
      9 #define ICON_WARNING " "
     10 
     11 #define CAPACITYFILE        "/sys/class/power_supply/BAT0/capacity"
     12 #define STATUSFILE          "/sys/class/power_supply/BAT0/status"
     13 #define MAX_CHARS_PER_LINE  20
     14 
     15 size_t batteryu(char *str, int sigval)
     16 {
     17     int capacity;
     18     _Bool charging;
     19 
     20     char tmpstring[MAX_CHARS_PER_LINE];
     21 
     22     FILE *statfile;
     23 
     24     statfile = fopen(CAPACITYFILE, "r");
     25     fgets(tmpstring, MAX_CHARS_PER_LINE, statfile);
     26     fclose(statfile);
     27     sscanf(tmpstring, "%i", &capacity);
     28 
     29     statfile = fopen(STATUSFILE, "r");
     30     fgets(tmpstring, MAX_CHARS_PER_LINE, statfile);
     31     fclose(statfile);
     32     charging = !strcmp(tmpstring, "Charging\n");
     33 
     34     if (charging) {
     35         return SPRINTF(str, ICON_CHARGE "%03d%%", capacity);
     36     } else {
     37         char *warn = ICON_WARNING "LOW BATTERY ";
     38         if (capacity > 10) warn = "";
     39         return SPRINTF(str, "%s" ICON "%03d%%", warn, capacity);
     40     }
     41 }
     42 
     43 void batteryc(int button)
     44 {
     45     cspawn((char *[]){ "sh", "-c", "notify-send \"$(acpi -b)\"", NULL});
     46 }