demo: fix demo_getc() to use MAXSLEEP (12.5ms)

This commit is contained in:
nick black 2020-12-06 12:28:22 -05:00
parent 4cf4a4ce0b
commit 7140d1349c
No known key found for this signature in database
GPG Key ID: 5F43400C21CBFACC
3 changed files with 13 additions and 8 deletions

View File

@ -89,6 +89,7 @@ demo_getc_blocking(struct notcurses* nc, ncinput* ni){
/*-------------------------------time helpers----------------------------*/ /*-------------------------------time helpers----------------------------*/
#define GIG 1000000000ul #define GIG 1000000000ul
#define MAXSLEEP (GIG / 80) // nanoseconds
static inline uint64_t static inline uint64_t
timespec_to_ns(const struct timespec* ts){ timespec_to_ns(const struct timespec* ts){

View File

@ -26,6 +26,8 @@ static int plot_grab_y = -1;
// position of the plot *when grab started* // position of the plot *when grab started*
static int plot_pos_y; static int plot_pos_y;
#define FPSHZ 10
// how many columns for runtime? // how many columns for runtime?
#define HUD_ROWS (3 + 2) // 2 for borders #define HUD_ROWS (3 + 2) // 2 for borders
static const int HUD_COLS = 23 + 2; // 2 for borders static const int HUD_COLS = 23 + 2; // 2 for borders
@ -513,7 +515,7 @@ demo_nanosleep_abstime_ns(struct notcurses* nc, uint64_t deadline){
clock_gettime(CLOCK_MONOTONIC, &now); clock_gettime(CLOCK_MONOTONIC, &now);
while(deadline > timespec_to_ns(&now)){ while(deadline > timespec_to_ns(&now)){
fsleep.tv_sec = 0; fsleep.tv_sec = 0;
fsleep.tv_nsec = GIG / 100; fsleep.tv_nsec = MAXSLEEP;
if(deadline - timespec_to_ns(&now) < GIG / 100){ if(deadline - timespec_to_ns(&now) < GIG / 100){
fsleep.tv_nsec = deadline - timespec_to_ns(&now); fsleep.tv_nsec = deadline - timespec_to_ns(&now);
} }
@ -563,7 +565,7 @@ int demo_render(struct notcurses* nc){
if(!plot_hidden){ if(!plot_hidden){
ncplane_move_top(ncuplot_plane(plot)); ncplane_move_top(ncuplot_plane(plot));
} }
uint64_t ns = (timespec_to_ns(&ts) - plottimestart) / (GIG / 10); uint64_t ns = (timespec_to_ns(&ts) - plottimestart) / (GIG / FPSHZ);
ncuplot_add_sample(plot, ns, 1); ncuplot_add_sample(plot, ns, 1);
} }
if(menu){ if(menu){

View File

@ -47,20 +47,22 @@ handle_mouse(const ncinput* ni){
// absolute deadline, so convert it up. // absolute deadline, so convert it up.
char32_t demo_getc(struct notcurses* nc, const struct timespec* ts, ncinput* ni){ char32_t demo_getc(struct notcurses* nc, const struct timespec* ts, ncinput* ni){
struct timespec now; struct timespec now;
clock_gettime(CLOCK_REALTIME, &now);
uint64_t ns; uint64_t ns;
struct timespec abstime;
// yes, i'd like CLOCK_MONOTONIC too, but pthread_cond_timedwait() is based off // yes, i'd like CLOCK_MONOTONIC too, but pthread_cond_timedwait() is based off
// of crappy CLOCK_REALTIME :/ // of crappy CLOCK_REALTIME :/
// abstime shouldn't be further out than our maximum sleep time -- this can // abstime shouldn't be further out than our maximum sleep time -- this can
// lead to 0 frames output during the wait (happening now with zoo FIXME) // lead to 0 frames output during the wait (happening now with zoo FIXME)
if(ts){ if(ts){
clock_gettime(CLOCK_REALTIME, &now); ns = timespec_to_ns(ts);
ns = timespec_to_ns(&now) + timespec_to_ns(ts);
ns_to_timespec(ns, &abstime);
}else{ }else{
abstime.tv_sec = ~0; ns = MAXSLEEP;
abstime.tv_nsec = ~0;
} }
if(ns > MAXSLEEP){
ns = MAXSLEEP;
}
struct timespec abstime;
ns_to_timespec(ns + timespec_to_ns(&now), &abstime);
bool handoff = false; // does the input go back to the user? bool handoff = false; // does the input go back to the user?
char32_t id; char32_t id;
do{ do{