Compare commits

...

4 Commits

Author SHA1 Message Date
a5e133084b
Volume plugin
By: xomf
2024-06-01 21:29:35 +03:00
e45a17a138
Merge branch 'main' of git.mcorangehq.xyz:XOR64/dim 2024-05-31 21:07:02 +03:00
7cfe67c002
Merge branch 'main' of git.mcorangehq.xyz:XOR64/dim 2024-05-31 02:16:27 +03:00
b72ba45c79
uwu 2024-05-30 22:54:43 +03:00
2 changed files with 64 additions and 0 deletions

View File

@ -15,6 +15,7 @@ $(BIN): src/main.c src/plug.c src/socket.c src/util.c
modules/%.dim: src/modules/%.c modules/%.dim: src/modules/%.c
mkdir -p $(dir $@)
$(CC) -o $@ $^ -rdynamic -shared -fPIC $(CCARGS) $(CC) -o $@ $^ -rdynamic -shared -fPIC $(CCARGS)
compile_commands.json: compile_commands.json:

63
src/modules/volume.h Normal file
View 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) {}