From 52aa2298a1b42c3da24e3bb39fee1ed894231c03 Mon Sep 17 00:00:00 2001 From: Inqiyad Sabr Date: Tue, 28 Oct 2025 15:22:06 +0600 Subject: Add screen brightness module Though it's only for Unix likes... I really wanna support BSDs next time. --- Makefile | 2 +- README.md | 5 ++++- bstatus.c | 1 + modules.h | 5 +++++ scrbr.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 scrbr.c diff --git a/Makefile b/Makefile index 1610951..8f02e10 100644 --- a/Makefile +++ b/Makefile @@ -27,6 +27,6 @@ clean: $(RM) ./*.o install: - install -s -m0755 bstatus $(DESTDIR) + install -s -m 0755 bstatus $(DESTDIR) .PHONY: make clean install diff --git a/README.md b/README.md index a41babf..154a186 100644 --- a/README.md +++ b/README.md @@ -24,10 +24,13 @@ The modules aren't directly portable, I'd say to modify and port it than to comp - [x] datetime - [x] battery - [x] cpu -- [ ] memory +- [x] memory +- [x] brightness (screen) - [ ] volume - [ ] network +All work perfectly fine if you are using Linux. But I would love to write a bstatus seperately for the BSDs as well, instead of having them all in there to bloat the source code. + # Contribute Send a patch [to my email](mailto:sabr@ariamath.xyz) or [at xmpp](xmpp:sabr@ariamath.xyz). diff --git a/bstatus.c b/bstatus.c index 30eb098..1f3e735 100644 --- a/bstatus.c +++ b/bstatus.c @@ -16,6 +16,7 @@ int main(void) datetime(); battery(); cpu(); + scrbr(); mem(); write(1,"],",2); sleep(1); diff --git a/modules.h b/modules.h index 2db07ce..c7c66bf 100644 --- a/modules.h +++ b/modules.h @@ -18,6 +18,11 @@ void cpu(void); #define MEM_H void mem(void); #endif +// screen brightness +#ifndef SCRBR_H +#define SCRBR_H +void scrbr(void); +#endif // cmus #ifndef CMUS_H #define CMUS_H diff --git a/scrbr.c b/scrbr.c new file mode 100644 index 0000000..398a414 --- /dev/null +++ b/scrbr.c @@ -0,0 +1,42 @@ +#include +#include +#include "block.h" +#define ICON_BR "󰳲 " +#define FG "#222222" +#define BG "#dfb24999" +#define BDIR "/sys/class/backlight/amdgpu_bl1" +#define BMAXF max_brightness_f +#define BCURF current_brightness_f +FILE *BCURF, *BMAXF; +void scrbr(void) +{ + char *buf; + unsigned short current_brightness; + unsigned short max_brightness; + unsigned short size; + + if (!BCURF) BCURF = fopen(BDIR"/brightness","r"); + if (!BMAXF) BMAXF = fopen(BDIR"/max_brightness","r"); + + fseek(BCURF, 0, SEEK_END); + fseek(BMAXF, 0, SEEK_END); + + size = ftell(BCURF); + buf = malloc(size); + rewind(BCURF); + fgets(buf, size, BCURF); + current_brightness = atoi(buf); + free(buf); + + size = ftell(BMAXF); + buf = malloc(size); + rewind(BMAXF); + fgets(buf, size, BMAXF); + max_brightness = atoi(buf); + free(buf); + + char brightness[10]; + snprintf(brightness,10,ICON_BR"%3d%%", + current_brightness*100/max_brightness); + block(brightness,FG,BG,BG); +} -- cgit v1.2.3