From e3eca8d51a7d6c0676b973713a1ff043eb0095c7 Mon Sep 17 00:00:00 2001 From: Paul LeoNerd Evans Date: Mon, 6 Oct 2008 23:36:14 +0100 Subject: Use vtable in driver to indirect calls to the CSI driver --- driver-csi.c | 13 ++++++++++--- termkey-internal.h | 7 ++++--- termkey.c | 14 +++++--------- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/driver-csi.c b/driver-csi.c index a3e3e7e..a6f5241 100644 --- a/driver-csi.c +++ b/driver-csi.c @@ -24,7 +24,7 @@ typedef struct { struct keyinfo *csifuncs; } termkey_csi; -void *termkeycsi_new_driver(termkey_t *tk) +static void *new_driver(termkey_t *tk) { termkey_csi *csi = malloc(sizeof *csi); @@ -50,7 +50,7 @@ void *termkeycsi_new_driver(termkey_t *tk) return csi; } -void termkeycsi_free_driver(void *private) +static void free_driver(void *private) { termkey_csi *csi = private; @@ -324,7 +324,7 @@ static termkey_result getkey_ss3(termkey_t *tk, size_t introlen, termkey_key *ke return TERMKEY_RES_KEY; } -termkey_result termkeycsi_getkey(termkey_t *tk, termkey_key *key) +static termkey_result getkey(termkey_t *tk, termkey_key *key) { if(tk->buffcount == 0) return tk->is_closed ? TERMKEY_RES_EOF : TERMKEY_RES_NONE; @@ -580,3 +580,10 @@ termkey_keysym termkey_register_csifunc_full(termkey_t *tk, termkey_type type, t return sym; } + +struct termkey_driver termkey_driver_csi = { + .new_driver = new_driver, + .free_driver = free_driver, + + .getkey = getkey, +}; diff --git a/termkey-internal.h b/termkey-internal.h index e9fe1fd..7934306 100644 --- a/termkey-internal.h +++ b/termkey-internal.h @@ -8,8 +8,9 @@ struct termkey_driver { - void *(*new_driver)(void); - void (*free_driver)(void *); + void *(*new_driver)(termkey_t *tk); + void (*free_driver)(void *); + termkey_result (*getkey)(termkey_t *tk, termkey_key *key); }; struct termkey { @@ -34,6 +35,6 @@ struct termkey { void *driver_info; }; -void *termkeycsi_new_driver(termkey_t *t); +extern struct termkey_driver termkey_driver_csi; #endif diff --git a/termkey.c b/termkey.c index 393606b..662c6ff 100644 --- a/termkey.c +++ b/termkey.c @@ -8,12 +8,6 @@ #include -// TODO: Move these into t->driver -void *termkeycsi_new_driver(termkey_t *tk); -void termkeycsi_free_driver(void *private); -termkey_result termkeycsi_getkey(termkey_t *tk, termkey_key *key); -// END TODO - termkey_t *termkey_new_full(int fd, int flags, size_t buffsize, int waittime) { termkey_t *tk = malloc(sizeof(*tk)); @@ -65,7 +59,9 @@ 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->driver_info = termkeycsi_new_driver(tk); + tk->driver = termkey_driver_csi; + + tk->driver_info = (*tk->driver.new_driver)(tk); // Special built-in names termkey_register_keyname(tk, TERMKEY_SYM_NONE, "NONE"); @@ -168,7 +164,7 @@ void termkey_free(termkey_t *tk) free(tk->buffer); tk->buffer = NULL; free(tk->keynames); tk->keynames = NULL; - termkeycsi_free_driver(tk->driver_info); + (*tk->driver.free_driver)(tk->driver_info); tk->driver_info = NULL; /* Be nice to GC'ers, etc */ free(tk); @@ -194,7 +190,7 @@ int termkey_getwaittime(termkey_t *tk) termkey_result termkey_getkey(termkey_t *tk, termkey_key *key) { - return termkeycsi_getkey(tk, key); + return (*tk->driver.getkey)(tk, key); } termkey_result termkey_getkey_force(termkey_t *tk, termkey_key *key) -- cgit v1.2.3-70-g09d2