mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-09-13 02:42:09 +02:00
Gamepad: support JSON config
This commit is contained in:
+1
-1
@@ -282,7 +282,7 @@ set(LIBFASTFETCH_SRC
|
||||
src/modules/gpu/gpu.c
|
||||
src/modules/host/host.c
|
||||
src/modules/icons.c
|
||||
src/modules/gamepad.c
|
||||
src/modules/gamepad/gamepad.c
|
||||
src/modules/kernel/kernel.c
|
||||
src/modules/locale.c
|
||||
src/modules/localip.c
|
||||
|
||||
+2
-2
@@ -105,7 +105,7 @@ static void defaultConfig(FFinstance* instance)
|
||||
ffInitBluetoothOptions(&instance->config.bluetooth);
|
||||
initModuleArg(&instance->config.sound);
|
||||
ffInitSeparatorOptions(&instance->config.separator);
|
||||
initModuleArg(&instance->config.gamepad);
|
||||
ffInitGamepadOptions(&instance->config.gamepad);
|
||||
|
||||
ffStrbufInitA(&instance->config.libPCI, 0);
|
||||
ffStrbufInitA(&instance->config.libVulkan, 0);
|
||||
@@ -332,7 +332,7 @@ static void destroyConfig(FFinstance* instance)
|
||||
ffDestroyBluetoothOptions(&instance->config.bluetooth);
|
||||
ffDestroySeparatorOptions(&instance->config.separator);
|
||||
destroyModuleArg(&instance->config.sound);
|
||||
destroyModuleArg(&instance->config.gamepad);
|
||||
ffDestroyGamepadOptions(&instance->config.gamepad);
|
||||
|
||||
ffStrbufDestroy(&instance->config.libPCI);
|
||||
ffStrbufDestroy(&instance->config.libVulkan);
|
||||
|
||||
+3
-3
@@ -1024,7 +1024,7 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
|
||||
else if(ffParseBluetoothCommandOptions(&instance->config.bluetooth, key, value)) {}
|
||||
else if(ffParseSeparatorCommandOptions(&instance->config.separator, key, value)) {}
|
||||
else if(optionParseModuleArgs(key, value, "sound", &instance->config.sound)) {}
|
||||
else if(optionParseModuleArgs(key, value, "gamepad", &instance->config.gamepad)) {}
|
||||
else if(ffParseGamepadCommandOptions(&instance->config.gamepad, key, value)) {}
|
||||
|
||||
///////////////////
|
||||
//Library options//
|
||||
@@ -1290,8 +1290,8 @@ static void parseStructureCommand(FFinstance* instance, const char* line)
|
||||
ffPrintBluetooth(instance, &instance->config.bluetooth);
|
||||
else if(strcasecmp(line, "sound") == 0)
|
||||
ffPrintSound(instance);
|
||||
else if(strcasecmp(line, "gamepad") == 0)
|
||||
ffPrintGamepad(instance);
|
||||
else if(strcasecmp(line, FF_GAMEPAD_MODULE_NAME) == 0)
|
||||
ffPrintGamepad(instance, &instance->config.gamepad);
|
||||
else if(strcasecmp(line, FF_JSONCONFIG_MODULE_NAME) == 0)
|
||||
ffPrintJsonConfig(instance);
|
||||
else
|
||||
|
||||
+1
-2
@@ -129,7 +129,7 @@ typedef struct FFconfig
|
||||
FFBluetoothOptions bluetooth;
|
||||
FFSeparatorOptions separator;
|
||||
FFModuleArgs sound;
|
||||
FFModuleArgs gamepad;
|
||||
FFGamepadOptions gamepad;
|
||||
|
||||
FFstrbuf libPCI;
|
||||
FFstrbuf libVulkan;
|
||||
@@ -262,6 +262,5 @@ void ffPrintOpenGL(FFinstance* instance);
|
||||
void ffPrintOpenCL(FFinstance* instance);
|
||||
void ffPrintUsers(FFinstance* instance);
|
||||
void ffPrintSound(FFinstance* instance);
|
||||
void ffPrintGamepad(FFinstance* instance);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,48 +0,0 @@
|
||||
#include "common/printing.h"
|
||||
#include "detection/gamepad/gamepad.h"
|
||||
|
||||
#define FF_GAMEPAD_MODULE_NAME "Gamepad"
|
||||
#define FF_GAMEPAD_NUM_FORMAT_ARGS 2
|
||||
|
||||
static void printDevice(FFinstance* instance, const FFGamepadDevice* device, uint8_t index)
|
||||
{
|
||||
if(instance->config.gamepad.outputFormat.length == 0)
|
||||
{
|
||||
ffPrintLogoAndKey(instance, FF_GAMEPAD_MODULE_NAME, index, &instance->config.gamepad.key);
|
||||
ffStrbufPutTo(&device->name, stdout);
|
||||
}
|
||||
else
|
||||
{
|
||||
ffPrintFormat(instance, FF_GAMEPAD_MODULE_NAME, index, &instance->config.gamepad, FF_GAMEPAD_NUM_FORMAT_ARGS, (FFformatarg[]) {
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &device->name},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &device->identifier},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void ffPrintGamepad(FFinstance* instance)
|
||||
{
|
||||
FF_LIST_AUTO_DESTROY result;
|
||||
ffListInit(&result, sizeof(FFGamepadDevice));
|
||||
const char* error = ffDetectGamepad(instance, &result);
|
||||
|
||||
if(error)
|
||||
{
|
||||
ffPrintError(instance, FF_GAMEPAD_MODULE_NAME, 0, &instance->config.gamepad, "%s", error);
|
||||
return;
|
||||
}
|
||||
|
||||
if(!result.length)
|
||||
{
|
||||
ffPrintError(instance, FF_GAMEPAD_MODULE_NAME, 0, &instance->config.gamepad, "No devices detected");
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t index = 0;
|
||||
FF_LIST_FOR_EACH(FFGamepadDevice, device, result)
|
||||
{
|
||||
printDevice(instance, device, result.length > 1 ? ++index : 0);
|
||||
ffStrbufDestroy(&device->identifier);
|
||||
ffStrbufDestroy(&device->name);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
#include "common/printing.h"
|
||||
#include "detection/gamepad/gamepad.h"
|
||||
#include "modules/gamepad/gamepad.h"
|
||||
|
||||
#define FF_GAMEPAD_NUM_FORMAT_ARGS 2
|
||||
|
||||
static void printDevice(FFinstance* instance, FFGamepadOptions* options, const FFGamepadDevice* device, uint8_t index)
|
||||
{
|
||||
if(options->moduleArgs.outputFormat.length == 0)
|
||||
{
|
||||
ffPrintLogoAndKey(instance, FF_GAMEPAD_MODULE_NAME, index, &options->moduleArgs.key);
|
||||
ffStrbufPutTo(&device->name, stdout);
|
||||
}
|
||||
else
|
||||
{
|
||||
ffPrintFormat(instance, FF_GAMEPAD_MODULE_NAME, index, &options->moduleArgs, FF_GAMEPAD_NUM_FORMAT_ARGS, (FFformatarg[]) {
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &device->name},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &device->identifier},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void ffPrintGamepad(FFinstance* instance, FFGamepadOptions* options)
|
||||
{
|
||||
FF_LIST_AUTO_DESTROY result;
|
||||
ffListInit(&result, sizeof(FFGamepadDevice));
|
||||
const char* error = ffDetectGamepad(instance, &result);
|
||||
|
||||
if(error)
|
||||
{
|
||||
ffPrintError(instance, FF_GAMEPAD_MODULE_NAME, 0, &options->moduleArgs, "%s", error);
|
||||
return;
|
||||
}
|
||||
|
||||
if(!result.length)
|
||||
{
|
||||
ffPrintError(instance, FF_GAMEPAD_MODULE_NAME, 0, &options->moduleArgs, "No devices detected");
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t index = 0;
|
||||
FF_LIST_FOR_EACH(FFGamepadDevice, device, result)
|
||||
{
|
||||
printDevice(instance, options, device, result.length > 1 ? ++index : 0);
|
||||
ffStrbufDestroy(&device->identifier);
|
||||
ffStrbufDestroy(&device->name);
|
||||
}
|
||||
}
|
||||
|
||||
void ffInitGamepadOptions(FFGamepadOptions* options)
|
||||
{
|
||||
options->moduleName = FF_GAMEPAD_MODULE_NAME;
|
||||
ffOptionInitModuleArg(&options->moduleArgs);
|
||||
}
|
||||
|
||||
bool ffParseGamepadCommandOptions(FFGamepadOptions* options, const char* key, const char* value)
|
||||
{
|
||||
const char* subKey = ffOptionTestPrefix(key, FF_GAMEPAD_MODULE_NAME);
|
||||
if (!subKey) return false;
|
||||
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void ffDestroyGamepadOptions(FFGamepadOptions* options)
|
||||
{
|
||||
ffOptionDestroyModuleArg(&options->moduleArgs);
|
||||
}
|
||||
|
||||
#ifdef FF_HAVE_JSONC
|
||||
void ffParseGamepadJsonObject(FFinstance* instance, json_object* module)
|
||||
{
|
||||
FFGamepadOptions __attribute__((__cleanup__(ffDestroyGamepadOptions))) options;
|
||||
ffInitGamepadOptions(&options);
|
||||
|
||||
if (module)
|
||||
{
|
||||
json_object_object_foreach(module, key, val)
|
||||
{
|
||||
if (ffJsonConfigParseModuleArgs(key, val, &options.moduleArgs))
|
||||
continue;
|
||||
|
||||
ffPrintError(instance, FF_GAMEPAD_MODULE_NAME, 0, &options.moduleArgs, "Unknown JSON key %s", key);
|
||||
}
|
||||
}
|
||||
|
||||
ffPrintGamepad(instance, &options);
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include "fastfetch.h"
|
||||
|
||||
#define FF_GAMEPAD_MODULE_NAME "Gamepad"
|
||||
|
||||
void ffPrintGamepad(FFinstance* instance, FFGamepadOptions* options);
|
||||
void ffInitGamepadOptions(FFGamepadOptions* options);
|
||||
bool ffParseGamepadCommandOptions(FFGamepadOptions* options, const char* key, const char* value);
|
||||
void ffDestroyGamepadOptions(FFGamepadOptions* options);
|
||||
|
||||
#ifdef FF_HAVE_JSONC
|
||||
#include "common/jsonconfig.h"
|
||||
void ffParseGamepadJsonObject(FFinstance* instance, json_object* module);
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
// This file will be included in "fastfetch.h", do NOT put unnecessary things here
|
||||
|
||||
#include "common/option.h"
|
||||
|
||||
typedef struct FFGamepadOptions
|
||||
{
|
||||
const char* moduleName;
|
||||
FFModuleArgs moduleArgs;
|
||||
} FFGamepadOptions;
|
||||
@@ -58,6 +58,7 @@ static bool parseModuleJsonObject(FFinstance* instance, const char* type, json_o
|
||||
|
||||
case 'G': {
|
||||
return
|
||||
tryModule(instance, type, module, FF_GAMEPAD_MODULE_NAME, ffParseGamepadJsonObject) ||
|
||||
tryModule(instance, type, module, FF_GPU_MODULE_NAME, ffParseGPUJsonObject) ||
|
||||
false;
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
#include "modules/datetime/datetime.h"
|
||||
#include "modules/disk/disk.h"
|
||||
#include "modules/display/display.h"
|
||||
#include "modules/gamepad/gamepad.h"
|
||||
#include "modules/gpu/gpu.h"
|
||||
#include "modules/host/host.h"
|
||||
#include "modules/kernel/kernel.h"
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
#include "modules/disk/option.h"
|
||||
#include "modules/display/option.h"
|
||||
#include "modules/host/option.h"
|
||||
#include "modules/gamepad/option.h"
|
||||
#include "modules/gpu/option.h"
|
||||
#include "modules/kernel/option.h"
|
||||
#include "modules/os/option.h"
|
||||
|
||||
Reference in New Issue
Block a user