1 // Copyright (c) 2011-2015 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
30 #include "DsrSender.h"
31 #include "EventLoop.h"
32 #include "Win32Console.h"
39 class Win32ConsoleBuffer;
41 class Agent : public EventLoop, public DsrSender
44 Agent(LPCWSTR controlPipeName,
50 void sendDsr() override;
53 NamedPipe &connectToControlPipe(LPCWSTR pipeName);
54 NamedPipe &createDataServerPipe(bool write, const wchar_t *kind);
57 void pollControlPipe();
58 void handlePacket(ReadBuffer &packet);
59 void writePacket(WriteBuffer &packet);
60 void handleStartProcessPacket(ReadBuffer &packet);
61 void handleSetSizePacket(ReadBuffer &packet);
62 void handleGetConsoleProcessListPacket(ReadBuffer &packet);
66 virtual void onPollTimeout() override;
67 virtual void onPipeIo(NamedPipe &namedPipe) override;
70 void autoClosePipesForShutdown();
71 std::unique_ptr<Win32ConsoleBuffer> openPrimaryBuffer();
72 void resizeWindow(int cols, int rows);
74 void syncConsoleTitle();
77 const bool m_useConerr;
78 const bool m_plainMode;
79 const int m_mouseMode;
80 Win32Console m_console;
81 std::unique_ptr<Scraper> m_primaryScraper;
82 std::unique_ptr<Scraper> m_errorScraper;
83 std::unique_ptr<Win32ConsoleBuffer> m_errorBuffer;
84 NamedPipe *m_controlPipe = nullptr;
85 NamedPipe *m_coninPipe = nullptr;
86 NamedPipe *m_conoutPipe = nullptr;
87 NamedPipe *m_conerrPipe = nullptr;
88 bool m_autoShutdown = false;
89 bool m_exitAfterShutdown = false;
90 bool m_closingOutputPipes = false;
91 std::unique_ptr<ConsoleInput> m_consoleInput;
92 HANDLE m_childProcess = nullptr;
94 // If the title is initialized to the empty string, then cmd.exe will
95 // sometimes print this error:
96 // Not enough storage is available to process this command.
97 // It happens on Windows 7 when logged into a Cygwin SSH session, for
98 // example. Using a title of a single space character avoids the problem.
99 // See https://github.com/rprichard/winpty/issues/74.
100 std::wstring m_currentTitle = L" ";