android_input_handler.cpp 14 KB

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