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 { "power-line-frequency", V4L2_CID_POWER_LINE_FREQUENCY },
65 { "hue-auto", V4L2_CID_HUE_AUTO },
66 { "white-balance-temperature", V4L2_CID_WHITE_BALANCE_TEMPERATURE },
67 { "sharpness", V4L2_CID_SHARPNESS },
68 { "backlight-compensation", V4L2_CID_BACKLIGHT_COMPENSATION },
69 { "chroma-gain-auto", V4L2_CID_CHROMA_AGC },
70 { "color-killer", V4L2_CID_COLOR_KILLER },
71 { "color-effect", V4L2_CID_COLORFX },
72 { "rotate", V4L2_CID_ROTATE },
73 { "bg-color", V4L2_CID_BG_COLOR }, // NOTE: output only
74 { "chroma-gain", V4L2_CID_CHROMA_GAIN },
76 { "illuminators-1", V4L2_CID_ILLUMINATORS_1 }, // NOTE: don't care?
77 { "illuminators-2", V4L2_CID_ILLUMINATORS_2 },
78 #define CTRL_CID_KNOWN(cid) \
79 ((((uint32_t)cid) - V4L2_CID_BRIGHTNESS) \
80 <= (V4L2_CID_VCENTER - V4L2_CID_BRIGHTNESS))
89 int32_t default_value;
90 struct vlc_v4l2_ctrl *next;
93 static int ControlSet (const vlc_v4l2_ctrl_t *c, int_fast32_t value)
95 struct v4l2_control ctrl = {
99 if (v4l2_ioctl (c->fd, VIDIOC_S_CTRL, &ctrl) < 0)
104 static int ControlSet64 (const vlc_v4l2_ctrl_t *c, int64_t value)
106 struct v4l2_ext_control ext_ctrl = {
110 ext_ctrl.value64 = value;
111 struct v4l2_ext_controls ext_ctrls = {
112 .ctrl_class = V4L2_CTRL_ID2CLASS(c->id),
115 .controls = &ext_ctrl,
118 if (v4l2_ioctl (c->fd, VIDIOC_S_EXT_CTRLS, &ext_ctrls) < 0)
123 static int ControlSetCallback (vlc_object_t *obj, const char *var,
124 vlc_value_t old, vlc_value_t cur, void *data)
126 const vlc_v4l2_ctrl_t *ctrl = data;
131 case V4L2_CTRL_TYPE_INTEGER:
132 case V4L2_CTRL_TYPE_MENU:
133 case V4L2_CTRL_TYPE_BITMASK:
134 ret = ControlSet (ctrl, cur.i_int);
136 case V4L2_CTRL_TYPE_BOOLEAN:
137 ret = ControlSet (ctrl, cur.b_bool);
139 case V4L2_CTRL_TYPE_BUTTON:
140 ret = ControlSet (ctrl, 0);
142 case V4L2_CTRL_TYPE_INTEGER64:
143 ret = ControlSet64 (ctrl, cur.i_int);
151 msg_Err (obj, "cannot set control %s: %m", var);
158 static void ControlsReset (vlc_object_t *obj, vlc_v4l2_ctrl_t *list)
164 case V4L2_CTRL_TYPE_INTEGER:
165 case V4L2_CTRL_TYPE_MENU:
166 var_SetInteger (obj, list->name, list->default_value);
168 case V4L2_CTRL_TYPE_BOOLEAN:
169 var_SetBool (obj, list->name, list->default_value);
177 static int ControlsResetCallback (vlc_object_t *obj, const char *var,
178 vlc_value_t old, vlc_value_t cur, void *data)
180 ControlsReset (obj, data);
181 (void) var; (void) old; (void) cur;
185 static void ControlsSetFromString (vlc_object_t *obj,
186 const vlc_v4l2_ctrl_t *list)
188 char *buf = var_InheritString (obj, CFG_PREFIX"set-ctrls");
196 char *end = strchr (p, '}');
200 while (p != NULL && *p)
202 const char *name, *value;
204 p += strspn (p, ", ");
206 end = strchr (p, ',');
209 p = end; /* next name/value pair */
211 end = strchr (name, '=');
214 /* TODO? support button controls that way? */
215 msg_Err (obj, "syntax error in \"%s\": missing '='", name);
221 for (const vlc_v4l2_ctrl_t *c = list; c != NULL; c = c->next)
222 if (!strcasecmp (name, c->name))
225 case V4L2_CTRL_TYPE_INTEGER:
226 case V4L2_CTRL_TYPE_BOOLEAN:
227 case V4L2_CTRL_TYPE_MENU:
229 long val = strtol (value, &end, 0);
232 msg_Err (obj, "syntax error in \"%s\": "
233 " not an integer", value);
240 case V4L2_CTRL_TYPE_INTEGER64:
242 long long val = strtoll (value, &end, 0);
245 msg_Err (obj, "syntax error in \"%s\": "
246 " not an integer", value);
249 ControlSet64 (c, val);
253 case V4L2_CTRL_TYPE_BITMASK:
255 unsigned long val = strtoul (value, &end, 0);
258 msg_Err (obj, "syntax error in \"%s\": "
259 " not an integer", value);
267 msg_Err (obj, "setting \"%s\" not supported", name);
271 msg_Err (obj, "control \"%s\" not available", name);
276 static int cidcmp (const void *a, const void *b)
278 const uint32_t *id = a;
279 const vlc_v4l2_ctrl_name_t *name = b;
281 return (int32_t)(*id - name->cid);
285 * Creates a VLC-V4L2 control structure:
286 * In particular, determines a name suitable for a VLC object variable.
287 * \param query V4L2 control query structure [IN]
288 * \return NULL on error
290 static vlc_v4l2_ctrl_t *ControlCreate (int fd,
291 const struct v4l2_queryctrl *query)
293 vlc_v4l2_ctrl_t *ctrl = malloc (sizeof (*ctrl));
294 if (unlikely(ctrl == NULL))
298 ctrl->id = query->id;
299 ctrl->type = query->type;
301 /* Search for a well-known control */
302 const vlc_v4l2_ctrl_name_t *known;
303 known = bsearch (&query->id, controls, sizeof (controls) / sizeof (*known),
304 sizeof (*known), cidcmp);
306 strcpy (ctrl->name, known->name);
308 /* Fallback to automatically-generated control name */
311 for (i = 0; query->name[i]; i++)
313 unsigned char c = query->name[i];
320 ctrl->name[i] = '\0';
323 ctrl->default_value = query->default_value;
328 #define CTRL_FLAGS_IGNORE \
329 (V4L2_CTRL_FLAG_DISABLED /* not implemented at all */ \
330 |V4L2_CTRL_FLAG_READ_ONLY /* value is constant */ \
331 |V4L2_CTRL_FLAG_VOLATILE /* value is (variable but) read-only */)
333 static vlc_v4l2_ctrl_t *ControlAddInteger (vlc_object_t *obj, int fd,
334 const struct v4l2_queryctrl *query)
336 msg_Dbg (obj, " integer %s (%08"PRIX32")", query->name, query->id);
337 if (query->flags & (CTRL_FLAGS_IGNORE | V4L2_CTRL_FLAG_WRITE_ONLY))
340 vlc_v4l2_ctrl_t *c = ControlCreate (fd, query);
341 if (unlikely(c == NULL))
344 if (var_Create (obj, c->name, VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND))
351 struct v4l2_control ctrl = { .id = query->id };
353 if (v4l2_ioctl (fd, VIDIOC_G_CTRL, &ctrl) >= 0)
355 msg_Dbg (obj, " current: %3"PRId32", default: %3"PRId32,
356 ctrl.value, query->default_value);
357 val.i_int = ctrl.value;
358 var_Change (obj, c->name, VLC_VAR_SETVALUE, &val, NULL);
360 val.i_int = query->minimum;
361 var_Change (obj, c->name, VLC_VAR_SETMIN, &val, NULL);
362 val.i_int = query->maximum;
363 var_Change (obj, c->name, VLC_VAR_SETMAX, &val, NULL);
364 if (query->step != 1)
366 val.i_int = query->step;
367 var_Change (obj, c->name, VLC_VAR_SETSTEP, &val, NULL);
369 val.i_int = query->default_value;
370 var_Change (obj, c->name, VLC_VAR_SETDEFAULT, &val, NULL);
374 static vlc_v4l2_ctrl_t *ControlAddBoolean (vlc_object_t *obj, int fd,
375 const struct v4l2_queryctrl *query)
377 msg_Dbg (obj, " boolean %s (%08"PRIX32")", query->name, query->id);
378 if (query->flags & (CTRL_FLAGS_IGNORE | V4L2_CTRL_FLAG_WRITE_ONLY))
381 vlc_v4l2_ctrl_t *c = ControlCreate (fd, query);
382 if (unlikely(c == NULL))
385 if (var_Create (obj, c->name, VLC_VAR_BOOL | VLC_VAR_ISCOMMAND))
392 struct v4l2_control ctrl = { .id = query->id };
394 if (v4l2_ioctl (fd, VIDIOC_G_CTRL, &ctrl) >= 0)
396 msg_Dbg (obj, " current: %s, default: %s",
397 ctrl.value ? " true" : "false",
398 query->default_value ? " true" : "false");
399 val.b_bool = ctrl.value;
400 var_Change (obj, c->name, VLC_VAR_SETVALUE, &val, NULL);
402 val.b_bool = query->default_value;
403 var_Change (obj, c->name, VLC_VAR_SETDEFAULT, &val, NULL);
407 static vlc_v4l2_ctrl_t *ControlAddMenu (vlc_object_t *obj, int fd,
408 const struct v4l2_queryctrl *query)
410 msg_Dbg (obj, " menu %s (%08"PRIX32")", query->name, query->id);
411 if (query->flags & (CTRL_FLAGS_IGNORE | V4L2_CTRL_FLAG_WRITE_ONLY))
414 vlc_v4l2_ctrl_t *c = ControlCreate (fd, query);
415 if (unlikely(c == NULL))
418 if (var_Create (obj, c->name, VLC_VAR_INTEGER | VLC_VAR_HASCHOICE
419 | VLC_VAR_ISCOMMAND))
426 struct v4l2_control ctrl = { .id = query->id };
428 if (v4l2_ioctl (fd, VIDIOC_G_CTRL, &ctrl) >= 0)
430 msg_Dbg (obj, " current: %"PRId32", default: %"PRId32,
431 ctrl.value, query->default_value);
432 val.i_int = ctrl.value;
433 var_Change (obj, c->name, VLC_VAR_SETVALUE, &val, NULL);
435 val.b_bool = query->default_value;
436 var_Change (obj, c->name, VLC_VAR_SETDEFAULT, &val, NULL);
437 val.i_int = query->minimum;
438 var_Change (obj, c->name, VLC_VAR_SETMIN, &val, NULL);
439 val.i_int = query->maximum;
440 var_Change (obj, c->name, VLC_VAR_SETMAX, &val, NULL);
442 /* Import menu choices */
443 for (uint_fast32_t idx = query->minimum;
444 idx <= (uint_fast32_t)query->maximum;
447 struct v4l2_querymenu menu = { .id = query->id, .index = idx };
449 if (v4l2_ioctl (fd, VIDIOC_QUERYMENU, &menu) < 0)
451 msg_Dbg (obj, " choice %"PRIu32") %s", menu.index, menu.name);
454 val.i_int = menu.index;
455 text.psz_string = (char *)menu.name;
456 var_Change (obj, c->name, VLC_VAR_ADDCHOICE, &val, &text);
461 static vlc_v4l2_ctrl_t *ControlAddButton (vlc_object_t *obj, int fd,
462 const struct v4l2_queryctrl *query)
464 msg_Dbg (obj, " button %s (%08"PRIX32")", query->name, query->id);
465 if (query->flags & CTRL_FLAGS_IGNORE)
468 vlc_v4l2_ctrl_t *c = ControlCreate (fd, query);
469 if (unlikely(c == NULL))
472 if (var_Create (obj, c->name, VLC_VAR_VOID | VLC_VAR_ISCOMMAND))
480 static vlc_v4l2_ctrl_t *ControlAddInteger64 (vlc_object_t *obj, int fd,
481 const struct v4l2_queryctrl *query)
483 msg_Dbg (obj, " 64-bits %s (%08"PRIX32")", query->name, query->id);
484 if (query->flags & (CTRL_FLAGS_IGNORE | V4L2_CTRL_FLAG_WRITE_ONLY))
487 vlc_v4l2_ctrl_t *c = ControlCreate (fd, query);
488 if (unlikely(c == NULL))
491 if (var_Create (obj, c->name, VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND))
497 struct v4l2_ext_control ext_ctrl = { .id = c->id, .size = 0, };
498 struct v4l2_ext_controls ext_ctrls = {
499 .ctrl_class = V4L2_CTRL_ID2CLASS(c->id),
502 .controls = &ext_ctrl,
505 if (v4l2_ioctl (c->fd, VIDIOC_G_EXT_CTRLS, &ext_ctrls) >= 0)
507 vlc_value_t val = { .i_int = ext_ctrl.value64 };
509 msg_Dbg (obj, " current: %"PRId64, val.i_int);
510 var_Change (obj, c->name, VLC_VAR_SETVALUE, &val, NULL);
516 static vlc_v4l2_ctrl_t *ControlAddClass (vlc_object_t *obj, int fd,
517 const struct v4l2_queryctrl *query)
519 msg_Dbg (obj, "control class %s:", query->name);
524 static vlc_v4l2_ctrl_t *ControlAddBitMask (vlc_object_t *obj, int fd,
525 const struct v4l2_queryctrl *query)
527 msg_Dbg (obj, " bit mask %s (%08"PRIX32")", query->name, query->id);
528 if (query->flags & (CTRL_FLAGS_IGNORE | V4L2_CTRL_FLAG_WRITE_ONLY))
531 vlc_v4l2_ctrl_t *c = ControlCreate (fd, query);
532 if (unlikely(c == NULL))
535 if (var_Create (obj, c->name, VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND))
542 struct v4l2_control ctrl = { .id = query->id };
544 if (v4l2_ioctl (fd, VIDIOC_G_CTRL, &ctrl) >= 0)
546 msg_Dbg (obj, " current: 0x%08"PRIX32", default: 0x%08"PRIX32,
547 ctrl.value, query->default_value);
548 val.i_int = ctrl.value;
549 var_Change (obj, c->name, VLC_VAR_SETVALUE, &val, NULL);
552 var_Change (obj, c->name, VLC_VAR_SETMIN, &val, NULL);
553 val.i_int = (uint32_t)query->maximum;
554 var_Change (obj, c->name, VLC_VAR_SETMAX, &val, NULL);
555 val.i_int = query->default_value;
556 var_Change (obj, c->name, VLC_VAR_SETDEFAULT, &val, NULL);
560 static vlc_v4l2_ctrl_t *ControlAddUnknown (vlc_object_t *obj, int fd,
561 const struct v4l2_queryctrl *query)
563 msg_Dbg (obj, " unknown %s (%08"PRIX32")", query->name, query->id);
564 msg_Warn (obj, " unknown control type %u", (unsigned)query->type);
569 typedef vlc_v4l2_ctrl_t *(*ctrl_type_cb) (vlc_object_t *, int,
570 const struct v4l2_queryctrl *);
573 * Lists all user-class v4l2 controls, sets them to the user specified
574 * value and create the relevant variables to enable run-time changes.
576 vlc_v4l2_ctrl_t *ControlsInit (vlc_object_t *obj, int fd)
578 /* A list of controls that can be modified at run-time is stored in the
579 * "controls" variable. The V4L2 controls dialog can be built from this. */
580 var_Create (obj, "controls", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE);
582 static const ctrl_type_cb handlers[] =
584 [V4L2_CTRL_TYPE_INTEGER] = ControlAddInteger,
585 [V4L2_CTRL_TYPE_BOOLEAN] = ControlAddBoolean,
586 [V4L2_CTRL_TYPE_MENU] = ControlAddMenu,
587 [V4L2_CTRL_TYPE_BUTTON] = ControlAddButton,
588 [V4L2_CTRL_TYPE_INTEGER64] = ControlAddInteger64,
589 [V4L2_CTRL_TYPE_CTRL_CLASS] = ControlAddClass,
590 [V4L2_CTRL_TYPE_BITMASK] = ControlAddBitMask,
593 vlc_v4l2_ctrl_t *list = NULL;
594 struct v4l2_queryctrl query;
596 query.id = V4L2_CTRL_FLAG_NEXT_CTRL;
597 while (v4l2_ioctl (fd, VIDIOC_QUERYCTRL, &query) >= 0)
599 ctrl_type_cb handler = NULL;
600 if (query.type < (sizeof (handlers) / sizeof (handlers[0])))
601 handler = handlers[query.type];
603 handler = ControlAddUnknown;
605 vlc_v4l2_ctrl_t *c = handler (obj, fd, &query);
608 vlc_value_t val, text;
610 var_AddCallback (obj, c->name, ControlSetCallback, c);
611 text.psz_string = (char *)query.name;
612 var_Change (obj, c->name, VLC_VAR_SETTEXT, &text, NULL);
613 val.i_int = query.id;
614 text.psz_string = (char *)c->name;
615 var_Change (obj, "controls", VLC_VAR_ADDCHOICE, &val, &text);
620 query.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
623 /* Set well-known controls from VLC configuration */
624 for (vlc_v4l2_ctrl_t *ctrl = list; ctrl != NULL; ctrl = ctrl->next)
626 if (!CTRL_CID_KNOWN (ctrl->id))
629 char varname[sizeof (CFG_PREFIX) + sizeof (ctrl->name) - 1];
630 sprintf (varname, CFG_PREFIX"%s", ctrl->name);
632 int64_t val = var_InheritInteger (obj, varname);
634 continue; /* the VLC default value: "do not modify" */
635 ControlSet (ctrl, val); /* NOTE: all known are integers or booleans */
638 /* Set any control from the VLC configuration control string */
639 ControlsSetFromString (obj, list);
641 /* Add a control to reset all controls to their default values */
643 vlc_value_t val, text;
645 var_Create (obj, "reset", VLC_VAR_VOID | VLC_VAR_ISCOMMAND);
646 val.psz_string = _("Reset defaults");
647 var_Change (obj, "reset", VLC_VAR_SETTEXT, &val, NULL);
650 text.psz_string = (char *)"reset";
651 var_Change (obj, "controls", VLC_VAR_ADDCHOICE, &val, &text);
652 var_AddCallback (obj, "reset", ControlsResetCallback, list);
654 if (var_InheritBool (obj, CFG_PREFIX"controls-reset"))
655 ControlsReset (obj, list);
660 void ControlsDeinit (vlc_object_t *obj, vlc_v4l2_ctrl_t *list)
662 var_DelCallback (obj, "reset", ControlsResetCallback, list);
663 var_Destroy (obj, "reset");
667 vlc_v4l2_ctrl_t *next = list->next;
669 var_DelCallback (obj, list->name, ControlSetCallback, list);
670 var_Destroy (obj, list->name);
675 var_Destroy (obj, "controls");