Skip to content

Instantly share code, notes, and snippets.

@ArseniyShestakov
Last active February 9, 2016 01:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ArseniyShestakov/fcd91b9235f0c2b0cff8 to your computer and use it in GitHub Desktop.
Save ArseniyShestakov/fcd91b9235f0c2b0cff8 to your computer and use it in GitHub Desktop.
apt-get install expect python-xlib
QEMU command to start telenet server:
-monitor telnet:127.0.0.1:1234,server,nowait \
#!/usr/bin/expect
set command [lindex $argv 0]
spawn telnet 127.0.0.1 1234
expect "(qemu)"
send "$command"
send "\r"
expect "(qemu)"
#!/usr/bin/python
from Xlib.display import Display
from Xlib import X
from Xlib.ext import record
from Xlib.protocol import rq
import shlex
from subprocess import Popen, PIPE, call
from time import sleep
import re
disp = None
hotkey_id = 96
path_to_expect = "/home/i/lol.expect"
usb_ids = "046d:c066"
usb_name = "G9x Laser Mouse"
def get_exitcode_stdout_stderr(args):
"""
Device 0.2, Port 2, Speed 12 Mb/s, Product G9x Laser Mouse
Execute the external command and get its exitcode, stdout and stderr.
args = shlex.split(cmd)
"""
proc = Popen(args, stdout=PIPE, stderr=PIPE)
out, err = proc.communicate()
exitcode = proc.returncode
#
return exitcode, out, err
def handler(reply):
global hotkey_id, path_to_expect, usb_name, usb_ids
""" This function is called when a xlib event is fired """
data = reply.data
while len(data):
event, data = rq.EventField(None).parse_binary_value(data, disp.display, None, None)
if event.type == X.KeyPress and event.detail == hotkey_id:
_, out, _ = get_exitcode_stdout_stderr([path_to_expect, "info usb"])
print out
if usb_name in out:
print "F12 pressed - unlock mouse"
m = re.search('.+Device ([0-9\.]+), Port [0-9\.]+, Speed [0-9\.]+ Mb/s, Product ' + usb_name + '.+', out)
if m:
found = m.group(1)
get_exitcode_stdout_stderr([path_to_expect, "usb_del " + found])
print "SUCCESS - ID found!"
else:
print "FAIL - ID not found!"
else:
print "F12 pressed - lock mouse"
get_exitcode_stdout_stderr([path_to_expect, "usb_add host:" + usb_ids])
call(["setxkbmap", "us"])
# get current display
disp = Display()
root = disp.screen().root
# Monitor keypress and button press
ctx = disp.record_create_context(
0,
[record.AllClients],
[{
'core_requests': (0, 0),
'core_replies': (0, 0),
'ext_requests': (0, 0, 0, 0),
'ext_replies': (0, 0, 0, 0),
'delivered_events': (0, 0),
'device_events': (X.KeyReleaseMask, X.ButtonReleaseMask),
'errors': (0, 0),
'client_started': False,
'client_died': False,
}])
disp.record_enable_context(ctx, handler)
disp.record_free_context(ctx)
while 1:
# Infinite wait, doesn't do anything as no events are grabbed
event = root.display.next_event()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment