This repository has been archived on 2023-02-21. You can view files and clone it, but cannot push or open issues or pull requests.
quick-text-bar/modules/oss.c

64 lines
1.4 KiB
C
Raw Normal View History

2022-08-28 14:27:03 -07:00
#include "oss.h"
#include "../util.h"
#include <fcntl.h>
#include <unistd.h>
#include <sys/soundcard.h>
#include <stdio.h>
typedef struct {
int fd;
int pcm;
int vol;
int mute;
} mixer_data;
static mixer_data main_mixer;
#define ioctl_checked(fd, request, arg) _ioctl_checked(fd, request, #request, arg)
static void _ioctl_checked(int fd, unsigned long request, const char *rq_name, void *arg) {
if (ioctl(fd, request, arg) < 0) {
qtb_log("ioctl request %s failed", rq_name);
qtb_die();
}
}
static void open_mixer(const char *path, mixer_data *mixer) {
mixer->fd = open(path, O_RDONLY);
if (mixer->fd < 0) {
qtb_log("could not open mixer '%s'", path);
qtb_die();
}
}
static void read_mixer(mixer_data *mixer) {
ioctl_checked(mixer->fd, SOUND_MIXER_READ_PCM, &mixer->pcm);
mixer->pcm >>= 8;
ioctl_checked(mixer->fd, SOUND_MIXER_READ_VOLUME, &mixer->vol);
mixer->vol >>= 8;
mixer->mute = !(mixer->vol * mixer->pcm);
}
void oss_module_init() {
open_mixer("/dev/mixer", &main_mixer);
}
static const char *get_volume_icon(int volume, int mute) {
if (mute) {
return "";
} else if (volume > 50) {
return "";
} else if (volume > 0) {
return "奔";
}
return "";
}
char *oss_module_poll() {
read_mixer(&main_mixer);
const char *icon = get_volume_icon(main_mixer.vol, main_mixer.mute);
char *buff = qtb_malloc(32);
sprintf(buff, "%s%3d%%",icon, main_mixer.vol);
return buff;
}