Struct Connection

Source
pub struct Connection<D> { /* private fields */ }
Expand description

Wayland connection state.

This struct manages a buffered Wayland socket, keeps track of objects and request/event queues and dispatches object events.

Set WAYLAND_DEBUG=1 environment variable to get debug messages.

Implementations§

Source§

impl<D> Connection<D>

Source

pub fn connect() -> Result<Self, ConnectError>

Connect to a Wayland socket and create a registry.

From the Wayland Book:

To find the Unix socket to connect to, most implementations just do what libwayland does:

  1. If WAYLAND_SOCKET is set, interpret it as a file descriptor number on which the connection is already established, assuming that the parent process configured the connection for us.
  2. If WAYLAND_DISPLAY is set, concat with XDG_RUNTIME_DIR to form the path to the Unix socket.
  3. Assume the socket name is wayland-0 and concat with XDG_RUNTIME_DIR to form the path to the Unix socket.
  4. Give up.

Current implementation follows libwayland except for the step 3.

Examples found in repository?
examples/list_globals.rs (line 4)
3fn main() {
4    let mut conn = Connection::<()>::connect().unwrap();
5    conn.blocking_roundtrip().unwrap();
6
7    for global in conn.globals() {
8        println!("{} v{}", global.interface.to_string_lossy(), global.version);
9    }
10}
More examples
Hide additional examples
examples/output_watcher.rs (line 9)
8fn main() {
9    let mut conn = Connection::connect().unwrap();
10    let mut state = State::default();
11
12    conn.add_registry_cb(wl_registry_cb);
13
14    loop {
15        conn.flush(IoMode::Blocking).unwrap();
16        conn.recv_events(IoMode::Blocking).unwrap();
17        conn.dispatch_events(&mut state);
18    }
19}
examples/output_info.rs (line 8)
7fn main() {
8    let mut conn = Connection::connect().unwrap();
9    conn.blocking_roundtrip().unwrap();
10
11    let mut state = State {
12        outputs: conn
13            .globals()
14            .iter()
15            .filter(|g| g.is::<WlOutput>())
16            .map(|g| g.clone())
17            .collect::<Vec<_>>()
18            .into_iter()
19            .map(|g| g.bind_with_cb(&mut conn, 2..=4, wl_output_cb).unwrap())
20            .map(|output| (output, OutputInfo::default()))
21            .collect(),
22    };
23
24    conn.flush(IoMode::Blocking).unwrap();
25
26    while !state.outputs.iter().all(|x| x.1.done) {
27        conn.recv_events(IoMode::Blocking).unwrap();
28        conn.dispatch_events(&mut state);
29    }
30
31    for (_, output) in state.outputs {
32        dbg!(output);
33    }
34}
Source

pub fn connect_and_collect_globals() -> Result<(Self, Vec<GlobalArgs>), ConnectError>

👎Deprecated: use blocking_roundtrip() + bind_singleton() instead

connect and collect the initial set of advertised globals.

This will empty the event queue, so no callbacks will be called on the received globals.

Source

pub async fn async_connect_and_collect_globals() -> Result<(Self, Vec<GlobalArgs>), ConnectError>

👎Deprecated: use async_roundtrip() + bind_singleton() instead
Available on crate feature tokio only.

Async version of connect_and_collect_globals.

Source

pub fn registry(&self) -> WlRegistry

Get Wayland registry.

At the moment, only a single registry can be created. This might or might not change in the future, considering registries cannot be destroyed.

Source

pub fn globals(&self) -> &[GlobalArgs]

Get a list of available globals.

The order of globals is not specified.

Note that this function has knowledge of all events received from the compositor, even the ones that had not been dispatched in dispatch_events yet.

Examples found in repository?
examples/list_globals.rs (line 7)
3fn main() {
4    let mut conn = Connection::<()>::connect().unwrap();
5    conn.blocking_roundtrip().unwrap();
6
7    for global in conn.globals() {
8        println!("{} v{}", global.interface.to_string_lossy(), global.version);
9    }
10}
More examples
Hide additional examples
examples/output_info.rs (line 13)
7fn main() {
8    let mut conn = Connection::connect().unwrap();
9    conn.blocking_roundtrip().unwrap();
10
11    let mut state = State {
12        outputs: conn
13            .globals()
14            .iter()
15            .filter(|g| g.is::<WlOutput>())
16            .map(|g| g.clone())
17            .collect::<Vec<_>>()
18            .into_iter()
19            .map(|g| g.bind_with_cb(&mut conn, 2..=4, wl_output_cb).unwrap())
20            .map(|output| (output, OutputInfo::default()))
21            .collect(),
22    };
23
24    conn.flush(IoMode::Blocking).unwrap();
25
26    while !state.outputs.iter().all(|x| x.1.done) {
27        conn.recv_events(IoMode::Blocking).unwrap();
28        conn.dispatch_events(&mut state);
29    }
30
31    for (_, output) in state.outputs {
32        dbg!(output);
33    }
34}
Source

pub fn bind_singleton<P: Proxy>( &mut self, version: impl VersionBounds, ) -> Result<P, BindError>

Bind a singleton global.

Use this function to only bind singleton globals. If more than one global of the requeseted interface is available, the behaviour is not specified.

Note that this function has knowledge of all events received from the compositor, even the ones that had not been dispatched in dispatch_events yet.

The version argmuent can be a:

  • Number - require a specific version
  • Range to inclusive (..=b - bind a version in range [1, b])
  • Range inclusive (a..=b - bind a version in range [a, b])
Source

pub fn bind_singleton_with_cb<P: Proxy, F: FnMut(EventCtx<'_, D, P>) + Send + 'static>( &mut self, version: impl VersionBounds, cb: F, ) -> Result<P, BindError>

Same as bind_singleton but also sets the callback

Source

pub fn add_registry_cb<F: FnMut(&mut Connection<D>, &mut D, &Event) + Send + 'static>( &mut self, cb: F, )

Register a registry event callback.

In this library, wl_registry is the only object which can have any number of callbacks, which are triggered in the order in which they were added.

§Panics

This method panics if called from the context of a registry callback.

Examples found in repository?
examples/output_watcher.rs (line 12)
8fn main() {
9    let mut conn = Connection::connect().unwrap();
10    let mut state = State::default();
11
12    conn.add_registry_cb(wl_registry_cb);
13
14    loop {
15        conn.flush(IoMode::Blocking).unwrap();
16        conn.recv_events(IoMode::Blocking).unwrap();
17        conn.dispatch_events(&mut state);
18    }
19}
Source

pub fn set_callback_for<P: Proxy, F: FnMut(EventCtx<'_, D, P>) + Send + 'static>( &mut self, proxy: P, cb: F, )

Set a callback for a given object.

§Panics

This method panics if current set of objects does not contain an object with id identical to proxy.id(), internally stored object differs from proxy or object is dead.

It also panics if proxy is a wl_registry. Use add_registry_cb to listen to registry events.

Calling this function on a destroyed object will most likely panic, but this is not guarantied due to id-reuse.

Source

pub fn clear_callbacks<D2>(self) -> Connection<D2>

👎Deprecated: this function is error-prone and best avoided

Remove all callbacks.

You can use this function to change the “state type” of a connection.

Source

pub fn blocking_roundtrip(&mut self) -> Result<()>

Perform a blocking roundtrip.

This function flushes the buffer of pending requests. All received events during the roundtrip are queued.

Examples found in repository?
examples/list_globals.rs (line 5)
3fn main() {
4    let mut conn = Connection::<()>::connect().unwrap();
5    conn.blocking_roundtrip().unwrap();
6
7    for global in conn.globals() {
8        println!("{} v{}", global.interface.to_string_lossy(), global.version);
9    }
10}
More examples
Hide additional examples
examples/output_info.rs (line 9)
7fn main() {
8    let mut conn = Connection::connect().unwrap();
9    conn.blocking_roundtrip().unwrap();
10
11    let mut state = State {
12        outputs: conn
13            .globals()
14            .iter()
15            .filter(|g| g.is::<WlOutput>())
16            .map(|g| g.clone())
17            .collect::<Vec<_>>()
18            .into_iter()
19            .map(|g| g.bind_with_cb(&mut conn, 2..=4, wl_output_cb).unwrap())
20            .map(|output| (output, OutputInfo::default()))
21            .collect(),
22    };
23
24    conn.flush(IoMode::Blocking).unwrap();
25
26    while !state.outputs.iter().all(|x| x.1.done) {
27        conn.recv_events(IoMode::Blocking).unwrap();
28        conn.dispatch_events(&mut state);
29    }
30
31    for (_, output) in state.outputs {
32        dbg!(output);
33    }
34}
Source

pub async fn async_roundtrip(&mut self) -> Result<()>

Available on crate feature tokio only.

Async version of blocking_roundtrip.

Source

pub fn recv_events(&mut self, mode: IoMode) -> Result<()>

Receive events from Wayland socket.

If mode is Blocking, this function will block the current thread until at least one event is read.

If mode is NonBlocking, this function will read form the socket until reading would block. If at least one event was received, Ok will be returned. Otherwise, WouldBlock will be propagated.

Regular IO errors are propagated as usual.

Examples found in repository?
examples/output_watcher.rs (line 16)
8fn main() {
9    let mut conn = Connection::connect().unwrap();
10    let mut state = State::default();
11
12    conn.add_registry_cb(wl_registry_cb);
13
14    loop {
15        conn.flush(IoMode::Blocking).unwrap();
16        conn.recv_events(IoMode::Blocking).unwrap();
17        conn.dispatch_events(&mut state);
18    }
19}
More examples
Hide additional examples
examples/output_info.rs (line 27)
7fn main() {
8    let mut conn = Connection::connect().unwrap();
9    conn.blocking_roundtrip().unwrap();
10
11    let mut state = State {
12        outputs: conn
13            .globals()
14            .iter()
15            .filter(|g| g.is::<WlOutput>())
16            .map(|g| g.clone())
17            .collect::<Vec<_>>()
18            .into_iter()
19            .map(|g| g.bind_with_cb(&mut conn, 2..=4, wl_output_cb).unwrap())
20            .map(|output| (output, OutputInfo::default()))
21            .collect(),
22    };
23
24    conn.flush(IoMode::Blocking).unwrap();
25
26    while !state.outputs.iter().all(|x| x.1.done) {
27        conn.recv_events(IoMode::Blocking).unwrap();
28        conn.dispatch_events(&mut state);
29    }
30
31    for (_, output) in state.outputs {
32        dbg!(output);
33    }
34}
Source

pub async fn async_recv_events(&mut self) -> Result<()>

Available on crate feature tokio only.

Async version of recv_events.

Source

pub fn flush(&mut self, mode: IoMode) -> Result<()>

Send the queue of pending request to the server.

Examples found in repository?
examples/output_watcher.rs (line 15)
8fn main() {
9    let mut conn = Connection::connect().unwrap();
10    let mut state = State::default();
11
12    conn.add_registry_cb(wl_registry_cb);
13
14    loop {
15        conn.flush(IoMode::Blocking).unwrap();
16        conn.recv_events(IoMode::Blocking).unwrap();
17        conn.dispatch_events(&mut state);
18    }
19}
More examples
Hide additional examples
examples/output_info.rs (line 24)
7fn main() {
8    let mut conn = Connection::connect().unwrap();
9    conn.blocking_roundtrip().unwrap();
10
11    let mut state = State {
12        outputs: conn
13            .globals()
14            .iter()
15            .filter(|g| g.is::<WlOutput>())
16            .map(|g| g.clone())
17            .collect::<Vec<_>>()
18            .into_iter()
19            .map(|g| g.bind_with_cb(&mut conn, 2..=4, wl_output_cb).unwrap())
20            .map(|output| (output, OutputInfo::default()))
21            .collect(),
22    };
23
24    conn.flush(IoMode::Blocking).unwrap();
25
26    while !state.outputs.iter().all(|x| x.1.done) {
27        conn.recv_events(IoMode::Blocking).unwrap();
28        conn.dispatch_events(&mut state);
29    }
30
31    for (_, output) in state.outputs {
32        dbg!(output);
33    }
34}
Source

pub async fn async_flush(&mut self) -> Result<()>

Available on crate feature tokio only.

Async version of flush.

Source

pub fn dispatch_events(&mut self, state: &mut D)

Empty the queue of pending events, calling a callback (if set) for each event.

§Panics

This method panics if called from the context of a callback.

Examples found in repository?
examples/output_watcher.rs (line 17)
8fn main() {
9    let mut conn = Connection::connect().unwrap();
10    let mut state = State::default();
11
12    conn.add_registry_cb(wl_registry_cb);
13
14    loop {
15        conn.flush(IoMode::Blocking).unwrap();
16        conn.recv_events(IoMode::Blocking).unwrap();
17        conn.dispatch_events(&mut state);
18    }
19}
More examples
Hide additional examples
examples/output_info.rs (line 28)
7fn main() {
8    let mut conn = Connection::connect().unwrap();
9    conn.blocking_roundtrip().unwrap();
10
11    let mut state = State {
12        outputs: conn
13            .globals()
14            .iter()
15            .filter(|g| g.is::<WlOutput>())
16            .map(|g| g.clone())
17            .collect::<Vec<_>>()
18            .into_iter()
19            .map(|g| g.bind_with_cb(&mut conn, 2..=4, wl_output_cb).unwrap())
20            .map(|output| (output, OutputInfo::default()))
21            .collect(),
22    };
23
24    conn.flush(IoMode::Blocking).unwrap();
25
26    while !state.outputs.iter().all(|x| x.1.done) {
27        conn.recv_events(IoMode::Blocking).unwrap();
28        conn.dispatch_events(&mut state);
29    }
30
31    for (_, output) in state.outputs {
32        dbg!(output);
33    }
34}
Source

pub fn break_dispatch_loop(&mut self)

Call this function from a callback to break the dispatch loop.

This will cause dispatch_events to return. Events that go after current event are left in the queue.

Trait Implementations§

Source§

impl<D> AsRawFd for Connection<D>

Source§

fn as_raw_fd(&self) -> RawFd

Extracts the raw file descriptor. Read more

Auto Trait Implementations§

§

impl<D> Freeze for Connection<D>

§

impl<D> !RefUnwindSafe for Connection<D>

§

impl<D> Send for Connection<D>

§

impl<D> !Sync for Connection<D>

§

impl<D> Unpin for Connection<D>

§

impl<D> !UnwindSafe for Connection<D>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.