Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/terminal.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,34 @@ char32_t Terminal::read_unicode_character( int escDelay_ ) {
}
}

#else // #ifndef _WIN32

/**
* Read a character from the Windows console and return the Unicode (char32_t)
* character it encodes. `ReadConsoleW` hands over UTF-16, from which a surrogate
* pair is one code point.
*
* @return char32_t Unicode character, or zero if the console cannot be read
*/
char32_t Terminal::read_unicode_character( int ) {
wchar_t units[2] = {};
DWORD read( 0 );

if ( !ReadConsoleW( _consoleIn, &units[0], 1, &read, nullptr ) || ( read != 1 ) ) {
return 0;
}

/* A high surrogate is only half of a code point; the low half follows it. */
if ( ( units[0] >= 0xD800 ) && ( units[0] <= 0xDBFF ) ) {
if ( !ReadConsoleW( _consoleIn, &units[1], 1, &read, nullptr ) || ( read != 1 ) ) {
return 0;
}
return static_cast<char32_t>( 0x10000 + ( ( units[0] - 0xD800 ) << 10 ) + ( units[1] - 0xDC00 ) );
}

return static_cast<char32_t>( units[0] );
}

#endif // #ifndef _WIN32

void beep(int fd_) {
Expand Down
5 changes: 3 additions & 2 deletions src/terminal.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,10 @@ public:
void jump_cursor( int, int );
void set_cursor_visible( bool );
void set_escdelay(int delay);
#ifndef _WIN32
/* A positive delay limits the wait, zero waits indefinitely, a negative one does not wait. */
/* A positive delay limits the wait, zero waits indefinitely, a negative one does not wait.
* On Windows the delay is ignored and the call blocks until the console delivers a character. */
char32_t read_unicode_character( int escDelay_ = 0 );
#ifndef _WIN32
int read_verbatim( char32_t*, int );
int install_window_change_handler( void );
#endif
Expand Down
Loading