diff --git a/.gitignore b/.gitignore index bee8a64..b72bc3d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ __pycache__ +server/pieces +server/newlist +server/oldlist diff --git a/server/backends/port8080.py b/server/backends/port8080.py index 54e3edb..ea5bfd5 100644 --- a/server/backends/port8080.py +++ b/server/backends/port8080.py @@ -1,48 +1,14 @@ -import os import requests -import threading -import json + def get_img(): raw_img = requests.get('http://localhost:8080/get_capture.bmp').content - with open('full_img.bmp', 'wb+') as f: + with open('img.bmp', 'wb+') as f: f.write(raw_img) - os.system('convert full_img.bmp -resize 800x480 img.bmp') - - with open('crops.json') as f: - crops = json.load(f) + return 'img.bmp' - threads = [] - for crop in crops: - threads.append(threading.Thread(target=os.system, args=(f'convert img.bmp -crop {crop} pieces/{crop.split("+", 1)[1].replace("+", "x")}.jpg',))) - threads[-1].start() - for thread in threads: - thread.join() - os.system("md5sum -c oldlist 2>/dev/null | grep FAILED > newlist") - changed = [] - with open('newlist') as f: - lines = f.readlines() - for line in lines: - changed.append(line.split(": ")[0].split('.')[0].split('/')[1]) - os.system("md5sum pieces/* > oldlist") - return changed - -def get_full_img(): - raw_img = requests.get('http://localhost:8080/get_capture.bmp').content - - with open('full_full.bmp', 'wb+') as f: - f.write(raw_img) - - os.system('convert full_full.bmp -resize 800x480 full.jpg') - - os.unlink('full_full.bmp') - - return 'full.jpg' - -def touch(x, y, w, is_long): - x = round(800 * x / w) - y = round(800 * y / w) +def touch(x, y, is_long): requests.get(f'http://localhost:8080/set_touch?x={x}&y={y}&hold={1 if is_long else 0}') diff --git a/server/backends/x11.py b/server/backends/x11.py index a4a5a9b..2961bae 100644 --- a/server/backends/x11.py +++ b/server/backends/x11.py @@ -1,41 +1,21 @@ import os -import requests import threading -import json import time + +def _long_touch(x, y): + os.system(f'xdotool mousemove {x} {y} mousedown 1') + time.sleep(2) + os.system(f'xdotool mouseup 1') + + def get_img(): os.system('xwd -root -silent | convert xwd:- img.bmp') - - with open('crops.json') as f: - crops = json.load(f) + return 'img.bmp' - threads = [] - for crop in crops: - threads.append(threading.Thread(target=os.system, args=(f'convert img.bmp -crop {crop} pieces/{crop.split("+", 1)[1].replace("+", "x")}.jpg',))) - threads[-1].start() - for thread in threads: - thread.join() - os.system("md5sum -c oldlist 2>/dev/null | grep FAILED > newlist") - changed = [] - with open('newlist') as f: - lines = f.readlines() - for line in lines: - changed.append(line.split(": ")[0].split('.')[0].split('/')[1]) - os.system("md5sum pieces/* > oldlist") - return changed - -def get_full_img(): - os.system('xwd -root -silent | convert xwd:- full.jpg') - return 'full.jpg' - -def touch(x, y, w, is_long): - x = round(800 * x / w) - y = round(800 * y / w) +def touch(x, y, is_long): if is_long: - os.system(f'xdotool mousemove {x} {y} mousedown 1') - time.sleep(2) - os.system(f'xdotool mouseup 1') + threading.Thread(target=_long_touch, args=(x, y,)).start() else: os.system(f'xdotool mousemove {x} {y} click 1') diff --git a/server/imgproc.py b/server/imgproc.py new file mode 100644 index 0000000..fecbe79 --- /dev/null +++ b/server/imgproc.py @@ -0,0 +1,42 @@ +import json +import threading +import os + +def get_split_imgs(): + bmp_path = backend.get_img() + + with open('crops.json') as f: + crops = json.load(f) + + threads = [] + for crop in crops: + threads.append(threading.Thread(target=os.system, args=(f'convert {bmp_path} -crop {crop} pieces/{crop.split("+", 1)[1].replace("+", "x")}.jpg',))) + threads[-1].start() + for thread in threads: + thread.join() + + os.unlink(bmp_path) + + os.system("md5sum -c oldlist 2>/dev/null | grep FAILED > newlist") + changed = [] + with open('newlist') as f: + lines = f.readlines() + for line in lines: + changed.append(line.split(": ")[0].split('.')[0].split('/')[1]) + os.system("md5sum pieces/* > oldlist") + + return changed + +def get_full_img(): + bmp_path = backend.get_img() + + os.system(f'convert {bmp_path} img.jpg') + + os.unlink(bmp_path) + + return 'img.jpg' + +def touch(x, y, w, is_long): + x = round(800 * x / w) + y = round(800 * y / w) + backend.touch(x, y, is_long) diff --git a/server/wss.py b/server/wss.py index 127667b..3119426 100644 --- a/server/wss.py +++ b/server/wss.py @@ -7,19 +7,21 @@ import time import shutil import tempfile from websocket_server import WebsocketServer - +import imgproc as hcapi # Select backend - backends.port8080 uses HamClock's port 8080 service; # backends.x11 uses an X11 server (typically Xvfb) (make sure DISPLAY is set # correctly!) -#import backends.port8080 as hcapi -import backends.x11 as hcapi +#import backends.port8080 as backend +import backends.x11 as backend + +hcapi.backend = backend def cycle(): try: - changed = hcapi.get_img() + changed = hcapi.get_split_imgs() except Exception as e: for client in clients.values(): client.send('err%noconn%Server failed to capture screenshot', 'ERR')