Sometimes it may be useful to list the controls using a different output
format than the current one used by --list-ctrls, for instance a new
printing format could output a string which can be later fed to
--set-ctrl.

Add an abstraction mechanism to make it possible to add new output
formats for controls.

Signed-off-by: Antonio Ospite <a...@ao2.it>
---
 utils/v4l2-ctl/v4l2-ctl-common.cpp | 32 ++++++++++++++++++++----------
 1 file changed, 22 insertions(+), 10 deletions(-)

diff --git a/utils/v4l2-ctl/v4l2-ctl-common.cpp 
b/utils/v4l2-ctl/v4l2-ctl-common.cpp
index 5d41d720..7777b45c 100644
--- a/utils/v4l2-ctl/v4l2-ctl-common.cpp
+++ b/utils/v4l2-ctl/v4l2-ctl-common.cpp
@@ -30,6 +30,12 @@ struct ctrl_subset {
        unsigned size[V4L2_CTRL_MAX_DIMS];
 };
 
+struct print_format {
+       void (*print_class_name)(const char *);
+       void (*print_qctrl)(int, struct v4l2_query_ext_ctrl *, struct 
v4l2_ext_control *, int);
+       int show_menus;
+};
+
 typedef std::map<unsigned, std::vector<struct v4l2_ext_control> > 
class2ctrls_map;
 
 typedef std::map<std::string, struct v4l2_query_ext_ctrl> ctrl_qmap;
@@ -408,7 +414,7 @@ static void print_class_name(const char *name)
        printf("\n%s\n\n", name);
 }
 
-static int print_control(int fd, struct v4l2_query_ext_ctrl &qctrl, int 
show_menus)
+static int print_control(int fd, struct v4l2_query_ext_ctrl &qctrl, struct 
print_format *format)
 {
        struct v4l2_control ctrl;
        struct v4l2_ext_control ext_ctrl;
@@ -420,17 +426,17 @@ static int print_control(int fd, struct 
v4l2_query_ext_ctrl &qctrl, int show_men
        if (qctrl.flags & V4L2_CTRL_FLAG_DISABLED)
                return 1;
        if (qctrl.type == V4L2_CTRL_TYPE_CTRL_CLASS) {
-               print_class_name(qctrl.name);
+               format->print_class_name(qctrl.name);
                return 1;
        }
        ext_ctrl.id = qctrl.id;
        if ((qctrl.flags & V4L2_CTRL_FLAG_WRITE_ONLY) ||
            qctrl.type == V4L2_CTRL_TYPE_BUTTON) {
-               print_qctrl(fd, &qctrl, &ext_ctrl, show_menus);
+               format->print_qctrl(fd, &qctrl, &ext_ctrl, format->show_menus);
                return 1;
        }
        if (qctrl.type >= V4L2_CTRL_COMPOUND_TYPES) {
-               print_qctrl(fd, &qctrl, NULL, show_menus);
+               format->print_qctrl(fd, &qctrl, NULL, format->show_menus);
                return 1;
        }
        ctrls.which = V4L2_CTRL_ID2WHICH(qctrl.id);
@@ -460,7 +466,7 @@ static int print_control(int fd, struct v4l2_query_ext_ctrl 
&qctrl, int show_men
                }
                ext_ctrl.value = ctrl.value;
        }
-       print_qctrl(fd, &qctrl, &ext_ctrl, show_menus);
+       format->print_qctrl(fd, &qctrl, &ext_ctrl, format->show_menus);
        if (qctrl.type == V4L2_CTRL_TYPE_STRING)
                free(ext_ctrl.string);
        return 1;
@@ -512,7 +518,7 @@ static int query_ext_ctrl_ioctl(int fd, struct 
v4l2_query_ext_ctrl &qctrl)
        return rc;
 }
 
-static void list_controls(int fd, int show_menus)
+static void list_controls(int fd, struct print_format *format)
 {
        const unsigned next_fl = V4L2_CTRL_FLAG_NEXT_CTRL | 
V4L2_CTRL_FLAG_NEXT_COMPOUND;
        struct v4l2_query_ext_ctrl qctrl;
@@ -521,7 +527,7 @@ static void list_controls(int fd, int show_menus)
        memset(&qctrl, 0, sizeof(qctrl));
        qctrl.id = next_fl;
        while (query_ext_ctrl_ioctl(fd, qctrl) == 0) {
-               print_control(fd, qctrl, show_menus);
+               print_control(fd, qctrl, format);
                qctrl.id |= next_fl;
        }
        if (qctrl.id != next_fl)
@@ -529,11 +535,11 @@ static void list_controls(int fd, int show_menus)
        for (id = V4L2_CID_USER_BASE; id < V4L2_CID_LASTP1; id++) {
                qctrl.id = id;
                if (query_ext_ctrl_ioctl(fd, qctrl) == 0)
-                       print_control(fd, qctrl, show_menus);
+                       print_control(fd, qctrl, format);
        }
        for (qctrl.id = V4L2_CID_PRIVATE_BASE;
                        query_ext_ctrl_ioctl(fd, qctrl) == 0; qctrl.id++) {
-               print_control(fd, qctrl, show_menus);
+               print_control(fd, qctrl, format);
        }
 }
 
@@ -1097,6 +1103,12 @@ void common_get(cv4l_fd &_fd)
 void common_list(cv4l_fd &fd)
 {
        if (options[OptListCtrls] || options[OptListCtrlsMenus]) {
-               list_controls(fd.g_fd(), options[OptListCtrlsMenus]);
+               struct print_format classic_format = {
+                       .print_class_name = print_class_name,
+                       .print_qctrl = print_qctrl,
+                       .show_menus = options[OptListCtrlsMenus],
+               };
+
+               list_controls(fd.g_fd(), &classic_format);
        }
 }
-- 
2.20.1

Reply via email to