Merge branch 'getter' into dev

# Conflicts:
#	src/Color.cpp
#	src/FontType.h
#	src/internal.cpp
#	src/vkvm.cpp
This commit is contained in:
Julian Hinxlage 2019-11-07 17:35:30 +01:00
commit 5b378366b8
12 changed files with 232 additions and 86 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

@ -5,3 +5,28 @@ Color::Color(unsigned char red, unsigned char green, unsigned char blue)
: red(red), green(green), blue(blue){ : red(red), green(green), blue(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);

View File

@ -1,22 +0,0 @@
#include <string>
#include <utility>
#include "Font.h"
Font::Font(std::string name, int height, int width) {
this->name = std::move(name);
this->height = height;
this->width = width;
}
std::string Font::getName() {
return name;
}
int Font::getHeight() {
return height;
}
int Font::getWidth() {
return width;
}

20
src/FontType.cpp Normal file
View File

@ -0,0 +1,20 @@
#include "FontType.h"
FontType::FontType(std::string name, int height, int width) {
this->name = std::move(name);
this->height = height;
this->width = width;
}
std::string FontType::getName() {
return name;
}
int FontType::getHeight() {
return height;
}
int FontType::getWidth() {
return width;
}

View File

@ -3,14 +3,16 @@
#ifndef LIBRARY_FONT_H #ifndef LIBRARY_FONT_H
#define LIBRARY_FONT_H #define LIBRARY_FONT_H
class Font { #include <string>
class FontType {
private: private:
std::string name; std::string name;
int height; int height;
int width; int width;
public: public:
Font(std::string name, int height, int width); FontType(std::string name, int height, int width);
std::string getName(); std::string getName();
int getHeight(); int getHeight();
int getWidth(); int getWidth();
@ -18,6 +20,6 @@ public:
}; };
const static Font font_1 = Font("DummyFont", 10, 5); const static FontType font_1 = FontType("DummyFont", 10, 5);
#endif #endif

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) {
@ -27,21 +43,30 @@ bool callEvent(EventType type) {
} }
void setLayoutVersion(LayoutVersion newValue) { void setLayoutVersion(LayoutVersion newValue) {
lockSharedMemory();
getRegisters()->layout_version = newValue;
unlockSharedMemory();
} }
void setCharactersPerLine(int newValue) { void setCharactersPerColumn(int newValue) {
lockSharedMemory();
getRegisters()->characters_per_column = newValue;
unlockSharedMemory();
} }
void setCharactersPerRow(int newValue) { void setCharactersPerRow(int newValue) {
lockSharedMemory();
getRegisters()->characters_per_row = newValue;
unlockSharedMemory();
} }
void setMousePosition(int x, int y) { void setMousePosition(int x, int y) {
lockSharedMemory();
getRegisters()->mouse_pos_x = x;
getRegisters()->mouse_pos_y = y;
unlockSharedMemory();
} }
void buttonPressed(KeyCode keyCode) { 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,29 +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;
}
bool setPixel(int x, int y, Color color) { //set default values
return true; setCharactersPerRow(60);
} setCharactersPerColumn(20);
setHeight(600);
Color getPixel(int x, int y) { setWidth(800);
return {255, 0, 0}; 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){
@ -34,101 +41,149 @@ bool registerEvent(EventType type, std::function<void()> handler) {
} }
}); });
unlockSharedMemory();
return true; return true;
} }
bool setText(std::string text) { 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; 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() { std::string getText() {
return "Hallo Welt"; return std::string (getTextArea());
} }
LayoutVersion getLayoutVersion() { LayoutVersion getLayoutVersion() {
return V1; return (LayoutVersion)getRegisters()->layout_version;
} }
void reset() { void reset() {
//TODO
} }
int getWidth() { int getWidth() {
return 360; return getRegisters()->width_pixels;
} }
void setWidth(int newValue) { void setWidth(int newValue) {
lockSharedMemory();
getRegisters()->width_pixels = newValue;
unlockSharedMemory();
} }
int getHeight() { int getHeight() {
return 360; return getRegisters()->height_pixels;
} }
void setHeight(int newValue) { void setHeight(int newValue) {
lockSharedMemory();
getRegisters()->height_pixels = newValue;
unlockSharedMemory();
} }
GraphicMode getMode() { GraphicMode getMode() {
return TrueColor; return getRegisters()->graphicMode;
} }
void setMode(GraphicMode newValue) { void setMode(GraphicMode newValue) {
lockSharedMemory();
getRegisters()->graphicMode = newValue;
unlockSharedMemory();
} }
int getRedrawInterval() { int getRedrawInterval() {
return 10; return getRegisters()->autoRedrawInterval;
} }
void setRedrawInterval(int newValue) { void setRedrawInterval(int newValue) {
lockSharedMemory();
getRegisters()->autoRedrawInterval = newValue;
unlockSharedMemory();
} }
int getTimerInterruptInterval() { int getTimerInterruptInterval() {
return 5; return getRegisters()->timerInterruptInterval;
} }
void setTimerInterruptInterval(int newValue) { void setTimerInterruptInterval(int newValue) {
lockSharedMemory();
getRegisters()->timerInterruptInterval = newValue;
unlockSharedMemory();
} }
Color getBackgroundColor() { Color getBackgroundColor() {
return black; return getRegisters()->background_color;
} }
void setBackgroundColor(Color newValue) { void setBackgroundColor(Color newValue) {
lockSharedMemory();
getRegisters()->background_color = newValue;
unlockSharedMemory();
} }
Color getForegroundColor() { Color getForegroundColor() {
return white; return getRegisters()->foreground_color;
} }
void setForegroundColor(Color newValue) { void setForegroundColor(Color newValue) {
lockSharedMemory();
getRegisters()->foreground_color = newValue;
unlockSharedMemory();
} }
int getCharactersPerRow() { int getCharactersPerRow() {
return 5; return getRegisters()->characters_per_row;
} }
int getCharactersPerColumn() { int getCharactersPerColumn() {
return 5; return getRegisters()->characters_per_column;
} }
Font getFont() { FontType getFont() {
//TODO
return font_1; return font_1;
} }
void setFont(Font newValue) { void setFont(FontType newValue) {
//TODO
lockSharedMemory();
getRegisters()->textMode_font = 0;
unlockSharedMemory();
} }
std::pair<int, int> getMousePosition() { std::pair<int, int> getMousePosition() {
return {10, 50}; return {getRegisters()->mouse_pos_x, getRegisters()->mouse_pos_y};
} }
KeyCode getLastPressedKey() { KeyCode getLastPressedKey() {
return KeyCode(50); //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