implement notcurses_at_yx()

This commit is contained in:
nick black 2019-12-31 21:30:20 -05:00
parent 35aa7f6e85
commit 48177b8474
No known key found for this signature in database
GPG Key ID: 5F43400C21CBFACC
2 changed files with 23 additions and 22 deletions

View File

@ -171,13 +171,15 @@ int ncplane_at_cursor(ncplane* n, cell* c){
}
int ncplane_at_yx(ncplane* n, int y, int x, cell* c){
if(y >= n->leny || x >= n->lenx){
return true;
int ret = -1;
pthread_mutex_lock(&n->nc->lock);
if(y < n->leny && x < n->lenx){
if(y >= 0 && x >= 0){
ret = cell_duplicate(n, c, &n->fb[fbcellidx(n, y, x)]);
}
}
if(y < 0 || x < 0){
return true;
}
return cell_duplicate(n, c, &n->fb[fbcellidx(n, y, x)]);
pthread_mutex_unlock(&n->nc->lock);
return ret;
}
cell* ncplane_cell_ref_yx(ncplane* n, int y, int x){
@ -1515,22 +1517,6 @@ void ncplane_yx(const ncplane* n, int* y, int* x){
pthread_mutex_unlock(&n->nc->lock);
}
// copy the UTF8-encoded EGC out of the cell, whether simple or complex. the
// result is not tied to the ncplane, and persists across erases / destruction.
static inline char*
cell_egc_copy(const ncplane* n, const cell* c){
char* ret;
if(cell_simple_p(c)){
if( (ret = malloc(2)) ){
ret[0] = c->gcluster;
ret[1] = '\0';
}
}else{
ret = strdup(cell_extended_gcluster(n, c));
}
return ret;
}
void ncplane_erase(ncplane* n){
ncplane_lock(n);
// we must preserve the background, but a pure cell_duplicate() would be

View File

@ -619,3 +619,18 @@ int notcurses_render(notcurses* nc){
return ret;
}
char* notcurses_at_yx(notcurses* nc, int y, int x, cell* c){
char* egc = NULL;
pthread_mutex_lock(&nc->lock);
if(nc->lastframe){
if(y >= 0 && y < nc->lfdimy){
if(x >= 0 || x < nc->lfdimx){
const cell* srccell = &nc->lastframe[y * nc->lfdimx + x];
memcpy(c, srccell, sizeof(*c)); // unsafe copy of gcluster
egc = cell_egc_copy(nc->stdscr, srccell);
}
}
}
pthread_mutex_unlock(&nc->lock);
return egc;
}