~ fixed registerEvent

This commit is contained in:
Julian Hinxlage 2019-11-12 15:40:36 +01:00
parent a1f9f62f51
commit 7bb6846b33
4 changed files with 28 additions and 32 deletions

View File

@ -1,9 +1,9 @@
#include "KeyCode.h" #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; return value;
} }

View File

@ -5,10 +5,10 @@
class KeyCode { class KeyCode {
private: private:
int16_t value; int value;
public: public:
explicit KeyCode(int16_t value) noexcept; explicit KeyCode(int value) noexcept;
int16_t getValue(); int getValue();
}; };
const static KeyCode Backspace = KeyCode(8); const static KeyCode Backspace = KeyCode(8);

View File

@ -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<char *>(shmat(shmId, nullptr, 0));
semaphoreOperation(LOCK);
memcpy(address, shm_pointer + offset, size);
shmdt(shm_pointer); //close shm_pointer
semaphoreOperation(UNLOCK);
}
}
char *getSharedMemory() { char *getSharedMemory() {
/* constexpr bool useLocal = false;
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);
}
*/
if(useLocal){
//using a local buffer for shared memory testing //using a local buffer for shared memory testing
if (localSharedMemory.empty()) { if (localSharedMemory.empty()) {
initSemaphore(); initSemaphore();
localSharedMemory.resize(impl.sharedMemorySize * 1024); localSharedMemory.resize(impl.sharedMemorySize * 1024);
} }
return &localSharedMemory[0]; 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);
}
}
}
void getSharedMemory(char *address, int size, int offset) {
lockSharedMemory();
memcpy(address, getSharedMemory() + offset, size);
unlockSharedMemory();
} }
int getSharedMemorySize() { int getSharedMemorySize() {

View File

@ -46,7 +46,7 @@ Color getPixel(int x, int y);
* @param handler function to call. * @param handler function to call.
* @return true if handler could be registered, false if it failed. * @return true if handler could be registered, false if it failed.
*/ */
bool registerEvent(EventType type, std::function<void()> handler); bool registerEvent(EventType type, const std::function<void()> &handler);
/** /**
* set displayed text in Text mode * set displayed text in Text mode