Compare commits
No commits in common. "legacy" and "master" have entirely different histories.
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
__pycache__
|
36
README.md
36
README.md
@ -1,37 +1,27 @@
|
||||
# input-over-ssh
|
||||
|
||||
Mouse and keyboard forwarding over SSH, using *xinput* and *xdotool*, with relative mouse movement. A useful adjunct to VNC in situations where relative mouse movement is important, e.g. 3D video games.
|
||||
Forwarding arbitrary input devices over SSH, such as gamepads/game controllers, mice and keyboards. Includes support for relative mouse pointer movement. See [here](https://yingtongli.me/blog/2019/12/01/input-over-ssh-2.html) for additional background and discussion.
|
||||
|
||||
## Dependencies
|
||||
|
||||
* Client (sending the mouse and keyboard of this device)
|
||||
* *xinput* – to monitor mouse and keyboard
|
||||
* *unclutter* from *unclutter-xfixes-git* – to hide cursor
|
||||
* Server (mouse and keyboard sent to this device)
|
||||
* *xdotool* – to send mouse and keyboard events
|
||||
|
||||
## Configuration
|
||||
|
||||
In *forward.py*, `MOUSEHOLD_X` and `MOUSEHOLD_Y` define the location at which the mouse cursor will be centered. Each frame, the client-side cursor will be reset to this position to measure relative movement. If `MOUSEHOLD_W` and `MOUSEHOLD_H` are non-zero, a Tkinter window of the given dimensions will be drawn around that location to prevent clicks interfering with other applications.
|
||||
|
||||
`DEVICE_MOUSE` and `DEVICE_KEYBOARD` give the names of the keyboard and mouse devices to monitor, as shown in the output of `xinput list`.
|
||||
(For the legacy implementation using xinput/xdotool, see the [legacy branch](https://yingtongli.me/git/input-over-ssh/tree/?h=legacy).)
|
||||
|
||||
## Usage
|
||||
|
||||
To run the script, pipe the output to SSH, ensuring that Python output is unbuffered using the `-u` flag:
|
||||
To use input-over-ssh, download input-over-ssh on both the client and server, and install [python-evdev](https://pypi.org/project/evdev/) as a dependency.
|
||||
|
||||
Then navigate to the root directory on the client and run:
|
||||
|
||||
```
|
||||
python -u forward.py | ssh hostname.example.com bash
|
||||
python -m input_over_ssh.client -L
|
||||
```
|
||||
|
||||
To exit, simply press Ctrl+Q.
|
||||
This will print a list of all available evdev devices. If no devices appear, ensure the current user has access to the raw */dev/input* device files (e.g. by adding the user to the *input* group).
|
||||
|
||||
## Combining with VNC
|
||||
Then pass the path of the device to be forwarded, and pipe the output to an instance of input-over-ssh running on the server. Also pass the `-u` flag to Python when running the client, to force unbuffered output. For example:
|
||||
|
||||
This setup is easily combined with VNC in view-only mode to provide a full GUI experience.
|
||||
```
|
||||
python -u -m input_over_ssh.client -p /dev/input/event1 | ssh hostname.example.com 'PYTHONPATH=/path/to/input-over-ssh python -m input_over_ssh.server'
|
||||
```
|
||||
|
||||
On the host, disable the CursorPosUpdates/CursorShapeUpdates VNC extensions to force the server to directly render the cursor. For *x11vnc*, simply pass the `-nocursorpos -nocursorshape` flags.
|
||||
For the adventurous, you ought to be able to replace ssh with netcat/socat over UDP to further reduce latency.
|
||||
|
||||
On the client, enable view-only mode to prevent conflict between the VNC client and input-over-ssh. For TigerVNC *vncviewer*, simply pass the `ViewOnly=1` flag.
|
||||
|
||||
Then run input-over-ssh as usual, and you should be able to control the remote cursor. This provides a lightweight, open-source alternative to services like Steam Remote Play.
|
||||
For a full list of command-line options, run `python -m input_over_ssh.client --help`.
|
||||
|
125
forward.py
125
forward.py
@ -1,125 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
# input-over-ssh: Mouse and keyboard forwarding over SSH
|
||||
# Copyright © 2019 Lee Yingtong Li (RunasSudo)
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
MOUSEHOLD_X = 1366//2
|
||||
MOUSEHOLD_Y = 728//2
|
||||
MOUSEHOLD_W = 0
|
||||
MOUSEHOLD_H = 0
|
||||
|
||||
DEVICE_MOUSE = 'Logitech USB Optical Mouse'
|
||||
DEVICE_KEYBOARD = 'AT Translated Set 2 keyboard'
|
||||
|
||||
import asyncio
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
import tkinter
|
||||
|
||||
if MOUSEHOLD_W > 0:
|
||||
root = tkinter.Tk()
|
||||
root.geometry('{}x{}+{}+{}'.format(MOUSEHOLD_W, MOUSEHOLD_H, MOUSEHOLD_X - MOUSEHOLD_W//2, MOUSEHOLD_Y - MOUSEHOLD_H//2))
|
||||
root.update()
|
||||
|
||||
p_unclutter = None
|
||||
async def hide_cursor():
|
||||
global p_unclutter
|
||||
p_unclutter = await asyncio.create_subprocess_exec('unclutter', '--timeout', '0', '--jitter', '9999', '--ignore-buttons', '1,2,3,4,5')
|
||||
#await p.wait()
|
||||
|
||||
async def mousemove():
|
||||
last_mousemove = time.time()
|
||||
|
||||
while True:
|
||||
p = await asyncio.create_subprocess_exec('xdotool', 'getmouselocation', stdout=subprocess.PIPE)
|
||||
stdout, _ = await p.communicate()
|
||||
result = {x[:x.index(':')]: int(x[x.index(':')+1:]) for x in stdout.decode('utf-8').strip().split(' ')}
|
||||
|
||||
dx, dy = result['x'] - MOUSEHOLD_X, result['y'] - MOUSEHOLD_Y
|
||||
if dx != 0 or dy != 0:
|
||||
print(dx, dy, file=sys.stderr)
|
||||
print('DISPLAY=:0 xdotool mousemove_relative -- {} {}'.format(dx, dy))
|
||||
|
||||
p = await asyncio.create_subprocess_exec('xdotool', 'mousemove', str(MOUSEHOLD_X), str(MOUSEHOLD_Y))
|
||||
await p.wait()
|
||||
|
||||
wait_s = (last_mousemove + 1/30) - time.time()
|
||||
last_mousemove = time.time()
|
||||
if wait_s > 0:
|
||||
await asyncio.sleep(wait_s)
|
||||
|
||||
async def mouseinp():
|
||||
# Get mouse device
|
||||
p = await asyncio.create_subprocess_exec('xinput', 'list', stdout=subprocess.PIPE)
|
||||
stdout, _ = await p.communicate()
|
||||
device = re.search(DEVICE_MOUSE + '.*?id=([0-9]+)', stdout.decode('utf-8')).group(1)
|
||||
print('Mouse device {}'.format(device), file=sys.stderr)
|
||||
|
||||
p_test = await asyncio.create_subprocess_exec('xinput', 'test', device, stdout=subprocess.PIPE)
|
||||
while True:
|
||||
line = (await p_test.stdout.readline()).decode('utf-8').strip()
|
||||
if line.startswith('button press'):
|
||||
print(line, file=sys.stderr)
|
||||
button = line.split()[-1]
|
||||
print('DISPLAY=:0 xdotool mousedown {}'.format(button))
|
||||
elif line.startswith('button release'):
|
||||
print(line, file=sys.stderr)
|
||||
button = line.split()[-1]
|
||||
print('DISPLAY=:0 xdotool mouseup {}'.format(button))
|
||||
|
||||
async def keyboardinp():
|
||||
is_ctrl = False # 37
|
||||
|
||||
# Get keyboard device
|
||||
p = await asyncio.create_subprocess_exec('xinput', 'list', stdout=subprocess.PIPE)
|
||||
stdout, _ = await p.communicate()
|
||||
device = re.search(DEVICE_KEYBOARD + '.*?id=([0-9]+)', stdout.decode('utf-8')).group(1)
|
||||
print('Keyboard device {}'.format(device), file=sys.stderr)
|
||||
|
||||
p_test = await asyncio.create_subprocess_exec('xinput', 'test', device, stdout=subprocess.PIPE)
|
||||
while True:
|
||||
line = (await p_test.stdout.readline()).decode('utf-8').strip()
|
||||
if line.startswith('key press'):
|
||||
print(line, file=sys.stderr)
|
||||
button = line.split()[-1]
|
||||
|
||||
if button == '37':
|
||||
is_ctrl = True
|
||||
if button == '24' and is_ctrl: # Ctrl+Q
|
||||
print('DISPLAY=:0 xdotool keyup 37') # Release Ctrl
|
||||
await p_unclutter.terminate()
|
||||
sys.exit(0)
|
||||
return
|
||||
|
||||
# Add leading 0 to force interpretation as key code
|
||||
print('DISPLAY=:0 xdotool keydown 0{}'.format(button))
|
||||
elif line.startswith('key release'):
|
||||
print(line, file=sys.stderr)
|
||||
button = line.split()[-1]
|
||||
|
||||
if button == '37':
|
||||
is_ctrl = False
|
||||
|
||||
print('DISPLAY=:0 xdotool keyup 0{}'.format(button))
|
||||
|
||||
async def main():
|
||||
asyncio.create_task(hide_cursor())
|
||||
asyncio.create_task(mousemove())
|
||||
asyncio.create_task(mouseinp())
|
||||
await asyncio.create_task(keyboardinp())
|
||||
|
||||
asyncio.run(main())
|
1
input_over_ssh/__init__.py
Normal file
1
input_over_ssh/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
|
84
input_over_ssh/client.py
Normal file
84
input_over_ssh/client.py
Normal file
@ -0,0 +1,84 @@
|
||||
# input-over-ssh: Forwarding arbitrary input devices over SSH
|
||||
# Copyright © 2019 Lee Yingtong Li (RunasSudo)
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
PROTOCOL_VERSION = '2'
|
||||
|
||||
import argparse
|
||||
import asyncio
|
||||
import evdev
|
||||
import json
|
||||
|
||||
async def do_forward_device(i, device):
|
||||
async for event in device.async_read_loop():
|
||||
print(json.dumps([i, event.type, event.code, event.value]))
|
||||
|
||||
async def forward_device(i, device):
|
||||
if args.exclusive:
|
||||
with device.grab_context():
|
||||
await do_forward_device(i, device)
|
||||
else:
|
||||
await do_forward_device(i, device)
|
||||
|
||||
def encode_device(device):
|
||||
cap = device.capabilities()
|
||||
del cap[0] # Filter out EV_SYN, otherwise we get OSError 22 Invalid argument
|
||||
cap_json = {}
|
||||
for k, v in cap.items():
|
||||
cap_json[k] = [x if not isinstance(x, tuple) else [x[0], x[1]._asdict()] for x in v]
|
||||
return {'name': device.name, 'capabilities': cap_json, 'vendor': device.info.vendor, 'product': device.info.product}
|
||||
|
||||
async def run_forward():
|
||||
# Find devices
|
||||
devices_by_name = {}
|
||||
if args.device_by_name:
|
||||
for path in evdev.list_devices():
|
||||
device = evdev.InputDevice(path)
|
||||
devices_by_name[device.name] = device
|
||||
|
||||
devices = []
|
||||
for path in args.device_by_path:
|
||||
devices.append(evdev.InputDevice(path))
|
||||
for name in args.device_by_name:
|
||||
devices.append(devices_by_name[name])
|
||||
|
||||
# Report version
|
||||
print(PROTOCOL_VERSION)
|
||||
|
||||
# Report devices
|
||||
print(json.dumps([encode_device(device) for device in devices]))
|
||||
|
||||
tasks = []
|
||||
for i, device in enumerate(devices):
|
||||
tasks.append(asyncio.create_task(forward_device(i, device)))
|
||||
|
||||
await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED)
|
||||
|
||||
async def list_devices():
|
||||
for path in evdev.list_devices():
|
||||
device = evdev.InputDevice(path)
|
||||
print('{} {}'.format(device.path, device.name))
|
||||
|
||||
parser = argparse.ArgumentParser(description='input-over-ssh client')
|
||||
parser.add_argument('-L', '--list-devices', dest='action', action='store_const', const=list_devices, help='List available input devices')
|
||||
parser.add_argument('-p', '--device-by-path', action='append', default=[], help='Forward device with the given path')
|
||||
parser.add_argument('-n', '--device-by-name', action='append', default=[], help='Forward device with the given name')
|
||||
parser.add_argument('-e', '--exclusive', action='store_true', help='Grab the device for exclusive input')
|
||||
args = parser.parse_args()
|
||||
|
||||
if not args.action:
|
||||
args.action = run_forward
|
||||
|
||||
asyncio.run(args.action())
|
40
input_over_ssh/server.py
Normal file
40
input_over_ssh/server.py
Normal file
@ -0,0 +1,40 @@
|
||||
#!/usr/bin/env python3
|
||||
# input-over-ssh: Forwarding arbitrary input devices over SSH
|
||||
# Copyright © 2019 Lee Yingtong Li (RunasSudo)
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
PROTOCOL_VERSION = '2'
|
||||
|
||||
import evdev
|
||||
import json
|
||||
|
||||
version = input()
|
||||
if version != PROTOCOL_VERSION:
|
||||
raise Exception('Invalid protocol version. Got {}, expected {}.'.format(version, PROTOCOL_VERSION))
|
||||
|
||||
devices_json = json.loads(input())
|
||||
devices = []
|
||||
for device_json in devices_json:
|
||||
capabilities = {}
|
||||
for k, v in device_json['capabilities'].items():
|
||||
capabilities[int(k)] = [x if not isinstance(x, list) else (x[0], evdev.AbsInfo(**x[1])) for x in v]
|
||||
devices.append(evdev.UInput(capabilities, name=device_json['name'] + ' (via input-over-ssh)', vendor=device_json['vendor'], product=device_json['product']))
|
||||
|
||||
print('Device created')
|
||||
|
||||
while True:
|
||||
event = json.loads(input())
|
||||
#print(event)
|
||||
devices[event[0]].write(event[1], event[2], event[3])
|
Reference in New Issue
Block a user