audio.py 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #
  2. # The Qubes OS Project, https://www.qubes-os.org/
  3. #
  4. # Copyright (C) 2019 Frédéric Pierret <frederic.pierret@qubes-os.org>
  5. #
  6. # This library is free software; you can redistribute it and/or
  7. # modify it under the terms of the GNU Lesser General Public
  8. # License as published by the Free Software Foundation; either
  9. # version 2.1 of the License, or (at your option) any later version.
  10. #
  11. # This library is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. # Lesser General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Lesser General Public
  17. # License along with this library; if not, see <https://www.gnu.org/licenses/>.
  18. #
  19. import asyncio
  20. import qubes.config
  21. import qubes.ext
  22. class AUDIO(qubes.ext.Extension):
  23. # pylint: disable=unused-argument,no-self-use
  24. @staticmethod
  25. def attached_vms(vm):
  26. for domain in vm.app.domains:
  27. if getattr(domain, 'audiovm', None) and domain.audiovm == vm:
  28. yield domain
  29. @qubes.ext.handler('domain-pre-shutdown')
  30. @asyncio.coroutine
  31. def on_domain_pre_shutdown(self, vm, event, **kwargs):
  32. attached_vms = [domain for domain in self.attached_vms(vm) if
  33. domain.is_running()]
  34. if attached_vms and not kwargs.get('force', False):
  35. raise qubes.exc.QubesVMError(
  36. self, 'There are running VMs using this VM as AudioVM: '
  37. '{}'.format(', '.join(vm.name for vm in attached_vms)))
  38. @qubes.ext.handler('domain-init', 'domain-load')
  39. def on_domain_init_load(self, vm, event):
  40. if getattr(vm, 'audiovm', None):
  41. if 'audiovm-' + vm.audiovm.name not in vm.tags:
  42. self.on_property_set(vm, event, name='audiovm',
  43. newvalue=vm.audiovm)
  44. @qubes.ext.handler('property-reset:audiovm')
  45. def on_property_reset(self, subject, event, name, oldvalue=None):
  46. newvalue = getattr(subject, 'audiovm', None)
  47. self.on_property_set(subject, event, name, newvalue, oldvalue)
  48. @qubes.ext.handler('property-set:audiovm')
  49. def on_property_set(self, subject, event, name, newvalue, oldvalue=None):
  50. # Clean other 'audiovm-XXX' tags.
  51. # pulseaudio agent (module-vchan-sink) can connect to only one domain
  52. tags_list = list(subject.tags)
  53. for tag in tags_list:
  54. if tag.startswith('audiovm-'):
  55. subject.tags.remove(tag)
  56. if newvalue:
  57. audiovm = 'audiovm-' + newvalue.name
  58. subject.tags.add(audiovm)
  59. @qubes.ext.handler('domain-qdb-create')
  60. def on_domain_qdb_create(self, vm, event):
  61. # Add AudioVM Xen ID for gui-agent
  62. if getattr(vm, 'audiovm', None):
  63. if vm != vm.audiovm and vm.audiovm.is_running():
  64. vm.untrusted_qdb.write('/qubes-audio-domain-xid',
  65. str(vm.audiovm.xid))
  66. @qubes.ext.handler('property-set:default_audiovm', system=True)
  67. def on_property_set_default_audiovm(self, app, event, name, newvalue,
  68. oldvalue=None):
  69. for vm in app.domains:
  70. if hasattr(vm, 'audiovm') and vm.property_is_default('audiovm'):
  71. vm.fire_event('property-set:audiovm',
  72. name='audiovm', newvalue=newvalue,
  73. oldvalue=oldvalue)
  74. @qubes.ext.handler('domain-start')
  75. def on_domain_start(self, vm, event, **kwargs):
  76. attached_vms = [domain for domain in self.attached_vms(vm) if
  77. domain.is_running()]
  78. for attached_vm in attached_vms:
  79. attached_vm.untrusted_qdb.write('/qubes-audio-domain-xid',
  80. str(vm.xid))