[download]

local/bin/dwm-status

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