os_haiku.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*************************************************************************/
  2. /* os_haiku.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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/gles2/rasterizer_gles2.h"
  32. #include "main/main.h"
  33. #include "servers/physics_3d/physics_server_3d_sw.h"
  34. #include "servers/rendering/rendering_server_raster.h"
  35. #include "servers/rendering/rendering_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() const {
  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 "GLES2";
  68. }
  69. int OS_Haiku::get_current_video_driver() const {
  70. return video_driver_index;
  71. }
  72. Error OS_Haiku::initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver) {
  73. main_loop = NULL;
  74. current_video_mode = p_desired;
  75. app = new HaikuApplication();
  76. BRect frame;
  77. frame.Set(50, 50, 50 + current_video_mode.width - 1, 50 + current_video_mode.height - 1);
  78. window = new HaikuDirectWindow(frame);
  79. window->SetVideoMode(&current_video_mode);
  80. if (current_video_mode.fullscreen) {
  81. window->SetFullScreen(true);
  82. }
  83. if (!current_video_mode.resizable) {
  84. uint32 flags = window->Flags();
  85. flags |= B_NOT_RESIZABLE;
  86. window->SetFlags(flags);
  87. }
  88. #if defined(OPENGL_ENABLED)
  89. context_gl = memnew(ContextGL_Haiku(window));
  90. context_gl->initialize();
  91. context_gl->make_current();
  92. context_gl->set_use_vsync(current_video_mode.use_vsync);
  93. // FIXME: That's not how the rasterizer setup should happen.
  94. RasterizerGLES2::register_config();
  95. RasterizerGLES2::make_current();
  96. #endif
  97. rendering_server = memnew(RenderingServerRaster);
  98. // FIXME: Reimplement threaded rendering
  99. if (get_render_thread_mode() != RENDER_THREAD_UNSAFE) {
  100. rendering_server = memnew(RenderingServerWrapMT(rendering_server, false));
  101. }
  102. ERR_FAIL_COND_V(!rendering_server, ERR_UNAVAILABLE);
  103. video_driver_index = p_video_driver;
  104. input = memnew(InputDefault);
  105. window->SetInput(input);
  106. window->Show();
  107. rendering_server->init();
  108. AudioDriverManager::initialize(p_audio_driver);
  109. return OK;
  110. }
  111. void OS_Haiku::finalize() {
  112. if (main_loop) {
  113. memdelete(main_loop);
  114. }
  115. main_loop = NULL;
  116. rendering_server->finish();
  117. memdelete(rendering_server);
  118. memdelete(input);
  119. #if defined(OPENGL_ENABLED)
  120. memdelete(context_gl);
  121. #endif
  122. }
  123. void OS_Haiku::set_main_loop(MainLoop *p_main_loop) {
  124. main_loop = p_main_loop;
  125. input->set_main_loop(p_main_loop);
  126. window->SetMainLoop(p_main_loop);
  127. }
  128. MainLoop *OS_Haiku::get_main_loop() const {
  129. return main_loop;
  130. }
  131. void OS_Haiku::delete_main_loop() {
  132. if (main_loop) {
  133. memdelete(main_loop);
  134. }
  135. main_loop = NULL;
  136. window->SetMainLoop(NULL);
  137. }
  138. void OS_Haiku::release_rendering_thread() {
  139. context_gl->release_current();
  140. }
  141. void OS_Haiku::make_rendering_thread() {
  142. context_gl->make_current();
  143. }
  144. bool OS_Haiku::can_draw() const {
  145. // TODO: implement
  146. return true;
  147. }
  148. void OS_Haiku::swap_buffers() {
  149. context_gl->swap_buffers();
  150. }
  151. Point2 OS_Haiku::get_mouse_position() const {
  152. return window->GetLastMousePosition();
  153. }
  154. int OS_Haiku::get_mouse_button_state() const {
  155. return window->GetLastButtonMask();
  156. }
  157. void OS_Haiku::set_cursor_shape(CursorShape p_shape) {
  158. //ERR_PRINT("set_cursor_shape() NOT IMPLEMENTED");
  159. }
  160. OS::CursorShape OS_Haiku::get_cursor_shape() const {
  161. // TODO: implement get_cursor_shape
  162. }
  163. void OS_Haiku::set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot) {
  164. // TODO
  165. }
  166. int OS_Haiku::get_screen_count() const {
  167. // TODO: implement get_screen_count()
  168. return 1;
  169. }
  170. int OS_Haiku::get_current_screen() const {
  171. // TODO: implement get_current_screen()
  172. return 0;
  173. }
  174. void OS_Haiku::set_current_screen(int p_screen) {
  175. // TODO: implement set_current_screen()
  176. }
  177. Point2 OS_Haiku::get_screen_position(int p_screen) const {
  178. // TODO: make this work with the p_screen parameter
  179. BScreen *screen = new BScreen(window);
  180. BRect frame = screen->Frame();
  181. delete screen;
  182. return Point2i(frame.left, frame.top);
  183. }
  184. Size2 OS_Haiku::get_screen_size(int p_screen) const {
  185. // TODO: make this work with the p_screen parameter
  186. BScreen *screen = new BScreen(window);
  187. BRect frame = screen->Frame();
  188. delete screen;
  189. return Size2i(frame.IntegerWidth() + 1, frame.IntegerHeight() + 1);
  190. }
  191. void OS_Haiku::set_window_title(const String &p_title) {
  192. window->SetTitle(p_title.utf8().get_data());
  193. }
  194. Size2 OS_Haiku::get_window_size() const {
  195. BSize size = window->Size();
  196. return Size2i(size.IntegerWidth() + 1, size.IntegerHeight() + 1);
  197. }
  198. void OS_Haiku::set_window_size(const Size2 p_size) {
  199. // TODO: why does it stop redrawing after this is called?
  200. window->ResizeTo(p_size.x, p_size.y);
  201. }
  202. Point2 OS_Haiku::get_window_position() const {
  203. BPoint point(0, 0);
  204. window->ConvertToScreen(&point);
  205. return Point2i(point.x, point.y);
  206. }
  207. void OS_Haiku::set_window_position(const Point2 &p_position) {
  208. window->MoveTo(p_position.x, p_position.y);
  209. }
  210. void OS_Haiku::set_window_fullscreen(bool p_enabled) {
  211. window->SetFullScreen(p_enabled);
  212. current_video_mode.fullscreen = p_enabled;
  213. rendering_server->init();
  214. }
  215. bool OS_Haiku::is_window_fullscreen() const {
  216. return current_video_mode.fullscreen;
  217. }
  218. void OS_Haiku::set_window_resizable(bool p_enabled) {
  219. uint32 flags = window->Flags();
  220. if (p_enabled) {
  221. flags &= ~(B_NOT_RESIZABLE);
  222. } else {
  223. flags |= B_NOT_RESIZABLE;
  224. }
  225. window->SetFlags(flags);
  226. current_video_mode.resizable = p_enabled;
  227. }
  228. bool OS_Haiku::is_window_resizable() const {
  229. return current_video_mode.resizable;
  230. }
  231. void OS_Haiku::set_window_minimized(bool p_enabled) {
  232. window->Minimize(p_enabled);
  233. }
  234. bool OS_Haiku::is_window_minimized() const {
  235. return window->IsMinimized();
  236. }
  237. void OS_Haiku::set_window_maximized(bool p_enabled) {
  238. window->Minimize(!p_enabled);
  239. }
  240. bool OS_Haiku::is_window_maximized() const {
  241. return !window->IsMinimized();
  242. }
  243. void OS_Haiku::set_video_mode(const VideoMode &p_video_mode, int p_screen) {
  244. ERR_PRINT("set_video_mode() NOT IMPLEMENTED");
  245. }
  246. OS::VideoMode OS_Haiku::get_video_mode(int p_screen) const {
  247. return current_video_mode;
  248. }
  249. void OS_Haiku::get_fullscreen_mode_list(List<VideoMode> *p_list, int p_screen) const {
  250. ERR_PRINT("get_fullscreen_mode_list() NOT IMPLEMENTED");
  251. }
  252. String OS_Haiku::get_executable_path() const {
  253. return OS::get_executable_path();
  254. }
  255. bool OS_Haiku::_check_internal_feature_support(const String &p_feature) {
  256. return p_feature == "pc";
  257. }
  258. String OS_Haiku::get_config_path() const {
  259. if (has_environment("XDG_CONFIG_HOME")) {
  260. return get_environment("XDG_CONFIG_HOME");
  261. } else if (has_environment("HOME")) {
  262. return get_environment("HOME").plus_file("config/settings");
  263. } else {
  264. return ".";
  265. }
  266. }
  267. String OS_Haiku::get_data_path() const {
  268. if (has_environment("XDG_DATA_HOME")) {
  269. return get_environment("XDG_DATA_HOME");
  270. } else if (has_environment("HOME")) {
  271. return get_environment("HOME").plus_file("config/data");
  272. } else {
  273. return get_config_path();
  274. }
  275. }
  276. String OS_Haiku::get_cache_path() const {
  277. if (has_environment("XDG_CACHE_HOME")) {
  278. return get_environment("XDG_CACHE_HOME");
  279. } else if (has_environment("HOME")) {
  280. return get_environment("HOME").plus_file("config/cache");
  281. } else {
  282. return get_config_path();
  283. }
  284. }