From: <sv...@op...> - 2024-09-23 17:31:50
|
Author: manx Date: Mon Sep 23 19:31:44 2024 New Revision: 21703 URL: https://source.openmpt.org/browse/openmpt/?op=revision&rev=21703 Log: [Ref] openmpt123: Cleanup setting up terminal for UI mode. Modified: trunk/OpenMPT/openmpt123/openmpt123.cpp Modified: trunk/OpenMPT/openmpt123/openmpt123.cpp ============================================================================== --- trunk/OpenMPT/openmpt123/openmpt123.cpp Mon Sep 23 18:43:33 2024 (r21702) +++ trunk/OpenMPT/openmpt123/openmpt123.cpp Mon Sep 23 19:31:44 2024 (r21703) @@ -2126,12 +2126,14 @@ } }; -class terminal_input_guard { +class terminal_ui_guard { public: - terminal_input_guard( bool /* enable */ ) { - return; - } - ~terminal_input_guard() = default; + terminal_ui_guard() = default; + terminal_ui_guard( const terminal_ui_guard & ) = delete; + terminal_ui_guard( terminal_ui_guard && ) = default; + terminal_ui_guard & operator=( const terminal_ui_guard & ) = delete; + terminal_ui_guard & operator=( terminal_ui_guard && ) = default; + ~terminal_ui_guard() = default; }; #else @@ -2145,28 +2147,28 @@ ~FILE_mode_guard() = default; }; -class terminal_input_guard { +class terminal_ui_guard { private: bool changed = false; termios saved_attributes; public: - terminal_input_guard( bool enable ) { - if ( !enable ) { - return; - } - termios tattr; + terminal_ui_guard() { if ( !isatty( STDIN_FILENO ) ) { return; } tcgetattr( STDIN_FILENO, &saved_attributes ); - tcgetattr( STDIN_FILENO, &tattr ); + termios tattr = saved_attributes; tattr.c_lflag &= ~( ICANON | ECHO ); tattr.c_cc[VMIN] = 1; tattr.c_cc[VTIME] = 0; tcsetattr( STDIN_FILENO, TCSAFLUSH, &tattr ); changed = true; } - ~terminal_input_guard() { + terminal_ui_guard( const terminal_ui_guard & ) = delete; + terminal_ui_guard( terminal_ui_guard && ) = default; + terminal_ui_guard & operator=( const terminal_ui_guard & ) = delete; + terminal_ui_guard & operator=( terminal_ui_guard && ) = default; + ~terminal_ui_guard() { if ( changed ) { tcsetattr(STDIN_FILENO, TCSANOW, &saved_attributes); } @@ -2291,8 +2293,8 @@ // set stdout binary FILE_mode_guard stdout_guard( stdout, stdout_can_ui ? FILE_mode::unchanged : FILE_mode::binary ); - // setup terminal - terminal_input_guard input_guard( stdin_can_ui && ( flags.mode == Mode::UI ) ); + // setup terminal for ui mode + std::optional<terminal_ui_guard> input_guard{ ( stdin_can_ui && ( flags.mode == Mode::UI ) ? std::make_optional<terminal_ui_guard>() : std::nullopt ) }; textout & log = flags.quiet ? static_cast<textout&>( dummy_log ) : static_cast<textout&>( stdout_can_ui ? std_out : std_err ); |