### ----------------------------------------------------
### -- Gnome::Glib
### -- Licenses: Artistic-2.0
### -- Authors: Marcel Timmerman
### -- File: lib/Gnome/Glib/Source.rakumod
### ----------------------------------------------------
### -- Chunk 2 of 3
 (gpointer data)
{
  SomeWidget *self = data;

  GDK_THREADS_ENTER ();
  // do stuff with self
  self->idle_id = 0;
  GDK_THREADS_LEAVE ();

  return G_SOURCE_REMOVE;
}

static void
some_widget_do_stuff_later (SomeWidget *self)
{
  self->idle_id = g_idle_add (idle_callback, self);
}

static void
some_widget_finalize (GObject *object)
{
  SomeWidget *self = SOME_WIDGET (object);

  if (self->idle_id)
    g_source_remove (self->idle_id);

  G_OBJECT_CLASS (parent_class)->finalize (object);
}
=end comment

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.

=begin comment
static gboolean
idle_callback (gpointer data)
{
  SomeWidget *self = data;

  GDK_THREADS_ENTER ();
  if (!g_source_is_destroyed (g_main_current_source ()))
    {
      // do stuff with self
    }
  GDK_THREADS_LEAVE ();

  return FALSE;
}
=end comment

Calls to this function from a thread other than the one acquired by the B<Gnome::Glib::MainContext> the B<Gnome::Glib::Source> 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: C<True> if the source has been destroyed

  method is-destroyed ( --> Bool )

=end pod

method is-destroyed ( --> Bool ) {
  g_source_is_destroyed(self._get-native-object-no-reffing).Bool
}

sub g_source_is_destroyed (
  N-GObject $source --> gboolean
) is native(&glib-lib)
  { * }
}}

#`{{
#-------------------------------------------------------------------------------
#TM:0:modify-unix-fd:
=begin pod
=head2 modify-unix-fd

Updates the event mask to watch for the fd identified by I<tag>.

I<tag> is the tag returned from C<add-unix-fd()>.

If you want to remove a fd, don't set its event mask to zero. Instead, call C<g-source-remove-unix-fd()>.

This API is only intended to be used by implementations of B<Gnome::Glib::Source>. Do not call this API on a B<Gnome::Glib::Source> that you did not create.

As the name suggests, this function is not available on Windows.

  method modify-unix-fd ( Pointer $tag, GIOCondition $new_events )

=item Pointer $tag; the tag from C<add-unix-fd()>
=item GIOCondition $new_events; the new event mask to watch
=end pod

method modify-unix-fd ( Pointer $tag, GIOCondition $new_events ) {

  g_source_modify_unix_fd(
    self._get-native-object-no-reffing, $tag, $new_events
  );
}

sub g_source_modify_unix_fd (
  N-GObject $source, gpointer $tag, GIOCondition $new_events
) is native(&glib-lib)
  { * }

#-------------------------------------------------------------------------------
#TM:0:query-unix-fd:
=begin pod
=head2 query-unix-fd

Queries the events reported for the fd corresponding to I<tag> on I<source> during the last poll.

The return value of this function is only defined when the function is called from the check or dispatch functions for I<source>.

This API is only intended to be used by implementations of B<Gnome::Glib::Source>. Do not call this API on a B<Gnome::Glib::Source> that you did not create.

As the name suggests, this function is not available on Windows.

Returns: the conditions reported on the fd

  method query-unix-fd ( Pointer $tag --> GIOCondition )

=item Pointer $tag; the tag from C<add-unix-fd()>
=end pod

method query-unix-fd ( Pointer $tag --> GIOCondition ) {

  g_source_query_unix_fd(
    self._get-native-object-no-reffing, $tag
  )
}

sub g_source_query_unix_fd (
  N-GObject $source, gpointer $tag --> GIOCondition
) is native(&glib-lib)
  { * }
}}

#-------------------------------------------------------------------------------
#TM:0:_g_source_ref:
#`{{
=begin pod
=head2 ref

Increases the reference count on a source by one.

Returns: I<source>

  method ref ( --> N-GObject )

=end pod

method ref ( --> N-GObject ) {

  g_source_ref(
    self._get-native-object-no-reffing,
  )
}
}}

sub _g_source_ref (
  N-GObject $source --> N-GObject
) is native(&glib-lib)
  is symbol('g_source_ref')
  { * }

#-------------------------------------------------------------------------------
#TM:0:remove:
=begin pod
=head2 remove

Removes the source with the given ID from the default main context. You must use C<destroy()> for sources added to a non-default main context.

The ID of a B<Gnome::Glib::Source> is given by C<g-source-get-id()>, or will be returned by the functions C<g-source-attach()>, C<g-idle-add()>, C<g-idle-add-full()>, C<g-timeout-add()>, C<g-timeout-add-full()>, C<g-child-watch-add()>, C<g-child-watch-add-full()>, C<g-io-add-watch()>, and C<g-io-add-watch-full()>.

It is a programmer error to attempt to remove a non-existent source.

More specifically: source IDs can be reissued after a source has been destroyed and therefore it is never valid to use this function with a source ID which may have already been removed. An example is when scheduling an idle to run in another thread with C<g-idle-add()>: the idle may already have run and been removed by the time this function is called on its (now invalid) source ID. This source ID may have been reissued, leading to the operation being performed against the wrong source.

Returns: For historical reasons, this function always returns C<True>

  method remove ( UInt $tag --> Bool )

=item UInt $tag; the ID of the source to remove.
=end pod

method remove ( UInt $tag --> Bool ) {

  g_source_remove(
    self._get-native-object-no-reffing, $tag
  ).Bool
}

sub g_source_remove (
  guint $tag --> gboolean
) is native(&glib-lib)
  { * }

#`{{
#-------------------------------------------------------------------------------
#TM:0:remove-by-funcs-user-data:
=begin pod
=head2 remove-by-funcs-user-data

Removes a source from the default main loop context given the source functions and user data. If multiple sources exist with the same source functions and user data, only one will be destroyed.

Returns: C<True> if a source was found and removed.

  method remove-by-funcs-user-data ( N-GSourceFuncs $funcs, Pointer $user_data --> Bool )

=item N-GSourceFuncs $funcs; The I<source-funcs> passed to C<new()>
=item Pointer $user_data; the user data for the callback
=end pod

method remove-by-funcs-user-data ( N-GSourceFuncs $funcs, Pointer $user_data --> Bool ) {

  g_source_remove_by_funcs_user_data(
    self._get-native-object-no-reffing, $funcs, $user_data
  ).Bool
}

sub g_source_remove_by_funcs_user_data (
  N-GSourceFuncs $funcs, gpointer $user_data --> gboolean
) is native(&glib-lib)
  { * }
}}
#`{{
#-------------------------------------------------------------------------------
#TM:0:remove-by-user-data:
=begin pod
=head2 remove-by-user-data

Removes a source from the default main loop context given the user data for the callback. If multiple sources exist with the same user data, only one will be destroyed.

Returns: C<True> if a source was found and removed.

  method remove-by-user-data ( Pointer $user_data --> Bool )

=item Pointer $user_data; the user-data for the callback.
=end pod

method remove-by-user-data ( Pointer $user_data --> Bool ) {

  g_source_remove_by_user_data(
    self._get-native-object-no-reffing, $user_data
  ).Bool
}

sub g_source_remove_by_user_data (
  gpointer $user_data --> gboolean
) is native(&glib-lib)
  { * }
}}

#-------------------------------------------------------------------------------
#TM:0:remove-child-source:
=begin pod
=head2 remove-child-source

Detaches I<child-source> from I<source> and destroys it.

This API is only intended to be used by implementations of B<Gnome::Glib::Source>. Do not call this API on a B<Gnome::Glib::Source> that you did not create.

  method remove-child-source ( N-GObject $child_source )

=item N-GObject $child_source; a B<Gnome::Glib::Source> previously passed to C<add-child-source()>.
=end pod

method remove-child-source ( N-GObject $child_source ) {

  g_source_remove_child_source(
    self._get-native-object-no-reffing, $child_source
  );
}

sub g_source_remove_child_source (
  N-GObject $source, N-GObject $child_source
) is native(&glib-lib)
  { * }

#`{{
#-------------------------------------------------------------------------------
#TM:0:remove-poll:
=begin pod
=head2 remove-poll

Removes a file descriptor from the set of file descriptors polled for this source.

This API is only intended to be used by implementations of B<Gnome::Glib::Source>. Do not call this API on a B<Gnome::Glib::Source> that you did not create.

  method remove-poll ( GPollFD $fd )

=item GPollFD $fd; a B<Gnome::Glib::PollFD> structure previously passed to C<add-poll()>.
=end pod

method remove-poll ( GPollFD $fd ) {

  g_source_remove_poll(
    self._get-native-object-no-reffing, $fd
  );
}

sub g_source_remove_poll (
  N-GObject $source, GPollFD $fd
) is native(&glib-lib)
  { * }

#-------------------------------------------------------------------------------
#TM:0:remove-unix-fd:
=begin pod
=head2 remove-unix-fd

Reverses the effect of a previous call to C<add-unix-fd()>.

You only need to call this if you want to remove an fd from being watched while keeping the same source around. In the normal case you will just want to destroy the source.

This API is only intended to be used by implementations of B<Gnome::Glib::Source>. Do not call this API on a B<Gnome::Glib::Source> that you did not create.

As the name suggests, this function is not available on Windows.

  method remove-unix-fd ( Pointer $tag )

=item Pointer $tag; the tag from C<add-unix-fd()>
=end pod

method remove-unix-fd ( Pointer $tag ) {

  g_source_remove_unix_fd(
    self._get-native-object-no-reffing, $tag
  );
}

sub g_source_remove_unix_fd (
  N-GObject $source, gpointer $tag
) is native(&glib-lib)
  { * }
}}


#-------------------------------------------------------------------------------
#TM:0:set-callback:
=begin pod
=head2 set-callback

Sets the callback function for a source. The callback for a source is called from the source's dispatch function.

=comment The exact type of I<func> depends on the type of source; ie. you should not count on I<func> being called with I<data> as its first parameter. Cast I<func> with C<G-SOURCE-FUNC()> to avoid warnings about incompatible function types.

=comment See [memory management of sources][mainloop-memory-management] for details on how to handle memory management of I<data>.

Typically, you won't use this function. Instead use functions specific to the type of source you are using, such as C<g-idle-add()> or C<g-timeout-add()>.

It is safe to call this function multiple times on a source which has already been attached to a context. The changes will take effect for the next time the source is dispatched after this call returns.

  method set-callback (
    Any:D $handler-object, Str:D $method, Str $method-notify = Str,
    *%user-options
  )

=item $handler-object; User object where both methods are defined
=item Str $method; name of callback handler
=item Str $method-notify; name of callback handler. Ignored when $method-notify is undefined. This function is called when the source is removed.
=item %user-options; optional named arguments to be provided to both callbacks

=end pod

method set-callback (
  Any:D $handler-object, Str:D $method, Str $method-notify = Str,
  *%user-options
) {
  die X::Gnome.new(:message("Method $method or handler object not found"))
    unless $handler-object.^can($method);

  if ?$method-notify and $handler-object.can($method-notify) {
    g_source_set_callback(
      self._get-native-object-no-reffing,
      sub ( gpointer $d --> gboolean ) {
        $handler-object."$method"(|%user-options);
      }, gpointer,
      sub ( gpointer $d ) {
        $handler-object."$method-notify"(|%user-options);
      },
    )
  }

  else {

    g_source_set_callback(
      self._get-native-object-no-reffing,
      sub ( gpointer $d --> gboolean ) {
        $handler-object."$method"(|%user-options);
      }, gpointer,
      Callable
    )
  }
}

sub g_source_set_callback (
  N-GObject $source,
  Callable $func ( gpointer --> gboolean ),
  gpointer $data,
  Callable $notify ( gpointer )
) is native(&glib-lib)
  { * }

#`{{
#-------------------------------------------------------------------------------
# TM:0:set-callback-indirect:
=begin pod
=head2 set-callback-indirect

Sets the callback function storing the data as a refcounted callback "object". This is used internally. Note that calling C<set-callback-indirect()> assumes an initial reference count on I<callback-data>, and thus I<callback-funcs>->unref will eventually be called once more than I<callback-funcs>->ref.

It is safe to call this function multiple times on a source which has already been attached to a context. The changes will take effect for the next time the source is dispatched after this call returns.

  method set-callback-indirect ( Pointer $callback_data, N-GObjectCallbackFuncs $callback_funcs )

=item Pointer $callback_data; pointer to callback data "object"
=item N-GObjectCallbackFuncs $callback_funcs; functions for reference counting I<callback-data> and getting the callback and data
=end pod

method set-callback-indirect ( Pointer $callback_data, N-GObjectCallbackFuncs $callback_funcs ) {

  g_source_set_callback_indirect(
    self._get-native-object-no-reffing, $callback_data, $callback_funcs
  );
}

sub g_source_set_callback_indirect (
  N-GObject $source, gpointer $callback_data, N-GObjectCallbackFuncs $callback_funcs
) is native(&glib-lib)
  { * }

#-------------------------------------------------------------------------------
#TM:0:set-can-recurse:
=begin pod
=head2 set-can-recurse

Sets whether a source can be called recursively. If I<can-recurse> is C<True>, then while the source is being dispatched then this source will be processed normally. Otherwise, all processing of this source is blocked until the dispatch function returns.

  method set-can-recurse ( Bool $can_recurse )

=item Bool $can_recurse; whether recursion is allowed for this source
=end pod

method set-can-recurse ( Bool $can_recurse ) {

  g_source_set_can_recurse(
    self._get-native-object-no-reffing, $can_recurse
  );
}

sub g_source_set_can_recurse (
  N-GObject $source, gboolean $can_recurse
) is native(&glib-lib)
  { * }

#-------------------------------------------------------------------------------
#TM:0:set-funcs:
=begin pod
=head2 set-funcs

Sets the source functions (can be used to override default implementations) of an unattached source.

  method set-funcs ( N-GSourceFuncs $funcs )

=item N-GSourceFuncs $funcs; the new B<Gnome::Glib::SourceFuncs>
=end pod

method set-funcs ( N-GSourceFuncs $funcs ) {

  g_source_set_funcs(
    self._get-native-object-no-reffing, $funcs
  );
}

sub g_source_set_funcs (
  N-GObject $source, N-GSourceFuncs $funcs
) is native(&glib-lib)
  { * }
}}

#-------------------------------------------------------------------------------
#TM:0:set-name:
=begin pod
=head2 set-name

Sets a name for the source, used in debugging and profiling. The name defaults to B<NULL>.

The source name should describe in a human-readable way what the source does. For example, "X11 event queue" or "GTK+ repaint idle handler" or whatever it is.

It is permitted to call this function multiple times, but is not recommended due to the potential performance impact. For example, one could change the name in the "check" function of a B<Gnome::Glib::SourceFuncs> to include details like the event type in the source name.

Use caution if changing the name while another thread may be accessing it with C<get-name()>; that function does not copy the value, and changing the value will free it while the other thread may be attempting to use it.

  method set-name ( Str $name )

=item Str $name; debug name for the source
=end pod

method set-name ( Str $name ) {
  g_source_set_name(self._get-native-object-no-reffing, $name);
}

sub g_source_set_name (
  N-GObject $source, gchar-ptr $name
) is native(&glib-lib)
  { * }

#-------------------------------------------------------------------------------
#TM:0:set-name-by-id:
=begin pod
=head2 set-name-by-id

Sets the name of a source using its ID.

This is a convenience utility to set source names from the return value of C<g-idle-add()>, C<timeout-add()>, etc.

It is a programmer error to attempt to set the name of a non-existent source.

More specifically: source IDs can be reissued after a source has been destroyed and therefore it is never valid to use this function with a source ID which may have already been removed. An example is when scheduling an idle to run in another thread with C<g-idle-add()>: the idle may already have run and been removed by the time this function is called on its (now invalid) source ID. This source ID may have been reissued, leading to the operation being performed against the wrong source.

  method set-name-by-id ( UInt $tag, Str $name )

=item UInt $tag; a B<Gnome::Glib::Source> ID
=item Str $name; debug name for the source
=end pod

method set-name-by-id ( UInt $tag, Str $name ) {

  g_source_set_name_by_id(
    self._get-native-object-no-reffing, $tag, $name
  );
}

sub g_source_set_name_by_id (
  guint $tag, gchar-ptr $name
) is native(&glib-lib)
  { * }

#-------------------------------------------------------------------------------
#TM:0:set-priority:
=begin pod
=head2 set-priority

Sets the priority of a source. While the main loop is being run, a source will be dispatched if it is ready to be dispatched and no sources at a higher (numerically smaller) priority are ready to be dispatched.

A child source always has the same priority as its parent. It is not permitted to change the priority of a source once it has been added as a child of another source.

  method set-priority ( Int $priority )

=item Int $priority; the new priority.
=end pod

method set-priority ( Int $priority ) {

  g_source_set_priority(
    self._get-native-object-no-reffing, $priority
  );
}

sub g_source_set_priority (
  N-GObject $source, gint $priority
) is native(&glib-lib)
  { * }

#-------------------------------------------------------------------------------
#TM:0:set-ready-time:
=begin pod
=head2 set-ready-time

Sets a B<Gnome::Glib::Source> to be dispatched when the given monotonic time is reached (or passed). If the monotonic time is in the past (as it always will be if I<ready-time> is 0) then the source will be dispatched immediately.

If I<ready-time> is -1 then the source is never woken up on the basis of the passage of time.

Dispatching the source does not reset the ready time. You should do so yourself, from the source dispatch function.

Note that if you have a pair of sources where the ready time of one suggests that it will be delivered first but the priority for the other suggests that it would be delivered first, and the ready time for both sources is reached during the same main context iteration, then the order of dispatch is undefined.

It is a no-op to call this function on a B<Gnome::Glib::Source> which has already been destroyed with C<destroy()>.

This API is only intended to be used by implementations of B<Gnome::Glib::Source>. Do not call this API on a B<Gnome::Glib::Source> that you did not create.

  method set-ready-time ( Int $ready_time )

=item Int $ready_time; the monotonic time at which the source will be ready, 0 for "immediately", -1 for "never"
=end pod

method set-ready-time ( Int $ready_time ) {

  g_source_set_ready_time(
    self._get-native-object-no-reffing, $ready_time
  );
}

sub g_source_set_ready_time (
  N-GObject $source, gint64 $ready_time
) is native(&glib-lib)
  { * }

#-------------------------------------------------------------------------------
#TM:0:timeout-add:
=begin pod
=head2 timeout-add

Sets a function to be called at regular intervals, with the default priority, C<G-PRIORITY-DEFAULT>. The function is called repeatedly until it returns C<False>, at which point the timeout is automatically destroyed and the function will not be called again. The first call to the function will be at the end of the first I<$interval>.

Note that timeout functions may be delayed, due to the processing of other event sources. Thus they should not be relied on for precise timing. After each call to the timeout function, the time of the next timeout is recalculated based on the current time and the given interval (it does not try to 'catch up' time lost in delays).

=comment See [memory management of sources][mainloop-memory-management] for details on how to handle the return value and memory management of I<data>.

If you want to have a timer in the "seconds" range and do not care about the exact time of the first call of the timer, use the C<timeout-add-seconds()> function; this function allows for more optimizations and more efficient system power usage.

=comment This internally creates a main loop source using C<g-timeout-source-new()> and attaches it to the global B<Gnome::Glib::MainContext> using C<attach()>, so the callback will be invoked in whichever thread is running that main context. You can do these steps manually if you need greater control or to use a custom main context.

The interval given is in terms of monotonic time, not wall clock time. See C<get-monotonic-time()>.

Returns: the ID (greater than 0) of the event source.

  method timeout-add (
    UInt $interval,
    Any:D $handler-object, Str:D $method, *%user-options
    --> UInt
  )

=item UInt $interval; the time between calls to the function, in milliseconds (1/1000ths of a second)
=item $handler-object; User object where $method is defined
=item Str $method; name of callback handler
=item %user-options; optional named arguments to be provided to the callback
=end pod


method timeout-add (
  UInt $interval, Any:D $handler-object, Str:D $method, *%user-options
  --> UInt
) {
  die X::Gnome.new(:message("Method $method or handler object not found"))
    unless $handler-object.^can($method);

  g_timeout_add(
    $interval,
    sub ( gpointer $d --> gboolean ) {
      $handler-object."$method"(|%user-options);
    },
    gpointer
  )
}

sub g_timeout_add (
  guint $interval,
  Callable $func ( gpointer --> gboolean ),
  gpointer $data --> guint
) is native(&glib-lib)
  { * }

#-------------------------------------------------------------------------------
#TM:0:timeout-add-full:
=begin pod
=head2 timeout-add-full

Sets a function to be called at regular intervals, with the given priority. The function is called repeatedly until it returns C<False>, at which point the timeout is automatically destroyed and the function will not be called again. The I<notify> function is called when the timeout is destroyed. The first call to the function will be at the end of the first I<interval>.

Note that timeout functions may be delayed, due to the processing of other event sources. Thus they should not be relied on for precise timing. After each call to the timeout function, the time of the next timeout is recalculated based on the current time and the given interval (it does not try to 'catch up' time lost in delays).

=comment See [memory management of sources][mainloop-memory-management] for details on how to handle the return value and memory management of I<data>.

This internally creates a main loop source using C<g-timeout-source-new()> and attaches it to the global B<Gnome::Glib::MainContext> using C<attach()>, so the callback will be invoked in whichever thread is running that main context. You can do these steps manually if you need greater control or to use a custom main context.

The interval given is in terms of monotonic time, not wall clock time. See C<g-get-monotonic-time()>.

Returns: the ID (greater than 0) of the event source.

  method timeout-add-full (
    Int $priority, UInt $interval,
    Any:D $handler-object, Str:D $method, Str $method-notify = Str,
    *%user-options
    --> UInt
  )

=item Int $priority; the priority of the timeout source. Typically this will be in the range between C<G-PRIORITY-DEFAULT> and C<G-PRIORITY-HIGH>.
=item UInt $interval; the time between calls to the function, in milliseconds (1/1000ths of a second)
=item $handler-object; User object where both methods are defined
=item Str $method; name of callback handler
=item Str $method-notify; name of callback handler. Ignored when $method-notify is undefined. This function is called when the source is removed.
=item %user-options; optional named arguments to be provided to both callbacks
=end pod

method timeout-add-full (
  Int $priority, UInt $interval,
  Any:D $handler-object, Str:D $method, Str $method-notify = Str,
  *%user-options
  --> UInt
) {
  die X::Gnome.new(:message("Method $method or handler object not found"))
    unless $handler-object.^can($method);

  if ?$method-notify and $handler-object.can($method-notify) {
    g_timeout_add_full(
      $priority, $interval,
      sub ( gpointer $d --> gboolean ) {
        $handler-object."$method"(|%user-options);
      }, gpointer,
      sub ( gpointer $d ) {
        $handler-object."$method-notify"(|%user-options);
      },
    )
  }

  else {
    g_timeout_add_full(
      $priority, $interval,
      sub ( gpointer $d --> gboolean ) {
        $handler-object."$method"(|%user-options);
      }, gpointer,
      Nil
    )
  }
}

sub g_timeout_add_full (
  gint $priority, guint $interval,
  Callable $func ( gpointer --> gboolean ), gpointer $data,
  Callable $notify ( gpointer )
  --> guint
) is native(&glib-lib)
  { * }

#-------------------------------------------------------------------------------
#TM:0:timeout-add-seconds:
=begin pod
=head2 timeout-add-seconds

Sets a function to be called at regular intervals with the default priority, C<G-PRIORITY-DEFAULT>. The function is called repeatedly until it returns C<False>, at which point the timeout is automatically destroyed and the function will not be called again.

This internally creates a main loop source using C<g-timeout-source-new-seconds()> and attaches it to the main loop context using C<attach()>. You can do these steps manually if you need greater control. Also see C<timeout-add-seconds-full()>.

Note that the first call of the timer may not be precise for timeouts of one second. If you need finer precision and have such a timeout, you may want to use C<timeout-add()> instead.

=comment See [memory management of sources][mainloop-memory-management] for details on how to handle the return value and memory management of I<data>.

The interval given is in terms of monotonic time, not wall clock time. See C<g-get-monotonic-time()>.

Returns: the ID (greater than 0) of the event source.

  method timeout-add-seconds (
    UInt $interval, Any:D $handler-object, Str:D $method, *%user-options
    --> UInt
  )

=item UInt $interval; the time between calls to the function, in seconds
=item $handler-object; User object where $method is defined
=item Str $method; name of callback handler
=item %user-options; optional named arguments to be provided to the callback
=end pod

method timeout-add-seconds (
  UInt $interval, Any:D $handler-object, Str:D $method, *%user-options
  --> UInt
) {
  g_timeout_add_seconds(
    $interval,
    sub ( gpointer $d --> gboolean ) {
      $handler-object."$method"(|%user-options);
    },
    gpointer
  )
}

sub g_timeout_add_seconds (
  guint $interval,
  Callable $func ( gpointer --> gboolean ),
  gpointer $data --> guint
) is native(&glib-lib)
  { * }

#-------------------------------------------------------------------------------
#TM:0:timeout-add-seconds-full:
=begin pod
=head2 timeout-add-seconds-full

Sets a function to be called at regular intervals, with I<priority>. The function is called repeatedly until it returns C<False>, at which point the timeout is automatically destroyed and the function will not be called again.

Unlike C<timeout-add()>, this function operates at whole second granularity. The initial starting point of the timer is determined by the implementation and the implementation is expected to group multiple timers together so that they fire all at the same time. To allow this grouping, the I<$interval> to the first timer is rounded and can deviate up to one second from the specified interval. Subsequent timer iterations will generally run at the specified interval.

Note that timeout functions may be delayed, due to the processing of other event sources. Thus they should not be relied on for precise timing. After each call to the timeout function, the time of the next timeout is recalculated based on the current time and the given I<$interval>

=comment See [memory management of sources][mainloop-memory-management] for details on how to handle the return value and memory management of I<data>.

If you want timing more precise than whole seconds, use C<timeout-add()> instead.

The grouping of timers to fire at the same time results in a more power and CPU efficient behavior so if your timer is in multiples of seconds and you don't require the first timer exactly one second from now, the use of C<timeout-add-seconds()> is preferred over C<timeout-add()>.

This internally creates a main loop source using C<timeout-source-new-seconds()> and attaches it to the main loop context using C<attach()>. You can do these steps manually if you need greater control.

The interval given is in terms of monotonic time, not wall clock time. See C<get-monotonic-time()>.

Returns: the ID (greater than 0) of the event source.

  method timeout-add-seconds-full (
    Int $priority, UInt $interval,
    Any:D $handler-object, Str:D $method, Str $method-notify = Str,
    *%user-options
    --> UInt
  )

=item Int $priority; the priority of the timeout source. Typically this will be in the range between C<G-PRIORITY-DEFAULT> and C<G-PRIORITY-HIGH>.
=item UInt $interval; the time between calls to the function, in seconds
=item $handler-object; User object where both methods are defined
=item Str $method; name of callback handler
=item Str $method-notify; name of callback handler. Ignored when $method-notify is undefined. This function is called when the source is removed.
=item %user-options; optional named arguments to be provided to both callbacks
=end pod

method timeout-add-seconds-full (
  Int $priority, UInt $interval,
  Any:D $handler-object, Str:D $method, Str $method-notify = Str,
  *%user-options
  --> UInt
) {
  die X::Gnome.new(:message("Method $method or handler object not found"))
    unless $handler-object.^can($method);

  if ?$method-notify and $handler-object.can($method-notify) {
    g_timeout_add_seconds_full(
      $priority, $interval,
      sub ( gpointer $d --> gboolean ) {
        $handler-object."$method"(|%user-options);
      }, gpointer,
      sub ( gpointer $d ) {
        $handler-object."$method-notify"(|%user-options);
      },
    )
  }

  else {
    g_timeout_add_seconds_full(
      $priority, $interval,
      sub ( gpointer $d --> gboolean ) {
        $handler-object."$method"(|%user-options);
      }, gpointer,
      Nil
    )
  }
}

sub g_timeout_add_seconds_full (
  gint $priority, guint $interval,
  Callable $func ( gpointer --> gboolean ),
  gpointer $data,
  Callable $notify ( gpointer )
  --> guint
) is native(&glib-lib)
  { * }

#-------------------------------------------------------------------------------
#TM:1:_g_timeout_source_new:
#`{{
=begin pod
=head2 g-timeout-source-new

Creates a new timeout source.

The source will not initially be associated with any B<Gnome::Glib::MainContext> and must be added to one with C<attach()> before it will be executed.

The interval given is in terms of monotonic time, not wall clock time. See C<g-get-monotonic-time()>.

Returns: the newly-created timeout source

  method g-timeout-source-new ( UInt $interval --> N-GObject )

=item UInt $interval; the timeout interval in milliseconds.
=end pod

method g-timeout-source-new ( UInt $interval --> N-GObject ) {

  g_timeout_source_new(
    self._get-native-object-no-reffing, $interval
  )
}
}}

sub _g_timeout_source_new ( guint $interval --> N-GObject )
  is native(&glib-lib)
  is symbol('g_timeout_source_new')
  { * }

#-------------------------------------------------------------------------------
#TM:1:_g_timeout_source_new_seconds:
#`{{
=begin pod
=head2 g-timeout-source-new-seconds

Creates a new timeout source.

The source will not initially be associated with any B<Gnome::Glib::MainContext> and must be added to one with C<attach()> before it will be executed.

The scheduling granularity/accuracy of this timeout source will be in seconds.

The interval given is in terms of monotonic time, not wall clock time. See C<g-get-monotonic-time()>.

Returns: the newly-created timeout source

  method g-timeout-source-new-seconds ( UInt $interval --> N-GObject )

=item UInt $interval; the timeout interval in seconds
=end pod

method g-timeout-source-new-seconds ( UInt $interval --> N-GObject ) {

  g_timeout_source_new_seconds(
    self._get-native-object-no-reffing, $interval
  )
}
}}

sub _g_timeout_source_new_seconds (
  guint $interval --> N-GObject
) is native(&glib-lib)
  is symbol('g_timeout_source_new_seconds')
  { * }

#-------------------------------------------------------------------------------
#TM:0:_g_source_unref:
#`{{
=begin pod
=head2 unref

Decreases the reference count of a source by one. If the resulting reference count is zero the source and associated memory will be destroyed.

  method unref ( )

=end pod

method unref ( ) {

  g_source_unref(
    self._get-native-object-no-reffing,
  );
}
}}

sub _g_source_unref (
  N-GObject $source
) is native(&glib-lib)
  is symbol('g_source_unref')
  { * }

#`{{
#-------------------------------------------------------------------------------
#TM:1:_g_source_new:
#`{{
=begin pod
=head2 _g_source_new

Creates a new B<Gnome::Glib::Source> structure. The size is specified to allow creating structures derived from B<Gnome::Glib::Source> that contain additional data. The size passed in must be at least `sizeof (N-GObject)`.

The source will not initially be associated with any B<Gnome::Glib::MainContext> and must be added to one with C<attach()> before it will be executed.

Returns: the newly-created B<Gnome::Glib::Source>.

  method