From bcc3dfff3a053014aaee17093901ffead06f3cb7 Mon Sep 17 00:00:00 2001 From: Jean Pierre Cimalando Date: Tue, 6 Apr 2021 22:34:08 +0200 Subject: [PATCH 1/2] Add native helpers to get OS name with version --- plugins/editor/src/editor/NativeHelpers.cpp | 59 +++++++++++++++++++++ plugins/editor/src/editor/NativeHelpers.h | 2 + plugins/editor/src/editor/NativeHelpers.mm | 11 ++++ 3 files changed, 72 insertions(+) diff --git a/plugins/editor/src/editor/NativeHelpers.cpp b/plugins/editor/src/editor/NativeHelpers.cpp index ab9af708..714db433 100644 --- a/plugins/editor/src/editor/NativeHelpers.cpp +++ b/plugins/editor/src/editor/NativeHelpers.cpp @@ -22,6 +22,17 @@ static WCHAR *stringToWideChar(const char *str, int strCch = -1) return strW.release(); } +static char* stringToUTF8(const wchar_t *strW, int strWCch = -1) +{ + unsigned strSize = WideCharToMultiByte(CP_UTF8, 0, strW, strWCch, nullptr, 0, nullptr, nullptr); + if (strSize == 0) + return {}; + std::unique_ptr str(new char[strSize]); + if (WideCharToMultiByte(CP_UTF8, 0, strW, strWCch, str.get(), strSize, nullptr, nullptr) == 0) + return {}; + return str.release(); +} + bool openFileInExternalEditor(const char *filename) { std::wstring path = stringToWideChar(filename); @@ -74,6 +85,32 @@ bool askQuestion(const char *text) int ret = MessageBoxW(nullptr, stringToWideChar(text), L"Question", MB_YESNO); return ret == IDYES; } + +std::string getOperatingSystemName() +{ + LSTATUS status; + HKEY key = nullptr; + const WCHAR keyPath[] = L"Software\\Microsoft\\Windows NT\\CurrentVersion"; + const WCHAR valueName[] = L"ProductName"; + const char fallbackName[] = "Windows (unknown)"; + + status = RegOpenKeyExW(HKEY_LOCAL_MACHINE, keyPath, 0, KEY_QUERY_VALUE, &key); + if (status != ERROR_SUCCESS) + return fallbackName; + + DWORD valueSize = 32768 * sizeof(WCHAR); + std::unique_ptr valueW(new WCHAR[(valueSize / sizeof(WCHAR)) + 1]()); + DWORD valueType; + status = RegQueryValueExW( + key, valueName, nullptr, + &valueType, reinterpret_cast(valueW.get()), &valueSize); + RegCloseKey(key); + if (status != ERROR_SUCCESS || (valueType != REG_SZ && valueType != REG_EXPAND_SZ)) + return fallbackName; + + std::unique_ptr valueUTF8(stringToUTF8(valueW.get())); + return valueUTF8.get(); +} #elif defined(__APPLE__) // implemented in NativeHelpers.mm #else @@ -173,4 +210,26 @@ bool isZenityAvailable() { return access(zenityPath, X_OK) == 0; } + +std::string getOperatingSystemName() +{ + std::string name; + name.reserve(256); + + if (char *osName = g_get_os_info(G_OS_INFO_KEY_NAME)) { + name.append(osName); + g_free(osName); + } + else { + name.append("Unknown"); + } + + if (char *osVersion = g_get_os_info(G_OS_INFO_KEY_VERSION_ID)) { + name.push_back(' '); + name.append(osVersion); + g_free(osVersion); + } + + return name; +} #endif diff --git a/plugins/editor/src/editor/NativeHelpers.h b/plugins/editor/src/editor/NativeHelpers.h index 41c52a72..db21af28 100644 --- a/plugins/editor/src/editor/NativeHelpers.h +++ b/plugins/editor/src/editor/NativeHelpers.h @@ -5,11 +5,13 @@ // If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz #pragma once +#include bool openFileInExternalEditor(const char *filename); bool openDirectoryInExplorer(const char *filename); bool openURLWithExternalProgram(const char *url); bool askQuestion(const char *text); +std::string getOperatingSystemName(); #if !defined(_WIN32) && !defined(__APPLE__) bool isZenityAvailable(); diff --git a/plugins/editor/src/editor/NativeHelpers.mm b/plugins/editor/src/editor/NativeHelpers.mm index 0db886fd..1f6ee93f 100644 --- a/plugins/editor/src/editor/NativeHelpers.mm +++ b/plugins/editor/src/editor/NativeHelpers.mm @@ -48,4 +48,15 @@ bool askQuestion(const char *text) NSInteger button = [alert runModal]; return button == NSAlertFirstButtonReturn; } + +std::string getOperatingSystemName() +{ +#if TARGET_OS_IOS + NSString *osName = @"iOS"; +#elif TARGET_OS_MAC + NSString *osName = @"macOS"; +#endif + NSString *osVersion = [[NSProcessInfo processInfo] operatingSystemVersionString]; + return [[NSString stringWithFormat:@"%@ %@", osName, osVersion] UTF8String]; +} #endif From f181a5db757d6880e3d47d6de7c988ee9f06871b Mon Sep 17 00:00:00 2001 From: Jean Pierre Cimalando Date: Tue, 6 Apr 2021 22:53:06 +0200 Subject: [PATCH 2/2] Add compatibility OS detection with uname --- plugins/editor/src/editor/NativeHelpers.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/plugins/editor/src/editor/NativeHelpers.cpp b/plugins/editor/src/editor/NativeHelpers.cpp index 714db433..336c81d6 100644 --- a/plugins/editor/src/editor/NativeHelpers.cpp +++ b/plugins/editor/src/editor/NativeHelpers.cpp @@ -117,6 +117,7 @@ std::string getOperatingSystemName() #include #include #include +#include #include #include #include @@ -216,6 +217,7 @@ std::string getOperatingSystemName() std::string name; name.reserve(256); +#if GLIB_CHECK_VERSION(2, 64, 0) if (char *osName = g_get_os_info(G_OS_INFO_KEY_NAME)) { name.append(osName); g_free(osName); @@ -229,6 +231,19 @@ std::string getOperatingSystemName() name.append(osVersion); g_free(osVersion); } +#else + utsname un {}; + int ret = uname(&un); + if (ret != -1 && un.sysname[0] != '\0') + name.append(un.sysname); + else { + name.append("Unknown"); + } + if (ret != -1 && un.release[0] != '\0') { + name.push_back(' '); + name.append(un.release); + } +#endif return name; }