2023-06-07 10:39:53 +08:00
|
|
|
#include "common/printing.h"
|
2023-06-10 15:01:54 +08:00
|
|
|
#include "common/jsonconfig.h"
|
2023-06-07 10:39:53 +08:00
|
|
|
#include "detection/displayserver/displayserver.h"
|
2023-10-30 22:46:38 +08:00
|
|
|
#include "detection/de/de.h"
|
2023-06-07 10:39:53 +08:00
|
|
|
#include "modules/de/de.h"
|
2023-06-19 15:21:49 +08:00
|
|
|
#include "util/stringUtils.h"
|
2023-06-07 10:39:53 +08:00
|
|
|
|
|
|
|
|
#define FF_DE_NUM_FORMAT_ARGS 3
|
|
|
|
|
|
2023-06-24 10:59:53 +08:00
|
|
|
void ffPrintDE(FFDEOptions* options)
|
2023-06-07 10:39:53 +08:00
|
|
|
{
|
2023-06-24 10:59:53 +08:00
|
|
|
const FFDisplayServerResult* result = ffConnectDisplayServer();
|
2023-06-07 10:39:53 +08:00
|
|
|
|
|
|
|
|
if(result->dePrettyName.length == 0)
|
|
|
|
|
{
|
2023-06-24 10:59:53 +08:00
|
|
|
ffPrintError(FF_DE_MODULE_NAME, 0, &options->moduleArgs, "No DE found");
|
2023-06-07 10:39:53 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-30 22:46:38 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY version = ffStrbufCreate();
|
|
|
|
|
ffDetectDEVersion(&result->dePrettyName, &version, options);
|
|
|
|
|
|
2023-06-07 10:39:53 +08:00
|
|
|
if(options->moduleArgs.outputFormat.length == 0)
|
|
|
|
|
{
|
2023-08-15 21:40:22 +08:00
|
|
|
ffPrintLogoAndKey(FF_DE_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
|
2023-06-07 10:39:53 +08:00
|
|
|
|
|
|
|
|
ffStrbufWriteTo(&result->dePrettyName, stdout);
|
|
|
|
|
|
2023-10-30 22:46:38 +08:00
|
|
|
if(version.length > 0)
|
2023-06-07 10:39:53 +08:00
|
|
|
{
|
|
|
|
|
putchar(' ');
|
2023-10-30 22:46:38 +08:00
|
|
|
ffStrbufWriteTo(&version, stdout);
|
2023-06-07 10:39:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
putchar('\n');
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-06-24 10:59:53 +08:00
|
|
|
ffPrintFormat(FF_DE_MODULE_NAME, 0, &options->moduleArgs, FF_DE_NUM_FORMAT_ARGS, (FFformatarg[]){
|
2023-06-07 10:39:53 +08:00
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &result->deProcessName},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &result->dePrettyName},
|
2023-10-30 22:46:38 +08:00
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &version}
|
2023-06-07 10:39:53 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ffParseDECommandOptions(FFDEOptions* options, const char* key, const char* value)
|
|
|
|
|
{
|
|
|
|
|
const char* subKey = ffOptionTestPrefix(key, FF_DE_MODULE_NAME);
|
|
|
|
|
if (!subKey) return false;
|
|
|
|
|
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
|
|
|
|
|
return true;
|
|
|
|
|
|
2023-10-31 09:54:12 +08:00
|
|
|
if (ffStrEqualsIgnCase(subKey, "slow-version-detection"))
|
2023-10-30 22:46:38 +08:00
|
|
|
{
|
|
|
|
|
options->slowVersionDetection = ffOptionParseBoolean(value);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-07 10:39:53 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-17 10:56:54 +08:00
|
|
|
void ffParseDEJsonObject(FFDEOptions* options, yyjson_val* module)
|
2023-06-07 10:39:53 +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 10:39:53 +08:00
|
|
|
{
|
2023-08-17 16:09:41 +08:00
|
|
|
const char* key = yyjson_get_str(key_);
|
|
|
|
|
if(ffStrEqualsIgnCase(key, "type"))
|
|
|
|
|
continue;
|
2023-06-10 15:01:54 +08:00
|
|
|
|
2023-08-17 16:09:41 +08:00
|
|
|
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs))
|
|
|
|
|
continue;
|
2023-06-07 10:39:53 +08:00
|
|
|
|
2023-10-30 22:46:38 +08:00
|
|
|
if (ffStrEqualsIgnCase(key, "slowVersionDetection"))
|
|
|
|
|
{
|
|
|
|
|
options->slowVersionDetection = yyjson_get_bool(val);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-17 16:09:41 +08:00
|
|
|
ffPrintError(FF_DE_MODULE_NAME, 0, &options->moduleArgs, "Unknown JSON key %s", key);
|
2023-06-07 10:39:53 +08:00
|
|
|
}
|
|
|
|
|
}
|
2023-08-31 13:45:19 +08:00
|
|
|
|
2023-10-24 14:41:29 +08:00
|
|
|
void ffGenerateDEJsonConfig(FFDEOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
|
|
|
|
|
{
|
|
|
|
|
__attribute__((__cleanup__(ffDestroyDEOptions))) FFDEOptions defaultOptions;
|
|
|
|
|
ffInitDEOptions(&defaultOptions);
|
|
|
|
|
|
|
|
|
|
ffJsonConfigGenerateModuleArgsConfig(doc, module, &defaultOptions.moduleArgs, &options->moduleArgs);
|
2023-10-30 22:46:38 +08:00
|
|
|
|
|
|
|
|
if (defaultOptions.slowVersionDetection != options->slowVersionDetection)
|
|
|
|
|
yyjson_mut_obj_add_bool(doc, module, "slowVersionDetection", options->slowVersionDetection);
|
2023-10-24 14:41:29 +08:00
|
|
|
}
|
|
|
|
|
|
2023-10-23 10:14:22 +08:00
|
|
|
void ffGenerateDEJsonResult(FF_MAYBE_UNUSED FFDEOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
|
2023-08-31 13:45:19 +08:00
|
|
|
{
|
|
|
|
|
const FFDisplayServerResult* result = ffConnectDisplayServer();
|
|
|
|
|
|
|
|
|
|
if(result->dePrettyName.length == 0)
|
|
|
|
|
{
|
|
|
|
|
yyjson_mut_obj_add_str(doc, module, "error", "No DE found");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-30 22:46:38 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY version = ffStrbufCreate();
|
|
|
|
|
ffDetectDEVersion(&result->dePrettyName, &version, options);
|
|
|
|
|
|
2023-08-31 13:45:19 +08:00
|
|
|
yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
|
|
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "processName", &result->deProcessName);
|
|
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "prettyName", &result->dePrettyName);
|
2023-10-30 22:46:38 +08:00
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "version", &version);
|
2023-08-31 13:45:19 +08:00
|
|
|
}
|
2023-10-07 13:40:40 +08:00
|
|
|
|
|
|
|
|
void ffPrintDEHelpFormat(void)
|
|
|
|
|
{
|
|
|
|
|
ffPrintModuleFormatHelp(FF_DE_MODULE_NAME, "{2} {3}", FF_DE_NUM_FORMAT_ARGS, (const char* []) {
|
|
|
|
|
"DE process name",
|
|
|
|
|
"DE pretty name",
|
|
|
|
|
"DE version"
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-10-23 13:32:18 +08:00
|
|
|
|
|
|
|
|
void ffInitDEOptions(FFDEOptions* options)
|
|
|
|
|
{
|
2023-10-23 13:39:07 +08:00
|
|
|
ffOptionInitModuleBaseInfo(
|
|
|
|
|
&options->moduleInfo,
|
|
|
|
|
FF_DE_MODULE_NAME,
|
2023-11-17 14:12:17 +08:00
|
|
|
"Print desktop environment name",
|
2023-10-23 13:39:07 +08:00
|
|
|
ffParseDECommandOptions,
|
|
|
|
|
ffParseDEJsonObject,
|
|
|
|
|
ffPrintDE,
|
|
|
|
|
ffGenerateDEJsonResult,
|
|
|
|
|
ffPrintDEHelpFormat,
|
2023-10-24 14:41:29 +08:00
|
|
|
ffGenerateDEJsonConfig
|
2023-10-23 13:39:07 +08:00
|
|
|
);
|
2023-10-23 13:32:18 +08:00
|
|
|
ffOptionInitModuleArg(&options->moduleArgs);
|
2023-10-30 22:46:38 +08:00
|
|
|
|
|
|
|
|
options->slowVersionDetection = false;
|
2023-10-23 13:32:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ffDestroyDEOptions(FFDEOptions* options)
|
|
|
|
|
{
|
|
|
|
|
ffOptionDestroyModuleArg(&options->moduleArgs);
|
|
|
|
|
}
|