From 65e8f879efa651ae8c912b63d9e7f8f293fca4cb Mon Sep 17 00:00:00 2001 From: _N0x Date: Tue, 19 Oct 2021 17:29:12 +0200 Subject: [PATCH] First testing draft of the program. Displays the up and download speed --- Makefile | 32 +++++++++++ config.def.h | 14 +++++ config.h | 14 +++++ cstatbar.c | 148 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 208 insertions(+) create mode 100644 Makefile create mode 100644 config.def.h create mode 100644 config.h create mode 100644 cstatbar.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3c2432e --- /dev/null +++ b/Makefile @@ -0,0 +1,32 @@ +# cStatBar Makefile + +SRC = cstatbar.c +OBJ = ${SRC:.c=.o} +CC = cc +CFLAGS = -std=c99 -g +LDFLAGS = -lX11 + + +all: options cstatbar + +options: + @echo cstatusbar build options are as follow: + @echo "CFLAGS = ${CFLAGS}" + @echo "LDFLAGS = ${LDFLAGS}" + @echo "CC = ${CC}" + +.c.o: + ${CC} -c ${CFLAGS} $< + +${OBJ}: config.h + +config.h: + cp config.def.h $@ + +cstatbar: ${OBJ} + ${CC} -o $@ ${OBJ} ${LDFLAGS} + +clean: + rm -f cstatbar ${OBJ} + +.PHONY: all options clean diff --git a/config.def.h b/config.def.h new file mode 100644 index 0000000..478aac7 --- /dev/null +++ b/config.def.h @@ -0,0 +1,14 @@ +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 * icons[] = { /* Array of icons to be used for the status bar segments */ + "", // Icon for date + "", // Icon for battery + "", // Icon for CPU usage + "", // Icon for RAM usage + "", // Icon for disk + "龍", // Icon for network speed + "", // Icon for upload speed + "", // Icon for download speed + "", // Icon for network information + "" // Icon for music infos +}; diff --git a/config.h b/config.h new file mode 100644 index 0000000..478aac7 --- /dev/null +++ b/config.h @@ -0,0 +1,14 @@ +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 * icons[] = { /* Array of icons to be used for the status bar segments */ + "", // Icon for date + "", // Icon for battery + "", // Icon for CPU usage + "", // Icon for RAM usage + "", // Icon for disk + "龍", // Icon for network speed + "", // Icon for upload speed + "", // Icon for download speed + "", // Icon for network information + "" // Icon for music infos +}; diff --git a/cstatbar.c b/cstatbar.c new file mode 100644 index 0000000..6c1b4ef --- /dev/null +++ b/cstatbar.c @@ -0,0 +1,148 @@ +/* cStatBar + * + * A simple statusbar to be used with DWM + * Gathers different kinds of system info and displays it as the windows title + * + * Requires dwm to have the status2d patch applied and a font that supports icons e.g. Nerdfonts + * + * author: _N0x + * version: 0.1 + */ + +#include +#include +#include +#include +#include +#include + +#include "config.h" + +/* enums */ +enum { IconDateTime, IconBattery, IconCPU, IconRAM, IconDisk, + IconNetSpeed, IconNetSpeedUp, IconNetSpeedDown, IconNetwork, IconMusic }; + +/* function declaration */ +void setup(); +void setxroot(char *title); +char *readinfile(char *filename); +char *getnetworkspeed(); +char *cs(int count, ...); + +/* variables */ +static const char *iface; +static const char kbs[] = "kb/s"; + + +/* 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); +} + + +/* Takes in a filename and returns the content of the file */ +char * readinfile(char *fileName) +{ + FILE * fd; + char * c; + int len; + + fd = fopen(fileName, "r"); + if (fd==NULL) + { + perror("Could not read in File.\n"); + exit(EXIT_FAILURE); + } + + fseek(fd, 0, SEEK_END); + len = ftell(fd); + fseek(fd, 0, SEEK_SET); + c = malloc(len); + fread(c, 1, len, fd); + + return c; +} + + +char * getnetworkspeed() +{ + 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