2019-11-07 16:34:15 +01:00
|
|
|
#include "SharedMemoryAccess.h"
|
2019-11-12 14:07:02 +01:00
|
|
|
#include "internal.h"
|
|
|
|
#include "vkvm.h"
|
2019-11-07 16:34:15 +01:00
|
|
|
|
2019-11-06 13:41:24 +01:00
|
|
|
#include <csignal>
|
2019-11-12 14:07:02 +01:00
|
|
|
#include <unistd.h>
|
2019-10-29 15:24:57 +01:00
|
|
|
|
|
|
|
void initialize(int pid) {
|
2019-11-04 23:17:15 +01:00
|
|
|
impl.sharedMemoryPid = pid;
|
2019-11-12 14:07:02 +01:00
|
|
|
|
|
|
|
const int sharedMemoryKey = 12345;
|
|
|
|
impl.sharedMemoryKey = sharedMemoryKey;
|
|
|
|
|
|
|
|
const int sharedMemorySize = 8000;
|
|
|
|
impl.sharedMemorySize = sharedMemorySize;
|
2019-11-07 17:02:00 +01:00
|
|
|
|
2019-11-07 16:34:15 +01:00
|
|
|
//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);
|
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();
|
2019-11-07 16:34:15 +01:00
|
|
|
|
|
|
|
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]();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-11-07 16:34:15 +01:00
|
|
|
unlockSharedMemory();
|
|
|
|
|
2019-11-06 13:41:24 +01:00
|
|
|
return true;
|
|
|
|
}
|
2019-11-07 17:02:00 +01:00
|
|
|
|
2019-11-07 16:34:15 +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;
|
|
|
|
}
|
|
|
|
|
2019-11-07 16:34:15 +01:00
|
|
|
Color getPixel(int x, int y) {
|
2019-11-12 14:07:02 +01:00
|
|
|
//TODO(julian): other than RGB colores
|
2019-11-07 16:34:15 +01:00
|
|
|
//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]};
|
2019-11-07 16:34:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2019-11-06 13:41:24 +01:00
|
|
|
return true;
|
|
|
|
}
|
2019-11-07 16:34:15 +01:00
|
|
|
|
2019-11-07 17:02:00 +01:00
|
|
|
std::string getText() {
|
2019-11-07 16:34:15 +01:00
|
|
|
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() {
|
2019-11-07 16:34:15 +01:00
|
|
|
return getRegisters()->width_pixels;
|
2019-11-07 17:02:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void setWidth(int newValue) {
|
2019-11-07 16:34:15 +01:00
|
|
|
lockSharedMemory();
|
|
|
|
getRegisters()->width_pixels = newValue;
|
|
|
|
unlockSharedMemory();
|
2019-11-07 17:02:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int getHeight() {
|
2019-11-07 16:34:15 +01:00
|
|
|
return getRegisters()->height_pixels;
|
2019-11-07 17:02:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void setHeight(int newValue) {
|
2019-11-07 16:34:15 +01:00
|
|
|
lockSharedMemory();
|
|
|
|
getRegisters()->height_pixels = newValue;
|
|
|
|
unlockSharedMemory();
|
2019-11-07 17:02:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
GraphicMode getMode() {
|
2019-11-07 16:34:15 +01:00
|
|
|
return getRegisters()->graphicMode;
|
2019-11-07 17:02:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void setMode(GraphicMode newValue) {
|
2019-11-07 16:34:15 +01:00
|
|
|
lockSharedMemory();
|
|
|
|
getRegisters()->graphicMode = newValue;
|
|
|
|
unlockSharedMemory();
|
2019-11-07 17:02:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int getRedrawInterval() {
|
2019-11-07 16:34:15 +01:00
|
|
|
return getRegisters()->autoRedrawInterval;
|
2019-11-07 17:02:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void setRedrawInterval(int newValue) {
|
2019-11-07 16:34:15 +01:00
|
|
|
lockSharedMemory();
|
|
|
|
getRegisters()->autoRedrawInterval = newValue;
|
|
|
|
unlockSharedMemory();
|
2019-11-07 17:02:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int getTimerInterruptInterval() {
|
2019-11-07 16:34:15 +01:00
|
|
|
return getRegisters()->timerInterruptInterval;
|
2019-11-07 17:02:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void setTimerInterruptInterval(int newValue) {
|
2019-11-07 16:34:15 +01:00
|
|
|
lockSharedMemory();
|
|
|
|
getRegisters()->timerInterruptInterval = newValue;
|
|
|
|
unlockSharedMemory();
|
2019-11-07 17:02:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Color getBackgroundColor() {
|
2019-11-07 16:34:15 +01:00
|
|
|
return getRegisters()->background_color;
|
2019-11-07 17:02:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void setBackgroundColor(Color newValue) {
|
2019-11-07 16:34:15 +01:00
|
|
|
lockSharedMemory();
|
|
|
|
getRegisters()->background_color = newValue;
|
|
|
|
unlockSharedMemory();
|
2019-11-07 17:02:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Color getForegroundColor() {
|
2019-11-07 16:34:15 +01:00
|
|
|
return getRegisters()->foreground_color;
|
2019-11-07 17:02:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void setForegroundColor(Color newValue) {
|
2019-11-07 16:34:15 +01:00
|
|
|
lockSharedMemory();
|
|
|
|
getRegisters()->foreground_color = newValue;
|
|
|
|
unlockSharedMemory();
|
2019-11-07 17:02:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int getCharactersPerRow() {
|
2019-11-07 16:34:15 +01:00
|
|
|
return getRegisters()->characters_per_row;
|
2019-11-07 17:02:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int getCharactersPerColumn() {
|
2019-11-07 16:34:15 +01:00
|
|
|
return getRegisters()->characters_per_column;
|
2019-11-07 17:02:00 +01:00
|
|
|
}
|
|
|
|
|
2019-11-07 16:34:15 +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
|
2019-11-07 16:34:15 +01:00
|
|
|
lockSharedMemory();
|
2019-11-12 14:07:02 +01:00
|
|
|
getRegisters()->textMode_font = newValue.getId();
|
2019-11-07 16:34:15 +01:00
|
|
|
unlockSharedMemory();
|
2019-11-07 17:02:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<int, int> getMousePosition() {
|
2019-11-07 16:34:15 +01:00
|
|
|
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
|
2019-11-07 16:34:15 +01:00
|
|
|
return KeyCode(0);
|
2019-11-07 17:02:00 +01:00
|
|
|
}
|