diff options
author | Paul LeoNerd Evans <leonerd@leonerd.org.uk> | 2008-10-07 22:22:59 +0100 |
---|---|---|
committer | Paul LeoNerd Evans <leonerd@leonerd.org.uk> | 2008-10-07 22:22:59 +0100 |
commit | 145dca73b24f361d54c48a9ad689abf64663c684 (patch) | |
tree | 501f45ea14252a5a9483f5b387bd94abea4c057e /termkey.c | |
parent | 8b7c2b5d4f79cf600df60a583accd04fb273c300 (diff) | |
download | termo-145dca73b24f361d54c48a9ad689abf64663c684.tar.gz termo-145dca73b24f361d54c48a9ad689abf64663c684.tar.xz termo-145dca73b24f361d54c48a9ad689abf64663c684.zip |
Moved 'eatbytes' back into core code, put a code ptr in the termkey struct as a "protected" method
Diffstat (limited to 'termkey.c')
-rw-r--r-- | termkey.c | 24 |
1 files changed, 24 insertions, 0 deletions
@@ -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); |