robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. Keyesrefactor(os): deduplicate console/fd boilerplate #40509f46857d1df

master
5.0 KiB177 linesraw
1#include <string.h>
2
3#include "nvim/globals.h"
4#include "nvim/memory.h"
5#include "nvim/os/fs.h"
6#include "nvim/os/input.h"
7#include "nvim/os/os.h"
8#include "nvim/os/os_win_console.h"
9
10#include "os/os_win_console.c.generated.h"
11
12static char origTitle[256] = { 0 };
13static HWND hWnd = NULL;
14static HICON hOrigIconSmall = NULL;
15static HICON hOrigIcon = NULL;
16
17/// Re-enable normal Ctrl-C processing after detached startup.
18///
19/// On Windows, UV_PROCESS_DETACHED implies CREATE_NEW_PROCESS_GROUP, which
20/// disables Ctrl-C handling for the new process. Restore the default behavior
21/// once the embedded server has a console so terminal jobs inherit it.
22void os_enable_ctrl_c(void)
23{
24  SetConsoleCtrlHandler(NULL, false);
25}
26
27int os_open_conin_fd(void)
28{
29  const HANDLE conin_handle = CreateFile("CONIN$",
30                                         GENERIC_READ | GENERIC_WRITE,
31                                         FILE_SHARE_READ | FILE_SHARE_WRITE,
32                                         (LPSECURITY_ATTRIBUTES)NULL,
33                                         OPEN_EXISTING, 0, (HANDLE)NULL);
34  assert(conin_handle != INVALID_HANDLE_VALUE);
35  int conin_fd = _open_osfhandle((intptr_t)conin_handle, _O_RDONLY);
36  assert(conin_fd != -1);
37  return conin_fd;
38}
39
40void os_clear_hwnd(void)
41{
42  hWnd = NULL;
43}
44
45void os_redirect_stdin_to_conin(void)
46{
47  close(STDIN_FILENO);
48  const int conin_fd = os_open_conin_fd();
49  assert(conin_fd == STDIN_FILENO);
50}
51
52void os_redirect_stdout_stderr_to_conout(void)
53{
54  const HANDLE conout_handle =
55    CreateFile("CONOUT$",
56               GENERIC_READ | GENERIC_WRITE,
57               FILE_SHARE_READ | FILE_SHARE_WRITE,
58               (LPSECURITY_ATTRIBUTES)NULL,
59               OPEN_EXISTING, 0, (HANDLE)NULL);
60  assert(conout_handle != INVALID_HANDLE_VALUE);
61  close(STDOUT_FILENO);
62  const int conout_fd = _open_osfhandle((intptr_t)conout_handle, 0);
63  assert(conout_fd == STDOUT_FILENO);
64  close(STDERR_FILENO);
65  const int conerr_fd = _open_osfhandle((intptr_t)conout_handle, 0);
66  assert(conerr_fd == STDERR_FILENO);
67}
68
69/// Points this process's stdio at the current console: enables Ctrl-C handling and replaces
70/// stdin/stdout/stderr with CONIN$/CONOUT$. The caller must have already acquired a console
71/// (via AttachConsole, AllocConsole, etc.).
72void os_reattach_console_stdio(void)
73{
74  os_enable_ctrl_c();
75  os_redirect_stdin_to_conin();
76  os_redirect_stdout_stderr_to_conout();
77}
78
79/// Detach from the current console and switch stdio to a hidden private one.
80///
81/// Used when an embedded server must outlive its parent console, while keeping
82/// CONIN$/CONOUT$ and ConPTY functional for :terminal and stdio writes.
83void os_swap_to_hidden_console(void)
84{
85  FreeConsole();
86  AllocConsole();
87  ShowWindow(GetConsoleWindow(), SW_HIDE);
88  os_reattach_console_stdio();
89}
90
91/// Resets Windows console icon if we got an original one on startup.
92void os_icon_reset(void)
93{
94  if (hWnd == NULL) {
95    return;
96  }
97
98  if (hOrigIconSmall) {
99    SendMessage(hWnd, WM_SETICON, (WPARAM)ICON_SMALL, (LPARAM)hOrigIconSmall);
100  }
101  if (hOrigIcon) {
102    SendMessage(hWnd, WM_SETICON, (WPARAM)ICON_BIG, (LPARAM)hOrigIcon);
103  }
104}
105
106/// Sets Nvim logo as Windows console icon.
107///
108/// Saves the original icon so it can be restored at exit.
109void os_icon_init(void)
110{
111  if ((hWnd = GetConsoleWindow()) == NULL) {
112    return;
113  }
114
115  char *vimruntime = os_getenv("VIMRUNTIME");
116  if (vimruntime != NULL) {
117    snprintf(NameBuff, MAXPATHL, "%s/neovim.ico", vimruntime);
118    if (!os_path_exists(NameBuff)) {
119      WLOG("neovim.ico not found: %s", NameBuff);
120    } else {
121      HICON hVimIcon = LoadImage(NULL, NameBuff, IMAGE_ICON, 64, 64,
122                                 LR_LOADFROMFILE | LR_LOADMAP3DCOLORS);
123      hOrigIconSmall = (HICON)SendMessage(hWnd, WM_SETICON, (WPARAM)ICON_SMALL, (LPARAM)hVimIcon);
124      hOrigIcon = (HICON)SendMessage(hWnd, WM_SETICON, (WPARAM)ICON_BIG, (LPARAM)hVimIcon);
125    }
126    xfree(vimruntime);
127  }
128}
129
130/// Saves the original Windows console title.
131void os_title_save(void)
132{
133  GetConsoleTitle(origTitle, sizeof(origTitle));
134}
135
136/// Resets the original Windows console title.
137void os_title_reset(void)
138{
139  SetConsoleTitle(origTitle);
140}
141
142#if !defined(ENABLE_VIRTUAL_TERMINAL_PROCESSING)
143# define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
144#endif
145/// Guesses the terminal-type.  Calls SetConsoleMode() and uv_set_vterm_state()
146/// if appropriate.
147///
148/// @param[in,out] term Name of the guessed terminal, statically-allocated
149/// @param out_fd stdout file descriptor
150void os_tty_guess_term(const char **term, int out_fd)
151{
152  bool conemu_ansi = strequal(os_getenv_noalloc("ConEmuANSI"), "ON");
153  bool vtp = false;
154
155  HANDLE handle = (HANDLE)_get_osfhandle(out_fd);
156  DWORD dwMode;
157  if (handle != INVALID_HANDLE_VALUE && GetConsoleMode(handle, &dwMode)) {
158    dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
159    if (SetConsoleMode(handle, dwMode)) {
160      vtp = true;
161    }
162  }
163
164  if (*term == NULL) {
165    if (vtp) {
166      *term = "vtpcon";
167    } else if (conemu_ansi) {
168      *term = "conemu";
169    } else {
170      *term = "win32con";
171    }
172  }
173
174  if (conemu_ansi) {
175    uv_tty_set_vterm_state(UV_TTY_SUPPORTED);
176  }
177}