diff --git a/main/main.cpp b/main/main.cpp index 6b53efe..141faa2 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -1,20 +1,14 @@ +#include "../src/SharedMemory.h" +#include "vkvm.hpp" #include #include #include -#include -#include "../src/SharedMemory.h" -#include "vkvm.hpp" int main() { vkvm::initialize(0); initSharedMemory(); - struct sigaction sigIntHandler; - sigIntHandler.sa_handler = deleteSharedMemory; - sigemptyset(&sigIntHandler.sa_mask); - sigIntHandler.sa_flags = 0; - sigaction(SIGINT, &sigIntHandler, nullptr); vkvm::setDefaultValues(); std::atexit(deleteSharedMemory); std::string eingabe; diff --git a/src/SharedMemory.cpp b/src/SharedMemory.cpp index 374d980..5da689f 100644 --- a/src/SharedMemory.cpp +++ b/src/SharedMemory.cpp @@ -1,16 +1,11 @@ -#include #include "SharedMemory.h" -#include -#include #include +#include +#include +#include #include - - - - -key_t changedKey() -{ +key_t changedKey() { std::ofstream keyFile; keyFile.open("/tmp/vkvmKey.log", std::ofstream::out | std::ofstream::trunc); keyFile.clear(); @@ -18,44 +13,40 @@ key_t changedKey() std::cout << "Type in a new Key for the Shared Memory Segment" << std::endl; std::cout << "new Key#"; std::cin >> newKey; - std::cout <<"New Key is -> " << newKey << std::endl; + std::cout << "New Key is -> " << newKey << std::endl; keyFile << newKey; keyFile.close(); return key_t(newKey); } -std::string getAnswerFromUser() -{ - std::cout <<"answer# "; +std::string getAnswerFromUser() { + std::cout << "answer# "; std::string answer; std::cin >> answer; - if(answer.compare("y") == 0 || answer.compare("n") == 0|| answer.compare("override") == 0) { + if (answer == "y" || answer == "n" || answer == "override") { return answer; - } else { - std::cout << "wrong input: pls answer with (y) for yes, (n) for no, or (override)\n"; - getAnswerFromUser(); } + std::cout << "wrong input: pls answer with (y) for yes, (n) for no, or (override)\n"; + getAnswerFromUser(); return "-0"; } -void initSharedMemory(void) { - if(vkvm::impl.sharedMemoryKey != key_t(0)) { - if (shmget(vkvm::impl.sharedMemoryKey, Max_Memory_Size, IPC_CREAT | IPC_EXCL | 0666) < 0) - { +void initSharedMemory() { + if (vkvm::impl.sharedMemoryKey != key_t(0)) { + if (shmget(vkvm::impl.sharedMemoryKey, Max_Memory_Size, IPC_CREAT | IPC_EXCL | 0666) < 0) { std::cout << "Shared Memory with Key: " << vkvm::impl.sharedMemoryKey << " is already in use\n" - <<"You can change the Key( type -> y or n ), or you can override the shared memory Segment(type -> override)\n"; + << "You can change the Key( type -> y or n ), or you can override the shared memory Segment(type -> override)\n"; std::string answer = getAnswerFromUser(); - if(answer.compare("y") == 0) - { + if (answer == "y") { vkvm::impl.sharedMemoryKey = changedKey(); std::cout << "new Try with Key: " << vkvm::impl.sharedMemoryKey << std::endl; initSharedMemory(); - } else if(answer.compare("n") == 0) { - std::cout << "This will end shared memory" < " << vkvm::impl.sharedMemoryKey << " is not allowed here " << std::endl; + std::cerr << "key -> " << vkvm::impl.sharedMemoryKey << " is not allowed here " << std::endl; vkvm::impl.sharedMemoryKey = changedKey(); initSharedMemory(); } } -void deleteSharedMemory(int s) -{ - std::cerr << "programm ended with Code: " << s << std::endl; - if (shmctl(shmget(vkvm::impl.sharedMemoryKey, 0, 0), IPC_RMID, 0) < 0 ) - { - std::cerr << "Failed to remove shared Memory, maybe not existing: try cmd: ipcs, then delete with ipcrm -m $memID " << std::endl; - } else { - std::cout << "shared Memory deleted" << std::endl; - } -} - -void deleteSharedMemory(void) -{ - if (shmctl(shmget(vkvm::impl.sharedMemoryKey, 0, 0), IPC_RMID, 0) < 0 ) - { +void deleteSharedMemory() { + int id = shmctl(shmget(vkvm::impl.sharedMemoryKey, 0, 0), IPC_RMID, 0); + if (id < 0) { std::cerr << "Failed to remove shared Memory, maybe not existing: try cmd: ipcs " << std::endl; } else { + int x =0; std::cout << "shared Memory deleted" << std::endl; } } -void getConfig(){ +void getConfig() { auto config = cpptoml::parse_file(configFile); int layoutVersion = config->get_as("layoutVersion").value_or(0); @@ -123,7 +103,7 @@ void getConfig(){ vkvm::setForegroundColor(vkvm::Color(foregroundColor)); vkvm::setCharactersPerRow(charactersPerRow); vkvm::setCharactersPerColumn(charactersPerColumn); - vkvm::setFont(vkvm::FontType(font,"",0,0)); + vkvm::setFont(vkvm::FontType(font, "", 0, 0)); vkvm::setMousePosition(mousePositionX, mousePositionY); } diff --git a/src/SharedMemory.h b/src/SharedMemory.h index fb91dad..c15fef4 100644 --- a/src/SharedMemory.h +++ b/src/SharedMemory.h @@ -1,16 +1,16 @@ #ifndef SHARED_MEMORY_H #define SHARED_MEMORY_H + +#include "internal.hpp" //NOLINT #include -#include -#include "internal.hpp" //ID-Speicherbereich //Größe des Speicherbereichs hier 8MB (8000) //Shared-Memory-Segment erstellen oder öffnen – shmget() -#define Max_Memory_Size 8000 * 512 +constexpr int Max_Memory_Size (8000 * 512); void deleteSharedMemory(int s); -void deleteSharedMemory(void); -void initSharedMemory(void); +void deleteSharedMemory(); +void initSharedMemory(); void getConfig(); key_t changedKey(); std::string getAnswerFromUser(); diff --git a/test/test_demo.cpp b/test/test_demo.cpp index b86e002..25b878f 100644 --- a/test/test_demo.cpp +++ b/test/test_demo.cpp @@ -1,5 +1,4 @@ #include "../src/SharedMemory.h" -#include TEST_CASE("Demo test") { REQUIRE(42 == 42); diff --git a/test/test_main.cpp b/test/test_main.cpp index 6372e38..7096005 100644 --- a/test/test_main.cpp +++ b/test/test_main.cpp @@ -1,6 +1,4 @@ #define CATCH_CONFIG_MAIN -#include - //Dont touch this file. // add your own tests in this directory according to https://github.com/catchorg/Catch2/blob/master/docs/tutorial.md