summaryrefslogtreecommitdiff
path: root/src/config_cpu.c
blob: 486af106800dbb6581a4b82dfcf532ab82fb823e (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_cpu.h"
   19 
   20 void config_cpu_init(struct node* n) {
   21   n->data = NULL;
   22   n->type = CTYPE_CPU;
   23   n->loadfunc = &config_cpu_load;
   24   n->loadkey = &load_cpu_key;
   25 }
   26 
   27 void load_cpu_key(struct node* n, char* key, char* val) {
   28 }
   29 
   30 int config_cpu_load(struct node* n) {
   31   double avgs[1] = {0};
   32   double level = 0;
   33   int nproc = 0;
   34 
   35   nproc = get_nprocs();
   36   getloadavg(avgs, 1);
   37 
   38   if(n->label[0] == '\0')
   39     strcpy(n->label, "cpu:");
   40 
   41   // Get level
   42   if(nproc < avgs[0]) {
   43     level = 1;
   44   } else {
   45     level = avgs[0] / nproc;
   46   }
   47 
   48   // Set color
   49   if(level > .8) {
   50     strcpy(n->color, C_RED);
   51   } else if(level > .5) {
   52     strcpy(n->color, C_ORANGE);
   53   } else if(level > .3) {
   54     strcpy(n->color, C_YELLOW);
   55   } else {
   56     strcpy(n->color, C_GREEN);
   57   }
   58 
   59   if(strcmp(n->display, "bar") == 0) {
   60     print_bar(12, (float) level, n->text);
   61   } else {
   62     // Default to percent display
   63     sprintf(n->text, "%02d%%", (int)(level * 100));
   64   }
   65   return 0;
   66 }

Generated by cgit