From 0d932e3679f3c2b1826c889f05155342f80ba48d Mon Sep 17 00:00:00 2001 From: Lars Date: Thu, 2 May 2024 10:12:51 +0200 Subject: [PATCH 1/2] Initial support for wlr_switch Added Switch class as a wrapper for wlr_switch, added SwitchToggleEvent and SwitchType and SwitchState enums --- wlroots/ffi_build.py | 37 +++++++++++++++++++ wlroots/wlr_types/switch.py | 73 +++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 wlroots/wlr_types/switch.py diff --git a/wlroots/ffi_build.py b/wlroots/ffi_build.py index c8a9a9fa..b5bd2fd4 100644 --- a/wlroots/ffi_build.py +++ b/wlroots/ffi_build.py @@ -2342,6 +2342,42 @@ def has_xwayland() -> bool: struct wlr_surface *surface); """ + +# types/wlr_switch.h +CDEF += """ +struct wlr_switch { + struct wlr_input_device base; + + struct { + struct wl_signal toggle; + } events; + + void *data; + + ...; +}; + +enum wlr_switch_type { + WLR_SWITCH_TYPE_LID, + WLR_SWITCH_TYPE_TABLET_MODE, +}; + +enum wlr_switch_state { + WLR_SWITCH_STATE_OFF = 0, + WLR_SWITCH_STATE_ON, +}; + +struct wlr_switch_toggle_event { + uint32_t time_msec; + enum wlr_switch_type switch_type; + enum wlr_switch_state switch_state; +}; + +struct wlr_switch *wlr_switch_from_input_device( + struct wlr_input_device *input_device); +""" + + # types/wlr_touch.h CDEF += """ struct wlr_touch { @@ -3010,6 +3046,7 @@ def has_xwayland() -> bool: #include #include #include +#include #include #include #include diff --git a/wlroots/wlr_types/switch.py b/wlroots/wlr_types/switch.py new file mode 100644 index 00000000..4d1005ac --- /dev/null +++ b/wlroots/wlr_types/switch.py @@ -0,0 +1,73 @@ +from __future__ import annotations + +import enum +from weakref import WeakKeyDictionary + +from pywayland.server import Signal +from wlroots import Ptr, PtrHasData, ffi, lib + +from .input_device import InputDevice + +_weakkeydict: WeakKeyDictionary = WeakKeyDictionary() + + +@enum.unique +class SwitchType(enum.IntEnum): + LID = lib.WLR_SWITCH_TYPE_LID + TABLET_MODE = lib.WLR_SWITCH_TYPE_TABLET_MODE + + +@enum.unique +class SwitchState(enum.IntEnum): + STATE_OFF = lib.WLR_SWITCH_STATE_OFF + STATE_ON = lib.WLR_SWITCH_STATE_ON + + +class Switch(PtrHasData): + """ + A switch input device. + + Typically a switch input device can indicate whether a laptop lid is opened + or closed, or whether tablet mode is enabled. + + See https://wayland.freedesktop.org/libinput/doc/latest/switches.html + """ + + def __init__(self, ptr) -> None: + self._ptr = ptr + self.toggle_event = Signal( + ptr=ffi.addressof(self._ptr.events.toggle), data_wrapper=SwitchToggleEvent + ) + + @staticmethod + def from_input_device(input_device: InputDevice) -> Switch: + """ + Get a Switch from an InputDevice. + + Asserts that the input device is a switch. + """ + return Switch(lib.wlr_switch_from_input_device(input_device._ptr)) + + @property + def base(self) -> InputDevice: + device_ptr = ffi.addressof(self._ptr.base) + _weakkeydict[device_ptr] = self._ptr + return InputDevice(device_ptr) + + +class SwitchToggleEvent(Ptr): + + def __init__(self, ptr): + self._ptr = ptr + + @property + def time_msec(self) -> int: + return self._ptr.time_msec + + @property + def switch_type(self) -> SwitchType: + return SwitchType(self._ptr.switch_type) + + @property + def switch_state(self) -> SwitchState: + return SwitchState(self._ptr.switch_state) From 78061b9b9d5fc7a6d13f6494214ae5e931f702d6 Mon Sep 17 00:00:00 2001 From: Lars Date: Thu, 2 May 2024 11:46:42 +0200 Subject: [PATCH 2/2] Added nl between import statements --- wlroots/wlr_types/switch.py | 1 + 1 file changed, 1 insertion(+) diff --git a/wlroots/wlr_types/switch.py b/wlroots/wlr_types/switch.py index 4d1005ac..b69d423d 100644 --- a/wlroots/wlr_types/switch.py +++ b/wlroots/wlr_types/switch.py @@ -4,6 +4,7 @@ from weakref import WeakKeyDictionary from pywayland.server import Signal + from wlroots import Ptr, PtrHasData, ffi, lib from .input_device import InputDevice