Skip to content

Commit

Permalink
Partial fix for issue #260 : getch() should return byte codes, meanin…
Browse files Browse the repository at this point in the history
…g characters past 256 should be expanded into multi-byte strings.
  • Loading branch information
Bill-Gray committed Mar 4, 2023
1 parent bfb6e33 commit bd50b2b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
4 changes: 2 additions & 2 deletions curses.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ Defined by this header:
#define PDC_VER_MINOR 3
#define PDC_VER_CHANGE 5
#define PDC_VER_YEAR 2023
#define PDC_VER_MONTH 01
#define PDC_VER_DAY 16
#define PDC_VER_MONTH 03
#define PDC_VER_DAY 04

#define PDC_STRINGIZE( x) #x
#define PDC_stringize( x) PDC_STRINGIZE( x)
Expand Down
45 changes: 42 additions & 3 deletions pdcurses/getch.c
Original file line number Diff line number Diff line change
Expand Up @@ -467,11 +467,11 @@ bool PDC_is_function_key( const int key)

#define WAIT_FOREVER -1

int wgetch(WINDOW *win)
static int _raw_wgetch(WINDOW *win)
{
int key = ERR, remaining_millisecs;

PDC_LOG(("wgetch() - called\n"));
PDC_LOG(("_raw_wgetch() - called\n"));

assert( SP);
assert( win);
Expand Down Expand Up @@ -701,6 +701,45 @@ int PDC_return_key_modifiers(bool flag)
return PDC_modifiers_set();
}

int wgetch(WINDOW *win)
{
#ifndef PDC_WIDE
return( _raw_wgetch( win));
#else
static unsigned char buffered[8];
static size_t n_buff = 0;
int rval;

if( n_buff)
{
size_t i;
rval = buffered[0];
n_buff--;
for( i = 0; i < n_buff; i++)
buffered[i] = buffered[i + 1];
}
else
{
rval = _raw_wgetch(win);
if( rval != ERR && (rval < 0 || rval > 127)
&& !PDC_is_function_key( rval))
{
wchar_t c = (wchar_t)rval;

n_buff = PDC_wcstombs( (char *)buffered, &c, 1);
if( (int)n_buff <= 0)
{
n_buff = 0;
rval = ERR;
}
else /* successfully converted to multi-byte string */
rval = wgetch( win);
}
}
return( rval);
#endif
}

#ifdef PDC_WIDE
int wget_wch(WINDOW *win, wint_t *wch)
{
Expand All @@ -712,7 +751,7 @@ int wget_wch(WINDOW *win, wint_t *wch)
if (!wch)
return ERR;

key = wgetch(win);
key = _raw_wgetch(win);

if (key == ERR)
return ERR;
Expand Down

0 comments on commit bd50b2b

Please sign in to comment.