/** * @file include/ActivMiddleware/ActivBase/ConsoleWindow.h * * @brief Header file for console window. */ #if (!defined ACTIV_BASE_CONSOLE_WINDOW_H) #define ACTIV_BASE_CONSOLE_WINDOW_H #include "ActivMiddleware/ActivBase/External.h" #include "ActivMiddleware/Misc/TypeSafeFlags.h" #if (defined ACTIV_PLATFORM_WASM) #include "ActivMiddleware/CxxYZ/mutex.h" extern "C" typedef struct _win WINDOW; #elif (defined ACTIV_PLATFORM_POSIX) #include "ActivMiddleware/CxxYZ/mutex.h" extern "C" typedef struct _win_st WINDOW; #endif //! Activ namespace. namespace Activ { class MessageBuilder; class MessageValidator; // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Console window class. */ class ConsoleWindow : private Boost::noncopyable { public: /** * @brief Cursor coordinate. */ class Coord { public: /** * @brief Default constructor. x & y coordinates set to 0. */ Coord(); /** * @brief Constructor. * * @param x coordinate. * @param y coordinate. */ Coord(const int16_t x, const int16_t y); /** * @brief Equality operator. * * @param rhs to compare with. * * @return boolean. */ bool operator==(const Coord &rhs) const; /** * @brief Inequality operator. * * @param rhs to compare with. * * @return boolean. */ bool operator!=(const Coord &rhs) const; /** * @brief Serialize to a MessageBuilder. * * @param messageBuilder to serialize into. * * @return STATUS_CODE_SUCCESS or an error code. */ ACTIV_BASE_API StatusCode Serialize(MessageBuilder &messageBuilder) const; /** * @brief Deserialize from a MessageValidator into this object. * * @param messageValidator to deserialize from. * * @return STATUS_CODE_SUCCESS or an error code. */ ACTIV_BASE_API StatusCode Deserialize(MessageValidator &messageValidator); int16_t m_x; int16_t m_y; }; //! Flags. class Flags : public TypeSafeFlags::Underlying { public: typedef TypeSafeFlags::Value None; typedef TypeSafeFlags::Value NoAutoscroll; ///< Don't scroll the window when output reaches the bottom typedef TypeSafeFlags::Value ImmediateRefresh; ///< Refresh display immediately after a write typedef TypeSafeFlags::Type Type; }; /** * @brief Constructor. * * @param flags flags defining console behaviour. */ ACTIV_BASE_API ConsoleWindow(const Flags::Type flags = Flags::Type()); /** * @brief Destructor. */ ACTIV_BASE_API ~ConsoleWindow(); /** * @brief Wait for input on the console. * * Note the resolution of the timeout on Win32 is around 10ms, and on Linux is 100ms. * * @param timeoutPeriod how long to wait for input, in ms. * * @retval STATUS_CODE_SUCCESS there is input queued on the console. * @retval STATUS_CODE_TIMEOUT no input queued in the timeout period. * @retval STATUS_CODE_FAILURE an error occured. */ ACTIV_BASE_API StatusCode WaitForInput(const TimeoutPeriod timeoutPeriod = 100) const; /** * @brief Read a complete line from the console. Note returned buffer will not contain terminating * cr / lf characters. This is a blocking call. * * @param input reference to a string to receive input. * * @retval STATUS_CODE_SUCCESS. * @retval ... did not read a complete line. */ ACTIV_BASE_API StatusCode ReadLine(String &input) const; /** * @brief Get the visible console display size. * * @param size to receive the size. * * @retval STATUS_CODE_SUCCESS * @retval ... */ ACTIV_BASE_API StatusCode GetDisplaySize(Coord &size) const; /** * @brief Set the visible console display size. * * If the current buffer size is smaller in either dimension than the requested display size, it is increased. * * @param size the new size. * * @retval STATUS_CODE_SUCCESS * @retval ... */ ACTIV_BASE_API StatusCode SetDisplaySize(const Coord &size); /** * @brief Get the console buffer size. The buffer size may be larger than the visible display. * * @param size to receive the size. * * @retval STATUS_CODE_SUCCESS * @retval ... */ ACTIV_BASE_API StatusCode GetBufferSize(Coord &size) const; /** * @brief Set the console buffer size. The buffer size may be larger than the visible display. * * If the requested console buffer size is smaller in either dimension than the current display size, it is increased. * * @param size the new size. * * @retval STATUS_CODE_SUCCESS * @retval ... */ ACTIV_BASE_API StatusCode SetBufferSize(const Coord &size); /** * @brief Get the current cursor position. * * @param coord to receive the cursor position. * * @retval STATUS_CODE_SUCCESS * @retval ... */ ACTIV_BASE_API StatusCode GetCursorPosition(Coord &coord) const; /** * @brief Set the cursor position. * * @param coord the position to place the cursor. * * @retval STATUS_CODE_SUCCESS * @retval ... */ ACTIV_BASE_API StatusCode SetCursorPosition(const Coord &coord); /** * @brief Set the cursor visibility. * * @param isVisible true to make cursor visible, false to make invisible. * * @retval STATUS_CODE_SUCCESS * @retval ... */ ACTIV_BASE_API StatusCode SetCursorVisibility(const bool isVisible); /** * @brief Clear the console from a coordinate to the end. * * Note the current cursor position will not be altered by this call. * * @param coord the coordinate to clear from. * * @retval STATUS_CODE_SUCCESS * @retval ... */ ACTIV_BASE_API StatusCode ClearFrom(const Coord &coord); /** * @brief Write a string beginning at a specified coordinate. * * Note the current cursor position will not be altered by this call. * * @param coord the coordinate to start writing from. * @param pStr the string to write. * @param length number of characters in \a string to write, default is all. * * @retval STATUS_CODE_SUCCESS * @retval ... */ ACTIV_BASE_API StatusCode WriteAt(const Coord &coord, const char * const pStr, const size_t length = 0); /** * @brief Write formatted output to the console from the current cursor position. * * @param pStr the format string. * @param ... args. */ ACTIV_PRINTF_FUNCTION(printf, 2, 3) ACTIV_BASE_API void Printf(const char * const pStr, ...) const; /** * @brief Set title of console. * * @param title */ ACTIV_BASE_API static void SetTitle(const String &title); private: //! Platform-common construction. void CommonCtor(); //! Platform-common destruction. void CommonDtor(); #if (defined ACTIV_PLATFORM_WINDOWS) const HANDLE m_outputHandle; ///< Console output handle. const HANDLE m_inputHandle; ///< Console input handle. DWORD m_initialMode; ///< Initial console mode settings. #elif (defined ACTIV_PLATFORM_POSIX) WINDOW * const m_pWindow; ///< ncurses WINDOW. mutable CxxYZ::mutex m_mutex; Coord m_displaySize; ///< To keep track of current display size. #else #error Configure for this platform #endif bool m_initialIsCursorVisible; ///< Initial cursor visibility. Coord m_initialDisplaySize; ///< Initial display size. Coord m_initialBufferSize; ///< Initial buffer size. }; // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- inline ConsoleWindow::Coord::Coord() : m_x(0), m_y(0) { } // --------------------------------------------------------------------------------------------------------------------------------- inline ConsoleWindow::Coord::Coord(const int16_t x, const int16_t y) : m_x(x), m_y(y) { } // --------------------------------------------------------------------------------------------------------------------------------- inline bool ConsoleWindow::Coord::operator==(const Coord &rhs) const { return (m_x == rhs.m_x) && (m_y == rhs.m_y); } // --------------------------------------------------------------------------------------------------------------------------------- inline bool ConsoleWindow::Coord::operator!=(const Coord &rhs) const { return !operator==(rhs); } } // namespace Activ #endif // (!defined ACTIV_BASE_CONSOLE_WINDOW_H)