From f4b12223e2cbf586f84147432b607129e4845e98 Mon Sep 17 00:00:00 2001 From: Bill Richardson Date: Wed, 4 Jun 2014 18:04:41 -0700 Subject: Tweaks to lightbar simulation Improve the get_time() function, add support for GNU readline if desired. BUG=none BRANCH=ToT TEST=manual cd extra make ./lightbar Change-Id: Iedf84253b0e616a6a1b502415a487a2e6248cb2e Signed-off-by: Bill Richardson Reviewed-on: https://chromium-review.googlesource.com/202698 Reviewed-by: Randall Spangler --- extra/Makefile | 8 ++++++++ extra/input.c | 38 ++++++++++++++++++++++++++++++++++++-- extra/main.c | 26 ++++++++++++++++++++++++-- extra/simulation.h | 1 + 4 files changed, 69 insertions(+), 4 deletions(-) diff --git a/extra/Makefile b/extra/Makefile index 0b6c7f7cf7..e7ef73ec00 100644 --- a/extra/Makefile +++ b/extra/Makefile @@ -6,10 +6,18 @@ PROG= lightbar HEADERS= simulation.h SRCS= main.c windows.c input.c ../common/lightbar.c +# comment this out if you don't have libreadline installed +HAS_GNU_READLINE=1 + INCLUDE= -I. -I../include CFLAGS= -g -Wall -Werror -pthread ${INCLUDE} -DLIGHTBAR_SIMULATION LDFLAGS = -lX11 -lxcb -lrt +ifneq ($(HAS_GNU_READLINE),) +CFLAGS += -DHAS_GNU_READLINE +LDFLAGS += -lreadline +endif + all: ${PROG} ${PROG} : ${SRCS} ${HEADERS} Makefile diff --git a/extra/input.c b/extra/input.c index dcb2a98891..fe4f7cd19f 100644 --- a/extra/input.c +++ b/extra/input.c @@ -11,6 +11,40 @@ #include "simulation.h" +#ifdef HAS_GNU_READLINE +#include +#include + +char *get_input(const char *prompt) +{ + static char *line; + + if (line) { + free(line); + line = 0; + } + + line = readline(prompt); + + if (line && *line) + add_history(line); + + return line; +} + +#else /* no readline */ + +char *get_input(const char *prompt) +{ + static char mybuf[80]; + char *got; + printf("%s", prompt); + got = fgets(mybuf, sizeof(mybuf), stdin); + return got; +} + +#endif /* HAS_GNU_READLINE */ + void *entry_input(void *ptr) { char *got, buf[80]; @@ -20,9 +54,9 @@ void *entry_input(void *ptr) int ret; do { - printf("lightbar%% "); - got = fgets(buf, sizeof(buf), stdin); + got = get_input("lightbar% "); if (got) { + strcpy(buf, got); argc = 0; argv[argc++] = "lightbar"; word = str = buf; diff --git a/extra/main.c b/extra/main.c index 9c9e26d08d..6433f49557 100644 --- a/extra/main.c +++ b/extra/main.c @@ -5,12 +5,14 @@ */ #include #include +#include #include #include #include #include #include #include +#include #include "simulation.h" @@ -28,6 +30,7 @@ int main(int argc, char *argv[]) printf("\nLook at the README file.\n"); printf("Click in the window.\n"); printf("Type \"help\" for commands.\n\n"); + fflush(stdout); init_windows(); @@ -77,7 +80,7 @@ uint32_t task_wait_event(int timeout_us) if (timeout_us > 0) { clock_gettime(CLOCK_REALTIME, &t); - timespec_incr(&t, 0, timeout_us * TS_USEC); + timespec_incr(&t, timeout_us / SECOND, timeout_us * TS_USEC); if (ETIMEDOUT == pthread_cond_timedwait(&task_cond, &task_mutex, &t)) @@ -124,9 +127,28 @@ void cprintf(int zero, const char *fmt, ...) free(newfmt); } +void cprints(int zero, const char *fmt, ...) +{ + va_list ap; + + printf("[TT "); + va_start(ap, fmt); + vprintf(fmt, ap); + va_end(ap); + printf("]\n"); +} + timestamp_t get_time(void) { - timestamp_t ret = { .val = 0UL }; + static struct timespec t_start; + struct timespec t; + timestamp_t ret; + + if (!t_start.tv_sec) + clock_gettime(CLOCK_REALTIME, &t_start); + clock_gettime(CLOCK_REALTIME, &t); + ret.val = (t.tv_sec - t_start.tv_sec) * SECOND + + (t.tv_nsec - t_start.tv_nsec) / TS_USEC; return ret; } diff --git a/extra/simulation.h b/extra/simulation.h index 7ec55ce0cd..a746ca8549 100644 --- a/extra/simulation.h +++ b/extra/simulation.h @@ -47,6 +47,7 @@ int fake_consolecmd_lightbar(int argc, char *argv[]); /* Non-standard standard library functions */ void cprintf(int zero, const char *fmt, ...); +void cprints(int zero, const char *fmt, ...); #define ccprintf(fmt...) cprintf(0, fmt) #define strtoi strtol -- cgit v1.2.1