robin/laufey

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

akiyosifeat(terminal): try to find newer conpty.dll on Windows #40328d6a1b4110a

master
6.3 KiB200 linesraw
1#include <uv.h>
2
3#include "nvim/log.h"
4#include "nvim/os/os.h"
5#include "nvim/os/pty_conpty_win.h"
6#include "nvim/vim_defs.h"
7
8#ifndef EXTENDED_STARTUPINFO_PRESENT
9# define EXTENDED_STARTUPINFO_PRESENT 0x00080000
10#endif
11#ifndef PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE
12# define PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE 0x00020016
13#endif
14
15HRESULT(WINAPI *pCreatePseudoConsole)(COORD, HANDLE, HANDLE, DWORD, HPCON *);
16HRESULT(WINAPI *pResizePseudoConsole)(HPCON, COORD);
17void(WINAPI *pClosePseudoConsole)(HPCON);
18
19bool os_has_conpty_working(void)
20{
21  static TriState has_conpty = kNone;
22  if (has_conpty == kNone) {
23    has_conpty = os_dyn_conpty_init();
24  }
25
26  return has_conpty == kTrue;
27}
28
29TriState os_dyn_conpty_init(void)
30{
31  uv_lib_t conpty_lib;
32
33  if (!uv_dlopen("conpty.dll", &conpty_lib)) {
34    ILOG("Loaded new ConPTY from conpty.dll");
35  } else {
36    // uv_dlopen() allocates an error message even on failure, so close the
37    // handle before reusing conpty_lib for the system ConPTY fallback.
38    uv_dlclose(&conpty_lib);
39    if (uv_dlopen("kernel32.dll", &conpty_lib)) {
40      uv_dlclose(&conpty_lib);
41      return kFalse;
42    }
43    ILOG("Loaded system ConPTY from kernel32.dll");
44  }
45
46  static struct {
47    char *name;
48    FARPROC *ptr;
49  } conpty_entry[] = {
50    { "CreatePseudoConsole", (FARPROC *)&pCreatePseudoConsole },
51    { "ResizePseudoConsole", (FARPROC *)&pResizePseudoConsole },
52    { "ClosePseudoConsole", (FARPROC *)&pClosePseudoConsole },
53    { NULL, NULL }
54  };
55  for (int i = 0;
56       conpty_entry[i].name != NULL && conpty_entry[i].ptr != NULL; i++) {
57    if (uv_dlsym(&conpty_lib, conpty_entry[i].name, (void **)conpty_entry[i].ptr)) {
58      uv_dlclose(&conpty_lib);
59      return kFalse;
60    }
61  }
62  return kTrue;
63}
64
65conpty_t *os_conpty_init(char **in_name, char **out_name, uint16_t width, uint16_t height)
66{
67  static int count = 0;
68  conpty_t *conpty_object = xcalloc(1, sizeof(*conpty_object));
69  const char *emsg = NULL;
70  HANDLE in_read = INVALID_HANDLE_VALUE;
71  HANDLE out_write = INVALID_HANDLE_VALUE;
72  char buf[MAXPATHL];
73  SECURITY_ATTRIBUTES sa = { 0 };
74  const DWORD mode = PIPE_ACCESS_INBOUND
75                     | PIPE_ACCESS_OUTBOUND | FILE_FLAG_FIRST_PIPE_INSTANCE;
76
77  sa.nLength = sizeof(sa);
78  snprintf(buf, sizeof(buf), "\\\\.\\pipe\\nvim-term-in-%lld-%d",
79           os_get_pid(), count);
80  *in_name = xstrdup(buf);
81  if ((in_read = CreateNamedPipeA(*in_name,
82                                  mode,
83                                  PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,
84                                  1,
85                                  0,
86                                  0,
87                                  30000,
88                                  &sa)) == INVALID_HANDLE_VALUE) {
89    emsg = "create input pipe failed";
90    goto failed;
91  }
92  snprintf(buf, sizeof(buf), "\\\\.\\pipe\\nvim-term-out-%lld-%d",
93           os_get_pid(), count);
94  *out_name = xstrdup(buf);
95  if ((out_write = CreateNamedPipeA(*out_name,
96                                    mode,
97                                    PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,
98                                    1,
99                                    0,
100                                    0,
101                                    30000,
102                                    &sa)) == INVALID_HANDLE_VALUE) {
103    emsg = "create output pipe failed";
104    goto failed;
105  }
106  assert(width <= SHRT_MAX);
107  assert(height <= SHRT_MAX);
108  COORD size = { (int16_t)width, (int16_t)height };
109  HRESULT hr;
110  hr = pCreatePseudoConsole(size, in_read, out_write, 0, &conpty_object->pty);
111  if (FAILED(hr)) {
112    emsg = "create pseudo console failed";
113    goto failed;
114  }
115
116  conpty_object->si_ex.StartupInfo.cb = sizeof(conpty_object->si_ex);
117  size_t bytes_required;
118  InitializeProcThreadAttributeList(NULL, 1, 0,  &bytes_required);
119  conpty_object->si_ex.lpAttributeList =
120    (PPROC_THREAD_ATTRIBUTE_LIST)xmalloc(bytes_required);
121  if (!InitializeProcThreadAttributeList(conpty_object->si_ex.lpAttributeList,
122                                         1,
123                                         0,
124                                         &bytes_required)) {
125    emsg = "InitializeProcThreadAttributeList failed";
126    goto failed;
127  }
128  if (!UpdateProcThreadAttribute(conpty_object->si_ex.lpAttributeList,
129                                 0,
130                                 PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE,
131                                 conpty_object->pty,
132                                 sizeof(conpty_object->pty),
133                                 NULL,
134                                 NULL)) {
135    emsg = "UpdateProcThreadAttribute failed";
136    goto failed;
137  }
138  count++;
139  goto finished;
140
141failed:
142  ELOG("os_conpty_init:%s : error code: %d",
143       emsg, os_translate_sys_error((int)GetLastError()));
144  os_conpty_free(conpty_object);
145  conpty_object = NULL;
146finished:
147  if (in_read != INVALID_HANDLE_VALUE) {
148    CloseHandle(in_read);
149  }
150  if (out_write != INVALID_HANDLE_VALUE) {
151    CloseHandle(out_write);
152  }
153  return conpty_object;
154}
155
156bool os_conpty_spawn(conpty_t *conpty_object, HANDLE *proc_handle, wchar_t *name, wchar_t *cmd_line,
157                     wchar_t *cwd, wchar_t *env)
158{
159  PROCESS_INFORMATION pi = { 0 };
160  if (!CreateProcessW(name,
161                      cmd_line,
162                      NULL,
163                      NULL,
164                      false,
165                      EXTENDED_STARTUPINFO_PRESENT | CREATE_UNICODE_ENVIRONMENT,
166                      env,
167                      cwd,
168                      &conpty_object->si_ex.StartupInfo,
169                      &pi)) {
170    return false;
171  }
172  *proc_handle = pi.hProcess;
173  return true;
174}
175
176void os_conpty_set_size(conpty_t *conpty_object, uint16_t width, uint16_t height)
177{
178  assert(width <= SHRT_MAX);
179  assert(height <= SHRT_MAX);
180  COORD size = { (int16_t)width, (int16_t)height };
181  if (pResizePseudoConsole(conpty_object->pty, size) != S_OK) {
182    ELOG("ResizePseudoConsole failed: error code: %d",
183         os_translate_sys_error((int)GetLastError()));
184  }
185}
186
187void os_conpty_free(void *data)
188{
189  conpty_t *conpty_object = data;
190  if (conpty_object != NULL) {
191    if (conpty_object->si_ex.lpAttributeList != NULL) {
192      DeleteProcThreadAttributeList(conpty_object->si_ex.lpAttributeList);
193      xfree(conpty_object->si_ex.lpAttributeList);
194    }
195    if (conpty_object->pty != NULL) {
196      pClosePseudoConsole(conpty_object->pty);
197    }
198  }
199  xfree(conpty_object);
200}