shared-memory/src/SharedMemory.cpp

110 lines
4.1 KiB
C++

#include "SharedMemory.h"
#include <cpptoml.h>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <vkvm.hpp>
key_t changedKey() {
std::ofstream keyFile;
keyFile.open("/tmp/vkvmKey.log", std::ofstream::out | std::ofstream::trunc);
keyFile.clear();
int newKey;
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;
keyFile << newKey;
keyFile.close();
return key_t(newKey);
}
std::string getAnswerFromUser() {
std::cout << "answer# ";
std::string answer;
std::cin >> answer;
if (answer == "y" || answer == "n" || answer == "override") {
return answer;
}
std::cout << "wrong input: pls answer with (y) for yes, (n) for no, or (override)\n";
getAnswerFromUser();
return "-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";
std::string answer = getAnswerFromUser();
if (answer == "y") {
vkvm::impl.sharedMemoryKey = changedKey();
std::cout << "new Try with Key: " << vkvm::impl.sharedMemoryKey << std::endl;
initSharedMemory();
} else if (answer == "n") {
std::cout << "This will end shared memory" << std::endl;
exit(0);
} else if (answer == "override") {
deleteSharedMemory();
initSharedMemory();
}
} else {
std::cout << "Shared Memory is (Re-) allocated with Key: " << vkvm::impl.sharedMemoryKey << std::endl;
}
} else {
std::cerr << "key -> " << vkvm::impl.sharedMemoryKey << " is not allowed here " << std::endl;
vkvm::impl.sharedMemoryKey = changedKey();
initSharedMemory();
}
}
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() {
auto config = cpptoml::parse_file(configFile);
int layoutVersion = config->get_as<int>("layoutVersion").value_or(0);
int width = config->get_as<int>("width").value_or(0);
int height = config->get_as<int>("height").value_or(0);
int mode = config->get_as<int>("mode").value_or(0);
int timerInterruptInterval = config->get_as<int>("timerInterruptInterval").value_or(0);
int backgroundColor = config->get_as<int>("backgroundColor").value_or(0);
int foregroundColor = config->get_as<int>("foregroundColor").value_or(0);
int charactersPerRow = config->get_as<int>("charactersPerRow").value_or(0);
int charactersPerColumn = config->get_as<int>("charactersPerColumn").value_or(0);
int font = config->get_as<int>("font").value_or(0);
int mousePositionX = config->get_as<int>("mousePositionX").value_or(0);
int mousePositionY = config->get_as<int>("mousePositionY").value_or(0);
/*
//convert to hex
std::string result;
std::stringstream ss;
ss << std::hex << result;
ss >> result;
*/
vkvm::setLayoutVersion((vkvm::LayoutVersion) layoutVersion);
vkvm::setWidth(width);
vkvm::setHeight(height);
vkvm::setMode((vkvm::GraphicMode) mode);
vkvm::setTimerInterruptInterval(timerInterruptInterval);
vkvm::setBackgroundColor(vkvm::Color(backgroundColor));
vkvm::setForegroundColor(vkvm::Color(foregroundColor));
vkvm::setCharactersPerRow(charactersPerRow);
vkvm::setCharactersPerColumn(charactersPerColumn);
vkvm::setFont(vkvm::FontType(font, "", 0, 0));
vkvm::setMousePosition(mousePositionX, mousePositionY);
}