rpikvm/hidinput.py
2020-05-24 11:08:49 +02:00

68 lines
1.5 KiB
Python

from flask import Response
from flask import Flask
from flask import render_template
import threading
import argparse
import datetime
import time
lock = threading.Lock()
# initialize a flask object
app = Flask(__name__)
NULL = chr(0)
hiddev = None
def init():
hiddev=open('/dev/hidg0', 'rb+')
def hidwrite(data):
if hiddev is None:
return False
hiddev.write(data.encode())
hiddev.flush()
def send_key(hidkey, shift, alt, ctlr, superkey):
# TODO handle mod keys
hidwrite(NULL*2+chr(hidkey)+NULL*5)
hidwrite(NULL*8)
def get_hid_by_scancode(scancode):
@app.route("/")
def index():
return render_template("hid.html")
@app.route("/hid/keyboard", methods=["POST"])
def keypress():
return Response("Stisknuto ", mimetype = "text/plain")
# check to see if this is the main thread of execution
if __name__ == '__main__':
# construct the argument parser and parse command line arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--ip", type=str, required=True,
help="ip address of the device")
ap.add_argument("-o", "--port", type=int, required=True,
help="ephemeral port number of the server (1024 to 65535)")
ap.add_argument("-f", "--frame-count", type=int, default=32,
help="# of frames used to construct the background model")
args = vars(ap.parse_args())
app.run(host=args["ip"], port=args["port"], debug=True,
threaded=True, use_reloader=False)
# Clean up
if hiddev is not None:
print("Closing hid")
hiddev.close()