1 /*****************************************************************************
2 * controls.c : Video4Linux2 device controls for vlc
3 *****************************************************************************
4 * Copyright (C) 2002-2011 the VideoLAN team
6 * Authors: Benjamin Pracht <bigben at videolan dot org>
7 * Richard Hosking <richard at hovis dot net>
8 * Antoine Cellerier <dionoea at videolan d.t org>
9 * Dennis Lou <dlou99 at yahoo dot com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
33 #include <sys/ioctl.h>
35 typedef struct vlc_v4l2_ctrl_name
39 } vlc_v4l2_ctrl_name_t;
41 /* NOTE: must be sorted by ID */
42 static const vlc_v4l2_ctrl_name_t controls[] =
44 { "brightness", V4L2_CID_BRIGHTNESS },
45 { "contrast", V4L2_CID_CONTRAST },
46 { "saturation", V4L2_CID_SATURATION },
47 { "hue", V4L2_CID_HUE },
48 { "audio-volume", V4L2_CID_AUDIO_VOLUME },
49 { "audio-balance", V4L2_CID_AUDIO_BALANCE },
50 { "audio-bass", V4L2_CID_AUDIO_BASS },
51 { "audio-treble", V4L2_CID_AUDIO_TREBLE },
52 { "audio-mute", V4L2_CID_AUDIO_MUTE },
53 { "audio-loudness", V4L2_CID_AUDIO_LOUDNESS },
54 { "auto-white-balance", V4L2_CID_AUTO_WHITE_BALANCE },
55 { "do-white-balance", V4L2_CID_DO_WHITE_BALANCE },
56 { "red-balance", V4L2_CID_RED_BALANCE },
57 { "blue-balance", V4L2_CID_BLUE_BALANCE },
58 { "gamma", V4L2_CID_GAMMA },
59 { "exposure", V4L2_CID_EXPOSURE },
60 { "autogain", V4L2_CID_AUTOGAIN },
61 { "gain", V4L2_CID_GAIN },
62 { "hflip", V4L2_CID_HFLIP },
63 { "vflip", V4L2_CID_VFLIP },
64 /* TODO: add more standardized controls */
65 #define CTRL_CID_KNOWN(cid) \
66 ((((uint32_t)cid) - V4L2_CID_BRIGHTNESS) \
67 <= (V4L2_CID_VCENTER - V4L2_CID_BRIGHTNESS))
70 typedef struct vlc_v4l2_ctrl
74 enum v4l2_ctrl_type type;
76 int32_t default_value;
77 struct vlc_v4l2_ctrl *next;
80 static int ControlSet (const vlc_v4l2_ctrl_t *c, int_fast32_t value)
82 struct v4l2_control ctrl = {
86 if (v4l2_ioctl (c->fd, VIDIOC_S_CTRL, &ctrl) < 0)
91 static int ControlSet64 (const vlc_v4l2_ctrl_t *c, int64_t value)
93 struct v4l2_ext_control ext_ctrl = {
98 struct v4l2_ext_controls ext_ctrls = {
99 .ctrl_class = V4L2_CTRL_ID2CLASS(c->id),
102 .controls = &ext_ctrl,
105 if (v4l2_ioctl (c->fd, VIDIOC_S_EXT_CTRLS, &ext_ctrls) < 0)
110 static int ControlSetCallback (vlc_object_t *obj, const char *var,
111 vlc_value_t old, vlc_value_t cur, void *data)
113 const vlc_v4l2_ctrl_t *ctrl = data;
118 case V4L2_CTRL_TYPE_INTEGER:
119 case V4L2_CTRL_TYPE_MENU:
120 case V4L2_CTRL_TYPE_BITMASK:
121 ret = ControlSet (ctrl, cur.i_int);
123 case V4L2_CTRL_TYPE_BOOLEAN:
124 ret = ControlSet (ctrl, cur.b_bool);
126 case V4L2_CTRL_TYPE_BUTTON:
127 ret = ControlSet (ctrl, 0);
129 case V4L2_CTRL_TYPE_INTEGER64:
130 ret = ControlSet64 (ctrl, cur.i_int);
138 msg_Err (obj, "cannot set control %s: %m", var);
145 static void ControlsReset (vlc_object_t *obj, vlc_v4l2_ctrl_t *list)
151 case V4L2_CTRL_TYPE_INTEGER:
152 case V4L2_CTRL_TYPE_MENU:
153 var_SetInteger (obj, list->name, list->default_value);
155 case V4L2_CTRL_TYPE_BOOLEAN:
156 var_SetBool (obj, list->name, list->default_value);
164 static int ControlsResetCallback (vlc_object_t *obj, const char *var,
165 vlc_value_t old, vlc_value_t cur, void *data)
167 ControlsReset (obj, data);
168 (void) var; (void) old; (void) cur;
172 static void ControlsSetFromString (vlc_object_t *obj,
173 const vlc_v4l2_ctrl_t *list)
175 char *buf = var_InheritString (obj, CFG_PREFIX"set-ctrls");
183 char *end = strchr (p, '}');
187 while (p != NULL && *p)
189 const char *name, *value;
191 p += strspn (p, ", ");
193 end = strchr (p, ',');
196 p = end; /* next name/value pair */
198 end = strchr (name, '=');
201 /* TODO? support button controls that way? */
202 msg_Err (obj, "syntax error in \"%s\": missing '='", name);
208 for (const vlc_v4l2_ctrl_t *c = list; c != NULL; c = c->next)
209 if (!strcasecmp (name, c->name))
212 case V4L2_CTRL_TYPE_INTEGER:
213 case V4L2_CTRL_TYPE_BOOLEAN:
214 case V4L2_CTRL_TYPE_MENU:
216 long val = strtol (value, &end, 0);
219 msg_Err (obj, "syntax error in \"%s\": "
220 " not an integer", value);
227 case V4L2_CTRL_TYPE_INTEGER64:
229 long long val = strtoll (value, &end, 0);
232 msg_Err (obj, "syntax error in \"%s\": "
233 " not an integer", value);
236 ControlSet64 (c, val);
240 case V4L2_CTRL_TYPE_BITMASK:
242 unsigned long val = strtoul (value, &end, 0);
245 msg_Err (obj, "syntax error in \"%s\": "
246 " not an integer", value);
254 msg_Err (obj, "setting \"%s\" not supported", name);
258 msg_Err (obj, "control \"%s\" not available", name);
263 static int cidcmp (const void *a, const void *b)
265 const uint32_t *id = a;
266 const vlc_v4l2_ctrl_name_t *name = b;
268 return (int32_t)(*id - name->cid);
272 * Creates a VLC-V4L2 control structure:
273 * In particular, determines a name suitable for a VLC object variable.
274 * \param query V4L2 control query structure [IN]
275 * \return NULL on error
277 static vlc_v4l2_ctrl_t *ControlCreate (int fd,
278 const struct v4l2_queryctrl *query)
280 vlc_v4l2_ctrl_t *ctrl = malloc (sizeof (*ctrl));
281 if (unlikely(ctrl == NULL))
285 ctrl->id = query->id;
286 ctrl->type = query->type;
288 /* Search for a well-known control */
289 const vlc_v4l2_ctrl_name_t *known;
290 known = bsearch (&query->id, controls, sizeof (controls) / sizeof (*known),
291 sizeof (*known), cidcmp);
293 strcpy (ctrl->name, known->name);
295 /* Fallback to automatically-generated control name */
298 for (i = 0; query->name[i]; i++)
300 unsigned char c = query->name[i];
307 ctrl->name[i] = '\0';
310 ctrl->default_value = query->default_value;
315 #define CTRL_FLAGS_IGNORE \
316 (V4L2_CTRL_FLAG_DISABLED /* not implemented at all */ \
317 |V4L2_CTRL_FLAG_READ_ONLY /* value is constant */ \
318 |V4L2_CTRL_FLAG_VOLATILE /* value is (variable but) read-only */)
320 static vlc_v4l2_ctrl_t *ControlAddInteger (vlc_object_t *obj, int fd,
321 const struct v4l2_queryctrl *query)
323 msg_Dbg (obj, " integer %s (%08"PRIX32")", query->name, query->id);
324 if (query->flags & (CTRL_FLAGS_IGNORE | V4L2_CTRL_FLAG_WRITE_ONLY))
327 vlc_v4l2_ctrl_t *c = ControlCreate (fd, query);
328 if (unlikely(c == NULL))
331 if (var_Create (obj, c->name, VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND))
338 struct v4l2_control ctrl = { .id = query->id };
340 if (v4l2_ioctl (fd, VIDIOC_G_CTRL, &ctrl) >= 0)
342 msg_Dbg (obj, " current: %3"PRId32", default: %3"PRId32,
343 ctrl.value, query->default_value);
344 val.i_int = ctrl.value;
345 var_Change (obj, c->name, VLC_VAR_SETVALUE, &val, NULL);
347 val.i_int = query->minimum;
348 var_Change (obj, c->name, VLC_VAR_SETMIN, &val, NULL);
349 val.i_int = query->maximum;
350 var_Change (obj, c->name, VLC_VAR_SETMAX, &val, NULL);
351 if (query->step != 1)
353 val.i_int = query->step;
354 var_Change (obj, c->name, VLC_VAR_SETSTEP, &val, NULL);
356 val.i_int = query->default_value;
357 var_Change (obj, c->name, VLC_VAR_SETDEFAULT, &val, NULL);
361 static vlc_v4l2_ctrl_t *ControlAddBoolean (vlc_object_t *obj, int fd,
362 const struct v4l2_queryctrl *query)
364 msg_Dbg (obj, " boolean %s (%08"PRIX32")", query->name, query->id);
365 if (query->flags & (CTRL_FLAGS_IGNORE | V4L2_CTRL_FLAG_WRITE_ONLY))
368 vlc_v4l2_ctrl_t *c = ControlCreate (fd, query);
369 if (unlikely(c == NULL))
372 if (var_Create (obj, c->name, VLC_VAR_BOOL | VLC_VAR_ISCOMMAND))
379 struct v4l2_control ctrl = { .id = query->id };
381 if (v4l2_ioctl (fd, VIDIOC_G_CTRL, &ctrl) >= 0)
383 msg_Dbg (obj, " current: %s, default: %s",
384 ctrl.value ? " true" : "false",
385 query->default_value ? " true" : "false");
386 val.b_bool = ctrl.value;
387 var_Change (obj, c->name, VLC_VAR_SETVALUE, &val, NULL);
389 val.b_bool = query->default_value;
390 var_Change (obj, c->name, VLC_VAR_SETDEFAULT, &val, NULL);
394 static vlc_v4l2_ctrl_t *ControlAddMenu (vlc_object_t *obj, int fd,
395 const struct v4l2_queryctrl *query)
397 msg_Dbg (obj, " menu %s (%08"PRIX32")", query->name, query->id);
398 if (query->flags & (CTRL_FLAGS_IGNORE | V4L2_CTRL_FLAG_WRITE_ONLY))
401 vlc_v4l2_ctrl_t *c = ControlCreate (fd, query);
402 if (unlikely(c == NULL))
405 if (var_Create (obj, c->name, VLC_VAR_INTEGER | VLC_VAR_HASCHOICE
406 | VLC_VAR_ISCOMMAND))
413 struct v4l2_control ctrl = { .id = query->id };
415 if (v4l2_ioctl (fd, VIDIOC_G_CTRL, &ctrl) >= 0)
417 msg_Dbg (obj, " current: %"PRId32", default: %"PRId32,
418 ctrl.value, query->default_value);
419 val.i_int = ctrl.value;
420 var_Change (obj, c->name, VLC_VAR_SETVALUE, &val, NULL);
422 val.b_bool = query->default_value;
423 var_Change (obj, c->name, VLC_VAR_SETDEFAULT, &val, NULL);
424 val.i_int = query->minimum;
425 var_Change (obj, c->name, VLC_VAR_SETMIN, &val, NULL);
426 val.i_int = query->maximum;
427 var_Change (obj, c->name, VLC_VAR_SETMAX, &val, NULL);
429 /* Import menu choices */
430 for (uint_fast32_t idx = query->minimum;
431 idx <= (uint_fast32_t)query->maximum;
434 struct v4l2_querymenu menu = { .id = query->id, .index = idx };
436 if (v4l2_ioctl (fd, VIDIOC_QUERYMENU, &menu) < 0)
438 msg_Dbg (obj, " choice %"PRIu32") %s", menu.index, menu.name);
441 val.i_int = menu.index;
442 text.psz_string = (char *)menu.name;
443 var_Change (obj, c->name, VLC_VAR_ADDCHOICE, &val, &text);
448 static vlc_v4l2_ctrl_t *ControlAddButton (vlc_object_t *obj, int fd,
449 const struct v4l2_queryctrl *query)
451 msg_Dbg (obj, " button %s (%08"PRIX32")", query->name, query->id);
452 if (query->flags & CTRL_FLAGS_IGNORE)
455 vlc_v4l2_ctrl_t *c = ControlCreate (fd, query);
456 if (unlikely(c == NULL))
459 if (var_Create (obj, c->name, VLC_VAR_VOID | VLC_VAR_ISCOMMAND))
465 static vlc_v4l2_ctrl_t *ControlAddInteger64 (vlc_object_t *obj, int fd,
466 const struct v4l2_queryctrl *query)
468 msg_Dbg (obj, " 64-bits %s (%08"PRIX32")", query->name, query->id);
469 if (query->flags & (CTRL_FLAGS_IGNORE | V4L2_CTRL_FLAG_WRITE_ONLY))
472 vlc_v4l2_ctrl_t *c = ControlCreate (fd, query);
473 if (unlikely(c == NULL))
476 if (var_Create (obj, c->name, VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND))
485 static vlc_v4l2_ctrl_t *ControlAddClass (vlc_object_t *obj, int fd,
486 const struct v4l2_queryctrl *query)
488 msg_Dbg (obj, "control class %s:", query->name);
493 static vlc_v4l2_ctrl_t *ControlAddBitMask (vlc_object_t *obj, int fd,
494 const struct v4l2_queryctrl *query)
496 msg_Dbg (obj, " bit mask %s (%08"PRIX32")", query->name, query->id);
497 if (query->flags & (CTRL_FLAGS_IGNORE | V4L2_CTRL_FLAG_WRITE_ONLY))
500 vlc_v4l2_ctrl_t *c = ControlCreate (fd, query);
501 if (unlikely(c == NULL))
504 if (var_Create (obj, c->name, VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND))
511 struct v4l2_control ctrl = { .id = query->id };
513 if (v4l2_ioctl (fd, VIDIOC_G_CTRL, &ctrl) >= 0)
515 msg_Dbg (obj, " current: 0x%08"PRIX32", default: 0x%08"PRIX32,
516 ctrl.value, query->default_value);
517 val.i_int = ctrl.value;
518 var_Change (obj, c->name, VLC_VAR_SETVALUE, &val, NULL);
521 var_Change (obj, c->name, VLC_VAR_SETMIN, &val, NULL);
522 val.i_int = (uint32_t)query->maximum;
523 var_Change (obj, c->name, VLC_VAR_SETMAX, &val, NULL);
524 val.i_int = query->default_value;
525 var_Change (obj, c->name, VLC_VAR_SETDEFAULT, &val, NULL);
529 static vlc_v4l2_ctrl_t *ControlAddUnknown (vlc_object_t *obj, int fd,
530 const struct v4l2_queryctrl *query)
532 msg_Dbg (obj, " unknown %s (%08"PRIX32")", query->name, query->id);
533 msg_Warn (obj, " unknown control type %u", (unsigned)query->type);
538 typedef vlc_v4l2_ctrl_t *(*ctrl_type_cb) (vlc_object_t *, int,
539 const struct v4l2_queryctrl *);
542 * Lists all user-class v4l2 controls, sets them to the user specified
543 * value and create the relevant variables to enable run-time changes.
545 vlc_v4l2_ctrl_t *ControlsInit (vlc_object_t *obj, int fd)
547 /* A list of controls that can be modified at run-time is stored in the
548 * "controls" variable. The V4L2 controls dialog can be built from this. */
549 var_Create (obj, "controls", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE);
551 static const ctrl_type_cb handlers[] =
553 [V4L2_CTRL_TYPE_INTEGER] = ControlAddInteger,
554 [V4L2_CTRL_TYPE_BOOLEAN] = ControlAddBoolean,
555 [V4L2_CTRL_TYPE_MENU] = ControlAddMenu,
556 [V4L2_CTRL_TYPE_BUTTON] = ControlAddButton,
557 [V4L2_CTRL_TYPE_INTEGER64] = ControlAddInteger64,
558 [V4L2_CTRL_TYPE_CTRL_CLASS] = ControlAddClass,
559 [V4L2_CTRL_TYPE_BITMASK] = ControlAddBitMask,
562 vlc_v4l2_ctrl_t *list = NULL;
563 struct v4l2_queryctrl query;
565 query.id = V4L2_CTRL_FLAG_NEXT_CTRL;
566 while (v4l2_ioctl (fd, VIDIOC_QUERYCTRL, &query) >= 0)
568 ctrl_type_cb handler = NULL;
569 if (query.type < (sizeof (handlers) / sizeof (handlers[0])))
570 handler = handlers[query.type];
572 handler = ControlAddUnknown;
574 vlc_v4l2_ctrl_t *c = handler (obj, fd, &query);
577 vlc_value_t val, text;
579 var_AddCallback (obj, c->name, ControlSetCallback, c);
580 text.psz_string = (char *)query.name;
581 var_Change (obj, c->name, VLC_VAR_SETTEXT, &text, NULL);
582 val.i_int = query.id;
583 text.psz_string = (char *)c->name;
584 var_Change (obj, "controls", VLC_VAR_ADDCHOICE, &val, &text);
589 query.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
592 /* Set well-known controls from VLC configuration */
593 for (vlc_v4l2_ctrl_t *ctrl = list; ctrl != NULL; ctrl = ctrl->next)
595 if (!CTRL_CID_KNOWN (ctrl->id))
598 char varname[sizeof (CFG_PREFIX) + sizeof (ctrl->name) - 1];
599 sprintf (varname, CFG_PREFIX"%s", ctrl->name);
601 int64_t val = var_InheritInteger (obj, varname);
603 continue; /* the VLC default value: "do not modify" */
604 ControlSet (ctrl, val); /* NOTE: all known are integers or booleans */
607 /* Set any control from the VLC configuration control string */
608 ControlsSetFromString (obj, list);
610 /* Add a control to reset all controls to their default values */
612 vlc_value_t val, text;
614 var_Create (obj, "reset", VLC_VAR_VOID | VLC_VAR_ISCOMMAND);
615 val.psz_string = _("Reset defaults");
616 var_Change (obj, "reset", VLC_VAR_SETTEXT, &val, NULL);
619 text.psz_string = (char *)"reset";
620 var_Change (obj, "controls", VLC_VAR_ADDCHOICE, &val, &text);
621 var_AddCallback (obj, "reset", ControlsResetCallback, list);
623 if (var_InheritBool (obj, CFG_PREFIX"controls-reset"))
624 ControlsReset (obj, list);
629 void ControlsDeinit (vlc_object_t *obj, vlc_v4l2_ctrl_t *list)
631 var_DelCallback (obj, "reset", ControlsResetCallback, list);
632 var_Destroy (obj, "reset");
636 vlc_v4l2_ctrl_t *next = list->next;
638 var_DelCallback (obj, list->name, ControlSetCallback, list);
639 var_Destroy (obj, list->name);
644 var_Destroy (obj, "controls");