robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f46857d1df
master
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{ 24SetConsoleCtrlHandler (NULL , false); 25} 26 27int os_open_conin_fd (void ) 28{ 29const HANDLE conin_handle = CreateFile ("CONIN$" , 30GENERIC_READ |GENERIC_WRITE , 31FILE_SHARE_READ |FILE_SHARE_WRITE , 32 (LPSECURITY_ATTRIBUTES )NULL , 33OPEN_EXISTING ,0 , (HANDLE )NULL ); 34assert (conin_handle != INVALID_HANDLE_VALUE ); 35int conin_fd = _open_osfhandle ((intptr_t )conin_handle ,_O_RDONLY ); 36assert (conin_fd != -1 ); 37return conin_fd ; 38} 39 40void os_clear_hwnd (void ) 41{ 42hWnd = NULL ; 43} 44 45void os_redirect_stdin_to_conin (void ) 46{ 47close (STDIN_FILENO ); 48const int conin_fd = os_open_conin_fd (); 49assert (conin_fd == STDIN_FILENO ); 50} 51 52void os_redirect_stdout_stderr_to_conout (void ) 53{ 54const HANDLE conout_handle = 55CreateFile ("CONOUT$" , 56GENERIC_READ |GENERIC_WRITE , 57FILE_SHARE_READ |FILE_SHARE_WRITE , 58 (LPSECURITY_ATTRIBUTES )NULL , 59OPEN_EXISTING ,0 , (HANDLE )NULL ); 60assert (conout_handle != INVALID_HANDLE_VALUE ); 61close (STDOUT_FILENO ); 62const int conout_fd = _open_osfhandle ((intptr_t )conout_handle ,0 ); 63assert (conout_fd == STDOUT_FILENO ); 64close (STDERR_FILENO ); 65const int conerr_fd = _open_osfhandle ((intptr_t )conout_handle ,0 ); 66assert (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{ 74os_enable_ctrl_c (); 75os_redirect_stdin_to_conin (); 76os_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{ 85FreeConsole (); 86AllocConsole (); 87ShowWindow (GetConsoleWindow (),SW_HIDE ); 88os_reattach_console_stdio (); 89} 90 91/// Resets Windows console icon if we got an original one on startup. 92void os_icon_reset (void ) 93{ 94if (hWnd == NULL ) { 95return ; 96 } 97 98if (hOrigIconSmall ) { 99SendMessage (hWnd ,WM_SETICON , (WPARAM )ICON_SMALL , (LPARAM )hOrigIconSmall ); 100 } 101if (hOrigIcon ) { 102SendMessage (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{ 111if ((hWnd = GetConsoleWindow ())== NULL ) { 112return ; 113 } 114 115char * vimruntime = os_getenv ("VIMRUNTIME" ); 116if (vimruntime != NULL ) { 117snprintf (NameBuff ,MAXPATHL ,"%s/neovim.ico" ,vimruntime ); 118if (!os_path_exists (NameBuff )) { 119WLOG ("neovim.ico not found: %s" ,NameBuff ); 120 }else { 121HICON hVimIcon = LoadImage (NULL ,NameBuff ,IMAGE_ICON ,64 ,64 , 122LR_LOADFROMFILE |LR_LOADMAP3DCOLORS ); 123hOrigIconSmall = (HICON )SendMessage (hWnd ,WM_SETICON , (WPARAM )ICON_SMALL , (LPARAM )hVimIcon ); 124hOrigIcon = (HICON )SendMessage (hWnd ,WM_SETICON , (WPARAM )ICON_BIG , (LPARAM )hVimIcon ); 125 } 126xfree (vimruntime ); 127 } 128} 129 130/// Saves the original Windows console title. 131void os_title_save (void ) 132{ 133GetConsoleTitle (origTitle ,sizeof (origTitle )); 134} 135 136/// Resets the original Windows console title. 137void os_title_reset (void ) 138{ 139SetConsoleTitle (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{ 152bool conemu_ansi = strequal (os_getenv_noalloc ("ConEmuANSI" ),"ON" ); 153bool vtp = false; 154 155HANDLE handle = (HANDLE )_get_osfhandle (out_fd ); 156DWORD dwMode ; 157if (handle != INVALID_HANDLE_VALUE && GetConsoleMode (handle ,& dwMode )) { 158dwMode |=ENABLE_VIRTUAL_TERMINAL_PROCESSING ; 159if (SetConsoleMode (handle ,dwMode )) { 160vtp = true; 161 } 162 } 163 164if (* term == NULL ) { 165if (vtp ) { 166* term = "vtpcon" ; 167 }else if (conemu_ansi ) { 168* term = "conemu" ; 169 }else { 170* term = "win32con" ; 171 } 172 } 173 174if (conemu_ansi ) { 175uv_tty_set_vterm_state (UV_TTY_SUPPORTED ); 176 } 177}