From b6c91e837372f824b76e7f3d0ee9b803c5456780 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Wed, 8 Jan 2020 10:58:03 +0100 Subject: [PATCH 1/4] ~ staring refactoring --- src/GUI.cpp | 98 ++++++++++++++++++++++++++-------------------- src/GUI.hpp | 73 +--------------------------------- src/GUI_Window.cpp | 2 +- src/GUI_Window.hpp | 35 +++++++++++++++++ src/Image.cpp | 15 ++----- src/Image.hpp | 26 ++++++++++++ src/Statusbar.cpp | 21 +++++++--- src/Statusbar.hpp | 21 ++++++++++ test/test_GUI.cpp | 2 +- 9 files changed, 160 insertions(+), 133 deletions(-) create mode 100644 src/GUI_Window.hpp create mode 100644 src/Image.hpp create mode 100644 src/Statusbar.hpp diff --git a/src/GUI.cpp b/src/GUI.cpp index 2cde0d1..c19d254 100644 --- a/src/GUI.cpp +++ b/src/GUI.cpp @@ -1,3 +1,6 @@ +#include "GUI_Window.hpp" +#include "Image.hpp" +#include "Statusbar.hpp" #include "GUI.hpp" @@ -5,60 +8,69 @@ * The GUI_run-function starts all functions needed for the GUI. */ -auto GUI_run(int argc, char **argv) -> int { - vkvm::initialize(0); - int window_height, window_width; - window_height = vkvm::getHeight(); - window_width = vkvm::getWidth(); - char *resolusion = get_resolusion(window_height, window_width); - std::cout << resolusion << std::endl; - auto *window = new GUI_Window(window_width, window_height + 30, "example"); - Statusbar *status[5]; - //Dummy-Values TBD - window->begin(); - auto *image = new Image(0, 30, window_width, window_height); +int redrawInterval; +int width; +int height; - status[0] = new Statusbar(0, 0, 300, 30, resolusion); +static void redrawCallback(void * pointer); +static void refreshCallback(void * pointer); + +int GUI_run(int argc, char **argv) { + vkvm::initialize(0); + width = vkvm::getWidth(); + height = vkvm::getHeight(); + redrawInterval = vkvm::getRedrawInterval(); + char *resolution = get_resolution(width, height); + std::cout << resolution << std::endl; + auto *window = new GUI_Window(width, height + 30, "vKVM GUI"); + + Statusbar *status[5]; + + window->begin(); + auto *image = new Image(0, 30, width, height); + + status[0] = new Statusbar(0, 0, 300, 30, resolution); status[1] = new Statusbar(300, 0, 170, 30, "Event:"); status[2] = new Statusbar(470, 0, 200, 30, "Mouse Position:"); vkvm::registerEvent(vkvm::EventType::Redraw, [image]() { - redraw(image); + image->getPixels(); + image->redraw(); }); - vkvm::registerEvent(vkvm::EventType::UpdateControlRegisters, [image]() { - image->setDelay(vkvm::getRedrawInterval()); + vkvm::registerEvent(vkvm::EventType::UpdateControlRegisters, [image, window, status]() { + int newRedrawInterval = vkvm::getRedrawInterval(); + if(newRedrawInterval != redrawInterval) { + redrawInterval = newRedrawInterval; + } + + int newWidth = vkvm::getWidth(); + int newHeight = vkvm::getHeight(); + + if(newWidth != width || newHeight != height) { + width = newWidth; + height = newHeight; + window->size(width, height + 30); + image->size(width, height); + status[0]->label(get_resolution(width, height)); + + window->redraw(); + } }); - Fl::repeat_timeout(image->getDelay(), get_new_image, image); + + Fl::repeat_timeout((double) redrawInterval / 1000, refreshCallback, image); window->end(); window->show(argc, argv); return Fl::run(); } -char *get_resolusion(int window_height, int window_width) { - char *resolusion = new char[25]; - std::string str_temp; - strcpy(resolusion, "Resolution Height:"); - str_temp = std::to_string(window_height); - strcat(resolusion, str_temp.c_str()); - strcat(resolusion, " Width:"); - str_temp = std::to_string(window_width); - strcat(resolusion, str_temp.c_str()); - return resolusion; +static void refreshCallback(void * pointer) { + auto *image = static_cast(pointer); + image->getPixels(); + + Fl::repeat_timeout((double) redrawInterval / 1000, redrawCallback, image); } -void redraw(Image *image) { - image->getPixels(); +static void redrawCallback(void * pointer) { + auto *image = static_cast(pointer); image->redraw(); -} - -void get_new_image(void *pointer) { - Image *image = ((Image *) pointer); - image->getPixels(); - Fl::repeat_timeout(image->getDelay(), refresh_image, pointer); -} - -void refresh_image(void *pointer) { - ((Image *) pointer)->redraw(); - get_new_image(pointer); -} - - + refreshCallback(image); +} \ No newline at end of file diff --git a/src/GUI.hpp b/src/GUI.hpp index 7943393..1658551 100644 --- a/src/GUI.hpp +++ b/src/GUI.hpp @@ -1,79 +1,8 @@ -#include -#include -#include -#include -#include -#include -#include -#include "Mouse_Status.hpp" -#include "vkvm.hpp" -#include "internal.hpp" - #ifndef GUI_GUI_HPP #define GUI_GUI_HPP -/** GUI_Window - * The GUI_Window-class generates a window, within the window it recognizes the curretn mouse-position. - * It also recognizes if a button is pushed on the keyboard or the mouse. Furthermore the class depict the - * content of the Image-class and provides functions to refresh it. - * @param x The mouse-position on the x-axis. - * @param y The mouse-position on the y-axis. - * @param button The button that was pushed last. - */ -class GUI_Window : public Fl_Window { - int x, y, button,lastX,lastY,mouse_status; - vkvm::KeyCode keyCode; - - int handle(int e) override; - - static char *position_to_string(int x, int y); - -public: - GUI_Window(int x, int y, const char *l); -}; - -/** The constructor of the image class, get additional - * @param x: The mouse-position on the x-axis. - * @param y: The mouse-position on the y-axis: - * @param w: The width of a single pixel, - * @param h: The height of a single pixel. - */ - -class Image : public Fl_Widget { - uchar *buf; - double delay; - -public: - Image(int x, int y, int w, int h); - - void draw() override; - - void getPixels(); - - void setDelay(double delay); - - double getDelay(); -}; - -/** Statusbar - * The class inherits a char *text from the Fl_Box. It also shows the window-resolution and the mouse-position. - * @param text: A pointer to a char-array, that can contain a text like a status or whatever you want. - */ -class Statusbar : public Fl_Box { - char* text; -public: - Statusbar(int x, int y, int w, int h, char *text); -}; - -char *get_resolusion(int window_height, int window_width); - -void refresh_image(void *pointer); - -void get_new_image(void *pointer); - -void redraw(Image *image); int GUI_run(int argc, char **argv); -#endif //GUI_GUI_HPP +#endif diff --git a/src/GUI_Window.cpp b/src/GUI_Window.cpp index 13e1d75..073bf17 100644 --- a/src/GUI_Window.cpp +++ b/src/GUI_Window.cpp @@ -1,4 +1,4 @@ -#include "GUI.hpp" +#include "GUI_Window.hpp" /*Function to handle the input*/ diff --git a/src/GUI_Window.hpp b/src/GUI_Window.hpp new file mode 100644 index 0000000..0cf2ab3 --- /dev/null +++ b/src/GUI_Window.hpp @@ -0,0 +1,35 @@ +#include +#include +#include +#include +#include +#include +#include +#include "Mouse_Status.hpp" +#include "vkvm.hpp" +#include "internal.hpp" +#include "Image.hpp" + +#ifndef GUI_GUI_HPP +#define GUI_GUI_HPP + +/** GUI_Window + * The GUI_Window-class generates a window, within the window it recognizes the curretn mouse-position. + * It also recognizes if a button is pushed on the keyboard or the mouse. Furthermore the class depict the + * content of the Image-class and provides functions to refresh it. + * @param x The mouse-position on the x-axis. + * @param y The mouse-position on the y-axis. + * @param button The button that was pushed last. + */ +class GUI_Window : public Fl_Window { + int x, y, button,lastX,lastY,mouse_status; + vkvm::KeyCode keyCode; + + int handle(int e) override; + + static char *position_to_string(int x, int y); + +public: + GUI_Window(int x, int y, const char *l); +}; +#endif //GUI_GUI_HPP diff --git a/src/Image.cpp b/src/Image.cpp index 9ebee9f..629efca 100644 --- a/src/Image.cpp +++ b/src/Image.cpp @@ -1,4 +1,6 @@ -#include "GUI.hpp" +#include +#include +#include "Image.hpp" /** Image * The Image-class draws the bitmap that it get from the Shared Memory. @@ -13,7 +15,6 @@ auto Image::draw() -> void { Image::Image(int x, int y, int w, int h) : Fl_Widget(x, y, w, h, nullptr) { buf = new uchar[w * h * 3]; - delay = ((double)vkvm::getRedrawInterval())/1000; } /*A function to change the colors of the image-class. It reads the colors from the Shared Memory-Class*/ @@ -26,12 +27,4 @@ auto Image::getPixels() -> void { buf[(i * w() + j) * 3 + 2] = c.getBlue(); } } -} - -auto Image::getDelay() -> double { - return delay; -} - -auto Image::setDelay(double delay) -> void { - this->delay = delay; -} +} \ No newline at end of file diff --git a/src/Image.hpp b/src/Image.hpp new file mode 100644 index 0000000..17a8f96 --- /dev/null +++ b/src/Image.hpp @@ -0,0 +1,26 @@ +#ifndef GUI_IMAGE_HPP +#define GUI_IMAGE_HPP + + +#include +#include + +/** The constructor of the image class, get additional + * @param x: The mouse-position on the x-axis. + * @param y: The mouse-position on the y-axis: + * @param w: The width of a single pixel, + * @param h: The height of a single pixel. + */ + +class Image : public Fl_Widget { + uchar *buf; + +public: + Image(int x, int y, int w, int h); + + void draw() override; + + void getPixels(); +}; + +#endif diff --git a/src/Statusbar.cpp b/src/Statusbar.cpp index 694d84a..cbaf44e 100644 --- a/src/Statusbar.cpp +++ b/src/Statusbar.cpp @@ -1,4 +1,5 @@ -#include "GUI.hpp" +#include "GUI_Window.hpp" +#include "Statusbar.hpp" /** The constructor of Statusbar has to get additional parameters. @@ -7,8 +8,18 @@ * @param w: The width of a single pixel, * @param h: The height of a single pixel. */ -Statusbar::Statusbar(int x, int y, int w, int h, char *text) : - Fl_Box(x, y, w, h, text) { - this->text = text; -}; +Statusbar::Statusbar(int x, int y, int w, int h, char *text) : Fl_Box(x, y, w, h, text) { +} + +char *get_resolution(int window_width, int window_height) { + char *resolution = new char[25]; + std::string str_temp; + strcpy(resolution, "Resolution:"); + str_temp = std::to_string(window_width); + strcat(resolution, str_temp.c_str()); + strcat(resolution, " x "); + str_temp = std::to_string(window_height); + strcat(resolution, str_temp.c_str()); + return resolution; +} \ No newline at end of file diff --git a/src/Statusbar.hpp b/src/Statusbar.hpp new file mode 100644 index 0000000..fb52e31 --- /dev/null +++ b/src/Statusbar.hpp @@ -0,0 +1,21 @@ +// +// Created by joethei on 08.01.20. +// + +#ifndef GUI_STATUSBAR_HPP +#define GUI_STATUSBAR_HPP + +/** Statusbar + * The class inherits a char *text from the Fl_Box. It also shows the window-resolution and the mouse-position. + * @param text: A pointer to a char-array, that can contain a text like a status or whatever you want. + */ +class Statusbar : public Fl_Box { +public: + Statusbar(int x, int y, int w, int h, char *text); +}; + +char *get_resolution(int window_width, int window_height); + + + +#endif diff --git a/test/test_GUI.cpp b/test/test_GUI.cpp index 2010995..4ef4364 100644 --- a/test/test_GUI.cpp +++ b/test/test_GUI.cpp @@ -1,5 +1,5 @@ #include -#include "../src/GUI.hpp" +#include "../src/GUI_Window.hpp" TEST_CASE("GUI Test") { REQUIRE(42 == 42); From b552376075b794794c2c3b45e9f85d21ea23037b Mon Sep 17 00:00:00 2001 From: "@Nurullah.Damla" Date: Wed, 8 Jan 2020 13:13:18 +0100 Subject: [PATCH 2/4] add for MACOS --- CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3eb4a05..ff3f293 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,6 +8,8 @@ if ("${CMAKE_BINARY_DIR}" STREQUAL "${CMAKE_SOURCE_DIR}") endif() set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES "bin" "doc" "CMakeFiles" "lib" "include") +set(CMAKE_C_COMPILER "clang") +set(CMAKE_CXX_COMPILER "clang++") project(GUI) set(CMAKE_CXX_STANDARD 14) From d8ea12e5670cec0dede42d053d097669466f9593 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Wed, 8 Jan 2020 16:21:50 +0100 Subject: [PATCH 3/4] ~ trying to update refresh rate --- src/GUI.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/GUI.cpp b/src/GUI.cpp index c19d254..4688d11 100644 --- a/src/GUI.cpp +++ b/src/GUI.cpp @@ -39,6 +39,9 @@ int GUI_run(int argc, char **argv) { vkvm::registerEvent(vkvm::EventType::UpdateControlRegisters, [image, window, status]() { int newRedrawInterval = vkvm::getRedrawInterval(); if(newRedrawInterval != redrawInterval) { + /*if(redrawInterval == -1 && newRedrawInterval != -1) { + refreshCallback(image); + }*/ redrawInterval = newRedrawInterval; } @@ -66,7 +69,10 @@ static void refreshCallback(void * pointer) { auto *image = static_cast(pointer); image->getPixels(); - Fl::repeat_timeout((double) redrawInterval / 1000, redrawCallback, image); + if(redrawInterval != -1) { + Fl::repeat_timeout((double) redrawInterval / 1000, redrawCallback, image); + } + } static void redrawCallback(void * pointer) { From 6ae920c7b34b23b933d4191ce576288da9556ab7 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Wed, 8 Jan 2020 22:26:07 +0100 Subject: [PATCH 4/4] ~ disabling redrawing now possible ~ move statusbar elements a bit ~ use clang not g++ --- .gitlab-ci.yml | 4 ++-- src/GUI.cpp | 40 +++++++++++++++++++--------------------- src/GUI_Window.cpp | 7 ------- 3 files changed, 21 insertions(+), 30 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 2815013..85d35f3 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -31,7 +31,7 @@ make_test: - docker-ci script: - apt-get update - - apt-get install -y g++ make cmake clang-tidy libfltk1.3 libfltk1.3-dev + - apt-get install -y clang make cmake clang-tidy libfltk1.3 libfltk1.3-dev - mkdir current - ls | grep -v current | xargs mv -t current - git clone https://github.com/catchorg/Catch2.git @@ -57,7 +57,7 @@ cmake_build: - docker-ci script: - apt-get update - - apt-get install -y g++ make cmake clang-tidy libfltk1.3 libfltk1.3-dev + - apt-get install -y clang make cmake clang-tidy libfltk1.3 libfltk1.3-dev - mkdir current - ls | grep -v current | xargs mv -t current - git clone https://github.com/catchorg/Catch2.git diff --git a/src/GUI.cpp b/src/GUI.cpp index 4688d11..a686db0 100644 --- a/src/GUI.cpp +++ b/src/GUI.cpp @@ -13,25 +13,25 @@ int width; int height; static void redrawCallback(void * pointer); -static void refreshCallback(void * pointer); int GUI_run(int argc, char **argv) { vkvm::initialize(0); + //vkvm::setLogLevel(vkvm::LogLevel::DEBUG); width = vkvm::getWidth(); height = vkvm::getHeight(); redrawInterval = vkvm::getRedrawInterval(); - char *resolution = get_resolution(width, height); - std::cout << resolution << std::endl; - auto *window = new GUI_Window(width, height + 30, "vKVM GUI"); + char * resolution = get_resolution(width, height); + vkvm::log(vkvm::LogLevel::DEBUG, resolution); + auto * window = new GUI_Window(width, height + 30, "vKVM GUI"); Statusbar *status[5]; window->begin(); auto *image = new Image(0, 30, width, height); - status[0] = new Statusbar(0, 0, 300, 30, resolution); - status[1] = new Statusbar(300, 0, 170, 30, "Event:"); - status[2] = new Statusbar(470, 0, 200, 30, "Mouse Position:"); + status[0] = new Statusbar(0, 0, 200, 30, resolution); + status[1] = new Statusbar(200, 0, 170, 30, "Event:"); + status[2] = new Statusbar(400, 0, 170, 30, "Mouse Position:"); vkvm::registerEvent(vkvm::EventType::Redraw, [image]() { image->getPixels(); image->redraw(); @@ -39,9 +39,14 @@ int GUI_run(int argc, char **argv) { vkvm::registerEvent(vkvm::EventType::UpdateControlRegisters, [image, window, status]() { int newRedrawInterval = vkvm::getRedrawInterval(); if(newRedrawInterval != redrawInterval) { - /*if(redrawInterval == -1 && newRedrawInterval != -1) { - refreshCallback(image); - }*/ + if(redrawInterval == -1 && newRedrawInterval != -1) { + vkvm::log(vkvm::LogLevel::DEBUG, "adding timeout"); + Fl::add_timeout(1.0, redrawCallback, image); + } + if(newRedrawInterval == -1) { + vkvm::log(vkvm::LogLevel::DEBUG, "removing timeout"); + Fl::remove_timeout(redrawCallback); + } redrawInterval = newRedrawInterval; } @@ -59,24 +64,17 @@ int GUI_run(int argc, char **argv) { } }); - Fl::repeat_timeout((double) redrawInterval / 1000, refreshCallback, image); + Fl::add_timeout(1.0, redrawCallback, image); window->end(); window->show(argc, argv); return Fl::run(); } -static void refreshCallback(void * pointer) { +static void redrawCallback(void * pointer) { auto *image = static_cast(pointer); image->getPixels(); - if(redrawInterval != -1) { - Fl::repeat_timeout((double) redrawInterval / 1000, redrawCallback, image); - } - -} - -static void redrawCallback(void * pointer) { - auto *image = static_cast(pointer); image->redraw(); - refreshCallback(image); + Fl::repeat_timeout(redrawInterval / 1000.0, redrawCallback, image); + } \ No newline at end of file diff --git a/src/GUI_Window.cpp b/src/GUI_Window.cpp index 073bf17..c260d64 100644 --- a/src/GUI_Window.cpp +++ b/src/GUI_Window.cpp @@ -13,17 +13,14 @@ auto GUI_Window::handle(int e) -> int { mouse_status |= Mouse_Status::left_down; this->child(2)->label("Event:Mouse Left Down"); vkvm::callEvent(vkvm::EventType::MouseLeftDown); - // std::cout<child(2)->label("Event:Mouse Right Down"); vkvm::callEvent(vkvm::EventType::MouseRightDown); - // std::cout<child(2)->label("Event:Mouse Middle Down"); vkvm::callEvent(vkvm::EventType::MouseMiddleDown); - // std::cout< int { mouse_status &= Mouse_Status::left_up; this->child(2)->label("Event:Mouse Left Up"); vkvm::callEvent(vkvm::EventType::MouseLeftUp); - // std::cout<child(2)->label("Event:Mouse Right Up"); vkvm::callEvent(vkvm::EventType::MouseRightUp); - // std::cout<child(2)->label("Event:Mouse Middle Up"); vkvm::callEvent(vkvm::EventType::MouseMiddleUp); - // std::cout< int { return 1; case FL_KEYUP: button = Fl::event_button(); - std::cout << button << std::endl; keyCode = vkvm::KeyCode(button); vkvm::buttonPressed(keyCode); vkvm::callEvent(vkvm::EventType::KeyUp);