audio.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. # property-del <=> property-reset-to-default
  45. @qubes.ext.handler('property-del:audiovm')
  46. def on_property_del(self, subject, event, name, oldvalue=None):
  47. newvalue = getattr(subject, 'audiovm', None)
  48. self.on_property_set(subject, event, name, newvalue, oldvalue)
  49. @qubes.ext.handler('property-set:audiovm')
  50. def on_property_set(self, subject, event, name, newvalue, oldvalue=None):
  51. # Clean other 'audiovm-XXX' tags.
  52. # pulseaudio agent (module-vchan-sink) can connect to only one domain
  53. tags_list = list(subject.tags)
  54. for tag in tags_list:
  55. if tag.startswith('audiovm-'):
  56. subject.tags.remove(tag)
  57. if newvalue:
  58. audiovm = 'audiovm-' + newvalue.name
  59. subject.tags.add(audiovm)
  60. @qubes.ext.handler('domain-qdb-create')
  61. def on_domain_qdb_create(self, vm, event):
  62. # Add AudioVM Xen ID for gui-agent
  63. if getattr(vm, 'audiovm', None):
  64. if vm != vm.audiovm and vm.audiovm.is_running():
  65. vm.untrusted_qdb.write('/qubes-audio-domain-xid',
  66. str(vm.audiovm.xid))
  67. @qubes.ext.handler('property-set:default_audiovm', system=True)
  68. def on_property_set_default_audiovm(self, app, event, name, newvalue,
  69. oldvalue=None):
  70. for vm in app.domains:
  71. if hasattr(vm, 'audiovm') and vm.property_is_default('audiovm'):
  72. vm.fire_event('property-set:audiovm',
  73. name='audiovm', newvalue=newvalue,
  74. oldvalue=oldvalue)
  75. @qubes.ext.handler('domain-start')
  76. def on_domain_start(self, vm, event, **kwargs):
  77. attached_vms = [domain for domain in self.attached_vms(vm) if
  78. domain.is_running()]
  79. for attached_vm in attached_vms:
  80. attached_vm.untrusted_qdb.write('/qubes-audio-domain-xid',
  81. str(vm.xid))