1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files)
// DO NOT EDIT

use crate::{translate::*, MainContext};

crate::wrapper! {
    /// The `GSource` struct is an opaque data type
    /// representing an event source.
    #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
    pub struct Source(Shared<ffi::GSource>);

    match fn {
        ref => |ptr| ffi::g_source_ref(ptr),
        unref => |ptr| ffi::g_source_unref(ptr),
        type_ => || ffi::g_source_get_type(),
    }
}

impl Source {
    //#[doc(alias = "g_source_new")]
    //pub fn new(source_funcs: /*Ignored*/&mut SourceFuncs, struct_size: u32) -> Source {
    //    unsafe { TODO: call ffi:g_source_new() }
    //}

    /// Adds @child_source to @self as a "polled" source; when @self is
    /// added to a #GMainContext, @child_source will be automatically added
    /// with the same priority, when @child_source is triggered, it will
    /// cause @self to dispatch (in addition to calling its own
    /// callback), and when @self is destroyed, it will destroy
    /// @child_source as well. (@self will also still be dispatched if
    /// its own prepare/check functions indicate that it is ready.)
    ///
    /// If you don't need @child_source to do anything on its own when it
    /// triggers, you can call g_source_set_dummy_callback() on it to set a
    /// callback that does nothing (except return [`true`] if appropriate).
    ///
    /// @self will hold a reference on @child_source while @child_source
    /// is attached to it.
    ///
    /// This API is only intended to be used by implementations of #GSource.
    /// Do not call this API on a #GSource that you did not create.
    /// ## `child_source`
    /// a second #GSource that @self should "poll"
    #[doc(alias = "g_source_add_child_source")]
    pub fn add_child_source(&self, child_source: &Source) {
        unsafe {
            ffi::g_source_add_child_source(self.to_glib_none().0, child_source.to_glib_none().0);
        }
    }

    //#[doc(alias = "g_source_add_poll")]
    //pub fn add_poll(&self, fd: /*Ignored*/&mut PollFD) {
    //    unsafe { TODO: call ffi:g_source_add_poll() }
    //}

    //#[doc(alias = "g_source_add_unix_fd")]
    //pub fn add_unix_fd(&self, fd: i32, events: IOCondition) -> /*Unimplemented*/Basic: Pointer {
    //    unsafe { TODO: call ffi:g_source_add_unix_fd() }
    //}

    #[doc(alias = "g_source_destroy")]
    pub fn destroy(&self) {
        unsafe {
            ffi::g_source_destroy(self.to_glib_none().0);
        }
    }

    /// Checks whether a source is allowed to be called recursively.
    /// see g_source_set_can_recurse().
    ///
    /// # Returns
    ///
    /// whether recursion is allowed.
    #[doc(alias = "g_source_get_can_recurse")]
    #[doc(alias = "get_can_recurse")]
    pub fn can_recurse(&self) -> bool {
        unsafe { from_glib(ffi::g_source_get_can_recurse(self.to_glib_none().0)) }
    }

    /// Gets the #GMainContext with which the source is associated.
    ///
    /// You can call this on a source that has been destroyed, provided
    /// that the #GMainContext it was attached to still exists (in which
    /// case it will return that #GMainContext). In particular, you can
    /// always call this function on the source returned from
    /// g_main_current_source(). But calling this function on a source
    /// whose #GMainContext has been destroyed is an error.
    ///
    /// # Returns
    ///
    /// the #GMainContext with which the
    ///               source is associated, or [`None`] if the context has not
    ///               yet been added to a source.
    #[doc(alias = "g_source_get_context")]
    #[doc(alias = "get_context")]
    pub fn context(&self) -> Option<MainContext> {
        unsafe { from_glib_none(ffi::g_source_get_context(self.to_glib_none().0)) }
    }

    /// Gets a name for the source, used in debugging and profiling.  The
    /// name may be #NULL if it has never been set with g_source_set_name().
    ///
    /// # Returns
    ///
    /// the name of the source
    #[doc(alias = "g_source_get_name")]
    #[doc(alias = "get_name")]
    pub fn name(&self) -> Option<crate::GString> {
        unsafe { from_glib_none(ffi::g_source_get_name(self.to_glib_none().0)) }
    }

    /// Gets the priority of a source.
    ///
    /// # Returns
    ///
    /// the priority of the source
    #[doc(alias = "g_source_get_priority")]
    #[doc(alias = "get_priority")]
    pub fn priority(&self) -> i32 {
        unsafe { ffi::g_source_get_priority(self.to_glib_none().0) }
    }

    /// Gets the "ready time" of @self, as set by
    /// g_source_set_ready_time().
    ///
    /// Any time before or equal to the current monotonic time (including 0)
    /// is an indication that the source will fire immediately.
    ///
    /// # Returns
    ///
    /// the monotonic ready time, -1 for "never"
    #[doc(alias = "g_source_get_ready_time")]
    #[doc(alias = "get_ready_time")]
    pub fn ready_time(&self) -> i64 {
        unsafe { ffi::g_source_get_ready_time(self.to_glib_none().0) }
    }

    /// Gets the time to be used when checking this source. The advantage of
    /// calling this function over calling g_get_monotonic_time() directly is
    /// that when checking multiple sources, GLib can cache a single value
    /// instead of having to repeatedly get the system monotonic time.
    ///
    /// The time here is the system monotonic time, if available, or some
    /// other reasonable alternative otherwise.  See g_get_monotonic_time().
    ///
    /// # Returns
    ///
    /// the monotonic time in microseconds
    #[doc(alias = "g_source_get_time")]
    #[doc(alias = "get_time")]
    pub fn time(&self) -> i64 {
        unsafe { ffi::g_source_get_time(self.to_glib_none().0) }
    }

    /// Returns whether @self has been destroyed.
    ///
    /// This is important when you operate upon your objects
    /// from within idle handlers, but may have freed the object
    /// before the dispatch of your idle handler.
    ///
    ///
    ///
    /// **⚠️ The following code is in C ⚠️**
    ///
    /// ```C
    /// static gboolean
    /// idle_callback (gpointer data)
    /// {
    ///   SomeWidget *self = data;
    ///
    ///   g_mutex_lock (&self->idle_id_mutex);
    ///   // do stuff with self
    ///   self->idle_id = 0;
    ///   g_mutex_unlock (&self->idle_id_mutex);
    ///
    ///   return G_SOURCE_REMOVE;
    /// }
    ///
    /// static void
    /// some_widget_do_stuff_later (SomeWidget *self)
    /// {
    ///   g_mutex_lock (&self->idle_id_mutex);
    ///   self->idle_id = g_idle_add (idle_callback, self);
    ///   g_mutex_unlock (&self->idle_id_mutex);
    /// }
    ///
    /// static void
    /// some_widget_init (SomeWidget *self)
    /// {
    ///   g_mutex_init (&self->idle_id_mutex);
    ///
    ///   // ...
    /// }
    ///
    /// static void
    /// some_widget_finalize (GObject *object)
    /// {
    ///   SomeWidget *self = SOME_WIDGET (object);
    ///
    ///   if (self->idle_id)
    ///     g_source_remove (self->idle_id);
    ///
    ///   g_mutex_clear (&self->idle_id_mutex);
    ///
    ///   G_OBJECT_CLASS (parent_class)->finalize (object);
    /// }
    /// ```
    ///
    /// This will fail in a multi-threaded application if the
    /// widget is destroyed before the idle handler fires due
    /// to the use after free in the callback. A solution, to
    /// this particular problem, is to check to if the source
    /// has already been destroy within the callback.
    ///
    ///
    ///
    /// **⚠️ The following code is in C ⚠️**
    ///
    /// ```C
    /// static gboolean
    /// idle_callback (gpointer data)
    /// {
    ///   SomeWidget *self = data;
    ///
    ///   g_mutex_lock (&self->idle_id_mutex);
    ///   if (!g_source_is_destroyed (g_main_current_source ()))
    ///     {
    ///       // do stuff with self
    ///     }
    ///   g_mutex_unlock (&self->idle_id_mutex);
    ///
    ///   return FALSE;
    /// }
    /// ```
    ///
    /// Calls to this function from a thread other than the one acquired by the
    /// #GMainContext the #GSource is attached to are typically redundant, as the
    /// source could be destroyed immediately after this function returns. However,
    /// once a source is destroyed it cannot be un-destroyed, so this function can be
    /// used for opportunistic checks from any thread.
    ///
    /// # Returns
    ///
    /// [`true`] if the source has been destroyed
    #[doc(alias = "g_source_is_destroyed")]
    pub fn is_destroyed(&self) -> bool {
        unsafe { from_glib(ffi::g_source_is_destroyed(self.to_glib_none().0)) }
    }

    //#[doc(alias = "g_source_modify_unix_fd")]
    //pub fn modify_unix_fd(&self, tag: /*Unimplemented*/Basic: Pointer, new_events: IOCondition) {
    //    unsafe { TODO: call ffi:g_source_modify_unix_fd() }
    //}

    //#[doc(alias = "g_source_query_unix_fd")]
    //pub fn query_unix_fd(&self, tag: /*Unimplemented*/Basic: Pointer) -> IOCondition {
    //    unsafe { TODO: call ffi:g_source_query_unix_fd() }
    //}

    /// Detaches @child_source from @self and destroys it.
    ///
    /// This API is only intended to be used by implementations of #GSource.
    /// Do not call this API on a #GSource that you did not create.
    /// ## `child_source`
    /// a #GSource previously passed to
    ///     g_source_add_child_source().
    #[doc(alias = "g_source_remove_child_source")]
    pub fn remove_child_source(&self, child_source: &Source) {
        unsafe {
            ffi::g_source_remove_child_source(self.to_glib_none().0, child_source.to_glib_none().0);
        }
    }

    //#[doc(alias = "g_source_remove_poll")]
    //pub fn remove_poll(&self, fd: /*Ignored*/&mut PollFD) {
    //    unsafe { TODO: call ffi:g_source_remove_poll() }
    //}

    //#[doc(alias = "g_source_remove_unix_fd")]
    //pub fn remove_unix_fd(&self, tag: /*Unimplemented*/Basic: Pointer) {
    //    unsafe { TODO: call ffi:g_source_remove_unix_fd() }
    //}

    //#[doc(alias = "g_source_remove_by_funcs_user_data")]
    //pub fn remove_by_funcs_user_data(funcs: /*Ignored*/&mut SourceFuncs, user_data: /*Unimplemented*/Option<Basic: Pointer>) -> bool {
    //    unsafe { TODO: call ffi:g_source_remove_by_funcs_user_data() }
    //}

    //#[doc(alias = "g_source_remove_by_user_data")]
    //pub fn remove_by_user_data(user_data: /*Unimplemented*/Option<Basic: Pointer>) -> bool {
    //    unsafe { TODO: call ffi:g_source_remove_by_user_data() }
    //}
}

unsafe impl Send for Source {}
unsafe impl Sync for Source {}