android_input_handler.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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. #include "android_input_handler.h"
  31. #include "android_keys_utils.h"
  32. #include "display_server_android.h"
  33. void AndroidInputHandler::process_joy_event(AndroidInputHandler::JoypadEvent p_event) {
  34. switch (p_event.type) {
  35. case JOY_EVENT_BUTTON:
  36. Input::get_singleton()->joy_button(p_event.device, (JoyButton)p_event.index, p_event.pressed);
  37. break;
  38. case JOY_EVENT_AXIS:
  39. Input::get_singleton()->joy_axis(p_event.device, (JoyAxis)p_event.index, p_event.value);
  40. break;
  41. case JOY_EVENT_HAT:
  42. Input::get_singleton()->joy_hat(p_event.device, p_event.hat);
  43. break;
  44. default:
  45. return;
  46. }
  47. }
  48. void AndroidInputHandler::_set_key_modifier_state(Ref<InputEventWithModifiers> ev, Key p_keycode) {
  49. if (p_keycode != Key::SHIFT) {
  50. ev->set_shift_pressed(shift_mem);
  51. }
  52. if (p_keycode != Key::ALT) {
  53. ev->set_alt_pressed(alt_mem);
  54. }
  55. if (p_keycode != Key::META) {
  56. ev->set_meta_pressed(meta_mem);
  57. }
  58. if (p_keycode != Key::CTRL) {
  59. ev->set_ctrl_pressed(control_mem);
  60. }
  61. }
  62. void AndroidInputHandler::process_key_event(int p_physical_keycode, int p_unicode, int p_key_label, bool p_pressed, bool p_echo) {
  63. static char32_t prev_wc = 0;
  64. char32_t unicode = p_unicode;
  65. if ((p_unicode & 0xfffffc00) == 0xd800) {
  66. if (prev_wc != 0) {
  67. ERR_PRINT("invalid utf16 surrogate input");
  68. }
  69. prev_wc = unicode;
  70. return; // Skip surrogate.
  71. } else if ((unicode & 0xfffffc00) == 0xdc00) {
  72. if (prev_wc == 0) {
  73. ERR_PRINT("invalid utf16 surrogate input");
  74. return; // Skip invalid surrogate.
  75. }
  76. unicode = (prev_wc << 10UL) + unicode - ((0xd800 << 10UL) + 0xdc00 - 0x10000);
  77. prev_wc = 0;
  78. } else {
  79. prev_wc = 0;
  80. }
  81. Ref<InputEventKey> ev;
  82. ev.instantiate();
  83. Key physical_keycode = godot_code_from_android_code(p_physical_keycode);
  84. Key keycode;
  85. if (unicode == '\b') { // 0x08
  86. keycode = Key::BACKSPACE;
  87. } else if (unicode == '\t') { // 0x09
  88. keycode = Key::TAB;
  89. } else if (unicode == '\n') { // 0x0A
  90. keycode = Key::ENTER;
  91. } else if (unicode == 0x1B) {
  92. keycode = Key::ESCAPE;
  93. } else if (unicode == 0x1F) {
  94. keycode = Key::KEY_DELETE;
  95. } else {
  96. keycode = fix_keycode(unicode, physical_keycode);
  97. }
  98. switch (physical_keycode) {
  99. case Key::SHIFT: {
  100. shift_mem = p_pressed;
  101. } break;
  102. case Key::ALT: {
  103. alt_mem = p_pressed;
  104. } break;
  105. case Key::CTRL: {
  106. control_mem = p_pressed;
  107. } break;
  108. case Key::META: {
  109. meta_mem = p_pressed;
  110. } break;
  111. default:
  112. break;
  113. }
  114. ev->set_keycode(keycode);
  115. ev->set_physical_keycode(physical_keycode);
  116. ev->set_key_label(fix_key_label(p_key_label, keycode));
  117. ev->set_unicode(fix_unicode(unicode));
  118. ev->set_location(godot_location_from_android_code(p_physical_keycode));
  119. ev->set_pressed(p_pressed);
  120. ev->set_echo(p_echo);
  121. _set_key_modifier_state(ev, keycode);
  122. if (p_physical_keycode == AKEYCODE_BACK) {
  123. if (DisplayServerAndroid *dsa = Object::cast_to<DisplayServerAndroid>(DisplayServer::get_singleton())) {
  124. dsa->send_window_event(DisplayServer::WINDOW_EVENT_GO_BACK_REQUEST, true);
  125. }
  126. }
  127. Input::get_singleton()->parse_input_event(ev);
  128. }
  129. void AndroidInputHandler::_cancel_all_touch() {
  130. _parse_all_touch(false, true);
  131. touch.clear();
  132. }
  133. void AndroidInputHandler::_parse_all_touch(bool p_pressed, bool p_canceled, bool p_double_tap) {
  134. if (touch.size()) {
  135. //end all if exist
  136. for (int i = 0; i < touch.size(); i++) {
  137. Ref<InputEventScreenTouch> ev;
  138. ev.instantiate();
  139. ev->set_index(touch[i].id);
  140. ev->set_pressed(p_pressed);
  141. ev->set_canceled(p_canceled);
  142. ev->set_position(touch[i].pos);
  143. ev->set_double_tap(p_double_tap);
  144. Input::get_singleton()->parse_input_event(ev);
  145. }
  146. }
  147. }
  148. void AndroidInputHandler::_release_all_touch() {
  149. _parse_all_touch(false, false);
  150. touch.clear();
  151. }
  152. void AndroidInputHandler::process_touch_event(int p_event, int p_pointer, const Vector<TouchPos> &p_points, bool p_double_tap) {
  153. switch (p_event) {
  154. case AMOTION_EVENT_ACTION_DOWN: { //gesture begin
  155. // Release any remaining touches or mouse event
  156. _release_mouse_event_info();
  157. _release_all_touch();
  158. touch.resize(p_points.size());
  159. for (int i = 0; i < p_points.size(); i++) {
  160. touch.write[i].id = p_points[i].id;
  161. touch.write[i].pos = p_points[i].pos;
  162. }
  163. //send touch
  164. _parse_all_touch(true, false, p_double_tap);
  165. } break;
  166. case AMOTION_EVENT_ACTION_MOVE: { //motion
  167. if (touch.size() != p_points.size()) {
  168. return;
  169. }
  170. for (int i = 0; i < touch.size(); i++) {
  171. int idx = -1;
  172. for (int j = 0; j < p_points.size(); j++) {
  173. if (touch[i].id == p_points[j].id) {
  174. idx = j;
  175. break;
  176. }
  177. }
  178. ERR_CONTINUE(idx == -1);
  179. if (touch[i].pos == p_points[idx].pos) {
  180. continue; // Don't move unnecessarily.
  181. }
  182. Ref<InputEventScreenDrag> ev;
  183. ev.instantiate();
  184. ev->set_index(touch[i].id);
  185. ev->set_position(p_points[idx].pos);
  186. ev->set_relative(p_points[idx].pos - touch[i].pos);
  187. ev->set_relative_screen_position(ev->get_relative());
  188. Input::get_singleton()->parse_input_event(ev);
  189. touch.write[i].pos = p_points[idx].pos;
  190. }
  191. } break;
  192. case AMOTION_EVENT_ACTION_CANCEL: {
  193. _cancel_all_touch();
  194. } break;
  195. case AMOTION_EVENT_ACTION_UP: { //release
  196. _release_all_touch();
  197. } break;
  198. case AMOTION_EVENT_ACTION_POINTER_DOWN: { // add touch
  199. for (int i = 0; i < p_points.size(); i++) {
  200. if (p_points[i].id == p_pointer) {
  201. TouchPos tp = p_points[i];
  202. touch.push_back(tp);
  203. Ref<InputEventScreenTouch> ev;
  204. ev.instantiate();
  205. ev->set_index(tp.id);
  206. ev->set_pressed(true);
  207. ev->set_position(tp.pos);
  208. Input::get_singleton()->parse_input_event(ev);
  209. break;
  210. }
  211. }
  212. } break;
  213. case AMOTION_EVENT_ACTION_POINTER_UP: { // remove touch
  214. for (int i = 0; i < touch.size(); i++) {
  215. if (touch[i].id == p_pointer) {
  216. Ref<InputEventScreenTouch> ev;
  217. ev.instantiate();
  218. ev->set_index(touch[i].id);
  219. ev->set_pressed(false);
  220. ev->set_position(touch[i].pos);
  221. Input::get_singleton()->parse_input_event(ev);
  222. touch.remove_at(i);
  223. break;
  224. }
  225. }
  226. } break;
  227. }
  228. }
  229. void AndroidInputHandler::_cancel_mouse_event_info(bool p_source_mouse_relative) {
  230. buttons_state = BitField<MouseButtonMask>();
  231. _parse_mouse_event_info(BitField<MouseButtonMask>(), false, true, false, p_source_mouse_relative);
  232. mouse_event_info.valid = false;
  233. }
  234. 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) {
  235. if (!mouse_event_info.valid) {
  236. return;
  237. }
  238. Ref<InputEventMouseButton> ev;
  239. ev.instantiate();
  240. _set_key_modifier_state(ev, Key::NONE);
  241. if (p_source_mouse_relative) {
  242. ev->set_position(hover_prev_pos);
  243. ev->set_global_position(hover_prev_pos);
  244. } else {
  245. ev->set_position(mouse_event_info.pos);
  246. ev->set_global_position(mouse_event_info.pos);
  247. hover_prev_pos = mouse_event_info.pos;
  248. }
  249. ev->set_pressed(p_pressed);
  250. ev->set_canceled(p_canceled);
  251. BitField<MouseButtonMask> changed_button_mask = BitField<MouseButtonMask>(buttons_state.operator int64_t() ^ event_buttons_mask.operator int64_t());
  252. buttons_state = event_buttons_mask;
  253. ev->set_button_index(_button_index_from_mask(changed_button_mask));
  254. ev->set_button_mask(event_buttons_mask);
  255. ev->set_double_click(p_double_click);
  256. Input::get_singleton()->parse_input_event(ev);
  257. }
  258. void AndroidInputHandler::_release_mouse_event_info(bool p_source_mouse_relative) {
  259. _parse_mouse_event_info(BitField<MouseButtonMask>(), false, false, false, p_source_mouse_relative);
  260. mouse_event_info.valid = false;
  261. }
  262. 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) {
  263. BitField<MouseButtonMask> event_buttons_mask = _android_button_mask_to_godot_button_mask(p_event_android_buttons_mask);
  264. switch (p_event_action) {
  265. case AMOTION_EVENT_ACTION_HOVER_MOVE: // hover move
  266. case AMOTION_EVENT_ACTION_HOVER_ENTER: // hover enter
  267. case AMOTION_EVENT_ACTION_HOVER_EXIT: { // hover exit
  268. // https://developer.android.com/reference/android/view/MotionEvent.html#ACTION_HOVER_ENTER
  269. Ref<InputEventMouseMotion> ev;
  270. ev.instantiate();
  271. _set_key_modifier_state(ev, Key::NONE);
  272. ev->set_position(p_event_pos);
  273. ev->set_global_position(p_event_pos);
  274. ev->set_relative(p_event_pos - hover_prev_pos);
  275. ev->set_relative_screen_position(ev->get_relative());
  276. Input::get_singleton()->parse_input_event(ev);
  277. hover_prev_pos = p_event_pos;
  278. } break;
  279. case AMOTION_EVENT_ACTION_DOWN:
  280. case AMOTION_EVENT_ACTION_BUTTON_PRESS: {
  281. // Release any remaining touches or mouse event
  282. _release_mouse_event_info();
  283. _release_all_touch();
  284. mouse_event_info.valid = true;
  285. mouse_event_info.pos = p_event_pos;
  286. _parse_mouse_event_info(event_buttons_mask, true, false, p_double_click, p_source_mouse_relative);
  287. } break;
  288. case AMOTION_EVENT_ACTION_CANCEL: {
  289. _cancel_mouse_event_info(p_source_mouse_relative);
  290. } break;
  291. case AMOTION_EVENT_ACTION_UP:
  292. case AMOTION_EVENT_ACTION_BUTTON_RELEASE: {
  293. _release_mouse_event_info(p_source_mouse_relative);
  294. } break;
  295. case AMOTION_EVENT_ACTION_MOVE: {
  296. if (!mouse_event_info.valid) {
  297. return;
  298. }
  299. Ref<InputEventMouseMotion> ev;
  300. ev.instantiate();
  301. _set_key_modifier_state(ev, Key::NONE);
  302. if (p_source_mouse_relative) {
  303. ev->set_position(hover_prev_pos);
  304. ev->set_global_position(hover_prev_pos);
  305. ev->set_relative(p_event_pos);
  306. ev->set_relative_screen_position(p_event_pos);
  307. } else {
  308. ev->set_position(p_event_pos);
  309. ev->set_global_position(p_event_pos);
  310. ev->set_relative(p_event_pos - hover_prev_pos);
  311. ev->set_relative_screen_position(ev->get_relative());
  312. mouse_event_info.pos = p_event_pos;
  313. hover_prev_pos = p_event_pos;
  314. }
  315. ev->set_button_mask(event_buttons_mask);
  316. ev->set_pressure(p_pressure);
  317. ev->set_tilt(p_tilt);
  318. Input::get_singleton()->parse_input_event(ev);
  319. } break;
  320. case AMOTION_EVENT_ACTION_SCROLL: {
  321. Ref<InputEventMouseButton> ev;
  322. ev.instantiate();
  323. _set_key_modifier_state(ev, Key::NONE);
  324. if (p_source_mouse_relative) {
  325. ev->set_position(hover_prev_pos);
  326. ev->set_global_position(hover_prev_pos);
  327. } else {
  328. ev->set_position(p_event_pos);
  329. ev->set_global_position(p_event_pos);
  330. }
  331. ev->set_pressed(true);
  332. buttons_state = event_buttons_mask;
  333. if (p_delta.y > 0) {
  334. _wheel_button_click(event_buttons_mask, ev, MouseButton::WHEEL_UP, p_delta.y);
  335. } else if (p_delta.y < 0) {
  336. _wheel_button_click(event_buttons_mask, ev, MouseButton::WHEEL_DOWN, -p_delta.y);
  337. }
  338. if (p_delta.x > 0) {
  339. _wheel_button_click(event_buttons_mask, ev, MouseButton::WHEEL_RIGHT, p_delta.x);
  340. } else if (p_delta.x < 0) {
  341. _wheel_button_click(event_buttons_mask, ev, MouseButton::WHEEL_LEFT, -p_delta.x);
  342. }
  343. } break;
  344. }
  345. }
  346. void AndroidInputHandler::_wheel_button_click(BitField<MouseButtonMask> event_buttons_mask, const Ref<InputEventMouseButton> &ev, MouseButton wheel_button, float factor) {
  347. Ref<InputEventMouseButton> evd = ev->duplicate();
  348. _set_key_modifier_state(evd, Key::NONE);
  349. evd->set_button_index(wheel_button);
  350. evd->set_button_mask(BitField<MouseButtonMask>(event_buttons_mask.operator int64_t() ^ int64_t(mouse_button_to_mask(wheel_button))));
  351. evd->set_factor(factor);
  352. Input::get_singleton()->parse_input_event(evd);
  353. Ref<InputEventMouseButton> evdd = evd->duplicate();
  354. evdd->set_pressed(false);
  355. evdd->set_button_mask(event_buttons_mask);
  356. Input::get_singleton()->parse_input_event(evdd);
  357. }
  358. void AndroidInputHandler::process_magnify(Point2 p_pos, float p_factor) {
  359. Ref<InputEventMagnifyGesture> magnify_event;
  360. magnify_event.instantiate();
  361. _set_key_modifier_state(magnify_event, Key::NONE);
  362. magnify_event->set_position(p_pos);
  363. magnify_event->set_factor(p_factor);
  364. Input::get_singleton()->parse_input_event(magnify_event);
  365. }
  366. void AndroidInputHandler::process_pan(Point2 p_pos, Vector2 p_delta) {
  367. Ref<InputEventPanGesture> pan_event;
  368. pan_event.instantiate();
  369. _set_key_modifier_state(pan_event, Key::NONE);
  370. pan_event->set_position(p_pos);
  371. pan_event->set_delta(p_delta);
  372. Input::get_singleton()->parse_input_event(pan_event);
  373. }
  374. MouseButton AndroidInputHandler::_button_index_from_mask(BitField<MouseButtonMask> button_mask) {
  375. switch (MouseButtonMask(button_mask.operator int64_t())) {
  376. case MouseButtonMask::LEFT:
  377. return MouseButton::LEFT;
  378. case MouseButtonMask::RIGHT:
  379. return MouseButton::RIGHT;
  380. case MouseButtonMask::MIDDLE:
  381. return MouseButton::MIDDLE;
  382. case MouseButtonMask::MB_XBUTTON1:
  383. return MouseButton::MB_XBUTTON1;
  384. case MouseButtonMask::MB_XBUTTON2:
  385. return MouseButton::MB_XBUTTON2;
  386. default:
  387. return MouseButton::NONE;
  388. }
  389. }
  390. BitField<MouseButtonMask> AndroidInputHandler::_android_button_mask_to_godot_button_mask(int android_button_mask) {
  391. BitField<MouseButtonMask> godot_button_mask;
  392. if (android_button_mask & AMOTION_EVENT_BUTTON_PRIMARY) {
  393. godot_button_mask.set_flag(MouseButtonMask::LEFT);
  394. }
  395. if (android_button_mask & AMOTION_EVENT_BUTTON_SECONDARY) {
  396. godot_button_mask.set_flag(MouseButtonMask::RIGHT);
  397. }
  398. if (android_button_mask & AMOTION_EVENT_BUTTON_TERTIARY) {
  399. godot_button_mask.set_flag(MouseButtonMask::MIDDLE);
  400. }
  401. if (android_button_mask & AMOTION_EVENT_BUTTON_BACK) {
  402. godot_button_mask.set_flag(MouseButtonMask::MB_XBUTTON1);
  403. }
  404. if (android_button_mask & AMOTION_EVENT_BUTTON_FORWARD) {
  405. godot_button_mask.set_flag(MouseButtonMask::MB_XBUTTON2);
  406. }
  407. return godot_button_mask;
  408. }