Initial C version

This commit is contained in:
Jordon Brooks 2024-05-22 13:42:37 +01:00
parent fa0e309875
commit d5b097c6ec
Signed by: jordon
GPG key ID: DBD9758CD53E786A
33 changed files with 123 additions and 1969 deletions

1
.gitignore vendored
View file

@ -21,3 +21,4 @@ build/**
# Blacklist specific build directories # Blacklist specific build directories
linuxbuild/ linuxbuild/
build/ build/
.idea/

View file

@ -14,19 +14,9 @@
cmake_minimum_required(VERSION 3.10) cmake_minimum_required(VERSION 3.10)
project(HarmonyLinkLib VERSION 2.1.1) project(HarmonyLinkLib VERSION 2.1.1)
include(FetchContent) # Specify the C++ standard
set(CMAKE_C_STANDARD 11)
# Fetch fmt library set(CMAKE_C_STANDARD_REQUIRED True)
FetchContent_Declare(
fmt
GIT_REPOSITORY https://github.com/fmtlib/fmt.git
GIT_TAG 10.2.1 # Specify the desired version of {fmt}
)
FetchContent_MakeAvailable(fmt)
set_target_properties(fmt PROPERTIES FOLDER External)
set_target_properties(fmt PROPERTIES POSITION_INDEPENDENT_CODE TRUE)
# Find the current Git branch and the last commit timestamp # Find the current Git branch and the last commit timestamp
find_package(Git QUIET) find_package(Git QUIET)
@ -50,10 +40,6 @@ endif()
configure_file(include/Version.h.in Version.generated.h) configure_file(include/Version.h.in Version.generated.h)
# Specify the C++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)
# Define metadata variables # Define metadata variables
set(FILE_DESCRIPTION "Enhances handheld gaming with intelligent hardware recognition, dynamic adaptability, and robust API access for Windows and Linux, including Steam Deck and Wine support.") set(FILE_DESCRIPTION "Enhances handheld gaming with intelligent hardware recognition, dynamic adaptability, and robust API access for Windows and Linux, including Steam Deck and Wine support.")
set(INTERNAL_NAME "HarmonyLinkLib") set(INTERNAL_NAME "HarmonyLinkLib")
@ -66,63 +52,43 @@ configure_file(${CMAKE_CURRENT_SOURCE_DIR}/Resources/Version.rc.in ${CMAKE_CURRE
# Explicitly list source files # Explicitly list source files
set(COMMON_SOURCES set(COMMON_SOURCES
"src/Platform/IPlatformUtilities.cpp" "src/dllmain.c"
"src/HarmonyLinkLib.cpp" "src/HarmonyLinkLib.c"
"src/Version.cpp" "src/Version.c"
"src/dllmain.cpp"
"src/Platform/WineUtilities.cpp" "src/Structs/Battery.c"
"src/Utilities.cpp"
) )
# Explicitly list include files # Explicitly list include files
set(COMMON_INCLUDES set(COMMON_INCLUDES
"include/Core.h" "include/Core.h"
"include/Structs/FBattery.h"
"include/Structs/FOSVerInfo.h"
"include/Structs/FDevice.h"
"include/Structs/FCPUInfo.h"
"include/Enums/EDevice.h"
"include/Enums/EPlatform.h"
"include/Enums/ESteamDeck.h"
"include/FString.h"
"include/HarmonyLinkLib.h" "include/HarmonyLinkLib.h"
"include/Version.h" "include/Version.h"
"src/Platform/IPlatformUtilities.h"
"src/Platform/WineUtilities.h" "include/Enums/Device.h"
"src/Utilities.h" "include/Enums/Platform.h"
) "include/Enums/SteamDeck.h"
# Adding fmt headers explicitly "include/Structs/Battery.h"
set(FMT_HEADERS
"${fmt_SOURCE_DIR}/include/fmt"
) )
set(WINDOWS_SOURCES set(WINDOWS_SOURCES
"src/Platform/Windows/WindowsUtilities.cpp"
) )
set(WINDOWS_INCLUDES set(WINDOWS_INCLUDES
"src/Platform/Windows/WindowsUtilities.h"
) )
set(LINUX_SOURCES set(LINUX_SOURCES
"src/Platform/Unix/Linux/LinuxUtilities.cpp"
"src/Platform/Unix/UnixUtilities.cpp"
) )
set(LINUX_INCLUDES set(LINUX_INCLUDES
"src/Platform/Unix/Linux/LinuxUtilities.h"
"src/Platform/Unix/UnixUtilities.h"
) )
set(MAC_SOURCES set(MAC_SOURCES
"src/Platform/Unix/Mac/MacUtilities.cpp"
"src/Platform/Unix/UnixUtilities.cpp"
) )
set(MAC_INCLUDES set(MAC_INCLUDES
"src/Platform/Unix/Mac/MacUtilities.h"
"src/Platform/Unix/UnixUtilities.h"
) )
# Platform-specific definitions # Platform-specific definitions
@ -166,9 +132,6 @@ target_include_directories(HarmonyLinkLibStatic
) )
target_compile_definitions(HarmonyLinkLibStatic PRIVATE HARMONYLINKLIB_STATIC) target_compile_definitions(HarmonyLinkLibStatic PRIVATE HARMONYLINKLIB_STATIC)
# Include fmt headers
target_include_directories(HarmonyLinkLibStatic PRIVATE ${FMT_HEADERS})
# Set output directories for all build types # Set output directories for all build types
foreach(TYPE IN ITEMS DEBUG RELEASE) foreach(TYPE IN ITEMS DEBUG RELEASE)
string(TOUPPER ${TYPE} TYPE_UPPER) string(TOUPPER ${TYPE} TYPE_UPPER)
@ -182,10 +145,6 @@ foreach(TYPE IN ITEMS DEBUG RELEASE)
) )
endforeach() endforeach()
target_link_libraries(HarmonyLinkLibStatic PRIVATE fmt)
target_link_libraries(HarmonyLinkLibShared PRIVATE fmt)
if (UNIX) if (UNIX)
target_link_libraries(HarmonyLinkLibStatic PRIVATE stdc++fs)
target_link_libraries(HarmonyLinkLibShared PRIVATE stdc++fs)
endif() endif()

View file

@ -17,21 +17,15 @@
// Undefine the LINUX macro to avoid conflicts with the enum definition. // Undefine the LINUX macro to avoid conflicts with the enum definition.
#undef LINUX #undef LINUX
#include <cstdint> typedef enum
// Enum class for representing different types of devices
namespace HarmonyLinkLib
{ {
enum class EDevice : uint8_t EDevice_UNKNOWN,
{ EDevice_DESKTOP,
UNKNOWN, EDevice_LAPTOP,
DESKTOP, EDevice_HANDHELD,
LAPTOP,
HANDHELD,
STEAM_DECK, EDevice_STEAM_DECK,
// ROG_ALLY // EDevice_ROG_ALLY
// AYONEO_SLIDE // EDevice_AYONEO_SLIDE
// etc... // etc...
}; } EDevice;
}

View file

@ -1,29 +0,0 @@
// Copyright (c) 2024 Jordon Brooks
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include <cstdint>
// Enum class for representing different types of devices
namespace HarmonyLinkLib
{
enum class ESteamDeck : uint8_t
{
NONE, // Device is not a steam deck
UNKNOWN, // Device is a steam deck but model cannot be determined
LCD,
OLED,
};
}

View file

@ -12,4 +12,13 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include "Version.h" #pragma once
typedef enum
{
EPlatform_UNKNOWN,
EPlatform_WINDOWS,
EPlatform_LINUX,
EPlatform_MAC,
EPlatform_UNIX,
} EPlatform;

View file

@ -14,14 +14,10 @@
#pragma once #pragma once
struct HarmonyLinkStruct typedef enum
{ {
// Virtual destructor is important for proper cleanup of derived types ESteamDeck_NONE, // Device is not a steam deck
virtual ~HarmonyLinkStruct() = default; ESteamDeck_UNKNOWN, // Device is a steam deck but model cannot be determined
ESteamDeck_LCD,
// Method to deallocate the object ESteamDeck_OLED,
void free() const } ESteamDeck;
{
delete this;
}
};

View file

@ -1,177 +0,0 @@
// Copyright (c) 2024 Jordon Brooks
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include <memory>
#include <cstring>
#include <string>
#include "Core.h" // Replace with actual API export macro
/**
* @file FString.h
* @brief FString Class - Custom String Management for DLL Export
*
* The FString class is a custom string management class designed to safely encapsulate
* string handling and memory management, especially for use in DLLs (Dynamic Link Libraries).
* It resolves the common issue of exporting classes containing standard library types,
* such as std::string, across DLL boundaries, which can lead to compiler warnings or errors.
*
* Features:
* - Implements basic string operations such as construction, destruction, copy, and move semantics.
* - Utilizes smart pointers (std::unique_ptr) for automatic memory management, reducing the risk of memory leaks.
* - Provides a simple interface for interacting with character strings, similar to std::string.
* - Includes both deep copy semantics for safety and move semantics for performance in object transfers.
*
* Usage:
* FString can be used as a safer alternative to std::string for classes that are exported from a DLL.
* It handles memory allocation/deallocation automatically and provides a basic set of string operations.
*
* Example:
* FString myString("Hello, world!");
* std::cout << myString.c_str() << std::endl; // Output: Hello, world!
*
* Note:
* - The class is currently designed with basic functionality. Additional features such as
* string concatenation, substring, and comparison operations can be added as needed.
* - Ensure that the HarmonyLinkLibApi.h (or equivalent) file correctly defines the export macro
* for DLL usage.
*
*/
namespace HarmonyLinkLib
{
class HARMONYLINKLIB_API FString {
public:
FString() : data_(new char[1]) {
data_[0] = '\0';
}
FString(const char* str) {
const size_t len = strlen(str);
data_ = new char[len + 1];
memcpy(data_, str, len + 1);
}
// Copy constructor
FString(const FString& other) {
const size_t len = strlen(other.data_);
data_ = new char[len + 1];
memcpy(data_, other.data_, len + 1);
}
~FString() {
delete[] data_;
}
// Copy assignment operator
FString& operator=(const FString& other) {
if (this != &other) {
delete[] data_;
const size_t len = strlen(other.data_);
data_ = new char[len + 1];
memcpy(data_, other.data_, len + 1);
}
return *this;
}
// Concatenation operator for FString objects
FString operator+(const FString& other) const {
size_t thisLen = strlen(this->data_);
size_t otherLen = strlen(other.data_);
char* concatenated = new char[thisLen + otherLen + 1];
memcpy(concatenated, this->data_, thisLen);
memcpy(concatenated + thisLen, other.data_, otherLen + 1);
FString result(concatenated);
delete[] concatenated;
return result;
}
// Concatenation operator for const char*
FString operator+(const char* other) const {
return *this + FString(other);
}
// Friend function to allow concatenation with const char* on the left-hand side
friend FString operator+(const char* lhs, const FString& rhs) {
return FString(lhs) + rhs;
}
// Move constructor
FString(FString&& other) noexcept : data_(other.data_) {
other.data_ = nullptr;
}
FString(const std::string& str) {
const size_t len = str.length();
data_ = new char[len + 1];
memcpy(data_, str.c_str(), len + 1);
}
// Move assignment operator
FString& operator=(FString&& other) noexcept {
if (this != &other) {
delete[] data_;
data_ = other.data_;
other.data_ = nullptr;
}
return *this;
}
bool operator==(const FString& other) const {
return strcmp(data_, other.data_) == 0;
}
// Method to get a lowercase version of the string
static FString to_lower(FString& from)
{
for (size_t i = 0; i < strlen(from.data_); ++i) {
from.data_[i] = static_cast<char>(std::tolower(static_cast<unsigned char>(from.data_[i])));
}
return from;
}
// Overloaded static method to handle const char*
static FString to_lower(const char* from) {
FString temp_string(from); // Create an FString from const char*
return to_lower(temp_string); // Reuse the existing to_lower method
}
const char* c_str() const {
return data_;
}
private:
char* data_ = nullptr;
};
}
namespace std {
template<>
struct hash<HarmonyLinkLib::FString> {
size_t operator()(const HarmonyLinkLib::FString& s) const {
size_t hashValue = 5381; // Starting value recommended by the djb2 algorithm
const char* str = s.c_str();
for (size_t i = 0; str[i] != '\0'; ++i) {
hashValue = ((hashValue << 5) + hashValue) + static_cast<unsigned char>(str[i]);
// Equivalent to hashValue * 33 + str[i]
}
return hashValue;
}
};
}

View file

@ -1,4 +1,4 @@
// Copyright (c) 2024 Jordon Brooks // Copyright (c) 2024 Jordon Brooks
// //
// Licensed under the Apache License, Version 2.0 (the "License"); // Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. // you may not use this file except in compliance with the License.
@ -12,49 +12,9 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
/**
* IMPORTANT REMINDER:
* Do NOT use standard output functions like std::cout and printf anywhere in this codebase.
*
* Reason:
* Unreal Engine 5's packaging tool encounters issues with these functions, leading to
* packaging failures. The engine sets stdout to UTF-8, which can cause conflicts with
* these standard functions, resulting in a "SECURE CRT: Invalid parameter detected" error
* during packaging.
*
* This issue once required an extensive debugging effort that lasted over 8 hours.
* To prevent similar issues in the future and ensure smooth packaging, always use
* wide-character versions of these functions, such as wprintf and std::wcout, when working
* within the DLL. These functions are compatible with the UTF-8 setting in Unreal Engine 5.
*
*/
#pragma once #pragma once
#include <stdbool.h>
#include "Core.h" #include "Core.h"
#include "Structs/FBattery.h"
#include "Structs/FCPUInfo.h"
#include "Structs/FDevice.h"
#include "Structs/FOSVerInfo.h"
HARMONYLINKLIB_API bool HarmonyLink_Init(void);
class IPlatformUtilities;
namespace HarmonyLinkLib
{
extern "C" HARMONYLINKLIB_API bool get_is_wine();
extern "C" HARMONYLINKLIB_API bool get_is_linux();
extern "C" HARMONYLINKLIB_API bool get_is_docked();
extern "C" HARMONYLINKLIB_API FCPUInfo* get_cpu_info();
extern "C" HARMONYLINKLIB_API FDevice* get_device_info();
extern "C" HARMONYLINKLIB_API FOSVerInfo* get_os_version();
extern "C" HARMONYLINKLIB_API FBattery* get_battery_status();
}

View file

@ -14,17 +14,16 @@
#pragma once #pragma once
#include <cstdint> #include <stdbool.h>
// Enum class for representing different operating platforms #include "Core.h"
namespace HarmonyLinkLib
typedef struct
{ {
enum class EPlatform : uint8_t bool has_battery;
{ bool is_connected_to_ac;
UNKNOWN, unsigned char battery_percent;
WINDOWS, } FBattery;
LINUX,
MAC, HARMONYLINKLIB_API void FBattery_print(const FBattery* self);
UNIX,
};
}

View file

@ -1,36 +0,0 @@
// Copyright (c) 2024 Jordon Brooks
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include <cstdint>
#include <iostream>
#include "HarmonyLinkStruct.h"
namespace HarmonyLinkLib
{
struct FBattery : HarmonyLinkStruct
{
bool has_battery;
bool is_connected_to_ac;
uint8_t battery_percent;
void to_string() const {
std::wcout << "Battery present: " << (has_battery ? "'Yes'" : "'No'") << '\n';
std::wcout << "Connected to AC: " << (is_connected_to_ac ? "'Yes'" : "'No'") << '\n';
std::wcout << "Battery percent: '" << static_cast<int>(battery_percent) << "%'" << '\n';
}
};
}

View file

@ -1,54 +0,0 @@
// Copyright (c) 2024 Jordon Brooks
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include <cstdint>
#include <unordered_set>
#include "FString.h"
#include "HarmonyLinkStruct.h"
namespace HarmonyLinkLib
{
struct FCPUInfo : HarmonyLinkStruct
{
FString VendorID;
FString Model_Name;
uint32_t Physical_Cores = 0;
uint32_t Logical_Cores = 0;
std::unordered_set<FString> Flags;
void print() const
{
wprintf(L"VendorID: '%hs'\n", VendorID.c_str());
wprintf(L"Model_Name: '%hs'\n", Model_Name.c_str());
wprintf(L"Physical_Cores: '%d'\n", Physical_Cores);
wprintf(L"Logical_Cores: '%d'\n", Logical_Cores);
// Initialize a string to hold all flags
std::string allFlags;
for (const auto& flag : Flags) {
allFlags += std::string(flag.c_str()) + " "; // Append each flag followed by a space
}
// Remove the trailing space
if (!allFlags.empty()) {
allFlags.pop_back();
}
wprintf(L"Flags: '%hs'\n", allFlags.c_str());
}
};
}

View file

@ -1,32 +0,0 @@
// Copyright (c) 2024 Jordon Brooks
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include <HarmonyLinkStruct.h>
#include "Enums/EDevice.h"
#include "Enums/EPlatform.h"
#include "Enums/ESteamDeck.h"
namespace HarmonyLinkLib
{
// Struct to represent a specific device with both platform and device type
struct FDevice : HarmonyLinkStruct
{
EPlatform platform = EPlatform::UNKNOWN;
EDevice device = EDevice::UNKNOWN;
ESteamDeck steam_deck_model = ESteamDeck::NONE;
};
}

View file

@ -1,64 +0,0 @@
// Copyright (c) 2024 Jordon Brooks
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include "FString.h"
#include "HarmonyLinkStruct.h"
namespace HarmonyLinkLib
{
struct FOSVerInfo : HarmonyLinkStruct {
// 'name' represents the operating system's name, e.g., "Ubuntu" in Linux or "Windows" in Windows systems.
FString name;
// 'version' is a numerical representation of the OS version. In Linux, it might be the kernel version,
// whereas in Windows, it could be the major version number like 10 for Windows 10.
uint32_t version = 0;
// 'id' is a unique identifier for the OS. In Linux, it might be a lower-case string like "ubuntu".
// In Windows, this could map to the edition ID, such as "Professional".
FString id;
// 'version_id' is a string representing the specific version of the OS.
// For example, "20.04" in Ubuntu or "1909" in Windows 10.
FString version_id;
// 'version_codename' is a codename for the OS version, if available.
// This is common in Linux distributions (e.g., "focal" for Ubuntu 20.04) but not typically used in Windows.
FString version_codename;
// 'pretty_name' is a more readable version of the name, potentially including the version and codename.
// For example, "Ubuntu 20.04 LTS (Focal Fossa)" or "Windows 10 Pro".
FString pretty_name;
// 'variant_id' is an identifier for the specific variant of the OS, if available.
// For example, in Linux distributions, this might be "desktop" for the desktop edition,
// "server" for the server edition, or "core" for a minimal installation. It helps distinguish
// between different flavors or editions of the same version.
// On the Steam Deck, this is set to "steamdeck".
FString variant_id;
void print() const
{
wprintf(L"Name: '%hs'\n", name.c_str());
wprintf(L"Version: '%d'\n", version);
wprintf(L"ID: '%hs'\n", id.c_str());
wprintf(L"Version ID: '%hs'\n", version_id.c_str());
wprintf(L"Version Codename: '%hs'\n", version_codename.c_str());
wprintf(L"Pretty Name: '%hs'\n", pretty_name.c_str());
wprintf(L"Variant ID: '%hs'\n", variant_id.c_str());
}
};
}

View file

@ -14,45 +14,11 @@
#pragma once #pragma once
#include <stdbool.h>
#include "Core.h" #include "Core.h"
#include <Version.generated.h>
#include "FString.h" HARMONYLINKLIB_API char* get_version_string(void);
HARMONYLINKLIB_API char* get_version_build_timestamp(void);
namespace HarmonyLinkLib HARMONYLINKLIB_API char* get_git_branch(void);
{ HARMONYLINKLIB_API char* get_git_commit_timestamp(void);
class HARMONYLINKLIB_API version HARMONYLINKLIB_API bool get_is_debug(void);
{
public:
version() = default;
static FString ToString()
{
return HARMONYLINK_VERSION;
}
static FString get_build_timestamp()
{
return {__TIMESTAMP__};
}
static FString get_git_branch()
{
return {GIT_BRANCH_NAME};
}
static FString get_git_commit_timestamp()
{
return {GIT_COMMIT_TIMESTAMP};
}
static bool is_debug()
{
#ifdef DEBUG_MODE
return true;
#else
return false;
#endif
}
};
}

View file

@ -12,27 +12,17 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include "LinuxUtilities.h" #include "HarmonyLinkLib.h"
#include <wchar.h>
#include "Version.h"
#include <fstream> bool HarmonyLink_Init(void)
#include <string>
#include "Platform/WineUtilities.h"
namespace HarmonyLinkLib
{ {
std::shared_ptr<FBattery> LinuxUtilities::get_battery_status() wprintf(L"HarmonyLink V%hs Copyright (C) 2023 Jordon Brooks\n", get_version_string());
{ wprintf(L"Build Timestamp: %hs\n", get_version_build_timestamp());
return WineUtilities::get_battery_status(); wprintf(L"Git Branch: %hs\n", get_git_branch());
} wprintf(L"Git Commit Timestamp: %hs\n", get_git_commit_timestamp());
wprintf(L"Build type: %ls\n", get_is_debug() ? L"True" : L"False");
std::shared_ptr<FOSVerInfo> LinuxUtilities::get_os_version() return 1;
{
return WineUtilities::get_linux_info();
}
std::shared_ptr<FCPUInfo> LinuxUtilities::get_cpu_info()
{
return WineUtilities::get_cpu_info();
}
} }

View file

@ -1,130 +0,0 @@
// Copyright (c) 2024 Jordon Brooks
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "HarmonyLinkLib.h"
#include <iostream>
#include "Platform/IPlatformUtilities.h"
namespace HarmonyLinkLib
{
std::shared_ptr<IPlatformUtilities> PlatformUtilities = IPlatformUtilities::GetInstance();
bool get_is_wine()
{
if (!PlatformUtilities)
{
std::wcout << "Failed to get platform utilities!\n";
return false;
}
return PlatformUtilities->is_running_under_wine();
}
bool get_is_linux()
{
if (!PlatformUtilities)
{
std::wcout << "Failed to get platform utilities!\n";
return false;
}
return PlatformUtilities->is_linux();
}
bool get_is_docked()
{
if (!PlatformUtilities)
{
std::wcout << "Failed to get platform utilities!\n";
return false;
}
return PlatformUtilities->is_docked();
}
FCPUInfo* get_cpu_info()
{
if (!PlatformUtilities)
{
std::wcout << "Failed to get platform utilities!\n";
return nullptr;
}
const std::shared_ptr<FCPUInfo> cpu_info = PlatformUtilities->get_cpu_info();
if (!cpu_info)
{
return nullptr;
}
FCPUInfo* new_cpu_info = new FCPUInfo(*cpu_info);
return new_cpu_info;
}
FDevice* get_device_info()
{
if (!PlatformUtilities)
{
std::wcout << "Failed to get platform utilities!\n";
return nullptr;
}
const std::shared_ptr<FDevice> device = PlatformUtilities->get_device();
if (!device)
{
return nullptr;
}
FDevice* new_device = new FDevice(*device);
return new_device;
}
FOSVerInfo* get_os_version()
{
if (!PlatformUtilities)
{
std::wcout << "Failed to get platform utilities!\n";
return nullptr;
}
const std::shared_ptr<FOSVerInfo> os_version_info = PlatformUtilities->get_os_version();
if (!os_version_info)
{
return nullptr;
}
FOSVerInfo* new_os_info = new FOSVerInfo(*os_version_info);
return new_os_info;
}
FBattery* get_battery_status()
{
if (!PlatformUtilities)
{
std::wcout << "Failed to get platform utilities!\n";
return nullptr;
}
const std::shared_ptr<FBattery> new_battery = PlatformUtilities->get_battery_status();
if (!new_battery)
{
return nullptr;
}
FBattery* battery = new FBattery(*new_battery);
return battery;
}
}

View file

@ -1,245 +0,0 @@
// Copyright (c) 2024 Jordon Brooks
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "IPlatformUtilities.h"
#include <fmt/core.h>
#include <set>
#include <unordered_map>
#include "Utilities.h"
#include "WineUtilities.h"
#if BUILD_WINDOWS
#include "Windows/WindowsUtilities.h"
#elif BUILD_LINUX
#include "Unix/Linux/LinuxUtilities.h"
#elif BUILD_MAC
#include "Unix/Mac/MacUtilities.h"
#elif BUILD_UNIX
#include "Unix/Mac/MacUtilities.h"
#endif
namespace HarmonyLinkLib
{
static std::shared_ptr<IPlatformUtilities> INSTANCE = nullptr;
std::shared_ptr<IPlatformUtilities>& IPlatformUtilities::GetInstance()
{
if (!INSTANCE)
{
#if BUILD_WINDOWS
INSTANCE = std::make_shared<WindowsUtilities>();
#elif BUILD_LINUX
INSTANCE = std::make_shared<LinuxUtilities>();
#elif BUILD_MAC
INSTANCE = std::make_shared<MacUtilities>();
#elif BUILD_UNIX
INSTANCE = std::make_shared<UnixUtilities>();
// ... other platform checks
#else
std::wcout << "Platform is not supported.\n"
#endif
}
return INSTANCE;
}
bool IPlatformUtilities::is_running_under_wine()
{
return WineUtilities::is_wine_present();
}
bool IPlatformUtilities::is_linux()
{
#ifdef BUILD_LINUX
return true;
#else
return is_running_under_wine();
#endif
}
bool IPlatformUtilities::is_steam_deck()
{
const std::shared_ptr<FDevice> device = get_device();
return device && device->device == EDevice::STEAM_DECK;
}
bool IPlatformUtilities::is_docked()
{
static constexpr uint8_t CHARGING_SCORE = 3;
static constexpr uint8_t EXTERNAL_MONITOR_SCORE = 4;
static constexpr uint8_t STEAM_DECK_RESOLUTION_SCORE = 3;
static constexpr uint8_t KEYBOARD_DETECTION_SCORE = 1;
static constexpr uint8_t MOUSE_DETECTION_SCORE = 2;
static constexpr uint8_t CONTROLLER_DETECTION_SCORE = 3;
static constexpr uint8_t FINAL_TARGET_DETECTION_SCORE = 9;
const std::shared_ptr<FDevice> device = get_device();
if (!device)
{
std::wcout << "Error: failed to get device.\n";
return false;
}
if (device->device != EDevice::STEAM_DECK)
{
std::wcout << "Error: Dock detection is currently only supported on Steam Decks.\n";
return false;
}
uint8_t score = 0;
Utilities::DebugPrint("Detected: ", false);
if (is_charging())
{
Utilities::DebugPrint("Charging, ", false);
score += CHARGING_SCORE;
}
if (get_is_external_monitor_connected())
{
Utilities::DebugPrint("External monitor, ", false);
score += EXTERNAL_MONITOR_SCORE;
}
if (get_is_steam_deck_native_resolution())
{
Utilities::DebugPrint("Non-native resolution, ", false);
score += STEAM_DECK_RESOLUTION_SCORE;
}
if (get_keyboard_detected())
{
Utilities::DebugPrint("keyboard ", false);
score += KEYBOARD_DETECTION_SCORE;
}
if (get_mouse_detected())
{
Utilities::DebugPrint("mouse, ", false);
score += MOUSE_DETECTION_SCORE;
}
if (get_external_controller_detected())
{
Utilities::DebugPrint("external controller, ", false);
score += CONTROLLER_DETECTION_SCORE;
}
Utilities::DebugPrint(fmt::format("Score: {}/{}", score, FINAL_TARGET_DETECTION_SCORE).c_str());
return score >= FINAL_TARGET_DETECTION_SCORE;
}
std::shared_ptr<FDevice> IPlatformUtilities::get_device()
{
FDevice new_device;
if (is_linux())
{
new_device.platform = EPlatform::LINUX;
}
else
{
new_device.platform = EPlatform::WINDOWS;
}
const std::shared_ptr<FBattery> battery_status = get_battery_status();
if (battery_status && !battery_status->has_battery)
{
new_device.device = EDevice::DESKTOP;
}
else
{
new_device.device = EDevice::LAPTOP;
}
const ESteamDeck steam_deck_model = detect_steam_deck(new_device);
if (steam_deck_model != ESteamDeck::NONE) {
new_device.device = EDevice::STEAM_DECK;
new_device.steam_deck_model = steam_deck_model;
}
return std::make_shared<FDevice>(new_device);
}
// Helper function to check if the device is a Steam Deck
ESteamDeck IPlatformUtilities::detect_steam_deck(const FDevice& device) {
// Check if the device is already identified as a Steam Deck
if (device.device == EDevice::STEAM_DECK && device.steam_deck_model != ESteamDeck::NONE) {
return device.steam_deck_model;
}
ESteamDeck steam_deck_model = ESteamDeck::NONE;
// Retrieve and process CPU information
const std::shared_ptr<FCPUInfo> cpu_info = get_cpu_info();
if (!cpu_info) {
wprintf(L"CPU information not available.\n");
} else {
// Convert the CPU model name to lower case once
FString cpu_model_lower = FString::to_lower(cpu_info->Model_Name);
// Map of CPU models to their corresponding Steam Deck models
static const std::unordered_map<FString, ESteamDeck> model_map = {
{FString::to_lower("amd custom apu 0405"), ESteamDeck::LCD},
{FString::to_lower("amd custom apu 0932"), ESteamDeck::OLED}
};
auto iterator = model_map.find(cpu_model_lower);
if (iterator != model_map.end()) {
steam_deck_model = iterator->second;
wprintf(L"Steam Deck detected by CPU model name: %hs.\n", cpu_model_lower.c_str());
}
}
// Check for Steam Deck by OS version only if no model has been detected yet
if (steam_deck_model == ESteamDeck::NONE)
{
if (const std::shared_ptr<FOSVerInfo> version = get_os_version())
{
if (version->variant_id == "steamdeck" && version->name == "SteamOS")
{
// Use UNKNOWN if OS matches but CPU model doesn't fit known profiles
steam_deck_model = ESteamDeck::UNKNOWN;
wprintf(L"Steam Deck OS detected but model is unknown.\n");
}
}
else
{
wprintf(L"OS version information not available.\n");
}
}
return steam_deck_model;
}
bool IPlatformUtilities::is_connected_to_ac()
{
const std::shared_ptr<FBattery> battery = get_battery_status();
return battery && battery->is_connected_to_ac;
}
bool IPlatformUtilities::is_charging()
{
const std::shared_ptr<FBattery> battery = get_battery_status();
return battery && battery->has_battery && battery->is_connected_to_ac;
}
}

View file

@ -1,60 +0,0 @@
// Copyright (c) 2024 Jordon Brooks
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include "Enums/ESteamDeck.h"
#include "Structs/FBattery.h"
#include "Structs/FCPUInfo.h"
#include "Structs/FDevice.h"
#include "Structs/FOSVerInfo.h"
namespace HarmonyLinkLib
{
class IPlatformUtilities {
public:
static std::shared_ptr<IPlatformUtilities>& GetInstance();
IPlatformUtilities() = default;
IPlatformUtilities(const IPlatformUtilities& other) = default;
IPlatformUtilities(IPlatformUtilities&& other) = default;
IPlatformUtilities& operator=(const IPlatformUtilities& other) = default;
IPlatformUtilities& operator=(IPlatformUtilities&& other) = default;
virtual ~IPlatformUtilities() = default;
// General OS-level functions
virtual bool is_running_under_wine();
virtual bool is_linux();
virtual bool is_steam_deck();
virtual bool is_docked();
virtual std::shared_ptr<FDevice> get_device();
virtual std::shared_ptr<FCPUInfo> get_cpu_info() = 0;
virtual std::shared_ptr<FBattery> get_battery_status() = 0;
virtual std::shared_ptr<FOSVerInfo> get_os_version() = 0;
virtual bool get_is_external_monitor_connected() = 0;
virtual bool get_keyboard_detected() = 0;
virtual bool get_mouse_detected() = 0;
virtual bool get_external_controller_detected() = 0;
virtual bool get_is_steam_deck_native_resolution() = 0;
//virtual bool get_is_ethernet_connected() = 0;
//virtual bool get_is_external_input_detected() = 0;
ESteamDeck detect_steam_deck(const FDevice& device);
bool is_connected_to_ac();
bool is_charging();
// Add more virtual functions for other OS interactions here
};
}

View file

@ -1,37 +0,0 @@
// Copyright (c) 2024 Jordon Brooks
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include "Structs/FOSVerInfo.h"
#include "Platform/Unix/UnixUtilities.h"
namespace HarmonyLinkLib
{
class LinuxUtilities : public UnixUtilities {
public:
// Implementation for other Linux-specific functions
std::shared_ptr<FBattery> get_battery_status() override;
std::shared_ptr<FOSVerInfo> get_os_version() override;
std::shared_ptr<FCPUInfo> get_cpu_info() override;
bool is_linux() override
{
return true;
}
};
}

View file

@ -1,72 +0,0 @@
// Copyright (c) 2024 Jordon Brooks
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "UnixUtilities.h"
namespace HarmonyLinkLib
{
bool UnixUtilities::is_running_under_wine()
{
std::wcout << "This feature is not supported on unix systems yet.\n";
return false;
}
std::shared_ptr<FCPUInfo> UnixUtilities::get_cpu_info()
{
std::wcout << "This feature is not supported on unix systems yet.\n";
return nullptr;
}
std::shared_ptr<FBattery> UnixUtilities::get_battery_status()
{
std::wcout << "This feature is not supported on unix systems yet.\n";
return nullptr;
}
std::shared_ptr<FOSVerInfo> UnixUtilities::get_os_version()
{
std::wcout << "This feature is not supported on unix systems yet.\n";
return nullptr;
}
bool UnixUtilities::get_is_external_monitor_connected()
{
std::wcout << "This feature is not supported on unix-based systems yet.\n";
return false;
}
bool UnixUtilities::get_keyboard_detected()
{
std::wcout << "This feature is not supported on unix-based systems yet.\n";
return false;
}
bool UnixUtilities::get_mouse_detected()
{
std::wcout << "This feature is not supported on unix-based systems yet.\n";
return false;
}
bool UnixUtilities::get_external_controller_detected()
{
std::wcout << "This feature is not supported on unix-based systems yet.\n";
return false;
}
bool UnixUtilities::get_is_steam_deck_native_resolution()
{
std::wcout << "This feature is not supported on unix-based systems yet.\n";
return false;
}
}

View file

@ -1,42 +0,0 @@
// Copyright (c) 2024 Jordon Brooks
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include "Platform/IPlatformUtilities.h"
namespace HarmonyLinkLib
{
// This is more of a "catch all" for all unix-based systems
// that don't have their own implementations.
class UnixUtilities : public IPlatformUtilities {
public:
bool is_running_under_wine() override;
std::shared_ptr<FCPUInfo> get_cpu_info() override;
std::shared_ptr<FBattery> get_battery_status() override;
std::shared_ptr<FOSVerInfo> get_os_version() override;
bool get_is_external_monitor_connected() override;
bool get_keyboard_detected() override;
bool get_mouse_detected() override;
bool get_external_controller_detected() override;
bool get_is_steam_deck_native_resolution() override;
// Implementation for other Unix/Linux-specific functions
};
}

View file

@ -1,177 +0,0 @@
// Copyright (c) 2024 Jordon Brooks
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "WindowsUtilities.h"
#include <sstream>
#include <Windows.h>
#include <XInput.h>
#pragma comment(lib, "XInput.lib")
#include "Platform/WineUtilities.h"
#include <algorithm>
namespace HarmonyLinkLib
{
std::shared_ptr<FBattery> WindowsUtilities::get_battery_status()
{
if (is_linux())
{
return WineUtilities::get_battery_status();
}
FBattery result;
SYSTEM_POWER_STATUS status;
if (GetSystemPowerStatus(&status)) {
result.has_battery = status.BatteryFlag != 128; // 128 indicates no battery
result.is_connected_to_ac = status.ACLineStatus == 1;
result.battery_percent = result.has_battery ? status.BatteryLifePercent : 0;
} else {
std::wcout << "Failed to get power statistics.\n";
}
return std::make_shared<FBattery>(result);
}
std::shared_ptr<FCPUInfo> WindowsUtilities::get_cpu_info()
{
if (is_linux())
{
return WineUtilities::get_cpu_info();
}
return {};
}
std::shared_ptr<FOSVerInfo> WindowsUtilities::get_os_version()
{
if (is_linux())
{
return WineUtilities::get_linux_info();
}
OSVERSIONINFOEX os_version_info;
ZeroMemory(&os_version_info, sizeof(OSVERSIONINFOEX));
os_version_info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
if (!GetVersionEx(reinterpret_cast<OSVERSIONINFO*>(&os_version_info))) {
// Handle error if needed
return nullptr;
}
FOSVerInfo os_version;
HKEY h_key;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion"), 0, KEY_READ, &h_key) == ERROR_SUCCESS)
{
DWORD dw_size;
DWORD dw_type;
char sz_product_name[256];
dw_size = sizeof(sz_product_name);
if (RegQueryValueEx(h_key, TEXT("ProductName"), nullptr, &dw_type, reinterpret_cast<LPBYTE>(sz_product_name), &dw_size) == ERROR_SUCCESS)
{
os_version.pretty_name = sz_product_name;
}
RegCloseKey(h_key);
}
std::stringstream version;
version << os_version_info.dwMajorVersion << "." << os_version_info.dwMinorVersion;
os_version.version_id = version.str();
os_version.name = "Windows";
os_version.version = os_version_info.dwBuildNumber; // Build number as the version
return std::make_shared<FOSVerInfo>(os_version);
}
bool WindowsUtilities::get_is_external_monitor_connected()
{
// SM_CMONITORS returns the count of all display monitors.
const int monitorCount = GetSystemMetrics(SM_CMONITORS);
// More than one monitor implies an external monitor is connected.
return monitorCount > 1;
}
bool WindowsUtilities::get_keyboard_detected()
{
UINT n_devices;
std::vector<RAWINPUTDEVICELIST> devices;
GetRawInputDeviceList(devices.data(), &n_devices, sizeof(RAWINPUTDEVICELIST));
if (n_devices == 0)
{
return false;
}
return std::any_of(devices.begin(), devices.end(), [](const RAWINPUTDEVICELIST& device)
{
return device.dwType == RIM_TYPEKEYBOARD;
});
}
bool WindowsUtilities::get_mouse_detected()
{
UINT n_devices;
std::vector<RAWINPUTDEVICELIST> devices;
GetRawInputDeviceList(devices.data(), &n_devices, sizeof(RAWINPUTDEVICELIST));
if (n_devices == 0)
{
return false;
}
return std::any_of(devices.begin(), devices.end(), [](const RAWINPUTDEVICELIST& device)
{
return device.dwType == RIM_TYPEMOUSE;
});
}
bool WindowsUtilities::get_external_controller_detected()
{
static_assert(XUSER_MAX_COUNT <= UINT8_MAX, "XUSER_MAX_COUNT exceeds uint8_t size");
uint8_t connectedGamepads = 0;
for (DWORD i = 0; i < XUSER_MAX_COUNT; ++i) {
XINPUT_STATE state;
ZeroMemory(&state, sizeof(XINPUT_STATE));
if (XInputGetState(i, &state) == ERROR_SUCCESS) {
connectedGamepads++;
}
}
return connectedGamepads > 1;
}
bool WindowsUtilities::get_is_steam_deck_native_resolution()
{
DEVMODE devMode;
devMode.dmSize = sizeof(DEVMODE);
// Get the current display settings for the primary monitor
if (EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &devMode)) {
// Check if the resolution is higher than 800p (1280x800)
if (devMode.dmPelsWidth > 1280 || devMode.dmPelsHeight > 800) {
return true;
}
}
return false;
}
}

View file

@ -1,40 +0,0 @@
// Copyright (c) 2024 Jordon Brooks
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include "Platform/IPlatformUtilities.h"
namespace HarmonyLinkLib
{
class WindowsUtilities : public IPlatformUtilities
{
public:
std::shared_ptr<FBattery> get_battery_status() override;
std::shared_ptr<FCPUInfo> get_cpu_info() override;
std::shared_ptr<FOSVerInfo> get_os_version() override;
bool get_is_external_monitor_connected() override;
bool get_keyboard_detected() override;
bool get_mouse_detected() override;
bool get_external_controller_detected() override;
bool get_is_steam_deck_native_resolution() override;
};
}

View file

@ -1,216 +0,0 @@
// Copyright (c) 2024 Jordon Brooks
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "WineUtilities.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include <unordered_map>
#include <filesystem>
#ifdef BUILD_WINDOWS
#include <windows.h>
#endif
namespace HarmonyLinkLib
{
bool force_detect_wine = false;
std::shared_ptr<HarmonyLinkLib::FBattery> HarmonyLinkLib::WineUtilities::get_battery_status()
{
std::string append;
if (is_wine_present())
{
append = "Z:";
}
FBattery result = {};
for (int i = 0; i <= 9; ++i) {
if (std::string bat_path = append + "/sys/class/power_supply/BAT" + std::to_string(i); std::filesystem::exists(bat_path)) {
result.has_battery = true;
std::ifstream status_file(bat_path + "/status");
std::string status;
if (status_file.is_open() && std::getline(status_file, status)) {
if (status == "Charging" || status == "AC") {
result.is_connected_to_ac = true;
}
}
std::ifstream capacity_file(bat_path + "/capacity");
if (capacity_file.is_open() && std::getline(capacity_file, status)) {
result.battery_percent = static_cast<uint8_t>(std::stoi(status));
break; // assuming you only need data from the first battery found
}
}
}
return std::make_shared<FBattery>(result);
}
std::shared_ptr<FCPUInfo> WineUtilities::get_cpu_info()
{
std::wcout << "Getting cpu info\n";
std::string append;
if (is_wine_present())
{
append = "Z:";
}
FCPUInfo cpu_info;
std::ifstream file(append + "/proc/cpuinfo");
std::unordered_map<HarmonyLinkLib::FString, HarmonyLinkLib::FString> hashmap;
if (file) {
std::string line;
while (std::getline(file, line)) {
std::istringstream line_stream(line);
std::string key, value;
if (std::getline(line_stream, key, ':')) {
key.erase(key.find_last_not_of(" \t") + 1); // Trim trailing whitespace from key
if (std::getline(line_stream, value)) {
value.erase(0, value.find_first_not_of(" \t")); // Trim leading whitespace from value
// Aggregate flags
if (key == "flags") {
std::istringstream flag_stream(value);
std::string flag;
while (flag_stream >> flag) {
flag.erase(flag.find_last_not_of(" \t") + 1); // Trim trailing whitespace from flag
cpu_info.Flags.insert(HarmonyLinkLib::FString(flag));
//printf("Flag detected: %s\n", flag.c_str());
}
} else {
hashmap[key] = value;
}
}
}
}
file.close();
}
// Now you can access the values using the keys:
cpu_info.VendorID = hashmap["vendor_id"];
cpu_info.Model_Name = hashmap["model name"];
try {
cpu_info.Physical_Cores = std::stoi(hashmap["cpu cores"].c_str());
} catch (const std::invalid_argument& ia) {
std::wcerr << "Invalid argument: " << ia.what() << '\n';
} catch (const std::out_of_range& oor) {
std::wcerr << "Out of Range error: " << oor.what() << '\n';
}
cpu_info.Logical_Cores = (cpu_info.Flags.find("ht") != cpu_info.Flags.end()) ? cpu_info.Physical_Cores * 2 : cpu_info.Physical_Cores;
return std::make_shared<FCPUInfo>(cpu_info);
}
std::shared_ptr<FOSVerInfo> WineUtilities::get_linux_info()
{
std::string append;
if (is_wine_present())
{
append = "Z:";
}
FOSVerInfo os_info;
std::ifstream file(append + "/etc/os-release");
std::unordered_map<HarmonyLinkLib::FString, HarmonyLinkLib::FString> hashmap;
if (file) {
std::string line;
while (std::getline(file, line)) {
std::istringstream lineStream(line);
std::string key, value;
if (std::getline(lineStream, key, '=')) {
if (std::getline(lineStream, value)) {
// Remove leading and trailing whitespace
size_t firstNonSpace = value.find_first_not_of(" \t");
size_t lastNonSpace = value.find_last_not_of(" \t");
if (firstNonSpace != std::string::npos && lastNonSpace != std::string::npos) {
value = value.substr(firstNonSpace, lastNonSpace - firstNonSpace + 1);
} else {
value.clear(); // If value is all whitespace, make it empty
}
// Check for double quotes and remove them
if (!value.empty() && value.front() == '"' && value.back() == '"') {
value = value.substr(1, value.length() - 2);
}
hashmap[key] = value;
}
}
}
file.close();
}
// Now you can access the values using the keys:
os_info.name = hashmap["NAME"];
os_info.id = hashmap["ID"];
os_info.version_id = hashmap["VERSION_ID"];
os_info.version_codename = hashmap["VERSION_CODENAME"];
os_info.pretty_name = hashmap["PRETTY_NAME"];
try {
os_info.version = std::stoi(hashmap["VERSION"].c_str());
} catch (const std::invalid_argument& ia) {
std::cerr << "Invalid argument: " << ia.what() << '\n';
// Handle the error, perhaps by setting a default value or leaving the field unchanged
} catch (const std::out_of_range& oor) {
std::cerr << "Out of Range error: " << oor.what() << '\n';
// Handle the error, perhaps by setting a default value or leaving the field unchanged
}
return std::make_shared<FOSVerInfo>(os_info);
}
bool WineUtilities::is_wine_present()
{
static bool isWineCached = false; // Static variable to store the cached result
static bool isWine = false; // Static variable to indicate if the caching has been done
if (!isWineCached)
{
// Only detect wine if force_detect_wine is true or if we haven't cached the result yet
#ifdef BUILD_WINDOWS
std::wcout << "Detecting wine...\n";
bool HasFound = GetProcAddress(GetModuleHandle("ntdll.dll"), "wine_get_version") != nullptr;
if (!HasFound)
HasFound = GetProcAddress(GetModuleHandle("ntdll.dll"), "proton_get_version") != nullptr;
wprintf(L"wine %s found\n", HasFound ? L"has been" : L"not");
isWine = HasFound; // Cache the result
#else
isWine = false; // In non-Windows builds, always set isWine to false
#endif
isWineCached = true; // Indicate that the result is now cached
}
return isWine; // Return the cached result
}
bool WineUtilities::force_detect_wine_presence()
{
force_detect_wine = true;
return is_wine_present();
}
}

View file

@ -1,60 +0,0 @@
// Copyright (c) 2024 Jordon Brooks
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include "Structs/FBattery.h"
#include "Structs/FCPUInfo.h"
#include "Structs/FOSVerInfo.h"
namespace HarmonyLinkLib
{
class WineUtilities
{
public:
static std::shared_ptr<FBattery> get_battery_status();
static std::shared_ptr<FCPUInfo> get_cpu_info();
/**
* @brief Retrieves Linux OS version information from a specified file.
*
* This function parses a file (typically a Linux OS release file) at the given location
* to extract operating system version information. It reads key-value pairs from the file,
* processes them to handle whitespace and quotes, and then stores them in an FOSVerInfo
* structure. If the file location is invalid or the file cannot be opened, it returns an
* empty FOSVerInfo structure. Errors during parsing, such as invalid format or out of range
* values, are handled with exception catching. In Windows builds where Wine is detected,
* this function can use the file location 'Z:/etc/os-release' to retrieve the underlying
* Linux system information.
*
* @param file_location The location of the file containing OS version information.
* @return A shared pointer to a structure containing the parsed OS version information.
*/
static std::shared_ptr<FOSVerInfo> get_linux_info();
/**
* @brief Detects the presence of Wine or Proton in Windows builds.
*
* This function assesses if the application is running under Wine or Proton by
* querying specific functions in the 'ntdll.dll' module. It is only active in
* Windows builds, returning false for non-Windows builds.
*
* @return bool True if Wine or Proton is detected, false otherwise.
*/
static bool is_wine_present();
static bool force_detect_wine_presence();
};
}

View file

@ -12,14 +12,13 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#pragma once #include "Structs/Battery.h"
#include "Platform/Unix/UnixUtilities.h" #include <wchar.h>
namespace HarmonyLinkLib void FBattery_print(const FBattery* self)
{ {
class MacUtitities : public UnixUtilities { wprintf(L"Battery present: %ls\n", self->has_battery ? L"'Yes'" : L"'No'");
public: wprintf(L"Connected to AC: %ls\n", self->is_connected_to_ac ? L"'Yes'" : L"'No'");
// Mac-specific overrides and additional functionality wprintf(L"Battery percent: %ls\n", self->battery_percent ? L"'Yes'" : L"'No'");
};
} }

View file

@ -1,53 +0,0 @@
// Copyright (c) 2024 Jordon Brooks
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "Utilities.h"
#include <iostream>
#include "FString.h"
void HarmonyLinkLib::Utilities::DebugPrint(const FString& String, bool AddNewline)
{
#ifdef DEBUG_MODE
std::wcout << String.c_str();
if (AddNewline)
{
std::wcout << L"\n";
}
#endif
}
void HarmonyLinkLib::Utilities::DebugPrint(const char* String, bool AddNewline)
{
#ifdef DEBUG_MODE
std::wcout << std::wstring(String, String + std::strlen(String));
if (AddNewline) {
std::wcout << L"\n";
}
#endif
}
void HarmonyLinkLib::Utilities::DebugPrint(const wchar_t* String, bool AddNewline)
{
#ifdef DEBUG_MODE
std::wcout << String;
if (AddNewline) {
std::wcout << L"\n";
}
#endif
}

View file

@ -12,19 +12,35 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#pragma once #include "Version.h"
#include "HarmonyLinkLib.h" #include <Version.generated.h>
namespace HarmonyLinkLib char* get_version_string(void)
{ {
class FString; return HARMONYLINK_VERSION;
}
class Utilities
{ char* get_version_build_timestamp(void)
public: {
static void DebugPrint(const FString& String, bool AddNewline = true); return __TIMESTAMP__;
static void DebugPrint(const char* String, bool AddNewline = true); }
static void DebugPrint(const wchar_t* String, bool AddNewline = true);
}; char* get_git_branch(void)
{
return GIT_BRANCH_NAME;
}
char* get_git_commit_timestamp(void)
{
return GIT_COMMIT_TIMESTAMP;
}
bool get_is_debug(void)
{
#ifdef DEBUG_MODE
return true;
#else
return false;
#endif
} }

View file

View file

@ -1,61 +0,0 @@
// Copyright (c) 2024 Jordon Brooks
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <iostream>
#include "Version.h"
namespace HarmonyLinkLib
{
void HarmonyLinkInit()
{
std::wcout << "HarmonyLink V" << version::ToString().c_str() << " Copyright (C) 2023 Jordon Brooks\n";
}
}
#if BUILD_WINDOWS
#include <windows.h>
// Standard DLL entry point
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
switch (fdwReason) {
case DLL_PROCESS_ATTACH:
// Code to run when the DLL is loaded
HarmonyLinkLib::HarmonyLinkInit();
break;
case DLL_THREAD_ATTACH:
// Code to run when a thread is created during the DLL's lifetime
case DLL_THREAD_DETACH:
// Code to run when a thread ends normally.
case DLL_PROCESS_DETACH:
// Code to run when the DLL is unloaded
default:
break;
}
return TRUE; // Successful DLL_PROCESS_ATTACH.
}
#endif
#if BUILD_UNIX
__attribute__((constructor))
static void onLibraryLoad() {
// Code to run when the library is loaded
HarmonyLinkLib::HarmonyLinkInit();
}
__attribute__((destructor))
static void onLibraryUnload() {
// Code to run when the library is unloaded
}
#endif

View file

@ -15,11 +15,11 @@ cmake_minimum_required(VERSION 3.10)
project(HarmonyLinkTest) project(HarmonyLinkTest)
# Specify the C++ standard # Specify the C++ standard
set(CMAKE_CXX_STANDARD 17) set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True) set(CMAKE_C_STANDARD_REQUIRED True)
# Automatically add all .cpp and .h/.hpp files in the src directory # Automatically add all .cpp and .h/.hpp files in the src directory
file(GLOB_RECURSE TEST_SOURCES "src/*.cpp") file(GLOB_RECURSE TEST_SOURCES "src/*.c")
file(GLOB_RECURSE TEST_HEADERS "src/*.h" "src/*.hpp") file(GLOB_RECURSE TEST_HEADERS "src/*.h" "src/*.hpp")
# Add executable for static library # Add executable for static library

View file

@ -12,9 +12,20 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include "MacUtilities.h" #include <wchar.h>
#include "HarmonyLinkLib.h"
namespace HarmonyLinkLib int main(void)
{ {
wprintf(L"Hello from C!\n");
} if (!HarmonyLink_Init())
{
wprintf(L"Error: Failed to initialise HarmonyLink!\n");
return 1;
}
wprintf(L"Successfully Initialised HarmonyLink!\n");
return 0;
}

View file

@ -1,161 +0,0 @@
// Copyright (c) 2024 Jordon Brooks
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <iostream>
#include <chrono>
#include <thread>
#include <atomic>
#include "HarmonyLinkLib.h"
// Include necessary headers for platform-specific functionality
#ifdef BUILD_WINDOWS
#include <conio.h> // For _kbhit() and _getch()
#include <windows.h> // For system("cls")
#else
#include <unistd.h> // For read()
#include <termios.h> // For termios
#include <stdio.h> // For getchar()
#include <fcntl.h> // For F_GETFL, F_SETFL and O_NONBLOCK
#endif
std::atomic<bool> quitFlag(false);
// Function to clear the screen cross-platform
void clearScreen() {
#ifdef _WIN32
system("cls");
#else
std::cout << "\x1B[2J\x1B[H";
#endif
}
// Function to check if 'q' or 'Q' is pressed in Windows
void checkForQuit() {
while (!quitFlag) {
#ifdef BUILD_WINDOWS
if (_kbhit()) {
const char c = static_cast<char>(_getch());
if (c == 'q' || c == 'Q') {
quitFlag = true;
break;
}
}
#else
struct termios oldt, newt;
int ch;
int oldf;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
fcntl(STDIN_FILENO, F_SETFL, oldf);
if (ch != EOF) {
ungetc(ch, stdin);
if (ch == 'q' || ch == 'Q') {
quitFlag = true;
break;
}
}
#endif
// Checks for input every roughly 60 frames
std::this_thread::sleep_for(std::chrono::milliseconds(16));
}
}
int main()
{
std::cout << "Hello, World!" << '\n';
std::thread inputThread(checkForQuit);
const bool isWine = HarmonyLinkLib::get_is_wine();
const char* test = isWine ? "is" : "isn't";
const HarmonyLinkLib::FOSVerInfo* os_info = HarmonyLinkLib::get_os_version();
const HarmonyLinkLib::FDevice* device_info = HarmonyLinkLib::get_device_info();
const HarmonyLinkLib::FCPUInfo* cpu_info = HarmonyLinkLib::get_cpu_info();
// This loop is to test how stable & expensive these functions are
while (!quitFlag)
{
// Clear the screen
clearScreen();
std::wcout << "This program " << test << " running under wine.\n";
if (cpu_info)
{
cpu_info->print();
}
if (os_info)
{
os_info->print();
}
if (device_info)
{
wprintf(L"Is SteamDeck: %s\n", device_info->device == HarmonyLinkLib::EDevice::STEAM_DECK ? L"true" : L"false");
}
// we can't do this before the loop because we need updated values
if (const HarmonyLinkLib::FBattery* battery = HarmonyLinkLib::get_battery_status())
{
battery->to_string();
battery->free();
}
const bool is_docked = HarmonyLinkLib::get_is_docked();
const char* dock_check_string = is_docked ? "is" : "isn't";
wprintf(L"Device %hs docked\n", dock_check_string);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
if (inputThread.joinable())
{
inputThread.join();
}
if (os_info)
{
os_info->free();
}
if (device_info)
{
device_info->free();
}
if (cpu_info)
{
cpu_info->free();
}
return 0;
}