os_linuxbsd.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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/io/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::alert(const String &p_alert, const String &p_title) {
  48. const char *message_programs[] = { "zenity", "kdialog", "Xdialog", "xmessage" };
  49. String path = get_environment("PATH");
  50. Vector<String> path_elems = path.split(":", false);
  51. String program;
  52. for (int i = 0; i < path_elems.size(); i++) {
  53. for (uint64_t k = 0; k < sizeof(message_programs) / sizeof(char *); k++) {
  54. String tested_path = path_elems[i].plus_file(message_programs[k]);
  55. if (FileAccess::exists(tested_path)) {
  56. program = tested_path;
  57. break;
  58. }
  59. }
  60. if (program.length()) {
  61. break;
  62. }
  63. }
  64. List<String> args;
  65. if (program.ends_with("zenity")) {
  66. args.push_back("--error");
  67. args.push_back("--width");
  68. args.push_back("500");
  69. args.push_back("--title");
  70. args.push_back(p_title);
  71. args.push_back("--text");
  72. args.push_back(p_alert);
  73. }
  74. if (program.ends_with("kdialog")) {
  75. args.push_back("--error");
  76. args.push_back(p_alert);
  77. args.push_back("--title");
  78. args.push_back(p_title);
  79. }
  80. if (program.ends_with("Xdialog")) {
  81. args.push_back("--title");
  82. args.push_back(p_title);
  83. args.push_back("--msgbox");
  84. args.push_back(p_alert);
  85. args.push_back("0");
  86. args.push_back("0");
  87. }
  88. if (program.ends_with("xmessage")) {
  89. args.push_back("-center");
  90. args.push_back("-title");
  91. args.push_back(p_title);
  92. args.push_back(p_alert);
  93. }
  94. if (program.length()) {
  95. execute(program, args);
  96. } else {
  97. print_line(p_alert);
  98. }
  99. }
  100. void OS_LinuxBSD::initialize() {
  101. crash_handler.initialize();
  102. OS_Unix::initialize_core();
  103. }
  104. void OS_LinuxBSD::initialize_joypads() {
  105. #ifdef JOYDEV_ENABLED
  106. joypad = memnew(JoypadLinux(Input::get_singleton()));
  107. #endif
  108. }
  109. String OS_LinuxBSD::get_unique_id() const {
  110. static String machine_id;
  111. if (machine_id.is_empty()) {
  112. if (FileAccess *f = FileAccess::open("/etc/machine-id", FileAccess::READ)) {
  113. while (machine_id.is_empty() && !f->eof_reached()) {
  114. machine_id = f->get_line().strip_edges();
  115. }
  116. f->close();
  117. memdelete(f);
  118. }
  119. }
  120. return machine_id;
  121. }
  122. void OS_LinuxBSD::finalize() {
  123. if (main_loop) {
  124. memdelete(main_loop);
  125. }
  126. main_loop = nullptr;
  127. #ifdef ALSAMIDI_ENABLED
  128. driver_alsamidi.close();
  129. #endif
  130. #ifdef JOYDEV_ENABLED
  131. if (joypad) {
  132. memdelete(joypad);
  133. }
  134. #endif
  135. }
  136. MainLoop *OS_LinuxBSD::get_main_loop() const {
  137. return main_loop;
  138. }
  139. void OS_LinuxBSD::delete_main_loop() {
  140. if (main_loop) {
  141. memdelete(main_loop);
  142. }
  143. main_loop = nullptr;
  144. }
  145. void OS_LinuxBSD::set_main_loop(MainLoop *p_main_loop) {
  146. main_loop = p_main_loop;
  147. }
  148. String OS_LinuxBSD::get_name() const {
  149. #ifdef __linux__
  150. return "Linux";
  151. #elif defined(__FreeBSD__)
  152. return "FreeBSD";
  153. #elif defined(__NetBSD__)
  154. return "NetBSD";
  155. #elif defined(__OpenBSD__)
  156. return "OpenBSD";
  157. #else
  158. return "BSD";
  159. #endif
  160. }
  161. Error OS_LinuxBSD::shell_open(String p_uri) {
  162. Error ok;
  163. int err_code;
  164. List<String> args;
  165. args.push_back(p_uri);
  166. // Agnostic
  167. ok = execute("xdg-open", args, nullptr, &err_code);
  168. if (ok == OK && !err_code) {
  169. return OK;
  170. } else if (err_code == 2) {
  171. return ERR_FILE_NOT_FOUND;
  172. }
  173. // GNOME
  174. args.push_front("open"); // The command is `gio open`, so we need to add it to args
  175. ok = execute("gio", args, nullptr, &err_code);
  176. if (ok == OK && !err_code) {
  177. return OK;
  178. } else if (err_code == 2) {
  179. return ERR_FILE_NOT_FOUND;
  180. }
  181. args.pop_front();
  182. ok = execute("gvfs-open", args, nullptr, &err_code);
  183. if (ok == OK && !err_code) {
  184. return OK;
  185. } else if (err_code == 2) {
  186. return ERR_FILE_NOT_FOUND;
  187. }
  188. // KDE
  189. ok = execute("kde-open5", args, nullptr, &err_code);
  190. if (ok == OK && !err_code) {
  191. return OK;
  192. }
  193. ok = execute("kde-open", args, nullptr, &err_code);
  194. return !err_code ? ok : FAILED;
  195. }
  196. bool OS_LinuxBSD::_check_internal_feature_support(const String &p_feature) {
  197. return p_feature == "pc";
  198. }
  199. String OS_LinuxBSD::get_config_path() const {
  200. if (has_environment("XDG_CONFIG_HOME")) {
  201. if (get_environment("XDG_CONFIG_HOME").is_absolute_path()) {
  202. return get_environment("XDG_CONFIG_HOME");
  203. } else {
  204. 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.");
  205. return has_environment("HOME") ? get_environment("HOME").plus_file(".config") : ".";
  206. }
  207. } else if (has_environment("HOME")) {
  208. return get_environment("HOME").plus_file(".config");
  209. } else {
  210. return ".";
  211. }
  212. }
  213. String OS_LinuxBSD::get_data_path() const {
  214. if (has_environment("XDG_DATA_HOME")) {
  215. if (get_environment("XDG_DATA_HOME").is_absolute_path()) {
  216. return get_environment("XDG_DATA_HOME");
  217. } else {
  218. 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.");
  219. return has_environment("HOME") ? get_environment("HOME").plus_file(".local/share") : get_config_path();
  220. }
  221. } else if (has_environment("HOME")) {
  222. return get_environment("HOME").plus_file(".local/share");
  223. } else {
  224. return get_config_path();
  225. }
  226. }
  227. String OS_LinuxBSD::get_cache_path() const {
  228. if (has_environment("XDG_CACHE_HOME")) {
  229. if (get_environment("XDG_CACHE_HOME").is_absolute_path()) {
  230. return get_environment("XDG_CACHE_HOME");
  231. } else {
  232. 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.");
  233. return has_environment("HOME") ? get_environment("HOME").plus_file(".cache") : get_config_path();
  234. }
  235. } else if (has_environment("HOME")) {
  236. return get_environment("HOME").plus_file(".cache");
  237. } else {
  238. return get_config_path();
  239. }
  240. }
  241. String OS_LinuxBSD::get_system_dir(SystemDir p_dir) const {
  242. String xdgparam;
  243. switch (p_dir) {
  244. case SYSTEM_DIR_DESKTOP: {
  245. xdgparam = "DESKTOP";
  246. } break;
  247. case SYSTEM_DIR_DCIM: {
  248. xdgparam = "PICTURES";
  249. } break;
  250. case SYSTEM_DIR_DOCUMENTS: {
  251. xdgparam = "DOCUMENTS";
  252. } break;
  253. case SYSTEM_DIR_DOWNLOADS: {
  254. xdgparam = "DOWNLOAD";
  255. } break;
  256. case SYSTEM_DIR_MOVIES: {
  257. xdgparam = "VIDEOS";
  258. } break;
  259. case SYSTEM_DIR_MUSIC: {
  260. xdgparam = "MUSIC";
  261. } break;
  262. case SYSTEM_DIR_PICTURES: {
  263. xdgparam = "PICTURES";
  264. } break;
  265. case SYSTEM_DIR_RINGTONES: {
  266. xdgparam = "MUSIC";
  267. } break;
  268. }
  269. String pipe;
  270. List<String> arg;
  271. arg.push_back(xdgparam);
  272. Error err = const_cast<OS_LinuxBSD *>(this)->execute("xdg-user-dir", arg, &pipe);
  273. if (err != OK) {
  274. return ".";
  275. }
  276. return pipe.strip_edges();
  277. }
  278. void OS_LinuxBSD::run() {
  279. force_quit = false;
  280. if (!main_loop) {
  281. return;
  282. }
  283. main_loop->initialize();
  284. //uint64_t last_ticks=get_ticks_usec();
  285. //int frames=0;
  286. //uint64_t frame=0;
  287. while (!force_quit) {
  288. DisplayServer::get_singleton()->process_events(); // get rid of pending events
  289. #ifdef JOYDEV_ENABLED
  290. joypad->process_joypads();
  291. #endif
  292. if (Main::iteration()) {
  293. break;
  294. }
  295. };
  296. main_loop->finalize();
  297. }
  298. void OS_LinuxBSD::disable_crash_handler() {
  299. crash_handler.disable();
  300. }
  301. bool OS_LinuxBSD::is_disable_crash_handler() const {
  302. return crash_handler.is_disabled();
  303. }
  304. static String get_mountpoint(const String &p_path) {
  305. struct stat s;
  306. if (stat(p_path.utf8().get_data(), &s)) {
  307. return "";
  308. }
  309. #ifdef HAVE_MNTENT
  310. dev_t dev = s.st_dev;
  311. FILE *fd = setmntent("/proc/mounts", "r");
  312. if (!fd) {
  313. return "";
  314. }
  315. struct mntent mnt;
  316. char buf[1024];
  317. size_t buflen = 1024;
  318. while (getmntent_r(fd, &mnt, buf, buflen)) {
  319. if (!stat(mnt.mnt_dir, &s) && s.st_dev == dev) {
  320. endmntent(fd);
  321. return String(mnt.mnt_dir);
  322. }
  323. }
  324. endmntent(fd);
  325. #endif
  326. return "";
  327. }
  328. Error OS_LinuxBSD::move_to_trash(const String &p_path) {
  329. int err_code;
  330. List<String> args;
  331. args.push_back(p_path);
  332. args.push_front("trash"); // The command is `gio trash <file_name>` so we need to add it to args.
  333. Error result = execute("gio", args, nullptr, &err_code); // For GNOME based machines.
  334. if (result == OK && !err_code) {
  335. return OK;
  336. } else if (err_code == 2) {
  337. return ERR_FILE_NOT_FOUND;
  338. }
  339. args.pop_front();
  340. args.push_front("move");
  341. args.push_back("trash:/"); // The command is `kioclient5 move <file_name> trash:/`.
  342. result = execute("kioclient5", args, nullptr, &err_code); // For KDE based machines.
  343. if (result == OK && !err_code) {
  344. return OK;
  345. } else if (err_code == 2) {
  346. return ERR_FILE_NOT_FOUND;
  347. }
  348. args.pop_front();
  349. args.pop_back();
  350. result = execute("gvfs-trash", args, nullptr, &err_code); // For older Linux machines.
  351. if (result == OK && !err_code) {
  352. return OK;
  353. } else if (err_code == 2) {
  354. return ERR_FILE_NOT_FOUND;
  355. }
  356. // If the commands `kioclient5`, `gio` or `gvfs-trash` don't exist on the system we do it manually.
  357. String trash_path = "";
  358. String mnt = get_mountpoint(p_path);
  359. // If there is a directory "[Mountpoint]/.Trash-[UID], use it as the trash can.
  360. if (mnt != "") {
  361. String path(mnt + "/.Trash-" + itos(getuid()));
  362. struct stat s;
  363. if (!stat(path.utf8().get_data(), &s)) {
  364. trash_path = path;
  365. }
  366. }
  367. // Otherwise, if ${XDG_DATA_HOME} is defined, use "${XDG_DATA_HOME}/Trash" as the trash can.
  368. if (trash_path == "") {
  369. char *dhome = getenv("XDG_DATA_HOME");
  370. if (dhome) {
  371. trash_path = String(dhome) + "/Trash";
  372. }
  373. }
  374. // Otherwise, if ${HOME} is defined, use "${HOME}/.local/share/Trash" as the trash can.
  375. if (trash_path == "") {
  376. char *home = getenv("HOME");
  377. if (home) {
  378. trash_path = String(home) + "/.local/share/Trash";
  379. }
  380. }
  381. // Issue an error if none of the previous locations is appropriate for the trash can.
  382. ERR_FAIL_COND_V_MSG(trash_path == "", FAILED, "Could not determine the trash can location");
  383. // Create needed directories for decided trash can location.
  384. {
  385. DirAccess *dir_access = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  386. Error err = dir_access->make_dir_recursive(trash_path);
  387. // Issue an error if trash can is not created proprely.
  388. ERR_FAIL_COND_V_MSG(err != OK, err, "Could not create the trash path \"" + trash_path + "\"");
  389. err = dir_access->make_dir_recursive(trash_path + "/files");
  390. ERR_FAIL_COND_V_MSG(err != OK, err, "Could not create the trash path \"" + trash_path + "\"/files");
  391. err = dir_access->make_dir_recursive(trash_path + "/info");
  392. ERR_FAIL_COND_V_MSG(err != OK, err, "Could not create the trash path \"" + trash_path + "\"/info");
  393. memdelete(dir_access);
  394. }
  395. // The trash can is successfully created, now we check that we don't exceed our file name length limit.
  396. // If the file name is too long trim it so we can add the identifying number and ".trashinfo".
  397. // Assumes that the file name length limit is 255 characters.
  398. String file_name = basename(p_path.utf8().get_data());
  399. if (file_name.length() > 240) {
  400. file_name = file_name.substr(0, file_name.length() - 15);
  401. }
  402. String dest_path = trash_path + "/files/" + file_name;
  403. struct stat buff;
  404. int id_number = 0;
  405. String fn = file_name;
  406. // Checks if a resource with the same name already exist in the trash can,
  407. // if there is, add an identifying number to our resource's name.
  408. while (stat(dest_path.utf8().get_data(), &buff) == 0) {
  409. id_number++;
  410. // Added a limit to check for identically named files already on the trash can
  411. // if there are too many it could make the editor unresponsive.
  412. ERR_FAIL_COND_V_MSG(id_number > 99, FAILED, "Too many identically named resources already in the trash can.");
  413. fn = file_name + "." + itos(id_number);
  414. dest_path = trash_path + "/files/" + fn;
  415. }
  416. file_name = fn;
  417. // Generates the .trashinfo file
  418. OS::Date date = OS::get_singleton()->get_date(false);
  419. OS::Time time = OS::get_singleton()->get_time(false);
  420. String timestamp = vformat("%04d-%02d-%02dT%02d:%02d:", date.year, (int)date.month, date.day, time.hour, time.minute);
  421. timestamp = vformat("%s%02d", timestamp, time.second); // vformat only supports up to 6 arguments.
  422. String trash_info = "[Trash Info]\nPath=" + p_path.uri_encode() + "\nDeletionDate=" + timestamp + "\n";
  423. {
  424. Error err;
  425. FileAccess *file = FileAccess::open(trash_path + "/info/" + file_name + ".trashinfo", FileAccess::WRITE, &err);
  426. ERR_FAIL_COND_V_MSG(err != OK, err, "Can't create trashinfo file:" + trash_path + "/info/" + file_name + ".trashinfo");
  427. file->store_string(trash_info);
  428. file->close();
  429. // Rename our resource before moving it to the trash can.
  430. DirAccess *dir_access = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  431. err = dir_access->rename(p_path, p_path.get_base_dir() + "/" + file_name);
  432. ERR_FAIL_COND_V_MSG(err != OK, err, "Can't rename file \"" + p_path + "\"");
  433. memdelete(dir_access);
  434. }
  435. // Move the given resource to the trash can.
  436. // Do not use DirAccess:rename() because it can't move files across multiple mountpoints.
  437. List<String> mv_args;
  438. mv_args.push_back(p_path.get_base_dir() + "/" + file_name);
  439. mv_args.push_back(trash_path + "/files");
  440. {
  441. int retval;
  442. Error err = execute("mv", mv_args, nullptr, &retval);
  443. // Issue an error if "mv" failed to move the given resource to the trash can.
  444. if (err != OK || retval != 0) {
  445. ERR_PRINT("move_to_trash: Could not move the resource \"" + p_path + "\" to the trash can \"" + trash_path + "/files\"");
  446. DirAccess *dir_access = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  447. err = dir_access->rename(p_path.get_base_dir() + "/" + file_name, p_path);
  448. memdelete(dir_access);
  449. 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);
  450. return FAILED;
  451. }
  452. }
  453. return OK;
  454. }
  455. OS_LinuxBSD::OS_LinuxBSD() {
  456. main_loop = nullptr;
  457. force_quit = false;
  458. #ifdef PULSEAUDIO_ENABLED
  459. AudioDriverManager::add_driver(&driver_pulseaudio);
  460. #endif
  461. #ifdef ALSA_ENABLED
  462. AudioDriverManager::add_driver(&driver_alsa);
  463. #endif
  464. #ifdef X11_ENABLED
  465. DisplayServerX11::register_x11_driver();
  466. #endif
  467. }