2018-02-15 19:07:47 +08:00
|
|
|
#include "inputmanager.h"
|
2018-02-09 01:14:50 +08:00
|
|
|
|
|
|
|
#include "convert.h"
|
2018-02-15 19:24:16 +08:00
|
|
|
#include "lockutil.h"
|
2018-02-13 17:10:18 +08:00
|
|
|
#include "log.h"
|
2018-02-09 01:14:50 +08:00
|
|
|
|
2018-03-05 21:54:12 +08:00
|
|
|
// Convert window coordinates (as provided by SDL_GetMouseState() to renderer coordinates (as provided in SDL mouse events)
|
|
|
|
//
|
|
|
|
// See my question:
|
|
|
|
// <https://stackoverflow.com/questions/49111054/how-to-get-mouse-position-on-mouse-wheel-event>
|
|
|
|
static void convert_to_renderer_coordinates(SDL_Renderer *renderer, int *x, int *y) {
|
|
|
|
SDL_Rect viewport;
|
|
|
|
float scale_x, scale_y;
|
|
|
|
SDL_RenderGetViewport(renderer, &viewport);
|
|
|
|
SDL_RenderGetScale(renderer, &scale_x, &scale_y);
|
|
|
|
*x = (int) (*x / scale_x) - viewport.x;
|
|
|
|
*y = (int) (*y / scale_y) - viewport.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct point get_mouse_point(struct screen *screen) {
|
2018-02-09 01:14:50 +08:00
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
SDL_GetMouseState(&x, &y);
|
2018-03-05 21:54:12 +08:00
|
|
|
convert_to_renderer_coordinates(screen->renderer, &x, &y);
|
2018-02-09 01:14:50 +08:00
|
|
|
SDL_assert_release(x >= 0 && x < 0x10000 && y >= 0 && y < 0x10000);
|
|
|
|
return (struct point) {
|
|
|
|
.x = (Uint16) x,
|
|
|
|
.y = (Uint16) y,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static SDL_bool is_ctrl_down(void) {
|
|
|
|
const Uint8 *state = SDL_GetKeyboardState(NULL);
|
|
|
|
return state[SDL_SCANCODE_LCTRL] || state[SDL_SCANCODE_RCTRL];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void send_keycode(struct controller *controller, enum android_keycode keycode, const char *name) {
|
|
|
|
// send DOWN event
|
2018-03-07 21:46:31 +08:00
|
|
|
struct control_event control_event;
|
|
|
|
control_event.type = CONTROL_EVENT_TYPE_KEYCODE;
|
|
|
|
control_event.keycode_event.action = AKEY_EVENT_ACTION_DOWN;
|
|
|
|
control_event.keycode_event.keycode = keycode;
|
|
|
|
control_event.keycode_event.metastate = 0;
|
2018-02-09 01:14:50 +08:00
|
|
|
|
|
|
|
if (!controller_push_event(controller, &control_event)) {
|
2018-02-13 17:10:18 +08:00
|
|
|
LOGW("Cannot send %s (DOWN)", name);
|
2018-02-09 01:14:50 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// send UP event
|
|
|
|
control_event.keycode_event.action = AKEY_EVENT_ACTION_UP;
|
|
|
|
if (!controller_push_event(controller, &control_event)) {
|
2018-02-13 17:10:18 +08:00
|
|
|
LOGW("Cannot send %s (UP)", name);
|
2018-02-09 01:14:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void action_home(struct controller *controller) {
|
|
|
|
send_keycode(controller, AKEYCODE_HOME, "HOME");
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void action_back(struct controller *controller) {
|
|
|
|
send_keycode(controller, AKEYCODE_BACK, "BACK");
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void action_app_switch(struct controller *controller) {
|
|
|
|
send_keycode(controller, AKEYCODE_APP_SWITCH, "APP_SWITCH");
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void action_power(struct controller *controller) {
|
|
|
|
send_keycode(controller, AKEYCODE_POWER, "POWER");
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void action_volume_up(struct controller *controller) {
|
|
|
|
send_keycode(controller, AKEYCODE_VOLUME_UP, "VOLUME_UP");
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void action_volume_down(struct controller *controller) {
|
|
|
|
send_keycode(controller, AKEYCODE_VOLUME_DOWN, "VOLUME_DOWN");
|
|
|
|
}
|
|
|
|
|
2018-03-21 09:04:40 +08:00
|
|
|
static inline void action_shake(struct controller *controller) {
|
|
|
|
send_keycode(controller, AKEYCODE_MENU, "SHAKE");
|
|
|
|
}
|
|
|
|
|
2018-03-10 07:11:52 +08:00
|
|
|
// turn the screen on if it was off, press BACK otherwise
|
|
|
|
static void press_back_or_turn_screen_on(struct controller *controller) {
|
2018-03-07 21:46:31 +08:00
|
|
|
struct control_event control_event;
|
|
|
|
control_event.type = CONTROL_EVENT_TYPE_COMMAND;
|
2018-03-10 07:11:52 +08:00
|
|
|
control_event.command_event.action = CONTROL_EVENT_COMMAND_BACK_OR_SCREEN_ON;
|
2018-03-07 21:46:31 +08:00
|
|
|
|
2018-02-09 01:14:50 +08:00
|
|
|
if (!controller_push_event(controller, &control_event)) {
|
2018-02-13 17:10:18 +08:00
|
|
|
LOGW("Cannot turn screen on");
|
2018-02-09 01:14:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-15 19:24:16 +08:00
|
|
|
static void switch_fps_counter_state(struct frames *frames) {
|
|
|
|
mutex_lock(frames->mutex);
|
|
|
|
if (frames->fps_counter.started) {
|
|
|
|
LOGI("FPS counter stopped");
|
|
|
|
fps_counter_stop(&frames->fps_counter);
|
|
|
|
} else {
|
|
|
|
LOGI("FPS counter started");
|
|
|
|
fps_counter_start(&frames->fps_counter);
|
|
|
|
}
|
|
|
|
mutex_unlock(frames->mutex);
|
|
|
|
}
|
|
|
|
|
2018-03-07 22:29:33 +08:00
|
|
|
static void clipboard_paste(struct controller *controller) {
|
|
|
|
char *text = SDL_GetClipboardText();
|
|
|
|
if (!text) {
|
|
|
|
LOGW("Cannot get clipboard text: %s", SDL_GetError());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!*text) {
|
|
|
|
// empty text
|
|
|
|
SDL_free(text);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct control_event control_event;
|
|
|
|
control_event.type = CONTROL_EVENT_TYPE_TEXT;
|
|
|
|
control_event.text_event.text = text;
|
|
|
|
if (!controller_push_event(controller, &control_event)) {
|
|
|
|
SDL_free(text);
|
|
|
|
LOGW("Cannot send clipboard paste event");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-15 19:07:47 +08:00
|
|
|
void input_manager_process_text_input(struct input_manager *input_manager,
|
|
|
|
const SDL_TextInputEvent *event) {
|
2018-02-09 01:14:50 +08:00
|
|
|
if (is_ctrl_down()) {
|
|
|
|
switch (event->text[0]) {
|
|
|
|
case '+':
|
2018-02-15 19:07:47 +08:00
|
|
|
action_volume_up(input_manager->controller);
|
2018-02-09 01:14:50 +08:00
|
|
|
break;
|
|
|
|
case '-':
|
2018-02-15 19:07:47 +08:00
|
|
|
action_volume_down(input_manager->controller);
|
2018-02-09 01:14:50 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct control_event control_event;
|
|
|
|
control_event.type = CONTROL_EVENT_TYPE_TEXT;
|
2018-03-07 22:29:33 +08:00
|
|
|
control_event.text_event.text = SDL_strdup(event->text);
|
|
|
|
if (!control_event.text_event.text) {
|
|
|
|
LOGW("Cannot strdup input text");
|
|
|
|
return;
|
|
|
|
}
|
2018-02-15 19:07:47 +08:00
|
|
|
if (!controller_push_event(input_manager->controller, &control_event)) {
|
2018-02-13 17:10:18 +08:00
|
|
|
LOGW("Cannot send text event");
|
2018-02-09 01:14:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-15 19:07:47 +08:00
|
|
|
void input_manager_process_key(struct input_manager *input_manager,
|
|
|
|
const SDL_KeyboardEvent *event) {
|
2018-02-09 01:14:50 +08:00
|
|
|
SDL_bool ctrl = event->keysym.mod & (KMOD_LCTRL | KMOD_RCTRL);
|
|
|
|
|
|
|
|
// capture all Ctrl events
|
|
|
|
if (ctrl) {
|
2018-03-06 18:11:07 +08:00
|
|
|
SDL_bool repeat = event->repeat;
|
|
|
|
|
2018-02-09 01:14:50 +08:00
|
|
|
// only consider keydown events, and ignore repeated events
|
|
|
|
if (repeat || event->type != SDL_KEYDOWN) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-06 18:11:07 +08:00
|
|
|
SDL_bool shift = event->keysym.mod & (KMOD_LSHIFT | KMOD_RSHIFT);
|
2018-02-09 01:14:50 +08:00
|
|
|
if (shift) {
|
|
|
|
// currently, there is no shortcut implying SHIFT
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-06 18:11:07 +08:00
|
|
|
SDL_Keycode keycode = event->keysym.sym;
|
2018-02-09 01:14:50 +08:00
|
|
|
switch (keycode) {
|
|
|
|
case SDLK_h:
|
2018-02-15 19:07:47 +08:00
|
|
|
action_home(input_manager->controller);
|
2018-02-09 01:14:50 +08:00
|
|
|
return;
|
|
|
|
case SDLK_b: // fall-through
|
|
|
|
case SDLK_BACKSPACE:
|
2018-02-15 19:07:47 +08:00
|
|
|
action_back(input_manager->controller);
|
2018-02-09 01:14:50 +08:00
|
|
|
return;
|
|
|
|
case SDLK_m:
|
2018-02-15 19:07:47 +08:00
|
|
|
action_app_switch(input_manager->controller);
|
2018-02-09 01:14:50 +08:00
|
|
|
return;
|
2018-03-21 09:04:40 +08:00
|
|
|
case SDLK_s:
|
|
|
|
action_shake(input_manager->controller);
|
|
|
|
return;
|
2018-02-09 01:14:50 +08:00
|
|
|
case SDLK_p:
|
2018-02-15 19:07:47 +08:00
|
|
|
action_power(input_manager->controller);
|
2018-02-09 01:14:50 +08:00
|
|
|
return;
|
2018-03-07 22:29:33 +08:00
|
|
|
case SDLK_v:
|
|
|
|
clipboard_paste(input_manager->controller);
|
|
|
|
return;
|
2018-02-09 01:14:50 +08:00
|
|
|
case SDLK_f:
|
2018-02-15 19:07:47 +08:00
|
|
|
screen_switch_fullscreen(input_manager->screen);
|
2018-02-09 01:14:50 +08:00
|
|
|
return;
|
|
|
|
case SDLK_x:
|
2018-02-15 19:07:47 +08:00
|
|
|
screen_resize_to_fit(input_manager->screen);
|
2018-02-09 01:14:50 +08:00
|
|
|
return;
|
|
|
|
case SDLK_g:
|
2018-02-15 19:07:47 +08:00
|
|
|
screen_resize_to_pixel_perfect(input_manager->screen);
|
2018-02-09 01:14:50 +08:00
|
|
|
return;
|
2018-02-15 19:24:16 +08:00
|
|
|
case SDLK_i:
|
|
|
|
switch_fps_counter_state(input_manager->frames);
|
|
|
|
return;
|
2018-02-09 01:14:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct control_event control_event;
|
|
|
|
if (input_key_from_sdl_to_android(event, &control_event)) {
|
2018-02-15 19:07:47 +08:00
|
|
|
if (!controller_push_event(input_manager->controller, &control_event)) {
|
2018-02-13 17:10:18 +08:00
|
|
|
LOGW("Cannot send control event");
|
2018-02-09 01:14:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-15 19:07:47 +08:00
|
|
|
void input_manager_process_mouse_motion(struct input_manager *input_manager,
|
|
|
|
const SDL_MouseMotionEvent *event) {
|
2018-02-09 01:14:50 +08:00
|
|
|
if (!event->state) {
|
|
|
|
// do not send motion events when no button is pressed
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
struct control_event control_event;
|
2018-02-15 19:07:47 +08:00
|
|
|
if (mouse_motion_from_sdl_to_android(event, input_manager->screen->frame_size, &control_event)) {
|
|
|
|
if (!controller_push_event(input_manager->controller, &control_event)) {
|
2018-02-13 17:10:18 +08:00
|
|
|
LOGW("Cannot send mouse motion event");
|
2018-02-09 01:14:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-15 19:07:47 +08:00
|
|
|
void input_manager_process_mouse_button(struct input_manager *input_manager,
|
|
|
|
const SDL_MouseButtonEvent *event) {
|
2018-03-10 07:40:55 +08:00
|
|
|
if (event->type == SDL_MOUSEBUTTONDOWN) {
|
|
|
|
if (event->button == SDL_BUTTON_RIGHT) {
|
|
|
|
press_back_or_turn_screen_on(input_manager->controller);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (event->button == SDL_BUTTON_MIDDLE) {
|
|
|
|
action_home(input_manager->controller);
|
|
|
|
return;
|
|
|
|
}
|
2018-03-13 15:32:48 +08:00
|
|
|
// double-click on black borders resize to fit the device screen
|
|
|
|
if (event->button == SDL_BUTTON_LEFT && event->clicks == 2) {
|
|
|
|
SDL_bool outside_device_screen =
|
|
|
|
event->x < 0 || event->x >= input_manager->screen->frame_size.width ||
|
|
|
|
event->y < 0 || event->y >= input_manager->screen->frame_size.height;
|
|
|
|
if (outside_device_screen) {
|
|
|
|
screen_resize_to_fit(input_manager->screen);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2018-02-09 01:14:50 +08:00
|
|
|
};
|
|
|
|
struct control_event control_event;
|
2018-02-15 19:07:47 +08:00
|
|
|
if (mouse_button_from_sdl_to_android(event, input_manager->screen->frame_size, &control_event)) {
|
|
|
|
if (!controller_push_event(input_manager->controller, &control_event)) {
|
2018-02-13 17:10:18 +08:00
|
|
|
LOGW("Cannot send mouse button event");
|
2018-02-09 01:14:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-15 19:07:47 +08:00
|
|
|
void input_manager_process_mouse_wheel(struct input_manager *input_manager,
|
|
|
|
const SDL_MouseWheelEvent *event) {
|
2018-02-09 01:14:50 +08:00
|
|
|
struct position position = {
|
2018-02-15 19:07:47 +08:00
|
|
|
.screen_size = input_manager->screen->frame_size,
|
2018-03-05 21:54:12 +08:00
|
|
|
.point = get_mouse_point(input_manager->screen),
|
2018-02-09 01:14:50 +08:00
|
|
|
};
|
|
|
|
struct control_event control_event;
|
|
|
|
if (mouse_wheel_from_sdl_to_android(event, position, &control_event)) {
|
2018-02-15 19:07:47 +08:00
|
|
|
if (!controller_push_event(input_manager->controller, &control_event)) {
|
2018-03-15 23:14:40 +08:00
|
|
|
LOGW("Cannot send mouse wheel event");
|
2018-02-09 01:14:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|