Added icon colors and makestatitem function
This commit is contained in:
parent
a1b5579c18
commit
30c54729b5
@ -1,6 +1,6 @@
|
|||||||
static const int cycletime = 2; /* time between updates in second */
|
static const int cycletime = 2; /* time between updates in second */
|
||||||
static const char interface[] = "enp4s0"; /* The name of the inteface to display information for */
|
static const char interface[] = "enp4s0"; /* The name of the inteface to display information for */
|
||||||
static const char * icons[] = { /* Array of icons to be used for the status bar segments */
|
static const char * icons[] = { /* Array of icons to be used for the status bar segments */
|
||||||
"", // Icon for date
|
"", // Icon for date
|
||||||
"", // Icon for battery
|
"", // Icon for battery
|
||||||
"", // Icon for CPU usage
|
"", // Icon for CPU usage
|
||||||
@ -12,3 +12,7 @@ static const char * icons[] = { /* Array of icons to be used for the status
|
|||||||
"", // Icon for network information
|
"", // Icon for network information
|
||||||
"" // Icon for music infos
|
"" // Icon for music infos
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static const unsigned int icon_color = 1; /* 1 enables colors for icons (needs DWM status2d patch */
|
||||||
|
static const char icon_colorfg[] = "#eeeeee"; /* The foreground color for icons */
|
||||||
|
static const char icon_colorbg[] = "#408977"; /* The background color for icons */
|
||||||
|
160
cstatbar.c
160
cstatbar.c
@ -19,33 +19,86 @@
|
|||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
/* enums */
|
/* enums */
|
||||||
enum { IconDateTime, IconBattery, IconCPU, IconRAM, IconDisk,
|
enum Icon { IconDateTime, IconBattery, IconCPU, IconRAM, IconDisk,
|
||||||
IconNetSpeed, IconNetSpeedUp, IconNetSpeedDown, IconNetwork, IconMusic };
|
IconNetSpeed, IconNetSpeedUp, IconNetSpeedDown, IconNetwork, IconMusic };
|
||||||
|
|
||||||
/* function declaration */
|
/* function declaration */
|
||||||
|
char *cs(int count, ...);
|
||||||
|
char *getnetworkspeed();
|
||||||
|
char *makestatitem(enum Icon icon, char *val);
|
||||||
|
char *readinfile(char *filename);
|
||||||
void setup();
|
void setup();
|
||||||
void setxroot(char *title);
|
void setxroot(char *title);
|
||||||
char *readinfile(char *filename);
|
|
||||||
char *getnetworkspeed();
|
|
||||||
char *cs(int count, ...);
|
|
||||||
|
|
||||||
/* variables */
|
/* variables */
|
||||||
static const char *iface;
|
static const char *iface;
|
||||||
static const char kbs[] = "kb/s";
|
static const char kbs[] = "kb/s";
|
||||||
|
static unsigned int ic;
|
||||||
|
static const char *icb;
|
||||||
|
static const char *icf;
|
||||||
|
|
||||||
|
|
||||||
/* Uses functionality from Xlib to set the window root name to a specified string */
|
/* a variadic function to take in count number of strings and concatenate them into one single string */
|
||||||
void setxroot(char *title)
|
char * cs(int count, ...)
|
||||||
{
|
{
|
||||||
Display * dpy = XOpenDisplay(NULL);
|
va_list ap;
|
||||||
|
va_start(ap, count);
|
||||||
|
|
||||||
|
int len = 1;
|
||||||
|
for(int i=0; i<count; i++) { len += strlen(va_arg(ap, char*)); }
|
||||||
|
va_end(ap);
|
||||||
|
|
||||||
int screen = DefaultScreen(dpy);
|
char * result = malloc(len * sizeof(char));
|
||||||
Window root = RootWindow(dpy, screen);
|
int pos = 0;
|
||||||
|
|
||||||
XStoreName(dpy, root, title);
|
va_start(ap, count);
|
||||||
XFlush(dpy);
|
for(int i=0; i<count; i++){
|
||||||
|
char * tmp = va_arg(ap, char *);
|
||||||
|
strcpy(result + pos, tmp);
|
||||||
|
pos += strlen(tmp);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
XCloseDisplay(dpy);
|
|
||||||
|
/* get the rx and tx network speed from /sys/ */
|
||||||
|
char * getnetworkspeed()
|
||||||
|
{
|
||||||
|
char *tmp,*R1,*R2,*T1,*T2;
|
||||||
|
char TT[12], RR[12];
|
||||||
|
|
||||||
|
tmp = cs(3, "/sys/class/net/", iface, "/statistics/rx_bytes");
|
||||||
|
R1 = readinfile(tmp);
|
||||||
|
free(tmp);
|
||||||
|
|
||||||
|
tmp = cs(3, "/sys/class/net/", iface, "/statistics/tx_bytes");
|
||||||
|
T1 = readinfile(tmp);
|
||||||
|
free(tmp);
|
||||||
|
|
||||||
|
sleep(1);
|
||||||
|
|
||||||
|
tmp = cs(3, "/sys/class/net/", iface, "/statistics/rx_bytes");
|
||||||
|
R2 = readinfile(tmp);
|
||||||
|
free(tmp);
|
||||||
|
|
||||||
|
tmp = cs(3, "/sys/class/net/", iface, "/statistics/tx_bytes");
|
||||||
|
T2 = readinfile(tmp);
|
||||||
|
free(tmp);
|
||||||
|
|
||||||
|
sprintf(RR, "%d", (atoi(R2) - atoi(R1)) / 1024);
|
||||||
|
sprintf(TT, "%d", (atoi(T2) - atoi(T1)) / 1024);
|
||||||
|
|
||||||
|
return cs(9,
|
||||||
|
icons[IconNetSpeedUp], " ", TT, kbs, " ",
|
||||||
|
icons[IconNetSpeedDown], " ", RR, kbs);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* creates a formatet string for a status item. Adds color if enabled */
|
||||||
|
char * makestatitem(enum Icon icon, char *val){
|
||||||
|
return cs(4, " ",
|
||||||
|
ic ? cs(7 ,"^b", icb, "^^c", icf, "^ ", icons[icon], " ^d^") : icons[icon],
|
||||||
|
" ", val);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -73,65 +126,27 @@ char * readinfile(char *fileName)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
char * getnetworkspeed()
|
/* basic setup method for internal variable etc */
|
||||||
{
|
|
||||||
char *tmp,*result,*R1,*R2,*T1,*T2;
|
|
||||||
char TT[12], RR[12];
|
|
||||||
|
|
||||||
tmp = cs(3, "/sys/class/net/", iface, "/statistics/rx_bytes");
|
|
||||||
R1 = readinfile(tmp);
|
|
||||||
free(tmp);
|
|
||||||
|
|
||||||
tmp = cs(3, "/sys/class/net/", iface, "/statistics/tx_bytes");
|
|
||||||
T1 = readinfile(tmp);
|
|
||||||
free(tmp);
|
|
||||||
|
|
||||||
sleep(1);
|
|
||||||
|
|
||||||
tmp = cs(3, "/sys/class/net/", iface, "/statistics/rx_bytes");
|
|
||||||
R2 = readinfile(tmp);
|
|
||||||
free(tmp);
|
|
||||||
|
|
||||||
tmp = cs(3, "/sys/class/net/", iface, "/statistics/tx_bytes");
|
|
||||||
T2 = readinfile(tmp);
|
|
||||||
free(tmp);
|
|
||||||
|
|
||||||
sprintf(RR, "%d", (atoi(R2) - atoi(R1)) / 1024);
|
|
||||||
sprintf(TT, "%d", (atoi(T2) - atoi(T1)) / 1024);
|
|
||||||
|
|
||||||
result = cs(11, icons[IconNetSpeed], " ",
|
|
||||||
icons[IconNetSpeedUp], " ", TT, kbs, " ",
|
|
||||||
icons[IconNetSpeedDown], " ", RR, kbs);
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* a variadic function to take in count number of strings and concatenate them into one single string */
|
|
||||||
char * cs(int count, ...)
|
|
||||||
{
|
|
||||||
va_list ap;
|
|
||||||
va_start(ap, count);
|
|
||||||
|
|
||||||
int len = 1;
|
|
||||||
for(int i=0; i<count; i++) { len += strlen(va_arg(ap, char*)); }
|
|
||||||
va_end(ap);
|
|
||||||
|
|
||||||
char * result = malloc(len * sizeof(char));
|
|
||||||
int pos = 0;
|
|
||||||
|
|
||||||
va_start(ap, count);
|
|
||||||
for(int i=0; i<count; i++){
|
|
||||||
char * tmp = va_arg(ap, char *);
|
|
||||||
strcpy(result + pos, tmp);
|
|
||||||
pos += strlen(tmp);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void setup(){
|
void setup(){
|
||||||
iface = interface;
|
iface = interface;
|
||||||
|
ic = icon_color;
|
||||||
|
icb = icon_colorbg;
|
||||||
|
icf = icon_colorfg;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* Uses functionality from Xlib to set the window root name to a specified string */
|
||||||
|
void setxroot(char *title)
|
||||||
|
{
|
||||||
|
Display * dpy = XOpenDisplay(NULL);
|
||||||
|
|
||||||
|
int screen = DefaultScreen(dpy);
|
||||||
|
Window root = RootWindow(dpy, screen);
|
||||||
|
|
||||||
|
XStoreName(dpy, root, title);
|
||||||
|
XFlush(dpy);
|
||||||
|
|
||||||
|
XCloseDisplay(dpy);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -140,8 +155,9 @@ int main(int argc, char *argv[])
|
|||||||
setup();
|
setup();
|
||||||
|
|
||||||
while(1) {
|
while(1) {
|
||||||
//setxroot(name);
|
char * ns = makestatitem(IconNetSpeed, getnetworkspeed());
|
||||||
printf("%s\n", getnetworkspeed());
|
|
||||||
|
setxroot(ns);
|
||||||
|
|
||||||
sleep(cycletime);
|
sleep(cycletime);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user