+ local memory as shared memory

+ getters/setters
This commit is contained in:
Julian Hinxlage 2019-11-07 16:34:15 +01:00
parent e30280241e
commit 5439f89e2c
11 changed files with 314 additions and 27 deletions

View File

@ -22,7 +22,7 @@ file(GLOB_RECURSE TESTS test/*.cpp)
include_directories(src) include_directories(src)
include_directories(test) include_directories(test)
add_library(library ${SOURCES} ${HEADERS}) add_library(library ${SOURCES} ${HEADERS} src/FontType.cpp)
file(COPY "${CMAKE_SOURCE_DIR}/src/" file(COPY "${CMAKE_SOURCE_DIR}/src/"
DESTINATION "${CMAKE_SOURCE_DIR}/include" DESTINATION "${CMAKE_SOURCE_DIR}/include"

View File

@ -6,3 +6,27 @@ Color::Color(unsigned char red, unsigned char green, unsigned char blue)
} }
unsigned char Color::getRed() {
return red;
}
unsigned char Color::getGreen() {
return green;
}
unsigned char Color::getBlue() {
return blue;
}
void Color::setRed(unsigned char value) {
red = value;
}
void Color::setGreen(unsigned char value) {
green = value;
}
void Color::setBlue(unsigned char value) {
blue = value;
}

View File

@ -17,6 +17,14 @@ private:
public: public:
Color(unsigned char red, unsigned char green, unsigned char blue); Color(unsigned char red, unsigned char green, unsigned char blue);
unsigned char getRed();
unsigned char getGreen();
unsigned char getBlue();
void setRed(unsigned char value);
void setGreen(unsigned char value);
void setBlue(unsigned char value);
}; };
const static Color black = Color(0, 0, 0); const static Color black = Color(0, 0, 0);

18
src/FontType.cpp Normal file
View File

@ -0,0 +1,18 @@
#include "FontType.h"
FontType::FontType() {
}
std::string FontType::getName() {
return std::__cxx11::string();
}
int FontType::getHeight() {
return 0;
}
int FontType::getWidth() {
return 0;
}

View File

@ -1,12 +1,12 @@
#ifndef LIBRARY_FONT_H #ifndef LIBRARY_FONT_H
#define LIBRARY_FONT_H #define LIBRARY_FONT_H
#include <string>
class Font { class FontType {
private:
Font();
public: public:
FontType();
std::string getName(); std::string getName();
int getHeight(); int getHeight();
int getWidth(); int getWidth();

View File

@ -24,7 +24,9 @@
int semId; int semId;
struct sembuf semaphore; struct sembuf semaphore;
int initSemaphore (void) { std::vector<char> localSharedMemory;
int initSemaphore () {
/* Testen, ob das Semaphor bereits existiert */ /* Testen, ob das Semaphor bereits existiert */
semId = semget (SEM_KEY, 0, IPC_PRIVATE); semId = semget (SEM_KEY, 0, IPC_PRIVATE);
if (semId < 0) { if (semId < 0) {
@ -44,11 +46,11 @@ int initSemaphore (void) {
} }
int semaphoreOperation (int op) { int semaphoreOperation (int op) {
semaphore.sem_op = op; semaphore.sem_op = (short)op;
semaphore.sem_flg = SEM_UNDO; semaphore.sem_flg = SEM_UNDO;
if( semop (semId, &semaphore, 1) == -1) { if( semop (semId, &semaphore, 1) == -1) {
perror(" semop "); perror(" semop ");
exit (EXIT_FAILURE); return -1;
} }
return 1; return 1;
} }
@ -56,7 +58,7 @@ int semaphoreOperation (int op) {
void writeSharedMemory(char *data, int size, int offset) { void writeSharedMemory(char *data, int size, int offset) {
int shmId = shmget(impl.sharedMemoryKey, NULL, 0); // dont init just get the ID if already existing. int shmId = shmget(impl.sharedMemoryKey, NULL, 0); // dont init just get the ID if already existing.
if(shmId < 0 ) { if(shmId < 0 ) {
exit(EXIT_FAILURE); return;
/* we could return -1, not sure if it will be useful to continue after there is no shm allocated for any reason*/ /* we could return -1, not sure if it will be useful to continue after there is no shm allocated for any reason*/
} else { } else {
char *shm_pointer = (char *) shmat(shmId, NULL, 0); char *shm_pointer = (char *) shmat(shmId, NULL, 0);
@ -66,12 +68,34 @@ void writeSharedMemory(char *data, int size, int offset) {
} }
} }
const char* getSharedMemory() { char* getSharedMemory() {
/*
int shmId = shmget(impl.sharedMemoryKey, NULL, 0); int shmId = shmget(impl.sharedMemoryKey, NULL, 0);
if(shmId < 0) { if(shmId < 0) {
exit(EXIT_FAILURE); return nullptr;
/* we could return -1, not sure if it will be useful to continue after there is no shm allocated for any reason*/ // we could return -1, not sure if it will be useful to continue after there is no shm allocated for any reason
} else { } else {
return (char *) shmat(shmId, NULL, 0); return (char *) shmat(shmId, NULL, 0);
} }
} */
//using a local buffer for shared memory testing
if(localSharedMemory.empty()){
initSemaphore();
localSharedMemory.resize(impl.sharedMemorySize * 1024);
}
return &localSharedMemory[0];
}
int getSharedMemorySize() {
return impl.sharedMemorySize;
}
void lockSharedMemory() {
semaphoreOperation(LOCK);
}
void unlockSharedMemory() {
semaphoreOperation(UNLOCK);
}

View File

@ -7,10 +7,10 @@
/** /**
* only to read the shared memory * use lock and unlock when writing
* @return pointer to the shared memory * @return pointer to the shared memory
*/ */
const char *getSharedMemory(); char *getSharedMemory();
/** /**
* *
@ -18,6 +18,10 @@ const char *getSharedMemory();
*/ */
int getSharedMemorySize(); int getSharedMemorySize();
void lockSharedMemory();
void unlockSharedMemory();
/** /**
* *
* @param data poiter to data * @param data poiter to data

View File

@ -1,9 +1,10 @@
#include "internal.h" #include "internal.h"
#include "SharedMemoryAccess.h"
#include "vkvm.h"
#include <sys/shm.h> #include <sys/shm.h>
#include <csignal> #include <csignal>
#include "SharedMemoryAccess.h"
Impl impl; Impl impl;
void sendSignal(pid_t pid, int signalNumber) { void sendSignal(pid_t pid, int signalNumber) {
@ -15,7 +16,22 @@ void onSignal(int signalNumber, void(*callback)(int)) {
} }
InterruptEntry *getInterrupTable(){ InterruptEntry *getInterrupTable(){
return (InterruptEntry*)((char*)getSharedMemory() + sizeof(Registers) + 1024/*reserved*/); return (InterruptEntry*)(getSharedMemory() + sizeof(Registers) + impl.reservedSize);
}
Registers *getRegisters(){
return (Registers*)getSharedMemory();
}
char *getTextArea(){
return ((getSharedMemory() + sizeof(Registers) + impl.reservedSize) +
sizeof(InterruptEntry) * impl.interruptEntyCount);
}
char *getPixelArea(){
return (((getSharedMemory() + sizeof(Registers) + impl.reservedSize) +
sizeof(InterruptEntry) * impl.interruptEntyCount) +
getCharactersPerRow() * getCharactersPerColumn());
} }
bool callEvent(EventType type) { bool callEvent(EventType type) {
@ -25,3 +41,32 @@ bool callEvent(EventType type) {
} }
return true; return true;
} }
void setLayoutVersion(LayoutVersion newValue) {
lockSharedMemory();
getRegisters()->layout_version = newValue;
unlockSharedMemory();
}
void setCharactersPerColumn(int newValue) {
lockSharedMemory();
getRegisters()->characters_per_column = newValue;
unlockSharedMemory();
}
void setCharactersPerRow(int newValue) {
lockSharedMemory();
getRegisters()->characters_per_row = newValue;
unlockSharedMemory();
}
void setMousePosition(int x, int y) {
lockSharedMemory();
getRegisters()->mouse_pos_x = x;
getRegisters()->mouse_pos_y = y;
unlockSharedMemory();
}
void buttonPressed(KeyCode keyCode) {
//TODO
}

View File

@ -50,6 +50,7 @@ struct Impl {
key_t sharedMemoryKey; key_t sharedMemoryKey;
int sharedMemorySize; int sharedMemorySize;
int interruptEntyCount = 64; int interruptEntyCount = 64;
int reservedSize = 1024;
std::vector<std::function<void()>> eventTable; std::vector<std::function<void()>> eventTable;
}; };
@ -70,6 +71,10 @@ void onSignal(int signalNumber, void(*callback)(int));
InterruptEntry *getInterrupTable(); InterruptEntry *getInterrupTable();
Registers *getRegisters();
char *getTextArea();
char *getPixelArea();
/** /**
* set layout version. * set layout version.
@ -78,10 +83,10 @@ InterruptEntry *getInterrupTable();
void setLayoutVersion(LayoutVersion newValue); void setLayoutVersion(LayoutVersion newValue);
/** /**
* set characters per line for current font. * set characters per column for current font.
* @param newValue characters per line. * @param newValue characters per column.
*/ */
void setCharactersPerLine(int newValue); void setCharactersPerColumn(int newValue);
/** /**
* set characters per row for current font. * set characters per row for current font.

View File

@ -1,21 +1,36 @@
#include "vkvm.h" #include "vkvm.h"
#include "internal.h" #include "internal.h"
#include "SharedMemoryAccess.h"
#include <unistd.h> #include <unistd.h>
#include <csignal> #include <csignal>
void initialize(int pid) { void initialize(int pid) {
impl.sharedMemoryPid = pid; impl.sharedMemoryPid = pid;
impl.sharedMemoryKey = 12345; impl.sharedMemoryKey = 12345;
impl.sharedMemorySize = 0; 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) { bool registerEvent(EventType type, std::function<void()> handler) {
int signum = SIGUSR1 + impl.eventTable.size(); int signum = SIGUSR1 + impl.eventTable.size();
auto ivt = getInterrupTable(); auto ivt = getInterrupTable();
lockSharedMemory();
ivt[type].pid = getpid(); ivt[type].pid = getpid();
ivt[type].signum = signum; ivt[type].signum = signum;
impl.eventTable.push_back(handler); impl.eventTable.push_back(handler);
onSignal(signum, [](int sig){ onSignal(signum, [](int sig){
@ -26,5 +41,149 @@ bool registerEvent(EventType type, std::function<void()> handler) {
} }
}); });
unlockSharedMemory();
return true; 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 FontType();
}
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);
}

View File

@ -6,7 +6,7 @@
#include "Color.h" #include "Color.h"
#include "EventType.h" #include "EventType.h"
#include "GraphicMode.h" #include "GraphicMode.h"
#include "Font.h" #include "FontType.h"
#include "KeyCode.h" #include "KeyCode.h"
#include "LayoutVersion.h" #include "LayoutVersion.h"
@ -178,13 +178,13 @@ int getCharactersPerColumn();
* get currently used font in text mode. * get currently used font in text mode.
* @return currently used font. * @return currently used font.
*/ */
Font getFont(); FontType getFont();
/** /**
* set text mode font. * set text mode font.
* @param newValue new text mode font. * @param newValue new text mode font.
*/ */
void setFont(Font newValue); void setFont(FontType newValue);
/** /**
* get current mouse position * get current mouse position