#!/bin/bash # This script attaches webcam and audio to a specific AppVm. # It is specific for QubesOS. # Copyright (C) 2017 Fabrizio Romano Genovese # This copyrighted material is made available to anyone wishing to use, # modify, copy, or redistribute it subject to the terms and conditions of # the GNU General Public License v.2, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY expressed or implied, including the implied warranties of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General # Public License for more details. # You should have received a copy of the # GNU General Public License along with this program; if not, # see # Put this file in /etc/acpi/actions. Do not forget to give # sudo chmod +x camera.sh # to make it executable. # ALL THE COMMANDS IN THE SCRIPT HAVE TO BE GIVEN VIA sudo SPECIFYING DISPLAY # BECAUSE OF QUBESOS WEIRDNESS. # IN OTHER SYSTEMS EVERYTHING IS MAYBE EASIER. camera="sys-usb:2-1.6" user="Nigredo" AppVM="personal" # Checks if a VM exists checker() { vmstate=`sudo -u Nigredo -H sh -c "DISPLAY=:0 qvm-check $1 | grep \"not\""` if [ -z "$vmstate" ]; then echo "VM $1 exists." else echo "The VM $1 does not exist. aborting" exit 2 fi } # Attach ($2=true) or detach ($1=false) microphone to AppVM $1 # Thanks to Rusty Bird for the correct dbus command. microphone() { checker $1 sudo -u Nigredo -H sh -c "DISPLAY=:0 dbus-send --type=method_call --dest=org.QubesOS.Audio.\"$1\" \ /org/qubesos/audio org.freedesktop.DBus.Properties.Set \ string:org.QubesOS.Audio string:RecAllowed variant:boolean:\"$2\"" } # Check if the script has been used before using a tmp file. If not, creates it # And initializes notification counter. If yes, parses the content of the tmp file # Then attaches/detaches camera using qvm-usb and microphone using microphone subroutine. # Then displays the relevant notifications and updates the variables to be written in the tmp file. if [ ! -f "/tmp/camera_notification_id.dat" ]; then notification_id="0" sudo -u Nigredo -H sh -c "qvm-usb -a $AppVM $camera" microphone $AppVM true notification_id=$(sudo -u $user -H sh -c "DISPLAY:=0 notify-send-improved \"Camera Control\" --print-id -t 1000 \"Camera and microphone connected to $AppVM\" | tee /dev/tty") camera_id="$notification_id.ON" else camera_id=`cat /tmp/camera_notification_id.dat` notification_id=`echo $camera_id | cut -f1 -d"."` camera_status=`echo $camera_id | cut -f2 -d"."` if [ $camera_status = "ON" ]; then sudo -u Nigredo -H sh -c "qvm-usb -d $camera" microphone $AppVM false notification_id=$(sudo -u $user -H sh -c "DISPLAY=:0 notify-send-improved \"Camera Control\" --print-id -t 1000 \"Camera and microphone disconnected from $AppVM\" | tee /dev/tty") camera_id="$notification_id.OFF" else sudo -u Nigredo -H sh -c "qvm-usb -a $AppVM $camera" microphone $AppVM true notification_id=$(sudo -u $user -H sh -c "DISPLAY=:0 notify-send-improved \"Camera Control\" --print-id -t 1000 \"Camera and microphone connected to $AppVM\" | tee /dev/tty") camera_id="$notification_id.ON" fi fi # Update the tmp file echo "${camera_id}" > /tmp/camera_notification_id.dat