diff --git a/HarmonyLinkTest/src/main.c b/HarmonyLinkTest/src/main.c index 4752dd4..8481c73 100644 --- a/HarmonyLinkTest/src/main.c +++ b/HarmonyLinkTest/src/main.c @@ -13,7 +13,7 @@ // limitations under the License. #include -#include "HarmonyLinkLib.h" +#include "LibHarmonyLink.h" int main(void) { @@ -26,6 +26,12 @@ int main(void) } wprintf(L"Successfully Initialised HarmonyLink!\n"); + + const wchar_t* IsLinux = HL_Is_Linux() ? L"True" : L"False"; + wprintf(L"Is Linux: %ls\n", IsLinux); + + const wchar_t* IsWine = HL_Is_Wine() ? L"True" : L"False"; + wprintf(L"Is Wine: %ls\n", IsWine); return 0; } diff --git a/HarmonyLinkTest_CPP/src/main.cpp b/HarmonyLinkTest_CPP/src/main.cpp index 2128219..7680ffd 100644 --- a/HarmonyLinkTest_CPP/src/main.cpp +++ b/HarmonyLinkTest_CPP/src/main.cpp @@ -17,7 +17,7 @@ #include #include -#include "HarmonyLinkLib.h" +#include "LibHarmonyLink.h" // Include necessary headers for platform-specific functionality #ifdef BUILD_WINDOWS @@ -32,6 +32,8 @@ std::atomic quitFlag(false); +std::atomic HasError(false); + // Function to clear the screen cross-platform void clearScreen() { #ifdef BUILD_WINDOWS @@ -44,7 +46,7 @@ void clearScreen() { // Function to check if 'q' or 'Q' is pressed in Windows void checkForQuit() { - while (!quitFlag) { + while (!quitFlag || !HasError) { #ifdef BUILD_WINDOWS if (_kbhit()) { const char c = static_cast(_getch()); @@ -87,19 +89,25 @@ int main() { std::cout << "Hello, World!" << '\n'; - std::thread inputThread(checkForQuit); - if (!LibHarmonyLink::HL_Init()) { - printf("Failed to Initialize HarmonyLink!"); + printf("Failed to Initialize HarmonyLink!\n"); + + HasError = true; + return 1; } - printf("HarmonyLink Initialized!"); + printf("HarmonyLink Initialized!\n"); - std::this_thread::sleep_for(std::chrono::milliseconds(5000)); + std::thread inputThread(checkForQuit); - //const bool isWine = LibHarmonyLink::get_is_wine(); - //const char* test = isWine ? "is" : "isn't"; + const char* isLinux = LibHarmonyLink::HL_Is_Linux() ? "is" : "isn't"; + printf("This program %s running under Linux.\n", isLinux); + + const bool isWine = LibHarmonyLink::HL_Is_Wine(); + const char* test = isWine ? "is" : "isn't"; + + printf("This program %s running under wine.\n", test); //const HarmonyLinkLib::FOSVerInfo* os_info = HarmonyLinkLib::get_os_version(); @@ -107,14 +115,13 @@ int main() //const HarmonyLinkLib::FCPUInfo* cpu_info = HarmonyLinkLib::get_cpu_info(); + HasError = true; // This loop is to test how stable & expensive these functions are - while (!quitFlag) + while (!quitFlag || !HasError) { // Clear the screen clearScreen(); - //std::wcout << "This program " << test << " running under wine.\n"; - //if (cpu_info) //{ // cpu_info->print(); diff --git a/LibHarmonyLink/CMakeLists.txt b/LibHarmonyLink/CMakeLists.txt index 217c38e..fd1c097 100644 --- a/LibHarmonyLink/CMakeLists.txt +++ b/LibHarmonyLink/CMakeLists.txt @@ -52,9 +52,10 @@ configure_file(${CMAKE_CURRENT_SOURCE_DIR}/Resources/Version.rc.in ${CMAKE_CURRE # Explicitly list source files set(COMMON_SOURCES - src/HarmonyLinkLib.c + src/LibHarmonyLink.c src/Version.c src/Utilities.c + src/Wine.c src/Structs/Battery.c src/Structs/CPUInfo.c @@ -66,9 +67,10 @@ set(COMMON_SOURCES # Explicitly list include files set(COMMON_INCLUDES include/Core.h - include/HarmonyLinkLib.h + include/LibHarmonyLink.h include/Version.h include/Utilities.h + include/Wine.h include/Enums/Device.h include/Enums/Platform.h diff --git a/LibHarmonyLink/include/LibHarmonyLink.h b/LibHarmonyLink/include/LibHarmonyLink.h new file mode 100644 index 0000000..9a7d790 --- /dev/null +++ b/LibHarmonyLink/include/LibHarmonyLink.h @@ -0,0 +1,90 @@ +// 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 +#include "Core.h" + +#ifdef __cplusplus +namespace LibHarmonyLink { +extern "C" { +#endif + +/** + * @brief Initializes the HarmonyLink library. + * + * This function initializes the HarmonyLink library by printing the version, + * detecting if the application is running under Wine or on Linux. + * + * @return Returns true on successful initialization. + */ +HARMONYLINK_API bool HL_Init(void); +/** + * @brief Checks if the HarmonyLink library is running under Wine. + * + * This function returns the result of Get_IsWine. + * + * @return Returns true if the HarmonyLink library is running under Wine, false otherwise. + */ +HARMONYLINK_API bool HL_Is_Wine(void); + +/** + * @brief Checks if the HarmonyLink library is running on Linux. + * + * This function returns the result of Get_IsLinux. + * + * @return Returns true if the HarmonyLink library is running on Linux, false otherwise. + */ +HARMONYLINK_API bool HL_Is_Linux(void); + +/** + * @brief Handles the detection of Linux. + * + * This function detects if the application is running on Linux and caches the result. + * If the application is not running on Linux, it checks if it is running under Wine. + */ +void Handle_Detect_Linux(void); + +/** + * @brief Handles the detection of Wine. + * + * This function detects if the application is running under Wine and caches the result. + */ +void Handle_Detect_Wine(void); + +/** + * @brief Checks if the application is running under Wine. + * + * This function returns the cached result of the Wine detection. If the result + * is not cached, it will call Handle_Detect_Wine to detect and cache the result. + * + * @return Returns true if the application is running under Wine, false otherwise. + */ +bool Get_IsWine(void); + +/** + * @brief Checks if the application is running on Linux. + * + * This function returns the cached result of the Linux detection. If the result + * is not cached, it will call Handle_Detect_Linux to detect and cache the result. + * + * @return Returns true if the application is running on Linux, false otherwise. + */ +bool Get_IsLinux(void); + +#ifdef __cplusplus +} +} +#endif diff --git a/LibHarmonyLink/include/HarmonyLinkLib.h b/LibHarmonyLink/include/Wine.h similarity index 95% rename from LibHarmonyLink/include/HarmonyLinkLib.h rename to LibHarmonyLink/include/Wine.h index a62e00c..9614ee4 100644 --- a/LibHarmonyLink/include/HarmonyLinkLib.h +++ b/LibHarmonyLink/include/Wine.h @@ -22,7 +22,7 @@ namespace LibHarmonyLink { extern "C" { #endif -HARMONYLINK_API bool HL_Init(void); +bool Detect_Wine(void); #ifdef __cplusplus } diff --git a/LibHarmonyLink/src/HarmonyLinkLib.c b/LibHarmonyLink/src/HarmonyLinkLib.c deleted file mode 100644 index ae7ae47..0000000 --- a/LibHarmonyLink/src/HarmonyLinkLib.c +++ /dev/null @@ -1,24 +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 -#include "HarmonyLinkLib.h" - -#include "Version.h" - -bool HL_Init(void) -{ - HL_version_print(); - return 1; -} diff --git a/LibHarmonyLink/src/LibHarmonyLink.c b/LibHarmonyLink/src/LibHarmonyLink.c new file mode 100644 index 0000000..18bf5ec --- /dev/null +++ b/LibHarmonyLink/src/LibHarmonyLink.c @@ -0,0 +1,84 @@ +// 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 +#include "LibHarmonyLink.h" + +#include "Version.h" +#include "Wine.h" + +static bool bIsWine = false; +static bool bCachedIsWine = false; + +static bool bIsLinux = false; +static bool bCachedIsLinux = false; + +bool HL_Init(void) +{ + HL_version_print(); + Handle_Detect_Wine(); + Handle_Detect_Linux(); + return 1; +} + +void Handle_Detect_Wine(void) +{ +#ifdef BUILD_WINDOWS + bIsWine = Detect_Wine(); +#else + bIsWine = false; +#endif + +bCachedIsWine = true; +} + +bool Get_IsWine(void) +{ + if (!bCachedIsWine) + { + Handle_Detect_Wine(); + } + + return bIsWine; +} + +bool Get_IsLinux(void) +{ + if (!bCachedIsLinux) + { + Handle_Detect_Linux(); + } + + return bIsLinux; +} + +void Handle_Detect_Linux(void) +{ +#ifdef BUILD_LINUX + bIsLinux = true; +#else + bIsLinux = Get_IsWine(); +#endif + +bCachedIsLinux = true; +} + +bool HL_Is_Wine(void) +{ + return Get_IsWine(); +} +bool HL_Is_Linux(void) +{ + return Get_IsLinux(); +} diff --git a/LibHarmonyLink/src/Wine.c b/LibHarmonyLink/src/Wine.c new file mode 100644 index 0000000..f2e3b1c --- /dev/null +++ b/LibHarmonyLink/src/Wine.c @@ -0,0 +1,41 @@ +// 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 "Wine.h" + +#include +#include +#ifdef BUILD_WINDOWS +#include +#endif + +bool Detect_Wine(void) +{ + bool isWine = false; + +#ifdef BUILD_WINDOWS + printf("Detecting wine...\n"); + bool HasFound = GetProcAddress(GetModuleHandleA("ntdll.dll"), "wine_get_version") != NULL; + + if (!HasFound) + HasFound = GetProcAddress(GetModuleHandleA("ntdll.dll"), "proton_get_version") != NULL; + + printf("wine %s found\n", HasFound ? "has been" : "not"); + + isWine = HasFound; // Cache the result +#else + isWine = false; // In non-Windows builds, always set isWine to false +#endif + return isWine; // Return the cached result +} \ No newline at end of file