about summary refs log tree commit diff
path: root/fakefbdev/SharedBuffer.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'fakefbdev/SharedBuffer.cpp')
-rw-r--r--fakefbdev/SharedBuffer.cpp46
1 files changed, 46 insertions, 0 deletions
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 <cerrno>
+#include <cstdio>
+#include <cstring>
+#include <fcntl.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+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;
+}