qmp.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/env python3
  2. # qmp.py <socket> <command> [args] - talks to the Windows VM's QMP socket (winvm.sh)
  3. # shot <out.png> screendump of the VM display
  4. # shots <out dir> <seconds> screendumps as fast as QEMU makes them, <out dir>/<n>.png + times.txt
  5. # keys <key> [key...] one press of each (QEMU qcode names, "ctrl-alt-delete" style combos with -)
  6. # type <text> types ASCII text
  7. # pointer <x> <y> [w h] moves an absolute pointer (usb-tablet) to pixel x, y of a w x h screen (default 1920x1080)
  8. # click [left|right] one button press and release at the pointer
  9. # raw '<json>' any QMP command, prints the reply
  10. import json, os, socket, sys, time
  11. class QMP:
  12. def __init__(self, path):
  13. self.s = socket.socket(socket.AF_UNIX)
  14. self.s.connect(path)
  15. self.f = self.s.makefile('rw')
  16. self.read()
  17. self.cmd('qmp_capabilities')
  18. def read(self):
  19. while True:
  20. msg = json.loads(self.f.readline())
  21. if 'event' not in msg:
  22. return msg
  23. def cmd(self, name, **args):
  24. self.f.write(json.dumps({'execute': name, 'arguments': args} if args else {'execute': name}) + '\n')
  25. self.f.flush()
  26. reply = self.read()
  27. if 'error' in reply:
  28. raise RuntimeError('%s: %s' % (name, reply['error']))
  29. return reply.get('return')
  30. def keys(self, combo, hold=80):
  31. self.cmd('send-key', keys=[{'type': 'qcode', 'data': k} for k in combo.split('-')], **{'hold-time': hold})
  32. SHIFTED = {'!': '1', '@': '2', '#': '3', '$': '4', '%': '5', '^': '6', '&': '7', '*': '8', '(': '9', ')': '0',
  33. '_': 'minus', '+': 'equal', '{': 'bracket_left', '}': 'bracket_right', '|': 'backslash', ':': 'semicolon',
  34. '"': 'apostrophe', '<': 'comma', '>': 'dot', '?': 'slash', '~': 'grave_accent'}
  35. PLAIN = {' ': 'spc', '-': 'minus', '=': 'equal', '[': 'bracket_left', ']': 'bracket_right', '\\': 'backslash',
  36. ';': 'semicolon', "'": 'apostrophe', ',': 'comma', '.': 'dot', '/': 'slash', '`': 'grave_accent',
  37. '\n': 'ret', '\t': 'tab'}
  38. def qcode(c):
  39. if c.isalpha():
  40. return ('shift-' if c.isupper() else '') + c.lower()
  41. if c.isdigit():
  42. return c
  43. if c in PLAIN:
  44. return PLAIN[c]
  45. return 'shift-' + SHIFTED[c]
  46. def main():
  47. q = QMP(sys.argv[1])
  48. op, args = sys.argv[2], sys.argv[3:]
  49. if op == 'shot':
  50. q.cmd('screendump', filename=os.path.abspath(args[0]), format='png')
  51. elif op == 'shots':
  52. out, secs = args[0], float(args[1])
  53. os.makedirs(out, exist_ok=True)
  54. times, t0, n = [], time.time(), 0
  55. while time.time() - t0 < secs:
  56. q.cmd('screendump', filename=os.path.abspath('%s/%05d.ppm' % (out, n)))
  57. times.append('%d %.4f\n' % (n, time.time() - t0))
  58. n += 1
  59. open(out + '/times.txt', 'w').write(''.join(times))
  60. elif op == 'keys':
  61. for k in args:
  62. q.keys(k)
  63. time.sleep(0.1)
  64. elif op == 'type':
  65. for c in ' '.join(args):
  66. q.keys(qcode(c), hold=30)
  67. time.sleep(0.02)
  68. elif op == 'pointer':
  69. x, y = float(args[0]), float(args[1])
  70. w, h = (float(args[2]), float(args[3])) if len(args) > 3 else (1920.0, 1080.0)
  71. q.cmd('input-send-event', events=[
  72. {'type': 'abs', 'data': {'axis': 'x', 'value': round(x * 32767 / (w - 1))}},
  73. {'type': 'abs', 'data': {'axis': 'y', 'value': round(y * 32767 / (h - 1))}}])
  74. elif op == 'click':
  75. button = args[0] if args else 'left'
  76. for down in (True, False):
  77. q.cmd('input-send-event', events=[{'type': 'btn', 'data': {'down': down, 'button': button}}])
  78. time.sleep(0.08)
  79. elif op == 'raw':
  80. msg = json.loads(args[0])
  81. print(json.dumps(q.cmd(msg['execute'], **msg.get('arguments', {}))))
  82. main()