mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2025-02-20 11:43:27 +08:00
Player: add JSON config support
This commit is contained in:
parent
fde86657f2
commit
a4a801d83a
@ -291,8 +291,8 @@ set(LIBFASTFETCH_SRC
|
||||
src/modules/opengl/opengl.c
|
||||
src/modules/os/os.c
|
||||
src/modules/packages/packages.c
|
||||
src/modules/player.c
|
||||
src/modules/processes.c
|
||||
src/modules/player/player.c
|
||||
src/modules/poweradapter/poweradapter.c
|
||||
src/modules/publicip.c
|
||||
src/modules/display/display.c
|
||||
|
@ -93,7 +93,7 @@ static void defaultConfig(FFinstance* instance)
|
||||
initModuleArg(&instance->config.publicIP);
|
||||
initModuleArg(&instance->config.weather);
|
||||
ffInitWifiOptions(&instance->config.wifi);
|
||||
initModuleArg(&instance->config.player);
|
||||
ffInitPlayerOptions(&instance->config.player);
|
||||
ffInitMediaOptions(&instance->config.media);
|
||||
ffInitDateTimeOptions(&instance->config.dateTime);
|
||||
ffInitVulkanOptions(&instance->config.vulkan);
|
||||
@ -138,8 +138,6 @@ static void defaultConfig(FFinstance* instance)
|
||||
instance->config.weatherTimeout = 0;
|
||||
ffStrbufInitS(&instance->config.weatherOutputFormat, "%t+-+%C+(%l)");
|
||||
|
||||
ffStrbufInit(&instance->config.playerName);
|
||||
|
||||
instance->config.percentType = 1;
|
||||
}
|
||||
|
||||
@ -313,7 +311,7 @@ static void destroyConfig(FFinstance* instance)
|
||||
ffDestroyWallpaperOptions(&instance->config.wallpaper);
|
||||
destroyModuleArg(&instance->config.weather);
|
||||
ffDestroyWifiOptions(&instance->config.wifi);
|
||||
destroyModuleArg(&instance->config.player);
|
||||
ffDestroyPlayerOptions(&instance->config.player);
|
||||
ffDestroyMediaOptions(&instance->config.media);
|
||||
ffDestroyDateTimeOptions(&instance->config.dateTime);
|
||||
ffDestroyVulkanOptions(&instance->config.vulkan);
|
||||
@ -353,7 +351,6 @@ static void destroyConfig(FFinstance* instance)
|
||||
|
||||
ffStrbufDestroy(&instance->config.publicIpUrl);
|
||||
ffStrbufDestroy(&instance->config.weatherOutputFormat);
|
||||
ffStrbufDestroy(&instance->config.playerName);
|
||||
}
|
||||
|
||||
static void destroyState(FFinstance* instance)
|
||||
|
@ -101,14 +101,14 @@ static bool getBusProperties(FFDBusData* data, const char* busName, FFMediaResul
|
||||
|
||||
static void getCustomBus(FFDBusData* data, const FFinstance* instance, FFMediaResult* result)
|
||||
{
|
||||
if(ffStrbufStartsWithS(&instance->config.playerName, FF_DBUS_MPRIS_PREFIX))
|
||||
if(ffStrbufStartsWithS(&instance->config.player.name, FF_DBUS_MPRIS_PREFIX))
|
||||
{
|
||||
getBusProperties(data, instance->config.playerName.chars, result);
|
||||
getBusProperties(data, instance->config.player.name.chars, result);
|
||||
return;
|
||||
}
|
||||
|
||||
FF_STRBUF_AUTO_DESTROY busName = ffStrbufCreateS(FF_DBUS_MPRIS_PREFIX);
|
||||
ffStrbufAppend(&busName, &instance->config.playerName);
|
||||
ffStrbufAppend(&busName, &instance->config.player.name);
|
||||
getBusProperties(data, busName.chars, result);
|
||||
}
|
||||
|
||||
@ -158,7 +158,7 @@ static const char* getMedia(const FFinstance* instance, FFMediaResult* result)
|
||||
if(error != NULL)
|
||||
return error;
|
||||
|
||||
if(instance->config.playerName.length > 0)
|
||||
if(instance->config.player.name.length > 0)
|
||||
getCustomBus(&data, instance, result);
|
||||
else
|
||||
getBestBus(&data, result);
|
||||
|
@ -1011,7 +1011,7 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
|
||||
else if(ffParseLocalIpCommandOptions(&instance->config.localIP, key, value)) {}
|
||||
else if(optionParseModuleArgs(key, value, "publicip", &instance->config.publicIP)) {}
|
||||
else if(optionParseModuleArgs(key, value, "weather", &instance->config.weather)) {}
|
||||
else if(optionParseModuleArgs(key, value, "player", &instance->config.player)) {}
|
||||
else if(ffParsePlayerCommandOptions(&instance->config.player, key, value)) {}
|
||||
else if(ffParseMediaCommandOptions(&instance->config.media, key, value)) {}
|
||||
else if(ffParseDateTimeCommandOptions(&instance->config.dateTime, key, value)) {}
|
||||
else if(ffParseVulkanCommandOptions(&instance->config.vulkan, key, value)) {}
|
||||
@ -1088,8 +1088,6 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
|
||||
//Module options//
|
||||
//////////////////
|
||||
|
||||
else if(strcasecmp(key, "--player-name") == 0)
|
||||
optionParseString(key, value, &instance->config.playerName);
|
||||
else if(strcasecmp(key, "--publicip-url") == 0)
|
||||
optionParseString(key, value, &instance->config.publicIpUrl);
|
||||
else if(strcasecmp(key, "--publicip-timeout") == 0)
|
||||
@ -1230,8 +1228,8 @@ static void parseStructureCommand(FFinstance* instance, const char* line)
|
||||
ffPrintWifi(instance, &instance->config.wifi);
|
||||
else if(strcasecmp(line, "weather") == 0)
|
||||
ffPrintWeather(instance);
|
||||
else if(strcasecmp(line, "player") == 0)
|
||||
ffPrintPlayer(instance);
|
||||
else if(strcasecmp(line, FF_PLAYER_MODULE_NAME) == 0)
|
||||
ffPrintPlayer(instance, &instance->config.player);
|
||||
else if(strcasecmp(line, FF_MEDIA_MODULE_NAME) == 0)
|
||||
ffPrintMedia(instance, &instance->config.media);
|
||||
else if(strcasecmp(line, FF_DATETIME_MODULE_NAME) == 0)
|
||||
|
@ -85,7 +85,7 @@ typedef struct FFconfig
|
||||
FFLocalIpOptions localIP;
|
||||
FFModuleArgs publicIP;
|
||||
FFModuleArgs weather;
|
||||
FFModuleArgs player;
|
||||
FFPlayerOptions player;
|
||||
FFMediaOptions media;
|
||||
FFDateTimeOptions dateTime;
|
||||
FFVulkanOptions vulkan;
|
||||
@ -129,8 +129,6 @@ typedef struct FFconfig
|
||||
FFstrbuf weatherOutputFormat;
|
||||
uint32_t weatherTimeout;
|
||||
|
||||
FFstrbuf playerName;
|
||||
|
||||
uint32_t percentType;
|
||||
|
||||
bool jsonConfig;
|
||||
@ -190,7 +188,6 @@ void ffPrepareWeather(FFinstance* instance);
|
||||
void ffPrintChassis(FFinstance* instance);
|
||||
void ffPrintProcesses(FFinstance* instance);
|
||||
void ffPrintTheme(FFinstance* instance);
|
||||
void ffPrintPlayer(FFinstance* instance);
|
||||
void ffPrintPublicIp(FFinstance* instance);
|
||||
void ffPrintWeather(FFinstance* instance);
|
||||
|
||||
|
@ -50,7 +50,7 @@ int main(int argc, char** argv)
|
||||
ffPrintDisk(&instance, &instance.config.disk);
|
||||
ffPrintBattery(&instance, &instance.config.battery);
|
||||
ffPrintPowerAdapter(&instance, &instance.config.powerAdapter);
|
||||
//ffPrintPlayer(&instance);
|
||||
//ffPrintPlayer(&instance, &instance.config.player);
|
||||
//ffPrintMedia(&instance, &instance.config.media);
|
||||
//ffPrintLocalIp(&instance);
|
||||
//ffPrintPublicIp(&instance);
|
||||
|
@ -113,6 +113,7 @@ static bool parseModuleJsonObject(FFinstance* instance, const char* type, json_o
|
||||
case 'P': {
|
||||
return
|
||||
tryModule(instance, type, module, FF_PACKAGES_MODULE_NAME, ffParsePackagesJsonObject) ||
|
||||
tryModule(instance, type, module, FF_PLAYER_MODULE_NAME, ffParsePlayerJsonObject) ||
|
||||
tryModule(instance, type, module, FF_POWERADAPTER_MODULE_NAME, ffParsePowerAdapterJsonObject) ||
|
||||
false;
|
||||
}
|
||||
|
@ -32,6 +32,7 @@
|
||||
#include "modules/opencl/opencl.h"
|
||||
#include "modules/os/os.h"
|
||||
#include "modules/packages/packages.h"
|
||||
#include "modules/player/player.h"
|
||||
#include "modules/poweradapter/poweradapter.h"
|
||||
#include "modules/separator/separator.h"
|
||||
#include "modules/shell/shell.h"
|
||||
|
@ -30,6 +30,7 @@
|
||||
#include "modules/opencl/option.h"
|
||||
#include "modules/os/option.h"
|
||||
#include "modules/packages/option.h"
|
||||
#include "modules/player/option.h"
|
||||
#include "modules/poweradapter/option.h"
|
||||
#include "modules/separator/option.h"
|
||||
#include "modules/shell/option.h"
|
||||
|
13
src/modules/player/option.h
Normal file
13
src/modules/player/option.h
Normal file
@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
// This file will be included in "fastfetch.h", do NOT put unnecessary things here
|
||||
|
||||
#include "common/option.h"
|
||||
|
||||
typedef struct FFPlayerOptions
|
||||
{
|
||||
const char* moduleName;
|
||||
FFModuleArgs moduleArgs;
|
||||
|
||||
FFstrbuf name;
|
||||
} FFPlayerOptions;
|
@ -1,19 +1,20 @@
|
||||
#include "fastfetch.h"
|
||||
#include "common/printing.h"
|
||||
#include "detection/media/media.h"
|
||||
#include "modules/player/player.h"
|
||||
|
||||
#include <ctype.h>
|
||||
|
||||
#define FF_PLAYER_MODULE_NAME "Media Player"
|
||||
#define FF_PLAYER_DISPLAY_NAME "Media Player"
|
||||
#define FF_PLAYER_NUM_FORMAT_ARGS 4
|
||||
|
||||
void ffPrintPlayer(FFinstance* instance)
|
||||
void ffPrintPlayer(FFinstance* instance, FFPlayerOptions* options)
|
||||
{
|
||||
const FFMediaResult* media = ffDetectMedia(instance);
|
||||
|
||||
if(media->error.length > 0)
|
||||
{
|
||||
ffPrintError(instance, FF_PLAYER_MODULE_NAME, 0, &instance->config.player, "%s", media->error.chars);
|
||||
ffPrintError(instance, FF_PLAYER_DISPLAY_NAME, 0, &options->moduleArgs, "%s", media->error.chars);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -55,14 +56,14 @@ void ffPrintPlayer(FFinstance* instance)
|
||||
if(playerPrettyIsCustom)
|
||||
ffStrbufAppendC(&playerPretty, ')');
|
||||
|
||||
if(instance->config.player.outputFormat.length == 0)
|
||||
if(options->moduleArgs.outputFormat.length == 0)
|
||||
{
|
||||
ffPrintLogoAndKey(instance, FF_PLAYER_MODULE_NAME, 0, &instance->config.player.key);
|
||||
ffPrintLogoAndKey(instance, FF_PLAYER_DISPLAY_NAME, 0, &options->moduleArgs.key);
|
||||
ffStrbufPutTo(&playerPretty, stdout);
|
||||
}
|
||||
else
|
||||
{
|
||||
ffPrintFormat(instance, FF_PLAYER_MODULE_NAME, 0, &instance->config.player, FF_PLAYER_NUM_FORMAT_ARGS, (FFformatarg[]){
|
||||
ffPrintFormat(instance, FF_PLAYER_DISPLAY_NAME, 0, &options->moduleArgs, FF_PLAYER_NUM_FORMAT_ARGS, (FFformatarg[]){
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &playerPretty},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &media->player},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &media->playerId},
|
||||
@ -70,3 +71,49 @@ void ffPrintPlayer(FFinstance* instance)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void ffInitPlayerOptions(FFPlayerOptions* options)
|
||||
{
|
||||
options->moduleName = FF_PLAYER_MODULE_NAME;
|
||||
ffOptionInitModuleArg(&options->moduleArgs);
|
||||
|
||||
ffStrbufInit(&options->name);
|
||||
}
|
||||
|
||||
bool ffParsePlayerCommandOptions(FFPlayerOptions* options, const char* key, const char* value)
|
||||
{
|
||||
const char* subKey = ffOptionTestPrefix(key, FF_PLAYER_MODULE_NAME);
|
||||
if (!subKey) return false;
|
||||
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void ffDestroyPlayerOptions(FFPlayerOptions* options)
|
||||
{
|
||||
ffOptionDestroyModuleArg(&options->moduleArgs);
|
||||
|
||||
ffStrbufDestroy(&options->name);
|
||||
}
|
||||
|
||||
#ifdef FF_HAVE_JSONC
|
||||
void ffParsePlayerJsonObject(FFinstance* instance, json_object* module)
|
||||
{
|
||||
FFPlayerOptions __attribute__((__cleanup__(ffDestroyPlayerOptions))) options;
|
||||
ffInitPlayerOptions(&options);
|
||||
|
||||
if (module)
|
||||
{
|
||||
json_object_object_foreach(module, key, val)
|
||||
{
|
||||
if (ffJsonConfigParseModuleArgs(key, val, &options.moduleArgs))
|
||||
continue;
|
||||
|
||||
ffPrintError(instance, FF_PLAYER_MODULE_NAME, 0, &options.moduleArgs, "Unknown JSON key %s", key);
|
||||
}
|
||||
}
|
||||
|
||||
ffPrintPlayer(instance, &options);
|
||||
}
|
||||
#endif
|
16
src/modules/player/player.h
Normal file
16
src/modules/player/player.h
Normal file
@ -0,0 +1,16 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "fastfetch.h"
|
||||
|
||||
#define FF_PLAYER_MODULE_NAME "Player"
|
||||
|
||||
void ffPrintPlayer(FFinstance* instance, FFPlayerOptions* options);
|
||||
void ffInitPlayerOptions(FFPlayerOptions* options);
|
||||
bool ffParsePlayerCommandOptions(FFPlayerOptions* options, const char* key, const char* value);
|
||||
void ffDestroyPlayerOptions(FFPlayerOptions* options);
|
||||
|
||||
#ifdef FF_HAVE_JSONC
|
||||
#include "common/jsonconfig.h"
|
||||
void ffParsePlayerJsonObject(FFinstance* instance, json_object* module);
|
||||
#endif
|
Loading…
x
Reference in New Issue
Block a user