Skip to main content

glib/auto/
source.rs

1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4
5use crate::{ffi, translate::*};
6
7crate::wrapper! {
8    /// The `GSource` struct is an opaque data type
9    /// representing an event source.
10    #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
11    pub struct Source(Shared<ffi::GSource>);
12
13    match fn {
14        ref => |ptr| ffi::g_source_ref(ptr),
15        unref => |ptr| ffi::g_source_unref(ptr),
16        type_ => || ffi::g_source_get_type(),
17    }
18}
19
20impl Source {
21    //#[doc(alias = "g_source_new")]
22    //pub fn new(source_funcs: /*Ignored*/&mut SourceFuncs, struct_size: u32) -> Source {
23    //    unsafe { TODO: call ffi:g_source_new() }
24    //}
25
26    /// Adds @child_source to @self as a ‘polled’ source.
27    ///
28    /// When @self is added to a [`MainContext`][crate::MainContext], @child_source will be
29    /// automatically added with the same priority. When @child_source is triggered,
30    /// it will cause @self to dispatch (in addition to calling its own callback),
31    /// and when @self is destroyed, it will destroy @child_source as well.
32    ///
33    /// The @self will also still be dispatched if its own prepare/check functions
34    /// indicate that it is ready.
35    ///
36    /// If you don’t need @child_source to do anything on its own when it
37    /// triggers, you can call `g_source_set_dummy_callback()` on it to set a
38    /// callback that does nothing (except return true if appropriate).
39    ///
40    /// The @self will hold a reference on @child_source while @child_source
41    /// is attached to it.
42    ///
43    /// This API is only intended to be used by implementations of [`Source`][crate::Source].
44    /// Do not call this API on a [`Source`][crate::Source] that you did not create.
45    /// ## `child_source`
46    /// a second source that @self should ‘poll’
47    #[doc(alias = "g_source_add_child_source")]
48    pub fn add_child_source(&self, child_source: &Source) {
49        unsafe {
50            ffi::g_source_add_child_source(self.to_glib_none().0, child_source.to_glib_none().0);
51        }
52    }
53
54    //#[doc(alias = "g_source_add_poll")]
55    //pub fn add_poll(&self, fd: /*Ignored*/&mut PollFD) {
56    //    unsafe { TODO: call ffi:g_source_add_poll() }
57    //}
58
59    //#[doc(alias = "g_source_add_unix_fd")]
60    //pub fn add_unix_fd(&self, fd: i32, events: IOCondition) -> /*Unimplemented*/Basic: Pointer {
61    //    unsafe { TODO: call ffi:g_source_add_unix_fd() }
62    //}
63
64    /// Unsets any previously set ready time.
65    ///
66    /// If the source does not have a ready time set, this function
67    /// does nothing.
68    #[cfg(feature = "v2_90")]
69    #[cfg_attr(docsrs, doc(cfg(feature = "v2_90")))]
70    #[doc(alias = "g_source_clear_ready_time")]
71    pub fn clear_ready_time(&self) {
72        unsafe {
73            ffi::g_source_clear_ready_time(self.to_glib_none().0);
74        }
75    }
76
77    #[doc(alias = "g_source_destroy")]
78    pub fn destroy(&self) {
79        unsafe {
80            ffi::g_source_destroy(self.to_glib_none().0);
81        }
82    }
83
84    /// Checks whether a source is allowed to be called recursively.
85    ///
86    /// See `GLib::Source::set_can_recurse()`.
87    ///
88    /// # Returns
89    ///
90    /// whether recursion is allowed
91    #[doc(alias = "g_source_get_can_recurse")]
92    #[doc(alias = "get_can_recurse")]
93    pub fn can_recurse(&self) -> bool {
94        unsafe { from_glib(ffi::g_source_get_can_recurse(self.to_glib_none().0)) }
95    }
96
97    /// Gets a name for the source, used in debugging and profiling.
98    ///
99    /// The
100    /// name may be `NULL` if it has never been set with `GLib::Source::set_name()`.
101    ///
102    /// # Returns
103    ///
104    /// the name of the source
105    #[doc(alias = "g_source_get_name")]
106    #[doc(alias = "get_name")]
107    pub fn name(&self) -> Option<crate::GString> {
108        unsafe { from_glib_none(ffi::g_source_get_name(self.to_glib_none().0)) }
109    }
110
111    /// Gets the priority of a source.
112    ///
113    /// # Returns
114    ///
115    /// the priority of the source
116    #[doc(alias = "g_source_get_priority")]
117    #[doc(alias = "get_priority")]
118    pub fn priority(&self) -> i32 {
119        unsafe { ffi::g_source_get_priority(self.to_glib_none().0) }
120    }
121
122    /// Gets the ‘ready time’ of @self, as set by
123    /// `GLib::Source::set_ready_time()`.
124    ///
125    /// Any time before or equal to the current monotonic time (including zero)
126    /// is an indication that the source will fire immediately.
127    ///
128    /// # Returns
129    ///
130    /// the monotonic ready time, `-1` for ‘never’
131    #[doc(alias = "g_source_get_ready_time")]
132    #[doc(alias = "get_ready_time")]
133    pub fn ready_time(&self) -> i64 {
134        unsafe { ffi::g_source_get_ready_time(self.to_glib_none().0) }
135    }
136
137    /// Gets the ‘ready time’ of @self, as set by
138    /// `GLib::Source::set_ready_time_ns()`. If no ready time has been set
139    /// or it has been cleared via method@GLib.Source.clear_ready_time], this
140    /// function returns false.
141    ///
142    /// Any time before or equal to the current monotonic time (including zero)
143    /// is an indication that the source will fire immediately.
144    ///
145    /// # Returns
146    ///
147    /// true if the source has a ready time set.
148    ///
149    /// ## `ready_time`
150    /// Set to the ready time
151    ///   on success
152    #[cfg(feature = "v2_90")]
153    #[cfg_attr(docsrs, doc(cfg(feature = "v2_90")))]
154    #[doc(alias = "g_source_get_ready_time_ns")]
155    #[doc(alias = "get_ready_time_ns")]
156    pub fn ready_time_ns(&self) -> Option<u64> {
157        unsafe {
158            let mut ready_time = std::mem::MaybeUninit::uninit();
159            let ret = from_glib(ffi::g_source_get_ready_time_ns(
160                self.to_glib_none().0,
161                ready_time.as_mut_ptr(),
162            ));
163            if ret {
164                Some(ready_time.assume_init())
165            } else {
166                None
167            }
168        }
169    }
170
171    /// Gets the time to be used when checking this source.
172    ///
173    /// The advantage of
174    /// calling this function over calling [`monotonic_time()`][crate::monotonic_time()] directly is
175    /// that when checking multiple sources, GLib can cache a single value
176    /// instead of having to repeatedly get the system monotonic time.
177    ///
178    /// The time here is the system monotonic time, if available, or some
179    /// other reasonable alternative otherwise.  See [`monotonic_time()`][crate::monotonic_time()].
180    ///
181    /// # Returns
182    ///
183    /// the monotonic time in microseconds
184    #[doc(alias = "g_source_get_time")]
185    #[doc(alias = "get_time")]
186    pub fn time(&self) -> i64 {
187        unsafe { ffi::g_source_get_time(self.to_glib_none().0) }
188    }
189
190    /// Gets the time to be used when checking this source.
191    ///
192    /// The advantage of calling this function over calling
193    /// [`monotonic_time_ns()`][crate::monotonic_time_ns()] directly is
194    /// that when checking multiple sources, GLib can cache a single value
195    /// instead of having to repeatedly get the system monotonic time.
196    ///
197    /// The time here is the system monotonic time, if available, or some
198    /// other reasonable alternative otherwise.  See [`monotonic_time_ns()`][crate::monotonic_time_ns()].
199    ///
200    /// # Returns
201    ///
202    /// the monotonic time in nanoseconds
203    #[cfg(feature = "v2_90")]
204    #[cfg_attr(docsrs, doc(cfg(feature = "v2_90")))]
205    #[doc(alias = "g_source_get_time_ns")]
206    #[doc(alias = "get_time_ns")]
207    pub fn time_ns(&self) -> u64 {
208        unsafe { ffi::g_source_get_time_ns(self.to_glib_none().0) }
209    }
210
211    /// Returns whether @self has been destroyed.
212    ///
213    /// This is important when you operate upon your objects
214    /// from within idle handlers, but may have freed the object
215    /// before the dispatch of your idle handler.
216    ///
217    /// **⚠️ The following code is in c ⚠️**
218    ///
219    /// ```c
220    /// static gboolean
221    /// idle_callback (gpointer data)
222    /// {
223    ///   SomeWidget *self = data;
224    ///
225    ///   g_mutex_lock (&self->idle_id_mutex);
226    ///   // do stuff with self
227    ///   self->idle_id = 0;
228    ///   g_mutex_unlock (&self->idle_id_mutex);
229    ///
230    ///   return G_SOURCE_REMOVE;
231    /// }
232    ///
233    /// static void
234    /// some_widget_do_stuff_later (SomeWidget *self)
235    /// {
236    ///   g_mutex_lock (&self->idle_id_mutex);
237    ///   self->idle_id = g_idle_add (idle_callback, self);
238    ///   g_mutex_unlock (&self->idle_id_mutex);
239    /// }
240    ///
241    /// static void
242    /// some_widget_init (SomeWidget *self)
243    /// {
244    ///   g_mutex_init (&self->idle_id_mutex);
245    ///
246    ///   // ...
247    /// }
248    ///
249    /// static void
250    /// some_widget_finalize (GObject *object)
251    /// {
252    ///   SomeWidget *self = SOME_WIDGET (object);
253    ///
254    ///   if (self->idle_id)
255    ///     g_source_remove (self->idle_id);
256    ///
257    ///   g_mutex_clear (&self->idle_id_mutex);
258    ///
259    ///   G_OBJECT_CLASS (parent_class)->finalize (object);
260    /// }
261    /// ```
262    ///
263    /// This will fail in a multi-threaded application if the
264    /// widget is destroyed before the idle handler fires due
265    /// to the use after free in the callback. A solution, to
266    /// this particular problem, is to check to if the source
267    /// has already been destroy within the callback.
268    ///
269    /// **⚠️ The following code is in c ⚠️**
270    ///
271    /// ```c
272    /// static gboolean
273    /// idle_callback (gpointer data)
274    /// {
275    ///   SomeWidget *self = data;
276    ///
277    ///   g_mutex_lock (&self->idle_id_mutex);
278    ///   if (!g_source_is_destroyed (g_main_current_source ()))
279    ///     {
280    ///       // do stuff with self
281    ///     }
282    ///   g_mutex_unlock (&self->idle_id_mutex);
283    ///
284    ///   return FALSE;
285    /// }
286    /// ```
287    ///
288    /// Calls to this function from a thread other than the one acquired by the
289    /// [`MainContext`][crate::MainContext] the [`Source`][crate::Source] is attached to are typically
290    /// redundant, as the source could be destroyed immediately after this function
291    /// returns. However, once a source is destroyed it cannot be un-destroyed, so
292    /// this function can be used for opportunistic checks from any thread.
293    ///
294    /// # Returns
295    ///
296    /// true if the source has been destroyed, false otherwise
297    #[doc(alias = "g_source_is_destroyed")]
298    pub fn is_destroyed(&self) -> bool {
299        unsafe { from_glib(ffi::g_source_is_destroyed(self.to_glib_none().0)) }
300    }
301
302    //#[doc(alias = "g_source_modify_unix_fd")]
303    //pub fn modify_unix_fd(&self, tag: /*Unimplemented*/Basic: Pointer, new_events: IOCondition) {
304    //    unsafe { TODO: call ffi:g_source_modify_unix_fd() }
305    //}
306
307    //#[doc(alias = "g_source_query_unix_fd")]
308    //pub fn query_unix_fd(&self, tag: /*Unimplemented*/Basic: Pointer) -> IOCondition {
309    //    unsafe { TODO: call ffi:g_source_query_unix_fd() }
310    //}
311
312    /// Detaches @child_source from @self and destroys it.
313    ///
314    /// This API is only intended to be used by implementations of [`Source`][crate::Source].
315    /// Do not call this API on a [`Source`][crate::Source] that you did not create.
316    /// ## `child_source`
317    /// a source previously passed to
318    ///   [`add_child_source()`][Self::add_child_source()]
319    #[doc(alias = "g_source_remove_child_source")]
320    pub fn remove_child_source(&self, child_source: &Source) {
321        unsafe {
322            ffi::g_source_remove_child_source(self.to_glib_none().0, child_source.to_glib_none().0);
323        }
324    }
325
326    //#[doc(alias = "g_source_remove_poll")]
327    //pub fn remove_poll(&self, fd: /*Ignored*/&mut PollFD) {
328    //    unsafe { TODO: call ffi:g_source_remove_poll() }
329    //}
330
331    //#[doc(alias = "g_source_remove_unix_fd")]
332    //pub fn remove_unix_fd(&self, tag: /*Unimplemented*/Basic: Pointer) {
333    //    unsafe { TODO: call ffi:g_source_remove_unix_fd() }
334    //}
335
336    //#[doc(alias = "g_source_remove_by_funcs_user_data")]
337    //pub fn remove_by_funcs_user_data(funcs: /*Ignored*/&mut SourceFuncs, user_data: /*Unimplemented*/Option<Basic: Pointer>) -> bool {
338    //    unsafe { TODO: call ffi:g_source_remove_by_funcs_user_data() }
339    //}
340
341    //#[doc(alias = "g_source_remove_by_user_data")]
342    //pub fn remove_by_user_data(user_data: /*Unimplemented*/Option<Basic: Pointer>) -> bool {
343    //    unsafe { TODO: call ffi:g_source_remove_by_user_data() }
344    //}
345}
346
347unsafe impl Send for Source {}
348unsafe impl Sync for Source {}