Compare commits
7 Commits
dc7f72f6ef
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
6357677d13
|
|||
|
63de55f170
|
|||
|
41473a484a
|
|||
|
a5e133084b
|
|||
|
e45a17a138
|
|||
|
7cfe67c002
|
|||
|
b72ba45c79
|
6
Makefile
6
Makefile
@@ -5,7 +5,8 @@ CCARGS=-Isrc/include -Wall -pedantic
|
||||
MODULES= \
|
||||
modules/clock.dim \
|
||||
modules/battery.dim \
|
||||
modules/timesince.dim
|
||||
modules/timesince.dim \
|
||||
modules/volume.dim
|
||||
|
||||
|
||||
all: $(BIN) $(MODULES) compile_commands.json
|
||||
@@ -15,7 +16,8 @@ $(BIN): src/main.c src/plug.c src/socket.c src/util.c
|
||||
|
||||
|
||||
modules/%.dim: src/modules/%.c
|
||||
$(CC) -o $@ $^ -rdynamic -shared -fPIC $(CCARGS)
|
||||
mkdir -p $(dir $@)
|
||||
$(CC) -o $@ $^ -rdynamic -shared -fPIC $(CCARGS) -lasound
|
||||
|
||||
compile_commands.json:
|
||||
compiledb -n make
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
|
||||
typedef void* (*plug_init_f) (void);
|
||||
typedef void* (*plug_pre_reload_f) (void);
|
||||
typedef void (*plug_post_reload_f) (void*);
|
||||
@@ -31,5 +32,6 @@ char* poll_plugins(char* sep);
|
||||
void free_plugins(void);
|
||||
void reload_plugins(void);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
36
src/include/plug_utils.h
Normal file
36
src/include/plug_utils.h
Normal file
@@ -0,0 +1,36 @@
|
||||
|
||||
#ifndef _H_PLUG_UTILS
|
||||
#define _H_PLUG_UTILS
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
void* make_result_val(void* v, int size) {
|
||||
void* v2 = malloc(size);
|
||||
memcpy(v2, v, size);
|
||||
return v2;
|
||||
}
|
||||
|
||||
#define make_result(is_ok, v) ((Result){ \
|
||||
.is_ok = (is_ok), \
|
||||
.val = make_result_val((v), sizeof((v))) \
|
||||
})
|
||||
|
||||
#define Ok(v) make_result(true, v)
|
||||
#define Ok(v) make_result(true, v)
|
||||
|
||||
|
||||
typedef struct Result {
|
||||
bool is_ok;
|
||||
void* val;
|
||||
} Result;
|
||||
|
||||
void* res_unwrap(Result r) {
|
||||
if (!r.is_ok) assert(0);
|
||||
return r.val;
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
10
src/main.c
10
src/main.c
@@ -8,6 +8,16 @@
|
||||
#include "plug.h"
|
||||
#include "socket.h"
|
||||
|
||||
// TODO: Modules to add:
|
||||
// - Sound level
|
||||
// - cpu %
|
||||
// - ram %
|
||||
// - weather
|
||||
// - cpu temp
|
||||
// - uptime
|
||||
// - storage usage
|
||||
// - Network name + strength
|
||||
|
||||
void interrupt_handler(int);
|
||||
void print_help(void);
|
||||
|
||||
|
||||
63
src/modules/volume.c
Normal file
63
src/modules/volume.c
Normal file
@@ -0,0 +1,63 @@
|
||||
// Authored by xomf <xomf@the-atf-shot-my.dog>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <assert.h>
|
||||
#include <stdbool.h>
|
||||
#include <alsa/asoundlib.h>
|
||||
|
||||
|
||||
typedef struct plug_t {
|
||||
char* name;
|
||||
char* version;
|
||||
} plug_t;
|
||||
|
||||
|
||||
static plug_t* p = {0};
|
||||
|
||||
int get_alsa_volume(void) {
|
||||
long min, max, volume;
|
||||
snd_mixer_t* handle;
|
||||
snd_mixer_selem_id_t* sid;
|
||||
const char* card = "default";
|
||||
const char* selem_name = "Master";
|
||||
snd_mixer_open(&handle, 0);
|
||||
snd_mixer_attach(handle, card);
|
||||
snd_mixer_selem_register(handle, NULL, NULL);
|
||||
snd_mixer_load(handle);
|
||||
snd_mixer_selem_id_alloca(&sid);
|
||||
snd_mixer_selem_id_set_index(sid, 0);
|
||||
snd_mixer_selem_id_set_name(sid, selem_name);
|
||||
snd_mixer_elem_t* elem = snd_mixer_find_selem(handle, sid);
|
||||
snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
|
||||
snd_mixer_selem_get_playback_volume(elem, SND_MIXER_SCHN_MONO, &volume);
|
||||
snd_mixer_close(handle);
|
||||
|
||||
int percentage = (volume - min) * 100 / (max - min);
|
||||
return percentage;
|
||||
}
|
||||
|
||||
|
||||
void* plug_init(void) {
|
||||
p = malloc(sizeof(plug_t));
|
||||
assert(p);
|
||||
p->name = "Volume";
|
||||
p->version = "0.0.1";
|
||||
return p;
|
||||
}
|
||||
|
||||
void* plug_pre_reload(void) {
|
||||
return p;
|
||||
}
|
||||
|
||||
void plug_post_reload(void* pp) {
|
||||
p = pp;
|
||||
}
|
||||
|
||||
void plug_poll(char* buf, size_t len) {
|
||||
int volume = get_alsa_volume();
|
||||
snprintf(buf, len, "Volume: %d%%", volume);
|
||||
}
|
||||
|
||||
void plug_free(void) {}
|
||||
Reference in New Issue
Block a user