Moved 'eatbytes' back into core code, put a code ptr in the termkey struct as a "protected" method

This commit is contained in:
Paul LeoNerd Evans
2008-10-07 22:22:59 +01:00
parent 8b7c2b5d4f
commit 145dca73b2
3 changed files with 43 additions and 32 deletions

View File

@@ -13,6 +13,9 @@ static struct termkey_driver *drivers[] = {
NULL,
};
// Forwards for the "protected" methods
static void eatbytes(termkey_t *tk, size_t count);
termkey_t *termkey_new_full(int fd, int flags, size_t buffsize, int waittime)
{
termkey_t *tk = malloc(sizeof(*tk));
@@ -64,6 +67,8 @@ termkey_t *termkey_new_full(int fd, int flags, size_t buffsize, int waittime)
for(i = 0; i < tk->nkeynames; i++)
tk->keynames[i] = NULL;
tk->method.eatbytes = &eatbytes;
for(i = 0; drivers[i]; i++) {
void *driver_info = (*drivers[i]->new_driver)(tk);
if(!driver_info)
@@ -133,6 +138,25 @@ int termkey_getwaittime(termkey_t *tk)
return tk->waittime;
}
static void eatbytes(termkey_t *tk, size_t count)
{
if(count >= tk->buffcount) {
tk->buffstart = 0;
tk->buffcount = 0;
return;
}
tk->buffstart += count;
tk->buffcount -= count;
size_t halfsize = tk->buffsize / 2;
if(tk->buffstart > halfsize) {
memcpy(tk->buffer, tk->buffer + halfsize, halfsize);
tk->buffstart -= halfsize;
}
}
termkey_result termkey_getkey(termkey_t *tk, termkey_key *key)
{
return (*tk->driver.getkey)(tk, key);