library/src/vkvm.cpp

190 lines
4.1 KiB
C++

#include "vkvm.h"
#include "internal.h"
#include "SharedMemoryAccess.h"
#include <unistd.h>
#include <csignal>
void initialize(int pid) {
impl.sharedMemoryPid = pid;
impl.sharedMemoryKey = 12345;
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<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: 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 font_1;
}
void setFont(FontType newValue) {
//TODO
lockSharedMemory();
getRegisters()->textMode_font = 0;
unlockSharedMemory();
}
std::pair<int, int> getMousePosition() {
return {getRegisters()->mouse_pos_x, getRegisters()->mouse_pos_y};
}
KeyCode getLastPressedKey() {
//TODO
return KeyCode(0);
}