| 1 | /**************************************************************************/ |
| 2 | /* android_input_handler.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "android_input_handler.h" |
| 32 | |
| 33 | #include "android_keys_utils.h" |
| 34 | #include "display_server_android.h" |
| 35 | |
| 36 | void AndroidInputHandler::process_joy_event(AndroidInputHandler::JoypadEvent p_event) { |
| 37 | switch (p_event.type) { |
| 38 | case JOY_EVENT_BUTTON: |
| 39 | Input::get_singleton()->joy_button(p_event.device, (JoyButton)p_event.index, p_event.pressed); |
| 40 | break; |
| 41 | case JOY_EVENT_AXIS: |
| 42 | Input::get_singleton()->joy_axis(p_event.device, (JoyAxis)p_event.index, p_event.value); |
| 43 | break; |
| 44 | case JOY_EVENT_HAT: |
| 45 | Input::get_singleton()->joy_hat(p_event.device, p_event.hat); |
| 46 | break; |
| 47 | default: |
| 48 | return; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | void AndroidInputHandler::_set_key_modifier_state(Ref<InputEventWithModifiers> ev, Key p_keycode) { |
| 53 | if (p_keycode != Key::SHIFT) { |
| 54 | ev->set_shift_pressed(shift_mem); |
| 55 | } |
| 56 | if (p_keycode != Key::ALT) { |
| 57 | ev->set_alt_pressed(alt_mem); |
| 58 | } |
| 59 | if (p_keycode != Key::META) { |
| 60 | ev->set_meta_pressed(meta_mem); |
| 61 | } |
| 62 | if (p_keycode != Key::CTRL) { |
| 63 | ev->set_ctrl_pressed(control_mem); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | void AndroidInputHandler::process_key_event(int p_physical_keycode, int p_unicode, int p_key_label, bool p_pressed, bool p_echo) { |
| 68 | static char32_t prev_wc = 0; |
| 69 | char32_t unicode = p_unicode; |
| 70 | if ((p_unicode & 0xfffffc00) == 0xd800) { |
| 71 | if (prev_wc != 0) { |
| 72 | ERR_PRINT("invalid utf16 surrogate input"); |
| 73 | } |
| 74 | prev_wc = unicode; |
| 75 | return; // Skip surrogate. |
| 76 | } else if ((unicode & 0xfffffc00) == 0xdc00) { |
| 77 | if (prev_wc == 0) { |
| 78 | ERR_PRINT("invalid utf16 surrogate input"); |
| 79 | return; // Skip invalid surrogate. |
| 80 | } |
| 81 | unicode = (prev_wc << 10UL) + unicode - ((0xd800 << 10UL) + 0xdc00 - 0x10000); |
| 82 | prev_wc = 0; |
| 83 | } else { |
| 84 | prev_wc = 0; |
| 85 | } |
| 86 | |
| 87 | Ref<InputEventKey> ev; |
| 88 | ev.instantiate(); |
| 89 | |
| 90 | Key physical_keycode = godot_code_from_android_code(p_physical_keycode); |
| 91 | Key keycode; |
| 92 | if (unicode == '\b') { // 0x08 |
| 93 | keycode = Key::BACKSPACE; |
| 94 | } else if (unicode == '\t') { // 0x09 |
| 95 | keycode = Key::TAB; |
| 96 | } else if (unicode == '\n') { // 0x0A |
| 97 | keycode = Key::ENTER; |
| 98 | } else if (unicode == 0x1B) { |
| 99 | keycode = Key::ESCAPE; |
| 100 | } else if (unicode == 0x1F) { |
| 101 | keycode = Key::KEY_DELETE; |
| 102 | } else { |
| 103 | keycode = fix_keycode(unicode, physical_keycode); |
| 104 | } |
| 105 | |
| 106 | switch (physical_keycode) { |
| 107 | case Key::SHIFT: { |
| 108 | shift_mem = p_pressed; |
| 109 | } break; |
| 110 | case Key::ALT: { |
| 111 | alt_mem = p_pressed; |
| 112 | } break; |
| 113 | case Key::CTRL: { |
| 114 | control_mem = p_pressed; |
| 115 | } break; |
| 116 | case Key::META: { |
| 117 | meta_mem = p_pressed; |
| 118 | } break; |
| 119 | default: |
| 120 | break; |
| 121 | } |
| 122 | |
| 123 | ev->set_keycode(keycode); |
| 124 | ev->set_physical_keycode(physical_keycode); |
| 125 | ev->set_key_label(fix_key_label(p_key_label, keycode)); |
| 126 | ev->set_unicode(fix_unicode(unicode)); |
| 127 | ev->set_location(godot_location_from_android_code(p_physical_keycode)); |
| 128 | ev->set_pressed(p_pressed); |
| 129 | ev->set_echo(p_echo); |
| 130 | |
| 131 | _set_key_modifier_state(ev, keycode); |
| 132 | |
| 133 | if (p_physical_keycode == AKEYCODE_BACK && p_pressed) { |
| 134 | if (DisplayServerAndroid *dsa = Object::cast_to<DisplayServerAndroid>(DisplayServer::get_singleton())) { |
| 135 | dsa->send_window_event(DisplayServer::WINDOW_EVENT_GO_BACK_REQUEST, true); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | Input::get_singleton()->parse_input_event(ev); |
| 140 | } |
| 141 | |
| 142 | void AndroidInputHandler::_cancel_all_touch() { |
| 143 | _parse_all_touch(false, true); |
| 144 | touch.clear(); |
| 145 | } |
| 146 | |
| 147 | void AndroidInputHandler::_parse_all_touch(bool p_pressed, bool p_canceled, bool p_double_tap) { |
| 148 | if (touch.size()) { |
| 149 | //end all if exist |
| 150 | for (int i = 0; i < touch.size(); i++) { |
| 151 | Ref<InputEventScreenTouch> ev; |
| 152 | ev.instantiate(); |
| 153 | ev->set_index(touch[i].id); |
| 154 | ev->set_pressed(p_pressed); |
| 155 | ev->set_canceled(p_canceled); |
| 156 | ev->set_position(touch[i].pos); |
| 157 | ev->set_double_tap(p_double_tap); |
| 158 | Input::get_singleton()->parse_input_event(ev); |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | void AndroidInputHandler::_release_all_touch() { |
| 164 | _parse_all_touch(false, false); |
| 165 | touch.clear(); |
| 166 | } |
| 167 | |
| 168 | void AndroidInputHandler::process_touch_event(int p_event, int p_pointer, const Vector<TouchPos> &p_points, bool p_double_tap) { |
| 169 | switch (p_event) { |
| 170 | case AMOTION_EVENT_ACTION_DOWN: { //gesture begin |
| 171 | // Release any remaining touches or mouse event |
| 172 | _release_mouse_event_info(); |
| 173 | _release_all_touch(); |
| 174 | |
| 175 | touch.resize(p_points.size()); |
| 176 | for (int i = 0; i < p_points.size(); i++) { |
| 177 | touch.write[i].id = p_points[i].id; |
| 178 | touch.write[i].pos = p_points[i].pos; |
| 179 | touch.write[i].pressure = p_points[i].pressure; |
| 180 | touch.write[i].tilt = p_points[i].tilt; |
| 181 | } |
| 182 | |
| 183 | //send touch |
| 184 | _parse_all_touch(true, false, p_double_tap); |
| 185 | |
| 186 | } break; |
| 187 | case AMOTION_EVENT_ACTION_MOVE: { //motion |
| 188 | if (touch.size() != p_points.size()) { |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | for (int i = 0; i < touch.size(); i++) { |
| 193 | int idx = -1; |
| 194 | for (int j = 0; j < p_points.size(); j++) { |
| 195 | if (touch[i].id == p_points[j].id) { |
| 196 | idx = j; |
| 197 | break; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | ERR_CONTINUE(idx == -1); |
| 202 | |
| 203 | if (touch[i].pos == p_points[idx].pos) { |
| 204 | continue; // Don't move unnecessarily. |
| 205 | } |
| 206 | |
| 207 | Ref<InputEventScreenDrag> ev; |
| 208 | ev.instantiate(); |
| 209 | ev->set_index(touch[i].id); |
| 210 | ev->set_position(p_points[idx].pos); |
| 211 | ev->set_relative(p_points[idx].pos - touch[i].pos); |
| 212 | ev->set_relative_screen_position(ev->get_relative()); |
| 213 | ev->set_pressure(p_points[idx].pressure); |
| 214 | ev->set_tilt(p_points[idx].tilt); |
| 215 | Input::get_singleton()->parse_input_event(ev); |
| 216 | touch.write[i].pos = p_points[idx].pos; |
| 217 | } |
| 218 | |
| 219 | } break; |
| 220 | case AMOTION_EVENT_ACTION_CANCEL: { |
| 221 | _cancel_all_touch(); |
| 222 | } break; |
| 223 | case AMOTION_EVENT_ACTION_UP: { //release |
| 224 | _release_all_touch(); |
| 225 | } break; |
| 226 | case AMOTION_EVENT_ACTION_POINTER_DOWN: { // add touch |
| 227 | for (int i = 0; i < p_points.size(); i++) { |
| 228 | if (p_points[i].id == p_pointer) { |
| 229 | TouchPos tp = p_points[i]; |
| 230 | touch.push_back(tp); |
| 231 | |
| 232 | Ref<InputEventScreenTouch> ev; |
| 233 | ev.instantiate(); |
| 234 | |
| 235 | ev->set_index(tp.id); |
| 236 | ev->set_pressed(true); |
| 237 | ev->set_position(tp.pos); |
| 238 | Input::get_singleton()->parse_input_event(ev); |
| 239 | |
| 240 | break; |
| 241 | } |
| 242 | } |
| 243 | } break; |
| 244 | case AMOTION_EVENT_ACTION_POINTER_UP: { // remove touch |
| 245 | for (int i = 0; i < touch.size(); i++) { |
| 246 | if (touch[i].id == p_pointer) { |
| 247 | Ref<InputEventScreenTouch> ev; |
| 248 | ev.instantiate(); |
| 249 | ev->set_index(touch[i].id); |
| 250 | ev->set_pressed(false); |
| 251 | ev->set_position(touch[i].pos); |
| 252 | Input::get_singleton()->parse_input_event(ev); |
| 253 | touch.remove_at(i); |
| 254 | |
| 255 | break; |
| 256 | } |
| 257 | } |
| 258 | } break; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | void AndroidInputHandler::_cancel_mouse_event_info(bool p_source_mouse_relative) { |
| 263 | buttons_state = BitField<MouseButtonMask>(); |
| 264 | _parse_mouse_event_info(BitField<MouseButtonMask>(), false, true, false, p_source_mouse_relative); |
| 265 | mouse_event_info.valid = false; |
| 266 | } |
| 267 | |
| 268 | void 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) { |
| 269 | if (!mouse_event_info.valid) { |
| 270 | return; |
| 271 | } |
| 272 | |
| 273 | Ref<InputEventMouseButton> ev; |
| 274 | ev.instantiate(); |
| 275 | _set_key_modifier_state(ev, Key::NONE); |
| 276 | if (p_source_mouse_relative) { |
| 277 | ev->set_position(hover_prev_pos); |
| 278 | ev->set_global_position(hover_prev_pos); |
| 279 | } else { |
| 280 | ev->set_position(mouse_event_info.pos); |
| 281 | ev->set_global_position(mouse_event_info.pos); |
| 282 | hover_prev_pos = mouse_event_info.pos; |
| 283 | } |
| 284 | ev->set_pressed(p_pressed); |
| 285 | ev->set_canceled(p_canceled); |
| 286 | BitField<MouseButtonMask> changed_button_mask = buttons_state.get_different(event_buttons_mask); |
| 287 | |
| 288 | buttons_state = event_buttons_mask; |
| 289 | |
| 290 | ev->set_button_index(_button_index_from_mask(changed_button_mask)); |
| 291 | ev->set_button_mask(event_buttons_mask); |
| 292 | ev->set_double_click(p_double_click); |
| 293 | Input::get_singleton()->parse_input_event(ev); |
| 294 | } |
| 295 | |
| 296 | void AndroidInputHandler::_release_mouse_event_info(bool p_source_mouse_relative) { |
| 297 | _parse_mouse_event_info(BitField<MouseButtonMask>(), false, false, false, p_source_mouse_relative); |
| 298 | mouse_event_info.valid = false; |
| 299 | } |
| 300 | |
| 301 | void 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) { |
| 302 | BitField<MouseButtonMask> event_buttons_mask = _android_button_mask_to_godot_button_mask(p_event_android_buttons_mask); |
| 303 | switch (p_event_action) { |
| 304 | case AMOTION_EVENT_ACTION_HOVER_MOVE: // hover move |
| 305 | case AMOTION_EVENT_ACTION_HOVER_ENTER: // hover enter |
| 306 | case AMOTION_EVENT_ACTION_HOVER_EXIT: { // hover exit |
| 307 | // https://developer.android.com/reference/android/view/MotionEvent.html#ACTION_HOVER_ENTER |
| 308 | Ref<InputEventMouseMotion> ev; |
| 309 | ev.instantiate(); |
| 310 | _set_key_modifier_state(ev, Key::NONE); |
| 311 | ev->set_position(p_event_pos); |
| 312 | ev->set_global_position(p_event_pos); |
| 313 | ev->set_relative(p_event_pos - hover_prev_pos); |
| 314 | ev->set_relative_screen_position(ev->get_relative()); |
| 315 | Input::get_singleton()->parse_input_event(ev); |
| 316 | hover_prev_pos = p_event_pos; |
| 317 | } break; |
| 318 | |
| 319 | case AMOTION_EVENT_ACTION_DOWN: |
| 320 | case AMOTION_EVENT_ACTION_BUTTON_PRESS: { |
| 321 | // Release any remaining touches or mouse event |
| 322 | _release_mouse_event_info(); |
| 323 | _release_all_touch(); |
| 324 | |
| 325 | mouse_event_info.valid = true; |
| 326 | mouse_event_info.pos = p_event_pos; |
| 327 | _parse_mouse_event_info(event_buttons_mask, true, false, p_double_click, p_source_mouse_relative); |
| 328 | } break; |
| 329 | |
| 330 | case AMOTION_EVENT_ACTION_CANCEL: { |
| 331 | _cancel_mouse_event_info(p_source_mouse_relative); |
| 332 | } break; |
| 333 | |
| 334 | case AMOTION_EVENT_ACTION_UP: |
| 335 | case AMOTION_EVENT_ACTION_BUTTON_RELEASE: { |
| 336 | _release_mouse_event_info(p_source_mouse_relative); |
| 337 | } break; |
| 338 | |
| 339 | case AMOTION_EVENT_ACTION_MOVE: { |
| 340 | if (!p_source_mouse_relative && !mouse_event_info.valid) { |
| 341 | return; |
| 342 | } |
| 343 | |
| 344 | Ref<InputEventMouseMotion> ev; |
| 345 | ev.instantiate(); |
| 346 | _set_key_modifier_state(ev, Key::NONE); |
| 347 | if (p_source_mouse_relative) { |
| 348 | ev->set_position(hover_prev_pos); |
| 349 | ev->set_global_position(hover_prev_pos); |
| 350 | ev->set_relative(p_event_pos); |
| 351 | ev->set_relative_screen_position(p_event_pos); |
| 352 | } else { |
| 353 | ev->set_position(p_event_pos); |
| 354 | ev->set_global_position(p_event_pos); |
| 355 | ev->set_relative(p_event_pos - hover_prev_pos); |
| 356 | ev->set_relative_screen_position(ev->get_relative()); |
| 357 | mouse_event_info.pos = p_event_pos; |
| 358 | hover_prev_pos = p_event_pos; |
| 359 | } |
| 360 | ev->set_button_mask(event_buttons_mask); |
| 361 | ev->set_pressure(p_pressure); |
| 362 | ev->set_tilt(p_tilt); |
| 363 | Input::get_singleton()->parse_input_event(ev); |
| 364 | } break; |
| 365 | |
| 366 | case AMOTION_EVENT_ACTION_SCROLL: { |
| 367 | Ref<InputEventMouseButton> ev; |
| 368 | ev.instantiate(); |
| 369 | _set_key_modifier_state(ev, Key::NONE); |
| 370 | if (p_source_mouse_relative) { |
| 371 | ev->set_position(hover_prev_pos); |
| 372 | ev->set_global_position(hover_prev_pos); |
| 373 | } else { |
| 374 | ev->set_position(p_event_pos); |
| 375 | ev->set_global_position(p_event_pos); |
| 376 | } |
| 377 | ev->set_pressed(true); |
| 378 | buttons_state = event_buttons_mask; |
| 379 | if (p_delta.y > 0) { |
| 380 | _wheel_button_click(event_buttons_mask, ev, MouseButton::WHEEL_UP, p_delta.y); |
| 381 | } else if (p_delta.y < 0) { |
| 382 | _wheel_button_click(event_buttons_mask, ev, MouseButton::WHEEL_DOWN, -p_delta.y); |
| 383 | } |
| 384 | |
| 385 | if (p_delta.x > 0) { |
| 386 | _wheel_button_click(event_buttons_mask, ev, MouseButton::WHEEL_RIGHT, p_delta.x); |
| 387 | } else if (p_delta.x < 0) { |
| 388 | _wheel_button_click(event_buttons_mask, ev, MouseButton::WHEEL_LEFT, -p_delta.x); |
| 389 | } |
| 390 | } break; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | void AndroidInputHandler::_wheel_button_click(BitField<MouseButtonMask> event_buttons_mask, const Ref<InputEventMouseButton> &ev, MouseButton wheel_button, float factor) { |
| 395 | Ref<InputEventMouseButton> evd = ev->duplicate(); |
| 396 | _set_key_modifier_state(evd, Key::NONE); |
| 397 | evd->set_button_index(wheel_button); |
| 398 | evd->set_button_mask(event_buttons_mask.get_different(mouse_button_to_mask(wheel_button))); |
| 399 | evd->set_factor(factor); |
| 400 | Input::get_singleton()->parse_input_event(evd); |
| 401 | Ref<InputEventMouseButton> evdd = evd->duplicate(); |
| 402 | evdd->set_pressed(false); |
| 403 | evdd->set_button_mask(event_buttons_mask); |
| 404 | Input::get_singleton()->parse_input_event(evdd); |
| 405 | } |
| 406 | |
| 407 | void AndroidInputHandler::process_magnify(Point2 p_pos, float p_factor) { |
| 408 | Ref<InputEventMagnifyGesture> magnify_event; |
| 409 | magnify_event.instantiate(); |
| 410 | _set_key_modifier_state(magnify_event, Key::NONE); |
| 411 | magnify_event->set_position(p_pos); |
| 412 | magnify_event->set_factor(p_factor); |
| 413 | Input::get_singleton()->parse_input_event(magnify_event); |
| 414 | } |
| 415 | |
| 416 | void AndroidInputHandler::process_pan(Point2 p_pos, Vector2 p_delta) { |
| 417 | Ref<InputEventPanGesture> pan_event; |
| 418 | pan_event.instantiate(); |
| 419 | _set_key_modifier_state(pan_event, Key::NONE); |
| 420 | pan_event->set_position(p_pos); |
| 421 | pan_event->set_delta(p_delta); |
| 422 | Input::get_singleton()->parse_input_event(pan_event); |
| 423 | } |
| 424 | |
| 425 | MouseButton AndroidInputHandler::_button_index_from_mask(BitField<MouseButtonMask> button_mask) { |
| 426 | switch (button_mask) { |
| 427 | case MouseButtonMask::LEFT: |
| 428 | return MouseButton::LEFT; |
| 429 | case MouseButtonMask::RIGHT: |
| 430 | return MouseButton::RIGHT; |
| 431 | case MouseButtonMask::MIDDLE: |
| 432 | return MouseButton::MIDDLE; |
| 433 | case MouseButtonMask::MB_XBUTTON1: |
| 434 | return MouseButton::MB_XBUTTON1; |
| 435 | case MouseButtonMask::MB_XBUTTON2: |
| 436 | return MouseButton::MB_XBUTTON2; |
| 437 | default: |
| 438 | return MouseButton::NONE; |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | BitField<MouseButtonMask> AndroidInputHandler::_android_button_mask_to_godot_button_mask(int android_button_mask) { |
| 443 | BitField<MouseButtonMask> godot_button_mask = MouseButtonMask::NONE; |
| 444 | if (android_button_mask & AMOTION_EVENT_BUTTON_PRIMARY) { |
| 445 | godot_button_mask.set_flag(MouseButtonMask::LEFT); |
| 446 | } |
| 447 | if (android_button_mask & AMOTION_EVENT_BUTTON_SECONDARY) { |
| 448 | godot_button_mask.set_flag(MouseButtonMask::RIGHT); |
| 449 | } |
| 450 | if (android_button_mask & AMOTION_EVENT_BUTTON_TERTIARY) { |
| 451 | godot_button_mask.set_flag(MouseButtonMask::MIDDLE); |
| 452 | } |
| 453 | if (android_button_mask & AMOTION_EVENT_BUTTON_BACK) { |
| 454 | godot_button_mask.set_flag(MouseButtonMask::MB_XBUTTON1); |
| 455 | } |
| 456 | if (android_button_mask & AMOTION_EVENT_BUTTON_FORWARD) { |
| 457 | godot_button_mask.set_flag(MouseButtonMask::MB_XBUTTON2); |
| 458 | } |
| 459 | |
| 460 | return godot_button_mask; |
| 461 | } |