master ohmu/fastfetch-openbsd / src / detection / sound / sound_sunos.c
ohmu  ·  2026-10-15
  1#include "sound.h"
  2#include "common/debug.h"
  3#include "common/io.h"
  4#include "common/strutil.h"
  5
  6#include <fcntl.h>
  7#include <unistd.h>
  8#if __has_include(<sys/soundcard.h>)
  9    #include <sys/soundcard.h>
 10#else
 11    // Strangely, they don't provide this file on default installation
 12    #include "audio_oss_sunos.h"
 13#endif
 14
 15const char* ffDetectSound(FFSoundOptions* options, FFlist* devices) {
 16    int defaultDev;
 17    {
 18        char mixerp[12];
 19        ssize_t plen = readlink("/dev/audio", mixerp, ARRAY_SIZE(mixerp));
 20        if (plen < 6) {
 21            FF_DEBUG("readlink(/dev/audio) failed");
 22            return "readlink(/dev/audio) failed";
 23        }
 24        defaultDev = mixerp[plen - 1] - '0';
 25        if (defaultDev < 0 || defaultDev > 9) {
 26            FF_DEBUG("Invalid mixer device");
 27            return "Invalid mixer device";
 28        }
 29    }
 30
 31    char path[] = "/dev/mixer0";
 32
 33    FF_STRBUF_AUTO_DESTROY sndstat = ffStrbufCreate();
 34
 35    struct oss_sysinfo info = { .nummixers = 9 };
 36
 37    // The implementation is very different from *BSD's. They call it OSS4
 38    for (int idev = 0; idev < info.nummixers; ++idev) {
 39        bool isMain = idev == defaultDev;
 40        if ((options->soundType & FF_SOUND_TYPE_MAIN) && !isMain) {
 41            continue;
 42        }
 43
 44        path[strlen("/dev/mixer")] = (char) ('0' + idev);
 45        FF_AUTO_CLOSE_FD int fd = open(path, O_RDWR | O_CLOEXEC);
 46        if (fd < 0) {
 47            break;
 48        }
 49
 50        if (idev == 0) {
 51            if (ioctl(fd, SNDCTL_SYSINFO, &info) != 0) {
 52                FF_DEBUG("ioctl(SNDCTL_SYSINFO) failed: %s", strerror(errno));
 53                return "ioctl(SNDCTL_SYSINFO) failed";
 54            }
 55            if (ffAppendFDBuffer(fd, &sndstat)) {
 56                ffStrbufSubstrAfterFirstS(&sndstat, "\nMixers:");
 57            }
 58        }
 59
 60        struct oss_mixerinfo mi = {};
 61        if (ioctl(fd, SNDCTL_MIXERINFO, &mi) < 0) {
 62            continue;
 63        }
 64
 65        if (options->soundType == FF_SOUND_TYPE_ACTIVE && !mi.enabled) {
 66            continue;
 67        }
 68
 69        int volume = -1;
 70        for (int iext = 0; iext < mi.nrext; ++iext) {
 71            struct oss_mixext me = { .dev = mi.dev, .ctrl = iext };
 72            if (ioctl(fd, SNDCTL_MIX_EXTINFO, &me) < 0) {
 73                continue;
 74            }
 75            if (me.flags & MIXF_PCMVOL) {
 76                struct oss_mixer_value mv = { .dev = mi.dev, .ctrl = iext, .timestamp = me.timestamp };
 77                if (ioctl(fd, SNDCTL_MIX_READ, &mv) >= 0) {
 78                    mv.value -= me.minvalue;
 79                    me.maxvalue -= me.minvalue;
 80                    volume = (uint8_t) ((mv.value * 100 + me.maxvalue / 2) / me.maxvalue);
 81                }
 82                break;
 83            }
 84        }
 85        if (volume == -1) {
 86            continue;
 87        }
 88
 89        FFSoundDevice* device = FF_LIST_ADD(FFSoundDevice, *devices);
 90        ffStrbufInitS(&device->identifier, path);
 91        char buf[16];
 92        int bufLen = snprintf(buf, ARRAY_SIZE(buf), "\n%d: ", mi.dev);
 93        assert(bufLen > 3);
 94        const char* pLine = memmem(sndstat.chars, sndstat.length, buf, (size_t) bufLen);
 95        if (pLine) {
 96            pLine += bufLen;
 97            const char* pEnd = strchr(pLine, '\n');
 98            if (!pEnd) {
 99                pEnd = sndstat.chars + sndstat.length;
100            }
101            ffStrbufInitNS(&device->name, (uint32_t) (pEnd - pLine), pLine);
102        } else {
103            ffStrbufInitS(&device->name, mi.name);
104        }
105        ffStrbufTrimRightSpace(&device->name);
106        ffStrbufInitF(&device->platformApi, "%s %s", info.product, info.version);
107        device->volume = (uint8_t) volume;
108        device->type = (mi.enabled ? FF_SOUND_TYPE_ACTIVE : FF_SOUND_TYPE_NONE) |
109            (isMain ? FF_SOUND_TYPE_MAIN : FF_SOUND_TYPE_NONE);
110    }
111
112    return nullptr;
113}