2018-08-15 23:01:54 +08:00
|
|
|
#include "input_manager.h"
|
2018-02-09 01:14:50 +08:00
|
|
|
|
2018-09-09 22:31:41 +08:00
|
|
|
#include <SDL2/SDL_assert.h>
|
2019-09-30 04:36:56 +08:00
|
|
|
|
|
|
|
#include "config.h"
|
2019-09-15 22:27:37 +08:00
|
|
|
#include "event_converter.h"
|
2018-08-15 23:01:54 +08:00
|
|
|
#include "lock_util.h"
|
2018-02-13 17:10:18 +08:00
|
|
|
#include "log.h"
|
2018-02-09 01:14:50 +08:00
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
// Convert window coordinates (as provided by SDL_GetMouseState() to renderer
|
|
|
|
// coordinates (as provided in SDL mouse events)
|
2018-03-05 21:54:12 +08:00
|
|
|
//
|
|
|
|
// See my question:
|
|
|
|
// <https://stackoverflow.com/questions/49111054/how-to-get-mouse-position-on-mouse-wheel-event>
|
2019-03-03 03:09:56 +08:00
|
|
|
static void
|
|
|
|
convert_to_renderer_coordinates(SDL_Renderer *renderer, int *x, int *y) {
|
2018-03-05 21:54:12 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
static struct point
|
|
|
|
get_mouse_point(struct screen *screen) {
|
2018-11-27 15:54:31 +08:00
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
SDL_GetMouseState(&x, &y);
|
|
|
|
convert_to_renderer_coordinates(screen->renderer, &x, &y);
|
|
|
|
return (struct point) {
|
|
|
|
.x = x,
|
|
|
|
.y = y,
|
|
|
|
};
|
2018-02-09 01:14:50 +08:00
|
|
|
}
|
|
|
|
|
2018-06-25 03:09:23 +08:00
|
|
|
static const int ACTION_DOWN = 1;
|
|
|
|
static const int ACTION_UP = 1 << 1;
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
static void
|
|
|
|
send_keycode(struct controller *controller, enum android_keycode keycode,
|
|
|
|
int actions, const char *name) {
|
2018-02-09 01:14:50 +08:00
|
|
|
// send DOWN event
|
2019-05-31 20:55:11 +08:00
|
|
|
struct control_msg msg;
|
|
|
|
msg.type = CONTROL_MSG_TYPE_INJECT_KEYCODE;
|
|
|
|
msg.inject_keycode.keycode = keycode;
|
|
|
|
msg.inject_keycode.metastate = 0;
|
2018-02-09 01:14:50 +08:00
|
|
|
|
2018-06-25 03:09:23 +08:00
|
|
|
if (actions & ACTION_DOWN) {
|
2019-05-31 20:55:11 +08:00
|
|
|
msg.inject_keycode.action = AKEY_EVENT_ACTION_DOWN;
|
|
|
|
if (!controller_push_msg(controller, &msg)) {
|
2019-06-24 02:49:38 +08:00
|
|
|
LOGW("Could not request 'inject %s (DOWN)'", name);
|
2018-06-25 03:09:23 +08:00
|
|
|
return;
|
|
|
|
}
|
2018-02-09 01:14:50 +08:00
|
|
|
}
|
|
|
|
|
2018-06-25 03:09:23 +08:00
|
|
|
if (actions & ACTION_UP) {
|
2019-05-31 20:55:11 +08:00
|
|
|
msg.inject_keycode.action = AKEY_EVENT_ACTION_UP;
|
|
|
|
if (!controller_push_msg(controller, &msg)) {
|
2019-06-24 02:49:38 +08:00
|
|
|
LOGW("Could not request 'inject %s (UP)'", name);
|
2018-06-25 03:09:23 +08:00
|
|
|
}
|
2018-02-09 01:14:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
static inline void
|
|
|
|
action_home(struct controller *controller, int actions) {
|
2018-06-25 03:09:23 +08:00
|
|
|
send_keycode(controller, AKEYCODE_HOME, actions, "HOME");
|
2018-02-09 01:14:50 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
static inline void
|
|
|
|
action_back(struct controller *controller, int actions) {
|
2018-06-25 03:09:23 +08:00
|
|
|
send_keycode(controller, AKEYCODE_BACK, actions, "BACK");
|
2018-02-09 01:14:50 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
static inline void
|
|
|
|
action_app_switch(struct controller *controller, int actions) {
|
2018-06-25 03:09:23 +08:00
|
|
|
send_keycode(controller, AKEYCODE_APP_SWITCH, actions, "APP_SWITCH");
|
2018-02-09 01:14:50 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
static inline void
|
|
|
|
action_power(struct controller *controller, int actions) {
|
2018-06-25 03:09:23 +08:00
|
|
|
send_keycode(controller, AKEYCODE_POWER, actions, "POWER");
|
2018-02-09 01:14:50 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
static inline void
|
|
|
|
action_volume_up(struct controller *controller, int actions) {
|
2018-06-25 03:09:23 +08:00
|
|
|
send_keycode(controller, AKEYCODE_VOLUME_UP, actions, "VOLUME_UP");
|
2018-02-09 01:14:50 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
static inline void
|
|
|
|
action_volume_down(struct controller *controller, int actions) {
|
2018-06-25 03:09:23 +08:00
|
|
|
send_keycode(controller, AKEYCODE_VOLUME_DOWN, actions, "VOLUME_DOWN");
|
2018-02-09 01:14:50 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
static inline void
|
|
|
|
action_menu(struct controller *controller, int actions) {
|
2018-06-25 03:09:23 +08:00
|
|
|
send_keycode(controller, AKEYCODE_MENU, actions, "MENU");
|
2018-03-21 09:04:40 +08:00
|
|
|
}
|
|
|
|
|
2018-03-10 07:11:52 +08:00
|
|
|
// turn the screen on if it was off, press BACK otherwise
|
2019-03-03 03:09:56 +08:00
|
|
|
static void
|
|
|
|
press_back_or_turn_screen_on(struct controller *controller) {
|
2019-05-31 20:55:11 +08:00
|
|
|
struct control_msg msg;
|
|
|
|
msg.type = CONTROL_MSG_TYPE_BACK_OR_SCREEN_ON;
|
2018-03-07 21:46:31 +08:00
|
|
|
|
2019-05-31 20:55:11 +08:00
|
|
|
if (!controller_push_msg(controller, &msg)) {
|
2019-09-28 10:37:25 +08:00
|
|
|
LOGW("Could not request 'press back or turn screen on'");
|
2018-02-09 01:14:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
static void
|
|
|
|
expand_notification_panel(struct controller *controller) {
|
2019-05-31 20:55:11 +08:00
|
|
|
struct control_msg msg;
|
|
|
|
msg.type = CONTROL_MSG_TYPE_EXPAND_NOTIFICATION_PANEL;
|
2019-02-27 03:35:37 +08:00
|
|
|
|
2019-05-31 20:55:11 +08:00
|
|
|
if (!controller_push_msg(controller, &msg)) {
|
2019-06-24 02:49:38 +08:00
|
|
|
LOGW("Could not request 'expand notification panel'");
|
2019-02-27 03:35:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
static void
|
|
|
|
collapse_notification_panel(struct controller *controller) {
|
2019-05-31 20:55:11 +08:00
|
|
|
struct control_msg msg;
|
|
|
|
msg.type = CONTROL_MSG_TYPE_COLLAPSE_NOTIFICATION_PANEL;
|
2019-02-27 03:35:37 +08:00
|
|
|
|
2019-05-31 20:55:11 +08:00
|
|
|
if (!controller_push_msg(controller, &msg)) {
|
2019-06-24 02:49:38 +08:00
|
|
|
LOGW("Could not request 'collapse notification panel'");
|
2019-02-27 03:35:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-30 21:23:01 +08:00
|
|
|
static void
|
|
|
|
request_device_clipboard(struct controller *controller) {
|
2019-05-31 20:55:11 +08:00
|
|
|
struct control_msg msg;
|
|
|
|
msg.type = CONTROL_MSG_TYPE_GET_CLIPBOARD;
|
2019-05-30 21:23:01 +08:00
|
|
|
|
2019-05-31 20:55:11 +08:00
|
|
|
if (!controller_push_msg(controller, &msg)) {
|
2019-06-24 02:49:38 +08:00
|
|
|
LOGW("Could not request device clipboard");
|
2019-05-30 21:23:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-31 02:25:23 +08:00
|
|
|
static void
|
|
|
|
set_device_clipboard(struct controller *controller) {
|
|
|
|
char *text = SDL_GetClipboardText();
|
|
|
|
if (!text) {
|
2019-06-24 02:49:38 +08:00
|
|
|
LOGW("Could not get clipboard text: %s", SDL_GetError());
|
2019-05-31 02:25:23 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!*text) {
|
|
|
|
// empty text
|
|
|
|
SDL_free(text);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-31 20:55:11 +08:00
|
|
|
struct control_msg msg;
|
|
|
|
msg.type = CONTROL_MSG_TYPE_SET_CLIPBOARD;
|
|
|
|
msg.set_clipboard.text = text;
|
2019-05-31 02:25:23 +08:00
|
|
|
|
2019-05-31 20:55:11 +08:00
|
|
|
if (!controller_push_msg(controller, &msg)) {
|
2019-05-31 02:25:23 +08:00
|
|
|
SDL_free(text);
|
2019-06-24 02:49:38 +08:00
|
|
|
LOGW("Could not request 'set device clipboard'");
|
2019-05-31 02:25:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-16 03:23:30 +08:00
|
|
|
static void
|
|
|
|
set_screen_power_mode(struct controller *controller,
|
|
|
|
enum screen_power_mode mode) {
|
|
|
|
struct control_msg msg;
|
|
|
|
msg.type = CONTROL_MSG_TYPE_SET_SCREEN_POWER_MODE;
|
|
|
|
msg.set_screen_power_mode.mode = mode;
|
|
|
|
|
|
|
|
if (!controller_push_msg(controller, &msg)) {
|
2019-06-24 02:49:38 +08:00
|
|
|
LOGW("Could not request 'set screen power mode'");
|
2019-03-16 03:23:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
static void
|
2019-06-07 22:55:19 +08:00
|
|
|
switch_fps_counter_state(struct fps_counter *fps_counter) {
|
|
|
|
// the started state can only be written from the current thread, so there
|
|
|
|
// is no ToCToU issue
|
|
|
|
if (fps_counter_is_started(fps_counter)) {
|
|
|
|
fps_counter_stop(fps_counter);
|
2018-02-15 19:24:16 +08:00
|
|
|
LOGI("FPS counter stopped");
|
|
|
|
} else {
|
2019-06-07 22:55:19 +08:00
|
|
|
if (fps_counter_start(fps_counter)) {
|
|
|
|
LOGI("FPS counter started");
|
|
|
|
} else {
|
|
|
|
LOGE("FPS counter starting failed");
|
|
|
|
}
|
2018-02-15 19:24:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
static void
|
|
|
|
clipboard_paste(struct controller *controller) {
|
2018-03-07 22:29:33 +08:00
|
|
|
char *text = SDL_GetClipboardText();
|
|
|
|
if (!text) {
|
2019-06-24 02:49:38 +08:00
|
|
|
LOGW("Could not get clipboard text: %s", SDL_GetError());
|
2018-03-07 22:29:33 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!*text) {
|
|
|
|
// empty text
|
|
|
|
SDL_free(text);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-31 20:55:11 +08:00
|
|
|
struct control_msg msg;
|
|
|
|
msg.type = CONTROL_MSG_TYPE_INJECT_TEXT;
|
|
|
|
msg.inject_text.text = text;
|
|
|
|
if (!controller_push_msg(controller, &msg)) {
|
2018-03-07 22:29:33 +08:00
|
|
|
SDL_free(text);
|
2019-06-24 02:49:38 +08:00
|
|
|
LOGW("Could not request 'paste clipboard'");
|
2018-03-07 22:29:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
void
|
2019-10-20 17:51:54 +08:00
|
|
|
input_manager_process_text_input(struct input_manager *im,
|
2019-03-03 03:09:56 +08:00
|
|
|
const SDL_TextInputEvent *event) {
|
2018-09-09 22:31:41 +08:00
|
|
|
char c = event->text[0];
|
|
|
|
if (isalpha(c) || c == ' ') {
|
|
|
|
SDL_assert(event->text[1] == '\0');
|
|
|
|
// letters and space are handled as raw key event
|
|
|
|
return;
|
|
|
|
}
|
2019-05-31 20:55:11 +08:00
|
|
|
struct control_msg msg;
|
|
|
|
msg.type = CONTROL_MSG_TYPE_INJECT_TEXT;
|
|
|
|
msg.inject_text.text = SDL_strdup(event->text);
|
|
|
|
if (!msg.inject_text.text) {
|
2019-06-24 02:49:38 +08:00
|
|
|
LOGW("Could not strdup input text");
|
2018-03-07 22:29:33 +08:00
|
|
|
return;
|
|
|
|
}
|
2019-10-20 17:51:54 +08:00
|
|
|
if (!controller_push_msg(im->controller, &msg)) {
|
2019-05-31 20:55:11 +08:00
|
|
|
SDL_free(msg.inject_text.text);
|
2019-06-24 02:49:38 +08:00
|
|
|
LOGW("Could not request 'inject text'");
|
2018-02-09 01:14:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-20 18:49:47 +08:00
|
|
|
static bool
|
|
|
|
convert_input_key(const SDL_KeyboardEvent *from, struct control_msg *to) {
|
|
|
|
to->type = CONTROL_MSG_TYPE_INJECT_KEYCODE;
|
|
|
|
|
|
|
|
if (!convert_keycode_action(from->type, &to->inject_keycode.action)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t mod = from->keysym.mod;
|
|
|
|
if (!convert_keycode(from->keysym.sym, &to->inject_keycode.keycode, mod)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
to->inject_keycode.metastate = convert_meta_state(mod);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
void
|
2019-10-20 17:51:54 +08:00
|
|
|
input_manager_process_key(struct input_manager *im,
|
2019-03-03 18:05:26 +08:00
|
|
|
const SDL_KeyboardEvent *event,
|
|
|
|
bool control) {
|
2019-06-01 05:25:41 +08:00
|
|
|
// control: indicates the state of the command-line option --no-control
|
|
|
|
// ctrl: the Ctrl key
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
bool ctrl = event->keysym.mod & (KMOD_LCTRL | KMOD_RCTRL);
|
|
|
|
bool alt = event->keysym.mod & (KMOD_LALT | KMOD_RALT);
|
|
|
|
bool meta = event->keysym.mod & (KMOD_LGUI | KMOD_RGUI);
|
2018-10-25 00:51:02 +08:00
|
|
|
|
2019-07-31 22:11:08 +08:00
|
|
|
// use Cmd on macOS, Ctrl on other platforms
|
|
|
|
#ifdef __APPLE__
|
|
|
|
bool cmd = !ctrl && meta;
|
|
|
|
#else
|
|
|
|
if (meta) {
|
|
|
|
// no shortcuts involve Meta on platforms other than macOS, and it must
|
|
|
|
// not be forwarded to the device
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
bool cmd = ctrl; // && !meta, already guaranteed
|
|
|
|
#endif
|
|
|
|
|
2018-10-25 01:03:07 +08:00
|
|
|
if (alt) {
|
2019-07-31 22:11:08 +08:00
|
|
|
// no shortcuts involve Alt, and it must not be forwarded to the device
|
2018-10-25 00:51:02 +08:00
|
|
|
return;
|
|
|
|
}
|
2018-02-09 01:14:50 +08:00
|
|
|
|
2019-10-20 17:51:54 +08:00
|
|
|
struct controller *controller = im->controller;
|
2019-06-01 05:25:41 +08:00
|
|
|
|
2018-02-09 01:14:50 +08:00
|
|
|
// capture all Ctrl events
|
2019-07-31 22:11:08 +08:00
|
|
|
if (ctrl || cmd) {
|
2018-03-06 18:11:07 +08:00
|
|
|
SDL_Keycode keycode = event->keysym.sym;
|
2019-06-01 05:25:41 +08:00
|
|
|
bool down = event->type == SDL_KEYDOWN;
|
|
|
|
int action = down ? ACTION_DOWN : ACTION_UP;
|
2019-03-03 06:52:22 +08:00
|
|
|
bool repeat = event->repeat;
|
|
|
|
bool shift = event->keysym.mod & (KMOD_LSHIFT | KMOD_RSHIFT);
|
2018-02-09 01:14:50 +08:00
|
|
|
switch (keycode) {
|
|
|
|
case SDLK_h:
|
2019-07-31 22:11:08 +08:00
|
|
|
// Ctrl+h on all platform, since Cmd+h is already captured by
|
|
|
|
// the system on macOS to hide the window
|
2019-03-03 05:40:51 +08:00
|
|
|
if (control && ctrl && !meta && !shift && !repeat) {
|
2019-06-01 05:25:41 +08:00
|
|
|
action_home(controller, action);
|
2018-06-25 03:48:34 +08:00
|
|
|
}
|
2018-02-09 01:14:50 +08:00
|
|
|
return;
|
|
|
|
case SDLK_b: // fall-through
|
|
|
|
case SDLK_BACKSPACE:
|
2019-07-31 22:11:08 +08:00
|
|
|
if (control && cmd && !shift && !repeat) {
|
2019-06-01 05:25:41 +08:00
|
|
|
action_back(controller, action);
|
2018-06-25 03:48:34 +08:00
|
|
|
}
|
2018-02-09 01:14:50 +08:00
|
|
|
return;
|
2018-03-23 17:10:24 +08:00
|
|
|
case SDLK_s:
|
2019-07-31 22:11:08 +08:00
|
|
|
if (control && cmd && !shift && !repeat) {
|
2019-06-01 05:25:41 +08:00
|
|
|
action_app_switch(controller, action);
|
2018-06-25 03:48:34 +08:00
|
|
|
}
|
2018-02-09 01:14:50 +08:00
|
|
|
return;
|
2018-03-23 17:10:24 +08:00
|
|
|
case SDLK_m:
|
2019-07-31 22:11:08 +08:00
|
|
|
// Ctrl+m on all platform, since Cmd+m is already captured by
|
|
|
|
// the system on macOS to minimize the window
|
2019-03-03 05:40:51 +08:00
|
|
|
if (control && ctrl && !meta && !shift && !repeat) {
|
2019-06-01 05:25:41 +08:00
|
|
|
action_menu(controller, action);
|
2018-06-25 03:48:34 +08:00
|
|
|
}
|
2018-03-21 09:04:40 +08:00
|
|
|
return;
|
2018-02-09 01:14:50 +08:00
|
|
|
case SDLK_p:
|
2019-07-31 22:11:08 +08:00
|
|
|
if (control && cmd && !shift && !repeat) {
|
2019-06-01 05:25:41 +08:00
|
|
|
action_power(controller, action);
|
2018-06-25 03:48:34 +08:00
|
|
|
}
|
2018-02-09 01:14:50 +08:00
|
|
|
return;
|
2019-03-16 03:23:30 +08:00
|
|
|
case SDLK_o:
|
2019-07-31 22:11:08 +08:00
|
|
|
if (control && cmd && !shift && down) {
|
2019-06-03 17:44:39 +08:00
|
|
|
set_screen_power_mode(controller, SCREEN_POWER_MODE_OFF);
|
2019-03-16 03:23:30 +08:00
|
|
|
}
|
|
|
|
return;
|
2018-04-03 20:20:33 +08:00
|
|
|
case SDLK_DOWN:
|
2019-07-31 22:11:08 +08:00
|
|
|
if (control && cmd && !shift) {
|
2018-10-25 01:03:07 +08:00
|
|
|
// forward repeated events
|
2019-06-01 05:25:41 +08:00
|
|
|
action_volume_down(controller, action);
|
2018-10-25 01:03:07 +08:00
|
|
|
}
|
2018-04-03 20:20:33 +08:00
|
|
|
return;
|
|
|
|
case SDLK_UP:
|
2019-07-31 22:11:08 +08:00
|
|
|
if (control && cmd && !shift) {
|
2018-10-25 01:03:07 +08:00
|
|
|
// forward repeated events
|
2019-06-01 05:25:41 +08:00
|
|
|
action_volume_up(controller, action);
|
2018-10-25 01:03:07 +08:00
|
|
|
}
|
2018-04-03 20:20:33 +08:00
|
|
|
return;
|
2019-05-30 21:23:01 +08:00
|
|
|
case SDLK_c:
|
2019-07-31 22:11:08 +08:00
|
|
|
if (control && cmd && !shift && !repeat && down) {
|
2019-06-01 05:25:41 +08:00
|
|
|
request_device_clipboard(controller);
|
2019-05-30 21:23:01 +08:00
|
|
|
}
|
|
|
|
return;
|
2018-03-07 22:29:33 +08:00
|
|
|
case SDLK_v:
|
2019-07-31 22:11:08 +08:00
|
|
|
if (control && cmd && !repeat && down) {
|
2019-05-31 02:25:23 +08:00
|
|
|
if (shift) {
|
|
|
|
// store the text in the device clipboard
|
2019-06-01 05:25:41 +08:00
|
|
|
set_device_clipboard(controller);
|
2019-05-31 02:25:23 +08:00
|
|
|
} else {
|
|
|
|
// inject the text as input events
|
2019-06-01 05:25:41 +08:00
|
|
|
clipboard_paste(controller);
|
2019-05-31 02:25:23 +08:00
|
|
|
}
|
2018-06-25 03:09:23 +08:00
|
|
|
}
|
2018-03-07 22:29:33 +08:00
|
|
|
return;
|
2018-02-09 01:14:50 +08:00
|
|
|
case SDLK_f:
|
2019-07-31 22:11:08 +08:00
|
|
|
if (!shift && cmd && !repeat && down) {
|
2019-10-20 17:51:54 +08:00
|
|
|
screen_switch_fullscreen(im->screen);
|
2018-06-25 03:09:23 +08:00
|
|
|
}
|
2018-02-09 01:14:50 +08:00
|
|
|
return;
|
|
|
|
case SDLK_x:
|
2019-07-31 22:11:08 +08:00
|
|
|
if (!shift && cmd && !repeat && down) {
|
2019-10-20 17:51:54 +08:00
|
|
|
screen_resize_to_fit(im->screen);
|
2018-06-25 03:09:23 +08:00
|
|
|
}
|
2018-02-09 01:14:50 +08:00
|
|
|
return;
|
|
|
|
case SDLK_g:
|
2019-07-31 22:11:08 +08:00
|
|
|
if (!shift && cmd && !repeat && down) {
|
2019-10-20 17:51:54 +08:00
|
|
|
screen_resize_to_pixel_perfect(im->screen);
|
2018-06-25 03:09:23 +08:00
|
|
|
}
|
2018-02-09 01:14:50 +08:00
|
|
|
return;
|
2018-02-15 19:24:16 +08:00
|
|
|
case SDLK_i:
|
2019-07-31 22:11:08 +08:00
|
|
|
if (!shift && cmd && !repeat && down) {
|
2019-06-07 22:55:19 +08:00
|
|
|
struct fps_counter *fps_counter =
|
2019-10-20 17:51:54 +08:00
|
|
|
im->video_buffer->fps_counter;
|
2019-06-07 22:55:19 +08:00
|
|
|
switch_fps_counter_state(fps_counter);
|
2018-06-25 03:09:23 +08:00
|
|
|
}
|
2019-02-27 03:35:37 +08:00
|
|
|
return;
|
|
|
|
case SDLK_n:
|
2019-07-31 22:11:08 +08:00
|
|
|
if (control && cmd && !repeat && down) {
|
2019-02-27 03:35:37 +08:00
|
|
|
if (shift) {
|
2019-06-01 05:25:41 +08:00
|
|
|
collapse_notification_panel(controller);
|
2019-02-27 03:35:37 +08:00
|
|
|
} else {
|
2019-06-01 05:25:41 +08:00
|
|
|
expand_notification_panel(controller);
|
2019-02-27 03:35:37 +08:00
|
|
|
}
|
|
|
|
}
|
2018-02-15 19:24:16 +08:00
|
|
|
return;
|
2018-02-09 01:14:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-03 05:40:51 +08:00
|
|
|
if (!control) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-31 20:55:11 +08:00
|
|
|
struct control_msg msg;
|
2019-09-15 22:32:46 +08:00
|
|
|
if (convert_input_key(event, &msg)) {
|
2019-06-01 05:25:41 +08:00
|
|
|
if (!controller_push_msg(controller, &msg)) {
|
2019-06-24 02:49:38 +08:00
|
|
|
LOGW("Could not request 'inject keycode'");
|
2018-02-09 01:14:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-20 18:49:47 +08:00
|
|
|
static bool
|
|
|
|
convert_mouse_motion(const SDL_MouseMotionEvent *from, struct size screen_size,
|
|
|
|
struct control_msg *to) {
|
|
|
|
to->type = CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT;
|
|
|
|
to->inject_touch_event.action = AMOTION_EVENT_ACTION_MOVE;
|
|
|
|
to->inject_touch_event.pointer_id = POINTER_ID_MOUSE;
|
|
|
|
to->inject_touch_event.position.screen_size = screen_size;
|
|
|
|
to->inject_touch_event.position.point.x = from->x;
|
|
|
|
to->inject_touch_event.position.point.y = from->y;
|
|
|
|
to->inject_touch_event.pressure = 1.f;
|
|
|
|
to->inject_touch_event.buttons = convert_mouse_buttons(from->state);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
void
|
2019-10-20 17:51:54 +08:00
|
|
|
input_manager_process_mouse_motion(struct input_manager *im,
|
2019-03-03 03:09:56 +08:00
|
|
|
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;
|
|
|
|
}
|
2019-09-23 03:33:16 +08:00
|
|
|
if (event->which == SDL_TOUCH_MOUSEID) {
|
|
|
|
// simulated from touch events, so it's a duplicate
|
|
|
|
return;
|
|
|
|
}
|
2019-05-31 20:55:11 +08:00
|
|
|
struct control_msg msg;
|
2019-10-20 17:51:54 +08:00
|
|
|
if (convert_mouse_motion(event, im->screen->frame_size, &msg)) {
|
|
|
|
if (!controller_push_msg(im->controller, &msg)) {
|
2019-06-24 02:49:38 +08:00
|
|
|
LOGW("Could not request 'inject mouse motion event'");
|
2018-02-09 01:14:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-20 18:49:47 +08:00
|
|
|
static bool
|
|
|
|
convert_touch(const SDL_TouchFingerEvent *from, struct size screen_size,
|
|
|
|
struct control_msg *to) {
|
|
|
|
to->type = CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT;
|
|
|
|
|
|
|
|
if (!convert_touch_action(from->type, &to->inject_touch_event.action)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
to->inject_touch_event.pointer_id = from->fingerId;
|
|
|
|
to->inject_touch_event.position.screen_size = screen_size;
|
|
|
|
// SDL touch event coordinates are normalized in the range [0; 1]
|
|
|
|
to->inject_touch_event.position.point.x = from->x * screen_size.width;
|
|
|
|
to->inject_touch_event.position.point.y = from->y * screen_size.height;
|
|
|
|
to->inject_touch_event.pressure = from->pressure;
|
|
|
|
to->inject_touch_event.buttons = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-09-23 03:30:05 +08:00
|
|
|
void
|
2019-10-20 17:51:54 +08:00
|
|
|
input_manager_process_touch(struct input_manager *im,
|
2019-09-23 03:30:05 +08:00
|
|
|
const SDL_TouchFingerEvent *event) {
|
|
|
|
struct control_msg msg;
|
2019-10-20 17:51:54 +08:00
|
|
|
if (convert_touch(event, im->screen->frame_size, &msg)) {
|
|
|
|
if (!controller_push_msg(im->controller, &msg)) {
|
2019-09-23 03:30:05 +08:00
|
|
|
LOGW("Could not request 'inject touch event'");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
static bool
|
2019-10-20 17:51:54 +08:00
|
|
|
is_outside_device_screen(struct input_manager *im, int x, int y)
|
2018-11-19 04:20:21 +08:00
|
|
|
{
|
2019-10-20 17:51:54 +08:00
|
|
|
return x < 0 || x >= im->screen->frame_size.width ||
|
|
|
|
y < 0 || y >= im->screen->frame_size.height;
|
2018-11-19 04:20:21 +08:00
|
|
|
}
|
|
|
|
|
2019-10-20 18:49:47 +08:00
|
|
|
static bool
|
|
|
|
convert_mouse_button(const SDL_MouseButtonEvent *from, struct size screen_size,
|
|
|
|
struct control_msg *to) {
|
|
|
|
to->type = CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT;
|
|
|
|
|
|
|
|
if (!convert_mouse_action(from->type, &to->inject_touch_event.action)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
to->inject_touch_event.pointer_id = POINTER_ID_MOUSE;
|
|
|
|
to->inject_touch_event.position.screen_size = screen_size;
|
|
|
|
to->inject_touch_event.position.point.x = from->x;
|
|
|
|
to->inject_touch_event.position.point.y = from->y;
|
|
|
|
to->inject_touch_event.pressure = 1.f;
|
|
|
|
to->inject_touch_event.buttons =
|
|
|
|
convert_mouse_buttons(SDL_BUTTON(from->button));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
void
|
2019-10-20 17:51:54 +08:00
|
|
|
input_manager_process_mouse_button(struct input_manager *im,
|
2019-03-03 18:05:26 +08:00
|
|
|
const SDL_MouseButtonEvent *event,
|
|
|
|
bool control) {
|
2019-09-23 03:33:16 +08:00
|
|
|
if (event->which == SDL_TOUCH_MOUSEID) {
|
|
|
|
// simulated from touch events, so it's a duplicate
|
|
|
|
return;
|
|
|
|
}
|
2018-03-10 07:40:55 +08:00
|
|
|
if (event->type == SDL_MOUSEBUTTONDOWN) {
|
2019-03-03 05:40:51 +08:00
|
|
|
if (control && event->button == SDL_BUTTON_RIGHT) {
|
2019-10-20 17:51:54 +08:00
|
|
|
press_back_or_turn_screen_on(im->controller);
|
2018-03-10 07:40:55 +08:00
|
|
|
return;
|
|
|
|
}
|
2019-03-03 05:40:51 +08:00
|
|
|
if (control && event->button == SDL_BUTTON_MIDDLE) {
|
2019-10-20 17:51:54 +08:00
|
|
|
action_home(im->controller, ACTION_DOWN | ACTION_UP);
|
2018-03-10 07:40:55 +08:00
|
|
|
return;
|
|
|
|
}
|
2018-03-13 15:32:48 +08:00
|
|
|
// double-click on black borders resize to fit the device screen
|
2018-11-27 15:54:31 +08:00
|
|
|
if (event->button == SDL_BUTTON_LEFT && event->clicks == 2) {
|
2019-05-31 20:55:11 +08:00
|
|
|
bool outside =
|
2019-10-20 17:51:54 +08:00
|
|
|
is_outside_device_screen(im, event->x, event->y);
|
2018-11-27 15:54:31 +08:00
|
|
|
if (outside) {
|
2019-10-20 17:51:54 +08:00
|
|
|
screen_resize_to_fit(im->screen);
|
2019-01-18 20:21:25 +08:00
|
|
|
return;
|
2018-11-27 15:54:31 +08:00
|
|
|
}
|
2018-03-13 15:32:48 +08:00
|
|
|
}
|
2018-11-19 04:20:21 +08:00
|
|
|
// otherwise, send the click event to the device
|
2018-08-11 21:06:38 +08:00
|
|
|
}
|
2018-11-19 04:20:21 +08:00
|
|
|
|
2019-03-03 05:40:51 +08:00
|
|
|
if (!control) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-31 20:55:11 +08:00
|
|
|
struct control_msg msg;
|
2019-10-20 17:51:54 +08:00
|
|
|
if (convert_mouse_button(event, im->screen->frame_size, &msg)) {
|
|
|
|
if (!controller_push_msg(im->controller, &msg)) {
|
2019-06-24 02:49:38 +08:00
|
|
|
LOGW("Could not request 'inject mouse button event'");
|
2018-02-09 01:14:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-20 18:49:47 +08:00
|
|
|
static bool
|
|
|
|
convert_mouse_wheel(const SDL_MouseWheelEvent *from, struct position position,
|
|
|
|
struct control_msg *to) {
|
|
|
|
to->type = CONTROL_MSG_TYPE_INJECT_SCROLL_EVENT;
|
|
|
|
|
|
|
|
to->inject_scroll_event.position = position;
|
|
|
|
|
|
|
|
int mul = from->direction == SDL_MOUSEWHEEL_NORMAL ? 1 : -1;
|
|
|
|
// SDL behavior seems inconsistent between horizontal and vertical scrolling
|
|
|
|
// so reverse the horizontal
|
|
|
|
// <https://wiki.libsdl.org/SDL_MouseWheelEvent#Remarks>
|
|
|
|
to->inject_scroll_event.hscroll = -mul * from->x;
|
|
|
|
to->inject_scroll_event.vscroll = mul * from->y;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
void
|
2019-10-20 17:51:54 +08:00
|
|
|
input_manager_process_mouse_wheel(struct input_manager *im,
|
2019-03-03 03:09:56 +08:00
|
|
|
const SDL_MouseWheelEvent *event) {
|
2018-02-09 01:14:50 +08:00
|
|
|
struct position position = {
|
2019-10-20 17:51:54 +08:00
|
|
|
.screen_size = im->screen->frame_size,
|
|
|
|
.point = get_mouse_point(im->screen),
|
2018-02-09 01:14:50 +08:00
|
|
|
};
|
2019-05-31 20:55:11 +08:00
|
|
|
struct control_msg msg;
|
2019-09-15 22:32:46 +08:00
|
|
|
if (convert_mouse_wheel(event, position, &msg)) {
|
2019-10-20 17:51:54 +08:00
|
|
|
if (!controller_push_msg(im->controller, &msg)) {
|
2019-06-24 02:49:38 +08:00
|
|
|
LOGW("Could not request 'inject mouse wheel event'");
|
2018-02-09 01:14:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|