gui.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. if self.is_guid_running(vm):
  69. return
  70. if not vm.features.check_with_template('gui', not vm.hvm):
  71. vm.log.debug('Not starting gui daemon, disabled by features')
  72. return
  73. if not os.getenv('DISPLAY'):
  74. vm.log.error('Not starting gui daemon, no DISPLAY set')
  75. return
  76. vm.log.info('Starting gui daemon')
  77. guid_cmd = [qubes.config.system_path['qubes_guid_path'],
  78. '-d', str(vm.xid), '-N', vm.name,
  79. '-c', vm.label.color,
  80. '-i', vm.label.icon_path,
  81. '-l', str(vm.label.index)]
  82. if extra_guid_args is not None:
  83. guid_cmd += extra_guid_args
  84. if vm.debug:
  85. guid_cmd += ['-v', '-v']
  86. # elif not verbose:
  87. else:
  88. guid_cmd += ['-q']
  89. if vm.hvm:
  90. guid_cmd += ['-Q', '-n']
  91. stubdom_guid_pidfile = \
  92. '/var/run/qubes/guid-running.{}'.format(self.get_stubdom_xid(vm))
  93. if not vm.debug and os.path.exists(stubdom_guid_pidfile):
  94. # Terminate stubdom guid once "real" gui agent connects
  95. stubdom_guid_pid = open(stubdom_guid_pidfile, 'r').read().strip()
  96. guid_cmd += ['-K', stubdom_guid_pid]
  97. try:
  98. subprocess.check_call(guid_cmd)
  99. except subprocess.CalledProcessError:
  100. raise qubes.exc.QubesVMError(vm,
  101. 'Cannot start qubes-guid for domain {!r}'.format(vm.name))
  102. vm.fire_event('monitor-layout-change')
  103. vm.wait_for_session()
  104. @staticmethod
  105. def get_stubdom_xid(vm):
  106. if vm.xid < 0:
  107. return -1
  108. if vm.app.vmm.xs is None:
  109. return -1
  110. stubdom_xid_str = vm.app.vmm.xs.read('',
  111. '/local/domain/{}/image/device-model-domid'.format(vm.xid))
  112. if stubdom_xid_str is None or not stubdom_xid_str.isdigit():
  113. return -1
  114. return int(stubdom_xid_str)
  115. @staticmethod
  116. def send_gui_mode(vm):
  117. vm.run_service('qubes.SetGuiMode',
  118. input=('SEAMLESS'
  119. if vm.features.get('gui-seamless', False)
  120. else 'FULLSCREEN'))
  121. @qubes.ext.handler('domain-spawn')
  122. def on_domain_spawn(self, vm, event, start_guid=True, **kwargs):
  123. if not start_guid:
  124. return
  125. if not vm.hvm:
  126. return
  127. if not os.getenv('DISPLAY'):
  128. vm.log.error('Not starting gui daemon, no DISPLAY set')
  129. return
  130. guid_cmd = [qubes.config.system_path['qubes_guid_path'],
  131. '-d', str(self.get_stubdom_xid(vm)),
  132. '-t', str(vm.xid),
  133. '-N', vm.name,
  134. '-c', vm.label.color,
  135. '-i', vm.label.icon_path,
  136. '-l', str(vm.label.index),
  137. ]
  138. if vm.debug:
  139. guid_cmd += ['-v', '-v']
  140. else:
  141. guid_cmd += ['-q']
  142. try:
  143. subprocess.check_call(guid_cmd)
  144. except subprocess.CalledProcesException:
  145. raise qubes.exc.QubesVMError(vm, 'Cannot start gui daemon')
  146. @qubes.ext.handler('monitor-layout-change')
  147. def on_monitor_layout_change(self, vm, event, monitor_layout=None):
  148. # pylint: disable=no-self-use
  149. if vm.features.check_with_template('no-monitor-layout', False) \
  150. or not vm.is_running():
  151. return
  152. if monitor_layout is None:
  153. monitor_layout = get_monitor_layout()
  154. if not monitor_layout:
  155. return
  156. pipe = vm.run('QUBESRPC qubes.SetMonitorLayout dom0',
  157. passio_popen=True, wait=True)
  158. pipe.stdin.write(''.join(monitor_layout))
  159. pipe.stdin.close()
  160. pipe.wait()
  161. @staticmethod
  162. def is_guid_running(vm):
  163. '''Check whether gui daemon for this domain is available.
  164. :returns: :py:obj:`True` if guid is running, \
  165. :py:obj:`False` otherwise.
  166. :rtype: bool
  167. '''
  168. xid = vm.xid
  169. if xid < 0:
  170. return False
  171. if not os.path.exists('/var/run/qubes/guid-running.{}'.format(xid)):
  172. return False
  173. return True
  174. @qubes.ext.handler('domain-is-fully-usable')
  175. def on_domain_is_fully_usable(self, vm, event):
  176. if not self.is_guid_running(vm):
  177. yield False