os_linuxbsd.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*************************************************************************/
  2. /* os_linuxbsd.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_linuxbsd.h"
  31. #include "core/os/dir_access.h"
  32. #include "main/main.h"
  33. #ifdef X11_ENABLED
  34. #include "display_server_x11.h"
  35. #endif
  36. #ifdef HAVE_MNTENT
  37. #include <mntent.h>
  38. #endif
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <string.h>
  42. #include <dlfcn.h>
  43. #include <fcntl.h>
  44. #include <sys/stat.h>
  45. #include <sys/types.h>
  46. #include <unistd.h>
  47. void OS_LinuxBSD::initialize() {
  48. crash_handler.initialize();
  49. OS_Unix::initialize_core();
  50. }
  51. void OS_LinuxBSD::initialize_joypads() {
  52. #ifdef JOYDEV_ENABLED
  53. joypad = memnew(JoypadLinux(Input::get_singleton()));
  54. #endif
  55. }
  56. String OS_LinuxBSD::get_unique_id() const {
  57. static String machine_id;
  58. if (machine_id.empty()) {
  59. if (FileAccess *f = FileAccess::open("/etc/machine-id", FileAccess::READ)) {
  60. while (machine_id.empty() && !f->eof_reached()) {
  61. machine_id = f->get_line().strip_edges();
  62. }
  63. f->close();
  64. memdelete(f);
  65. }
  66. }
  67. return machine_id;
  68. }
  69. void OS_LinuxBSD::finalize() {
  70. if (main_loop) {
  71. memdelete(main_loop);
  72. }
  73. main_loop = nullptr;
  74. #ifdef ALSAMIDI_ENABLED
  75. driver_alsamidi.close();
  76. #endif
  77. #ifdef JOYDEV_ENABLED
  78. if (joypad) {
  79. memdelete(joypad);
  80. }
  81. #endif
  82. }
  83. MainLoop *OS_LinuxBSD::get_main_loop() const {
  84. return main_loop;
  85. }
  86. void OS_LinuxBSD::delete_main_loop() {
  87. if (main_loop) {
  88. memdelete(main_loop);
  89. }
  90. main_loop = nullptr;
  91. }
  92. void OS_LinuxBSD::set_main_loop(MainLoop *p_main_loop) {
  93. main_loop = p_main_loop;
  94. }
  95. String OS_LinuxBSD::get_name() const {
  96. #ifdef __linux__
  97. return "Linux";
  98. #elif defined(__FreeBSD__)
  99. return "FreeBSD";
  100. #elif defined(__NetBSD__)
  101. return "NetBSD";
  102. #else
  103. return "BSD";
  104. #endif
  105. }
  106. Error OS_LinuxBSD::shell_open(String p_uri) {
  107. Error ok;
  108. List<String> args;
  109. args.push_back(p_uri);
  110. ok = execute("xdg-open", args, false);
  111. if (ok == OK) {
  112. return OK;
  113. }
  114. ok = execute("gnome-open", args, false);
  115. if (ok == OK) {
  116. return OK;
  117. }
  118. ok = execute("kde-open", args, false);
  119. return ok;
  120. }
  121. bool OS_LinuxBSD::_check_internal_feature_support(const String &p_feature) {
  122. return p_feature == "pc";
  123. }
  124. String OS_LinuxBSD::get_config_path() const {
  125. if (has_environment("XDG_CONFIG_HOME")) {
  126. return get_environment("XDG_CONFIG_HOME");
  127. } else if (has_environment("HOME")) {
  128. return get_environment("HOME").plus_file(".config");
  129. } else {
  130. return ".";
  131. }
  132. }
  133. String OS_LinuxBSD::get_data_path() const {
  134. if (has_environment("XDG_DATA_HOME")) {
  135. return get_environment("XDG_DATA_HOME");
  136. } else if (has_environment("HOME")) {
  137. return get_environment("HOME").plus_file(".local/share");
  138. } else {
  139. return get_config_path();
  140. }
  141. }
  142. String OS_LinuxBSD::get_cache_path() const {
  143. if (has_environment("XDG_CACHE_HOME")) {
  144. return get_environment("XDG_CACHE_HOME");
  145. } else if (has_environment("HOME")) {
  146. return get_environment("HOME").plus_file(".cache");
  147. } else {
  148. return get_config_path();
  149. }
  150. }
  151. String OS_LinuxBSD::get_system_dir(SystemDir p_dir) const {
  152. String xdgparam;
  153. switch (p_dir) {
  154. case SYSTEM_DIR_DESKTOP: {
  155. xdgparam = "DESKTOP";
  156. } break;
  157. case SYSTEM_DIR_DCIM: {
  158. xdgparam = "PICTURES";
  159. } break;
  160. case SYSTEM_DIR_DOCUMENTS: {
  161. xdgparam = "DOCUMENTS";
  162. } break;
  163. case SYSTEM_DIR_DOWNLOADS: {
  164. xdgparam = "DOWNLOAD";
  165. } break;
  166. case SYSTEM_DIR_MOVIES: {
  167. xdgparam = "VIDEOS";
  168. } break;
  169. case SYSTEM_DIR_MUSIC: {
  170. xdgparam = "MUSIC";
  171. } break;
  172. case SYSTEM_DIR_PICTURES: {
  173. xdgparam = "PICTURES";
  174. } break;
  175. case SYSTEM_DIR_RINGTONES: {
  176. xdgparam = "MUSIC";
  177. } break;
  178. }
  179. String pipe;
  180. List<String> arg;
  181. arg.push_back(xdgparam);
  182. Error err = const_cast<OS_LinuxBSD *>(this)->execute("xdg-user-dir", arg, true, nullptr, &pipe);
  183. if (err != OK) {
  184. return ".";
  185. }
  186. return pipe.strip_edges();
  187. }
  188. void OS_LinuxBSD::run() {
  189. force_quit = false;
  190. if (!main_loop) {
  191. return;
  192. }
  193. main_loop->init();
  194. //uint64_t last_ticks=get_ticks_usec();
  195. //int frames=0;
  196. //uint64_t frame=0;
  197. while (!force_quit) {
  198. DisplayServer::get_singleton()->process_events(); // get rid of pending events
  199. #ifdef JOYDEV_ENABLED
  200. joypad->process_joypads();
  201. #endif
  202. if (Main::iteration()) {
  203. break;
  204. }
  205. };
  206. main_loop->finish();
  207. }
  208. void OS_LinuxBSD::disable_crash_handler() {
  209. crash_handler.disable();
  210. }
  211. bool OS_LinuxBSD::is_disable_crash_handler() const {
  212. return crash_handler.is_disabled();
  213. }
  214. static String get_mountpoint(const String &p_path) {
  215. struct stat s;
  216. if (stat(p_path.utf8().get_data(), &s)) {
  217. return "";
  218. }
  219. #ifdef HAVE_MNTENT
  220. dev_t dev = s.st_dev;
  221. FILE *fd = setmntent("/proc/mounts", "r");
  222. if (!fd) {
  223. return "";
  224. }
  225. struct mntent mnt;
  226. char buf[1024];
  227. size_t buflen = 1024;
  228. while (getmntent_r(fd, &mnt, buf, buflen)) {
  229. if (!stat(mnt.mnt_dir, &s) && s.st_dev == dev) {
  230. endmntent(fd);
  231. return String(mnt.mnt_dir);
  232. }
  233. }
  234. endmntent(fd);
  235. #endif
  236. return "";
  237. }
  238. Error OS_LinuxBSD::move_to_trash(const String &p_path) {
  239. String trash_can = "";
  240. String mnt = get_mountpoint(p_path);
  241. // If there is a directory "[Mountpoint]/.Trash-[UID]/files", use it as the trash can.
  242. if (mnt != "") {
  243. String path(mnt + "/.Trash-" + itos(getuid()) + "/files");
  244. struct stat s;
  245. if (!stat(path.utf8().get_data(), &s)) {
  246. trash_can = path;
  247. }
  248. }
  249. // Otherwise, if ${XDG_DATA_HOME} is defined, use "${XDG_DATA_HOME}/Trash/files" as the trash can.
  250. if (trash_can == "") {
  251. char *dhome = getenv("XDG_DATA_HOME");
  252. if (dhome) {
  253. trash_can = String(dhome) + "/Trash/files";
  254. }
  255. }
  256. // Otherwise, if ${HOME} is defined, use "${HOME}/.local/share/Trash/files" as the trash can.
  257. if (trash_can == "") {
  258. char *home = getenv("HOME");
  259. if (home) {
  260. trash_can = String(home) + "/.local/share/Trash/files";
  261. }
  262. }
  263. // Issue an error if none of the previous locations is appropriate for the trash can.
  264. if (trash_can == "") {
  265. ERR_PRINT("move_to_trash: Could not determine the trash can location");
  266. return FAILED;
  267. }
  268. // Create needed directories for decided trash can location.
  269. DirAccess *dir_access = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  270. Error err = dir_access->make_dir_recursive(trash_can);
  271. memdelete(dir_access);
  272. // Issue an error if trash can is not created proprely.
  273. if (err != OK) {
  274. ERR_PRINT("move_to_trash: Could not create the trash can \"" + trash_can + "\"");
  275. return err;
  276. }
  277. // The trash can is successfully created, now move the given resource to it.
  278. // Do not use DirAccess:rename() because it can't move files across multiple mountpoints.
  279. List<String> mv_args;
  280. mv_args.push_back(p_path);
  281. mv_args.push_back(trash_can);
  282. int retval;
  283. err = execute("mv", mv_args, true, nullptr, nullptr, &retval);
  284. // Issue an error if "mv" failed to move the given resource to the trash can.
  285. if (err != OK || retval != 0) {
  286. ERR_PRINT("move_to_trash: Could not move the resource \"" + p_path + "\" to the trash can \"" + trash_can + "\"");
  287. return FAILED;
  288. }
  289. return OK;
  290. }
  291. OS_LinuxBSD::OS_LinuxBSD() {
  292. main_loop = nullptr;
  293. force_quit = false;
  294. #ifdef PULSEAUDIO_ENABLED
  295. AudioDriverManager::add_driver(&driver_pulseaudio);
  296. #endif
  297. #ifdef ALSA_ENABLED
  298. AudioDriverManager::add_driver(&driver_alsa);
  299. #endif
  300. #ifdef X11_ENABLED
  301. DisplayServerX11::register_x11_driver();
  302. #endif
  303. }