From 4d2affb862c85e1b26e4b187d8200a86d02bd31b Mon Sep 17 00:00:00 2001 From: amass <168062547@qq.com> Date: Tue, 1 Oct 2024 23:33:22 +0800 Subject: [PATCH] update for linux. --- Analyser/Application.cpp | 4 ---- Peripheral/CMakeLists.txt | 2 +- Peripheral/DeviceDiscovery.cpp | 38 +++++++++++++++++++++++++++++++++- Peripheral/DeviceDiscovery.h | 2 -- 4 files changed, 38 insertions(+), 8 deletions(-) diff --git a/Analyser/Application.cpp b/Analyser/Application.cpp index 5c3a02a..e468508 100644 --- a/Analyser/Application.cpp +++ b/Analyser/Application.cpp @@ -20,9 +20,7 @@ #include #include #include -#ifdef WIN32 #include "DeviceDiscovery.h" -#endif constexpr uint32_t ImageSliceSize = (4000 - 32); @@ -123,13 +121,11 @@ QStringList Application::availableSerialPorts() const { QStringList Application::availableUsbVideoCameras() const { QStringList ret; -#ifdef WIN32 DeviceDiscovery d; auto devices = d.devices(); for (auto &device : devices) { ret << QString::fromStdString(device); } -#endif return ret; } diff --git a/Peripheral/CMakeLists.txt b/Peripheral/CMakeLists.txt index 3e49664..93164ea 100644 --- a/Peripheral/CMakeLists.txt +++ b/Peripheral/CMakeLists.txt @@ -4,7 +4,7 @@ find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS SerialPort) set(CMAKE_AUTOMOC ON) add_library(Peripheral - $<$:DeviceDiscovery.h DeviceDiscovery.cpp> + DeviceDiscovery.h DeviceDiscovery.cpp CdcUpdater.h CdcUpdater.cpp ) diff --git a/Peripheral/DeviceDiscovery.cpp b/Peripheral/DeviceDiscovery.cpp index 8ffee6e..b7fb216 100644 --- a/Peripheral/DeviceDiscovery.cpp +++ b/Peripheral/DeviceDiscovery.cpp @@ -3,14 +3,25 @@ #include "StringUtility.h" #include #include +#include +#ifdef Q_OS_WIN #include #include +#include +#include +#else +#include +#include +#include +#endif struct DeviceDiscovery::Device { - Device(IMFMediaSource *source); ~Device(); +#ifdef Q_OS_WIN + Device(IMFMediaSource *source); IMFMediaSource *source = nullptr; IMFSourceReader *reader = nullptr; +#endif }; template @@ -24,6 +35,7 @@ void SafeRelease(T **ppT) { DeviceDiscovery::DeviceDiscovery() { } +#ifdef Q_OS_WIN static std::string deviceName(IMFActivate *device) { std::string ret; WCHAR *friendlyName = nullptr; @@ -186,12 +198,36 @@ DeviceDiscovery::Device::Device(IMFMediaSource *source) : source(source) { LOG(error) << "MFCreateSourceReaderFromMediaSource() failed, result: " << result; } } +#else +std::vector DeviceDiscovery::devices() { + std::vector ret; + for (const auto &entry : std::filesystem::directory_iterator("/dev")) { + if (entry.is_character_file() && entry.path().string().find("video") != std::string::npos) { + int fd = open(entry.path().c_str(), O_RDWR); + if (fd < 0) { + continue; + } + + struct v4l2_capability cap; + if (ioctl(fd, VIDIOC_QUERYCAP, &cap) == 0) { + if (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) { + ret.push_back(entry.path().string()); + } + } + close(fd); + } + } + return ret; +} +#endif DeviceDiscovery::Device::~Device() { +#ifdef Q_OS_WIN if (source != nullptr) { source->Release(); } if (reader != nullptr) { reader->Release(); } +#endif } diff --git a/Peripheral/DeviceDiscovery.h b/Peripheral/DeviceDiscovery.h index 4f16963..566d80e 100644 --- a/Peripheral/DeviceDiscovery.h +++ b/Peripheral/DeviceDiscovery.h @@ -2,8 +2,6 @@ #define __DEVICEDISCOVERY_H__ #include -#include -#include #include #include #include