| 1 | /*++ |
| 2 | |
| 3 | Copyright (c) Microsoft. All rights reserved. |
| 4 | |
| 5 | Module Name: |
| 6 | |
| 7 | ConsoleState.h |
| 8 | |
| 9 | Abstract: |
| 10 | |
| 11 | This file contains function declarations for the ConsoleState helper class. |
| 12 | |
| 13 | --*/ |
| 14 | |
| 15 | #pragma once |
| 16 | |
| 17 | #include <wil/filesystem.h> |
| 18 | #include <wil/result.h> |
| 19 | #include "wslservice.h" |
| 20 | |
| 21 | namespace wsl::windows::common { |
| 22 | |
| 23 | // RAII wrapper for console state configuration and restoration |
| 24 | class ConsoleState |
| 25 | { |
| 26 | public: |
| 27 | ConsoleState(); |
| 28 | ~ConsoleState(); |
| 29 | ConsoleState(const ConsoleState&) = delete; |
| 30 | ConsoleState& operator=(const ConsoleState&) = delete; |
| 31 | ConsoleState(ConsoleState&&) = delete; |
| 32 | ConsoleState& operator=(ConsoleState&&) = delete; |
| 33 | |
| 34 | COORD GetWindowSize() const; |
| 35 | void SetInteractiveMode(); |
| 36 | |
| 37 | // Sets the console output code page to UTF-8 for the lifetime of this object, restoring the |
| 38 | // saved code page on destruction, without altering any console modes. Use for non-interactive |
| 39 | // relays that stream raw UTF-8 bytes (e.g. container logs, non-TTY process output) so |
| 40 | // multi-byte characters render correctly under any console output code page. |
| 41 | void SetOutputCodePageUtf8(); |
| 42 | |
| 43 | private: |
| 44 | void RestoreConsoleState(); |
| 45 | |
| 46 | wil::unique_hfile m_InputHandle; |
| 47 | wil::unique_hfile m_OutputHandle; |
| 48 | bool m_interactiveModeConfigured{false}; |
| 49 | std::optional<DWORD> m_SavedInputMode{}; |
| 50 | std::optional<UINT> m_SavedInputCodePage{}; |
| 51 | std::optional<DWORD> m_SavedOutputMode{}; |
| 52 | std::optional<UINT> m_SavedOutputCodePage{}; |
| 53 | }; |
| 54 | } // namespace wsl::windows::common |