1// This is an open source non-commercial project. Dear PVS-Studio, please check
2// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
3
4//
5// Terminal/console utils
6//
7
8#include "nvim/os/os.h"
9
10#ifdef INCLUDE_GENERATED_DECLARATIONS
11# include "os/tty.c.generated.h"
12#endif
13
14#ifdef WIN32
15# if !defined(ENABLE_VIRTUAL_TERMINAL_PROCESSING)
16# define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
17# endif
18/// Guesses the terminal-type. Calls SetConsoleMode() and uv_set_vterm_state()
19/// if appropriate.
20///
21/// @param[in,out] term Name of the guessed terminal, statically-allocated
22/// @param out_fd stdout file descriptor
23void os_tty_guess_term(const char **term, int out_fd)
24{
25 bool winpty = (os_getenv("NVIM") != NULL);
26
27 if (winpty) {
28 // Force TERM=win32con when running in winpty.
29 *term = "win32con";
30 uv_set_vterm_state(UV_UNSUPPORTED);
31 return;
32 }
33
34 bool conemu_ansi = strequal(os_getenv("ConEmuANSI"), "ON");
35 bool vtp = false;
36
37 HANDLE handle = (HANDLE)_get_osfhandle(out_fd);
38 DWORD dwMode;
39 if (handle != INVALID_HANDLE_VALUE && GetConsoleMode(handle, &dwMode)) {
40 dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
41 if (SetConsoleMode(handle, dwMode)) {
42 vtp = true;
43 }
44 }
45
46 if (*term == NULL) {
47 if (vtp) {
48 *term = "vtpcon";
49 } else if (conemu_ansi) {
50 *term = "conemu";
51 } else {
52 *term = "win32con";
53 }
54 }
55
56 if (conemu_ansi) {
57 uv_set_vterm_state(UV_SUPPORTED);
58 }
59}
60#endif
61