os_server.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*************************************************************************/
  2. /* os_server.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_server.h"
  31. #include "core/print_string.h"
  32. #include "drivers/dummy/rasterizer_dummy.h"
  33. #include "drivers/dummy/texture_loader_dummy.h"
  34. #include "servers/visual/visual_server_raster.h"
  35. #include "main/main.h"
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <unistd.h>
  39. int OS_Server::get_video_driver_count() const {
  40. return 1;
  41. }
  42. const char *OS_Server::get_video_driver_name(int p_driver) const {
  43. return "Dummy";
  44. }
  45. int OS_Server::get_audio_driver_count() const {
  46. return 1;
  47. }
  48. const char *OS_Server::get_audio_driver_name(int p_driver) const {
  49. return "Dummy";
  50. }
  51. int OS_Server::get_current_video_driver() const {
  52. return video_driver_index;
  53. }
  54. void OS_Server::initialize_core() {
  55. crash_handler.initialize();
  56. OS_Unix::initialize_core();
  57. #ifdef __APPLE__
  58. SemaphoreOSX::make_default();
  59. #endif
  60. }
  61. Error OS_Server::initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver) {
  62. args = OS::get_singleton()->get_cmdline_args();
  63. current_videomode = p_desired;
  64. main_loop = NULL;
  65. RasterizerDummy::make_current();
  66. video_driver_index = p_video_driver; // unused in server platform, but should still be initialized
  67. visual_server = memnew(VisualServerRaster);
  68. visual_server->init();
  69. AudioDriverManager::initialize(p_audio_driver);
  70. input = memnew(InputDefault);
  71. #ifdef __APPLE__
  72. power_manager = memnew(PowerOSX);
  73. #else
  74. power_manager = memnew(PowerX11);
  75. #endif
  76. _ensure_user_data_dir();
  77. resource_loader_dummy.instance();
  78. ResourceLoader::add_resource_format_loader(resource_loader_dummy);
  79. return OK;
  80. }
  81. void OS_Server::finalize() {
  82. if (main_loop)
  83. memdelete(main_loop);
  84. main_loop = NULL;
  85. visual_server->finish();
  86. memdelete(visual_server);
  87. memdelete(input);
  88. memdelete(power_manager);
  89. ResourceLoader::remove_resource_format_loader(resource_loader_dummy);
  90. resource_loader_dummy.unref();
  91. args.clear();
  92. }
  93. void OS_Server::set_mouse_show(bool p_show) {
  94. }
  95. void OS_Server::set_mouse_grab(bool p_grab) {
  96. grab = p_grab;
  97. }
  98. bool OS_Server::is_mouse_grab_enabled() const {
  99. return grab;
  100. }
  101. int OS_Server::get_mouse_button_state() const {
  102. return 0;
  103. }
  104. Point2 OS_Server::get_mouse_position() const {
  105. return Point2();
  106. }
  107. void OS_Server::set_window_title(const String &p_title) {
  108. }
  109. void OS_Server::set_video_mode(const VideoMode &p_video_mode, int p_screen) {
  110. }
  111. OS::VideoMode OS_Server::get_video_mode(int p_screen) const {
  112. return current_videomode;
  113. }
  114. Size2 OS_Server::get_window_size() const {
  115. return Vector2(current_videomode.width, current_videomode.height);
  116. }
  117. void OS_Server::get_fullscreen_mode_list(List<VideoMode> *p_list, int p_screen) const {
  118. }
  119. MainLoop *OS_Server::get_main_loop() const {
  120. return main_loop;
  121. }
  122. void OS_Server::delete_main_loop() {
  123. if (main_loop)
  124. memdelete(main_loop);
  125. main_loop = NULL;
  126. }
  127. void OS_Server::set_main_loop(MainLoop *p_main_loop) {
  128. main_loop = p_main_loop;
  129. input->set_main_loop(p_main_loop);
  130. }
  131. bool OS_Server::can_draw() const {
  132. return false; //can never draw
  133. };
  134. String OS_Server::get_name() const {
  135. return "Server";
  136. }
  137. void OS_Server::move_window_to_foreground() {
  138. }
  139. OS::PowerState OS_Server::get_power_state() {
  140. return power_manager->get_power_state();
  141. }
  142. int OS_Server::get_power_seconds_left() {
  143. return power_manager->get_power_seconds_left();
  144. }
  145. int OS_Server::get_power_percent_left() {
  146. return power_manager->get_power_percent_left();
  147. }
  148. bool OS_Server::_check_internal_feature_support(const String &p_feature) {
  149. return p_feature == "pc";
  150. }
  151. void OS_Server::run() {
  152. force_quit = false;
  153. if (!main_loop)
  154. return;
  155. main_loop->init();
  156. while (!force_quit) {
  157. if (Main::iteration())
  158. break;
  159. };
  160. main_loop->finish();
  161. }
  162. String OS_Server::get_config_path() const {
  163. if (has_environment("XDG_CONFIG_HOME")) {
  164. return get_environment("XDG_CONFIG_HOME");
  165. } else if (has_environment("HOME")) {
  166. return get_environment("HOME").plus_file(".config");
  167. } else {
  168. return ".";
  169. }
  170. }
  171. String OS_Server::get_data_path() const {
  172. if (has_environment("XDG_DATA_HOME")) {
  173. return get_environment("XDG_DATA_HOME");
  174. } else if (has_environment("HOME")) {
  175. return get_environment("HOME").plus_file(".local/share");
  176. } else {
  177. return get_config_path();
  178. }
  179. }
  180. String OS_Server::get_cache_path() const {
  181. if (has_environment("XDG_CACHE_HOME")) {
  182. return get_environment("XDG_CACHE_HOME");
  183. } else if (has_environment("HOME")) {
  184. return get_environment("HOME").plus_file(".cache");
  185. } else {
  186. return get_config_path();
  187. }
  188. }
  189. String OS_Server::get_system_dir(SystemDir p_dir) const {
  190. String xdgparam;
  191. switch (p_dir) {
  192. case SYSTEM_DIR_DESKTOP: {
  193. xdgparam = "DESKTOP";
  194. } break;
  195. case SYSTEM_DIR_DCIM: {
  196. xdgparam = "PICTURES";
  197. } break;
  198. case SYSTEM_DIR_DOCUMENTS: {
  199. xdgparam = "DOCUMENTS";
  200. } break;
  201. case SYSTEM_DIR_DOWNLOADS: {
  202. xdgparam = "DOWNLOAD";
  203. } break;
  204. case SYSTEM_DIR_MOVIES: {
  205. xdgparam = "VIDEOS";
  206. } break;
  207. case SYSTEM_DIR_MUSIC: {
  208. xdgparam = "MUSIC";
  209. } break;
  210. case SYSTEM_DIR_PICTURES: {
  211. xdgparam = "PICTURES";
  212. } break;
  213. case SYSTEM_DIR_RINGTONES: {
  214. xdgparam = "MUSIC";
  215. } break;
  216. }
  217. String pipe;
  218. List<String> arg;
  219. arg.push_back(xdgparam);
  220. Error err = const_cast<OS_Server *>(this)->execute("xdg-user-dir", arg, true, NULL, &pipe);
  221. if (err != OK)
  222. return ".";
  223. return pipe.strip_edges();
  224. }
  225. void OS_Server::disable_crash_handler() {
  226. crash_handler.disable();
  227. }
  228. bool OS_Server::is_disable_crash_handler() const {
  229. return crash_handler.is_disabled();
  230. }
  231. OS_Server::OS_Server() {
  232. //adriver here
  233. grab = false;
  234. };