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
else:
s.connect((cfg.ip_address, cfg.port))
print(f"Connection established to {cfg.ip_address}")
yield s

View File

@ -22,15 +22,23 @@ def receive_event(s: socket) -> tuple[int, tuple[int, int, int]]:
def handle_client(s: socket):
devices = {}
d = {}
for fd, device in receive_devices(s):
devices[fd] = device
d[fd] = device
while True:
fd, event = receive_event(s)
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:
devices[fd].syn()
d[fd].syn()
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 select import select
import json
from time import time
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))
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]):
devices = []
ctrl2 = False
guest_input = True
d = []
device_types = {}
for device_info in devices_info:
devices.append(InputDevice(device_info.path))
devices[-1].grab()
send_device(s, devices[-1])
d.append(InputDevice(device_info.path))
device_types[device_info.path] = device_info.device_type
send_device(s, d[-1])
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:
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 event in devices[fd].read():
send_input_event(s, fd, event)
s.sendall(b'\xff'*20)
for device in devices:
device.ungrab()
events = [event for event in d[fd].read()]
if device_types[fd] == "keyboard":
active_keys = d[fd].active_keys()
if 29 in active_keys and 97 in active_keys:
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)