From 6eaea5ca33cfaeea7a7fc9434e082ce1a9e0c6c5 Mon Sep 17 00:00:00 2001 From: Malte Voos Date: Wed, 3 Apr 2024 18:12:50 +0200 Subject: init --- fakefbdev/SharedBuffer.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 fakefbdev/SharedBuffer.cpp (limited to 'fakefbdev/SharedBuffer.cpp') diff --git a/fakefbdev/SharedBuffer.cpp b/fakefbdev/SharedBuffer.cpp new file mode 100644 index 0000000..fbc3121 --- /dev/null +++ b/fakefbdev/SharedBuffer.cpp @@ -0,0 +1,46 @@ +#include "SharedBuffer.h" + +#include +#include +#include +#include +#include +#include +#include + +SharedFB::SharedFB(const char* path) : fd(shm_open(path, O_RDWR, 0755)) { + if (fd == -1) { + fd = shm_open(path, O_RDWR | O_CREAT, 0755); + } + + if (fd < 0) { + perror("Can't open shm"); + return; + } + + ftruncate(fd, fb_size); + mem = (uint16_t*)mmap(nullptr, fb_size, PROT_WRITE, MAP_SHARED, fd, 0); +} + +SharedFB::~SharedFB() { + if (mem != nullptr) { + munmap(mem, fb_size); + } +} + +SharedFB::SharedFB(SharedFB&& other) noexcept : fd(other.fd), mem(other.mem) { + other.fd = -1; + other.mem = nullptr; +} + +SharedFB& +SharedFB::operator=(SharedFB&& other) noexcept { + // TODO: release + this->fd = other.fd; + this->mem = other.mem; + + other.fd = -1; + other.mem = nullptr; + + return *this; +} -- cgit 1.4.1