mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-10-29 16:26:39 +01:00
Gamepad: support JSON config
This commit is contained in:
@@ -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