Skip to content

Commit

Permalink
Update client codegen and example
Browse files Browse the repository at this point in the history
  • Loading branch information
morr0ne committed Aug 17, 2024
1 parent 36c24b5 commit 2798aef
Show file tree
Hide file tree
Showing 4 changed files with 4,405 additions and 2,584 deletions.
71 changes: 49 additions & 22 deletions examples/client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{borrow::Cow, collections::HashMap, os::unix::net::UnixStream, path::Path};
use std::{borrow::Cow, collections::HashMap, os::unix::net::UnixStream, path::Path, sync::Arc};

use anyhow::Result;
use futures_util::{SinkExt, TryStreamExt};
Expand All @@ -8,51 +8,78 @@ use waynest::{
};

struct Client {
socket: Socket,
objects: HashMap<ObjectId, Arc<dyn Dispatcher>>,
}

struct Display {
socket: Socket,
}

impl Client {
pub fn new() -> Result<Self> {
let xdg_runtime_dir = std::env::var("XDG_RUNTIME_DIR")?;
let wayland_connection = std::env::var("WAYLAND_DISPLAY")
.map(Cow::Owned)
.unwrap_or(Cow::Borrowed("wayland-0"));
let mut objects: HashMap<ObjectId, Arc<dyn Dispatcher>> = HashMap::new();

objects.insert(ObjectId::DISPLAY, Arc::new(Display::new()));

let socket_path = Path::new(&xdg_runtime_dir).join(wayland_connection.as_ref());
Ok(Self { objects })
}

let socket = Socket::new(UnixStream::connect(socket_path)?)?;
pub async fn handle_message(&mut self, message: &Message) {
let dispatcher = self
.objects
.get(&message.object_id)
.expect("Invalid object id");

dispatcher.dispatch(message);
}

let objects = HashMap::new();
pub fn display(&self) -> &Display {
let dispatcher = self
.objects
.get(&ObjectId::DISPLAY)
.expect("Invalid object id");

Ok(Self { socket, objects })
dispatcher.downcast_ref().unwrap()
}
}

pub async fn next_message(&mut self) -> Result<Option<Message>, DecodeError> {
self.socket.try_next().await
#[derive(Clone)]
struct Display {
// socket: Socket,
}

impl Display {
pub fn new() -> Self {
Self {}
}
}

impl Dispatcher for Client {
fn socket(&mut self) -> &mut Socket {
&mut self.socket
impl Dispatcher for Display {
fn dispatch(&self, message: &Message) {
todo!()
}
}

impl WlDisplay for Display {}

#[tokio::main]
async fn main() -> Result<()> {
let registry = unsafe { ObjectId::from_raw(2) };

let mut client = Client::new()?;
let xdg_runtime_dir = std::env::var("XDG_RUNTIME_DIR")?;
let wayland_connection = std::env::var("WAYLAND_DISPLAY")
.map(Cow::Owned)
.unwrap_or(Cow::Borrowed("wayland-0"));

let socket_path = Path::new(&xdg_runtime_dir).join(wayland_connection.as_ref());

let mut socket = Socket::new(UnixStream::connect(socket_path)?)?;

let client = Client::new()?;

let display = client.display();

client.get_registry(registry).await?;
display
.get_registry(&mut socket, ObjectId::DISPLAY, registry)
.await?;

while let Some(message) = client.next_message().await? {
while let Some(message) = socket.try_next().await? {
dbg!(message);
}

Expand Down
20 changes: 13 additions & 7 deletions gen/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ pub fn generate_client_code(protocols: &[Protocol]) -> TokenStream {
inner_modules.push(quote! {
#(#docs)*
pub mod #module_name {
use futures_util::SinkExt;

#(#enums)*

#[doc = #trait_docs]
pub trait #trait_name: crate::client::Dispatcher {
pub trait #trait_name {
const INTERFACE: &'static str = #name;
const VERSION: u32 = #version;

Expand Down Expand Up @@ -91,7 +93,11 @@ fn write_requests(
request.name.to_snek_case()
);

let mut args = vec![quote! { &self }];
let mut args = vec![
quote! { &self },
quote! { socket: &mut crate::wire::Socket },
quote! { object_id: crate::wire::ObjectId },
];

for arg in &request.args {
let mut ty =
Expand Down Expand Up @@ -152,17 +158,17 @@ fn write_requests(

requests.push(quote! {
#(#docs)*
async fn #name(#(#args),*) -> crate::server::Result<()> {
tracing::debug!(#tracing_inner, object.id);
async fn #name(#(#args),*) -> crate::client::Result<()> {
tracing::debug!(#tracing_inner, object_id);

let (payload,fds) = crate::wire::PayloadBuilder::new()
#(#build_args)*
.build();

self.socket()
.send_message(crate::wire::Message::new(object.id, #opcode, payload, fds))
socket
.send(crate::wire::Message::new(object_id, #opcode, payload, fds))
.await
.map_err(crate::server::error::Error::IoError)
.map_err(crate::client::Error::IoError)
}
});
}
Expand Down
9 changes: 6 additions & 3 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
mod error;
pub mod protocol;

use downcast_rs::{impl_downcast, DowncastSync};
pub use error::{Error, Result};

use crate::wire::Socket;
use crate::wire::{Message, Socket};

pub trait Dispatcher {
fn socket(&mut self) -> &mut Socket;
pub trait Dispatcher: DowncastSync {
fn dispatch(&self, message: &Message);
}

impl_downcast!(sync Dispatcher);
Loading

0 comments on commit 2798aef

Please sign in to comment.