Working wine & Linux detection

This commit is contained in:
Jordon Brooks 2024-07-14 16:42:34 +01:00
parent c68c039c70
commit 6669a6a102
Signed by: jordon
GPG key ID: DBD9758CD53E786A
8 changed files with 246 additions and 40 deletions

View file

@ -13,7 +13,7 @@
// limitations under the License. // limitations under the License.
#include <wchar.h> #include <wchar.h>
#include "HarmonyLinkLib.h" #include "LibHarmonyLink.h"
int main(void) int main(void)
{ {
@ -27,5 +27,11 @@ int main(void)
wprintf(L"Successfully Initialised HarmonyLink!\n"); 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; return 0;
} }

View file

@ -17,7 +17,7 @@
#include <thread> #include <thread>
#include <atomic> #include <atomic>
#include "HarmonyLinkLib.h" #include "LibHarmonyLink.h"
// Include necessary headers for platform-specific functionality // Include necessary headers for platform-specific functionality
#ifdef BUILD_WINDOWS #ifdef BUILD_WINDOWS
@ -32,6 +32,8 @@
std::atomic<bool> quitFlag(false); std::atomic<bool> quitFlag(false);
std::atomic<bool> HasError(false);
// Function to clear the screen cross-platform // Function to clear the screen cross-platform
void clearScreen() { void clearScreen() {
#ifdef BUILD_WINDOWS #ifdef BUILD_WINDOWS
@ -44,7 +46,7 @@ void clearScreen() {
// Function to check if 'q' or 'Q' is pressed in Windows // Function to check if 'q' or 'Q' is pressed in Windows
void checkForQuit() { void checkForQuit() {
while (!quitFlag) { while (!quitFlag || !HasError) {
#ifdef BUILD_WINDOWS #ifdef BUILD_WINDOWS
if (_kbhit()) { if (_kbhit()) {
const char c = static_cast<char>(_getch()); const char c = static_cast<char>(_getch());
@ -87,19 +89,25 @@ int main()
{ {
std::cout << "Hello, World!" << '\n'; std::cout << "Hello, World!" << '\n';
std::thread inputThread(checkForQuit);
if (!LibHarmonyLink::HL_Init()) 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* isLinux = LibHarmonyLink::HL_Is_Linux() ? "is" : "isn't";
//const char* test = isWine ? "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(); //const HarmonyLinkLib::FOSVerInfo* os_info = HarmonyLinkLib::get_os_version();
@ -107,14 +115,13 @@ int main()
//const HarmonyLinkLib::FCPUInfo* cpu_info = HarmonyLinkLib::get_cpu_info(); //const HarmonyLinkLib::FCPUInfo* cpu_info = HarmonyLinkLib::get_cpu_info();
HasError = true;
// This loop is to test how stable & expensive these functions are // This loop is to test how stable & expensive these functions are
while (!quitFlag) while (!quitFlag || !HasError)
{ {
// Clear the screen // Clear the screen
clearScreen(); clearScreen();
//std::wcout << "This program " << test << " running under wine.\n";
//if (cpu_info) //if (cpu_info)
//{ //{
// cpu_info->print(); // cpu_info->print();

View file

@ -52,9 +52,10 @@ 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/HarmonyLinkLib.c src/LibHarmonyLink.c
src/Version.c src/Version.c
src/Utilities.c src/Utilities.c
src/Wine.c
src/Structs/Battery.c src/Structs/Battery.c
src/Structs/CPUInfo.c src/Structs/CPUInfo.c
@ -66,9 +67,10 @@ set(COMMON_SOURCES
# Explicitly list include files # Explicitly list include files
set(COMMON_INCLUDES set(COMMON_INCLUDES
include/Core.h include/Core.h
include/HarmonyLinkLib.h include/LibHarmonyLink.h
include/Version.h include/Version.h
include/Utilities.h include/Utilities.h
include/Wine.h
include/Enums/Device.h include/Enums/Device.h
include/Enums/Platform.h include/Enums/Platform.h

View file

@ -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 <stdbool.h>
#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

View file

@ -22,7 +22,7 @@ namespace LibHarmonyLink {
extern "C" { extern "C" {
#endif #endif
HARMONYLINK_API bool HL_Init(void); bool Detect_Wine(void);
#ifdef __cplusplus #ifdef __cplusplus
} }

View file

@ -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 <stdbool.h>
#include "HarmonyLinkLib.h"
#include "Version.h"
bool HL_Init(void)
{
HL_version_print();
return 1;
}

View file

@ -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 <stdbool.h>
#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();
}

41
LibHarmonyLink/src/Wine.c Normal file
View file

@ -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 <stdio.h>
#include <stdbool.h>
#ifdef BUILD_WINDOWS
#include <windows.h>
#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
}