master ohmu/fastfetch-openbsd / src / detection / brightness / brightness_obsd.c
ohmu  ·  2026-10-15
 1#include "brightness.h"
 2#include "common/debug.h"
 3#include "common/io.h"
 4
 5#include <dev/wscons/wsconsio.h>
 6#include <sys/ioctl.h>
 7#include <unistd.h>
 8#include <fcntl.h>
 9
10const char* ffDetectBrightness([[maybe_unused]] FFBrightnessOptions* options, FFlist* result) {
11    char path[] = "/dev/ttyCX";
12    for (char i = '0'; i <= '9'; ++i) {
13        path[strlen("/dev/ttyC")] = i;
14
15        FF_AUTO_CLOSE_FD int devfd = open(path, O_RDONLY | O_CLOEXEC);
16
17        if (devfd < 0) {
18            if (errno == EACCES && i == '0') {
19                FF_DEBUG("open(%s) failed: %s", path, strerror(errno));
20                return "Permission denied when opening tty device";
21            }
22            if (errno == ENOENT) {
23                break;
24            }
25            continue;
26        }
27
28        struct wsdisplay_param param = {
29            .param = WSDISPLAYIO_PARAM_BRIGHTNESS,
30        };
31
32        if (ioctl(devfd, WSDISPLAYIO_GETPARAM, &param) < 0) {
33            continue;
34        }
35
36        FFBrightnessResult* brightness = FF_LIST_ADD(FFBrightnessResult, *result);
37        ffStrbufInitF(&brightness->name, "ttyC%c", i);
38
39        brightness->max = param.max;
40        brightness->min = param.min;
41        brightness->current = param.curval;
42        brightness->builtin = true;
43    }
44
45    return nullptr;
46}