library/src/vkvm.cpp

193 lines
4.4 KiB
C++
Raw Normal View History

#include "SharedMemoryAccess.h"
2019-11-12 14:07:02 +01:00
#include "internal.h"
#include "vkvm.h"
2019-11-06 13:41:24 +01:00
#include <csignal>
2019-11-12 14:07:02 +01:00
#include <unistd.h>
2019-11-12 15:03:17 +01:00
// NOLINT
void initialize(int pid) {
2019-11-04 23:17:15 +01:00
impl.sharedMemoryPid = pid;
2019-11-12 14:07:02 +01:00
2019-11-12 15:03:17 +01:00
impl.sharedMemoryKey = 12345;// NOLINT
2019-11-12 14:07:02 +01:00
2019-11-12 15:03:17 +01:00
impl.sharedMemorySize = 8000;// NOLINT
2019-11-07 17:02:00 +01:00
//set default values
2019-11-12 15:03:17 +01:00
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
2019-11-07 17:02:00 +01:00
}
2019-11-12 14:07:02 +01:00
bool registerEvent(EventType type, const std::function<void()> &handler) {
2019-11-06 13:41:24 +01:00
int signum = SIGUSR1 + impl.eventTable.size();
auto ivt = getInterrupTable();
lockSharedMemory();
2019-11-06 13:41:24 +01:00
ivt[type].pid = getpid();
ivt[type].signum = signum;
2019-11-07 12:41:01 +01:00
impl.eventTable.push_back(handler);
2019-11-06 13:41:24 +01:00
onSignal(signum, [](int sig){
if(sig >= SIGUSR1){
if((sig - SIGUSR1) < impl.eventTable.size()){
impl.eventTable[sig - SIGUSR1]();
}
}
});
unlockSharedMemory();
2019-11-06 13:41:24 +01:00
return true;
}
2019-11-07 17:02:00 +01:00
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();
2019-11-07 17:02:00 +01:00
return false;
}
Color getPixel(int x, int y) {
2019-11-12 14:07:02 +01:00
//TODO(julian): other than RGB colores
//only RGB colores for now
2019-11-12 14:07:02 +01:00
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();
2019-11-12 15:03:17 +01:00
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();
2019-11-06 13:41:24 +01:00
return true;
}
2019-11-07 17:02:00 +01:00
std::string getText() {
return std::string (getTextArea());
2019-11-07 17:02:00 +01:00
}
LayoutVersion getLayoutVersion() {
2019-11-12 14:07:02 +01:00
return static_cast<LayoutVersion>(getRegisters()->layout_version);
2019-11-07 17:02:00 +01:00
}
void reset() {
2019-11-12 14:07:02 +01:00
//TODO(julian): reset
2019-11-07 17:02:00 +01:00
}
int getWidth() {
return getRegisters()->width_pixels;
2019-11-07 17:02:00 +01:00
}
void setWidth(int newValue) {
lockSharedMemory();
getRegisters()->width_pixels = newValue;
unlockSharedMemory();
2019-11-07 17:02:00 +01:00
}
int getHeight() {
return getRegisters()->height_pixels;
2019-11-07 17:02:00 +01:00
}
void setHeight(int newValue) {
lockSharedMemory();
getRegisters()->height_pixels = newValue;
unlockSharedMemory();
2019-11-07 17:02:00 +01:00
}
GraphicMode getMode() {
return getRegisters()->graphicMode;
2019-11-07 17:02:00 +01:00
}
void setMode(GraphicMode newValue) {
lockSharedMemory();
getRegisters()->graphicMode = newValue;
unlockSharedMemory();
2019-11-07 17:02:00 +01:00
}
int getRedrawInterval() {
return getRegisters()->autoRedrawInterval;
2019-11-07 17:02:00 +01:00
}
void setRedrawInterval(int newValue) {
lockSharedMemory();
getRegisters()->autoRedrawInterval = newValue;
unlockSharedMemory();
2019-11-07 17:02:00 +01:00
}
int getTimerInterruptInterval() {
return getRegisters()->timerInterruptInterval;
2019-11-07 17:02:00 +01:00
}
void setTimerInterruptInterval(int newValue) {
lockSharedMemory();
getRegisters()->timerInterruptInterval = newValue;
unlockSharedMemory();
2019-11-07 17:02:00 +01:00
}
Color getBackgroundColor() {
return getRegisters()->background_color;
2019-11-07 17:02:00 +01:00
}
void setBackgroundColor(Color newValue) {
lockSharedMemory();
getRegisters()->background_color = newValue;
unlockSharedMemory();
2019-11-07 17:02:00 +01:00
}
Color getForegroundColor() {
return getRegisters()->foreground_color;
2019-11-07 17:02:00 +01:00
}
void setForegroundColor(Color newValue) {
lockSharedMemory();
getRegisters()->foreground_color = newValue;
unlockSharedMemory();
2019-11-07 17:02:00 +01:00
}
int getCharactersPerRow() {
return getRegisters()->characters_per_row;
2019-11-07 17:02:00 +01:00
}
int getCharactersPerColumn() {
return getRegisters()->characters_per_column;
2019-11-07 17:02:00 +01:00
}
FontType getFont() {
2019-11-12 14:07:02 +01:00
//TODO(julian): get font properly
2019-11-07 17:02:00 +01:00
return font_1;
}
2019-11-12 14:07:02 +01:00
void setFont(const FontType &newValue) {
//TODO(julian): setFont properly
lockSharedMemory();
2019-11-12 14:07:02 +01:00
getRegisters()->textMode_font = newValue.getId();
unlockSharedMemory();
2019-11-07 17:02:00 +01:00
}
std::pair<int, int> getMousePosition() {
return {getRegisters()->mouse_pos_x, getRegisters()->mouse_pos_y};
2019-11-07 17:02:00 +01:00
}
KeyCode getLastPressedKey() {
2019-11-12 14:07:02 +01:00
//TODO(julian): get key properly
return KeyCode(0);
2019-11-07 17:02:00 +01:00
}