2022-07-25 13:15:01 +02:00
|
|
|
#include "common/printing.h"
|
2023-06-10 15:01:54 +08:00
|
|
|
#include "common/jsonconfig.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"
|
2023-06-19 15:21:49 +08:00
|
|
|
#include "util/stringUtils.h"
|
2022-02-13 23:51:13 +01:00
|
|
|
|
2022-03-23 15:43:30 +01:00
|
|
|
#include <ctype.h>
|
|
|
|
|
|
2023-06-07 16:19:07 +08:00
|
|
|
#define FF_PLAYER_DISPLAY_NAME "Media Player"
|
2022-09-11 13:09:33 +02:00
|
|
|
#define FF_PLAYER_NUM_FORMAT_ARGS 4
|
2022-02-13 23:51:13 +01:00
|
|
|
|
2023-06-24 10:59:53 +08:00
|
|
|
void ffPrintPlayer(FFPlayerOptions* options)
|
2022-02-13 23:51:13 +01:00
|
|
|
{
|
2023-06-24 10:59:53 +08:00
|
|
|
const FFMediaResult* media = ffDetectMedia();
|
2022-02-13 23:51:13 +01:00
|
|
|
|
2022-09-19 17:53:00 +02:00
|
|
|
if(media->error.length > 0)
|
2022-02-13 23:51:13 +01:00
|
|
|
{
|
2024-03-07 11:10:18 +08:00
|
|
|
ffPrintError(FF_PLAYER_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", media->error.chars);
|
2022-02-13 23:51:13 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-11 14:44:47 +08:00
|
|
|
if (media->player.length == 0)
|
|
|
|
|
{
|
|
|
|
|
ffPrintError(FF_PLAYER_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No media player detected");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-15 15:29:54 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY playerPretty = ffStrbufCreate();
|
2022-03-23 15:43:30 +01:00
|
|
|
|
2024-06-11 14:44:47 +08:00
|
|
|
if (media->url.length > 0)
|
|
|
|
|
{
|
|
|
|
|
//If we are on a website, prepend the website name
|
|
|
|
|
if(
|
|
|
|
|
ffStrbufIgnCaseEqualS(&media->playerId, "spotify") ||
|
|
|
|
|
ffStrbufIgnCaseEqualS(&media->playerId, "vlc")
|
|
|
|
|
) {} // do noting, surely not a website, even if the url is set
|
|
|
|
|
else if(ffStrbufStartsWithS(&media->url, "https://www."))
|
|
|
|
|
ffStrbufAppendS(&playerPretty, media->url.chars + 12);
|
|
|
|
|
else if(ffStrbufStartsWithS(&media->url, "http://www."))
|
|
|
|
|
ffStrbufAppendS(&playerPretty, media->url.chars + 11);
|
|
|
|
|
else if(ffStrbufStartsWithS(&media->url, "https://"))
|
|
|
|
|
ffStrbufAppendS(&playerPretty, media->url.chars + 8);
|
|
|
|
|
else if(ffStrbufStartsWithS(&media->url, "http://"))
|
|
|
|
|
ffStrbufAppendS(&playerPretty, media->url.chars + 7);
|
|
|
|
|
}
|
2022-03-23 15:43:30 +01: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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Check again for length, as we may have removed everything.
|
|
|
|
|
bool playerPrettyIsCustom = playerPretty.length > 0;
|
|
|
|
|
|
|
|
|
|
//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]);
|
|
|
|
|
|
|
|
|
|
if(playerPrettyIsCustom)
|
|
|
|
|
ffStrbufAppendS(&playerPretty, " (");
|
|
|
|
|
|
|
|
|
|
ffStrbufAppend(&playerPretty, &media->player);
|
|
|
|
|
|
|
|
|
|
if(playerPrettyIsCustom)
|
|
|
|
|
ffStrbufAppendC(&playerPretty, ')');
|
|
|
|
|
|
2023-06-07 16:19:07 +08:00
|
|
|
if(options->moduleArgs.outputFormat.length == 0)
|
2022-02-13 23:51:13 +01:00
|
|
|
{
|
2023-08-15 21:40:22 +08:00
|
|
|
ffPrintLogoAndKey(FF_PLAYER_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
|
2022-03-23 15:43:30 +01:00
|
|
|
ffStrbufPutTo(&playerPretty, stdout);
|
2022-02-13 23:51:13 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-03-07 10:47:28 +08:00
|
|
|
FF_PRINT_FORMAT_CHECKED(FF_PLAYER_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, FF_PLAYER_NUM_FORMAT_ARGS, ((FFformatarg[]){
|
2024-05-27 16:41:37 +08:00
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &playerPretty, "player"},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &media->player, "name"},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &media->playerId, "id"},
|
2024-06-11 14:44:47 +08:00
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &media->url, "url"},
|
2024-03-07 10:47:28 +08:00
|
|
|
}));
|
2022-02-13 23:51:13 +01:00
|
|
|
}
|
|
|
|
|
}
|
2023-06-07 16:19:07 +08:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-17 10:56:54 +08:00
|
|
|
void ffParsePlayerJsonObject(FFPlayerOptions* options, yyjson_val* module)
|
2023-06-07 16:19:07 +08:00
|
|
|
{
|
2023-08-17 16:09:41 +08:00
|
|
|
yyjson_val *key_, *val;
|
|
|
|
|
size_t idx, max;
|
|
|
|
|
yyjson_obj_foreach(module, idx, max, key_, val)
|
2023-06-07 16:19:07 +08:00
|
|
|
{
|
2023-08-17 16:09:41 +08:00
|
|
|
const char* key = yyjson_get_str(key_);
|
|
|
|
|
if(ffStrEqualsIgnCase(key, "type"))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs))
|
|
|
|
|
continue;
|
|
|
|
|
|
2024-03-07 11:10:18 +08:00
|
|
|
ffPrintError(FF_PLAYER_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", key);
|
2023-06-07 16:19:07 +08:00
|
|
|
}
|
|
|
|
|
}
|
2023-09-06 15:53:52 +08:00
|
|
|
|
2023-10-24 14:41:29 +08:00
|
|
|
void ffGeneratePlayerJsonConfig(FFPlayerOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
|
|
|
|
|
{
|
|
|
|
|
__attribute__((__cleanup__(ffDestroyPlayerOptions))) FFPlayerOptions defaultOptions;
|
|
|
|
|
ffInitPlayerOptions(&defaultOptions);
|
|
|
|
|
|
|
|
|
|
ffJsonConfigGenerateModuleArgsConfig(doc, module, &defaultOptions.moduleArgs, &options->moduleArgs);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-23 10:14:22 +08:00
|
|
|
void ffGeneratePlayerJsonResult(FF_MAYBE_UNUSED FFMediaOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
|
2023-09-06 15:53:52 +08:00
|
|
|
{
|
|
|
|
|
const FFMediaResult* media = ffDetectMedia();
|
|
|
|
|
|
|
|
|
|
if(media->error.length > 0)
|
|
|
|
|
{
|
|
|
|
|
yyjson_mut_obj_add_strbuf(doc, module, "error", &media->error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
|
|
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "player", &media->player);
|
|
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "playerId", &media->playerId);
|
|
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "url", &media->url);
|
|
|
|
|
}
|
2023-10-07 13:40:40 +08:00
|
|
|
|
|
|
|
|
void ffPrintPlayerHelpFormat(void)
|
|
|
|
|
{
|
2024-03-07 10:47:28 +08:00
|
|
|
FF_PRINT_MODULE_FORMAT_HELP_CHECKED(FF_PLAYER_MODULE_NAME, "{1}", FF_PLAYER_NUM_FORMAT_ARGS, ((const char* []) {
|
2024-05-27 16:41:37 +08:00
|
|
|
"Pretty player name - player",
|
|
|
|
|
"Player name - name",
|
|
|
|
|
"Player Identifier - id",
|
|
|
|
|
"URL name - url",
|
2024-03-07 10:47:28 +08:00
|
|
|
}));
|
2023-10-07 13:40:40 +08:00
|
|
|
}
|
2023-10-23 13:32:18 +08:00
|
|
|
|
|
|
|
|
void ffInitPlayerOptions(FFPlayerOptions* options)
|
|
|
|
|
{
|
2023-10-23 13:39:07 +08:00
|
|
|
ffOptionInitModuleBaseInfo(
|
|
|
|
|
&options->moduleInfo,
|
|
|
|
|
FF_PLAYER_MODULE_NAME,
|
2023-11-17 14:12:17 +08:00
|
|
|
"Print music player name",
|
2023-10-23 13:39:07 +08:00
|
|
|
ffParsePlayerCommandOptions,
|
|
|
|
|
ffParsePlayerJsonObject,
|
|
|
|
|
ffPrintPlayer,
|
|
|
|
|
ffGeneratePlayerJsonResult,
|
|
|
|
|
ffPrintPlayerHelpFormat,
|
2023-10-24 14:41:29 +08:00
|
|
|
ffGeneratePlayerJsonConfig
|
2023-10-23 13:39:07 +08:00
|
|
|
);
|
2024-07-23 23:02:35 +08:00
|
|
|
ffOptionInitModuleArg(&options->moduleArgs, "");
|
2023-10-23 13:32:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ffDestroyPlayerOptions(FFPlayerOptions* options)
|
|
|
|
|
{
|
|
|
|
|
ffOptionDestroyModuleArg(&options->moduleArgs);
|
|
|
|
|
}
|