From 1c72c76c59d5ce4ff342d2bcdc36047c503c4f41 Mon Sep 17 00:00:00 2001 From: Vincent-FK Date: Fri, 25 Oct 2019 06:57:11 +0800 Subject: [PATCH] add correct VT cleaning after game Signed-off-by: Vincent-FK --- RetroFE/Source/Control/UserInput.cpp | 2 +- RetroFE/Source/Execute/Launcher.cpp | 25 ++++++++++ RetroFE/Source/RetroFE.cpp | 9 +++- RetroFE/Source/Utility/Utils.cpp | 71 ++++++++++++++++++++++++++++ RetroFE/Source/Utility/Utils.h | 3 ++ 5 files changed, 108 insertions(+), 2 deletions(-) diff --git a/RetroFE/Source/Control/UserInput.cpp b/RetroFE/Source/Control/UserInput.cpp index 9adbfe6..7b32c3d 100644 --- a/RetroFE/Source/Control/UserInput.cpp +++ b/RetroFE/Source/Control/UserInput.cpp @@ -155,7 +155,6 @@ bool UserInput::initialize() MapKey("addPlaylist", KeyCodeAddPlaylist, false); MapKey("removePlaylist", KeyCodeRemovePlaylist, false); MapKey("random", KeyCodeRandom, false); - MapKey("menu", KeyCodeMenu, false); bool retVal = true; @@ -181,6 +180,7 @@ bool UserInput::initialize() retVal = MapKey("select", KeyCodeSelect) && retVal; retVal = MapKey("back", KeyCodeBack) && retVal; retVal = MapKey("quit", KeyCodeQuit) && retVal; + retVal = MapKey("menu", KeyCodeMenu) && retVal; return retVal; } diff --git a/RetroFE/Source/Execute/Launcher.cpp b/RetroFE/Source/Execute/Launcher.cpp index 60140f7..86abf24 100644 --- a/RetroFE/Source/Execute/Launcher.cpp +++ b/RetroFE/Source/Execute/Launcher.cpp @@ -119,9 +119,34 @@ bool Launcher::run(std::string collection, Item *collectionItem) if(!execute(executablePath, args, currentDirectory)) { Logger::write(Logger::ZONE_ERROR, "Launcher", "Failed to launch."); + + /// Clean VT + int current_VT = Utils::getVTid(); + if(current_VT >= 0){ + printf("ERROR Cleaning VT %d\n", current_VT); + Utils::termfix(current_VT); + } + else{ + for(int i=0; i<=12; i++){ + Utils::termfix(i); + } + } + return false; } + /// Clean VT + int current_VT = Utils::getVTid(); + if(current_VT >= 0){ + printf("Cleaning VT %d\n", current_VT); + Utils::termfix(current_VT); + } + else{ + for(int i=0; i<=12; i++){ + Utils::termfix(i); + } + } + return true; } diff --git a/RetroFE/Source/RetroFE.cpp b/RetroFE/Source/RetroFE.cpp index efb8a52..362b7a8 100644 --- a/RetroFE/Source/RetroFE.cpp +++ b/RetroFE/Source/RetroFE.cpp @@ -60,6 +60,13 @@ #define FPS 30 // TODO: set in conf file +//#define DEBUG_FPS +#ifdef DEBUG_FPS +#define DEBUG_FPS_PRINTF(...) printf(__VA_ARGS__); +#else +#define DEBUG_FPS_PRINTF(...) +#endif //MENU_DEBUG + RetroFE::RetroFE( Configuration &c ) : initialized(false) @@ -110,7 +117,7 @@ void RetroFE::render( ) avg_draw_time += draw_time; avg_draw_time_nb_vals++; if(avg_draw_time_nb_vals >= FPS*5){ - printf("Average draw time: %dms\n", avg_draw_time/avg_draw_time_nb_vals); + DEBUG_FPS_PRINTF("Average draw time: %dms\n", avg_draw_time/avg_draw_time_nb_vals); avg_draw_time=0; avg_draw_time_nb_vals=0; } diff --git a/RetroFE/Source/Utility/Utils.cpp b/RetroFE/Source/Utility/Utils.cpp index 45ef150..fb0ad1a 100644 --- a/RetroFE/Source/Utility/Utils.cpp +++ b/RetroFE/Source/Utility/Utils.cpp @@ -32,6 +32,13 @@ #include +/* The following devices are the same on all systems. */ +#define CURRENT_TTY "/dev/tty" +#define DEV_CONSOLE "/dev/console" +/*Linux, normal names */ +# define CURRENT_VC "/dev/tty0" + + Utils::Utils() { } @@ -287,3 +294,67 @@ int Utils::termfix(uint32_t ttyId){ //printf("Success\n"); return res; } + +int Utils::open_a_console(const char *fnam) +{ + int fd; + + /* try read-write */ + fd = open(fnam, O_RDWR); + + /* if failed, try read-only */ + if (fd < 0 && errno == EACCES) + fd = open(fnam, O_RDONLY); + + /* if failed, try write-only */ + if (fd < 0 && errno == EACCES) + fd = open(fnam, O_WRONLY); + + return fd; +} + +/* + * Get an fd for use with kbd/console ioctls. + * We try several things because opening /dev/console will fail + * if someone else used X (which does a chown on /dev/console). + */ +int Utils::get_console_fd_or_die(void) +{ + static const char *const console_names[] = { + DEV_CONSOLE, CURRENT_VC, CURRENT_TTY + }; + + int fd; + + for (fd = 2; fd >= 0; fd--) { + int fd4name; + int choice_fd; + char arg; + + fd4name = open_a_console(console_names[fd]); + chk_std: + choice_fd = (fd4name >= 0 ? fd4name : fd); + + arg = 0; + if (ioctl(choice_fd, KDGKBTYPE, &arg) == 0) + return choice_fd; + if (fd4name >= 0) { + fd4name = -1; + goto chk_std; + } + } + + printf("can't open console"); + /*return fd; - total failure */ +} + + +int Utils::getVTid(){ + struct vt_stat vtstat; + + vtstat.v_active = 0; + ioctl(get_console_fd_or_die(), VT_GETSTATE, &vtstat); + //printf("Active VT: %d\n", vtstat.v_active); + + return vtstat.v_active; +} diff --git a/RetroFE/Source/Utility/Utils.h b/RetroFE/Source/Utility/Utils.h index f83e381..040f7e4 100644 --- a/RetroFE/Source/Utility/Utils.h +++ b/RetroFE/Source/Utility/Utils.h @@ -45,6 +45,7 @@ public: static std::string combinePath(std::string path1, std::string path2, std::string path3, std::string path4, std::string path5); static int termfix(uint32_t ttyId); + static int getVTid(); #ifdef WIN32 static const char pathSeparator = '\\'; @@ -55,5 +56,7 @@ public: private: Utils(); virtual ~Utils(); + static int open_a_console(const char *fnam); + static int get_console_fd_or_die(void); };