1 // Copyright (c) 2011-2016 Ryan Prichard
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to
5 // deal in the Software without restriction, including without limitation the
6 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 // sell copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 #include "Win32Console.h"
28 #include "../shared/DebugClient.h"
29 #include "../shared/WinptyAssert.h"
31 Win32Console::Win32Console() : m_titleWorkBuf(16)
33 // The console window must be non-NULL. It is used for two purposes:
34 // (1) "Freezing" the console to detect the exact number of lines that
36 // (2) Killing processes attached to the console, by posting a WM_CLOSE
37 // message to the console window.
38 m_hwnd = GetConsoleWindow();
39 ASSERT(m_hwnd != nullptr);
42 std::wstring Win32Console::title()
45 // Calling GetConsoleTitleW is tricky, because its behavior changed
46 // from XP->Vista, then again from Win7->Win8. The Vista+Win7 behavior
47 // is especially broken.
49 // The MSDN documentation documents nSize as the "size of the buffer
50 // pointed to by the lpConsoleTitle parameter, in characters" and the
51 // successful return value as "the length of the console window's
52 // title, in characters."
54 // On XP, the function returns the title length, AFTER truncation
55 // (excluding the NUL terminator). If the title is blank, the API
56 // returns 0 and does not NUL-terminate the buffer. To accommodate
57 // XP, the function must:
58 // * Terminate the buffer itself.
59 // * Double the size of the title buffer in a loop.
61 // On Vista and up, the function returns the non-truncated title
62 // length (excluding the NUL terminator).
64 // On Vista and Windows 7, there is a bug where the buffer size is
65 // interpreted as a byte count rather than a wchar_t count. To
66 // work around this, we must pass GetConsoleTitleW a buffer that is
67 // twice as large as what is actually needed.
69 // See misc/*/Test_GetConsoleTitleW.cc for tests demonstrating Windows'
72 DWORD count = GetConsoleTitleW(m_titleWorkBuf.data(),
73 m_titleWorkBuf.size());
74 const size_t needed = (count + 1) * sizeof(wchar_t);
75 if (m_titleWorkBuf.size() < needed) {
76 m_titleWorkBuf.resize(needed);
79 m_titleWorkBuf[count] = L'\0';
80 return m_titleWorkBuf.data();
84 void Win32Console::setTitle(const std::wstring &title)
86 if (!SetConsoleTitleW(title.c_str())) {
87 trace("SetConsoleTitleW failed");
91 void Win32Console::setFrozen(bool frozen) {
92 const int SC_CONSOLE_MARK = 0xFFF2;
93 const int SC_CONSOLE_SELECT_ALL = 0xFFF5;
94 if (frozen == m_frozen) {
97 // Enter selection mode by activating either Mark or SelectAll.
98 const int command = m_freezeUsesMark ? SC_CONSOLE_MARK
99 : SC_CONSOLE_SELECT_ALL;
100 SendMessage(m_hwnd, WM_SYSCOMMAND, command, 0);
103 // Send Escape to cancel the selection.
104 SendMessage(m_hwnd, WM_CHAR, 27, 0x00010001);