os_linuxbsd.cpp 14 KB

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