qubes_monitor_layout_notify.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #!/usr/bin/python2 -O
  2. # vim: fileencoding=utf-8
  3. #
  4. # The Qubes OS Project, https://www.qubes-os.org/
  5. #
  6. # Copyright (C) 2015-2016 Joanna Rutkowska <joanna@invisiblethingslab.com>
  7. # Copyright (C) 2015-2016 Wojtek Porczyk <woju@invisiblethingslab.com>
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License along
  20. # with this program; if not, write to the Free Software Foundation, Inc.,
  21. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. #
  23. '''qvm-create - Create new Qubes OS store'''
  24. # TODO allow to set properties and create domains
  25. import re
  26. import subprocess
  27. import threading
  28. import qubes.tools
  29. # "LVDS connected 1024x768+0+0 (normal left inverted right) 304mm x 228mm"
  30. REGEX_OUTPUT = re.compile(r'''
  31. (?x) # ignore whitespace
  32. ^ # start of string
  33. (?P<output>[A-Za-z0-9\-]*)[ ] # LVDS VGA etc
  34. (?P<connect>(dis)?connected)[ ]# dis/connected
  35. (?P<primary>(primary)?)[ ]?
  36. (( # a group
  37. (?P<width>\d+)x # either 1024x768+0+0
  38. (?P<height>\d+)[+]
  39. (?P<x>\d+)[+]
  40. (?P<y>\d+)
  41. )|[\D]) # or not a digit
  42. .* # ignore rest of line
  43. ''')
  44. def get_monitor_layout():
  45. outputs = []
  46. for line in subprocess.Popen(
  47. ['xrandr', '-q'], stdout=subprocess.PIPE).stdout:
  48. if not line.startswith("Screen") and not line.startswith(" "):
  49. output_params = REGEX_OUTPUT.match(line).groupdict()
  50. if output_params['width']:
  51. outputs.append("%s %s %s %s\n" % (
  52. output_params['width'],
  53. output_params['height'],
  54. output_params['x'],
  55. output_params['y']))
  56. return outputs
  57. parser = qubes.tools.QubesArgumentParser(
  58. description='Send monitor layout to one qube or to all of them',
  59. want_app=True,
  60. want_vm=True,
  61. want_vm_optional=True)
  62. def main(args=None):
  63. '''Main routine of :program:`qubes-create`.
  64. :param list args: Optional arguments to override those delivered from \
  65. command line.
  66. '''
  67. args = parser.parse_args(args)
  68. monitor_layout = get_monitor_layout()
  69. # notify only if we've got a non-empty monitor_layout or else we
  70. # break proper qube resolution set by gui-agent
  71. if not monitor_layout:
  72. args.app.log.error('cannot get monitor layout')
  73. return 1
  74. if args.vm:
  75. args.vm.fire_event('monitor-layout-change', monitor_layout)
  76. else:
  77. subprocess.check_call(['killall', '-HUP', 'qubes-guid'])
  78. threads = []
  79. for vm in args.app.domains:
  80. thread = threading.Thread(name=vm.name, target=vm.fire_event,
  81. args=('monitor-layout-change',),
  82. kwargs={'monitor_layout': monitor_layout})
  83. threads.append(thread)
  84. thread.run()
  85. for thread in threads:
  86. thread.join()
  87. return 0