+ everything namespaced

~ fixed clang-tidy errors
This commit is contained in:
Johannes Theiner 2019-11-13 13:37:49 +01:00
parent 5aa0a7862f
commit c8569eb3b4
28 changed files with 847 additions and 789 deletions

View File

@ -5,11 +5,11 @@ bool=false
# explicitly set IFS to contain only a line feed # explicitly set IFS to contain only a line feed
IFS=' IFS='
' '
filelist="$(find . -not \( -path './*build*' -prune \) -type f ! -name "$(printf "*\n*")")" filelist="$(find . -not \( -path './*build*' -prune \) -not \( -path './include' -prune \) -not \( -path './lib' -prune \) -type f ! -name "$(printf "*\n*")")"
for file in $filelist; do for file in $filelist; do
if echo "$file" | grep -q -E ".*(\.cpp|\.h|\.hpp)$" ; then if echo "$file" | grep -q -E ".*(\.cpp|\.h|\.hpp)$" ; then
#Extra check missing dependencies due to clang-tidy doesn't toggle exit code. #Extra check missing dependencies due to clang-tidy doesn't toggle exit code.
clang_tidy_lib_check="$(clang-tidy -warnings-as-errors='*' -header-filter='.*,-cpptoml.hpp' -checks='*,-llvm-header-guard,-fuchsia-statically-constructed-objects,-fuchsia-default-arguments,-fuchsia-default-arguments-calls,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-pro-bounds-constant-array-index,-bugprone-narrowing-conversions,-cppcoreguidelines-narrowing-conversions,-cppcoreguidelines-pro-type-const-cast,-cppcoreguidelines-pro-type-reinterpret-cast,-hicpp-signed-bitwise,-bugprone-exception-escape,-cppcoreguidelines-pro-type-member-init,-cppcoreguidelines-pro-type-cstyle-cast,-hicpp-member-init' "$file" -- -I. -std=c++14 2>&1)" clang_tidy_lib_check="$(clang-tidy -warnings-as-errors='*' -header-filter='.*,-cpptoml.hpp' -checks='*,-llvm-header-guard,-fuchsia-statically-constructed-objects,-fuchsia*,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-pro-bounds-constant-array-index,-bugprone-narrowing-conversions,-cppcoreguidelines-narrowing-conversions,-cppcoreguidelines-pro-type-const-cast,-cppcoreguidelines-pro-type-reinterpret-cast,-hicpp-signed-bitwise,-bugprone-exception-escape,-cppcoreguidelines-pro-type-member-init,-cppcoreguidelines-pro-type-cstyle-cast,-hicpp-member-init,-google-readability-namespace-comments,-llvm-namespace-comment' "$file" -- -I. -std=c++14 2>&1)"
for tidy_line in $clang_tidy_lib_check; do for tidy_line in $clang_tidy_lib_check; do
echo "$tidy_line" | grep -q -v -E "^Error while processing*" echo "$tidy_line" | grep -q -v -E "^Error while processing*"
if [ $? -eq 1 ]; then if [ $? -eq 1 ]; then

View File

@ -17,7 +17,7 @@ set(CMAKE_CXX_CLANG_TIDY clang-tidy;-header-filter=.;-checks=*;)
file(GLOB_RECURSE SOURCES src/*.cpp) file(GLOB_RECURSE SOURCES src/*.cpp)
file(GLOB_RECURSE HEADERS src/*.h) file(GLOB_RECURSE HEADERS src/*.hpp)
file(GLOB_RECURSE TESTS test/*.cpp) file(GLOB_RECURSE TESTS test/*.cpp)
include_directories(src) include_directories(src)
include_directories(test) include_directories(test)

View File

@ -1,32 +1,33 @@
#include "Color.hpp"
#include "Color.h" namespace vkvm {
Color::Color(unsigned char red, unsigned char green, unsigned char blue) noexcept Color::Color(unsigned char red, unsigned char green, unsigned char blue) noexcept
: red(red), green(green), blue(blue){ : red(red), green(green), blue(blue) {
} }
unsigned char Color::getRed() { auto Color::getRed() -> unsigned char {
return red; return red;
} }
unsigned char Color::getGreen() { auto Color::getGreen() -> unsigned char {
return green; return green;
} }
unsigned char Color::getBlue() { auto Color::getBlue() -> unsigned char {
return blue; return blue;
} }
void Color::setRed(unsigned char value) { auto Color::setRed(unsigned char value) -> void {
red = value; red = value;
} }
void Color::setGreen(unsigned char value) { auto Color::setGreen(unsigned char value) -> void {
green = value; green = value;
} }
void Color::setBlue(unsigned char value) { auto Color::setBlue(unsigned char value) -> void {
blue = value; blue = value;
}
} }

View File

@ -1,33 +0,0 @@
#ifndef LIBRARY_COLOR_H
#define LIBRARY_COLOR_H
/**
* color values represented as rgb values.
* @author Johannes Theiner
* @since 0.1.0
*/
class Color {
private:
unsigned char red;
unsigned char green;
unsigned char blue;
public:
Color(unsigned char red, unsigned char green, unsigned char blue) noexcept;
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 white = Color(255, 255, 255);
#endif

40
src/Color.hpp Normal file
View File

@ -0,0 +1,40 @@
#ifndef LIBRARY_COLOR_HPP
#define LIBRARY_COLOR_HPP
namespace vkvm {
/**
* color values represented as rgb values.
* @author Johannes Theiner
* @since 0.1.0
*/
class Color {
private:
unsigned char red;
unsigned char green;
unsigned char blue;
public:
Color(unsigned char red, unsigned char green, unsigned char blue) noexcept;
auto getRed() -> unsigned char;
auto getGreen() -> unsigned char;
auto getBlue() -> unsigned char;
auto setRed(unsigned char value) -> void;
auto setGreen(unsigned char value) -> void;
auto setBlue(unsigned char value) -> void;
};
const static Color black = Color(0, 0, 0);
const static Color white = Color(255, 255, 255);
}
#endif

View File

@ -1,20 +0,0 @@
#ifndef LIBRARY_EVENTTYPE_H
#define LIBRARY_EVENTTYPE_H
/**
* types of events.
* @since 0.1.0
* @uthor Johannes Theiner
*/
enum EventType {
Timer = 1,
MouseMove = 2,
MouseButton = 3,
KeyDown = 4,
KeyUp = 5,
UpdateControlRegisters = 6,
Redraw = 7,
RenderText = 8
};
#endif

24
src/EventType.hpp Normal file
View File

@ -0,0 +1,24 @@
#ifndef LIBRARY_EVENTTYPE_HPP
#define LIBRARY_EVENTTYPE_HPP
namespace vkvm {
/**
* types of events.
* @since 0.1.0
* @uthor Johannes Theiner
*/
enum EventType {
Timer = 1,
MouseMove = 2,
MouseButton = 3,
KeyDown = 4,
KeyUp = 5,
UpdateControlRegisters = 6,
Redraw = 7,
RenderText = 8
};
}
#endif

View File

@ -1,25 +1,28 @@
#include "FontType.h" #include "FontType.hpp"
namespace vkvm {
FontType::FontType(int id, std::string name, int height, int width) noexcept { FontType::FontType(int id, std::string name, int height, int width) noexcept {
this->id = id; this->id = id;
this->name = std::move(name); this->name = std::move(name);
this->height = height; this->height = height;
this->width = width; this->width = width;
} }
int FontType::getId() const{ auto FontType::getId() const -> int {
return id; return id;
} }
std::string FontType::getName() const{ auto FontType::getName() const -> std::string {
return name; return name;
} }
int FontType::getHeight() const{ auto FontType::getHeight() const -> int {
return height; return height;
} }
int FontType::getWidth() const{ auto FontType::getWidth() const -> int {
return width; return width;
}
} }

View File

@ -1,27 +0,0 @@
#ifndef LIBRARY_FONT_H
#define LIBRARY_FONT_H
#include <string>
#include <utility>
class FontType {
private:
int id;
std::string name;
int height;
int width;
public:
FontType(int id, std::string name, int height, int width) noexcept;
int getId() const;
std::string getName() const;
int getHeight() const;
int getWidth() const;
};
const static FontType font_1 = FontType(1, "DummyFont", 10, 5);
#endif

35
src/FontType.hpp Normal file
View File

@ -0,0 +1,35 @@
#ifndef LIBRARY_FONT_H
#define LIBRARY_FONT_H
#include <string>
#include <utility>
namespace vkvm {
class FontType {
private:
int id;
std::string name;
int height;
int width;
public:
FontType(int id, std::string name, int height, int width) noexcept;
auto getId() const -> int;
auto getName() const -> std::string;
auto getHeight() const -> int;
auto getWidth() const -> int;
};
const static FontType font_1 = FontType(1, "DummyFont", 10, 5);
}
#endif

View File

@ -1,16 +0,0 @@
#ifndef LIBRARY_GRAPHICMODE_H
#define LIBRARY_GRAPHICMODE_H
/**
* all possible graphics modes.
* @author Johannes Theiner
* @since 0.1.0
*/
enum GraphicMode {
Text = 1,
TwoColors = 2,
Gray_256 = 3,
TrueColor = 4,
};
#endif

19
src/GraphicMode.hpp Normal file
View File

@ -0,0 +1,19 @@
#ifndef LIBRARY_GRAPHICMODE_HPP
#define LIBRARY_GRAPHICMODE_HPP
namespace vkvm {
/**
* all possible graphics modes.
* @author Johannes Theiner
* @since 0.1.0
*/
enum GraphicMode {
Text = 1,
TwoColors = 2,
Gray_256 = 3,
TrueColor = 4,
};
}
#endif

View File

@ -1,9 +1,11 @@
#include "KeyCode.hpp"
#include "KeyCode.h" namespace vkvm {
KeyCode::KeyCode(int value) noexcept : value(value) {} KeyCode::KeyCode(int value) noexcept : value(value) {}
int KeyCode::getValue() { auto KeyCode::getValue() -> int {
return value; return value;
} }
}

View File

@ -1,17 +0,0 @@
#ifndef LIBRARY_KEYCODE_H
#define LIBRARY_KEYCODE_H
#include <sys/types.h>
class KeyCode {
private:
int value;
public:
explicit KeyCode(int value) noexcept;
int getValue();
};
const static KeyCode Backspace = KeyCode(8);
const static KeyCode tabulator = KeyCode(9);
#endif

22
src/KeyCode.hpp Normal file
View File

@ -0,0 +1,22 @@
#ifndef LIBRARY_KEYCODE_HPP
#define LIBRARY_KEYCODE_HPP
#include <sys/types.h>
namespace vkvm {
class KeyCode {
private:
int value;
public:
explicit KeyCode(int value) noexcept;
auto getValue() -> int;
};
const static KeyCode Backspace = KeyCode(8);
const static KeyCode tabulator = KeyCode(9);
}
#endif

View File

@ -1,8 +0,0 @@
#ifndef LIBRARY_LAYOUTVERSION_H
#define LIBRARY_LAYOUTVERSION_H
enum LayoutVersion {
V1 = 1
};
#endif

12
src/LayoutVersion.hpp Normal file
View File

@ -0,0 +1,12 @@
#ifndef LIBRARY_LAYOUTVERSION_HPP
#define LIBRARY_LAYOUTVERSION_HPP
namespace vkvm {
enum LayoutVersion {
V1 = 1
};
}
#endif

View File

@ -1,6 +1,3 @@
//
// Created by Cigerxwin Chaker on 05.11.19.
//
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
@ -12,21 +9,22 @@
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
#include "SharedMemoryAccess.h" /* header is important for the shmID. name could be different. maybe not needed cause: (shmget(memory_access_key, NULL, 0)) */ #include "SharedMemoryAccess.hpp" /* header is important for the shmID. name could be different. maybe not needed cause: (shmget(memory_access_key, NULL, 0)) */
#include "internal.h" #include "internal.hpp"
constexpr int PERM = 0666; /* access rights */ namespace vkvm {
constexpr int LOCK = -1; constexpr int PERM = 0666; /* access rights */
constexpr int UNLOCK = 1; constexpr int LOCK = -1;
constexpr int SEM_KEY = 123458L; constexpr int UNLOCK = 1;
constexpr int SEM_KEY = 123458L;
//int memoryAccessKey; /* var type is int. but could be another type. */ //TODO: look after type in sharedmemory group //int memoryAccessKey; /* var type is int. but could be another type. */ //TODO: look after type in sharedmemory group
int semId; int semId;
struct sembuf semaphore; struct sembuf semaphore;
std::vector<char> localSharedMemory; std::vector<char> localSharedMemory;
int initSemaphore() { auto initSemaphore() -> int {
/* 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,9 +42,9 @@ int initSemaphore() {
} }
} }
return 1; return 1;
} }
int semaphoreOperation(int op) { auto semaphoreOperation(int op) -> int {
semaphore.sem_op = static_cast<int16_t>(op); semaphore.sem_op = static_cast<int16_t>(op);
semaphore.sem_flg = SEM_UNDO; semaphore.sem_flg = SEM_UNDO;
if (semop(semId, &semaphore, 1) == -1) { if (semop(semId, &semaphore, 1) == -1) {
@ -54,15 +52,15 @@ int semaphoreOperation(int op) {
return -1; return -1;
} }
return 1; return 1;
} }
void writeSharedMemory(char *data, int size, int offset) { auto writeSharedMemory(char *data, int size, int offset) -> void {
lockSharedMemory(); lockSharedMemory();
memcpy(getSharedMemory() + offset, data, size); memcpy(getSharedMemory() + offset, data, size);
unlockSharedMemory(); unlockSharedMemory();
} }
char *getSharedMemory() { auto getSharedMemory() -> char * {
bool useLocal = false; bool useLocal = false;
if(!useLocal){ if(!useLocal){
@ -83,22 +81,24 @@ char *getSharedMemory() {
} }
return &localSharedMemory[0]; return &localSharedMemory[0];
} }
} }
void getSharedMemory(char *address, int size, int offset) { auto getSharedMemory(char *address, int size, int offset) -> void {
lockSharedMemory(); lockSharedMemory();
memcpy(address, getSharedMemory() + offset, size); memcpy(address, getSharedMemory() + offset, size);
unlockSharedMemory(); unlockSharedMemory();
} }
int getSharedMemorySize() { auto getSharedMemorySize() -> int {
return impl.sharedMemorySize; return impl.sharedMemorySize;
} }
void lockSharedMemory() { auto lockSharedMemory() -> void {
semaphoreOperation(LOCK); semaphoreOperation(LOCK);
} }
void unlockSharedMemory() { auto unlockSharedMemory() -> void {
semaphoreOperation(UNLOCK); semaphoreOperation(UNLOCK);
}
} }

View File

@ -1,17 +1,14 @@
//
// Created by Cigerxwin Chaker on 05.11.19.
//
#ifndef LIBRARY_SHAREDMEMORYACCESSS_H #ifndef LIBRARY_SHAREDMEMORYACCESSS_H
#define LIBRARY_SHAREDMEMORYACCESSS_H #define LIBRARY_SHAREDMEMORYACCESSS_H
int initSemaphore(); namespace vkvm {
auto initSemaphore() -> int;
/** /**
* use lock and unlock when writing * use lock and unlock when writing
* @return pointer to the shared memory * @return pointer to the shared memory
*/ */
char *getSharedMemory(); auto getSharedMemory() -> char *;
/** /**
* use lock and unlock when writing, * use lock and unlock when writing,
@ -20,17 +17,17 @@ char *getSharedMemory();
* @param size of data * @param size of data
* @param offset where the data is written on the shared memory * @param offset where the data is written on the shared memory
*/ */
void getSharedMemory(char *address, int size, int offset); auto getSharedMemory(char *address, int size, int offset) -> void;
/** /**
* *
* @return the size of the shared memory * @return the size of the shared memory
*/ */
int getSharedMemorySize(); auto getSharedMemorySize() -> int;
void lockSharedMemory(); auto lockSharedMemory() -> void;
void unlockSharedMemory(); auto unlockSharedMemory() -> void;
/** /**
* *
@ -38,6 +35,7 @@ void unlockSharedMemory();
* @param size of data * @param size of data
* @param offset where the data is written on the shared memory * @param offset where the data is written on the shared memory
*/ */
void writeSharedMemory(char *data, int size, int offset); auto writeSharedMemory(char *data, int size, int offset) -> void;
}
#endif //LIBRARY_SHAREDMEMORYACCESSS_H #endif

View File

@ -1,78 +1,83 @@
#include "internal.h" #include "internal.hpp"
#include "SharedMemoryAccess.h" #include "SharedMemoryAccess.hpp"
#include "vkvm.h" #include "vkvm.hpp"
#include <csignal> #include <csignal>
#include <sys/shm.h> #include <sys/shm.h>
Impl impl;
void sendSignal(pid_t pid, int signalNumber) { namespace vkvm {
Impl impl;
auto sendSignal(pid_t pid, int signalNumber) -> void {
kill(pid, signalNumber); kill(pid, signalNumber);
} }
void onSignal(int signalNumber, void(*callback)(int)) { auto onSignal(int signalNumber, void(*callback)(int)) -> void {
signal(signalNumber, callback); signal(signalNumber, callback);
} }
InterruptEntry *getInterrupTable(){ auto getInterrupTable() -> InterruptEntry * {
return reinterpret_cast<InterruptEntry*>(getSharedMemory() + sizeof(Registers) + impl.reservedSize); return reinterpret_cast<InterruptEntry *>(getSharedMemory() + sizeof(Registers) + impl.reservedSize);
} }
Registers *getRegisters(){ auto getRegisters() -> Registers * {
return reinterpret_cast<Registers*>(getSharedMemory()); return reinterpret_cast<Registers *>(getSharedMemory());
} }
char *getTextArea(){ auto getTextArea() -> char * {
return ((getSharedMemory() + sizeof(Registers) + impl.reservedSize) + return ((getSharedMemory() + sizeof(Registers) + impl.reservedSize) +
sizeof(InterruptEntry) * impl.interruptEntyCount); sizeof(InterruptEntry) * impl.interruptEntyCount);
} }
char *getPixelArea(){ auto getPixelArea() -> char * {
return (((getSharedMemory() + sizeof(Registers) + impl.reservedSize) + return (((getSharedMemory() + sizeof(Registers) + impl.reservedSize) +
sizeof(InterruptEntry) * impl.interruptEntyCount) + sizeof(InterruptEntry) * impl.interruptEntyCount) +
getCharactersPerRow() * getCharactersPerColumn()); getCharactersPerRow() * getCharactersPerColumn());
} }
bool callEvent(EventType type) { auto callEvent(EventType type) -> bool {
auto ivt = getInterrupTable(); auto ivt = getInterrupTable();
if(ivt[type].pid != 0){ if (ivt[type].pid != 0) {
sendSignal(ivt[type].pid, ivt[type].signum); sendSignal(ivt[type].pid, ivt[type].signum);
} }
return true; return true;
} }
void setLayoutVersion(LayoutVersion newValue) { auto setLayoutVersion(LayoutVersion newValue) -> void {
lockSharedMemory(); lockSharedMemory();
getRegisters()->layout_version = newValue; getRegisters()->layout_version = newValue;
unlockSharedMemory(); unlockSharedMemory();
} }
void setCharactersPerColumn(int newValue) { auto setCharactersPerColumn(int newValue) -> void {
lockSharedMemory(); lockSharedMemory();
getRegisters()->characters_per_column = newValue; getRegisters()->characters_per_column = newValue;
unlockSharedMemory(); unlockSharedMemory();
} }
void setCharactersPerRow(int newValue) { auto setCharactersPerRow(int newValue) -> void {
lockSharedMemory(); lockSharedMemory();
getRegisters()->characters_per_row = newValue; getRegisters()->characters_per_row = newValue;
unlockSharedMemory(); unlockSharedMemory();
} }
void setMousePosition(int x, int y) { auto setMousePosition(int x, int y) -> void {
lockSharedMemory(); lockSharedMemory();
getRegisters()->mouse_pos_x = x; getRegisters()->mouse_pos_x = x;
getRegisters()->mouse_pos_y = y; getRegisters()->mouse_pos_y = y;
unlockSharedMemory(); unlockSharedMemory();
} }
void buttonPressed(KeyCode keyCode) { auto buttonPressed(KeyCode keyCode) -> void {
lockSharedMemory(); lockSharedMemory();
auto reg = getRegisters(); auto reg = getRegisters();
if(reg->keyboardBuffer_index_write == sizeof(reg->keyboardBuffer)){ if (reg->keyboardBuffer_index_write == sizeof(reg->keyboardBuffer)) {
reg->keyboardBuffer_index_write = 0; reg->keyboardBuffer_index_write = 0;
} }
reg->keyboardBuffer[reg->keyboardBuffer_index_write++] = keyCode.getValue(); reg->keyboardBuffer[reg->keyboardBuffer_index_write++] = keyCode.getValue();
unlockSharedMemory(); unlockSharedMemory();
}
} }

View File

@ -1,126 +0,0 @@
#ifndef LIBRARY_INTERNAL_H
#define LIBRARY_INTERNAL_H
#include "EventType.h"
#include "KeyCode.h"
#include "LayoutVersion.h"
#include "GraphicMode.h"
#include "Color.h"
#include <sys/types.h>
#include <vector>
#include <functional>
/**
* the Control Registers
* @author Julian Hinxlage
* @since 0.1.0
*/
struct Registers {
int layout_version;
int trigger_reset;
int width_pixels;
int height_pixels;
GraphicMode graphicMode;
int autoRedrawInterval;
int timerInterruptInterval;
Color background_color;
Color foreground_color;
int characters_per_row;
int characters_per_column;
int textMode_font;
int textMode_font_width;
int textMode_font_height;
int mouse_pos_x;
int mouse_pos_y;
short keyboardBuffer[16];
int keyboardBuffer_index_write;
int keyboardBuffer_index_read;
};
struct InterruptEntry {
int pid;
int signum;
};
/**
* internal values for the library.
*/
struct Impl {
int sharedMemoryPid;
key_t sharedMemoryKey;
int sharedMemorySize;
int interruptEntyCount = 64;
int reservedSize = 1024;
std::vector<std::function<void()>> eventTable;
};
extern Impl impl;
/**
* send a signal to a process
* @param pid of the process to which the signal is send
* @param signalNumber
*/
void sendSignal(pid_t pid, int signalNumber);
/**
* calls the callback if a signal is received
* @param signalNumber
*/
void onSignal(int signalNumber, void(*callback)(int));
InterruptEntry *getInterrupTable();
Registers *getRegisters();
char *getTextArea();
char *getPixelArea();
/**
* set layout version.
* @param newValue new layout version number.
*/
void setLayoutVersion(LayoutVersion newValue);
/**
* set characters per column for current font.
* @param newValue characters per column.
*/
void setCharactersPerColumn(int newValue);
/**
* set characters per row for current font.
* @param newValue
*/
void setCharactersPerRow(int newValue);
/**
* call a specific event.
* @param type
* @return true if there is a handler registered.
*/
bool callEvent(EventType type);
/**
* set mouse position to x,y value.
* @param x x coordinate
* @param y y coordinate
*/
void setMousePosition(int x, int y);
/**
* register pressed button.
* @param keyCode pressed key.
*/
void buttonPressed(KeyCode keyCode);
// Shared Memory Layout
// --------------------------------------------------------------------
// struct ControlRegisters
// char reserved[1024]
// Interrupt Vector Table [64]
// text area [max_textMode_width * max_textMode_height]
// pixel area [max_height_pixels * max_height_pixels * sizeof(uint_32)]
#endif

132
src/internal.hpp Normal file
View File

@ -0,0 +1,132 @@
#ifndef LIBRARY_INTERNAL_HPP
#define LIBRARY_INTERNAL_HPP
#include "EventType.hpp"
#include "KeyCode.hpp"
#include "LayoutVersion.hpp"
#include "GraphicMode.hpp"
#include "Color.hpp"
#include <sys/types.h>
#include <vector>
#include <functional>
namespace vkvm {
/**
* the Control Registers
* @author Julian Hinxlage
* @since 0.1.0
*/
struct Registers {
int layout_version;
int trigger_reset;
int width_pixels;
int height_pixels;
GraphicMode graphicMode;
int autoRedrawInterval;
int timerInterruptInterval;
Color background_color;
Color foreground_color;
int characters_per_row;
int characters_per_column;
int textMode_font;
int textMode_font_width;
int textMode_font_height;
int mouse_pos_x;
int mouse_pos_y;
short keyboardBuffer[16];
int keyboardBuffer_index_write;
int keyboardBuffer_index_read;
};
struct InterruptEntry {
int pid;
int signum;
};
/**
* internal values for the library.
*/
struct Impl {
int sharedMemoryPid;
key_t sharedMemoryKey;
int sharedMemorySize;
int interruptEntyCount = 64;
int reservedSize = 1024;
std::vector<std::function<void()>> eventTable;
};
extern Impl impl;
/**
* send a signal to a process
* @param pid of the process to which the signal is send
* @param signalNumber
*/
auto sendSignal(pid_t pid, int signalNumber) -> void;
/**
* calls the callback if a signal is received
* @param signalNumber
*/
auto onSignal(int signalNumber, void(*callback)(int)) -> void;
auto getInterrupTable() -> InterruptEntry *;
auto getRegisters() -> Registers *;
auto getTextArea() -> char * ;
auto getPixelArea() -> char *;
/**
* set layout version.
* @param newValue new layout version number.
*/
auto setLayoutVersion(LayoutVersion newValue) -> void;
/**
* set characters per column for current font.
* @param newValue characters per column.
*/
auto setCharactersPerColumn(int newValue) -> void;
/**
* set characters per row for current font.
* @param newValue
*/
auto setCharactersPerRow(int newValue) -> void;
/**
* call a specific event.
* @param type
* @return true if there is a handler registered.
*/
auto callEvent(EventType type) -> bool;
/**
* set mouse position to x,y value.
* @param x x coordinate
* @param y y coordinate
*/
auto setMousePosition(int x, int y) -> void;
/**
* register pressed button.
* @param keyCode pressed key.
*/
auto buttonPressed(KeyCode keyCode) -> void;
// Shared Memory Layout
// --------------------------------------------------------------------
// struct ControlRegisters
// char reserved[1024]
// Interrupt Vector Table [64]
// text area [max_textMode_width * max_textMode_height]
// pixel area [max_height_pixels * max_height_pixels * sizeof(uint_32)]
}
#endif

View File

@ -4,11 +4,12 @@
* @since 0.1.0 * @since 0.1.0
*/ */
#include "log.h" #include "log.hpp"
#include <iostream> #include <iostream>
//converts the level to a string of the level namespace vkvm {
auto getLevelName(LogLevel level){ //converts the level to a string of the level
auto getLevelName(LogLevel level) -> std::string {
switch(level){ switch(level){
case LogLevel::DEBUG: case LogLevel::DEBUG:
return "DEBUG"; return "DEBUG";
@ -23,10 +24,10 @@ auto getLevelName(LogLevel level){
default: default:
return "NON"; return "NON";
} }
} }
//converts the level to a ansi color code //converts the level to a ansi color code
auto getLevelColor(LogLevel level){ auto getLevelColor(LogLevel level) -> std::string {
switch(level){ switch(level){
case LogLevel::DEBUG: case LogLevel::DEBUG:
return "0;37"; return "0;37";
@ -41,12 +42,12 @@ auto getLevelColor(LogLevel level){
default: default:
return "0"; return "0";
} }
} }
LogLevel logLevel = LogLevel::INFO; LogLevel logLevel = LogLevel::INFO;
//log the current time //log the current time
void logTime(){ auto logTime() -> void {
time_t rawtime; time_t rawtime;
time(&rawtime); time(&rawtime);
struct tm *timeinfo; struct tm *timeinfo;
@ -68,10 +69,10 @@ void logTime(){
std::cout << "0"; std::cout << "0";
} }
std::cout << timeinfo->tm_sec; std::cout << timeinfo->tm_sec;
} }
//log the message //log the message
void log(LogLevel level, const std::string &msg) { auto log(LogLevel level, const std::string &msg) -> void {
if(level >= logLevel) { if(level >= logLevel) {
std::string levelName = getLevelName(level); std::string levelName = getLevelName(level);
const int maxLevelNameLength = 8; const int maxLevelNameLength = 8;
@ -104,9 +105,10 @@ void log(LogLevel level, const std::string &msg) {
} }
std::cout << "\n"; std::cout << "\n";
} }
} }
void setLogLevel(LogLevel level) { auto setLogLevel(LogLevel level) -> void {
logLevel = level; logLevel = level;
} }
}

View File

@ -1,50 +0,0 @@
#ifndef LIBRARY_LOG_H
#define LIBRARY_LOG_H
#include <string>
#include <sstream>
enum LogLevel{
DEBUG = 1,
INFO = 2,
WARNING = 3,
ERROR = 4,
CRITICAL = 5,
OFF = 6
};
/**
* log the messgae with logLevel and timestamp
* @author Julian Hinxlage
* @since 0.1.0
*/
void log(LogLevel level, const std::string &msg);
template<typename T>
static void buildString(std::stringstream &stream, T t) {
stream << t;
}
template<typename T, typename... Ts>
static void buildString(std::stringstream &stream, T t, Ts... ts) {
stream << t;
buildString(stream, ts...);
}
template<typename... T>
static void log(LogLevel level, T... t){
std::stringstream stream;
buildString(stream, t...);
log(level, stream.str());
}
/**
* set the logLevel, the log function will use this to determine if the message is logged
* @author Julian Hinxlage
* @since 0.1.0
*/
void setLogLevel(LogLevel level);
#endif //LIBRARY_LOG_H

52
src/log.hpp Normal file
View File

@ -0,0 +1,52 @@
#ifndef LIBRARY_LOG_HPP
#define LIBRARY_LOG_HPP
#include <string>
#include <sstream>
namespace vkvm {
enum LogLevel{
DEBUG = 1,
INFO = 2,
WARNING = 3,
ERROR = 4,
CRITICAL = 5,
OFF = 6
};
/**
* log the messgae with logLevel and timestamp
* @author Julian Hinxlage
* @since 0.1.0
*/
auto log(LogLevel level, const std::string &msg) -> void;
template<typename T>
static auto buildString(std::stringstream &stream, T t) -> void {
stream << t;
}
template<typename T, typename... Ts>
static auto buildString(std::stringstream &stream, T t, Ts... ts) -> void {
stream << t;
buildString(stream, ts...);
}
template<typename... T>
static auto log(LogLevel level, T... t) -> void {
std::stringstream stream;
buildString(stream, t...);
log(level, stream.str());
}
/**
* set the logLevel, the log function will use this to determine if the message is logged
* @author Julian Hinxlage
* @since 0.1.0
*/
auto setLogLevel(LogLevel level) -> void;
}
#endif

View File

@ -1,20 +1,21 @@
#include "SharedMemoryAccess.h" #include "SharedMemoryAccess.hpp"
#include "internal.h" #include "internal.hpp"
#include "vkvm.h" #include "vkvm.hpp"
#include <csignal> #include <csignal>
#include <unistd.h> #include <unistd.h>
// NOLINT namespace vkvm {
void initialize(int pid) { // NOLINT
auto initialize(int pid) -> void {
impl.sharedMemoryPid = pid; impl.sharedMemoryPid = pid;
impl.sharedMemoryKey = 12345;// NOLINT impl.sharedMemoryKey = 12345;// NOLINT
impl.sharedMemorySize = 8000;// NOLINT impl.sharedMemorySize = 8000;// NOLINT
initSemaphore(); initSemaphore();
setDefaultValues(); setDefaultValues();
} }
void setDefaultValues(){ auto setDefaultValues() -> void {
if(getSharedMemory() != nullptr) { if(getSharedMemory() != nullptr) {
//set default values //set default values
setCharactersPerRow(60);// NOLINT setCharactersPerRow(60);// NOLINT
@ -28,9 +29,9 @@ void setDefaultValues(){
setRedrawInterval(20);// NOLINT setRedrawInterval(20);// NOLINT
setTimerInterruptInterval(10);// NOLINT setTimerInterruptInterval(10);// NOLINT
} }
} }
bool registerEvent(EventType type, const std::function<void()> &handler) { auto registerEvent(EventType type, const std::function<void()> &handler) -> bool {
int signum = SIGUSR1 + impl.eventTable.size(); int signum = SIGUSR1 + impl.eventTable.size();
auto ivt = getInterrupTable(); auto ivt = getInterrupTable();
@ -51,24 +52,24 @@ bool registerEvent(EventType type, const std::function<void()> &handler) {
unlockSharedMemory(); unlockSharedMemory();
return true; return true;
} }
bool setPixel(int x, int y, Color color) { auto setPixel(int x, int y, Color color) -> bool {
char *ptr = getPixelArea() + (y * getWidth() + x) * 3; char *ptr = getPixelArea() + (y * getWidth() + x) * 3;
ptr[0] = color.getRed(); ptr[0] = color.getRed();
ptr[1] = color.getGreen(); ptr[1] = color.getGreen();
ptr[2] = color.getBlue(); ptr[2] = color.getBlue();
return false; return false;
} }
Color getPixel(int x, int y) { auto getPixel(int x, int y) -> Color {
//TODO(julian): other than RGB colores //TODO(julian): other than RGB colores
//only RGB colores for now //only RGB colores for now
unsigned char *ptr = reinterpret_cast<unsigned char *>(getPixelArea()) + (y * getWidth() + x) * 3; unsigned char *ptr = reinterpret_cast<unsigned char *>(getPixelArea()) + (y * getWidth() + x) * 3;
return {ptr[0], ptr[1], ptr[2]}; return {ptr[0], ptr[1], ptr[2]};
} }
bool setText(std::string text) { auto setText(std::string text) -> bool {
lockSharedMemory(); lockSharedMemory();
char *ptr = getTextArea(); char *ptr = getTextArea();
for(int i = 0; i < static_cast<int>(text.size());i++){ for(int i = 0; i < static_cast<int>(text.size());i++){
@ -82,115 +83,117 @@ bool setText(std::string text) {
} }
unlockSharedMemory(); unlockSharedMemory();
return true; return true;
} }
std::string getText() { auto getText() -> std::string {
return std::string (getTextArea()); return std::string (getTextArea());
} }
LayoutVersion getLayoutVersion() { auto getLayoutVersion() -> LayoutVersion {
return static_cast<LayoutVersion>(getRegisters()->layout_version); return static_cast<LayoutVersion>(getRegisters()->layout_version);
} }
void reset() { auto reset() -> void {
//TODO(julian): reset //TODO(julian): reset
} }
int getWidth() { auto getWidth() -> int {
return getRegisters()->width_pixels; return getRegisters()->width_pixels;
} }
void setWidth(int newValue) { auto setWidth(int newValue) -> void {
lockSharedMemory(); lockSharedMemory();
getRegisters()->width_pixels = newValue; getRegisters()->width_pixels = newValue;
unlockSharedMemory(); unlockSharedMemory();
} }
int getHeight() { auto getHeight() -> int {
return getRegisters()->height_pixels; return getRegisters()->height_pixels;
} }
void setHeight(int newValue) { auto setHeight(int newValue) -> void {
lockSharedMemory(); lockSharedMemory();
getRegisters()->height_pixels = newValue; getRegisters()->height_pixels = newValue;
unlockSharedMemory(); unlockSharedMemory();
} }
GraphicMode getMode() { auto getMode() -> GraphicMode {
return getRegisters()->graphicMode; return getRegisters()->graphicMode;
} }
void setMode(GraphicMode newValue) { auto setMode(GraphicMode newValue) -> void {
lockSharedMemory(); lockSharedMemory();
getRegisters()->graphicMode = newValue; getRegisters()->graphicMode = newValue;
unlockSharedMemory(); unlockSharedMemory();
} }
int getRedrawInterval() { auto getRedrawInterval() -> int {
return getRegisters()->autoRedrawInterval; return getRegisters()->autoRedrawInterval;
} }
void setRedrawInterval(int newValue) { auto setRedrawInterval(int newValue) -> void {
lockSharedMemory(); lockSharedMemory();
getRegisters()->autoRedrawInterval = newValue; getRegisters()->autoRedrawInterval = newValue;
unlockSharedMemory(); unlockSharedMemory();
} }
int getTimerInterruptInterval() { auto getTimerInterruptInterval() -> int {
return getRegisters()->timerInterruptInterval; return getRegisters()->timerInterruptInterval;
} }
void setTimerInterruptInterval(int newValue) { auto setTimerInterruptInterval(int newValue) -> void {
lockSharedMemory(); lockSharedMemory();
getRegisters()->timerInterruptInterval = newValue; getRegisters()->timerInterruptInterval = newValue;
unlockSharedMemory(); unlockSharedMemory();
} }
Color getBackgroundColor() { auto getBackgroundColor() -> Color {
return getRegisters()->background_color; return getRegisters()->background_color;
} }
void setBackgroundColor(Color newValue) { auto setBackgroundColor(Color newValue) -> void {
lockSharedMemory(); lockSharedMemory();
getRegisters()->background_color = newValue; getRegisters()->background_color = newValue;
unlockSharedMemory(); unlockSharedMemory();
} }
Color getForegroundColor() { auto getForegroundColor() -> Color {
return getRegisters()->foreground_color; return getRegisters()->foreground_color;
} }
void setForegroundColor(Color newValue) { auto setForegroundColor(Color newValue) -> void {
lockSharedMemory(); lockSharedMemory();
getRegisters()->foreground_color = newValue; getRegisters()->foreground_color = newValue;
unlockSharedMemory(); unlockSharedMemory();
} }
int getCharactersPerRow() { auto getCharactersPerRow() -> int {
return getRegisters()->characters_per_row; return getRegisters()->characters_per_row;
} }
int getCharactersPerColumn() { auto getCharactersPerColumn() -> int {
return getRegisters()->characters_per_column; return getRegisters()->characters_per_column;
} }
FontType getFont() { auto getFont() -> FontType {
//TODO(julian): get font properly //TODO(julian): get font properly
return font_1; return font_1;
} }
void setFont(const FontType &newValue) { auto setFont(const FontType &newValue) -> void {
//TODO(julian): setFont properly //TODO(julian): setFont properly
lockSharedMemory(); lockSharedMemory();
getRegisters()->textMode_font = newValue.getId(); getRegisters()->textMode_font = newValue.getId();
unlockSharedMemory(); unlockSharedMemory();
} }
std::pair<int, int> getMousePosition() { auto getMousePosition() -> Coordinates {
return {getRegisters()->mouse_pos_x, getRegisters()->mouse_pos_y}; return {getRegisters()->mouse_pos_x, getRegisters()->mouse_pos_y};
} }
KeyCode getLastPressedKey() { auto getLastPressedKey() -> KeyCode {
//TODO(julian): get key properly //TODO(julian): get key properly
return KeyCode(0); return KeyCode(0);
}
} }

View File

@ -1,30 +1,34 @@
#ifndef LIBRARY_VKVM_H #ifndef LIBRARY_VKVM_HPP
#define LIBRARY_VKVM_H #define LIBRARY_VKVM_HPP
#include "Color.h" #include "Color.hpp"
#include "EventType.h" #include "EventType.hpp"
#include "GraphicMode.h" #include "GraphicMode.hpp"
#include "FontType.h" #include "FontType.hpp"
#include "KeyCode.h" #include "KeyCode.hpp"
#include "LayoutVersion.h" #include "LayoutVersion.hpp"
#include "log.h" #include "log.hpp"
#include <functional> #include <functional>
#include <string> #include <string>
/** namespace vkvm {
/**
* @since 0.1.0 * @since 0.1.0
* @author Johannes Theiner * @author Johannes Theiner
*/ */
//TODO: better documentation struct Coordinates {
int x;
int y;
};
/** /**
* initialize the connection with the shared-memory application * initialize the connection with the shared-memory application
* @param pid pip of shared-memory application * @param pid pip of shared-memory application
*/ */
void initialize(int pid); auto initialize(int pid) -> void;
void setDefaultValues(); auto setDefaultValues() -> void;
/** /**
* set pixel at a x,y position to a certain color. * set pixel at a x,y position to a certain color.
@ -33,7 +37,7 @@ void setDefaultValues();
* @param color color of pixel * @param color color of pixel
* @return true if operation succeeded, false if it failed. * @return true if operation succeeded, false if it failed.
*/ */
bool setPixel(int x, int y, Color color); auto setPixel(int x, int y, Color color) -> bool;
/** /**
* get color of pixel at x,y position * get color of pixel at x,y position
@ -41,7 +45,7 @@ bool setPixel(int x, int y, Color color);
* @param y y coordinate of pixel * @param y y coordinate of pixel
* @return color of pixel * @return color of pixel
*/ */
Color getPixel(int x, int y); auto getPixel(int x, int y) -> Color;
/** /**
* register handler for event. * register handler for event.
@ -49,19 +53,19 @@ Color getPixel(int x, int y);
* @param handler function to call. * @param handler function to call.
* @return true if handler could be registered, false if it failed. * @return true if handler could be registered, false if it failed.
*/ */
bool registerEvent(EventType type, const std::function<void()> &handler); auto registerEvent(EventType type, const std::function<void()> &handler) -> bool;
/** /**
* set displayed text in Text mode * set displayed text in Text mode
* @param text text to display * @param text text to display
* @return if text could be set, false if it could not be set. * @return if text could be set, false if it could not be set.
*/ */
bool setText(std::string text); auto setText(std::string text) -> bool;
/** /**
* get currently saved/displayed text. * get currently saved/displayed text.
*/ */
std::string getText(); auto getText() -> std::string;
//Control registers start here //Control registers start here
@ -71,71 +75,71 @@ std::string getText();
* get version of the used layout * get version of the used layout
* @return layout version * @return layout version
*/ */
LayoutVersion getLayoutVersion(); auto getLayoutVersion() -> LayoutVersion;
/** /**
* reset all values to default. * reset all values to default.
*/ */
void reset(); auto reset() -> void;
/** /**
* get width of window. * get width of window.
* @return width of window. * @return width of window.
*/ */
int getWidth(); auto getWidth() -> int;
/** /**
* set width of window. * set width of window.
* @param newValue new width for window. * @param newValue new width for window.
*/ */
void setWidth(int newValue); auto setWidth(int newValue) -> void;
/** /**
* get height for window. * get height for window.
* @return height of window. * @return height of window.
*/ */
int getHeight(); auto getHeight() -> int;
/** /**
* set height of window. * set height of window.
* @param newValue new height for window. * @param newValue new height for window.
*/ */
void setHeight(int newValue); auto setHeight(int newValue) -> void;
/** /**
* get graphics display mode. * get graphics display mode.
* @return GraphicMode. * @return GraphicMode.
*/ */
GraphicMode getMode(); auto getMode() -> GraphicMode;
/** /**
* set new graphics display mode. * set new graphics display mode.
* @param newValue new graphics display mode. * @param newValue new graphics display mode.
*/ */
void setMode(GraphicMode newValue); auto setMode(GraphicMode newValue) -> void;
/** /**
* get interval between redraws in milliseconds. * get interval between redraws in milliseconds.
*/ */
int getRedrawInterval(); auto getRedrawInterval() -> int;
/** /**
* set interval between redraws. * set interval between redraws.
* @param newValue new interval in milliseconds. * @param newValue new interval in milliseconds.
*/ */
void setRedrawInterval(int newValue); auto setRedrawInterval(int newValue) -> void;
/** /**
* get time between timer interrupts. * get time between timer interrupts.
* @return time between interrupts in milliseconds. * @return time between interrupts in milliseconds.
*/ */
int getTimerInterruptInterval(); auto getTimerInterruptInterval() -> int;
/** /**
* set time between timer interrupts. * set time between timer interrupts.
* @param newValue new time between interrupts in milliseconds. * @param newValue new time between interrupts in milliseconds.
*/ */
void setTimerInterruptInterval(int newValue); auto setTimerInterruptInterval(int newValue) -> void;
//black/white mode //black/white mode
@ -143,25 +147,25 @@ void setTimerInterruptInterval(int newValue);
* get background color in two color mode. * get background color in two color mode.
* @return background color. * @return background color.
*/ */
Color getBackgroundColor(); auto getBackgroundColor() -> Color;
/** /**
* set background color in two color mode. * set background color in two color mode.
* @param newValue new background color. * @param newValue new background color.
*/ */
void setBackgroundColor(Color newValue); auto setBackgroundColor(Color newValue) -> void;
/** /**
* get foreground color in two color mode. * get foreground color in two color mode.
* @return foreground color. * @return foreground color.
*/ */
Color getForegroundColor(); auto getForegroundColor() -> Color;
/** /**
* set foreground color in two color mode. * set foreground color in two color mode.
* @param newValue new foreground color. * @param newValue new foreground color.
*/ */
void setForegroundColor(Color newValue); auto setForegroundColor(Color newValue) -> void;
//text mode //text mode
@ -169,36 +173,37 @@ void setForegroundColor(Color newValue);
* get characters per row in text mode. * get characters per row in text mode.
* @return characters per row. * @return characters per row.
*/ */
int getCharactersPerRow(); auto getCharactersPerRow() -> int;
/** /**
* get characters per column in text mode. * get characters per column in text mode.
* @return characters per column. * @return characters per column.
*/ */
int getCharactersPerColumn(); auto getCharactersPerColumn() -> int;
/** /**
* get currently used font in text mode. * get currently used font in text mode.
* @return currently used font. * @return currently used font.
*/ */
FontType getFont(); auto getFont() -> FontType;
/** /**
* set text mode font. * set text mode font.
* @param newValue new text mode font. * @param newValue new text mode font.
*/ */
void setFont(const FontType &newValue); auto setFont(const FontType &newValue) -> void;
/** /**
* get current mouse position * get current mouse position
* @return mouse position as x,y pair * @return mouse position as x,y pair
*/ */
std::pair<int, int> getMousePosition(); auto getMousePosition() -> Coordinates;
/** /**
* get key code of last key press. * get key code of last key press.
* @return KeyCode of last key press. * @return KeyCode of last key press.
*/ */
KeyCode getLastPressedKey(); auto getLastPressedKey() -> KeyCode;
}
#endif #endif

View File

@ -1,10 +1,10 @@
#include "catch2/catch.hpp" #include "catch2/catch.hpp"
#include "../src/vkvm.h" #include "../src/vkvm.hpp"
TEST_CASE("add works") { TEST_CASE("add works") {
initialize(0); vkvm::initialize(0);
setText("Hello World"); vkvm::setText("Hello World");
SECTION("equals") { SECTION("equals") {
REQUIRE(getText() == "Hello World"); REQUIRE(vkvm::getText() == "Hello World");
} }
} }