diff options
Diffstat (limited to 'src/modules')
| -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 |
11 files changed, 321 insertions, 52 deletions
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 |
