summaryrefslogtreecommitdiff
path: root/src/config_bat.c
blob: 1b1e5814e619de257dccda9d86917eb9a3e33db4 (plain)
    1 /**
    2  * I3cstatus prints a configurable status bar for the i3 window manager
    3  * Copyright (C) 2021 Aaron Ball <nullspoon@oper.io>
    4  * 
    5  * This program is free software: you can redistribute it and/or modify
    6  * it under the terms of the GNU General Public License as published by
    7  * the Free Software Foundation, either version 3 of the License, or
    8  * (at your option) any later version.
    9  * 
   10  * This program is distributed in the hope that it will be useful,
   11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13  * GNU General Public License for more details.
   14  * 
   15  * You should have received a copy of the GNU General Public License
   16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
   17  */
   18 #include "config_bat.h"
   19 
   20 void config_bat_init(struct node* n) {
   21   n->data = malloc(sizeof(struct config_bat));
   22   n->type = CTYPE_BAT;
   23   n->loadfunc = &config_bat_load;
   24   n->loadkey = &load_bat_key;
   25 }
   26 
   27 long _fgetl(char* path) {
   28   char buf[256] = {'\0'};
   29   FILE* fd;
   30 
   31   fd = fopen(path, "r");
   32   fgets(buf, 256, fd);
   33   fclose(fd);
   34 
   35   return strtol(buf, NULL, 10);
   36 }
   37 
   38 int _bat_is_charging(char* bat) {
   39   char buf[256] = {'\0'};
   40   FILE* fd      = fopen(bat, "r");
   41   fgets(buf, 256, fd);
   42   fclose(fd);
   43 
   44   if(strncmp(buf, "Discharging", 11) == 0)
   45     return 0;
   46   return 1;
   47 }
   48 
   49 
   50 int config_bat_load(struct node* n) {
   51   long level;
   52   char batstatus[256];
   53   char batcapacity[256];
   54 
   55   sprintf(batstatus, "/sys/class/power_supply/%s/status", ((struct config_bat*)n->data)->name);
   56   sprintf(batcapacity, "/sys/class/power_supply/%s/capacity", ((struct config_bat*)n->data)->name);
   57 
   58   level = _fgetl(batcapacity);
   59 
   60   if(level > 70) {
   61     strcpy(n->color, C_GREEN);
   62   } else if(level > 50) {
   63     strcpy(n->color, C_YELLOW);
   64   } else if(level > 30) {
   65     strcpy(n->color, C_ORANGE);
   66   } else {
   67     strcpy(n->color, C_RED);
   68   }
   69 
   70   if(_bat_is_charging(batstatus) == 1) {
   71     sprintf(n->text, "%lc", 0x26A1);
   72   } else {
   73     strcpy(n->text, " ");
   74   }
   75 
   76   if(strcmp(n->display, "bar") == 0) {
   77     print_bar(n->width, (float) level / 100, &n->text[strlen(n->text)]);
   78   } else {
   79     sprintf(n->text, "%02d%%", (int)(level / 100));
   80   }
   81 
   82   return 0;
   83 }
   84 
   85 void load_bat_key(struct node* n, char* key, char* val) {
   86   if(strcmp(key, "name") == 0)
   87     strcpy(((struct config_bat*)n->data)->name, val);
   88 }

Generated by cgit