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>
55 #define WIN32_LEAN_AND_MEAN
62 #define ERR_DU LOG_STREAM(err, log_desktop)
63 #define LOG_DU LOG_STREAM(info, log_desktop)
80 HMODULE ntdll = GetModuleHandle(L
"ntdll.dll");
85 return GetProcAddress(ntdll,
"wine_get_version") !=
nullptr;
95 std::string windows_release_id()
98 DWORD
size =
sizeof(buf);
100 auto res = RegGetValueA(HKEY_LOCAL_MACHINE,
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion",
"DisplayVersion", RRF_RT_REG_SZ,
nullptr, buf, &
size);
101 if(res != ERROR_SUCCESS) {
102 res = RegGetValueA(HKEY_LOCAL_MACHINE,
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion",
"ReleaseId", RRF_RT_REG_SZ,
nullptr, buf, &
size);
105 return std::string{res == ERROR_SUCCESS ? buf :
""};
108 std::string windows_runtime_arch()
111 SecureZeroMemory(&si,
sizeof(SYSTEM_INFO));
112 GetNativeSystemInfo(&si);
114 switch(si.wProcessorArchitecture) {
115 case PROCESSOR_ARCHITECTURE_INTEL:
117 case PROCESSOR_ARCHITECTURE_AMD64:
119 case PROCESSOR_ARCHITECTURE_ARM:
121 case PROCESSOR_ARCHITECTURE_ARM64:
123 case PROCESSOR_ARCHITECTURE_IA64:
126 return _(
"cpu_architecture^<unknown>");
136 struct posix_pipe_release_policy
138 void operator()(std::FILE*
f)
const {
if(
f !=
nullptr) { pclose(
f); } }
147 typedef std::unique_ptr<std::FILE, posix_pipe_release_policy> scoped_posix_pipe;
154 std::string read_pipe_line(scoped_posix_pipe&
p)
166 while((
c = std::fgetc(
p.get())) &&
c != EOF &&
c !=
'\n' &&
c !=
'\r') {
167 ver.push_back(
static_cast<char>(
c));
173 std::map<std::string, std::string> parse_fdo_osrelease(
const std::string&
path)
180 std::map<std::string, std::string> res;
185 for(std::string
s; std::getline(*
in,
s);) {
186 if(
s.empty() ||
s.front() ==
'#') {
190 auto eqsign_pos =
s.find(
'=');
191 if(!eqsign_pos || eqsign_pos == std::string::npos) {
195 auto lhs =
s.substr(0, eqsign_pos),
196 rhs = eqsign_pos + 1 <
s.length() ?
utils::unescape(
s.substr(eqsign_pos + 1)) :
"";
202 if(rhs.length() >= 2 && rhs.front() ==
'"' && rhs.back() ==
'"') {
207 res.emplace(std::move(lhs), std::move(rhs));
219 #if defined(__APPLE__) || defined(_X11)
224 ERR_DU <<
"os_version: uname error (" << strerror(errno) <<
")";
228 #if defined(__APPLE__)
242 std::map<std::string, std::string> osrel;
244 static const std::string fdo_osrel_etc =
"/etc/os-release";
245 static const std::string fdo_osrel_usr =
"/usr/lib/os-release";
248 osrel = parse_fdo_osrelease(fdo_osrel_etc);
250 osrel = parse_fdo_osrelease(fdo_osrel_usr);
254 auto osrel_distname = osrel[
"PRETTY_NAME"];
255 if(osrel_distname.empty()) {
256 osrel_distname = osrel[
"NAME"];
259 if(!osrel_distname.empty()) {
260 return osrel_distname +
" " + u.machine;
267 static const std::string lsb_release_bin =
"/usr/bin/lsb_release";
270 static const std::string cmdline = lsb_release_bin +
" -s -d";
272 scoped_posix_pipe
p(popen(cmdline.c_str(),
"r"));
273 std::string ver = read_pipe_line(
p);
275 if(ver.length() >= 2 && ver[0] ==
'"' && ver[ver.length() - 1] ==
'"') {
276 ver.erase(ver.length() - 1, 1);
282 return ver +
" " + u.machine;
295 #elif defined(_WIN32)
301 static const std::string base
302 = !on_wine() ?
"Microsoft Windows" :
"Wine/Microsoft Windows";
306 SecureZeroMemory(&v,
sizeof(OSVERSIONINFOEX));
307 v.dwOSVersionInfoSize =
sizeof(OSVERSIONINFOEX);
313 #pragma warning(push)
314 #pragma warning(disable:4996)
316 if(!GetVersionEx(
reinterpret_cast<OSVERSIONINFO*
>(&v))) {
317 ERR_DU <<
"os_version: GetVersionEx error (" << GetLastError() <<
')';
324 const DWORD vnum = v.dwMajorVersion * 100 + v.dwMinorVersion;
337 version =
"Server 2003";
340 if(v.wProductType == VER_NT_WORKSTATION) {
343 version =
"Server 2008";
347 if(v.wProductType == VER_NT_WORKSTATION) {
350 version =
"Server 2008 R2";
354 if(v.wProductType == VER_NT_WORKSTATION) {
357 version =
"Server 2012";
361 if(v.wProductType == VER_NT_WORKSTATION) {
364 version =
"Server 2012 R2";
368 if(v.wProductType == VER_NT_WORKSTATION) {
369 version = v.dwBuildNumber < 22000 ?
"10" :
"11";
370 const auto& release_id = windows_release_id();
371 if(!release_id.empty()) {
373 version += release_id;
379 if(v.wProductType != VER_NT_WORKSTATION) {
384 if(*v.szCSDVersion) {
386 version += unicode_cast<std::string>(std::wstring(v.szCSDVersion));
392 << v.dwMajorVersion <<
'.'
393 << v.dwMinorVersion <<
'.'
397 return base +
" " + version +
" " + windows_runtime_arch();
405 ERR_DU <<
"os_version(): unsupported platform";
406 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(const std::string &str)
Length in characters of a UTF-8 string.
void trim(std::string_view &s)
std::string unescape(const std::string &str)
Remove all escape characters (backslash)
static map_location::DIRECTION s
static lg::log_domain log_desktop("desktop")