diff src/os_win32.c @ 16451:7ae2396cef62 v8.1.1230

patch 8.1.1230: a lot of code is shared between vim.exe and gvim.exe commit https://github.com/vim/vim/commit/afde13b62b8fa25dac4635d5caee8d088b937ee0 Author: Bram Moolenaar <Bram@vim.org> Date: Sun Apr 28 19:46:49 2019 +0200 patch 8.1.1230: a lot of code is shared between vim.exe and gvim.exe Problem: A lot of code is shared between vim.exe and gvim.exe. Solution: Optionally put the shared code in vim.dll. (Ken Takata, closes #4287)
author Bram Moolenaar <Bram@vim.org>
date Sun, 28 Apr 2019 20:00:10 +0200
parents 973070a30381
children 4e9bea9b8025
line wrap: on
line diff
--- a/src/os_win32.c
+++ b/src/os_win32.c
@@ -151,7 +151,7 @@ typedef int LPSECURITY_ATTRIBUTES;
 # define wcsicmp(a, b) wcscmpi((a), (b))
 #endif
 
-#ifndef FEAT_GUI_MSWIN
+#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
 /* Win32 Console handles for input and output */
 static HANDLE g_hConIn  = INVALID_HANDLE_VALUE;
 static HANDLE g_hConOut = INVALID_HANDLE_VALUE;
@@ -179,7 +179,8 @@ static void gotoxy(unsigned x, unsigned 
 static void standout(void);
 static int s_cursor_visible = TRUE;
 static int did_create_conin = FALSE;
-#else
+#endif
+#ifdef FEAT_GUI_MSWIN
 static int s_dont_use_vimrun = TRUE;
 static int need_vimrun_warning = FALSE;
 static char *vimrun_path = "vimrun ";
@@ -193,7 +194,7 @@ static int conpty_working = 0;
 static int conpty_stable = 0;
 static void vtp_flag_init();
 
-#ifndef FEAT_GUI_MSWIN
+#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
 static int vtp_working = 0;
 static void vtp_init();
 static void vtp_exit();
@@ -227,7 +228,7 @@ static void reset_console_color_rgb(void
 # define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
 #endif
 
-#ifndef FEAT_GUI_MSWIN
+#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
 static int suppress_winsize = 1;	/* don't fiddle with console */
 #endif
 
@@ -235,7 +236,7 @@ static char_u *exe_path = NULL;
 
 static BOOL win8_or_later = FALSE;
 
-#ifndef FEAT_GUI_MSWIN
+#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
 /* Dynamic loading for portability */
 typedef struct _DYN_CONSOLE_SCREEN_BUFFER_INFOEX
 {
@@ -284,7 +285,7 @@ get_build_number(void)
     return ver;
 }
 
-#ifndef FEAT_GUI_MSWIN
+#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
 /*
  * Version of ReadConsoleInput() that works with IME.
  * Works around problems on Windows 8.
@@ -496,6 +497,28 @@ vimLoadLib(char *name)
     return dll;
 }
 
+#if defined(VIMDLL) || defined(PROTO)
+/*
+ * Check if the current executable file is for the GUI subsystem.
+ */
+    int
+mch_is_gui_executable(void)
+{
+    PBYTE		pImage = (PBYTE)GetModuleHandle(NULL);
+    PIMAGE_DOS_HEADER	pDOS = (PIMAGE_DOS_HEADER)pImage;
+    PIMAGE_NT_HEADERS	pPE;
+
+    if (pDOS->e_magic != IMAGE_DOS_SIGNATURE)
+	return FALSE;
+    pPE = (PIMAGE_NT_HEADERS)(pImage + pDOS->e_lfanew);
+    if (pPE->Signature != IMAGE_NT_SIGNATURE)
+	return FALSE;
+    if (pPE->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_GUI)
+	return TRUE;
+    return FALSE;
+}
+#endif
+
 #if defined(DYNAMIC_ICONV) || defined(DYNAMIC_GETTEXT) || defined(PROTO)
 /*
  * Get related information about 'funcname' which is imported by 'hInst'.
@@ -816,7 +839,7 @@ PlatformId(void)
     }
 }
 
-#ifndef FEAT_GUI_MSWIN
+#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
 
 #define SHIFT  (SHIFT_PRESSED)
 #define CTRL   (RIGHT_CTRL_PRESSED | LEFT_CTRL_PRESSED)
@@ -1116,7 +1139,7 @@ decode_key_event(
 /*
  * For the GUI the mouse handling is in gui_w32.c.
  */
-# ifdef FEAT_GUI_MSWIN
+# if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
     void
 mch_setmouse(int on UNUSED)
 {
@@ -1136,6 +1159,10 @@ mch_setmouse(int on)
 {
     DWORD cmodein;
 
+#  ifdef VIMDLL
+    if (gui.in_use)
+	return;
+#  endif
     if (!g_fMouseAvail)
 	return;
 
@@ -1454,6 +1481,11 @@ mch_update_cursor(void)
     int		idx;
     int		thickness;
 
+# ifdef VIMDLL
+    if (gui.in_use)
+	return;
+# endif
+
     /*
      * How the cursor is drawn depends on the current mode.
      */
@@ -1467,7 +1499,7 @@ mch_update_cursor(void)
 }
 #endif
 
-#ifndef FEAT_GUI_MSWIN	    /* this isn't used for the GUI */
+#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
 /*
  * Handle FOCUS_EVENT.
  */
@@ -1677,13 +1709,16 @@ WaitForChar(long msec, int ignore_input)
     return FALSE;
 }
 
-#ifndef FEAT_GUI_MSWIN
 /*
  * return non-zero if a character is available
  */
     int
 mch_char_avail(void)
 {
+# ifdef VIMDLL
+    if (gui.in_use)
+	return TRUE;
+# endif
     return WaitForChar(0L, FALSE);
 }
 
@@ -1694,10 +1729,13 @@ mch_char_avail(void)
     int
 mch_check_messages(void)
 {
+#  ifdef VIMDLL
+    if (gui.in_use)
+	return TRUE;
+#  endif
     return WaitForChar(0L, TRUE);
 }
 # endif
-#endif
 
 /*
  * Create the console input.  Used when reading stdin doesn't work.
@@ -1779,7 +1817,7 @@ mch_inchar(
     long	time UNUSED,
     int		tb_change_cnt UNUSED)
 {
-#ifndef FEAT_GUI_MSWIN	    /* this isn't used for the GUI */
+#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
 
     int		len;
     int		c;
@@ -1787,6 +1825,11 @@ mch_inchar(
     static char_u   typeahead[TYPEAHEADLEN];	/* previously typed bytes. */
     static int	    typeaheadlen = 0;
 
+# ifdef VIMDLL
+    if (gui.in_use)
+	return 0;
+# endif
+
     /* First use any typeahead that was kept because "buf" was too small. */
     if (typeaheadlen > 0)
 	goto theend;
@@ -2092,8 +2135,8 @@ bad_param_handler(const wchar_t *express
 /*
  * GUI version of mch_init().
  */
-    void
-mch_init(void)
+    static void
+mch_init_g(void)
 {
 #ifndef __MINGW32__
     extern int _fmode;
@@ -2160,7 +2203,9 @@ mch_init(void)
 }
 
 
-#else /* FEAT_GUI_MSWIN */
+#endif /* FEAT_GUI_MSWIN */
+
+#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
 
 #define SRWIDTH(sr) ((sr).Right - (sr).Left + 1)
 #define SRHEIGHT(sr) ((sr).Bottom - (sr).Top + 1)
@@ -2442,11 +2487,6 @@ static ConsoleBuffer g_cbNonTermcap = { 
 static ConsoleBuffer g_cbTermcap = { 0 };
 
 #ifdef FEAT_TITLE
-#ifdef __BORLANDC__
-typedef HWND (__stdcall *GETCONSOLEWINDOWPROC)(VOID);
-#else
-typedef HWND (WINAPI *GETCONSOLEWINDOWPROC)(VOID);
-#endif
 char g_szOrigTitle[256] = { 0 };
 HWND g_hWnd = NULL;	/* also used in os_mswin.c */
 static HICON g_hOrigIconSmall = NULL;
@@ -2563,8 +2603,8 @@ static DWORD g_cmodeout = 0;
 /*
  * non-GUI version of mch_init().
  */
-    void
-mch_init(void)
+    static void
+mch_init_c(void)
 {
 #ifndef FEAT_RESTORE_ORIG_SCREEN
     CONSOLE_SCREEN_BUFFER_INFO csbi;
@@ -2660,8 +2700,8 @@ mch_init(void)
  * Shut down and exit with status `r'
  * Careful: mch_exit() may be called before mch_init()!
  */
-    void
-mch_exit(int r)
+    static void
+mch_exit_c(int r)
 {
     exiting = TRUE;
 
@@ -2711,6 +2751,36 @@ mch_exit(int r)
 }
 #endif /* !FEAT_GUI_MSWIN */
 
+    void
+mch_init(void)
+{
+#ifdef VIMDLL
+    if (gui.starting)
+	mch_init_g();
+    else
+	mch_init_c();
+#elif defined(FEAT_GUI_MSWIN)
+    mch_init_g();
+#else
+    mch_init_c();
+#endif
+}
+
+    void
+mch_exit(int r)
+{
+#ifdef VIMDLL
+    if (gui.starting || gui.in_use)
+	mch_exit_g(r);
+    else
+	mch_exit_c(r);
+#elif defined(FEAT_GUI_MSWIN)
+    mch_exit_g(r);
+#else
+    mch_exit_c(r);
+#endif
+}
+
 /*
  * Do we have an interactive window?
  */
@@ -2721,9 +2791,13 @@ mch_check_win(
 {
     get_exe_name();
 
-#ifdef FEAT_GUI_MSWIN
+#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
     return OK;	    /* GUI always has a tty */
 #else
+# ifdef VIMDLL
+    if (gui.in_use)
+	return OK;
+# endif
     if (isatty(1))
 	return OK;
     return FAIL;
@@ -3446,7 +3520,7 @@ mch_free_acl(vim_acl_T acl)
 #endif
 }
 
-#ifndef FEAT_GUI_MSWIN
+#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
 
 /*
  * handler for ctrl-break, ctrl-c interrupts, and fatal events.
@@ -3516,6 +3590,10 @@ mch_settmode(int tmode)
     DWORD cmodeout;
     BOOL bEnableHandler;
 
+# ifdef VIMDLL
+    if (gui.in_use)
+	return;
+# endif
     GetConsoleMode(g_hConIn, &cmodein);
     GetConsoleMode(g_hConOut, &cmodeout);
     if (tmode == TMODE_RAW)
@@ -3528,7 +3606,7 @@ mch_settmode(int tmode)
 #endif
 	cmodeout &= ~(
 #ifdef FEAT_TERMGUICOLORS
-	    /* Do not turn off the ENABLE_PROCESSRD_OUTPUT flag when using
+	    /* Do not turn off the ENABLE_PROCESSED_OUTPUT flag when using
 	     * VTP. */
 	    ((vtp_working) ? 0 : ENABLE_PROCESSED_OUTPUT) |
 #else
@@ -3570,6 +3648,10 @@ mch_get_shellsize(void)
 {
     CONSOLE_SCREEN_BUFFER_INFO csbi;
 
+# ifdef VIMDLL
+    if (gui.in_use)
+	return OK;
+# endif
     if (!g_fTermcapMode && g_cbTermcap.IsValid)
     {
 	/*
@@ -3717,6 +3799,10 @@ mch_set_shellsize(void)
 {
     COORD coordScreen;
 
+# ifdef VIMDLL
+    if (gui.in_use)
+	return;
+# endif
     /* Don't change window size while still starting up */
     if (suppress_winsize != 0)
     {
@@ -3744,6 +3830,10 @@ mch_set_shellsize(void)
     void
 mch_new_shellsize(void)
 {
+# ifdef VIMDLL
+    if (gui.in_use)
+	return;
+# endif
     set_scroll_region(0, 0, Columns - 1, Rows - 1);
 }
 
@@ -4369,7 +4459,7 @@ mch_system_piped(char *cmd, int options)
 }
 
     static int
-mch_system(char *cmd, int options)
+mch_system_g(char *cmd, int options)
 {
     /* if we can pipe and the shelltemp option is off */
     if (!p_stmp)
@@ -4377,10 +4467,11 @@ mch_system(char *cmd, int options)
     else
 	return mch_system_classic(cmd, options);
 }
-#else
-
+#endif
+
+#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
     static int
-mch_system(char *cmd, int options)
+mch_system_c(char *cmd, int options)
 {
     int		ret;
     WCHAR	*wcmd;
@@ -4396,6 +4487,21 @@ mch_system(char *cmd, int options)
 
 #endif
 
+    static int
+mch_system(char *cmd, int options)
+{
+#ifdef VIMDLL
+    if (gui.in_use)
+	return mch_system_g(cmd, options);
+    else
+	return mch_system_c(cmd, options);
+#elif defined(FEAT_GUI_MSWIN)
+    return mch_system_g(cmd, options);
+#else
+    return mch_system_c(cmd, options);
+#endif
+}
+
 #if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
 /*
  * Use a terminal window to run a shell command in.
@@ -4527,7 +4633,11 @@ mch_call_shell(
 #endif
 #if defined(FEAT_GUI) && defined(FEAT_TERMINAL)
     /* TODO: make the terminal window work with input or output redirected. */
-    if (vim_strchr(p_go, GO_TERMINAL) != NULL
+    if (
+# ifdef VIMDLL
+	gui.in_use &&
+# endif
+	vim_strchr(p_go, GO_TERMINAL) != NULL
 	 && (options & (SHELL_FILTER|SHELL_DOOUT|SHELL_WRITE|SHELL_READ)) == 0)
     {
 	/* Use a terminal window to run the command in. */
@@ -4679,7 +4789,10 @@ mch_call_shell(
 	    {
 		x = -1;
 #ifdef FEAT_GUI_MSWIN
-		emsg(_("E371: Command not found"));
+# ifdef VIMDLL
+		if (gui.in_use)
+# endif
+		    emsg(_("E371: Command not found"));
 #endif
 	    }
 
@@ -4699,7 +4812,7 @@ mch_call_shell(
 	{
 	    cmdlen = (
 #ifdef FEAT_GUI_MSWIN
-		(!p_stmp ? 0 : STRLEN(vimrun_path)) +
+		(gui.in_use ? (!p_stmp ? 0 : STRLEN(vimrun_path)) : 0) +
 #endif
 		STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10);
 
@@ -4707,7 +4820,11 @@ mch_call_shell(
 	    if (newcmd != NULL)
 	    {
 #if defined(FEAT_GUI_MSWIN)
-		if (need_vimrun_warning)
+		if (
+# ifdef VIMDLL
+		    gui.in_use &&
+# endif
+		    need_vimrun_warning)
 		{
 		    char *msg = _("VIMRUN.EXE not found in your $PATH.\n"
 			"External commands will not pause after completion.\n"
@@ -4722,7 +4839,11 @@ mch_call_shell(
 		    vim_free(wtitle);
 		    need_vimrun_warning = FALSE;
 		}
-		if (!s_dont_use_vimrun && p_stmp)
+		if (
+# ifdef VIMDLL
+		    gui.in_use &&
+# endif
+		    !s_dont_use_vimrun && p_stmp)
 		    /* Use vimrun to execute the command.  It opens a console
 		     * window, which can be closed without killing Vim. */
 		    vim_snprintf((char *)newcmd, cmdlen, "%s%s%s %s %s",
@@ -4746,7 +4867,8 @@ mch_call_shell(
     /* Print the return value, unless "vimrun" was used. */
     if (x != 0 && !(options & SHELL_SILENT) && !emsg_silent
 #if defined(FEAT_GUI_MSWIN)
-		&& ((options & SHELL_DOOUT) || s_dont_use_vimrun || !p_stmp)
+	    && (gui.in_use ?
+		((options & SHELL_DOOUT) || s_dont_use_vimrun || !p_stmp) : 1)
 #endif
 	    )
     {
@@ -5278,7 +5400,7 @@ mch_clear_job(job_T *job)
 #endif
 
 
-#ifndef FEAT_GUI_MSWIN
+#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
 
 /*
  * Start termcap mode
@@ -5397,7 +5519,7 @@ termcap_mode_end(void)
 #endif /* FEAT_GUI_MSWIN */
 
 
-#ifdef FEAT_GUI_MSWIN
+#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
     void
 mch_write(
     char_u  *s UNUSED,
@@ -5694,7 +5816,7 @@ gotoxy(
 
 /*
  * Set the current text attribute = (foreground | background)
- * See ../doc/os_win32.txt for the numbers.
+ * See ../runtime/doc/os_win32.txt for the numbers.
  */
     static void
 textattr(WORD wAttr)
@@ -5924,6 +6046,11 @@ mch_write(
     char_u  *s,
     int	    len)
 {
+# ifdef VIMDLL
+    if (gui.in_use)
+	return;
+# endif
+
     s[len] = NUL;
 
     if (!term_console)
@@ -6222,9 +6349,16 @@ mch_delay(
     long    msec,
     int	    ignoreinput UNUSED)
 {
-#ifdef FEAT_GUI_MSWIN
+#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
     Sleep((int)msec);	    /* never wait for input */
 #else /* Console */
+# ifdef VIMDLL
+    if (gui.in_use)
+    {
+	Sleep((int)msec);	    /* never wait for input */
+	return;
+    }
+# endif
     if (ignoreinput)
 # ifdef FEAT_MZSCHEME
 	if (mzthreads_allowed() && p_mzq > 0 && msec > p_mzq)
@@ -6286,13 +6420,16 @@ mch_remove(char_u *name)
     void
 mch_breakcheck(int force)
 {
-#ifndef FEAT_GUI_MSWIN	    /* never used */
-    if (g_fCtrlCPressed || g_fCBrkPressed)
-    {
-	ctrl_break_was_pressed = g_fCBrkPressed;
-	g_fCtrlCPressed = g_fCBrkPressed = FALSE;
-	got_int = TRUE;
-    }
+#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
+# ifdef VIMDLL
+    if (!gui.in_use)
+# endif
+	if (g_fCtrlCPressed || g_fCBrkPressed)
+	{
+	    ctrl_break_was_pressed = g_fCBrkPressed;
+	    g_fCtrlCPressed = g_fCBrkPressed = FALSE;
+	    got_int = TRUE;
+	}
 #endif
 }
 
@@ -6308,7 +6445,7 @@ mch_total_mem(int special UNUSED)
     MEMORYSTATUSEX  ms;
 
     /* Need to use GlobalMemoryStatusEx() when there is more memory than
-     * what fits in 32 bits. But it's not always available. */
+     * what fits in 32 bits. */
     ms.dwLength = sizeof(MEMORYSTATUSEX);
     GlobalMemoryStatusEx(&ms);
     if (ms.ullAvailVirtual < ms.ullTotalPhys)
@@ -7098,17 +7235,22 @@ mch_setenv(char *var, char *value, int x
 vtp_flag_init(void)
 {
     DWORD   ver = get_build_number();
-#ifndef FEAT_GUI_MSWIN
+#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
     DWORD   mode;
     HANDLE  out;
 
-    out = GetStdHandle(STD_OUTPUT_HANDLE);
-
-    vtp_working = (ver >= VTP_FIRST_SUPPORT_BUILD) ? 1 : 0;
-    GetConsoleMode(out, &mode);
-    mode |= (ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
-    if (SetConsoleMode(out, mode) == 0)
-	vtp_working = 0;
+# ifdef VIMDLL
+    if (!gui.in_use)
+# endif
+    {
+	out = GetStdHandle(STD_OUTPUT_HANDLE);
+
+	vtp_working = (ver >= VTP_FIRST_SUPPORT_BUILD) ? 1 : 0;
+	GetConsoleMode(out, &mode);
+	mode |= (ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
+	if (SetConsoleMode(out, mode) == 0)
+	    vtp_working = 0;
+    }
 #endif
 
     if (ver >= CONPTY_FIRST_SUPPORT_BUILD)
@@ -7118,7 +7260,7 @@ vtp_flag_init(void)
 
 }
 
-#if !defined(FEAT_GUI_MSWIN) || defined(PROTO)
+#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL) || defined(PROTO)
 
     static void
 vtp_init(void)
@@ -7343,7 +7485,7 @@ is_conpty_stable(void)
     return conpty_stable;
 }
 
-#if !defined(FEAT_GUI_MSWIN) || defined(PROTO)
+#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL) || defined(PROTO)
     void
 resize_console_buf(void)
 {