From 0bff959eed1b47d6da50f95447409026471d2e8e Mon Sep 17 00:00:00 2001 From: nick black Date: Wed, 5 Feb 2020 07:50:55 -0500 Subject: [PATCH] demo: don't allow HUD to drive rendering --- src/demo/input.c | 10 +++++----- src/lib/notcurses.c | 25 +++++++++++++++++++------ 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/demo/input.c b/src/demo/input.c index d5bd6302f..4f253be99 100644 --- a/src/demo/input.c +++ b/src/demo/input.c @@ -66,7 +66,7 @@ pass_along(const ncinput* ni){ } static int -handle_mouse(struct notcurses* nc, const ncinput* ni){ +handle_mouse(const ncinput* ni){ if(ni->id != NCKEY_BUTTON1 && ni->id != NCKEY_RELEASE){ return 0; } @@ -76,9 +76,9 @@ handle_mouse(struct notcurses* nc, const ncinput* ni){ }else{ ret = hud_grab(ni->y, ni->x); } - if(ret == 0){ - ret = demo_render(nc); - } + // do not render here. the demos, if coded properly, will be regularly + // rendering (if via demo_nanosleep() if nothing else). rendering based off + // HUD movements can cause disruptions due to the main thread being unready. return ret; } @@ -92,7 +92,7 @@ ultramegaok_demo(void* vnc){ continue; } if(nckey_mouse_p(ni.id)){ - handle_mouse(nc, &ni); + handle_mouse(&ni); }else{ if(ni.id == 'q'){ interrupt_demo(); diff --git a/src/lib/notcurses.c b/src/lib/notcurses.c index 3fc03f5cb..7f7c2ec0b 100644 --- a/src/lib/notcurses.c +++ b/src/lib/notcurses.c @@ -289,14 +289,12 @@ ncplane_create(notcurses* nc, int rows, int cols, int yoff, int xoff){ p->channels = 0; egcpool_init(&p->pool); cell_init(&p->basecell); - // FIXME we ought lock the notcurses struct here, but that breaks inside of - // e.g. ncplane_dup(). the latter ought be purely ncplane-based locking! - //pthread_mutex_lock(&nc->lock); + pthread_mutex_lock(&nc->lock); p->z = nc->top; nc->top = p; p->nc = nc; nc->stats.fbbytes += fbsize; - //pthread_mutex_unlock(&nc->lock); + pthread_mutex_unlock(&nc->lock); return p; } @@ -357,7 +355,8 @@ ncplane_cursor_move_yx_locked(ncplane* n, int y, int x){ } ncplane* ncplane_dup(ncplane* n, void* opaque){ - ncplane_lock(n); + // FIXME need ncplane-level locking around n; notcurses-level locking breaks + // on calls to ncplane_destroy()/ncplane_new() int dimy = n->leny; int dimx = n->lenx; int aty = n->absy; @@ -378,7 +377,6 @@ ncplane* ncplane_dup(ncplane* n, void* opaque){ memcpy(newn->fb, n->fb, sizeof(*n->fb) * dimy * dimy); } } - ncplane_unlock(n); return newn; } @@ -1197,52 +1195,66 @@ advance_cursor(ncplane* n, int cols){ // 'n' ends up above 'above' int ncplane_move_above_unsafe(ncplane* restrict n, ncplane* restrict above){ + ncplane_lock(n); if(n->z == above){ + ncplane_unlock(n); return 0; } ncplane** an = find_above_ncplane(n); if(an == NULL){ + ncplane_unlock(n); return -1; } ncplane** aa = find_above_ncplane(above); if(aa == NULL){ + ncplane_unlock(n); return -1; } *an = n->z; // splice n out n->z = above; // attach above below n *aa = n; // spline n in above + ncplane_unlock(n); return 0; } // 'n' ends up below 'below' int ncplane_move_below_unsafe(ncplane* restrict n, ncplane* restrict below){ + ncplane_lock(n); if(below->z == n){ + ncplane_unlock(n); return 0; } ncplane** an = find_above_ncplane(n); if(an == NULL){ + ncplane_unlock(n); return -1; } *an = n->z; // splice n out n->z = below->z; // reattach subbelow list to n below->z = n; // splice n in below + ncplane_unlock(n); return 0; } int ncplane_move_top(ncplane* n){ + ncplane_lock(n); ncplane** an = find_above_ncplane(n); if(an == NULL){ + ncplane_unlock(n); return -1; } *an = n->z; // splice n out n->z = n->nc->top; n->nc->top = n; + ncplane_unlock(n); return 0; } int ncplane_move_bottom(ncplane* n){ + ncplane_lock(n); ncplane** an = find_above_ncplane(n); if(an == NULL){ + ncplane_unlock(n); return -1; } *an = n->z; // splice n out @@ -1252,6 +1264,7 @@ int ncplane_move_bottom(ncplane* n){ } *an = n; n->z = NULL; + ncplane_unlock(n); return 0; }