aboutsummaryrefslogtreecommitdiffstats
path: root/libgamepad_get_button_by_name.c
blob: b44726c867a342679e238a57ac81ece8534ff056 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/* 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)) {
	numerical:
		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 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;
#define X(NAME)\
		if (!strcasecmp(name, #NAME))\
			return (int16_t)NAME;
		LIST_ALIASES(X)
#undef X
	}
	return -1;
}