Create  Edit  Diff  FrontPage  Index  Search  Changes  Login

readline for win32

static int readline_getc(FILE *);
static int
readline_getc(FILE *input)
{
    rb_io_t *ifp = 0;
    VALUE c;
    if (!readline_instream) return rl_getc(input);
    GetOpenFile(readline_instream, ifp);
    if (rl_instream != ifp->stdio_file) return rl_getc(input);
#if !defined(_WIN32)
    c = rb_funcall(readline_instream, id_getbyte, 0, 0);
    if (NIL_P(c)) return EOF;
    return NUM2CHR(c);
#else
    {
        INPUT_RECORD ir;
        int n;
        static int prior_key = '0';
        for (;;) {
            if (prior_key > 0xff) {
                prior_key = rl_getc(ifp->stdio_file);
                return prior_key;
           }
           if (PeekConsoleInput((HANDLE)_get_osfhandle(ifp->fd), &ir, 1, &n)
                && n == 1) {
                if (ir.EventType == KEY_EVENT && ir.Event.KeyEvent.bKeyDown) {
                    prior_key = rl_getc(ifp->stdio_file);
                    return prior_key;
                } else {
                    ReadConsoleInput((HANDLE)_get_osfhandle(ifp->fd), &ir, 1, &n);
                }
            } else {
                HANDLE h = (HANDLE)_get_osfhandle(ifp->fd);
                rb_w32_wait_events(&h, 1, 10);
            }
        }
    }
#endif
}
Last modified:2010/09/10 23:21:59
Keyword(s):
References: