diff options
Diffstat (limited to 'src/modules')
| -rw-r--r-- | src/modules/.dirstamp | 0 | ||||
| -rw-r--r-- | src/modules/battery.c | 73 | ||||
| -rw-r--r-- | src/modules/battery.h | 22 | ||||
| -rw-r--r-- | src/modules/pulse.c | 136 | ||||
| -rw-r--r-- | src/modules/pulse.h | 26 | ||||
| -rw-r--r-- | src/modules/time.c | 29 | ||||
| -rw-r--r-- | src/modules/time.h | 15 |
7 files changed, 301 insertions, 0 deletions
diff --git a/src/modules/.dirstamp b/src/modules/.dirstamp new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/modules/.dirstamp diff --git a/src/modules/battery.c b/src/modules/battery.c new file mode 100644 index 0000000..6c656fe --- /dev/null +++ b/src/modules/battery.c @@ -0,0 +1,73 @@ +#include "battery.h" + +#include <stdlib.h> +#include <string.h> +#include <tgmath.h> + +#include <fcntl.h> +#include <unistd.h> + +#define UPDATE_FREQUENCY 20 + +static int +open_battery_file(const char * name) +{ + char path[128]; + snprintf(path, 128, "/sys/class/power_supply/BAT0/%s", name); + return open(path, O_RDONLY); +} + +static int +battery_value(int file) +{ + int val = 0; + char buf[16]; + + memset(&buf, '\0', 16); + lseek(file, 0, SEEK_SET); + read(file, &buf, sizeof(char) * 16); + sscanf(buf, "%d", &val); + + return val; +} + +struct battery * +battery_new() +{ + struct battery * b = malloc(sizeof(struct battery)); + int file = open_battery_file("charge_full"); + + b->full = battery_value(file); + b->file_current = open_battery_file("charge_now"); + b->last_update = 0; + + close(file); + + return b; +} + +void +battery_free(struct battery * b) +{ + close(b->file_current); + free(b); +} + +void battery_update(struct battery * b) +{ + int now; + float percentf; + int percent; + time_t now_time = time(NULL); + + if((now_time - b->last_update) >= UPDATE_FREQUENCY) { + b->last_update = now_time; + + now = battery_value(b->file_current); + + percentf = (100.0f / b->full) * now; + percent = round(percentf); + + snprintf(b->value, BATTERY_VALUE_SIZE, "%d%%", percent); + } +} diff --git a/src/modules/battery.h b/src/modules/battery.h new file mode 100644 index 0000000..92489b5 --- /dev/null +++ b/src/modules/battery.h @@ -0,0 +1,22 @@ +#ifndef _DWXINFO_MODULES_BATTERY_H_ +#define _DWXINFO_MODULES_BATTERY_H_ + +#include <stdio.h> +#include <time.h> + +#define BATTERY_VALUE_SIZE 5 + +struct battery { + int full; + int file_current; + time_t last_update; + + char value[BATTERY_VALUE_SIZE]; +}; + +struct battery * battery_new(); +void battery_free(struct battery *); + +void battery_update(struct battery *); + +#endif diff --git a/src/modules/pulse.c b/src/modules/pulse.c new file mode 100644 index 0000000..cbbfe35 --- /dev/null +++ b/src/modules/pulse.c @@ -0,0 +1,136 @@ +#include "pulse.h" + +#include "../signal.h" + +#include <pulse/mainloop.h> +#include <pulse/context.h> +#include <pulse/introspect.h> +#include <pulse/volume.h> + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <tgmath.h> + +static void +run_loop(struct pulse * p) +{ + p->run_loop = true; + while(p->run_loop && signal_running) { + pa_mainloop_iterate(p->ml, 1, NULL); + } +} + +static void +stop_loop(struct pulse * p) +{ + p->run_loop = false; +} + +static void +server_info_cb(pa_context * ctx, const pa_server_info * info, void * ud) +{ + struct pulse * p = (struct pulse *) ud; + + size_t s = strlen(info->default_sink_name); + + p->default_sink = malloc(s + 1); + memset(p->default_sink, '\0', s + 1); + strncpy(p->default_sink, info->default_sink_name, s); + + stop_loop(p); +} + +static void +state_cb(pa_context * ctx, void * ud) +{ + pa_context_state_t state = pa_context_get_state(ctx); + struct pulse * p = (struct pulse *) ud; + + switch(state) { + + case PA_CONTEXT_READY: + pa_context_get_server_info(p->ctx, server_info_cb, p); + break; + case PA_CONTEXT_FAILED: + case PA_CONTEXT_TERMINATED: + stop_loop(p); + break; + default: break; + + } +} + +struct pulse * +pulse_new() +{ + struct pulse * p = malloc(sizeof(struct pulse)); + + memset(p->value, '\0', PULSE_VALUE_SIZE); + + p->default_sink = NULL; + p->run_loop = false; + + p->ml = pa_mainloop_new(); + p->ctx = pa_context_new(pa_mainloop_get_api(p->ml), NULL); + + pa_context_set_state_callback(p->ctx, state_cb, p); + + pa_context_connect(p->ctx, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL); + + run_loop(p); + + return p; +} + +void +pulse_free(struct pulse * p) +{ + if(p->default_sink != NULL) free(p->default_sink); + + if(p->ctx->proplist) { + pa_proplist_free(p->ctx->proplist); + } + + pa_context_disconnect(p->ctx); + pa_context_unref(p->ctx); + pa_mainloop_free(p->ml); + + free(p); +} + +static void +sink_info_cb(pa_context * ctx, const pa_sink_info * info, int eol, void * ud) +{ + struct pulse * p = (struct pulse *) ud; + pa_volume_t vol, norm; + + if(eol > 0) { + stop_loop(p); + } + else { + if(info->mute) { + strncpy(p->value, "M", PULSE_VALUE_SIZE); + } + else { + vol = pa_cvolume_avg(&info->volume); + vol -= PA_VOLUME_MUTED; + norm = PA_VOLUME_NORM - PA_VOLUME_MUTED; + + vol = round((100.0f / norm) * vol); + snprintf(p->value, PULSE_VALUE_SIZE, "%d%%", vol); + } + } +} + +void +pulse_update(struct pulse * p) +{ + pa_operation * op; + + if(p->default_sink != NULL) { + op = pa_context_get_sink_info_by_name(p->ctx, p->default_sink, sink_info_cb, p); + pa_operation_unref(op); + run_loop(p); + } +} diff --git a/src/modules/pulse.h b/src/modules/pulse.h new file mode 100644 index 0000000..708d05c --- /dev/null +++ b/src/modules/pulse.h @@ -0,0 +1,26 @@ +#ifndef _DWXINFO_PULSE_H_ +#define _DWXINFO_PULSE_H_ + +#include <stdbool.h> + +#define PULSE_VALUE_SIZE 5 + +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 *); + +#endif diff --git a/src/modules/time.c b/src/modules/time.c new file mode 100644 index 0000000..84f894e --- /dev/null +++ b/src/modules/time.c @@ -0,0 +1,29 @@ +#include "time.h" + +#include <stdlib.h> +#include <time.h> +#include <tgmath.h> + +struct time * +time_new() +{ + struct time * t = malloc(sizeof(struct time)); + return t; +} + +void +time_free(struct time * t) +{ + free(t); +} + +void +time_update(struct time * t) +{ + time_t timer; + struct tm * tm; + + time(&timer); + tm = gmtime(&timer); + strftime(t->value, TIME_VALUE_SIZE, "%F [%a] %T", tm); +} diff --git a/src/modules/time.h b/src/modules/time.h new file mode 100644 index 0000000..2dec7d5 --- /dev/null +++ b/src/modules/time.h @@ -0,0 +1,15 @@ +#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 *); + +#endif |
