2023-12-20 14:00:46 -05:00
# include "backend.h"
2026-01-28 20:27:21 +01:00
# include "cli/open.h"
2025-12-29 17:17:08 +01:00
# include "cli/recstart.h"
2025-04-15 19:06:12 +01:00
# include "common/cli_utils.h"
# include "common/detect_header.h"
2025-12-08 17:50:52 +01:00
# include "core/cli/cli.h"
2025-05-31 20:26:47 +02:00
# include "core/plugin.h"
# include "core/resources.h"
2022-05-02 18:59:03 +02:00
# include "core/style.h"
2025-05-03 12:12:36 +02:00
# include "imgui/imgui.h"
2025-04-15 19:06:12 +01:00
# include "imgui/imgui_filedrop.h"
# include "imgui/imgui_image.h"
2021-03-22 18:53:09 +01:00
# include "init.h"
2023-12-31 11:11:36 -05:00
# include "loader/loader.h"
2025-04-15 19:06:12 +01:00
# include "logger.h"
# include "main_ui.h"
2025-05-03 12:12:36 +02:00
# include "nfd.h"
2025-07-08 17:42:18 +01:00
// #include "processing.h"
2025-12-29 17:17:08 +01:00
# include "recorder/recorder.h"
2025-01-20 11:01:28 +01:00
# include "satdump_vars.h"
2025-07-26 10:46:03 +02:00
# include <GLFW/glfw3.h>
2025-05-31 20:26:47 +02:00
# include <exception>
2025-04-15 19:06:12 +01:00
# include <filesystem>
# include <signal.h>
# include <string>
# include <vector>
2022-11-27 21:30:17 +01:00
2023-12-20 14:00:46 -05:00
GLFWwindow * window ;
2023-10-16 16:18:37 -04:00
static volatile bool signal_caught = false ;
2023-11-06 09:17:59 -05:00
bool fallback_gl = false ;
2023-10-16 16:18:37 -04:00
void sig_handler_ui ( int signo )
{
2023-11-06 15:46:35 -05:00
if ( signo = = SIGINT | | signo = = SIGTERM )
2023-10-16 16:18:37 -04:00
signal_caught = true ;
}
2021-08-15 16:47:44 +02:00
2025-04-15 19:06:12 +01:00
static void glfw_error_callback ( int error , const char * description ) { logger - > error ( " Glfw Error " + std : : to_string ( error ) + " : " + description ) ; }
2021-02-17 21:13:36 +01:00
2023-11-08 15:09:07 +01:00
void window_content_scale_callback ( GLFWwindow * , float xscale , float )
2023-08-23 15:45:10 -04:00
{
2024-01-21 17:30:00 -05:00
backend : : device_scale = xscale / style : : macos_framebuffer_scale ( ) ;
satdump : : update_ui = true ;
2023-08-23 15:45:10 -04:00
}
2023-11-13 09:00:28 -05:00
void bindBaseTextureFunctions ( ) ;
void bindAdvancedTextureFunctions ( ) ;
2022-09-17 18:42:46 +02:00
2025-04-15 19:06:12 +01:00
void glfw_drop_callback ( GLFWwindow * window , int count , const char * * paths )
{
std : : vector < std : : string > files ;
for ( int i = 0 ; i < count ; i + + )
files . push_back ( std : : string ( paths [ i ] ) ) ;
satdump : : eventBus - > fire_event < satdump : : imgui_utils : : FileDropEvent > ( { files } ) ;
}
2025-07-14 13:28:49 +02:00
# ifdef _WIN32
2025-07-14 13:27:09 +02:00
# define WINVER 0x0501 // Allow use of features specific to Windows XP or later.
# define _WIN32_WINNT 0x0501
# define WIN32_LEAN_AND_MEAN
# include "fcntl.h"
# include "io.h"
# include "stdio.h"
# include "stdlib.h"
# include "windows.h"
# pragma comment(lib, "User32.lib")
// https://www.tillett.info/2013/05/13/how-to-create-a-windows-program-that-works-as-both-as-a-gui-and-console-application/
// Attach output of application to parent console
static BOOL attachOutputToConsole ( void )
{
HANDLE consoleHandleOut , consoleHandleError ;
if ( AttachConsole ( ATTACH_PARENT_PROCESS ) )
{
// Redirect unbuffered STDOUT to the console
consoleHandleOut = GetStdHandle ( STD_OUTPUT_HANDLE ) ;
if ( consoleHandleOut ! = INVALID_HANDLE_VALUE )
{
freopen ( " CONOUT$ " , " w " , stdout ) ;
setvbuf ( stdout , NULL , _IONBF , 0 ) ;
}
else
{
return FALSE ;
}
// Redirect unbuffered STDERR to the console
consoleHandleError = GetStdHandle ( STD_ERROR_HANDLE ) ;
if ( consoleHandleError ! = INVALID_HANDLE_VALUE )
{
freopen ( " CONOUT$ " , " w " , stderr ) ;
setvbuf ( stderr , NULL , _IONBF , 0 ) ;
}
else
{
return FALSE ;
}
return TRUE ;
}
// Not a console application
return FALSE ;
}
# endif
2025-05-03 12:12:36 +02:00
2021-02-17 22:25:03 +01:00
int main ( int argc , char * argv [ ] )
{
2025-07-14 13:28:49 +02:00
# ifdef _WIN32
2025-07-14 13:27:09 +02:00
attachOutputToConsole ( ) ;
# endif
2021-02-18 20:42:08 +01:00
initLogger ( ) ;
2022-02-22 19:55:11 +01:00
// logger->warn("\n" + parameters.dump(4));
// exit(0);
2021-11-19 14:25:08 +01:00
2025-07-27 17:13:13 +02:00
# if defined(__linux__) // Wayland GLFW support is frankly broken at the moment, so force X11
2025-07-28 20:33:25 +02:00
# if defined(GLFW_PLATFORM_X11)
2025-07-26 10:46:03 +02:00
glfwInitHint ( GLFW_PLATFORM , GLFW_PLATFORM_X11 ) ;
2025-07-28 20:33:25 +02:00
# endif
2025-07-26 10:46:03 +02:00
# endif
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
2023-08-31 20:27:07 -04:00
# if GLFW_VERSION_MAJOR > 3 || (GLFW_VERSION_MAJOR == 3 && GLFW_VERSION_MINOR >= 3)
2023-08-23 15:45:10 -04:00
glfwWindowHint ( GLFW_SCALE_TO_MONITOR , GLFW_TRUE ) ;
2023-08-31 20:27:07 -04:00
# endif
2022-09-17 18:42:46 +02:00
2025-06-23 00:14:50 +02:00
# if GLFW_VERSION_MAJOR > 3 || (GLFW_VERSION_MAJOR == 3 && GLFW_VERSION_MINOR >= 4)
glfwWindowHintString ( GLFW_WAYLAND_APP_ID , " satdump " ) ;
# endif
2023-11-03 15:03:13 -04:00
// Decide GL+GLSL versions
2023-11-08 15:09:07 +01:00
const char * OPENGL_VERSIONS_GLSL [ ] = { " #version 100 " , " #version 130 " , " #version 150 " } ;
2023-11-04 15:01:06 -04:00
int selected_glsl ;
2023-11-03 15:03:13 -04:00
# if defined(IMGUI_IMPL_OPENGL_ES2)
// GL ES 2.0 + GLSL 100
2023-11-04 15:01:06 -04:00
selected_glsl = 0 ;
2023-11-03 15:03:13 -04:00
glfwWindowHint ( GLFW_CONTEXT_VERSION_MAJOR , 2 ) ;
glfwWindowHint ( GLFW_CONTEXT_VERSION_MINOR , 0 ) ;
glfwWindowHint ( GLFW_CLIENT_API , GLFW_OPENGL_ES_API ) ;
# elif defined(__APPLE__)
// GL 3.2 + GLSL 150
2023-11-06 09:17:59 -05:00
selected_glsl = 2 ;
2021-02-17 22:25:03 +01:00
glfwWindowHint ( GLFW_CONTEXT_VERSION_MAJOR , 3 ) ;
glfwWindowHint ( GLFW_CONTEXT_VERSION_MINOR , 2 ) ;
2023-11-08 15:09:07 +01:00
glfwWindowHint ( GLFW_OPENGL_PROFILE , GLFW_OPENGL_CORE_PROFILE ) ; // 3.2+ only
glfwWindowHint ( GLFW_OPENGL_FORWARD_COMPAT , GL_TRUE ) ; // Required on Mac
2023-11-03 15:03:13 -04:00
# else
// GL 3.0 + GLSL 130
2023-11-06 09:17:59 -05:00
selected_glsl = 1 ;
2023-11-03 15:03:13 -04:00
glfwWindowHint ( GLFW_CONTEXT_VERSION_MAJOR , 3 ) ;
glfwWindowHint ( GLFW_CONTEXT_VERSION_MINOR , 0 ) ;
2023-11-08 15:09:07 +01:00
// glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 3.2+ only
// glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // 3.0+ only
2023-11-03 15:03:13 -04:00
# endif
2022-09-17 18:42:46 +02:00
2025-06-01 14:28:04 +02:00
window = glfwCreateWindow ( 1000 , 600 , satdump : : getSatDumpVersionName ( ) . c_str ( ) , nullptr , nullptr ) ;
2023-11-03 15:03:13 -04:00
if ( window = = nullptr )
2022-11-29 00:56:03 +01:00
{
2023-11-04 15:01:06 -04:00
logger - > warn ( " Could not init GLFW Window; falling back to OpenGL 2.1... " ) ;
glfwDefaultWindowHints ( ) ;
2023-11-06 09:17:59 -05:00
fallback_gl = true ;
2025-01-20 11:01:28 +01:00
window = glfwCreateWindow ( 1000 , 600 , std : : string ( " SatDump v " + ( std : : string ) satdump : : SATDUMP_VERSION ) . c_str ( ) , nullptr , nullptr ) ;
2023-11-04 15:01:06 -04:00
if ( window = = nullptr )
{
2025-05-03 12:12:36 +02:00
// TODOREWORK bring this back pfd::message("SatDump", "Could not start SatDump UI. Please make sure your graphics card supports OpenGL 2.1 or newer", pfd::choice::ok, pfd::icon::error);
2023-11-04 15:01:06 -04:00
logger - > critical ( " Could not init GLFW Window! Exiting " ) ;
exit ( 1 ) ;
}
2022-11-29 00:56:03 +01:00
}
2021-02-14 12:05:44 +01:00
2025-04-15 19:06:12 +01:00
glfwSetDropCallback ( window , glfw_drop_callback ) ;
2021-02-17 22:25:03 +01:00
glfwMakeContextCurrent ( window ) ;
2025-04-15 19:06:12 +01: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 20:07:49 +01:00
2023-12-20 14:00:46 -05:00
// Bind texture/backend functions
bindBackendFunctions ( ) ;
2023-11-13 09:00:28 -05:00
bindBaseTextureFunctions ( ) ;
2023-11-12 23:55:41 -05:00
# ifndef IMGUI_IMPL_OPENGL_ES2
2023-11-12 21:48:43 -05:00
if ( ! fallback_gl )
2023-11-13 09:00:28 -05:00
bindAdvancedTextureFunctions ( ) ;
2023-11-12 23:55:41 -05:00
# endif
2023-11-12 19:34:01 -05: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 ) ) ;
2024-01-16 14:10:14 -05:00
logger - > debug ( " Max texture size: %zu " , maxTextureSize ) ;
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 ) ;
2023-11-06 11:39:38 -05:00
# ifndef IMGUI_IMPL_OPENGL_ES2
2023-11-06 09:17:59 -05:00
if ( fallback_gl )
ImGui_ImplOpenGL2_Init ( ) ;
else
2023-11-06 11:39:38 -05:00
# endif
2023-11-06 09:17:59 -05:00
ImGui_ImplOpenGL3_Init ( OPENGL_VERSIONS_GLSL [ selected_glsl ] ) ;
2021-02-16 13:47:53 +01:00
2023-11-08 15:09:07 +01:00
// Handle DPI changes
2023-08-31 20:27:07 -04:00
# if GLFW_VERSION_MAJOR > 3 || (GLFW_VERSION_MAJOR == 3 && GLFW_VERSION_MINOR >= 3)
2023-08-23 15:45:10 -04:00
glfwSetWindowContentScaleCallback ( window , window_content_scale_callback ) ;
2023-08-31 20:27:07 -04:00
# endif
2023-08-23 15:45:10 -04:00
2023-11-08 15:09:07 +01:00
// Set font
2024-01-21 17:30:00 -05:00
style : : setFonts ( backend : : device_scale ) ;
2023-08-16 14:13:14 -04:00
// Init Loading Screen
2024-01-21 17:30:00 -05:00
std : : shared_ptr < satdump : : LoadingScreenSink > loading_screen_sink = std : : make_shared < satdump : : LoadingScreenSink > ( ) ;
2023-08-16 14:13:14 -04:00
logger - > add_sink ( loading_screen_sink ) ;
2025-12-08 17:50:52 +01:00
// Basic flags
bool verbose = satdump : : cli : : checkVerbose ( argc , argv ) ;
// Init SatDump, silent or verbose as requested
if ( ! verbose )
logger - > set_level ( slog : : LOG_WARN ) ;
2023-08-16 14:13:14 -04:00
satdump : : tle_do_update_on_init = false ;
2024-10-07 22:05:30 -04:00
satdump : : initSatdump ( true ) ;
2025-12-08 17:50:52 +01:00
if ( ! verbose )
logger - > set_level ( slog : : LOG_TRACE ) ;
2023-08-16 14:13:14 -04:00
2025-12-29 17:17:08 +01:00
// Register our own commands
satdump : : eventBus - > register_handler < satdump : : cli : : RegisterSubcommandEvent > (
[ ] ( const satdump : : cli : : RegisterSubcommandEvent & evt )
{
if ( evt . is_gui )
2026-01-28 20:27:21 +01:00
{
2025-12-29 17:17:08 +01:00
evt . cmd_handlers . push_back ( std : : make_shared < satdump : : RecStartCmdHandler > ( ) ) ;
2026-01-28 20:27:21 +01:00
evt . cmd_handlers . push_back ( std : : make_shared < satdump : : OpenCmdHandler > ( ) ) ;
}
2025-12-29 17:17:08 +01:00
} ) ;
2025-12-09 20:46:12 +01:00
// Parse commands, if present
satdump : : cli : : CommandHandler cli_handler ( true ) ;
if ( argc > 1 )
{
int v = cli_handler . parse ( argc , argv ) ;
if ( v ! = 0 )
exit ( v ) ;
}
2022-03-30 15:41:23 +02:00
// Init UI
2024-01-21 17:30:00 -05:00
satdump : : initMainUI ( ) ;
2021-02-17 23:21:59 +01:00
2023-11-08 15:09:07 +01:00
// Shut down loading screen
2023-08-16 21:35:34 -04:00
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
2025-12-09 20:46:12 +01:00
// Attempt to run command, if present
if ( cli_handler . run ( ) )
exit ( 1 ) ;
2025-12-08 17:50:52 +01:00
2023-10-16 16:18:37 -04:00
// Attach signal
signal ( SIGINT , sig_handler_ui ) ;
2023-11-06 15:46:35 -05:00
signal ( SIGTERM , sig_handler_ui ) ;
2023-10-16 16:18:37 -04:00
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
{
2023-12-31 16:49:23 -05:00
satdump : : renderMainUI ( ) ;
2023-10-16 16:18:37 -04:00
} while ( ! glfwWindowShouldClose ( window ) & & ! signal_caught ) ;
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
2023-11-06 11:39:38 -05:00
# ifndef IMGUI_IMPL_OPENGL_ES2
2023-11-08 15:09:07 +01:00
if ( fallback_gl )
2023-11-06 09:17:59 -05:00
ImGui_ImplOpenGL2_Shutdown ( ) ;
else
2023-11-06 11:39:38 -05:00
# endif
2023-11-06 09:17:59 -05:00
ImGui_ImplOpenGL3_Shutdown ( ) ;
2021-02-17 22:25:03 +01:00
ImGui_ImplGlfw_Shutdown ( ) ;
ImGui : : DestroyContext ( ) ;
glfwDestroyWindow ( window ) ;
glfwTerminate ( ) ;
2021-02-18 11:33:29 +01:00
2021-11-19 19:22:11 +01:00
logger - > info ( " UI Exit " ) ;
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 + + )
2021-02-23 17:11:12 +01:00
{
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-02-23 17:11:12 +01:00
}
2021-05-05 15:52:12 +02:00
logger - > info ( " Exiting! " ) ;
2025-12-15 20:00:10 +01:00
# ifdef __APPLE__
exit ( 0 ) ;
# else
quick_exit ( 0 ) ;
# endif
2021-08-23 15:04:00 +01:00
}