From 2413c3cc5434e7db711578c9065afb1f9ba1c8ad Mon Sep 17 00:00:00 2001 From: flagarde Date: Mon, 30 Oct 2023 09:16:12 +0100 Subject: [PATCH] fixes (#326) --- cpp-terminal/platforms/terminal.cpp | 36 ++++++++++++++++++----------- cpp-terminal/terminal.cpp | 4 ++-- cpp-terminal/terminal.hpp | 10 ++++---- cpp-terminal/version.cpp.in | 34 +++++++++++++++++++++++---- cpp-terminal/version.hpp | 10 ++++---- examples/attach_console.cpp | 1 - tests/CMakeLists.txt | 1 + tests/version.test.cpp | 22 ++++++++++++++++++ 8 files changed, 87 insertions(+), 31 deletions(-) create mode 100644 tests/version.test.cpp diff --git a/cpp-terminal/platforms/terminal.cpp b/cpp-terminal/platforms/terminal.cpp index 0fba2927..9fa66be5 100644 --- a/cpp-terminal/platforms/terminal.cpp +++ b/cpp-terminal/platforms/terminal.cpp @@ -13,7 +13,7 @@ #include "cpp-terminal/platforms/exception.hpp" #include "cpp-terminal/platforms/file.hpp" -#ifdef _WIN32 +#if defined(_WIN32) #include #include #if !defined(ENABLE_VIRTUAL_TERMINAL_PROCESSING) @@ -75,7 +75,7 @@ void Term::Terminal::store_and_restore() { if(GetConsoleMode(Private::out.handle(), &originalOut) == 0) { throw Term::Private::WindowsError(GetLastError()); } if(GetConsoleMode(Private::in.handle(), &originalIn) == 0) { throw Term::Private::WindowsError(GetLastError()); } - DWORD in{(originalIn & ~ENABLE_QUICK_EDIT_MODE) | (ENABLE_EXTENDED_FLAGS | activateFocusEvents() | activateMouseEvents())}; + DWORD in{(originalIn & ~(ENABLE_QUICK_EDIT_MODE | setFocusEvents() | setMouseEvents())) | (ENABLE_EXTENDED_FLAGS)}; DWORD out{originalOut}; if(!m_terminfo.isLegacy()) { @@ -104,15 +104,15 @@ void Term::Terminal::store_and_restore() } else { - desactivateMouseEvents(); - desactivateFocusEvents(); + unsetMouseEvents(); + unsetFocusEvents(); if(!Private::out.null()) if(tcsetattr(Private::out.fd(), TCSAFLUSH, &orig_termios) == -1) { throw Term::Exception("tcsetattr() failed in destructor"); } } #endif } -int Term::Terminal::activateMouseEvents() +std::int16_t Term::Terminal::setMouseEvents() { #if defined(_WIN32) return ENABLE_MOUSE_INPUT; @@ -121,7 +121,7 @@ int Term::Terminal::activateMouseEvents() #endif } -int Term::Terminal::desactivateMouseEvents() +std::int16_t Term::Terminal::unsetMouseEvents() { #if defined(_WIN32) return ENABLE_MOUSE_INPUT; @@ -130,7 +130,7 @@ int Term::Terminal::desactivateMouseEvents() #endif } -int Term::Terminal::activateFocusEvents() +std::int16_t Term::Terminal::setFocusEvents() { #if defined(_WIN32) return ENABLE_WINDOW_INPUT; @@ -139,7 +139,7 @@ int Term::Terminal::activateFocusEvents() #endif } -int Term::Terminal::desactivateFocusEvents() +std::int16_t Term::Terminal::unsetFocusEvents() { #if defined(_WIN32) return ENABLE_WINDOW_INPUT; @@ -177,8 +177,16 @@ void Term::Terminal::setMode() activated = true; } DWORD send = flags; - if(m_options.has(Option::Raw)) { send &= ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT | ENABLE_PROCESSED_INPUT); } - else if(m_options.has(Option::Cooked)) { send |= (ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT | ENABLE_PROCESSED_INPUT); } + if(m_options.has(Option::Raw)) + { + send &= ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT | ENABLE_PROCESSED_INPUT); + send |= (setFocusEvents() | setMouseEvents()); + } + else if(m_options.has(Option::Cooked)) + { + send |= (ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT | ENABLE_PROCESSED_INPUT); + send &= ~(setFocusEvents() | setMouseEvents()); + } if(m_options.has(Option::NoSignalKeys)) { send &= ~ENABLE_PROCESSED_INPUT; } else if(m_options.has(Option::SignalKeys)) { send |= ENABLE_PROCESSED_INPUT; } if(!Private::out.null()) @@ -204,14 +212,14 @@ void Term::Terminal::setMode() send.c_lflag &= ~(ECHO | ICANON | IEXTEN); send.c_cc[VMIN] = 1; send.c_cc[VTIME] = 0; - activateMouseEvents(); - activateFocusEvents(); + setMouseEvents(); + setFocusEvents(); } else if(m_options.has(Option::Cooked)) { send = raw; - desactivateMouseEvents(); - desactivateFocusEvents(); + unsetMouseEvents(); + unsetFocusEvents(); } if(m_options.has(Option::NoSignalKeys)) { send.c_lflag &= ~ISIG; } //FIXME need others flags ! else if(m_options.has(Option::NoSignalKeys)) { send.c_lflag |= ISIG; } diff --git a/cpp-terminal/terminal.cpp b/cpp-terminal/terminal.cpp index a76bfc61..9a7be388 100644 --- a/cpp-terminal/terminal.cpp +++ b/cpp-terminal/terminal.cpp @@ -62,8 +62,8 @@ Term::Terminal::~Terminal() if(m_options.has(Option::NoCursor)) Term::Private::out.write(cursor_on()); set_unset_utf8(); store_and_restore(); - desactivateFocusEvents(); - desactivateMouseEvents(); + unsetFocusEvents(); + unsetMouseEvents(); } catch(const Term::Exception& e) { diff --git a/cpp-terminal/terminal.hpp b/cpp-terminal/terminal.hpp index acd3f52a..555f09e1 100644 --- a/cpp-terminal/terminal.hpp +++ b/cpp-terminal/terminal.hpp @@ -11,6 +11,8 @@ #include "cpp-terminal/options.hpp" #include "cpp-terminal/terminfo.hpp" +#include + namespace Term { @@ -22,10 +24,10 @@ class Terminal void setOptions(); void applyOptions(); void setMode(); - int activateMouseEvents(); - int desactivateMouseEvents(); - int activateFocusEvents(); - int desactivateFocusEvents(); + std::int16_t setMouseEvents(); + std::int16_t unsetMouseEvents(); + std::int16_t setFocusEvents(); + std::int16_t unsetFocusEvents(); void set_unset_utf8(); Term::Terminfo m_terminfo; Term::Options m_options; diff --git a/cpp-terminal/version.cpp.in b/cpp-terminal/version.cpp.in index b3cd26d6..42d155a0 100644 --- a/cpp-terminal/version.cpp.in +++ b/cpp-terminal/version.cpp.in @@ -10,9 +10,33 @@ #include "cpp-terminal/version.hpp" // clang-format off -const std::uint16_t Term::Version::major{@cpp-terminal_VERSION_MAJOR@}; -const std::uint16_t Term::Version::minor{@cpp-terminal_VERSION_MINOR@}; -const std::uint16_t Term::Version::patch{@cpp-terminal_VERSION_PATCH@}; -const std::string Term::Version::string{"@cpp-terminal_VERSION_MAJOR@.@cpp-terminal_VERSION_MINOR@.@cpp-terminal_VERSION_PATCH@"}; -const std::string Term::homepage{"@cpp-terminal_HOMEPAGE_URL@"}; +std::uint16_t Term::Version::major() +{ + static std::uint16_t ret{@cpp-terminal_VERSION_MAJOR@}; + return ret; +} + +std::uint16_t Term::Version::minor() +{ + static std::uint16_t ret{@cpp-terminal_VERSION_MINOR@}; + return ret; +} + +std::uint16_t Term::Version::patch() +{ + static std::uint16_t ret{@cpp-terminal_VERSION_PATCH@}; + return ret; +} + +std::string Term::Version::string() +{ + static std::string ret{"@cpp-terminal_VERSION_MAJOR@.@cpp-terminal_VERSION_MINOR@.@cpp-terminal_VERSION_PATCH@"}; + return ret; +} + +std::string Term::homepage() +{ + static std::string ret{"@cpp-terminal_HOMEPAGE_URL@"}; + return ret; +} // clang-format on diff --git a/cpp-terminal/version.hpp b/cpp-terminal/version.hpp index 549bea69..12feec51 100644 --- a/cpp-terminal/version.hpp +++ b/cpp-terminal/version.hpp @@ -16,11 +16,11 @@ namespace Term { namespace Version { -extern const std::uint16_t major; ///< Major version of cpp-terminal. -extern const std::uint16_t minor; ///< Minor version of cpp-terminal. -extern const std::uint16_t patch; ///< Patch version of cpp-terminal. -extern const std::string string; ///< String containing the version of cpp-terminal ("Major.Minor.Patch"). +std::uint16_t major(); ///< Major version of cpp-terminal. +std::uint16_t minor(); ///< Minor version of cpp-terminal. +std::uint16_t patch(); ///< Patch version of cpp-terminal. +std::string string(); ///< String containing the version of cpp-terminal ("Major.Minor.Patch"). } // namespace Version -extern const std::string homepage; ///< Homepage of cpp-terminal. +std::string homepage(); ///< Homepage of cpp-terminal. } // namespace Term diff --git a/examples/attach_console.cpp b/examples/attach_console.cpp index e769ff32..9731c9db 100644 --- a/examples/attach_console.cpp +++ b/examples/attach_console.cpp @@ -24,7 +24,6 @@ int __stdcall WinMain(HINSTANCE, HINSTANCE, LPSTR, int) int main() #endif { - std::cout << "Running cpp-terminal version: " << Term::Version::string << " website : " << Term::homepage << std::endl << std::endl; try { std::string mode; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 2afa0dcd..ff277d2a 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -38,6 +38,7 @@ cppterminal_test(SOURCE events) cppterminal_test(SOURCE exception) cppterminal_test(SOURCE unicode) cppterminal_test(SOURCE options) +cppterminal_test(SOURCE version) if (NOT MINGW AND NOT MSYS) add_executable(Args args.test.cpp) diff --git a/tests/version.test.cpp b/tests/version.test.cpp new file mode 100644 index 00000000..8961e7f8 --- /dev/null +++ b/tests/version.test.cpp @@ -0,0 +1,22 @@ +/* +* cpp-terminal +* C++ library for writing multi-platform terminal applications. +* +* SPDX-FileCopyrightText: 2019-2023 cpp-terminal +* +* SPDX-License-Identifier: MIT +*/ + +#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN +#include "cpp-terminal/version.hpp" + +#include "doctest/doctest.h" + +TEST_CASE("cpp-terminal version") +{ + CHECK(Term::Version::major() > 0); + CHECK(Term::Version::minor() >= 0); + CHECK(Term::Version::patch() >= 0); + CHECK(Term::Version::string() == std::to_string(Term::Version::major()) + "." + std::to_string(Term::Version::minor()) + "." + std::to_string(Term::Version::patch())); + CHECK(Term::homepage() == "https://github.com/jupyter-xeus/cpp-terminal"); +}