Files
fastfetch/src/modules/player/player.c
T

127 lines
5.2 KiB
C
Raw Normal View History

2026-01-06 09:48:38 +08:00
#include "common/printing.h"
#include "common/jsonconfig.h"
2026-05-28 15:58:08 +08:00
#include "common/strutil.h"
2022-09-19 20:05:23 +08:00
#include "detection/media/media.h"
2023-06-07 16:19:07 +08:00
#include "modules/player/player.h"
2022-02-13 23:51:13 +01:00
#include <ctype.h>
2023-06-07 16:19:07 +08:00
#define FF_PLAYER_DISPLAY_NAME "Media Player"
2022-02-13 23:51:13 +01:00
2026-03-29 09:28:49 +08:00
bool ffPrintPlayer(FFPlayerOptions* options) {
const FFMediaResult* media = ffDetectMedia(false);
2022-02-13 23:51:13 +01:00
2026-03-29 09:28:49 +08:00
if (media->error.length > 0) {
ffPrintError(FF_PLAYER_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", media->error.chars);
return false;
2022-02-13 23:51:13 +01:00
}
2026-03-29 09:28:49 +08:00
if (media->player.length == 0) {
2024-06-11 14:44:47 +08:00
ffPrintError(FF_PLAYER_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No media player detected");
return false;
2024-06-11 14:44:47 +08:00
}
FF_STRBUF_AUTO_DESTROY playerPretty = ffStrbufCreate();
2026-03-29 09:28:49 +08:00
if (media->url.length > 0) {
// If we are on a website, prepend the website name
if (
2024-06-11 14:44:47 +08:00
ffStrbufIgnCaseEqualS(&media->playerId, "spotify") ||
2026-03-29 09:28:49 +08:00
ffStrbufIgnCaseEqualS(&media->playerId, "vlc")) {
} // do noting, surely not a website, even if the url is set
else if (ffStrbufStartsWithS(&media->url, "https://www.")) {
2024-06-11 14:44:47 +08:00
ffStrbufAppendS(&playerPretty, media->url.chars + 12);
2026-03-29 09:28:49 +08:00
} else if (ffStrbufStartsWithS(&media->url, "http://www.")) {
2024-06-11 14:44:47 +08:00
ffStrbufAppendS(&playerPretty, media->url.chars + 11);
2026-03-29 09:28:49 +08:00
} else if (ffStrbufStartsWithS(&media->url, "https://")) {
2024-06-11 14:44:47 +08:00
ffStrbufAppendS(&playerPretty, media->url.chars + 8);
2026-03-29 09:28:49 +08:00
} else if (ffStrbufStartsWithS(&media->url, "http://")) {
2024-06-11 14:44:47 +08:00
ffStrbufAppendS(&playerPretty, media->url.chars + 7);
2026-03-29 09:28:49 +08:00
}
2024-06-11 14:44:47 +08:00
}
2026-03-29 09:28:49 +08:00
// If we found a website name, make it more pretty
if (playerPretty.length > 0) {
ffStrbufSubstrBeforeFirstC(&playerPretty, '/'); // Remove the path
ffStrbufSubstrBeforeLastC(&playerPretty, '.'); // Remove the TLD
}
2026-03-29 09:28:49 +08:00
// Check again for length, as we may have removed everything.
bool playerPrettyIsCustom = playerPretty.length > 0;
2026-03-29 09:28:49 +08:00
// If we don't have subdomains, it is usually more pretty to capitalize the first letter.
if (playerPrettyIsCustom && ffStrbufFirstIndexC(&playerPretty, '.') == playerPretty.length) {
playerPretty.chars[0] = (char) toupper(playerPretty.chars[0]);
2026-03-29 09:28:49 +08:00
}
2026-03-29 09:28:49 +08:00
if (playerPrettyIsCustom) {
ffStrbufAppendS(&playerPretty, " (");
2026-03-29 09:28:49 +08:00
}
ffStrbufAppend(&playerPretty, &media->player);
2026-03-29 09:28:49 +08:00
if (playerPrettyIsCustom) {
ffStrbufAppendC(&playerPretty, ')');
2026-03-29 09:28:49 +08:00
}
2026-03-29 09:28:49 +08:00
if (options->moduleArgs.outputFormat.length == 0) {
ffPrintLogoAndKey(FF_PLAYER_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
ffStrbufPutTo(&playerPretty, stdout);
2026-03-29 09:28:49 +08:00
} else {
FF_PRINT_FORMAT_CHECKED(FF_PLAYER_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
FF_ARG(playerPretty, "player"),
FF_ARG(media->player, "name"),
FF_ARG(media->playerId, "id"),
FF_ARG(media->url, "url"),
}));
2022-02-13 23:51:13 +01:00
}
return true;
2022-02-13 23:51:13 +01:00
}
2023-06-07 16:19:07 +08:00
2026-03-29 09:28:49 +08:00
void ffParsePlayerJsonObject(FFPlayerOptions* options, yyjson_val* module) {
yyjson_val *key, *val;
2023-08-17 16:09:41 +08:00
size_t idx, max;
2026-03-29 09:28:49 +08:00
yyjson_obj_foreach (module, idx, max, key, val) {
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) {
2023-08-17 16:09:41 +08:00
continue;
2026-03-29 09:28:49 +08:00
}
2023-08-17 16:09:41 +08:00
ffPrintError(FF_PLAYER_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
2023-06-07 16:19:07 +08:00
}
}
2023-09-06 15:53:52 +08:00
2026-03-29 09:28:49 +08:00
void ffGeneratePlayerJsonConfig(FFPlayerOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
2023-10-24 14:41:29 +08:00
}
2026-04-09 16:20:59 +08:00
bool ffGeneratePlayerJsonResult(FF_A_UNUSED FFMediaOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
yyjson_mut_obj_add_str(doc, module, "error", "Player module is an alias of Media module");
return false;
2023-09-06 15:53:52 +08:00
}
2023-10-07 13:40:40 +08:00
2026-03-29 09:28:49 +08:00
void ffInitPlayerOptions(FFPlayerOptions* options) {
ffOptionInitModuleArg(&options->moduleArgs, "󰥠");
}
2026-03-29 09:28:49 +08:00
void ffDestroyPlayerOptions(FFPlayerOptions* options) {
ffOptionDestroyModuleArg(&options->moduleArgs);
}
2025-08-03 22:50:56 +08:00
FFModuleBaseInfo ffPlayerModuleInfo = {
2024-12-11 10:47:02 +08:00
.name = FF_PLAYER_MODULE_NAME,
2026-04-16 18:27:11 +08:00
.description = "Print the music player name that is currently active",
.initOptions = (void*) ffInitPlayerOptions,
.destroyOptions = (void*) ffDestroyPlayerOptions,
2024-12-11 10:47:02 +08:00
.parseJsonObject = (void*) ffParsePlayerJsonObject,
.printModule = (void*) ffPrintPlayer,
.generateJsonResult = (void*) ffGeneratePlayerJsonResult,
.generateJsonConfig = (void*) ffGeneratePlayerJsonConfig,
.formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
2026-03-30 10:27:23 +08:00
{ "Pretty player name", "player" },
{ "Player name", "name" },
{ "Player Identifier", "id" },
{ "URL name", "url" },
}))
};