robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
d6a1b4110a
master
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{ 21static TriState has_conpty = kNone ; 22if (has_conpty == kNone ) { 23has_conpty = os_dyn_conpty_init (); 24 } 25 26return has_conpty == kTrue ; 27} 28 29TriState os_dyn_conpty_init (void ) 30{ 31uv_lib_t conpty_lib ; 32 33if (!uv_dlopen ("conpty.dll" ,& conpty_lib )) { 34ILOG ("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. 38uv_dlclose (& conpty_lib ); 39if (uv_dlopen ("kernel32.dll" ,& conpty_lib )) { 40uv_dlclose (& conpty_lib ); 41return kFalse ; 42 } 43ILOG ("Loaded system ConPTY from kernel32.dll" ); 44 } 45 46static struct { 47char * name ; 48FARPROC * ptr ; 49 }conpty_entry []= { 50 {"CreatePseudoConsole" , (FARPROC * )& pCreatePseudoConsole }, 51 {"ResizePseudoConsole" , (FARPROC * )& pResizePseudoConsole }, 52 {"ClosePseudoConsole" , (FARPROC * )& pClosePseudoConsole }, 53 {NULL ,NULL } 54 }; 55for (int i = 0 ; 56conpty_entry [i ].name != NULL && conpty_entry [i ].ptr != NULL ;i ++ ) { 57if (uv_dlsym (& conpty_lib ,conpty_entry [i ].name , (void ** )conpty_entry [i ].ptr )) { 58uv_dlclose (& conpty_lib ); 59return kFalse ; 60 } 61 } 62return kTrue ; 63} 64 65conpty_t * os_conpty_init (char ** in_name ,char ** out_name ,uint16_t width ,uint16_t height ) 66{ 67static int count = 0 ; 68conpty_t * conpty_object = xcalloc (1 ,sizeof (* conpty_object )); 69const char * emsg = NULL ; 70HANDLE in_read = INVALID_HANDLE_VALUE ; 71HANDLE out_write = INVALID_HANDLE_VALUE ; 72char buf [MAXPATHL ]; 73SECURITY_ATTRIBUTES sa = {0 }; 74const DWORD mode = PIPE_ACCESS_INBOUND 75 |PIPE_ACCESS_OUTBOUND |FILE_FLAG_FIRST_PIPE_INSTANCE ; 76 77sa .nLength = sizeof (sa ); 78snprintf (buf ,sizeof (buf ),"\\\\.\\pipe\\nvim-term-in-%lld-%d" , 79os_get_pid (),count ); 80* in_name = xstrdup (buf ); 81if ((in_read = CreateNamedPipeA (* in_name , 82mode , 83PIPE_TYPE_BYTE |PIPE_READMODE_BYTE |PIPE_WAIT , 841 , 850 , 860 , 8730000 , 88& sa ))== INVALID_HANDLE_VALUE ) { 89emsg = "create input pipe failed" ; 90 gotofailed ; 91 } 92snprintf (buf ,sizeof (buf ),"\\\\.\\pipe\\nvim-term-out-%lld-%d" , 93os_get_pid (),count ); 94* out_name = xstrdup (buf ); 95if ((out_write = CreateNamedPipeA (* out_name , 96mode , 97PIPE_TYPE_BYTE |PIPE_READMODE_BYTE |PIPE_WAIT , 981 , 990 , 1000 , 10130000 , 102& sa ))== INVALID_HANDLE_VALUE ) { 103emsg = "create output pipe failed" ; 104 gotofailed ; 105 } 106assert (width <=SHRT_MAX ); 107assert (height <=SHRT_MAX ); 108COORD size = { (int16_t )width , (int16_t )height }; 109HRESULT hr ; 110hr = pCreatePseudoConsole (size ,in_read ,out_write ,0 ,& conpty_object -> pty ); 111if (FAILED (hr )) { 112emsg = "create pseudo console failed" ; 113 gotofailed ; 114 } 115 116conpty_object -> si_ex .StartupInfo .cb = sizeof (conpty_object -> si_ex ); 117size_t bytes_required ; 118InitializeProcThreadAttributeList (NULL ,1 ,0 ,& bytes_required ); 119conpty_object -> si_ex .lpAttributeList = 120 (PPROC_THREAD_ATTRIBUTE_LIST )xmalloc (bytes_required ); 121if (!InitializeProcThreadAttributeList (conpty_object -> si_ex .lpAttributeList , 1221 , 1230 , 124& bytes_required )) { 125emsg = "InitializeProcThreadAttributeList failed" ; 126 gotofailed ; 127 } 128if (!UpdateProcThreadAttribute (conpty_object -> si_ex .lpAttributeList , 1290 , 130PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE , 131conpty_object -> pty , 132sizeof (conpty_object -> pty ), 133NULL , 134NULL )) { 135emsg = "UpdateProcThreadAttribute failed" ; 136 gotofailed ; 137 } 138count ++ ; 139 gotofinished ; 140 141failed : 142ELOG ("os_conpty_init:%s : error code: %d" , 143emsg ,os_translate_sys_error ((int )GetLastError ())); 144os_conpty_free (conpty_object ); 145conpty_object = NULL ; 146finished : 147if (in_read != INVALID_HANDLE_VALUE ) { 148CloseHandle (in_read ); 149 } 150if (out_write != INVALID_HANDLE_VALUE ) { 151CloseHandle (out_write ); 152 } 153return conpty_object ; 154} 155 156bool os_conpty_spawn (conpty_t * conpty_object ,HANDLE * proc_handle ,wchar_t * name ,wchar_t * cmd_line , 157wchar_t * cwd ,wchar_t * env ) 158{ 159PROCESS_INFORMATION pi = {0 }; 160if (!CreateProcessW (name , 161cmd_line , 162NULL , 163NULL , 164 false, 165EXTENDED_STARTUPINFO_PRESENT |CREATE_UNICODE_ENVIRONMENT , 166env , 167cwd , 168& conpty_object -> si_ex .StartupInfo , 169& pi )) { 170return false; 171 } 172* proc_handle = pi .hProcess ; 173return true; 174} 175 176void os_conpty_set_size (conpty_t * conpty_object ,uint16_t width ,uint16_t height ) 177{ 178assert (width <=SHRT_MAX ); 179assert (height <=SHRT_MAX ); 180COORD size = { (int16_t )width , (int16_t )height }; 181if (pResizePseudoConsole (conpty_object -> pty ,size )!= S_OK ) { 182ELOG ("ResizePseudoConsole failed: error code: %d" , 183os_translate_sys_error ((int )GetLastError ())); 184 } 185} 186 187void os_conpty_free (void * data ) 188{ 189conpty_t * conpty_object = data ; 190if (conpty_object != NULL ) { 191if (conpty_object -> si_ex .lpAttributeList != NULL ) { 192DeleteProcThreadAttributeList (conpty_object -> si_ex .lpAttributeList ); 193xfree (conpty_object -> si_ex .lpAttributeList ); 194 } 195if (conpty_object -> pty != NULL ) { 196pClosePseudoConsole (conpty_object -> pty ); 197 } 198 } 199xfree (conpty_object ); 200}