--- /dev/null
+SUBSYSTEM=="video4linux", RUN+="/etc/v4l2-persistent-settings/v4l2-settings-restore.sh"
--- /dev/null
+install:
+ install -m 755 -d $(DESTDIR)/etc/v4l2-persistent-settings
+ install -m 755 v4l2-settings-save.sh $(DESTDIR)/etc/v4l2-persistent-settings/v4l2-settings-save.sh
+ install -m 755 v4l2-settings-restore.sh $(DESTDIR)/etc/v4l2-persistent-settings/v4l2-settings-restore.sh
+ install -m 644 default.conf $(DESTDIR)/etc/default/v4l2-persistent-settings
+ install -m 644 90-v4l2-persistent-settings.rules $(DESTDIR)/lib/udev/rules.d/
+ udevadm control --reload
+
+uninstall:
+ rm -f $(DESTDIR)/lib/udev/rules.d/90-v4l2-persistent-settings.rules
+ udevadm control --reload
+ rm -f $(DESTDIR)/etc/default/v4l2-persistent-settings
+ rm -f $(DESTDIR)/etc/v4l2-persistent-settings/v4l2-settings-restore.sh
+ rm -f $(DESTDIR)/etc/v4l2-persistent-settings/v4l2-settings-save.sh
+ rmdir $(DESTDIR)/etc/v4l2-persistent-settings || echo 'Try running "make purge".' 1>&2
+
+purge: uninstall
+ rm -rf $(DESTDIR)/etc/v4l2-persistent-settings
--- /dev/null
+Every time a new V4L device is initialized by the linux kernel, it start with
+the controls default settings hardcoded in the driver.
+
+There is not central mechanism to make changes to controls values more
+"persistent": applications have to save and restore the settings themselves.
+
+This is particularly annoying for settings like "Power Line Frequency"
+provided by some cameras, this is a setting which you would very rarely need
+to change once you determined the right value to use for your country to fix
+light flickering.
+
+So here are some convenience scripts to save and restore settings in an
+application-independent way.
+
+Install the scripts with:
+
+ $ sudo make install
+
+
+Change the settings with your preferred v4l2 program like qv4l2 or v4l2-ctl,
+e.g.:
+
+ $ v4l2-ctl -d /dev/video0 --set-ctrl gain_automatic=0
+
+
+Save the settings:
+
+
+ $ sudo /etc/v4l2-persistent-settings/v4l2-settings-save.sh /dev/video0
+
+
+The settings will be restored the next time the device is connected.
+
+Inspired by:
+https://superuser.com/questions/471597/linux-v4l-webcam-make-settings-stick
+
+
+NOTE:
+
+The scripts use v4l2-ctl from the v4l-utils[1] instead of v4l2ctrl from
+v4l2ucp[2] (which can save to a file mor easily) because the former is
+actively maintained and also because the latter may bring in Qt4 as
+a dependency which can be undesirable on minimal systems.
+
+[1] https://git.linuxtv.org/v4l-utils.git
+[2] https://sourceforge.net/projects/v4l2ucp/
--- /dev/null
+- Teach v4l2-ctl how to save and load settings from a file, and remove the
+ shell mumbo-jumbo used in v4l2-settings-save.sh and
+ v4l2-settings-restore.sh.
+
+- Maybe use systemd unit files instead of shell scripts, or follow what
+ alsactl does.
--- /dev/null
+#!/bin/sh
+
+set -x
+set -e
+
+DEBUG="true" \
+ ID_V4L_PRODUCT="dummy" \
+ ./v4l2-settings-save.sh /dev/video0
+
+DEBUG="true" \
+ ACTION="add" \
+ DEVNAME="/dev/video0" \
+ ID_V4L_PRODUCT="dummy" \
+ ./v4l2-settings-restore.sh
--- /dev/null
+SETTINGS_DIR="/etc/v4l2-persistent-settings"
--- /dev/null
+#!/bin/bash
+#
+# v4l2-settings-restore.sh - restore v4l2 device settings
+#
+# Copyright (C) 2018 Antonio Ospite <ao2@ao2.it>
+#
+# This program is free software. It comes without any warranty, to
+# the extent permitted by applicable law. You can redistribute it
+# and/or modify it under the terms of the Do What The Fuck You Want
+# To Public License, Version 2, as published by Sam Hocevar. See
+# http://sam.zoy.org/wtfpl/COPYING for more details.
+#
+# NOTE: this script is meant to be called by an udev rule.
+
+[ "$DEBUG" = "true" ] && set -x
+set -e
+
+source /etc/default/v4l2-persistent-settings || :
+
+: "${SETTINGS_DIR:=/tmp}"
+
+SETTINGS_FILE="${SETTINGS_DIR}/${ID_V4L_PRODUCT}.conf"
+
+if [ "$ACTION" = "add" ] && [ -f "$SETTINGS_FILE" ];
+then
+ while read -r setting;
+ do
+ CTRL=$(echo -n "$setting" | cut -d ' ' -f 1)
+ VAL=$(echo -n "$setting" | cut -d ' ' -f 2)
+ v4l2-ctl -d "$DEVNAME" --set-ctrl "${CTRL}=${VAL}"
+ done < "$SETTINGS_FILE"
+fi
--- /dev/null
+#!/bin/bash
+#
+# v4l2-settings-save.sh - save v4l2 device settings
+#
+# Copyright (C) 2018 Antonio Ospite <ao2@ao2.it>
+#
+# This program is free software. It comes without any warranty, to
+# the extent permitted by applicable law. You can redistribute it
+# and/or modify it under the terms of the Do What The Fuck You Want
+# To Public License, Version 2, as published by Sam Hocevar. See
+# http://sam.zoy.org/wtfpl/COPYING for more details.
+
+[ "$DEBUG" = "true" ] && set -x
+set -e
+
+[ -e "$1" ] || { echo "usage: $(basename "$0") <videodev>" 1>&2; exit 1; }
+
+DEVNAME="$1"
+
+source /etc/default/v4l2-persistent-settings || :
+
+: "${SETTINGS_DIR:=/tmp}"
+
+: "${ID_V4L_PRODUCT:=$(v4l2-ctl -d "$DEVNAME" --info | grep "Card type" | cut -d ':' -f 2 | sed 's/^[[:space:]]//' | tr -d '\n')}"
+
+SETTINGS_FILE="${SETTINGS_DIR}/${ID_V4L_PRODUCT}.conf"
+
+# Save the latest settings.
+v4l2-ctl -d "$DEVNAME" -l | sed -n '/^[[:space:]]/s/^[[:space:]]*\([^[:space:]]*\).*value=\([^[:space:]]*\).*$/\1 \2/p' > "$SETTINGS_FILE"
+