installed pty
[VSoRC/.git] / node_modules / node-pty / deps / winpty / src / agent / Win32Console.cc
1 // Copyright (c) 2011-2016 Ryan Prichard
2 //
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:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
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
19 // IN THE SOFTWARE.
20
21 #include "Win32Console.h"
22
23 #include <windows.h>
24 #include <wchar.h>
25
26 #include <string>
27
28 #include "../shared/DebugClient.h"
29 #include "../shared/WinptyAssert.h"
30
31 Win32Console::Win32Console() : m_titleWorkBuf(16)
32 {
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
35     //      have scrolled.
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);
40 }
41
42 std::wstring Win32Console::title()
43 {
44     while (true) {
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.
48         //
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."
53         //
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.
60         //
61         // On Vista and up, the function returns the non-truncated title
62         // length (excluding the NUL terminator).
63         //
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.
68         //
69         // See misc/*/Test_GetConsoleTitleW.cc for tests demonstrating Windows'
70         // behavior.
71
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);
77             continue;
78         }
79         m_titleWorkBuf[count] = L'\0';
80         return m_titleWorkBuf.data();
81     }
82 }
83
84 void Win32Console::setTitle(const std::wstring &title)
85 {
86     if (!SetConsoleTitleW(title.c_str())) {
87         trace("SetConsoleTitleW failed");
88     }
89 }
90
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) {
95         // Do nothing.
96     } else if (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);
101         m_frozen = true;
102     } else {
103         // Send Escape to cancel the selection.
104         SendMessage(m_hwnd, WM_CHAR, 27, 0x00010001);
105         m_frozen = false;
106     }
107 }