From c5acdaaef05a5e1fb53e77f61d5aa0d5495f0c43 Mon Sep 17 00:00:00 2001 From: nick black Date: Mon, 16 Dec 2019 18:13:38 -0500 Subject: [PATCH] don't allow moving a plane above/below itself #71 --- include/notcurses.h | 27 ++++++++++++++++++++++++--- src/lib/notcurses.c | 4 ++-- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/include/notcurses.h b/include/notcurses.h index 2fa7f2e15..aaff9a10b 100644 --- a/include/notcurses.h +++ b/include/notcurses.h @@ -345,11 +345,32 @@ API void ncplane_yx(const struct ncplane* n, int* RESTRICT y, int* RESTRICT x); API int ncplane_move_top(struct ncplane* n); API int ncplane_move_bottom(struct ncplane* n); +// Splice ncplane 'n' out of the z-buffer, and reinsert it above 'above'. +API int ncplane_move_above_unsafe(struct ncplane* RESTRICT n, + struct ncplane* RESTRICT above); + +static inline int +ncplane_move_above(struct ncplane* n, struct ncplane* above){ + if(n == above){ + return -1; + } + return ncplane_move_above_unsafe(n, above); +} + // Splice ncplane 'n' out of the z-buffer, and reinsert it below 'below'. -API int ncplane_move_below(struct ncplane* RESTRICT n, struct ncplane* RESTRICT below); +API int ncplane_move_below_unsafe(struct ncplane* RESTRICT n, + struct ncplane* RESTRICT below); -// Splice ncplane 'n' out of the z-buffer, and reinsert it above 'above'. -API int ncplane_move_above(struct ncplane* RESTRICT n, struct ncplane* RESTRICT above); +static inline int +ncplane_move_below(struct ncplane* n, struct ncplane* below){ + if(n == below){ + return -1; + } + return ncplane_move_below_unsafe(n, below); +} + +// Return the plane above this one, or NULL if this is at the top. +API struct ncplane* ncplane_below(struct ncplane* n); // Return the plane below this one, or NULL if this is at the bottom. API struct ncplane* ncplane_below(struct ncplane* n); diff --git a/src/lib/notcurses.c b/src/lib/notcurses.c index 481e4cf15..0515a83a2 100644 --- a/src/lib/notcurses.c +++ b/src/lib/notcurses.c @@ -1174,7 +1174,7 @@ visible_cell(cell* c, int y, int x, ncplane* n, bool* damage){ // Call with c->gcluster == 3, falpha == 3, balpha == 0, *retp == topplane. // 'n' ends up above 'above' -int ncplane_move_above(ncplane* restrict n, ncplane* restrict above){ +int ncplane_move_above_unsafe(ncplane* restrict n, ncplane* restrict above){ ncplane** an = find_above_ncplane(n); if(an == NULL){ return -1; @@ -1190,7 +1190,7 @@ int ncplane_move_above(ncplane* restrict n, ncplane* restrict above){ } // 'n' ends up below 'below' -int ncplane_move_below(ncplane* restrict n, ncplane* restrict below){ +int ncplane_move_below_unsafe(ncplane* restrict n, ncplane* restrict below){ ncplane** an = find_above_ncplane(n); if(an == NULL){ return -1;