os_linuxbsd.cpp 14 KB

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