os_haiku.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*************************************************************************/
  2. /* os_haiku.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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 "drivers/gles3/rasterizer_gles3.h"
  31. #include "os_haiku.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. 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. RasterizerGLES3::register_config();
  94. RasterizerGLES3::make_current();
  95. #endif
  96. // FIXME: Reimplement threaded rendering? Or remove?
  97. if (get_render_thread_mode() != RENDER_THREAD_UNSAFE) {
  98. visual_server = memnew(VisualServerWrapMT(visual_server, false));
  99. } else {
  100. visual_server = memnew(VisualServerRaster);
  101. }
  102. ERR_FAIL_COND_V(!visual_server, ERR_UNAVAILABLE);
  103. video_driver_index = p_video_driver;
  104. input = memnew(InputDefault);
  105. window->SetInput(input);
  106. window->Show();
  107. visual_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. visual_server->finish();
  117. memdelete(visual_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. void OS_Haiku::set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot) {
  161. // TODO
  162. }
  163. int OS_Haiku::get_screen_count() const {
  164. // TODO: implement get_screen_count()
  165. return 1;
  166. }
  167. int OS_Haiku::get_current_screen() const {
  168. // TODO: implement get_current_screen()
  169. return 0;
  170. }
  171. void OS_Haiku::set_current_screen(int p_screen) {
  172. // TODO: implement set_current_screen()
  173. }
  174. Point2 OS_Haiku::get_screen_position(int p_screen) const {
  175. // TODO: make this work with the p_screen parameter
  176. BScreen *screen = new BScreen(window);
  177. BRect frame = screen->Frame();
  178. delete screen;
  179. return Point2i(frame.left, frame.top);
  180. }
  181. Size2 OS_Haiku::get_screen_size(int p_screen) const {
  182. // TODO: make this work with the p_screen parameter
  183. BScreen *screen = new BScreen(window);
  184. BRect frame = screen->Frame();
  185. delete screen;
  186. return Size2i(frame.IntegerWidth() + 1, frame.IntegerHeight() + 1);
  187. }
  188. void OS_Haiku::set_window_title(const String &p_title) {
  189. window->SetTitle(p_title.utf8().get_data());
  190. }
  191. Size2 OS_Haiku::get_window_size() const {
  192. BSize size = window->Size();
  193. return Size2i(size.IntegerWidth() + 1, size.IntegerHeight() + 1);
  194. }
  195. void OS_Haiku::set_window_size(const Size2 p_size) {
  196. // TODO: why does it stop redrawing after this is called?
  197. window->ResizeTo(p_size.x, p_size.y);
  198. }
  199. Point2 OS_Haiku::get_window_position() const {
  200. BPoint point(0, 0);
  201. window->ConvertToScreen(&point);
  202. return Point2i(point.x, point.y);
  203. }
  204. void OS_Haiku::set_window_position(const Point2 &p_position) {
  205. window->MoveTo(p_position.x, p_position.y);
  206. }
  207. void OS_Haiku::set_window_fullscreen(bool p_enabled) {
  208. window->SetFullScreen(p_enabled);
  209. current_video_mode.fullscreen = p_enabled;
  210. visual_server->init();
  211. }
  212. bool OS_Haiku::is_window_fullscreen() const {
  213. return current_video_mode.fullscreen;
  214. }
  215. void OS_Haiku::set_window_resizable(bool p_enabled) {
  216. uint32 flags = window->Flags();
  217. if (p_enabled) {
  218. flags &= ~(B_NOT_RESIZABLE);
  219. } else {
  220. flags |= B_NOT_RESIZABLE;
  221. }
  222. window->SetFlags(flags);
  223. current_video_mode.resizable = p_enabled;
  224. }
  225. bool OS_Haiku::is_window_resizable() const {
  226. return current_video_mode.resizable;
  227. }
  228. void OS_Haiku::set_window_minimized(bool p_enabled) {
  229. window->Minimize(p_enabled);
  230. }
  231. bool OS_Haiku::is_window_minimized() const {
  232. return window->IsMinimized();
  233. }
  234. void OS_Haiku::set_window_maximized(bool p_enabled) {
  235. window->Minimize(!p_enabled);
  236. }
  237. bool OS_Haiku::is_window_maximized() const {
  238. return !window->IsMinimized();
  239. }
  240. void OS_Haiku::set_video_mode(const VideoMode &p_video_mode, int p_screen) {
  241. ERR_PRINT("set_video_mode() NOT IMPLEMENTED");
  242. }
  243. OS::VideoMode OS_Haiku::get_video_mode(int p_screen) const {
  244. return current_video_mode;
  245. }
  246. void OS_Haiku::get_fullscreen_mode_list(List<VideoMode> *p_list, int p_screen) const {
  247. ERR_PRINT("get_fullscreen_mode_list() NOT IMPLEMENTED");
  248. }
  249. String OS_Haiku::get_executable_path() const {
  250. return OS::get_executable_path();
  251. }
  252. bool OS_Haiku::_check_internal_feature_support(const String &p_feature) {
  253. return p_feature == "pc" || p_feature == "s3tc";
  254. }
  255. String OS_Haiku::get_config_path() const {
  256. if (has_environment("XDG_CONFIG_HOME")) {
  257. return get_environment("XDG_CONFIG_HOME");
  258. } else if (has_environment("HOME")) {
  259. return get_environment("HOME").plus_file("config/settings");
  260. } else {
  261. return ".";
  262. }
  263. }
  264. String OS_Haiku::get_data_path() const {
  265. if (has_environment("XDG_DATA_HOME")) {
  266. return get_environment("XDG_DATA_HOME");
  267. } else if (has_environment("HOME")) {
  268. return get_environment("HOME").plus_file("config/data");
  269. } else {
  270. return get_config_path();
  271. }
  272. }
  273. String OS_Haiku::get_cache_path() const {
  274. if (has_environment("XDG_CACHE_HOME")) {
  275. return get_environment("XDG_CACHE_HOME");
  276. } else if (has_environment("HOME")) {
  277. return get_environment("HOME").plus_file("config/cache");
  278. } else {
  279. return get_config_path();
  280. }
  281. }
  282. OS::PowerState OS_Haiku::get_power_state() {
  283. WARN_PRINT("Power management is not implemented on this platform, defaulting to POWERSTATE_UNKNOWN");
  284. return OS::POWERSTATE_UNKNOWN;
  285. }
  286. int OS_Haiku::get_power_seconds_left() {
  287. WARN_PRINT("Power management is not implemented on this platform, defaulting to -1");
  288. return -1;
  289. }
  290. int OS_Haiku::get_power_percent_left() {
  291. WARN_PRINT("Power management is not implemented on this platform, defaulting to -1");
  292. return -1;
  293. }