From c42265e36d2138b1b737753a8cd71c1962c4cbc6 Mon Sep 17 00:00:00 2001 From: Mars Date: Thu, 30 Jan 2025 00:29:07 -0500 Subject: [PATCH] window s --- meson.build | 61 +++++++++++------------- src/os/windows.cpp | 115 ++++++++++++++++++++++++++++----------------- src/util/macros.h | 13 ++--- 3 files changed, 104 insertions(+), 85 deletions(-) diff --git a/meson.build b/meson.build index 58e98a3..73791ae 100644 --- a/meson.build +++ b/meson.build @@ -1,11 +1,12 @@ project( - 'draconis++', 'cpp', + 'draconis++', + 'cpp', version: '0.1.0', default_options: [ 'default_library=static', 'warning_level=everything', - 'buildtype=debugoptimized' - ] + 'buildtype=debugoptimized', + ], ) cpp = meson.get_compiler('cpp') @@ -14,21 +15,23 @@ if host_machine.system() == 'darwin' add_languages('objcpp') objcpp = meson.get_compiler('objcpp') add_project_arguments( - objcpp.get_supported_arguments([ - '-std=c++2b', - '-Wno-c++20-compat', - '-Wno-c++20-extensions', - '-Wno-c++98-compat', - '-Wno-c++98-compat-pedantic', - '-Wno-disabled-macro-expansion', - '-Wno-missing-prototypes', - '-Wno-padded', - '-Wno-pre-c++20-compat-pedantic', - '-Wno-switch-default', - '-Wunused-function', - '-fvisibility=hidden' - ]), - language: 'objcpp' + objcpp.get_supported_arguments( + [ + '-std=c++2b', + '-Wno-c++20-compat', + '-Wno-c++20-extensions', + '-Wno-c++98-compat', + '-Wno-c++98-compat-pedantic', + '-Wno-disabled-macro-expansion', + '-Wno-missing-prototypes', + '-Wno-padded', + '-Wno-pre-c++20-compat-pedantic', + '-Wno-switch-default', + '-Wunused-function', + '-fvisibility=hidden', + ], + ), + language: 'objcpp', ) endif @@ -44,23 +47,16 @@ common_cpp_args = [ '-Wno-pre-c++20-compat-pedantic', '-Wno-switch-default', '-Wunused-function', - '-fvisibility=hidden' + '-fvisibility=hidden', ] if host_machine.system() == 'windows' common_cpp_args += ['-DCURL_STATICLIB'] endif -add_project_arguments( - cpp.get_supported_arguments(common_cpp_args), - language: 'cpp' -) +add_project_arguments(cpp.get_supported_arguments(common_cpp_args), language: 'cpp') -source_file_names = [ - 'src/main.cpp', - 'src/config/config.cpp', - 'src/config/weather.cpp' -] +source_file_names = ['src/main.cpp', 'src/config/config.cpp', 'src/config/weather.cpp'] if host_machine.system() == 'linux' source_file_names += ['src/os/linux.cpp'] @@ -86,8 +82,8 @@ deps = [ dependency('libcurl'), dependency('tomlplusplus'), dependency('yyjson'), - dependency('ftxui'), - dependency('reflectcpp') + dependency('ftxui', modules: ['ftxui::screen', 'ftxui::dom', 'ftxui::component']), + dependency('reflectcpp'), ] if host_machine.system() == 'darwin' @@ -114,6 +110,5 @@ executable( sources, objc_args: objc_args, link_args: link_args, - dependencies: deps -) - + dependencies: deps, +) \ No newline at end of file diff --git a/src/os/windows.cpp b/src/os/windows.cpp index 857e530..9b8b5ec 100644 --- a/src/os/windows.cpp +++ b/src/os/windows.cpp @@ -1,6 +1,7 @@ #ifdef __WIN32__ #include +#include #include #include #include @@ -9,6 +10,36 @@ #include "os.h" +using RtlGetVersionPtr = NTSTATUS(WINAPI*)(PRTL_OSVERSIONINFOW); + +namespace { + fn GetRegistryValue(const HKEY& hKey, const string& subKey, const string& valueName) -> string { + HKEY key = nullptr; + if (RegOpenKeyExA(hKey, subKey.c_str(), 0, KEY_READ, &key) != ERROR_SUCCESS) + return ""; + + DWORD dataSize = 0; + if (RegQueryValueExA(key, valueName.c_str(), nullptr, nullptr, nullptr, &dataSize) != ERROR_SUCCESS) { + RegCloseKey(key); + return ""; + } + + string value(dataSize, '\0'); + if (RegQueryValueExA(key, valueName.c_str(), nullptr, nullptr, std::bit_cast(value.data()), &dataSize) != + ERROR_SUCCESS) { + RegCloseKey(key); + return ""; + } + + RegCloseKey(key); + // Remove null terminator if present + if (!value.empty() && value.back() == '\0') + value.pop_back(); + + return value; + } +} + fn GetMemInfo() -> u64 { u64 mem = 0; GetPhysicallyInstalledSystemMemory(&mem); @@ -37,60 +68,27 @@ fn GetNowPlaying() -> string { } // If we reach this point, there is no current session - return "No current media session."; - } catch (...) { return "Failed to get media properties."; } -} - -fn GetRegistryValue(const HKEY& hKey, const string& subKey, const string& valueName) -> string { - HKEY key = nullptr; - if (RegOpenKeyExA(hKey, subKey.c_str(), 0, KEY_READ, &key) != ERROR_SUCCESS) return ""; - - DWORD dataSize = 0; - if (RegQueryValueExA(key, valueName.c_str(), nullptr, nullptr, nullptr, &dataSize) != - ERROR_SUCCESS) { - RegCloseKey(key); + } catch (const winrt::hresult_error& e) { + ERROR_LOG("Error: {}", to_string(e.message())); return ""; } - - string value(dataSize, '\0'); - if (RegQueryValueExA( - key, - valueName.c_str(), - nullptr, - nullptr, - reinterpret_cast(value.data()), // NOLINT(*-reinterpret-cast) - &dataSize - ) != ERROR_SUCCESS) { - RegCloseKey(key); - return ""; - } - - RegCloseKey(key); - // Remove null terminator if present - if (!value.empty() && value.back() == '\0') - value.pop_back(); - - return value; } fn GetOSVersion() -> string { - string productName = GetRegistryValue( - HKEY_LOCAL_MACHINE, R"(SOFTWARE\Microsoft\Windows NT\CurrentVersion)", "ProductName" - ); + string productName = + GetRegistryValue(HKEY_LOCAL_MACHINE, R"(SOFTWARE\Microsoft\Windows NT\CurrentVersion)", "ProductName"); - const string displayVersion = GetRegistryValue( - HKEY_LOCAL_MACHINE, R"(SOFTWARE\Microsoft\Windows NT\CurrentVersion)", "DisplayVersion" - ); + const string displayVersion = + GetRegistryValue(HKEY_LOCAL_MACHINE, R"(SOFTWARE\Microsoft\Windows NT\CurrentVersion)", "DisplayVersion"); - const string releaseId = GetRegistryValue( - HKEY_LOCAL_MACHINE, R"(SOFTWARE\Microsoft\Windows NT\CurrentVersion)", "ReleaseId" - ); + const string releaseId = + GetRegistryValue(HKEY_LOCAL_MACHINE, R"(SOFTWARE\Microsoft\Windows NT\CurrentVersion)", "ReleaseId"); // Check for Windows 11 - if (const i32 buildNumber = stoi(GetRegistryValue( - HKEY_LOCAL_MACHINE, R"(SOFTWARE\Microsoft\Windows NT\CurrentVersion)", "CurrentBuildNumber" - )); + if (const i32 buildNumber = stoi( + GetRegistryValue(HKEY_LOCAL_MACHINE, R"(SOFTWARE\Microsoft\Windows NT\CurrentVersion)", "CurrentBuildNumber") + ); buildNumber >= 22000 && productName.find("Windows 10") != string::npos) productName.replace(productName.find("Windows 10"), 10, "Windows 11"); @@ -108,4 +106,33 @@ fn GetOSVersion() -> string { return ""; } +fn GetHost() -> string { + string hostName = GetRegistryValue(HKEY_LOCAL_MACHINE, R"(SYSTEM\HardwareConfig\Current)", "SystemFamily"); + + if (hostName.empty()) + hostName = GetRegistryValue( + HKEY_LOCAL_MACHINE, R"(SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName)", "ComputerName" + ); + + return hostName; +} + +fn GetKernelVersion() -> string { + std::stringstream versionStream; + HMODULE ntdllHandle = GetModuleHandleW(L"ntdll.dll"); + + if (ntdllHandle) { + auto rtlGetVersion = std::bit_cast(GetProcAddress(ntdllHandle, "RtlGetVersion")); + if (rtlGetVersion) { + RTL_OSVERSIONINFOW osInfo = {}; + osInfo.dwOSVersionInfoSize = sizeof(osInfo); + if (rtlGetVersion(&osInfo) == 0) + versionStream << osInfo.dwMajorVersion << "." << osInfo.dwMinorVersion << "." << osInfo.dwBuildNumber << "." + << osInfo.dwPlatformId; + } + } + + return versionStream.str(); +} + #endif diff --git a/src/util/macros.h b/src/util/macros.h index 239079a..43f8229 100644 --- a/src/util/macros.h +++ b/src/util/macros.h @@ -1,5 +1,8 @@ #pragma once +// probably stupid but it fixes the issue with windows.h defining ERROR +#undef ERROR + #include #include #include @@ -14,19 +17,13 @@ namespace log_colors { using fmt::terminal_color; constexpr fmt::terminal_color debug = terminal_color::cyan, info = terminal_color::green, warn = terminal_color::yellow, error = terminal_color::red, - timestamp = terminal_color::bright_white, - file_info = terminal_color::bright_white; + timestamp = terminal_color::bright_white, file_info = terminal_color::bright_white; } enum class LogLevel : u8 { DEBUG, INFO, WARN, ERROR }; template -void LogImpl( - LogLevel level, - const std::source_location& loc, - fmt::format_string fmt, - Args&&... args -) { +void LogImpl(LogLevel level, const std::source_location& loc, fmt::format_string fmt, Args&&... args) { const time_t now = std::time(nullptr); const auto [color, levelStr] = [&] { switch (level) {