This commit is contained in:
Mars 2025-01-30 00:29:07 -05:00
parent 3fbe24c3bb
commit c42265e36d
3 changed files with 104 additions and 85 deletions

View file

@ -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,
)

View file

@ -1,6 +1,7 @@
#ifdef __WIN32__
#include <exception>
#include <iostream>
#include <windows.h>
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Media.Control.h>
@ -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<LPBYTE>(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<LPBYTE>(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<RtlGetVersionPtr>(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

View file

@ -1,5 +1,8 @@
#pragma once
// probably stupid but it fixes the issue with windows.h defining ERROR
#undef ERROR
#include <filesystem>
#include <fmt/chrono.h>
#include <fmt/color.h>
@ -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 <typename... Args>
void LogImpl(
LogLevel level,
const std::source_location& loc,
fmt::format_string<Args...> fmt,
Args&&... args
) {
void LogImpl(LogLevel level, const std::source_location& loc, fmt::format_string<Args...> fmt, Args&&... args) {
const time_t now = std::time(nullptr);
const auto [color, levelStr] = [&] {
switch (level) {