commit 612007bf250ae8a0b6280500c78f6de74b135b1c
parent ee925c823cb1623baa256a70dfba469214120c47
Author: ssnf <ssnf@ssnf.xyz>
Date: Thu, 10 Jun 2021 03:26:33 +0000
simplified config process
Diffstat:
A | .gitignore | | | 1 | + |
M | Makefile | | | 33 | ++++++++++++++++++++++++--------- |
A | config.def.h | | | 6 | ++++++ |
A | linux.c | | | 153 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
M | sbar.c | | | 21 | +++++++-------------- |
M | sbar.h | | | 76 | ++++++++++++++++++++++++++++++++++++++-------------------------------------- |
D | unix.c | | | 162 | ------------------------------------------------------------------------------- |
7 files changed, 229 insertions(+), 223 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -0,0 +1 @@
+config.h
diff --git a/Makefile b/Makefile
@@ -1,15 +1,30 @@
-DESTDIR=~/.local
-LIBS=-lX11
-OPTS=-std=c99 -Wno-parentheses -Wpedantic -Wall -funsigned-char -D_POSIX_C_SOURCE=199309L
+CC = c99
+CFLAGS = -Os -Wall -Wpedantic -D_DEFAULT_SOURCE
+LIBS = -lX11
+LDFLAGS = ${LIBS}
+
+DST = ~/.local
+SRC = sbar.c linux.c
+OBJ = ${SRC:.c=.o}
+
+all: sbar config.h
+
+config.h:
+ cp -n config.def.h $@
+
+%.o: %.c
+ ${CC} ${CFLAGS} -c $<
+
+sbar: ${OBJ}
+ ${CC} ${LDFLAGS} ${OBJ} -o $@
-SRC = sbar.c unix.c
-sbar:
- ${CC} -o $@ ${LIBS} ${OPTS} *.c
clean:
rm -f sbar ${OBJ}
-install:
- mkdir -p ${DESTDIR}/bin
+
+install: all
+ mkdir -p ${DST}/bin
chmod +x sbar
- cp -f sbar ${DESTDIR}/bin
+ cp -f sbar ${DST}/bin
+
uninstall:
rm ${DESTIDIR}/bin/sbar
diff --git a/config.def.h b/config.def.h
@@ -0,0 +1,6 @@
+#define FMT "%s %s %s %s %s", part[0].text, part[1].text, ram.text, date.text, bat.text
+
+Partition part[] = {
+ {"/"},
+ {"/home"},
+};
diff --git a/linux.c b/linux.c
@@ -0,0 +1,153 @@
+#include "sbar.h"
+
+#include <time.h>
+#include <unistd.h>
+#include <X11/Xlib.h>
+#include <sys/statfs.h>
+
+void wait(double);
+void Partinit();
+void Partchk();
+void Raminit();
+void Ramchk();
+void Datechk();
+void Batchk();
+
+static Display* display;
+
+void
+wait(double t)
+{
+ usleep((uchar)t * 1000000);
+}
+
+void
+Partinit()
+{
+ struct statfs stat;
+ uint i, size;
+
+ for (i = 0; part[i].path; ++i) {
+ statfs(part[i].path, &stat);
+ size = (stat.f_blocks * stat.f_bsize) >> 20;
+ part[i].size_f = (float)size / 1024.0f;
+ }
+}
+
+void
+Partchk()
+{
+ struct statfs stat;
+ uint i, free;
+
+ for (i = 0; part[i].path; ++i) {
+ statfs(part[i].path, &stat);
+ free = (stat.f_bfree * stat.f_bsize) >> 23;
+ if (free != part[i].free) {
+ part[i].free = free;
+ ++title.update;
+ snprintf(part[i].text, 32, "%s:(%2.2fGB/%2.2fGB)", part[i].path, part[i].size_f - ((float)free / 128.0f), part[i].size_f);
+ }
+ }
+}
+
+void
+Raminit()
+{
+ FILE* meminfo;
+ char buf[32];
+ ulong size;
+
+ if (!(meminfo = fopen("/proc/meminfo", "r"))) return;
+ fread(buf, 32, 1, meminfo);
+ fclose(meminfo);
+ sscanf(buf, "MemTotal: %lu", &size);
+ ram.size = size;
+ ram.unit = 'M';
+ if (size > 1024*1024) {
+ ram.unit = 'G';
+ size >>= 10;
+ }
+ ram.size_f = size/1024.0f;
+}
+
+void
+Ramchk()
+{
+ FILE* meminfo;
+ char buf[1024];
+ ulong i, free;
+ float used_f;
+ uchar unit;
+
+ if (!(meminfo = fopen("/proc/meminfo", "r"))) return;
+ fread(buf, 1024, 1, meminfo);
+ fclose(meminfo);
+ free = 0;
+ sscanf(strstr(buf, "MemFree"), "MemFree: %lu", &free);
+ if ((ram.unit == 'M' && free >> 3 == ram.free >> 3) ||
+ (ram.unit == 'G' && free >> 13 == ram.free >> 13)) return;
+ ram.free = free;
+ sscanf(strstr(buf, "Buffers"), "Buffers: %lu", &i);
+ free += i;
+ sscanf(strstr(buf, "Cached"), "Cached: %lu", &i);
+ free += i;
+ sscanf(strstr(buf, "Shmem"), "Shmem: %lu", &i);
+ free -= i;
+ sscanf(strstr(buf, "SReclaimable"), "SReclaimable: %lu", &i);
+ free += i;
+ unit = 'M';
+ used_f = (ram.size - free) / 1024.0f;
+ if (used_f > 1024) {
+ unit = 'G';
+ used_f /= 1024.0f;
+ }
+ ++title.update;
+ snprintf(ram.text, 24, "R(%.02f%cB/%.02f%cB)", used_f, unit, ram.size_f, ram.unit);
+}
+
+void
+Datechk()
+{
+ FILE* f;
+ uint h, m, s, d, M;
+
+ if (!(f = fopen("/sys/class/rtc/rtc0/time", "r"))) return;
+ fscanf(f, "%u:%u:%u", &h, &m, &s);
+ fclose(f);
+ if (s == date.s) return;
+ if (h > 12) h -= 12;
+ date.s = s;
+ if (!(f = fopen("/sys/class/rtc/rtc0/date", "r"))) return;
+ fscanf(f, "%*u-%u-%u", &M, &d);
+ fclose(f);
+ ++title.update;
+ snprintf(date.text, 16, "%u/%u %u:%02u:%02u", M, d, h, m, s);
+}
+
+void
+Batchk()
+{
+ FILE* f;
+ uint c;
+
+ if (!(f = fopen("/sys/class/power_supply/BAT0/capacity", "r"))) return;
+ fscanf(f, "%u", &c);
+ fclose(f);
+ if (c == bat.c) return;
+ bat.c = c;
+ snprintf(bat.text, 19, "B(%u%%)", c);
+}
+
+void
+Titleinit()
+{
+ display = XOpenDisplay(NULL);
+}
+
+void
+Titleset()
+{
+ XStoreName(display, DefaultRootWindow(display), title.text);
+ XFlush(display);
+}
diff --git a/sbar.c b/sbar.c
@@ -1,22 +1,11 @@
#include "sbar.h"
+#include "config.h"
-Partition part[] = {
- {.path = "/"},
- {.path = "/home"}
-};
Bat bat;
Ram ram;
Date date;
Title title;
-void
-update()
-{
- title.update = 0;
- snprintf(title.text, 256, "%s %s %s %s %s", part[0].text, part[1].text, ram.text, date.text, bat.text);
- Titleset();
-}
-
int
main()
{
@@ -28,7 +17,11 @@ main()
Ramchk();
Datechk();
Batchk();
- if (title.update) update();
- wait(.016);
+ if (title.update) {
+ title.update = 0;
+ snprintf(title.text, 256, FMT);
+ Titleset();
+ }
+ wait(time);
}
}
diff --git a/sbar.h b/sbar.h
@@ -5,57 +5,57 @@
#define LENGTH(a) (sizeof(a)/sizeof(a[0]))
-typedef unsigned char uchar;
-typedef unsigned int uint;
-typedef unsigned long ulong;
-typedef struct Title Title;
-typedef struct Partition Partition;
-typedef struct Ram Ram;
-typedef struct Date Date;
-typedef struct Bat Bat;
+typedef unsigned char uchar;
+typedef unsigned int uint;
+typedef unsigned long ulong;
+typedef struct Title Title;
+typedef struct Partition Partition;
+typedef struct Ram Ram;
+typedef struct Date Date;
+typedef struct Bat Bat;
struct Title {
- char text[256];
- uchar update;
+ char text[256];
+ uchar update;
};
-void Titleinit();
-void Titleset();
struct Partition {
- char* path;
- char text[32];
- ulong free;
- float size_f;
+ char* path;
+ char text[32];
+ ulong free;
+ float size_f;
};
-void Partinit();
-void Partchk();
struct Ram {
- char text[24];
- ulong free;
- ulong size;
- uchar unit;
- float size_f;
+ char text[24];
+ ulong free;
+ ulong size;
+ uchar unit;
+ float size_f;
};
-void Raminit();
-void Ramchk();
struct Date {
- char text[16];
- uint s;
+ char text[16];
+ uint s;
};
-void Datechk();
struct Bat {
- char text[8];
- ulong c;
+ char text[8];
+ ulong c;
};
-void Batchk();
-void wait(double);
-
-extern Title title;
-extern Partition part[2];
-extern Ram ram;
-extern Date date;
-extern Bat bat;
+void Datechk();
+void Batchk();
+void Partchk();
+void Partinit();
+void Ramchk();
+void Raminit();
+void Titleset();
+void Titleinit();
+void wait(double);
+
+extern Title title;
+extern Partition part[];
+extern Ram ram;
+extern Date date;
+extern Bat bat;
diff --git a/unix.c b/unix.c
@@ -1,162 +0,0 @@
-#include "sbar.h"
-
-#include <time.h>
-#include <unistd.h>
-#include <X11/Xlib.h>
-#include <sys/statfs.h>
-
-void wait(double);
-void Partinit();
-void Partchk();
-void Raminit();
-void Ramchk();
-void Datechk();
-void Batchk();
-
-void
-wait(double t)
-{
- static struct timespec ts;
- static double lastwait;
- ulong i;
-
- if (t != lastwait) {
- lastwait = t;
- for (i = 0; t >= 1; --t, ++i);
- ts.tv_sec = i;
- ts.tv_nsec = t * 1000000000;
- }
- nanosleep(&ts, NULL);
-}
-
-void
-Partinit()
-{
- struct statfs stat;
- uint i, size;
-
- for (i = 0; i < LENGTH(part); ++i) {
- statfs(part[i].path, &stat);
- size = (stat.f_blocks * stat.f_bsize) >> 20;
- part[i].size_f = (float)size / 1024.0f;
- }
-}
-
-void
-Partchk()
-{
- struct statfs stat;
- uint i, free;
-
- for (i = 0; i < LENGTH(part); ++i) {
- statfs(part[i].path, &stat);
- free = (stat.f_bfree * stat.f_bsize) >> 23;
- if (free != part[i].free) {
- part[i].free = free;
- ++title.update;
- snprintf(part[i].text, 25, "%s:(%2.2fGB/%2.2fGB)", part[i].path, part[i].size_f - ((float)free / 128.0f), part[i].size_f);
- }
- }
-}
-
-void
-Raminit()
-{
- FILE* meminfo;
- char buf[32];
- ulong size;
-
- if (!(meminfo = fopen("/proc/meminfo", "r"))) return;
- fread(buf, 32, 1, meminfo);
- fclose(meminfo);
- sscanf(buf, "MemTotal: %lu", &size);
- ram.size = size;
- ram.unit = 'M';
- if (size > 1024*1024) {
- ram.unit = 'G';
- size >>= 10;
- }
- ram.size_f = size/1024.0f;
-}
-
-void
-Ramchk()
-{
- FILE* meminfo;
- char buf[1024];
- ulong i, free;
- float used_f;
- uchar unit;
-
- if (!(meminfo = fopen("/proc/meminfo", "r"))) return;
- fread(buf, 1024, 1, meminfo);
- fclose(meminfo);
- free = 0;
- sscanf(strstr(buf, "MemFree"), "MemFree: %lu", &free);
- if (ram.unit == 'M' && free >> 3 == ram.free >> 3 ||
- ram.unit == 'G' && free >> 13 == ram.free >> 13) return;
- ram.free = free;
- sscanf(strstr(buf, "Buffers"), "Buffers: %lu", &i);
- free += i;
- sscanf(strstr(buf, "Cached"), "Cached: %lu", &i);
- free += i;
- sscanf(strstr(buf, "Shmem"), "Shmem: %lu", &i);
- free -= i;
- sscanf(strstr(buf, "SReclaimable"), "SReclaimable: %lu", &i);
- free += i;
- unit = 'M';
- used_f = (ram.size - free) / 1024.0f;
- if (used_f > 1024) {
- unit = 'G';
- used_f /= 1024.0f;
- }
- ++title.update;
- snprintf(ram.text, 24, "R(%.02f%cB/%.02f%cB)", used_f, unit, ram.size_f, ram.unit);
-}
-
-void
-Datechk()
-{
- FILE* f;
- uint h, m, s, d, M;
-
- if (!(f = fopen("/sys/class/rtc/rtc0/time", "r"))) return;
- fscanf(f, "%u:%u:%u", &h, &m, &s);
- fclose(f);
- if (s == date.s) return;
- if (h > 12) h -= 12;
- date.s = s;
- if (!(f = fopen("/sys/class/rtc/rtc0/date", "r"))) return;
- fscanf(f, "%*u-%u-%u", &M, &d);
- fclose(f);
- ++title.update;
- snprintf(date.text, 16, "%u/%u %u:%02u:%02u", M, d, h, m, s);
-}
-
-void
-Batchk()
-{
- FILE* f;
- uint c;
-
- if (!(f = fopen("/sys/class/power_supply/BAT0/capacity", "r"))) return;
- fscanf(f, "%u", &c);
- fclose(f);
- if (c == bat.c) return;
- bat.c = c;
- snprintf(bat.text, 19, "B(%u%%)", c);
-}
-
-static Display* display;
-void
-Titleinit()
-{
- display = XOpenDisplay(NULL);
-}
-
-void
-Titleset()
-{
- XStoreName(display, DefaultRootWindow(display), title.text);
- XFlush(display);
-}