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

150 lines
6.0 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>
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) {
2026-08-14 23:33:54 +08:00
ffPrintError(FF_MODULE_GET_DISPLAY_NAME(Player), 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) {
2026-08-14 23:33:54 +08:00
ffPrintError(FF_MODULE_GET_DISPLAY_NAME(Player), 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) {
2026-08-14 23:33:54 +08:00
ffPrintLogoAndKey(FF_MODULE_GET_DISPLAY_NAME(Player), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
ffStrbufPutTo(&playerPretty, stdout);
2026-03-29 09:28:49 +08:00
} else {
2026-08-14 23:33:54 +08:00
FF_PRINT_FORMAT_CHECKED(FF_MODULE_GET_DISPLAY_NAME(Player), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
2026-03-29 09:28:49 +08:00
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
2026-08-14 23:33:54 +08:00
ffPrintError(FF_MODULE_GET_DISPLAY_NAME(Player), 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
[[gnu::cold]]
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
}
bool ffGeneratePlayerJsonResult([[maybe_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 = {
2026-08-14 23:33:54 +08:00
.name = "Player",
2026-04-16 18:27:11 +08:00
.description = "Print the music player name that is currently active",
2026-08-14 20:38:58 +08:00
.displayName = {
.en = "Media Player",
2026-08-16 10:51:42 +08:00
.ar = "مشغل الوسائط",
2026-08-21 10:19:06 +08:00
.cs = "Přehrávač médií",
2026-08-14 20:38:58 +08:00
.de = "Medienplayer",
.es = "Reproductor multimedia",
2026-10-03 13:35:56 +03:00
.fi = "Mediasoitin",
2026-08-14 20:38:58 +08:00
.fr = "Lecteur multimédia",
2026-08-20 16:25:32 +00:00
.gl = "Reprodutor multimedia",
2026-08-16 10:51:42 +08:00
.he = "נגן מדיה",
2026-08-21 10:19:06 +08:00
.id = "Pemutar Media",
2026-08-14 20:38:58 +08:00
.it = "Lettore multimediale",
.ja = "メディアプレイヤー",
.ko = "미디어 플레이어",
.pl = "Odtwarzacz multimediów",
2026-08-16 10:37:06 +08:00
.pt = "Reprodutor de mídia",
2026-08-14 20:38:58 +08:00
.ru = "Медиаплеер",
2026-08-21 10:19:06 +08:00
.tr = "Medya Oynatıcı",
.uk = "Медіаплеєр",
.vi = "Trình phát phương tiện",
2026-08-14 20:38:58 +08:00
.zh_CN = "媒体播放器",
.zh_TW = "媒體播放器",
},
.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" },
})),
2026-09-22 14:49:43 +08:00
.defaultOrder = 47,
};