1
0
Fork 0
forked from wry/wry

autocommit 2022-02-06 20:12:09 CET

This commit is contained in:
Julian Orth 2022-02-06 20:12:09 +01:00
parent 3f50b0c75e
commit 96038f49bc
18 changed files with 60 additions and 90 deletions

View file

@ -491,61 +491,41 @@ fn to_camel(s: &BStr) -> BString {
res.into()
}
fn write_type<W: Write>(f: &mut W, ty: &Type, role: TypeRole) -> Result<()> {
fn write_type<W: Write>(f: &mut W, ty: &Type) -> Result<()> {
match ty {
Type::Id(id) => write!(f, "{}Id", id)?,
Type::U32 => write!(f, "u32")?,
Type::I32 => write!(f, "i32")?,
Type::Str if role == TypeRole::In => write!(f, "&'a str")?,
Type::Str => write!(f, "String")?,
Type::BStr if role == TypeRole::In => write!(f, "&'a BStr")?,
Type::BStr => write!(f, "BString")?,
Type::Str => write!(f, "&'a str")?,
Type::BStr => write!(f, "&'a BStr")?,
Type::Fixed => write!(f, "Fixed")?,
Type::Fd => write!(f, "Rc<OwnedFd>")?,
Type::Array(n) if role == TypeRole::In => {
Type::Array(n) => {
write!(f, "&'a [")?;
write_type(f, n, role)?;
write_type(f, n)?;
write!(f, "]")?;
},
Type::Array(n) => {
write!(f, "Vec<")?;
write_type(f, n, role)?;
write!(f, ">")?;
}
Type::Pod(p) => f.write_all(p.as_bytes())?,
}
Ok(())
}
fn write_field<W: Write>(f: &mut W, field: &Field, role: TypeRole) -> Result<()> {
fn write_field<W: Write>(f: &mut W, field: &Field) -> Result<()> {
write!(f, " pub {}: ", field.name)?;
write_type(f, &field.ty.val, role)?;
write_type(f, &field.ty.val)?;
writeln!(f, ",")?;
Ok(())
}
#[derive(Copy, Clone, Eq, PartialEq)]
enum TypeRole {
Unified,
In,
Out,
}
fn write_message_type<W: Write>(f: &mut W, obj: &BStr, message: &Message, role: TypeRole) -> Result<()> {
let suffix = match role {
TypeRole::Unified => "",
TypeRole::In => "In",
TypeRole::Out => "Out",
};
let needs_lifetime = role == TypeRole::In;
fn write_message_type<W: Write>(f: &mut W, obj: &BStr, message: &Message, needs_lifetime: bool) -> Result<()> {
let lifetime = if needs_lifetime { "<'a>" } else { "" };
writeln!(f, " pub struct {}{}{} {{", message.camel_name, suffix, lifetime)?;
writeln!(f, " pub struct {}{} {{", message.camel_name, lifetime)?;
writeln!(f, " pub self_id: {}Id,", obj)?;
for field in &message.fields {
write_field(f, &field.val, role)?;
write_field(f, &field.val)?;
}
writeln!(f, " }}")?;
writeln!(f, " impl{} std::fmt::Debug for {}{}{} {{", lifetime, message.camel_name, suffix, lifetime)?;
writeln!(f, " impl{} std::fmt::Debug for {}{} {{", lifetime, message.camel_name, lifetime)?;
writeln!(f, " fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {{")?;
write!(f, r#" write!(fmt, "{}("#, message.name)?;
for (i, field) in message.fields.iter().enumerate() {
@ -577,19 +557,14 @@ fn write_message<W: Write>(f: &mut W, obj: &BStr, message: &Message) -> Result<(
let uppercase = uppercase.as_bstr();
writeln!(f)?;
writeln!(f, " pub const {}: u32 = {};", uppercase, message.id.val)?;
if has_reference_type {
write_message_type(f, obj, message, TypeRole::In)?;
write_message_type(f, obj, message, TypeRole::Out)?;
} else {
write_message_type(f, obj, message, TypeRole::Unified)?;
}
write_message_type(f, obj, message, has_reference_type)?;
let lifetime = if has_reference_type { "<'a>"} else {""};
let parser = if message.fields.len() > 0 {
"parser"
} else {
"_parser"
};
writeln!(f, " impl<'a> RequestParser<'a> for {}{}{} {{", message.camel_name, if has_reference_type { "In" } else { "" }, lifetime)?;
writeln!(f, " impl<'a> RequestParser<'a> for {}{} {{", message.camel_name, lifetime)?;
writeln!(f, " fn parse({}: &mut MsgParser<'_, 'a>) -> Result<Self, MsgParserError> {{", parser)?;
writeln!(f, " Ok(Self {{")?;
writeln!(f, " self_id: {}Id::NONE,", obj)?;
@ -610,7 +585,7 @@ fn write_message<W: Write>(f: &mut W, obj: &BStr, message: &Message) -> Result<(
writeln!(f, " }})")?;
writeln!(f, " }}")?;
writeln!(f, " }}")?;
writeln!(f, " impl EventFormatter for {}{} {{", message.camel_name, if has_reference_type { "Out" } else { "" })?;
writeln!(f, " impl{} EventFormatter for {}{} {{", lifetime, message.camel_name, lifetime)?;
writeln!(f, " fn format(self, fmt: &mut MsgFormatter<'_>) {{")?;
writeln!(f, " fmt.header(self.self_id, {});", uppercase)?;
fn write_fmt_expr<W: Write>(f: &mut W, prefix: &str, ty: &Type, access: &str) -> Result<()> {
@ -625,8 +600,7 @@ fn write_message<W: Write>(f: &mut W, obj: &BStr, message: &Message) -> Result<(
Type::Pod(..) => "binary",
};
let rf = match ty {
Type::Str | Type::BStr | Type::Pod(..) => "&",
Type::Array(..) => "&*",
Type::Pod(..) => "&",
_ => "",
};
writeln!(f, " {}fmt.{}({}{});", prefix, p, rf, access)?;
@ -673,7 +647,7 @@ pub fn main() -> Result<()> {
let mut f = open("wire.rs")?;
writeln!(f, "use std::rc::Rc;")?;
writeln!(f, "use uapi::OwnedFd;")?;
writeln!(f, "use bstr::{{BStr, BString}};")?;
writeln!(f, "use bstr::BStr;")?;
writeln!(f, "use crate::fixed::Fixed;")?;
writeln!(f, "use crate::client::{{EventFormatter, RequestParser}};")?;
writeln!(f, "use crate::object::ObjectId;")?;

View file

@ -258,7 +258,7 @@ impl Client {
}
}
pub fn protocol_error(&self, obj: &dyn Object, code: u32, message: String) {
pub fn protocol_error(&self, obj: &dyn Object, code: u32, message: &str) {
if let Ok(d) = self.display() {
d.send_error(obj.id(), code, message);
}

View file

@ -76,19 +76,19 @@ impl WlDataOffer {
}
pub fn send_offer(self: &Rc<Self>, mime_type: &str) {
self.client.event(OfferOut {
self.client.event(Offer {
self_id: self.id,
mime_type: mime_type.to_string(),
mime_type,
})
}
fn accept(&self, parser: MsgParser<'_, '_>) -> Result<(), AcceptError> {
let _req: AcceptIn = self.client.parse(self, parser)?;
let _req: Accept = self.client.parse(self, parser)?;
Ok(())
}
fn receive(&self, parser: MsgParser<'_, '_>) -> Result<(), ReceiveError> {
let req: ReceiveIn = self.client.parse(self, parser)?;
let req: Receive = self.client.parse(self, parser)?;
if let Some(src) = self.source.get() {
src.send_send(req.mime_type, req.fd);
src.client.flush();

View file

@ -94,15 +94,15 @@ impl WlDataSource {
}
pub fn send_send(&self, mime_type: &str, fd: Rc<OwnedFd>) {
self.client.event(SendOut {
self.client.event(Send {
self_id: self.id,
mime_type: mime_type.to_string(),
mime_type,
fd,
})
}
fn offer(&self, parser: MsgParser<'_, '_>) -> Result<(), OfferError> {
let req: OfferIn = self.client.parse(self, parser)?;
let req: Offer = self.client.parse(self, parser)?;
if self
.mime_types
.borrow_mut()

View file

@ -54,9 +54,9 @@ impl WlDisplay {
&self,
object_id: O,
code: u32,
message: String,
message: &str,
) {
self.client.event(ErrorOut {
self.client.event(Error {
self_id: self.id,
object_id: object_id.into(),
code,
@ -72,16 +72,16 @@ impl WlDisplay {
obj.interface().name(),
request
);
self.send_error(id, INVALID_METHOD, msg)
self.send_error(id, INVALID_METHOD, &msg)
}
pub fn send_invalid_object(self: &Rc<Self>, id: ObjectId) {
let msg = format!("Object {} does not exist", id,);
self.send_error(id, INVALID_OBJECT, msg)
self.send_error(id, INVALID_OBJECT, &msg)
}
pub fn send_implementation_error(self: &Rc<Self>, msg: String) {
self.send_error(WL_DISPLAY_ID, IMPLEMENTATION, msg)
self.send_error(WL_DISPLAY_ID, IMPLEMENTATION, &msg)
}
pub fn send_delete_id(self: &Rc<Self>, id: ObjectId) {

View file

@ -64,9 +64,9 @@ pub struct WlDrm {
impl WlDrm {
fn send_device(self: &Rc<Self>, device: &Rc<CString>) {
self.client.event(DeviceOut {
self.client.event(Device {
self_id: self.id,
name: device.as_bytes().as_bstr().to_owned(),
name: device.as_bytes().as_bstr(),
})
}

View file

@ -150,15 +150,15 @@ pub const SEND_SCALE_SINCE: u32 = 2;
impl WlOutput {
fn send_geometry(&self) {
let event = GeometryOut {
let event = Geometry {
self_id: self.id,
x: 0,
y: 0,
physical_width: self.global.width.get() as _,
physical_height: self.global.height.get() as _,
subpixel: SP_UNKNOWN,
make: "i4".to_string(),
model: "i4".to_string(),
make: "i4",
model: "i4",
transform: TF_NORMAL,
};
self.client.event(event);

View file

@ -23,10 +23,10 @@ impl WlRegistry {
}
pub fn send_global(self: &Rc<Self>, global: &Rc<dyn Global>) {
self.client.event(GlobalOut {
self.client.event(crate::wire::wl_registry::Global {
self_id: self.id,
name: global.name().raw(),
interface: global.interface().name().to_string(),
interface: global.interface().name(),
version: global.version(),
})
}
@ -39,7 +39,7 @@ impl WlRegistry {
}
fn bind(&self, parser: MsgParser<'_, '_>) -> Result<(), BindError> {
let bind: BindIn = self.client.parse(self, parser)?;
let bind: Bind = self.client.parse(self, parser)?;
let global = self.client.state.globals.get(GlobalName::from_raw(bind.name))?;
if global.interface().name() != bind.interface {
return Err(BindError::InvalidInterface(InterfaceError {

View file

@ -35,7 +35,6 @@ pub use handling::NodeSeatState;
use std::cell::{Cell, RefCell};
use std::collections::hash_map::Entry;
use std::io::Write;
use std::ops::Deref;
use std::rc::Rc;
use thiserror::Error;
use uapi::{c, OwnedFd};
@ -312,10 +311,10 @@ impl WlSeat {
})
}
fn send_name(self: &Rc<Self>, name: &Rc<String>) {
self.client.event(NameOut {
fn send_name(self: &Rc<Self>, name: &str) {
self.client.event(Name {
self_id: self.id,
name: name.deref().clone(),
name,
})
}

View file

@ -229,7 +229,7 @@ impl WlSeatGlobal {
let pressed_keys: Vec<_> = self.pressed_keys.borrow().iter().copied().collect();
let serial = self.serial.fetch_add(1);
self.surface_kb_event(0, &surface, |k| {
k.send_enter(serial, surface.id, pressed_keys.clone())
k.send_enter(serial, surface.id, &pressed_keys)
});
let ModifierState {
mods_depressed,

View file

@ -75,9 +75,9 @@ impl WlKeyboard {
self: &Rc<Self>,
serial: u32,
surface: WlSurfaceId,
keys: Vec<u32>,
keys: &[u32],
) {
self.seat.client.event(EnterOut {
self.seat.client.event(Enter {
self_id: self.id,
serial,
surface,

View file

@ -228,7 +228,7 @@ impl XdgSurface {
self.surface.client.protocol_error(
&**self,
ALREADY_CONSTRUCTED,
format!(
&format!(
"wl_surface {} already has an assigned xdg_toplevel",
self.surface.id
),
@ -253,7 +253,7 @@ impl XdgSurface {
self.surface.client.protocol_error(
&**self,
ALREADY_CONSTRUCTED,
format!(
&format!(
"wl_surface {} already has an assigned xdg_popup",
self.surface.id
),

View file

@ -150,11 +150,12 @@ impl XdgToplevel {
}
fn send_configure(&self, width: i32, height: i32) {
self.xdg.surface.client.event(ConfigureOut {
let states: Vec<_> = self.states.borrow().iter().copied().collect();
self.xdg.surface.client.event(Configure {
self_id: self.id,
width,
height,
states: self.states.borrow().iter().copied().collect(),
states: &states,
})
}
@ -185,12 +186,12 @@ impl XdgToplevel {
}
fn set_title(&self, parser: MsgParser<'_, '_>) -> Result<(), SetTitleError> {
let _req: SetTitleIn = self.xdg.surface.client.parse(self, parser)?;
let _req: SetTitle = self.xdg.surface.client.parse(self, parser)?;
Ok(())
}
fn set_app_id(&self, parser: MsgParser<'_, '_>) -> Result<(), SetAppIdError> {
let req: SetAppIdIn = self.xdg.surface.client.parse(self, parser)?;
let req: SetAppId = self.xdg.surface.client.parse(self, parser)?;
self.bugs.set(bugs::get(req.app_id));
Ok(())
}

View file

@ -166,7 +166,7 @@ impl XdgPositioner {
self.client.protocol_error(
self,
INVALID_INPUT,
format!("Cannot set a non-positive size"),
&format!("Cannot set a non-positive size"),
);
return Err(SetSizeError::NonPositiveSize);
}
@ -182,7 +182,7 @@ impl XdgPositioner {
self.client.protocol_error(
self,
INVALID_INPUT,
format!("Cannot set an anchor rect with negative size"),
&format!("Cannot set an anchor rect with negative size"),
);
return Err(SetAnchorRectError::NegativeAnchorRect);
}
@ -248,7 +248,7 @@ impl XdgPositioner {
self.client.protocol_error(
self,
INVALID_INPUT,
format!("Cannot set a negative parent size"),
&format!("Cannot set a negative parent size"),
);
return Err(SetParentSizeError::NegativeParentSize);
}

View file

@ -64,7 +64,7 @@ impl XdgWmBase {
self.client.protocol_error(
self,
DEFUNCT_SURFACES,
format!(
&format!(
"Cannot destroy xdg_wm_base object {} before destroying its surfaces",
self.id
),

View file

@ -56,14 +56,14 @@ impl ZwpPrimarySelectionOfferV1 {
}
pub fn send_offer(self: &Rc<Self>, mime_type: &str) {
self.client.event(OfferOut {
self.client.event(Offer {
self_id: self.id,
mime_type: mime_type.to_string(),
mime_type,
})
}
fn receive(&self, parser: MsgParser<'_, '_>) -> Result<(), ReceiveError> {
let req: ReceiveIn = self.client.parse(self, parser)?;
let req: Receive = self.client.parse(self, parser)?;
if let Some(src) = self.source.get() {
src.send_send(req.mime_type, req.fd);
src.client.flush();

View file

@ -73,15 +73,15 @@ impl ZwpPrimarySelectionSourceV1 {
}
pub fn send_send(&self, mime_type: &str, fd: Rc<OwnedFd>) {
self.client.event(SendOut {
self.client.event(Send {
self_id: self.id,
mime_type: mime_type.to_string(),
mime_type,
fd,
})
}
fn offer(&self, parser: MsgParser<'_, '_>) -> Result<(), OfferError> {
let req: OfferIn = self.client.parse(self, parser)?;
let req: Offer = self.client.parse(self, parser)?;
if self
.mime_types
.borrow_mut()

View file

@ -47,10 +47,6 @@ impl OutBuffer {
pub fn is_full(&self) -> bool {
self.write_pos > BUF_SIZE
}
pub fn len(&self) -> usize {
self.write_pos
}
}
const LIMIT_PENDING: usize = 10;