16 #define GETTEXT_DOMAIN "wesnoth-lib"
28 #if defined(__APPLE__) || defined(_X11)
29 #include <sys/utsname.h>
32 #if defined(__APPLE__)
38 #include <boost/algorithm/string/trim.hpp>
46 #include <boost/algorithm/string/trim.hpp>
54 #define WIN32_LEAN_AND_MEAN
61 #include <SDL2/SDL_system.h>
65 #define ERR_DU LOG_STREAM(err, log_desktop)
66 #define LOG_DU LOG_STREAM(info, log_desktop)
83 HMODULE ntdll = GetModuleHandle(L
"ntdll.dll");
88 return GetProcAddress(ntdll,
"wine_get_version") !=
nullptr;
98 std::string windows_release_id()
101 DWORD
size =
sizeof(buf);
103 auto res = RegGetValueA(HKEY_LOCAL_MACHINE,
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion",
"DisplayVersion", RRF_RT_REG_SZ,
nullptr, buf, &
size);
104 if(res != ERROR_SUCCESS) {
105 res = RegGetValueA(HKEY_LOCAL_MACHINE,
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion",
"ReleaseId", RRF_RT_REG_SZ,
nullptr, buf, &
size);
108 return std::string{res == ERROR_SUCCESS ? buf :
""};
111 std::string windows_runtime_arch()
114 SecureZeroMemory(&si,
sizeof(SYSTEM_INFO));
115 GetNativeSystemInfo(&si);
117 switch(si.wProcessorArchitecture) {
118 case PROCESSOR_ARCHITECTURE_INTEL:
120 case PROCESSOR_ARCHITECTURE_AMD64:
122 case PROCESSOR_ARCHITECTURE_ARM:
124 case PROCESSOR_ARCHITECTURE_ARM64:
126 case PROCESSOR_ARCHITECTURE_IA64:
129 return _(
"cpu_architecture^<unknown>");
139 struct posix_pipe_release_policy
141 void operator()(std::FILE*
f)
const {
if(
f !=
nullptr) { pclose(
f); } }
150 typedef std::unique_ptr<std::FILE, posix_pipe_release_policy> scoped_posix_pipe;
157 std::string read_pipe_line(scoped_posix_pipe&
p)
169 while((
c = std::fgetc(
p.get())) &&
c != EOF &&
c !=
'\n' &&
c !=
'\r') {
170 ver.push_back(
static_cast<char>(
c));
176 std::map<std::string, std::string> parse_fdo_osrelease(
const std::string&
path)
183 std::map<std::string, std::string> res;
188 for(std::string
s; std::getline(*
in,
s);) {
189 if(
s.empty() ||
s.front() ==
'#') {
193 auto eqsign_pos =
s.find(
'=');
194 if(!eqsign_pos || eqsign_pos == std::string::npos) {
198 auto lhs =
s.substr(0, eqsign_pos),
199 rhs = eqsign_pos + 1 <
s.length() ?
utils::unescape(
s.substr(eqsign_pos + 1)) :
"";
205 if(rhs.length() >= 2 && rhs.front() ==
'"' && rhs.back() ==
'"') {
210 res.emplace(std::move(lhs), std::move(rhs));
222 #if defined(__APPLE__) || defined(_X11)
227 ERR_DU <<
"os_version: uname error (" << strerror(errno) <<
")";
231 #if defined(__APPLE__)
245 std::map<std::string, std::string> osrel;
247 static const std::string fdo_osrel_etc =
"/etc/os-release";
248 static const std::string fdo_osrel_usr =
"/usr/lib/os-release";
251 osrel = parse_fdo_osrelease(fdo_osrel_etc);
253 osrel = parse_fdo_osrelease(fdo_osrel_usr);
257 auto osrel_distname = osrel[
"PRETTY_NAME"];
258 if(osrel_distname.empty()) {
259 osrel_distname = osrel[
"NAME"];
262 if(!osrel_distname.empty()) {
263 return osrel_distname +
" " + u.machine;
270 static const std::string lsb_release_bin =
"/usr/bin/lsb_release";
273 static const std::string cmdline = lsb_release_bin +
" -s -d";
275 scoped_posix_pipe
p(popen(cmdline.c_str(),
"r"));
276 std::string ver = read_pipe_line(
p);
278 if(ver.length() >= 2 && ver[0] ==
'"' && ver[ver.length() - 1] ==
'"') {
279 ver.erase(ver.length() - 1, 1);
285 return ver +
" " + u.machine;
298 #elif defined(_WIN32)
304 static const std::string base
305 = !on_wine() ?
"Microsoft Windows" :
"Wine/Microsoft Windows";
309 SecureZeroMemory(&v,
sizeof(OSVERSIONINFOEX));
310 v.dwOSVersionInfoSize =
sizeof(OSVERSIONINFOEX);
316 #pragma warning(push)
317 #pragma warning(disable:4996)
319 if(!GetVersionEx(
reinterpret_cast<OSVERSIONINFO*
>(&v))) {
320 ERR_DU <<
"os_version: GetVersionEx error (" << GetLastError() <<
')';
327 const DWORD vnum = v.dwMajorVersion * 100 + v.dwMinorVersion;
340 version =
"Server 2003";
343 if(v.wProductType == VER_NT_WORKSTATION) {
346 version =
"Server 2008";
350 if(v.wProductType == VER_NT_WORKSTATION) {
353 version =
"Server 2008 R2";
357 if(v.wProductType == VER_NT_WORKSTATION) {
360 version =
"Server 2012";
364 if(v.wProductType == VER_NT_WORKSTATION) {
367 version =
"Server 2012 R2";
371 if(v.wProductType == VER_NT_WORKSTATION) {
372 version = v.dwBuildNumber < 22000 ?
"10" :
"11";
373 const auto& release_id = windows_release_id();
374 if(!release_id.empty()) {
376 version += release_id;
382 if(v.wProductType != VER_NT_WORKSTATION) {
387 if(*v.szCSDVersion) {
389 version += unicode_cast<std::string>(std::wstring(v.szCSDVersion));
395 << v.dwMajorVersion <<
'.'
396 << v.dwMinorVersion <<
'.'
400 return base +
" " + version +
" " + windows_runtime_arch();
402 #elif defined(__ANDROID__)
404 return formatter() <<
"Android (API level " << SDL_GetAndroidSDKVersion() <<
")";
412 ERR_DU <<
"os_version(): unsupported platform";
413 return _(
"operating_system^<unknown>");
Platform identification and version information functions.
Declarations for File-IO.
unsigned in
If equal to search_counter, the node is off the list.
static std::string _(const char *str)
Standard logging facilities (interface).
std::string os_version()
Returns a string with the running OS name and version information.
filesystem::scoped_istream istream_file(const std::string &fname, bool treat_failure_as_error)
static bool file_exists(const bfs::path &fpath)
std::size_t size(std::string_view str)
Length in characters of a UTF-8 string.
void trim(std::string_view &s)
std::string unescape(std::string_view str)
Remove all escape characters (backslash)
static map_location::direction s
static lg::log_domain log_desktop("desktop")