Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import asyncio
- import evdev
- import evdev.ecodes as e
- import os
- import re
- import sys
- from subprocess import run, check_output
- source_devices = sys.argv[1:]
- # Listen to events from the following device.
- real_devices = [
- evdev.InputDevice(device)
- for device in source_devices
- ]
- # Then create our own device for our own events.
- host_devices = [
- evdev.UInput.from_device(device)
- for device in real_devices
- ]
- guest_devices = [
- evdev.UInput.from_device(device)
- for device in real_devices
- ]
- host_device_paths = [
- os.path.join("/dev/input/by-id", "host-%s" % os.path.basename(path))
- for path in source_devices
- ]
- guest_device_paths = [
- os.path.join("/dev/input/by-id", "guest-%s" % os.path.basename(path))
- for path in source_devices
- ]
- virtual_devices = host_devices + guest_devices
- virtual_device_paths = host_device_paths + guest_device_paths
- # Unlink all old devices so you don't have any phantom guest-* devices from launching previous VMs.
- for devname in os.listdir("/dev/input/by-id"):
- if re.match("^(host|guest)\\-", devname):
- run(["unlink", "--", os.path.join("/dev/input/by-id", devname)])
- for (device, path) in zip(virtual_devices, virtual_device_paths):
- if os.path.exists(path):
- run(["unlink", "--", path])
- run(["ln", "-s", device.device, path])
- # Now we monopolize the original devices.
- for device in real_devices:
- device.grab()
- async def replicate(source_device, host_device, guest_device):
- global hooks
- async for event in source_device.async_read_loop():
- if event.type == e.EV_SYN:
- continue
- mode = "guest" if event.code not in [e.KEY_GRAVE] else "host"
- target_device = {
- "host": host_device,
- "guest": guest_device,
- }[mode]
- target_device.write_event(event)
- target_device.syn()
- for source_device, host_device, guest_device in zip(real_devices, host_devices, guest_devices):
- asyncio.ensure_future(replicate(source_device, host_device, guest_device))
- event_loop = asyncio.get_event_loop()
- event_loop.run_forever()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement