mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-10-30 17:41:41 +01:00
Sound: support JSON config
This commit is contained in:
@@ -97,6 +97,7 @@ static bool parseModuleJsonObject(FFinstance* instance, const char* type, json_o
|
||||
case 'S': {
|
||||
return
|
||||
tryModule(instance, type, module, FF_SEPARATOR_MODULE_NAME, ffParseSeparatorJsonObject) ||
|
||||
tryModule(instance, type, module, FF_SOUND_MODULE_NAME, ffParseSoundJsonObject) ||
|
||||
false;
|
||||
}
|
||||
|
||||
|
||||
@@ -26,5 +26,6 @@
|
||||
#include "modules/locale/locale.h"
|
||||
#include "modules/localip/localip.h"
|
||||
#include "modules/separator/separator.h"
|
||||
#include "modules/sound/sound.h"
|
||||
#include "modules/title/title.h"
|
||||
#include "modules/jsonconfig/jsonconfig.h"
|
||||
|
||||
@@ -24,4 +24,5 @@
|
||||
#include "modules/localip/option.h"
|
||||
#include "modules/os/option.h"
|
||||
#include "modules/separator/option.h"
|
||||
#include "modules/sound/option.h"
|
||||
#include "modules/title/option.h"
|
||||
|
||||
@@ -1,85 +0,0 @@
|
||||
#include "common/printing.h"
|
||||
#include "detection/sound/sound.h"
|
||||
|
||||
#define FF_SOUND_MODULE_NAME "Sound"
|
||||
#define FF_SOUND_NUM_FORMAT_ARGS 4
|
||||
|
||||
static void printDevice(FFinstance* instance, const FFSoundDevice* device, uint8_t index)
|
||||
{
|
||||
if(instance->config.sound.outputFormat.length == 0)
|
||||
{
|
||||
ffPrintLogoAndKey(instance, FF_SOUND_MODULE_NAME, index, &instance->config.sound.key);
|
||||
ffStrbufWriteTo(&device->name, stdout);
|
||||
|
||||
if(device->volume != FF_SOUND_VOLUME_UNKNOWN)
|
||||
{
|
||||
if(device->volume > 0)
|
||||
printf(" (%d%%)", device->volume);
|
||||
else
|
||||
fputs(" (muted)", stdout);
|
||||
}
|
||||
|
||||
if(device->main && index > 0)
|
||||
fputs(" (*)", stdout);
|
||||
|
||||
putchar('\n');
|
||||
}
|
||||
else
|
||||
{
|
||||
ffPrintFormat(instance, FF_SOUND_MODULE_NAME, index, &instance->config.sound, FF_SOUND_NUM_FORMAT_ARGS, (FFformatarg[]) {
|
||||
{FF_FORMAT_ARG_TYPE_BOOL, &device->main},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &device->name},
|
||||
{FF_FORMAT_ARG_TYPE_UINT8, &device->volume},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &device->identifier}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
static void printSound(FFinstance* instance, FFlist* devices)
|
||||
{
|
||||
FF_LIST_AUTO_DESTROY filtered;
|
||||
ffListInit(&filtered, sizeof(FFSoundDevice*));
|
||||
|
||||
FF_LIST_FOR_EACH(FFSoundDevice, device, *devices)
|
||||
{
|
||||
switch (instance->config.soundType)
|
||||
{
|
||||
case FF_SOUND_TYPE_MAIN: if (!device->main) continue; break;
|
||||
case FF_SOUND_TYPE_ACTIVE: if (!device->active) continue; break;
|
||||
case FF_SOUND_TYPE_ALL: break;
|
||||
}
|
||||
|
||||
*(FFSoundDevice**)ffListAdd(&filtered) = device;
|
||||
}
|
||||
|
||||
if(filtered.length == 0)
|
||||
{
|
||||
ffPrintError(instance, FF_SOUND_MODULE_NAME, 0, &instance->config.sound, "No active sound devices found");
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t index = 1;
|
||||
FF_LIST_FOR_EACH(FFSoundDevice*, device, filtered)
|
||||
printDevice(instance, *device, filtered.length == 1 ? 0 : index++);
|
||||
}
|
||||
|
||||
void ffPrintSound(FFinstance* instance)
|
||||
{
|
||||
FF_LIST_AUTO_DESTROY result;
|
||||
ffListInit(&result, sizeof(FFSoundDevice));
|
||||
const char* error = ffDetectSound(instance, &result);
|
||||
|
||||
if(error)
|
||||
{
|
||||
ffPrintError(instance, FF_SOUND_MODULE_NAME, 0, &instance->config.sound, "%s", error);
|
||||
return;
|
||||
}
|
||||
|
||||
printSound(instance, &result);
|
||||
|
||||
FF_LIST_FOR_EACH(FFSoundDevice, device, result)
|
||||
{
|
||||
ffStrbufDestroy(&device->identifier);
|
||||
ffStrbufDestroy(&device->name);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
// This file will be included in "fastfetch.h", do NOT put unnecessary things here
|
||||
|
||||
#include "common/option.h"
|
||||
|
||||
typedef enum FFSoundType
|
||||
{
|
||||
FF_SOUND_TYPE_MAIN,
|
||||
FF_SOUND_TYPE_ACTIVE,
|
||||
FF_SOUND_TYPE_ALL,
|
||||
} FFSoundType;
|
||||
|
||||
typedef struct FFSoundOptions
|
||||
{
|
||||
const char* moduleName;
|
||||
FFModuleArgs moduleArgs;
|
||||
|
||||
FFSoundType soundType;
|
||||
} FFSoundOptions;
|
||||
@@ -0,0 +1,154 @@
|
||||
#include "common/printing.h"
|
||||
#include "detection/sound/sound.h"
|
||||
#include "modules/sound/sound.h"
|
||||
|
||||
#define FF_SOUND_NUM_FORMAT_ARGS 4
|
||||
|
||||
static void printDevice(FFinstance* instance, FFSoundOptions* options, const FFSoundDevice* device, uint8_t index)
|
||||
{
|
||||
if(options->moduleArgs.outputFormat.length == 0)
|
||||
{
|
||||
ffPrintLogoAndKey(instance, FF_SOUND_MODULE_NAME, index, &options->moduleArgs.key);
|
||||
ffStrbufWriteTo(&device->name, stdout);
|
||||
|
||||
if(device->volume != FF_SOUND_VOLUME_UNKNOWN)
|
||||
{
|
||||
if(device->volume > 0)
|
||||
printf(" (%d%%)", device->volume);
|
||||
else
|
||||
fputs(" (muted)", stdout);
|
||||
}
|
||||
|
||||
if(device->main && index > 0)
|
||||
fputs(" (*)", stdout);
|
||||
|
||||
putchar('\n');
|
||||
}
|
||||
else
|
||||
{
|
||||
ffPrintFormat(instance, FF_SOUND_MODULE_NAME, index, &options->moduleArgs, FF_SOUND_NUM_FORMAT_ARGS, (FFformatarg[]) {
|
||||
{FF_FORMAT_ARG_TYPE_BOOL, &device->main},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &device->name},
|
||||
{FF_FORMAT_ARG_TYPE_UINT8, &device->volume},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &device->identifier}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void ffPrintSound(FFinstance* instance, FFSoundOptions* options)
|
||||
{
|
||||
FF_LIST_AUTO_DESTROY result;
|
||||
ffListInit(&result, sizeof(FFSoundDevice));
|
||||
const char* error = ffDetectSound(instance, &result);
|
||||
|
||||
if(error)
|
||||
{
|
||||
ffPrintError(instance, FF_SOUND_MODULE_NAME, 0, &options->moduleArgs, "%s", error);
|
||||
return;
|
||||
}
|
||||
|
||||
FF_LIST_AUTO_DESTROY filtered;
|
||||
ffListInit(&filtered, sizeof(FFSoundDevice*));
|
||||
|
||||
FF_LIST_FOR_EACH(FFSoundDevice, device, result)
|
||||
{
|
||||
switch (options->soundType)
|
||||
{
|
||||
case FF_SOUND_TYPE_MAIN: if (!device->main) continue; break;
|
||||
case FF_SOUND_TYPE_ACTIVE: if (!device->active) continue; break;
|
||||
case FF_SOUND_TYPE_ALL: break;
|
||||
}
|
||||
|
||||
*(FFSoundDevice**)ffListAdd(&filtered) = device;
|
||||
}
|
||||
|
||||
if(filtered.length == 0)
|
||||
{
|
||||
ffPrintError(instance, FF_SOUND_MODULE_NAME, 0, &options->moduleArgs, "No active sound devices found");
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t index = 1;
|
||||
FF_LIST_FOR_EACH(FFSoundDevice*, device, filtered)
|
||||
{
|
||||
printDevice(instance, options, *device, filtered.length == 1 ? 0 : index++);
|
||||
}
|
||||
|
||||
FF_LIST_FOR_EACH(FFSoundDevice, device, result)
|
||||
{
|
||||
ffStrbufDestroy(&device->identifier);
|
||||
ffStrbufDestroy(&device->name);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void ffInitSoundOptions(FFSoundOptions* options)
|
||||
{
|
||||
options->moduleName = FF_SOUND_MODULE_NAME;
|
||||
ffOptionInitModuleArg(&options->moduleArgs);
|
||||
|
||||
options->soundType = FF_SOUND_TYPE_MAIN;
|
||||
}
|
||||
|
||||
bool ffParseSoundCommandOptions(FFSoundOptions* options, const char* key, const char* value)
|
||||
{
|
||||
const char* subKey = ffOptionTestPrefix(key, FF_SOUND_MODULE_NAME);
|
||||
if (!subKey) return false;
|
||||
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
|
||||
return true;
|
||||
|
||||
if (strcasecmp(subKey, "sound-type") == 0)
|
||||
{
|
||||
options->soundType = (FFSoundType) ffOptionParseEnum(key, value, (FFKeyValuePair[]) {
|
||||
{ "main", FF_SOUND_TYPE_MAIN },
|
||||
{ "active", FF_SOUND_TYPE_ACTIVE },
|
||||
{ "all", FF_SOUND_TYPE_ALL },
|
||||
{},
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void ffDestroySoundOptions(FFSoundOptions* options)
|
||||
{
|
||||
ffOptionDestroyModuleArg(&options->moduleArgs);
|
||||
}
|
||||
|
||||
#ifdef FF_HAVE_JSONC
|
||||
void ffParseSoundJsonObject(FFinstance* instance, json_object* module)
|
||||
{
|
||||
FFSoundOptions __attribute__((__cleanup__(ffDestroySoundOptions))) options;
|
||||
ffInitSoundOptions(&options);
|
||||
|
||||
if (module)
|
||||
{
|
||||
json_object_object_foreach(module, key, val)
|
||||
{
|
||||
if (ffJsonConfigParseModuleArgs(key, val, &options.moduleArgs))
|
||||
continue;
|
||||
|
||||
if (strcasecmp(key, "soundType") == 0)
|
||||
{
|
||||
int value;
|
||||
const char* error = ffJsonConfigParseEnum(val, &value, (FFKeyValuePair[]) {
|
||||
{ "main", FF_SOUND_TYPE_MAIN },
|
||||
{ "active", FF_SOUND_TYPE_ACTIVE },
|
||||
{ "all", FF_SOUND_TYPE_ALL },
|
||||
{},
|
||||
});
|
||||
if (error)
|
||||
ffPrintError(instance, FF_SOUND_MODULE_NAME, 0, &options.moduleArgs, "Invalid %s value: %s", key, error);
|
||||
else
|
||||
options.soundType = (FFSoundType) value;
|
||||
continue;
|
||||
}
|
||||
|
||||
ffPrintError(instance, FF_SOUND_MODULE_NAME, 0, &options.moduleArgs, "Unknown JSON key %s", key);
|
||||
}
|
||||
}
|
||||
|
||||
ffPrintSound(instance, &options);
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include "fastfetch.h"
|
||||
#include "modules/title/option.h"
|
||||
|
||||
#define FF_SOUND_MODULE_NAME "Sound"
|
||||
|
||||
void ffPrintSound(FFinstance* instance, FFSoundOptions* options);
|
||||
void ffInitSoundOptions(FFSoundOptions* options);
|
||||
bool ffParseSoundCommandOptions(FFSoundOptions* options, const char* key, const char* value);
|
||||
void ffDestroySoundOptions(FFSoundOptions* options);
|
||||
|
||||
#ifdef FF_HAVE_JSONC
|
||||
#include "common/jsonconfig.h"
|
||||
void ffParseSoundJsonObject(FFinstance* instance, json_object* module);
|
||||
#endif
|
||||
Reference in New Issue
Block a user