From a037cdfe5a5ee590a8ff06dd1e5840f7e0892f61 Mon Sep 17 00:00:00 2001 From: nick black Date: Fri, 29 Nov 2019 00:20:30 -0500 Subject: [PATCH] add new notcurses-input binary #8 --- CMakeLists.txt | 17 +++++++++++++++++ include/notcurses.h | 23 +++++++++++++++-------- src/input/input.cpp | 16 ++++++++++++++++ src/lib/notcurses.c | 12 +++++++++++- 4 files changed, 59 insertions(+), 9 deletions(-) create mode 100644 src/input/input.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index c5af27bf6..7d5894b73 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -63,6 +63,22 @@ target_compile_options(notcurses-demo -Wall -Wextra -W -Wshadow ) +file(GLOB INPUTSRCS CONFIGURE_DEPENDS src/input/*.cpp) +add_executable(notcurses-input ${INPUTSRCS}) +target_include_directories(notcurses-input + PRIVATE + include + "${PROJECT_BINARY_DIR}/include" +) +target_link_libraries(notcurses-input + PRIVATE + notcurses +) +target_compile_options(notcurses-input + PRIVATE + -Wall -Wextra -W -Wshadow +) + file(GLOB VIEWSRCS CONFIGURE_DEPENDS src/view/*.cpp) add_executable(notcurses-view ${VIEWSRCS}) target_include_directories(notcurses-view @@ -135,6 +151,7 @@ install(FILES install(TARGETS notcurses-demo DESTINATION bin) install(TARGETS notcurses-view DESTINATION bin) +install(TARGETS notcurses-input DESTINATION bin) install(TARGETS notcurses LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} diff --git a/include/notcurses.h b/include/notcurses.h index d82f95387..ab8c83617 100644 --- a/include/notcurses.h +++ b/include/notcurses.h @@ -174,10 +174,9 @@ API int ncplane_move_below(struct ncplane* RESTRICT n, struct ncplane* RESTRICT // 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); -// Retrieve the topmost cell at this location on the screen, returning it in -// 'c'. If there is more than a byte of gcluster, it will be returned as a heap -// allocation in '*gclust', and '*c' will be 0x80. -API void notcurses_getc(const struct notcurses* n, cell* c, char** gclust); +// Retrieve the topmost cell at the cursor location on the specified plane, +// returning it in 'c'. This copy is safe to use until the ncplane is destroyed. +API void ncplane_at_cursor(const struct ncplane* n, cell* c); // Manipulate the opaque user pointer associated with this plane. // ncplane_set_userptr() returns the previous userptr after replacing @@ -212,10 +211,18 @@ API void ncplane_cursor_yx(const struct ncplane* n, int* RESTRICT y, // On failure, -1 is returned. API int ncplane_putc(struct ncplane* n, const cell* c); -// Retrieve the cell under this plane's cursor, returning it in 'c'. If there -// is more than a byte of gcluster, it will be returned as a heap allocation in -// '*gclust', and '*c' will be 0x80. Returns -1 on error, 0 on success. -API int ncplane_getc(const struct ncplane* n, cell* c, char** gclust); +// Return an input from stdin, if one is available. Note that we do *not* +// attempt to read an EGC in its entirety. 'c' will reference a single +// UTF-8-encoded Unicode codepoint. This is a non-blocking operation. If no +// input is available, 0 is returned. On other errors, -1 is returned. +// Otherwise, the number of bytes in the UTF-8 character are returned. +API int ncplane_getc(const struct ncplane* n, cell* c); + +// The same as ncplane_getc(), but blocking until input is read. It can still +// return early due to interruption by signal, in which case 0 is returned. On +// any other error, -1 is returned. Otherwise, the number of bytes in the UTF-8 +// character are returned. +API int ncplane_getc_blocking(const struct ncplane* n, cell* c); // Write a series of cells to the current location, using the current style. // They will be interpreted as a series of columns (according to the definition diff --git a/src/input/input.cpp b/src/input/input.cpp new file mode 100644 index 000000000..599d0b9d8 --- /dev/null +++ b/src/input/input.cpp @@ -0,0 +1,16 @@ +#include +#include + +int main(void){ + notcurses_options opts{}; + opts.outfp = stdout; + struct notcurses* nc = notcurses_init(&opts); + if(nc == NULL){ + return EXIT_FAILURE;; + } + // FIXME read input + if(notcurses_stop(nc)){ + return EXIT_FAILURE;; + } + return EXIT_SUCCESS; +} diff --git a/src/lib/notcurses.c b/src/lib/notcurses.c index 8450200f2..ea7be8a6e 100644 --- a/src/lib/notcurses.c +++ b/src/lib/notcurses.c @@ -1062,7 +1062,7 @@ int ncplane_putc(ncplane* n, const cell* c){ return ret; } -int ncplane_getc(const ncplane* n, cell* c, char** gclust){ +int ncplane_cursor_at(const ncplane* n, cell* c, char** gclust){ if(n->y == n->leny && n->x == n->lenx){ return -1; } @@ -1291,3 +1291,13 @@ void ncplane_erase(ncplane* n){ n->channels = 0; n->attrword = 0; } + +int ncplane_getc(const struct ncplane* n, cell* c){ + int r = getc(n->nc->ttyfp); + return r; +} + +int ncplane_getc_blocking(const struct ncplane* n, cell* c){ + int r = getc(n->nc->ttyfp); + return r; +}