From 7bb6846b33366d0351af88e9d3e1927073b1543c Mon Sep 17 00:00:00 2001 From: Julian Hinxlage Date: Tue, 12 Nov 2019 15:40:36 +0100 Subject: [PATCH] ~ fixed registerEvent --- src/KeyCode.cpp | 4 ++-- src/KeyCode.h | 6 ++--- src/SharedMemoryAccess.cpp | 48 +++++++++++++++++--------------------- src/vkvm.h | 2 +- 4 files changed, 28 insertions(+), 32 deletions(-) diff --git a/src/KeyCode.cpp b/src/KeyCode.cpp index 950ddfc..49c4a6e 100644 --- a/src/KeyCode.cpp +++ b/src/KeyCode.cpp @@ -1,9 +1,9 @@ #include "KeyCode.h" -KeyCode::KeyCode(int16_t value) noexcept : value(value) {} +KeyCode::KeyCode(int value) noexcept : value(value) {} -int16_t KeyCode::getValue() { +int KeyCode::getValue() { return value; } diff --git a/src/KeyCode.h b/src/KeyCode.h index f3452fe..eb97ab9 100644 --- a/src/KeyCode.h +++ b/src/KeyCode.h @@ -5,10 +5,10 @@ class KeyCode { private: - int16_t value; + int value; public: - explicit KeyCode(int16_t value) noexcept; - int16_t getValue(); + explicit KeyCode(int value) noexcept; + int getValue(); }; const static KeyCode Backspace = KeyCode(8); diff --git a/src/SharedMemoryAccess.cpp b/src/SharedMemoryAccess.cpp index e410c3f..636b09c 100644 --- a/src/SharedMemoryAccess.cpp +++ b/src/SharedMemoryAccess.cpp @@ -69,35 +69,31 @@ void writeSharedMemory(char *data, int size, int offset) { } } -void getSharedMemory(char *address, int size, int offset) { - int shmId = shmget(impl.sharedMemoryKey, 0, 0); - if (shmId >= 0) { - auto *shm_pointer = reinterpret_cast(shmat(shmId, nullptr, 0)); - semaphoreOperation(LOCK); - memcpy(address, shm_pointer + offset, size); - shmdt(shm_pointer); //close shm_pointer - semaphoreOperation(UNLOCK); +char *getSharedMemory() { + constexpr bool useLocal = false; + + if(useLocal){ + //using a local buffer for shared memory testing + if (localSharedMemory.empty()) { + initSemaphore(); + localSharedMemory.resize(impl.sharedMemorySize * 1024); + } + return &localSharedMemory[0]; + }else { + int shmId = shmget(impl.sharedMemoryKey, NULL, 0); + if(shmId < 0) { + // we could return -1, not sure if it will be useful to continue after there is no shm allocated for any reason + return nullptr; + } else { + return (char *) shmat(shmId, NULL, 0); + } } } -char *getSharedMemory() { - /* - int shmId = shmget(impl.sharedMemoryKey, NULL, 0); - if(shmId < 0) { - return nullptr; - // we could return -1, not sure if it will be useful to continue after there is no shm allocated for any reason - } else { - return (char *) shmat(shmId, NULL, 0); - } - */ - - //using a local buffer for shared memory testing - if (localSharedMemory.empty()) { - initSemaphore(); - localSharedMemory.resize(impl.sharedMemorySize * 1024); - } - - return &localSharedMemory[0]; +void getSharedMemory(char *address, int size, int offset) { + lockSharedMemory(); + memcpy(address, getSharedMemory() + offset, size); + unlockSharedMemory(); } int getSharedMemorySize() { diff --git a/src/vkvm.h b/src/vkvm.h index a8bc68c..e908471 100644 --- a/src/vkvm.h +++ b/src/vkvm.h @@ -46,7 +46,7 @@ Color getPixel(int x, int y); * @param handler function to call. * @return true if handler could be registered, false if it failed. */ -bool registerEvent(EventType type, std::function handler); +bool registerEvent(EventType type, const std::function &handler); /** * set displayed text in Text mode