os_haiku.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*************************************************************************/
  2. /* os_haiku.cpp */
  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. #include "os_haiku.h"
  31. #include "drivers/gles3/rasterizer_gles3.h"
  32. #include "main/main.h"
  33. #include "servers/physics/physics_server_sw.h"
  34. #include "servers/visual/visual_server_raster.h"
  35. #include "servers/visual/visual_server_wrap_mt.h"
  36. #include <Screen.h>
  37. OS_Haiku::OS_Haiku() {
  38. #ifdef MEDIA_KIT_ENABLED
  39. AudioDriverManager::add_driver(&driver_media_kit);
  40. #endif
  41. };
  42. void OS_Haiku::run() {
  43. if (!main_loop) {
  44. return;
  45. }
  46. main_loop->init();
  47. context_gl->release_current();
  48. // TODO: clean up
  49. BMessenger *bms = new BMessenger(window);
  50. BMessage *msg = new BMessage();
  51. bms->SendMessage(LOCKGL_MSG, msg);
  52. window->StartMessageRunner();
  53. app->Run();
  54. window->StopMessageRunner();
  55. delete app;
  56. delete bms;
  57. delete msg;
  58. main_loop->finish();
  59. }
  60. String OS_Haiku::get_name() {
  61. return "Haiku";
  62. }
  63. int OS_Haiku::get_video_driver_count() const {
  64. return 1;
  65. }
  66. const char *OS_Haiku::get_video_driver_name(int p_driver) const {
  67. return "GLES3";
  68. }
  69. Error OS_Haiku::initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver) {
  70. main_loop = NULL;
  71. current_video_mode = p_desired;
  72. app = new HaikuApplication();
  73. BRect frame;
  74. frame.Set(50, 50, 50 + current_video_mode.width - 1, 50 + current_video_mode.height - 1);
  75. window = new HaikuDirectWindow(frame);
  76. window->SetVideoMode(&current_video_mode);
  77. if (current_video_mode.fullscreen) {
  78. window->SetFullScreen(true);
  79. }
  80. if (!current_video_mode.resizable) {
  81. uint32 flags = window->Flags();
  82. flags |= B_NOT_RESIZABLE;
  83. window->SetFlags(flags);
  84. }
  85. #if defined(OPENGL_ENABLED)
  86. context_gl = memnew(ContextGL_Haiku(window));
  87. context_gl->initialize();
  88. context_gl->make_current();
  89. context_gl->set_use_vsync(current_video_mode.use_vsync);
  90. /* Port to GLES 3 rasterizer */
  91. //rasterizer = memnew(RasterizerGLES2);
  92. #endif
  93. visual_server = memnew(VisualServerRaster(rasterizer));
  94. ERR_FAIL_COND_V(!visual_server, ERR_UNAVAILABLE);
  95. // TODO: enable multithreaded VS
  96. /*
  97. if (get_render_thread_mode() != RENDER_THREAD_UNSAFE) {
  98. visual_server = memnew(VisualServerWrapMT(visual_server, get_render_thread_mode() == RENDER_SEPARATE_THREAD));
  99. }
  100. */
  101. input = memnew(InputDefault);
  102. window->SetInput(input);
  103. window->Show();
  104. visual_server->init();
  105. AudioDriverManager::initialize(p_audio_driver);
  106. power_manager = memnew(PowerHaiku);
  107. return OK;
  108. }
  109. void OS_Haiku::finalize() {
  110. if (main_loop) {
  111. memdelete(main_loop);
  112. }
  113. main_loop = NULL;
  114. visual_server->finish();
  115. memdelete(visual_server);
  116. memdelete(rasterizer);
  117. memdelete(input);
  118. #if defined(OPENGL_ENABLED)
  119. memdelete(context_gl);
  120. #endif
  121. }
  122. void OS_Haiku::set_main_loop(MainLoop *p_main_loop) {
  123. main_loop = p_main_loop;
  124. input->set_main_loop(p_main_loop);
  125. window->SetMainLoop(p_main_loop);
  126. }
  127. MainLoop *OS_Haiku::get_main_loop() const {
  128. return main_loop;
  129. }
  130. void OS_Haiku::delete_main_loop() {
  131. if (main_loop) {
  132. memdelete(main_loop);
  133. }
  134. main_loop = NULL;
  135. window->SetMainLoop(NULL);
  136. }
  137. void OS_Haiku::release_rendering_thread() {
  138. context_gl->release_current();
  139. }
  140. void OS_Haiku::make_rendering_thread() {
  141. context_gl->make_current();
  142. }
  143. bool OS_Haiku::can_draw() const {
  144. // TODO: implement
  145. return true;
  146. }
  147. void OS_Haiku::swap_buffers() {
  148. context_gl->swap_buffers();
  149. }
  150. Point2 OS_Haiku::get_mouse_position() const {
  151. return window->GetLastMousePosition();
  152. }
  153. int OS_Haiku::get_mouse_button_state() const {
  154. return window->GetLastButtonMask();
  155. }
  156. void OS_Haiku::set_cursor_shape(CursorShape p_shape) {
  157. //ERR_PRINT("set_cursor_shape() NOT IMPLEMENTED");
  158. }
  159. void OS_Haiku::set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot) {
  160. // TODO
  161. }
  162. int OS_Haiku::get_screen_count() const {
  163. // TODO: implement get_screen_count()
  164. return 1;
  165. }
  166. int OS_Haiku::get_current_screen() const {
  167. // TODO: implement get_current_screen()
  168. return 0;
  169. }
  170. void OS_Haiku::set_current_screen(int p_screen) {
  171. // TODO: implement set_current_screen()
  172. }
  173. Point2 OS_Haiku::get_screen_position(int p_screen) const {
  174. // TODO: make this work with the p_screen parameter
  175. BScreen *screen = new BScreen(window);
  176. BRect frame = screen->Frame();
  177. delete screen;
  178. return Point2i(frame.left, frame.top);
  179. }
  180. Size2 OS_Haiku::get_screen_size(int p_screen) const {
  181. // TODO: make this work with the p_screen parameter
  182. BScreen *screen = new BScreen(window);
  183. BRect frame = screen->Frame();
  184. delete screen;
  185. return Size2i(frame.IntegerWidth() + 1, frame.IntegerHeight() + 1);
  186. }
  187. void OS_Haiku::set_window_title(const String &p_title) {
  188. window->SetTitle(p_title.utf8().get_data());
  189. }
  190. Size2 OS_Haiku::get_window_size() const {
  191. BSize size = window->Size();
  192. return Size2i(size.IntegerWidth() + 1, size.IntegerHeight() + 1);
  193. }
  194. void OS_Haiku::set_window_size(const Size2 p_size) {
  195. // TODO: why does it stop redrawing after this is called?
  196. window->ResizeTo(p_size.x, p_size.y);
  197. }
  198. Point2 OS_Haiku::get_window_position() const {
  199. BPoint point(0, 0);
  200. window->ConvertToScreen(&point);
  201. return Point2i(point.x, point.y);
  202. }
  203. void OS_Haiku::set_window_position(const Point2 &p_position) {
  204. window->MoveTo(p_position.x, p_position.y);
  205. }
  206. void OS_Haiku::set_window_fullscreen(bool p_enabled) {
  207. window->SetFullScreen(p_enabled);
  208. current_video_mode.fullscreen = p_enabled;
  209. visual_server->init();
  210. }
  211. bool OS_Haiku::is_window_fullscreen() const {
  212. return current_video_mode.fullscreen;
  213. }
  214. void OS_Haiku::set_window_resizable(bool p_enabled) {
  215. uint32 flags = window->Flags();
  216. if (p_enabled) {
  217. flags &= ~(B_NOT_RESIZABLE);
  218. } else {
  219. flags |= B_NOT_RESIZABLE;
  220. }
  221. window->SetFlags(flags);
  222. current_video_mode.resizable = p_enabled;
  223. }
  224. bool OS_Haiku::is_window_resizable() const {
  225. return current_video_mode.resizable;
  226. }
  227. void OS_Haiku::set_window_minimized(bool p_enabled) {
  228. window->Minimize(p_enabled);
  229. }
  230. bool OS_Haiku::is_window_minimized() const {
  231. return window->IsMinimized();
  232. }
  233. void OS_Haiku::set_window_maximized(bool p_enabled) {
  234. window->Minimize(!p_enabled);
  235. }
  236. bool OS_Haiku::is_window_maximized() const {
  237. return !window->IsMinimized();
  238. }
  239. void OS_Haiku::set_video_mode(const VideoMode &p_video_mode, int p_screen) {
  240. ERR_PRINT("set_video_mode() NOT IMPLEMENTED");
  241. }
  242. OS::VideoMode OS_Haiku::get_video_mode(int p_screen) const {
  243. return current_video_mode;
  244. }
  245. void OS_Haiku::get_fullscreen_mode_list(List<VideoMode> *p_list, int p_screen) const {
  246. ERR_PRINT("get_fullscreen_mode_list() NOT IMPLEMENTED");
  247. }
  248. String OS_Haiku::get_executable_path() const {
  249. return OS::get_executable_path();
  250. }
  251. bool OS_Haiku::_check_internal_feature_support(const String &p_feature) {
  252. return p_feature == "pc" || p_feature == "s3tc";
  253. }
  254. String OS_Haiku::get_config_path() const {
  255. if (has_environment("XDG_CONFIG_HOME")) {
  256. return get_environment("XDG_CONFIG_HOME");
  257. } else if (has_environment("HOME")) {
  258. return get_environment("HOME").plus_file(".config");
  259. } else {
  260. return ".";
  261. }
  262. }
  263. String OS_Haiku::get_data_path() const {
  264. if (has_environment("XDG_DATA_HOME")) {
  265. return get_environment("XDG_DATA_HOME");
  266. } else if (has_environment("HOME")) {
  267. return get_environment("HOME").plus_file(".local/share");
  268. } else {
  269. return get_config_path();
  270. }
  271. }
  272. String OS_Haiku::get_cache_path() const {
  273. if (has_environment("XDG_CACHE_HOME")) {
  274. return get_environment("XDG_CACHE_HOME");
  275. } else if (has_environment("HOME")) {
  276. return get_environment("HOME").plus_file(".cache");
  277. } else {
  278. return get_config_path();
  279. }
  280. }