Skip to content

Commit

Permalink
Windows: better handling of input console mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
gwsw committed Oct 7, 2023
1 parent 4ad71af commit 06b944e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
10 changes: 4 additions & 6 deletions screen.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ public int sgr_mode; /* Honor ANSI sequences rather than using above
static DWORD init_console_output_mode;
extern DWORD init_console_input_mode;
extern DWORD curr_console_input_mode;
extern DWORD base_console_input_mode;
extern DWORD mouse_console_input_mode;
public int vt_enabled = -1; /* Is virtual terminal processing available? */
#endif
#else
Expand Down Expand Up @@ -285,10 +287,6 @@ extern int hilite_search;
#if MSDOS_COMPILER==WIN32C
extern int wscroll;
extern HANDLE tty;
#ifndef ENABLE_EXTENDED_FLAGS
#define ENABLE_EXTENDED_FLAGS 0x80
#define ENABLE_QUICK_EDIT_MODE 0x40
#endif
#else
extern int tty;
#endif
Expand Down Expand Up @@ -1699,7 +1697,7 @@ public void init_mouse(void)
ltputs(sc_s_mousecap, sc_height, putchr);
#else
#if MSDOS_COMPILER==WIN32C
curr_console_input_mode = (curr_console_input_mode | ENABLE_MOUSE_INPUT) & ~(ENABLE_QUICK_EDIT_MODE & init_console_input_mode);
curr_console_input_mode = mouse_console_input_mode;
SetConsoleMode(tty, curr_console_input_mode);
#endif
#endif
Expand All @@ -1715,7 +1713,7 @@ public void deinit_mouse(void)
ltputs(sc_e_mousecap, sc_height, putchr);
#else
#if MSDOS_COMPILER==WIN32C
curr_console_input_mode = (curr_console_input_mode & ~ENABLE_MOUSE_INPUT) | (ENABLE_QUICK_EDIT_MODE & init_console_input_mode);
curr_console_input_mode = base_console_input_mode;
SetConsoleMode(tty, curr_console_input_mode);
#endif
#endif
Expand Down
18 changes: 15 additions & 3 deletions ttyin.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,18 @@
#define _WIN32_WINNT 0x400
#endif
#include <windows.h>
#ifndef ENABLE_EXTENDED_FLAGS
#define ENABLE_EXTENDED_FLAGS 0x80
#define ENABLE_QUICK_EDIT_MODE 0x40
#endif
#ifndef ENABLE_VIRTUAL_TERMINAL_INPUT
#define ENABLE_VIRTUAL_TERMINAL_INPUT 0x0200
#endif
public HANDLE tty;
public DWORD init_console_input_mode;
public DWORD curr_console_input_mode;
public DWORD base_console_input_mode;
public DWORD mouse_console_input_mode;
#else
public int tty;
#endif
Expand Down Expand Up @@ -88,10 +97,13 @@ public void open_getchr(void)
tty = CreateFile("CONIN$", GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ, &sa,
OPEN_EXISTING, 0L, NULL);
/* Make sure we get Ctrl+C events. */
GetConsoleMode(tty, &init_console_input_mode);
curr_console_input_mode |= ENABLE_PROCESSED_INPUT | ENABLE_EXTENDED_FLAGS | (ENABLE_QUICK_EDIT_MODE & init_console_input_mode);
curr_console_input_mode &= ~(ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT);
/* base mode: ensure we get ctrl-C events, and don't get VT input. */
base_console_input_mode = (init_console_input_mode | ENABLE_PROCESSED_INPUT) & ~ENABLE_VIRTUAL_TERMINAL_INPUT;
/* mouse mode: enable mouse and disable quick edit. */
mouse_console_input_mode = (base_console_input_mode | ENABLE_MOUSE_INPUT | ENABLE_EXTENDED_FLAGS) & ~ENABLE_QUICK_EDIT_MODE;
/* Start with base mode. If --mouse is given, switch to mouse mode in init_mouse. */
curr_console_input_mode = base_console_input_mode;
SetConsoleMode(tty, curr_console_input_mode);
#else
#if MSDOS_COMPILER
Expand Down

0 comments on commit 06b944e

Please sign in to comment.