+ Tests for Text
+ Tests for Events + clearText
This commit is contained in:
parent
ee359b7302
commit
1a6b8e0f2b
@ -14,9 +14,9 @@ namespace vkvm {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Color::Color(unsigned int hex) noexcept {
|
Color::Color(unsigned int hex) noexcept {
|
||||||
red = (unsigned char) ((hex >> 16 & 0xFF));
|
red = static_cast<unsigned char>(hex >> 16 & 0xFF);
|
||||||
green = (unsigned char) ((hex >> 8u & 0xFF));
|
green = static_cast<unsigned char>(hex >> 8U & 0xFF);
|
||||||
blue = (unsigned char) ((hex & 0xFF));
|
blue = static_cast<unsigned char>((hex & 0xFF));
|
||||||
}
|
}
|
||||||
|
|
||||||
auto Color::getRed() -> unsigned char {
|
auto Color::getRed() -> unsigned char {
|
||||||
|
@ -1,12 +1,11 @@
|
|||||||
#ifndef LIBRARY_KEYCODE_HPP
|
#ifndef LIBRARY_KEYCODE_HPP
|
||||||
#define LIBRARY_KEYCODE_HPP
|
#define LIBRARY_KEYCODE_HPP
|
||||||
|
|
||||||
#include <sys/types.h>
|
|
||||||
|
|
||||||
namespace vkvm {
|
namespace vkvm {
|
||||||
|
|
||||||
enum KeyCode {
|
enum KeyCode {
|
||||||
Backspcce = 32,
|
Backspace = 32,
|
||||||
Tab = 33,
|
Tab = 33,
|
||||||
Enter = 37,
|
Enter = 37,
|
||||||
ShiftLeft = 249,
|
ShiftLeft = 249,
|
||||||
|
@ -1,25 +1,21 @@
|
|||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <cstdlib>
|
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <iostream>
|
|
||||||
#include <sys/ipc.h>
|
#include <sys/ipc.h>
|
||||||
#include <sys/sem.h>
|
#include <sys/sem.h>
|
||||||
#include <sys/shm.h>
|
#include <sys/shm.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
#include "SharedMemoryAccess.hpp" /* header is important for the shmID. name could be different. maybe not needed cause: (shmget(memory_access_key, NULL, 0)) */
|
#include "SharedMemoryAccess.hpp"
|
||||||
#include "internal.hpp"
|
#include "internal.hpp"
|
||||||
#include "log.hpp"
|
#include "log.hpp"
|
||||||
|
|
||||||
namespace vkvm {
|
namespace vkvm {
|
||||||
constexpr int PERM = 0666; /* access rights */
|
constexpr int PERMISSION = 0666;
|
||||||
constexpr int LOCK = -1;
|
constexpr int LOCK = -1;
|
||||||
constexpr int UNLOCK = 1;
|
constexpr int UNLOCK = 1;
|
||||||
constexpr int SEM_KEY = 123458L;
|
constexpr int SEM_KEY = 123458L;
|
||||||
|
|
||||||
//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;
|
||||||
|
|
||||||
@ -33,7 +29,7 @@ namespace vkvm {
|
|||||||
/* Alle Zugriffsrechte der Dateikreierungsmaske */
|
/* Alle Zugriffsrechte der Dateikreierungsmaske */
|
||||||
/* erlauben */
|
/* erlauben */
|
||||||
umask(0);
|
umask(0);
|
||||||
semId = semget(SEM_KEY, 1, IPC_CREAT | IPC_EXCL | PERM);
|
semId = semget(SEM_KEY, 1, IPC_CREAT | IPC_EXCL | PERMISSION);
|
||||||
if (semId < 0) {
|
if (semId < 0) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -50,6 +46,7 @@ namespace vkvm {
|
|||||||
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 ");
|
||||||
|
log(LogLevel::CRITICAL, "could not operate on semaphore, exiting");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -61,6 +61,7 @@ constexpr int keyboardBufferSize = 16;
|
|||||||
std::vector<std::function<void()>> eventTable;
|
std::vector<std::function<void()>> eventTable;
|
||||||
bool localMemoryWarn = true;
|
bool localMemoryWarn = true;
|
||||||
char *sharedMemory = nullptr;
|
char *sharedMemory = nullptr;
|
||||||
|
int maxTextLength = 60;
|
||||||
};
|
};
|
||||||
|
|
||||||
extern Impl impl;
|
extern Impl impl;
|
||||||
|
86
src/vkvm.cpp
86
src/vkvm.cpp
@ -17,7 +17,7 @@ namespace vkvm {
|
|||||||
|
|
||||||
auto setDefaultValues() -> void {
|
auto setDefaultValues() -> void {
|
||||||
impl.localMemoryWarn = false;
|
impl.localMemoryWarn = false;
|
||||||
if(getSharedMemory() != nullptr) {
|
if (getSharedMemory() != nullptr) {
|
||||||
//set default values
|
//set default values
|
||||||
setMode(GraphicMode::RGB);
|
setMode(GraphicMode::RGB);
|
||||||
setCharactersPerRow(60);
|
setCharactersPerRow(60);
|
||||||
@ -29,7 +29,7 @@ namespace vkvm {
|
|||||||
setForegroundColor(white);
|
setForegroundColor(white);
|
||||||
setRedrawInterval(20);
|
setRedrawInterval(20);
|
||||||
setTimerInterruptInterval(10);
|
setTimerInterruptInterval(10);
|
||||||
setFont(FontType(3,"",0,0));
|
setFont(FontType(3, "", 0, 0));
|
||||||
}
|
}
|
||||||
impl.localMemoryWarn = true;
|
impl.localMemoryWarn = true;
|
||||||
}
|
}
|
||||||
@ -40,17 +40,17 @@ namespace vkvm {
|
|||||||
|
|
||||||
lockSharedMemory();
|
lockSharedMemory();
|
||||||
|
|
||||||
for(int i = 0; i < impl.interruptEntrysPerEventType;i++){
|
for (int i = 0; i < impl.interruptEntrysPerEventType; i++) {
|
||||||
auto &entry= ivt[type * impl.interruptEntrysPerEventType + i];
|
auto &entry = ivt[type * impl.interruptEntrysPerEventType + i];
|
||||||
if (entry.pid == 0) {
|
if (entry.pid == 0) {
|
||||||
|
|
||||||
entry.pid = getpid();
|
entry.pid = getpid();
|
||||||
entry.signum = signum;
|
entry.signum = signum;
|
||||||
impl.eventTable.push_back(handler);
|
impl.eventTable.push_back(handler);
|
||||||
|
|
||||||
onSignal(signum, [](int sig){
|
onSignal(signum, [](int sig) {
|
||||||
if(sig >= SIGUSR1){
|
if (sig >= SIGUSR1) {
|
||||||
if((sig - SIGUSR1) < impl.eventTable.size()){
|
if ((sig - SIGUSR1) < impl.eventTable.size()) {
|
||||||
impl.eventTable[sig - SIGUSR1]();
|
impl.eventTable[sig - SIGUSR1]();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -64,22 +64,21 @@ namespace vkvm {
|
|||||||
}
|
}
|
||||||
|
|
||||||
auto setPixel(int x, int y, Color color) -> bool {
|
auto setPixel(int x, int y, Color color) -> bool {
|
||||||
if(x > getWidth() || y > getHeight()) {
|
if (x > getWidth() || y > getHeight()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
lockSharedMemory();
|
lockSharedMemory();
|
||||||
auto reg = getRegisters();
|
auto reg = getRegisters();
|
||||||
const int bitsPerPixel = 8;
|
const int bitsPerPixel = 8;
|
||||||
|
|
||||||
switch(reg->graphicMode) {
|
switch (reg->graphicMode) {
|
||||||
case Text: {
|
case Text: {
|
||||||
int pixelIndex = (y * getWidth() + x);
|
int pixelIndex = (y * getWidth() + x);
|
||||||
unsigned char *ptr = reinterpret_cast<unsigned char *>(getPixelArea()) + (pixelIndex / bitsPerPixel);
|
unsigned char *ptr = reinterpret_cast<unsigned char *>(getPixelArea()) + (pixelIndex / bitsPerPixel);
|
||||||
if(color == reg->foreground_color){
|
if (color == reg->foreground_color) {
|
||||||
//set bit to 1
|
//set bit to 1
|
||||||
ptr[0] |= (1 << (pixelIndex % bitsPerPixel));
|
ptr[0] |= (1 << (pixelIndex % bitsPerPixel));
|
||||||
}
|
} else {
|
||||||
else{
|
|
||||||
//set bit to 0
|
//set bit to 0
|
||||||
ptr[0] &= ~(1 << (pixelIndex % bitsPerPixel));
|
ptr[0] &= ~(1 << (pixelIndex % bitsPerPixel));
|
||||||
}
|
}
|
||||||
@ -88,11 +87,10 @@ namespace vkvm {
|
|||||||
case TwoColors: {
|
case TwoColors: {
|
||||||
int pixelIndex = (y * getWidth() + x);
|
int pixelIndex = (y * getWidth() + x);
|
||||||
unsigned char *ptr = reinterpret_cast<unsigned char *>(getPixelArea()) + (pixelIndex / bitsPerPixel);
|
unsigned char *ptr = reinterpret_cast<unsigned char *>(getPixelArea()) + (pixelIndex / bitsPerPixel);
|
||||||
if(color == reg->foreground_color){
|
if (color == reg->foreground_color) {
|
||||||
//set bit to 1
|
//set bit to 1
|
||||||
ptr[0] |= (1 << (pixelIndex % bitsPerPixel));
|
ptr[0] |= (1 << (pixelIndex % bitsPerPixel));
|
||||||
}
|
} else {
|
||||||
else{
|
|
||||||
//set bit to 0
|
//set bit to 0
|
||||||
ptr[0] &= ~(1 << (pixelIndex % bitsPerPixel));
|
ptr[0] &= ~(1 << (pixelIndex % bitsPerPixel));
|
||||||
}
|
}
|
||||||
@ -118,23 +116,23 @@ namespace vkvm {
|
|||||||
}
|
}
|
||||||
|
|
||||||
auto getPixel(int x, int y) -> Color {
|
auto getPixel(int x, int y) -> Color {
|
||||||
if(x > getWidth() || y > getHeight()) {
|
if (x > getWidth() || y > getHeight()) {
|
||||||
return getBackgroundColor();
|
return getBackgroundColor();
|
||||||
}
|
}
|
||||||
|
|
||||||
Color color = Color(0,0,0);
|
Color color = Color(0, 0, 0);
|
||||||
auto reg = getRegisters();
|
auto reg = getRegisters();
|
||||||
const int bitsPerPixel = 8;
|
const int bitsPerPixel = 8;
|
||||||
|
|
||||||
switch(reg->graphicMode) {
|
switch (reg->graphicMode) {
|
||||||
case Text: {
|
case Text: {
|
||||||
int pixelIndex = (y * getWidth() + x);
|
int pixelIndex = (y * getWidth() + x);
|
||||||
unsigned char *ptr = reinterpret_cast<unsigned char *>(getPixelArea()) + pixelIndex / bitsPerPixel;
|
unsigned char *ptr = reinterpret_cast<unsigned char *>(getPixelArea()) + pixelIndex / bitsPerPixel;
|
||||||
bool bit = static_cast<bool>(ptr[0] & (1 << (pixelIndex % bitsPerPixel)));
|
bool bit = static_cast<bool>(ptr[0] & (1 << (pixelIndex % bitsPerPixel)));
|
||||||
if(bit){
|
if (bit) {
|
||||||
color = reg->foreground_color;
|
color = reg->foreground_color;
|
||||||
}else{
|
} else {
|
||||||
color =reg->background_color;
|
color = reg->background_color;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -142,10 +140,10 @@ namespace vkvm {
|
|||||||
int pixelIndex = (y * getWidth() + x);
|
int pixelIndex = (y * getWidth() + x);
|
||||||
unsigned char *ptr = reinterpret_cast<unsigned char *>(getPixelArea()) + (pixelIndex / bitsPerPixel);
|
unsigned char *ptr = reinterpret_cast<unsigned char *>(getPixelArea()) + (pixelIndex / bitsPerPixel);
|
||||||
bool bit = static_cast<bool>(ptr[0] & (1 << (pixelIndex % bitsPerPixel)));
|
bool bit = static_cast<bool>(ptr[0] & (1 << (pixelIndex % bitsPerPixel)));
|
||||||
if(bit){
|
if (bit) {
|
||||||
color = reg->foreground_color;
|
color = reg->foreground_color;
|
||||||
}else{
|
} else {
|
||||||
color =reg->background_color;
|
color = reg->background_color;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -166,13 +164,12 @@ namespace vkvm {
|
|||||||
auto setText(std::string text) -> bool {
|
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 < text.size(); i++) {
|
||||||
if(i >= getCharactersPerColumn() * getCharactersPerRow()){
|
if (i < getCharactersPerColumn() * getCharactersPerRow() && i < impl.maxTextLength) {
|
||||||
break;
|
|
||||||
}
|
|
||||||
ptr[i] = text[i];
|
ptr[i] = text[i];
|
||||||
}
|
}
|
||||||
if(text.size() < getCharactersPerColumn() * getCharactersPerRow()){
|
}
|
||||||
|
if (text.size() < getCharactersPerColumn() * getCharactersPerRow()) {
|
||||||
ptr[text.size()] = '\0';
|
ptr[text.size()] = '\0';
|
||||||
}
|
}
|
||||||
unlockSharedMemory();
|
unlockSharedMemory();
|
||||||
@ -180,7 +177,18 @@ namespace vkvm {
|
|||||||
}
|
}
|
||||||
|
|
||||||
auto getText() -> std::string {
|
auto getText() -> std::string {
|
||||||
return std::string (getTextArea());
|
return std::string(getTextArea());
|
||||||
|
}
|
||||||
|
|
||||||
|
auto clearText() -> bool {
|
||||||
|
lockSharedMemory();
|
||||||
|
char *ptr = getTextArea();
|
||||||
|
for (int i = 0; i < impl.maxTextLength; ++i) {
|
||||||
|
ptr[i] = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
unlockSharedMemory();
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto getLayoutVersion() -> LayoutVersion {
|
auto getLayoutVersion() -> LayoutVersion {
|
||||||
@ -218,29 +226,29 @@ namespace vkvm {
|
|||||||
auto setMode(GraphicMode newValue) -> void {
|
auto setMode(GraphicMode newValue) -> void {
|
||||||
lockSharedMemory();
|
lockSharedMemory();
|
||||||
auto reg = getRegisters();
|
auto reg = getRegisters();
|
||||||
if(reg->graphicMode != newValue){
|
if (reg->graphicMode != newValue) {
|
||||||
|
|
||||||
std::vector<Color> pixels;
|
std::vector<Color> pixels;
|
||||||
int height = reg->height_pixels;
|
int height = reg->height_pixels;
|
||||||
int width = reg->width_pixels;
|
int width = reg->width_pixels;
|
||||||
pixels.resize(height * width);
|
pixels.resize(height * width);
|
||||||
|
|
||||||
for(int y = 0; y < height;y++){
|
for (int y = 0; y < height; y++) {
|
||||||
for(int x = 0;x < width;x++){
|
for (int x = 0; x < width; x++) {
|
||||||
pixels[y * width + x] = getPixel(x,y);
|
pixels[y * width + x] = getPixel(x, y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
getRegisters()->graphicMode = newValue;
|
getRegisters()->graphicMode = newValue;
|
||||||
|
|
||||||
unlockSharedMemory();
|
unlockSharedMemory();
|
||||||
for(int y = 0; y < height;y++){
|
for (int y = 0; y < height; y++) {
|
||||||
for(int x = 0;x < width;x++){
|
for (int x = 0; x < width; x++) {
|
||||||
setPixel(x,y, pixels[y * width + x]);
|
setPixel(x, y, pixels[y * width + x]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
lockSharedMemory();
|
lockSharedMemory();
|
||||||
}else{
|
} else {
|
||||||
reg->graphicMode = newValue;
|
reg->graphicMode = newValue;
|
||||||
}
|
}
|
||||||
unlockSharedMemory();
|
unlockSharedMemory();
|
||||||
@ -314,7 +322,7 @@ namespace vkvm {
|
|||||||
lockSharedMemory();
|
lockSharedMemory();
|
||||||
auto keyCode = KeyCode(0);
|
auto keyCode = KeyCode(0);
|
||||||
auto reg = getRegisters();
|
auto reg = getRegisters();
|
||||||
if(reg->keyboardBuffer_index_read != reg->keyboardBuffer_index_write) {
|
if (reg->keyboardBuffer_index_read != reg->keyboardBuffer_index_write) {
|
||||||
keyCode = static_cast<KeyCode>(reg->keyboardBuffer[reg->keyboardBuffer_index_read++]);
|
keyCode = static_cast<KeyCode>(reg->keyboardBuffer[reg->keyboardBuffer_index_read++]);
|
||||||
if (reg->keyboardBuffer_index_read >= sizeof(reg->keyboardBuffer)) {
|
if (reg->keyboardBuffer_index_read >= sizeof(reg->keyboardBuffer)) {
|
||||||
reg->keyboardBuffer_index_read = 0;
|
reg->keyboardBuffer_index_read = 0;
|
||||||
|
@ -67,6 +67,13 @@ namespace vkvm {
|
|||||||
*/
|
*/
|
||||||
auto getText() -> std::string;
|
auto getText() -> std::string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* clear text area
|
||||||
|
* @return if text could be cleared, false if it could not be cleared.
|
||||||
|
*/
|
||||||
|
auto clearText() -> bool;
|
||||||
|
|
||||||
|
|
||||||
//Control registers start here
|
//Control registers start here
|
||||||
|
|
||||||
//all modes
|
//all modes
|
||||||
|
@ -4,8 +4,6 @@
|
|||||||
|
|
||||||
TEST_CASE("Colors") {
|
TEST_CASE("Colors") {
|
||||||
vkvm::initialize(0);
|
vkvm::initialize(0);
|
||||||
vkvm::setWidth(400);
|
|
||||||
vkvm::setHeight(400);
|
|
||||||
vkvm::setMode(vkvm::RGB);
|
vkvm::setMode(vkvm::RGB);
|
||||||
REQUIRE(vkvm::setPixel(10, 10, vkvm::black));
|
REQUIRE(vkvm::setPixel(10, 10, vkvm::black));
|
||||||
REQUIRE(vkvm::setPixel(11, 11, vkvm::white));
|
REQUIRE(vkvm::setPixel(11, 11, vkvm::white));
|
||||||
@ -46,6 +44,9 @@ TEST_CASE("Colors") {
|
|||||||
vkvm::setBackgroundColor(vkvm::blue);
|
vkvm::setBackgroundColor(vkvm::blue);
|
||||||
vkvm::setForegroundColor(vkvm::red);
|
vkvm::setForegroundColor(vkvm::red);
|
||||||
|
|
||||||
|
REQUIRE(vkvm::getBackgroundColor() == vkvm::blue);
|
||||||
|
REQUIRE(vkvm::getForegroundColor() == vkvm::red);
|
||||||
|
|
||||||
REQUIRE(vkvm::getPixel(10, 10) == vkvm::blue);
|
REQUIRE(vkvm::getPixel(10, 10) == vkvm::blue);
|
||||||
REQUIRE(vkvm::getPixel(11, 11) == vkvm::red);
|
REQUIRE(vkvm::getPixel(11, 11) == vkvm::red);
|
||||||
REQUIRE(vkvm::getPixel(12, 12) == vkvm::blue);
|
REQUIRE(vkvm::getPixel(12, 12) == vkvm::blue);
|
||||||
|
32
test/event_test.cpp
Normal file
32
test/event_test.cpp
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
#include "../src/internal.hpp"
|
||||||
|
#include "../src/vkvm.hpp"
|
||||||
|
#include <catch2/catch.hpp>
|
||||||
|
|
||||||
|
TEST_CASE("Event") {
|
||||||
|
vkvm::initialize(0);
|
||||||
|
|
||||||
|
SECTION("Register") {
|
||||||
|
bool mouseMove = false;
|
||||||
|
bool timer = false;
|
||||||
|
bool timer2 = false;
|
||||||
|
vkvm::registerEvent(vkvm::MouseMove, [&]() {
|
||||||
|
mouseMove = true;
|
||||||
|
});
|
||||||
|
vkvm::registerEvent(vkvm::Timer, [&]() {
|
||||||
|
timer = true;
|
||||||
|
});
|
||||||
|
vkvm::registerEvent(vkvm::Timer, [&]() {
|
||||||
|
timer2 = true;
|
||||||
|
});
|
||||||
|
|
||||||
|
REQUIRE_FALSE(mouseMove);
|
||||||
|
vkvm::callEvent(vkvm::MouseMove);
|
||||||
|
REQUIRE(mouseMove);
|
||||||
|
|
||||||
|
REQUIRE_FALSE(timer);
|
||||||
|
REQUIRE_FALSE(timer2);
|
||||||
|
vkvm::callEvent(vkvm::Timer);
|
||||||
|
REQUIRE(timer);
|
||||||
|
REQUIRE(timer2);
|
||||||
|
}
|
||||||
|
}
|
24
test/text_test.cpp
Normal file
24
test/text_test.cpp
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
#include "../src/internal.hpp"
|
||||||
|
#include "../src/vkvm.hpp"
|
||||||
|
#include <catch2/catch.hpp>
|
||||||
|
|
||||||
|
TEST_CASE("Text") {
|
||||||
|
vkvm::initialize(0);
|
||||||
|
|
||||||
|
REQUIRE(vkvm::setText("Hello World, this is a test"));
|
||||||
|
REQUIRE(vkvm::getText() == "Hello World, this is a test");
|
||||||
|
|
||||||
|
REQUIRE(vkvm::setText("Hello World"));
|
||||||
|
REQUIRE(vkvm::getText() == "Hello World");
|
||||||
|
|
||||||
|
vkvm::setCharactersPerColumn(5);
|
||||||
|
vkvm::setCharactersPerRow(1);
|
||||||
|
|
||||||
|
REQUIRE(vkvm::clearText());
|
||||||
|
|
||||||
|
REQUIRE(vkvm::getCharactersPerColumn() == 5);
|
||||||
|
REQUIRE(vkvm::getCharactersPerRow() == 1);
|
||||||
|
|
||||||
|
REQUIRE(vkvm::setText("Hello World"));
|
||||||
|
REQUIRE(vkvm::getText() == "Hello");
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user