Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/drivers/unix/os_unix.h
9903 views
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
31
#pragma once
32
33
#ifdef UNIX_ENABLED
34
35
#include "core/os/os.h"
36
#include "drivers/unix/ip_unix.h"
37
38
#if defined(__GLIBC__) || defined(WEB_ENABLED)
39
#include <iconv.h>
40
#include <langinfo.h>
41
#define gd_iconv_t iconv_t
42
#define gd_iconv_open iconv_open
43
#define gd_iconv iconv
44
#define gd_iconv_close iconv_close
45
#else
46
typedef void *gd_iconv_t;
47
typedef gd_iconv_t (*PIConvOpen)(const char *, const char *);
48
typedef size_t (*PIConv)(gd_iconv_t, char **, size_t *, char **, size_t *);
49
typedef int (*PIConvClose)(gd_iconv_t);
50
typedef const char *(*PIConvLocaleCharset)(void);
51
#endif
52
53
class OS_Unix : public OS {
54
struct ProcessInfo {
55
mutable bool is_running = true;
56
mutable int exit_code = -1;
57
};
58
HashMap<ProcessID, ProcessInfo> *process_map = nullptr;
59
Mutex process_map_mutex;
60
61
#if defined(__GLIBC__) || defined(WEB_ENABLED)
62
bool _iconv_ok = true;
63
#else
64
bool _iconv_ok = false;
65
66
PIConvOpen gd_iconv_open = nullptr;
67
PIConv gd_iconv = nullptr;
68
PIConvClose gd_iconv_close = nullptr;
69
PIConvLocaleCharset gd_locale_charset = nullptr;
70
71
void _load_iconv();
72
#endif
73
74
static int _wait_for_pid_completion(const pid_t p_pid, int *r_status, int p_options, pid_t *r_pid = nullptr);
75
bool _check_pid_is_running(const pid_t p_pid, int *r_status) const;
76
77
protected:
78
// UNIX only handles the core functions.
79
// inheriting platforms under unix (eg. X11) should handle the rest
80
81
virtual void initialize_core();
82
virtual int unix_initialize_audio(int p_audio_driver);
83
84
virtual void finalize_core() override;
85
86
public:
87
OS_Unix();
88
89
virtual Vector<String> get_video_adapter_driver_info() const override;
90
91
virtual String get_stdin_string(int64_t p_buffer_size = 1024) override;
92
virtual PackedByteArray get_stdin_buffer(int64_t p_buffer_size = 1024) override;
93
virtual StdHandleType get_stdin_type() const override;
94
virtual StdHandleType get_stdout_type() const override;
95
virtual StdHandleType get_stderr_type() const override;
96
97
virtual Error get_entropy(uint8_t *r_buffer, int p_bytes) override;
98
99
virtual Error open_dynamic_library(const String &p_path, void *&p_library_handle, GDExtensionData *p_data = nullptr) override;
100
virtual Error close_dynamic_library(void *p_library_handle) override;
101
virtual Error get_dynamic_library_symbol_handle(void *p_library_handle, const String &p_name, void *&p_symbol_handle, bool p_optional = false) override;
102
103
virtual Error set_cwd(const String &p_cwd) override;
104
105
virtual String get_name() const override;
106
virtual String get_distribution_name() const override;
107
virtual String get_version() const override;
108
109
virtual String get_temp_path() const override;
110
111
virtual DateTime get_datetime(bool p_utc) const override;
112
virtual TimeZoneInfo get_time_zone_info() const override;
113
114
virtual double get_unix_time() const override;
115
116
virtual void delay_usec(uint32_t p_usec) const override;
117
virtual uint64_t get_ticks_usec() const override;
118
119
virtual Dictionary get_memory_info() const override;
120
121
virtual String multibyte_to_string(const String &p_encoding, const PackedByteArray &p_array) const override;
122
virtual PackedByteArray string_to_multibyte(const String &p_encoding, const String &p_string) const override;
123
124
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;
125
virtual Dictionary execute_with_pipe(const String &p_path, const List<String> &p_arguments, bool p_blocking = true) override;
126
virtual Error create_process(const String &p_path, const List<String> &p_arguments, ProcessID *r_child_id = nullptr, bool p_open_console = false) override;
127
virtual Error kill(const ProcessID &p_pid) override;
128
virtual int get_process_id() const override;
129
virtual bool is_process_running(const ProcessID &p_pid) const override;
130
virtual int get_process_exit_code(const ProcessID &p_pid) const override;
131
132
virtual bool has_environment(const String &p_var) const override;
133
virtual String get_environment(const String &p_var) const override;
134
virtual void set_environment(const String &p_var, const String &p_value) const override;
135
virtual void unset_environment(const String &p_var) const override;
136
137
virtual String get_locale() const override;
138
139
virtual void initialize_debugging() override;
140
141
virtual String get_executable_path() const override;
142
virtual String get_user_data_dir(const String &p_user_dir) const override;
143
};
144
145
class UnixTerminalLogger : public StdLogger {
146
public:
147
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;
148
virtual ~UnixTerminalLogger();
149
};
150
151
#endif // UNIX_ENABLED
152
153