public inbox for linux-media@vger.kernel.org
 help / color / mirror / Atom feed
From: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
To: Sakari Ailus <sakari.ailus@iki.fi>
Cc: linux-media@vger.kernel.org,
	Kieran Bingham <kieran.bingham@ideasonboard.com>
Subject: Re: [PATCH yavta 1/7] yavta: Refactor video_list_controls()
Date: Wed, 20 Feb 2019 16:07:53 +0200	[thread overview]
Message-ID: <20190220140753.GA3516@pendragon.ideasonboard.com> (raw)
In-Reply-To: <20190220132157.g222mjamfuyh5t2l@valkosipuli.retiisi.org.uk>

Hi Sakari,

On Wed, Feb 20, 2019 at 03:21:57PM +0200, Sakari Ailus wrote:
> On Wed, Feb 20, 2019 at 02:51:17PM +0200, Laurent Pinchart wrote:
> > Separate iteration over controls from printing, in order to reuse the
> > iteration to implement control reset.
> > 
> > Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> > ---
> >  yavta.c | 133 ++++++++++++++++++++++++++++++++++----------------------
> >  1 file changed, 82 insertions(+), 51 deletions(-)
> > 
> > diff --git a/yavta.c b/yavta.c
> > index 2d3b2d096f7d..98bc09810ff1 100644
> > --- a/yavta.c
> > +++ b/yavta.c
> > @@ -484,9 +484,12 @@ static int query_control(struct device *dev, unsigned int id,
> >  	query->id = id;
> >  
> >  	ret = ioctl(dev->fd, VIDIOC_QUERYCTRL, query);
> > -	if (ret < 0 && errno != EINVAL)
> > -		printf("unable to query control 0x%8.8x: %s (%d).\n",
> > -		       id, strerror(errno), errno);
> > +	if (ret < 0) {
> > +		ret = -errno;
> > +		if (ret != -EINVAL)
> > +			printf("unable to query control 0x%8.8x: %s (%d).\n",
> > +			       id, strerror(errno), errno);
> > +	}
> >  
> >  	return ret;
> >  }
> > @@ -1120,7 +1123,45 @@ static int video_enable(struct device *dev, int enable)
> >  	return 0;
> >  }
> >  
> > -static void video_query_menu(struct device *dev, struct v4l2_queryctrl *query,
> > +static int video_for_each_control(struct device *dev,
> > +				  int(*callback)(struct device *dev, const struct v4l2_queryctrl *query))
> 
> This is over 80 characters per line. How about wrapping? Also int on the
> above line is desperate for some breathing room before the opening
> parenthesis.

One option would be to turn the callback into a typedef.

I'm thinking about doing some refactoring in yavta, possibly splitting
it in multiple source files, as it's getting quite big. Control handling
is a candidate to be moved to a separate file. What do you think ?

I'm also wondering whether I should enumerate controls when opening the
device and caching them, to operate on the cache later on.

> > +{
> > +	struct v4l2_queryctrl query;
> > +	unsigned int nctrls = 0;
> > +	unsigned int id;
> > +	int ret;
> > +
> > +#ifndef V4L2_CTRL_FLAG_NEXT_CTRL
> 
> This was added back in 2012. Do you think it's still worth checking for it?
> 
> Not related to this patch though, just a general remark.

Please see patch 7/7 :-)

> > +	unsigned int i;
> > +
> > +	for (i = V4L2_CID_BASE; i <= V4L2_CID_LASTP1; ++i) {
> > +		id = i;
> > +#else
> > +	id = 0;
> > +	while (1) {
> > +		id |= V4L2_CTRL_FLAG_NEXT_CTRL;
> > +#endif
> > +
> > +		ret = query_control(dev, id, &query);
> > +		if (ret == -EINVAL)
> > +			break;
> > +		if (ret < 0)
> > +			return ret;
> > +
> > +		id = query.id;
> > +
> > +		ret = callback(dev, &query);
> > +		if (ret < 0)
> > +			return ret;
> > +
> > +		if (ret)
> > +			nctrls++;
> > +	}
> > +
> > +	return nctrls;
> > +}
> > +
> > +static void video_query_menu(struct device *dev, const struct v4l2_queryctrl *query,
> >  			     unsigned int value)
> >  {
> >  	struct v4l2_querymenu menu;
> > @@ -1142,83 +1183,68 @@ static void video_query_menu(struct device *dev, struct v4l2_queryctrl *query,
> >  	};
> >  }
> >  
> > -static int video_print_control(struct device *dev, unsigned int id, bool full)
> > +static int video_print_control(struct device *dev,
> > +			       const struct v4l2_queryctrl *query, bool full)
> >  {
> >  	struct v4l2_ext_control ctrl;
> > -	struct v4l2_queryctrl query;
> >  	char sval[24];
> >  	char *current = sval;
> >  	int ret;
> >  
> > -	ret = query_control(dev, id, &query);
> > -	if (ret < 0)
> > -		return ret;
> > +	if (query->flags & V4L2_CTRL_FLAG_DISABLED)
> > +		return 0;
> >  
> > -	if (query.flags & V4L2_CTRL_FLAG_DISABLED)
> > -		return query.id;
> > -
> > -	if (query.type == V4L2_CTRL_TYPE_CTRL_CLASS) {
> > -		printf("--- %s (class 0x%08x) ---\n", query.name, query.id);
> > -		return query.id;
> > +	if (query->type == V4L2_CTRL_TYPE_CTRL_CLASS) {
> > +		printf("--- %s (class 0x%08x) ---\n", query->name, query->id);
> > +		return 0;
> >  	}
> >  
> > -	ret = get_control(dev, &query, &ctrl);
> > +	ret = get_control(dev, query, &ctrl);
> >  	if (ret < 0)
> >  		strcpy(sval, "n/a");
> > -	else if (query.type == V4L2_CTRL_TYPE_INTEGER64)
> > +	else if (query->type == V4L2_CTRL_TYPE_INTEGER64)
> >  		sprintf(sval, "%lld", ctrl.value64);
> > -	else if (query.type == V4L2_CTRL_TYPE_STRING)
> > +	else if (query->type == V4L2_CTRL_TYPE_STRING)
> >  		current = ctrl.string;
> >  	else
> >  		sprintf(sval, "%d", ctrl.value);
> >  
> >  	if (full)
> >  		printf("control 0x%08x `%s' min %d max %d step %d default %d current %s.\n",
> > -			query.id, query.name, query.minimum, query.maximum,
> > -			query.step, query.default_value, current);
> > +			query->id, query->name, query->minimum, query->maximum,
> > +			query->step, query->default_value, current);
> >  	else
> > -		printf("control 0x%08x current %s.\n", query.id, current);
> > +		printf("control 0x%08x current %s.\n", query->id, current);
> >  
> > -	if (query.type == V4L2_CTRL_TYPE_STRING)
> > +	if (query->type == V4L2_CTRL_TYPE_STRING)
> >  		free(ctrl.string);
> >  
> >  	if (!full)
> > -		return query.id;
> > +		return 1;
> >  
> > -	if (query.type == V4L2_CTRL_TYPE_MENU ||
> > -	    query.type == V4L2_CTRL_TYPE_INTEGER_MENU)
> > -		video_query_menu(dev, &query, ctrl.value);
> > +	if (query->type == V4L2_CTRL_TYPE_MENU ||
> > +	    query->type == V4L2_CTRL_TYPE_INTEGER_MENU)
> > +		video_query_menu(dev, query, ctrl.value);
> >  
> > -	return query.id;
> > +	return 1;
> > +}
> > +
> > +static int __video_print_control(struct device *dev,
> > +				 const struct v4l2_queryctrl *query)
> > +{
> > +	return video_print_control(dev, query, true);
> >  }
> >  
> >  static void video_list_controls(struct device *dev)
> >  {
> > -	unsigned int nctrls = 0;
> > -	unsigned int id;
> >  	int ret;
> >  
> > -#ifndef V4L2_CTRL_FLAG_NEXT_CTRL
> > -	unsigned int i;
> > +	ret = video_for_each_control(dev, __video_print_control);
> > +	if (ret < 0)
> > +		return;
> >  
> > -	for (i = V4L2_CID_BASE; i <= V4L2_CID_LASTP1; ++i) {
> > -		id = i;
> > -#else
> > -	id = 0;
> > -	while (1) {
> > -		id |= V4L2_CTRL_FLAG_NEXT_CTRL;
> > -#endif
> > -
> > -		ret = video_print_control(dev, id, true);
> > -		if (ret < 0)
> > -			break;
> > -
> > -		id = ret;
> > -		nctrls++;
> > -	}
> > -
> > -	if (nctrls)
> > -		printf("%u control%s found.\n", nctrls, nctrls > 1 ? "s" : "");
> > +	if (ret)
> > +		printf("%u control%s found.\n", ret, ret > 1 ? "s" : "");
> >  	else
> >  		printf("No control found.\n");
> >  }
> > @@ -2184,8 +2210,13 @@ int main(int argc, char *argv[])
> >  	if (do_log_status)
> >  		video_log_status(&dev);
> >  
> > -	if (do_get_control)
> > -		video_print_control(&dev, ctrl_name, false);
> > +	if (do_get_control) {
> > +		struct v4l2_queryctrl query;
> > +
> > +		ret = query_control(&dev, ctrl_name, &query);
> > +		if (ret == 0)
> > +			video_print_control(&dev, &query, false);
> > +	}
> >  
> >  	if (do_set_control)
> >  		set_control(&dev, ctrl_name, ctrl_value);

-- 
Regards,

Laurent Pinchart

  reply	other threads:[~2019-02-20 14:08 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-02-20 12:51 [PATCH yavta 0/7] Compound controls and controls reset support Laurent Pinchart
2019-02-20 12:51 ` [PATCH yavta 1/7] yavta: Refactor video_list_controls() Laurent Pinchart
2019-02-20 13:21   ` Sakari Ailus
2019-02-20 14:07     ` Laurent Pinchart [this message]
2019-02-20 14:13       ` Sakari Ailus
2019-02-20 12:51 ` [PATCH yavta 2/7] Implement VIDIOC_QUERY_EXT_CTRL support Laurent Pinchart
2019-02-20 12:51 ` [PATCH yavta 3/7] Implement compound control get support Laurent Pinchart
2019-02-20 14:06   ` Sakari Ailus
2019-02-20 14:55     ` Laurent Pinchart
2019-02-20 21:16       ` Sakari Ailus
2019-02-20 12:51 ` [PATCH yavta 4/7] Implement compound control set support Laurent Pinchart
2019-02-20 21:21   ` Sakari Ailus
2019-02-20 12:51 ` [PATCH yavta 5/7] Add support to reset device controls Laurent Pinchart
2019-02-20 12:51 ` [PATCH yavta 6/7] Support setting control from values stored in a file Laurent Pinchart
2019-02-20 21:26   ` Sakari Ailus
2019-02-22 11:47     ` Laurent Pinchart
2019-02-22 12:32       ` Sakari Ailus
2019-02-20 12:51 ` [PATCH yavta 7/7] Remove unneeded conditional compilation for old V4L2 API versions Laurent Pinchart
2019-02-20 21:33 ` [PATCH yavta 0/7] Compound controls and controls reset support Sakari Ailus

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190220140753.GA3516@pendragon.ideasonboard.com \
    --to=laurent.pinchart@ideasonboard.com \
    --cc=kieran.bingham@ideasonboard.com \
    --cc=linux-media@vger.kernel.org \
    --cc=sakari.ailus@iki.fi \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox