From 5e7cfe29767ba1d655bffadea31ec68ff91f7f35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Thu, 22 Feb 2024 10:47:13 +0800 Subject: [PATCH] Display: add option `--display-order` --- doc/json_schema.json | 9 ++++++++ src/data/help.json | 13 +++++++++++ src/modules/display/display.c | 42 +++++++++++++++++++++++++++++++++++ src/modules/display/option.h | 8 +++++++ 4 files changed, 72 insertions(+) diff --git a/doc/json_schema.json b/doc/json_schema.json index 080b77085..64f632868 100644 --- a/doc/json_schema.json +++ b/doc/json_schema.json @@ -1111,6 +1111,15 @@ "type": "boolean", "default": false }, + "order": { + "description": "Set the order should be used when printing", + "enum": [ + "none", + "asc", + "desc" + ], + "default": "none" + }, "key": { "$ref": "#/$defs/key" }, diff --git a/src/data/help.json b/src/data/help.json index 1bda97454..89c7a24cf 100644 --- a/src/data/help.json +++ b/src/data/help.json @@ -1047,6 +1047,19 @@ "default": false } }, + { + "long": "display-order", + "desc": "Set the order should be used when printing displays", + "arg": { + "type": "enum", + "enum": { + "none": "Use the detected order", + "asc": "Sort by display name ascendingly", + "desc": "Sort by display name descendingly" + }, + "default": "none" + } + }, { "long": "brightness-ddcci-sleep", "desc": "Set the sleep times (in ms) when sending DDC/CI requests", diff --git a/src/modules/display/display.c b/src/modules/display/display.c index 78d196885..06e1baac1 100644 --- a/src/modules/display/display.c +++ b/src/modules/display/display.c @@ -6,6 +6,16 @@ #define FF_DISPLAY_NUM_FORMAT_ARGS 8 +static int sortByNameAsc(FFDisplayResult* a, FFDisplayResult* b) +{ + return ffStrbufComp(&a->name, &b->name); +} + +static int sortByNameDesc(FFDisplayResult* a, FFDisplayResult* b) +{ + return -ffStrbufComp(&a->name, &b->name); +} + void ffPrintDisplay(FFDisplayOptions* options) { const FFDisplayServerResult* dsResult = ffConnectDisplayServer(); @@ -16,6 +26,11 @@ void ffPrintDisplay(FFDisplayOptions* options) return; } + if (options->order != FF_DISPLAY_ORDER_NONE) + { + ffListSort((FFlist*) &dsResult->displays, (void*) (options->order == FF_DISPLAY_ORDER_ASC ? sortByNameAsc : sortByNameDesc)); + } + if (options->compactType != FF_DISPLAY_COMPACT_TYPE_NONE) { ffPrintLogoAndKey(FF_DISPLAY_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT); @@ -133,6 +148,17 @@ bool ffParseDisplayCommandOptions(FFDisplayOptions* options, const char* key, co return true; } + if (ffStrEqualsIgnCase(subKey, "order")) + { + options->order = (FFDisplayOrder) ffOptionParseEnum(key, value, (FFKeyValuePair[]) { + { "asc", FF_DISPLAY_ORDER_ASC }, + { "desc", FF_DISPLAY_ORDER_DESC }, + { "none", FF_DISPLAY_ORDER_NONE }, + {}, + }); + return true; + } + return false; } @@ -171,6 +197,22 @@ void ffParseDisplayJsonObject(FFDisplayOptions* options, yyjson_val* module) continue; } + if (ffStrEqualsIgnCase(key, "order")) + { + int value; + const char* error = ffJsonConfigParseEnum(val, &value, (FFKeyValuePair[]) { + { "asc", FF_DISPLAY_ORDER_ASC }, + { "desc", FF_DISPLAY_ORDER_DESC }, + { "none", FF_DISPLAY_ORDER_NONE }, + {}, + }); + if (error) + ffPrintError(FF_DISPLAY_MODULE_NAME, 0, &options->moduleArgs, "Invalid %s value: %s", key, error); + else + options->order = (FFDisplayOrder) value; + continue; + } + ffPrintError(FF_DISPLAY_MODULE_NAME, 0, &options->moduleArgs, "Unknown JSON key %s", key); } } diff --git a/src/modules/display/option.h b/src/modules/display/option.h index 70b4c2be0..bc84ff886 100644 --- a/src/modules/display/option.h +++ b/src/modules/display/option.h @@ -11,6 +11,13 @@ typedef enum FFDisplayCompactType FF_DISPLAY_COMPACT_TYPE_SCALED_BIT = 1 << 1, } FFDisplayCompactType; +typedef enum FFDisplayOrder +{ + FF_DISPLAY_ORDER_NONE, + FF_DISPLAY_ORDER_ASC, + FF_DISPLAY_ORDER_DESC, +} FFDisplayOrder; + typedef struct FFDisplayOptions { FFModuleBaseInfo moduleInfo; @@ -18,4 +25,5 @@ typedef struct FFDisplayOptions FFDisplayCompactType compactType; bool preciseRefreshRate; + FFDisplayOrder order; } FFDisplayOptions;