os_server.cpp 7.6 KB

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