aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2022-07-27 18:04:20 +0200
committerMattias Andrée <maandree@kth.se>2022-07-27 18:04:20 +0200
commit6af529f89881b3cfd6cbb03de1ab411ddbaa6dd1 (patch)
tree1dda09d69a8b48c08b82748da3f7dfa9fb1c9da2
parentAdd inline functions for testing support (diff)
downloadlibgamepad-6af529f89881b3cfd6cbb03de1ab411ddbaa6dd1.tar.gz
libgamepad-6af529f89881b3cfd6cbb03de1ab411ddbaa6dd1.tar.bz2
libgamepad-6af529f89881b3cfd6cbb03de1ab411ddbaa6dd1.tar.xz
Add support for prefix-prefixed numerical values for reverse name-lookup (for future internal use)
Signed-off-by: Mattias Andrée <maandree@kth.se>
-rw-r--r--libgamepad_get_absolute_axis_by_name.c7
-rw-r--r--libgamepad_get_button_by_name.c7
-rw-r--r--libgamepad_get_relative_axis_by_name.c7
3 files changed, 21 insertions, 0 deletions
diff --git a/libgamepad_get_absolute_axis_by_name.c b/libgamepad_get_absolute_axis_by_name.c
index 4d5aaa4..69b8d6f 100644
--- a/libgamepad_get_absolute_axis_by_name.c
+++ b/libgamepad_get_absolute_axis_by_name.c
@@ -11,6 +11,7 @@ libgamepad_get_absolute_axis_by_name(const char *name)
if (!name || !*name)
return -1;
if (isdigit(*name)) {
+ numerical:
saved_errno = errno;
errno = 0;
code = (size_t)strtoul(name, &end, 0);
@@ -21,6 +22,12 @@ libgamepad_get_absolute_axis_by_name(const char *name)
errno = saved_errno;
return (int16_t)code;
} else {
+ if (!strncasecmp(name, "ABS_#", 5)) {
+ if (isdigit(name[5])) {
+ name = &name[5];
+ goto numerical;
+ }
+ }
for (code = 0; code < ELEMSOF(libgamepad_absolute_axis_names__); code++)
if (!strcasecmp(name, libgamepad_absolute_axis_names__[code]))
return (int16_t)code;
diff --git a/libgamepad_get_button_by_name.c b/libgamepad_get_button_by_name.c
index 2b37e93..63af9d8 100644
--- a/libgamepad_get_button_by_name.c
+++ b/libgamepad_get_button_by_name.c
@@ -27,6 +27,7 @@ libgamepad_get_button_by_name(const char *name)
if (!name || !*name)
return -1;
if (isdigit(*name)) {
+ numerical:
saved_errno = errno;
errno = 0;
code = (size_t)strtoul(name, &end, 0);
@@ -37,6 +38,12 @@ libgamepad_get_button_by_name(const char *name)
errno = saved_errno;
return (int16_t)code;
} else {
+ if (!strncasecmp(name, "KEY_#", 5) || !strncasecmp(name, "BTN_#", 5)) {
+ if (isdigit(name[5])) {
+ name = &name[5];
+ goto numerical;
+ }
+ }
for (code = 0; code < ELEMSOF(libgamepad_button_names__); code++)
if (!strcasecmp(name, libgamepad_button_names__[code]))
return (int16_t)code;
diff --git a/libgamepad_get_relative_axis_by_name.c b/libgamepad_get_relative_axis_by_name.c
index f7f2a72..34b9827 100644
--- a/libgamepad_get_relative_axis_by_name.c
+++ b/libgamepad_get_relative_axis_by_name.c
@@ -11,6 +11,7 @@ libgamepad_get_relative_axis_by_name(const char *name)
if (!name || !*name)
return -1;
if (isdigit(*name)) {
+ numerical:
saved_errno = errno;
errno = 0;
code = (size_t)strtoul(name, &end, 0);
@@ -21,6 +22,12 @@ libgamepad_get_relative_axis_by_name(const char *name)
errno = saved_errno;
return (int16_t)code;
} else {
+ if (!strncasecmp(name, "REL_#", 5)) {
+ if (isdigit(name[5])) {
+ name = &name[5];
+ goto numerical;
+ }
+ }
for (code = 0; code < ELEMSOF(libgamepad_relative_axis_names__); code++)
if (!strcasecmp(name, libgamepad_relative_axis_names__[code]))
return (int16_t)code;