Commit d7d50530 authored by Taddeüs Kroes's avatar Taddeüs Kroes

Move x11 code to separate file, write keypress functions

parent cfb67915
import time
from random import randint
from Xlib import display, X, XK, ext
from PIL import Image
WINDOW_WIDTH = 1600
WINDOW_HEIGHT = 900
BOARD_X = 367
BOARD_Y = 129
BOARD_WIDTH = 420
BOARD_HEIGHT = 638
KEY_DELAY= 0.015
disp = display.Display()
def find_window(name):
def traverse(window):
if window.get_wm_name() == name:
return window
for child in window.query_tree().children:
win = traverse(child)
if win:
return win
return traverse(display.Display().screen().root)
def get_exapunks_window():
win = find_window('EXAPUNKS')
assert win, 'EXAPUNKS window not found'
geo = win.get_geometry()
assert geo.width == WINDOW_WIDTH
assert geo.height == WINDOW_HEIGHT
return win
def focus_window(window):
window.set_input_focus(X.RevertToNone, X.CurrentTime)
window.raise_window()
disp.sync()
def screenshot_board(window):
raw = window.get_image(BOARD_X, BOARD_Y, BOARD_WIDTH, BOARD_HEIGHT,
X.ZPixmap, 0xffffffff)
dim = BOARD_WIDTH, BOARD_HEIGHT
im = Image.frombytes('RGB', dim, raw.data, 'raw', 'BGRX')
return im.convert('HSV')
def press_key(window, key):
keysym = XK.string_to_keysym(key)
keycode = disp.keysym_to_keycode(keysym)
ext.xtest.fake_input(disp, X.KeyPress, keycode)
disp.sync()
time.sleep(KEY_DELAY)
ext.xtest.fake_input(disp, X.KeyRelease, keycode)
disp.sync()
time.sleep(KEY_DELAY)
if __name__ == '__main__':
win = get_exapunks_window()
win.raise_window()
old_focus = disp.get_input_focus()
disp.set_input_focus(win, X.RevertToParent, X.CurrentTime)
def press_keys(keys):
for key in keys:
press_key(win, key)
#press_keys('aaaj')
#while True:
# press_keys('djkj' * 7)
# press_keys('ajkj' * 7)
while True:
press_key(win, 'adjk'[randint(0, 3)])
disp.set_input_focus(old_focus.focus, X.RevertToParent, X.CurrentTime)
disp.sync()
#!/usr/bin/env python3
import os
import time
import numpy as np
from Xlib import display, X
from PIL import Image
COLUMNS = 7
WINDOW_WIDTH = 1600
WINDOW_HEIGHT = 900
BLOCK_SIZE = 60
BOARD_X = 367
BOARD_Y = 129
BOARD_WIDTH = COLUMNS * BLOCK_SIZE
BOARD_HEIGHT = 638
MAX_COLUMN_HEIGHT = 546
DETECT_COLUMN_OFFSET_X = 8, 50
......@@ -49,43 +39,6 @@ def is_hue(h, hexpect):
return abs(h - hexpect) <= HUE_TOLERANCE
def find_window(name):
def traverse(window):
if window.get_wm_name() == name:
return window
for child in window.query_tree().children:
win = traverse(child)
if win:
return win
return traverse(display.Display().screen().root)
def get_exapunks_window():
win = find_window('EXAPUNKS')
assert win, 'EXAPUNKS window not found'
geo = win.get_geometry()
assert geo.width == WINDOW_WIDTH
assert geo.height == WINDOW_HEIGHT
return win
def focus_window(window):
window.set_input_focus(X.RevertToNone, X.CurrentTime)
window.raise_window()
display.Display().sync()
def screenshot_board(window):
start = time.time()
raw = window.get_image(BOARD_X, BOARD_Y, BOARD_WIDTH, BOARD_HEIGHT,
X.ZPixmap, 0xffffffff)
dim = BOARD_WIDTH, BOARD_HEIGHT
im = Image.frombytes('RGB', dim, raw.data, 'raw', 'BGRX')
return im.convert('HSV')
def detect_columns(board):
def saturated(x, y):
h, s, v = board.getpixel((x, y))
......@@ -165,6 +118,8 @@ def print_board(blocks, exa, held):
if __name__ == '__main__':
from iteraction import get_exapunks_window, screenshot_board
win = get_exapunks_window()
win.raise_window()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment