2023-08-09 13:25:22 +08:00
|
|
|
#include "common/printing.h"
|
|
|
|
|
#include "common/jsonconfig.h"
|
2023-08-09 16:31:24 +08:00
|
|
|
#include "detection/monitor/monitor.h"
|
|
|
|
|
#include "modules/monitor/monitor.h"
|
2023-08-09 13:25:22 +08:00
|
|
|
#include "util/stringUtils.h"
|
|
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
|
|
2024-03-07 10:47:28 +08:00
|
|
|
#define FF_MONITOR_NUM_FORMAT_ARGS 10
|
2023-08-09 13:25:22 +08:00
|
|
|
|
2023-08-09 16:31:24 +08:00
|
|
|
void ffPrintMonitor(FFMonitorOptions* options)
|
2023-08-09 13:25:22 +08:00
|
|
|
{
|
2023-08-09 16:31:24 +08:00
|
|
|
FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFMonitorResult));
|
2023-08-09 13:25:22 +08:00
|
|
|
|
2023-08-09 16:31:24 +08:00
|
|
|
const char* error = ffDetectMonitor(&result);
|
2023-08-09 13:25:22 +08:00
|
|
|
|
|
|
|
|
if(error)
|
|
|
|
|
{
|
2024-03-07 11:10:18 +08:00
|
|
|
ffPrintError(FF_MONITOR_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
|
2023-08-09 13:25:22 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!result.length)
|
|
|
|
|
{
|
2024-03-07 11:10:18 +08:00
|
|
|
ffPrintError(FF_MONITOR_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No physical display detected");
|
2023-08-09 13:25:22 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FF_STRBUF_AUTO_DESTROY key = ffStrbufCreate();
|
2023-08-15 21:40:22 +08:00
|
|
|
uint32_t index = 0;
|
2023-08-09 16:31:24 +08:00
|
|
|
FF_LIST_FOR_EACH(FFMonitorResult, display, result)
|
2023-08-09 13:25:22 +08:00
|
|
|
{
|
|
|
|
|
double inch = sqrt(display->physicalWidth * display->physicalWidth + display->physicalHeight * display->physicalHeight) / 25.4;
|
2023-08-09 16:18:57 +08:00
|
|
|
double ppi = sqrt(display->width * display->width + display->height * display->height) / inch;
|
2023-08-09 13:25:22 +08:00
|
|
|
|
2023-08-15 21:40:22 +08:00
|
|
|
ffStrbufClear(&key);
|
|
|
|
|
if(options->moduleArgs.key.length == 0)
|
|
|
|
|
{
|
|
|
|
|
ffStrbufAppendF(&key, "%s (%s)", FF_MONITOR_MODULE_NAME, display->name.chars);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
uint32_t moduleIndex = result.length == 1 ? 0 : index + 1;
|
2024-03-07 10:47:28 +08:00
|
|
|
FF_PARSE_FORMAT_STRING_CHECKED(&key, &options->moduleArgs.key, 2, ((FFformatarg[]){
|
2023-08-15 21:40:22 +08:00
|
|
|
{FF_FORMAT_ARG_TYPE_UINT, &moduleIndex},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &display->name},
|
2024-03-07 10:47:28 +08:00
|
|
|
}));
|
2023-08-15 21:40:22 +08:00
|
|
|
}
|
|
|
|
|
|
2023-08-09 13:25:22 +08:00
|
|
|
if(options->moduleArgs.outputFormat.length == 0)
|
|
|
|
|
{
|
2023-08-15 21:40:22 +08:00
|
|
|
ffPrintLogoAndKey(key.chars, 0, &options->moduleArgs, FF_PRINT_TYPE_NO_CUSTOM_KEY);
|
2023-08-09 13:25:22 +08:00
|
|
|
|
2023-08-09 16:18:57 +08:00
|
|
|
printf("%ux%u px", display->width, display->height);
|
2023-08-09 13:25:22 +08:00
|
|
|
if (inch > 0)
|
2023-08-09 16:18:57 +08:00
|
|
|
printf(" - %ux%u mm (%.2f inches, %.2f ppi)\n", display->physicalWidth, display->physicalHeight, inch, ppi);
|
2023-08-09 13:25:22 +08:00
|
|
|
else
|
|
|
|
|
putchar('\n');
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-01-10 16:34:14 +08:00
|
|
|
char buf[32];
|
|
|
|
|
if (display->serial)
|
|
|
|
|
{
|
|
|
|
|
const uint8_t* nums = (uint8_t*) &display->serial;
|
|
|
|
|
snprintf(buf, sizeof(buf), "%2X-%2X-%2X-%2X", nums[0], nums[1], nums[2], nums[3]);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
buf[0] = '\0';
|
|
|
|
|
|
2024-03-07 10:47:28 +08:00
|
|
|
FF_PRINT_FORMAT_CHECKED(key.chars, 0, &options->moduleArgs, FF_PRINT_TYPE_NO_CUSTOM_KEY, FF_MONITOR_NUM_FORMAT_ARGS, ((FFformatarg[]) {
|
2023-08-09 13:25:22 +08:00
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &display->name},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_UINT, &display->width},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_UINT, &display->height},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_UINT, &display->physicalWidth},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_UINT, &display->physicalHeight},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_DOUBLE, &inch},
|
2023-08-09 16:18:57 +08:00
|
|
|
{FF_FORMAT_ARG_TYPE_DOUBLE, &ppi},
|
2024-01-10 16:34:14 +08:00
|
|
|
{FF_FORMAT_ARG_TYPE_UINT16, &display->manufactureYear},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_UINT16, &display->manufactureWeek},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRING, buf},
|
2024-03-07 10:47:28 +08:00
|
|
|
}));
|
2023-08-09 13:25:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ffStrbufDestroy(&display->name);
|
2023-08-15 21:40:22 +08:00
|
|
|
++index;
|
2023-08-09 13:25:22 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-09 16:31:24 +08:00
|
|
|
bool ffParseMonitorCommandOptions(FFMonitorOptions* options, const char* key, const char* value)
|
2023-08-09 13:25:22 +08:00
|
|
|
{
|
2023-08-09 16:31:24 +08:00
|
|
|
const char* subKey = ffOptionTestPrefix(key, FF_MONITOR_MODULE_NAME);
|
2023-08-09 13:25:22 +08:00
|
|
|
if (!subKey) return false;
|
|
|
|
|
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-17 10:56:54 +08:00
|
|
|
void ffParseMonitorJsonObject(FFMonitorOptions* options, yyjson_val* module)
|
2023-08-09 13:25:22 +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-08-09 13:25:22 +08:00
|
|
|
{
|
2023-08-17 16:09:41 +08:00
|
|
|
const char* key = yyjson_get_str(key_);
|
|
|
|
|
if(ffStrEqualsIgnCase(key, "type"))
|
|
|
|
|
continue;
|
2023-08-09 13:25:22 +08:00
|
|
|
|
2023-08-17 16:09:41 +08:00
|
|
|
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs))
|
|
|
|
|
continue;
|
2023-08-09 13:25:22 +08:00
|
|
|
|
2024-03-07 11:10:18 +08:00
|
|
|
ffPrintError(FF_MONITOR_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", key);
|
2023-08-09 13:25:22 +08:00
|
|
|
}
|
|
|
|
|
}
|
2023-09-06 16:33:51 +08:00
|
|
|
|
2023-10-24 14:41:29 +08:00
|
|
|
void ffGenerateMonitorJsonConfig(FFMonitorOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
|
|
|
|
|
{
|
|
|
|
|
__attribute__((__cleanup__(ffDestroyMonitorOptions))) FFMonitorOptions defaultOptions;
|
|
|
|
|
ffInitMonitorOptions(&defaultOptions);
|
|
|
|
|
|
|
|
|
|
ffJsonConfigGenerateModuleArgsConfig(doc, module, &defaultOptions.moduleArgs, &options->moduleArgs);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-23 10:14:22 +08:00
|
|
|
void ffGenerateMonitorJsonResult(FF_MAYBE_UNUSED FFMonitorOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
|
2023-09-06 16:33:51 +08:00
|
|
|
{
|
|
|
|
|
FF_LIST_AUTO_DESTROY results = ffListCreate(sizeof(FFMonitorResult));
|
|
|
|
|
|
|
|
|
|
const char* error = ffDetectMonitor(&results);
|
|
|
|
|
|
|
|
|
|
if (error)
|
|
|
|
|
{
|
|
|
|
|
yyjson_mut_obj_add_str(doc, module, "error", error);
|
|
|
|
|
}
|
|
|
|
|
else if(results.length == 0)
|
|
|
|
|
{
|
|
|
|
|
yyjson_mut_obj_add_str(doc, module, "error", "No monitors found");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result");
|
|
|
|
|
FF_LIST_FOR_EACH(FFMonitorResult, item, results)
|
|
|
|
|
{
|
|
|
|
|
yyjson_mut_val* obj = yyjson_mut_arr_add_obj(doc, arr);
|
|
|
|
|
yyjson_mut_obj_add_bool(doc, obj, "hdrCompatible", item->hdrCompatible);
|
|
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "name", &item->name);
|
2024-01-10 16:34:14 +08:00
|
|
|
|
|
|
|
|
yyjson_mut_val* resolution = yyjson_mut_obj_add_obj(doc, obj, "resolution");
|
|
|
|
|
yyjson_mut_obj_add_uint(doc, resolution, "width", item->width);
|
|
|
|
|
yyjson_mut_obj_add_uint(doc, resolution, "height", item->height);
|
|
|
|
|
|
|
|
|
|
yyjson_mut_val* physical = yyjson_mut_obj_add_obj(doc, obj, "physical");
|
|
|
|
|
yyjson_mut_obj_add_uint(doc, physical, "height", item->physicalHeight);
|
|
|
|
|
yyjson_mut_obj_add_uint(doc, physical, "width", item->physicalWidth);
|
|
|
|
|
|
|
|
|
|
if (item->manufactureYear)
|
|
|
|
|
{
|
|
|
|
|
yyjson_mut_val* manufactureDate = yyjson_mut_obj_add_obj(doc, obj, "manufactureDate");
|
|
|
|
|
yyjson_mut_obj_add_uint(doc, manufactureDate, "year", item->manufactureYear);
|
|
|
|
|
yyjson_mut_obj_add_uint(doc, manufactureDate, "week", item->manufactureWeek);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
yyjson_mut_obj_add_null(doc, obj, "manufactureDate");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (item->serial)
|
|
|
|
|
yyjson_mut_obj_add_uint(doc, obj, "serial", item->serial);
|
|
|
|
|
else
|
|
|
|
|
yyjson_mut_obj_add_null(doc, obj, "serial");
|
2023-09-06 16:33:51 +08:00
|
|
|
}
|
|
|
|
|
}
|
2023-09-10 18:39:45 +08:00
|
|
|
|
|
|
|
|
FF_LIST_FOR_EACH(FFMonitorResult, item, results)
|
|
|
|
|
{
|
|
|
|
|
ffStrbufDestroy(&item->name);
|
|
|
|
|
}
|
2023-09-06 16:33:51 +08:00
|
|
|
}
|
2023-10-07 13:40:40 +08:00
|
|
|
|
|
|
|
|
void ffPrintMonitorHelpFormat(void)
|
|
|
|
|
{
|
2024-03-07 10:47:28 +08:00
|
|
|
FF_PRINT_MODULE_FORMAT_HELP_CHECKED(FF_MONITOR_MODULE_NAME, "{2}x{3} px - {4}x{5} mm ({6} inches, {7} ppi)", FF_MONITOR_NUM_FORMAT_ARGS, ((const char* []) {
|
2023-10-07 13:40:40 +08:00
|
|
|
"Display name",
|
2024-03-07 10:47:28 +08:00
|
|
|
"Native resolution width in pixels",
|
|
|
|
|
"Native resolution height in pixels",
|
|
|
|
|
"Physical width in millimeters",
|
|
|
|
|
"Physical height in millimeters",
|
|
|
|
|
"Physical diagonal length in inches",
|
|
|
|
|
"Pixels per inch (PPI)",
|
2024-01-10 16:34:14 +08:00
|
|
|
"Year of manufacturing",
|
|
|
|
|
"Nth week of manufacturing in the year",
|
|
|
|
|
"Serial number",
|
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 ffInitMonitorOptions(FFMonitorOptions* options)
|
|
|
|
|
{
|
2023-10-23 13:39:07 +08:00
|
|
|
ffOptionInitModuleBaseInfo(
|
|
|
|
|
&options->moduleInfo,
|
|
|
|
|
FF_MONITOR_MODULE_NAME,
|
2023-11-17 14:12:17 +08:00
|
|
|
"Print connected physical monitor information",
|
2023-10-23 13:39:07 +08:00
|
|
|
ffParseMonitorCommandOptions,
|
|
|
|
|
ffParseMonitorJsonObject,
|
|
|
|
|
ffPrintMonitor,
|
|
|
|
|
ffGenerateMonitorJsonResult,
|
|
|
|
|
ffPrintMonitorHelpFormat,
|
2023-10-24 14:41:29 +08:00
|
|
|
ffGenerateMonitorJsonConfig
|
2023-10-23 13:39:07 +08:00
|
|
|
);
|
2023-10-23 13:32:18 +08:00
|
|
|
ffOptionInitModuleArg(&options->moduleArgs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ffDestroyMonitorOptions(FFMonitorOptions* options)
|
|
|
|
|
{
|
|
|
|
|
ffOptionDestroyModuleArg(&options->moduleArgs);
|
|
|
|
|
}
|