aboutsummaryrefslogtreecommitdiffstats
path: root/src/input.c
blob: 8bb4048064adf0d22e60043345958bd51598e0d9 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * Copyright (c) 2019-2021, yzrh <yzrh@noema.org>
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <SDL2/SDL.h>

#include "input.h"

void
input_keyboard(SDL_Keycode sym, Snake_Input *code)
{
	switch (sym) {
		case SDLK_LEFT:
			*code = SNAKE_LEFT;
			break;
		case SDLK_UP:
			*code = SNAKE_UP;
			break;
		case SDLK_DOWN:
			*code = SNAKE_DOWN;
			break;
		case SDLK_RIGHT:
			*code = SNAKE_RIGHT;
			break;
		case SDLK_SPACE:
			*code = SNAKE_JUMP;
			break;
		case SDLK_RETURN:
			*code = SNAKE_SELECT;
			break;
		case SDLK_ESCAPE:
			*code = SNAKE_PAUSE;
			break;
		case SDLK_r:
			*code = SNAKE_RESET;
			break;
		case SDLK_g:
			*code = SNAKE_GOD;
			break;
		case SDLK_h:
			*code = SNAKE_GROW;
			break;
		case SDLK_t:
			*code = SNAKE_TELEPORT;
			break;
		case SDLK_m:
			*code = SNAKE_MUSIC;
			break;
		case SDLK_COMMA:
			*code = SNAKE_TICK_DECREASE;
			break;
		case SDLK_PERIOD:
			*code = SNAKE_TICK_INCREASE;
			break;
		case SDLK_s:
			*code = SNAKE_LEFT;
			break;
		case SDLK_e:
			*code = SNAKE_UP;
			break;
		case SDLK_d:
			*code = SNAKE_DOWN;
			break;
		case SDLK_f:
			*code = SNAKE_RIGHT;
			break;
		default:
			*code = SNAKE_EMPTY;
			break;
	}
}

void
input_controller(uint8_t button, Snake_Input *code)
{
	switch (button) {
		case SDL_CONTROLLER_BUTTON_DPAD_LEFT:
			*code = SNAKE_LEFT;
			break;
		case SDL_CONTROLLER_BUTTON_DPAD_UP:
			*code = SNAKE_UP;
			break;
		case SDL_CONTROLLER_BUTTON_DPAD_DOWN:
			*code = SNAKE_DOWN;
			break;
		case SDL_CONTROLLER_BUTTON_DPAD_RIGHT:
			*code = SNAKE_RIGHT;
			break;
		case SDL_CONTROLLER_BUTTON_START:
			*code = SNAKE_SELECT;
			break;
		case SDL_CONTROLLER_BUTTON_BACK:
			*code = SNAKE_PAUSE;
			break;
		case SDL_CONTROLLER_BUTTON_GUIDE:
			*code = SNAKE_GOD;
			break;
		case SDL_CONTROLLER_BUTTON_A:
			*code = SNAKE_JUMP;
			break;
		case SDL_CONTROLLER_BUTTON_B:
			break;
		case SDL_CONTROLLER_BUTTON_X:
			*code = SNAKE_RESET;
			break;
		case SDL_CONTROLLER_BUTTON_Y:
			*code = SNAKE_MUSIC;
			break;
		case SDL_CONTROLLER_BUTTON_LEFTSTICK:
			*code = SNAKE_TELEPORT;
			break;
		case SDL_CONTROLLER_BUTTON_RIGHTSTICK:
			*code = SNAKE_GROW;
			break;
		case SDL_CONTROLLER_BUTTON_LEFTSHOULDER:
			*code = SNAKE_TICK_DECREASE;
			break;
		case SDL_CONTROLLER_BUTTON_RIGHTSHOULDER:
			*code = SNAKE_TICK_INCREASE;
			break;
		default:
			*code = SNAKE_EMPTY;
			break;
	}
}

#ifdef GPIO

void
input_gpio(int gpio, uint8_t *button)
{
	switch (gpio) {
		case 0:
			*button = SDL_CONTROLLER_BUTTON_DPAD_LEFT;
			break;
		case 1:
			*button = SDL_CONTROLLER_BUTTON_DPAD_UP;
			break;
		case 2:
			*button = SDL_CONTROLLER_BUTTON_DPAD_DOWN;
			break;
		case 3:
			*button = SDL_CONTROLLER_BUTTON_DPAD_RIGHT;
			break;
		case 4:
			*button = SDL_CONTROLLER_BUTTON_BACK;
			break;
		case 5:
			*button = SDL_CONTROLLER_BUTTON_GUIDE;
			break;
		case 6:
			*button = SDL_CONTROLLER_BUTTON_START;
			break;
		case 7:
			*button = SDL_CONTROLLER_BUTTON_A;
			break;
		case 8:
			*button = SDL_CONTROLLER_BUTTON_B;
			break;
		case 9:
			*button = SDL_CONTROLLER_BUTTON_X;
			break;
		case 10:
			*button = SDL_CONTROLLER_BUTTON_Y;
			break;
		case 11:
			*button = SDL_CONTROLLER_BUTTON_LEFTSHOULDER;
			break;
		case 12:
			*button = SDL_CONTROLLER_BUTTON_RIGHTSHOULDER;
			break;
		case 13:
			*button = SDL_CONTROLLER_BUTTON_LEFTSTICK;
			break;
		case 14:
			*button = SDL_CONTROLLER_BUTTON_RIGHTSTICK;
			break;
		default:
			*button = SDL_CONTROLLER_BUTTON_INVALID;
			break;
	}
}

#endif /* GPIO */