forked from libretro/RetroArch
-
Notifications
You must be signed in to change notification settings - Fork 2
/
menu_event.c
351 lines (302 loc) · 12.1 KB
/
menu_event.c
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
* Copyright (C) 2011-2017 - Daniel De Matteis
* Copyright (C) 2014-2017 - Jean-André Santoni
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif
#include <string/stdstring.h>
#include "widgets/menu_input_dialog.h"
#include "widgets/menu_osk.h"
#include "menu_driver.h"
#include "menu_animation.h"
#include "../configuration.h"
#include "../retroarch.h"
#include "../tasks/tasks_internal.h"
static unsigned char menu_keyboard_key_state[RETROK_LAST] = {0};
/* This function gets called for handling pointer events.
*
* Pointer events are touchscreen events that are spawned
* by touchpad/touchscreen. */
static int menu_event_pointer(unsigned *action)
{
rarch_joypad_info_t joypad_info;
int pointer_x, pointer_y;
size_t fb_pitch;
unsigned fb_width, fb_height;
const struct retro_keybind *binds[MAX_USERS] = {NULL};
const input_driver_t *input_ptr = input_get_ptr();
void *input_data = input_get_data();
menu_input_t *menu_input = menu_input_get_ptr();
int pointer_device = menu_driver_is_texture_set()
?
RETRO_DEVICE_POINTER : RARCH_DEVICE_POINTER_SCREEN;
menu_display_get_fb_size(&fb_width, &fb_height,
&fb_pitch);
joypad_info.joy_idx = 0;
joypad_info.auto_binds = NULL;
joypad_info.axis_threshold = 0.0f;
pointer_x =
input_ptr->input_state(input_data, joypad_info, binds,
0, pointer_device, 0, RETRO_DEVICE_ID_POINTER_X);
pointer_y =
input_ptr->input_state(input_data, joypad_info, binds,
0, pointer_device, 0, RETRO_DEVICE_ID_POINTER_Y);
menu_input->pointer.pressed[0] = input_ptr->input_state(input_data,
joypad_info,
binds,
0, pointer_device, 0, RETRO_DEVICE_ID_POINTER_PRESSED);
menu_input->pointer.pressed[1] = input_ptr->input_state(input_data,
joypad_info,
binds,
0, pointer_device, 1, RETRO_DEVICE_ID_POINTER_PRESSED);
menu_input->pointer.back = input_ptr->input_state(input_data,
joypad_info,
binds,
0, pointer_device, 0, RARCH_DEVICE_ID_POINTER_BACK);
menu_input->pointer.x = ((pointer_x + 0x7fff) * (int)fb_width) / 0xFFFF;
menu_input->pointer.y = ((pointer_y + 0x7fff) * (int)fb_height) / 0xFFFF;
return 0;
}
/* Check if a specific keyboard key has been pressed. */
unsigned char menu_event_kb_is_set(enum retro_key key)
{
return menu_keyboard_key_state[key];
}
/* Set a specific keyboard key latch. */
static void menu_event_kb_set_internal(unsigned idx, unsigned char key)
{
menu_keyboard_key_state[idx] = key;
}
/* Set a specific keyboard key.
*
* 'down' sets the latch (true would
* mean the key is being pressed down, while 'false' would mean that
* the key has been released).
**/
void menu_event_kb_set(bool down, enum retro_key key)
{
if (key == RETROK_UNKNOWN)
{
unsigned i;
for (i = 0; i < RETROK_LAST; i++)
menu_event_kb_set_internal(i, (menu_event_kb_is_set((enum retro_key)i) & 1) << 1);
}
else
menu_event_kb_set_internal(key, ((menu_event_kb_is_set(key) & 1) << 1) | down);
}
/*
* This function gets called in order to process all input events
* for the current frame.
*
* Sends input code to menu for one frame.
*
* It uses as input the local variables' input' and 'trigger_input'.
*
* Mouse and touch input events get processed inside this function.
*
* NOTE: 'input' and 'trigger_input' is sourced from the keyboard and/or
* the gamepad. It does not contain input state derived from the mouse
* and/or touch - this gets dealt with separately within this function.
*
* TODO/FIXME - maybe needs to be overhauled so we can send multiple
* events per frame if we want to, and we shouldn't send the
* entire button state either but do a separate event per button
* state.
*/
unsigned menu_event(retro_bits_t* p_input, retro_bits_t* p_trigger_input)
{
menu_animation_ctx_delta_t delta;
float delta_time;
/* Used for key repeat */
static float delay_timer = 0.0f;
static float delay_count = 0.0f;
unsigned ret = MENU_ACTION_NOOP;
static bool initial_held = true;
static bool first_held = false;
bool set_scroll = false;
bool mouse_enabled = false;
size_t new_scroll_accel = 0;
menu_input_t *menu_input = NULL;
settings_t *settings = config_get_ptr();
static unsigned ok_old = 0;
bool input_swap_override = input_autoconfigure_get_swap_override();
unsigned menu_ok_btn = (!input_swap_override &&
settings->bools.input_menu_swap_ok_cancel_buttons) ?
RETRO_DEVICE_ID_JOYPAD_B : RETRO_DEVICE_ID_JOYPAD_A;
unsigned menu_cancel_btn = (!input_swap_override &&
settings->bools.input_menu_swap_ok_cancel_buttons) ?
RETRO_DEVICE_ID_JOYPAD_A : RETRO_DEVICE_ID_JOYPAD_B;
unsigned ok_current = BIT256_GET_PTR(p_input, menu_ok_btn );
unsigned ok_trigger = ok_current & ~ok_old;
ok_old = ok_current;
if (bits_any_set(p_input->data, ARRAY_SIZE(p_input->data)))
{
if (!first_held)
{
/* don't run anything first frame, only capture held inputs
* for old_input_state. */
first_held = true;
delay_timer = initial_held ? 12 : 6;
delay_count = 0;
}
if (delay_count >= delay_timer)
{
uint32_t input_repeat = 0;
BIT32_SET(input_repeat, RETRO_DEVICE_ID_JOYPAD_UP);
BIT32_SET(input_repeat, RETRO_DEVICE_ID_JOYPAD_DOWN);
BIT32_SET(input_repeat, RETRO_DEVICE_ID_JOYPAD_LEFT);
BIT32_SET(input_repeat, RETRO_DEVICE_ID_JOYPAD_RIGHT);
BIT32_SET(input_repeat, RETRO_DEVICE_ID_JOYPAD_L);
BIT32_SET(input_repeat, RETRO_DEVICE_ID_JOYPAD_R);
set_scroll = true;
first_held = false;
p_trigger_input->data[0] |= p_input->data[0] & input_repeat;
menu_driver_ctl(MENU_NAVIGATION_CTL_GET_SCROLL_ACCEL,
&new_scroll_accel);
new_scroll_accel = MIN(new_scroll_accel + 1, 64);
}
initial_held = false;
}
else
{
set_scroll = true;
first_held = false;
initial_held = true;
}
if (set_scroll)
menu_driver_ctl(MENU_NAVIGATION_CTL_SET_SCROLL_ACCEL,
&new_scroll_accel);
menu_animation_ctl(MENU_ANIMATION_CTL_DELTA_TIME, &delta_time);
delta.current = delta_time;
if (menu_animation_get_ideal_delta_time(&delta))
delay_count += delta.ideal;
if (menu_input_dialog_get_display_kb())
{
menu_event_osk_iterate();
if (BIT256_GET_PTR(p_trigger_input, RETRO_DEVICE_ID_JOYPAD_DOWN))
{
if (menu_event_get_osk_ptr() < 33)
menu_event_set_osk_ptr(menu_event_get_osk_ptr() + OSK_CHARS_PER_LINE);
}
if (BIT256_GET_PTR(p_trigger_input, RETRO_DEVICE_ID_JOYPAD_UP))
{
if (menu_event_get_osk_ptr() >= OSK_CHARS_PER_LINE)
menu_event_set_osk_ptr(menu_event_get_osk_ptr() - OSK_CHARS_PER_LINE);
}
if (BIT256_GET_PTR(p_trigger_input, RETRO_DEVICE_ID_JOYPAD_RIGHT))
{
if (menu_event_get_osk_ptr() < 43)
menu_event_set_osk_ptr(menu_event_get_osk_ptr() + 1);
}
if (BIT256_GET_PTR(p_trigger_input, RETRO_DEVICE_ID_JOYPAD_LEFT))
{
if (menu_event_get_osk_ptr() >= 1)
menu_event_set_osk_ptr(menu_event_get_osk_ptr() - 1);
}
if (BIT256_GET_PTR(p_trigger_input, RETRO_DEVICE_ID_JOYPAD_L))
{
if (menu_event_get_osk_idx() > OSK_TYPE_UNKNOWN + 1)
menu_event_set_osk_idx((enum osk_type)(menu_event_get_osk_idx() - 1));
else
menu_event_set_osk_idx((enum osk_type)(OSK_TYPE_LAST - 1));
}
if (BIT256_GET_PTR(p_trigger_input, RETRO_DEVICE_ID_JOYPAD_R))
{
if (menu_event_get_osk_idx() < OSK_TYPE_LAST - 1)
menu_event_set_osk_idx((enum osk_type)(menu_event_get_osk_idx() + 1));
else
menu_event_set_osk_idx((enum osk_type)(OSK_TYPE_UNKNOWN + 1));
}
if (BIT256_GET_PTR(p_trigger_input, menu_ok_btn))
{
if (menu_event_get_osk_ptr() >= 0)
menu_event_osk_append(menu_event_get_osk_ptr());
}
if (BIT256_GET_PTR(p_trigger_input, menu_cancel_btn))
{
input_keyboard_event(true, '\x7f', '\x7f', 0, RETRO_DEVICE_KEYBOARD);
}
/* send return key to close keyboard input window */
if (BIT256_GET_PTR(p_trigger_input, RETRO_DEVICE_ID_JOYPAD_START))
input_keyboard_event(true, '\n', '\n', 0, RETRO_DEVICE_KEYBOARD);
BIT256_CLEAR_ALL_PTR(p_trigger_input);
}
else
{
if (BIT256_GET_PTR(p_trigger_input, RETRO_DEVICE_ID_JOYPAD_UP))
ret = MENU_ACTION_UP;
else if (BIT256_GET_PTR(p_trigger_input, RETRO_DEVICE_ID_JOYPAD_DOWN))
ret = MENU_ACTION_DOWN;
else if (BIT256_GET_PTR(p_trigger_input, RETRO_DEVICE_ID_JOYPAD_LEFT))
ret = MENU_ACTION_LEFT;
else if (BIT256_GET_PTR(p_trigger_input, RETRO_DEVICE_ID_JOYPAD_RIGHT))
ret = MENU_ACTION_RIGHT;
else if (BIT256_GET_PTR(p_trigger_input, RETRO_DEVICE_ID_JOYPAD_L))
ret = MENU_ACTION_SCROLL_UP;
else if (BIT256_GET_PTR(p_trigger_input, RETRO_DEVICE_ID_JOYPAD_R))
ret = MENU_ACTION_SCROLL_DOWN;
else if (ok_trigger)
ret = MENU_ACTION_OK;
else if (BIT256_GET_PTR(p_trigger_input, menu_cancel_btn))
ret = MENU_ACTION_CANCEL;
else if (BIT256_GET_PTR(p_trigger_input, RETRO_DEVICE_ID_JOYPAD_X))
ret = MENU_ACTION_SEARCH;
else if (BIT256_GET_PTR(p_trigger_input, RETRO_DEVICE_ID_JOYPAD_Y))
ret = MENU_ACTION_SCAN;
else if (BIT256_GET_PTR(p_trigger_input, RETRO_DEVICE_ID_JOYPAD_START))
ret = MENU_ACTION_START;
else if (BIT256_GET_PTR(p_trigger_input, RETRO_DEVICE_ID_JOYPAD_SELECT))
ret = MENU_ACTION_INFO;
else if (BIT256_GET_PTR(p_trigger_input, RARCH_MENU_TOGGLE))
ret = MENU_ACTION_TOGGLE;
}
if (menu_event_kb_is_set(RETROK_F11))
{
command_event(CMD_EVENT_GRAB_MOUSE_TOGGLE, NULL);
menu_event_kb_set_internal(RETROK_F11, 0);
}
if (BIT256_GET_PTR(p_trigger_input, RARCH_QUIT_KEY))
return MENU_ACTION_QUIT;
mouse_enabled = settings->bools.menu_mouse_enable;
#ifdef HAVE_OVERLAY
if (!mouse_enabled)
mouse_enabled = !(settings->bools.input_overlay_enable
&& input_overlay_is_alive(overlay_ptr));
#endif
if (!(menu_input = menu_input_get_ptr()))
return 0;
if (!mouse_enabled)
menu_input->mouse.ptr = 0;
if (settings->bools.menu_pointer_enable)
menu_event_pointer(&ret);
else
{
menu_input->pointer.x = 0;
menu_input->pointer.y = 0;
menu_input->pointer.dx = 0;
menu_input->pointer.dy = 0;
menu_input->pointer.accel = 0;
menu_input->pointer.pressed[0] = false;
menu_input->pointer.pressed[1] = false;
menu_input->pointer.back = false;
menu_input->pointer.ptr = 0;
}
return ret;
}