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::*, MainContext};
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; when @self is
27 /// added to a [`MainContext`][crate::MainContext], @child_source will be automatically
28 /// added with the same priority, when @child_source is triggered, it will
29 /// cause @self to dispatch (in addition to calling its own
30 /// callback), and when @self is destroyed, it will destroy
31 /// @child_source as well. (@self will also still be dispatched if
32 /// its own prepare/check functions indicate that it is ready.)
33 ///
34 /// If you don't need @child_source to do anything on its own when it
35 /// triggers, you can call g_source_set_dummy_callback() on it to set a
36 /// callback that does nothing (except return [`true`] if appropriate).
37 ///
38 /// @self will hold a reference on @child_source while @child_source
39 /// is attached to it.
40 ///
41 /// This API is only intended to be used by implementations of
42 /// [`Source`][crate::Source]. Do not call this API on a [`Source`][crate::Source] that
43 /// you did not create.
44 /// ## `child_source`
45 /// a second #GSource that @self should "poll"
46 #[doc(alias = "g_source_add_child_source")]
47 pub fn add_child_source(&self, child_source: &Source) {
48 unsafe {
49 ffi::g_source_add_child_source(self.to_glib_none().0, child_source.to_glib_none().0);
50 }
51 }
52
53 //#[doc(alias = "g_source_add_poll")]
54 //pub fn add_poll(&self, fd: /*Ignored*/&mut PollFD) {
55 // unsafe { TODO: call ffi:g_source_add_poll() }
56 //}
57
58 //#[doc(alias = "g_source_add_unix_fd")]
59 //pub fn add_unix_fd(&self, fd: i32, events: IOCondition) -> /*Unimplemented*/Basic: Pointer {
60 // unsafe { TODO: call ffi:g_source_add_unix_fd() }
61 //}
62
63 #[doc(alias = "g_source_destroy")]
64 pub fn destroy(&self) {
65 unsafe {
66 ffi::g_source_destroy(self.to_glib_none().0);
67 }
68 }
69
70 /// Checks whether a source is allowed to be called recursively.
71 /// see `GLib::Source::set_can_recurse()`.
72 ///
73 /// # Returns
74 ///
75 /// whether recursion is allowed.
76 #[doc(alias = "g_source_get_can_recurse")]
77 #[doc(alias = "get_can_recurse")]
78 pub fn can_recurse(&self) -> bool {
79 unsafe { from_glib(ffi::g_source_get_can_recurse(self.to_glib_none().0)) }
80 }
81
82 /// Gets the [`MainContext`][crate::MainContext] with which the source is associated.
83 ///
84 /// You can call this on a source that has been destroyed, provided
85 /// that the [`MainContext`][crate::MainContext] it was attached to still exists (in which
86 /// case it will return that [`MainContext`][crate::MainContext]). In particular, you can
87 /// always call this function on the source returned from
88 /// [`main_current_source()`][crate::main_current_source()]. But calling this function on a source
89 /// whose [`MainContext`][crate::MainContext] has been destroyed is an error.
90 ///
91 /// # Returns
92 ///
93 /// the #GMainContext with which the
94 /// source is associated, or [`None`] if the context has not
95 /// yet been added to a source.
96 #[doc(alias = "g_source_get_context")]
97 #[doc(alias = "get_context")]
98 pub fn context(&self) -> Option<MainContext> {
99 unsafe { from_glib_none(ffi::g_source_get_context(self.to_glib_none().0)) }
100 }
101
102 /// Gets a name for the source, used in debugging and profiling. The
103 /// name may be #NULL if it has never been set with `GLib::Source::set_name()`.
104 ///
105 /// # Returns
106 ///
107 /// the name of the source
108 #[doc(alias = "g_source_get_name")]
109 #[doc(alias = "get_name")]
110 pub fn name(&self) -> Option<crate::GString> {
111 unsafe { from_glib_none(ffi::g_source_get_name(self.to_glib_none().0)) }
112 }
113
114 /// Gets the priority of a source.
115 ///
116 /// # Returns
117 ///
118 /// the priority of the source
119 #[doc(alias = "g_source_get_priority")]
120 #[doc(alias = "get_priority")]
121 pub fn priority(&self) -> i32 {
122 unsafe { ffi::g_source_get_priority(self.to_glib_none().0) }
123 }
124
125 /// Gets the "ready time" of @self, as set by
126 /// `GLib::Source::set_ready_time()`.
127 ///
128 /// Any time before or equal to the current monotonic time (including 0)
129 /// is an indication that the source will fire immediately.
130 ///
131 /// # Returns
132 ///
133 /// the monotonic ready time, -1 for "never"
134 #[doc(alias = "g_source_get_ready_time")]
135 #[doc(alias = "get_ready_time")]
136 pub fn ready_time(&self) -> i64 {
137 unsafe { ffi::g_source_get_ready_time(self.to_glib_none().0) }
138 }
139
140 /// Gets the time to be used when checking this source. The advantage of
141 /// calling this function over calling [`monotonic_time()`][crate::monotonic_time()] directly is
142 /// that when checking multiple sources, GLib can cache a single value
143 /// instead of having to repeatedly get the system monotonic time.
144 ///
145 /// The time here is the system monotonic time, if available, or some
146 /// other reasonable alternative otherwise. See [`monotonic_time()`][crate::monotonic_time()].
147 ///
148 /// # Returns
149 ///
150 /// the monotonic time in microseconds
151 #[doc(alias = "g_source_get_time")]
152 #[doc(alias = "get_time")]
153 pub fn time(&self) -> i64 {
154 unsafe { ffi::g_source_get_time(self.to_glib_none().0) }
155 }
156
157 /// Returns whether @self has been destroyed.
158 ///
159 /// This is important when you operate upon your objects
160 /// from within idle handlers, but may have freed the object
161 /// before the dispatch of your idle handler.
162 ///
163 ///
164 ///
165 /// **⚠️ The following code is in C ⚠️**
166 ///
167 /// ```C
168 /// static gboolean
169 /// idle_callback (gpointer data)
170 /// {
171 /// SomeWidget *self = data;
172 ///
173 /// g_mutex_lock (&self->idle_id_mutex);
174 /// // do stuff with self
175 /// self->idle_id = 0;
176 /// g_mutex_unlock (&self->idle_id_mutex);
177 ///
178 /// return G_SOURCE_REMOVE;
179 /// }
180 ///
181 /// static void
182 /// some_widget_do_stuff_later (SomeWidget *self)
183 /// {
184 /// g_mutex_lock (&self->idle_id_mutex);
185 /// self->idle_id = g_idle_add (idle_callback, self);
186 /// g_mutex_unlock (&self->idle_id_mutex);
187 /// }
188 ///
189 /// static void
190 /// some_widget_init (SomeWidget *self)
191 /// {
192 /// g_mutex_init (&self->idle_id_mutex);
193 ///
194 /// // ...
195 /// }
196 ///
197 /// static void
198 /// some_widget_finalize (GObject *object)
199 /// {
200 /// SomeWidget *self = SOME_WIDGET (object);
201 ///
202 /// if (self->idle_id)
203 /// g_source_remove (self->idle_id);
204 ///
205 /// g_mutex_clear (&self->idle_id_mutex);
206 ///
207 /// G_OBJECT_CLASS (parent_class)->finalize (object);
208 /// }
209 /// ```
210 ///
211 /// This will fail in a multi-threaded application if the
212 /// widget is destroyed before the idle handler fires due
213 /// to the use after free in the callback. A solution, to
214 /// this particular problem, is to check to if the source
215 /// has already been destroy within the callback.
216 ///
217 ///
218 ///
219 /// **⚠️ The following code is in C ⚠️**
220 ///
221 /// ```C
222 /// static gboolean
223 /// idle_callback (gpointer data)
224 /// {
225 /// SomeWidget *self = data;
226 ///
227 /// g_mutex_lock (&self->idle_id_mutex);
228 /// if (!g_source_is_destroyed (g_main_current_source ()))
229 /// {
230 /// // do stuff with self
231 /// }
232 /// g_mutex_unlock (&self->idle_id_mutex);
233 ///
234 /// return FALSE;
235 /// }
236 /// ```
237 ///
238 /// Calls to this function from a thread other than the one acquired by the
239 /// [`MainContext`][crate::MainContext] the #GSource is attached to are typically
240 /// redundant, as the source could be destroyed immediately after this function
241 /// returns. However, once a source is destroyed it cannot be un-destroyed, so
242 /// this function can be used for opportunistic checks from any thread.
243 ///
244 /// # Returns
245 ///
246 /// [`true`] if the source has been destroyed
247 #[doc(alias = "g_source_is_destroyed")]
248 pub fn is_destroyed(&self) -> bool {
249 unsafe { from_glib(ffi::g_source_is_destroyed(self.to_glib_none().0)) }
250 }
251
252 //#[doc(alias = "g_source_modify_unix_fd")]
253 //pub fn modify_unix_fd(&self, tag: /*Unimplemented*/Basic: Pointer, new_events: IOCondition) {
254 // unsafe { TODO: call ffi:g_source_modify_unix_fd() }
255 //}
256
257 //#[doc(alias = "g_source_query_unix_fd")]
258 //pub fn query_unix_fd(&self, tag: /*Unimplemented*/Basic: Pointer) -> IOCondition {
259 // unsafe { TODO: call ffi:g_source_query_unix_fd() }
260 //}
261
262 /// Detaches @child_source from @self and destroys it.
263 ///
264 /// This API is only intended to be used by implementations of #GSource.
265 /// Do not call this API on a #GSource that you did not create.
266 /// ## `child_source`
267 /// a #GSource previously passed to
268 /// [`add_child_source()`][Self::add_child_source()].
269 #[doc(alias = "g_source_remove_child_source")]
270 pub fn remove_child_source(&self, child_source: &Source) {
271 unsafe {
272 ffi::g_source_remove_child_source(self.to_glib_none().0, child_source.to_glib_none().0);
273 }
274 }
275
276 //#[doc(alias = "g_source_remove_poll")]
277 //pub fn remove_poll(&self, fd: /*Ignored*/&mut PollFD) {
278 // unsafe { TODO: call ffi:g_source_remove_poll() }
279 //}
280
281 //#[doc(alias = "g_source_remove_unix_fd")]
282 //pub fn remove_unix_fd(&self, tag: /*Unimplemented*/Basic: Pointer) {
283 // unsafe { TODO: call ffi:g_source_remove_unix_fd() }
284 //}
285
286 //#[doc(alias = "g_source_remove_by_funcs_user_data")]
287 //pub fn remove_by_funcs_user_data(funcs: /*Ignored*/&mut SourceFuncs, user_data: /*Unimplemented*/Option<Basic: Pointer>) -> bool {
288 // unsafe { TODO: call ffi:g_source_remove_by_funcs_user_data() }
289 //}
290
291 //#[doc(alias = "g_source_remove_by_user_data")]
292 //pub fn remove_by_user_data(user_data: /*Unimplemented*/Option<Basic: Pointer>) -> bool {
293 // unsafe { TODO: call ffi:g_source_remove_by_user_data() }
294 //}
295}
296
297unsafe impl Send for Source {}
298unsafe impl Sync for Source {}