OpenVPN 3 Core Library
Loading...
Searching...
No Matches
console.hpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2012- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
12// console utilities for Windows
13
14#ifndef OPENVPN_WIN_CONSOLE_H
15#define OPENVPN_WIN_CONSOLE_H
16
17#include <windows.h>
18#include <string>
20
22
23class Input
24{
25 Input(const Input &) = delete;
26 Input &operator=(const Input &) = delete;
27
28 public:
29 Input(bool password)
30 : std_input(Handle::undefined()),
32 {
33 // disable control-C
34 ::SetConsoleCtrlHandler(nullptr, TRUE);
35
36 HANDLE in = ::GetStdHandle(STD_INPUT_HANDLE);
37 DWORD mode = 0;
38 if (Handle::defined(in) && ::GetConsoleMode(in, &mode))
39 {
40 // running on a console
41 DWORD newmode = mode
42 & ~(ENABLE_WINDOW_INPUT
43 | ENABLE_ECHO_INPUT
44 | ENABLE_MOUSE_INPUT);
45
46 if (!password)
47 {
48 newmode &= ~(ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT);
49 }
50
51 if (newmode == mode || ::SetConsoleMode(in, newmode))
52 {
53 std_input = in;
54 console_mode_save = mode;
55 }
56 }
57 }
58
60 {
62 ::SetConsoleMode(std_input, console_mode_save);
63 }
64
65 bool available()
66 {
68 {
69 DWORD n;
70 if (::GetNumberOfConsoleInputEvents(std_input, &n))
71 return n > 0;
72 }
73 return false;
74 }
75
76 std::string get_password(const std::string &prompt)
77 {
78 std::cout << prompt;
79 std::string s;
80 std::getline(std::cin, s);
81 std::cout << std::endl;
82
83 return s;
84 }
85
86 unsigned int get()
87 {
89 {
90 INPUT_RECORD ir;
91 do
92 {
93 DWORD n;
94 if (!available())
95 return 0;
96 if (!::ReadConsoleInputA(std_input, &ir, 1, &n))
97 return 0;
98 } while (ir.EventType != KEY_EVENT || ir.Event.KeyEvent.bKeyDown != TRUE);
99 return keyboard_ir_to_key(&ir);
100 }
101 else
102 return 0;
103 }
104
105 private:
106 unsigned int keyboard_ir_to_key(INPUT_RECORD *ir)
107 {
108 if (ir->Event.KeyEvent.uChar.AsciiChar == 0)
109 return ir->Event.KeyEvent.wVirtualScanCode;
110
111 if ((ir->Event.KeyEvent.dwControlKeyState
112 & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED))
113 && (ir->Event.KeyEvent.wVirtualKeyCode != 18))
114 return ir->Event.KeyEvent.wVirtualScanCode * 256;
115
116 return ir->Event.KeyEvent.uChar.AsciiChar;
117 }
118
119 HANDLE std_input;
121};
122
123class Title
124{
125 Title(const Title &) = delete;
126 Title &operator=(const Title &) = delete;
127
128 public:
129 Title(const std::string &new_title)
130 : old_title_defined(false)
131 {
132 char title[256];
133 if (::GetConsoleTitleA(title, sizeof(title)))
134 {
135 old_title = title;
136 old_title_defined = true;
137 }
138 ::SetConsoleTitleA(new_title.c_str());
139 }
140
142 {
144 ::SetConsoleTitleA(old_title.c_str());
145 }
146
147 private:
149 std::string old_title;
150};
151} // namespace openvpn::Win::Console
152
153#endif
Input & operator=(const Input &)=delete
Input(const Input &)=delete
std::string get_password(const std::string &prompt)
Definition console.hpp:76
unsigned int keyboard_ir_to_key(INPUT_RECORD *ir)
Definition console.hpp:106
Title(const Title &)=delete
Title(const std::string &new_title)
Definition console.hpp:129
Title & operator=(const Title &)=delete
bool defined(HANDLE handle)
Definition handle.hpp:25