os_android.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*************************************************************************/
  2. /* os_android.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. #ifndef OS_ANDROID_H
  31. #define OS_ANDROID_H
  32. #include "drivers/unix/os_unix.h"
  33. #include "main/input_default.h"
  34. #include "os/input.h"
  35. #include "os/main_loop.h"
  36. #include "servers/audio/audio_server_sw.h"
  37. #include "servers/physics/physics_server_sw.h"
  38. #include "servers/physics_2d/physics_2d_server_sw.h"
  39. #include "servers/physics_2d/physics_2d_server_wrap_mt.h"
  40. #include "servers/spatial_sound/spatial_sound_server_sw.h"
  41. #include "servers/spatial_sound_2d/spatial_sound_2d_server_sw.h"
  42. #include "servers/visual/rasterizer.h"
  43. //#ifdef USE_JAVA_FILE_ACCESS
  44. #ifdef ANDROID_NATIVE_ACTIVITY
  45. #include <android/log.h>
  46. #include <android/sensor.h>
  47. #include <android_native_app_glue.h>
  48. #else
  49. #endif
  50. #include "audio_driver_jandroid.h"
  51. #include "audio_driver_opensl.h"
  52. typedef void (*GFXInitFunc)(void *ud, bool gl2);
  53. typedef int (*OpenURIFunc)(const String &);
  54. typedef String (*GetDataDirFunc)();
  55. typedef String (*GetLocaleFunc)();
  56. typedef void (*SetClipboardFunc)(const String &);
  57. typedef String (*GetClipboardFunc)();
  58. typedef void (*EmitErrorSignal)(const String &, const String &, const String &, const String &, int);
  59. typedef String (*GetModelFunc)();
  60. typedef int (*GetScreenDPIFunc)();
  61. typedef String (*GetUniqueIDFunc)();
  62. typedef void (*ShowVirtualKeyboardFunc)(const String &);
  63. typedef void (*HideVirtualKeyboardFunc)();
  64. typedef void (*SetScreenOrientationFunc)(int);
  65. typedef String (*GetSystemDirFunc)(int);
  66. typedef void (*VideoPlayFunc)(const String &);
  67. typedef bool (*VideoIsPlayingFunc)();
  68. typedef void (*VideoPauseFunc)();
  69. typedef void (*VideoStopFunc)();
  70. typedef void (*SetKeepScreenOnFunc)(bool p_enabled);
  71. typedef void (*AlertFunc)(const String &, const String &);
  72. class OS_Android : public OS_Unix {
  73. public:
  74. struct TouchPos {
  75. int id;
  76. Point2 pos;
  77. };
  78. enum {
  79. JOY_EVENT_BUTTON = 0,
  80. JOY_EVENT_AXIS = 1,
  81. JOY_EVENT_HAT = 2
  82. };
  83. struct JoystickEvent {
  84. int device;
  85. int type;
  86. int index;
  87. bool pressed;
  88. float value;
  89. int hat;
  90. };
  91. private:
  92. Vector<TouchPos> touch;
  93. Point2 last_mouse;
  94. unsigned int last_id;
  95. GFXInitFunc gfx_init_func;
  96. void *gfx_init_ud;
  97. bool use_gl2;
  98. bool use_reload_hooks;
  99. bool use_apk_expansion;
  100. bool use_16bits_fbo;
  101. Rasterizer *rasterizer;
  102. VisualServer *visual_server;
  103. AudioServerSW *audio_server;
  104. SampleManagerMallocSW *sample_manager;
  105. SpatialSoundServerSW *spatial_sound_server;
  106. SpatialSound2DServerSW *spatial_sound_2d_server;
  107. PhysicsServer *physics_server;
  108. Physics2DServer *physics_2d_server;
  109. mutable String data_dir_cache;
  110. #if 0
  111. AudioDriverAndroid audio_driver_android;
  112. #else
  113. AudioDriverOpenSL audio_driver_android;
  114. #endif
  115. const char *gl_extensions;
  116. InputDefault *input;
  117. VideoMode default_videomode;
  118. MainLoop *main_loop;
  119. OpenURIFunc open_uri_func;
  120. GetDataDirFunc get_data_dir_func;
  121. GetLocaleFunc get_locale_func;
  122. SetClipboardFunc set_clipboard_func;
  123. GetClipboardFunc get_clipboard_func;
  124. EmitErrorSignal emit_error_signal;
  125. GetModelFunc get_model_func;
  126. GetScreenDPIFunc get_screen_dpi_func;
  127. ShowVirtualKeyboardFunc show_virtual_keyboard_func;
  128. HideVirtualKeyboardFunc hide_virtual_keyboard_func;
  129. SetScreenOrientationFunc set_screen_orientation_func;
  130. GetUniqueIDFunc get_unique_id_func;
  131. GetSystemDirFunc get_system_dir_func;
  132. VideoPlayFunc video_play_func;
  133. VideoIsPlayingFunc video_is_playing_func;
  134. VideoPauseFunc video_pause_func;
  135. VideoStopFunc video_stop_func;
  136. SetKeepScreenOnFunc set_keep_screen_on_func;
  137. AlertFunc alert_func;
  138. public:
  139. // functions used by main to initialize/deintialize the OS
  140. virtual int get_video_driver_count() const;
  141. virtual const char *get_video_driver_name(int p_driver) const;
  142. virtual VideoMode get_default_video_mode() const;
  143. virtual int get_audio_driver_count() const;
  144. virtual const char *get_audio_driver_name(int p_driver) const;
  145. virtual void initialize_core();
  146. virtual void initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver);
  147. virtual void set_main_loop(MainLoop *p_main_loop);
  148. virtual void delete_main_loop();
  149. virtual void finalize();
  150. typedef int64_t ProcessID;
  151. static OS *get_singleton();
  152. void print_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type);
  153. virtual void vprint(const char *p_format, va_list p_list, bool p_stderr = false);
  154. virtual void print(const char *p_format, ...);
  155. virtual void alert(const String &p_alert, const String &p_title = "ALERT!");
  156. virtual void set_mouse_show(bool p_show);
  157. virtual void set_mouse_grab(bool p_grab);
  158. virtual bool is_mouse_grab_enabled() const;
  159. virtual Point2 get_mouse_pos() const;
  160. virtual int get_mouse_button_state() const;
  161. virtual void set_window_title(const String &p_title);
  162. virtual void set_video_mode(const VideoMode &p_video_mode, int p_screen = 0);
  163. virtual VideoMode get_video_mode(int p_screen = 0) const;
  164. virtual void get_fullscreen_mode_list(List<VideoMode> *p_list, int p_screen = 0) const;
  165. virtual void set_keep_screen_on(bool p_enabled);
  166. virtual Size2 get_window_size() const;
  167. virtual String get_name();
  168. virtual MainLoop *get_main_loop() const;
  169. virtual bool can_draw() const;
  170. virtual void set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot);
  171. virtual void set_cursor_shape(CursorShape p_shape);
  172. void main_loop_begin();
  173. bool main_loop_iterate();
  174. void main_loop_request_quit();
  175. void main_loop_end();
  176. void main_loop_focusout();
  177. void main_loop_focusin();
  178. virtual bool has_touchscreen_ui_hint() const;
  179. virtual bool has_virtual_keyboard() const;
  180. virtual void show_virtual_keyboard(const String &p_existing_text, const Rect2 &p_screen_rect = Rect2());
  181. virtual void hide_virtual_keyboard();
  182. void set_opengl_extensions(const char *p_gl_extensions);
  183. void set_display_size(Size2 p_size);
  184. void reload_gfx();
  185. void set_context_is_16_bits(bool p_is_16);
  186. void set_need_reload_hooks(bool p_needs_them);
  187. virtual void set_screen_orientation(ScreenOrientation p_orientation);
  188. virtual Error shell_open(String p_uri);
  189. virtual String get_data_dir() const;
  190. virtual String get_resource_dir() const;
  191. virtual String get_locale() const;
  192. virtual void set_clipboard(const String &p_text);
  193. virtual String get_clipboard() const;
  194. virtual String get_model_name() const;
  195. virtual int get_screen_dpi(int p_screen = 0) const;
  196. virtual String get_unique_ID() const;
  197. virtual String get_system_dir(SystemDir p_dir) const;
  198. void process_accelerometer(const Vector3 &p_accelerometer);
  199. void process_gravitymeter(const Vector3 &p_gravitymeter);
  200. void process_magnetometer(const Vector3 &p_magnetometer);
  201. void process_gyroscope(const Vector3 &p_gyroscope);
  202. void process_touch(int p_what, int p_pointer, const Vector<TouchPos> &p_points);
  203. void process_joy_event(JoystickEvent p_event);
  204. void process_event(InputEvent p_event);
  205. void init_video_mode(int p_video_width, int p_video_height);
  206. virtual Error native_video_play(String p_path, float p_volume);
  207. virtual bool native_video_is_playing();
  208. virtual void native_video_pause();
  209. virtual void native_video_stop();
  210. virtual bool is_joy_known(int p_device);
  211. virtual String get_joy_guid(int p_device) const;
  212. void joy_connection_changed(int p_device, bool p_connected, String p_name);
  213. OS_Android(GFXInitFunc p_gfx_init_func, void *p_gfx_init_ud, OpenURIFunc p_open_uri_func, GetDataDirFunc p_get_data_dir_func, GetLocaleFunc p_get_locale_func, GetModelFunc p_get_model_func, GetScreenDPIFunc p_get_screen_dpi_func, ShowVirtualKeyboardFunc p_show_vk, HideVirtualKeyboardFunc p_hide_vk, SetScreenOrientationFunc p_screen_orient, GetUniqueIDFunc p_get_unique_id, GetSystemDirFunc p_get_sdir_func, VideoPlayFunc p_video_play_func, VideoIsPlayingFunc p_video_is_playing_func, VideoPauseFunc p_video_pause_func, VideoStopFunc p_video_stop_func, SetKeepScreenOnFunc p_set_keep_screen_on_func, AlertFunc p_alert_func, bool p_use_apk_expansion, SetClipboardFunc p_set_clipboard_func, GetClipboardFunc p_get_clipboard_func, EmitErrorSignal p_emit_error_signal);
  214. ~OS_Android();
  215. };
  216. #endif