Skip to content

Commit

Permalink
[codegen] support int enums
Browse files Browse the repository at this point in the history
  • Loading branch information
aslpavel committed Sep 27, 2024
1 parent 38cc38e commit 41563d7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 26 deletions.
30 changes: 9 additions & 21 deletions wayland/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,15 +388,15 @@ class ArgUInt(Arg):

def __init__(self, name: str, enum: str | None = None):
super().__init__(name)
self.enum: str | None = enum
self.enum: str | None = enum # it is enum represented as integer

def pack(self, write: io.BytesIO, value: Any) -> None:
if isinstance(value, Enum):
write.write(self.struct.pack(value.value))
elif isinstance(value, int) and value >= 0:
elif isinstance(value, int):
write.write(self.struct.pack(value))
else:
raise TypeError(f"[{self.name}] unsigend integer expected")
raise TypeError(f"[{self.name}] integer expected")

def unpack(
self,
Expand All @@ -407,28 +407,15 @@ def unpack(
return self.struct.unpack(read.read(self.struct.size))[0]

def __str__(self) -> str:
type_name = self.__class__.__name__
if self.enum:
return f'ArgUInt("{self.name}", "{self.enum}")'
return f'ArgUInt("{self.name}")'
return f'{type_name}("{self.name}", "{self.enum}")'
return f'{type_name}("{self.name}")'


class ArgInt(Arg):
type_name: ClassVar[str] = "int"
class ArgInt(ArgUInt):
struct: ClassVar[Struct] = Struct("i")

def pack(self, write: io.BytesIO, value: Any) -> None:
if not isinstance(value, int):
raise TypeError(f"[{self.name}] signed integer expected")
write.write(self.struct.pack(value))

def unpack(
self,
read: io.BytesIO,
connection: Connection,
hint: Any | None = None,
) -> Any:
return self.struct.unpack(read.read(self.struct.size))[0]


class ArgFixed(Arg):
"""Signed 24.8 floating point value"""
Expand Down Expand Up @@ -957,7 +944,8 @@ def load(cls, path: str) -> Protocol:
enum_name = arg_node.get("enum")
args.append(ArgUInt(arg_name, enum_name))
elif arg_type == "int":
args.append(ArgInt(arg_name))
enum_name = arg_node.get("enum")
args.append(ArgInt(arg_name, enum_name))
elif arg_type == "fixed":
args.append(ArgFixed(arg_name))
elif arg_type == "string":
Expand Down
8 changes: 4 additions & 4 deletions wayland/protocol/wayland.py
Original file line number Diff line number Diff line change
Expand Up @@ -1171,7 +1171,7 @@ class WlSurface(Proxy):
WRequest("set_opaque_region", [ArgObject("region", "wl_region", True)]),
WRequest("set_input_region", [ArgObject("region", "wl_region", True)]),
WRequest("commit", []),
WRequest("set_buffer_transform", [ArgInt("transform")]),
WRequest("set_buffer_transform", [ArgInt("transform", "wl_output.transform")]),
WRequest("set_buffer_scale", [ArgInt("scale")]),
WRequest("damage_buffer", [ArgInt("x"), ArgInt("y"), ArgInt("width"), ArgInt("height")]),
WRequest("offset", [ArgInt("x"), ArgInt("y")]),
Expand Down Expand Up @@ -1235,7 +1235,7 @@ def commit(self) -> None:
self._call(OpCode(6), tuple())
return None

def set_buffer_transform(self, transform: int) -> None:
def set_buffer_transform(self, transform: WlOutput.Transform) -> None:
"""sets the buffer transformation"""
self._call(OpCode(7), (transform,))
return None
Expand Down Expand Up @@ -1770,7 +1770,7 @@ class WlOutput(Proxy):
WRequest("release", []),
],
events=[
WEvent("geometry", [ArgInt("x"), ArgInt("y"), ArgInt("physical_width"), ArgInt("physical_height"), ArgInt("subpixel"), ArgStr("make"), ArgStr("model"), ArgInt("transform")]),
WEvent("geometry", [ArgInt("x"), ArgInt("y"), ArgInt("physical_width"), ArgInt("physical_height"), ArgInt("subpixel", "subpixel"), ArgStr("make"), ArgStr("model"), ArgInt("transform", "transform")]),
WEvent("mode", [ArgUInt("flags", "mode"), ArgInt("width"), ArgInt("height"), ArgInt("refresh")]),
WEvent("done", []),
WEvent("scale", [ArgInt("factor")]),
Expand Down Expand Up @@ -1827,7 +1827,7 @@ def __enter__(self) -> WlOutput:
def __exit__(self, *_: Any) -> None:
self.release()

def on_geometry(self, handler: Callable[[int, int, int, int, int, str, str, int], bool]) -> Callable[[int, int, int, int, int, str, str, int], bool] | None:
def on_geometry(self, handler: Callable[[int, int, int, int, Subpixel, str, str, Transform], bool]) -> Callable[[int, int, int, int, Subpixel, str, str, Transform], bool] | None:
"""properties of the output"""
_opcode = OpCode(0)
old_handler, self._handlers[_opcode] = self._handlers[_opcode], handler
Expand Down
2 changes: 1 addition & 1 deletion wayland/protocol/wlr_layer_shell_unstable_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from typing import Any, ClassVar
from collections.abc import Callable
from ..base import *
from .xdg_shell import *
from .wayland import *
from .xdg_shell import *

__all__ = [
"ZwlrLayerShellV1",
Expand Down

0 comments on commit 41563d7

Please sign in to comment.