diff options
Diffstat (limited to 'lib/keystroke.c')
-rw-r--r-- | lib/keystroke.c | 92 |
1 files changed, 49 insertions, 43 deletions
diff --git a/lib/keystroke.c b/lib/keystroke.c index c6eb811e..2b5d8128 100644 --- a/lib/keystroke.c +++ b/lib/keystroke.c @@ -19,8 +19,7 @@ * Boston, MA 02111-1307, USA. */ -#include <stdbool.h> -#include <string.h> +#include "misc.h" #include "keystroke.h" @@ -28,7 +27,6 @@ #include "memory.h" #include "mqueue.h" -#include "zassert.h" /*============================================================================== */ @@ -262,17 +260,19 @@ enum stream_state kst_iac_option, /* waiting for option (just seen IAC X) */ kst_iac_sub, /* waiting for IAC SE */ } ; +typedef enum stream_state stream_state_t ; struct keystroke_state { - enum stream_state state ; - unsigned len ; - uint8_t raw[keystroke_max_len] ; + stream_state_t state ; + uint len ; + uint8_t raw[keystroke_max_len] ; } ; +typedef struct keystroke_state keystroke_state_t ; struct keystroke_stream { - vio_fifo_t fifo ; /* the keystrokes */ + vio_fifo fifo ; /* the keystrokes */ keystroke_callback* iac_callback ; void* iac_callback_context ; @@ -280,14 +280,15 @@ struct keystroke_stream uint8_t CSI ; /* CSI character value (if any) */ bool eof_met ; /* nothing more to come */ + bool timed_out ; /* eof because timed out */ bool steal_this ; /* steal current keystroke when complete */ bool iac ; /* last character was an IAC */ - struct keystroke_state in ; /* current keystroke being collected */ + keystroke_state_t in ; /* current keystroke being collected */ - struct keystroke_state pushed_in ; + keystroke_state_t pushed_in ; /* keystroke interrupted by IAC */ } ; @@ -301,7 +302,8 @@ enum { keystroke_buffer_len = 2000 } ; /* should be plenty ! */ static void keystroke_in_push(keystroke_stream stream, uint8_t u) ; static void keystroke_in_pop(keystroke_stream stream) ; -inline static int keystroke_set_null(keystroke_stream stream, keystroke stroke); +inline static bool keystroke_set_null(keystroke_stream stream, + keystroke stroke) ; inline static uint8_t keystroke_get_byte(keystroke_stream stream) ; inline static void keystroke_add_raw(keystroke_stream stream, uint8_t u) ; static void keystroke_put_char(keystroke_stream stream, uint32_t u) ; @@ -317,7 +319,7 @@ static void keystroke_steal_esc(keystroke steal, keystroke_stream stream, uint8_t u) ; static void keystroke_steal_csi(keystroke steal, keystroke_stream stream, uint8_t u) ; -static void keystroke_put(keystroke_stream stream, enum keystroke_type type, +static void keystroke_put(keystroke_stream stream, keystroke_compound_t type, bool broken, uint8_t* bytes, int len) ; /*============================================================================== @@ -353,12 +355,13 @@ keystroke_stream_new(uint8_t csi_char, keystroke_callback* iac_callback, * iac_callback = NULL -- none * iac_callback_context = NULL -- none * - * eof_met = false -- no EOF yet - * steal = false -- no stealing set - * iac = false -- last character was not an IAC + * eof_met = false -- no EOF yet + * timed_out = false -- not timed out yet + * steal = false -- no stealing set + * iac = false -- last character was not an IAC * - * in.state = kst_null - * in.len = 0 -- nothing in the buffer + * in.state = kst_null + * in.len = 0 -- nothing in the buffer * * pushed_in.state ) ditto * pushed_in.len ) @@ -368,7 +371,7 @@ keystroke_stream_new(uint8_t csi_char, keystroke_callback* iac_callback, stream->iac_callback = iac_callback ; stream->iac_callback_context = iac_callback_context ; - vio_fifo_init_new(&stream->fifo, keystroke_buffer_len) ; + stream->fifo = vio_fifo_new(keystroke_buffer_len) ; stream->CSI = (csi_char != '\0') ? csi_char : 0x1B ; @@ -385,7 +388,7 @@ keystroke_stream_free(keystroke_stream stream) { if (stream != NULL) { - vio_fifo_reset_keep(&stream->fifo) ; + stream->fifo = vio_fifo_free(stream->fifo) ; XFREE(MTYPE_KEY_STREAM, stream) ; } ; @@ -407,7 +410,7 @@ keystroke_stream_free(keystroke_stream stream) extern bool keystroke_stream_empty(keystroke_stream stream) { - return (stream == NULL) || vio_fifo_empty(&stream->fifo) ; + return (stream == NULL) || vio_fifo_empty(stream->fifo) ; } ; /*------------------------------------------------------------------------------ @@ -428,7 +431,7 @@ keystroke_stream_eof(keystroke_stream stream) * is converted to a broken keystroke and placed in the stream. * (So eof_met => no partial keystroke.) */ - return (stream == NULL) || (vio_fifo_empty(&stream->fifo) && stream->eof_met); + return (stream == NULL) || (vio_fifo_empty(stream->fifo) && stream->eof_met) ; } ; /*------------------------------------------------------------------------------ @@ -436,14 +439,15 @@ keystroke_stream_eof(keystroke_stream stream) * * * discard contents of the stream, including any partial keystroke. * - * * set the stream "eof_met". + * * set the stream "eof_met" with or without timed_out */ extern void -keystroke_stream_set_eof(keystroke_stream stream) +keystroke_stream_set_eof(keystroke_stream stream, bool timed_out) { - vio_fifo_reset_keep(&stream->fifo) ; + vio_fifo_clear(stream->fifo, true) ; /* and clear marks */ stream->eof_met = true ; /* essential information */ + stream->timed_out = timed_out ; /* variant of eof */ stream->steal_this = false ; /* keep tidy */ stream->iac = false ; @@ -472,7 +476,7 @@ keystroke_stream_set_eof(keystroke_stream stream) * If steal != NULL the keystroke will be set to the stolen keystroke. That * will be type ks_null if nothing was available, and may be knull_eof. * - * Note that never steals broken or truncated keystrokes. + * Note that never steals broken or truncated keystrokes, or IAC. * * Passing len == 0 and ptr == NULL signals EOF to the keystroke_stream. * @@ -495,6 +499,7 @@ keystroke_input(keystroke_stream stream, uint8_t* ptr, size_t len, if ((len == 0) && (ptr == NULL)) { stream->eof_met = true ; + stream->timed_out = false ; stream->steal_this = false ; /* Loop to deal with any pending IAC and partial keystroke. @@ -858,10 +863,10 @@ keystroke_in_pop(keystroke_stream stream) /*============================================================================== * Fetch next keystroke from keystroke stream * - * Returns: 1 => have a stroke type != ks_null - * 0 => stroke type is ks_null (may be EOF). + * Returns: true => have a stroke type != ks_null + * false => stroke type is ks_null (may be EOF). */ -extern int +extern bool keystroke_get(keystroke_stream stream, keystroke stroke) { int b ; @@ -869,7 +874,7 @@ keystroke_get(keystroke_stream stream, keystroke stroke) uint8_t* e ; /* Get first byte and deal with FIFO empty response */ - b = vio_fifo_get_byte(&stream->fifo) ; + b = vio_fifo_get_byte(stream->fifo) ; if (b < 0) return keystroke_set_null(stream, stroke) ; @@ -884,7 +889,7 @@ keystroke_get(keystroke_stream stream, keystroke stroke) stroke->len = 1 ; stroke->buf[0] = b ; - return 1 ; + return true ; } ; /* Sex the compound keystroke */ @@ -954,7 +959,7 @@ keystroke_get(keystroke_stream stream, keystroke stroke) zabort("unknown keystroke type") ; } ; - return 1 ; + return true ; } ; /*------------------------------------------------------------------------------ @@ -962,16 +967,17 @@ keystroke_get(keystroke_stream stream, keystroke stroke) * * Returns: 0 */ -inline static int +inline static bool keystroke_set_null(keystroke_stream stream, keystroke stroke) { stroke->type = ks_null ; - stroke->value = stream->eof_met ? knull_eof : knull_not_eof ; - + stroke->value = stream->eof_met ? (stream->timed_out ? knull_timed_out + : knull_eof) + : knull_not_eof ; stroke->flags = 0 ; stroke->len = 0 ; - return 0 ; + return false ; } ; /*------------------------------------------------------------------------------ @@ -983,7 +989,7 @@ keystroke_set_null(keystroke_stream stream, keystroke stroke) inline static uint8_t keystroke_get_byte(keystroke_stream stream) { - int b = vio_fifo_get_byte(&stream->fifo) ; + int b = vio_fifo_get_byte(stream->fifo) ; passert(b >= 0) ; @@ -1012,8 +1018,8 @@ keystroke_add_raw(keystroke_stream stream, uint8_t u) static void keystroke_put_char(keystroke_stream stream, uint32_t u) { - if (u < 0x80) - vio_fifo_put_byte(&stream->fifo, (uint8_t)u) ; + if (u < kf_compound) + vio_fifo_put_byte(stream->fifo, (uint8_t)u) ; else { uint8_t buf[4] ; @@ -1028,7 +1034,7 @@ keystroke_put_char(keystroke_stream stream, uint32_t u) } while (u != 0) ; - keystroke_put(stream, ks_char, 0, p, (buf + 4) - p) ; + keystroke_put(stream, ks_char, false, p, (buf + 4) - p) ; } ; } ; @@ -1126,12 +1132,12 @@ keystroke_clear_iac(keystroke_stream stream) } ; /*------------------------------------------------------------------------------ - * Store <first> <len> [<bytes>] + * Store <first> <len> [<bytes>] -- compound keystroke. * * If len == 0, bytes may be NULL */ static void -keystroke_put(keystroke_stream stream, enum keystroke_type type, bool broken, +keystroke_put(keystroke_stream stream, keystroke_compound_t type, bool broken, uint8_t* bytes, int len) { if (len > keystroke_max_len) @@ -1140,12 +1146,12 @@ keystroke_put(keystroke_stream stream, enum keystroke_type type, bool broken, type |= kf_truncated ; } ; - vio_fifo_put_byte(&stream->fifo, + vio_fifo_put_byte(stream->fifo, kf_compound | (broken ? kf_broken : 0) | type) ; - vio_fifo_put_byte(&stream->fifo, len) ; + vio_fifo_put_byte(stream->fifo, len) ; if (len > 0) - vio_fifo_put(&stream->fifo, (void*)bytes, len) ; + vio_fifo_put_bytes(stream->fifo, (void*)bytes, len) ; } ; /*------------------------------------------------------------------------------ |