Skip to content

Commit

Permalink
Merge pull request #185 from heuer/feature_switch
Browse files Browse the repository at this point in the history
Support for wlr_switch
  • Loading branch information
flacjacket authored May 5, 2024
2 parents aa3da66 + 78061b9 commit 8081cac
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
37 changes: 37 additions & 0 deletions wlroots/ffi_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -3010,6 +3046,7 @@ def has_xwayland() -> bool:
#include <wlr/types/wlr_server_decoration.h>
#include <wlr/types/wlr_session_lock_v1.h>
#include <wlr/types/wlr_single_pixel_buffer_v1.h>
#include <wlr/types/wlr_switch.h>
#include <wlr/types/wlr_touch.h>
#include <wlr/types/wlr_virtual_keyboard_v1.h>
#include <wlr/types/wlr_virtual_pointer_v1.h>
Expand Down
74 changes: 74 additions & 0 deletions wlroots/wlr_types/switch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
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)

0 comments on commit 8081cac

Please sign in to comment.