From 145dca73b24f361d54c48a9ad689abf64663c684 Mon Sep 17 00:00:00 2001 From: Paul LeoNerd Evans Date: Tue, 7 Oct 2008 22:22:59 +0100 Subject: Moved 'eatbytes' back into core code, put a code ptr in the termkey struct as a "protected" method --- termkey.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'termkey.c') diff --git a/termkey.c b/termkey.c index 6c246a4..18cac6c 100644 --- a/termkey.c +++ b/termkey.c @@ -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); -- cgit v1.2.3-54-g00ecf