Update: Translations from eints
[openttd-github.git] / src / os / unix / unix.cpp
blob1cfce1cd2b416752abb5ab06c0c9638fed355323
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
8 /** @file unix.cpp Implementation of Unix specific file handling. */
10 #include "../../stdafx.h"
11 #include "../../textbuf_gui.h"
12 #include "../../debug.h"
13 #include "../../string_func.h"
14 #include "../../fios.h"
15 #include "../../thread.h"
18 #include <dirent.h>
19 #include <unistd.h>
20 #include <sys/stat.h>
21 #include <time.h>
22 #include <signal.h>
23 #include <pthread.h>
25 #ifdef WITH_SDL2
26 #include <SDL.h>
27 #endif
29 #ifdef __EMSCRIPTEN__
30 # include <emscripten.h>
31 #endif
33 #ifdef __APPLE__
34 # include <sys/mount.h>
35 #elif (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L) || defined(__GLIBC__)
36 # define HAS_STATVFS
37 #endif
39 #if defined(OPENBSD) || defined(__NetBSD__) || defined(__FreeBSD__)
40 # define HAS_SYSCTL
41 #endif
43 #ifdef HAS_STATVFS
44 #include <sys/statvfs.h>
45 #endif
47 #ifdef HAS_SYSCTL
48 #include <sys/sysctl.h>
49 #endif
51 #if defined(__APPLE__)
52 # include "../macosx/macos.h"
53 #endif
55 #include "../../safeguards.h"
57 bool FiosIsRoot(const std::string &path)
59 return path == PATHSEP;
62 void FiosGetDrives(FileList &)
64 return;
67 std::optional<uint64_t> FiosGetDiskFreeSpace(const std::string &path)
69 #ifdef __APPLE__
70 struct statfs s;
72 if (statfs(path.c_str(), &s) == 0) return static_cast<uint64_t>(s.f_bsize) * s.f_bavail;
73 #elif defined(HAS_STATVFS)
74 struct statvfs s;
76 if (statvfs(path.c_str(), &s) == 0) return static_cast<uint64_t>(s.f_frsize) * s.f_bavail;
77 #endif
78 return std::nullopt;
81 bool FiosIsHiddenFile(const std::filesystem::path &path)
83 return path.filename().string().starts_with(".");
86 #ifdef WITH_ICONV
88 #include <iconv.h>
89 #include "../../debug.h"
90 #include "../../string_func.h"
92 const char *GetCurrentLocale(const char *param);
94 #define INTERNALCODE "UTF-8"
96 /**
97 * Try and try to decipher the current locale from environmental
98 * variables. MacOSX is hardcoded, other OS's are dynamic. If no suitable
99 * locale can be found, don't do any conversion ""
101 static const char *GetLocalCode()
103 #if defined(__APPLE__)
104 return "UTF-8-MAC";
105 #else
106 /* Strip locale (eg en_US.UTF-8) to only have UTF-8 */
107 const char *locale = GetCurrentLocale("LC_CTYPE");
108 if (locale != nullptr) locale = strchr(locale, '.');
110 return (locale == nullptr) ? "" : locale + 1;
111 #endif
115 * Convert between locales, which from and which to is set in the calling
116 * functions OTTD2FS() and FS2OTTD().
118 static std::string convert_tofrom_fs(iconv_t convd, const std::string &name)
120 /* There are different implementations of iconv. The older ones,
121 * e.g. SUSv2, pass a const pointer, whereas the newer ones, e.g.
122 * IEEE 1003.1 (2004), pass a non-const pointer. */
123 #ifdef HAVE_NON_CONST_ICONV
124 char *inbuf = const_cast<char*>(name.data());
125 #else
126 const char *inbuf = name.data();
127 #endif
129 /* If the output is UTF-32, then 1 ASCII character becomes 4 bytes. */
130 size_t inlen = name.size();
131 std::string buf(inlen * 4, '\0');
133 size_t outlen = buf.size();
134 char *outbuf = buf.data();
135 iconv(convd, nullptr, nullptr, nullptr, nullptr);
136 if (iconv(convd, &inbuf, &inlen, &outbuf, &outlen) == SIZE_MAX) {
137 Debug(misc, 0, "[iconv] error converting '{}'. Errno {}", name, errno);
138 return name;
141 buf.resize(outbuf - buf.data());
142 return buf;
146 * Convert from OpenTTD's encoding to that of the local environment
147 * @param name pointer to a valid string that will be converted
148 * @return pointer to a new stringbuffer that contains the converted string
150 std::string OTTD2FS(const std::string &name)
152 static iconv_t convd = (iconv_t)(-1);
153 if (convd == (iconv_t)(-1)) {
154 const char *env = GetLocalCode();
155 convd = iconv_open(env, INTERNALCODE);
156 if (convd == (iconv_t)(-1)) {
157 Debug(misc, 0, "[iconv] conversion from codeset '{}' to '{}' unsupported", INTERNALCODE, env);
158 return name;
162 return convert_tofrom_fs(convd, name);
166 * Convert to OpenTTD's encoding from that of the local environment
167 * @param name valid string that will be converted
168 * @return pointer to a new stringbuffer that contains the converted string
170 std::string FS2OTTD(const std::string &name)
172 static iconv_t convd = (iconv_t)(-1);
173 if (convd == (iconv_t)(-1)) {
174 const char *env = GetLocalCode();
175 convd = iconv_open(INTERNALCODE, env);
176 if (convd == (iconv_t)(-1)) {
177 Debug(misc, 0, "[iconv] conversion from codeset '{}' to '{}' unsupported", env, INTERNALCODE);
178 return name;
182 return convert_tofrom_fs(convd, name);
185 #endif /* WITH_ICONV */
187 void ShowInfoI(const std::string &str)
189 fmt::print(stderr, "{}\n", str);
192 #if !defined(__APPLE__)
193 void ShowOSErrorBox(const char *buf, bool)
195 /* All unix systems, except OSX. Only use escape codes on a TTY. */
196 if (isatty(fileno(stderr))) {
197 fmt::print(stderr, "\033[1;31mError: {}\033[0;39m\n", buf);
198 } else {
199 fmt::print(stderr, "Error: {}\n", buf);
202 #endif
204 #ifndef WITH_COCOA
205 std::optional<std::string> GetClipboardContents()
207 #ifdef WITH_SDL2
208 if (SDL_HasClipboardText() == SDL_FALSE) return std::nullopt;
210 char *clip = SDL_GetClipboardText();
211 if (clip != nullptr) {
212 std::string result = clip;
213 SDL_free(clip);
214 return result;
216 #endif
218 return std::nullopt;
220 #endif
223 #if defined(__EMSCRIPTEN__)
224 void OSOpenBrowser(const std::string &url)
226 /* Implementation in pre.js */
227 EM_ASM({ if (window["openttd_open_url"]) window.openttd_open_url($0, $1) }, url.c_str(), url.size());
229 #elif !defined( __APPLE__)
230 void OSOpenBrowser(const std::string &url)
232 pid_t child_pid = fork();
233 if (child_pid != 0) return;
235 const char *args[3];
236 args[0] = "xdg-open";
237 args[1] = url.c_str();
238 args[2] = nullptr;
239 execvp(args[0], const_cast<char * const *>(args));
240 Debug(misc, 0, "Failed to open url: {}", url);
241 exit(0);
243 #endif /* __APPLE__ */
245 void SetCurrentThreadName([[maybe_unused]] const char *threadName)
247 #if defined(__GLIBC__)
248 if (threadName) pthread_setname_np(pthread_self(), threadName);
249 #endif /* defined(__GLIBC__) */
250 #if defined(__APPLE__)
251 MacOSSetThreadName(threadName);
252 #endif /* defined(__APPLE__) */