From 05f73da2dac9e1e4787c10c96df997f2fb1e6a6c Mon Sep 17 00:00:00 2001 From: nick black Date: Mon, 29 Mar 2021 20:12:34 -0400 Subject: [PATCH] directmode: get cursor position if we need to for cursor move #1486 --- src/lib/direct.c | 52 +++++++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/src/lib/direct.c b/src/lib/direct.c index faa0e4e8d..8b7393e18 100644 --- a/src/lib/direct.c +++ b/src/lib/direct.c @@ -121,29 +121,6 @@ int ncdirect_cursor_disable(ncdirect* nc){ return term_emit(nc->tcache.civis, nc->ttyfp, true); } -int ncdirect_cursor_move_yx(ncdirect* n, int y, int x){ - if(y == -1){ // keep row the same, horizontal move only - if(!n->tcache.hpa){ - return -1; - } - return term_emit(tiparm(n->tcache.hpa, x), n->ttyfp, false); - }else if(x == -1){ // keep column the same, vertical move only - if(!n->tcache.vpa){ - return -1; - } - return term_emit(tiparm(n->tcache.vpa, y), n->ttyfp, false); - } - if(n->tcache.cup){ - return term_emit(tiparm(n->tcache.cup, y, x), n->ttyfp, false); - }else if(n->tcache.vpa && n->tcache.hpa){ - if(term_emit(tiparm(n->tcache.hpa, x), n->ttyfp, false) == 0 && - term_emit(tiparm(n->tcache.vpa, y), n->ttyfp, false) == 0){ - return 0; - } - } - return -1; -} - static int cursor_yx_get(int ttyfd, int* y, int* x){ if(writen(ttyfd, "\033[6n", 4) != 4){ @@ -208,6 +185,35 @@ cursor_yx_get(int ttyfd, int* y, int* x){ return 0; } +int ncdirect_cursor_move_yx(ncdirect* n, int y, int x){ + if(y == -1){ // keep row the same, horizontal move only + if(n->tcache.hpa){ + return term_emit(tiparm(n->tcache.hpa, x), n->ttyfp, false); + }else{ + if(cursor_yx_get(n->ctermfd, &y, NULL)){ + return -1; + } + } + }else if(x == -1){ // keep column the same, vertical move only + if(!n->tcache.vpa){ + return term_emit(tiparm(n->tcache.vpa, y), n->ttyfp, false); + }else{ + if(cursor_yx_get(n->ctermfd, NULL, &x)){ + return -1; + } + } + } + if(n->tcache.cup){ + return term_emit(tiparm(n->tcache.cup, y, x), n->ttyfp, false); + }else if(n->tcache.vpa && n->tcache.hpa){ + if(term_emit(tiparm(n->tcache.hpa, x), n->ttyfp, false) == 0 && + term_emit(tiparm(n->tcache.vpa, y), n->ttyfp, false) == 0){ + return 0; + } + } + return -1; +} + // an algorithm to detect inverted cursor reporting on terminals 2x2 or larger: // * get initial cursor position / push cursor position // * move right using cursor-independent routines