pub struct QueueWithData<T>where
T: 'static,{ /* private fields */ }Expand description
An adapter for Queues with mutable data.
This type is returned by Connection::create_queue_with_data and
Connection::create_local_queue_with_data and can also be created from any queue
by calling Queue::with_data.
This type must be used to dispatch queues that were created with one of the two
functions above. It derefs to Queue but re-declares all of the dispatching
functions to also accept a &mut T that will be passed to the event handlers.
§Example
let lib = Libwayland::open().unwrap();
let con = lib.connect_to_default_display().unwrap();
let (_queue, queue) = con.create_queue_with_data::<State>(c"queue name");
struct State {
done: bool,
}
let mut state = State {
done: false,
};
let sync = queue.display::<WlDisplay>().sync();
proxy::set_event_handler(&sync, WlCallback::on_done(|state: &mut State, _, _| {
state.done = true;
}));
queue.dispatch_roundtrip_blocking(&mut state).unwrap();
assert!(state.done);Implementations§
Source§impl<T> QueueWithData<T>where
T: 'static,
impl<T> QueueWithData<T>where
T: 'static,
Sourcepub fn dispatch_blocking(&self, data: &mut T) -> Result<u64>
pub fn dispatch_blocking(&self, data: &mut T) -> Result<u64>
Blocks the current thread until at least one event has been dispatched.
This function is the same as Queue::dispatch_blocking but accepts a &mut T
that will be passed to event handlers.
§Panic
- Panics if this is a local queue and the current thread is not the thread that this queue was created in.
§Example
let lib = Libwayland::open().unwrap();
let con = lib.connect_to_default_display().unwrap();
let (_queue, queue) = con.create_queue_with_data::<State>(c"queue name");
struct State {
// ...
}
let mut state = State {
// ...
};
// For this example, ensure that the compositor sends an event in the near future.
let _sync = queue.display::<WlDisplay>().sync();
queue.dispatch_blocking(&mut state).unwrap();Sourcepub async fn dispatch_async(&self, data: &mut T) -> Result<u64>
pub async fn dispatch_async(&self, data: &mut T) -> Result<u64>
Completes when at least one event has been dispatched.
This function is the same as QueueWithData::dispatch_blocking except that it is
async and does not block the current thread.
§Panic
- Panics if this is a local queue and the thread polling the future is not the thread that this queue was created in.
§Example
let lib = Libwayland::open().unwrap();
let con = lib.connect_to_default_display().unwrap();
let (_queue, queue) = con.create_queue_with_data(c"queue name");
struct State {
// ...
}
let mut state = State {
// ...
};
// For this example, ensure that the compositor sends an event in the near future.
let _sync = queue.display::<WlDisplay>().sync();
queue.dispatch_async(&mut state).await.unwrap();Sourcepub fn dispatch_roundtrip_blocking(&self, data: &mut T) -> Result<()>
pub fn dispatch_roundtrip_blocking(&self, data: &mut T) -> Result<()>
Blocks the current thread until the compositor has processed all previous requests and all of its response events have been dispatched.
This function is the same as Queue::dispatch_roundtrip_blocking but accepts a
&mut T that will be passed to event handlers.
§Panic
- Panics if this is a local queue and the current thread is not the thread that this queue was created in.
§Example
let lib = Libwayland::open().unwrap();
let con = lib.connect_to_default_display().unwrap();
let (_queue, queue) = con.create_queue_with_data::<State>(c"");
let display: WlDisplay = queue.display();
struct State {
done: bool,
}
let mut state = State {
done: false,
};
// send some messages to the compositor
let sync = display.sync();
proxy::set_event_handler(&sync, WlCallback::on_done(move |state: &mut State, _, _| {
state.done = true;
}));
// perform a roundtrip
queue.dispatch_roundtrip_blocking(&mut state).unwrap();
// assert that we've received the response
assert!(state.done);Examples found in repository?
22fn main() {
23 let lib = Libwayland::open().unwrap();
24 let con = lib.connect_to_default_display().unwrap();
25 let (_queue, queue) = con.create_queue_with_data::<State>(c"get-registry");
26
27 // Create a new registry that will receive the globals and can later be used to
28 // bind them.
29 let mut state = State {
30 registry: queue.display::<WlDisplay>().get_registry(),
31 globals: vec![],
32 };
33
34 // Since we only want to create a snapshot, we don't care about
35 // global_remove events. This allows us to use the functional event handler
36 // form.
37 proxy::set_event_handler(
38 &state.registry,
39 WlRegistry::on_global(|state: &mut State, _, name, interface, version| {
40 state.globals.push(Global {
41 name,
42 interface: interface.to_string(),
43 version,
44 });
45 }),
46 );
47 queue.dispatch_roundtrip_blocking(&mut state).unwrap();
48
49 println!("{:#?}", state.globals);
50}Sourcepub async fn dispatch_roundtrip_async(&self, data: &mut T) -> Result<()>
pub async fn dispatch_roundtrip_async(&self, data: &mut T) -> Result<()>
Completes when the compositor has processed all previous requests and all of its response events have been dispatched.
This function is the same as QueueWithData::dispatch_roundtrip_blocking except
that it is async and does not block the current thread.
If the future completes with Ok(()), then the future completes after (in the
sense of the C++ memory model) the event handlers of all previous events have been
invoked.
§Panic
- Panics if this is a local queue and the current thread is not the thread that this queue was created in.
§Example
let lib = Libwayland::open().unwrap();
let con = lib.connect_to_default_display().unwrap();
let (_queue, queue) = con.create_queue_with_data::<State>(c"queue name");
let display: WlDisplay = queue.display();
struct State {
done: bool,
}
let mut state = State {
done: false,
};
// send some messages to the compositor
let sync = display.sync();
proxy::set_event_handler(&sync, WlCallback::on_done(move |state: &mut State, _, _| {
state.done = true;
}));
// perform a roundtrip
queue.dispatch_roundtrip_async(&mut state).await.unwrap();
// assert that we've received the response
assert!(state.done);Sourcepub fn dispatch_pending(&self, data: &mut T) -> Result<u64>
pub fn dispatch_pending(&self, data: &mut T) -> Result<u64>
Dispatches enqueued events.
This function is the same as Queue::dispatch_pending but accepts a &mut T
that will be passed to event handlers.
§Panic
- Panics if this is a local queue and the current thread is not the thread that this queue was created in.
§Example
let lib = Libwayland::open().unwrap();
let con = lib.connect_to_default_display().unwrap();
let (_queue, queue) = con.create_queue_with_data(c"queue name");
let display: WlDisplay = queue.display();
struct State {
done: bool,
}
let mut state = State {
done: false,
};
let sync = display.sync();
proxy::set_event_handler(&sync, WlCallback::on_done(move |state: &mut State, _, _| {
state.done = true;
}));
while !state.done {
queue.wait_for_events().await.unwrap();
// Dispatch the events.
queue.dispatch_pending(&mut state).unwrap();
}Methods from Deref<Target = Queue>§
Sourcepub fn dispatch_scope_blocking<'env, T, F>(&self, f: F) -> T
pub fn dispatch_scope_blocking<'env, T, F>(&self, f: F) -> T
Creates a blocking scope for event handlers with shorter than 'static lifetime.
The scope can be used to attach event handlers to proxies. The following
restriction applies: Such event handlers will only be invoked while inside this
function. Once this function returns, the OwnedProxy::NO_OP_EVENT_HANDLER of
the proxy is invoked instead.
§Panic
Panics if this is a local queue and the current thread is not the thread that this queue was created in.
§Example
let lib = Libwayland::open().unwrap();
let con = lib.connect_to_default_display().unwrap();
let queue = con.create_local_queue(c"queue name");
let sync = queue.display::<WlDisplay>().sync();
let done = Cell::new(false);
queue.dispatch_scope_blocking(|scope| {
scope.set_event_handler_local(&sync, WlCallback::on_done(|_, _| done.set(true)));
queue.dispatch_roundtrip_blocking().unwrap();
});
assert!(done.get());Examples found in repository?
56pub fn get_singletons(display: &WlDisplay) -> Singletons {
57 let map = Mutex::new(HashMap::new());
58
59 let queue = proxy::queue(display);
60 let wl_registry = display.get_registry();
61
62 queue.dispatch_scope_blocking(|scope| {
63 scope.set_event_handler(
64 &wl_registry,
65 WlRegistry::on_global(|_, name, interface, version| {
66 map.lock().insert(interface.to_owned(), (name, version));
67 }),
68 );
69 queue.dispatch_roundtrip_blocking().unwrap();
70 });
71
72 Singletons {
73 wl_registry,
74 map: map.into_inner(),
75 }
76}More examples
27fn get_registry_snapshot(queue: &Queue) -> (WlRegistry, Vec<Global>) {
28 // Create a new registry that will receive the globals and can later be used to
29 // bind them.
30 let registry = queue.display::<WlDisplay>().get_registry();
31 let globals = Mutex::new(vec![]);
32 // Since we don't care about registry events after this function returns, we can
33 // use a dispatch scope. The event handlers in this scope will not be called after
34 // the function returns.
35 queue.dispatch_scope_blocking(|scope| {
36 scope.set_event_handler(
37 ®istry,
38 // Since we only want to create a snapshot, we don't care about
39 // global_remove events. This allows us to use the functional event handler
40 // form.
41 WlRegistry::on_global(|_, name, interface, version| {
42 globals.lock().push(Global {
43 name,
44 interface: interface.to_string(),
45 version,
46 });
47 }),
48 );
49 queue.dispatch_roundtrip_blocking().unwrap();
50 });
51 // The event handler will no longer be called after this function returns but
52 // the registry can still be used to bind globals.
53 (registry, globals.into_inner())
54}Sourcepub async fn dispatch_scope_async<'env, T, F>(&self, f: F) -> Twhere
F: for<'scope> AsyncFnOnce(&'scope Scope<'scope, 'env>) -> T,
pub async fn dispatch_scope_async<'env, T, F>(&self, f: F) -> Twhere
F: for<'scope> AsyncFnOnce(&'scope Scope<'scope, 'env>) -> T,
Creates an async scope for event handlers with shorter than 'static lifetime.
The scope can be used to attach event handlers to proxies. The following
restriction applies: Such event handlers will only be invoked while the future is
being polled. If an event needs to be dispatched in any other situation, the
OwnedProxy::NO_OP_EVENT_HANDLER of the proxy is invoked instead.
In particular, dispatching the queue from a outside this future while this future exists is unlikely to have the desired effect.
§Panic
Panics if this is a local queue and the thread polling the future is not the thread that this queue was created in.
§Example
let lib = Libwayland::open().unwrap();
let con = lib.connect_to_default_display().unwrap();
let queue = con.create_local_queue(c"queue name");
let sync = queue.display::<WlDisplay>().sync();
let done = Cell::new(false);
queue.dispatch_scope_async(async |scope| {
scope.set_event_handler_local(&sync, WlCallback::on_done(|_, _| done.set(true)));
queue.dispatch_roundtrip_async().await.unwrap();
}).await;
assert!(done.get());Examples found in repository?
78pub async fn get_singletons_async(display: &WlDisplay) -> Singletons {
79 let map = Mutex::new(HashMap::new());
80
81 let queue = proxy::queue(display);
82 let wl_registry = display.get_registry();
83
84 queue
85 .dispatch_scope_async(async |scope| {
86 scope.set_event_handler(
87 &wl_registry,
88 WlRegistry::on_global(|_, name, interface, version| {
89 map.lock().insert(interface.to_owned(), (name, version));
90 }),
91 );
92 queue.dispatch_roundtrip_async().await.unwrap();
93 })
94 .await;
95
96 Singletons {
97 wl_registry,
98 map: map.into_inner(),
99 }
100}More examples
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 ®istry,
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}Sourcepub fn with_data<T>(&self) -> QueueWithData<T>where
T: 'static,
pub fn with_data<T>(&self) -> QueueWithData<T>where
T: 'static,
Creates an adapter for queues with mutable data.
If the queue was created with Connection::create_queue_with_data or
Connection::create_local_queue_with_data, then this function can only be used
with the same T that was used in those function calls.
For convenience, if this queue was created without data, this function can be
used with any T.
§Panic
This function panics if this queue
- was created with
Connection::create_queue_with_dataorConnection::create_local_queue_with_data, and - it was created with a different data type.
Sourcepub fn libwayland(&self) -> &'static Libwayland
pub fn libwayland(&self) -> &'static Libwayland
Returns a reference to the Libwayland singleton.
Examples found in repository?
347 unsafe fn handle_event(
348 &self,
349 queue: &Queue,
350 data: *mut u8,
351 slf: &UntypedBorrowedProxy,
352 opcode: u32,
353 args: *mut wl_argument,
354 ) {
355 // SAFETY: This function requires that slf has the interface INTERFACE
356 let slf = unsafe { proxy::low_level::from_untyped_borrowed::<WlDisplayRef>(slf) };
357 match opcode {
358 0 => {
359 // SAFETY: INTERFACE requires that there are 3 arguments
360 let args = unsafe { &*args.cast::<[wl_argument; 3]>() };
361 // SAFETY: - INTERFACE requires that args[0] contains an object
362 let arg0 = unsafe {
363 if let Some(p) = NonNull::new(args[0].o.cast()) {
364 Some(UntypedBorrowedProxy::new_immutable(queue.libwayland(), p))
365 } else {
366 None
367 }
368 };
369 let arg0 = arg0.as_ref();
370 // SAFETY: - INTERFACE requires that args[1] contains a uint
371 let arg1 = unsafe { args[1].u };
372 // SAFETY: - INTERFACE requires that args[2] contains a string
373 // - if the pointer is not null, then it is a c string
374 let arg2 = unsafe { convert_string_arg("wl_display", "message", args[2].s) };
375 self.0.error(slf, arg0, arg1, arg2);
376 }
377 1 => {
378 // SAFETY: INTERFACE requires that there are 1 arguments
379 let args = unsafe { &*args.cast::<[wl_argument; 1]>() };
380 // SAFETY: - INTERFACE requires that args[0] contains a uint
381 let arg0 = unsafe { args[0].u };
382 self.0.delete_id(slf, arg0);
383 }
384 _ => {
385 invalid_opcode("wl_display", opcode);
386 }
387 }
388 }More examples
360 unsafe fn handle_event(
361 &self,
362 queue: &Queue,
363 data: *mut u8,
364 slf: &UntypedBorrowedProxy,
365 opcode: u32,
366 args: *mut wl_argument,
367 ) {
368 // SAFETY: This function requires that slf has the interface INTERFACE
369 let slf = unsafe { proxy::low_level::from_untyped_borrowed::<WlDisplayRef>(slf) };
370 // SAFETY: This function requires that data is `&mut T` where `T`
371 // has the type id returned by `Self::mutable_type`, i.e.,
372 // `T = H::Data`.
373 let data: &mut H::Data = unsafe { &mut *data.cast() };
374 match opcode {
375 0 => {
376 // SAFETY: INTERFACE requires that there are 3 arguments
377 let args = unsafe { &*args.cast::<[wl_argument; 3]>() };
378 // SAFETY: - INTERFACE requires that args[0] contains an object
379 let arg0 = unsafe {
380 if let Some(p) = NonNull::new(args[0].o.cast()) {
381 Some(UntypedBorrowedProxy::new_immutable(queue.libwayland(), p))
382 } else {
383 None
384 }
385 };
386 let arg0 = arg0.as_ref();
387 // SAFETY: - INTERFACE requires that args[1] contains a uint
388 let arg1 = unsafe { args[1].u };
389 // SAFETY: - INTERFACE requires that args[2] contains a string
390 // - if the pointer is not null, then it is a c string
391 let arg2 = unsafe { convert_string_arg("wl_display", "message", args[2].s) };
392 self.0.error(data, slf, arg0, arg1, arg2);
393 }
394 1 => {
395 // SAFETY: INTERFACE requires that there are 1 arguments
396 let args = unsafe { &*args.cast::<[wl_argument; 1]>() };
397 // SAFETY: - INTERFACE requires that args[0] contains a uint
398 let arg0 = unsafe { args[0].u };
399 self.0.delete_id(data, slf, arg0);
400 }
401 _ => {
402 invalid_opcode("wl_display", opcode);
403 }
404 }
405 }968 unsafe fn handle_event(
969 &self,
970 queue: &Queue,
971 data: *mut u8,
972 slf: &UntypedBorrowedProxy,
973 opcode: u32,
974 args: *mut wl_argument,
975 ) {
976 // SAFETY: This function requires that slf has the interface INTERFACE
977 let slf = unsafe { proxy::low_level::from_untyped_borrowed::<WlSurfaceRef>(slf) };
978 match opcode {
979 0 => {
980 // SAFETY: INTERFACE requires that there are 1 arguments
981 let args = unsafe { &*args.cast::<[wl_argument; 1]>() };
982 // SAFETY: - INTERFACE requires that args[0] contains an object
983 let arg0 = unsafe {
984 if let Some(p) = NonNull::new(args[0].o.cast()) {
985 Some(UntypedBorrowedProxy::new_immutable(queue.libwayland(), p))
986 } else {
987 None
988 }
989 };
990 // SAFETY: - INTERFACE requires that the object has the interface WlOutput::WL_INTERFACE
991 let arg0 = arg0.as_ref().map(|arg0| unsafe {
992 proxy::low_level::from_untyped_borrowed::<WlOutputRef>(arg0)
993 });
994 self.0.enter(slf, arg0);
995 }
996 1 => {
997 // SAFETY: INTERFACE requires that there are 1 arguments
998 let args = unsafe { &*args.cast::<[wl_argument; 1]>() };
999 // SAFETY: - INTERFACE requires that args[0] contains an object
1000 let arg0 = unsafe {
1001 if let Some(p) = NonNull::new(args[0].o.cast()) {
1002 Some(UntypedBorrowedProxy::new_immutable(queue.libwayland(), p))
1003 } else {
1004 None
1005 }
1006 };
1007 // SAFETY: - INTERFACE requires that the object has the interface WlOutput::WL_INTERFACE
1008 let arg0 = arg0.as_ref().map(|arg0| unsafe {
1009 proxy::low_level::from_untyped_borrowed::<WlOutputRef>(arg0)
1010 });
1011 self.0.leave(slf, arg0);
1012 }
1013 2 => {
1014 // SAFETY: INTERFACE requires that there are 1 arguments
1015 let args = unsafe { &*args.cast::<[wl_argument; 1]>() };
1016 // SAFETY: - INTERFACE requires that args[0] contains an int
1017 let arg0 = unsafe { args[0].i };
1018 self.0.preferred_buffer_scale(slf, arg0);
1019 }
1020 3 => {
1021 // SAFETY: INTERFACE requires that there are 1 arguments
1022 let args = unsafe { &*args.cast::<[wl_argument; 1]>() };
1023 // SAFETY: - INTERFACE requires that args[0] contains a uint
1024 let arg0 = unsafe { WlOutputTransform(args[0].u) };
1025 self.0.preferred_buffer_transform(slf, arg0);
1026 }
1027 _ => {
1028 invalid_opcode("wl_surface", opcode);
1029 }
1030 }
1031 }985 unsafe fn handle_event(
986 &self,
987 queue: &Queue,
988 data: *mut u8,
989 slf: &UntypedBorrowedProxy,
990 opcode: u32,
991 args: *mut wl_argument,
992 ) {
993 // SAFETY: This function requires that slf has the interface INTERFACE
994 let slf = unsafe { proxy::low_level::from_untyped_borrowed::<WlSurfaceRef>(slf) };
995 // SAFETY: This function requires that data is `&mut T` where `T`
996 // has the type id returned by `Self::mutable_type`, i.e.,
997 // `T = H::Data`.
998 let data: &mut H::Data = unsafe { &mut *data.cast() };
999 match opcode {
1000 0 => {
1001 // SAFETY: INTERFACE requires that there are 1 arguments
1002 let args = unsafe { &*args.cast::<[wl_argument; 1]>() };
1003 // SAFETY: - INTERFACE requires that args[0] contains an object
1004 let arg0 = unsafe {
1005 if let Some(p) = NonNull::new(args[0].o.cast()) {
1006 Some(UntypedBorrowedProxy::new_immutable(queue.libwayland(), p))
1007 } else {
1008 None
1009 }
1010 };
1011 // SAFETY: - INTERFACE requires that the object has the interface WlOutput::WL_INTERFACE
1012 let arg0 = arg0.as_ref().map(|arg0| unsafe {
1013 proxy::low_level::from_untyped_borrowed::<WlOutputRef>(arg0)
1014 });
1015 self.0.enter(data, slf, arg0);
1016 }
1017 1 => {
1018 // SAFETY: INTERFACE requires that there are 1 arguments
1019 let args = unsafe { &*args.cast::<[wl_argument; 1]>() };
1020 // SAFETY: - INTERFACE requires that args[0] contains an object
1021 let arg0 = unsafe {
1022 if let Some(p) = NonNull::new(args[0].o.cast()) {
1023 Some(UntypedBorrowedProxy::new_immutable(queue.libwayland(), p))
1024 } else {
1025 None
1026 }
1027 };
1028 // SAFETY: - INTERFACE requires that the object has the interface WlOutput::WL_INTERFACE
1029 let arg0 = arg0.as_ref().map(|arg0| unsafe {
1030 proxy::low_level::from_untyped_borrowed::<WlOutputRef>(arg0)
1031 });
1032 self.0.leave(data, slf, arg0);
1033 }
1034 2 => {
1035 // SAFETY: INTERFACE requires that there are 1 arguments
1036 let args = unsafe { &*args.cast::<[wl_argument; 1]>() };
1037 // SAFETY: - INTERFACE requires that args[0] contains an int
1038 let arg0 = unsafe { args[0].i };
1039 self.0.preferred_buffer_scale(data, slf, arg0);
1040 }
1041 3 => {
1042 // SAFETY: INTERFACE requires that there are 1 arguments
1043 let args = unsafe { &*args.cast::<[wl_argument; 1]>() };
1044 // SAFETY: - INTERFACE requires that args[0] contains a uint
1045 let arg0 = unsafe { WlOutputTransform(args[0].u) };
1046 self.0.preferred_buffer_transform(data, slf, arg0);
1047 }
1048 _ => {
1049 invalid_opcode("wl_surface", opcode);
1050 }
1051 }
1052 }419 unsafe fn handle_event(
420 &self,
421 queue: &Queue,
422 data: *mut u8,
423 slf: &UntypedBorrowedProxy,
424 opcode: u32,
425 args: *mut wl_argument,
426 ) {
427 // SAFETY: This function requires that slf has the interface INTERFACE
428 let slf = unsafe { proxy::low_level::from_untyped_borrowed::<WlTouchRef>(slf) };
429 match opcode {
430 0 => {
431 // SAFETY: INTERFACE requires that there are 6 arguments
432 let args = unsafe { &*args.cast::<[wl_argument; 6]>() };
433 // SAFETY: - INTERFACE requires that args[0] contains a uint
434 let arg0 = unsafe { args[0].u };
435 // SAFETY: - INTERFACE requires that args[1] contains a uint
436 let arg1 = unsafe { args[1].u };
437 // SAFETY: - INTERFACE requires that args[2] contains an object
438 let arg2 = unsafe {
439 if let Some(p) = NonNull::new(args[2].o.cast()) {
440 Some(UntypedBorrowedProxy::new_immutable(queue.libwayland(), p))
441 } else {
442 None
443 }
444 };
445 // SAFETY: - INTERFACE requires that the object has the interface WlSurface::WL_INTERFACE
446 let arg2 = arg2.as_ref().map(|arg2| unsafe {
447 proxy::low_level::from_untyped_borrowed::<WlSurfaceRef>(arg2)
448 });
449 // SAFETY: - INTERFACE requires that args[3] contains an int
450 let arg3 = unsafe { args[3].i };
451 // SAFETY: - INTERFACE requires that args[4] contains a fixed
452 let arg4 = unsafe { Fixed::from_wire(args[4].f) };
453 // SAFETY: - INTERFACE requires that args[5] contains a fixed
454 let arg5 = unsafe { Fixed::from_wire(args[5].f) };
455 self.0.down(slf, arg0, arg1, arg2, arg3, arg4, arg5);
456 }
457 1 => {
458 // SAFETY: INTERFACE requires that there are 3 arguments
459 let args = unsafe { &*args.cast::<[wl_argument; 3]>() };
460 // SAFETY: - INTERFACE requires that args[0] contains a uint
461 let arg0 = unsafe { args[0].u };
462 // SAFETY: - INTERFACE requires that args[1] contains a uint
463 let arg1 = unsafe { args[1].u };
464 // SAFETY: - INTERFACE requires that args[2] contains an int
465 let arg2 = unsafe { args[2].i };
466 self.0.up(slf, arg0, arg1, arg2);
467 }
468 2 => {
469 // SAFETY: INTERFACE requires that there are 4 arguments
470 let args = unsafe { &*args.cast::<[wl_argument; 4]>() };
471 // SAFETY: - INTERFACE requires that args[0] contains a uint
472 let arg0 = unsafe { args[0].u };
473 // SAFETY: - INTERFACE requires that args[1] contains an int
474 let arg1 = unsafe { args[1].i };
475 // SAFETY: - INTERFACE requires that args[2] contains a fixed
476 let arg2 = unsafe { Fixed::from_wire(args[2].f) };
477 // SAFETY: - INTERFACE requires that args[3] contains a fixed
478 let arg3 = unsafe { Fixed::from_wire(args[3].f) };
479 self.0.motion(slf, arg0, arg1, arg2, arg3);
480 }
481 3 => {
482 self.0.frame(slf);
483 }
484 4 => {
485 self.0.cancel(slf);
486 }
487 5 => {
488 // SAFETY: INTERFACE requires that there are 3 arguments
489 let args = unsafe { &*args.cast::<[wl_argument; 3]>() };
490 // SAFETY: - INTERFACE requires that args[0] contains an int
491 let arg0 = unsafe { args[0].i };
492 // SAFETY: - INTERFACE requires that args[1] contains a fixed
493 let arg1 = unsafe { Fixed::from_wire(args[1].f) };
494 // SAFETY: - INTERFACE requires that args[2] contains a fixed
495 let arg2 = unsafe { Fixed::from_wire(args[2].f) };
496 self.0.shape(slf, arg0, arg1, arg2);
497 }
498 6 => {
499 // SAFETY: INTERFACE requires that there are 2 arguments
500 let args = unsafe { &*args.cast::<[wl_argument; 2]>() };
501 // SAFETY: - INTERFACE requires that args[0] contains an int
502 let arg0 = unsafe { args[0].i };
503 // SAFETY: - INTERFACE requires that args[1] contains a fixed
504 let arg1 = unsafe { Fixed::from_wire(args[1].f) };
505 self.0.orientation(slf, arg0, arg1);
506 }
507 _ => {
508 invalid_opcode("wl_touch", opcode);
509 }
510 }
511 }447 unsafe fn handle_event(
448 &self,
449 queue: &Queue,
450 data: *mut u8,
451 slf: &UntypedBorrowedProxy,
452 opcode: u32,
453 args: *mut wl_argument,
454 ) {
455 // SAFETY: This function requires that slf has the interface INTERFACE
456 let slf = unsafe { proxy::low_level::from_untyped_borrowed::<WlTouchRef>(slf) };
457 // SAFETY: This function requires that data is `&mut T` where `T`
458 // has the type id returned by `Self::mutable_type`, i.e.,
459 // `T = H::Data`.
460 let data: &mut H::Data = unsafe { &mut *data.cast() };
461 match opcode {
462 0 => {
463 // SAFETY: INTERFACE requires that there are 6 arguments
464 let args = unsafe { &*args.cast::<[wl_argument; 6]>() };
465 // SAFETY: - INTERFACE requires that args[0] contains a uint
466 let arg0 = unsafe { args[0].u };
467 // SAFETY: - INTERFACE requires that args[1] contains a uint
468 let arg1 = unsafe { args[1].u };
469 // SAFETY: - INTERFACE requires that args[2] contains an object
470 let arg2 = unsafe {
471 if let Some(p) = NonNull::new(args[2].o.cast()) {
472 Some(UntypedBorrowedProxy::new_immutable(queue.libwayland(), p))
473 } else {
474 None
475 }
476 };
477 // SAFETY: - INTERFACE requires that the object has the interface WlSurface::WL_INTERFACE
478 let arg2 = arg2.as_ref().map(|arg2| unsafe {
479 proxy::low_level::from_untyped_borrowed::<WlSurfaceRef>(arg2)
480 });
481 // SAFETY: - INTERFACE requires that args[3] contains an int
482 let arg3 = unsafe { args[3].i };
483 // SAFETY: - INTERFACE requires that args[4] contains a fixed
484 let arg4 = unsafe { Fixed::from_wire(args[4].f) };
485 // SAFETY: - INTERFACE requires that args[5] contains a fixed
486 let arg5 = unsafe { Fixed::from_wire(args[5].f) };
487 self.0.down(data, slf, arg0, arg1, arg2, arg3, arg4, arg5);
488 }
489 1 => {
490 // SAFETY: INTERFACE requires that there are 3 arguments
491 let args = unsafe { &*args.cast::<[wl_argument; 3]>() };
492 // SAFETY: - INTERFACE requires that args[0] contains a uint
493 let arg0 = unsafe { args[0].u };
494 // SAFETY: - INTERFACE requires that args[1] contains a uint
495 let arg1 = unsafe { args[1].u };
496 // SAFETY: - INTERFACE requires that args[2] contains an int
497 let arg2 = unsafe { args[2].i };
498 self.0.up(data, slf, arg0, arg1, arg2);
499 }
500 2 => {
501 // SAFETY: INTERFACE requires that there are 4 arguments
502 let args = unsafe { &*args.cast::<[wl_argument; 4]>() };
503 // SAFETY: - INTERFACE requires that args[0] contains a uint
504 let arg0 = unsafe { args[0].u };
505 // SAFETY: - INTERFACE requires that args[1] contains an int
506 let arg1 = unsafe { args[1].i };
507 // SAFETY: - INTERFACE requires that args[2] contains a fixed
508 let arg2 = unsafe { Fixed::from_wire(args[2].f) };
509 // SAFETY: - INTERFACE requires that args[3] contains a fixed
510 let arg3 = unsafe { Fixed::from_wire(args[3].f) };
511 self.0.motion(data, slf, arg0, arg1, arg2, arg3);
512 }
513 3 => {
514 self.0.frame(data, slf);
515 }
516 4 => {
517 self.0.cancel(data, slf);
518 }
519 5 => {
520 // SAFETY: INTERFACE requires that there are 3 arguments
521 let args = unsafe { &*args.cast::<[wl_argument; 3]>() };
522 // SAFETY: - INTERFACE requires that args[0] contains an int
523 let arg0 = unsafe { args[0].i };
524 // SAFETY: - INTERFACE requires that args[1] contains a fixed
525 let arg1 = unsafe { Fixed::from_wire(args[1].f) };
526 // SAFETY: - INTERFACE requires that args[2] contains a fixed
527 let arg2 = unsafe { Fixed::from_wire(args[2].f) };
528 self.0.shape(data, slf, arg0, arg1, arg2);
529 }
530 6 => {
531 // SAFETY: INTERFACE requires that there are 2 arguments
532 let args = unsafe { &*args.cast::<[wl_argument; 2]>() };
533 // SAFETY: - INTERFACE requires that args[0] contains an int
534 let arg0 = unsafe { args[0].i };
535 // SAFETY: - INTERFACE requires that args[1] contains a fixed
536 let arg1 = unsafe { Fixed::from_wire(args[1].f) };
537 self.0.orientation(data, slf, arg0, arg1);
538 }
539 _ => {
540 invalid_opcode("wl_touch", opcode);
541 }
542 }
543 }- examples/simple-window/../common/protocols/wayland/wl_data_device.rs
- examples/simple-window/../common/protocols_data/wayland/wl_data_device.rs
- examples/simple-window/../common/protocols/wayland/wl_keyboard.rs
- examples/simple-window/../common/protocols/tablet_v2/zwp_tablet_pad_v2.rs
- examples/simple-window/../common/protocols_data/wayland/wl_keyboard.rs
- examples/simple-window/../common/protocols_data/tablet_v2/zwp_tablet_pad_v2.rs
- examples/simple-window/../common/protocols/wayland/wl_pointer.rs
- examples/simple-window/../common/protocols_data/wayland/wl_pointer.rs
- examples/simple-window/../common/protocols/tablet_v2/zwp_tablet_tool_v2.rs
- examples/simple-window/../common/protocols_data/tablet_v2/zwp_tablet_tool_v2.rs
Sourcepub fn connection(&self) -> &Connection
pub fn connection(&self) -> &Connection
Returns the connection that this queue belongs to.
§Example
let lib = Libwayland::open().unwrap();
let con = lib.connect_to_default_display().unwrap();
let queue = con.create_queue(c"queue name");
assert_eq!(queue.connection(), &con);Sourcepub fn lock_dispatch(&self) -> DispatchLock<'_>
pub fn lock_dispatch(&self) -> DispatchLock<'_>
Acquires the queue’s re-entrant lock.
See the description of Queue for why you might use this.
§Example
let lib = Libwayland::open().unwrap();
let con = lib.connect_to_default_display().unwrap();
let queue = con.create_queue(c"queue name");
let lock = queue.lock_dispatch();
let thread = {
let queue = queue.clone();
thread::spawn(move || {
// this dispatch will not start until the lock is dropped.
queue.dispatch_roundtrip_blocking().unwrap();
})
};
// this dispatch starts immediately since the lock is re-entrant
queue.dispatch_roundtrip_blocking().unwrap();
drop(lock);
thread.join().unwrap();Sourcepub fn display<T>(&self) -> Twhere
T: OwnedProxy,
pub fn display<T>(&self) -> Twhere
T: OwnedProxy,
Creates a wrapper proxy around the singleton wl_display object.
The proxy is a wrapper and no event handler can be attached to it.
The proxy is attached to this queue.
§Panic
Panics if the interface of T is not compatible with the wl_display interface.
§Example
let lib = Libwayland::open().unwrap();
let con = lib.connect_to_default_display().unwrap();
let queue = con.create_queue(c"queue name");
let display: WlDisplay = queue.display();
assert_eq!(proxy::queue(&display), &*queue);
assert_eq!(proxy::id(&*display), 1);Examples found in repository?
More examples
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}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 ®istry,
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}Sourcepub fn is_local(&self) -> bool
pub fn is_local(&self) -> bool
Returns whether this is a local queue.
The documentation of the Queue type explains the difference between local and
non-local queues.
§Example
let lib = Libwayland::open().unwrap();
let con = lib.connect_to_default_display().unwrap();
let queue1 = con.create_queue(c"non-local queue");
let queue2 = con.create_local_queue(c"local queue");
assert!(queue1.is_non_local());
assert!(queue2.is_local());Sourcepub fn is_non_local(&self) -> bool
pub fn is_non_local(&self) -> bool
Returns whether this is not a local queue.
This is the same as !self.is_local().
The documentation of the Queue type explains the difference between local and
non-local queues.
Sourcepub fn wl_event_queue(&self) -> NonNull<wl_event_queue>
pub fn wl_event_queue(&self) -> NonNull<wl_event_queue>
Returns the wl_event_queue pointer of this queue.
The returned pointer is valid for as long as this queue exists.
You must not dispatch the queue except through the Queue interface.
Otherwise the behavior is undefined.
Sourcepub fn dispatch_blocking(&self) -> Result<u64>
pub fn dispatch_blocking(&self) -> Result<u64>
Blocks the current thread until at least one event has been dispatched.
If you are in an async context, then you might want to use
Queue::dispatch_async instead.
This function should not be used when integrating with an existing, poll-based
event loop, as it might block indefinitely. Use Connection::create_watcher and
Queue::dispatch_pending instead.
This function cannot be used if the queue was created with
Connection::create_queue_with_data or
Connection::create_local_queue_with_data. Use
QueueWithData::dispatch_blocking instead.
The returned number is the number of events that have been dispatched by this call. The number can be zero if another thread dispatched the events before us.
§Panic
- Panics if this is a local queue and the current thread is not the thread that this queue was created in.
- Panics if the queue was created with
Connection::create_queue_with_dataorConnection::create_local_queue_with_data.
§Example
let lib = Libwayland::open().unwrap();
let con = lib.connect_to_default_display().unwrap();
let queue = con.create_queue(c"queue name");
// For this example, ensure that the compositor sends an event in the near future.
let _sync = queue.display::<WlDisplay>().sync();
queue.dispatch_blocking().unwrap();Examples found in repository?
More examples
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}Sourcepub async fn dispatch_async(&self) -> Result<u64>
pub async fn dispatch_async(&self) -> Result<u64>
Completes when at least one event has been dispatched.
This function is the same as Queue::dispatch_blocking except that it is async and does
not block the current thread.
§Panic
- Panics if this is a local queue and the thread polling the future is not the thread that this queue was created in.
- Panics if the queue was created with
Connection::create_queue_with_dataorConnection::create_local_queue_with_data.
§Example
let lib = Libwayland::open().unwrap();
let con = lib.connect_to_default_display().unwrap();
let queue = con.create_queue(c"queue name");
// For this example, ensure that the compositor sends an event in the near future.
let _sync = queue.display::<WlDisplay>().sync();
queue.dispatch_async().await.unwrap();Sourcepub fn dispatch_pending(&self) -> Result<u64>
pub fn dispatch_pending(&self) -> Result<u64>
Dispatches enqueued events.
This function does not read new events from the file descriptor.
This function can be used together with BorrowedQueue::wait_for_events or
Queue::create_watcher to dispatch the queue in an async context or event loop
respectively.
This function cannot be used if the queue was created with
Connection::create_queue_with_data or
Connection::create_local_queue_with_data. Use
QueueWithData::dispatch_pending instead.
The returned number is the number of events that were dispatched.
§Panic
- Panics if this is a local queue and the current thread is not the thread that this queue was created in.
- Panics if the queue was created with
Connection::create_queue_with_dataorConnection::create_local_queue_with_data.
§Example
let lib = Libwayland::open().unwrap();
let con = lib.connect_to_default_display().unwrap();
let queue = con.create_queue(c"queue name");
let display: WlDisplay = queue.display();
let done = Arc::new(AtomicBool::new(false));
let done2 = done.clone();
let sync = display.sync();
proxy::set_event_handler(&sync, WlCallback::on_done(move |_, _| {
done2.store(true, Relaxed);
}));
while !done.load(Relaxed) {
queue.wait_for_events().await.unwrap();
// Dispatch the events.
queue.dispatch_pending().unwrap();
}Examples found in repository?
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}More examples
11fn main() {
12 let lib = Libwayland::open().unwrap();
13 let con = lib.connect_to_default_display().unwrap();
14 let queue = con.create_local_queue(c"poll-integration");
15
16 // The watcher exposes a file descriptor that will become readable when the queue
17 // has new events.
18 let watcher = queue.create_watcher().unwrap();
19 let token = Token(0);
20
21 create_sync(&queue, 1);
22
23 let mut events = mio::Events::with_capacity(2);
24 let mut poll = mio::Poll::new().unwrap();
25 poll.registry()
26 .register(
27 &mut SourceFd(&watcher.as_raw_fd()),
28 token,
29 Interest::READABLE,
30 )
31 .unwrap();
32
33 loop {
34 // Flush requests before polling.
35 con.flush().unwrap();
36 poll.poll(&mut events, None).unwrap();
37 for event in events.iter() {
38 if event.token() == token {
39 queue.dispatch_pending().unwrap();
40 // Reset the watcher to clear the readability status.
41 watcher.reset().unwrap();
42 }
43 }
44 events.clear();
45 }
46}Sourcepub fn dispatch_roundtrip_blocking(&self) -> Result<()>
pub fn dispatch_roundtrip_blocking(&self) -> Result<()>
Blocks the current thread until the compositor has processed all previous requests and all of its response events have been dispatched.
If you are in an async context, then you might want to use
Queue::dispatch_roundtrip_async instead.
Since this function usually returns quickly, you might use this function even when integrating a wayland connection into an existing event loop and even in an async context. For example, a library that creates buffers might use this function during initialization to receive the full list of supported formats before returning.
This function cannot be used if the queue was created with
Connection::create_queue_with_data or
Connection::create_local_queue_with_data. Use
QueueWithData::dispatch_roundtrip_blocking instead.
If this function returns Ok(()), then the function returns after (in the sense
of the C++ memory model) the event handlers of all previous events have been
invoked.
§Panic
- Panics if this is a local queue and the current thread is not the thread that this queue was created in.
- Panics if the queue was created with
Connection::create_queue_with_dataorConnection::create_local_queue_with_data.
§Example
let lib = Libwayland::open().unwrap();
let con = lib.connect_to_default_display().unwrap();
let queue = con.create_queue(c"");
let display: WlDisplay = queue.display();
// send some messages to the compositor
let done = Arc::new(AtomicBool::new(false));
let done2 = done.clone();
let sync = display.sync();
proxy::set_event_handler(&sync, WlCallback::on_done(move |_, _| {
done2.store(true, Relaxed);
}));
// perform a roundtrip
queue.dispatch_roundtrip_blocking().unwrap();
// assert that we've received the response
assert!(done.load(Relaxed));Examples found in repository?
56pub fn get_singletons(display: &WlDisplay) -> Singletons {
57 let map = Mutex::new(HashMap::new());
58
59 let queue = proxy::queue(display);
60 let wl_registry = display.get_registry();
61
62 queue.dispatch_scope_blocking(|scope| {
63 scope.set_event_handler(
64 &wl_registry,
65 WlRegistry::on_global(|_, name, interface, version| {
66 map.lock().insert(interface.to_owned(), (name, version));
67 }),
68 );
69 queue.dispatch_roundtrip_blocking().unwrap();
70 });
71
72 Singletons {
73 wl_registry,
74 map: map.into_inner(),
75 }
76}More examples
27fn get_registry_snapshot(queue: &Queue) -> (WlRegistry, Vec<Global>) {
28 // Create a new registry that will receive the globals and can later be used to
29 // bind them.
30 let registry = queue.display::<WlDisplay>().get_registry();
31 let globals = Mutex::new(vec![]);
32 // Since we don't care about registry events after this function returns, we can
33 // use a dispatch scope. The event handlers in this scope will not be called after
34 // the function returns.
35 queue.dispatch_scope_blocking(|scope| {
36 scope.set_event_handler(
37 ®istry,
38 // Since we only want to create a snapshot, we don't care about
39 // global_remove events. This allows us to use the functional event handler
40 // form.
41 WlRegistry::on_global(|_, name, interface, version| {
42 globals.lock().push(Global {
43 name,
44 interface: interface.to_string(),
45 version,
46 });
47 }),
48 );
49 queue.dispatch_roundtrip_blocking().unwrap();
50 });
51 // The event handler will no longer be called after this function returns but
52 // the registry can still be used to bind globals.
53 (registry, globals.into_inner())
54}9fn main() {
10 // Load the `libwayland-client.so` dynamic library.
11 let lib = Libwayland::open().unwrap();
12 // Connect to the default display determined by the `WAYLAND_DISPLAY` env var.
13 let con = lib.connect_to_default_display().unwrap();
14 // Create a new event queue with the name `hello-wayland`. This name will show up
15 // when debugging applications with `WAYLAND_DEBUG=1`.
16 let queue = con.create_queue(c"hello-wayland");
17 // Get a reference to the `wl_display` singleton. This type was generated with the
18 // `wl-client-builder` crate.
19 let display: WlDisplay = queue.display();
20 // Create a `wl_callback` object. The compositor will immediately respond with a
21 // `wl_callback.done` event.
22 let sync = display.sync();
23 // Set the event handler of the proxy.
24 proxy::set_event_handler(
25 &sync,
26 // When only handling a single event, the following functional form can be used.
27 // In general, and when handling more than one event, the event handler trait must
28 // be implemented. In this case, `WlCallbackEventHandler`.
29 WlCallback::on_done(|_, _| println!("Hello wayland!")),
30 );
31 // Perform a roundtrip to ensure that the `done` event has been dispatched.
32 queue.dispatch_roundtrip_blocking().unwrap();
33}Sourcepub async fn dispatch_roundtrip_async(&self) -> Result<()>
pub async fn dispatch_roundtrip_async(&self) -> Result<()>
Completes when the compositor has processed all previous requests and all of its response events have been dispatched.
This function is the same as Queue::dispatch_roundtrip_blocking except that it is async and does
not block the current thread.
If the future completes with Ok(()), then the future completes after (in the
sense of the C++ memory model) the event handlers of all previous events have been
invoked.
§Panic
- Panics if this is a local queue and the thread polling the future is not the thread that this queue was created in.
- Panics if the queue was created with
Connection::create_queue_with_dataorConnection::create_local_queue_with_data.
§Example
let lib = Libwayland::open().unwrap();
let con = lib.connect_to_default_display().unwrap();
let queue = con.create_queue(c"queue name");
let display: WlDisplay = queue.display();
// send some messages to the compositor
let done = Arc::new(AtomicBool::new(false));
let done2 = done.clone();
let sync = display.sync();
proxy::set_event_handler(&sync, WlCallback::on_done(move |_, _| {
done2.store(true, Relaxed);
}));
// perform a roundtrip
queue.dispatch_roundtrip_async().await.unwrap();
// assert that we've received the response
assert!(done.load(Relaxed));Examples found in repository?
78pub async fn get_singletons_async(display: &WlDisplay) -> Singletons {
79 let map = Mutex::new(HashMap::new());
80
81 let queue = proxy::queue(display);
82 let wl_registry = display.get_registry();
83
84 queue
85 .dispatch_scope_async(async |scope| {
86 scope.set_event_handler(
87 &wl_registry,
88 WlRegistry::on_global(|_, name, interface, version| {
89 map.lock().insert(interface.to_owned(), (name, version));
90 }),
91 );
92 queue.dispatch_roundtrip_async().await.unwrap();
93 })
94 .await;
95
96 Singletons {
97 wl_registry,
98 map: map.into_inner(),
99 }
100}More examples
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 ®istry,
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}Sourcepub fn wrap_proxy<P>(&self, proxy: &P) -> P::Ownedwhere
P: BorrowedProxy,
pub fn wrap_proxy<P>(&self, proxy: &P) -> P::Ownedwhere
P: BorrowedProxy,
Creates a wrapper for an existing proxy.
The wrapper will be assigned to this queue. No event handler can be assigned to the wrapper.
§Panic
- Panics if the proxy and this queue don’t belong to the same
wl_display. - Panics if the proxy is already destroyed.
§Example
let lib = Libwayland::open().unwrap();
let con = lib.connect_to_default_display().unwrap();
let queue1 = con.create_queue(c"queue name");
let display1: WlDisplay = queue1.display();
assert_eq!(proxy::queue(&display1), &*queue1);
let queue2 = con.create_queue(c"second queue");
let display2 = queue2.wrap_proxy(&*display1);
assert_eq!(proxy::queue(&display2), &*queue2);Sourcepub unsafe fn wrap_wl_proxy<P>(&self, proxy: NonNull<wl_proxy>) -> Pwhere
P: OwnedProxy,
pub unsafe fn wrap_wl_proxy<P>(&self, proxy: NonNull<wl_proxy>) -> Pwhere
P: OwnedProxy,
Creates a wrapper for an existing wl_proxy.
The wrapper will be assigned to this queue. No event handler can be assigned to the wrapper.
If the wl_proxy already has a safe wrapper, the Queue::wrap_proxy function
can be used instead.
§Panic
- Panics if the proxy and this queue don’t belong to the same
wl_display.
§Safety
proxymust be a valid pointer.proxymust have an interface compatible withP::WL_INTERFACE.
§Example
Some frameworks, e.g. winit, expose libwayland wl_display and wl_surface
pointers. These can be imported into this crate as follows:
unsafe fn wrap_foreign_surface(display: NonNull<c_void>, wl_surface: NonNull<c_void>) {
let lib = Libwayland::open().unwrap();
// SAFETY: ...
let con = unsafe { lib.wrap_borrowed_pointer(display.cast()).unwrap() };
let queue = con.create_queue(c"queue name");
// SAFETY: ...
let surface: WlSurface = unsafe { queue.wrap_wl_proxy(wl_surface.cast()) };
}Sourcepub fn create_watcher(&self) -> Result<QueueWatcher>
pub fn create_watcher(&self) -> Result<QueueWatcher>
Creates a QueueWatcher for event-loop integration.
This is a shorthand for calling Connection::create_watcher with a queue list
containing exactly this queue.
Examples found in repository?
11fn main() {
12 let lib = Libwayland::open().unwrap();
13 let con = lib.connect_to_default_display().unwrap();
14 let queue = con.create_local_queue(c"poll-integration");
15
16 // The watcher exposes a file descriptor that will become readable when the queue
17 // has new events.
18 let watcher = queue.create_watcher().unwrap();
19 let token = Token(0);
20
21 create_sync(&queue, 1);
22
23 let mut events = mio::Events::with_capacity(2);
24 let mut poll = mio::Poll::new().unwrap();
25 poll.registry()
26 .register(
27 &mut SourceFd(&watcher.as_raw_fd()),
28 token,
29 Interest::READABLE,
30 )
31 .unwrap();
32
33 loop {
34 // Flush requests before polling.
35 con.flush().unwrap();
36 poll.poll(&mut events, None).unwrap();
37 for event in events.iter() {
38 if event.token() == token {
39 queue.dispatch_pending().unwrap();
40 // Reset the watcher to clear the readability status.
41 watcher.reset().unwrap();
42 }
43 }
44 events.clear();
45 }
46}Methods from Deref<Target = BorrowedQueue>§
Sourcepub async fn wait_for_events(&self) -> Result<()>
pub async fn wait_for_events(&self) -> Result<()>
Completes when there are new events in this queue.
When this function returns Ok(()), this queue has an event queued.
This is a shorthand for calling Connection::wait_for_events with a
queue list consisting of exactly this queue.
§Example
let lib = Libwayland::open().unwrap();
let con = lib.connect_to_default_display().unwrap();
let queue = con.create_queue(c"queue name");
let display: WlDisplay = queue.display();
let done = Arc::new(AtomicBool::new(false));
let done2 = done.clone();
let sync = display.sync();
proxy::set_event_handler(&sync, WlCallback::on_done(move |_, _| {
done2.store(true, Release);
}));
while !done.load(Acquire) {
queue.wait_for_events().await.unwrap();
queue.dispatch_pending().unwrap();
}Examples found in repository?
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}Sourcepub fn wl_event_queue(&self) -> Option<NonNull<wl_event_queue>>
pub fn wl_event_queue(&self) -> Option<NonNull<wl_event_queue>>
Returns the wl_event_queue representing this queue.
This function returns None if and only if this queue is the default queue of the
connection.
The returned pointer, if any, remains valid as long as this object exists.
§Example
let lib = Libwayland::open().unwrap();
let con = lib.connect_to_default_display().unwrap();
let queue = con.create_queue(c"queue name");
let default_queue = con.borrow_default_queue();
assert_eq!((**queue).wl_event_queue(), Some(queue.wl_event_queue()));
assert_eq!(default_queue.wl_event_queue(), None);