master ohmu/fastfetch-openbsd / src / detection / camera / camera_apple.m
ohmu  ·  2026-10-15
 1#include "camera.h"
 2#include "common/io.h"
 3
 4#import <AVFoundation/AVCaptureDevice.h>
 5
 6// warning: 'AVCaptureDeviceTypeExternalUnknown' is deprecated
 7#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
 8
 9#ifdef MAC_OS_VERSION_14_0
10// To make fastfetch compiled on newer macOS versions runs on older ones
11[[clang::weak_import]] AVF_EXPORT AVCaptureDeviceType const AVCaptureDeviceTypeExternal;
12#endif
13
14const char* ffDetectCamera(FFlist* result)
15{
16    #ifdef MAC_OS_X_VERSION_10_15
17    FF_SUPPRESS_IO(); // #822
18
19    AVCaptureDeviceType deviceType = nullptr;
20
21    #ifdef MAC_OS_VERSION_14_0
22    if (&AVCaptureDeviceTypeExternal)
23        deviceType = AVCaptureDeviceTypeExternal;
24    #endif
25    if (deviceType == nullptr)
26        deviceType = AVCaptureDeviceTypeExternalUnknown;
27
28    AVCaptureDeviceDiscoverySession* session = [AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:@[AVCaptureDeviceTypeBuiltInWideAngleCamera, deviceType]
29                                                                                mediaType:AVMediaTypeVideo
30                                                                                position:AVCaptureDevicePositionUnspecified];
31    if (!session)
32        return "Failed to create AVCaptureDeviceDiscoverySession";
33
34    for (AVCaptureDevice* device in session.devices)
35    {
36        FFCameraResult* camera = FF_LIST_ADD(FFCameraResult, *result);
37        ffStrbufInitS(&camera->name, device.localizedName.UTF8String);
38        ffStrbufInitS(&camera->vendor, device.manufacturer.UTF8String);
39        ffStrbufInitS(&camera->id, device.uniqueID.UTF8String);
40        ffStrbufInit(&camera->colorspace);
41        switch (device.activeColorSpace)
42        {
43            case AVCaptureColorSpace_sRGB: ffStrbufInitStatic(&camera->colorspace, "sRGB"); break;
44            case AVCaptureColorSpace_P3_D65: ffStrbufInitStatic(&camera->colorspace, "P3-D65"); break;
45            case 2 /*AVCaptureColorSpace_HLG_BT2020*/: ffStrbufInitStatic(&camera->colorspace, "BT2020-HLG"); break;
46            case 3 /*AVCaptureColorSpace_AppleLog*/: ffStrbufInitStatic(&camera->colorspace, "AppleLog"); break;
47            case 4 /*AVCaptureColorSpace_AppleLog2*/: ffStrbufInitStatic(&camera->colorspace, "AppleLog2"); break;
48        }
49
50        CMVideoDimensions size = CMVideoFormatDescriptionGetDimensions(device.activeFormat.formatDescription);
51        camera->width = size.width < 0 ? 0 : (uint32_t) size.width;
52        camera->height = size.height < 0 ? 0 : (uint32_t) size.height;
53    }
54    return nullptr;
55    #else
56    return "No support for old MacOS version";
57    #endif
58}