~ fixed clang-tidy errors

This commit is contained in:
Julian Hinxlage 2019-11-12 14:07:02 +01:00
parent 843be8d47c
commit f5c461a05b
14 changed files with 102 additions and 85 deletions

View File

@ -1,7 +1,7 @@
#include "Color.h" #include "Color.h"
Color::Color(unsigned char red, unsigned char green, unsigned char blue) Color::Color(unsigned char red, unsigned char green, unsigned char blue) noexcept
: red(red), green(green), blue(blue){ : red(red), green(green), blue(blue){
} }

View File

@ -15,7 +15,7 @@ private:
unsigned char blue; unsigned char blue;
public: public:
Color(unsigned char red, unsigned char green, unsigned char blue); Color(unsigned char red, unsigned char green, unsigned char blue) noexcept;
unsigned char getRed(); unsigned char getRed();
unsigned char getGreen(); unsigned char getGreen();

View File

@ -1,20 +1,25 @@
#include "FontType.h" #include "FontType.h"
FontType::FontType(std::string name, int height, int width) { FontType::FontType(int id, std::string name, int height, int width) noexcept {
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;
} }
std::string FontType::getName() { int FontType::getId() const{
return id;
}
std::string FontType::getName() const{
return name; return name;
} }
int FontType::getHeight() { int FontType::getHeight() const{
return height; return height;
} }
int FontType::getWidth() { int FontType::getWidth() const{
return width; return width;
} }

View File

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

View File

@ -1,9 +1,9 @@
#include "KeyCode.h" #include "KeyCode.h"
KeyCode::KeyCode(int value) : value(value) {} KeyCode::KeyCode(int16_t value) noexcept : value(value) {}
int KeyCode::getValue() { int16_t KeyCode::getValue() {
return value; return value;
} }

View File

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

View File

@ -1,24 +1,24 @@
// //
// Created by Cigerxwin Chaker on 05.11.19. // Created by Cigerxwin Chaker on 05.11.19.
// //
#include <iostream> #include <cstdio>
#include <sys/types.h> #include <cstdlib>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/sem.h>
#include <unistd.h>
#include <cstring> #include <cstring>
#include <iostream>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "internal.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.h" /* header is important for the shmID. name could be different. maybe not needed cause: (shmget(memory_access_key, NULL, 0)) */
#include "internal.h"
#define PERM 0666 /* Zugriffsrechte */ #define PERM 0666 /* access rights */
#define LOCK -1 #define LOCK (-1)
#define UNLOCK 1 #define UNLOCK (1)
#define SEM_KEY 123458L #define 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;
@ -26,29 +26,30 @@ struct sembuf semaphore;
std::vector<char> localSharedMemory; std::vector<char> localSharedMemory;
int initSemaphore () { 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) {
/* ... existiert noch nicht, also anlegen */ /* ... existiert noch nicht, also anlegen */
/* 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 | PERM);
if (semId < 0) { if (semId < 0) {
return -1; return -1;
} }
/* Semaphor mit 1 initialisieren */ /* Semaphor mit 1 initialisieren */
if (semctl (semId, 0, SETVAL, (int) 1) == -1) if (semctl(semId, 0, SETVAL, 1) == -1){
return -1; return -1;
}
} }
return 1; return 1;
} }
int semaphoreOperation (int op) { int semaphoreOperation(int op) {
semaphore.sem_op = (short)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) {
perror(" semop "); perror(" semop ");
return -1; return -1;
} }
@ -56,13 +57,10 @@ 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, 0, 0); // dont init just get the ID if already existing.
if(shmId < 0 ) { if (shmId >= 0) {
return;
/* we could return -1, not sure if it will be useful to continue after there is no shm allocated for any reason*/
} else {
std::cout << "writing" << std::endl; std::cout << "writing" << std::endl;
char *shm_pointer = (char *) shmat(shmId, NULL, 0); auto *shm_pointer = reinterpret_cast<char *>(shmat(shmId, nullptr, 0));
semaphoreOperation(LOCK); semaphoreOperation(LOCK);
memcpy(shm_pointer + offset, data, size); memcpy(shm_pointer + offset, data, size);
shmdt(shm_pointer); //close shm_pointer shmdt(shm_pointer); //close shm_pointer
@ -72,20 +70,17 @@ void writeSharedMemory(char *data, int size, int offset) {
} }
void getSharedMemory(char *address, int size, int offset) { void getSharedMemory(char *address, int size, int offset) {
int shmId = shmget(impl.sharedMemoryKey, NULL, 0); int shmId = shmget(impl.sharedMemoryKey, 0, 0);
if(shmId < 0) { if (shmId >= 0) {
return; auto *shm_pointer = reinterpret_cast<char *>(shmat(shmId, nullptr, 0));
} else {
char *shm_pointer = (char *) shmat(shmId, NULL, 0);
semaphoreOperation(LOCK); semaphoreOperation(LOCK);
memcpy(address, shm_pointer + offset, size); memcpy(address, shm_pointer + offset, size);
shmdt(shm_pointer); //close shm_pointer shmdt(shm_pointer); //close shm_pointer
semaphoreOperation(UNLOCK); semaphoreOperation(UNLOCK);
return;
} }
} }
char* getSharedMemory() { char *getSharedMemory() {
/* /*
int shmId = shmget(impl.sharedMemoryKey, NULL, 0); int shmId = shmget(impl.sharedMemoryKey, NULL, 0);
if(shmId < 0) { if(shmId < 0) {
@ -97,7 +92,7 @@ char* getSharedMemory() {
*/ */
//using a local buffer for shared memory testing //using a local buffer for shared memory testing
if(localSharedMemory.empty()){ if (localSharedMemory.empty()) {
initSemaphore(); initSemaphore();
localSharedMemory.resize(impl.sharedMemorySize * 1024); localSharedMemory.resize(impl.sharedMemorySize * 1024);
} }

View File

@ -37,6 +37,6 @@ 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 *date, int size, int offset); void writeSharedMemory(char *data, int size, int offset);
#endif //LIBRARY_SHAREDMEMORYACCESSS_H #endif //LIBRARY_SHAREDMEMORYACCESSS_H

View File

@ -2,8 +2,8 @@
#include "SharedMemoryAccess.h" #include "SharedMemoryAccess.h"
#include "vkvm.h" #include "vkvm.h"
#include <sys/shm.h>
#include <csignal> #include <csignal>
#include <sys/shm.h>
Impl impl; Impl impl;
@ -16,11 +16,11 @@ void onSignal(int signalNumber, void(*callback)(int)) {
} }
InterruptEntry *getInterrupTable(){ InterruptEntry *getInterrupTable(){
return (InterruptEntry*)(getSharedMemory() + sizeof(Registers) + impl.reservedSize); return reinterpret_cast<InterruptEntry*>(getSharedMemory() + sizeof(Registers) + impl.reservedSize);
} }
Registers *getRegisters(){ Registers *getRegisters(){
return (Registers*)getSharedMemory(); return reinterpret_cast<Registers*>(getSharedMemory());
} }
char *getTextArea(){ char *getTextArea(){
@ -68,5 +68,11 @@ void setMousePosition(int x, int y) {
} }
void buttonPressed(KeyCode keyCode) { void buttonPressed(KeyCode keyCode) {
//TODO lockSharedMemory();
auto reg = getRegisters();
if(reg->keyboardBuffer_index_write == sizeof(reg->keyboardBuffer)){
reg->keyboardBuffer_index_write = 0;
}
reg->keyboardBuffer[reg->keyboardBuffer_index_write++] = keyCode.getValue();
unlockSharedMemory();
} }

View File

@ -32,9 +32,9 @@ struct Registers {
int textMode_font_height; int textMode_font_height;
int mouse_pos_x; int mouse_pos_x;
int mouse_pos_y; int mouse_pos_y;
char keyboardBuffer[16]; short keyboardBuffer[16];
int keyboardBuffer_index_w; int keyboardBuffer_index_write;
int keyboardBuffer_index_r; int keyboardBuffer_index_read;
}; };
struct InterruptEntry { struct InterruptEntry {

View File

@ -8,7 +8,7 @@
#include <iostream> #include <iostream>
//converts the level to a string of the level //converts the level to a string of the level
const char *getLevelName(LogLevel level){ auto getLevelName(LogLevel level){
switch(level){ switch(level){
case LogLevel::DEBUG: case LogLevel::DEBUG:
return "DEBUG"; return "DEBUG";
@ -26,7 +26,7 @@ const char *getLevelName(LogLevel level){
} }
//converts the level to a ansi color code //converts the level to a ansi color code
const char *getLevelColor(LogLevel level){ auto getLevelColor(LogLevel level){
switch(level){ switch(level){
case LogLevel::DEBUG: case LogLevel::DEBUG:
return "0;37"; return "0;37";
@ -72,7 +72,7 @@ void logTime(){
void log(const std::string &msg, LogLevel level) { void log(const std::string &msg, LogLevel level) {
if(level >= logLevel) { if(level >= logLevel) {
std::string levelName = getLevelName(level); std::string levelName = getLevelName(level);
int maxLevelNameLength = 8; const int maxLevelNameLength = 8;
//time //time
std::cout << "["; std::cout << "[";
@ -92,7 +92,8 @@ void log(const std::string &msg, LogLevel level) {
if(c == '\n'){ if(c == '\n'){
//intend newlines so that they align with the start of the message //intend newlines so that they align with the start of the message
std::cout << "\n"; std::cout << "\n";
for(int i = 0; i < 22;i++){ const int paddingSize = 22;
for(int i = 0; i < paddingSize;i++){
std::cout << " "; std::cout << " ";
} }
}else{ }else{

View File

@ -1,14 +1,18 @@
#include "vkvm.h"
#include "internal.h"
#include "SharedMemoryAccess.h" #include "SharedMemoryAccess.h"
#include "internal.h"
#include "vkvm.h"
#include <unistd.h>
#include <csignal> #include <csignal>
#include <unistd.h>
void initialize(int pid) { void initialize(int pid) {
impl.sharedMemoryPid = pid; impl.sharedMemoryPid = pid;
impl.sharedMemoryKey = 12345;
impl.sharedMemorySize = 8000; const int sharedMemoryKey = 12345;
impl.sharedMemoryKey = sharedMemoryKey;
const int sharedMemorySize = 8000;
impl.sharedMemorySize = sharedMemorySize;
//set default values //set default values
setCharactersPerRow(60); setCharactersPerRow(60);
@ -23,7 +27,7 @@ void initialize(int pid) {
setTimerInterruptInterval(10); setTimerInterruptInterval(10);
} }
bool registerEvent(EventType type, std::function<void()> handler) { bool registerEvent(EventType type, const std::function<void()> &handler) {
int signum = SIGUSR1 + impl.eventTable.size(); int signum = SIGUSR1 + impl.eventTable.size();
auto ivt = getInterrupTable(); auto ivt = getInterrupTable();
@ -55,10 +59,10 @@ bool setPixel(int x, int y, Color color) {
} }
Color getPixel(int x, int y) { Color getPixel(int x, int y) {
//TODO: other than RGB colores //TODO(julian): other than RGB colores
//only RGB colores for now //only RGB colores for now
unsigned char *ptr = (unsigned char *)getPixelArea() + (y * getWidth() + x) * 3; unsigned char *ptr = reinterpret_cast<unsigned char *>(getPixelArea()) + (y * getWidth() + x) * 3;
return Color(ptr[0], ptr[1], ptr[2]); return {ptr[0], ptr[1], ptr[2]};
} }
bool setText(std::string text) { bool setText(std::string text) {
@ -82,11 +86,11 @@ std::string getText() {
} }
LayoutVersion getLayoutVersion() { LayoutVersion getLayoutVersion() {
return (LayoutVersion)getRegisters()->layout_version; return static_cast<LayoutVersion>(getRegisters()->layout_version);
} }
void reset() { void reset() {
//TODO //TODO(julian): reset
} }
int getWidth() { int getWidth() {
@ -168,14 +172,14 @@ int getCharactersPerColumn() {
} }
FontType getFont() { FontType getFont() {
//TODO //TODO(julian): get font properly
return font_1; return font_1;
} }
void setFont(FontType newValue) { void setFont(const FontType &newValue) {
//TODO //TODO(julian): setFont properly
lockSharedMemory(); lockSharedMemory();
getRegisters()->textMode_font = 0; getRegisters()->textMode_font = newValue.getId();
unlockSharedMemory(); unlockSharedMemory();
} }
@ -184,6 +188,6 @@ std::pair<int, int> getMousePosition() {
} }
KeyCode getLastPressedKey() { KeyCode getLastPressedKey() {
//TODO //TODO(julian): get key properly
return KeyCode(0); return KeyCode(0);
} }

View File

@ -1,14 +1,14 @@
#ifndef LIBRARY_VKVM_H #ifndef LIBRARY_VKVM_H
#define LIBRARY_VKVM_H #define LIBRARY_VKVM_H
#include <string>
#include <functional>
#include "Color.h" #include "Color.h"
#include "EventType.h" #include "EventType.h"
#include "GraphicMode.h" #include "GraphicMode.h"
#include "FontType.h" #include "FontType.h"
#include "KeyCode.h" #include "KeyCode.h"
#include "LayoutVersion.h" #include "LayoutVersion.h"
#include <functional>
#include <string>
/** /**
* @since 0.1.0 * @since 0.1.0
@ -184,7 +184,7 @@ FontType getFont();
* set text mode font. * set text mode font.
* @param newValue new text mode font. * @param newValue new text mode font.
*/ */
void setFont(FontType newValue); void setFont(const FontType &newValue);
/** /**
* get current mouse position * get current mouse position

View File

@ -2,7 +2,9 @@
#include "../src/vkvm.h" #include "../src/vkvm.h"
TEST_CASE("add works") { TEST_CASE("add works") {
initialize(0);
setText("Hello World");
SECTION("equals") { SECTION("equals") {
REQUIRE(getText() == "Hallo Welt"); REQUIRE(getText() == "Hello World");
} }
} }