aboutsummaryrefslogtreecommitdiffstats
path: root/libgamepad_get_button_by_name.c
diff options
context:
space:
mode:
Diffstat (limited to 'libgamepad_get_button_by_name.c')
-rw-r--r--libgamepad_get_button_by_name.c50
1 files changed, 50 insertions, 0 deletions
diff --git a/libgamepad_get_button_by_name.c b/libgamepad_get_button_by_name.c
new file mode 100644
index 0000000..2b37e93
--- /dev/null
+++ b/libgamepad_get_button_by_name.c
@@ -0,0 +1,50 @@
+/* See LICENSE file for copyright and license details. */
+#include "common.h"
+
+
+#define LIST_ALIASES(X)\
+ X(KEY_HANGUEL)\
+ X(KEY_SCREENLOCK)\
+ X(KEY_DIRECTION)\
+ X(KEY_DASHBOARD)\
+ X(KEY_BRIGHTNESS_ZERO)\
+ X(KEY_WIMAX)\
+ X(BTN_A)\
+ X(BTN_B)\
+ X(BTN_X)\
+ X(BTN_Y)\
+ X(KEY_ZOOM)\
+ X(KEY_SCREEN)\
+ X(KEY_BRIGHTNESS_TOGGLE)
+
+
+int16_t
+libgamepad_get_button_by_name(const char *name)
+{
+ size_t code;
+ int saved_errno;
+ char *end;
+ if (!name || !*name)
+ return -1;
+ if (isdigit(*name)) {
+ saved_errno = errno;
+ errno = 0;
+ code = (size_t)strtoul(name, &end, 0);
+ if (errno || *end || code > (size_t)KEY_MAX) {
+ errno = saved_errno;
+ return -1;
+ }
+ errno = saved_errno;
+ return (int16_t)code;
+ } else {
+ for (code = 0; code < ELEMSOF(libgamepad_button_names__); code++)
+ if (!strcasecmp(name, libgamepad_button_names__[code]))
+ return (int16_t)code;
+#define X(NAME)\
+ if (!strcasecmp(name, #NAME))\
+ return (int16_t)NAME;
+ LIST_ALIASES(X)
+#undef X
+ return -1;
+ }
+}