Canonicalise (a local copy of) the key structures given to termkey_keycmp() before comparing them

This commit is contained in:
Paul LeoNerd Evans 2011-09-06 22:49:05 +01:00
parent 3008ed29d1
commit 8f32ac525f
3 changed files with 34 additions and 15 deletions

View File

@ -6,7 +6,7 @@ int main(int argc, char *argv[])
TermKey *tk;
TermKeyKey key1, key2;
plan_tests(10);
plan_tests(12);
tk = termkey_new(0, TERMKEY_FLAG_NOTERMIOS);
@ -44,6 +44,18 @@ int main(int argc, char *argv[])
ok(termkey_keycmp(tk, &key1, &key2) < 0, "cmpkey orders KEYSYM after UNICODE");
ok(termkey_keycmp(tk, &key2, &key1) > 0, "cmpkey orders UNICODE before KEYSYM");
key1.type = TERMKEY_TYPE_KEYSYM;
key1.code.sym = TERMKEY_SYM_SPACE;
key1.modifiers = 0;
key2.type = TERMKEY_TYPE_UNICODE;
key2.code.codepoint = ' ';
key2.modifiers = 0;
is_int(termkey_keycmp(tk, &key1, &key2), 0, "cmpkey considers KEYSYM/SPACE and UNICODE/SP identical");
termkey_set_canonflags(tk, termkey_get_canonflags(tk) | TERMKEY_CANON_SPACESYMBOL);
is_int(termkey_keycmp(tk, &key1, &key2), 0, "cmpkey considers KEYSYM/SPACE and UNICODE/SP identical under SPACESYMBOL");
termkey_destroy(tk);
return exit_status();

View File

@ -1244,28 +1244,34 @@ char *termkey_strpkey(TermKey *tk, const char *str, TermKeyKey *key, TermKeyForm
return (char *)str;
}
int termkey_keycmp(TermKey *tk, const TermKeyKey *key1, const TermKeyKey *key2)
int termkey_keycmp(TermKey *tk, const TermKeyKey *key1p, const TermKeyKey *key2p)
{
if(key1->type != key2->type)
return key1->type - key2->type;
/* Copy the key structs since we'll be modifying them */
TermKeyKey key1 = *key1p, key2 = *key2p;
switch(key1->type) {
termkey_canonicalise(tk, &key1);
termkey_canonicalise(tk, &key2);
if(key1.type != key2.type)
return key1.type - key2.type;
switch(key1.type) {
case TERMKEY_TYPE_UNICODE:
if(key1->code.codepoint != key2->code.codepoint)
return key1->code.codepoint - key2->code.codepoint;
if(key1.code.codepoint != key2.code.codepoint)
return key1.code.codepoint - key2.code.codepoint;
case TERMKEY_TYPE_KEYSYM:
if(key1->code.sym != key2->code.sym)
return key1->code.sym - key2->code.sym;
if(key1.code.sym != key2.code.sym)
return key1.code.sym - key2.code.sym;
case TERMKEY_TYPE_FUNCTION:
if(key1->code.number != key2->code.number)
return key1->code.number - key2->code.number;
if(key1.code.number != key2.code.number)
return key1.code.number - key2.code.number;
case TERMKEY_TYPE_MOUSE:
{
int cmp = strncmp(key1->code.mouse, key2->code.mouse, 4);
int cmp = strncmp(key1.code.mouse, key2.code.mouse, 4);
if(cmp != 0)
return cmp;
}
}
return key1->modifiers - key2->modifiers;
return key1.modifiers - key2.modifiers;
}

View File

@ -11,7 +11,7 @@ termkey_keycmp \- compare two key events
.sp
Link with \fI-ltermkey\fP.
.SH DESCRIPTION
\fBtermkey_keycmp\fP() compares two key structures and applies a total ordering, returning a value that is positive, zero, or negative, to indicate if the given structures are increasing, identical, or decreasing.
\fBtermkey_keycmp\fP() compares two key structures and applies a total ordering, returning a value that is positive, zero, or negative, to indicate if the given structures are increasing, identical, or decreasing. Before comparison, copies of both referenced structures are taken, and canonicalised according to the rules for \fBtermkey_canonicalise\fP(3).
.PP
Two structures of differing type are ordered \fBTERMKEY_TYPE_UNICODE\fP, \fBTERMKEY_TYPE_KEYSYM\fP, \fBTERMKEY_TYPE_FUNCTION\fP, \fBTERMKEY_TYPE_MOUSE\fP. Unicode structures are ordered by codepoint, keysym structures are ordered by keysym number, function structures are ordered by function key number, and mouse structures are ordered opaquely by an unspecified but consistent ordering. Within these values, keys different in modifier bits are ordered by the modifiers.
.SH "RETURN VALUE"
@ -21,4 +21,5 @@ This function does not perform any canonicalisation of the key structures, and p
.SH "SEE ALSO"
.BR termkey_new (3),
.BR termkey_getkey (3),
.BR termkey_strpkey (3)
.BR termkey_strpkey (3),
.BR termkey_canonicalise (3)