Skip to content

Truly independent web browser

LibGfx+LibCompositing: Generate Rust codecs for compositor IPC types

Aliaksandr Kalenika0305d0158a3
diff --git a/Cargo.lock b/Cargo.lock
index 1b09a63db07..5faa2eaef09 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1777,6 +1777,7 @@ dependencies = [
"foldhash 0.2.0",
"hashbrown 0.17.1",
"ladybird_allocator",
+ "libipc_rust",
"rustc-demangle",
"yuv",
]
diff --git a/Libraries/LibCompositing/Rust/build.rs b/Libraries/LibCompositing/Rust/build.rs
index a9158062f20..98abe6e4369 100644
--- a/Libraries/LibCompositing/Rust/build.rs
+++ b/Libraries/LibCompositing/Rust/build.rs
@@ -260,7 +260,7 @@ fn display_list_payload_alignment(path: &Path) -> Result<String, Box<dyn Error>>
Ok(value.to_string())
}
-fn generate_ipc_endpoints(manifest_dir: &Path, out_dir: &Path) -> Result<(), Box<dyn Error>> {
+fn generate_ipc_definitions(manifest_dir: &Path, out_dir: &Path) -> Result<(), Box<dyn Error>> {
let source_dir = manifest_dir.join("../../..");
let generator = source_dir.join("Meta/Generators/generate_ipc_definitions.py");
println!("cargo:rerun-if-changed={}", generator.display());
@@ -273,19 +273,48 @@ fn generate_ipc_endpoints(manifest_dir: &Path, out_dir: &Path) -> Result<(), Box
println!("cargo:rerun-if-env-changed=PYTHON");
let python = env::var_os("PYTHON").unwrap_or_else(|| if cfg!(windows) { "python" } else { "python3" }.into());
- for endpoint in ["RenderClockClient", "RenderClockServer"] {
- let definition = source_dir.join(format!("Services/Compositor/{endpoint}.ipc"));
+ for (definition, generated_file) in [
+ (
+ "Libraries/LibCompositing/DisplayList/DisplayListProperties.ipc",
+ "DisplayListProperties.rs",
+ ),
+ ("Libraries/LibCompositing/Types.ipc", "Types.rs"),
+ ("Libraries/LibWebCommon/UIEvents/KeyCode.ipc", "KeyCode.rs"),
+ ("Libraries/LibWebCommon/UIEvents/MouseButton.ipc", "MouseButton.rs"),
+ (
+ "Libraries/LibWebCommon/Page/AsyncScrollNodeStableID.ipc",
+ "AsyncScrollNodeStableID.rs",
+ ),
+ ("Libraries/LibWebCommon/Page/InputEvent.ipc", "InputEvent.rs"),
+ (
+ "Libraries/LibWebCommon/Page/InputRoutingDecision.ipc",
+ "InputRoutingDecision.rs",
+ ),
+ ("Libraries/LibWebCommon/Page/InputTrace.ipc", "InputTrace.rs"),
+ (
+ "Libraries/LibWebCommon/Page/TextEditingCommand.ipc",
+ "TextEditingCommand.rs",
+ ),
+ (
+ "Services/Compositor/RenderClockClient.ipc",
+ "RenderClockClientEndpoint.rs",
+ ),
+ (
+ "Services/Compositor/RenderClockServer.ipc",
+ "RenderClockServerEndpoint.rs",
+ ),
+ ] {
+ let definition = source_dir.join(definition);
println!("cargo:rerun-if-changed={}", definition.display());
- let output = out_dir.join(format!("{endpoint}Endpoint.rs"));
let status = Command::new(&python)
.arg(&generator)
.arg("--input")
.arg(&definition)
.arg("--rust-output")
- .arg(&output)
+ .arg(out_dir.join(generated_file))
.status()?;
if !status.success() {
- return Err(format!("IPC endpoint generation failed for {endpoint}").into());
+ return Err(format!("IPC compiler failed for {}", definition.display()).into());
}
}
Ok(())
@@ -295,7 +324,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR")?);
let out_dir = PathBuf::from(env::var("OUT_DIR")?);
- generate_ipc_endpoints(&manifest_dir, &out_dir)?;
+ generate_ipc_definitions(&manifest_dir, &out_dir)?;
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=cbindgen.toml");
diff --git a/Libraries/LibCompositing/Rust/src/ipc_types.rs b/Libraries/LibCompositing/Rust/src/ipc_types.rs
index 1126e3a4f13..403bbd666c5 100644
--- a/Libraries/LibCompositing/Rust/src/ipc_types.rs
+++ b/Libraries/LibCompositing/Rust/src/ipc_types.rs
@@ -4,8 +4,9 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
-//! The types the Compositor's endpoints carry, under the names the IPC compiler gives them, and the endpoints it
-//! generates from Services/Compositor/*.ipc for the messages that name Rust.
+//! The types the Compositor's endpoints carry, under the names the IPC compiler gives them: the ones it generates from
+//! their .ipc declarations, the ones with a wire form of their own, and the endpoints it generates from
+//! Services/Compositor/*.ipc for the messages that name Rust.
#![allow(non_camel_case_types)]
@@ -16,6 +17,12 @@ use libipc_rust::wire::{Decode, DecodeError, Decoder, Encode, EncodeError, Encod
pub type Web_CompositorContextId = CompositorContextId;
pub type Web_UniqueNodeID = UniqueNodeId;
pub type Web_CSSPixelPoint = CssPixelPoint;
+pub type Gfx_IntPoint = libgfx_rust::IntPoint;
+pub type Gfx_FloatPoint = libgfx_rust::FloatPoint;
+pub type Gfx_IntSize = libgfx_rust::IntSize;
+pub type Gfx_IntRect = libgfx_rust::IntRect;
+pub type Gfx_FloatRect = libgfx_rust::FloatRect;
+pub type Gfx_Color = libgfx_rust::Color;
impl Encode for CompositorContextId {
fn encode(&self, encoder: &mut Encoder) -> Result<(), EncodeError> {
@@ -63,87 +70,6 @@ impl Decode for CssPixelPoint {
}
}
-#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
-#[repr(u8)]
-pub enum Web_AsyncScrollNodeKind {
- Viewport,
- Element,
- PseudoElement,
- VisualViewport,
-}
-
-impl Encode for Web_AsyncScrollNodeKind {
- fn encode(&self, encoder: &mut Encoder) -> Result<(), EncodeError> {
- encoder.encode(&(*self as u8))
- }
-}
-
-impl Decode for Web_AsyncScrollNodeKind {
- const MIN_ENCODED_SIZE: usize = size_of::<u8>();
-
- fn decode(decoder: &mut Decoder<'_>) -> Result<Self, DecodeError> {
- match decoder.decode::<u8>()? {
- 0 => Ok(Self::Viewport),
- 1 => Ok(Self::Element),
- 2 => Ok(Self::PseudoElement),
- 3 => Ok(Self::VisualViewport),
- _ => Err(DecodeError::InvalidValue("Web::AsyncScrollNodeKind")),
- }
- }
-}
-
-#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
-pub struct Web_AsyncScrollNodeStableID {
- pub node_id: UniqueNodeId,
- pub kind: Web_AsyncScrollNodeKind,
- pub pseudo_element_type: u8,
-}
-
-impl Encode for Web_AsyncScrollNodeStableID {
- fn encode(&self, encoder: &mut Encoder) -> Result<(), EncodeError> {
- encoder.encode(&self.node_id)?;
- encoder.encode(&self.kind)?;
- encoder.encode(&self.pseudo_element_type)
- }
-}
-
-impl Decode for Web_AsyncScrollNodeStableID {
- const MIN_ENCODED_SIZE: usize =
- UniqueNodeId::MIN_ENCODED_SIZE + Web_AsyncScrollNodeKind::MIN_ENCODED_SIZE + size_of::<u8>();
-
- fn decode(decoder: &mut Decoder<'_>) -> Result<Self, DecodeError> {
- Ok(Self {
- node_id: decoder.decode()?,
- kind: decoder.decode()?,
- pseudo_element_type: decoder.decode()?,
- })
- }
-}
-
-#[derive(Clone, Copy, Debug, PartialEq, Eq)]
-pub struct Web_CompositorScrollOffset {
- pub scroll_node: Web_AsyncScrollNodeStableID,
- pub offset: CssPixelPoint,
-}
-
-impl Encode for Web_CompositorScrollOffset {
- fn encode(&self, encoder: &mut Encoder) -> Result<(), EncodeError> {
- encoder.encode(&self.scroll_node)?;
- encoder.encode(&self.offset)
- }
-}
-
-impl Decode for Web_CompositorScrollOffset {
- const MIN_ENCODED_SIZE: usize = Web_AsyncScrollNodeStableID::MIN_ENCODED_SIZE + CssPixelPoint::MIN_ENCODED_SIZE;
-
- fn decode(decoder: &mut Decoder<'_>) -> Result<Self, DecodeError> {
- Ok(Self {
- scroll_node: decoder.decode()?,
- offset: decoder.decode()?,
- })
- }
-}
-
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Web_DevicePixelPoint {
pub x: i32,
@@ -168,5 +94,38 @@ impl Decode for Web_DevicePixelPoint {
}
}
+#[allow(non_upper_case_globals)]
+impl Web_UIEvents_KeyModifier {
+ pub const Mod_PlatformCtrl: Self = if cfg!(target_os = "macos") {
+ Self::Mod_Super
+ } else {
+ Self::Mod_Ctrl
+ };
+}
+
+impl Web_InputRoutingDecision {
+ // The name tests read a decision by. ENUMERATE_INPUT_ROUTING_DECISIONS gives each decision its name in kebab case.
+ pub fn name(self) -> String {
+ let mut name = String::new();
+ for character in format!("{self:?}").chars() {
+ if character.is_ascii_uppercase() && !name.is_empty() {
+ name.push('-');
+ }
+ name.push(character.to_ascii_lowercase());
+ }
+ name
+ }
+}
+
+include!(concat!(env!("OUT_DIR"), "/AsyncScrollNodeStableID.rs"));
+include!(concat!(env!("OUT_DIR"), "/DisplayListProperties.rs"));
+include!(concat!(env!("OUT_DIR"), "/InputEvent.rs"));
+include!(concat!(env!("OUT_DIR"), "/InputRoutingDecision.rs"));
+include!(concat!(env!("OUT_DIR"), "/InputTrace.rs"));
+include!(concat!(env!("OUT_DIR"), "/KeyCode.rs"));
+include!(concat!(env!("OUT_DIR"), "/MouseButton.rs"));
+include!(concat!(env!("OUT_DIR"), "/TextEditingCommand.rs"));
+include!(concat!(env!("OUT_DIR"), "/Types.rs"));
+
include!(concat!(env!("OUT_DIR"), "/RenderClockClientEndpoint.rs"));
include!(concat!(env!("OUT_DIR"), "/RenderClockServerEndpoint.rs"));
diff --git a/Libraries/LibGfx/Rust/Cargo.toml b/Libraries/LibGfx/Rust/Cargo.toml
index ae912904c11..a2568208b10 100644
--- a/Libraries/LibGfx/Rust/Cargo.toml
+++ b/Libraries/LibGfx/Rust/Cargo.toml
@@ -14,6 +14,7 @@ allocator = ["dep:ladybird_allocator"]
ak = { path = "../../../AK/Rust" }
foldhash = "0.2"
hashbrown = { version = "0.17.1", default-features = false }
+libipc_rust = { path = "../../LibIPC/Rust" }
ladybird_allocator = { path = "../../RustAllocator", optional = true }
rustc-demangle = "0.1"
yuv = "0.8.19"
diff --git a/Libraries/LibGfx/Rust/src/ipc.rs b/Libraries/LibGfx/Rust/src/ipc.rs
new file mode 100644
index 00000000000..a6fb336a9e9
--- /dev/null
+++ b/Libraries/LibGfx/Rust/src/ipc.rs
@@ -0,0 +1,122 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+//! Codecs for the Gfx values carried by IPC.
+
+use crate::{Color, FloatPoint, FloatRect, IntPoint, IntRect, IntSize};
+use libipc_rust::wire::{Decode, DecodeError, Decoder, Encode, EncodeError, Encoder};
+
+impl Encode for IntPoint {
+ fn encode(&self, encoder: &mut Encoder) -> Result<(), EncodeError> {
+ encoder.encode(&self.x)?;
+ encoder.encode(&self.y)
+ }
+}
+
+impl Decode for IntPoint {
+ const MIN_ENCODED_SIZE: usize = 2 * size_of::<i32>();
+
+ fn decode(decoder: &mut Decoder<'_>) -> Result<Self, DecodeError> {
+ Ok(Self {
+ x: decoder.decode()?,
+ y: decoder.decode()?,
+ })
+ }
+}
+
+impl Encode for FloatPoint {
+ fn encode(&self, encoder: &mut Encoder) -> Result<(), EncodeError> {
+ encoder.encode(&self.x)?;
+ encoder.encode(&self.y)
+ }
+}
+
+impl Decode for FloatPoint {
+ const MIN_ENCODED_SIZE: usize = 2 * size_of::<f32>();
+
+ fn decode(decoder: &mut Decoder<'_>) -> Result<Self, DecodeError> {
+ Ok(Self {
+ x: decoder.decode()?,
+ y: decoder.decode()?,
+ })
+ }
+}
+
+impl Encode for IntSize {
+ fn encode(&self, encoder: &mut Encoder) -> Result<(), EncodeError> {
+ encoder.encode(&self.width)?;
+ encoder.encode(&self.height)
+ }
+}
+
+impl Decode for IntSize {
+ const MIN_ENCODED_SIZE: usize = 2 * size_of::<i32>();
+
+ fn decode(decoder: &mut Decoder<'_>) -> Result<Self, DecodeError> {
+ Ok(Self {
+ width: decoder.decode()?,
+ height: decoder.decode()?,
+ })
+ }
+}
+
+impl Encode for IntRect {
+ fn encode(&self, encoder: &mut Encoder) -> Result<(), EncodeError> {
+ encoder.encode(&self.x)?;
+ encoder.encode(&self.y)?;
+ encoder.encode(&self.width)?;
+ encoder.encode(&self.height)
+ }
+}
+
+impl Decode for IntRect {
+ const MIN_ENCODED_SIZE: usize = 4 * size_of::<i32>();
+
+ fn decode(decoder: &mut Decoder<'_>) -> Result<Self, DecodeError> {
+ Ok(Self {
+ x: decoder.decode()?,
+ y: decoder.decode()?,
+ width: decoder.decode()?,
+ height: decoder.decode()?,
+ })
+ }
+}
+
+impl Encode for FloatRect {
+ fn encode(&self, encoder: &mut Encoder) -> Result<(), EncodeError> {
+ encoder.encode(&self.x)?;
+ encoder.encode(&self.y)?;
+ encoder.encode(&self.width)?;
+ encoder.encode(&self.height)
+ }
+}
+
+impl Decode for FloatRect {
+ const MIN_ENCODED_SIZE: usize = 4 * size_of::<f32>();
+
+ fn decode(decoder: &mut Decoder<'_>) -> Result<Self, DecodeError> {
+ Ok(Self {
+ x: decoder.decode()?,
+ y: decoder.decode()?,
+ width: decoder.decode()?,
+ height: decoder.decode()?,
+ })
+ }
+}
+
+impl Encode for Color {
+ fn encode(&self, encoder: &mut Encoder) -> Result<(), EncodeError> {
+ encoder.encode(&self.0)
+ }
+}
+
+impl Decode for Color {
+ const MIN_ENCODED_SIZE: usize = size_of::<u32>();
+
+ fn decode(decoder: &mut Decoder<'_>) -> Result<Self, DecodeError> {
+ Ok(Self(decoder.decode()?))
+ }
+}
diff --git a/Libraries/LibGfx/Rust/src/lib.rs b/Libraries/LibGfx/Rust/src/lib.rs
index c5573751c05..7426ce6fb9c 100644
--- a/Libraries/LibGfx/Rust/src/lib.rs
+++ b/Libraries/LibGfx/Rust/src/lib.rs
@@ -21,6 +21,7 @@ pub mod font;
pub mod font_catalog;
pub mod geometry;
pub mod image_frame;
+pub mod ipc;
pub mod math_table;
pub mod matrix;
pub mod paint_enums;