This commit is contained in:
Mars 2024-06-09 02:58:29 -04:00
commit 35a3dd159a
6 changed files with 275 additions and 0 deletions

24
.clang-format Normal file
View file

@ -0,0 +1,24 @@
AlignAfterOpenBracket: BlockIndent
AlignArrayOfStructures: Right
AlignConsecutiveAssignments: true
AlignConsecutiveDeclarations: true
AllowShortBlocksOnASingleLine: Always
AllowShortEnumsOnASingleLine: true
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: WithoutElse
AllowShortLoopsOnASingleLine: true
BasedOnStyle: Chromium
BinPackArguments: false
BinPackParameters: false
IndentAccessModifiers: false
IndentExternBlock: Indent
NamespaceIndentation: All
SpaceBeforeCpp11BracedList: true
SpacesBeforeTrailingComments: 1
IncludeBlocks: Regroup
IncludeCategories:
- Regex: '".*"'
Priority: 1
- Regex: '<.*>'
Priority: -1

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
.idea/
cmake-build-*/

15
CMakeLists.txt Normal file
View file

@ -0,0 +1,15 @@
cmake_minimum_required(VERSION 3.28)
project(draconis__)
set(CMAKE_CXX_STANDARD 26)
set(CMAKE_MAKE_PROGRAM "Ninja")
add_executable(draconis__ main.cpp
util.h)
find_package(fmt CONFIG REQUIRED)
find_package(cppwinrt CONFIG REQUIRED)
target_link_libraries(draconis__ PRIVATE fmt::fmt-header-only)
target_link_libraries(draconis__ PRIVATE WindowsApp)

199
main.cpp Normal file
View file

@ -0,0 +1,199 @@
#include <fmt/chrono.h>
#include <fmt/core.h>
#include <iostream>
#include <windows.h>
#include <winrt/Windows.Foundation.h> // ReSharper disable once CppUnusedIncludeDirective
#include <winrt/Windows.Media.Control.h>
#include <winrt/base.h>
#include <winrt/impl/windows.media.control.2.h>
#include "util.h"
using namespace winrt;
using namespace winrt::Windows::Media::Control;
struct KBToGiB {
u64 value;
};
template <>
struct fmt::formatter<KBToGiB> : formatter<double> {
template <typename FormatContext>
typename FormatContext::iterator
format(const KBToGiB KTG, FormatContext& ctx) {
typename FormatContext::iterator out = formatter<double>::format(
static_cast<double>(KTG.value) / pow(1024, 2), ctx
);
*out++ = 'G';
*out++ = 'i';
*out++ = 'B';
return out;
}
};
enum DateNum { Ones, Twos, Threes, Default };
DateNum ParseDate(std::string const& input) {
if (input == "1" || input == "21" || input == "31") return Ones;
if (input == "2" || input == "22") return Twos;
if (input == "3" || input == "23") return Threes;
return Default;
}
std::string GetNowPlaying() {
using namespace winrt::Windows::Media::Control;
using namespace winrt::Windows::Foundation;
using MediaProperties =
GlobalSystemMediaTransportControlsSessionMediaProperties;
using Session = GlobalSystemMediaTransportControlsSession;
using SessionManager = GlobalSystemMediaTransportControlsSessionManager;
try {
// Request the session manager asynchronously
const IAsyncOperation<SessionManager> sessionManagerOp =
SessionManager::RequestAsync();
const SessionManager sessionManager = sessionManagerOp.get();
if (const Session currentSession = sessionManager.GetCurrentSession()) {
// Try to get the media properties asynchronously
const IAsyncOperation<MediaProperties> mediaPropertiesOp =
currentSession.TryGetMediaPropertiesAsync();
const MediaProperties mediaProperties = mediaPropertiesOp.get();
// Convert the hstring title to std::string
return to_string(mediaProperties.Title());
}
// If we reach this point, there is no current session
return "No current media session.";
} catch (...) { return "Failed to get media properties."; }
}
std::string getRegistryValue(
const HKEY& hKey,
const std::string& subKey,
const std::string& valueName
) {
HKEY key;
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 "";
}
std::string value(dataSize, '\0');
if (RegQueryValueExA(
key,
valueName.c_str(),
nullptr,
nullptr,
reinterpret_cast<LPBYTE>(&value[0]),
&dataSize
) != ERROR_SUCCESS) {
RegCloseKey(key);
return "";
}
RegCloseKey(key);
// Remove null terminator if present
if (!value.empty() && value.back() == '\0') value.pop_back();
return value;
}
std::string getPrettyWindowsName() {
std::string productName = getRegistryValue(
HKEY_LOCAL_MACHINE,
R"(SOFTWARE\Microsoft\Windows NT\CurrentVersion)",
"ProductName"
);
const std::string displayVersion = getRegistryValue(
HKEY_LOCAL_MACHINE,
R"(SOFTWARE\Microsoft\Windows NT\CurrentVersion)",
"DisplayVersion"
);
const std::string releaseId = getRegistryValue(
HKEY_LOCAL_MACHINE,
R"(SOFTWARE\Microsoft\Windows NT\CurrentVersion)",
"ReleaseId"
);
const int buildNumber = stoi(getRegistryValue(
HKEY_LOCAL_MACHINE,
R"(SOFTWARE\Microsoft\Windows NT\CurrentVersion)",
"CurrentBuildNumber"
));
fmt::println("Build number: {}", buildNumber);
// Check if the build number is 22000 or higher
if (buildNumber >= 22000 &&
productName.find("Windows 10") != std::string::npos)
productName.replace(productName.find("Windows 10"), 10, "Windows 11");
if (!productName.empty()) {
std::string result = productName;
if (!displayVersion.empty())
result += " " + displayVersion;
else if (!releaseId.empty())
result += " " + releaseId;
return result;
}
return "";
}
int main() {
init_apartment();
u64 memInfo;
GetPhysicallyInstalledSystemMemory(&memInfo);
fmt::println("Installed RAM: {:.2f}", KBToGiB {memInfo});
fmt::println("Now playing: {}", GetNowPlaying());
const std::tm localTime = fmt::localtime(time(nullptr));
std::string date = fmt::format("{:%e}", localTime);
auto start = date.begin();
while (start != date.end() && std::isspace(*start)) ++start;
date.erase(date.begin(), start);
switch (ParseDate(date)) {
case Ones:
date += "st";
break;
case Twos:
date += "nd";
break;
case Threes:
date += "rd";
break;
case Default:
date += "th";
break;
}
fmt::println("{:%B} {}, {:%-I:%0M %p}", localTime, date, localTime);
fmt::println("Version: {}", getPrettyWindowsName());
uninit_apartment();
return 0;
}

23
util.h Normal file
View file

@ -0,0 +1,23 @@
#pragma once
#include <cstdint>
// Unsigned integers
using u8 = std::uint8_t;
using u16 = std::uint16_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;
// Signed integers
using i8 = std::int8_t;
using i16 = std::int16_t;
using i32 = std::int32_t;
using i64 = std::int64_t;
// Floating-points
using f32 = float;
using f64 = double;
// Size types
using usize = std::size_t;
using isize = std::ptrdiff_t;

12
vcpkg.json Normal file
View file

@ -0,0 +1,12 @@
{
"name" : "draconispp",
"version-string" : "1.0.0",
"builtin-baseline" : "14b91796a68c87bc8d5cb35911b39287ccb7bd95",
"dependencies" : [ {
"name" : "fmt",
"version>=" : "10.2.1#2"
}, {
"name" : "cppwinrt",
"version>=" : "2.0.240111.5"
} ]
}