satdump/src-ui/main.cpp

337 lines
11 KiB
C++
Raw Normal View History

2021-02-17 22:25:03 +01:00
#include "logger.h"
#include "core/style.h"
2021-02-18 20:42:08 +01:00
#include "gl.h"
#include "logger.h"
#include <signal.h>
2021-02-18 21:15:33 +01:00
#include <GLFW/glfw3.h>
2021-02-19 00:03:52 +01:00
#include "imgui/imgui_flags.h"
2021-03-22 18:53:09 +01:00
#include "init.h"
#include "processing.h"
2021-05-04 12:23:22 +02:00
#include <filesystem>
2021-08-15 16:47:44 +02:00
#include "main_ui.h"
#include "satdump_vars.h"
2022-09-16 11:37:17 +02:00
2023-08-16 14:13:14 -04:00
#include "loading_screen.h"
2023-01-23 14:23:09 +01:00
#include "common/cli_utils.h"
2022-11-27 21:36:34 +01:00
#include "../src-core/resources.h"
2022-11-27 21:30:17 +01:00
2021-08-15 16:47:44 +02:00
extern bool recorder_running;
2021-02-17 21:13:36 +01:00
static void glfw_error_callback(int error, const char *description)
{
logger->error("Glfw Error " + std::to_string(error) + ": " + description);
}
2023-08-31 20:27:07 -04:00
void window_content_scale_callback(GLFWwindow*, float xscale, float)
{
2023-09-03 21:53:04 -04:00
satdump::updateUI(xscale / style::macos_framebuffer_scale());
style::setFonts();
ImGui_ImplOpenGL3_DestroyFontsTexture();
ImGui_ImplOpenGL3_CreateFontsTexture();
}
void bindImageTextureFunctions();
2022-09-17 18:42:46 +02:00
// OpenGL versions to try to start
2022-09-17 18:49:28 +02:00
// Yes, I did check on SDR++'s way around that
2022-09-17 19:04:35 +02:00
const int OPENGL_VERSIONS_MAJOR[] = {3, 3, 2};
const int OPENGL_VERSIONS_MINOR[] = {0, 1, 1};
const char *OPENGL_VERSIONS_GLSL[] = {"#version 150", "#version 300 es", "#version 120"};
2022-09-17 18:54:50 +02:00
const bool OPENGL_VERSIONS_GLES[] = {false, true, false, false};
2021-02-17 22:25:03 +01:00
int main(int argc, char *argv[])
{
bindImageTextureFunctions();
2021-02-18 20:42:08 +01:00
initLogger();
if (argc < 5) // Check overall command
2021-02-18 20:42:08 +01:00
{
logger->error("Usage : " + std::string(argv[0]) + " [downlink] [input_level] [input_file] [output_file_or_directory] [additional options as required]");
2022-02-22 19:55:11 +01:00
logger->error("Extra options (examples. Any parameter used in modules can be used here) :");
logger->error(" --samplerate [baseband_samplerate] --baseband_format [f32/i16/i8/w8] --dc_block --iq_swap");
2021-02-18 20:42:08 +01:00
}
2022-02-23 12:00:34 +01:00
else
2022-03-10 19:56:37 +01:00
satdump::processing::is_processing = true;
2021-02-18 20:42:08 +01:00
2022-03-15 16:07:47 +01:00
std::string downlink_pipeline = satdump::processing::is_processing ? argv[1] : "";
std::string input_level = satdump::processing::is_processing ? argv[2] : "";
std::string input_file = satdump::processing::is_processing ? argv[3] : "";
std::string output_file = satdump::processing::is_processing ? argv[4] : "";
2021-06-06 14:11:02 +02:00
2022-02-22 19:55:11 +01:00
// Parse flags
2022-03-20 20:56:32 +01:00
nlohmann::json parameters = satdump::processing::is_processing ? parse_common_flags(argc - 5, &argv[5]) : "";
2021-06-12 21:14:32 +02:00
2022-02-22 19:55:11 +01:00
// logger->warn("\n" + parameters.dump(4));
// exit(0);
2021-11-19 14:25:08 +01:00
2021-02-17 22:25:03 +01:00
// Setup window
glfwSetErrorCallback(glfw_error_callback);
if (!glfwInit())
2021-02-14 12:05:44 +01:00
{
2021-02-17 22:25:03 +01:00
logger->critical("Could not init GLFW");
2021-02-14 12:05:44 +01:00
exit(1);
}
2021-02-18 20:42:08 +01:00
// GL STUFF
2022-09-17 18:42:46 +02:00
// Initialize GLFW
GLFWwindow *window = nullptr;
int final_gl_version = 0;
2023-08-31 20:27:07 -04:00
#if GLFW_VERSION_MAJOR > 3 || (GLFW_VERSION_MAJOR == 3 && GLFW_VERSION_MINOR >= 3)
glfwWindowHint(GLFW_SCALE_TO_MONITOR, GLFW_TRUE);
2023-08-31 20:27:07 -04:00
#endif
2022-09-17 18:42:46 +02:00
#ifdef __APPLE__
2021-02-17 22:25:03 +01:00
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 3.2+ only
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // Required on Mac
2021-02-14 12:05:44 +01:00
2022-09-17 18:42:46 +02:00
final_gl_version = 0;
window = glfwCreateWindow(1000, 600, std::string("SatDump v" + (std::string)SATDUMP_VERSION).c_str(), NULL, NULL);
if (window == NULL)
{
logger->critical("Could not init GLFW Window");
exit(1);
}
2022-11-29 15:25:20 +01:00
#elif defined(_WIN32)
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 3.2+ only
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // Required on Mac
final_gl_version = 0;
window = glfwCreateWindow(1000, 600, std::string("SatDump v" + (std::string)SATDUMP_VERSION).c_str(), NULL, NULL);
if (window == NULL) // Try 2.1. We assume it's the only available if 3.2 does not work
2021-02-14 12:05:44 +01:00
{
logger->warn("Could not init GLFW Window. Trying GL 2.1");
2022-12-03 01:30:16 +01:00
glfwDefaultWindowHints();
window = glfwCreateWindow(1000, 600, std::string("SatDump v" + (std::string)SATDUMP_VERSION).c_str(), NULL, NULL);
final_gl_version = 2;
if (window == NULL)
{
logger->critical("Could not init GLFW Window");
exit(1);
}
2021-02-14 12:05:44 +01:00
}
2022-09-17 18:42:46 +02:00
#else
2022-09-17 19:04:35 +02:00
const char *gl_override_rpi = getenv("MESA_GL_VERSION_OVERRIDE");
2022-09-17 19:05:53 +02:00
if (gl_override_rpi != nullptr)
2022-09-17 18:42:46 +02:00
{
2022-09-17 19:20:39 +02:00
if (std::string(gl_override_rpi).find("4.5") != std::string::npos)
2022-09-17 19:04:35 +02:00
{
2022-09-17 19:05:53 +02:00
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_API);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 3.2+ only
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // Required on Mac
// Create window with graphics context
window = glfwCreateWindow(1000, 600, std::string("SatDump v" + (std::string)SATDUMP_VERSION).c_str(), NULL, NULL);
final_gl_version = 2;
if (window == NULL)
{
2023-05-08 23:08:34 +02:00
logger->critical("Could not init GLFW Window with OpenGL 3.2 with %s", gl_override_rpi);
2022-09-17 19:05:53 +02:00
exit(1);
}
}
else
{
goto normal_gl;
2022-09-17 19:04:35 +02:00
}
}
else
{
2022-09-17 19:05:53 +02:00
normal_gl:
2022-09-17 19:04:35 +02:00
for (int i = 0; i < 4; i++)
{
glfwWindowHint(GLFW_CLIENT_API, OPENGL_VERSIONS_GLES[i] ? GLFW_OPENGL_ES_API : GLFW_OPENGL_API);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, OPENGL_VERSIONS_MAJOR[i]);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, OPENGL_VERSIONS_MINOR[i]);
if (OPENGL_VERSIONS_MAJOR[i] >= 3 && OPENGL_VERSIONS_MINOR[i] >= 2)
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 3.2+ only
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // Required on Mac
// Create window with graphics context
window = glfwCreateWindow(1000, 600, std::string("SatDump v" + (std::string)SATDUMP_VERSION).c_str(), NULL, NULL);
final_gl_version = i;
if (window == NULL)
2023-05-08 23:08:34 +02:00
logger->critical("Could not init GLFW Window with OpenGL %d.%d", OPENGL_VERSIONS_MAJOR[i], OPENGL_VERSIONS_MINOR[i]);
2022-09-17 19:04:35 +02:00
else
break;
}
2022-09-17 18:42:46 +02:00
}
#endif
2021-02-14 12:05:44 +01:00
2021-02-17 22:25:03 +01:00
glfwMakeContextCurrent(window);
2023-09-28 12:42:23 -04:00
glfwSwapInterval(0); // Disable vsync on loading screen - not needed since frames are only pushed on log updates, and not in a loop
// Vsync slows down init process when items are logged quickly
2021-02-17 22:25:03 +01:00
if (glewInit() != GLEW_OK)
{
logger->critical("Failed to initialize OpenGL loader!");
exit(1);
}
2021-02-17 20:07:49 +01:00
2021-02-17 22:25:03 +01:00
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO &io = ImGui::GetIO();
(void)io;
io.IniFilename = NULL;
2021-02-16 13:47:53 +01:00
2023-05-08 23:08:34 +02:00
logger->debug("Starting with OpenGL %s", (char *)glGetString(GL_VERSION));
2022-09-17 18:11:23 +02:00
2021-02-17 22:25:03 +01:00
// Setup Platform/Renderer bindings
ImGui_ImplGlfw_InitForOpenGL(window, true);
2022-09-17 18:42:46 +02:00
ImGui_ImplOpenGL3_Init(OPENGL_VERSIONS_GLSL[final_gl_version]);
2021-02-16 13:47:53 +01:00
2022-09-17 20:40:57 +02:00
// Setup Icon
GLFWimage img;
2022-09-16 12:46:13 -07:00
{
image::Image<uint8_t> image;
image.load_png(resources::getResourcePath("icon.png"));
uint8_t *px = new uint8_t[image.width() * image.height() * 4];
2022-09-17 20:40:57 +02:00
memset(px, 255, image.width() * image.height() * 4);
2022-09-16 12:46:13 -07:00
img.height = image.height();
img.width = image.width();
2023-08-11 18:16:23 +02:00
if (image.channels() == 4)
{
for (int y = 0; y < (int)image.height(); y++)
for (int x = 0; x < (int)image.width(); x++)
for (int c = 0; c < 4; c++)
px[image.width() * 4 * y + x * 4 + c] = image.channel(c)[image.width() * y + x];
}
else if (image.channels() == 3)
{
for (int y = 0; y < (int)image.height(); y++)
for (int x = 0; x < (int)image.width(); x++)
for (int c = 0; c < 3; c++)
px[image.width() * 4 * y + x * 4 + c] = image.channel(c)[image.width() * y + x];
}
2022-09-16 12:46:13 -07:00
image.clear();
img.pixels = px;
}
2023-09-18 16:15:44 -04:00
#ifndef _WIN32
2022-09-17 20:40:57 +02:00
glfwSetWindowIcon(window, 1, &img);
2023-09-18 16:15:44 -04:00
#endif
2022-09-16 11:37:17 +02:00
//Handle DPI changes
float display_scale;
2023-08-31 20:27:07 -04:00
#if GLFW_VERSION_MAJOR > 3 || (GLFW_VERSION_MAJOR == 3 && GLFW_VERSION_MINOR >= 3)
glfwSetWindowContentScaleCallback(window, window_content_scale_callback);
glfwGetWindowContentScale(window, &display_scale, nullptr);
2023-09-03 21:53:04 -04:00
display_scale /= style::macos_framebuffer_scale();
2023-08-31 20:27:07 -04:00
#else
display_scale = 1.0f;
#endif
2023-08-16 14:13:14 -04:00
//Set font
style::setFonts(display_scale);
2023-08-16 14:13:14 -04:00
// Init Loading Screen
std::shared_ptr<satdump::LoadingScreenSink> loading_screen_sink = std::make_shared<satdump::LoadingScreenSink>(window, display_scale, &img);
2023-08-16 14:13:14 -04:00
logger->add_sink(loading_screen_sink);
// Init SatDump
satdump::tle_do_update_on_init = false;
satdump::initSatdump();
2022-03-30 15:41:23 +02:00
// Init UI
satdump::initMainUI(display_scale);
2023-08-16 21:35:34 -04:00
//Shut down loading screen
logger->del_sink(loading_screen_sink);
loading_screen_sink.reset();
2023-09-28 12:42:23 -04:00
glfwSwapInterval(1); // Enable vsync for the rest of the program
2023-08-16 21:35:34 -04:00
//Set font again to adjust for DPI
style::setFonts();
ImGui_ImplOpenGL3_DestroyFontsTexture();
ImGui_ImplOpenGL3_CreateFontsTexture();
2022-03-10 19:56:37 +01:00
if (satdump::processing::is_processing)
2022-03-15 16:07:47 +01:00
{
2022-03-10 19:56:37 +01:00
satdump::ui_thread_pool.push([&](int)
{ satdump::processing::process(downlink_pipeline, input_level, input_file, output_file, parameters); });
2022-03-15 16:07:47 +01:00
}
2021-02-18 20:42:08 +01:00
2023-01-23 14:23:09 +01:00
// TLE
if (satdump::config::main_cfg["satdump_general"]["update_tles_startup"]["value"].get<bool>() || satdump::general_tle_registry.size() == 0)
satdump::ui_thread_pool.push([&](int)
{ satdump::updateTLEFile(satdump::user_path + "/satdump_tles.txt");
satdump::loadTLEFileIntoRegistry(satdump::user_path + "/satdump_tles.txt"); });
2021-02-17 22:25:03 +01:00
// Main loop
2023-01-22 23:40:56 +01:00
do
2021-02-17 22:25:03 +01:00
{
// Start the Dear ImGui frame
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
2021-02-16 13:47:53 +01:00
2021-02-17 22:25:03 +01:00
int wwidth, wheight;
glfwGetWindowSize(window, &wwidth, &wheight);
2023-08-11 18:16:23 +02:00
// std::cout<<wwidth<<std::endl;
2021-02-16 13:47:53 +01:00
2021-02-17 22:25:03 +01:00
// User rendering
2022-03-10 19:56:37 +01:00
satdump::renderMainUI(wwidth, wheight);
2021-02-14 12:05:44 +01:00
2021-02-17 22:25:03 +01:00
// Rendering
ImGui::Render();
int display_w, display_h;
glfwGetFramebufferSize(window, &display_w, &display_h);
glViewport(0, 0, display_w, display_h);
2021-02-14 12:05:44 +01:00
2022-03-30 15:41:23 +02:00
if (satdump::light_theme)
2021-06-12 21:14:32 +02:00
glClearColor(0.7f, 0.7f, 0.7f, 1.0f);
else
glClearColor(0.0666f, 0.0666f, 0.0666f, 1.0f);
2022-02-22 19:55:11 +01:00
// glClearColor(0.9f, 0.9f, 0.9f, 1.0f);
2021-02-17 22:25:03 +01:00
glClear(GL_COLOR_BUFFER_BIT);
2021-02-17 22:25:03 +01:00
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
2021-02-16 13:47:53 +01:00
2021-02-17 22:25:03 +01:00
glfwSwapBuffers(window);
glfwPollEvents();
2023-01-22 23:40:56 +01:00
} while (!glfwWindowShouldClose(window));
2021-02-14 12:05:44 +01:00
2022-05-08 16:35:54 +02:00
satdump::exitMainUI();
2021-02-17 22:25:03 +01:00
// Cleanup
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
glfwDestroyWindow(window);
glfwTerminate();
logger->info("UI Exit");
// If we're doing live processing, we want this to kill all threads quickly. Hence don't call destructors
2022-03-10 19:56:37 +01:00
if (satdump::processing::is_processing)
2021-11-22 00:30:10 +01:00
#ifdef __APPLE__
2021-11-22 00:47:00 +01:00
exit(0);
2021-11-22 00:30:10 +01:00
#else
quick_exit(0);
2021-11-22 00:30:10 +01:00
#endif
2022-03-10 19:56:37 +01:00
satdump::ui_thread_pool.stop();
2021-02-24 12:58:30 +01:00
2022-03-10 19:56:37 +01:00
for (int i = 0; i < satdump::ui_thread_pool.size(); i++)
{
2022-03-10 19:56:37 +01:00
if (satdump::ui_thread_pool.get_thread(i).joinable())
satdump::ui_thread_pool.get_thread(i).join();
}
2021-05-05 15:52:12 +02:00
logger->info("Exiting!");
}