1use crate::{ffi, ChannelEvent, Session};
7use glib::{
8 object::ObjectType as _,
9 prelude::*,
10 signal::{connect_raw, SignalHandlerId},
11 translate::*,
12};
13use std::{boxed::Box as Box_, pin::Pin};
14
15glib::wrapper! {
16 #[doc(alias = "SpiceChannel")]
17 pub struct Channel(Object<ffi::SpiceChannel, ffi::SpiceChannelClass>);
18
19 match fn {
20 type_ => || ffi::spice_channel_get_type(),
21 }
22}
23
24impl Channel {
25 pub const NONE: Option<&'static Channel> = None;
26
27 #[doc(alias = "spice_channel_new")]
28 pub fn new(s: &Session, type_: i32, id: i32) -> Channel {
29 skip_assert_initialized!();
30 unsafe { from_glib_full(ffi::spice_channel_new(s.to_glib_none().0, type_, id)) }
31 }
32
33 #[doc(alias = "spice_channel_string_to_type")]
34 pub fn string_to_type(str: &str) -> i32 {
35 assert_initialized_main_thread!();
36 unsafe { ffi::spice_channel_string_to_type(str.to_glib_none().0) }
37 }
38
39 #[doc(alias = "spice_channel_type_to_string")]
40 pub fn type_to_string(type_: i32) -> Option<glib::GString> {
41 assert_initialized_main_thread!();
42 unsafe { from_glib_none(ffi::spice_channel_type_to_string(type_)) }
43 }
44}
45
46pub trait ChannelExt: IsA<Channel> + 'static {
47 #[doc(alias = "spice_channel_connect")]
48 fn connect(&self) -> bool {
49 unsafe { from_glib(ffi::spice_channel_connect(self.as_ref().to_glib_none().0)) }
50 }
51
52 #[doc(alias = "spice_channel_disconnect")]
53 fn disconnect(&self, reason: ChannelEvent) {
54 unsafe {
55 ffi::spice_channel_disconnect(self.as_ref().to_glib_none().0, reason.into_glib());
56 }
57 }
58
59 #[doc(alias = "spice_channel_flush_async")]
60 fn flush_async<P: FnOnce(Result<(), glib::Error>) + 'static>(
61 &self,
62 cancellable: Option<&impl IsA<gio::Cancellable>>,
63 callback: P,
64 ) {
65 let main_context = glib::MainContext::ref_thread_default();
66 let is_main_context_owner = main_context.is_owner();
67 let has_acquired_main_context = (!is_main_context_owner)
68 .then(|| main_context.acquire().ok())
69 .flatten();
70 assert!(
71 is_main_context_owner || has_acquired_main_context.is_some(),
72 "Async operations only allowed if the thread is owning the MainContext"
73 );
74
75 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
76 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
77 unsafe extern "C" fn flush_async_trampoline<
78 P: FnOnce(Result<(), glib::Error>) + 'static,
79 >(
80 _source_object: *mut glib::gobject_ffi::GObject,
81 res: *mut gio::ffi::GAsyncResult,
82 user_data: glib::ffi::gpointer,
83 ) {
84 unsafe {
85 let mut error = std::ptr::null_mut();
86 ffi::spice_channel_flush_finish(_source_object as *mut _, res, &mut error);
87 let result = if error.is_null() {
88 Ok(())
89 } else {
90 Err(from_glib_full(error))
91 };
92 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
93 Box_::from_raw(user_data as *mut _);
94 let callback: P = callback.into_inner();
95 callback(result);
96 }
97 }
98 let callback = flush_async_trampoline::<P>;
99 unsafe {
100 ffi::spice_channel_flush_async(
101 self.as_ref().to_glib_none().0,
102 cancellable.map(|p| p.as_ref()).to_glib_none().0,
103 Some(callback),
104 Box_::into_raw(user_data) as *mut _,
105 );
106 }
107 }
108
109 fn flush_future(
110 &self,
111 ) -> Pin<Box_<dyn std::future::Future<Output = Result<(), glib::Error>> + 'static>> {
112 Box_::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
113 obj.flush_async(Some(cancellable), move |res| {
114 send.resolve(res);
115 });
116 }))
117 }
118
119 #[doc(alias = "spice_channel_get_error")]
120 #[doc(alias = "get_error")]
121 fn error(&self) -> Option<glib::Error> {
122 unsafe { from_glib_none(ffi::spice_channel_get_error(self.as_ref().to_glib_none().0)) }
123 }
124
125 #[doc(alias = "spice_channel_open_fd")]
126 fn open_fd(&self, fd: i32) -> bool {
127 unsafe {
128 from_glib(ffi::spice_channel_open_fd(
129 self.as_ref().to_glib_none().0,
130 fd,
131 ))
132 }
133 }
134
135 #[doc(alias = "spice_channel_test_capability")]
136 fn test_capability(&self, cap: u32) -> bool {
137 unsafe {
138 from_glib(ffi::spice_channel_test_capability(
139 self.as_ref().to_glib_none().0,
140 cap,
141 ))
142 }
143 }
144
145 #[doc(alias = "spice_channel_test_common_capability")]
146 fn test_common_capability(&self, cap: u32) -> bool {
147 unsafe {
148 from_glib(ffi::spice_channel_test_common_capability(
149 self.as_ref().to_glib_none().0,
150 cap,
151 ))
152 }
153 }
154
155 #[doc(alias = "channel-id")]
156 fn channel_id(&self) -> i32 {
157 ObjectExt::property(self.as_ref(), "channel-id")
158 }
159
160 #[doc(alias = "channel-type")]
161 fn channel_type(&self) -> i32 {
162 ObjectExt::property(self.as_ref(), "channel-type")
163 }
164
165 fn socket(&self) -> Option<gio::Socket> {
166 ObjectExt::property(self.as_ref(), "socket")
167 }
168
169 #[doc(alias = "spice-session")]
170 fn spice_session(&self) -> Option<Session> {
171 ObjectExt::property(self.as_ref(), "spice-session")
172 }
173
174 #[doc(alias = "total-read-bytes")]
175 fn total_read_bytes(&self) -> libc::c_ulong {
176 ObjectExt::property(self.as_ref(), "total-read-bytes")
177 }
178
179 #[doc(alias = "channel-event")]
180 fn connect_channel_event<F: Fn(&Self, ChannelEvent) + 'static>(&self, f: F) -> SignalHandlerId {
181 unsafe extern "C" fn channel_event_trampoline<
182 P: IsA<Channel>,
183 F: Fn(&P, ChannelEvent) + 'static,
184 >(
185 this: *mut ffi::SpiceChannel,
186 event: ffi::SpiceChannelEvent,
187 f: glib::ffi::gpointer,
188 ) {
189 unsafe {
190 let f: &F = &*(f as *const F);
191 f(
192 Channel::from_glib_borrow(this).unsafe_cast_ref(),
193 from_glib(event),
194 )
195 }
196 }
197 unsafe {
198 let f: Box_<F> = Box_::new(f);
199 connect_raw(
200 self.as_ptr() as *mut _,
201 c"channel-event".as_ptr(),
202 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
203 channel_event_trampoline::<Self, F> as *const (),
204 )),
205 Box_::into_raw(f),
206 )
207 }
208 }
209
210 #[doc(alias = "open-fd")]
211 fn connect_open_fd<F: Fn(&Self, i32) + 'static>(&self, f: F) -> SignalHandlerId {
212 unsafe extern "C" fn open_fd_trampoline<P: IsA<Channel>, F: Fn(&P, i32) + 'static>(
213 this: *mut ffi::SpiceChannel,
214 with_tls: std::ffi::c_int,
215 f: glib::ffi::gpointer,
216 ) {
217 unsafe {
218 let f: &F = &*(f as *const F);
219 f(Channel::from_glib_borrow(this).unsafe_cast_ref(), with_tls)
220 }
221 }
222 unsafe {
223 let f: Box_<F> = Box_::new(f);
224 connect_raw(
225 self.as_ptr() as *mut _,
226 c"open-fd".as_ptr(),
227 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
228 open_fd_trampoline::<Self, F> as *const (),
229 )),
230 Box_::into_raw(f),
231 )
232 }
233 }
234
235 #[doc(alias = "socket")]
236 fn connect_socket_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
237 unsafe extern "C" fn notify_socket_trampoline<P: IsA<Channel>, F: Fn(&P) + 'static>(
238 this: *mut ffi::SpiceChannel,
239 _param_spec: glib::ffi::gpointer,
240 f: glib::ffi::gpointer,
241 ) {
242 unsafe {
243 let f: &F = &*(f as *const F);
244 f(Channel::from_glib_borrow(this).unsafe_cast_ref())
245 }
246 }
247 unsafe {
248 let f: Box_<F> = Box_::new(f);
249 connect_raw(
250 self.as_ptr() as *mut _,
251 c"notify::socket".as_ptr(),
252 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
253 notify_socket_trampoline::<Self, F> as *const (),
254 )),
255 Box_::into_raw(f),
256 )
257 }
258 }
259
260 #[doc(alias = "total-read-bytes")]
261 fn connect_total_read_bytes_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
262 unsafe extern "C" fn notify_total_read_bytes_trampoline<
263 P: IsA<Channel>,
264 F: Fn(&P) + 'static,
265 >(
266 this: *mut ffi::SpiceChannel,
267 _param_spec: glib::ffi::gpointer,
268 f: glib::ffi::gpointer,
269 ) {
270 unsafe {
271 let f: &F = &*(f as *const F);
272 f(Channel::from_glib_borrow(this).unsafe_cast_ref())
273 }
274 }
275 unsafe {
276 let f: Box_<F> = Box_::new(f);
277 connect_raw(
278 self.as_ptr() as *mut _,
279 c"notify::total-read-bytes".as_ptr(),
280 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
281 notify_total_read_bytes_trampoline::<Self, F> as *const (),
282 )),
283 Box_::into_raw(f),
284 )
285 }
286 }
287}
288
289impl<O: IsA<Channel>> ChannelExt for O {}