From 6cff159abfc5ee74603ec5763298b2d9bac1796c Mon Sep 17 00:00:00 2001 From: Vincent-FK Date: Wed, 28 Oct 2020 08:46:46 +0100 Subject: [PATCH] catch USR1 signal to poweroff quicker Signed-off-by: Vincent-FK --- RetroFE/Source/Menu/MenuMode.cpp | 4 +++ RetroFE/Source/Menu/MenuMode.h | 8 +++-- RetroFE/Source/RetroFE.cpp | 55 ++++++++++++++++++++++++++++++-- RetroFE/Source/RetroFE.h | 12 ++++--- 4 files changed, 69 insertions(+), 10 deletions(-) diff --git a/RetroFE/Source/Menu/MenuMode.cpp b/RetroFE/Source/Menu/MenuMode.cpp index 95fecc8..b48d178 100755 --- a/RetroFE/Source/Menu/MenuMode.cpp +++ b/RetroFE/Source/Menu/MenuMode.cpp @@ -177,6 +177,10 @@ void MenuMode::end( ) return; } +void MenuMode::stop( ){ + stop_menu_loop = 1; +} + void MenuMode::draw_progress_bar(SDL_Surface * surface, uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint8_t percentage, uint16_t nb_bars){ diff --git a/RetroFE/Source/Menu/MenuMode.h b/RetroFE/Source/Menu/MenuMode.h index 8953054..5c905cf 100755 --- a/RetroFE/Source/Menu/MenuMode.h +++ b/RetroFE/Source/Menu/MenuMode.h @@ -52,15 +52,17 @@ typedef enum {ASPECT_RATIOS} ENUM_ASPECT_RATIOS_TYPES; #define SHELL_CMD_USB_UNMOUNT "share stop" #define SHELL_CMD_USB_CHECK_IS_SHARING "share is_sharing" #define SHELL_CMD_POWERDOWN "shutdown_funkey" +#define SHELL_CMD_SCHEDULE_POWERDOWN "sched_shutdown" class MenuMode { public: //MenuMode(); - static void init(Configuration &c); - static void end(); - static int launch( ); + static void init(Configuration &c); + static void end(); + static int launch( ); + static void stop( ); /*static SDL_Surface * draw_screen; diff --git a/RetroFE/Source/RetroFE.cpp b/RetroFE/Source/RetroFE.cpp index 0d2644b..aa0d6a1 100755 --- a/RetroFE/Source/RetroFE.cpp +++ b/RetroFE/Source/RetroFE.cpp @@ -27,6 +27,7 @@ #include "Utility/Utils.h" #include "Collection/MenuParser.h" #include "SDL.h" +#include #include "Control/UserInput.h" #include "Graphics/PageBuilder.h" #include "Graphics/Page.h" @@ -41,7 +42,7 @@ #include #include #include -#include +#include #if defined(__linux) || defined(__APPLE__) #include @@ -180,6 +181,10 @@ int RetroFE::initialize( void *context ) Logger::write( Logger::ZONE_INFO, "RetroFE", "Initialized meta database" ); } + /* Init Signals */ + Logger::write( Logger::ZONE_INFO, "RetroFE", "Initializing signal USR1..." ); + signal(SIGUSR1, instance->handle_sigusr1); + instance->initialized = true; return 0; @@ -979,7 +984,7 @@ void RetroFE::run( ) default: std::stringstream ss; ss << "Wrong state: " << state; - Logger::write( Logger::ZONE_ERROR, "RetroFE", "Wrong state: " + ss.str() ); + Logger::write( Logger::ZONE_ERROR, "RetroFE", ss.str() ); state = RETROFE_IDLE; break; } @@ -1527,3 +1532,49 @@ CollectionInfo *RetroFE::getMenuCollection( std::string collectionName ) collection->playlists["all"] = &collection->items; return collection; } + + + +/* Handler for SIGUSR1, caused by closing the console */ +void RetroFE::handle_sigusr1(int sig) +{ + printf("Caught signal USR1 %d\n", sig); + std::stringstream ss; + ss << "Caught signal USR1: " << sig; + Logger::write( Logger::ZONE_ERROR, "RetroFE", ss.str() ); + + /* Exit menu if it was launched */ + MenuMode::stop(); + + /** Poweroff */ + quick_poweroff(); +} + + +/* Quick save and turn off the console */ +void RetroFE::quick_poweroff() +{ + /* Vars */ + char shell_cmd[200]; + FILE *fp; + + /* Send command to kill any previously scheduled shutdown */ + sprintf(shell_cmd, "pkill %s", SHELL_CMD_SCHEDULE_POWERDOWN); + fp = popen(shell_cmd, "r"); + if (fp == NULL) { + printf("Failed to run command %s\n", shell_cmd); + std::stringstream ss; + ss << "Failed to run command " << shell_cmd; + Logger::write( Logger::ZONE_ERROR, "RetroFE", ss.str() ); + } + + /* Clean Poweroff */ + sprintf(shell_cmd, "%s", SHELL_CMD_POWERDOWN); + fp = popen(shell_cmd, "r"); + if (fp == NULL) { + printf("Failed to run command %s\n", shell_cmd); + std::stringstream ss; + ss << "Failed to run command " << shell_cmd; + Logger::write( Logger::ZONE_ERROR, "RetroFE", ss.str() ); + } +} diff --git a/RetroFE/Source/RetroFE.h b/RetroFE/Source/RetroFE.h index 7c25dad..9f2baff 100755 --- a/RetroFE/Source/RetroFE.h +++ b/RetroFE/Source/RetroFE.h @@ -89,11 +89,13 @@ public: void launchExit( ); private: - volatile bool initialized; - volatile bool initializeError; - volatile bool initMetaDb; - SDL_Thread *initializeThread; - static int initialize( void *context ); + volatile bool initialized; + volatile bool initializeError; + volatile bool initMetaDb; + SDL_Thread *initializeThread; + static int initialize( void *context ); + static void handle_sigusr1(int sig); + static void quick_poweroff( ); #undef X #define X(a, b) a,