diff options
| author | Sam Light <sam@lightscale.co.uk> | 2025-09-10 19:37:28 +0100 |
|---|---|---|
| committer | Sam Light <sam@lightscale.co.uk> | 2025-09-10 19:37:28 +0100 |
| commit | 4d3e462531d2172cb13319c7f79c0e2228e652fa (patch) | |
| tree | 6381426e806b30814890e74085afef17e3ee78f3 | |
| parent | b74a383e6ded85c122f98137936d78f768f8f938 (diff) | |
Big update
| -rw-r--r-- | Makefile.am | 5 | ||||
| -rw-r--r-- | src/dwminfo.c | 4 | ||||
| -rw-r--r-- | src/modules/battery.c | 78 | ||||
| -rw-r--r-- | src/modules/battery.h | 15 | ||||
| -rw-r--r-- | src/modules/brightness.c | 26 | ||||
| -rw-r--r-- | src/modules/brightness.h | 15 | ||||
| -rw-r--r-- | src/modules/cpu.c | 88 | ||||
| -rw-r--r-- | src/modules/cpu.h | 10 | ||||
| -rw-r--r-- | src/modules/memory.c | 91 | ||||
| -rw-r--r-- | src/modules/memory.h | 10 | ||||
| -rw-r--r-- | src/modules/pulse.h | 15 | ||||
| -rw-r--r-- | src/modules/time.c | 18 | ||||
| -rw-r--r-- | src/modules/time.h | 7 | ||||
| -rw-r--r-- | src/somebarinfo.c | 43 | ||||
| -rw-r--r-- | src/text.c | 59 | ||||
| -rw-r--r-- | src/text.h | 10 |
16 files changed, 408 insertions, 86 deletions
diff --git a/Makefile.am b/Makefile.am index 6889b3e..f04c219 100644 --- a/Makefile.am +++ b/Makefile.am @@ -7,7 +7,10 @@ COMMON_SOURCES=src/text.c \ src/signal.c \ src/modules/pulse.c \ src/modules/time.c \ - src/modules/battery.c + src/modules/battery.c \ + src/modules/cpu.c \ + src/modules/memory.c \ + src/modules/brightness.c somebarinfo_SOURCES=src/somebarinfo.c $(COMMON_SOURCES) diff --git a/src/dwminfo.c b/src/dwminfo.c index 8e04b43..f589355 100644 --- a/src/dwminfo.c +++ b/src/dwminfo.c @@ -34,7 +34,7 @@ static void update_status(struct state * s) { text_update(s->text); - XStoreName(s->display, s->window, s->text->value); + XStoreName(s->display, s->window, text_get_val(s->text)); XSync(s->display, 1); } @@ -43,7 +43,7 @@ run_loop(struct state * s) { while(signal_running) { update_status(s); - usleep(1000 * 500); + usleep(1000 * 1000); } } diff --git a/src/modules/battery.c b/src/modules/battery.c index 18a68de..02379c8 100644 --- a/src/modules/battery.c +++ b/src/modules/battery.c @@ -3,63 +3,99 @@ #include <stdlib.h> #include <string.h> #include <tgmath.h> - +#include <stdbool.h> +#include <stdio.h> #include <fcntl.h> #include <unistd.h> +#define VALUE_SIZE 16 +#define BATTERY_PATH "/sys/class/power_supply/BAT1/" + +struct battery { + int status; + int capacity; + time_t last_updated; + char value[VALUE_SIZE]; +}; + static int open_battery_file(const char * name) { char path[128]; - snprintf(path, 128, "/sys/class/power_supply/BAT0/%s", name); + snprintf(path, 128, "%s%s", BATTERY_PATH, name); return open(path, O_RDONLY); } static int battery_value(int file) { + const size_t buf_size = 4; int val = 0; - char buf[16]; + char buf[buf_size]; - memset(&buf, '\0', 16); + memset(&buf, '\0', buf_size); lseek(file, 0, SEEK_SET); - read(file, &buf, sizeof(char) * 16); + read(file, &buf, sizeof(char) * buf_size); sscanf(buf, "%d", &val); return val; } -struct battery * -battery_new() +static bool +battery_status_discharging(int file) { - struct battery * b = malloc(sizeof(struct battery)); - int file = open_battery_file("charge_full"); + const size_t buf_size = 16; + char buf[buf_size]; - b->full = battery_value(file); - b->file_current = open_battery_file("charge_now"); + memset(&buf, '\0', buf_size); + lseek(file, 0, SEEK_SET); + read(file, &buf, sizeof(char) * buf_size); - close(file); + return strncmp(buf, "Discharging\n", buf_size) == 0; +} +struct battery * +battery_new() +{ + struct battery * b = malloc(sizeof(struct battery)); + b->last_updated = 0; + b->capacity = open_battery_file("capacity"); + b->status = open_battery_file("status"); return b; } void battery_free(struct battery * b) { - close(b->file_current); + close(b->status); + close(b->capacity); free(b); } void battery_update(struct battery * b) { - int now; - float percentf; - int percent; - - now = battery_value(b->file_current); + int cap = battery_value(b->capacity); + bool discharge = battery_status_discharging(b->status); + snprintf( + b->value, VALUE_SIZE, + "%s% 4d%%", + discharge ? "\uf241" : "\uf0e7", + cap); +} - percentf = (100.0f / b->full) * now; - percent = round(percentf); +const char * +battery_get_val(struct battery * b) +{ + return b->value; +} - snprintf(b->value, BATTERY_VALUE_SIZE, "%d%%", percent); +bool +battery_should_update(struct battery * b, time_t now, unsigned short limit) +{ + double time_diff = difftime(now, b->last_updated); + if(time_diff >= limit) { + b->last_updated = now; + return true; + } + return false; } diff --git a/src/modules/battery.h b/src/modules/battery.h index f358e46..44b4086 100644 --- a/src/modules/battery.h +++ b/src/modules/battery.h @@ -1,20 +1,15 @@ #ifndef _DWXINFO_MODULES_BATTERY_H_ #define _DWXINFO_MODULES_BATTERY_H_ -#include <stdio.h> - -#define BATTERY_VALUE_SIZE 5 - -struct battery { - int full; - int file_current; - - char value[BATTERY_VALUE_SIZE]; -}; +#include <time.h> +#include <stdbool.h> struct battery * battery_new(); void battery_free(struct battery *); void battery_update(struct battery *); +const char * battery_get_val(struct battery *); +bool battery_should_update(struct battery *, time_t, unsigned short); + #endif diff --git a/src/modules/brightness.c b/src/modules/brightness.c new file mode 100644 index 0000000..47d4148 --- /dev/null +++ b/src/modules/brightness.c @@ -0,0 +1,26 @@ +#include "brightness.h" + +#include <stdlib.h> + +#define VALUE_SIZE 5 + +struct brightness { + char value[VALUE_SIZE]; +}; + +struct brightness * brightness_new() { + struct brightness * b = malloc(sizeof(struct brightness)); + return b; +} + +void brightness_free(struct brightness * b) { + free(b); +} + +void brightness_update(struct brightness * b) { + +} + +const char * brightness_get_val(struct brightness * b) { + return b->value; +} diff --git a/src/modules/brightness.h b/src/modules/brightness.h new file mode 100644 index 0000000..ba43457 --- /dev/null +++ b/src/modules/brightness.h @@ -0,0 +1,15 @@ +#ifndef _DWXINFO_MODULES_BRIGHTNESS_H_ +#define _DWXINFO_MODULES_BRIGHTNESS_H_ + +#include <time.h> +#include <stdbool.h> + +struct brightness * brightness_new(); +void brightness_free(struct brightness *); + +void brightness_update(struct brightness *); + +const char * brightness_get_val(struct brightness *); +//bool brightness_should_update(struct brightness *, time_t, unsigned short); + +#endif diff --git a/src/modules/cpu.c b/src/modules/cpu.c new file mode 100644 index 0000000..cf70524 --- /dev/null +++ b/src/modules/cpu.c @@ -0,0 +1,88 @@ +#include "cpu.h" + +#include <stdlib.h> +#include <string.h> +#include <stdio.h> +#include <unistd.h> +#include <inttypes.h> +#include <tgmath.h> + +#define VALUE_SIZE 32 + +#define FILE_STAT_PATH "/proc/stat" + +struct cpu_stat { + unsigned long usage; + unsigned long total; +}; + +struct cpu { + char value[VALUE_SIZE]; + + struct cpu_stat last; + FILE * statfd; +}; + +static void +parse_cpu_stat(struct cpu * c, struct cpu_stat * stat) +{ + char line[1024]; + char * tok = line + 5; + + stat->usage = 0; + stat->total = 0; + + rewind(c->statfd); + fflush(c->statfd); + fgets(line, 1024, c->statfd); + + stat->usage += strtoul(tok, &tok, 10); + stat->usage += strtoul(tok, &tok, 10); + stat->usage += strtoul(tok, &tok, 10); + stat->total += stat->usage + strtoul(tok, &tok, 10); +} + +struct cpu * +cpu_new() +{ + struct cpu * c = malloc(sizeof(struct cpu)); + memset(c->value, '\0', VALUE_SIZE); + c->statfd = fopen(FILE_STAT_PATH, "r"); + + parse_cpu_stat(c, &c->last); + + return c; +} + +void +cpu_free(struct cpu * c) +{ + fclose(c->statfd); + free(c); +} + +void +cpu_update(struct cpu * c) +{ + struct cpu_stat stat; + unsigned long usage_diff, total_diff; + + unsigned short percent; + + parse_cpu_stat(c, &stat); + + usage_diff = stat.usage - c->last.usage; + total_diff = stat.total - c->last.total; + + c->last = stat; + + percent = round((100.0f / total_diff) * usage_diff); + + snprintf(c->value, VALUE_SIZE, "\uf2db% 4d%%", percent); +} + +const char * +cpu_get_val(struct cpu * c) +{ + return c->value; +} diff --git a/src/modules/cpu.h b/src/modules/cpu.h new file mode 100644 index 0000000..fab2adb --- /dev/null +++ b/src/modules/cpu.h @@ -0,0 +1,10 @@ +#ifndef _DWXINFO_MODULES_CPU_H_ +#define _DWXINFO_MODULES_CPU_H_ + +struct cpu * cpu_new(); +void cpu_free(struct cpu *); + +void cpu_update(struct cpu *); +const char * cpu_get_val(struct cpu *); + +#endif diff --git a/src/modules/memory.c b/src/modules/memory.c new file mode 100644 index 0000000..b9230da --- /dev/null +++ b/src/modules/memory.c @@ -0,0 +1,91 @@ +#include "memory.h" + +#include <stdlib.h> +#include <string.h> +#include <stdio.h> +#include <stdbool.h> +#include <inttypes.h> +#include <tgmath.h> + +#define FILE_MEMINFO_PATH "/proc/meminfo" + +#define VALUE_SIZE 32 + +struct memory { + FILE * memfd; + char value[VALUE_SIZE]; +}; + +struct memory_info { + unsigned long total; + unsigned long avaliable; + unsigned short found; +}; + +struct memory * +memory_new() +{ + struct memory * m = malloc(sizeof(struct memory)); + + m->memfd = fopen(FILE_MEMINFO_PATH, "r"); + + return m; +} + +void +memory_free(struct memory * m) +{ + free(m); +} + +static char * +find_mem_value(char * line, const char * key) +{ + size_t klen = strlen(key); + if(strncmp(line, key, klen) == 0) { + return line + klen; + } + return NULL; +} + +void +memory_update(struct memory * m) +{ + const unsigned short lsize = 512; + char line[lsize]; + char * val; + + bool found_total = false, found_available = false; + unsigned long total = 0, available = 0; + + unsigned short percent = 0; + + rewind(m->memfd); + fflush(m->memfd); + + while((!found_total || !found_available) && !feof(m->memfd)) { + fgets(line, lsize, m->memfd); + + val = find_mem_value(line, "MemTotal: "); + if(!found_total && val != NULL) { + found_total = true; + total = strtoul(val, NULL, 10); + } + + val = find_mem_value(line, "MemAvailable: "); + if(!found_available && val != NULL) { + found_available = true; + available = strtoul(val, NULL, 10); + } + } + + percent = round((100.0f / total) * (total - available)); + + snprintf(m->value, VALUE_SIZE, "\uf538% 4d%%", percent); +} + +const char * +memory_get_val(struct memory * m) +{ + return m->value; +} diff --git a/src/modules/memory.h b/src/modules/memory.h new file mode 100644 index 0000000..db96d4b --- /dev/null +++ b/src/modules/memory.h @@ -0,0 +1,10 @@ +#ifndef _DWXINFO_MODULES_MEMORY_H_ +#define _DWXINFO_MODULES_MEMORY_H_ + +struct memory * memory_new(); +void memory_free(struct memory *); + +void memory_update(struct memory *); +const char * memory_get_val(struct memory *); + +#endif diff --git a/src/modules/pulse.h b/src/modules/pulse.h index 708d05c..9e24216 100644 --- a/src/modules/pulse.h +++ b/src/modules/pulse.h @@ -2,25 +2,16 @@ #define _DWXINFO_PULSE_H_ #include <stdbool.h> - -#define PULSE_VALUE_SIZE 5 +#include <time.h> typedef struct pa_mainloop pa_mainloop; typedef struct pa_context pa_context; -struct pulse { - pa_mainloop * ml; - pa_context * ctx; - char * default_sink; - - bool run_loop; - - char value[PULSE_VALUE_SIZE]; -}; - struct pulse * pulse_new(); void pulse_free(struct pulse *); void pulse_update(struct pulse *); +const char * pulse_get_val(struct pulse *); +bool pulse_should_update(struct pulse *, time_t, unsigned short); #endif diff --git a/src/modules/time.c b/src/modules/time.c index 84f894e..272c8f8 100644 --- a/src/modules/time.c +++ b/src/modules/time.c @@ -2,7 +2,13 @@ #include <stdlib.h> #include <time.h> -#include <tgmath.h> + +#define VALUE_SIZE 32 + +struct time { + char value[VALUE_SIZE]; +}; + struct time * time_new() @@ -24,6 +30,12 @@ time_update(struct time * t) struct tm * tm; time(&timer); - tm = gmtime(&timer); - strftime(t->value, TIME_VALUE_SIZE, "%F [%a] %T", tm); + tm = localtime(&timer); + strftime(t->value, VALUE_SIZE, "%F [%a] %T", tm); +} + +const char * +time_get_val(struct time * t) +{ + return t->value; } diff --git a/src/modules/time.h b/src/modules/time.h index 2dec7d5..8ffc164 100644 --- a/src/modules/time.h +++ b/src/modules/time.h @@ -1,15 +1,10 @@ #ifndef _DWXINFO_MODULES_TIME_H_ #define _DWXINFO_MODULES_TIME_H_ -#define TIME_VALUE_SIZE 32 - -struct time { - char value[TIME_VALUE_SIZE]; -}; - struct time * time_new(); void time_free(struct time *); void time_update(struct time *); +const char * time_get_val(struct time *); #endif diff --git a/src/somebarinfo.c b/src/somebarinfo.c index 0dc2d50..3371674 100644 --- a/src/somebarinfo.c +++ b/src/somebarinfo.c @@ -1,18 +1,41 @@ #include "signal.h" #include "text.h" -#include <stdio.h> #include <stdlib.h> #include <unistd.h> +#include <fcntl.h> #include <string.h> +#include <sys/stat.h> #define SOMEBAR_CMD "status" struct state { - FILE * file; + int fd; struct text * text; }; +static bool +file_exists(const char * path) +{ + int fd = open(path, O_RDONLY); + bool exists = fd > 0; + if(exists) close(fd); + return exists; +} + +static int +open_fifo(const char * path) +{ + int rc; + if(!file_exists(path)) { + rc = mkfifo(path, 0644); + if(rc < 0) return -1; + } + + return open(path, O_WRONLY); +} + + static int init_state(struct state * s) { @@ -30,8 +53,8 @@ init_state(struct state * s) s->text = NULL; - s->file = fopen(path, "a"); - if(s->file == NULL) return -1; + s->fd = open_fifo(path); + if(s->fd < 1) return -1; s->text = text_new(); return 0; @@ -40,7 +63,7 @@ init_state(struct state * s) static void deinit_state(struct state * s) { - if(s->file) fclose(s->file); + if(s->fd > 0) close(s->fd); if(s->text) text_free(s->text); } @@ -50,14 +73,16 @@ update_status(struct state * s) const size_t cmd_len = strlen(SOMEBAR_CMD), msg_len = TEXT_SIZE + cmd_len + 3; + + size_t actual_len; + char msg[msg_len]; text_update(s->text); + snprintf(msg, msg_len, "%s %s\n", SOMEBAR_CMD, text_get_val(s->text)); - snprintf(msg, msg_len, "%s %s\n", SOMEBAR_CMD, s->text->value); - - fputs(msg, s->file); - fflush(s->file); + actual_len = strlen(msg); + write(s->fd, msg, actual_len); } static void @@ -5,6 +5,9 @@ #include "modules/pulse.h" #include "modules/time.h" #include "modules/battery.h" +#include "modules/cpu.h" +#include "modules/memory.h" +#include "modules/brightness.h" #include <stdio.h> #include <stdlib.h> @@ -12,18 +15,32 @@ #include <time.h> #include <tgmath.h> +#define VALUE_SIZE TEXT_SIZE + +struct text { + char value[VALUE_SIZE]; + + struct battery * battery; + struct pulse * pulse; + struct time * time; + struct cpu * cpu; + struct memory * memory; + struct brightness * brightness; +}; + struct text * text_new() { struct text * t = malloc(sizeof(struct text)); - memset(t->value, '\0', TEXT_SIZE); - - t->last_update = 0; + memset(t->value, '\0', VALUE_SIZE); t->battery = battery_new(); t->time = time_new(); t->pulse = pulse_new(); + t->cpu = cpu_new(); + t->memory = memory_new(); + t->brightness = brightness_new(); return t; } @@ -34,6 +51,10 @@ text_free(struct text * t) time_free(t->time); pulse_free(t->pulse); battery_free(t->battery); + cpu_free(t->cpu); + memory_free(t->memory); + brightness_free(t->brightness); + free(t); } @@ -41,25 +62,37 @@ void text_update(struct text * t) { time_t now_time = time(NULL); - double time_diff = difftime(now_time, t->last_update); bool should_update = signal_should_update(); - t->last_update = now_time; - time_update(t->time); - if(should_update || time_diff > 3) { + cpu_update(t->cpu); + memory_update(t->memory); + + if(should_update || pulse_should_update(t->pulse, now_time, 4)) { pulse_update(t->pulse); } - if(should_update || time_diff > 5) { + if(should_update || battery_should_update(t->battery, now_time, 10)) { battery_update(t->battery); } + if(should_update) { + brightness_update(t->brightness); + } + snprintf( - t->value, TEXT_SIZE, - "VOL: %s | BAT: %s | %s", - t->pulse->value, - t->battery->value, - t->time->value); + t->value, VALUE_SIZE, + "%s | %s | %s | %s | %s", + pulse_get_val(t->pulse), + cpu_get_val(t->cpu), + memory_get_val(t->memory), + battery_get_val(t->battery), + time_get_val(t->time)); +} + +const char * +text_get_val(struct text * t) +{ + return t->value; } @@ -6,18 +6,10 @@ #define TEXT_SIZE 512 -struct text { - time_t last_update; - char value[TEXT_SIZE]; - - struct battery * battery; - struct pulse * pulse; - struct time * time; -}; - struct text * text_new(); void text_free(struct text *); void text_update(struct text *); +const char * text_get_val(struct text *); #endif |
