Skip to content

Commit 6cff159

Browse files
committed
catch USR1 signal to poweroff quicker
Signed-off-by: Vincent-FK <[email protected]>
1 parent b7b91d4 commit 6cff159

File tree

4 files changed

+69
-10
lines changed

4 files changed

+69
-10
lines changed

RetroFE/Source/Menu/MenuMode.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ void MenuMode::end( )
177177
return;
178178
}
179179

180+
void MenuMode::stop( ){
181+
stop_menu_loop = 1;
182+
}
183+
180184

181185
void MenuMode::draw_progress_bar(SDL_Surface * surface, uint16_t x, uint16_t y, uint16_t width,
182186
uint16_t height, uint8_t percentage, uint16_t nb_bars){

RetroFE/Source/Menu/MenuMode.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,17 @@ typedef enum {ASPECT_RATIOS} ENUM_ASPECT_RATIOS_TYPES;
5252
#define SHELL_CMD_USB_UNMOUNT "share stop"
5353
#define SHELL_CMD_USB_CHECK_IS_SHARING "share is_sharing"
5454
#define SHELL_CMD_POWERDOWN "shutdown_funkey"
55+
#define SHELL_CMD_SCHEDULE_POWERDOWN "sched_shutdown"
5556

5657
class MenuMode
5758
{
5859

5960
public:
6061
//MenuMode();
61-
static void init(Configuration &c);
62-
static void end();
63-
static int launch( );
62+
static void init(Configuration &c);
63+
static void end();
64+
static int launch( );
65+
static void stop( );
6466

6567
/*static SDL_Surface * draw_screen;
6668

RetroFE/Source/RetroFE.cpp

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "Utility/Utils.h"
2828
#include "Collection/MenuParser.h"
2929
#include "SDL.h"
30+
#include <SDL/SDL_ttf.h>
3031
#include "Control/UserInput.h"
3132
#include "Graphics/PageBuilder.h"
3233
#include "Graphics/Page.h"
@@ -41,7 +42,7 @@
4142
#include <tuple>
4243
#include <vector>
4344
#include <time.h>
44-
#include <SDL/SDL_ttf.h>
45+
#include <signal.h>
4546

4647
#if defined(__linux) || defined(__APPLE__)
4748
#include <sys/stat.h>
@@ -180,6 +181,10 @@ int RetroFE::initialize( void *context )
180181
Logger::write( Logger::ZONE_INFO, "RetroFE", "Initialized meta database" );
181182
}
182183

184+
/* Init Signals */
185+
Logger::write( Logger::ZONE_INFO, "RetroFE", "Initializing signal USR1..." );
186+
signal(SIGUSR1, instance->handle_sigusr1);
187+
183188
instance->initialized = true;
184189
return 0;
185190

@@ -979,7 +984,7 @@ void RetroFE::run( )
979984
default:
980985
std::stringstream ss;
981986
ss << "Wrong state: " << state;
982-
Logger::write( Logger::ZONE_ERROR, "RetroFE", "Wrong state: " + ss.str() );
987+
Logger::write( Logger::ZONE_ERROR, "RetroFE", ss.str() );
983988
state = RETROFE_IDLE;
984989
break;
985990
}
@@ -1527,3 +1532,49 @@ CollectionInfo *RetroFE::getMenuCollection( std::string collectionName )
15271532
collection->playlists["all"] = &collection->items;
15281533
return collection;
15291534
}
1535+
1536+
1537+
1538+
/* Handler for SIGUSR1, caused by closing the console */
1539+
void RetroFE::handle_sigusr1(int sig)
1540+
{
1541+
printf("Caught signal USR1 %d\n", sig);
1542+
std::stringstream ss;
1543+
ss << "Caught signal USR1: " << sig;
1544+
Logger::write( Logger::ZONE_ERROR, "RetroFE", ss.str() );
1545+
1546+
/* Exit menu if it was launched */
1547+
MenuMode::stop();
1548+
1549+
/** Poweroff */
1550+
quick_poweroff();
1551+
}
1552+
1553+
1554+
/* Quick save and turn off the console */
1555+
void RetroFE::quick_poweroff()
1556+
{
1557+
/* Vars */
1558+
char shell_cmd[200];
1559+
FILE *fp;
1560+
1561+
/* Send command to kill any previously scheduled shutdown */
1562+
sprintf(shell_cmd, "pkill %s", SHELL_CMD_SCHEDULE_POWERDOWN);
1563+
fp = popen(shell_cmd, "r");
1564+
if (fp == NULL) {
1565+
printf("Failed to run command %s\n", shell_cmd);
1566+
std::stringstream ss;
1567+
ss << "Failed to run command " << shell_cmd;
1568+
Logger::write( Logger::ZONE_ERROR, "RetroFE", ss.str() );
1569+
}
1570+
1571+
/* Clean Poweroff */
1572+
sprintf(shell_cmd, "%s", SHELL_CMD_POWERDOWN);
1573+
fp = popen(shell_cmd, "r");
1574+
if (fp == NULL) {
1575+
printf("Failed to run command %s\n", shell_cmd);
1576+
std::stringstream ss;
1577+
ss << "Failed to run command " << shell_cmd;
1578+
Logger::write( Logger::ZONE_ERROR, "RetroFE", ss.str() );
1579+
}
1580+
}

RetroFE/Source/RetroFE.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,13 @@ class RetroFE
8989
void launchExit( );
9090

9191
private:
92-
volatile bool initialized;
93-
volatile bool initializeError;
94-
volatile bool initMetaDb;
95-
SDL_Thread *initializeThread;
96-
static int initialize( void *context );
92+
volatile bool initialized;
93+
volatile bool initializeError;
94+
volatile bool initMetaDb;
95+
SDL_Thread *initializeThread;
96+
static int initialize( void *context );
97+
static void handle_sigusr1(int sig);
98+
static void quick_poweroff( );
9799

98100
#undef X
99101
#define X(a, b) a,

0 commit comments

Comments
 (0)