os_linuxbsd.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. int err_code;
  109. List<String> args;
  110. args.push_back(p_uri);
  111. // Agnostic
  112. ok = execute("xdg-open", args, true, nullptr, nullptr, &err_code);
  113. if (ok == OK && !err_code) {
  114. return OK;
  115. } else if (err_code == 2) {
  116. return ERR_FILE_NOT_FOUND;
  117. }
  118. // GNOME
  119. args.push_front("open"); // The command is `gio open`, so we need to add it to args
  120. ok = execute("gio", args, true, nullptr, nullptr, &err_code);
  121. if (ok == OK && !err_code) {
  122. return OK;
  123. } else if (err_code == 2) {
  124. return ERR_FILE_NOT_FOUND;
  125. }
  126. args.pop_front();
  127. ok = execute("gvfs-open", args, true, nullptr, nullptr, &err_code);
  128. if (ok == OK && !err_code) {
  129. return OK;
  130. } else if (err_code == 2) {
  131. return ERR_FILE_NOT_FOUND;
  132. }
  133. // KDE
  134. ok = execute("kde-open5", args, true, nullptr, nullptr, &err_code);
  135. if (ok == OK && !err_code) {
  136. return OK;
  137. }
  138. ok = execute("kde-open", args, true, nullptr, nullptr, &err_code);
  139. return !err_code ? ok : FAILED;
  140. }
  141. bool OS_LinuxBSD::_check_internal_feature_support(const String &p_feature) {
  142. return p_feature == "pc";
  143. }
  144. String OS_LinuxBSD::get_config_path() const {
  145. if (has_environment("XDG_CONFIG_HOME")) {
  146. return get_environment("XDG_CONFIG_HOME");
  147. } else if (has_environment("HOME")) {
  148. return get_environment("HOME").plus_file(".config");
  149. } else {
  150. return ".";
  151. }
  152. }
  153. String OS_LinuxBSD::get_data_path() const {
  154. if (has_environment("XDG_DATA_HOME")) {
  155. return get_environment("XDG_DATA_HOME");
  156. } else if (has_environment("HOME")) {
  157. return get_environment("HOME").plus_file(".local/share");
  158. } else {
  159. return get_config_path();
  160. }
  161. }
  162. String OS_LinuxBSD::get_cache_path() const {
  163. if (has_environment("XDG_CACHE_HOME")) {
  164. return get_environment("XDG_CACHE_HOME");
  165. } else if (has_environment("HOME")) {
  166. return get_environment("HOME").plus_file(".cache");
  167. } else {
  168. return get_config_path();
  169. }
  170. }
  171. String OS_LinuxBSD::get_system_dir(SystemDir p_dir) const {
  172. String xdgparam;
  173. switch (p_dir) {
  174. case SYSTEM_DIR_DESKTOP: {
  175. xdgparam = "DESKTOP";
  176. } break;
  177. case SYSTEM_DIR_DCIM: {
  178. xdgparam = "PICTURES";
  179. } break;
  180. case SYSTEM_DIR_DOCUMENTS: {
  181. xdgparam = "DOCUMENTS";
  182. } break;
  183. case SYSTEM_DIR_DOWNLOADS: {
  184. xdgparam = "DOWNLOAD";
  185. } break;
  186. case SYSTEM_DIR_MOVIES: {
  187. xdgparam = "VIDEOS";
  188. } break;
  189. case SYSTEM_DIR_MUSIC: {
  190. xdgparam = "MUSIC";
  191. } break;
  192. case SYSTEM_DIR_PICTURES: {
  193. xdgparam = "PICTURES";
  194. } break;
  195. case SYSTEM_DIR_RINGTONES: {
  196. xdgparam = "MUSIC";
  197. } break;
  198. }
  199. String pipe;
  200. List<String> arg;
  201. arg.push_back(xdgparam);
  202. Error err = const_cast<OS_LinuxBSD *>(this)->execute("xdg-user-dir", arg, true, nullptr, &pipe);
  203. if (err != OK) {
  204. return ".";
  205. }
  206. return pipe.strip_edges();
  207. }
  208. void OS_LinuxBSD::run() {
  209. force_quit = false;
  210. if (!main_loop) {
  211. return;
  212. }
  213. main_loop->initialize();
  214. //uint64_t last_ticks=get_ticks_usec();
  215. //int frames=0;
  216. //uint64_t frame=0;
  217. while (!force_quit) {
  218. DisplayServer::get_singleton()->process_events(); // get rid of pending events
  219. #ifdef JOYDEV_ENABLED
  220. joypad->process_joypads();
  221. #endif
  222. if (Main::iteration()) {
  223. break;
  224. }
  225. };
  226. main_loop->finalize();
  227. }
  228. void OS_LinuxBSD::disable_crash_handler() {
  229. crash_handler.disable();
  230. }
  231. bool OS_LinuxBSD::is_disable_crash_handler() const {
  232. return crash_handler.is_disabled();
  233. }
  234. static String get_mountpoint(const String &p_path) {
  235. struct stat s;
  236. if (stat(p_path.utf8().get_data(), &s)) {
  237. return "";
  238. }
  239. #ifdef HAVE_MNTENT
  240. dev_t dev = s.st_dev;
  241. FILE *fd = setmntent("/proc/mounts", "r");
  242. if (!fd) {
  243. return "";
  244. }
  245. struct mntent mnt;
  246. char buf[1024];
  247. size_t buflen = 1024;
  248. while (getmntent_r(fd, &mnt, buf, buflen)) {
  249. if (!stat(mnt.mnt_dir, &s) && s.st_dev == dev) {
  250. endmntent(fd);
  251. return String(mnt.mnt_dir);
  252. }
  253. }
  254. endmntent(fd);
  255. #endif
  256. return "";
  257. }
  258. Error OS_LinuxBSD::move_to_trash(const String &p_path) {
  259. int err_code;
  260. List<String> args;
  261. args.push_back(p_path);
  262. args.push_front("trash"); // The command is `gio trash <file_name>` so we need to add it to args.
  263. Error result = execute("gio", args, true, nullptr, nullptr, &err_code); // For GNOME based machines.
  264. if (result == OK && !err_code) {
  265. return OK;
  266. } else if (err_code == 2) {
  267. return ERR_FILE_NOT_FOUND;
  268. }
  269. args.pop_front();
  270. args.push_front("move");
  271. args.push_back("trash:/"); // The command is `kioclient5 move <file_name> trash:/`.
  272. result = execute("kioclient5", args, true, nullptr, nullptr, &err_code); // For KDE based machines.
  273. if (result == OK && !err_code) {
  274. return OK;
  275. } else if (err_code == 2) {
  276. return ERR_FILE_NOT_FOUND;
  277. }
  278. args.pop_front();
  279. args.pop_back();
  280. result = execute("gvfs-trash", args, true, nullptr, nullptr, &err_code); // For older Linux machines.
  281. if (result == OK && !err_code) {
  282. return OK;
  283. } else if (err_code == 2) {
  284. return ERR_FILE_NOT_FOUND;
  285. }
  286. // If the commands `kioclient5`, `gio` or `gvfs-trash` don't exist on the system we do it manually.
  287. String trash_path = "";
  288. String mnt = get_mountpoint(p_path);
  289. // If there is a directory "[Mountpoint]/.Trash-[UID], use it as the trash can.
  290. if (mnt != "") {
  291. String path(mnt + "/.Trash-" + itos(getuid()));
  292. struct stat s;
  293. if (!stat(path.utf8().get_data(), &s)) {
  294. trash_path = path;
  295. }
  296. }
  297. // Otherwise, if ${XDG_DATA_HOME} is defined, use "${XDG_DATA_HOME}/Trash" as the trash can.
  298. if (trash_path == "") {
  299. char *dhome = getenv("XDG_DATA_HOME");
  300. if (dhome) {
  301. trash_path = String(dhome) + "/Trash";
  302. }
  303. }
  304. // Otherwise, if ${HOME} is defined, use "${HOME}/.local/share/Trash" as the trash can.
  305. if (trash_path == "") {
  306. char *home = getenv("HOME");
  307. if (home) {
  308. trash_path = String(home) + "/.local/share/Trash";
  309. }
  310. }
  311. // Issue an error if none of the previous locations is appropriate for the trash can.
  312. ERR_FAIL_COND_V_MSG(trash_path == "", FAILED, "Could not determine the trash can location");
  313. // Create needed directories for decided trash can location.
  314. {
  315. DirAccess *dir_access = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  316. Error err = dir_access->make_dir_recursive(trash_path);
  317. // Issue an error if trash can is not created proprely.
  318. ERR_FAIL_COND_V_MSG(err != OK, err, "Could not create the trash path \"" + trash_path + "\"");
  319. err = dir_access->make_dir_recursive(trash_path + "/files");
  320. ERR_FAIL_COND_V_MSG(err != OK, err, "Could not create the trash path \"" + trash_path + "\"/files");
  321. err = dir_access->make_dir_recursive(trash_path + "/info");
  322. ERR_FAIL_COND_V_MSG(err != OK, err, "Could not create the trash path \"" + trash_path + "\"/info");
  323. memdelete(dir_access);
  324. }
  325. // The trash can is successfully created, now we check that we don't exceed our file name length limit.
  326. // If the file name is too long trim it so we can add the identifying number and ".trashinfo".
  327. // Assumes that the file name length limit is 255 characters.
  328. String file_name = basename(p_path.utf8().get_data());
  329. if (file_name.length() > 240) {
  330. file_name = file_name.substr(0, file_name.length() - 15);
  331. }
  332. String dest_path = trash_path + "/files/" + file_name;
  333. struct stat buff;
  334. int id_number = 0;
  335. String fn = file_name;
  336. // Checks if a resource with the same name already exist in the trash can,
  337. // if there is, add an identifying number to our resource's name.
  338. while (stat(dest_path.utf8().get_data(), &buff) == 0) {
  339. id_number++;
  340. // Added a limit to check for identically named files already on the trash can
  341. // if there are too many it could make the editor unresponsive.
  342. ERR_FAIL_COND_V_MSG(id_number > 99, FAILED, "Too many identically named resources already in the trash can.");
  343. fn = file_name + "." + itos(id_number);
  344. dest_path = trash_path + "/files/" + fn;
  345. }
  346. file_name = fn;
  347. // Generates the .trashinfo file
  348. OS::Date date = OS::get_singleton()->get_date(false);
  349. OS::Time time = OS::get_singleton()->get_time(false);
  350. String timestamp = vformat("%04d-%02d-%02dT%02d:%02d:", date.year, date.month, date.day, time.hour, time.min);
  351. timestamp = vformat("%s%02d", timestamp, time.sec); // vformat only supports up to 6 arguments.
  352. String trash_info = "[Trash Info]\nPath=" + p_path.http_escape() + "\nDeletionDate=" + timestamp + "\n";
  353. {
  354. Error err;
  355. FileAccess *file = FileAccess::open(trash_path + "/info/" + file_name + ".trashinfo", FileAccess::WRITE, &err);
  356. ERR_FAIL_COND_V_MSG(err != OK, err, "Can't create trashinfo file:" + trash_path + "/info/" + file_name + ".trashinfo");
  357. file->store_string(trash_info);
  358. file->close();
  359. // Rename our resource before moving it to the trash can.
  360. DirAccess *dir_access = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  361. err = dir_access->rename(p_path, p_path.get_base_dir() + "/" + file_name);
  362. ERR_FAIL_COND_V_MSG(err != OK, err, "Can't rename file \"" + p_path + "\"");
  363. memdelete(dir_access);
  364. }
  365. // Move the given resource to the trash can.
  366. // Do not use DirAccess:rename() because it can't move files across multiple mountpoints.
  367. List<String> mv_args;
  368. mv_args.push_back(p_path.get_base_dir() + "/" + file_name);
  369. mv_args.push_back(trash_path + "/files");
  370. {
  371. int retval;
  372. Error err = execute("mv", mv_args, true, nullptr, nullptr, &retval);
  373. // Issue an error if "mv" failed to move the given resource to the trash can.
  374. if (err != OK || retval != 0) {
  375. ERR_PRINT("move_to_trash: Could not move the resource \"" + p_path + "\" to the trash can \"" + trash_path + "/files\"");
  376. DirAccess *dir_access = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  377. err = dir_access->rename(p_path.get_base_dir() + "/" + file_name, p_path);
  378. memdelete(dir_access);
  379. ERR_FAIL_COND_V_MSG(err != OK, err, "Could not rename " + p_path.get_base_dir() + "/" + file_name + " back to its original name:" + p_path);
  380. return FAILED;
  381. }
  382. }
  383. return OK;
  384. }
  385. OS_LinuxBSD::OS_LinuxBSD() {
  386. main_loop = nullptr;
  387. force_quit = false;
  388. #ifdef PULSEAUDIO_ENABLED
  389. AudioDriverManager::add_driver(&driver_pulseaudio);
  390. #endif
  391. #ifdef ALSA_ENABLED
  392. AudioDriverManager::add_driver(&driver_alsa);
  393. #endif
  394. #ifdef X11_ENABLED
  395. DisplayServerX11::register_x11_driver();
  396. #endif
  397. }