From 54eb41cf3a824880fb0973badf9582bb0a17a982 Mon Sep 17 00:00:00 2001 From: nick black Date: Fri, 13 Dec 2019 20:25:48 -0500 Subject: [PATCH] panelreel-demo: done after demodelay * 5 #135 --- CMakeLists.txt | 2 +- src/demo/demo.h | 9 +++++++++ src/demo/panelreel.c | 32 ++++++++++++++++++++++---------- src/demo/widecolor.c | 2 +- 4 files changed, 33 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ee3761449..283ba795a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -75,7 +75,7 @@ target_compile_options(notcurses-demo ) target_compile_definitions(notcurses-demo PRIVATE - FORTIFY_SOURCE=2 + FORTIFY_SOURCE=2 _GNU_SOURCE ) # tiny proofs of concept, one binary per source file diff --git a/src/demo/demo.h b/src/demo/demo.h index 4dc8f4059..48b2c74e0 100644 --- a/src/demo/demo.h +++ b/src/demo/demo.h @@ -55,6 +55,15 @@ timespec_div(const struct timespec* ts, unsigned divisor, struct timespec* quots quots->tv_sec = ns / GIG; } +// divide the provided timespec 'ts' by 'multiplier' into 'product' +static inline void +timespec_mul(const struct timespec* ts, unsigned multiplier, struct timespec* product){ + uint64_t ns = timespec_to_ns(ts); + ns *= multiplier; + product->tv_nsec = ns % GIG; + product->tv_sec = ns / GIG; +} + #ifdef __cplusplus } #endif diff --git a/src/demo/panelreel.c b/src/demo/panelreel.c index 2f0cfed78..cbdef47c4 100644 --- a/src/demo/panelreel.c +++ b/src/demo/panelreel.c @@ -204,18 +204,27 @@ new_tabletctx(struct panelreel* pr, unsigned *id){ } static wchar_t -handle_input(struct notcurses* nc, struct panelreel* pr, int efd){ +handle_input(struct notcurses* nc, struct panelreel* pr, int efd, + const struct timespec* deadline){ struct pollfd fds[2] = { { .fd = STDIN_FILENO, .events = POLLIN, .revents = 0, }, { .fd = efd, .events = POLLIN, .revents = 0, }, }; + sigset_t sset; + sigemptyset(&sset); wchar_t key = -1; int pret; notcurses_render(nc); do{ - pret = poll(fds, sizeof(fds) / sizeof(*fds), -1); - if(pret < 0){ + struct timespec pollspec, cur; + clock_gettime(CLOCK_MONOTONIC, &cur); + timespec_subtract(&pollspec, deadline, &cur); + pret = ppoll(fds, sizeof(fds) / sizeof(*fds), &pollspec, &sset); + if(pret == 0){ + return 0; + }else if(pret < 0){ fprintf(stderr, "Error polling on stdin/eventfd (%s)\n", strerror(errno)); + return (wchar_t)-1; }else{ if(fds[0].revents & POLLIN){ key = notcurses_getc_blocking(nc); @@ -277,13 +286,11 @@ panelreel_demo_core(struct notcurses* nc, int efd, tabletctx** tctxs){ ncplane_cursor_move_yx(w, 1, 1); ncplane_printf(w, "a, b, c create tablets, DEL deletes, q quits."); // FIXME clrtoeol(); - /* - struct timespec fadets = { .tv_sec = 1, .tv_nsec = 0, }; - if(ncplane_fadein(panelreel_plane(pr), &fadets)){ - return NULL; - } - */ unsigned id = 0; + struct timespec deadline; + clock_gettime(CLOCK_MONOTONIC, &deadline); + ns_to_timespec((timespec_to_ns(&demodelay) * 5) + timespec_to_ns(&deadline), + &deadline); do{ ncplane_styles_set(w, 0); ncplane_set_fg_rgb(w, 197, 15, 31); @@ -293,7 +300,7 @@ panelreel_demo_core(struct notcurses* nc, int efd, tabletctx** tctxs){ // FIXME wclrtoeol(w); ncplane_set_fg_rgb(w, 0, 55, 218); wchar_t rw; - if((rw = handle_input(nc, pr, efd)) < 0){ + if((rw = handle_input(nc, pr, efd, &deadline)) <= 0){ done = true; break; } @@ -322,6 +329,11 @@ panelreel_demo_core(struct notcurses* nc, int efd, tabletctx** tctxs){ newtablet->next = *tctxs; *tctxs = newtablet; } + struct timespec cur; + clock_gettime(CLOCK_MONOTONIC, &cur); + if(timespec_subtract_ns(&cur, &deadline) >= 0){ + break; + } //panelreel_validate(w, pr); // do what, if not assert()ing? FIXME }while(!done); return pr; diff --git a/src/demo/widecolor.c b/src/demo/widecolor.c index 2cd8970ae..ec23473ec 100644 --- a/src/demo/widecolor.c +++ b/src/demo/widecolor.c @@ -1,6 +1,6 @@ #include #include -#include +#include #include #include #include