os_linuxbsd.cpp 9.1 KB

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