gui.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #
  2. # The Qubes OS Project, https://www.qubes-os.org/
  3. #
  4. # Copyright (C) 2010-2016 Joanna Rutkowska <joanna@invisiblethingslab.com>
  5. # Copyright (C) 2013-2016 Marek Marczykowski-Górecki
  6. # <marmarek@invisiblethingslab.com>
  7. # Copyright (C) 2014-2018 Wojtek Porczyk <woju@invisiblethingslab.com>
  8. # Copyright (C) 2019 Frédéric Pierret <frederic.pierret@qubes-os.org>
  9. #
  10. # This library is free software; you can redistribute it and/or
  11. # modify it under the terms of the GNU Lesser General Public
  12. # License as published by the Free Software Foundation; either
  13. # version 2.1 of the License, or (at your option) any later version.
  14. #
  15. # This library is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. # Lesser General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU Lesser General Public
  21. # License along with this library; if not, see <https://www.gnu.org/licenses/>.
  22. #
  23. import asyncio
  24. import qubes.config
  25. import qubes.ext
  26. import qubes.exc
  27. class GUI(qubes.ext.Extension):
  28. # pylint: disable=too-few-public-methods,unused-argument,no-self-use
  29. @staticmethod
  30. def attached_vms(vm):
  31. for domain in vm.app.domains:
  32. if getattr(domain, 'guivm', None) and domain.guivm == vm:
  33. yield domain
  34. @qubes.ext.handler('domain-pre-shutdown')
  35. def on_domain_pre_shutdown(self, vm, event, **kwargs):
  36. attached_vms = [domain for domain in self.attached_vms(vm) if
  37. domain.is_running()]
  38. if attached_vms and not kwargs.get('force', False):
  39. raise qubes.exc.QubesVMError(
  40. self, 'There are running VMs using this VM as GuiVM: '
  41. '{}'.format(', '.join(vm.name for vm in attached_vms)))
  42. @staticmethod
  43. def send_gui_mode(vm):
  44. vm.run_service('qubes.SetGuiMode',
  45. input=('SEAMLESS'
  46. if vm.features.get('gui-seamless', False)
  47. else 'FULLSCREEN'))
  48. @qubes.ext.handler('domain-init', 'domain-load')
  49. def on_domain_init_load(self, vm, event):
  50. if getattr(vm, 'guivm', None):
  51. if 'guivm-' + vm.guivm.name not in vm.tags:
  52. self.on_property_set(vm, event, name='guivm', newvalue=vm.guivm)
  53. @qubes.ext.handler('property-reset:guivm')
  54. def on_property_reset(self, subject, event, name, oldvalue=None):
  55. newvalue = getattr(subject, 'guivm', None)
  56. self.on_property_set(subject, event, name, newvalue, oldvalue)
  57. @qubes.ext.handler('property-set:guivm')
  58. def on_property_set(self, subject, event, name, newvalue, oldvalue=None):
  59. # Clean other 'guivm-XXX' tags.
  60. # gui-daemon can connect to only one domain
  61. tags_list = list(subject.tags)
  62. for tag in tags_list:
  63. if tag.startswith('guivm-'):
  64. subject.tags.remove(tag)
  65. if newvalue:
  66. guivm = 'guivm-' + newvalue.name
  67. subject.tags.add(guivm)
  68. @qubes.ext.handler('domain-qdb-create')
  69. def on_domain_qdb_create(self, vm, event):
  70. for feature in ('gui-videoram-overhead', 'gui-videoram-min'):
  71. try:
  72. vm.untrusted_qdb.write(
  73. '/qubes-{}'.format(feature),
  74. vm.features.check_with_template_and_adminvm(
  75. feature))
  76. except KeyError:
  77. pass
  78. # Add GuiVM Xen ID for gui-daemon
  79. if getattr(vm, 'guivm', None):
  80. if vm != vm.guivm:
  81. vm.untrusted_qdb.write('/keyboard-layout', vm.keyboard_layout)
  82. if vm.guivm.is_running():
  83. vm.untrusted_qdb.write('/qubes-gui-domain-xid',
  84. str(vm.guivm.xid))
  85. # Set GuiVM prefix
  86. guivm_windows_prefix = vm.features.get('guivm-windows-prefix', 'GuiVM')
  87. if vm.features.get('service.guivm-gui-agent', None):
  88. vm.untrusted_qdb.write('/guivm-windows-prefix',
  89. guivm_windows_prefix)
  90. @qubes.ext.handler('property-set:default_guivm', system=True)
  91. def on_property_set_default_guivm(self, app, event, name, newvalue,
  92. oldvalue=None):
  93. for vm in app.domains:
  94. if hasattr(vm, 'guivm') and vm.property_is_default('guivm'):
  95. vm.fire_event('property-set:guivm',
  96. name='guivm', newvalue=newvalue,
  97. oldvalue=oldvalue)
  98. @qubes.ext.handler('domain-start')
  99. @asyncio.coroutine
  100. def on_domain_start(self, vm, event, **kwargs):
  101. attached_vms = [domain for domain in self.attached_vms(vm) if
  102. domain.is_running()]
  103. for attached_vm in attached_vms:
  104. attached_vm.untrusted_qdb.write('/qubes-gui-domain-xid',
  105. str(vm.xid))
  106. if vm.features.get('input-dom0-proxy', None):
  107. yield from asyncio.create_subprocess_exec(
  108. '/usr/bin/qubes-input-trigger --all --dom0')
  109. @qubes.ext.handler('property-reset:keyboard_layout')
  110. def on_keyboard_reset(self, vm, event, name, oldvalue=None):
  111. if not vm.is_running():
  112. return
  113. kbd_layout = vm.keyboard_layout
  114. vm.untrusted_qdb.write('/keyboard-layout', kbd_layout)
  115. @qubes.ext.handler('property-set:keyboard_layout')
  116. def on_keyboard_set(self, vm, event, name, newvalue, oldvalue=None):
  117. for domain in vm.app.domains:
  118. if getattr(domain, 'guivm', None) == vm and \
  119. domain.property_is_default('keyboard_layout'):
  120. domain.fire_event('property-reset:keyboard_layout',
  121. name='keyboard_layout', oldvalue=oldvalue)
  122. if vm.is_running():
  123. vm.untrusted_qdb.write('/keyboard-layout', newvalue)