2024-02-02 15:09:54 +08:00
|
|
|
#include "camera.h"
|
2026-01-06 09:48:38 +08:00
|
|
|
#include "common/io.h"
|
2024-02-02 15:09:54 +08:00
|
|
|
|
|
|
|
|
#import <AVFoundation/AVCaptureDevice.h>
|
|
|
|
|
|
2024-05-06 09:25:06 +08:00
|
|
|
// warning: 'AVCaptureDeviceTypeExternalUnknown' is deprecated
|
|
|
|
|
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
|
|
|
|
|
2025-02-05 14:40:49 +08:00
|
|
|
#ifdef MAC_OS_VERSION_14_0
|
|
|
|
|
// To make fastfetch compiled on newer macOS versions runs on older ones
|
2026-04-09 16:20:59 +08:00
|
|
|
AVF_EXPORT FF_A_WEAK_IMPORT AVCaptureDeviceType const AVCaptureDeviceTypeExternal;
|
2025-02-05 14:40:49 +08:00
|
|
|
#endif
|
|
|
|
|
|
2024-02-02 15:09:54 +08:00
|
|
|
const char* ffDetectCamera(FFlist* result)
|
|
|
|
|
{
|
2024-05-19 09:31:08 +08:00
|
|
|
#ifdef MAC_OS_X_VERSION_10_15
|
2024-04-30 19:22:45 +08:00
|
|
|
FF_SUPPRESS_IO(); // #822
|
2024-05-06 09:25:06 +08:00
|
|
|
|
2026-07-10 18:08:04 +08:00
|
|
|
AVCaptureDeviceType deviceType = nullptr;
|
2024-05-06 09:25:06 +08:00
|
|
|
|
|
|
|
|
#ifdef MAC_OS_VERSION_14_0
|
2025-03-02 10:20:06 +08:00
|
|
|
// Strangely `@available(macOS 14.0, *)` doesn't work here (#1594)
|
2025-03-02 10:43:54 +08:00
|
|
|
if (@available(macOS 14.0, *))
|
2024-05-06 09:25:06 +08:00
|
|
|
{
|
2025-03-02 10:43:54 +08:00
|
|
|
if (&AVCaptureDeviceTypeExternal)
|
|
|
|
|
deviceType = AVCaptureDeviceTypeExternal;
|
2024-05-06 09:25:06 +08:00
|
|
|
}
|
|
|
|
|
#endif
|
2026-07-10 18:08:04 +08:00
|
|
|
if (deviceType == nullptr)
|
2024-05-06 09:25:06 +08:00
|
|
|
deviceType = AVCaptureDeviceTypeExternalUnknown;
|
|
|
|
|
|
|
|
|
|
AVCaptureDeviceDiscoverySession* session = [AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:@[AVCaptureDeviceTypeBuiltInWideAngleCamera, deviceType]
|
2024-04-30 18:56:45 +08:00
|
|
|
mediaType:AVMediaTypeVideo
|
|
|
|
|
position:AVCaptureDevicePositionUnspecified];
|
2024-02-02 15:09:54 +08:00
|
|
|
if (!session)
|
|
|
|
|
return "Failed to create AVCaptureDeviceDiscoverySession";
|
|
|
|
|
|
|
|
|
|
for (AVCaptureDevice* device in session.devices)
|
|
|
|
|
{
|
2026-04-14 10:41:18 +08:00
|
|
|
FFCameraResult* camera = FF_LIST_ADD(FFCameraResult, *result);
|
2024-02-02 15:09:54 +08:00
|
|
|
ffStrbufInitS(&camera->name, device.localizedName.UTF8String);
|
|
|
|
|
ffStrbufInitS(&camera->vendor, device.manufacturer.UTF8String);
|
|
|
|
|
ffStrbufInitS(&camera->id, device.uniqueID.UTF8String);
|
2024-02-04 10:26:06 +08:00
|
|
|
switch (device.activeColorSpace)
|
|
|
|
|
{
|
|
|
|
|
case AVCaptureColorSpace_sRGB: ffStrbufInitStatic(&camera->colorspace, "sRGB"); break;
|
|
|
|
|
case AVCaptureColorSpace_P3_D65: ffStrbufInitStatic(&camera->colorspace, "P3-D65"); break;
|
|
|
|
|
case 2 /*AVCaptureColorSpace_HLG_BT2020*/: ffStrbufInitStatic(&camera->colorspace, "BT2020-HLG"); break;
|
|
|
|
|
case 3 /*AVCaptureColorSpace_AppleLog*/: ffStrbufInitStatic(&camera->colorspace, "AppleLog"); break;
|
2026-04-13 16:16:57 +08:00
|
|
|
case 4 /*AVCaptureColorSpace_AppleLog2*/: ffStrbufInitStatic(&camera->colorspace, "AppleLog2"); break;
|
2024-02-04 10:26:06 +08:00
|
|
|
}
|
2024-02-02 15:09:54 +08:00
|
|
|
|
|
|
|
|
CMVideoDimensions size = CMVideoFormatDescriptionGetDimensions(device.activeFormat.formatDescription);
|
|
|
|
|
camera->width = size.width < 0 ? 0 : (uint32_t) size.width;
|
|
|
|
|
camera->height = size.height < 0 ? 0 : (uint32_t) size.height;
|
|
|
|
|
}
|
2026-07-10 18:08:04 +08:00
|
|
|
return nullptr;
|
2024-05-19 09:31:08 +08:00
|
|
|
#else
|
|
|
|
|
return "No support for old MacOS version";
|
|
|
|
|
#endif
|
2024-02-02 15:09:54 +08:00
|
|
|
}
|