android_input_handler.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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) {
  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 = physical_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_pressed(p_pressed);
  119. _set_key_modifier_state(ev, keycode);
  120. if (p_physical_keycode == AKEYCODE_BACK) {
  121. if (DisplayServerAndroid *dsa = Object::cast_to<DisplayServerAndroid>(DisplayServer::get_singleton())) {
  122. dsa->send_window_event(DisplayServer::WINDOW_EVENT_GO_BACK_REQUEST, true);
  123. }
  124. }
  125. Input::get_singleton()->parse_input_event(ev);
  126. }
  127. void AndroidInputHandler::_cancel_all_touch() {
  128. _parse_all_touch(false, false, true);
  129. touch.clear();
  130. }
  131. void AndroidInputHandler::_parse_all_touch(bool p_pressed, bool p_double_tap, bool reset_index) {
  132. if (touch.size()) {
  133. //end all if exist
  134. for (int i = 0; i < touch.size(); i++) {
  135. Ref<InputEventScreenTouch> ev;
  136. ev.instantiate();
  137. if (reset_index) {
  138. ev->set_index(-1);
  139. } else {
  140. ev->set_index(touch[i].id);
  141. }
  142. ev->set_pressed(p_pressed);
  143. ev->set_position(touch[i].pos);
  144. ev->set_double_tap(p_double_tap);
  145. Input::get_singleton()->parse_input_event(ev);
  146. }
  147. }
  148. }
  149. void AndroidInputHandler::_release_all_touch() {
  150. _parse_all_touch(false, false);
  151. touch.clear();
  152. }
  153. void AndroidInputHandler::process_touch_event(int p_event, int p_pointer, const Vector<TouchPos> &p_points, bool p_double_tap) {
  154. switch (p_event) {
  155. case AMOTION_EVENT_ACTION_DOWN: { //gesture begin
  156. // Release any remaining touches or mouse event
  157. _release_mouse_event_info();
  158. _release_all_touch();
  159. touch.resize(p_points.size());
  160. for (int i = 0; i < p_points.size(); i++) {
  161. touch.write[i].id = p_points[i].id;
  162. touch.write[i].pos = p_points[i].pos;
  163. }
  164. //send touch
  165. _parse_all_touch(true, p_double_tap);
  166. } break;
  167. case AMOTION_EVENT_ACTION_MOVE: { //motion
  168. if (touch.size() != p_points.size()) {
  169. return;
  170. }
  171. for (int i = 0; i < touch.size(); i++) {
  172. int idx = -1;
  173. for (int j = 0; j < p_points.size(); j++) {
  174. if (touch[i].id == p_points[j].id) {
  175. idx = j;
  176. break;
  177. }
  178. }
  179. ERR_CONTINUE(idx == -1);
  180. if (touch[i].pos == p_points[idx].pos) {
  181. continue; // Don't move unnecessarily.
  182. }
  183. Ref<InputEventScreenDrag> ev;
  184. ev.instantiate();
  185. ev->set_index(touch[i].id);
  186. ev->set_position(p_points[idx].pos);
  187. ev->set_relative(p_points[idx].pos - touch[i].pos);
  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, 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_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. BitField<MouseButtonMask> changed_button_mask = BitField<MouseButtonMask>(buttons_state.operator int64_t() ^ event_buttons_mask.operator int64_t());
  251. buttons_state = event_buttons_mask;
  252. ev->set_button_index(_button_index_from_mask(changed_button_mask));
  253. ev->set_button_mask(event_buttons_mask);
  254. ev->set_double_click(p_double_click);
  255. Input::get_singleton()->parse_input_event(ev);
  256. }
  257. void AndroidInputHandler::_release_mouse_event_info(bool p_source_mouse_relative) {
  258. _parse_mouse_event_info(BitField<MouseButtonMask>(), false, false, p_source_mouse_relative);
  259. mouse_event_info.valid = false;
  260. }
  261. 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) {
  262. BitField<MouseButtonMask> event_buttons_mask = _android_button_mask_to_godot_button_mask(p_event_android_buttons_mask);
  263. switch (p_event_action) {
  264. case AMOTION_EVENT_ACTION_HOVER_MOVE: // hover move
  265. case AMOTION_EVENT_ACTION_HOVER_ENTER: // hover enter
  266. case AMOTION_EVENT_ACTION_HOVER_EXIT: { // hover exit
  267. // https://developer.android.com/reference/android/view/MotionEvent.html#ACTION_HOVER_ENTER
  268. Ref<InputEventMouseMotion> ev;
  269. ev.instantiate();
  270. _set_key_modifier_state(ev, Key::NONE);
  271. ev->set_position(p_event_pos);
  272. ev->set_global_position(p_event_pos);
  273. ev->set_relative(p_event_pos - hover_prev_pos);
  274. Input::get_singleton()->parse_input_event(ev);
  275. hover_prev_pos = p_event_pos;
  276. } break;
  277. case AMOTION_EVENT_ACTION_DOWN:
  278. case AMOTION_EVENT_ACTION_BUTTON_PRESS: {
  279. // Release any remaining touches or mouse event
  280. _release_mouse_event_info();
  281. _release_all_touch();
  282. mouse_event_info.valid = true;
  283. mouse_event_info.pos = p_event_pos;
  284. _parse_mouse_event_info(event_buttons_mask, true, p_double_click, p_source_mouse_relative);
  285. } break;
  286. case AMOTION_EVENT_ACTION_CANCEL: {
  287. _cancel_mouse_event_info(p_source_mouse_relative);
  288. } break;
  289. case AMOTION_EVENT_ACTION_UP:
  290. case AMOTION_EVENT_ACTION_BUTTON_RELEASE: {
  291. _release_mouse_event_info(p_source_mouse_relative);
  292. } break;
  293. case AMOTION_EVENT_ACTION_MOVE: {
  294. if (!mouse_event_info.valid) {
  295. return;
  296. }
  297. Ref<InputEventMouseMotion> ev;
  298. ev.instantiate();
  299. _set_key_modifier_state(ev, Key::NONE);
  300. if (p_source_mouse_relative) {
  301. ev->set_position(hover_prev_pos);
  302. ev->set_global_position(hover_prev_pos);
  303. ev->set_relative(p_event_pos);
  304. } else {
  305. ev->set_position(p_event_pos);
  306. ev->set_global_position(p_event_pos);
  307. ev->set_relative(p_event_pos - hover_prev_pos);
  308. mouse_event_info.pos = p_event_pos;
  309. hover_prev_pos = p_event_pos;
  310. }
  311. ev->set_button_mask(event_buttons_mask);
  312. Input::get_singleton()->parse_input_event(ev);
  313. } break;
  314. case AMOTION_EVENT_ACTION_SCROLL: {
  315. Ref<InputEventMouseButton> ev;
  316. ev.instantiate();
  317. _set_key_modifier_state(ev, Key::NONE);
  318. if (p_source_mouse_relative) {
  319. ev->set_position(hover_prev_pos);
  320. ev->set_global_position(hover_prev_pos);
  321. } else {
  322. ev->set_position(p_event_pos);
  323. ev->set_global_position(p_event_pos);
  324. }
  325. ev->set_pressed(true);
  326. buttons_state = event_buttons_mask;
  327. if (p_delta.y > 0) {
  328. _wheel_button_click(event_buttons_mask, ev, MouseButton::WHEEL_UP, p_delta.y);
  329. } else if (p_delta.y < 0) {
  330. _wheel_button_click(event_buttons_mask, ev, MouseButton::WHEEL_DOWN, -p_delta.y);
  331. }
  332. if (p_delta.x > 0) {
  333. _wheel_button_click(event_buttons_mask, ev, MouseButton::WHEEL_RIGHT, p_delta.x);
  334. } else if (p_delta.x < 0) {
  335. _wheel_button_click(event_buttons_mask, ev, MouseButton::WHEEL_LEFT, -p_delta.x);
  336. }
  337. } break;
  338. }
  339. }
  340. void AndroidInputHandler::_wheel_button_click(BitField<MouseButtonMask> event_buttons_mask, const Ref<InputEventMouseButton> &ev, MouseButton wheel_button, float factor) {
  341. Ref<InputEventMouseButton> evd = ev->duplicate();
  342. _set_key_modifier_state(evd, Key::NONE);
  343. evd->set_button_index(wheel_button);
  344. evd->set_button_mask(BitField<MouseButtonMask>(event_buttons_mask.operator int64_t() ^ int64_t(mouse_button_to_mask(wheel_button))));
  345. evd->set_factor(factor);
  346. Input::get_singleton()->parse_input_event(evd);
  347. Ref<InputEventMouseButton> evdd = evd->duplicate();
  348. evdd->set_pressed(false);
  349. evdd->set_button_mask(event_buttons_mask);
  350. Input::get_singleton()->parse_input_event(evdd);
  351. }
  352. void AndroidInputHandler::process_magnify(Point2 p_pos, float p_factor) {
  353. Ref<InputEventMagnifyGesture> magnify_event;
  354. magnify_event.instantiate();
  355. _set_key_modifier_state(magnify_event, Key::NONE);
  356. magnify_event->set_position(p_pos);
  357. magnify_event->set_factor(p_factor);
  358. Input::get_singleton()->parse_input_event(magnify_event);
  359. }
  360. void AndroidInputHandler::process_pan(Point2 p_pos, Vector2 p_delta) {
  361. Ref<InputEventPanGesture> pan_event;
  362. pan_event.instantiate();
  363. _set_key_modifier_state(pan_event, Key::NONE);
  364. pan_event->set_position(p_pos);
  365. pan_event->set_delta(p_delta);
  366. Input::get_singleton()->parse_input_event(pan_event);
  367. }
  368. MouseButton AndroidInputHandler::_button_index_from_mask(BitField<MouseButtonMask> button_mask) {
  369. switch (MouseButtonMask(button_mask.operator int64_t())) {
  370. case MouseButtonMask::LEFT:
  371. return MouseButton::LEFT;
  372. case MouseButtonMask::RIGHT:
  373. return MouseButton::RIGHT;
  374. case MouseButtonMask::MIDDLE:
  375. return MouseButton::MIDDLE;
  376. case MouseButtonMask::MB_XBUTTON1:
  377. return MouseButton::MB_XBUTTON1;
  378. case MouseButtonMask::MB_XBUTTON2:
  379. return MouseButton::MB_XBUTTON2;
  380. default:
  381. return MouseButton::NONE;
  382. }
  383. }
  384. BitField<MouseButtonMask> AndroidInputHandler::_android_button_mask_to_godot_button_mask(int android_button_mask) {
  385. BitField<MouseButtonMask> godot_button_mask;
  386. if (android_button_mask & AMOTION_EVENT_BUTTON_PRIMARY) {
  387. godot_button_mask.set_flag(MouseButtonMask::LEFT);
  388. }
  389. if (android_button_mask & AMOTION_EVENT_BUTTON_SECONDARY) {
  390. godot_button_mask.set_flag(MouseButtonMask::RIGHT);
  391. }
  392. if (android_button_mask & AMOTION_EVENT_BUTTON_TERTIARY) {
  393. godot_button_mask.set_flag(MouseButtonMask::MIDDLE);
  394. }
  395. if (android_button_mask & AMOTION_EVENT_BUTTON_BACK) {
  396. godot_button_mask.set_flag(MouseButtonMask::MB_XBUTTON1);
  397. }
  398. if (android_button_mask & AMOTION_EVENT_BUTTON_FORWARD) {
  399. godot_button_mask.set_flag(MouseButtonMask::MB_XBUTTON2);
  400. }
  401. return godot_button_mask;
  402. }