library/src/vkvm.cpp

196 lines
4.5 KiB
C++

#include "SharedMemoryAccess.h"
#include "internal.h"
#include "vkvm.h"
#include <csignal>
#include <unistd.h>
// 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
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<void()> &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){
if(sig >= SIGUSR1){
if((sig - SIGUSR1) < impl.eventTable.size()){
impl.eventTable[sig - SIGUSR1]();
}
}
});
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<unsigned char *>(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<int>(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 static_cast<LayoutVersion>(getRegisters()->layout_version);
}
void reset() {
//TODO(julian): reset
}
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(julian): get font properly
return font_1;
}
void setFont(const FontType &newValue) {
//TODO(julian): setFont properly
lockSharedMemory();
getRegisters()->textMode_font = newValue.getId();
unlockSharedMemory();
}
std::pair<int, int> getMousePosition() {
return {getRegisters()->mouse_pos_x, getRegisters()->mouse_pos_y};
}
KeyCode getLastPressedKey() {
//TODO(julian): get key properly
return KeyCode(0);
}