Path: blob/master/platform/android/android_input_handler.cpp
10277 views
/**************************************************************************/1/* android_input_handler.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "android_input_handler.h"3132#include "android_keys_utils.h"33#include "display_server_android.h"3435void AndroidInputHandler::process_joy_event(AndroidInputHandler::JoypadEvent p_event) {36switch (p_event.type) {37case JOY_EVENT_BUTTON:38Input::get_singleton()->joy_button(p_event.device, (JoyButton)p_event.index, p_event.pressed);39break;40case JOY_EVENT_AXIS:41Input::get_singleton()->joy_axis(p_event.device, (JoyAxis)p_event.index, p_event.value);42break;43case JOY_EVENT_HAT:44Input::get_singleton()->joy_hat(p_event.device, p_event.hat);45break;46default:47return;48}49}5051void AndroidInputHandler::_set_key_modifier_state(Ref<InputEventWithModifiers> ev, Key p_keycode) {52if (p_keycode != Key::SHIFT) {53ev->set_shift_pressed(shift_mem);54}55if (p_keycode != Key::ALT) {56ev->set_alt_pressed(alt_mem);57}58if (p_keycode != Key::META) {59ev->set_meta_pressed(meta_mem);60}61if (p_keycode != Key::CTRL) {62ev->set_ctrl_pressed(control_mem);63}64}6566void AndroidInputHandler::process_key_event(int p_physical_keycode, int p_unicode, int p_key_label, bool p_pressed, bool p_echo) {67static char32_t prev_wc = 0;68char32_t unicode = p_unicode;69if ((p_unicode & 0xfffffc00) == 0xd800) {70if (prev_wc != 0) {71ERR_PRINT("invalid utf16 surrogate input");72}73prev_wc = unicode;74return; // Skip surrogate.75} else if ((unicode & 0xfffffc00) == 0xdc00) {76if (prev_wc == 0) {77ERR_PRINT("invalid utf16 surrogate input");78return; // Skip invalid surrogate.79}80unicode = (prev_wc << 10UL) + unicode - ((0xd800 << 10UL) + 0xdc00 - 0x10000);81prev_wc = 0;82} else {83prev_wc = 0;84}8586Ref<InputEventKey> ev;87ev.instantiate();8889Key physical_keycode = godot_code_from_android_code(p_physical_keycode);90Key keycode;91if (unicode == '\b') { // 0x0892keycode = Key::BACKSPACE;93} else if (unicode == '\t') { // 0x0994keycode = Key::TAB;95} else if (unicode == '\n') { // 0x0A96keycode = Key::ENTER;97} else if (unicode == 0x1B) {98keycode = Key::ESCAPE;99} else if (unicode == 0x1F) {100keycode = Key::KEY_DELETE;101} else {102keycode = fix_keycode(unicode, physical_keycode);103}104105switch (physical_keycode) {106case Key::SHIFT: {107shift_mem = p_pressed;108} break;109case Key::ALT: {110alt_mem = p_pressed;111} break;112case Key::CTRL: {113control_mem = p_pressed;114} break;115case Key::META: {116meta_mem = p_pressed;117} break;118default:119break;120}121122ev->set_keycode(keycode);123ev->set_physical_keycode(physical_keycode);124ev->set_key_label(fix_key_label(p_key_label, keycode));125ev->set_unicode(fix_unicode(unicode));126ev->set_location(godot_location_from_android_code(p_physical_keycode));127ev->set_pressed(p_pressed);128ev->set_echo(p_echo);129130_set_key_modifier_state(ev, keycode);131132if (p_physical_keycode == AKEYCODE_BACK && p_pressed) {133if (DisplayServerAndroid *dsa = Object::cast_to<DisplayServerAndroid>(DisplayServer::get_singleton())) {134dsa->send_window_event(DisplayServer::WINDOW_EVENT_GO_BACK_REQUEST, true);135}136}137138Input::get_singleton()->parse_input_event(ev);139}140141void AndroidInputHandler::_cancel_all_touch() {142_parse_all_touch(false, true);143touch.clear();144}145146void AndroidInputHandler::_parse_all_touch(bool p_pressed, bool p_canceled, bool p_double_tap) {147if (touch.size()) {148//end all if exist149for (int i = 0; i < touch.size(); i++) {150Ref<InputEventScreenTouch> ev;151ev.instantiate();152ev->set_index(touch[i].id);153ev->set_pressed(p_pressed);154ev->set_canceled(p_canceled);155ev->set_position(touch[i].pos);156ev->set_double_tap(p_double_tap);157Input::get_singleton()->parse_input_event(ev);158}159}160}161162void AndroidInputHandler::_release_all_touch() {163_parse_all_touch(false, false);164touch.clear();165}166167void AndroidInputHandler::process_touch_event(int p_event, int p_pointer, const Vector<TouchPos> &p_points, bool p_double_tap) {168switch (p_event) {169case AMOTION_EVENT_ACTION_DOWN: { //gesture begin170// Release any remaining touches or mouse event171_release_mouse_event_info();172_release_all_touch();173174touch.resize(p_points.size());175for (int i = 0; i < p_points.size(); i++) {176touch.write[i].id = p_points[i].id;177touch.write[i].pos = p_points[i].pos;178touch.write[i].pressure = p_points[i].pressure;179touch.write[i].tilt = p_points[i].tilt;180}181182//send touch183_parse_all_touch(true, false, p_double_tap);184185} break;186case AMOTION_EVENT_ACTION_MOVE: { //motion187if (touch.size() != p_points.size()) {188return;189}190191for (int i = 0; i < touch.size(); i++) {192int idx = -1;193for (int j = 0; j < p_points.size(); j++) {194if (touch[i].id == p_points[j].id) {195idx = j;196break;197}198}199200ERR_CONTINUE(idx == -1);201202if (touch[i].pos == p_points[idx].pos) {203continue; // Don't move unnecessarily.204}205206Ref<InputEventScreenDrag> ev;207ev.instantiate();208ev->set_index(touch[i].id);209ev->set_position(p_points[idx].pos);210ev->set_relative(p_points[idx].pos - touch[i].pos);211ev->set_relative_screen_position(ev->get_relative());212ev->set_pressure(p_points[idx].pressure);213ev->set_tilt(p_points[idx].tilt);214Input::get_singleton()->parse_input_event(ev);215touch.write[i].pos = p_points[idx].pos;216}217218} break;219case AMOTION_EVENT_ACTION_CANCEL: {220_cancel_all_touch();221} break;222case AMOTION_EVENT_ACTION_UP: { //release223_release_all_touch();224} break;225case AMOTION_EVENT_ACTION_POINTER_DOWN: { // add touch226for (int i = 0; i < p_points.size(); i++) {227if (p_points[i].id == p_pointer) {228TouchPos tp = p_points[i];229touch.push_back(tp);230231Ref<InputEventScreenTouch> ev;232ev.instantiate();233234ev->set_index(tp.id);235ev->set_pressed(true);236ev->set_position(tp.pos);237Input::get_singleton()->parse_input_event(ev);238239break;240}241}242} break;243case AMOTION_EVENT_ACTION_POINTER_UP: { // remove touch244for (int i = 0; i < touch.size(); i++) {245if (touch[i].id == p_pointer) {246Ref<InputEventScreenTouch> ev;247ev.instantiate();248ev->set_index(touch[i].id);249ev->set_pressed(false);250ev->set_position(touch[i].pos);251Input::get_singleton()->parse_input_event(ev);252touch.remove_at(i);253254break;255}256}257} break;258}259}260261void AndroidInputHandler::_cancel_mouse_event_info(bool p_source_mouse_relative) {262buttons_state = BitField<MouseButtonMask>();263_parse_mouse_event_info(BitField<MouseButtonMask>(), false, true, false, p_source_mouse_relative);264mouse_event_info.valid = false;265}266267void AndroidInputHandler::_parse_mouse_event_info(BitField<MouseButtonMask> event_buttons_mask, bool p_pressed, bool p_canceled, bool p_double_click, bool p_source_mouse_relative) {268if (!mouse_event_info.valid) {269return;270}271272Ref<InputEventMouseButton> ev;273ev.instantiate();274_set_key_modifier_state(ev, Key::NONE);275if (p_source_mouse_relative) {276ev->set_position(hover_prev_pos);277ev->set_global_position(hover_prev_pos);278} else {279ev->set_position(mouse_event_info.pos);280ev->set_global_position(mouse_event_info.pos);281hover_prev_pos = mouse_event_info.pos;282}283ev->set_pressed(p_pressed);284ev->set_canceled(p_canceled);285BitField<MouseButtonMask> changed_button_mask = buttons_state.get_different(event_buttons_mask);286287buttons_state = event_buttons_mask;288289ev->set_button_index(_button_index_from_mask(changed_button_mask));290ev->set_button_mask(event_buttons_mask);291ev->set_double_click(p_double_click);292Input::get_singleton()->parse_input_event(ev);293}294295void AndroidInputHandler::_release_mouse_event_info(bool p_source_mouse_relative) {296_parse_mouse_event_info(BitField<MouseButtonMask>(), false, false, false, p_source_mouse_relative);297mouse_event_info.valid = false;298}299300void AndroidInputHandler::process_mouse_event(int p_event_action, int p_event_android_buttons_mask, Point2 p_event_pos, Vector2 p_delta, bool p_double_click, bool p_source_mouse_relative, float p_pressure, Vector2 p_tilt) {301BitField<MouseButtonMask> event_buttons_mask = _android_button_mask_to_godot_button_mask(p_event_android_buttons_mask);302switch (p_event_action) {303case AMOTION_EVENT_ACTION_HOVER_MOVE: // hover move304case AMOTION_EVENT_ACTION_HOVER_ENTER: // hover enter305case AMOTION_EVENT_ACTION_HOVER_EXIT: { // hover exit306// https://developer.android.com/reference/android/view/MotionEvent.html#ACTION_HOVER_ENTER307Ref<InputEventMouseMotion> ev;308ev.instantiate();309_set_key_modifier_state(ev, Key::NONE);310ev->set_position(p_event_pos);311ev->set_global_position(p_event_pos);312ev->set_relative(p_event_pos - hover_prev_pos);313ev->set_relative_screen_position(ev->get_relative());314Input::get_singleton()->parse_input_event(ev);315hover_prev_pos = p_event_pos;316} break;317318case AMOTION_EVENT_ACTION_DOWN:319case AMOTION_EVENT_ACTION_BUTTON_PRESS: {320// Release any remaining touches or mouse event321_release_mouse_event_info();322_release_all_touch();323324mouse_event_info.valid = true;325mouse_event_info.pos = p_event_pos;326_parse_mouse_event_info(event_buttons_mask, true, false, p_double_click, p_source_mouse_relative);327} break;328329case AMOTION_EVENT_ACTION_CANCEL: {330_cancel_mouse_event_info(p_source_mouse_relative);331} break;332333case AMOTION_EVENT_ACTION_UP:334case AMOTION_EVENT_ACTION_BUTTON_RELEASE: {335_release_mouse_event_info(p_source_mouse_relative);336} break;337338case AMOTION_EVENT_ACTION_MOVE: {339if (!p_source_mouse_relative && !mouse_event_info.valid) {340return;341}342343Ref<InputEventMouseMotion> ev;344ev.instantiate();345_set_key_modifier_state(ev, Key::NONE);346if (p_source_mouse_relative) {347ev->set_position(hover_prev_pos);348ev->set_global_position(hover_prev_pos);349ev->set_relative(p_event_pos);350ev->set_relative_screen_position(p_event_pos);351} else {352ev->set_position(p_event_pos);353ev->set_global_position(p_event_pos);354ev->set_relative(p_event_pos - hover_prev_pos);355ev->set_relative_screen_position(ev->get_relative());356mouse_event_info.pos = p_event_pos;357hover_prev_pos = p_event_pos;358}359ev->set_button_mask(event_buttons_mask);360ev->set_pressure(p_pressure);361ev->set_tilt(p_tilt);362Input::get_singleton()->parse_input_event(ev);363} break;364365case AMOTION_EVENT_ACTION_SCROLL: {366Ref<InputEventMouseButton> ev;367ev.instantiate();368_set_key_modifier_state(ev, Key::NONE);369if (p_source_mouse_relative) {370ev->set_position(hover_prev_pos);371ev->set_global_position(hover_prev_pos);372} else {373ev->set_position(p_event_pos);374ev->set_global_position(p_event_pos);375}376ev->set_pressed(true);377buttons_state = event_buttons_mask;378if (p_delta.y > 0) {379_wheel_button_click(event_buttons_mask, ev, MouseButton::WHEEL_UP, p_delta.y);380} else if (p_delta.y < 0) {381_wheel_button_click(event_buttons_mask, ev, MouseButton::WHEEL_DOWN, -p_delta.y);382}383384if (p_delta.x > 0) {385_wheel_button_click(event_buttons_mask, ev, MouseButton::WHEEL_RIGHT, p_delta.x);386} else if (p_delta.x < 0) {387_wheel_button_click(event_buttons_mask, ev, MouseButton::WHEEL_LEFT, -p_delta.x);388}389} break;390}391}392393void AndroidInputHandler::_wheel_button_click(BitField<MouseButtonMask> event_buttons_mask, const Ref<InputEventMouseButton> &ev, MouseButton wheel_button, float factor) {394Ref<InputEventMouseButton> evd = ev->duplicate();395_set_key_modifier_state(evd, Key::NONE);396evd->set_button_index(wheel_button);397evd->set_button_mask(event_buttons_mask.get_different(mouse_button_to_mask(wheel_button)));398evd->set_factor(factor);399Input::get_singleton()->parse_input_event(evd);400Ref<InputEventMouseButton> evdd = evd->duplicate();401evdd->set_pressed(false);402evdd->set_button_mask(event_buttons_mask);403Input::get_singleton()->parse_input_event(evdd);404}405406void AndroidInputHandler::process_magnify(Point2 p_pos, float p_factor) {407Ref<InputEventMagnifyGesture> magnify_event;408magnify_event.instantiate();409_set_key_modifier_state(magnify_event, Key::NONE);410magnify_event->set_position(p_pos);411magnify_event->set_factor(p_factor);412Input::get_singleton()->parse_input_event(magnify_event);413}414415void AndroidInputHandler::process_pan(Point2 p_pos, Vector2 p_delta) {416Ref<InputEventPanGesture> pan_event;417pan_event.instantiate();418_set_key_modifier_state(pan_event, Key::NONE);419pan_event->set_position(p_pos);420pan_event->set_delta(p_delta);421Input::get_singleton()->parse_input_event(pan_event);422}423424MouseButton AndroidInputHandler::_button_index_from_mask(BitField<MouseButtonMask> button_mask) {425switch (button_mask) {426case MouseButtonMask::LEFT:427return MouseButton::LEFT;428case MouseButtonMask::RIGHT:429return MouseButton::RIGHT;430case MouseButtonMask::MIDDLE:431return MouseButton::MIDDLE;432case MouseButtonMask::MB_XBUTTON1:433return MouseButton::MB_XBUTTON1;434case MouseButtonMask::MB_XBUTTON2:435return MouseButton::MB_XBUTTON2;436default:437return MouseButton::NONE;438}439}440441BitField<MouseButtonMask> AndroidInputHandler::_android_button_mask_to_godot_button_mask(int android_button_mask) {442BitField<MouseButtonMask> godot_button_mask = MouseButtonMask::NONE;443if (android_button_mask & AMOTION_EVENT_BUTTON_PRIMARY) {444godot_button_mask.set_flag(MouseButtonMask::LEFT);445}446if (android_button_mask & AMOTION_EVENT_BUTTON_SECONDARY) {447godot_button_mask.set_flag(MouseButtonMask::RIGHT);448}449if (android_button_mask & AMOTION_EVENT_BUTTON_TERTIARY) {450godot_button_mask.set_flag(MouseButtonMask::MIDDLE);451}452if (android_button_mask & AMOTION_EVENT_BUTTON_BACK) {453godot_button_mask.set_flag(MouseButtonMask::MB_XBUTTON1);454}455if (android_button_mask & AMOTION_EVENT_BUTTON_FORWARD) {456godot_button_mask.set_flag(MouseButtonMask::MB_XBUTTON2);457}458459return godot_button_mask;460}461462463