[ncinput_equals] consider UNKNOWN == PRESS #2554

This commit is contained in:
nick black 2022-01-12 00:37:11 -05:00
parent 06d61b9025
commit 15e2c6eb12
No known key found for this signature in database
GPG Key ID: 5F43400C21CBFACC
5 changed files with 59 additions and 34 deletions

View File

@ -14,24 +14,32 @@ notcurses_input - input via notcurses
struct timespec; struct timespec;
struct notcurses; struct notcurses;
typedef enum {
NCTYPE_UNKNOWN,
NCTYPE_PRESS,
NCTYPE_REPEAT,
NCTYPE_RELEASE,
} ncintype_e;
// An input event. Cell coordinates are currently defined only for mouse
// events. It is not guaranteed that we can set the modifiers for a given
// ncinput. We encompass single Unicode codepoints, not complete EGCs.
// FIXME for abi4, combine the bools into |modifiers|
typedef struct ncinput { typedef struct ncinput {
uint32_t id; // Unicode codepoint uint32_t id; // Unicode codepoint or synthesized NCKEY event
int y; // Y cell coordinate of event, -1 for undefined int y, x; // y/x cell coordinate of event, -1 for undefined
int x; // X cell coordinate of event, -1 for undefined char utf8[5]; // utf8 representation, if one exists
char utf8[5]; // utf8 representation, when one exists // DEPRECATED do not use! going away in 4.0
bool alt; // Was Alt held during the event? bool alt; // was alt held?
bool shift; // Was Shift held during the event? bool shift; // was shift held?
bool ctrl; // Was Ctrl held during the event? bool ctrl; // was ctrl held?
enum { // END DEPRECATION
EVTYPE_UNKNOWN, ncintype_e evtype;
EVTYPE_PRESS, unsigned modifiers;// bitmask over NCKEY_MOD_*
EVTYPE_REPEAT, int ypx, xpx; // pixel offsets within cell, -1 for undefined
EVTYPE_RELEASE,
} evtype;
int ypx, xpx; // pixel offsets within cell, -1 for undefined
unsigned modifiers;
} ncinput; } ncinput;
#define NCMICE_NO_EVENTS 0 #define NCMICE_NO_EVENTS 0
#define NCMICE_MOVE_EVENT 0x1 #define NCMICE_MOVE_EVENT 0x1
#define NCMICE_BUTTON_EVENT 0x2 #define NCMICE_BUTTON_EVENT 0x2
@ -51,7 +59,7 @@ typedef struct ncinput {
**uint32_t notcurses_get_blocking(struct notcurses* ***n***, ncinput* ***ni***);** **uint32_t notcurses_get_blocking(struct notcurses* ***n***, ncinput* ***ni***);**
**int cnotcurses_mice_enable(struct notcurses* ***n***, unsigned ***eventmask***);** **int notcurses_mice_enable(struct notcurses* ***n***, unsigned ***eventmask***);**
**int notcurses_mice_disable(struct notcurses* ***n***);** **int notcurses_mice_disable(struct notcurses* ***n***);**
@ -71,6 +79,10 @@ typedef struct ncinput {
**bool ncinput_meta_p(const struct ncinput* ***n***);** **bool ncinput_meta_p(const struct ncinput* ***n***);**
**bool ncinput_super_p(const struct ncinput* ***n***);**
**bool ncinput_hyper_p(const struct ncinput* ***n***);**
# DESCRIPTION # DESCRIPTION
notcurses supports input from keyboards and mice, and any device that looks notcurses supports input from keyboards and mice, and any device that looks
@ -102,7 +114,9 @@ be called without the possibility of blocking.
**ncinput_equal_p** compares two **ncinput** structs for data equality (i.e. **ncinput_equal_p** compares two **ncinput** structs for data equality (i.e.
not considering padding), returning **true** if they represent the same not considering padding), returning **true** if they represent the same
input (though not necessarily the same input event). input (though not necessarily the same input event). Note that
**NCTYPE_UNKNOWN** and **NCTYPE_PRESS** are considered equivalent for the
purposes of **ncinput_equal_p**.
**notcurses_linesigs_disable** disables conversion of inputs **INTR**, **QUIT**, **notcurses_linesigs_disable** disables conversion of inputs **INTR**, **QUIT**,
**SUSP**, and **DSUSP** into **SIGINT**, **SIGQUIT**, and **SIGTSTP**. These **SUSP**, and **DSUSP** into **SIGINT**, **SIGQUIT**, and **SIGTSTP**. These

View File

@ -136,10 +136,10 @@ namespace ncpp
struct EvType struct EvType
{ {
static constexpr unsigned Unknown = ncinput::NCTYPE_UNKNOWN; static constexpr ncintype_e Unknown = NCTYPE_UNKNOWN;
static constexpr unsigned Press = ncinput::NCTYPE_PRESS; static constexpr ncintype_e Press = NCTYPE_PRESS;
static constexpr unsigned Repeat = ncinput::NCTYPE_REPEAT; static constexpr ncintype_e Repeat = NCTYPE_REPEAT;
static constexpr unsigned Release = ncinput::NCTYPE_RELEASE; static constexpr ncintype_e Release = NCTYPE_RELEASE;
}; };
} }

View File

@ -1130,6 +1130,13 @@ nckey_mouse_p(uint32_t r){
return r >= NCKEY_MOTION && r <= NCKEY_BUTTON11; return r >= NCKEY_MOTION && r <= NCKEY_BUTTON11;
} }
typedef enum {
NCTYPE_UNKNOWN,
NCTYPE_PRESS,
NCTYPE_REPEAT,
NCTYPE_RELEASE,
} ncintype_e;
// An input event. Cell coordinates are currently defined only for mouse // An input event. Cell coordinates are currently defined only for mouse
// events. It is not guaranteed that we can set the modifiers for a given // events. It is not guaranteed that we can set the modifiers for a given
// ncinput. We encompass single Unicode codepoints, not complete EGCs. // ncinput. We encompass single Unicode codepoints, not complete EGCs.
@ -1143,12 +1150,7 @@ typedef struct ncinput {
bool shift; // was shift held? bool shift; // was shift held?
bool ctrl; // was ctrl held? bool ctrl; // was ctrl held?
// END DEPRECATION // END DEPRECATION
enum { ncintype_e evtype;
NCTYPE_UNKNOWN,
NCTYPE_PRESS,
NCTYPE_REPEAT,
NCTYPE_RELEASE,
} evtype;
unsigned modifiers;// bitmask over NCKEY_MOD_* unsigned modifiers;// bitmask over NCKEY_MOD_*
int ypx, xpx; // pixel offsets within cell, -1 for undefined int ypx, xpx; // pixel offsets within cell, -1 for undefined
} ncinput; } ncinput;
@ -1193,19 +1195,28 @@ ncinput_numlock_p(const ncinput* n){
return (n->modifiers & NCKEY_MOD_NUMLOCK); return (n->modifiers & NCKEY_MOD_NUMLOCK);
} }
// compare two ncinput structs for data equality. // compare two ncinput structs for data equality. NCTYPE_PRESS and
// NCTYPE_UNKNOWN are considered to be equivalent.
static inline bool static inline bool
ncinput_equal_p(const ncinput* n1, const ncinput* n2){ ncinput_equal_p(const ncinput* n1, const ncinput* n2){
// don't need to check ->utf8; it's derived from id
if(n1->id != n2->id){ if(n1->id != n2->id){
return false; return false;
} }
if(n1->y != n2->y || n1->x != n2->x){ if(n1->y != n2->y || n1->x != n2->x){
return false; return false;
} }
if(n1->alt != n2->alt || n1->shift != n2->shift || n1->ctrl != n2->ctrl){ // don't need to check deprecated alt, ctrl, shift
if(n1->modifiers != n2->modifiers){
return false; return false;
} }
if(n1->evtype != n2->evtype){ if(n1->evtype != n2->evtype){
if((n1->evtype != NCTYPE_UNKNOWN && n1->evtype != NCTYPE_PRESS) ||
(n2->evtype != NCTYPE_UNKNOWN && n2->evtype != NCTYPE_PRESS)){
return false;
}
}
if(n1->ypx != n2->ypx || n1->xpx != n2->xpx){
return false; return false;
} }
return true; return true;

View File

@ -92,10 +92,10 @@ dup_menu_item(ncmenu_int_item* dst, const struct ncmenu_item* src){
return 0; return 0;
} }
size_t bytes = 1; // NUL terminator size_t bytes = 1; // NUL terminator
if(src->shortcut.alt){ if(ncinput_alt_p(&src->shortcut)){
bytes += strlen(ALTMOD); bytes += strlen(ALTMOD);
} }
if(src->shortcut.ctrl){ if(ncinput_ctrl_p(&src->shortcut)){
bytes += strlen(CTLMOD); bytes += strlen(CTLMOD);
} }
mbstate_t ps; mbstate_t ps;
@ -107,8 +107,8 @@ dup_menu_item(ncmenu_int_item* dst, const struct ncmenu_item* src){
} }
bytes += shortsize + 1; bytes += shortsize + 1;
char* sdup = malloc(bytes); char* sdup = malloc(bytes);
int n = snprintf(sdup, bytes, "%s%s", src->shortcut.alt ? ALTMOD : "", int n = snprintf(sdup, bytes, "%s%s", ncinput_alt_p(&src->shortcut) ? ALTMOD : "",
src->shortcut.ctrl ? CTLMOD : ""); ncinput_ctrl_p(&src->shortcut) ? CTLMOD : "");
if(n < 0 || (size_t)n >= bytes){ if(n < 0 || (size_t)n >= bytes){
free(sdup); free(sdup);
free(dst->desc); free(dst->desc);

View File

@ -883,7 +883,7 @@ TEST_CASE("Plane") {
REQUIRE(n); REQUIRE(n);
ncinput ni{}; ncinput ni{};
ni.id = NCKEY_BUTTON1; ni.id = NCKEY_BUTTON1;
ni.evtype = ncinput::NCTYPE_RELEASE; ni.evtype = ncpp::EvType::Release;
int total = 0; int total = 0;
for(ni.y = 0 ; ni.y < 5 ; ++ni.y){ for(ni.y = 0 ; ni.y < 5 ; ++ni.y){
for(ni.x = 0 ; ni.x < 5 ; ++ni.x){ for(ni.x = 0 ; ni.x < 5 ; ++ni.x){