Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
flagarde committed Oct 31, 2024
1 parent 8b45a74 commit 808d1be
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 11 deletions.
21 changes: 19 additions & 2 deletions cpp-terminal/private/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,25 @@ std::string Term::Private::InputFileHandler::read() const
ReadConsole(Private::in.handle(), &ret[0], static_cast<DWORD>(ret.size()), &nread, nullptr);
return ret.c_str();
#else
std::size_t nread{0};
Term::Private::Errno().check_if(::ioctl(Private::in.fd(), FIONREAD, &nread) != 0).throw_exception("::ioctl(Private::in.fd(), FIONREAD, &nread)"); //NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
#if defined(MAX_INPUT)
static const constexpr std::size_t max_input{MAX_INPUT};
#else
static const constexpr std::size_t max_input{256};
#endif
#if defined(_POSIX_MAX_INPUT)
static const constexpr std::size_t posix_max_input{MAX_INPUT};
#else
static const constexpr std::size_t posix_max_input{256};
#endif
static std::size_t nread{std::max(max_input, posix_max_input)};
try
{
Term::Private::Errno().check_if(::ioctl(Private::in.fd(), FIONREAD, &nread) != 0).throw_exception("::ioctl(Private::in.fd(), FIONREAD, &nread)"); //NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
}
catch(const ErrnoException& exception)
{
if(exception.code() != 25) throw;
}
std::string ret(nread, '\0');
if(nread != 0)
{
Expand Down
8 changes: 3 additions & 5 deletions cpp-terminal/private/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ void sendString(Term::Private::BlockingQueue& events, std::wstring& str)
}

#endif

Term::Private::Input::~Input()
{
if(m_thread.joinable()) m_thread.join();
Expand All @@ -91,10 +90,6 @@ int Term::Private::Input::m_poll{-1};

void Term::Private::Input::init_thread()
{
if(m_thread.joinable()) m_thread.join();
std::thread thread(Term::Private::Input::read_event);
m_thread.swap(thread);
Term::Private::Sigwinch::unblockSigwinch();
#if defined(__linux__)
m_poll = {::epoll_create1(EPOLL_CLOEXEC)};
::epoll_event signal;
Expand All @@ -106,6 +101,9 @@ void Term::Private::Input::init_thread()
input.data.fd = {Term::Private::in.fd()};
::epoll_ctl(m_poll, EPOLL_CTL_ADD, Term::Private::in.fd(), &input);
#endif
if(m_thread.joinable()) m_thread.join();
std::thread thread(Term::Private::Input::read_event);
m_thread.swap(thread);
}

void Term::Private::Input::read_event()
Expand Down
6 changes: 5 additions & 1 deletion cpp-terminal/private/terminal_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "cpp-terminal/private/env.hpp"
#include "cpp-terminal/private/exception.hpp"
#include "cpp-terminal/private/file.hpp"
#include "cpp-terminal/private/sigwinch.hpp"
#include "cpp-terminal/terminal.hpp"

#if defined(_WIN32)
Expand Down Expand Up @@ -108,11 +109,14 @@ try
static termios orig_termios;
if(!enabled)
{
Term::Private::Sigwinch::blockSigwinch();
Term::Private::Sigwinch::registerSigwinch();
if(!Private::out.null()) { Term::Private::Errno().check_if(tcgetattr(Private::out.fd(), &orig_termios) == -1).throw_exception("tcgetattr() failed"); }
enabled = true;
}
else
{
Term::Private::Sigwinch::unblockSigwinch();
unsetMouseEvents();
unsetFocusEvents();
if(!Private::out.null()) { Term::Private::Errno().check_if(tcsetattr(Private::out.fd(), TCSAFLUSH, &orig_termios) == -1).throw_exception("tcsetattr() failed in destructor"); }
Expand Down Expand Up @@ -220,7 +224,7 @@ void Term::Terminal::setMode() const
unsetFocusEvents();
}
if(m_options.has(Option::NoSignalKeys)) { send.c_lflag &= ~static_cast<std::size_t>(ISIG); } //FIXME need others flags !
else if(m_options.has(Option::NoSignalKeys)) { send.c_lflag |= ISIG; }
else if(m_options.has(Option::SignalKeys)) { send.c_lflag |= ISIG; }
Term::Private::Errno().check_if(tcsetattr(Private::out.fd(), TCSAFLUSH, &send) == -1).throw_exception("tcsetattr(Private::out.fd(), TCSAFLUSH, &send)");
}
#endif
Expand Down
3 changes: 0 additions & 3 deletions cpp-terminal/terminal_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "cpp-terminal/options.hpp"
#include "cpp-terminal/private/exception.hpp"
#include "cpp-terminal/private/file.hpp"
#include "cpp-terminal/private/sigwinch.hpp"
#include "cpp-terminal/screen.hpp"
#include "cpp-terminal/style.hpp"
#include "cpp-terminal/terminal.hpp" //FIXME avoid recursion
Expand All @@ -23,8 +22,6 @@ Term::Options Term::Terminal::getOptions() const noexcept { return m_options; }
Term::Terminal::Terminal() noexcept
try
{
Term::Private::Sigwinch::blockSigwinch();
Term::Private::Sigwinch::registerSigwinch();
store_and_restore();
setMode(); //Save the default cpp-terminal mode done in store_and_restore();
set_unset_utf8();
Expand Down
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ cppterminal_example(SOURCE prompt_not_immediate)
cppterminal_example(SOURCE prompt_simple)
cppterminal_example(SOURCE styles)
cppterminal_example(SOURCE utf8)
cppterminal_example(SOURCE signal)
cppterminal_example(SOURCE attach_console WIN32)
cppterminal_example(SOURCE attach_console_minimal WIN32)
33 changes: 33 additions & 0 deletions examples/signal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

/*
* cpp-terminal
* C++ library for writing multi-platform terminal applications.
*
* SPDX-FileCopyrightText: 2019-2024 cpp-terminal
*
* SPDX-License-Identifier: MIT
*/

#include "cpp-terminal/iostream.hpp"
#include "cpp-terminal/terminal.hpp"

#include <iostream>

int main()
{
std::at_quick_exit(
[]()
{
std::cout << "Unhandled exception\n" << std::flush;
//std::abort();
});
std::set_terminate(
[]()
{
std::cout << "Unhandled exception\n" << std::flush;
//std::abort();
});
Term::terminal.setOptions(Term::Option::Raw, Term::Option::SignalKeys);
while(true) {}
return 0;
}

0 comments on commit 808d1be

Please sign in to comment.