mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-09-13 02:14:37 +02:00
Refactor os detection code
This commit is contained in:
+5
-3
@@ -154,7 +154,6 @@ set(LIBFASTFETCH_SRC
|
||||
src/logo/image/image.c
|
||||
src/logo/image/im7.c
|
||||
src/logo/image/im6.c
|
||||
src/detection/os.c
|
||||
src/detection/qt.c
|
||||
src/detection/gtk.c
|
||||
src/detection/terminalShell.c
|
||||
@@ -164,6 +163,7 @@ set(LIBFASTFETCH_SRC
|
||||
src/detection/temps.c
|
||||
src/detection/title.c
|
||||
src/detection/host/host.c
|
||||
src/detection/os/os.c
|
||||
src/detection/displayserver/displayServer.c
|
||||
src/detection/displayserver/wayland.c
|
||||
src/detection/displayserver/xcb.c
|
||||
@@ -215,11 +215,13 @@ if(APPLE)
|
||||
)
|
||||
elseif(ANDROID)
|
||||
list(APPEND LIBFASTFETCH_SRC
|
||||
"src/detection/host/host_android.c"
|
||||
src/detection/host/host_android.c
|
||||
src/detection/os/os_android.c
|
||||
)
|
||||
elseif(UNIX)
|
||||
list(APPEND LIBFASTFETCH_SRC
|
||||
"src/detection/host/host_linux.c"
|
||||
src/detection/host/host_linux.c
|
||||
src/detection/os/os_linux.c
|
||||
)
|
||||
else()
|
||||
message(FATAL_ERROR "Unsupported platform")
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#include "host.h"
|
||||
|
||||
#include "common/settings.h"
|
||||
#include <ctype.h>
|
||||
|
||||
void ffDetectHostImpl(FFHostResult* host)
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
#include "os.h"
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
void ffDetectOSImpl(FFOSResult* os, const FFinstance* instance);
|
||||
|
||||
const FFOSResult* ffDetectOS(const FFinstance* instance)
|
||||
{
|
||||
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
static FFOSResult result;
|
||||
static bool init = false;
|
||||
pthread_mutex_lock(&mutex);
|
||||
if(init)
|
||||
{
|
||||
pthread_mutex_unlock(&mutex);
|
||||
return &result;
|
||||
}
|
||||
init = true;
|
||||
|
||||
ffDetectOSImpl(&result, instance);
|
||||
|
||||
pthread_mutex_unlock(&mutex);
|
||||
return &result;
|
||||
}
|
||||
@@ -7,7 +7,6 @@
|
||||
|
||||
typedef struct FFOSResult
|
||||
{
|
||||
FFstrbuf systemName;
|
||||
FFstrbuf name;
|
||||
FFstrbuf prettyName;
|
||||
FFstrbuf id;
|
||||
@@ -18,6 +17,7 @@ typedef struct FFOSResult
|
||||
FFstrbuf versionID;
|
||||
FFstrbuf codename;
|
||||
FFstrbuf buildID;
|
||||
FFstrbuf systemName;
|
||||
FFstrbuf architecture;
|
||||
} FFOSResult;
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
#include "os.h"
|
||||
#include "common/settings.h"
|
||||
|
||||
void ffDetectOSImpl(FFOSResult* os, const FFinstance* instance)
|
||||
{
|
||||
ffStrbufInit(&os->name);
|
||||
ffStrbufSetS(&os->name, "Android");
|
||||
|
||||
ffStbrufInit(&os->prettyName);
|
||||
ffStrbufSetS(&os->prettyName, "Android");
|
||||
|
||||
ffStrbufInit(&os->id);
|
||||
ffStrbufSetS(&os->id, "android");
|
||||
|
||||
ffStrbufInit(&os->version);
|
||||
ffSettingsGetAndroidProperty("ro.build.version.release", &os->version);
|
||||
|
||||
ffStrbufInit(&os->versionID);
|
||||
ffSettingsGetAndroidProperty("ro.build.version.release", &os->versionID);
|
||||
|
||||
ffStrbufInit(&os->codename);
|
||||
ffSettingsGetAndroidProperty("ro.build.version.codename", &os->codename);
|
||||
|
||||
ffStrbufInit(&os->buildID);
|
||||
ffSettingsGetAndroidProperty("ro.build.id", &os->buildID);
|
||||
|
||||
ffStrbufInitA(&os->idLike, 0);
|
||||
ffStrbufInitA(&os->variant, 0);
|
||||
ffStrbufInitA(&os->variantID, 0);
|
||||
}
|
||||
@@ -1,21 +1,20 @@
|
||||
#include "fastfetch.h"
|
||||
#include "detection/os.h"
|
||||
#include "os.h"
|
||||
#include "common/properties.h"
|
||||
#include "common/parsing.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#if __ANDROID__
|
||||
#include "common/settings.h"
|
||||
#else
|
||||
static inline bool allRelevantValuesSet(const FFOSResult* result)
|
||||
{
|
||||
return result->id.length > 0
|
||||
&& result->name.length > 0
|
||||
&& result->prettyName.length > 0
|
||||
;
|
||||
}
|
||||
|
||||
static bool parseFile(const char* fileName, FFOSResult* result)
|
||||
{
|
||||
if(result->id.length > 0 && result->name.length > 0 && result->prettyName.length > 0)
|
||||
return true;
|
||||
|
||||
return ffParsePropFileValues(fileName, 13, (FFpropquery[]) {
|
||||
{"NAME =", &result->name},
|
||||
{"DISTRIB_DESCRIPTION =", &result->prettyName},
|
||||
@@ -94,73 +93,59 @@ static void getUbuntuFlavour(FFOSResult* result)
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
const FFOSResult* ffDetectOS(const FFinstance* instance)
|
||||
static void detectOS(FFOSResult* os, const FFinstance* instance)
|
||||
{
|
||||
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
static FFOSResult result;
|
||||
static bool init = false;
|
||||
pthread_mutex_lock(&mutex);
|
||||
if(init)
|
||||
{
|
||||
pthread_mutex_unlock(&mutex);
|
||||
return &result;
|
||||
}
|
||||
init = true;
|
||||
|
||||
ffStrbufInit(&result.systemName);
|
||||
ffStrbufInit(&result.name);
|
||||
ffStrbufInit(&result.prettyName);
|
||||
ffStrbufInit(&result.id);
|
||||
ffStrbufInit(&result.idLike);
|
||||
ffStrbufInit(&result.variant);
|
||||
ffStrbufInit(&result.variantID);
|
||||
ffStrbufInit(&result.version);
|
||||
ffStrbufInit(&result.versionID);
|
||||
ffStrbufInit(&result.codename);
|
||||
ffStrbufInit(&result.buildID);
|
||||
ffStrbufInit(&result.architecture);
|
||||
|
||||
ffStrbufSetS(&result.systemName, instance->state.utsname.sysname);
|
||||
ffStrbufSetS(&result.architecture, instance->state.utsname.machine);
|
||||
|
||||
#if !__ANDROID__
|
||||
if(instance->config.osFile.length > 0)
|
||||
{
|
||||
parseFile(instance->config.osFile.chars, &result);
|
||||
parseFile(instance->config.osFile.chars, os);
|
||||
return;
|
||||
}
|
||||
else if(instance->config.escapeBedrock && parseFile(FASTFETCH_TARGET_DIR_ROOT"/bedrock/etc/bedrock-release", &result))
|
||||
|
||||
if(instance->config.escapeBedrock && parseFile(FASTFETCH_TARGET_DIR_ROOT"/bedrock/etc/bedrock-release", os))
|
||||
{
|
||||
if(result.id.length == 0)
|
||||
ffStrbufAppendS(&result.id, "bedrock");
|
||||
if(os->id.length == 0)
|
||||
ffStrbufAppendS(&os->id, "bedrock");
|
||||
|
||||
if(result.name.length == 0)
|
||||
ffStrbufAppendS(&result.name, "Bedrock");
|
||||
if(os->name.length == 0)
|
||||
ffStrbufAppendS(&os->name, "Bedrock");
|
||||
|
||||
if(result.prettyName.length == 0)
|
||||
ffStrbufAppendS(&result.prettyName, "Bedrock Linux");
|
||||
}
|
||||
else
|
||||
{
|
||||
parseFile(FASTFETCH_TARGET_DIR_ROOT"/etc/os-release", &result);
|
||||
parseFile(FASTFETCH_TARGET_DIR_USR"/lib/os-release", &result);
|
||||
parseFile(FASTFETCH_TARGET_DIR_ROOT"/etc/lsb-release", &result);
|
||||
if(os->prettyName.length == 0)
|
||||
ffStrbufAppendS(&os->prettyName, "Bedrock Linux");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if(ffStrbufIgnCaseCompS(&result.id, "ubuntu") == 0)
|
||||
getUbuntuFlavour(&result);
|
||||
parseFile(FASTFETCH_TARGET_DIR_ROOT"/etc/os-release", os);
|
||||
if(allRelevantValuesSet(os))
|
||||
return;
|
||||
|
||||
#else
|
||||
ffStrbufSetS(&result.name, "Android");
|
||||
ffStrbufSetS(&result.id, "android");
|
||||
ffSettingsGetAndroidProperty("ro.build.version.release", &result.versionID);
|
||||
ffSettingsGetAndroidProperty("ro.build.version.release", &result.version);
|
||||
ffSettingsGetAndroidProperty("ro.build.version.codename", &result.codename);
|
||||
ffSettingsGetAndroidProperty("ro.build.id", &result.buildID);
|
||||
#endif
|
||||
parseFile(FASTFETCH_TARGET_DIR_USR"/lib/os-release", os);
|
||||
if(allRelevantValuesSet(os))
|
||||
return;
|
||||
|
||||
pthread_mutex_unlock(&mutex);
|
||||
|
||||
return &result;
|
||||
parseFile(FASTFETCH_TARGET_DIR_ROOT"/etc/lsb-release", os);
|
||||
}
|
||||
|
||||
void ffDetectOSImpl(FFOSResult* os, const FFinstance* instance)
|
||||
{
|
||||
ffStrbufInit(&os->systemName);
|
||||
ffStrbufInit(&os->name);
|
||||
ffStrbufInit(&os->prettyName);
|
||||
ffStrbufInit(&os->id);
|
||||
ffStrbufInit(&os->idLike);
|
||||
ffStrbufInit(&os->variant);
|
||||
ffStrbufInit(&os->variantID);
|
||||
ffStrbufInit(&os->version);
|
||||
ffStrbufInit(&os->versionID);
|
||||
ffStrbufInit(&os->codename);
|
||||
ffStrbufInit(&os->buildID);
|
||||
ffStrbufInit(&os->architecture);
|
||||
|
||||
ffStrbufSetS(&os->systemName, instance->state.utsname.sysname);
|
||||
ffStrbufSetS(&os->architecture, instance->state.utsname.machine);
|
||||
|
||||
detectOS(os, instance);
|
||||
|
||||
if(ffStrbufIgnCaseCompS(&os->id, "ubuntu") == 0)
|
||||
getUbuntuFlavour(os);
|
||||
}
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
#include "logo.h"
|
||||
#include "common/io.h"
|
||||
#include "common/printing.h"
|
||||
#include "detection/os.h"
|
||||
#include "detection/os/os.h"
|
||||
#include "detection/terminalshell.h"
|
||||
|
||||
#include <ctype.h>
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
#include "fastfetch.h"
|
||||
#include "common/printing.h"
|
||||
#include "detection/os.h"
|
||||
#include "detection/os/os.h"
|
||||
|
||||
#include <ctype.h>
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#include "common/printing.h"
|
||||
#include "common/settings.h"
|
||||
#include "common/processing.h"
|
||||
#include "detection/os.h"
|
||||
#include "detection/os/os.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
Reference in New Issue
Block a user