diff options
Diffstat (limited to 'drivers/unix/os_unix.cpp')
-rw-r--r-- | drivers/unix/os_unix.cpp | 199 |
1 files changed, 85 insertions, 114 deletions
diff --git a/drivers/unix/os_unix.cpp b/drivers/unix/os_unix.cpp index c5eb343cc8..96c338f86b 100644 --- a/drivers/unix/os_unix.cpp +++ b/drivers/unix/os_unix.cpp @@ -32,23 +32,23 @@ #ifdef UNIX_ENABLED +#include "core/debugger/engine_debugger.h" +#include "core/debugger/script_debugger.h" #include "core/os/thread_dummy.h" #include "core/project_settings.h" #include "drivers/unix/dir_access_unix.h" #include "drivers/unix/file_access_unix.h" -#include "drivers/unix/mutex_posix.h" #include "drivers/unix/net_socket_posix.h" #include "drivers/unix/rw_lock_posix.h" -#include "drivers/unix/semaphore_posix.h" #include "drivers/unix/thread_posix.h" -#include "servers/visual_server.h" +#include "servers/rendering_server.h" #ifdef __APPLE__ #include <mach-o/dyld.h> #include <mach/mach_time.h> #endif -#if defined(__FreeBSD__) || defined(__OpenBSD__) +#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) #include <sys/param.h> #include <sys/sysctl.h> #endif @@ -91,46 +91,37 @@ static void _setup_clock() { #endif void OS_Unix::debug_break() { - assert(false); }; static void handle_interrupt(int sig) { - if (ScriptDebugger::get_singleton() == NULL) + if (!EngineDebugger::is_active()) { return; + } - ScriptDebugger::get_singleton()->set_depth(-1); - ScriptDebugger::get_singleton()->set_lines_left(1); + EngineDebugger::get_script_debugger()->set_depth(-1); + EngineDebugger::get_script_debugger()->set_lines_left(1); } void OS_Unix::initialize_debugging() { - - if (ScriptDebugger::get_singleton() != NULL) { + if (EngineDebugger::is_active()) { struct sigaction action; memset(&action, 0, sizeof(action)); action.sa_handler = handle_interrupt; - sigaction(SIGINT, &action, NULL); + sigaction(SIGINT, &action, nullptr); } } int OS_Unix::unix_initialize_audio(int p_audio_driver) { - return 0; } void OS_Unix::initialize_core() { - #ifdef NO_THREADS ThreadDummy::make_default(); - SemaphoreDummy::make_default(); - MutexDummy::make_default(); RWLockDummy::make_default(); #else ThreadPosix::make_default(); -#if !defined(OSX_ENABLED) && !defined(IPHONE_ENABLED) - SemaphorePosix::make_default(); -#endif - MutexPosix::make_default(); RWLockPosix::make_default(); #endif FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_RESOURCES); @@ -150,17 +141,14 @@ void OS_Unix::initialize_core() { } void OS_Unix::finalize_core() { - NetSocketPosix::cleanup(); } void OS_Unix::alert(const String &p_alert, const String &p_title) { - fprintf(stderr, "ERROR: %s\n", p_alert.utf8().get_data()); } String OS_Unix::get_stdin_string(bool p_block) { - if (p_block) { char buff[1024]; String ret = stdin_buf + fgets(buff, 1024, stdin); @@ -172,74 +160,64 @@ String OS_Unix::get_stdin_string(bool p_block) { } String OS_Unix::get_name() const { - return "Unix"; } -uint64_t OS_Unix::get_unix_time() const { - - return time(NULL); -}; - -uint64_t OS_Unix::get_system_time_secs() const { +double OS_Unix::get_unix_time() const { struct timeval tv_now; - gettimeofday(&tv_now, NULL); - return uint64_t(tv_now.tv_sec); -} - -uint64_t OS_Unix::get_system_time_msecs() const { - struct timeval tv_now; - gettimeofday(&tv_now, NULL); - return uint64_t(tv_now.tv_sec) * 1000 + uint64_t(tv_now.tv_usec) / 1000; -} + gettimeofday(&tv_now, nullptr); + return (double)tv_now.tv_sec + double(tv_now.tv_usec) / 1000000; +}; OS::Date OS_Unix::get_date(bool utc) const { - - time_t t = time(NULL); - struct tm *lt; - if (utc) - lt = gmtime(&t); - else - lt = localtime(&t); + time_t t = time(nullptr); + struct tm lt; + if (utc) { + gmtime_r(&t, <); + } else { + localtime_r(&t, <); + } Date ret; - ret.year = 1900 + lt->tm_year; + ret.year = 1900 + lt.tm_year; // Index starting at 1 to match OS_Unix::get_date // and Windows SYSTEMTIME and tm_mon follows the typical structure // of 0-11, noted here: http://www.cplusplus.com/reference/ctime/tm/ - ret.month = (Month)(lt->tm_mon + 1); - ret.day = lt->tm_mday; - ret.weekday = (Weekday)lt->tm_wday; - ret.dst = lt->tm_isdst; + ret.month = (Month)(lt.tm_mon + 1); + ret.day = lt.tm_mday; + ret.weekday = (Weekday)lt.tm_wday; + ret.dst = lt.tm_isdst; return ret; } OS::Time OS_Unix::get_time(bool utc) const { - time_t t = time(NULL); - struct tm *lt; - if (utc) - lt = gmtime(&t); - else - lt = localtime(&t); + time_t t = time(nullptr); + struct tm lt; + if (utc) { + gmtime_r(&t, <); + } else { + localtime_r(&t, <); + } Time ret; - ret.hour = lt->tm_hour; - ret.min = lt->tm_min; - ret.sec = lt->tm_sec; + ret.hour = lt.tm_hour; + ret.min = lt.tm_min; + ret.sec = lt.tm_sec; get_time_zone_info(); return ret; } OS::TimeZoneInfo OS_Unix::get_time_zone_info() const { - time_t t = time(NULL); - struct tm *lt = localtime(&t); + time_t t = time(nullptr); + struct tm lt; + localtime_r(&t, <); char name[16]; - strftime(name, 16, "%Z", lt); + strftime(name, 16, "%Z", <); name[15] = 0; TimeZoneInfo ret; ret.name = name; char bias_buf[16]; - strftime(bias_buf, 16, "%z", lt); + strftime(bias_buf, 16, "%z", <); int bias; bias_buf[15] = 0; sscanf(bias_buf, "%d", &bias); @@ -247,22 +225,22 @@ OS::TimeZoneInfo OS_Unix::get_time_zone_info() const { // convert from ISO 8601 (1 minute=1, 1 hour=100) to minutes int hour = (int)bias / 100; int minutes = bias % 100; - if (bias < 0) + if (bias < 0) { ret.bias = hour * 60 - minutes; - else + } else { ret.bias = hour * 60 + minutes; + } return ret; } void OS_Unix::delay_usec(uint32_t p_usec) const { - struct timespec rem = { static_cast<time_t>(p_usec / 1000000), (static_cast<long>(p_usec) % 1000000) * 1000 }; while (nanosleep(&rem, &rem) == EINTR) { } } -uint64_t OS_Unix::get_ticks_usec() const { +uint64_t OS_Unix::get_ticks_usec() const { #if defined(__APPLE__) uint64_t longtime = mach_absolute_time() * _clock_scale; #else @@ -278,19 +256,16 @@ uint64_t OS_Unix::get_ticks_usec() const { } Error OS_Unix::execute(const String &p_path, const List<String> &p_arguments, bool p_blocking, ProcessID *r_child_id, String *r_pipe, int *r_exitcode, bool read_stderr, Mutex *p_pipe_mutex) { - #ifdef __EMSCRIPTEN__ // Don't compile this code at all to avoid undefined references. // Actual virtual call goes to OS_JavaScript. ERR_FAIL_V(ERR_BUG); #else if (p_blocking && r_pipe) { - String argss; argss = "\"" + p_path + "\""; for (int i = 0; i < p_arguments.size(); i++) { - argss += String(" \"") + p_arguments[i] + "\""; } @@ -306,18 +281,18 @@ Error OS_Unix::execute(const String &p_path, const List<String> &p_arguments, bo char buf[65535]; while (fgets(buf, 65535, f)) { - if (p_pipe_mutex) { p_pipe_mutex->lock(); } - (*r_pipe) += buf; + (*r_pipe) += String::utf8(buf); if (p_pipe_mutex) { p_pipe_mutex->unlock(); } } int rv = pclose(f); - if (r_exitcode) + if (r_exitcode) { *r_exitcode = WEXITSTATUS(rv); + } return OK; } @@ -336,31 +311,33 @@ Error OS_Unix::execute(const String &p_path, const List<String> &p_arguments, bo Vector<CharString> cs; cs.push_back(p_path.utf8()); - for (int i = 0; i < p_arguments.size(); i++) + for (int i = 0; i < p_arguments.size(); i++) { cs.push_back(p_arguments[i].utf8()); + } Vector<char *> args; - for (int i = 0; i < cs.size(); i++) + for (int i = 0; i < cs.size(); i++) { args.push_back((char *)cs[i].get_data()); + } args.push_back(0); execvp(p_path.utf8().get_data(), &args[0]); // still alive? something failed.. fprintf(stderr, "**ERROR** OS_Unix::execute - Could not create child process while executing: %s\n", p_path.utf8().get_data()); - abort(); + raise(SIGKILL); } if (p_blocking) { - int status; waitpid(pid, &status, 0); - if (r_exitcode) + if (r_exitcode) { *r_exitcode = WEXITSTATUS(status); + } } else { - - if (r_child_id) + if (r_child_id) { *r_child_id = pid; + } } return OK; @@ -368,7 +345,6 @@ Error OS_Unix::execute(const String &p_path, const List<String> &p_arguments, bo } Error OS_Unix::kill(const ProcessID &p_pid) { - int ret = ::kill(p_pid, SIGKILL); if (!ret) { //avoid zombie process @@ -379,29 +355,27 @@ Error OS_Unix::kill(const ProcessID &p_pid) { } int OS_Unix::get_process_id() const { - return getpid(); }; bool OS_Unix::has_environment(const String &p_var) const { - - return getenv(p_var.utf8().get_data()) != NULL; + return getenv(p_var.utf8().get_data()) != nullptr; } String OS_Unix::get_locale() const { - - if (!has_environment("LANG")) + if (!has_environment("LANG")) { return "en"; + } String locale = get_environment("LANG"); int tp = locale.find("."); - if (tp != -1) + if (tp != -1) { locale = locale.substr(0, tp); + } return locale; } Error OS_Unix::open_dynamic_library(const String p_path, void *&p_library_handle, bool p_also_set_library_path) { - String path = p_path; if (FileAccess::exists(path) && path.is_rel_path()) { @@ -439,7 +413,7 @@ Error OS_Unix::get_dynamic_library_symbol_handle(void *p_library_handle, const S p_symbol_handle = dlsym(p_library_handle, p_name.utf8().get_data()); error = dlerror(); - if (error != NULL) { + if (error != nullptr) { ERR_FAIL_COND_V_MSG(!p_optional, ERR_CANT_RESOLVE, "Can't resolve symbol " + p_name + ". Error: " + error + "."); return ERR_CANT_RESOLVE; @@ -448,32 +422,29 @@ Error OS_Unix::get_dynamic_library_symbol_handle(void *p_library_handle, const S } Error OS_Unix::set_cwd(const String &p_cwd) { - - if (chdir(p_cwd.utf8().get_data()) != 0) + if (chdir(p_cwd.utf8().get_data()) != 0) { return ERR_CANT_OPEN; + } return OK; } String OS_Unix::get_environment(const String &p_var) const { - - if (getenv(p_var.utf8().get_data())) + if (getenv(p_var.utf8().get_data())) { return getenv(p_var.utf8().get_data()); + } return ""; } bool OS_Unix::set_environment(const String &p_var, const String &p_value) const { - return setenv(p_var.utf8().get_data(), p_value.utf8().get_data(), /* overwrite: */ true) == 0; } int OS_Unix::get_processor_count() const { - return sysconf(_SC_NPROCESSORS_CONF); } String OS_Unix::get_user_data_dir() const { - String appname = get_safe_dir_name(ProjectSettings::get_singleton()->get("application/config/name")); if (appname != "") { bool use_custom_dir = ProjectSettings::get_singleton()->get("application/config/use_custom_user_dir"); @@ -492,7 +463,6 @@ String OS_Unix::get_user_data_dir() const { } String OS_Unix::get_executable_path() const { - #ifdef __linux__ //fix for running from a symlink char buf[256]; @@ -507,7 +477,7 @@ String OS_Unix::get_executable_path() const { return OS::get_executable_path(); } return b; -#elif defined(__OpenBSD__) +#elif defined(__OpenBSD__) || defined(__NetBSD__) char resolved_path[MAXPATHLEN]; realpath(OS::get_executable_path().utf8().get_data(), resolved_path); @@ -517,7 +487,7 @@ String OS_Unix::get_executable_path() const { int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 }; char buf[MAXPATHLEN]; size_t len = sizeof(buf); - if (sysctl(mib, 4, buf, &len, NULL, 0) != 0) { + if (sysctl(mib, 4, buf, &len, nullptr, 0) != 0) { WARN_PRINT("Couldn't get executable path from sysctl"); return OS::get_executable_path(); } @@ -550,43 +520,44 @@ void UnixTerminalLogger::log_error(const char *p_function, const char *p_file, i } const char *err_details; - if (p_rationale && p_rationale[0]) + if (p_rationale && p_rationale[0]) { err_details = p_rationale; - else + } else { err_details = p_code; + } // Disable color codes if stdout is not a TTY. // This prevents Godot from writing ANSI escape codes when redirecting // stdout and stderr to a file. const bool tty = isatty(fileno(stdout)); - const char *red = tty ? "\E[0;31m" : ""; + const char *gray = tty ? "\E[0;90m" : ""; + const char *red = tty ? "\E[0;91m" : ""; const char *red_bold = tty ? "\E[1;31m" : ""; - const char *yellow = tty ? "\E[0;33m" : ""; + const char *yellow = tty ? "\E[0;93m" : ""; const char *yellow_bold = tty ? "\E[1;33m" : ""; - const char *magenta = tty ? "\E[0;35m" : ""; + const char *magenta = tty ? "\E[0;95m" : ""; const char *magenta_bold = tty ? "\E[1;35m" : ""; - const char *cyan = tty ? "\E[0;36m" : ""; + const char *cyan = tty ? "\E[0;96m" : ""; const char *cyan_bold = tty ? "\E[1;36m" : ""; const char *reset = tty ? "\E[0m" : ""; - const char *bold = tty ? "\E[1m" : ""; switch (p_type) { case ERR_WARNING: - logf_error("%sWARNING: %s: %s%s%s\n", yellow_bold, p_function, reset, bold, err_details); - logf_error("%s At: %s:%i.%s\n", yellow, p_file, p_line, reset); + logf_error("%sWARNING:%s %s\n", yellow_bold, yellow, err_details); + logf_error("%s at: %s (%s:%i)%s\n", gray, p_function, p_file, p_line, reset); break; case ERR_SCRIPT: - logf_error("%sSCRIPT ERROR: %s: %s%s%s\n", magenta_bold, p_function, reset, bold, err_details); - logf_error("%s At: %s:%i.%s\n", magenta, p_file, p_line, reset); + logf_error("%sSCRIPT ERROR:%s %s\n", magenta_bold, magenta, err_details); + logf_error("%s at: %s (%s:%i)%s\n", gray, p_function, p_file, p_line, reset); break; case ERR_SHADER: - logf_error("%sSHADER ERROR: %s: %s%s%s\n", cyan_bold, p_function, reset, bold, err_details); - logf_error("%s At: %s:%i.%s\n", cyan, p_file, p_line, reset); + logf_error("%sSHADER ERROR:%s %s\n", cyan_bold, cyan, err_details); + logf_error("%s at: %s (%s:%i)%s\n", gray, p_function, p_file, p_line, reset); break; case ERR_ERROR: default: - logf_error("%sERROR: %s: %s%s%s\n", red_bold, p_function, reset, bold, err_details); - logf_error("%s At: %s:%i.%s\n", red, p_file, p_line, reset); + logf_error("%sERROR:%s %s\n", red_bold, red, err_details); + logf_error("%s at: %s (%s:%i)%s\n", gray, p_function, p_file, p_line, reset); break; } } |