gui.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #!/usr/bin/env python
  2. # vim: fileencoding=utf-8
  3. #
  4. # The Qubes OS Project, https://www.qubes-os.org/
  5. #
  6. # Copyright (C) 2010-2016 Joanna Rutkowska <joanna@invisiblethingslab.com>
  7. # Copyright (C) 2013-2016 Marek Marczykowski-Górecki
  8. # <marmarek@invisiblethingslab.com>
  9. # Copyright (C) 2014-2016 Wojtek Porczyk <woju@invisiblethingslab.com>
  10. #
  11. # This program is free software; you can redistribute it and/or modify
  12. # it under the terms of the GNU General Public License as published by
  13. # the Free Software Foundation; either version 2 of the License, or
  14. # (at your option) any later version.
  15. #
  16. # This program is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. # GNU General Public License for more details.
  20. #
  21. # You should have received a copy of the GNU General Public License along
  22. # with this program; if not, write to the Free Software Foundation, Inc.,
  23. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. #
  25. import os
  26. import re
  27. import subprocess
  28. import qubes.config
  29. import qubes.ext
  30. # "LVDS connected 1024x768+0+0 (normal left inverted right) 304mm x 228mm"
  31. REGEX_OUTPUT = re.compile(r'''
  32. (?x) # ignore whitespace
  33. ^ # start of string
  34. (?P<output>[A-Za-z0-9\-]*)[ ] # LVDS VGA etc
  35. (?P<connect>(dis)?connected)[ ]# dis/connected
  36. (?P<primary>(primary)?)[ ]?
  37. (( # a group
  38. (?P<width>\d+)x # either 1024x768+0+0
  39. (?P<height>\d+)[+]
  40. (?P<x>\d+)[+]
  41. (?P<y>\d+)
  42. )|[\D]) # or not a digit
  43. .* # ignore rest of line
  44. ''')
  45. def get_monitor_layout():
  46. outputs = []
  47. for line in subprocess.Popen(
  48. ['xrandr', '-q'], stdout=subprocess.PIPE).stdout:
  49. if not line.startswith("Screen") and not line.startswith(" "):
  50. output_params = REGEX_OUTPUT.match(line).groupdict()
  51. if output_params['width']:
  52. outputs.append("%s %s %s %s\n" % (
  53. output_params['width'],
  54. output_params['height'],
  55. output_params['x'],
  56. output_params['y']))
  57. return outputs
  58. class GUI(qubes.ext.Extension):
  59. @qubes.ext.handler('domain-start', 'domain-cmd-pre-run')
  60. def start_guid(self, vm, event, preparing_dvm=False, start_guid=True,
  61. extra_guid_args=None, **kwargs):
  62. '''Launch gui daemon.
  63. GUI daemon securely displays windows from domain.
  64. ''' # pylint: disable=no-self-use,unused-argument
  65. if not start_guid or preparing_dvm \
  66. or not os.path.exists('/var/run/shm.id'):
  67. return
  68. # FIXME move this method to this extension, plugged to event
  69. if vm.is_guid_running():
  70. return
  71. if not vm.features.check_with_template('gui', not vm.hvm):
  72. vm.log.debug('Not starting gui daemon, disabled by features')
  73. return
  74. if not os.getenv('DISPLAY'):
  75. vm.log.error('Not starting gui daemon, no DISPLAY set')
  76. return
  77. vm.log.info('Starting gui daemon')
  78. guid_cmd = [qubes.config.system_path['qubes_guid_path'],
  79. '-d', str(vm.xid), '-N', vm.name,
  80. '-c', vm.label.color,
  81. '-i', vm.label.icon_path,
  82. '-l', str(vm.label.index)]
  83. if extra_guid_args is not None:
  84. guid_cmd += extra_guid_args
  85. if vm.debug:
  86. guid_cmd += ['-v', '-v']
  87. # elif not verbose:
  88. else:
  89. guid_cmd += ['-q']
  90. if vm.hvm:
  91. guid_cmd += ['-Q', '-n']
  92. stubdom_guid_pidfile = \
  93. '/var/run/qubes/guid-running.{}'.format(self.get_stubdom_xid(vm))
  94. if not vm.debug and os.path.exists(stubdom_guid_pidfile):
  95. # Terminate stubdom guid once "real" gui agent connects
  96. stubdom_guid_pid = open(stubdom_guid_pidfile, 'r').read().strip()
  97. guid_cmd += ['-K', stubdom_guid_pid]
  98. try:
  99. subprocess.check_call(guid_cmd)
  100. except subprocess.CalledProcessError:
  101. raise qubes.exc.QubesVMError(vm,
  102. 'Cannot start qubes-guid for domain {!r}'.format(vm.name))
  103. vm.fire_event('monitor-layout-change')
  104. vm.wait_for_session()
  105. @staticmethod
  106. def get_stubdom_xid(vm):
  107. if vm.xid < 0:
  108. return -1
  109. if vm.app.vmm.xs is None:
  110. return -1
  111. stubdom_xid_str = vm.app.vmm.xs.read('',
  112. '/local/domain/{}/image/device-model-domid'.format(vm.xid))
  113. if stubdom_xid_str is None or not stubdom_xid_str.isdigit():
  114. return -1
  115. return int(stubdom_xid_str)
  116. @staticmethod
  117. def send_gui_mode(vm):
  118. vm.run_service('qubes.SetGuiMode',
  119. input=('SEAMLESS'
  120. if vm.features.get('gui-seamless', False)
  121. else 'FULLSCREEN'))
  122. @qubes.ext.handler('domain-spawn')
  123. def on_domain_spawn(self, vm, event, start_guid=True, **kwargs):
  124. if not start_guid:
  125. return
  126. if not vm.hvm:
  127. return
  128. if not os.getenv('DISPLAY'):
  129. vm.log.error('Not starting gui daemon, no DISPLAY set')
  130. return
  131. guid_cmd = [qubes.config.system_path['qubes_guid_path'],
  132. '-d', str(self.get_stubdom_xid(vm)),
  133. '-t', str(vm.xid),
  134. '-N', vm.name,
  135. '-c', vm.label.color,
  136. '-i', vm.label.icon_path,
  137. '-l', str(vm.label.index),
  138. ]
  139. if vm.debug:
  140. guid_cmd += ['-v', '-v']
  141. else:
  142. guid_cmd += ['-q']
  143. try:
  144. subprocess.check_call(guid_cmd)
  145. except subprocess.CalledProcesException:
  146. raise qubes.exc.QubesVMError(vm, 'Cannot start gui daemon')
  147. @qubes.ext.handler('monitor-layout-change')
  148. def on_monitor_layout_change(self, vm, event, monitor_layout=None):
  149. # pylint: disable=no-self-use
  150. if vm.features.check_with_template('no-monitor-layout', False) \
  151. or not vm.is_running():
  152. return
  153. if monitor_layout is None:
  154. monitor_layout = get_monitor_layout()
  155. if not monitor_layout:
  156. return
  157. pipe = vm.run('QUBESRPC qubes.SetMonitorLayout dom0',
  158. passio_popen=True, wait=True)
  159. pipe.stdin.write(''.join(monitor_layout))
  160. pipe.stdin.close()
  161. pipe.wait()