=== modified file 'termkey-internal.h' --- termkey-internal.h 2008-11-09 19:58:11 +0000 +++ termkey-internal.h 2008-11-08 17:56:44 +0000 @@ -23,13 +23,6 @@ int modifier_set; }; -struct termkey_drivernode; -struct termkey_drivernode { - struct termkey_driver *driver; - void *info; - struct termkey_drivernode *next; -}; - struct termkey { int fd; int flags; @@ -51,7 +44,8 @@ // There are 32 C0 codes struct keyinfo c0[32]; - struct termkey_drivernode *drivers; + struct termkey_driver driver; + void *driver_info; // Now some "protected" methods for the driver to call but which we don't // want exported as real symbols in the library === modified file 'termkey.c' --- termkey.c 2008-11-09 19:58:11 +0000 +++ termkey.c 2008-11-09 19:48:06 +0000 @@ -157,38 +157,25 @@ const char *term = getenv("TERM"); - struct termkey_drivernode *tail = NULL; - for(i = 0; drivers[i]; i++) { - void *info = (*drivers[i]->new_driver)(tk, term); - if(!info) + void *driver_info = (*drivers[i]->new_driver)(tk, term); + if(!driver_info) continue; -#ifdef DEBUG - fprintf(stderr, "Loading the %s driver\n", drivers[i]->name); -#endif - - struct termkey_drivernode *thisdrv = malloc(sizeof(*thisdrv)); - if(!thisdrv) - goto abort_free_drivers; - - thisdrv->driver = drivers[i]; - thisdrv->info = info; - thisdrv->next = NULL; - - if(!tail) - tk->drivers = thisdrv; - else - tail->next = thisdrv; - - tail = thisdrv; + tk->driver = *(drivers[i]); + tk->driver_info = driver_info; + break; } - if(!tk->drivers) { + if(!tk->driver_info) { fprintf(stderr, "Unable to find a terminal driver\n"); goto abort_free_keynames; } +#ifdef DEBUG + fprintf(stderr, "Using the %s driver\n", tk->driver.name); +#endif + if(!(flags & TERMKEY_FLAG_NOTERMIOS)) { struct termios termios; if(tcgetattr(fd, &termios) == 0) { @@ -202,21 +189,11 @@ } } - struct termkey_drivernode *p; - for(p = tk->drivers; p; p = p->next) - if(p->driver->start_driver) - (*p->driver->start_driver)(tk, p->info); + if(tk->driver.start_driver) + (*tk->driver.start_driver)(tk, tk->driver_info); return tk; -abort_free_drivers: - for(p = tk->drivers; p; ) { - (*p->driver->free_driver)(p->info); - struct termkey_drivernode *next = p->next; - free(p); - p = next; - } - abort_free_keynames: free(tk->keynames); @@ -239,23 +216,16 @@ free(tk->buffer); tk->buffer = NULL; free(tk->keynames); tk->keynames = NULL; - struct termkey_drivernode *p; - for(p = tk->drivers; p; ) { - (*p->driver->free_driver)(p->info); - struct termkey_drivernode *next = p->next; - free(p); - p = next; - } + (*tk->driver.free_driver)(tk->driver_info); + tk->driver_info = NULL; /* Be nice to GC'ers, etc */ free(tk); } void termkey_destroy(termkey_t *tk) { - struct termkey_drivernode *p; - for(p = tk->drivers; p; p = p->next) - if(p->driver->stop_driver) - (*p->driver->stop_driver)(tk, p->info); + if(tk->driver.stop_driver) + (*tk->driver.stop_driver)(tk, tk->driver_info); if(tk->restore_termios_valid) tcsetattr(tk->fd, TCSANOW, &tk->restore_termios); @@ -565,39 +535,28 @@ termkey_result termkey_getkey(termkey_t *tk, termkey_key *key) { - int again = 0; - #ifdef DEBUG fprintf(stderr, "getkey(): buffer "); print_buffer(tk); fprintf(stderr, "\n"); #endif - termkey_result ret; - struct termkey_drivernode *p; - for(p = tk->drivers; p; p = p->next) { - ret = (p->driver->getkey)(tk, p->info, key, 0); + termkey_result ret = (*tk->driver.getkey)(tk, tk->driver_info, key, 0); #ifdef DEBUG - fprintf(stderr, "Driver %s yields %s\n", p->driver->name, res2str(ret)); + fprintf(stderr, "Driver %s yields %s\n", tk->driver.name, res2str(ret)); #endif - switch(ret) { - case TERMKEY_RES_KEY: - case TERMKEY_RES_EOF: - return ret; + switch(ret) { + case TERMKEY_RES_KEY: + case TERMKEY_RES_EOF: + case TERMKEY_RES_AGAIN: + return ret; - case TERMKEY_RES_AGAIN: - again = 1; - /* fallthrough */ - case TERMKEY_RES_NONE: - break; - } + case TERMKEY_RES_NONE: + break; } - if(again) - return TERMKEY_RES_AGAIN; - ret = getkey_simple(tk, key, 0); #ifdef DEBUG @@ -615,24 +574,20 @@ fprintf(stderr, "\n"); #endif - termkey_result ret; - struct termkey_drivernode *p; - for(p = tk->drivers; p; p = p->next) { - ret = (p->driver->getkey)(tk, p->info, key, 1); + termkey_result ret = (*tk->driver.getkey)(tk, tk->driver_info, key, 1); #ifdef DEBUG - fprintf(stderr, "Driver %s yields %s\n", p->driver->name, res2str(ret)); + fprintf(stderr, "Driver %s yields %s\n", tk->driver.name, res2str(ret)); #endif - switch(ret) { - case TERMKEY_RES_KEY: - case TERMKEY_RES_EOF: - return ret; + switch(ret) { + case TERMKEY_RES_KEY: + case TERMKEY_RES_EOF: + return ret; - case TERMKEY_RES_AGAIN: - case TERMKEY_RES_NONE: - break; - } + case TERMKEY_RES_AGAIN: + case TERMKEY_RES_NONE: + break; } ret = getkey_simple(tk, key, 1);