*nohtml*

local/bin/dwm-status

   1 //usr/bin/cc -xc $0 -lX11 -o$0~ && exec $0~
   2 
   3 #include <stdio.h>
   4 #include <unistd.h>
   5 #include <time.h>
   6 #include <wordexp.h>
   7 #include <X11/XKBlib.h>
   8 
   9 size_t readfile(const char *path){ // {{{
  10     size_t result = 0;
  11 
  12     wordexp_t p;
  13     wordexp(path, &p, 0);
  14 
  15     for (int i = 0; i < p.we_wordc; i++){
  16         FILE *fp;
  17         size_t data;
  18 
  19         if ((fp = fopen(p.we_wordv[i], "r")) == NULL)
  20             continue;
  21 
  22         fscanf(fp, "%zu", &data);
  23         fclose(fp);
  24 
  25         result += data;
  26     }
  27 
  28     wordfree(&p);
  29     return result;
  30 } // }}}
  31 void network(char *dl, char *up){ // {{{
  32     static size_t rxn = 0, txn = 0;
  33     size_t rxw = rxn, txw = txn;
  34 
  35     sprintf(dl, "0 KB");
  36     sprintf(up, "0 KB");
  37 
  38     rxn = readfile("/sys/class/net/*/statistics/rx_bytes");
  39     txn = readfile("/sys/class/net/*/statistics/tx_bytes");
  40 
  41     if (!rxw || !txw)
  42         return;
  43 
  44     int i, j;
  45     size_t rxd = (rxn - rxw);
  46     size_t txd = (txn - txw);
  47     const char* map[] = {"B", "KB", "MB"};
  48 
  49     for (i = 0; rxd >= 1024; i++, rxd /= 1024);
  50     for (j = 0; txd >= 1024; j++, txd /= 1024);
  51 
  52     if (i > 0) sprintf(dl, "%zu %s", rxd, map[i]);
  53     if (j > 0) sprintf(up, "%zu %s", txd, map[j]);
  54 } // }}}
  55 void volumebar(char *buf, int len){ // {{{
  56     size_t pos = (len * readfile( "~/.cache/volume") / 100);
  57 
  58     if (pos > len)
  59         return;
  60 
  61     int i;
  62     char bar[100] = "";
  63 
  64     for (i = 0; i < len; i++)
  65         bar[i] = (i < pos ? '=' : '-');
  66 
  67     bar[i] = '-';
  68     bar[pos] = '|';
  69 
  70     sprintf(buf, "%s", bar);
  71 } // }}}
  72 
  73 int main(int argc, char **argv){
  74     Display *dpy = XOpenDisplay(NULL);
  75 
  76     char status[1000] = "";
  77 
  78     XkbStateRec state;
  79     char vol[100] = "";
  80     char wet[100] = "";
  81 
  82     time_t tm;
  83     char date[100] = "";
  84 
  85     char net_dl[100] = "";
  86     char net_up[100] = "";
  87 
  88     for (int timer = 0;; timer++){
  89         volumebar(vol, 20);
  90         XkbGetState(dpy, XkbUseCoreKbd, &state);
  91 
  92         if (timer % 10 == 0){
  93             tm = time(NULL);
  94             strftime(date, sizeof(date), "%T", localtime(&tm));
  95 
  96             network(net_dl, net_up);
  97             sprintf(wet, "%zu°C", readfile("~/.cache/weather"));
  98         }
  99 
 100         sprintf(status, "  %s %s/s %s/s  %s   %s   %s ",
 101             wet, net_dl, net_up, vol, (state.group == 0 ? "En" : "Ru"), date
 102         );
 103 
 104         printf("%s\n", status);
 105         XStoreName(dpy, DefaultRootWindow(dpy), status);
 106 
 107         fflush(stdout);
 108         usleep(100000);
 109     }
 110 
 111     XCloseDisplay(dpy);
 112 }
 113 
 114 // vim: ft=c