From f5c461a05b4b18a4d0e780e081258ae766214112 Mon Sep 17 00:00:00 2001 From: Julian Hinxlage Date: Tue, 12 Nov 2019 14:07:02 +0100 Subject: [PATCH] ~ 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