Skip to content

Commit

Permalink
fixes (#326)
Browse files Browse the repository at this point in the history
  • Loading branch information
flagarde authored Oct 30, 2023
1 parent d33d5eb commit 2413c3c
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 31 deletions.
36 changes: 22 additions & 14 deletions cpp-terminal/platforms/terminal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "cpp-terminal/platforms/exception.hpp"
#include "cpp-terminal/platforms/file.hpp"

#ifdef _WIN32
#if defined(_WIN32)
#include <io.h>
#include <windows.h>
#if !defined(ENABLE_VIRTUAL_TERMINAL_PROCESSING)
Expand Down Expand Up @@ -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())
{
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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())
Expand All @@ -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; }
Expand Down
4 changes: 2 additions & 2 deletions cpp-terminal/terminal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
10 changes: 6 additions & 4 deletions cpp-terminal/terminal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include "cpp-terminal/options.hpp"
#include "cpp-terminal/terminfo.hpp"

#include <cstdint>

namespace Term
{

Expand All @@ -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;
Expand Down
34 changes: 29 additions & 5 deletions cpp-terminal/version.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 5 additions & 5 deletions cpp-terminal/version.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 0 additions & 1 deletion examples/attach_console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 22 additions & 0 deletions tests/version.test.cpp
Original file line number Diff line number Diff line change
@@ -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");
}

0 comments on commit 2413c3c

Please sign in to comment.