guanana/pikvm-lib

absolute mouse movement?

Opened this issue · 2 comments

Is your feature request related to a problem? Please describe.
clean interface for absolute mouse position

Describe the solution you'd like
some fn mouse_move_to(sx, sy) mapping screen 0..width to -i16..+i16, same for height

Describe alternatives you've considered

import ssl, time
import random
import json
import os

host = os.environ.get('HOST')
user = os.environ.get('USER', 'admin')
password = os.environ.get('PASSWORD', 'admin')

screen_width = 1920
screen_height = 1080

uri = f'wss://{host}/api/ws?stream=0'
print(f'connecting to {user}@{host} uri: {uri}')

headers = {"X-KVMD-User": user, "X-KVMD-Passwd": password}
print('headers', headers)

ws = websocket.WebSocket(sslopt={"cert_reqs": ssl.CERT_NONE})
ws.connect(uri, header=headers)

def scale_mouse_xy_to_i16(screenx, screeny, width=1920, height=1080):
    # Map from (0, width) to (i16)
    hid_x_i16 = int((screenx / width) * 0xffff - 0x8000)

    # Map from (0, height) to (i16)
    hid_y_i16 = int((screeny / height) * 0xffff - 0x8000)

    return (hid_x_i16, hid_y_i16)

def mouse_jump_to(screenx, screeny, width=1920, height=1080):
    print('mouse_move', screenx, screeny)

    kvmx, kvmy = scale_mouse_xy_to_i16(screenx, screeny)

    msg = json.dumps({
            "event_type": "mouse_move",
            "event": {
                "to": {
                    "x": kvmx,
                    "y": kvmy
                },
            }
        })
    ws.send(msg)

while True:
    for y in range(0, screen_height, 10):
        for x in range(0, screen_width, 19):
            mouse_jump_to(x, y)
            time.sleep(0.1)

ws.close()

Additional context
using kvm for automated testing without installing software on the remote device under test

@iceisfun It's something I wanted to take a look at (mouse control). It's a bit more time consuming than the rest of the library and that is why I decided to release first this part and then move into the mouse. It looks like you are into something with the code you shared. Any help will be appreciated.
The feature is in my radar but don't know when I will be able to start it

I have had great luck as I known the sw, sh just using this fn as is, I think the pikvm api just lacks a lot of documentation.

As I've looked at this more just documenting the APIs for HID keyboards, mice with some simple examples probably eliminates 90% of confusion with these things, we are using it for test harness inputs to a immutable device and it works wonderful.

If you get the session resolution it can be easily implemented as fn(sx, sy) but I have not explored what it takes to query the current resolution.

It would also be ideal to impl Mouse{Left, Right, WheelDown, WheelUp, ...} and make a example... Is this the correct repo to do PRs for that kind of stuff?

For our use case we have integrated this fine with the current code, I'm just thinking it might be easier for other pikvm people to have a simpler interface.