os_unix.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**************************************************************************/
  2. /* os_unix.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. #pragma once
  31. #ifdef UNIX_ENABLED
  32. #include "core/os/os.h"
  33. #include "drivers/unix/ip_unix.h"
  34. #if defined(__GLIBC__) || defined(WEB_ENABLED)
  35. #include <iconv.h>
  36. #include <langinfo.h>
  37. #define gd_iconv_t iconv_t
  38. #define gd_iconv_open iconv_open
  39. #define gd_iconv iconv
  40. #define gd_iconv_close iconv_close
  41. #else
  42. typedef void *gd_iconv_t;
  43. typedef gd_iconv_t (*PIConvOpen)(const char *, const char *);
  44. typedef size_t (*PIConv)(gd_iconv_t, char **, size_t *, char **, size_t *);
  45. typedef int (*PIConvClose)(gd_iconv_t);
  46. typedef const char *(*PIConvLocaleCharset)(void);
  47. #endif
  48. class OS_Unix : public OS {
  49. struct ProcessInfo {
  50. mutable bool is_running = true;
  51. mutable int exit_code = -1;
  52. };
  53. HashMap<ProcessID, ProcessInfo> *process_map = nullptr;
  54. Mutex process_map_mutex;
  55. #if defined(__GLIBC__) || defined(WEB_ENABLED)
  56. bool _iconv_ok = true;
  57. #else
  58. bool _iconv_ok = false;
  59. PIConvOpen gd_iconv_open = nullptr;
  60. PIConv gd_iconv = nullptr;
  61. PIConvClose gd_iconv_close = nullptr;
  62. PIConvLocaleCharset gd_locale_charset = nullptr;
  63. void _load_iconv();
  64. #endif
  65. static int _wait_for_pid_completion(const pid_t p_pid, int *r_status, int p_options);
  66. bool _check_pid_is_running(const pid_t p_pid, int *r_status) const;
  67. protected:
  68. // UNIX only handles the core functions.
  69. // inheriting platforms under unix (eg. X11) should handle the rest
  70. virtual void initialize_core();
  71. virtual int unix_initialize_audio(int p_audio_driver);
  72. virtual void finalize_core() override;
  73. public:
  74. OS_Unix();
  75. virtual Vector<String> get_video_adapter_driver_info() const override;
  76. virtual String get_stdin_string(int64_t p_buffer_size = 1024) override;
  77. virtual PackedByteArray get_stdin_buffer(int64_t p_buffer_size = 1024) override;
  78. virtual StdHandleType get_stdin_type() const override;
  79. virtual StdHandleType get_stdout_type() const override;
  80. virtual StdHandleType get_stderr_type() const override;
  81. virtual Error get_entropy(uint8_t *r_buffer, int p_bytes) override;
  82. virtual Error open_dynamic_library(const String &p_path, void *&p_library_handle, GDExtensionData *p_data = nullptr) override;
  83. virtual Error close_dynamic_library(void *p_library_handle) override;
  84. virtual Error get_dynamic_library_symbol_handle(void *p_library_handle, const String &p_name, void *&p_symbol_handle, bool p_optional = false) override;
  85. virtual Error set_cwd(const String &p_cwd) override;
  86. virtual String get_name() const override;
  87. virtual String get_distribution_name() const override;
  88. virtual String get_version() const override;
  89. virtual String get_temp_path() const override;
  90. virtual DateTime get_datetime(bool p_utc) const override;
  91. virtual TimeZoneInfo get_time_zone_info() const override;
  92. virtual double get_unix_time() const override;
  93. virtual void delay_usec(uint32_t p_usec) const override;
  94. virtual uint64_t get_ticks_usec() const override;
  95. virtual Dictionary get_memory_info() const override;
  96. virtual String multibyte_to_string(const String &p_encoding, const PackedByteArray &p_array) const override;
  97. virtual PackedByteArray string_to_multibyte(const String &p_encoding, const String &p_string) const override;
  98. virtual Error execute(const String &p_path, const List<String> &p_arguments, String *r_pipe = nullptr, int *r_exitcode = nullptr, bool read_stderr = false, Mutex *p_pipe_mutex = nullptr, bool p_open_console = false) override;
  99. virtual Dictionary execute_with_pipe(const String &p_path, const List<String> &p_arguments, bool p_blocking = true) override;
  100. virtual Error create_process(const String &p_path, const List<String> &p_arguments, ProcessID *r_child_id = nullptr, bool p_open_console = false) override;
  101. virtual Error kill(const ProcessID &p_pid) override;
  102. virtual int get_process_id() const override;
  103. virtual bool is_process_running(const ProcessID &p_pid) const override;
  104. virtual int get_process_exit_code(const ProcessID &p_pid) const override;
  105. virtual bool has_environment(const String &p_var) const override;
  106. virtual String get_environment(const String &p_var) const override;
  107. virtual void set_environment(const String &p_var, const String &p_value) const override;
  108. virtual void unset_environment(const String &p_var) const override;
  109. virtual String get_locale() const override;
  110. virtual void initialize_debugging() override;
  111. virtual String get_executable_path() const override;
  112. virtual String get_user_data_dir(const String &p_user_dir) const override;
  113. };
  114. class UnixTerminalLogger : public StdLogger {
  115. public:
  116. virtual void log_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, bool p_editor_notify = false, ErrorType p_type = ERR_ERROR, const Vector<Ref<ScriptBacktrace>> &p_script_backtraces = {}) override;
  117. virtual ~UnixTerminalLogger();
  118. };
  119. #endif // UNIX_ENABLED