Skip to content

Commit

Permalink
Merge pull request #4 from Geson-anko/feature/inherits-virtual-device
Browse files Browse the repository at this point in the history
Inherits Virtual Device class in python bindings
  • Loading branch information
Geson-anko authored Feb 12, 2025
2 parents da03d66 + 16b03a9 commit 6ef2f15
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 68 deletions.
3 changes: 2 additions & 1 deletion bindings/python/src/inputtino/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from importlib import metadata

from .base import DeviceDefinition
from .base import DeviceDefinition, VirtualDevice
from .joypad import (
ControllerButton,
Joypad,
Expand Down Expand Up @@ -39,4 +39,5 @@
"PenButtonType",
"PenTablet",
"PenToolType",
"VirtualDevice",
]
21 changes: 21 additions & 0 deletions bindings/python/src/inputtino/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,24 @@ def to_core(self) -> _core.DeviceDefinition:
definition.device_phys = self.device_phys
definition.device_uniq = self.device_uniq
return definition


class VirtualDevice:
"""Base class for all virtual input devices."""

def __init__(self, device: _core.VirtualDevice) -> None:
"""Initialize virtual device.
Args:
device: Core virtual device instance
"""
self._device = device

@property
def nodes(self) -> list[str]:
"""Get the device nodes created by this virtual device.
Returns:
List of device node paths representing the virtual device in /dev/input/
"""
return self._device.get_nodes()
14 changes: 3 additions & 11 deletions bindings/python/src/inputtino/joypad.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
PS5MotionType,
StickPosition,
)
from .base import DeviceDefinition
from .base import DeviceDefinition, VirtualDevice


class ControllerButton(IntFlag):
Expand Down Expand Up @@ -44,7 +44,7 @@ class ControllerButton(IntFlag):
Y = CoreButton.Y.value


class Joypad:
class Joypad(VirtualDevice):
"""Base class for all joypad implementations."""

def __init__(self, device_def: DeviceDefinition, joypad: _core.Joypad) -> None:
Expand All @@ -54,18 +54,10 @@ def __init__(self, device_def: DeviceDefinition, joypad: _core.Joypad) -> None:
device_def: Device definition for the joypad
joypad: Core joypad instance
"""
super().__init__(joypad)
self._device_def = device_def
self._joypad = joypad

@property
def nodes(self) -> list[str]:
"""Get device nodes created by this virtual joypad.
Returns:
List of device node paths
"""
return self._joypad.get_nodes()

def set_pressed_buttons(self, buttons: ControllerButton) -> None:
"""Set currently pressed buttons.
Expand Down
14 changes: 3 additions & 11 deletions bindings/python/src/inputtino/keyboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from typing_extensions import Self

from . import _core
from .base import DeviceDefinition
from .base import DeviceDefinition, VirtualDevice

# Default device definition for keyboard
DEFAULT_KEYBOARD = DeviceDefinition(
Expand All @@ -19,7 +19,7 @@
)


class Keyboard:
class Keyboard(VirtualDevice):
"""Virtual keyboard input device.
This class provides functionality to simulate keyboard input.
Expand Down Expand Up @@ -51,6 +51,7 @@ def __init__(
RuntimeError: If device creation fails
"""
self._keyboard = _core.Keyboard.create(device_def.to_core(), millis_repress_key)
super().__init__(self._keyboard)

def press(self, key_code: int) -> None:
"""Press a keyboard key.
Expand All @@ -68,15 +69,6 @@ def release(self, key_code: int) -> None:
"""
self._keyboard.release(key_code)

@property
def nodes(self) -> list[str]:
"""Get the device nodes created by this virtual keyboard.
Returns:
List of device node paths
"""
return self._keyboard.get_nodes()

def type(self, key_code: int, duration: float = 0.1) -> None:
"""Press and release a key with specified duration.
Expand Down
14 changes: 3 additions & 11 deletions bindings/python/src/inputtino/mouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from . import _core
from ._core import MouseButton
from .base import DeviceDefinition
from .base import DeviceDefinition, VirtualDevice

# Default device definition for mouse
DEFAULT_MOUSE = DeviceDefinition(
Expand All @@ -15,7 +15,7 @@
)


class Mouse:
class Mouse(VirtualDevice):
"""Virtual mouse input device.
This class provides functionality to simulate mouse movements,
Expand All @@ -33,6 +33,7 @@ def __init__(self, device_def: DeviceDefinition = DEFAULT_MOUSE) -> None:
RuntimeError: If device creation fails
"""
self._mouse = _core.Mouse.create(device_def.to_core())
super().__init__(self._mouse)

def move(self, delta_x: int, delta_y: int) -> None:
"""Move the mouse cursor relative to its current position.
Expand Down Expand Up @@ -88,15 +89,6 @@ def scroll_horizontal(self, distance: int) -> None:
"""
self._mouse.horizontal_scroll(distance)

@property
def nodes(self) -> list[str]:
"""Get the device nodes created by this virtual mouse.
Returns:
List of device node paths
"""
return self._mouse.get_nodes()

def click(
self, button: MouseButton = MouseButton.LEFT, duration: float = 0.0
) -> None:
Expand Down
14 changes: 3 additions & 11 deletions bindings/python/src/inputtino/pentablet.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from . import _core
from ._core import PenButtonType, PenToolType
from .base import DeviceDefinition
from .base import DeviceDefinition, VirtualDevice

# Default device definition for pen tablet
DEFAULT_PEN_TABLET = DeviceDefinition(
Expand All @@ -13,7 +13,7 @@
)


class PenTablet:
class PenTablet(VirtualDevice):
"""Virtual pen tablet input device.
This class provides functionality to simulate pen tablet input with
Expand All @@ -35,6 +35,7 @@ def __init__(self, device_def: DeviceDefinition = DEFAULT_PEN_TABLET) -> None:
RuntimeError: If device creation fails
"""
self._tablet = _core.PenTablet.create(device_def.to_core())
super().__init__(self._tablet)

def place_tool(
self,
Expand Down Expand Up @@ -70,12 +71,3 @@ def set_button(self, button: PenButtonType, pressed: bool) -> None:
pressed: True if button is pressed, False if released
"""
self._tablet.set_btn(button, pressed)

@property
def nodes(self) -> list[str]:
"""Get the device nodes created by this virtual pen tablet.
Returns:
List of device node paths
"""
return self._tablet.get_nodes()
14 changes: 3 additions & 11 deletions bindings/python/src/inputtino/touchscreen.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Touchscreen input device implementation."""

from . import _core
from .base import DeviceDefinition
from .base import DeviceDefinition, VirtualDevice

# Default device definition for touchscreen
DEFAULT_TOUCHSCREEN = DeviceDefinition(
Expand All @@ -12,7 +12,7 @@
)


class TouchScreen:
class TouchScreen(VirtualDevice):
"""Virtual touchscreen input device.
This class provides functionality to simulate touchscreen
Expand All @@ -31,6 +31,7 @@ def __init__(self, device_def: DeviceDefinition = DEFAULT_TOUCHSCREEN) -> None:
RuntimeError: If device creation fails
"""
self._touchscreen = _core.TouchScreen.create(device_def.to_core())
super().__init__(self._touchscreen)

def place_finger(
self,
Expand Down Expand Up @@ -58,12 +59,3 @@ def release_finger(self, finger_nr: int) -> None:
finger_nr: Finger number to release
"""
self._touchscreen.release_finger(finger_nr)

@property
def nodes(self) -> list[str]:
"""Get the device nodes created by this virtual touchscreen.
Returns:
List of device node paths
"""
return self._touchscreen.get_nodes()
14 changes: 3 additions & 11 deletions bindings/python/src/inputtino/trackpad.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Trackpad input device implementation."""

from . import _core
from .base import DeviceDefinition
from .base import DeviceDefinition, VirtualDevice

DEFAULT_TRACKPAD = DeviceDefinition(
name="Wolf (virtual) touchpad",
Expand All @@ -11,7 +11,7 @@
)


class Trackpad:
class Trackpad(VirtualDevice):
"""Virtual trackpad input device.
This class provides functionality to simulate multi-touch trackpad
Expand All @@ -33,6 +33,7 @@ def __init__(self, device_def: DeviceDefinition = DEFAULT_TRACKPAD) -> None:
RuntimeError: If device creation fails
"""
self._trackpad = _core.Trackpad.create(device_def.to_core())
super().__init__(self._trackpad)

def place_finger(
self,
Expand Down Expand Up @@ -71,12 +72,3 @@ def set_left_button(self, pressed: bool) -> None:
pressed: True to press the button, False to release
"""
self._trackpad.set_left_btn(pressed)

@property
def nodes(self) -> list[str]:
"""Get the device nodes created by this virtual trackpad.
Returns:
List of device node paths
"""
return self._trackpad.get_nodes()
23 changes: 22 additions & 1 deletion bindings/python/tests/test_base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from unittest.mock import MagicMock

import pytest

from inputtino import _core
from inputtino.base import DeviceDefinition
from inputtino.base import DeviceDefinition, VirtualDevice


def test_device_definition_creation():
Expand Down Expand Up @@ -37,3 +41,20 @@ def test_device_definition_to_core():
assert core_def.version == definition.version
assert core_def.device_phys == definition.device_phys
assert core_def.device_uniq == definition.device_uniq


@pytest.fixture
def mock_core_device():
"""Create a mock for core VirtualDevice."""
mock_device = MagicMock(spec=_core.VirtualDevice)
mock_device.get_nodes.return_value = ["/dev/input/event0", "/dev/input/mouse0"]
return mock_device


def test_virtual_device_nodes(mock_core_device):
"""Test getting device nodes from virtual device."""
device = VirtualDevice(mock_core_device)
nodes = device.nodes

assert nodes == ["/dev/input/event0", "/dev/input/mouse0"]
mock_core_device.get_nodes.assert_called_once()

0 comments on commit 6ef2f15

Please sign in to comment.