From 3d8bf74fbf2dd042d03ba3d993a7759e2fbb21d9 Mon Sep 17 00:00:00 2001 From: Julian Hinxlage Date: Mon, 4 Nov 2019 23:17:15 +0100 Subject: [PATCH 01/34] + getSharedMemory --- src/internal.cpp | 24 +++++++++++++++++++++++- src/internal.h | 15 +++++++++++---- src/vkvm.cpp | 4 +++- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/src/internal.cpp b/src/internal.cpp index d9575d2..2fefc58 100644 --- a/src/internal.cpp +++ b/src/internal.cpp @@ -1,5 +1,27 @@ #include "internal.h" +#include +Impl impl; -Internal internal; +void *getSharedMemory(){ + impl.sharedMemorySize = 8000; + auto id = shmget(impl.sharedMemoryKey, impl.sharedMemorySize, 0644 | IPC_CREAT); + if(id == -1){ + //error + impl.sharedMemorySize = 0; + return nullptr; + } + void *data = shmat(id, nullptr, 0); + if(data == (char*)(-1)){ + //error + impl.sharedMemorySize = 0; + return nullptr; + } + + return data; +} + +int getSharedMemorySize(){ + return impl.sharedMemorySize; +} diff --git a/src/internal.h b/src/internal.h index 65baee5..831ad27 100644 --- a/src/internal.h +++ b/src/internal.h @@ -6,13 +6,14 @@ #include "LayoutVersion.h" #include "GraphicMode.h" #include "Color.h" +#include /** * the Control Registers * @author Julian Hinxlage * @since 0.1.0 */ -struct Registers{ +struct Registers { int layout_version; int trigger_reset; int width_pixels; @@ -34,7 +35,7 @@ struct Registers{ int keyboardBuffer_index_r; }; -struct InterruptEntry{ +struct InterruptEntry { int pid; int signum; }; @@ -42,10 +43,16 @@ struct InterruptEntry{ /** * internal values for the library. */ -struct Internal{ +struct Impl { int sharedMemoryPid; + key_t sharedMemoryKey; + int sharedMemorySize; }; -extern Internal internal; +extern Impl impl; + +void *getSharedMemory(); + +int getSharedMemorySize(); /** * set layout version. diff --git a/src/vkvm.cpp b/src/vkvm.cpp index 45218e4..1b63d02 100644 --- a/src/vkvm.cpp +++ b/src/vkvm.cpp @@ -2,5 +2,7 @@ #include "internal.h" void initialize(int pid) { - internal.sharedMemoryPid = pid; + impl.sharedMemoryPid = pid; + impl.sharedMemoryKey = 892348; + impl.sharedMemorySize = 0; } From 53afcffba81afaf2c535dd72d612f7230d3ef150 Mon Sep 17 00:00:00 2001 From: Julian Hinxlage Date: Tue, 5 Nov 2019 15:08:17 +0100 Subject: [PATCH 02/34] + logging --- src/log.cpp | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/log.h | 19 +++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 src/log.cpp create mode 100644 src/log.h diff --git a/src/log.cpp b/src/log.cpp new file mode 100644 index 0000000..2dd85ff --- /dev/null +++ b/src/log.cpp @@ -0,0 +1,60 @@ + +#include "log.h" +#include + +const char *getLevelName(LogLevel level){ + switch(level){ + case LogLevel::DEBUG: + return "DEBUG"; + case LogLevel::INFO: + return "INFO"; + case LogLevel::WARNING: + return "WARNING"; + case LogLevel::ERROR: + return "ERROR"; + case LogLevel::CRITICAL: + return "CRITICAL"; + default: + return "NON"; + } +} + +const char *getLevelColor(LogLevel level){ + switch(level){ + case LogLevel::DEBUG: + return "0;37"; + case LogLevel::INFO: + return "0"; + case LogLevel::WARNING: + return "1;33"; + case LogLevel::ERROR: + return "1;31"; + case LogLevel::CRITICAL: + return "1;35"; + default: + return "0"; + } +} + +LogLevel logLevel = LogLevel::INFO; + +void log(const std::string &msg, LogLevel level) { + if(level >= logLevel) { + std::string levelName = getLevelName(level); + int maxLevelNameLength = 8; + + std::cout << "["; + //color and level name;lo + std::cout << "\033[" << getLevelColor(level) << "m" << levelName << "\033[0m"; + std::cout << "] "; + for (int i = levelName.size(); i < maxLevelNameLength; i++) { + std::cout << " "; + } + std::cout << msg << "\n"; + } +} + +void setLogLevel(LogLevel level) { + logLevel = level; +} + diff --git a/src/log.h b/src/log.h new file mode 100644 index 0000000..b7248b1 --- /dev/null +++ b/src/log.h @@ -0,0 +1,19 @@ +#ifndef LIBRARY_LOG_H +#define LIBRARY_LOG_H + +#include + +enum LogLevel{ + DEBUG = 1, + INFO = 2, + WARNING = 3, + ERROR = 4, + CRITICAL = 5, + OFF = 6 +}; + +void log(const std::string &msg, LogLevel level = LogLevel::INFO); + +void setLogLevel(LogLevel level); + +#endif //LIBRARY_LOG_H From 9b89e46de26a2dbe14018f90ed56c1b7232b8e9c Mon Sep 17 00:00:00 2001 From: Julian Hinxlage Date: Tue, 5 Nov 2019 15:19:51 +0100 Subject: [PATCH 03/34] + timestamps for logging --- src/log.cpp | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++- src/log.h | 11 +++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/src/log.cpp b/src/log.cpp index 2dd85ff..42dfbe5 100644 --- a/src/log.cpp +++ b/src/log.cpp @@ -1,7 +1,13 @@ +/** + * @author Julian Hinxlage + * @since 0.1.0 + */ + #include "log.h" #include +//converts the level to a string of the level const char *getLevelName(LogLevel level){ switch(level){ case LogLevel::DEBUG: @@ -19,6 +25,7 @@ const char *getLevelName(LogLevel level){ } } +//converts the level to a ansi color code const char *getLevelColor(LogLevel level){ switch(level){ case LogLevel::DEBUG: @@ -38,19 +45,61 @@ const char *getLevelColor(LogLevel level){ LogLevel logLevel = LogLevel::INFO; +//log the current time +void logTime(){ + time_t rawtime; + time(&rawtime); + struct tm *timeinfo; + timeinfo = localtime(&rawtime); + + if (timeinfo->tm_hour < 10) { + std::cout << "0"; + } + std::cout << timeinfo->tm_hour; + std::cout << ":"; + if (timeinfo->tm_min < 10) { + std::cout << "0"; + } + std::cout << timeinfo->tm_min; + std::cout << ":"; + if (timeinfo->tm_sec < 10) { + std::cout << "0"; + } + std::cout << timeinfo->tm_sec; +} + +//log the message void log(const std::string &msg, LogLevel level) { if(level >= logLevel) { std::string levelName = getLevelName(level); int maxLevelNameLength = 8; + //time std::cout << "["; + logTime(); + std::cout << "] "; + //color and level name;lo + std::cout << "["; std::cout << "\033[" << getLevelColor(level) << "m" << levelName << "\033[0m"; std::cout << "] "; for (int i = levelName.size(); i < maxLevelNameLength; i++) { std::cout << " "; } - std::cout << msg << "\n"; + + //message + for(char c : msg){ + if(c == '\n'){ + //intend newlines so that they align with the start of the message + std::cout << "\n"; + for(int i = 0; i < 22;i++){ + std::cout << " "; + } + }else{ + std::cout << c; + } + } + std::cout << "\n"; } } diff --git a/src/log.h b/src/log.h index b7248b1..6b7f94f 100644 --- a/src/log.h +++ b/src/log.h @@ -12,8 +12,19 @@ enum LogLevel{ OFF = 6 }; +/** + * log the messgae with logLevel and timestamp + * @author Julian Hinxlage + * @since 0.1.0 + */ void log(const std::string &msg, LogLevel level = LogLevel::INFO); + +/** + * set the logLevel, the log function will use this to determine if the message is logged + * @author Julian Hinxlage + * @since 0.1.0 + */ void setLogLevel(LogLevel level); #endif //LIBRARY_LOG_H From 5563867f1f414d31234ee8fa3855fcd073543dd0 Mon Sep 17 00:00:00 2001 From: Cigerxwin Chaker Date: Wed, 6 Nov 2019 11:05:11 +0100 Subject: [PATCH 04/34] first commit --- src/SharedMemoryAccess.cpp | 65 ++++++++++++++++++++++++++++++++++++++ src/SharedMemoryAccess.h | 20 ++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 src/SharedMemoryAccess.cpp create mode 100644 src/SharedMemoryAccess.h diff --git a/src/SharedMemoryAccess.cpp b/src/SharedMemoryAccess.cpp new file mode 100644 index 0000000..73a7d46 --- /dev/null +++ b/src/SharedMemoryAccess.cpp @@ -0,0 +1,65 @@ +// +// Created by Cigerxwin Chaker on 05.11.19. +// +#include +#include +#include +#include +#include +#include +#include +//#include "sharedMemory.h" /* header is important for the shmID. name could be different. maybe not needed cause: (shmget(memory_access_key, NULL, 0)) */ +#include +#include + +int initSemaphore (void) { + /* Testen, ob das Semaphor bereits existiert */ + semId = semget (semKey, 0, IPC_PRIVATE); + if (semId < 0) { + /* ... existiert noch nicht, also anlegen */ + /* Alle Zugriffsrechte der Dateikreierungsmaske */ + /* erlauben */ + umask(0); + semId = semget (semKey, 1, IPC_CREAT | IPC_EXCL | PERM); + if (semId < 0) { + return -1; + } + /* Semaphor mit 1 initialisieren */ + if (semctl (semId, 0, SETVAL, (int) 1) == -1) + return -1; + } + return 1; +} + +int semaphoreOperation (int op) { + semaphore.sem_op = op; + semaphore.sem_flg = SEM_UNDO; + if( semop (semId, &semaphore, 1) == -1) { + perror(" semop "); + exit (EXIT_FAILURE); + } + return 1; +} + +void writeToShm(char* text, int size) { + int shmId = shmget(memoryAccessKey, NULL, 0); + if(shmId < 0 ) { + exit(EXIT_FAILURE); + /* we could return -1, not sure if it will be useful to continue after there is no shm allocated for any reason*/ + } else { + char *shm_pointer = (char *) shmat(shmId, NULL, 0); + semaphore_operation(LOCK); + memcpy(shm_pointer, text, size); + semaphore_operation(UNLOCK); + } +} + +char* getShmPointer() { + int shmId = shmget(memoryAccessKey, NULL, 0); + if(shmId < 0) { + exit(EXIT_FAILURE); + /* 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); + } +} \ No newline at end of file diff --git a/src/SharedMemoryAccess.h b/src/SharedMemoryAccess.h new file mode 100644 index 0000000..9f26857 --- /dev/null +++ b/src/SharedMemoryAccess.h @@ -0,0 +1,20 @@ +// +// Created by Cigerxwin Chaker on 05.11.19. +// + +#ifndef LIBRARY_SHAREDMEMORYACCESSS_H +#define LIBRARY_SHAREDMEMORYACCESSS_H + +#define PERM 0666 /* Zugriffsrechte */ +#define LOCK -1 +#define UNLOCK 1 +#define SemKey 123458L + +int memoryAccessKey; /* var type is int. but could be another type. */ //TODO: look after type in sharedmemory group +int semId; +struct sembuf semaphore; +int initSemaphore (void); +int semaphoreOperation (int op); +char* getShmPointer(void); +void writeToShm(char* text, int size); +#endif //LIBRARY_SHAREDMEMORYACCESSS_H From 255b05cd1963c925a495ce2abf6cfb0f37d3662e Mon Sep 17 00:00:00 2001 From: Cigerxwin Chaker Date: Wed, 6 Nov 2019 11:38:46 +0100 Subject: [PATCH 05/34] =?UTF-8?q?shared=20memory=20access=20hinzugef=C3=BC?= =?UTF-8?q?gt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/SharedMemoryAccess.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SharedMemoryAccess.cpp b/src/SharedMemoryAccess.cpp index 73a7d46..e5e4543 100644 --- a/src/SharedMemoryAccess.cpp +++ b/src/SharedMemoryAccess.cpp @@ -42,7 +42,7 @@ int semaphoreOperation (int op) { } void writeToShm(char* text, int size) { - int shmId = shmget(memoryAccessKey, NULL, 0); + int shmId = shmget(memoryAccessKey, NULL, 0); // dont init just get the ID if already existing. if(shmId < 0 ) { exit(EXIT_FAILURE); /* we could return -1, not sure if it will be useful to continue after there is no shm allocated for any reason*/ From 9ce7b06c83ca4b6db12dbbd136639d150e61f40c Mon Sep 17 00:00:00 2001 From: Julian Hinxlage Date: Wed, 6 Nov 2019 11:54:26 +0100 Subject: [PATCH 06/34] + fixed SharedMemoryAccess (build errors) --- src/SharedMemoryAccess.cpp | 14 ++++++++------ src/SharedMemoryAccess.h | 3 ++- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/SharedMemoryAccess.cpp b/src/SharedMemoryAccess.cpp index e5e4543..9f13ff9 100644 --- a/src/SharedMemoryAccess.cpp +++ b/src/SharedMemoryAccess.cpp @@ -2,25 +2,27 @@ // Created by Cigerxwin Chaker on 05.11.19. // #include -#include +#include #include #include #include #include #include -//#include "sharedMemory.h" /* header is important for the shmID. name could be different. maybe not needed cause: (shmget(memory_access_key, NULL, 0)) */ #include #include +#include + +#include "SharedMemoryAccess.h" /* header is important for the shmID. name could be different. maybe not needed cause: (shmget(memory_access_key, NULL, 0)) */ int initSemaphore (void) { /* Testen, ob das Semaphor bereits existiert */ - semId = semget (semKey, 0, IPC_PRIVATE); + semId = semget (SEM_KEY, 0, IPC_PRIVATE); if (semId < 0) { /* ... existiert noch nicht, also anlegen */ /* Alle Zugriffsrechte der Dateikreierungsmaske */ /* erlauben */ umask(0); - semId = semget (semKey, 1, IPC_CREAT | IPC_EXCL | PERM); + semId = semget (SEM_KEY, 1, IPC_CREAT | IPC_EXCL | PERM); if (semId < 0) { return -1; } @@ -48,9 +50,9 @@ void writeToShm(char* text, int size) { /* we could return -1, not sure if it will be useful to continue after there is no shm allocated for any reason*/ } else { char *shm_pointer = (char *) shmat(shmId, NULL, 0); - semaphore_operation(LOCK); + semaphoreOperation(LOCK); memcpy(shm_pointer, text, size); - semaphore_operation(UNLOCK); + semaphoreOperation(UNLOCK); } } diff --git a/src/SharedMemoryAccess.h b/src/SharedMemoryAccess.h index 9f26857..d9e15bf 100644 --- a/src/SharedMemoryAccess.h +++ b/src/SharedMemoryAccess.h @@ -8,7 +8,7 @@ #define PERM 0666 /* Zugriffsrechte */ #define LOCK -1 #define UNLOCK 1 -#define SemKey 123458L +#define SEM_KEY 123458L int memoryAccessKey; /* var type is int. but could be another type. */ //TODO: look after type in sharedmemory group int semId; @@ -17,4 +17,5 @@ int initSemaphore (void); int semaphoreOperation (int op); char* getShmPointer(void); void writeToShm(char* text, int size); + #endif //LIBRARY_SHAREDMEMORYACCESSS_H From daa65080e692ccdc423133b197f153d37cf21169 Mon Sep 17 00:00:00 2001 From: Julian Hinxlage Date: Wed, 6 Nov 2019 12:47:34 +0100 Subject: [PATCH 07/34] + send an receive signals --- src/internal.cpp | 12 +++++++++++- src/internal.h | 13 +++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/internal.cpp b/src/internal.cpp index 2fefc58..e44032c 100644 --- a/src/internal.cpp +++ b/src/internal.cpp @@ -1,11 +1,13 @@ #include "internal.h" #include +#include + Impl impl; void *getSharedMemory(){ impl.sharedMemorySize = 8000; - auto id = shmget(impl.sharedMemoryKey, impl.sharedMemorySize, 0644 | IPC_CREAT); + auto id = shmget(impl.sharedMemoryKey, impl.sharedMemorySize, 0644u | IPC_CREAT); if(id == -1){ //error impl.sharedMemorySize = 0; @@ -25,3 +27,11 @@ void *getSharedMemory(){ int getSharedMemorySize(){ return impl.sharedMemorySize; } + +void sendSignal(pid_t pid, int signalNumber) { + kill(pid, signalNumber); +} + +void onSignal(int signalNumber, void(*callback)(int)) { + signal(signalNumber, callback); +} diff --git a/src/internal.h b/src/internal.h index 831ad27..2365c6e 100644 --- a/src/internal.h +++ b/src/internal.h @@ -54,6 +54,19 @@ void *getSharedMemory(); int getSharedMemorySize(); +/** + * send a signal to a process + * @param pid of the process to which the signal is send + * @param signalNumber + */ +void sendSignal(pid_t pid, int signalNumber); + +/** + * calls the callback if a signal is received + * @param signalNumber + */ +void onSignal(int signalNumber, void(*callback)(int)); + /** * set layout version. * @param newValue new layout version number. From 2dec86e752366df16cf8d44a41d70a6cdfcabc02 Mon Sep 17 00:00:00 2001 From: Julian Hinxlage Date: Wed, 6 Nov 2019 13:41:24 +0100 Subject: [PATCH 08/34] + register and call event --- src/internal.cpp | 12 ++++++++++++ src/internal.h | 8 ++++++++ src/vkvm.cpp | 20 ++++++++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/src/internal.cpp b/src/internal.cpp index e44032c..d4ef7e9 100644 --- a/src/internal.cpp +++ b/src/internal.cpp @@ -35,3 +35,15 @@ void sendSignal(pid_t pid, int signalNumber) { void onSignal(int signalNumber, void(*callback)(int)) { signal(signalNumber, callback); } + +InterruptEntry *getInterrupTable(){ + return (InterruptEntry*)((char*)getSharedMemory() + sizeof(Registers) + 1024/*reserved*/); +} + +bool callEvent(EventType type) { + auto ivt = getInterrupTable(); + if(ivt[type].pid != 0){ + sendSignal(ivt[type].pid, ivt[type].signum); + } + return true; +} diff --git a/src/internal.h b/src/internal.h index 2365c6e..a34a42b 100644 --- a/src/internal.h +++ b/src/internal.h @@ -7,6 +7,8 @@ #include "GraphicMode.h" #include "Color.h" #include +#include +#include /** * the Control Registers @@ -47,7 +49,10 @@ struct Impl { int sharedMemoryPid; key_t sharedMemoryKey; int sharedMemorySize; + int interruptEntyCount = 64; + std::vector> eventTable; }; + extern Impl impl; void *getSharedMemory(); @@ -67,6 +72,9 @@ void sendSignal(pid_t pid, int signalNumber); */ void onSignal(int signalNumber, void(*callback)(int)); + +InterruptEntry *getInterrupTable(); + /** * set layout version. * @param newValue new layout version number. diff --git a/src/vkvm.cpp b/src/vkvm.cpp index 1b63d02..27350f4 100644 --- a/src/vkvm.cpp +++ b/src/vkvm.cpp @@ -1,8 +1,28 @@ #include "vkvm.h" #include "internal.h" +#include +#include void initialize(int pid) { impl.sharedMemoryPid = pid; impl.sharedMemoryKey = 892348; impl.sharedMemorySize = 0; } + +bool registerEvent(EventType type, std::function handler) { + int signum = SIGUSR1 + impl.eventTable.size(); + + auto ivt = getInterrupTable(); + ivt[type].pid = getpid(); + ivt[type].signum = signum; + + onSignal(signum, [](int sig){ + if(sig >= SIGUSR1){ + if((sig - SIGUSR1) < impl.eventTable.size()){ + impl.eventTable[sig - SIGUSR1](); + } + } + }); + + return true; +} From 9e38b137923fb038b26d5766a0e08efd1923db31 Mon Sep 17 00:00:00 2001 From: Julian Hinxlage Date: Thu, 7 Nov 2019 12:05:06 +0100 Subject: [PATCH 09/34] + shared memory access cleanup --- src/SharedMemoryAccess.cpp | 20 +++++++++++++++----- src/SharedMemoryAccess.h | 30 +++++++++++++++++++----------- src/internal.h | 4 ---- 3 files changed, 34 insertions(+), 20 deletions(-) diff --git a/src/SharedMemoryAccess.cpp b/src/SharedMemoryAccess.cpp index 9f13ff9..8a4f4d0 100644 --- a/src/SharedMemoryAccess.cpp +++ b/src/SharedMemoryAccess.cpp @@ -12,8 +12,18 @@ #include #include +#include "internal.h" #include "SharedMemoryAccess.h" /* header is important for the shmID. name could be different. maybe not needed cause: (shmget(memory_access_key, NULL, 0)) */ +#define PERM 0666 /* Zugriffsrechte */ +#define LOCK -1 +#define UNLOCK 1 +#define SEM_KEY 123458L + +//int memoryAccessKey; /* var type is int. but could be another type. */ //TODO: look after type in sharedmemory group +int semId; +struct sembuf semaphore; + int initSemaphore (void) { /* Testen, ob das Semaphor bereits existiert */ semId = semget (SEM_KEY, 0, IPC_PRIVATE); @@ -43,21 +53,21 @@ int semaphoreOperation (int op) { return 1; } -void writeToShm(char* text, int size) { - int shmId = shmget(memoryAccessKey, NULL, 0); // dont init just get the ID if already existing. +void writeSharedMemory(char *data, int size, int offset) { + int shmId = shmget(impl.sharedMemoryKey, NULL, 0); // dont init just get the ID if already existing. if(shmId < 0 ) { exit(EXIT_FAILURE); /* we could return -1, not sure if it will be useful to continue after there is no shm allocated for any reason*/ } else { char *shm_pointer = (char *) shmat(shmId, NULL, 0); semaphoreOperation(LOCK); - memcpy(shm_pointer, text, size); + memcpy(shm_pointer + offset, data, size); semaphoreOperation(UNLOCK); } } -char* getShmPointer() { - int shmId = shmget(memoryAccessKey, NULL, 0); +const char* getSharedMemory() { + int shmId = shmget(impl.sharedMemoryKey, NULL, 0); if(shmId < 0) { exit(EXIT_FAILURE); /* we could return -1, not sure if it will be useful to continue after there is no shm allocated for any reason*/ diff --git a/src/SharedMemoryAccess.h b/src/SharedMemoryAccess.h index d9e15bf..cdb4c45 100644 --- a/src/SharedMemoryAccess.h +++ b/src/SharedMemoryAccess.h @@ -5,17 +5,25 @@ #ifndef LIBRARY_SHAREDMEMORYACCESSS_H #define LIBRARY_SHAREDMEMORYACCESSS_H -#define PERM 0666 /* Zugriffsrechte */ -#define LOCK -1 -#define UNLOCK 1 -#define SEM_KEY 123458L -int memoryAccessKey; /* var type is int. but could be another type. */ //TODO: look after type in sharedmemory group -int semId; -struct sembuf semaphore; -int initSemaphore (void); -int semaphoreOperation (int op); -char* getShmPointer(void); -void writeToShm(char* text, int size); +/** + * only to read the shared memory + * @return pointer to the shared memory + */ +const char *getSharedMemory(); + +/** + * + * @return the size of the shared memory + */ +int getSharedMemorySize(); + +/** + * + * @param data poiter to data + * @param size of data + * @param offset where the data is written on the shared memory + */ +void writeSharedMemory(char *data, int size, int offset); #endif //LIBRARY_SHAREDMEMORYACCESSS_H diff --git a/src/internal.h b/src/internal.h index a34a42b..a6f273a 100644 --- a/src/internal.h +++ b/src/internal.h @@ -55,10 +55,6 @@ struct Impl { extern Impl impl; -void *getSharedMemory(); - -int getSharedMemorySize(); - /** * send a signal to a process * @param pid of the process to which the signal is send From 9d0766612f353ab561847578da0e69cea19aabfc Mon Sep 17 00:00:00 2001 From: Julian Hinxlage Date: Thu, 7 Nov 2019 12:41:01 +0100 Subject: [PATCH 10/34] + getSharedMemory cleanup --- src/internal.cpp | 26 ++------------------------ src/vkvm.cpp | 2 ++ 2 files changed, 4 insertions(+), 24 deletions(-) diff --git a/src/internal.cpp b/src/internal.cpp index d4ef7e9..287d561 100644 --- a/src/internal.cpp +++ b/src/internal.cpp @@ -2,32 +2,10 @@ #include #include +#include "SharedMemoryAccess.h" + Impl impl; -void *getSharedMemory(){ - impl.sharedMemorySize = 8000; - - auto id = shmget(impl.sharedMemoryKey, impl.sharedMemorySize, 0644u | IPC_CREAT); - if(id == -1){ - //error - impl.sharedMemorySize = 0; - return nullptr; - } - - void *data = shmat(id, nullptr, 0); - if(data == (char*)(-1)){ - //error - impl.sharedMemorySize = 0; - return nullptr; - } - - return data; -} - -int getSharedMemorySize(){ - return impl.sharedMemorySize; -} - void sendSignal(pid_t pid, int signalNumber) { kill(pid, signalNumber); } diff --git a/src/vkvm.cpp b/src/vkvm.cpp index 27350f4..be44a83 100644 --- a/src/vkvm.cpp +++ b/src/vkvm.cpp @@ -16,6 +16,8 @@ bool registerEvent(EventType type, std::function handler) { ivt[type].pid = getpid(); ivt[type].signum = signum; + impl.eventTable.push_back(handler); + onSignal(signum, [](int sig){ if(sig >= SIGUSR1){ if((sig - SIGUSR1) < impl.eventTable.size()){ From c8f2ce4027bfcd820be34ae1a31e33ccc6551d92 Mon Sep 17 00:00:00 2001 From: Julian Hinxlage Date: Thu, 7 Nov 2019 12:54:39 +0100 Subject: [PATCH 11/34] + fixed internal.h incude --- src/Color.h | 4 ++-- src/KeyCode.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Color.h b/src/Color.h index 3d75e99..b01a9c6 100644 --- a/src/Color.h +++ b/src/Color.h @@ -19,7 +19,7 @@ public: }; -const Color black = Color(0, 0, 0); -const Color white = Color(255, 255, 255); +const static Color black = Color(0, 0, 0); +const static Color white = Color(255, 255, 255); #endif diff --git a/src/KeyCode.h b/src/KeyCode.h index 65ab74d..a59a5cd 100644 --- a/src/KeyCode.h +++ b/src/KeyCode.h @@ -9,7 +9,7 @@ public: int getValue(); }; -const KeyCode Backspace = KeyCode(8); -const KeyCode tabulator = KeyCode(9); +const static KeyCode Backspace = KeyCode(8); +const static KeyCode tabulator = KeyCode(9); #endif From e30280241e863d5caf827bc146481f45256f75bc Mon Sep 17 00:00:00 2001 From: Julian Hinxlage Date: Thu, 7 Nov 2019 12:58:37 +0100 Subject: [PATCH 12/34] + changed sharedMemoryKey --- src/vkvm.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vkvm.cpp b/src/vkvm.cpp index be44a83..c2e66c6 100644 --- a/src/vkvm.cpp +++ b/src/vkvm.cpp @@ -5,7 +5,7 @@ void initialize(int pid) { impl.sharedMemoryPid = pid; - impl.sharedMemoryKey = 892348; + impl.sharedMemoryKey = 12345; impl.sharedMemorySize = 0; } From 2f2e739d558f02139bf56ca11dca3b923aa6f22e Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Thu, 7 Nov 2019 13:58:02 +0000 Subject: [PATCH 13/34] + ci pipeline --- .ci/clang-tidy.sh | 32 +++++++++++++++++++++++++++++ .gitlab-ci.yml | 52 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 .ci/clang-tidy.sh create mode 100644 .gitlab-ci.yml diff --git a/.ci/clang-tidy.sh b/.ci/clang-tidy.sh new file mode 100644 index 0000000..eae5b12 --- /dev/null +++ b/.ci/clang-tidy.sh @@ -0,0 +1,32 @@ +#!/bin/sh + +echo "Doing clang-tidy..." +bool=false +# explicitly set IFS to contain only a line feed +IFS=' +' +filelist="$(find . -not \( -path './client/cpptoml/*' -prune \) -type f ! -name "$(printf "*\n*")")" +for file in $filelist; do + if echo "$file" | grep -q -E ".*\.cpp$" ; then + #Extra check missing dependencies due to clang-tidy doesn't toggle exit code. + clang_tidy_lib_check="$(clang-tidy -warnings-as-errors='*' -header-filter='.*,-cpptoml.hpp' -checks='*,-llvm-header-guard,-fuchsia-statically-constructed-objects,-fuchsia-default-arguments,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-pro-bounds-constant-array-index,-bugprone-narrowing-conversions,-cppcoreguidelines-narrowing-conversions,-cppcoreguidelines-pro-type-const-cast,-cppcoreguidelines-pro-type-reinterpret-cast,-hicpp-signed-bitwise,-bugprone-exception-escape,-cppcoreguidelines-pro-type-member-init,-cppcoreguidelines-pro-type-cstyle-cast,-hicpp-member-init' "$file" -- -I. -std=c++14 2>&1)" + for tidy_line in $clang_tidy_lib_check; do + echo "$tidy_line" | grep -q -v -E "^Error while processing*" + if [ $? -eq 1 ]; then + bool=true + fi + echo "$tidy_line" | grep -q -v -E ".* error: .*" + if [ $? -eq 1 ]; then + bool=true + fi + echo "$tidy_line" + done + fi +done +if $bool; then + exit 1 +else + echo "No clang-tidy errors found." +fi + +exit 0 diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..aa908c8 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,52 @@ +--- + +image: samueldebruyn/debian-git:latest + +stages: + - style + - test + - build + + +clang_tidy: + image: jhasse/clang-tidy + stage: style + tags: + - docker-ci + script: + - sh .ci/clang-tidy.sh; + +make_test: + stage: test + tags: + - docker-ci + script: + - apt-get update + - apt-get install -y g++ make cmake clang-tidy + - git clone https://github.com/catchorg/Catch2.git + - cd Catch2 + - cmake -Bbuild -H. -DBUILD_TESTING=OFF + - cmake --build build/ --target install + - cd .. + - mkdir build + - cd build + - cmake .. + - make + - make test + +cmake_build: + stage: build + tags: + - docker-ci + script: + - apt-get update + - apt-get install -y g++ make cmake clang-tidy + - git clone https://github.com/catchorg/Catch2.git + - cd Catch2 + - cmake -Bbuild -H. -DBUILD_TESTING=OFF + - cmake --build build/ --target install + - cd .. + - mkdir build + - cd build + - cmake .. + - make \ No newline at end of file From 5439f89e2cf02339de912ddc611b114fc86e21f5 Mon Sep 17 00:00:00 2001 From: Julian Hinxlage Date: Thu, 7 Nov 2019 16:34:15 +0100 Subject: [PATCH 14/34] + local memory as shared memory + getters/setters --- CMakeLists.txt | 2 +- src/Color.cpp | 24 ++++++ src/Color.h | 8 ++ src/FontType.cpp | 18 ++++ src/{Font.h => FontType.h} | 8 +- src/SharedMemoryAccess.cpp | 40 +++++++-- src/SharedMemoryAccess.h | 8 +- src/internal.cpp | 51 +++++++++++- src/internal.h | 11 ++- src/vkvm.cpp | 165 ++++++++++++++++++++++++++++++++++++- src/vkvm.h | 6 +- 11 files changed, 314 insertions(+), 27 deletions(-) create mode 100644 src/FontType.cpp rename src/{Font.h => FontType.h} (73%) diff --git a/CMakeLists.txt b/CMakeLists.txt index d9fd70d..e355699 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,7 +22,7 @@ file(GLOB_RECURSE TESTS test/*.cpp) include_directories(src) include_directories(test) -add_library(library ${SOURCES} ${HEADERS}) +add_library(library ${SOURCES} ${HEADERS} src/FontType.cpp) file(COPY "${CMAKE_SOURCE_DIR}/src/" DESTINATION "${CMAKE_SOURCE_DIR}/include" diff --git a/src/Color.cpp b/src/Color.cpp index 3daac50..7d796fb 100644 --- a/src/Color.cpp +++ b/src/Color.cpp @@ -6,3 +6,27 @@ Color::Color(unsigned char red, unsigned char green, unsigned char blue) } +unsigned char Color::getRed() { + return red; +} + +unsigned char Color::getGreen() { + return green; +} + +unsigned char Color::getBlue() { + return blue; +} + +void Color::setRed(unsigned char value) { + red = value; +} + +void Color::setGreen(unsigned char value) { + green = value; +} + +void Color::setBlue(unsigned char value) { + blue = value; +} + diff --git a/src/Color.h b/src/Color.h index b01a9c6..1191da4 100644 --- a/src/Color.h +++ b/src/Color.h @@ -17,6 +17,14 @@ private: public: Color(unsigned char red, unsigned char green, unsigned char blue); + unsigned char getRed(); + unsigned char getGreen(); + unsigned char getBlue(); + + void setRed(unsigned char value); + void setGreen(unsigned char value); + void setBlue(unsigned char value); + }; const static Color black = Color(0, 0, 0); diff --git a/src/FontType.cpp b/src/FontType.cpp new file mode 100644 index 0000000..87d67fb --- /dev/null +++ b/src/FontType.cpp @@ -0,0 +1,18 @@ +#include "FontType.h" + + +FontType::FontType() { + +} + +std::string FontType::getName() { + return std::__cxx11::string(); +} + +int FontType::getHeight() { + return 0; +} + +int FontType::getWidth() { + return 0; +} diff --git a/src/Font.h b/src/FontType.h similarity index 73% rename from src/Font.h rename to src/FontType.h index 308dbc8..70ad7c9 100644 --- a/src/Font.h +++ b/src/FontType.h @@ -1,12 +1,12 @@ #ifndef LIBRARY_FONT_H #define LIBRARY_FONT_H +#include -class Font { -private: - Font(); - +class FontType { public: + FontType(); + std::string getName(); int getHeight(); int getWidth(); diff --git a/src/SharedMemoryAccess.cpp b/src/SharedMemoryAccess.cpp index 8a4f4d0..0fd13dd 100644 --- a/src/SharedMemoryAccess.cpp +++ b/src/SharedMemoryAccess.cpp @@ -24,7 +24,9 @@ int semId; struct sembuf semaphore; -int initSemaphore (void) { +std::vector localSharedMemory; + +int initSemaphore () { /* Testen, ob das Semaphor bereits existiert */ semId = semget (SEM_KEY, 0, IPC_PRIVATE); if (semId < 0) { @@ -44,11 +46,11 @@ int initSemaphore (void) { } int semaphoreOperation (int op) { - semaphore.sem_op = op; + semaphore.sem_op = (short)op; semaphore.sem_flg = SEM_UNDO; if( semop (semId, &semaphore, 1) == -1) { perror(" semop "); - exit (EXIT_FAILURE); + return -1; } return 1; } @@ -56,7 +58,7 @@ int semaphoreOperation (int op) { void writeSharedMemory(char *data, int size, int offset) { int shmId = shmget(impl.sharedMemoryKey, NULL, 0); // dont init just get the ID if already existing. if(shmId < 0 ) { - exit(EXIT_FAILURE); + return; /* we could return -1, not sure if it will be useful to continue after there is no shm allocated for any reason*/ } else { char *shm_pointer = (char *) shmat(shmId, NULL, 0); @@ -66,12 +68,34 @@ void writeSharedMemory(char *data, int size, int offset) { } } -const char* getSharedMemory() { +char* getSharedMemory() { + /* int shmId = shmget(impl.sharedMemoryKey, NULL, 0); if(shmId < 0) { - exit(EXIT_FAILURE); - /* we could return -1, not sure if it will be useful to continue after there is no shm allocated for any reason*/ + 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); } -} \ No newline at end of file + */ + + //using a local buffer for shared memory testing + if(localSharedMemory.empty()){ + initSemaphore(); + localSharedMemory.resize(impl.sharedMemorySize * 1024); + } + + return &localSharedMemory[0]; +} + +int getSharedMemorySize() { + return impl.sharedMemorySize; +} + +void lockSharedMemory() { + semaphoreOperation(LOCK); +} + +void unlockSharedMemory() { + semaphoreOperation(UNLOCK); +} diff --git a/src/SharedMemoryAccess.h b/src/SharedMemoryAccess.h index cdb4c45..949307b 100644 --- a/src/SharedMemoryAccess.h +++ b/src/SharedMemoryAccess.h @@ -7,10 +7,10 @@ /** - * only to read the shared memory + * use lock and unlock when writing * @return pointer to the shared memory */ -const char *getSharedMemory(); +char *getSharedMemory(); /** * @@ -18,6 +18,10 @@ const char *getSharedMemory(); */ int getSharedMemorySize(); +void lockSharedMemory(); + +void unlockSharedMemory(); + /** * * @param data poiter to data diff --git a/src/internal.cpp b/src/internal.cpp index 287d561..4fb1a26 100644 --- a/src/internal.cpp +++ b/src/internal.cpp @@ -1,9 +1,10 @@ #include "internal.h" +#include "SharedMemoryAccess.h" +#include "vkvm.h" + #include #include -#include "SharedMemoryAccess.h" - Impl impl; void sendSignal(pid_t pid, int signalNumber) { @@ -15,7 +16,22 @@ void onSignal(int signalNumber, void(*callback)(int)) { } InterruptEntry *getInterrupTable(){ - return (InterruptEntry*)((char*)getSharedMemory() + sizeof(Registers) + 1024/*reserved*/); + return (InterruptEntry*)(getSharedMemory() + sizeof(Registers) + impl.reservedSize); +} + +Registers *getRegisters(){ + return (Registers*)getSharedMemory(); +} + +char *getTextArea(){ + return ((getSharedMemory() + sizeof(Registers) + impl.reservedSize) + + sizeof(InterruptEntry) * impl.interruptEntyCount); +} + +char *getPixelArea(){ + return (((getSharedMemory() + sizeof(Registers) + impl.reservedSize) + + sizeof(InterruptEntry) * impl.interruptEntyCount) + + getCharactersPerRow() * getCharactersPerColumn()); } bool callEvent(EventType type) { @@ -25,3 +41,32 @@ bool callEvent(EventType type) { } return true; } + +void setLayoutVersion(LayoutVersion newValue) { + lockSharedMemory(); + getRegisters()->layout_version = newValue; + unlockSharedMemory(); +} + +void setCharactersPerColumn(int newValue) { + lockSharedMemory(); + getRegisters()->characters_per_column = newValue; + unlockSharedMemory(); +} + +void setCharactersPerRow(int newValue) { + lockSharedMemory(); + getRegisters()->characters_per_row = newValue; + unlockSharedMemory(); +} + +void setMousePosition(int x, int y) { + lockSharedMemory(); + getRegisters()->mouse_pos_x = x; + getRegisters()->mouse_pos_y = y; + unlockSharedMemory(); +} + +void buttonPressed(KeyCode keyCode) { + //TODO +} diff --git a/src/internal.h b/src/internal.h index a6f273a..098575a 100644 --- a/src/internal.h +++ b/src/internal.h @@ -50,6 +50,7 @@ struct Impl { key_t sharedMemoryKey; int sharedMemorySize; int interruptEntyCount = 64; + int reservedSize = 1024; std::vector> eventTable; }; @@ -70,6 +71,10 @@ void onSignal(int signalNumber, void(*callback)(int)); InterruptEntry *getInterrupTable(); +Registers *getRegisters(); +char *getTextArea(); +char *getPixelArea(); + /** * set layout version. @@ -78,10 +83,10 @@ InterruptEntry *getInterrupTable(); void setLayoutVersion(LayoutVersion newValue); /** - * set characters per line for current font. - * @param newValue characters per line. + * set characters per column for current font. + * @param newValue characters per column. */ -void setCharactersPerLine(int newValue); +void setCharactersPerColumn(int newValue); /** * set characters per row for current font. diff --git a/src/vkvm.cpp b/src/vkvm.cpp index c2e66c6..02492cc 100644 --- a/src/vkvm.cpp +++ b/src/vkvm.cpp @@ -1,21 +1,36 @@ #include "vkvm.h" #include "internal.h" +#include "SharedMemoryAccess.h" + #include #include void initialize(int pid) { impl.sharedMemoryPid = pid; impl.sharedMemoryKey = 12345; - impl.sharedMemorySize = 0; + impl.sharedMemorySize = 8000; + + //set default values + setCharactersPerRow(60); + setCharactersPerColumn(20); + setHeight(600); + setWidth(800); + setMousePosition(42,42); + setBackgroundColor(Color(200,50,20)); + setForegroundColor(Color(20,200,50)); + setMode(GraphicMode::TrueColor); + setRedrawInterval(20); + setTimerInterruptInterval(10); } bool registerEvent(EventType type, std::function handler) { int signum = SIGUSR1 + impl.eventTable.size(); - auto ivt = getInterrupTable(); + + lockSharedMemory(); + ivt[type].pid = getpid(); ivt[type].signum = signum; - impl.eventTable.push_back(handler); onSignal(signum, [](int sig){ @@ -26,5 +41,149 @@ bool registerEvent(EventType type, std::function handler) { } }); + unlockSharedMemory(); + return true; } + +bool setPixel(int x, int y, Color color) { + char *ptr = getPixelArea() + (y * getWidth() + x) * 3; + ptr[0] = color.getRed(); + ptr[1] = color.getGreen(); + ptr[2] = color.getBlue(); + return false; +} + +Color getPixel(int x, int y) { + //TODO: other than RGB colores + //only RGB colores for now + unsigned char *ptr = (unsigned char *)getPixelArea() + (y * getWidth() + x) * 3; + return Color(ptr[0], ptr[1], ptr[2]); +} + +bool setText(std::string text) { + lockSharedMemory(); + char *ptr = getTextArea(); + for(int i = 0; i < text.size();i++){ + if(i >= getCharactersPerColumn() * getCharactersPerRow()){ + break; + } + ptr[i] = text[i]; + } + if(text.size() < getCharactersPerColumn() * getCharactersPerRow()){ + ptr[text.size()] = '\0'; + } + unlockSharedMemory(); + return true; +} + +std::string getText() { + return std::string (getTextArea()); +} + +LayoutVersion getLayoutVersion() { + return (LayoutVersion)getRegisters()->layout_version; +} + +void reset() { + //TODO +} + +int getWidth() { + return getRegisters()->width_pixels; +} + +void setWidth(int newValue) { + lockSharedMemory(); + getRegisters()->width_pixels = newValue; + unlockSharedMemory(); +} + +int getHeight() { + return getRegisters()->height_pixels; +} + +void setHeight(int newValue) { + lockSharedMemory(); + getRegisters()->height_pixels = newValue; + unlockSharedMemory(); +} + +GraphicMode getMode() { + return getRegisters()->graphicMode; +} + +void setMode(GraphicMode newValue) { + lockSharedMemory(); + getRegisters()->graphicMode = newValue; + unlockSharedMemory(); +} + +int getRedrawInterval() { + return getRegisters()->autoRedrawInterval; +} + +void setRedrawInterval(int newValue) { + lockSharedMemory(); + getRegisters()->autoRedrawInterval = newValue; + unlockSharedMemory(); +} + +int getTimerInterruptInterval() { + return getRegisters()->timerInterruptInterval; +} + +void setTimerInterruptInterval(int newValue) { + lockSharedMemory(); + getRegisters()->timerInterruptInterval = newValue; + unlockSharedMemory(); +} + +Color getBackgroundColor() { + return getRegisters()->background_color; +} + +void setBackgroundColor(Color newValue) { + lockSharedMemory(); + getRegisters()->background_color = newValue; + unlockSharedMemory(); +} + +Color getForegroundColor() { + return getRegisters()->foreground_color; +} + +void setForegroundColor(Color newValue) { + lockSharedMemory(); + getRegisters()->foreground_color = newValue; + unlockSharedMemory(); +} + +int getCharactersPerRow() { + return getRegisters()->characters_per_row; +} + +int getCharactersPerColumn() { + return getRegisters()->characters_per_column; +} + +FontType getFont() { + //TODO + return FontType(); +} + +void setFont(FontType newValue) { + //TODO + lockSharedMemory(); + getRegisters()->textMode_font = 0; + unlockSharedMemory(); +} + +std::pair getMousePosition() { + return {getRegisters()->mouse_pos_x, getRegisters()->mouse_pos_y}; +} + +KeyCode getLastPressedKey() { + //TODO + return KeyCode(0); +} diff --git a/src/vkvm.h b/src/vkvm.h index a95731f..180a1d5 100644 --- a/src/vkvm.h +++ b/src/vkvm.h @@ -6,7 +6,7 @@ #include "Color.h" #include "EventType.h" #include "GraphicMode.h" -#include "Font.h" +#include "FontType.h" #include "KeyCode.h" #include "LayoutVersion.h" @@ -178,13 +178,13 @@ int getCharactersPerColumn(); * get currently used font in text mode. * @return currently used font. */ -Font getFont(); +FontType getFont(); /** * set text mode font. * @param newValue new text mode font. */ -void setFont(Font newValue); +void setFont(FontType newValue); /** * get current mouse position From 145c7f4695a369c6955b1065fcdef6d52e21f91b Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Thu, 7 Nov 2019 17:02:00 +0100 Subject: [PATCH 15/34] + dummy getters --- src/Color.cpp | 1 - src/Font.cpp | 22 +++++++++ src/Font.h | 10 ++++- src/add.cpp | 5 --- src/add.h | 6 --- src/internal.cpp | 20 +++++++++ src/vkvm.cpp | 104 +++++++++++++++++++++++++++++++++++++++++++ test/add_test.cpp | 11 ----- test/public_test.cpp | 8 ++++ 9 files changed, 162 insertions(+), 25 deletions(-) create mode 100644 src/Font.cpp delete mode 100644 src/add.cpp delete mode 100644 src/add.h delete mode 100644 test/add_test.cpp create mode 100644 test/public_test.cpp diff --git a/src/Color.cpp b/src/Color.cpp index 3daac50..b4bdb15 100644 --- a/src/Color.cpp +++ b/src/Color.cpp @@ -5,4 +5,3 @@ Color::Color(unsigned char red, unsigned char green, unsigned char blue) : red(red), green(green), blue(blue){ } - diff --git a/src/Font.cpp b/src/Font.cpp new file mode 100644 index 0000000..fa42d3b --- /dev/null +++ b/src/Font.cpp @@ -0,0 +1,22 @@ + +#include +#include +#include "Font.h" + +Font::Font(std::string name, int height, int width) { + this->name = std::move(name); + this->height = height; + this->width = width; +} + +std::string Font::getName() { + return name; +} + +int Font::getHeight() { + return height; +} + +int Font::getWidth() { + return width; +} diff --git a/src/Font.h b/src/Font.h index 308dbc8..041c3c9 100644 --- a/src/Font.h +++ b/src/Font.h @@ -1,12 +1,16 @@ +#include + #ifndef LIBRARY_FONT_H #define LIBRARY_FONT_H - class Font { private: - Font(); + std::string name; + int height; + int width; public: + Font(std::string name, int height, int width); std::string getName(); int getHeight(); int getWidth(); @@ -14,4 +18,6 @@ public: }; +const static Font font_1 = Font("DummyFont", 10, 5); + #endif diff --git a/src/add.cpp b/src/add.cpp deleted file mode 100644 index c3f4e9f..0000000 --- a/src/add.cpp +++ /dev/null @@ -1,5 +0,0 @@ -#include "add.h" - -int add(int a, int b){ - return a + b; -} diff --git a/src/add.h b/src/add.h deleted file mode 100644 index c950a3c..0000000 --- a/src/add.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef LIBRARY_ADD_H -#define LIBRARY_ADD_H - -int add(int a, int b); - -#endif diff --git a/src/internal.cpp b/src/internal.cpp index 287d561..feba66a 100644 --- a/src/internal.cpp +++ b/src/internal.cpp @@ -25,3 +25,23 @@ bool callEvent(EventType type) { } return true; } + +void setLayoutVersion(LayoutVersion newValue) { + +} + +void setCharactersPerLine(int newValue) { + +} + +void setCharactersPerRow(int newValue) { + +} + +void setMousePosition(int x, int y) { + +} + +void buttonPressed(KeyCode keyCode) { + +} diff --git a/src/vkvm.cpp b/src/vkvm.cpp index c2e66c6..72d999b 100644 --- a/src/vkvm.cpp +++ b/src/vkvm.cpp @@ -9,6 +9,14 @@ void initialize(int pid) { impl.sharedMemorySize = 0; } +bool setPixel(int x, int y, Color color) { + return true; +} + +Color getPixel(int x, int y) { + return {255, 0, 0}; +} + bool registerEvent(EventType type, std::function handler) { int signum = SIGUSR1 + impl.eventTable.size(); @@ -28,3 +36,99 @@ bool registerEvent(EventType type, std::function handler) { return true; } + +bool setText(std::string text) { + return false; +} + +std::string getText() { + return "Hallo Welt"; +} + +LayoutVersion getLayoutVersion() { + return V1; +} + +void reset() { + +} + +int getWidth() { + return 360; +} + +void setWidth(int newValue) { + +} + +int getHeight() { + return 360; +} + +void setHeight(int newValue) { + +} + +GraphicMode getMode() { + return TrueColor; +} + +void setMode(GraphicMode newValue) { + +} + +int getRedrawInterval() { + return 10; +} + +void setRedrawInterval(int newValue) { + +} + +int getTimerInterruptInterval() { + return 5; +} + +void setTimerInterruptInterval(int newValue) { + +} + +Color getBackgroundColor() { + return black; +} + +void setBackgroundColor(Color newValue) { + +} + +Color getForegroundColor() { + return white; +} + +void setForegroundColor(Color newValue) { + +} + +int getCharactersPerRow() { + return 5; +} + +int getCharactersPerColumn() { + return 5; +} + +Font getFont() { + return font_1; +} + +void setFont(Font newValue) { + +} + +std::pair getMousePosition() { + return {10, 50}; +} + +KeyCode getLastPressedKey() { + return KeyCode(50); +} diff --git a/test/add_test.cpp b/test/add_test.cpp deleted file mode 100644 index 0b30d42..0000000 --- a/test/add_test.cpp +++ /dev/null @@ -1,11 +0,0 @@ -#include -#include "../src/add.h" - -TEST_CASE("add works") { - SECTION("equals") { - REQUIRE(add(2, 2) == 4); - } - SECTION("not equals") { - REQUIRE(add(2, 2) != 5); - } -} \ No newline at end of file diff --git a/test/public_test.cpp b/test/public_test.cpp new file mode 100644 index 0000000..6ee3b23 --- /dev/null +++ b/test/public_test.cpp @@ -0,0 +1,8 @@ +#include +#include "../src/vkvm.h" + +TEST_CASE("add works") { + SECTION("equals") { + REQUIRE(getText() == "Hallo Welt"); + } +} \ No newline at end of file From 6805da501404352fe16fb2010d1b9dc97d6b22af Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Thu, 7 Nov 2019 17:19:10 +0100 Subject: [PATCH 16/34] ~ fixing style issues --- .ci/clang-tidy.sh | 2 +- test/public_test.cpp | 2 +- test/test_main.cpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) mode change 100644 => 100755 .ci/clang-tidy.sh diff --git a/.ci/clang-tidy.sh b/.ci/clang-tidy.sh old mode 100644 new mode 100755 index eae5b12..c20c8fb --- a/.ci/clang-tidy.sh +++ b/.ci/clang-tidy.sh @@ -9,7 +9,7 @@ filelist="$(find . -not \( -path './client/cpptoml/*' -prune \) -type f ! -name for file in $filelist; do if echo "$file" | grep -q -E ".*\.cpp$" ; then #Extra check missing dependencies due to clang-tidy doesn't toggle exit code. - clang_tidy_lib_check="$(clang-tidy -warnings-as-errors='*' -header-filter='.*,-cpptoml.hpp' -checks='*,-llvm-header-guard,-fuchsia-statically-constructed-objects,-fuchsia-default-arguments,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-pro-bounds-constant-array-index,-bugprone-narrowing-conversions,-cppcoreguidelines-narrowing-conversions,-cppcoreguidelines-pro-type-const-cast,-cppcoreguidelines-pro-type-reinterpret-cast,-hicpp-signed-bitwise,-bugprone-exception-escape,-cppcoreguidelines-pro-type-member-init,-cppcoreguidelines-pro-type-cstyle-cast,-hicpp-member-init' "$file" -- -I. -std=c++14 2>&1)" + clang_tidy_lib_check="$(clang-tidy -warnings-as-errors='*' -header-filter='.*,-cpptoml.hpp' -checks='*,-llvm-header-guard,-fuchsia-statically-constructed-objects,-fuchsia-default-arguments,-fuchsia-default-argument-calls,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-pro-bounds-constant-array-index,-bugprone-narrowing-conversions,-cppcoreguidelines-narrowing-conversions,-cppcoreguidelines-pro-type-const-cast,-cppcoreguidelines-pro-type-reinterpret-cast,-hicpp-signed-bitwise,-bugprone-exception-escape,-cppcoreguidelines-pro-type-member-init,-cppcoreguidelines-pro-type-cstyle-cast,-hicpp-member-init' "$file" -- -I. -std=c++14 2>&1)" for tidy_line in $clang_tidy_lib_check; do echo "$tidy_line" | grep -q -v -E "^Error while processing*" if [ $? -eq 1 ]; then diff --git a/test/public_test.cpp b/test/public_test.cpp index 6ee3b23..2806ff4 100644 --- a/test/public_test.cpp +++ b/test/public_test.cpp @@ -1,4 +1,4 @@ -#include +#include "catch2/catch.hpp" #include "../src/vkvm.h" TEST_CASE("add works") { diff --git a/test/test_main.cpp b/test/test_main.cpp index 1a32dbe..ef0a1b7 100644 --- a/test/test_main.cpp +++ b/test/test_main.cpp @@ -1,6 +1,6 @@ #define CATCH_CONFIG_MAIN -#include +#include "catch2/catch.hpp" //Dont touch this file. // add your own tests in this directory according to https://github.com/catchorg/Catch2/blob/master/docs/tutorial.md \ No newline at end of file From 70ac30303635ae312f466cab57104b0ddb5f7c7b Mon Sep 17 00:00:00 2001 From: Julian Hinxlage Date: Thu, 7 Nov 2019 17:44:06 +0100 Subject: [PATCH 17/34] ~ updated changelog for 0.2.0 --- CHANGELOG.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9214fad..c8b82de 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,8 +6,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## [Unreleased] +## [0.2.0] +### Added +- shared memory access (local) +- signaling - Continuous Integration ## [0.1.0] - 2019-10-24 From f9c866231b4124ffedf6481b6ff772b3cdb4ad09 Mon Sep 17 00:00:00 2001 From: Shaohua Tong Date: Fri, 8 Nov 2019 18:30:27 +0100 Subject: [PATCH 18/34] getShareMemory(char *address, int size, int offset) hinzufuegen, ob man es braucht. --- src/SharedMemoryAccess.cpp | 17 +++++++++++++++++ src/SharedMemoryAccess.h | 13 +++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/SharedMemoryAccess.cpp b/src/SharedMemoryAccess.cpp index 0fd13dd..412d54b 100644 --- a/src/SharedMemoryAccess.cpp +++ b/src/SharedMemoryAccess.cpp @@ -61,10 +61,27 @@ void writeSharedMemory(char *data, int size, int offset) { return; /* we could return -1, not sure if it will be useful to continue after there is no shm allocated for any reason*/ } else { + std::cout << "writing" << std::endl; char *shm_pointer = (char *) shmat(shmId, NULL, 0); semaphoreOperation(LOCK); memcpy(shm_pointer + offset, data, size); + shmdt(shm_pointer); //close shm_pointer semaphoreOperation(UNLOCK); + std::cout << "writing successed" << std::endl; + } +} + +void getSharedMemory(char *address, int size, int offset) { + int shmId = shmget(impl.sharedMemoryKey, NULL, 0); + if(shmId < 0) { + return; + } else { + char *shm_pointer = (char *) shmat(shmId, NULL, 0); + semaphoreOperation(LOCK); + memcpy(address, shm_pointer + offset, size); + shmdt(shm_pointer); //close shm_pointer + semaphoreOperation(UNLOCK); + return; } } diff --git a/src/SharedMemoryAccess.h b/src/SharedMemoryAccess.h index 949307b..f50dffb 100644 --- a/src/SharedMemoryAccess.h +++ b/src/SharedMemoryAccess.h @@ -12,6 +12,15 @@ */ char *getSharedMemory(); +/** + * use lock and unlock when writing, + * set content to the address + * @param *address target address + * @param size of data + * @param offset where the data is written on the shared memory + */ +void getSharedMemory(char *address, int size, int offset); + /** * * @return the size of the shared memory @@ -24,10 +33,10 @@ void unlockSharedMemory(); /** * - * @param data poiter to data + * @param data pointer to data * @param size of data * @param offset where the data is written on the shared memory */ -void writeSharedMemory(char *data, int size, int offset); +void writeSharedMemory(char *date, int size, int offset); #endif //LIBRARY_SHAREDMEMORYACCESSS_H From 843be8d47cb4f86e5cc05f15ef769a9714107400 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Tue, 12 Nov 2019 11:11:13 +0000 Subject: [PATCH 19/34] ~ CI: clang_tidy step now contains catch2 & fltk --- .gitlab-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index aa908c8..52e2f38 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -9,7 +9,7 @@ stages: clang_tidy: - image: jhasse/clang-tidy + image: joethei/clang_tidy stage: style tags: - docker-ci From f5c461a05b4b18a4d0e780e081258ae766214112 Mon Sep 17 00:00:00 2001 From: Julian Hinxlage Date: Tue, 12 Nov 2019 14:07:02 +0100 Subject: [PATCH 20/34] ~ fixed clang-tidy errors --- src/Color.cpp | 2 +- src/Color.h | 2 +- src/FontType.cpp | 13 +++++--- src/FontType.h | 16 ++++++---- src/KeyCode.cpp | 4 +-- src/KeyCode.h | 8 +++-- src/SharedMemoryAccess.cpp | 65 ++++++++++++++++++-------------------- src/SharedMemoryAccess.h | 2 +- src/internal.cpp | 14 +++++--- src/internal.h | 6 ++-- src/log.cpp | 9 +++--- src/vkvm.cpp | 36 +++++++++++---------- src/vkvm.h | 6 ++-- test/public_test.cpp | 4 ++- 14 files changed, 102 insertions(+), 85 deletions(-) diff --git a/src/Color.cpp b/src/Color.cpp index 7d796fb..721ea91 100644 --- a/src/Color.cpp +++ b/src/Color.cpp @@ -1,7 +1,7 @@ #include "Color.h" -Color::Color(unsigned char red, unsigned char green, unsigned char blue) +Color::Color(unsigned char red, unsigned char green, unsigned char blue) noexcept : red(red), green(green), blue(blue){ } diff --git a/src/Color.h b/src/Color.h index 1191da4..a896cdb 100644 --- a/src/Color.h +++ b/src/Color.h @@ -15,7 +15,7 @@ private: unsigned char blue; public: - Color(unsigned char red, unsigned char green, unsigned char blue); + Color(unsigned char red, unsigned char green, unsigned char blue) noexcept; unsigned char getRed(); unsigned char getGreen(); diff --git a/src/FontType.cpp b/src/FontType.cpp index 6ef7a54..84b75c8 100644 --- a/src/FontType.cpp +++ b/src/FontType.cpp @@ -1,20 +1,25 @@ #include "FontType.h" -FontType::FontType(std::string name, int height, int width) { +FontType::FontType(int id, std::string name, int height, int width) noexcept { + this->id = id; this->name = std::move(name); this->height = height; this->width = width; } -std::string FontType::getName() { +int FontType::getId() const{ + return id; +} + +std::string FontType::getName() const{ return name; } -int FontType::getHeight() { +int FontType::getHeight() const{ return height; } -int FontType::getWidth() { +int FontType::getWidth() const{ return width; } diff --git a/src/FontType.h b/src/FontType.h index cf76bf4..82c32e9 100644 --- a/src/FontType.h +++ b/src/FontType.h @@ -1,25 +1,27 @@ -#include - #ifndef LIBRARY_FONT_H + #define LIBRARY_FONT_H #include +#include class FontType { private: + int id; std::string name; int height; int width; public: - FontType(std::string name, int height, int width); - std::string getName(); - int getHeight(); - int getWidth(); + FontType(int id, std::string name, int height, int width) noexcept; + int getId() const; + std::string getName() const; + int getHeight() const; + int getWidth() const; }; -const static FontType font_1 = FontType("DummyFont", 10, 5); +const static FontType font_1 = FontType(1, "DummyFont", 10, 5); #endif diff --git a/src/KeyCode.cpp b/src/KeyCode.cpp index 6e7e2c7..950ddfc 100644 --- a/src/KeyCode.cpp +++ b/src/KeyCode.cpp @@ -1,9 +1,9 @@ #include "KeyCode.h" -KeyCode::KeyCode(int value) : value(value) {} +KeyCode::KeyCode(int16_t value) noexcept : value(value) {} -int KeyCode::getValue() { +int16_t KeyCode::getValue() { return value; } diff --git a/src/KeyCode.h b/src/KeyCode.h index a59a5cd..f3452fe 100644 --- a/src/KeyCode.h +++ b/src/KeyCode.h @@ -1,12 +1,14 @@ #ifndef LIBRARY_KEYCODE_H #define LIBRARY_KEYCODE_H +#include + class KeyCode { private: - int value; + int16_t value; public: - explicit KeyCode(int value); - int getValue(); + explicit KeyCode(int16_t value) noexcept; + int16_t getValue(); }; const static KeyCode Backspace = KeyCode(8); diff --git a/src/SharedMemoryAccess.cpp b/src/SharedMemoryAccess.cpp index 412d54b..bdbd9b6 100644 --- a/src/SharedMemoryAccess.cpp +++ b/src/SharedMemoryAccess.cpp @@ -1,24 +1,24 @@ // // Created by Cigerxwin Chaker on 05.11.19. // -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include +#include #include +#include +#include +#include +#include +#include +#include +#include -#include "internal.h" #include "SharedMemoryAccess.h" /* header is important for the shmID. name could be different. maybe not needed cause: (shmget(memory_access_key, NULL, 0)) */ +#include "internal.h" -#define PERM 0666 /* Zugriffsrechte */ -#define LOCK -1 -#define UNLOCK 1 -#define SEM_KEY 123458L +#define PERM 0666 /* access rights */ +#define LOCK (-1) +#define UNLOCK (1) +#define SEM_KEY (123458L) //int memoryAccessKey; /* var type is int. but could be another type. */ //TODO: look after type in sharedmemory group int semId; @@ -26,29 +26,30 @@ struct sembuf semaphore; std::vector localSharedMemory; -int initSemaphore () { +int initSemaphore() { /* Testen, ob das Semaphor bereits existiert */ - semId = semget (SEM_KEY, 0, IPC_PRIVATE); + semId = semget(SEM_KEY, 0, IPC_PRIVATE); if (semId < 0) { /* ... existiert noch nicht, also anlegen */ /* Alle Zugriffsrechte der Dateikreierungsmaske */ /* erlauben */ umask(0); - semId = semget (SEM_KEY, 1, IPC_CREAT | IPC_EXCL | PERM); + semId = semget(SEM_KEY, 1, IPC_CREAT | IPC_EXCL | PERM); if (semId < 0) { return -1; } /* Semaphor mit 1 initialisieren */ - if (semctl (semId, 0, SETVAL, (int) 1) == -1) + if (semctl(semId, 0, SETVAL, 1) == -1){ return -1; + } } return 1; } -int semaphoreOperation (int op) { - semaphore.sem_op = (short)op; +int semaphoreOperation(int op) { + semaphore.sem_op = static_cast(op); semaphore.sem_flg = SEM_UNDO; - if( semop (semId, &semaphore, 1) == -1) { + if (semop(semId, &semaphore, 1) == -1) { perror(" semop "); return -1; } @@ -56,13 +57,10 @@ int semaphoreOperation (int op) { } void writeSharedMemory(char *data, int size, int offset) { - int shmId = shmget(impl.sharedMemoryKey, NULL, 0); // dont init just get the ID if already existing. - if(shmId < 0 ) { - return; - /* we could return -1, not sure if it will be useful to continue after there is no shm allocated for any reason*/ - } else { + int shmId = shmget(impl.sharedMemoryKey, 0, 0); // dont init just get the ID if already existing. + if (shmId >= 0) { std::cout << "writing" << std::endl; - char *shm_pointer = (char *) shmat(shmId, NULL, 0); + auto *shm_pointer = reinterpret_cast(shmat(shmId, nullptr, 0)); semaphoreOperation(LOCK); memcpy(shm_pointer + offset, data, size); shmdt(shm_pointer); //close shm_pointer @@ -72,20 +70,17 @@ void writeSharedMemory(char *data, int size, int offset) { } void getSharedMemory(char *address, int size, int offset) { - int shmId = shmget(impl.sharedMemoryKey, NULL, 0); - if(shmId < 0) { - return; - } else { - char *shm_pointer = (char *) shmat(shmId, NULL, 0); + 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); - return; } } -char* getSharedMemory() { +char *getSharedMemory() { /* int shmId = shmget(impl.sharedMemoryKey, NULL, 0); if(shmId < 0) { @@ -97,7 +92,7 @@ char* getSharedMemory() { */ //using a local buffer for shared memory testing - if(localSharedMemory.empty()){ + if (localSharedMemory.empty()) { initSemaphore(); localSharedMemory.resize(impl.sharedMemorySize * 1024); } diff --git a/src/SharedMemoryAccess.h b/src/SharedMemoryAccess.h index f50dffb..16cde8b 100644 --- a/src/SharedMemoryAccess.h +++ b/src/SharedMemoryAccess.h @@ -37,6 +37,6 @@ void unlockSharedMemory(); * @param size of data * @param offset where the data is written on the shared memory */ -void writeSharedMemory(char *date, int size, int offset); +void writeSharedMemory(char *data, int size, int offset); #endif //LIBRARY_SHAREDMEMORYACCESSS_H diff --git a/src/internal.cpp b/src/internal.cpp index 4fb1a26..21792e0 100644 --- a/src/internal.cpp +++ b/src/internal.cpp @@ -2,8 +2,8 @@ #include "SharedMemoryAccess.h" #include "vkvm.h" -#include #include +#include Impl impl; @@ -16,11 +16,11 @@ void onSignal(int signalNumber, void(*callback)(int)) { } InterruptEntry *getInterrupTable(){ - return (InterruptEntry*)(getSharedMemory() + sizeof(Registers) + impl.reservedSize); + return reinterpret_cast(getSharedMemory() + sizeof(Registers) + impl.reservedSize); } Registers *getRegisters(){ - return (Registers*)getSharedMemory(); + return reinterpret_cast(getSharedMemory()); } char *getTextArea(){ @@ -68,5 +68,11 @@ void setMousePosition(int x, int y) { } void buttonPressed(KeyCode keyCode) { - //TODO + lockSharedMemory(); + auto reg = getRegisters(); + if(reg->keyboardBuffer_index_write == sizeof(reg->keyboardBuffer)){ + reg->keyboardBuffer_index_write = 0; + } + reg->keyboardBuffer[reg->keyboardBuffer_index_write++] = keyCode.getValue(); + unlockSharedMemory(); } diff --git a/src/internal.h b/src/internal.h index 098575a..0cc1952 100644 --- a/src/internal.h +++ b/src/internal.h @@ -32,9 +32,9 @@ struct Registers { int textMode_font_height; int mouse_pos_x; int mouse_pos_y; - char keyboardBuffer[16]; - int keyboardBuffer_index_w; - int keyboardBuffer_index_r; + short keyboardBuffer[16]; + int keyboardBuffer_index_write; + int keyboardBuffer_index_read; }; struct InterruptEntry { diff --git a/src/log.cpp b/src/log.cpp index 42dfbe5..0b4448d 100644 --- a/src/log.cpp +++ b/src/log.cpp @@ -8,7 +8,7 @@ #include //converts the level to a string of the level -const char *getLevelName(LogLevel level){ +auto getLevelName(LogLevel level){ switch(level){ case LogLevel::DEBUG: return "DEBUG"; @@ -26,7 +26,7 @@ const char *getLevelName(LogLevel level){ } //converts the level to a ansi color code -const char *getLevelColor(LogLevel level){ +auto getLevelColor(LogLevel level){ switch(level){ case LogLevel::DEBUG: return "0;37"; @@ -72,7 +72,7 @@ void logTime(){ void log(const std::string &msg, LogLevel level) { if(level >= logLevel) { std::string levelName = getLevelName(level); - int maxLevelNameLength = 8; + const int maxLevelNameLength = 8; //time std::cout << "["; @@ -92,7 +92,8 @@ void log(const std::string &msg, LogLevel level) { if(c == '\n'){ //intend newlines so that they align with the start of the message std::cout << "\n"; - for(int i = 0; i < 22;i++){ + const int paddingSize = 22; + for(int i = 0; i < paddingSize;i++){ std::cout << " "; } }else{ diff --git a/src/vkvm.cpp b/src/vkvm.cpp index 9ea06b3..9899ebd 100644 --- a/src/vkvm.cpp +++ b/src/vkvm.cpp @@ -1,14 +1,18 @@ -#include "vkvm.h" -#include "internal.h" #include "SharedMemoryAccess.h" +#include "internal.h" +#include "vkvm.h" -#include #include +#include void initialize(int pid) { impl.sharedMemoryPid = pid; - impl.sharedMemoryKey = 12345; - impl.sharedMemorySize = 8000; + + const int sharedMemoryKey = 12345; + impl.sharedMemoryKey = sharedMemoryKey; + + const int sharedMemorySize = 8000; + impl.sharedMemorySize = sharedMemorySize; //set default values setCharactersPerRow(60); @@ -23,7 +27,7 @@ void initialize(int pid) { setTimerInterruptInterval(10); } -bool registerEvent(EventType type, std::function handler) { +bool registerEvent(EventType type, const std::function &handler) { int signum = SIGUSR1 + impl.eventTable.size(); auto ivt = getInterrupTable(); @@ -55,10 +59,10 @@ bool setPixel(int x, int y, Color color) { } Color getPixel(int x, int y) { - //TODO: other than RGB colores + //TODO(julian): other than RGB colores //only RGB colores for now - unsigned char *ptr = (unsigned char *)getPixelArea() + (y * getWidth() + x) * 3; - return Color(ptr[0], ptr[1], ptr[2]); + unsigned char *ptr = reinterpret_cast(getPixelArea()) + (y * getWidth() + x) * 3; + return {ptr[0], ptr[1], ptr[2]}; } bool setText(std::string text) { @@ -82,11 +86,11 @@ std::string getText() { } LayoutVersion getLayoutVersion() { - return (LayoutVersion)getRegisters()->layout_version; + return static_cast(getRegisters()->layout_version); } void reset() { - //TODO + //TODO(julian): reset } int getWidth() { @@ -168,14 +172,14 @@ int getCharactersPerColumn() { } FontType getFont() { - //TODO + //TODO(julian): get font properly return font_1; } -void setFont(FontType newValue) { - //TODO +void setFont(const FontType &newValue) { + //TODO(julian): setFont properly lockSharedMemory(); - getRegisters()->textMode_font = 0; + getRegisters()->textMode_font = newValue.getId(); unlockSharedMemory(); } @@ -184,6 +188,6 @@ std::pair getMousePosition() { } KeyCode getLastPressedKey() { - //TODO + //TODO(julian): get key properly return KeyCode(0); } diff --git a/src/vkvm.h b/src/vkvm.h index 180a1d5..a8bc68c 100644 --- a/src/vkvm.h +++ b/src/vkvm.h @@ -1,14 +1,14 @@ #ifndef LIBRARY_VKVM_H #define LIBRARY_VKVM_H -#include -#include #include "Color.h" #include "EventType.h" #include "GraphicMode.h" #include "FontType.h" #include "KeyCode.h" #include "LayoutVersion.h" +#include +#include /** * @since 0.1.0 @@ -184,7 +184,7 @@ FontType getFont(); * set text mode font. * @param newValue new text mode font. */ -void setFont(FontType newValue); +void setFont(const FontType &newValue); /** * get current mouse position diff --git a/test/public_test.cpp b/test/public_test.cpp index 2806ff4..93aaee1 100644 --- a/test/public_test.cpp +++ b/test/public_test.cpp @@ -2,7 +2,9 @@ #include "../src/vkvm.h" TEST_CASE("add works") { + initialize(0); + setText("Hello World"); SECTION("equals") { - REQUIRE(getText() == "Hallo Welt"); + REQUIRE(getText() == "Hello World"); } } \ No newline at end of file From eb3c5210383167acd4182c3decd2e0b29cf612d5 Mon Sep 17 00:00:00 2001 From: Julian Hinxlage Date: Tue, 12 Nov 2019 14:29:33 +0100 Subject: [PATCH 21/34] ~ fixed clang-tidy define errors --- src/SharedMemoryAccess.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/SharedMemoryAccess.cpp b/src/SharedMemoryAccess.cpp index bdbd9b6..e410c3f 100644 --- a/src/SharedMemoryAccess.cpp +++ b/src/SharedMemoryAccess.cpp @@ -15,10 +15,10 @@ #include "SharedMemoryAccess.h" /* header is important for the shmID. name could be different. maybe not needed cause: (shmget(memory_access_key, NULL, 0)) */ #include "internal.h" -#define PERM 0666 /* access rights */ -#define LOCK (-1) -#define UNLOCK (1) -#define SEM_KEY (123458L) +constexpr int PERM = 0666; /* access rights */ +constexpr int LOCK = -1; +constexpr int UNLOCK = 1; +constexpr int SEM_KEY = 123458L; //int memoryAccessKey; /* var type is int. but could be another type. */ //TODO: look after type in sharedmemory group int semId; From a1f9f62f5185af871c841f4180c767685fa0f44f Mon Sep 17 00:00:00 2001 From: Julian Hinxlage Date: Tue, 12 Nov 2019 15:03:17 +0100 Subject: [PATCH 22/34] ~ fixed clang-tidy magic-number errors --- src/log.cpp | 8 +++++--- src/vkvm.cpp | 29 ++++++++++++++--------------- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/log.cpp b/src/log.cpp index 0b4448d..4e053f5 100644 --- a/src/log.cpp +++ b/src/log.cpp @@ -52,17 +52,19 @@ void logTime(){ struct tm *timeinfo; timeinfo = localtime(&rawtime); - if (timeinfo->tm_hour < 10) { + constexpr int decimalBase = 10; + + if (timeinfo->tm_hour < decimalBase) { std::cout << "0"; } std::cout << timeinfo->tm_hour; std::cout << ":"; - if (timeinfo->tm_min < 10) { + if (timeinfo->tm_min < decimalBase) { std::cout << "0"; } std::cout << timeinfo->tm_min; std::cout << ":"; - if (timeinfo->tm_sec < 10) { + if (timeinfo->tm_sec < decimalBase) { std::cout << "0"; } std::cout << timeinfo->tm_sec; diff --git a/src/vkvm.cpp b/src/vkvm.cpp index 9899ebd..16d4bbc 100644 --- a/src/vkvm.cpp +++ b/src/vkvm.cpp @@ -5,26 +5,25 @@ #include #include +// NOLINT void initialize(int pid) { impl.sharedMemoryPid = pid; - const int sharedMemoryKey = 12345; - impl.sharedMemoryKey = sharedMemoryKey; + impl.sharedMemoryKey = 12345;// NOLINT - const int sharedMemorySize = 8000; - impl.sharedMemorySize = sharedMemorySize; + impl.sharedMemorySize = 8000;// NOLINT //set default values - setCharactersPerRow(60); - setCharactersPerColumn(20); - setHeight(600); - setWidth(800); - setMousePosition(42,42); - setBackgroundColor(Color(200,50,20)); - setForegroundColor(Color(20,200,50)); - setMode(GraphicMode::TrueColor); - setRedrawInterval(20); - setTimerInterruptInterval(10); + setCharactersPerRow(60);// NOLINT + setCharactersPerColumn(20);// NOLINT + setHeight(600);// NOLINT + setWidth(800);// NOLINT + setMousePosition(42,42);// NOLINT + setBackgroundColor(Color(200,50,20));// NOLINT + setForegroundColor(Color(20,200,50));// NOLINT + setMode(GraphicMode::TrueColor);// NOLINT + setRedrawInterval(20);// NOLINT + setTimerInterruptInterval(10);// NOLINT } bool registerEvent(EventType type, const std::function &handler) { @@ -68,7 +67,7 @@ Color getPixel(int x, int y) { bool setText(std::string text) { lockSharedMemory(); char *ptr = getTextArea(); - for(int i = 0; i < text.size();i++){ + for(int i = 0; i < static_cast(text.size());i++){ if(i >= getCharactersPerColumn() * getCharactersPerRow()){ break; } From 9e51071bc70db658d10b90afa6a64df9e43bf5bd Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Tue, 12 Nov 2019 15:10:40 +0100 Subject: [PATCH 23/34] fixing wrong clang-tidy arguments --- .ci/clang-tidy.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.ci/clang-tidy.sh b/.ci/clang-tidy.sh index c20c8fb..b0c6745 100755 --- a/.ci/clang-tidy.sh +++ b/.ci/clang-tidy.sh @@ -5,11 +5,11 @@ bool=false # explicitly set IFS to contain only a line feed IFS=' ' -filelist="$(find . -not \( -path './client/cpptoml/*' -prune \) -type f ! -name "$(printf "*\n*")")" +filelist="$(find . -not \( -path './*build*' -prune \) -type f ! -name "$(printf "*\n*")")" for file in $filelist; do if echo "$file" | grep -q -E ".*\.cpp$" ; then #Extra check missing dependencies due to clang-tidy doesn't toggle exit code. - clang_tidy_lib_check="$(clang-tidy -warnings-as-errors='*' -header-filter='.*,-cpptoml.hpp' -checks='*,-llvm-header-guard,-fuchsia-statically-constructed-objects,-fuchsia-default-arguments,-fuchsia-default-argument-calls,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-pro-bounds-constant-array-index,-bugprone-narrowing-conversions,-cppcoreguidelines-narrowing-conversions,-cppcoreguidelines-pro-type-const-cast,-cppcoreguidelines-pro-type-reinterpret-cast,-hicpp-signed-bitwise,-bugprone-exception-escape,-cppcoreguidelines-pro-type-member-init,-cppcoreguidelines-pro-type-cstyle-cast,-hicpp-member-init' "$file" -- -I. -std=c++14 2>&1)" + clang_tidy_lib_check="$(clang-tidy -warnings-as-errors='*' -header-filter='.*,-cpptoml.hpp' -checks='*,-llvm-header-guard,-fuchsia-statically-constructed-objects,-fuchsia-default-arguments,-fuchsia-default-arguments-calls,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-pro-bounds-constant-array-index,-bugprone-narrowing-conversions,-cppcoreguidelines-narrowing-conversions,-cppcoreguidelines-pro-type-const-cast,-cppcoreguidelines-pro-type-reinterpret-cast,-hicpp-signed-bitwise,-bugprone-exception-escape,-cppcoreguidelines-pro-type-member-init,-cppcoreguidelines-pro-type-cstyle-cast,-hicpp-member-init' "$file" -- -I. -std=c++14 2>&1)" for tidy_line in $clang_tidy_lib_check; do echo "$tidy_line" | grep -q -v -E "^Error while processing*" if [ $? -eq 1 ]; then From 2f0980b12503627e08365a5e3636fbf0c95dc495 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Tue, 12 Nov 2019 15:22:02 +0100 Subject: [PATCH 24/34] ~clang tidy checks all files now --- .ci/clang-tidy.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.ci/clang-tidy.sh b/.ci/clang-tidy.sh index b0c6745..913c79a 100755 --- a/.ci/clang-tidy.sh +++ b/.ci/clang-tidy.sh @@ -7,7 +7,7 @@ IFS=' ' filelist="$(find . -not \( -path './*build*' -prune \) -type f ! -name "$(printf "*\n*")")" for file in $filelist; do - if echo "$file" | grep -q -E ".*\.cpp$" ; then + if echo "$file" | grep -q -E ".*(\.cpp|\.h|\.hpp)$" ; then #Extra check missing dependencies due to clang-tidy doesn't toggle exit code. clang_tidy_lib_check="$(clang-tidy -warnings-as-errors='*' -header-filter='.*,-cpptoml.hpp' -checks='*,-llvm-header-guard,-fuchsia-statically-constructed-objects,-fuchsia-default-arguments,-fuchsia-default-arguments-calls,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-pro-bounds-constant-array-index,-bugprone-narrowing-conversions,-cppcoreguidelines-narrowing-conversions,-cppcoreguidelines-pro-type-const-cast,-cppcoreguidelines-pro-type-reinterpret-cast,-hicpp-signed-bitwise,-bugprone-exception-escape,-cppcoreguidelines-pro-type-member-init,-cppcoreguidelines-pro-type-cstyle-cast,-hicpp-member-init' "$file" -- -I. -std=c++14 2>&1)" for tidy_line in $clang_tidy_lib_check; do From 7bb6846b33366d0351af88e9d3e1927073b1543c Mon Sep 17 00:00:00 2001 From: Julian Hinxlage Date: Tue, 12 Nov 2019 15:40:36 +0100 Subject: [PATCH 25/34] ~ 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 From a42af25abc3af31830fd2c939ee31ec267070e8c Mon Sep 17 00:00:00 2001 From: Julian Hinxlage Date: Tue, 12 Nov 2019 15:58:00 +0100 Subject: [PATCH 26/34] ~ fixed initialize without shared memory --- src/vkvm.cpp | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/vkvm.cpp b/src/vkvm.cpp index 16d4bbc..a81cf22 100644 --- a/src/vkvm.cpp +++ b/src/vkvm.cpp @@ -13,17 +13,19 @@ void initialize(int pid) { impl.sharedMemorySize = 8000;// NOLINT - //set default values - setCharactersPerRow(60);// NOLINT - setCharactersPerColumn(20);// NOLINT - setHeight(600);// NOLINT - setWidth(800);// NOLINT - setMousePosition(42,42);// NOLINT - setBackgroundColor(Color(200,50,20));// NOLINT - setForegroundColor(Color(20,200,50));// NOLINT - setMode(GraphicMode::TrueColor);// NOLINT - setRedrawInterval(20);// NOLINT - setTimerInterruptInterval(10);// NOLINT + if(getSharedMemory() != nullptr) { + //set default values + setCharactersPerRow(60);// NOLINT + setCharactersPerColumn(20);// NOLINT + setHeight(600);// NOLINT + setWidth(800);// NOLINT + setMousePosition(42, 42);// NOLINT + setBackgroundColor(Color(200, 50, 20));// NOLINT + setForegroundColor(Color(20, 200, 50));// NOLINT + setMode(GraphicMode::TrueColor);// NOLINT + setRedrawInterval(20);// NOLINT + setTimerInterruptInterval(10);// NOLINT + } } bool registerEvent(EventType type, const std::function &handler) { From 051f366411d400a48180a89672babcce00ed492c Mon Sep 17 00:00:00 2001 From: Julian Hinxlage Date: Tue, 12 Nov 2019 16:25:57 +0100 Subject: [PATCH 27/34] + setDefaultValues --- src/vkvm.cpp | 5 +++-- src/vkvm.h | 2 ++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/vkvm.cpp b/src/vkvm.cpp index a81cf22..c73c524 100644 --- a/src/vkvm.cpp +++ b/src/vkvm.cpp @@ -8,11 +8,12 @@ // NOLINT void initialize(int pid) { impl.sharedMemoryPid = pid; - impl.sharedMemoryKey = 12345;// NOLINT - impl.sharedMemorySize = 8000;// NOLINT + setDefaultValues(); +} +void setDefaultValues(){ if(getSharedMemory() != nullptr) { //set default values setCharactersPerRow(60);// NOLINT diff --git a/src/vkvm.h b/src/vkvm.h index e908471..6d581b4 100644 --- a/src/vkvm.h +++ b/src/vkvm.h @@ -23,6 +23,8 @@ */ void initialize(int pid); +void setDefaultValues(); + /** * set pixel at a x,y position to a certain color. * @param x x coordinate of pixel From 0ae6f39f860d612134571fe745d4d5099b35d19f Mon Sep 17 00:00:00 2001 From: Julian Hinxlage Date: Tue, 12 Nov 2019 18:09:50 +0100 Subject: [PATCH 28/34] + logging with multiple arguments --- src/SharedMemoryAccess.cpp | 13 +++---------- src/log.cpp | 2 +- src/log.h | 22 +++++++++++++++++++++- src/vkvm.h | 1 + 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/SharedMemoryAccess.cpp b/src/SharedMemoryAccess.cpp index 636b09c..456e47b 100644 --- a/src/SharedMemoryAccess.cpp +++ b/src/SharedMemoryAccess.cpp @@ -57,16 +57,9 @@ int semaphoreOperation(int op) { } void writeSharedMemory(char *data, int size, int offset) { - int shmId = shmget(impl.sharedMemoryKey, 0, 0); // dont init just get the ID if already existing. - if (shmId >= 0) { - std::cout << "writing" << std::endl; - auto *shm_pointer = reinterpret_cast(shmat(shmId, nullptr, 0)); - semaphoreOperation(LOCK); - memcpy(shm_pointer + offset, data, size); - shmdt(shm_pointer); //close shm_pointer - semaphoreOperation(UNLOCK); - std::cout << "writing successed" << std::endl; - } + lockSharedMemory(); + memcpy(getSharedMemory() + offset, data, size); + unlockSharedMemory(); } char *getSharedMemory() { diff --git a/src/log.cpp b/src/log.cpp index 4e053f5..c70fc0a 100644 --- a/src/log.cpp +++ b/src/log.cpp @@ -71,7 +71,7 @@ void logTime(){ } //log the message -void log(const std::string &msg, LogLevel level) { +void log(LogLevel level, const std::string &msg) { if(level >= logLevel) { std::string levelName = getLevelName(level); const int maxLevelNameLength = 8; diff --git a/src/log.h b/src/log.h index 6b7f94f..049e28d 100644 --- a/src/log.h +++ b/src/log.h @@ -2,6 +2,7 @@ #define LIBRARY_LOG_H #include +#include enum LogLevel{ DEBUG = 1, @@ -17,7 +18,26 @@ enum LogLevel{ * @author Julian Hinxlage * @since 0.1.0 */ -void log(const std::string &msg, LogLevel level = LogLevel::INFO); +void log(LogLevel level, const std::string &msg); + + +template +static void buildString(std::stringstream &stream, T t) { + stream << t; +} + +template +static void buildString(std::stringstream &stream, T t, Ts... ts) { + stream << t; + buildString(stream, ts...); +} + +template +static void log(LogLevel level, T... t){ + std::stringstream stream; + buildString(stream, t...); + log(level, stream.str()); +} /** diff --git a/src/vkvm.h b/src/vkvm.h index 6d581b4..86a0663 100644 --- a/src/vkvm.h +++ b/src/vkvm.h @@ -7,6 +7,7 @@ #include "FontType.h" #include "KeyCode.h" #include "LayoutVersion.h" +#include "log.h" #include #include From 7b2ddf9e0a5f802dfbd8da15f7748cc840b4ca39 Mon Sep 17 00:00:00 2001 From: Julian Hinxlage Date: Tue, 12 Nov 2019 18:24:35 +0100 Subject: [PATCH 29/34] ~ fixed semaphore error --- src/SharedMemoryAccess.h | 1 + src/vkvm.cpp | 1 + 2 files changed, 2 insertions(+) diff --git a/src/SharedMemoryAccess.h b/src/SharedMemoryAccess.h index 16cde8b..514e46f 100644 --- a/src/SharedMemoryAccess.h +++ b/src/SharedMemoryAccess.h @@ -5,6 +5,7 @@ #ifndef LIBRARY_SHAREDMEMORYACCESSS_H #define LIBRARY_SHAREDMEMORYACCESSS_H +int initSemaphore(); /** * use lock and unlock when writing diff --git a/src/vkvm.cpp b/src/vkvm.cpp index c73c524..f137046 100644 --- a/src/vkvm.cpp +++ b/src/vkvm.cpp @@ -10,6 +10,7 @@ void initialize(int pid) { impl.sharedMemoryPid = pid; impl.sharedMemoryKey = 12345;// NOLINT impl.sharedMemorySize = 8000;// NOLINT + initSemaphore(); setDefaultValues(); } From 5aa0a7862f74853752ecdad299ce58b975cd2783 Mon Sep 17 00:00:00 2001 From: Julian Hinxlage Date: Tue, 12 Nov 2019 19:05:33 +0100 Subject: [PATCH 30/34] ~ use local memory if no shared memory exists --- src/SharedMemoryAccess.cpp | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/SharedMemoryAccess.cpp b/src/SharedMemoryAccess.cpp index 456e47b..7e12c69 100644 --- a/src/SharedMemoryAccess.cpp +++ b/src/SharedMemoryAccess.cpp @@ -63,23 +63,25 @@ void writeSharedMemory(char *data, int size, int offset) { } char *getSharedMemory() { - constexpr bool useLocal = false; + bool useLocal = false; - if(useLocal){ + if(!useLocal){ + int shmId = shmget(impl.sharedMemoryKey, NULL, 0); + if(shmId < 0) { + //no shared memory found + useLocal = true; + } else { + return (char *) shmat(shmId, NULL, 0); + } + } + + 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); - } } } From c8569eb3b4f33da5afd5cebb57c13651cdee9132 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Wed, 13 Nov 2019 13:37:49 +0100 Subject: [PATCH 31/34] + everything namespaced ~ fixed clang-tidy errors --- .ci/clang-tidy.sh | 4 +- CMakeLists.txt | 2 +- src/Color.cpp | 47 +-- src/Color.h | 33 -- src/Color.hpp | 40 +++ src/EventType.h | 20 -- src/EventType.hpp | 24 ++ src/FontType.cpp | 41 +-- src/FontType.h | 27 -- src/FontType.hpp | 35 ++ src/GraphicMode.h | 16 - src/GraphicMode.hpp | 19 ++ src/KeyCode.cpp | 12 +- src/KeyCode.h | 17 - src/KeyCode.hpp | 22 ++ src/LayoutVersion.h | 8 - src/LayoutVersion.hpp | 12 + src/SharedMemoryAccess.cpp | 150 ++++---- ...dMemoryAccess.h => SharedMemoryAccess.hpp} | 22 +- src/internal.cpp | 139 ++++---- src/internal.h | 126 ------- src/internal.hpp | 132 +++++++ src/log.cpp | 172 +++++----- src/log.h | 50 --- src/log.hpp | 52 +++ src/vkvm.cpp | 321 +++++++++--------- src/{vkvm.h => vkvm.hpp} | 85 ++--- test/public_test.cpp | 8 +- 28 files changed, 847 insertions(+), 789 deletions(-) delete mode 100644 src/Color.h create mode 100644 src/Color.hpp delete mode 100644 src/EventType.h create mode 100644 src/EventType.hpp delete mode 100644 src/FontType.h create mode 100644 src/FontType.hpp delete mode 100644 src/GraphicMode.h create mode 100644 src/GraphicMode.hpp delete mode 100644 src/KeyCode.h create mode 100644 src/KeyCode.hpp delete mode 100644 src/LayoutVersion.h create mode 100644 src/LayoutVersion.hpp rename src/{SharedMemoryAccess.h => SharedMemoryAccess.hpp} (60%) delete mode 100644 src/internal.h create mode 100644 src/internal.hpp delete mode 100644 src/log.h create mode 100644 src/log.hpp rename src/{vkvm.h => vkvm.hpp} (63%) diff --git a/.ci/clang-tidy.sh b/.ci/clang-tidy.sh index 913c79a..2e1b4e7 100755 --- a/.ci/clang-tidy.sh +++ b/.ci/clang-tidy.sh @@ -5,11 +5,11 @@ bool=false # explicitly set IFS to contain only a line feed IFS=' ' -filelist="$(find . -not \( -path './*build*' -prune \) -type f ! -name "$(printf "*\n*")")" +filelist="$(find . -not \( -path './*build*' -prune \) -not \( -path './include' -prune \) -not \( -path './lib' -prune \) -type f ! -name "$(printf "*\n*")")" for file in $filelist; do if echo "$file" | grep -q -E ".*(\.cpp|\.h|\.hpp)$" ; then #Extra check missing dependencies due to clang-tidy doesn't toggle exit code. - clang_tidy_lib_check="$(clang-tidy -warnings-as-errors='*' -header-filter='.*,-cpptoml.hpp' -checks='*,-llvm-header-guard,-fuchsia-statically-constructed-objects,-fuchsia-default-arguments,-fuchsia-default-arguments-calls,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-pro-bounds-constant-array-index,-bugprone-narrowing-conversions,-cppcoreguidelines-narrowing-conversions,-cppcoreguidelines-pro-type-const-cast,-cppcoreguidelines-pro-type-reinterpret-cast,-hicpp-signed-bitwise,-bugprone-exception-escape,-cppcoreguidelines-pro-type-member-init,-cppcoreguidelines-pro-type-cstyle-cast,-hicpp-member-init' "$file" -- -I. -std=c++14 2>&1)" + clang_tidy_lib_check="$(clang-tidy -warnings-as-errors='*' -header-filter='.*,-cpptoml.hpp' -checks='*,-llvm-header-guard,-fuchsia-statically-constructed-objects,-fuchsia*,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-pro-bounds-constant-array-index,-bugprone-narrowing-conversions,-cppcoreguidelines-narrowing-conversions,-cppcoreguidelines-pro-type-const-cast,-cppcoreguidelines-pro-type-reinterpret-cast,-hicpp-signed-bitwise,-bugprone-exception-escape,-cppcoreguidelines-pro-type-member-init,-cppcoreguidelines-pro-type-cstyle-cast,-hicpp-member-init,-google-readability-namespace-comments,-llvm-namespace-comment' "$file" -- -I. -std=c++14 2>&1)" for tidy_line in $clang_tidy_lib_check; do echo "$tidy_line" | grep -q -v -E "^Error while processing*" if [ $? -eq 1 ]; then diff --git a/CMakeLists.txt b/CMakeLists.txt index e355699..ba82a8c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,7 +17,7 @@ set(CMAKE_CXX_CLANG_TIDY clang-tidy;-header-filter=.;-checks=*;) file(GLOB_RECURSE SOURCES src/*.cpp) -file(GLOB_RECURSE HEADERS src/*.h) +file(GLOB_RECURSE HEADERS src/*.hpp) file(GLOB_RECURSE TESTS test/*.cpp) include_directories(src) include_directories(test) diff --git a/src/Color.cpp b/src/Color.cpp index 721ea91..85ac7df 100644 --- a/src/Color.cpp +++ b/src/Color.cpp @@ -1,32 +1,33 @@ +#include "Color.hpp" -#include "Color.h" +namespace vkvm { -Color::Color(unsigned char red, unsigned char green, unsigned char blue) noexcept - : red(red), green(green), blue(blue){ + Color::Color(unsigned char red, unsigned char green, unsigned char blue) noexcept + : red(red), green(green), blue(blue) { -} + } -unsigned char Color::getRed() { - return red; -} + auto Color::getRed() -> unsigned char { + return red; + } -unsigned char Color::getGreen() { - return green; -} + auto Color::getGreen() -> unsigned char { + return green; + } -unsigned char Color::getBlue() { - return blue; -} + auto Color::getBlue() -> unsigned char { + return blue; + } -void Color::setRed(unsigned char value) { - red = value; -} + auto Color::setRed(unsigned char value) -> void { + red = value; + } -void Color::setGreen(unsigned char value) { - green = value; -} - -void Color::setBlue(unsigned char value) { - blue = value; -} + auto Color::setGreen(unsigned char value) -> void { + green = value; + } + auto Color::setBlue(unsigned char value) -> void { + blue = value; + } +} \ No newline at end of file diff --git a/src/Color.h b/src/Color.h deleted file mode 100644 index a896cdb..0000000 --- a/src/Color.h +++ /dev/null @@ -1,33 +0,0 @@ -#ifndef LIBRARY_COLOR_H -#define LIBRARY_COLOR_H - - -/** - * color values represented as rgb values. - * @author Johannes Theiner - * @since 0.1.0 - */ -class Color { - -private: - unsigned char red; - unsigned char green; - unsigned char blue; - -public: - Color(unsigned char red, unsigned char green, unsigned char blue) noexcept; - - unsigned char getRed(); - unsigned char getGreen(); - unsigned char getBlue(); - - void setRed(unsigned char value); - void setGreen(unsigned char value); - void setBlue(unsigned char value); - -}; - -const static Color black = Color(0, 0, 0); -const static Color white = Color(255, 255, 255); - -#endif diff --git a/src/Color.hpp b/src/Color.hpp new file mode 100644 index 0000000..a8159b1 --- /dev/null +++ b/src/Color.hpp @@ -0,0 +1,40 @@ +#ifndef LIBRARY_COLOR_HPP +#define LIBRARY_COLOR_HPP + +namespace vkvm { + +/** + * color values represented as rgb values. + * @author Johannes Theiner + * @since 0.1.0 + */ + class Color { + + private: + unsigned char red; + unsigned char green; + unsigned char blue; + + public: + Color(unsigned char red, unsigned char green, unsigned char blue) noexcept; + + auto getRed() -> unsigned char; + + auto getGreen() -> unsigned char; + + auto getBlue() -> unsigned char; + + auto setRed(unsigned char value) -> void; + + auto setGreen(unsigned char value) -> void; + + auto setBlue(unsigned char value) -> void; + + }; + + const static Color black = Color(0, 0, 0); + const static Color white = Color(255, 255, 255); + +} + +#endif diff --git a/src/EventType.h b/src/EventType.h deleted file mode 100644 index 6daa1ea..0000000 --- a/src/EventType.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef LIBRARY_EVENTTYPE_H -#define LIBRARY_EVENTTYPE_H - -/** - * types of events. - * @since 0.1.0 - * @uthor Johannes Theiner - */ -enum EventType { - Timer = 1, - MouseMove = 2, - MouseButton = 3, - KeyDown = 4, - KeyUp = 5, - UpdateControlRegisters = 6, - Redraw = 7, - RenderText = 8 -}; - -#endif diff --git a/src/EventType.hpp b/src/EventType.hpp new file mode 100644 index 0000000..79f4d94 --- /dev/null +++ b/src/EventType.hpp @@ -0,0 +1,24 @@ +#ifndef LIBRARY_EVENTTYPE_HPP +#define LIBRARY_EVENTTYPE_HPP + +namespace vkvm { + +/** + * types of events. + * @since 0.1.0 + * @uthor Johannes Theiner + */ + enum EventType { + Timer = 1, + MouseMove = 2, + MouseButton = 3, + KeyDown = 4, + KeyUp = 5, + UpdateControlRegisters = 6, + Redraw = 7, + RenderText = 8 + }; + +} + +#endif diff --git a/src/FontType.cpp b/src/FontType.cpp index 84b75c8..46ebb00 100644 --- a/src/FontType.cpp +++ b/src/FontType.cpp @@ -1,25 +1,28 @@ -#include "FontType.h" +#include "FontType.hpp" +namespace vkvm { -FontType::FontType(int id, std::string name, int height, int width) noexcept { - this->id = id; - this->name = std::move(name); - this->height = height; - this->width = width; -} + FontType::FontType(int id, std::string name, int height, int width) noexcept { + this->id = id; + this->name = std::move(name); + this->height = height; + this->width = width; + } -int FontType::getId() const{ - return id; -} + auto FontType::getId() const -> int { + return id; + } -std::string FontType::getName() const{ - return name; -} + auto FontType::getName() const -> std::string { + return name; + } -int FontType::getHeight() const{ - return height; -} + auto FontType::getHeight() const -> int { + return height; + } -int FontType::getWidth() const{ - return width; -} + auto FontType::getWidth() const -> int { + return width; + } + +} \ No newline at end of file diff --git a/src/FontType.h b/src/FontType.h deleted file mode 100644 index 82c32e9..0000000 --- a/src/FontType.h +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef LIBRARY_FONT_H - -#define LIBRARY_FONT_H - -#include -#include - -class FontType { -private: - int id; - std::string name; - int height; - int width; - -public: - FontType(int id, std::string name, int height, int width) noexcept; - int getId() const; - std::string getName() const; - int getHeight() const; - int getWidth() const; - -}; - - -const static FontType font_1 = FontType(1, "DummyFont", 10, 5); - -#endif diff --git a/src/FontType.hpp b/src/FontType.hpp new file mode 100644 index 0000000..defecf0 --- /dev/null +++ b/src/FontType.hpp @@ -0,0 +1,35 @@ +#ifndef LIBRARY_FONT_H + +#define LIBRARY_FONT_H + +#include +#include + +namespace vkvm { + + class FontType { + private: + int id; + std::string name; + int height; + int width; + + public: + FontType(int id, std::string name, int height, int width) noexcept; + + auto getId() const -> int; + + auto getName() const -> std::string; + + auto getHeight() const -> int; + + auto getWidth() const -> int; + + }; + + + const static FontType font_1 = FontType(1, "DummyFont", 10, 5); + +} + +#endif diff --git a/src/GraphicMode.h b/src/GraphicMode.h deleted file mode 100644 index d447ee8..0000000 --- a/src/GraphicMode.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef LIBRARY_GRAPHICMODE_H -#define LIBRARY_GRAPHICMODE_H - -/** - * all possible graphics modes. - * @author Johannes Theiner - * @since 0.1.0 - */ -enum GraphicMode { - Text = 1, - TwoColors = 2, - Gray_256 = 3, - TrueColor = 4, -}; - -#endif diff --git a/src/GraphicMode.hpp b/src/GraphicMode.hpp new file mode 100644 index 0000000..c28b205 --- /dev/null +++ b/src/GraphicMode.hpp @@ -0,0 +1,19 @@ +#ifndef LIBRARY_GRAPHICMODE_HPP +#define LIBRARY_GRAPHICMODE_HPP + + +namespace vkvm { +/** + * all possible graphics modes. + * @author Johannes Theiner + * @since 0.1.0 + */ + enum GraphicMode { + Text = 1, + TwoColors = 2, + Gray_256 = 3, + TrueColor = 4, + }; +} + +#endif diff --git a/src/KeyCode.cpp b/src/KeyCode.cpp index 49c4a6e..aaa7e7d 100644 --- a/src/KeyCode.cpp +++ b/src/KeyCode.cpp @@ -1,9 +1,11 @@ +#include "KeyCode.hpp" -#include "KeyCode.h" +namespace vkvm { -KeyCode::KeyCode(int value) noexcept : value(value) {} + KeyCode::KeyCode(int value) noexcept : value(value) {} + + auto KeyCode::getValue() -> int { + return value; + } -int KeyCode::getValue() { - return value; } - diff --git a/src/KeyCode.h b/src/KeyCode.h deleted file mode 100644 index eb97ab9..0000000 --- a/src/KeyCode.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef LIBRARY_KEYCODE_H -#define LIBRARY_KEYCODE_H - -#include - -class KeyCode { -private: - int value; -public: - explicit KeyCode(int value) noexcept; - int getValue(); -}; - -const static KeyCode Backspace = KeyCode(8); -const static KeyCode tabulator = KeyCode(9); - -#endif diff --git a/src/KeyCode.hpp b/src/KeyCode.hpp new file mode 100644 index 0000000..34d2890 --- /dev/null +++ b/src/KeyCode.hpp @@ -0,0 +1,22 @@ +#ifndef LIBRARY_KEYCODE_HPP +#define LIBRARY_KEYCODE_HPP + +#include + +namespace vkvm { + + class KeyCode { + private: + int value; + public: + explicit KeyCode(int value) noexcept; + + auto getValue() -> int; + }; + + const static KeyCode Backspace = KeyCode(8); + const static KeyCode tabulator = KeyCode(9); + +} + +#endif \ No newline at end of file diff --git a/src/LayoutVersion.h b/src/LayoutVersion.h deleted file mode 100644 index ce24e93..0000000 --- a/src/LayoutVersion.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef LIBRARY_LAYOUTVERSION_H -#define LIBRARY_LAYOUTVERSION_H - -enum LayoutVersion { - V1 = 1 -}; - -#endif diff --git a/src/LayoutVersion.hpp b/src/LayoutVersion.hpp new file mode 100644 index 0000000..7e36985 --- /dev/null +++ b/src/LayoutVersion.hpp @@ -0,0 +1,12 @@ +#ifndef LIBRARY_LAYOUTVERSION_HPP +#define LIBRARY_LAYOUTVERSION_HPP + +namespace vkvm { + + enum LayoutVersion { + V1 = 1 + }; + +} + +#endif diff --git a/src/SharedMemoryAccess.cpp b/src/SharedMemoryAccess.cpp index 7e12c69..ff68a4d 100644 --- a/src/SharedMemoryAccess.cpp +++ b/src/SharedMemoryAccess.cpp @@ -1,6 +1,3 @@ -// -// Created by Cigerxwin Chaker on 05.11.19. -// #include #include #include @@ -12,93 +9,96 @@ #include #include -#include "SharedMemoryAccess.h" /* header is important for the shmID. name could be different. maybe not needed cause: (shmget(memory_access_key, NULL, 0)) */ -#include "internal.h" +#include "SharedMemoryAccess.hpp" /* header is important for the shmID. name could be different. maybe not needed cause: (shmget(memory_access_key, NULL, 0)) */ +#include "internal.hpp" -constexpr int PERM = 0666; /* access rights */ -constexpr int LOCK = -1; -constexpr int UNLOCK = 1; -constexpr int SEM_KEY = 123458L; +namespace vkvm { + constexpr int PERM = 0666; /* access rights */ + constexpr int LOCK = -1; + constexpr int UNLOCK = 1; + constexpr int SEM_KEY = 123458L; //int memoryAccessKey; /* var type is int. but could be another type. */ //TODO: look after type in sharedmemory group -int semId; -struct sembuf semaphore; + int semId; + struct sembuf semaphore; -std::vector localSharedMemory; + std::vector localSharedMemory; -int initSemaphore() { - /* Testen, ob das Semaphor bereits existiert */ - semId = semget(SEM_KEY, 0, IPC_PRIVATE); - if (semId < 0) { - /* ... existiert noch nicht, also anlegen */ - /* Alle Zugriffsrechte der Dateikreierungsmaske */ - /* erlauben */ - umask(0); - semId = semget(SEM_KEY, 1, IPC_CREAT | IPC_EXCL | PERM); + auto initSemaphore() -> int { + /* Testen, ob das Semaphor bereits existiert */ + semId = semget(SEM_KEY, 0, IPC_PRIVATE); if (semId < 0) { + /* ... existiert noch nicht, also anlegen */ + /* Alle Zugriffsrechte der Dateikreierungsmaske */ + /* erlauben */ + umask(0); + semId = semget(SEM_KEY, 1, IPC_CREAT | IPC_EXCL | PERM); + if (semId < 0) { + return -1; + } + /* Semaphor mit 1 initialisieren */ + if (semctl(semId, 0, SETVAL, 1) == -1){ + return -1; + } + } + return 1; + } + + auto semaphoreOperation(int op) -> int { + semaphore.sem_op = static_cast(op); + semaphore.sem_flg = SEM_UNDO; + if (semop(semId, &semaphore, 1) == -1) { + perror(" semop "); return -1; } - /* Semaphor mit 1 initialisieren */ - if (semctl(semId, 0, SETVAL, 1) == -1){ - return -1; + return 1; + } + + auto writeSharedMemory(char *data, int size, int offset) -> void { + lockSharedMemory(); + memcpy(getSharedMemory() + offset, data, size); + unlockSharedMemory(); + } + + auto getSharedMemory() -> char * { + bool useLocal = false; + + if(!useLocal){ + int shmId = shmget(impl.sharedMemoryKey, NULL, 0); + if(shmId < 0) { + //no shared memory found + useLocal = true; + } else { + return (char *) shmat(shmId, NULL, 0); + } } - } - return 1; -} -int semaphoreOperation(int op) { - semaphore.sem_op = static_cast(op); - semaphore.sem_flg = SEM_UNDO; - if (semop(semId, &semaphore, 1) == -1) { - perror(" semop "); - return -1; - } - return 1; -} - -void writeSharedMemory(char *data, int size, int offset) { - lockSharedMemory(); - memcpy(getSharedMemory() + offset, data, size); - unlockSharedMemory(); -} - -char *getSharedMemory() { - bool useLocal = false; - - if(!useLocal){ - int shmId = shmget(impl.sharedMemoryKey, NULL, 0); - if(shmId < 0) { - //no shared memory found - useLocal = true; - } else { - return (char *) shmat(shmId, NULL, 0); + if(useLocal) { + //using a local buffer for shared memory testing + if (localSharedMemory.empty()) { + initSemaphore(); + localSharedMemory.resize(impl.sharedMemorySize * 1024); + } + return &localSharedMemory[0]; } } - if(useLocal) { - //using a local buffer for shared memory testing - if (localSharedMemory.empty()) { - initSemaphore(); - localSharedMemory.resize(impl.sharedMemorySize * 1024); - } - return &localSharedMemory[0]; + auto getSharedMemory(char *address, int size, int offset) -> void { + lockSharedMemory(); + memcpy(address, getSharedMemory() + offset, size); + unlockSharedMemory(); } -} -void getSharedMemory(char *address, int size, int offset) { - lockSharedMemory(); - memcpy(address, getSharedMemory() + offset, size); - unlockSharedMemory(); -} + auto getSharedMemorySize() -> int { + return impl.sharedMemorySize; + } -int getSharedMemorySize() { - return impl.sharedMemorySize; -} + auto lockSharedMemory() -> void { + semaphoreOperation(LOCK); + } -void lockSharedMemory() { - semaphoreOperation(LOCK); -} + auto unlockSharedMemory() -> void { + semaphoreOperation(UNLOCK); + } -void unlockSharedMemory() { - semaphoreOperation(UNLOCK); -} +} \ No newline at end of file diff --git a/src/SharedMemoryAccess.h b/src/SharedMemoryAccess.hpp similarity index 60% rename from src/SharedMemoryAccess.h rename to src/SharedMemoryAccess.hpp index 514e46f..7068262 100644 --- a/src/SharedMemoryAccess.h +++ b/src/SharedMemoryAccess.hpp @@ -1,17 +1,14 @@ -// -// Created by Cigerxwin Chaker on 05.11.19. -// - #ifndef LIBRARY_SHAREDMEMORYACCESSS_H #define LIBRARY_SHAREDMEMORYACCESSS_H -int initSemaphore(); +namespace vkvm { + auto initSemaphore() -> int; /** * use lock and unlock when writing * @return pointer to the shared memory */ -char *getSharedMemory(); + auto getSharedMemory() -> char *; /** * use lock and unlock when writing, @@ -20,17 +17,17 @@ char *getSharedMemory(); * @param size of data * @param offset where the data is written on the shared memory */ -void getSharedMemory(char *address, int size, int offset); + auto getSharedMemory(char *address, int size, int offset) -> void; /** * * @return the size of the shared memory */ -int getSharedMemorySize(); + auto getSharedMemorySize() -> int; -void lockSharedMemory(); + auto lockSharedMemory() -> void; -void unlockSharedMemory(); + auto unlockSharedMemory() -> void; /** * @@ -38,6 +35,7 @@ void unlockSharedMemory(); * @param size of data * @param offset where the data is written on the shared memory */ -void writeSharedMemory(char *data, int size, int offset); + auto writeSharedMemory(char *data, int size, int offset) -> void; +} -#endif //LIBRARY_SHAREDMEMORYACCESSS_H +#endif diff --git a/src/internal.cpp b/src/internal.cpp index 21792e0..88d167a 100644 --- a/src/internal.cpp +++ b/src/internal.cpp @@ -1,78 +1,83 @@ -#include "internal.h" -#include "SharedMemoryAccess.h" -#include "vkvm.h" +#include "internal.hpp" +#include "SharedMemoryAccess.hpp" +#include "vkvm.hpp" #include #include -Impl impl; -void sendSignal(pid_t pid, int signalNumber) { - kill(pid, signalNumber); -} +namespace vkvm { -void onSignal(int signalNumber, void(*callback)(int)) { - signal(signalNumber, callback); -} + Impl impl; -InterruptEntry *getInterrupTable(){ - return reinterpret_cast(getSharedMemory() + sizeof(Registers) + impl.reservedSize); -} - -Registers *getRegisters(){ - return reinterpret_cast(getSharedMemory()); -} - -char *getTextArea(){ - return ((getSharedMemory() + sizeof(Registers) + impl.reservedSize) + - sizeof(InterruptEntry) * impl.interruptEntyCount); -} - -char *getPixelArea(){ - return (((getSharedMemory() + sizeof(Registers) + impl.reservedSize) + - sizeof(InterruptEntry) * impl.interruptEntyCount) + - getCharactersPerRow() * getCharactersPerColumn()); -} - -bool callEvent(EventType type) { - auto ivt = getInterrupTable(); - if(ivt[type].pid != 0){ - sendSignal(ivt[type].pid, ivt[type].signum); + auto sendSignal(pid_t pid, int signalNumber) -> void { + kill(pid, signalNumber); } - return true; -} -void setLayoutVersion(LayoutVersion newValue) { - lockSharedMemory(); - getRegisters()->layout_version = newValue; - unlockSharedMemory(); -} - -void setCharactersPerColumn(int newValue) { - lockSharedMemory(); - getRegisters()->characters_per_column = newValue; - unlockSharedMemory(); -} - -void setCharactersPerRow(int newValue) { - lockSharedMemory(); - getRegisters()->characters_per_row = newValue; - unlockSharedMemory(); -} - -void setMousePosition(int x, int y) { - lockSharedMemory(); - getRegisters()->mouse_pos_x = x; - getRegisters()->mouse_pos_y = y; - unlockSharedMemory(); -} - -void buttonPressed(KeyCode keyCode) { - lockSharedMemory(); - auto reg = getRegisters(); - if(reg->keyboardBuffer_index_write == sizeof(reg->keyboardBuffer)){ - reg->keyboardBuffer_index_write = 0; + auto onSignal(int signalNumber, void(*callback)(int)) -> void { + signal(signalNumber, callback); } - reg->keyboardBuffer[reg->keyboardBuffer_index_write++] = keyCode.getValue(); - unlockSharedMemory(); + + auto getInterrupTable() -> InterruptEntry * { + return reinterpret_cast(getSharedMemory() + sizeof(Registers) + impl.reservedSize); + } + + auto getRegisters() -> Registers * { + return reinterpret_cast(getSharedMemory()); + } + + auto getTextArea() -> char * { + return ((getSharedMemory() + sizeof(Registers) + impl.reservedSize) + + sizeof(InterruptEntry) * impl.interruptEntyCount); + } + + auto getPixelArea() -> char * { + return (((getSharedMemory() + sizeof(Registers) + impl.reservedSize) + + sizeof(InterruptEntry) * impl.interruptEntyCount) + + getCharactersPerRow() * getCharactersPerColumn()); + } + + auto callEvent(EventType type) -> bool { + auto ivt = getInterrupTable(); + if (ivt[type].pid != 0) { + sendSignal(ivt[type].pid, ivt[type].signum); + } + return true; + } + + auto setLayoutVersion(LayoutVersion newValue) -> void { + lockSharedMemory(); + getRegisters()->layout_version = newValue; + unlockSharedMemory(); + } + + auto setCharactersPerColumn(int newValue) -> void { + lockSharedMemory(); + getRegisters()->characters_per_column = newValue; + unlockSharedMemory(); + } + + auto setCharactersPerRow(int newValue) -> void { + lockSharedMemory(); + getRegisters()->characters_per_row = newValue; + unlockSharedMemory(); + } + + auto setMousePosition(int x, int y) -> void { + lockSharedMemory(); + getRegisters()->mouse_pos_x = x; + getRegisters()->mouse_pos_y = y; + unlockSharedMemory(); + } + + auto buttonPressed(KeyCode keyCode) -> void { + lockSharedMemory(); + auto reg = getRegisters(); + if (reg->keyboardBuffer_index_write == sizeof(reg->keyboardBuffer)) { + reg->keyboardBuffer_index_write = 0; + } + reg->keyboardBuffer[reg->keyboardBuffer_index_write++] = keyCode.getValue(); + unlockSharedMemory(); + } + } diff --git a/src/internal.h b/src/internal.h deleted file mode 100644 index 0cc1952..0000000 --- a/src/internal.h +++ /dev/null @@ -1,126 +0,0 @@ -#ifndef LIBRARY_INTERNAL_H -#define LIBRARY_INTERNAL_H - -#include "EventType.h" -#include "KeyCode.h" -#include "LayoutVersion.h" -#include "GraphicMode.h" -#include "Color.h" -#include -#include -#include - -/** - * the Control Registers - * @author Julian Hinxlage - * @since 0.1.0 - */ -struct Registers { - int layout_version; - int trigger_reset; - int width_pixels; - int height_pixels; - GraphicMode graphicMode; - int autoRedrawInterval; - int timerInterruptInterval; - Color background_color; - Color foreground_color; - int characters_per_row; - int characters_per_column; - int textMode_font; - int textMode_font_width; - int textMode_font_height; - int mouse_pos_x; - int mouse_pos_y; - short keyboardBuffer[16]; - int keyboardBuffer_index_write; - int keyboardBuffer_index_read; -}; - -struct InterruptEntry { - int pid; - int signum; -}; - -/** - * internal values for the library. - */ -struct Impl { - int sharedMemoryPid; - key_t sharedMemoryKey; - int sharedMemorySize; - int interruptEntyCount = 64; - int reservedSize = 1024; - std::vector> eventTable; -}; - -extern Impl impl; - -/** - * send a signal to a process - * @param pid of the process to which the signal is send - * @param signalNumber - */ -void sendSignal(pid_t pid, int signalNumber); - -/** - * calls the callback if a signal is received - * @param signalNumber - */ -void onSignal(int signalNumber, void(*callback)(int)); - - -InterruptEntry *getInterrupTable(); -Registers *getRegisters(); -char *getTextArea(); -char *getPixelArea(); - - -/** - * set layout version. - * @param newValue new layout version number. - */ -void setLayoutVersion(LayoutVersion newValue); - -/** - * set characters per column for current font. - * @param newValue characters per column. - */ -void setCharactersPerColumn(int newValue); - -/** - * set characters per row for current font. - * @param newValue - */ -void setCharactersPerRow(int newValue); - -/** - * call a specific event. - * @param type - * @return true if there is a handler registered. - */ -bool callEvent(EventType type); - -/** - * set mouse position to x,y value. - * @param x x coordinate - * @param y y coordinate - */ -void setMousePosition(int x, int y); - -/** - * register pressed button. - * @param keyCode pressed key. - */ -void buttonPressed(KeyCode keyCode); - -// Shared Memory Layout -// -------------------------------------------------------------------- -// struct ControlRegisters -// char reserved[1024] -// Interrupt Vector Table [64] -// text area [max_textMode_width * max_textMode_height] -// pixel area [max_height_pixels * max_height_pixels * sizeof(uint_32)] - - -#endif \ No newline at end of file diff --git a/src/internal.hpp b/src/internal.hpp new file mode 100644 index 0000000..af86223 --- /dev/null +++ b/src/internal.hpp @@ -0,0 +1,132 @@ +#ifndef LIBRARY_INTERNAL_HPP +#define LIBRARY_INTERNAL_HPP + +#include "EventType.hpp" +#include "KeyCode.hpp" +#include "LayoutVersion.hpp" +#include "GraphicMode.hpp" +#include "Color.hpp" +#include +#include +#include + +namespace vkvm { + +/** + * the Control Registers + * @author Julian Hinxlage + * @since 0.1.0 + */ + struct Registers { + int layout_version; + int trigger_reset; + int width_pixels; + int height_pixels; + GraphicMode graphicMode; + int autoRedrawInterval; + int timerInterruptInterval; + Color background_color; + Color foreground_color; + int characters_per_row; + int characters_per_column; + int textMode_font; + int textMode_font_width; + int textMode_font_height; + int mouse_pos_x; + int mouse_pos_y; + short keyboardBuffer[16]; + int keyboardBuffer_index_write; + int keyboardBuffer_index_read; + }; + + struct InterruptEntry { + int pid; + int signum; + }; + +/** + * internal values for the library. + */ + struct Impl { + int sharedMemoryPid; + key_t sharedMemoryKey; + int sharedMemorySize; + int interruptEntyCount = 64; + int reservedSize = 1024; + std::vector> eventTable; + }; + + extern Impl impl; + +/** + * send a signal to a process + * @param pid of the process to which the signal is send + * @param signalNumber + */ + auto sendSignal(pid_t pid, int signalNumber) -> void; + +/** + * calls the callback if a signal is received + * @param signalNumber + */ + auto onSignal(int signalNumber, void(*callback)(int)) -> void; + + + auto getInterrupTable() -> InterruptEntry *; + + auto getRegisters() -> Registers *; + + auto getTextArea() -> char * ; + + auto getPixelArea() -> char *; + + +/** + * set layout version. + * @param newValue new layout version number. + */ + auto setLayoutVersion(LayoutVersion newValue) -> void; + +/** + * set characters per column for current font. + * @param newValue characters per column. + */ + auto setCharactersPerColumn(int newValue) -> void; + +/** + * set characters per row for current font. + * @param newValue + */ + auto setCharactersPerRow(int newValue) -> void; + +/** + * call a specific event. + * @param type + * @return true if there is a handler registered. + */ + auto callEvent(EventType type) -> bool; + +/** + * set mouse position to x,y value. + * @param x x coordinate + * @param y y coordinate + */ + auto setMousePosition(int x, int y) -> void; + +/** + * register pressed button. + * @param keyCode pressed key. + */ + auto buttonPressed(KeyCode keyCode) -> void; + +// Shared Memory Layout +// -------------------------------------------------------------------- +// struct ControlRegisters +// char reserved[1024] +// Interrupt Vector Table [64] +// text area [max_textMode_width * max_textMode_height] +// pixel area [max_height_pixels * max_height_pixels * sizeof(uint_32)] + +} + +#endif \ No newline at end of file diff --git a/src/log.cpp b/src/log.cpp index c70fc0a..c015f84 100644 --- a/src/log.cpp +++ b/src/log.cpp @@ -4,109 +4,111 @@ * @since 0.1.0 */ -#include "log.h" +#include "log.hpp" #include -//converts the level to a string of the level -auto getLevelName(LogLevel level){ - switch(level){ - case LogLevel::DEBUG: - return "DEBUG"; - case LogLevel::INFO: - return "INFO"; - case LogLevel::WARNING: - return "WARNING"; - case LogLevel::ERROR: - return "ERROR"; - case LogLevel::CRITICAL: - return "CRITICAL"; - default: - return "NON"; +namespace vkvm { + //converts the level to a string of the level + auto getLevelName(LogLevel level) -> std::string { + switch(level){ + case LogLevel::DEBUG: + return "DEBUG"; + case LogLevel::INFO: + return "INFO"; + case LogLevel::WARNING: + return "WARNING"; + case LogLevel::ERROR: + return "ERROR"; + case LogLevel::CRITICAL: + return "CRITICAL"; + default: + return "NON"; + } } -} //converts the level to a ansi color code -auto getLevelColor(LogLevel level){ - switch(level){ - case LogLevel::DEBUG: - return "0;37"; - case LogLevel::INFO: - return "0"; - case LogLevel::WARNING: - return "1;33"; - case LogLevel::ERROR: - return "1;31"; - case LogLevel::CRITICAL: - return "1;35"; - default: - return "0"; + auto getLevelColor(LogLevel level) -> std::string { + switch(level){ + case LogLevel::DEBUG: + return "0;37"; + case LogLevel::INFO: + return "0"; + case LogLevel::WARNING: + return "1;33"; + case LogLevel::ERROR: + return "1;31"; + case LogLevel::CRITICAL: + return "1;35"; + default: + return "0"; + } } -} -LogLevel logLevel = LogLevel::INFO; + LogLevel logLevel = LogLevel::INFO; //log the current time -void logTime(){ - time_t rawtime; - time(&rawtime); - struct tm *timeinfo; - timeinfo = localtime(&rawtime); + auto logTime() -> void { + time_t rawtime; + time(&rawtime); + struct tm *timeinfo; + timeinfo = localtime(&rawtime); - constexpr int decimalBase = 10; + constexpr int decimalBase = 10; - if (timeinfo->tm_hour < decimalBase) { - std::cout << "0"; + if (timeinfo->tm_hour < decimalBase) { + std::cout << "0"; + } + std::cout << timeinfo->tm_hour; + std::cout << ":"; + if (timeinfo->tm_min < decimalBase) { + std::cout << "0"; + } + std::cout << timeinfo->tm_min; + std::cout << ":"; + if (timeinfo->tm_sec < decimalBase) { + std::cout << "0"; + } + std::cout << timeinfo->tm_sec; } - std::cout << timeinfo->tm_hour; - std::cout << ":"; - if (timeinfo->tm_min < decimalBase) { - std::cout << "0"; - } - std::cout << timeinfo->tm_min; - std::cout << ":"; - if (timeinfo->tm_sec < decimalBase) { - std::cout << "0"; - } - std::cout << timeinfo->tm_sec; -} //log the message -void log(LogLevel level, const std::string &msg) { - if(level >= logLevel) { - std::string levelName = getLevelName(level); - const int maxLevelNameLength = 8; + auto log(LogLevel level, const std::string &msg) -> void { + if(level >= logLevel) { + std::string levelName = getLevelName(level); + const int maxLevelNameLength = 8; - //time - std::cout << "["; - logTime(); - std::cout << "] "; + //time + std::cout << "["; + logTime(); + std::cout << "] "; - //color and level name;lo - std::cout << "["; - std::cout << "\033[" << getLevelColor(level) << "m" << levelName << "\033[0m"; - std::cout << "] "; - for (int i = levelName.size(); i < maxLevelNameLength; i++) { - std::cout << " "; - } - - //message - for(char c : msg){ - if(c == '\n'){ - //intend newlines so that they align with the start of the message - std::cout << "\n"; - const int paddingSize = 22; - for(int i = 0; i < paddingSize;i++){ - std::cout << " "; - } - }else{ - std::cout << c; + //color and level name;lo + std::cout << "["; + std::cout << "\033[" << getLevelColor(level) << "m" << levelName << "\033[0m"; + std::cout << "] "; + for (int i = levelName.size(); i < maxLevelNameLength; i++) { + std::cout << " "; } + + //message + for(char c : msg){ + if(c == '\n'){ + //intend newlines so that they align with the start of the message + std::cout << "\n"; + const int paddingSize = 22; + for(int i = 0; i < paddingSize;i++){ + std::cout << " "; + } + }else{ + std::cout << c; + } + } + std::cout << "\n"; } - std::cout << "\n"; } -} -void setLogLevel(LogLevel level) { - logLevel = level; -} + auto setLogLevel(LogLevel level) -> void { + logLevel = level; + } +} diff --git a/src/log.h b/src/log.h deleted file mode 100644 index 049e28d..0000000 --- a/src/log.h +++ /dev/null @@ -1,50 +0,0 @@ -#ifndef LIBRARY_LOG_H -#define LIBRARY_LOG_H - -#include -#include - -enum LogLevel{ - DEBUG = 1, - INFO = 2, - WARNING = 3, - ERROR = 4, - CRITICAL = 5, - OFF = 6 -}; - -/** - * log the messgae with logLevel and timestamp - * @author Julian Hinxlage - * @since 0.1.0 - */ -void log(LogLevel level, const std::string &msg); - - -template -static void buildString(std::stringstream &stream, T t) { - stream << t; -} - -template -static void buildString(std::stringstream &stream, T t, Ts... ts) { - stream << t; - buildString(stream, ts...); -} - -template -static void log(LogLevel level, T... t){ - std::stringstream stream; - buildString(stream, t...); - log(level, stream.str()); -} - - -/** - * set the logLevel, the log function will use this to determine if the message is logged - * @author Julian Hinxlage - * @since 0.1.0 - */ -void setLogLevel(LogLevel level); - -#endif //LIBRARY_LOG_H diff --git a/src/log.hpp b/src/log.hpp new file mode 100644 index 0000000..311bef8 --- /dev/null +++ b/src/log.hpp @@ -0,0 +1,52 @@ +#ifndef LIBRARY_LOG_HPP +#define LIBRARY_LOG_HPP + +#include +#include + +namespace vkvm { + enum LogLevel{ + DEBUG = 1, + INFO = 2, + WARNING = 3, + ERROR = 4, + CRITICAL = 5, + OFF = 6 + }; + +/** + * log the messgae with logLevel and timestamp + * @author Julian Hinxlage + * @since 0.1.0 + */ + auto log(LogLevel level, const std::string &msg) -> void; + + + template + static auto buildString(std::stringstream &stream, T t) -> void { + stream << t; + } + + template + static auto buildString(std::stringstream &stream, T t, Ts... ts) -> void { + stream << t; + buildString(stream, ts...); + } + + template + static auto log(LogLevel level, T... t) -> void { + std::stringstream stream; + buildString(stream, t...); + log(level, stream.str()); + } + + +/** + * set the logLevel, the log function will use this to determine if the message is logged + * @author Julian Hinxlage + * @since 0.1.0 + */ + auto setLogLevel(LogLevel level) -> void; +} + +#endif diff --git a/src/vkvm.cpp b/src/vkvm.cpp index f137046..5b0390b 100644 --- a/src/vkvm.cpp +++ b/src/vkvm.cpp @@ -1,196 +1,199 @@ -#include "SharedMemoryAccess.h" -#include "internal.h" -#include "vkvm.h" +#include "SharedMemoryAccess.hpp" +#include "internal.hpp" +#include "vkvm.hpp" #include #include -// NOLINT -void initialize(int pid) { - impl.sharedMemoryPid = pid; - impl.sharedMemoryKey = 12345;// NOLINT - impl.sharedMemorySize = 8000;// NOLINT - initSemaphore(); - setDefaultValues(); -} - -void setDefaultValues(){ - if(getSharedMemory() != nullptr) { - //set default values - setCharactersPerRow(60);// NOLINT - setCharactersPerColumn(20);// NOLINT - setHeight(600);// NOLINT - setWidth(800);// NOLINT - setMousePosition(42, 42);// NOLINT - setBackgroundColor(Color(200, 50, 20));// NOLINT - setForegroundColor(Color(20, 200, 50));// NOLINT - setMode(GraphicMode::TrueColor);// NOLINT - setRedrawInterval(20);// NOLINT - setTimerInterruptInterval(10);// NOLINT +namespace vkvm { + // NOLINT + auto initialize(int pid) -> void { + impl.sharedMemoryPid = pid; + impl.sharedMemoryKey = 12345;// NOLINT + impl.sharedMemorySize = 8000;// NOLINT + initSemaphore(); + setDefaultValues(); } -} -bool registerEvent(EventType type, const std::function &handler) { - int signum = SIGUSR1 + impl.eventTable.size(); - auto ivt = getInterrupTable(); + auto setDefaultValues() -> void { + if(getSharedMemory() != nullptr) { + //set default values + setCharactersPerRow(60);// NOLINT + setCharactersPerColumn(20);// NOLINT + setHeight(600);// NOLINT + setWidth(800);// NOLINT + setMousePosition(42, 42);// NOLINT + setBackgroundColor(Color(200, 50, 20));// NOLINT + setForegroundColor(Color(20, 200, 50));// NOLINT + setMode(GraphicMode::TrueColor);// NOLINT + setRedrawInterval(20);// NOLINT + setTimerInterruptInterval(10);// NOLINT + } + } - lockSharedMemory(); + auto registerEvent(EventType type, const std::function &handler) -> bool { + int signum = SIGUSR1 + impl.eventTable.size(); + auto ivt = getInterrupTable(); - ivt[type].pid = getpid(); - ivt[type].signum = signum; - impl.eventTable.push_back(handler); + lockSharedMemory(); - onSignal(signum, [](int sig){ - if(sig >= SIGUSR1){ - if((sig - SIGUSR1) < impl.eventTable.size()){ - impl.eventTable[sig - SIGUSR1](); + ivt[type].pid = getpid(); + ivt[type].signum = signum; + impl.eventTable.push_back(handler); + + onSignal(signum, [](int sig){ + if(sig >= SIGUSR1){ + if((sig - SIGUSR1) < impl.eventTable.size()){ + impl.eventTable[sig - SIGUSR1](); + } } - } - }); + }); - unlockSharedMemory(); + unlockSharedMemory(); - return true; -} - -bool setPixel(int x, int y, Color color) { - char *ptr = getPixelArea() + (y * getWidth() + x) * 3; - ptr[0] = color.getRed(); - ptr[1] = color.getGreen(); - ptr[2] = color.getBlue(); - return false; -} - -Color getPixel(int x, int y) { - //TODO(julian): other than RGB colores - //only RGB colores for now - unsigned char *ptr = reinterpret_cast(getPixelArea()) + (y * getWidth() + x) * 3; - return {ptr[0], ptr[1], ptr[2]}; -} - -bool setText(std::string text) { - lockSharedMemory(); - char *ptr = getTextArea(); - for(int i = 0; i < static_cast(text.size());i++){ - if(i >= getCharactersPerColumn() * getCharactersPerRow()){ - break; - } - ptr[i] = text[i]; + return true; } - if(text.size() < getCharactersPerColumn() * getCharactersPerRow()){ - ptr[text.size()] = '\0'; + + auto setPixel(int x, int y, Color color) -> bool { + char *ptr = getPixelArea() + (y * getWidth() + x) * 3; + ptr[0] = color.getRed(); + ptr[1] = color.getGreen(); + ptr[2] = color.getBlue(); + return false; } - unlockSharedMemory(); - return true; -} -std::string getText() { - return std::string (getTextArea()); -} + auto getPixel(int x, int y) -> Color { + //TODO(julian): other than RGB colores + //only RGB colores for now + unsigned char *ptr = reinterpret_cast(getPixelArea()) + (y * getWidth() + x) * 3; + return {ptr[0], ptr[1], ptr[2]}; + } -LayoutVersion getLayoutVersion() { - return static_cast(getRegisters()->layout_version); -} + auto setText(std::string text) -> bool { + lockSharedMemory(); + char *ptr = getTextArea(); + for(int i = 0; i < static_cast(text.size());i++){ + if(i >= getCharactersPerColumn() * getCharactersPerRow()){ + break; + } + ptr[i] = text[i]; + } + if(text.size() < getCharactersPerColumn() * getCharactersPerRow()){ + ptr[text.size()] = '\0'; + } + unlockSharedMemory(); + return true; + } -void reset() { - //TODO(julian): reset -} + auto getText() -> std::string { + return std::string (getTextArea()); + } -int getWidth() { - return getRegisters()->width_pixels; -} + auto getLayoutVersion() -> LayoutVersion { + return static_cast(getRegisters()->layout_version); + } -void setWidth(int newValue) { - lockSharedMemory(); - getRegisters()->width_pixels = newValue; - unlockSharedMemory(); -} + auto reset() -> void { + //TODO(julian): reset + } -int getHeight() { - return getRegisters()->height_pixels; -} + auto getWidth() -> int { + return getRegisters()->width_pixels; + } -void setHeight(int newValue) { - lockSharedMemory(); - getRegisters()->height_pixels = newValue; - unlockSharedMemory(); -} + auto setWidth(int newValue) -> void { + lockSharedMemory(); + getRegisters()->width_pixels = newValue; + unlockSharedMemory(); + } -GraphicMode getMode() { - return getRegisters()->graphicMode; -} + auto getHeight() -> int { + return getRegisters()->height_pixels; + } -void setMode(GraphicMode newValue) { - lockSharedMemory(); - getRegisters()->graphicMode = newValue; - unlockSharedMemory(); -} + auto setHeight(int newValue) -> void { + lockSharedMemory(); + getRegisters()->height_pixels = newValue; + unlockSharedMemory(); + } -int getRedrawInterval() { - return getRegisters()->autoRedrawInterval; -} + auto getMode() -> GraphicMode { + return getRegisters()->graphicMode; + } -void setRedrawInterval(int newValue) { - lockSharedMemory(); - getRegisters()->autoRedrawInterval = newValue; - unlockSharedMemory(); -} + auto setMode(GraphicMode newValue) -> void { + lockSharedMemory(); + getRegisters()->graphicMode = newValue; + unlockSharedMemory(); + } -int getTimerInterruptInterval() { - return getRegisters()->timerInterruptInterval; -} + auto getRedrawInterval() -> int { + return getRegisters()->autoRedrawInterval; + } -void setTimerInterruptInterval(int newValue) { - lockSharedMemory(); - getRegisters()->timerInterruptInterval = newValue; - unlockSharedMemory(); -} + auto setRedrawInterval(int newValue) -> void { + lockSharedMemory(); + getRegisters()->autoRedrawInterval = newValue; + unlockSharedMemory(); + } -Color getBackgroundColor() { - return getRegisters()->background_color; -} + auto getTimerInterruptInterval() -> int { + return getRegisters()->timerInterruptInterval; + } -void setBackgroundColor(Color newValue) { - lockSharedMemory(); - getRegisters()->background_color = newValue; - unlockSharedMemory(); -} + auto setTimerInterruptInterval(int newValue) -> void { + lockSharedMemory(); + getRegisters()->timerInterruptInterval = newValue; + unlockSharedMemory(); + } -Color getForegroundColor() { - return getRegisters()->foreground_color; -} + auto getBackgroundColor() -> Color { + return getRegisters()->background_color; + } -void setForegroundColor(Color newValue) { - lockSharedMemory(); - getRegisters()->foreground_color = newValue; - unlockSharedMemory(); -} + auto setBackgroundColor(Color newValue) -> void { + lockSharedMemory(); + getRegisters()->background_color = newValue; + unlockSharedMemory(); + } -int getCharactersPerRow() { - return getRegisters()->characters_per_row; -} + auto getForegroundColor() -> Color { + return getRegisters()->foreground_color; + } -int getCharactersPerColumn() { - return getRegisters()->characters_per_column; -} + auto setForegroundColor(Color newValue) -> void { + lockSharedMemory(); + getRegisters()->foreground_color = newValue; + unlockSharedMemory(); + } -FontType getFont() { - //TODO(julian): get font properly - return font_1; -} + auto getCharactersPerRow() -> int { + return getRegisters()->characters_per_row; + } -void setFont(const FontType &newValue) { - //TODO(julian): setFont properly - lockSharedMemory(); - getRegisters()->textMode_font = newValue.getId(); - unlockSharedMemory(); -} + auto getCharactersPerColumn() -> int { + return getRegisters()->characters_per_column; + } -std::pair getMousePosition() { - return {getRegisters()->mouse_pos_x, getRegisters()->mouse_pos_y}; -} + auto getFont() -> FontType { + //TODO(julian): get font properly + return font_1; + } -KeyCode getLastPressedKey() { - //TODO(julian): get key properly - return KeyCode(0); -} + auto setFont(const FontType &newValue) -> void { + //TODO(julian): setFont properly + lockSharedMemory(); + getRegisters()->textMode_font = newValue.getId(); + unlockSharedMemory(); + } + + auto getMousePosition() -> Coordinates { + return {getRegisters()->mouse_pos_x, getRegisters()->mouse_pos_y}; + } + + auto getLastPressedKey() -> KeyCode { + //TODO(julian): get key properly + return KeyCode(0); + } + +} \ No newline at end of file diff --git a/src/vkvm.h b/src/vkvm.hpp similarity index 63% rename from src/vkvm.h rename to src/vkvm.hpp index 86a0663..e403b29 100644 --- a/src/vkvm.h +++ b/src/vkvm.hpp @@ -1,30 +1,34 @@ -#ifndef LIBRARY_VKVM_H -#define LIBRARY_VKVM_H +#ifndef LIBRARY_VKVM_HPP +#define LIBRARY_VKVM_HPP -#include "Color.h" -#include "EventType.h" -#include "GraphicMode.h" -#include "FontType.h" -#include "KeyCode.h" -#include "LayoutVersion.h" -#include "log.h" +#include "Color.hpp" +#include "EventType.hpp" +#include "GraphicMode.hpp" +#include "FontType.hpp" +#include "KeyCode.hpp" +#include "LayoutVersion.hpp" +#include "log.hpp" #include #include -/** +namespace vkvm { + /** * @since 0.1.0 * @author Johannes Theiner */ -//TODO: better documentation + struct Coordinates { + int x; + int y; + }; /** * initialize the connection with the shared-memory application * @param pid pip of shared-memory application */ -void initialize(int pid); + auto initialize(int pid) -> void; -void setDefaultValues(); + auto setDefaultValues() -> void; /** * set pixel at a x,y position to a certain color. @@ -33,7 +37,7 @@ void setDefaultValues(); * @param color color of pixel * @return true if operation succeeded, false if it failed. */ -bool setPixel(int x, int y, Color color); + auto setPixel(int x, int y, Color color) -> bool; /** * get color of pixel at x,y position @@ -41,7 +45,7 @@ bool setPixel(int x, int y, Color color); * @param y y coordinate of pixel * @return color of pixel */ -Color getPixel(int x, int y); + auto getPixel(int x, int y) -> Color; /** * register handler for event. @@ -49,19 +53,19 @@ 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, const std::function &handler); + auto registerEvent(EventType type, const std::function &handler) -> bool; /** * set displayed text in Text mode * @param text text to display * @return if text could be set, false if it could not be set. */ -bool setText(std::string text); + auto setText(std::string text) -> bool; /** * get currently saved/displayed text. */ -std::string getText(); + auto getText() -> std::string; //Control registers start here @@ -71,71 +75,71 @@ std::string getText(); * get version of the used layout * @return layout version */ -LayoutVersion getLayoutVersion(); + auto getLayoutVersion() -> LayoutVersion; /** * reset all values to default. */ -void reset(); + auto reset() -> void; /** * get width of window. * @return width of window. */ -int getWidth(); + auto getWidth() -> int; /** * set width of window. * @param newValue new width for window. */ -void setWidth(int newValue); + auto setWidth(int newValue) -> void; /** * get height for window. * @return height of window. */ -int getHeight(); + auto getHeight() -> int; /** * set height of window. * @param newValue new height for window. */ -void setHeight(int newValue); + auto setHeight(int newValue) -> void; /** * get graphics display mode. * @return GraphicMode. */ -GraphicMode getMode(); + auto getMode() -> GraphicMode; /** * set new graphics display mode. * @param newValue new graphics display mode. */ -void setMode(GraphicMode newValue); + auto setMode(GraphicMode newValue) -> void; /** * get interval between redraws in milliseconds. */ -int getRedrawInterval(); + auto getRedrawInterval() -> int; /** * set interval between redraws. * @param newValue new interval in milliseconds. */ -void setRedrawInterval(int newValue); + auto setRedrawInterval(int newValue) -> void; /** * get time between timer interrupts. * @return time between interrupts in milliseconds. */ -int getTimerInterruptInterval(); + auto getTimerInterruptInterval() -> int; /** * set time between timer interrupts. * @param newValue new time between interrupts in milliseconds. */ -void setTimerInterruptInterval(int newValue); + auto setTimerInterruptInterval(int newValue) -> void; //black/white mode @@ -143,25 +147,25 @@ void setTimerInterruptInterval(int newValue); * get background color in two color mode. * @return background color. */ -Color getBackgroundColor(); + auto getBackgroundColor() -> Color; /** * set background color in two color mode. * @param newValue new background color. */ -void setBackgroundColor(Color newValue); + auto setBackgroundColor(Color newValue) -> void; /** * get foreground color in two color mode. * @return foreground color. */ -Color getForegroundColor(); + auto getForegroundColor() -> Color; /** * set foreground color in two color mode. * @param newValue new foreground color. */ -void setForegroundColor(Color newValue); + auto setForegroundColor(Color newValue) -> void; //text mode @@ -169,36 +173,37 @@ void setForegroundColor(Color newValue); * get characters per row in text mode. * @return characters per row. */ -int getCharactersPerRow(); + auto getCharactersPerRow() -> int; /** * get characters per column in text mode. * @return characters per column. */ -int getCharactersPerColumn(); + auto getCharactersPerColumn() -> int; /** * get currently used font in text mode. * @return currently used font. */ -FontType getFont(); + auto getFont() -> FontType; /** * set text mode font. * @param newValue new text mode font. */ -void setFont(const FontType &newValue); + auto setFont(const FontType &newValue) -> void; /** * get current mouse position * @return mouse position as x,y pair */ -std::pair getMousePosition(); + auto getMousePosition() -> Coordinates; /** * get key code of last key press. * @return KeyCode of last key press. */ -KeyCode getLastPressedKey(); + auto getLastPressedKey() -> KeyCode; +} #endif \ No newline at end of file diff --git a/test/public_test.cpp b/test/public_test.cpp index 93aaee1..05f9467 100644 --- a/test/public_test.cpp +++ b/test/public_test.cpp @@ -1,10 +1,10 @@ #include "catch2/catch.hpp" -#include "../src/vkvm.h" +#include "../src/vkvm.hpp" TEST_CASE("add works") { - initialize(0); - setText("Hello World"); + vkvm::initialize(0); + vkvm::setText("Hello World"); SECTION("equals") { - REQUIRE(getText() == "Hello World"); + REQUIRE(vkvm::getText() == "Hello World"); } } \ No newline at end of file From 7bc1afd2b59624329bb5beb31951369561bc55ac Mon Sep 17 00:00:00 2001 From: Julian Hinxlage Date: Wed, 13 Nov 2019 13:44:23 +0100 Subject: [PATCH 32/34] ~ fixed copy hpp to include --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ba82a8c..cb3797a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,7 +27,7 @@ add_library(library ${SOURCES} ${HEADERS} src/FontType.cpp) file(COPY "${CMAKE_SOURCE_DIR}/src/" DESTINATION "${CMAKE_SOURCE_DIR}/include" FILES_MATCHING - PATTERN *.h + PATTERN *.hpp ) set(LIBRARY_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/lib) From c1c08ca546271fd6536b26028d807c2c39743db1 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Wed, 13 Nov 2019 15:42:04 +0100 Subject: [PATCH 33/34] ~fixed all clang-tidy erros --- .ci/clang-tidy.sh | 2 +- .clang-tidy | 2 ++ CMakeLists.txt | 2 +- src/FontType.cpp | 8 ++------ src/FontType.hpp | 8 +++----- src/GraphicMode.hpp | 2 +- src/SharedMemoryAccess.cpp | 21 ++++++--------------- src/internal.cpp | 2 -- src/internal.hpp | 17 ++++++++++------- src/log.hpp | 2 +- src/vkvm.cpp | 2 +- src/vkvm.hpp | 2 +- 12 files changed, 29 insertions(+), 41 deletions(-) create mode 100644 .clang-tidy diff --git a/.ci/clang-tidy.sh b/.ci/clang-tidy.sh index 2e1b4e7..e5861a2 100755 --- a/.ci/clang-tidy.sh +++ b/.ci/clang-tidy.sh @@ -9,7 +9,7 @@ filelist="$(find . -not \( -path './*build*' -prune \) -not \( -path './include' for file in $filelist; do if echo "$file" | grep -q -E ".*(\.cpp|\.h|\.hpp)$" ; then #Extra check missing dependencies due to clang-tidy doesn't toggle exit code. - clang_tidy_lib_check="$(clang-tidy -warnings-as-errors='*' -header-filter='.*,-cpptoml.hpp' -checks='*,-llvm-header-guard,-fuchsia-statically-constructed-objects,-fuchsia*,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-pro-bounds-constant-array-index,-bugprone-narrowing-conversions,-cppcoreguidelines-narrowing-conversions,-cppcoreguidelines-pro-type-const-cast,-cppcoreguidelines-pro-type-reinterpret-cast,-hicpp-signed-bitwise,-bugprone-exception-escape,-cppcoreguidelines-pro-type-member-init,-cppcoreguidelines-pro-type-cstyle-cast,-hicpp-member-init,-google-readability-namespace-comments,-llvm-namespace-comment' "$file" -- -I. -std=c++14 2>&1)" + clang_tidy_lib_check="$(clang-tidy -warnings-as-errors='*' --color -header-filter='.*,-cpptoml.hpp' "$file" -- -I. -std=c++14 2>&1)" for tidy_line in $clang_tidy_lib_check; do echo "$tidy_line" | grep -q -v -E "^Error while processing*" if [ $? -eq 1 ]; then diff --git a/.clang-tidy b/.clang-tidy new file mode 100644 index 0000000..ab0e4e1 --- /dev/null +++ b/.clang-tidy @@ -0,0 +1,2 @@ +Checks: '*,-llvm-header-guard,-fuchsia*,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-pro-bounds-constant-array-index,-bugprone-narrowing-conversions,-cppcoreguidelines-narrowing-conversions,-cppcoreguidelines-pro-type-const-cast,-cppcoreguidelines-pro-type-reinterpret-cast,-hicpp-signed-bitwise,-bugprone-exception-escape,-cppcoreguidelines-pro-type-member-init,-cppcoreguidelines-pro-type-cstyle-cast,-hicpp-member-init,-google-readability-namespace-comments,-llvm-namespace-comment,-cppcoreguidelines-pro-type-vararg,-hicpp-vararg' +WarningsAsErrors: 'true' \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index ba82a8c..2b261c8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,7 +13,7 @@ set(CMAKE_CXX_STANDARD 14) # enable clang_tidy set(CMAKE_CXX_CLANG_TIDY "clang-tidy;-checks=*") -set(CMAKE_CXX_CLANG_TIDY clang-tidy;-header-filter=.;-checks=*;) +set(CMAKE_CXX_CLANG_TIDY clang-tidy;-header-filter=.;) file(GLOB_RECURSE SOURCES src/*.cpp) diff --git a/src/FontType.cpp b/src/FontType.cpp index 46ebb00..5e69054 100644 --- a/src/FontType.cpp +++ b/src/FontType.cpp @@ -2,11 +2,7 @@ namespace vkvm { - FontType::FontType(int id, std::string name, int height, int width) noexcept { - this->id = id; - this->name = std::move(name); - this->height = height; - this->width = width; + FontType::FontType(int id, const char * name, int height, int width) noexcept : id(id), name(name), height(height), width(width) { } auto FontType::getId() const -> int { @@ -14,7 +10,7 @@ namespace vkvm { } auto FontType::getName() const -> std::string { - return name; + return std::string(name); } auto FontType::getHeight() const -> int { diff --git a/src/FontType.hpp b/src/FontType.hpp index defecf0..349abd1 100644 --- a/src/FontType.hpp +++ b/src/FontType.hpp @@ -10,12 +10,12 @@ namespace vkvm { class FontType { private: int id; - std::string name; + const char * name; int height; int width; public: - FontType(int id, std::string name, int height, int width) noexcept; + FontType(int id, const char * name, int height, int width) noexcept; auto getId() const -> int; @@ -27,9 +27,7 @@ namespace vkvm { }; - - const static FontType font_1 = FontType(1, "DummyFont", 10, 5); - + static const FontType font_1 = FontType(1, "DummyFont", 10, 5); } #endif diff --git a/src/GraphicMode.hpp b/src/GraphicMode.hpp index c28b205..92d51d7 100644 --- a/src/GraphicMode.hpp +++ b/src/GraphicMode.hpp @@ -12,7 +12,7 @@ namespace vkvm { Text = 1, TwoColors = 2, Gray_256 = 3, - TrueColor = 4, + RGB = 4 }; } diff --git a/src/SharedMemoryAccess.cpp b/src/SharedMemoryAccess.cpp index ff68a4d..697b5b0 100644 --- a/src/SharedMemoryAccess.cpp +++ b/src/SharedMemoryAccess.cpp @@ -37,7 +37,7 @@ namespace vkvm { return -1; } /* Semaphor mit 1 initialisieren */ - if (semctl(semId, 0, SETVAL, 1) == -1){ + if (semctl(semId, 0, SETVAL, 1) == -1) { return -1; } } @@ -61,26 +61,17 @@ namespace vkvm { } auto getSharedMemory() -> char * { - bool useLocal = false; - - if(!useLocal){ - int shmId = shmget(impl.sharedMemoryKey, NULL, 0); - if(shmId < 0) { - //no shared memory found - useLocal = true; - } else { - return (char *) shmat(shmId, NULL, 0); - } - } - - if(useLocal) { + int shmId = shmget(impl.sharedMemoryKey, 0, 0); + if (shmId < 0) { //using a local buffer for shared memory testing if (localSharedMemory.empty()) { initSemaphore(); - localSharedMemory.resize(impl.sharedMemorySize * 1024); + constexpr int kilo = 1024; + localSharedMemory.resize(impl.sharedMemorySize * kilo); } return &localSharedMemory[0]; } + return static_cast(shmat(shmId, nullptr, 0)); } auto getSharedMemory(char *address, int size, int offset) -> void { diff --git a/src/internal.cpp b/src/internal.cpp index 88d167a..82b515a 100644 --- a/src/internal.cpp +++ b/src/internal.cpp @@ -3,8 +3,6 @@ #include "vkvm.hpp" #include -#include - namespace vkvm { diff --git a/src/internal.hpp b/src/internal.hpp index af86223..d237f92 100644 --- a/src/internal.hpp +++ b/src/internal.hpp @@ -1,14 +1,14 @@ #ifndef LIBRARY_INTERNAL_HPP #define LIBRARY_INTERNAL_HPP +#include "Color.hpp" #include "EventType.hpp" +#include "GraphicMode.hpp" #include "KeyCode.hpp" #include "LayoutVersion.hpp" -#include "GraphicMode.hpp" -#include "Color.hpp" +#include #include #include -#include namespace vkvm { @@ -17,6 +17,9 @@ namespace vkvm { * @author Julian Hinxlage * @since 0.1.0 */ + +constexpr int keyboardBufferSize = 16; + struct Registers { int layout_version; int trigger_reset; @@ -34,7 +37,7 @@ namespace vkvm { int textMode_font_height; int mouse_pos_x; int mouse_pos_y; - short keyboardBuffer[16]; + std::array keyboardBuffer; int keyboardBuffer_index_write; int keyboardBuffer_index_read; }; @@ -51,8 +54,8 @@ namespace vkvm { int sharedMemoryPid; key_t sharedMemoryKey; int sharedMemorySize; - int interruptEntyCount = 64; - int reservedSize = 1024; + int interruptEntyCount = 64; //NOLINT + int reservedSize = 1024; //NOLINT std::vector> eventTable; }; @@ -76,7 +79,7 @@ namespace vkvm { auto getRegisters() -> Registers *; - auto getTextArea() -> char * ; + auto getTextArea() -> char *; auto getPixelArea() -> char *; diff --git a/src/log.hpp b/src/log.hpp index 311bef8..8d80990 100644 --- a/src/log.hpp +++ b/src/log.hpp @@ -1,8 +1,8 @@ #ifndef LIBRARY_LOG_HPP #define LIBRARY_LOG_HPP -#include #include +#include namespace vkvm { enum LogLevel{ diff --git a/src/vkvm.cpp b/src/vkvm.cpp index 5b0390b..3493667 100644 --- a/src/vkvm.cpp +++ b/src/vkvm.cpp @@ -25,7 +25,7 @@ namespace vkvm { setMousePosition(42, 42);// NOLINT setBackgroundColor(Color(200, 50, 20));// NOLINT setForegroundColor(Color(20, 200, 50));// NOLINT - setMode(GraphicMode::TrueColor);// NOLINT + setMode(GraphicMode::RGB);// NOLINT setRedrawInterval(20);// NOLINT setTimerInterruptInterval(10);// NOLINT } diff --git a/src/vkvm.hpp b/src/vkvm.hpp index e403b29..8f86e7f 100644 --- a/src/vkvm.hpp +++ b/src/vkvm.hpp @@ -3,8 +3,8 @@ #include "Color.hpp" #include "EventType.hpp" -#include "GraphicMode.hpp" #include "FontType.hpp" +#include "GraphicMode.hpp" #include "KeyCode.hpp" #include "LayoutVersion.hpp" #include "log.hpp" From b9882cb181f782cfe6443e0ecbaa1e60de800044 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Thu, 14 Nov 2019 13:30:32 +0100 Subject: [PATCH 34/34] ~ remove trailing type declaration check --- .clang-tidy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.clang-tidy b/.clang-tidy index ab0e4e1..de644d1 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,2 +1,2 @@ -Checks: '*,-llvm-header-guard,-fuchsia*,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-pro-bounds-constant-array-index,-bugprone-narrowing-conversions,-cppcoreguidelines-narrowing-conversions,-cppcoreguidelines-pro-type-const-cast,-cppcoreguidelines-pro-type-reinterpret-cast,-hicpp-signed-bitwise,-bugprone-exception-escape,-cppcoreguidelines-pro-type-member-init,-cppcoreguidelines-pro-type-cstyle-cast,-hicpp-member-init,-google-readability-namespace-comments,-llvm-namespace-comment,-cppcoreguidelines-pro-type-vararg,-hicpp-vararg' +Checks: '*,-llvm-header-guard,-fuchsia*,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-pro-bounds-constant-array-index,-bugprone-narrowing-conversions,-cppcoreguidelines-narrowing-conversions,-cppcoreguidelines-pro-type-const-cast,-cppcoreguidelines-pro-type-reinterpret-cast,-hicpp-signed-bitwise,-bugprone-exception-escape,-cppcoreguidelines-pro-type-member-init,-cppcoreguidelines-pro-type-cstyle-cast,-hicpp-member-init,-google-readability-namespace-comments,-llvm-namespace-comment,-cppcoreguidelines-pro-type-vararg,-hicpp-vararg,-modernize-use-trailing-return-type' WarningsAsErrors: 'true' \ No newline at end of file