From 7759de96da9ecb1bef242bd4e6b6a3366edfafcf Mon Sep 17 00:00:00 2001 From: Ryzerth Date: Fri, 7 Aug 2020 14:29:06 +0200 Subject: [PATCH] added module system --- modules/demo/CMakeLists.txt | 55 +++++++++++++++++++++++++++++++++++++ modules/demo/src/main.cpp | 26 ++++++++++++++++++ src/main.cpp | 6 ++++ src/main_window.cpp | 25 ++++++----------- src/main_window.h | 17 ++++++++++++ src/module.cpp | 50 +++++++++++++++++++++++++++++++++ src/module.h | 38 +++++++++++++++++++++++++ src/version.h | 2 +- src/waterfall.cpp | 3 -- src/waterfall.h | 1 + 10 files changed, 203 insertions(+), 20 deletions(-) create mode 100644 modules/demo/CMakeLists.txt create mode 100644 modules/demo/src/main.cpp create mode 100644 src/module.cpp create mode 100644 src/module.h diff --git a/modules/demo/CMakeLists.txt b/modules/demo/CMakeLists.txt new file mode 100644 index 00000000..1b8ac869 --- /dev/null +++ b/modules/demo/CMakeLists.txt @@ -0,0 +1,55 @@ +cmake_minimum_required(VERSION 3.9) +project(demo) + +if (MSVC) + set(CMAKE_CXX_FLAGS "-O2 /std:c++17") + link_directories(demo "C:/Program Files/PothosSDR/lib/") + include_directories(demo "C:/Program Files/PothosSDR/include/volk/") + include_directories(demo "C:/Program Files/PothosSDR/include/") +else() + set(CMAKE_CXX_FLAGS "-O3 -std=c++17 -fsanitize=address -g") + include_directories(demo "/usr/include/volk") + link_libraries(pthread) + link_libraries(GL) + link_libraries(GLEW) + link_libraries(glfw) + link_libraries(fftw3) + link_libraries(fftw3f) + link_libraries(portaudio) + link_libraries(X11) + link_libraries(Xxf86vm) +endif (MSVC) + +link_libraries(volk) +link_libraries(SoapySDR) + +# Main code +include_directories(demo "src/") +include_directories(demo "../../src/") +include_directories(demo "../../src/imgui") +file(GLOB SRC "src/*.cpp") +file(GLOB IMGUI "../../src/imgui/*.cpp") +add_library(demo SHARED ${SRC} ${IMGUI}) +set_target_properties(demo PROPERTIES OUTPUT_NAME demo) + +if (MSVC) + # Glew + find_package(GLEW REQUIRED) + target_link_libraries(demo PRIVATE GLEW::GLEW) + + # GLFW3 + find_package(glfw3 CONFIG REQUIRED) + target_link_libraries(demo PRIVATE glfw) + + # FFTW3 + find_package(FFTW3 CONFIG REQUIRED) + target_link_libraries(demo PRIVATE FFTW3::fftw3) + find_package(FFTW3f CONFIG REQUIRED) + target_link_libraries(demo PRIVATE FFTW3::fftw3f) + + # PortAudio + find_package(portaudio CONFIG REQUIRED) + target_link_libraries(demo PRIVATE portaudio portaudio_static) +endif (MSVC) + +# cmake .. "-DCMAKE_TOOLCHAIN_FILE=C:/Users/Alex/vcpkg/scripts/buildsystems/vcpkg.cmake" -G "Visual Studio 15 2017 Win64" \ No newline at end of file diff --git a/modules/demo/src/main.cpp b/modules/demo/src/main.cpp new file mode 100644 index 00000000..f6892fe0 --- /dev/null +++ b/modules/demo/src/main.cpp @@ -0,0 +1,26 @@ +#include +#include + +mod::API_t* API; + +struct DemoContext_t { + std::string name; +}; + +MOD_EXPORT void* _INIT_(mod::API_t* _API, ImGuiContext* imctx, std::string _name) { + API = _API; + DemoContext_t* ctx = new DemoContext_t; + ctx->name = _name; + ImGui::SetCurrentContext(imctx); + return ctx; +} + +MOD_EXPORT void _DRAW_MENU_(DemoContext_t* ctx) { + char buf[100]; + sprintf(buf, "I'm %s", ctx->name.c_str()); + ImGui::Button(buf); +} + +MOD_EXPORT void _STOP_(DemoContext_t* ctx) { + delete ctx; +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index dab78f88..29c7323a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #ifdef _WIN32 #include @@ -72,6 +73,11 @@ int main() { spdlog::info("Loading band plans color table"); bandplan::loadColorTable("band_colors.json"); + spdlog::info("Loading test module"); + //mod::loadModule("../modules/demo/build/Release/demo.dll", "Demo Module 1"); + //mod::loadModule("../modules/demo/build/Release/demo.dll", "Demo Module 2"); + //mod::loadModule("../modules/demo/build/Release/demo.dll", "Demo Module 3"); + spdlog::info("Ready."); // Main loop diff --git a/src/main_window.cpp b/src/main_window.cpp index 1d9488f5..b70f9e21 100644 --- a/src/main_window.cpp +++ b/src/main_window.cpp @@ -1,20 +1,4 @@ #include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include std::thread worker; std::mutex fft_mtx; @@ -304,6 +288,15 @@ void drawWindow() { } } + int modCount = mod::moduleNames.size(); + mod::Module_t mod; + for (int i = 0; i < modCount; i++) { + if (ImGui::CollapsingHeader(mod::moduleNames[i].c_str())) { + mod = mod::modules[mod::moduleNames[i]]; + mod._DRAW_MENU_(mod.ctx); + } + } + if (ImGui::CollapsingHeader("Radio")) { ImGui::BeginGroup(); diff --git a/src/main_window.h b/src/main_window.h index 33b8db87..63be64d3 100644 --- a/src/main_window.h +++ b/src/main_window.h @@ -5,6 +5,23 @@ #include #include #include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #define WINDOW_FLAGS ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoBackground diff --git a/src/module.cpp b/src/module.cpp new file mode 100644 index 00000000..704790ec --- /dev/null +++ b/src/module.cpp @@ -0,0 +1,50 @@ +#include + +namespace mod { + API_t API; + std::map modules; + std::vector moduleNames; + + void loadModule(std::string path, std::string name) { + if (!std::filesystem::exists(path)) { + spdlog::error("{0} does not exist", path); + return; + } + if (!std::filesystem::is_regular_file(path)) { + spdlog::error("{0} isn't a loadable module", path); + return; + } + Module_t mod; +#ifdef _WIN32 + mod.inst = LoadLibraryA(path.c_str()); + if (mod.inst == NULL) { + spdlog::error("Couldn't load {0}.", name); + return; + } + mod._INIT_ = (void*(*)(mod::API_t*,ImGuiContext*,std::string))GetProcAddress(mod.inst, "_INIT_"); + mod._DRAW_MENU_ = (void(*)(void*))GetProcAddress(mod.inst, "_DRAW_MENU_"); + mod._STOP_ = (void(*)(void*))GetProcAddress(mod.inst, "_STOP_"); +#else + // Linux function here +#endif + if (mod._INIT_ == NULL) { + spdlog::error("Couldn't load {0} because it's missing _INIT_.", name); + return; + } + if (mod._DRAW_MENU_ == NULL) { + spdlog::error("Couldn't load {0} because it's missing _DRAW_MENU_.", name); + return; + } + if (mod._STOP_ == NULL) { + spdlog::error("Couldn't load {0} because it's missing _STOP_.", name); + return; + } + mod.ctx = mod._INIT_(&API, ImGui::GetCurrentContext(), name); + if (mod.ctx == NULL) { + spdlog::error("{0} Failed to initialize.", name); + } + modules[name] = mod; + moduleNames.push_back(name); + } +}; + diff --git a/src/module.h b/src/module.h new file mode 100644 index 00000000..46f4ca36 --- /dev/null +++ b/src/module.h @@ -0,0 +1,38 @@ +#pragma once +#include +#include +#include +#include +#include +#include + +#ifdef _WIN32 +#include +#define MOD_EXPORT extern "C" \ + __declspec(dllexport) +#else +#define MOD_EXPORT extern "C" +#endif + +namespace mod { + struct API_t { + + }; + + struct Module_t { +#ifdef _WIN32 + HINSTANCE inst; +#else + void* inst; +#endif + void* (*_INIT_)(API_t*, ImGuiContext*, std::string); + void (*_DRAW_MENU_)(void*); + void (*_STOP_)(void*); + void* ctx; + }; + + void loadModule(std::string path, std::string name); + + extern std::map modules; + extern std::vector moduleNames; +}; \ No newline at end of file diff --git a/src/version.h b/src/version.h index abb9a98e..c9e3de76 100644 --- a/src/version.h +++ b/src/version.h @@ -1,3 +1,3 @@ #pragma once -#define VERSION_STR "0.2.4_alpha" \ No newline at end of file +#define VERSION_STR "0.2.5_alpha" \ No newline at end of file diff --git a/src/waterfall.cpp b/src/waterfall.cpp index 1500a49d..ab2124c7 100644 --- a/src/waterfall.cpp +++ b/src/waterfall.cpp @@ -1,6 +1,4 @@ #include -#include - float COLOR_MAP[][3] = { {0x00, 0x00, 0x20}, @@ -20,7 +18,6 @@ float COLOR_MAP[][3] = { void doZoom(int offset, int width, int outWidth, std::vector data, float* out) { // NOTE: REMOVE THAT SHIT, IT'S JUST A HACKY FIX - if (offset < 0) { offset = 0; } diff --git a/src/waterfall.h b/src/waterfall.h index 362125c4..4edd907c 100644 --- a/src/waterfall.h +++ b/src/waterfall.h @@ -6,6 +6,7 @@ #include #include #include +#include #define WATERFALL_RESOLUTION 1000000