Have 'termkey_advisereadable' return a value indicating if it's likely useful to call it again

This commit is contained in:
Paul LeoNerd Evans 2008-02-10 19:23:18 +00:00
parent b3647c4861
commit c71e1928a2
2 changed files with 11 additions and 5 deletions

View File

@ -444,6 +444,7 @@ termkey_result termkey_getkey(termkey_t *tk, termkey_key *key)
case TERMKEY_RES_NONE:
case TERMKEY_RES_EOF:
case TERMKEY_RES_AGAIN:
break;
}
@ -569,17 +570,21 @@ void termkey_pushinput(termkey_t *tk, unsigned char *input, size_t inputlen)
tk->buffcount += inputlen;
}
void termkey_advisereadable(termkey_t *tk)
termkey_result termkey_advisereadable(termkey_t *tk)
{
unsigned char buffer[64]; // Smaller than the default size
size_t len = read(tk->fd, buffer, sizeof buffer);
if(len == -1 && errno == EAGAIN)
return;
else if(len < 1)
return TERMKEY_RES_NONE;
else if(len < 1) {
tk->is_closed = 1;
else
return TERMKEY_RES_NONE;
}
else {
termkey_pushinput(tk, buffer, len);
return TERMKEY_RES_AGAIN;
}
}
const char *termkey_describe_sym(termkey_t *tk, termkey_keysym code)

View File

@ -73,6 +73,7 @@ typedef enum {
TERMKEY_RES_NONE,
TERMKEY_RES_KEY,
TERMKEY_RES_EOF,
TERMKEY_RES_AGAIN,
} termkey_result;
enum {
@ -114,7 +115,7 @@ termkey_result termkey_waitkey(termkey_t *tk, termkey_key *key);
void termkey_pushinput(termkey_t *tk, unsigned char *input, size_t inputlen);
void termkey_advisereadable(termkey_t *tk);
termkey_result termkey_advisereadable(termkey_t *tk);
// Registration of keys and names
termkey_keysym termkey_register_keyname(termkey_t *tk, termkey_keysym code, const char *name);