diff --git a/src/base/io/Console.cpp b/src/base/io/Console.cpp index 30ff34a34..b6f9c37df 100644 --- a/src/base/io/Console.cpp +++ b/src/base/io/Console.cpp @@ -1,6 +1,6 @@ /* XMRig - * Copyright (c) 2018-2021 SChernykh - * Copyright (c) 2016-2021 XMRig , + * Copyright (c) 2018-2022 SChernykh + * Copyright (c) 2016-2022 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -14,16 +14,98 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see . + * + * Additional permission under GNU GPL version 3 section 7 + * + * If you modify this Program, or any covered work, by linking or combining + * it with OpenSSL (or a modified version of that library), containing parts + * covered by the terms of OpenSSL License and SSLeay License, the licensors + * of this Program grant you additional permission to convey the resulting work. */ #include "base/io/Console.h" +#include "base/io/log/Log.h" +#include "base/io/Signals.h" #include "base/kernel/interfaces/IConsoleListener.h" +#include "base/kernel/private/Title.h" +#include "base/kernel/Process.h" +#include "base/tools/Cvt.h" #include "base/tools/Handle.h" -xmrig::Console::Console(IConsoleListener *listener) - : m_listener(listener) +#ifdef XMRIG_FEATURE_EVENTS +# include "base/kernel/Events.h" +# include "base/kernel/events/ConsoleEvent.h" +#endif + + +namespace xmrig { + + +class Console::Private : public IConsoleListener { +public: + XMRIG_DISABLE_COPY_MOVE(Private) + + Private(); + ~Private() override; + +# ifdef XMRIG_OS_WIN + std::wstring title; +# endif + + IConsoleListener *listener = nullptr; + +protected: + void onConsoleCommand(char command) override; + +private: + static bool isSupported(); + + static void onAllocBuffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf); + static void onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf); + + char m_buf[1] = { 0 }; + uv_tty_t *m_tty = nullptr; +}; + + +} // namespace xmrig + + +xmrig::Console::Console() : + d(std::make_shared()) +{ + d->listener = d.get(); +} + + +xmrig::Console::Console(IConsoleListener *listener) : + d(std::make_shared()) +{ + d->listener = listener; +} + + +#ifdef XMRIG_OS_WIN +void xmrig::Console::setTitle(const Title &title) const +{ + SetConsoleTitleW(title.isEnabled() ? Cvt::toUtf16(title.value()).c_str() : d->title.c_str()); +} +#endif + + +xmrig::Console::Private::Private() +{ +# ifdef XMRIG_OS_WIN + { + constexpr size_t kMaxTitleLength = 8192; + + wchar_t title_w[kMaxTitleLength]{}; + title = { title_w, GetConsoleTitleW(title_w, kMaxTitleLength) }; + } +# endif + if (!isSupported()) { return; } @@ -37,11 +119,11 @@ xmrig::Console::Console(IConsoleListener *listener) } uv_tty_set_mode(m_tty, UV_TTY_MODE_RAW); - uv_read_start(reinterpret_cast(m_tty), Console::onAllocBuffer, Console::onRead); + uv_read_start(reinterpret_cast(m_tty), onAllocBuffer, onRead); } -xmrig::Console::~Console() +xmrig::Console::Private::~Private() { uv_tty_reset_mode(); @@ -49,28 +131,45 @@ xmrig::Console::~Console() } -bool xmrig::Console::isSupported() +void xmrig::Console::Private::onConsoleCommand(char command) +{ +# ifdef XMRIG_FEATURE_EVENTS + if (command == 3) { + LOG_WARN("%s " YELLOW_BOLD("Ctrl+C ") YELLOW("received, exiting"), Signals::tag()); + + Process::exit(0); + } + else { + Process::events().send(command); + } +# else + assert(!command); +# endif +} + + +bool xmrig::Console::Private::isSupported() { const uv_handle_type type = uv_guess_handle(0); return type == UV_TTY || type == UV_NAMED_PIPE; } -void xmrig::Console::onAllocBuffer(uv_handle_t *handle, size_t, uv_buf_t *buf) +void xmrig::Console::Private::onAllocBuffer(uv_handle_t *handle, size_t, uv_buf_t *buf) { - auto console = static_cast(handle->data); + auto console = static_cast(handle->data); buf->len = 1; buf->base = console->m_buf; } -void xmrig::Console::onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) +void xmrig::Console::Private::onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) { if (nread < 0) { return uv_close(reinterpret_cast(stream), nullptr); } if (nread == 1) { - static_cast(stream->data)->m_listener->onConsoleCommand(buf->base[0]); + static_cast(stream->data)->listener->onConsoleCommand(*buf->base); } } diff --git a/src/base/io/Console.h b/src/base/io/Console.h index 129bf8173..509db49a7 100644 --- a/src/base/io/Console.h +++ b/src/base/io/Console.h @@ -1,6 +1,6 @@ /* XMRig - * Copyright (c) 2018-2021 SChernykh - * Copyright (c) 2016-2021 XMRig , + * Copyright (c) 2018-2022 SChernykh + * Copyright (c) 2016-2022 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -14,6 +14,13 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see . + * + * Additional permission under GNU GPL version 3 section 7 + * + * If you modify this Program, or any covered work, by linking or combining + * it with OpenSSL (or a modified version of that library), containing parts + * covered by the terms of OpenSSL License and SSLeay License, the licensors + * of this Program grant you additional permission to convey the resulting work. */ #ifndef XMRIG_CONSOLE_H @@ -23,45 +30,32 @@ #include "base/tools/Object.h" -using uv_buf_t = struct uv_buf_t; -using uv_handle_t = struct uv_handle_s; -using uv_stream_t = struct uv_stream_s; -using uv_tty_t = struct uv_tty_s; - -#ifdef XMRIG_OS_WIN -using ssize_t = intptr_t; -#else -# include -#endif - - namespace xmrig { +class Title; class IConsoleListener; class Console { public: - XMRIG_DISABLE_COPY_MOVE_DEFAULT(Console) + XMRIG_DISABLE_COPY_MOVE(Console) - Console(IConsoleListener *listener); - ~Console(); + Console(); + explicit Console(IConsoleListener *listener); + ~Console() = default; + +# ifdef XMRIG_OS_WIN + void setTitle(const Title &title) const; +# endif private: - static bool isSupported(); - - static void onAllocBuffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf); - static void onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf); - - char m_buf[1] = { 0 }; - IConsoleListener *m_listener; - uv_tty_t *m_tty = nullptr; + XMRIG_DECL_PRIVATE() }; -} /* namespace xmrig */ +} // namespace xmrig -#endif /* XMRIG_CONSOLE_H */ +#endif // XMRIG_CONSOLE_H diff --git a/src/base/kernel/interfaces/IConsoleListener.h b/src/base/kernel/interfaces/IConsoleListener.h index 194bc72f9..2e60c672f 100644 --- a/src/base/kernel/interfaces/IConsoleListener.h +++ b/src/base/kernel/interfaces/IConsoleListener.h @@ -1,6 +1,6 @@ /* XMRig - * Copyright (c) 2018-2021 SChernykh - * Copyright (c) 2016-2021 XMRig , + * Copyright (c) 2018-2022 SChernykh + * Copyright (c) 2016-2022 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -14,6 +14,13 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see . + * + * Additional permission under GNU GPL version 3 section 7 + * + * If you modify this Program, or any covered work, by linking or combining + * it with OpenSSL (or a modified version of that library), containing parts + * covered by the terms of OpenSSL License and SSLeay License, the licensors + * of this Program grant you additional permission to convey the resulting work. */ #ifndef XMRIG_ICONSOLELISTENER_H @@ -38,7 +45,7 @@ public: }; -} /* namespace xmrig */ +} // namespace xmrig #endif // XMRIG_ICONSOLELISTENER_H diff --git a/src/base/kernel/interfaces/ISignalListener.h b/src/base/kernel/interfaces/ISignalListener.h index 75f47cf06..9fe8b0d17 100644 --- a/src/base/kernel/interfaces/ISignalListener.h +++ b/src/base/kernel/interfaces/ISignalListener.h @@ -1,6 +1,6 @@ /* XMRig - * Copyright (c) 2018-2020 SChernykh - * Copyright (c) 2016-2020 XMRig , + * Copyright (c) 2018-2022 SChernykh + * Copyright (c) 2016-2022 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -14,6 +14,13 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see . + * + * Additional permission under GNU GPL version 3 section 7 + * + * If you modify this Program, or any covered work, by linking or combining + * it with OpenSSL (or a modified version of that library), containing parts + * covered by the terms of OpenSSL License and SSLeay License, the licensors + * of this Program grant you additional permission to convey the resulting work. */ #ifndef XMRIG_ISIGNALLISTENER_H @@ -41,7 +48,7 @@ public: }; -} /* namespace xmrig */ +} // namespace xmrig #endif // XMRIG_ISIGNALLISTENER_H