gui.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 qubes.config
  24. import qubes.ext
  25. import qubes.exc
  26. class GUI(qubes.ext.Extension):
  27. # pylint: disable=too-few-public-methods,unused-argument,no-self-use
  28. @staticmethod
  29. def attached_vms(vm):
  30. for domain in vm.app.domains:
  31. if getattr(domain, 'guivm', None) and domain.guivm == vm:
  32. yield domain
  33. @qubes.ext.handler('domain-pre-shutdown')
  34. def on_domain_pre_shutdown(self, vm, event, **kwargs):
  35. attached_vms = [domain for domain in self.attached_vms(vm) if
  36. domain.is_running()]
  37. if attached_vms and not kwargs.get('force', False):
  38. raise qubes.exc.QubesVMError(
  39. self, 'There are running VMs using this VM as GuiVM: '
  40. '{}'.format(', '.join(vm.name for vm in attached_vms)))
  41. @staticmethod
  42. def send_gui_mode(vm):
  43. vm.run_service('qubes.SetGuiMode',
  44. input=('SEAMLESS'
  45. if vm.features.get('gui-seamless', False)
  46. else 'FULLSCREEN'))
  47. @qubes.ext.handler('domain-init', 'domain-load')
  48. def on_domain_init_load(self, vm, event):
  49. if getattr(vm, 'guivm', None):
  50. if 'guivm-' + vm.guivm.name not in vm.tags:
  51. self.on_property_set(vm, event, name='guivm', newvalue=vm.guivm)
  52. @qubes.ext.handler('property-reset:guivm')
  53. def on_property_reset(self, subject, event, name, oldvalue=None):
  54. newvalue = getattr(subject, 'guivm', None)
  55. self.on_property_set(subject, event, name, newvalue, oldvalue)
  56. @qubes.ext.handler('property-set:guivm')
  57. def on_property_set(self, subject, event, name, newvalue, oldvalue=None):
  58. # Clean other 'guivm-XXX' tags.
  59. # gui-daemon can connect to only one domain
  60. tags_list = list(subject.tags)
  61. for tag in tags_list:
  62. if tag.startswith('guivm-'):
  63. subject.tags.remove(tag)
  64. if newvalue:
  65. guivm = 'guivm-' + newvalue.name
  66. subject.tags.add(guivm)
  67. @qubes.ext.handler('domain-qdb-create')
  68. def on_domain_qdb_create(self, vm, event):
  69. for feature in ('gui-videoram-overhead', 'gui-videoram-min'):
  70. try:
  71. vm.untrusted_qdb.write(
  72. '/qubes-{}'.format(feature),
  73. vm.features.check_with_template_and_adminvm(
  74. feature))
  75. except KeyError:
  76. pass
  77. # Add GuiVM Xen ID for gui-daemon
  78. if getattr(vm, 'guivm', None):
  79. if vm != vm.guivm:
  80. vm.untrusted_qdb.write('/keyboard-layout', vm.keyboard_layout)
  81. if vm.guivm.is_running():
  82. vm.untrusted_qdb.write('/qubes-gui-domain-xid',
  83. str(vm.guivm.xid))
  84. # Set GuiVM prefix
  85. guivm_windows_prefix = vm.features.get('guivm-windows-prefix', 'GuiVM')
  86. if vm.features.get('service.guivm-gui-agent', None):
  87. vm.untrusted_qdb.write('/guivm-windows-prefix',
  88. guivm_windows_prefix)
  89. @qubes.ext.handler('property-set:default_guivm', system=True)
  90. def on_property_set_default_guivm(self, app, event, name, newvalue,
  91. oldvalue=None):
  92. for vm in app.domains:
  93. if hasattr(vm, 'guivm') and vm.property_is_default('guivm'):
  94. vm.fire_event('property-set:guivm',
  95. name='guivm', newvalue=newvalue,
  96. oldvalue=oldvalue)
  97. @qubes.ext.handler('domain-start')
  98. def on_domain_start(self, vm, event, **kwargs):
  99. attached_vms = [domain for domain in self.attached_vms(vm) if
  100. domain.is_running()]
  101. for attached_vm in attached_vms:
  102. attached_vm.untrusted_qdb.write('/qubes-gui-domain-xid',
  103. str(vm.xid))
  104. @qubes.ext.handler('property-reset:keyboard_layout')
  105. def on_keyboard_reset(self, vm, event, name, oldvalue=None):
  106. if not vm.is_running():
  107. return
  108. kbd_layout = vm.keyboard_layout
  109. vm.untrusted_qdb.write('/keyboard-layout', kbd_layout)
  110. @qubes.ext.handler('property-set:keyboard_layout')
  111. def on_keyboard_set(self, vm, event, name, newvalue, oldvalue=None):
  112. for domain in vm.app.domains:
  113. if getattr(domain, 'guivm', None) == vm and \
  114. domain.property_is_default('keyboard_layout'):
  115. domain.fire_event('property-reset:keyboard_layout',
  116. name='keyboard_layout', oldvalue=oldvalue)
  117. if vm.is_running():
  118. vm.untrusted_qdb.write('/keyboard-layout', newvalue)