Files
fastfetch/src/detection/camera/camera_linux.c
T

95 lines
3.2 KiB
C
Raw Normal View History

2024-02-02 22:14:25 +08:00
#include "camera.h"
2026-01-06 09:48:38 +08:00
#include "common/io.h"
2024-02-02 22:14:25 +08:00
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
2024-07-30 11:06:27 +08:00
#if FF_HAVE_LINUX_VIDEODEV2
2026-03-29 09:28:49 +08:00
# include <linux/videodev2.h>
2025-04-23 20:09:08 +08:00
#elif __has_include(<sys/videoio.h>) // OpenBSD
2026-03-29 09:28:49 +08:00
# include <sys/videoio.h>
# define FF_HAVE_LINUX_VIDEODEV2 1
2024-02-02 22:14:25 +08:00
#endif
2026-03-29 09:28:49 +08:00
const char* ffDetectCamera(FFlist* result) {
2024-07-30 11:06:27 +08:00
#if FF_HAVE_LINUX_VIDEODEV2
2024-02-02 22:14:25 +08:00
char path[] = "/dev/videoN";
2026-03-29 09:28:49 +08:00
for (uint32_t i = 0; i <= 9; ++i) {
2024-10-18 16:15:48 +08:00
path[ARRAY_SIZE(path) - 2] = (char) (i + '0');
2025-06-19 18:11:54 +08:00
FF_AUTO_CLOSE_FD int fd = open(path, O_RDONLY | O_CLOEXEC);
2026-03-29 09:28:49 +08:00
if (fd < 0) {
if (errno == ENOENT) {
2025-04-23 20:09:08 +08:00
break;
2026-03-29 09:28:49 +08:00
}
if (errno == ENXIO) {
2025-04-23 20:09:08 +08:00
continue;
2026-03-29 09:28:49 +08:00
}
2025-04-23 20:09:08 +08:00
return "Failed to open /dev/videoN";
}
2024-02-02 22:14:25 +08:00
struct v4l2_capability cap = {};
2026-03-29 09:28:49 +08:00
if (ioctl(fd, VIDIOC_QUERYCAP, &cap) < 0 || !(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
2024-02-02 22:14:25 +08:00
continue;
2026-03-29 09:28:49 +08:00
}
2024-02-02 22:14:25 +08:00
2026-03-30 10:27:23 +08:00
struct v4l2_format fmt = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
2026-03-29 09:28:49 +08:00
if (ioctl(fd, VIDIOC_G_FMT, &fmt) < 0) {
2024-02-02 22:14:25 +08:00
continue;
2026-03-29 09:28:49 +08:00
}
2024-02-02 22:14:25 +08:00
FFCameraResult* camera = (FFCameraResult*) ffListAdd(result);
ffStrbufInitS(&camera->name, (const char*) cap.card);
ffStrbufInit(&camera->vendor);
ffStrbufInitS(&camera->id, (const char*) cap.bus_info);
2026-03-29 09:28:49 +08:00
switch (fmt.fmt.pix.colorspace) {
case V4L2_COLORSPACE_SMPTE170M:
ffStrbufInitStatic(&camera->colorspace, "SMPTE 170M");
break;
case V4L2_COLORSPACE_SMPTE240M:
ffStrbufInitStatic(&camera->colorspace, "SMPTE 240M");
break;
case V4L2_COLORSPACE_BT878:
ffStrbufInitStatic(&camera->colorspace, "BT.878");
break;
case V4L2_COLORSPACE_470_SYSTEM_M:
ffStrbufInitStatic(&camera->colorspace, "NTSC");
break;
case V4L2_COLORSPACE_470_SYSTEM_BG:
ffStrbufInitStatic(&camera->colorspace, "EBU 3213");
break;
case V4L2_COLORSPACE_JPEG:
ffStrbufInitStatic(&camera->colorspace, "JPEG");
break;
case V4L2_COLORSPACE_REC709:
case V4L2_COLORSPACE_SRGB:
ffStrbufInitStatic(&camera->colorspace, "sRGB");
break;
case 9 /* V4L2_COLORSPACE_OPRGB */:
ffStrbufInitStatic(&camera->colorspace, "Adobe RGB");
break;
case 10 /* V4L2_COLORSPACE_BT2020 */:
ffStrbufInitStatic(&camera->colorspace, "BT.2020");
break;
case 11 /* V4L2_COLORSPACE_RAW */:
ffStrbufInitStatic(&camera->colorspace, "RAW");
break;
case 12 /* V4L2_COLORSPACE_DCI_P3 */:
ffStrbufInitStatic(&camera->colorspace, "DCI-P3");
break;
default:
ffStrbufInit(&camera->colorspace);
break;
2024-02-04 10:10:55 +08:00
}
2024-02-02 22:14:25 +08:00
camera->width = fmt.fmt.pix.width;
camera->height = fmt.fmt.pix.height;
}
return NULL;
#else
2024-02-04 14:54:53 +08:00
FF_UNUSED(result);
2024-02-02 22:14:25 +08:00
return "Fastfetch was compiled without <linux/videodev2.h>";
#endif
}