Libwayland

Struct Libwayland 

Source
pub struct Libwayland { /* private fields */ }
Expand description

A reference to the libwayland-client.so dynamic library.

You can obtain a reference by calling Self::open.

Implementations§

Source§

impl Libwayland

Source

pub fn connect_to_default_display(&'static self) -> Result<Connection>

Connects to the default display.

The default display is usually identified by the WAYLAND_DISPLAY environment variable but falls back to wayland-0 if the environment variable is not set.

§Example
let lib = Libwayland::open().unwrap();
let _con = lib.connect_to_default_display().unwrap();
Examples found in repository?
examples/get-registry/main.rs (line 12)
10fn main() {
11    let lib = Libwayland::open().unwrap();
12    let con = lib.connect_to_default_display().unwrap();
13    let queue = con.create_queue(c"get-registry");
14
15    let (_, snapshot) = get_registry_snapshot(&queue);
16    println!("{:#?}", snapshot);
17}
More examples
Hide additional examples
examples/async-wait/main.rs (line 12)
10async fn main() {
11    let lib = Libwayland::open().unwrap();
12    let con = lib.connect_to_default_display().unwrap();
13    let queue = con.create_local_queue(c"async-wait");
14
15    create_sync(&queue, 1);
16
17    loop {
18        queue.wait_for_events().await.unwrap();
19        queue.dispatch_pending().unwrap();
20    }
21}
examples/simple-window/main.rs (line 8)
6fn main() {
7    let lib = Libwayland::open().unwrap();
8    let con = lib.connect_to_default_display().unwrap();
9    let queue = con.create_local_queue(c"simple-window");
10    let singletons = get_singletons(&queue.display());
11    let simple_window = simple_window::prepare(singletons);
12    while !simple_window.exit.get() {
13        queue.dispatch_blocking().unwrap();
14    }
15}
examples/async-window/main.rs (line 11)
9async fn main() {
10    let lib = Libwayland::open().unwrap();
11    let con = lib.connect_to_default_display().unwrap();
12    let queue = con.create_local_queue(c"async-window");
13    let singletons = get_singletons_async(&queue.display()).await;
14    let simple_window = simple_window::prepare(singletons);
15    while !simple_window.exit.get() {
16        queue.dispatch_async().await.unwrap();
17    }
18}
examples/keyboard-events/main.rs (line 28)
26fn main() {
27    let lib = Libwayland::open().unwrap();
28    let con = lib.connect_to_default_display().unwrap();
29    let queue = con.create_local_queue(c"keyboard-events");
30    let singletons = get_singletons(&queue.display());
31    let simple_window = simple_window::prepare(singletons);
32
33    let wl_registry = queue.display::<WlDisplay>().get_registry();
34    proxy::set_event_handler_local(
35        &wl_registry,
36        RegistryEventHandler {
37            wl_registry: wl_registry.clone(),
38            seats: Default::default(),
39        },
40    );
41
42    while !simple_window.exit.get() {
43        queue.dispatch_blocking().unwrap();
44    }
45}
examples/async-roundtrip/main.rs (line 13)
11async fn main() {
12    let lib = Libwayland::open().unwrap();
13    let con = lib.connect_to_default_display().unwrap();
14    let queue = con.create_local_queue(c"async-roundtrip");
15    let registry = queue.display::<WlDisplay>().get_registry();
16    let num_globals = Cell::new(0);
17    queue
18        .dispatch_scope_async(async |scope| {
19            scope.set_event_handler_local(
20                &registry,
21                WlRegistry::on_global(|_, _, _, _| {
22                    num_globals.set(num_globals.get() + 1);
23                }),
24            );
25            // This function can be used to perform an async roundtrip. It is
26            // compatible with any async runtime. This example also demonstrates
27            // that this works in combination with scoped event handlers.
28            queue.dispatch_roundtrip_async().await.unwrap();
29        })
30        .await;
31    println!("number of globals: {}", num_globals.get());
32}
Source

pub fn connect_to_named_display( &'static self, display: &CStr, ) -> Result<Connection>

Connects to a display with a given name.

The name of the display should usually be of the form wayland-N or it should be the absolute path of a display socket.

§Example
let lib = Libwayland::open().unwrap();
let _con = lib.connect_to_named_display(c"wayland-1").unwrap();
Source

pub fn connect_to_fd(&'static self, fd: OwnedFd) -> Result<Connection>

Consumes an existing socket connected to a wayland compositor.

Unlike Libwayland::connect_to_default_display, this function does not perform any blocking IO.

Source

pub unsafe fn wrap_owned_pointer( &'static self, wl_display: NonNull<wl_display>, ) -> Result<Connection>

Takes ownership of an existing wl_display.

If the display is owned when the last clone of the Connection is dropped, the display will be destroyed. All proxies and queues created from the Connection will contain a clone of the connection to keep the connection alive.

For proxies and queues that already exist at the time this function is called, you must manage the lifetime requirements manually.

§Safety
  • wl_display must be valid and must stay valid for the lifetime of this object and its clones.
  • The display file descriptor must be open and owned by the wl_display.
  • If the display is owned by the time the connection is dropped, all proxies and queues created from this object must have been destroyed before then.
§Example
let lib = Libwayland::open().unwrap();
let wl_display = {
    let con = lib.connect_to_default_display().unwrap();
    con.take_ownership().unwrap()
};
// SAFETY: We took the display from a freshly created Connection so it is valid
//         and has no queues or proxies attached.
let _con = unsafe { lib.wrap_owned_pointer(wl_display) };
Source

pub unsafe fn wrap_borrowed_pointer( &'static self, wl_display: NonNull<wl_display>, ) -> Result<Connection>

Borrows an existing wl_display.

§Safety
  • wl_display must be valid and must stay valid for the lifetime of this object and its clones.
  • The display file descriptor must be open and owned by the wl_display.
§Example
let lib = Libwayland::open().unwrap();
let con = lib.connect_to_default_display().unwrap();
let wl_display = con.wl_display();
{
    // SAFETY: - We took the display from a freshly created Connection so it is valid.
    //         - We drop the connection before dropping the outer connection that
    //           owns the wl_display.
    let _con = unsafe { lib.wrap_borrowed_pointer(wl_display) };
}
Source§

impl Libwayland

Source

pub fn open() -> Result<&'static Self>

Obtains a reference to libwayland-client.so.

Examples found in repository?
examples/get-registry/main.rs (line 11)
10fn main() {
11    let lib = Libwayland::open().unwrap();
12    let con = lib.connect_to_default_display().unwrap();
13    let queue = con.create_queue(c"get-registry");
14
15    let (_, snapshot) = get_registry_snapshot(&queue);
16    println!("{:#?}", snapshot);
17}
More examples
Hide additional examples
examples/async-wait/main.rs (line 11)
10async fn main() {
11    let lib = Libwayland::open().unwrap();
12    let con = lib.connect_to_default_display().unwrap();
13    let queue = con.create_local_queue(c"async-wait");
14
15    create_sync(&queue, 1);
16
17    loop {
18        queue.wait_for_events().await.unwrap();
19        queue.dispatch_pending().unwrap();
20    }
21}
examples/simple-window/main.rs (line 7)
6fn main() {
7    let lib = Libwayland::open().unwrap();
8    let con = lib.connect_to_default_display().unwrap();
9    let queue = con.create_local_queue(c"simple-window");
10    let singletons = get_singletons(&queue.display());
11    let simple_window = simple_window::prepare(singletons);
12    while !simple_window.exit.get() {
13        queue.dispatch_blocking().unwrap();
14    }
15}
examples/async-window/main.rs (line 10)
9async fn main() {
10    let lib = Libwayland::open().unwrap();
11    let con = lib.connect_to_default_display().unwrap();
12    let queue = con.create_local_queue(c"async-window");
13    let singletons = get_singletons_async(&queue.display()).await;
14    let simple_window = simple_window::prepare(singletons);
15    while !simple_window.exit.get() {
16        queue.dispatch_async().await.unwrap();
17    }
18}
examples/keyboard-events/main.rs (line 27)
26fn main() {
27    let lib = Libwayland::open().unwrap();
28    let con = lib.connect_to_default_display().unwrap();
29    let queue = con.create_local_queue(c"keyboard-events");
30    let singletons = get_singletons(&queue.display());
31    let simple_window = simple_window::prepare(singletons);
32
33    let wl_registry = queue.display::<WlDisplay>().get_registry();
34    proxy::set_event_handler_local(
35        &wl_registry,
36        RegistryEventHandler {
37            wl_registry: wl_registry.clone(),
38            seats: Default::default(),
39        },
40    );
41
42    while !simple_window.exit.get() {
43        queue.dispatch_blocking().unwrap();
44    }
45}
examples/async-roundtrip/main.rs (line 12)
11async fn main() {
12    let lib = Libwayland::open().unwrap();
13    let con = lib.connect_to_default_display().unwrap();
14    let queue = con.create_local_queue(c"async-roundtrip");
15    let registry = queue.display::<WlDisplay>().get_registry();
16    let num_globals = Cell::new(0);
17    queue
18        .dispatch_scope_async(async |scope| {
19            scope.set_event_handler_local(
20                &registry,
21                WlRegistry::on_global(|_, _, _, _| {
22                    num_globals.set(num_globals.get() + 1);
23                }),
24            );
25            // This function can be used to perform an async roundtrip. It is
26            // compatible with any async runtime. This example also demonstrates
27            // that this works in combination with scoped event handlers.
28            queue.dispatch_roundtrip_async().await.unwrap();
29        })
30        .await;
31    println!("number of globals: {}", num_globals.get());
32}

Auto Trait Implementations§

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.