From 139149c9d33407eb909d7d4493e7e3c9d065eb09 Mon Sep 17 00:00:00 2001 From: Antonio Ospite Date: Wed, 21 Nov 2018 23:26:57 +0100 Subject: [PATCH] Initial import --- 90-v4l2-persistent-settings.rules | 1 + Makefile | 18 +++++++++++++ README | 46 ++++++++++++++++++++++++++++++++ TODO | 6 +++++ contrib/test-v4l2-persistent-settings.sh | 14 ++++++++++ default.conf | 1 + v4l2-settings-restore.sh | 32 ++++++++++++++++++++++ v4l2-settings-save.sh | 30 +++++++++++++++++++++ 8 files changed, 148 insertions(+) create mode 100644 90-v4l2-persistent-settings.rules create mode 100644 Makefile create mode 100644 README create mode 100644 TODO create mode 100755 contrib/test-v4l2-persistent-settings.sh create mode 100644 default.conf create mode 100755 v4l2-settings-restore.sh create mode 100755 v4l2-settings-save.sh diff --git a/90-v4l2-persistent-settings.rules b/90-v4l2-persistent-settings.rules new file mode 100644 index 0000000..25aa802 --- /dev/null +++ b/90-v4l2-persistent-settings.rules @@ -0,0 +1 @@ +SUBSYSTEM=="video4linux", RUN+="/etc/v4l2-persistent-settings/v4l2-settings-restore.sh" diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d36e58e --- /dev/null +++ b/Makefile @@ -0,0 +1,18 @@ +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 diff --git a/README b/README new file mode 100644 index 0000000..306d10a --- /dev/null +++ b/README @@ -0,0 +1,46 @@ +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/ diff --git a/TODO b/TODO new file mode 100644 index 0000000..5c6842f --- /dev/null +++ b/TODO @@ -0,0 +1,6 @@ +- 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. diff --git a/contrib/test-v4l2-persistent-settings.sh b/contrib/test-v4l2-persistent-settings.sh new file mode 100755 index 0000000..49bd177 --- /dev/null +++ b/contrib/test-v4l2-persistent-settings.sh @@ -0,0 +1,14 @@ +#!/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 diff --git a/default.conf b/default.conf new file mode 100644 index 0000000..b1f7105 --- /dev/null +++ b/default.conf @@ -0,0 +1 @@ +SETTINGS_DIR="/etc/v4l2-persistent-settings" diff --git a/v4l2-settings-restore.sh b/v4l2-settings-restore.sh new file mode 100755 index 0000000..bae3450 --- /dev/null +++ b/v4l2-settings-restore.sh @@ -0,0 +1,32 @@ +#!/bin/bash +# +# v4l2-settings-restore.sh - restore v4l2 device settings +# +# Copyright (C) 2018 Antonio Ospite +# +# 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 diff --git a/v4l2-settings-save.sh b/v4l2-settings-save.sh new file mode 100755 index 0000000..0c519d4 --- /dev/null +++ b/v4l2-settings-save.sh @@ -0,0 +1,30 @@ +#!/bin/bash +# +# v4l2-settings-save.sh - save v4l2 device settings +# +# Copyright (C) 2018 Antonio Ospite +# +# 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") " 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" + -- 2.1.4