display_server_android.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  1. /*************************************************************************/
  2. /* display_server_android.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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 "display_server_android.h"
  31. #include "android_keys_utils.h"
  32. #include "core/config/project_settings.h"
  33. #include "java_godot_io_wrapper.h"
  34. #include "java_godot_wrapper.h"
  35. #include "os_android.h"
  36. #if defined(VULKAN_ENABLED)
  37. #include "drivers/vulkan/rendering_device_vulkan.h"
  38. #include "platform/android/vulkan/vulkan_context_android.h"
  39. #include "servers/rendering/renderer_rd/renderer_compositor_rd.h"
  40. #endif
  41. DisplayServerAndroid *DisplayServerAndroid::get_singleton() {
  42. return (DisplayServerAndroid *)DisplayServer::get_singleton();
  43. }
  44. bool DisplayServerAndroid::has_feature(Feature p_feature) const {
  45. switch (p_feature) {
  46. //case FEATURE_CONSOLE_WINDOW:
  47. case FEATURE_CURSOR_SHAPE:
  48. //case FEATURE_CUSTOM_CURSOR_SHAPE:
  49. //case FEATURE_GLOBAL_MENU:
  50. //case FEATURE_HIDPI:
  51. //case FEATURE_ICON:
  52. //case FEATURE_IME:
  53. case FEATURE_MOUSE:
  54. //case FEATURE_MOUSE_WARP:
  55. //case FEATURE_NATIVE_DIALOG:
  56. //case FEATURE_NATIVE_ICON:
  57. //case FEATURE_WINDOW_TRANSPARENCY:
  58. case FEATURE_CLIPBOARD:
  59. case FEATURE_KEEP_SCREEN_ON:
  60. case FEATURE_ORIENTATION:
  61. case FEATURE_TOUCHSCREEN:
  62. case FEATURE_VIRTUAL_KEYBOARD:
  63. return true;
  64. default:
  65. return false;
  66. }
  67. }
  68. String DisplayServerAndroid::get_name() const {
  69. return "Android";
  70. }
  71. void DisplayServerAndroid::clipboard_set(const String &p_text) {
  72. GodotJavaWrapper *godot_java = OS_Android::get_singleton()->get_godot_java();
  73. ERR_FAIL_COND(!godot_java);
  74. if (godot_java->has_set_clipboard()) {
  75. godot_java->set_clipboard(p_text);
  76. } else {
  77. DisplayServer::clipboard_set(p_text);
  78. }
  79. }
  80. String DisplayServerAndroid::clipboard_get() const {
  81. GodotJavaWrapper *godot_java = OS_Android::get_singleton()->get_godot_java();
  82. ERR_FAIL_COND_V(!godot_java, String());
  83. if (godot_java->has_get_clipboard()) {
  84. return godot_java->get_clipboard();
  85. } else {
  86. return DisplayServer::clipboard_get();
  87. }
  88. }
  89. void DisplayServerAndroid::screen_set_keep_on(bool p_enable) {
  90. GodotJavaWrapper *godot_java = OS_Android::get_singleton()->get_godot_java();
  91. ERR_FAIL_COND(!godot_java);
  92. godot_java->set_keep_screen_on(p_enable);
  93. keep_screen_on = p_enable;
  94. }
  95. bool DisplayServerAndroid::screen_is_kept_on() const {
  96. return keep_screen_on;
  97. }
  98. void DisplayServerAndroid::screen_set_orientation(DisplayServer::ScreenOrientation p_orientation, int p_screen) {
  99. GodotIOJavaWrapper *godot_io_java = OS_Android::get_singleton()->get_godot_io_java();
  100. ERR_FAIL_COND(!godot_io_java);
  101. godot_io_java->set_screen_orientation(p_orientation);
  102. }
  103. DisplayServer::ScreenOrientation DisplayServerAndroid::screen_get_orientation(int p_screen) const {
  104. GodotIOJavaWrapper *godot_io_java = OS_Android::get_singleton()->get_godot_io_java();
  105. ERR_FAIL_COND_V(!godot_io_java, SCREEN_LANDSCAPE);
  106. return (ScreenOrientation)godot_io_java->get_screen_orientation();
  107. }
  108. int DisplayServerAndroid::get_screen_count() const {
  109. return 1;
  110. }
  111. Point2i DisplayServerAndroid::screen_get_position(int p_screen) const {
  112. return Point2i(0, 0);
  113. }
  114. Size2i DisplayServerAndroid::screen_get_size(int p_screen) const {
  115. return OS_Android::get_singleton()->get_display_size();
  116. }
  117. Rect2i DisplayServerAndroid::screen_get_usable_rect(int p_screen) const {
  118. GodotIOJavaWrapper *godot_io_java = OS_Android::get_singleton()->get_godot_io_java();
  119. ERR_FAIL_COND_V(!godot_io_java, Rect2i());
  120. int xywh[4];
  121. godot_io_java->screen_get_usable_rect(xywh);
  122. return Rect2i(xywh[0], xywh[1], xywh[2], xywh[3]);
  123. }
  124. int DisplayServerAndroid::screen_get_dpi(int p_screen) const {
  125. GodotIOJavaWrapper *godot_io_java = OS_Android::get_singleton()->get_godot_io_java();
  126. ERR_FAIL_COND_V(!godot_io_java, 0);
  127. return godot_io_java->get_screen_dpi();
  128. }
  129. bool DisplayServerAndroid::screen_is_touchscreen(int p_screen) const {
  130. return true;
  131. }
  132. void DisplayServerAndroid::virtual_keyboard_show(const String &p_existing_text, const Rect2 &p_screen_rect, bool p_multiline, int p_max_length, int p_cursor_start, int p_cursor_end) {
  133. GodotIOJavaWrapper *godot_io_java = OS_Android::get_singleton()->get_godot_io_java();
  134. ERR_FAIL_COND(!godot_io_java);
  135. if (godot_io_java->has_vk()) {
  136. godot_io_java->show_vk(p_existing_text, p_multiline, p_max_length, p_cursor_start, p_cursor_end);
  137. } else {
  138. ERR_PRINT("Virtual keyboard not available");
  139. }
  140. }
  141. void DisplayServerAndroid::virtual_keyboard_hide() {
  142. GodotIOJavaWrapper *godot_io_java = OS_Android::get_singleton()->get_godot_io_java();
  143. ERR_FAIL_COND(!godot_io_java);
  144. if (godot_io_java->has_vk()) {
  145. godot_io_java->hide_vk();
  146. } else {
  147. ERR_PRINT("Virtual keyboard not available");
  148. }
  149. }
  150. int DisplayServerAndroid::virtual_keyboard_get_height() const {
  151. GodotIOJavaWrapper *godot_io_java = OS_Android::get_singleton()->get_godot_io_java();
  152. ERR_FAIL_COND_V(!godot_io_java, 0);
  153. return godot_io_java->get_vk_height();
  154. }
  155. void DisplayServerAndroid::window_set_window_event_callback(const Callable &p_callable, DisplayServer::WindowID p_window) {
  156. window_event_callback = p_callable;
  157. }
  158. void DisplayServerAndroid::window_set_input_event_callback(const Callable &p_callable, DisplayServer::WindowID p_window) {
  159. input_event_callback = p_callable;
  160. }
  161. void DisplayServerAndroid::window_set_input_text_callback(const Callable &p_callable, DisplayServer::WindowID p_window) {
  162. input_text_callback = p_callable;
  163. }
  164. void DisplayServerAndroid::window_set_rect_changed_callback(const Callable &p_callable, DisplayServer::WindowID p_window) {
  165. rect_changed_callback = p_callable;
  166. }
  167. void DisplayServerAndroid::window_set_drop_files_callback(const Callable &p_callable, DisplayServer::WindowID p_window) {
  168. // Not supported on Android.
  169. }
  170. void DisplayServerAndroid::_window_callback(const Callable &p_callable, const Variant &p_arg) const {
  171. if (!p_callable.is_null()) {
  172. const Variant *argp = &p_arg;
  173. Variant ret;
  174. Callable::CallError ce;
  175. p_callable.call((const Variant **)&argp, 1, ret, ce);
  176. }
  177. }
  178. void DisplayServerAndroid::send_window_event(DisplayServer::WindowEvent p_event) const {
  179. _window_callback(window_event_callback, int(p_event));
  180. }
  181. void DisplayServerAndroid::send_input_event(const Ref<InputEvent> &p_event) const {
  182. _window_callback(input_event_callback, p_event);
  183. }
  184. void DisplayServerAndroid::send_input_text(const String &p_text) const {
  185. _window_callback(input_text_callback, p_text);
  186. }
  187. void DisplayServerAndroid::_dispatch_input_events(const Ref<InputEvent> &p_event) {
  188. DisplayServerAndroid::get_singleton()->send_input_event(p_event);
  189. }
  190. Vector<DisplayServer::WindowID> DisplayServerAndroid::get_window_list() const {
  191. Vector<WindowID> ret;
  192. ret.push_back(MAIN_WINDOW_ID);
  193. return ret;
  194. }
  195. DisplayServer::WindowID DisplayServerAndroid::get_window_at_screen_position(const Point2i &p_position) const {
  196. return MAIN_WINDOW_ID;
  197. }
  198. void DisplayServerAndroid::window_attach_instance_id(ObjectID p_instance, DisplayServer::WindowID p_window) {
  199. window_attached_instance_id = p_instance;
  200. }
  201. ObjectID DisplayServerAndroid::window_get_attached_instance_id(DisplayServer::WindowID p_window) const {
  202. return window_attached_instance_id;
  203. }
  204. void DisplayServerAndroid::window_set_title(const String &p_title, DisplayServer::WindowID p_window) {
  205. // Not supported on Android.
  206. }
  207. int DisplayServerAndroid::window_get_current_screen(DisplayServer::WindowID p_window) const {
  208. return SCREEN_OF_MAIN_WINDOW;
  209. }
  210. void DisplayServerAndroid::window_set_current_screen(int p_screen, DisplayServer::WindowID p_window) {
  211. // Not supported on Android.
  212. }
  213. Point2i DisplayServerAndroid::window_get_position(DisplayServer::WindowID p_window) const {
  214. return Point2i();
  215. }
  216. void DisplayServerAndroid::window_set_position(const Point2i &p_position, DisplayServer::WindowID p_window) {
  217. // Not supported on Android.
  218. }
  219. void DisplayServerAndroid::window_set_transient(DisplayServer::WindowID p_window, DisplayServer::WindowID p_parent) {
  220. // Not supported on Android.
  221. }
  222. void DisplayServerAndroid::window_set_max_size(const Size2i p_size, DisplayServer::WindowID p_window) {
  223. // Not supported on Android.
  224. }
  225. Size2i DisplayServerAndroid::window_get_max_size(DisplayServer::WindowID p_window) const {
  226. return Size2i();
  227. }
  228. void DisplayServerAndroid::window_set_min_size(const Size2i p_size, DisplayServer::WindowID p_window) {
  229. // Not supported on Android.
  230. }
  231. Size2i DisplayServerAndroid::window_get_min_size(DisplayServer::WindowID p_window) const {
  232. return Size2i();
  233. }
  234. void DisplayServerAndroid::window_set_size(const Size2i p_size, DisplayServer::WindowID p_window) {
  235. // Not supported on Android.
  236. }
  237. Size2i DisplayServerAndroid::window_get_size(DisplayServer::WindowID p_window) const {
  238. return OS_Android::get_singleton()->get_display_size();
  239. }
  240. Size2i DisplayServerAndroid::window_get_real_size(DisplayServer::WindowID p_window) const {
  241. return OS_Android::get_singleton()->get_display_size();
  242. }
  243. void DisplayServerAndroid::window_set_mode(DisplayServer::WindowMode p_mode, DisplayServer::WindowID p_window) {
  244. // Not supported on Android.
  245. }
  246. DisplayServer::WindowMode DisplayServerAndroid::window_get_mode(DisplayServer::WindowID p_window) const {
  247. return WINDOW_MODE_FULLSCREEN;
  248. }
  249. bool DisplayServerAndroid::window_is_maximize_allowed(DisplayServer::WindowID p_window) const {
  250. return false;
  251. }
  252. void DisplayServerAndroid::window_set_flag(DisplayServer::WindowFlags p_flag, bool p_enabled, DisplayServer::WindowID p_window) {
  253. // Not supported on Android.
  254. }
  255. bool DisplayServerAndroid::window_get_flag(DisplayServer::WindowFlags p_flag, DisplayServer::WindowID p_window) const {
  256. return false;
  257. }
  258. void DisplayServerAndroid::window_request_attention(DisplayServer::WindowID p_window) {
  259. // Not supported on Android.
  260. }
  261. void DisplayServerAndroid::window_move_to_foreground(DisplayServer::WindowID p_window) {
  262. // Not supported on Android.
  263. }
  264. bool DisplayServerAndroid::window_can_draw(DisplayServer::WindowID p_window) const {
  265. return true;
  266. }
  267. bool DisplayServerAndroid::can_any_window_draw() const {
  268. return true;
  269. }
  270. void DisplayServerAndroid::process_events() {
  271. Input::get_singleton()->flush_accumulated_events();
  272. }
  273. Vector<String> DisplayServerAndroid::get_rendering_drivers_func() {
  274. Vector<String> drivers;
  275. #ifdef OPENGL_ENABLED
  276. drivers.push_back("opengl");
  277. #endif
  278. #ifdef VULKAN_ENABLED
  279. drivers.push_back("vulkan");
  280. #endif
  281. return drivers;
  282. }
  283. DisplayServer *DisplayServerAndroid::create_func(const String &p_rendering_driver, DisplayServer::WindowMode p_mode, DisplayServer::VSyncMode p_vsync_mode, uint32_t p_flags, const Vector2i &p_resolution, Error &r_error) {
  284. DisplayServer *ds = memnew(DisplayServerAndroid(p_rendering_driver, p_mode, p_vsync_mode, p_flags, p_resolution, r_error));
  285. if (r_error != OK) {
  286. OS::get_singleton()->alert("Your video card driver does not support any of the supported Vulkan versions.", "Unable to initialize Video driver");
  287. }
  288. return ds;
  289. }
  290. void DisplayServerAndroid::register_android_driver() {
  291. register_create_function("android", create_func, get_rendering_drivers_func);
  292. }
  293. void DisplayServerAndroid::reset_window() {
  294. #if defined(VULKAN_ENABLED)
  295. if (rendering_driver == "vulkan") {
  296. ANativeWindow *native_window = OS_Android::get_singleton()->get_native_window();
  297. ERR_FAIL_COND(!native_window);
  298. ERR_FAIL_COND(!context_vulkan);
  299. VSyncMode last_vsync_mode = context_vulkan->get_vsync_mode(MAIN_WINDOW_ID);
  300. context_vulkan->window_destroy(MAIN_WINDOW_ID);
  301. Size2i display_size = OS_Android::get_singleton()->get_display_size();
  302. if (context_vulkan->window_create(native_window, last_vsync_mode, display_size.width, display_size.height) == -1) {
  303. memdelete(context_vulkan);
  304. context_vulkan = nullptr;
  305. ERR_FAIL_MSG("Failed to reset Vulkan window.");
  306. }
  307. }
  308. #endif
  309. }
  310. void DisplayServerAndroid::notify_surface_changed(int p_width, int p_height) {
  311. if (rect_changed_callback.is_null()) {
  312. return;
  313. }
  314. const Variant size = Rect2i(0, 0, p_width, p_height);
  315. const Variant *sizep = &size;
  316. Variant ret;
  317. Callable::CallError ce;
  318. rect_changed_callback.call(reinterpret_cast<const Variant **>(&sizep), 1, ret, ce);
  319. }
  320. DisplayServerAndroid::DisplayServerAndroid(const String &p_rendering_driver, DisplayServer::WindowMode p_mode, DisplayServer::VSyncMode p_vsync_mode, uint32_t p_flags, const Vector2i &p_resolution, Error &r_error) {
  321. rendering_driver = p_rendering_driver;
  322. // TODO: rendering_driver is broken, change when different drivers are supported again
  323. rendering_driver = "vulkan";
  324. keep_screen_on = GLOBAL_GET("display/window/energy_saving/keep_screen_on");
  325. #if defined(OPENGL_ENABLED)
  326. if (rendering_driver == "opengl") {
  327. bool gl_initialization_error = false;
  328. if (RasterizerGLES2::is_viable() == OK) {
  329. RasterizerGLES2::register_config();
  330. RasterizerGLES2::make_current();
  331. } else {
  332. gl_initialization_error = true;
  333. }
  334. if (gl_initialization_error) {
  335. OS::get_singleton()->alert("Your device does not support any of the supported OpenGL versions.\n"
  336. "Please try updating your Android version.",
  337. "Unable to initialize video driver");
  338. return;
  339. }
  340. }
  341. #endif
  342. #if defined(VULKAN_ENABLED)
  343. context_vulkan = nullptr;
  344. rendering_device_vulkan = nullptr;
  345. if (rendering_driver == "vulkan") {
  346. ANativeWindow *native_window = OS_Android::get_singleton()->get_native_window();
  347. ERR_FAIL_COND(!native_window);
  348. context_vulkan = memnew(VulkanContextAndroid);
  349. if (context_vulkan->initialize() != OK) {
  350. memdelete(context_vulkan);
  351. context_vulkan = nullptr;
  352. ERR_FAIL_MSG("Failed to initialize Vulkan context");
  353. }
  354. Size2i display_size = OS_Android::get_singleton()->get_display_size();
  355. if (context_vulkan->window_create(native_window, p_vsync_mode, display_size.width, display_size.height) == -1) {
  356. memdelete(context_vulkan);
  357. context_vulkan = nullptr;
  358. ERR_FAIL_MSG("Failed to create Vulkan window.");
  359. }
  360. rendering_device_vulkan = memnew(RenderingDeviceVulkan);
  361. rendering_device_vulkan->initialize(context_vulkan);
  362. RendererCompositorRD::make_current();
  363. }
  364. #endif
  365. Input::get_singleton()->set_event_dispatch_function(_dispatch_input_events);
  366. r_error = OK;
  367. }
  368. DisplayServerAndroid::~DisplayServerAndroid() {
  369. #if defined(VULKAN_ENABLED)
  370. if (rendering_driver == "vulkan") {
  371. if (rendering_device_vulkan) {
  372. rendering_device_vulkan->finalize();
  373. memdelete(rendering_device_vulkan);
  374. }
  375. if (context_vulkan) {
  376. memdelete(context_vulkan);
  377. }
  378. }
  379. #endif
  380. }
  381. void DisplayServerAndroid::process_joy_event(DisplayServerAndroid::JoypadEvent p_event) {
  382. switch (p_event.type) {
  383. case JOY_EVENT_BUTTON:
  384. Input::get_singleton()->joy_button(p_event.device, (JoyButton)p_event.index, p_event.pressed);
  385. break;
  386. case JOY_EVENT_AXIS:
  387. Input::JoyAxisValue value;
  388. value.min = -1;
  389. value.value = p_event.value;
  390. Input::get_singleton()->joy_axis(p_event.device, (JoyAxis)p_event.index, value);
  391. break;
  392. case JOY_EVENT_HAT:
  393. Input::get_singleton()->joy_hat(p_event.device, (HatMask)p_event.hat);
  394. break;
  395. default:
  396. return;
  397. }
  398. }
  399. void DisplayServerAndroid::_set_key_modifier_state(Ref<InputEventWithModifiers> ev) {
  400. ev->set_shift_pressed(shift_mem);
  401. ev->set_alt_pressed(alt_mem);
  402. ev->set_meta_pressed(meta_mem);
  403. ev->set_ctrl_pressed(control_mem);
  404. }
  405. void DisplayServerAndroid::process_key_event(int p_keycode, int p_scancode, int p_unicode_char, bool p_pressed) {
  406. static char32_t prev_wc = 0;
  407. char32_t unicode = p_unicode_char;
  408. if ((p_unicode_char & 0xfffffc00) == 0xd800) {
  409. if (prev_wc != 0) {
  410. ERR_PRINT("invalid utf16 surrogate input");
  411. }
  412. prev_wc = unicode;
  413. return; // Skip surrogate.
  414. } else if ((unicode & 0xfffffc00) == 0xdc00) {
  415. if (prev_wc == 0) {
  416. ERR_PRINT("invalid utf16 surrogate input");
  417. return; // Skip invalid surrogate.
  418. }
  419. unicode = (prev_wc << 10UL) + unicode - ((0xd800 << 10UL) + 0xdc00 - 0x10000);
  420. prev_wc = 0;
  421. } else {
  422. prev_wc = 0;
  423. }
  424. Ref<InputEventKey> ev;
  425. ev.instantiate();
  426. int val = unicode;
  427. int keycode = android_get_keysym(p_keycode);
  428. int phy_keycode = android_get_keysym(p_scancode);
  429. if (keycode == KEY_SHIFT) {
  430. shift_mem = p_pressed;
  431. }
  432. if (keycode == KEY_ALT) {
  433. alt_mem = p_pressed;
  434. }
  435. if (keycode == KEY_CTRL) {
  436. control_mem = p_pressed;
  437. }
  438. if (keycode == KEY_META) {
  439. meta_mem = p_pressed;
  440. }
  441. ev->set_keycode((Key)keycode);
  442. ev->set_physical_keycode((Key)phy_keycode);
  443. ev->set_unicode(val);
  444. ev->set_pressed(p_pressed);
  445. _set_key_modifier_state(ev);
  446. if (val == '\n') {
  447. ev->set_keycode(KEY_ENTER);
  448. } else if (val == 61448) {
  449. ev->set_keycode(KEY_BACKSPACE);
  450. ev->set_unicode(KEY_BACKSPACE);
  451. } else if (val == 61453) {
  452. ev->set_keycode(KEY_ENTER);
  453. ev->set_unicode(KEY_ENTER);
  454. } else if (p_keycode == 4) {
  455. OS_Android::get_singleton()->main_loop_request_go_back();
  456. }
  457. Input::get_singleton()->accumulate_input_event(ev);
  458. }
  459. void DisplayServerAndroid::process_touch(int p_event, int p_pointer, const Vector<DisplayServerAndroid::TouchPos> &p_points) {
  460. switch (p_event) {
  461. case AMOTION_EVENT_ACTION_DOWN: { //gesture begin
  462. if (touch.size()) {
  463. //end all if exist
  464. for (int i = 0; i < touch.size(); i++) {
  465. Ref<InputEventScreenTouch> ev;
  466. ev.instantiate();
  467. ev->set_index(touch[i].id);
  468. ev->set_pressed(false);
  469. ev->set_position(touch[i].pos);
  470. Input::get_singleton()->accumulate_input_event(ev);
  471. }
  472. }
  473. touch.resize(p_points.size());
  474. for (int i = 0; i < p_points.size(); i++) {
  475. touch.write[i].id = p_points[i].id;
  476. touch.write[i].pos = p_points[i].pos;
  477. }
  478. //send touch
  479. for (int i = 0; i < touch.size(); i++) {
  480. Ref<InputEventScreenTouch> ev;
  481. ev.instantiate();
  482. ev->set_index(touch[i].id);
  483. ev->set_pressed(true);
  484. ev->set_position(touch[i].pos);
  485. Input::get_singleton()->accumulate_input_event(ev);
  486. }
  487. } break;
  488. case AMOTION_EVENT_ACTION_MOVE: { //motion
  489. ERR_FAIL_COND(touch.size() != p_points.size());
  490. for (int i = 0; i < touch.size(); i++) {
  491. int idx = -1;
  492. for (int j = 0; j < p_points.size(); j++) {
  493. if (touch[i].id == p_points[j].id) {
  494. idx = j;
  495. break;
  496. }
  497. }
  498. ERR_CONTINUE(idx == -1);
  499. if (touch[i].pos == p_points[idx].pos)
  500. continue; //no move unncesearily
  501. Ref<InputEventScreenDrag> ev;
  502. ev.instantiate();
  503. ev->set_index(touch[i].id);
  504. ev->set_position(p_points[idx].pos);
  505. ev->set_relative(p_points[idx].pos - touch[i].pos);
  506. Input::get_singleton()->accumulate_input_event(ev);
  507. touch.write[i].pos = p_points[idx].pos;
  508. }
  509. } break;
  510. case AMOTION_EVENT_ACTION_CANCEL:
  511. case AMOTION_EVENT_ACTION_UP: { //release
  512. if (touch.size()) {
  513. //end all if exist
  514. for (int i = 0; i < touch.size(); i++) {
  515. Ref<InputEventScreenTouch> ev;
  516. ev.instantiate();
  517. ev->set_index(touch[i].id);
  518. ev->set_pressed(false);
  519. ev->set_position(touch[i].pos);
  520. Input::get_singleton()->accumulate_input_event(ev);
  521. }
  522. touch.clear();
  523. }
  524. } break;
  525. case AMOTION_EVENT_ACTION_POINTER_DOWN: { // add touch
  526. for (int i = 0; i < p_points.size(); i++) {
  527. if (p_points[i].id == p_pointer) {
  528. TouchPos tp = p_points[i];
  529. touch.push_back(tp);
  530. Ref<InputEventScreenTouch> ev;
  531. ev.instantiate();
  532. ev->set_index(tp.id);
  533. ev->set_pressed(true);
  534. ev->set_position(tp.pos);
  535. Input::get_singleton()->accumulate_input_event(ev);
  536. break;
  537. }
  538. }
  539. } break;
  540. case AMOTION_EVENT_ACTION_POINTER_UP: { // remove touch
  541. for (int i = 0; i < touch.size(); i++) {
  542. if (touch[i].id == p_pointer) {
  543. Ref<InputEventScreenTouch> ev;
  544. ev.instantiate();
  545. ev->set_index(touch[i].id);
  546. ev->set_pressed(false);
  547. ev->set_position(touch[i].pos);
  548. Input::get_singleton()->accumulate_input_event(ev);
  549. touch.remove(i);
  550. break;
  551. }
  552. }
  553. } break;
  554. }
  555. }
  556. void DisplayServerAndroid::process_hover(int p_type, Point2 p_pos) {
  557. // https://developer.android.com/reference/android/view/MotionEvent.html#ACTION_HOVER_ENTER
  558. switch (p_type) {
  559. case AMOTION_EVENT_ACTION_HOVER_MOVE: // hover move
  560. case AMOTION_EVENT_ACTION_HOVER_ENTER: // hover enter
  561. case AMOTION_EVENT_ACTION_HOVER_EXIT: { // hover exit
  562. Ref<InputEventMouseMotion> ev;
  563. ev.instantiate();
  564. _set_key_modifier_state(ev);
  565. ev->set_position(p_pos);
  566. ev->set_global_position(p_pos);
  567. ev->set_relative(p_pos - hover_prev_pos);
  568. Input::get_singleton()->accumulate_input_event(ev);
  569. hover_prev_pos = p_pos;
  570. } break;
  571. }
  572. }
  573. void DisplayServerAndroid::process_mouse_event(int input_device, int event_action, int event_android_buttons_mask, Point2 event_pos, float event_vertical_factor, float event_horizontal_factor) {
  574. MouseButton event_buttons_mask = _android_button_mask_to_godot_button_mask(event_android_buttons_mask);
  575. switch (event_action) {
  576. case AMOTION_EVENT_ACTION_BUTTON_PRESS:
  577. case AMOTION_EVENT_ACTION_BUTTON_RELEASE: {
  578. Ref<InputEventMouseButton> ev;
  579. ev.instantiate();
  580. _set_key_modifier_state(ev);
  581. if ((input_device & AINPUT_SOURCE_MOUSE) == AINPUT_SOURCE_MOUSE) {
  582. ev->set_position(event_pos);
  583. ev->set_global_position(event_pos);
  584. } else {
  585. ev->set_position(hover_prev_pos);
  586. ev->set_global_position(hover_prev_pos);
  587. }
  588. ev->set_pressed(event_action == AMOTION_EVENT_ACTION_BUTTON_PRESS);
  589. MouseButton changed_button_mask = MouseButton(buttons_state ^ event_buttons_mask);
  590. buttons_state = event_buttons_mask;
  591. ev->set_button_index(_button_index_from_mask(changed_button_mask));
  592. ev->set_button_mask(event_buttons_mask);
  593. Input::get_singleton()->accumulate_input_event(ev);
  594. } break;
  595. case AMOTION_EVENT_ACTION_MOVE: {
  596. Ref<InputEventMouseMotion> ev;
  597. ev.instantiate();
  598. _set_key_modifier_state(ev);
  599. if ((input_device & AINPUT_SOURCE_MOUSE) == AINPUT_SOURCE_MOUSE) {
  600. ev->set_position(event_pos);
  601. ev->set_global_position(event_pos);
  602. ev->set_relative(event_pos - hover_prev_pos);
  603. hover_prev_pos = event_pos;
  604. } else {
  605. ev->set_position(hover_prev_pos);
  606. ev->set_global_position(hover_prev_pos);
  607. ev->set_relative(event_pos);
  608. }
  609. ev->set_button_mask(event_buttons_mask);
  610. Input::get_singleton()->accumulate_input_event(ev);
  611. } break;
  612. case AMOTION_EVENT_ACTION_SCROLL: {
  613. Ref<InputEventMouseButton> ev;
  614. ev.instantiate();
  615. if ((input_device & AINPUT_SOURCE_MOUSE) == AINPUT_SOURCE_MOUSE) {
  616. ev->set_position(event_pos);
  617. ev->set_global_position(event_pos);
  618. } else {
  619. ev->set_position(hover_prev_pos);
  620. ev->set_global_position(hover_prev_pos);
  621. }
  622. ev->set_pressed(true);
  623. buttons_state = event_buttons_mask;
  624. if (event_vertical_factor > 0) {
  625. _wheel_button_click(event_buttons_mask, ev, MOUSE_BUTTON_WHEEL_UP, event_vertical_factor);
  626. } else if (event_vertical_factor < 0) {
  627. _wheel_button_click(event_buttons_mask, ev, MOUSE_BUTTON_WHEEL_DOWN, -event_vertical_factor);
  628. }
  629. if (event_horizontal_factor > 0) {
  630. _wheel_button_click(event_buttons_mask, ev, MOUSE_BUTTON_WHEEL_RIGHT, event_horizontal_factor);
  631. } else if (event_horizontal_factor < 0) {
  632. _wheel_button_click(event_buttons_mask, ev, MOUSE_BUTTON_WHEEL_LEFT, -event_horizontal_factor);
  633. }
  634. } break;
  635. }
  636. }
  637. void DisplayServerAndroid::_wheel_button_click(MouseButton event_buttons_mask, const Ref<InputEventMouseButton> &ev, MouseButton wheel_button, float factor) {
  638. Ref<InputEventMouseButton> evd = ev->duplicate();
  639. _set_key_modifier_state(evd);
  640. evd->set_button_index(wheel_button);
  641. evd->set_button_mask(MouseButton(event_buttons_mask ^ (1 << (wheel_button - 1))));
  642. evd->set_factor(factor);
  643. Input::get_singleton()->accumulate_input_event(evd);
  644. Ref<InputEventMouseButton> evdd = evd->duplicate();
  645. evdd->set_pressed(false);
  646. evdd->set_button_mask(event_buttons_mask);
  647. Input::get_singleton()->accumulate_input_event(evdd);
  648. }
  649. void DisplayServerAndroid::process_double_tap(int event_android_button_mask, Point2 p_pos) {
  650. MouseButton event_button_mask = _android_button_mask_to_godot_button_mask(event_android_button_mask);
  651. Ref<InputEventMouseButton> ev;
  652. ev.instantiate();
  653. _set_key_modifier_state(ev);
  654. ev->set_position(p_pos);
  655. ev->set_global_position(p_pos);
  656. ev->set_pressed(event_button_mask != 0);
  657. ev->set_button_index(_button_index_from_mask(event_button_mask));
  658. ev->set_button_mask(event_button_mask);
  659. ev->set_double_click(true);
  660. Input::get_singleton()->accumulate_input_event(ev);
  661. }
  662. MouseButton DisplayServerAndroid::_button_index_from_mask(MouseButton button_mask) {
  663. switch (button_mask) {
  664. case MOUSE_BUTTON_MASK_LEFT:
  665. return MOUSE_BUTTON_LEFT;
  666. case MOUSE_BUTTON_MASK_RIGHT:
  667. return MOUSE_BUTTON_RIGHT;
  668. case MOUSE_BUTTON_MASK_MIDDLE:
  669. return MOUSE_BUTTON_MIDDLE;
  670. case MOUSE_BUTTON_MASK_XBUTTON1:
  671. return MOUSE_BUTTON_XBUTTON1;
  672. case MOUSE_BUTTON_MASK_XBUTTON2:
  673. return MOUSE_BUTTON_XBUTTON2;
  674. default:
  675. return MOUSE_BUTTON_NONE;
  676. }
  677. }
  678. void DisplayServerAndroid::process_scroll(Point2 p_pos) {
  679. Ref<InputEventPanGesture> ev;
  680. ev.instantiate();
  681. _set_key_modifier_state(ev);
  682. ev->set_position(p_pos);
  683. ev->set_delta(p_pos - scroll_prev_pos);
  684. Input::get_singleton()->accumulate_input_event(ev);
  685. scroll_prev_pos = p_pos;
  686. }
  687. void DisplayServerAndroid::process_accelerometer(const Vector3 &p_accelerometer) {
  688. Input::get_singleton()->set_accelerometer(p_accelerometer);
  689. }
  690. void DisplayServerAndroid::process_gravity(const Vector3 &p_gravity) {
  691. Input::get_singleton()->set_gravity(p_gravity);
  692. }
  693. void DisplayServerAndroid::process_magnetometer(const Vector3 &p_magnetometer) {
  694. Input::get_singleton()->set_magnetometer(p_magnetometer);
  695. }
  696. void DisplayServerAndroid::process_gyroscope(const Vector3 &p_gyroscope) {
  697. Input::get_singleton()->set_gyroscope(p_gyroscope);
  698. }
  699. void DisplayServerAndroid::mouse_set_mode(MouseMode p_mode) {
  700. if (mouse_mode == p_mode) {
  701. return;
  702. }
  703. if (p_mode == MouseMode::MOUSE_MODE_HIDDEN) {
  704. OS_Android::get_singleton()->get_godot_java()->get_godot_view()->set_pointer_icon(CURSOR_TYPE_NULL);
  705. } else {
  706. cursor_set_shape(cursor_shape);
  707. }
  708. if (p_mode == MouseMode::MOUSE_MODE_CAPTURED) {
  709. OS_Android::get_singleton()->get_godot_java()->get_godot_view()->request_pointer_capture();
  710. } else {
  711. OS_Android::get_singleton()->get_godot_java()->get_godot_view()->release_pointer_capture();
  712. }
  713. mouse_mode = p_mode;
  714. }
  715. DisplayServer::MouseMode DisplayServerAndroid::mouse_get_mode() const {
  716. return mouse_mode;
  717. }
  718. Point2i DisplayServerAndroid::mouse_get_position() const {
  719. return hover_prev_pos;
  720. }
  721. MouseButton DisplayServerAndroid::mouse_get_button_state() const {
  722. return buttons_state;
  723. }
  724. MouseButton DisplayServerAndroid::_android_button_mask_to_godot_button_mask(int android_button_mask) {
  725. MouseButton godot_button_mask = MOUSE_BUTTON_NONE;
  726. if (android_button_mask & AMOTION_EVENT_BUTTON_PRIMARY) {
  727. godot_button_mask |= MOUSE_BUTTON_MASK_LEFT;
  728. }
  729. if (android_button_mask & AMOTION_EVENT_BUTTON_SECONDARY) {
  730. godot_button_mask |= MOUSE_BUTTON_MASK_RIGHT;
  731. }
  732. if (android_button_mask & AMOTION_EVENT_BUTTON_TERTIARY) {
  733. godot_button_mask |= MOUSE_BUTTON_MASK_MIDDLE;
  734. }
  735. if (android_button_mask & AMOTION_EVENT_BUTTON_BACK) {
  736. godot_button_mask |= MOUSE_BUTTON_MASK_XBUTTON1;
  737. }
  738. if (android_button_mask & AMOTION_EVENT_BUTTON_SECONDARY) {
  739. godot_button_mask |= MOUSE_BUTTON_MASK_XBUTTON2;
  740. }
  741. return godot_button_mask;
  742. }
  743. void DisplayServerAndroid::cursor_set_shape(DisplayServer::CursorShape p_shape) {
  744. if (cursor_shape == p_shape) {
  745. return;
  746. }
  747. cursor_shape = p_shape;
  748. if (mouse_mode == MouseMode::MOUSE_MODE_VISIBLE || mouse_mode == MouseMode::MOUSE_MODE_CONFINED) {
  749. OS_Android::get_singleton()->get_godot_java()->get_godot_view()->set_pointer_icon(android_cursors[cursor_shape]);
  750. }
  751. }
  752. DisplayServer::CursorShape DisplayServerAndroid::cursor_get_shape() const {
  753. return cursor_shape;
  754. }
  755. void DisplayServerAndroid::window_set_vsync_mode(DisplayServer::VSyncMode p_vsync_mode, WindowID p_window) {
  756. #if defined(VULKAN_ENABLED)
  757. context_vulkan->set_vsync_mode(p_window, p_vsync_mode);
  758. #endif
  759. }
  760. DisplayServer::VSyncMode DisplayServerAndroid::window_get_vsync_mode(WindowID p_window) const {
  761. #if defined(VULKAN_ENABLED)
  762. return context_vulkan->get_vsync_mode(p_window);
  763. #else
  764. return DisplayServer::VSYNC_ENABLED;
  765. #endif
  766. }