added guest-host switch on l-ctrl+r-ctrl

This commit is contained in:
Surferlul 2022-07-21 23:45:30 +02:00
parent 550812c9f7
commit 6aa3a32fc4
3 changed files with 98 additions and 16 deletions

View File

@ -20,6 +20,7 @@ def get_streams(cfg: NetConfig) -> Generator[socket, None, None]:
yield conn yield conn
else: else:
s.connect((cfg.ip_address, cfg.port)) s.connect((cfg.ip_address, cfg.port))
print(f"Connection established to {cfg.ip_address}")
yield s yield s

View File

@ -22,15 +22,23 @@ def receive_event(s: socket) -> tuple[int, tuple[int, int, int]]:
def handle_client(s: socket): def handle_client(s: socket):
devices = {} d = {}
for fd, device in receive_devices(s): for fd, device in receive_devices(s):
devices[fd] = device d[fd] = device
while True: while True:
fd, event = receive_event(s) fd, event = receive_event(s)
if fd == 4294967295: if fd == 4294967295:
break s.close()
for fd in d:
d[fd].close()
print("Exiting")
exit()
elif fd == 4278124286:
print("Control was given")
elif fd == 4261281277:
print("Control was taken")
elif event[0] == 0: elif event[0] == 0:
devices[fd].syn() d[fd].syn()
else: else:
devices[fd].write(*event) d[fd].write(*event)

View File

@ -2,6 +2,7 @@ from socket import socket
from evdev import InputDevice, InputEvent from evdev import InputDevice, InputEvent
from select import select from select import select
import json import json
from time import time
from ..config import DeviceInfo from ..config import DeviceInfo
@ -20,19 +21,91 @@ def send_input_event(s: socket, fd: int, input_event: InputEvent):
s.sendall(input_event.value.to_bytes(8, byteorder='big', signed=True)) s.sendall(input_event.value.to_bytes(8, byteorder='big', signed=True))
def exit_nice(s: socket, d: dict[int: InputDevice], guest_input: bool):
s.sendall(b'\xff' * 20)
if guest_input:
for fd in d:
d[fd].ungrab()
s.close()
print("Exiting")
exit()
def double_control(s: socket,
d: dict[int: InputDevice],
active_keys: list[int],
guest_input: bool,
ctrl2: bool,
device_fd: int,
event_value: int) -> bool:
if 14 in active_keys:
exit_nice(s, d, guest_input)
elif not ctrl2:
guest_input = not guest_input
event = ({29, 157} ^ {event_value}).pop()
send_input_event(s, device_fd, InputEvent(0, 0, 4, 4, event))
if guest_input:
send_input_event(s, device_fd, InputEvent(
0, 0, 1,
event if event == 29 else 97,
1,
))
send_input_event(s, device_fd, InputEvent(0, 0, 0, 0, 0))
print("Giving up control")
s.sendall(b'\xfe' * 20)
for fd in d:
d[fd].grab()
else:
send_input_event(s, device_fd, InputEvent(
0, 0, 1,
event if event == 29 else 97,
0,
))
send_input_event(s, device_fd, InputEvent(0, 0, 0, 0, 0))
print("Taking control")
s.sendall(b'\xfd' * 20)
for fd in d:
d[fd].ungrab()
return guest_input
def handle_client(s: socket, devices_info: list[DeviceInfo]): def handle_client(s: socket, devices_info: list[DeviceInfo]):
devices = []
ctrl2 = False
guest_input = True
d = []
device_types = {}
for device_info in devices_info: for device_info in devices_info:
devices.append(InputDevice(device_info.path)) d.append(InputDevice(device_info.path))
devices[-1].grab() device_types[device_info.path] = device_info.device_type
send_device(s, devices[-1]) send_device(s, d[-1])
s.sendall((0).to_bytes(4, byteorder='big')) s.sendall((0).to_bytes(4, byteorder='big'))
devices = {device.fd: device for device in devices} d = {device.fd: device for device in d}
device_types = {fd: device_types[d[fd].path] for fd in d}
prnt = time()
while True: while True:
r, w, x = select(devices, [], []) for fd in d:
if d[fd].active_keys():
break
else:
break
if time() > prnt:
print("Let go of all keys!")
prnt += 1
continue
for fd in d:
d[fd].grab()
while True:
r, w, x = select(d, [], [])
for fd in r: for fd in r:
for event in devices[fd].read(): events = [event for event in d[fd].read()]
send_input_event(s, fd, event) if device_types[fd] == "keyboard":
s.sendall(b'\xff'*20) active_keys = d[fd].active_keys()
for device in devices: if 29 in active_keys and 97 in active_keys:
device.ungrab() guest_input = double_control(s, d, active_keys, guest_input, ctrl2, fd, events[0].value)
ctrl2 = True
else:
ctrl2 = False
if guest_input:
for event in events:
send_input_event(s, fd, event)