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 #define ERR_DU LOG_STREAM(err, log_desktop)
62 #define LOG_DU LOG_STREAM(info, log_desktop)
79 HMODULE ntdll = GetModuleHandle(L
"ntdll.dll");
84 return GetProcAddress(ntdll,
"wine_get_version") !=
nullptr;
94 std::string windows_release_id()
97 DWORD
size =
sizeof(buf);
99 auto res = RegGetValueA(HKEY_LOCAL_MACHINE,
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion",
"DisplayVersion", RRF_RT_REG_SZ,
nullptr, buf, &
size);
100 if(res != ERROR_SUCCESS) {
101 res = RegGetValueA(HKEY_LOCAL_MACHINE,
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion",
"ReleaseId", RRF_RT_REG_SZ,
nullptr, buf, &
size);
104 return std::string{res == ERROR_SUCCESS ? buf :
""};
107 std::string windows_runtime_arch()
110 SecureZeroMemory(&si,
sizeof(SYSTEM_INFO));
111 GetNativeSystemInfo(&si);
113 switch(si.wProcessorArchitecture) {
114 case PROCESSOR_ARCHITECTURE_INTEL:
116 case PROCESSOR_ARCHITECTURE_AMD64:
118 case PROCESSOR_ARCHITECTURE_ARM:
120 case PROCESSOR_ARCHITECTURE_ARM64:
122 case PROCESSOR_ARCHITECTURE_IA64:
125 return _(
"cpu_architecture^<unknown>");
135 struct posix_pipe_release_policy
137 void operator()(std::FILE*
f)
const {
if(
f !=
nullptr) { pclose(
f); } }
146 typedef std::unique_ptr<std::FILE, posix_pipe_release_policy> scoped_posix_pipe;
153 std::string read_pipe_line(scoped_posix_pipe&
p)
165 while((
c = std::fgetc(
p.get())) &&
c != EOF &&
c !=
'\n' &&
c !=
'\r') {
166 ver.push_back(
static_cast<char>(
c));
172 std::map<std::string, std::string> parse_fdo_osrelease(
const std::string&
path)
179 std::map<std::string, std::string> res;
184 for(std::string
s; std::getline(*
in,
s);) {
185 if(
s.empty() ||
s.front() ==
'#') {
189 auto eqsign_pos =
s.find(
'=');
190 if(!eqsign_pos || eqsign_pos == std::string::npos) {
194 auto lhs =
s.substr(0, eqsign_pos),
195 rhs = eqsign_pos + 1 <
s.length() ?
utils::unescape(
s.substr(eqsign_pos + 1)) :
"";
201 if(rhs.length() >= 2 && rhs.front() ==
'"' && rhs.back() ==
'"') {
206 res.emplace(std::move(lhs), std::move(rhs));
218 #if defined(__APPLE__) || defined(_X11)
223 ERR_DU <<
"os_version: uname error (" << strerror(errno) <<
")";
227 #if defined(__APPLE__)
241 std::map<std::string, std::string> osrel;
243 static const std::string fdo_osrel_etc =
"/etc/os-release";
244 static const std::string fdo_osrel_usr =
"/usr/lib/os-release";
247 osrel = parse_fdo_osrelease(fdo_osrel_etc);
249 osrel = parse_fdo_osrelease(fdo_osrel_usr);
253 auto osrel_distname = osrel[
"PRETTY_NAME"];
254 if(osrel_distname.empty()) {
255 osrel_distname = osrel[
"NAME"];
258 if(!osrel_distname.empty()) {
259 return osrel_distname +
" " + u.machine;
266 static const std::string lsb_release_bin =
"/usr/bin/lsb_release";
269 static const std::string cmdline = lsb_release_bin +
" -s -d";
271 scoped_posix_pipe
p(popen(cmdline.c_str(),
"r"));
272 std::string ver = read_pipe_line(
p);
274 if(ver.length() >= 2 && ver[0] ==
'"' && ver[ver.length() - 1] ==
'"') {
275 ver.erase(ver.length() - 1, 1);
281 return ver +
" " + u.machine;
294 #elif defined(_WIN32)
300 static const std::string base
301 = !on_wine() ?
"Microsoft Windows" :
"Wine/Microsoft Windows";
305 SecureZeroMemory(&v,
sizeof(OSVERSIONINFOEX));
306 v.dwOSVersionInfoSize =
sizeof(OSVERSIONINFOEX);
312 #pragma warning(push)
313 #pragma warning(disable:4996)
315 if(!GetVersionEx(
reinterpret_cast<OSVERSIONINFO*
>(&v))) {
316 ERR_DU <<
"os_version: GetVersionEx error (" << GetLastError() <<
')';
323 const DWORD vnum = v.dwMajorVersion * 100 + v.dwMinorVersion;
336 version =
"Server 2003";
339 if(v.wProductType == VER_NT_WORKSTATION) {
342 version =
"Server 2008";
346 if(v.wProductType == VER_NT_WORKSTATION) {
349 version =
"Server 2008 R2";
353 if(v.wProductType == VER_NT_WORKSTATION) {
356 version =
"Server 2012";
360 if(v.wProductType == VER_NT_WORKSTATION) {
363 version =
"Server 2012 R2";
367 if(v.wProductType == VER_NT_WORKSTATION) {
368 version = v.dwBuildNumber < 22000 ?
"10" :
"11";
369 const auto& release_id = windows_release_id();
370 if(!release_id.empty()) {
372 version += release_id;
378 if(v.wProductType != VER_NT_WORKSTATION) {
383 if(*v.szCSDVersion) {
385 version += unicode_cast<std::string>(std::wstring(v.szCSDVersion));
391 << v.dwMajorVersion <<
'.'
392 << v.dwMinorVersion <<
'.'
396 return base +
" " + version +
" " + windows_runtime_arch();
404 ERR_DU <<
"os_version(): unsupported platform";
405 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")