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

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

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" {
#endif
HARMONYLINK_API bool HL_Init(void);
bool Detect_Wine(void);
#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
}