From ab752cafbde3755e7a09b3ecae021a6727cb7112 Mon Sep 17 00:00:00 2001 From: PancakeTAS Date: Thu, 25 Jun 2026 11:19:54 +0200 Subject: Project hierarchy overhaul --- CMakeLists.txt | 2 +- include/drm++/drm.hpp | 3 +- include/drm++/gem.hpp | 3 +- include/drm++/helper.hpp | 41 ---------------------- include/drm++/helper/ioctl.hpp | 78 +++++++++++++++++++++++++++++++++++++++++ include/drm++/helper/macros.hpp | 21 +++++++++++ include/drm++/helper/types.hpp | 22 ++++++++++++ include/drm++/ioctl.hpp | 77 ---------------------------------------- include/drm++/syncobject.hpp | 3 +- src/drm.cpp | 4 +-- src/gem.cpp | 4 +-- src/helper/ioctl.cpp | 47 +++++++++++++++++++++++++ src/ioctl.cpp | 47 ------------------------- src/syncobject.cpp | 4 +-- 14 files changed, 181 insertions(+), 175 deletions(-) delete mode 100644 include/drm++/helper.hpp create mode 100644 include/drm++/helper/ioctl.hpp create mode 100644 include/drm++/helper/macros.hpp create mode 100644 include/drm++/helper/types.hpp delete mode 100644 include/drm++/ioctl.hpp create mode 100644 src/helper/ioctl.cpp delete mode 100644 src/ioctl.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index a37e540..52e6ceb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,9 +8,9 @@ project(drm++ include(cmake/Diagnostics.cmake) add_library(drm++ SHARED + "src/helper/ioctl.cpp" "src/drm.cpp" "src/gem.cpp" - "src/ioctl.cpp" "src/syncobject.cpp") target_include_directories(drm++ diff --git a/include/drm++/drm.hpp b/include/drm++/drm.hpp index c75495b..2833adf 100644 --- a/include/drm++/drm.hpp +++ b/include/drm++/drm.hpp @@ -130,7 +130,8 @@ /// This library requires a C++ compiler capable of C++20 or newer. /// -#include "drm++/helper.hpp" +#include "helper/macros.hpp" +#include "helper/types.hpp" namespace drm { diff --git a/include/drm++/gem.hpp b/include/drm++/gem.hpp index 35829ee..e3e3306 100644 --- a/include/drm++/gem.hpp +++ b/include/drm++/gem.hpp @@ -68,7 +68,8 @@ /// This table is not exhaustive, but it is not recommended to use any other values for bpp. /// -#include "drm++/helper.hpp" +#include "helper/macros.hpp" +#include "helper/types.hpp" #include diff --git a/include/drm++/helper.hpp b/include/drm++/helper.hpp deleted file mode 100644 index 7b927bf..0000000 --- a/include/drm++/helper.hpp +++ /dev/null @@ -1,41 +0,0 @@ -/* SPDX-License-Identifier: MIT */ - -#pragma once - -#include -#include - -namespace drm { - -using u8 = uint8_t; -using u16 = uint16_t; -using u32 = uint32_t; -using u64 = uint64_t; -using s8 = int8_t; -using s16 = int16_t; -using s32 = int32_t; -using s64 = int64_t; - -template -using ref = std::reference_wrapper; - -} - -/* Helpful macros */ -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wunused-macros" - -#define GETTER(name) [[nodiscard]] auto name() const { return m_##name; } -#define DEFAULT_OPERATORS(classname) \ - classname(const classname&) = default; \ - classname& operator=(const classname&) = default; \ - classname(classname&&) = default; \ - classname& operator=(classname&&) = default; -#define NO_COPY(classname) \ - classname(const classname&) = delete; \ - classname& operator=(const classname&) = delete; -#define NO_MOVE(classname) \ - classname(classname&&) = delete; \ - classname& operator=(classname&&) = delete; - -#pragma clang diagnostic pop diff --git a/include/drm++/helper/ioctl.hpp b/include/drm++/helper/ioctl.hpp new file mode 100644 index 0000000..fe87f3c --- /dev/null +++ b/include/drm++/helper/ioctl.hpp @@ -0,0 +1,78 @@ +/* SPDX-License-Identifier: MIT */ + +#pragma once + +#include "macros.hpp" +#include "types.hpp" + +#include +#include + +#include + +namespace drm::ioctl { + +/// Type of ioctl() error. +enum class Error : u8 { + BadFileDescriptor, + InvalidArgument, + NotSupported, + Other //!< Check errno field +}; + +/// Exception class wrapping around ioctl(). +class Exception : public std::exception { +public: + /// Construct an exception from errno. + explicit Exception(int fd, unsigned long op); + + /// Get the error message. + [[nodiscard]] const char* what() const noexcept override { + return this->m_what.c_str(); + } + + // Private access + GETTER(fd) + GETTER(op) + GETTER(code) + GETTER(syserrno) + + // Default operators and destructor + DEFAULT_OPERATORS(Exception) + ~Exception() override; +private: + int m_fd; + unsigned long m_op; + Error m_code; + int m_syserrno; + std::string m_what; +}; + +/// Perform an ioctl() call. +/// @throws drm::ioctl::Exception on failure +inline void perform(int fd, unsigned long op) { + if (::ioctl(fd, op, nullptr) < 0) + throw Exception(fd, op); +} + +/// Perform an ioctl() call. +/// @throws drm::ioctl::Exception on failure +template +inline T perform(int fd, unsigned long op) { + T data{}; + + if (::ioctl(fd, op, &data) < 0) + throw Exception(fd, op); + + return data; +} + +/// Perform an ioctl() call. +/// @throws drm::ioctl::Exception on failure +template +inline void perform(int fd, unsigned long op, T& data) { + if (::ioctl(fd, op, &data) < 0) + throw Exception(fd, op); +} + +} diff --git a/include/drm++/helper/macros.hpp b/include/drm++/helper/macros.hpp new file mode 100644 index 0000000..3187177 --- /dev/null +++ b/include/drm++/helper/macros.hpp @@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: MIT */ + +#pragma once + +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wunused-macros" + +#define GETTER(name) [[nodiscard]] auto name() const { return m_##name; } +#define DEFAULT_OPERATORS(classname) \ + classname(const classname&) = default; \ + classname& operator=(const classname&) = default; \ + classname(classname&&) = default; \ + classname& operator=(classname&&) = default; +#define NO_COPY(classname) \ + classname(const classname&) = delete; \ + classname& operator=(const classname&) = delete; +#define NO_MOVE(classname) \ + classname(classname&&) = delete; \ + classname& operator=(classname&&) = delete; + +#pragma clang diagnostic pop diff --git a/include/drm++/helper/types.hpp b/include/drm++/helper/types.hpp new file mode 100644 index 0000000..0669700 --- /dev/null +++ b/include/drm++/helper/types.hpp @@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: MIT */ + +#pragma once + +#include +#include + +namespace drm { + +using u8 = uint8_t; +using u16 = uint16_t; +using u32 = uint32_t; +using u64 = uint64_t; +using s8 = int8_t; +using s16 = int16_t; +using s32 = int32_t; +using s64 = int64_t; + +template +using ref = std::reference_wrapper; + +} diff --git a/include/drm++/ioctl.hpp b/include/drm++/ioctl.hpp deleted file mode 100644 index 49f8a1c..0000000 --- a/include/drm++/ioctl.hpp +++ /dev/null @@ -1,77 +0,0 @@ -/* SPDX-License-Identifier: MIT */ - -#pragma once - -#include "drm++/helper.hpp" - -#include -#include - -#include - -namespace drm::ioctl { - -/// Type of ioctl() error. -enum class Error : u8 { - BadFileDescriptor, - InvalidArgument, - NotSupported, - Other //!< Check errno field -}; - -/// Exception class wrapping around ioctl(). -class Exception : public std::exception { -public: - /// Construct an exception from errno. - explicit Exception(int fd, unsigned long op); - - /// Get the error message. - [[nodiscard]] const char* what() const noexcept override { - return this->m_what.c_str(); - } - - // Private access - GETTER(fd) - GETTER(op) - GETTER(code) - GETTER(syserrno) - - // Default operators and destructor - DEFAULT_OPERATORS(Exception) - ~Exception() override; -private: - int m_fd; - unsigned long m_op; - Error m_code; - int m_syserrno; - std::string m_what; -}; - -/// Perform an ioctl() call. -/// @throws drm::ioctl::Exception on failure -inline void perform(int fd, unsigned long op) { - if (::ioctl(fd, op, nullptr) < 0) - throw Exception(fd, op); -} - -/// Perform an ioctl() call. -/// @throws drm::ioctl::Exception on failure -template -inline T perform(int fd, unsigned long op) { - T data{}; - - if (::ioctl(fd, op, &data) < 0) - throw Exception(fd, op); - - return data; -} - -/// Perform an ioctl() call. -/// @throws drm::ioctl::Exception on failure -template -inline void perform(int fd, unsigned long op, T& data) { - if (::ioctl(fd, op, &data) < 0) - throw Exception(fd, op); -} - -} diff --git a/include/drm++/syncobject.hpp b/include/drm++/syncobject.hpp index beee332..b5a032d 100644 --- a/include/drm++/syncobject.hpp +++ b/include/drm++/syncobject.hpp @@ -37,7 +37,8 @@ /// this by providing separate classes for each type of sync object. /// -#include "drm++/helper.hpp" +#include "helper/macros.hpp" +#include "helper/types.hpp" #include #include diff --git a/src/drm.cpp b/src/drm.cpp index 6bb0a3a..3046412 100644 --- a/src/drm.cpp +++ b/src/drm.cpp @@ -1,8 +1,8 @@ /* SPDX-License-Identifier: MIT */ #include "drm++/drm.hpp" -#include "drm++/helper.hpp" -#include "drm++/ioctl.hpp" +#include "drm++/helper/ioctl.hpp" +#include "drm++/helper/types.hpp" #include diff --git a/src/gem.cpp b/src/gem.cpp index b6b3d38..82a459d 100644 --- a/src/gem.cpp +++ b/src/gem.cpp @@ -1,8 +1,8 @@ /* SPDX-License-Identifier: MIT */ #include "drm++/gem.hpp" -#include "drm++/helper.hpp" -#include "drm++/ioctl.hpp" +#include "drm++/helper/ioctl.hpp" +#include "drm++/helper/types.hpp" #include #include diff --git a/src/helper/ioctl.cpp b/src/helper/ioctl.cpp new file mode 100644 index 0000000..21a1ccb --- /dev/null +++ b/src/helper/ioctl.cpp @@ -0,0 +1,47 @@ +/* SPDX-License-Identifier: MIT */ + +#include "drm++/helper/ioctl.hpp" + +#include +#include + +#include + +using namespace drm::ioctl; + +Exception::Exception(int fd, unsigned long op) + : m_fd(fd), m_op(op), m_syserrno(errno) { + switch (errno) { + case EBADF: + this->m_code = Error::BadFileDescriptor; + this->m_what = std::format( + "ioctl({}, 0x{:x}) failed: Invalid file descriptor", + this->m_fd, this->m_op + ); + break; + case EINVAL: + case ENOTTY: + this->m_code = Error::InvalidArgument; + this->m_what = std::format( + "ioctl({}, 0x{:x}) failed: Invalid argument (errno {})", + this->m_fd, this->m_op, this->m_syserrno + ); + break; + case ENOTSUP: + this->m_code = Error::NotSupported; + this->m_what = std::format( + "ioctl({}, 0x{:x}) failed: Operation not supported", + this->m_fd, this->m_op + ); + break; + default: + this->m_code = Error::Other; + this->m_what = std::format( + "ioctl({}, 0x{:x}) failed: Unknown error (errno {})", + this->m_fd, this->m_op, this->m_syserrno + ); + } +} + + +Exception::~Exception() = default; diff --git a/src/ioctl.cpp b/src/ioctl.cpp deleted file mode 100644 index 8b4962f..0000000 --- a/src/ioctl.cpp +++ /dev/null @@ -1,47 +0,0 @@ -/* SPDX-License-Identifier: MIT */ - -#include "drm++/ioctl.hpp" - -#include -#include - -#include - -using namespace drm::ioctl; - -Exception::Exception(int fd, unsigned long op) - : m_fd(fd), m_op(op), m_syserrno(errno) { - switch (errno) { - case EBADF: - this->m_code = Error::BadFileDescriptor; - this->m_what = std::format( - "ioctl({}, 0x{:x}) failed: Invalid file descriptor", - this->m_fd, this->m_op - ); - break; - case EINVAL: - case ENOTTY: - this->m_code = Error::InvalidArgument; - this->m_what = std::format( - "ioctl({}, 0x{:x}) failed: Invalid argument (errno {})", - this->m_fd, this->m_op, this->m_syserrno - ); - break; - case ENOTSUP: - this->m_code = Error::NotSupported; - this->m_what = std::format( - "ioctl({}, 0x{:x}) failed: Operation not supported", - this->m_fd, this->m_op - ); - break; - default: - this->m_code = Error::Other; - this->m_what = std::format( - "ioctl({}, 0x{:x}) failed: Unknown error (errno {})", - this->m_fd, this->m_op, this->m_syserrno - ); - } -} - - -Exception::~Exception() = default; diff --git a/src/syncobject.cpp b/src/syncobject.cpp index 4ec1a9c..6d7f68e 100644 --- a/src/syncobject.cpp +++ b/src/syncobject.cpp @@ -1,8 +1,8 @@ /* SPDX-License-Identifier: MIT */ #include "drm++/syncobject.hpp" -#include "drm++/helper.hpp" -#include "drm++/ioctl.hpp" +#include "drm++/helper/ioctl.hpp" +#include "drm++/helper/types.hpp" #include #include -- cgit v1.3.1