camera.sh 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/bin/bash
  2. # This script attaches webcam and audio to a specific AppVm.
  3. # It is specific for QubesOS.
  4. # Copyright (C) 2017 Fabrizio Romano Genovese <egonigredo@gmail.com>
  5. # This copyrighted material is made available to anyone wishing to use,
  6. # modify, copy, or redistribute it subject to the terms and conditions of
  7. # the GNU General Public License v.2, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful, but WITHOUT
  10. # ANY WARRANTY expressed or implied, including the implied warranties of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  12. # Public License for more details.
  13. # You should have received a copy of the
  14. # GNU General Public License along with this program; if not,
  15. # see <http://www.gnu.org/licenses/>
  16. # Put this file in /etc/acpi/actions. Do not forget to give
  17. # sudo chmod +x camera.sh
  18. # to make it executable.
  19. # ALL THE COMMANDS IN THE SCRIPT HAVE TO BE GIVEN VIA sudo SPECIFYING DISPLAY
  20. # BECAUSE OF QUBESOS WEIRDNESS.
  21. # IN OTHER SYSTEMS EVERYTHING IS MAYBE EASIER.
  22. camera="sys-usb:2-1.6"
  23. user="Nigredo"
  24. AppVM="personal"
  25. # Checks if a VM exists
  26. checker()
  27. {
  28. vmstate=`sudo -u Nigredo -H sh -c "DISPLAY=:0 qvm-check $1 | grep \"not\""`
  29. if [ -z "$vmstate" ]; then
  30. echo "VM $1 exists."
  31. else
  32. echo "The VM $1 does not exist. aborting"
  33. exit 2
  34. fi
  35. }
  36. # Attach ($2=true) or detach ($1=false) microphone to AppVM $1
  37. # Thanks to Rusty Bird for the correct dbus command.
  38. microphone()
  39. {
  40. checker $1
  41. sudo -u Nigredo -H sh -c "DISPLAY=:0 dbus-send --type=method_call --dest=org.QubesOS.Audio.\"$1\" \
  42. /org/qubesos/audio org.freedesktop.DBus.Properties.Set \
  43. string:org.QubesOS.Audio string:RecAllowed variant:boolean:\"$2\""
  44. }
  45. # Check if the script has been used before using a tmp file. If not, creates it
  46. # And initializes notification counter. If yes, parses the content of the tmp file
  47. # Then attaches/detaches camera using qvm-usb and microphone using microphone subroutine.
  48. # Then displays the relevant notifications and updates the variables to be written in the tmp file.
  49. if [ ! -f "/tmp/camera_notification_id.dat" ]; then
  50. notification_id="0"
  51. sudo -u Nigredo -H sh -c "qvm-usb -a $AppVM $camera"
  52. microphone $AppVM true
  53. 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")
  54. camera_id="$notification_id.ON"
  55. else
  56. camera_id=`cat /tmp/camera_notification_id.dat`
  57. notification_id=`echo $camera_id | cut -f1 -d"."`
  58. camera_status=`echo $camera_id | cut -f2 -d"."`
  59. if [ $camera_status = "ON" ]; then
  60. sudo -u Nigredo -H sh -c "qvm-usb -d $camera"
  61. microphone $AppVM false
  62. 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")
  63. camera_id="$notification_id.OFF"
  64. else
  65. sudo -u Nigredo -H sh -c "qvm-usb -a $AppVM $camera"
  66. microphone $AppVM true
  67. 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")
  68. camera_id="$notification_id.ON"
  69. fi
  70. fi
  71. # Update the tmp file
  72. echo "${camera_id}" > /tmp/camera_notification_id.dat