gmain: Factor out common GIdleSource code

This allows it to be reused and extended (internally) a little more.
This commit introduces no functional changes, but allows for more easy
additions in a following commit.

It introduces `GIdleSource` as a simple wrapper around `GSource`, which
will be extended in a following commit.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
This commit is contained in:
Philip Withnall 2022-05-27 13:22:10 +01:00
parent af02a614a2
commit c1477f79e7

View File

@ -235,6 +235,7 @@
/* Types */ /* Types */
typedef struct _GIdleSource GIdleSource;
typedef struct _GTimeoutSource GTimeoutSource; typedef struct _GTimeoutSource GTimeoutSource;
typedef struct _GChildWatchSource GChildWatchSource; typedef struct _GChildWatchSource GChildWatchSource;
typedef struct _GUnixSignalWatchSource GUnixSignalWatchSource; typedef struct _GUnixSignalWatchSource GUnixSignalWatchSource;
@ -332,6 +333,11 @@ struct _GMainLoop
gint ref_count; /* (atomic) */ gint ref_count; /* (atomic) */
}; };
struct _GIdleSource
{
GSource source;
};
struct _GTimeoutSource struct _GTimeoutSource
{ {
GSource source; GSource source;
@ -6012,6 +6018,23 @@ g_idle_dispatch (GSource *source,
return again; return again;
} }
static GSource *
idle_source_new (void)
{
GSource *source;
GIdleSource *idle_source;
source = g_source_new (&g_idle_funcs, sizeof (GIdleSource));
idle_source = (GIdleSource *) source;
g_source_set_priority (source, G_PRIORITY_DEFAULT_IDLE);
/* Set a default name on the source, just in case the caller does not. */
g_source_set_static_name (source, "GIdleSource");
return source;
}
/** /**
* g_idle_source_new: * g_idle_source_new:
* *
@ -6027,16 +6050,34 @@ g_idle_dispatch (GSource *source,
**/ **/
GSource * GSource *
g_idle_source_new (void) g_idle_source_new (void)
{
return idle_source_new ();
}
static guint
idle_add_full (gint priority,
GSourceFunc function,
gpointer data,
GDestroyNotify notify)
{ {
GSource *source; GSource *source;
guint id;
source = g_source_new (&g_idle_funcs, sizeof (GSource)); g_return_val_if_fail (function != NULL, 0);
g_source_set_priority (source, G_PRIORITY_DEFAULT_IDLE);
/* Set a default name on the source, just in case the caller does not. */ source = idle_source_new ();
g_source_set_static_name (source, "GIdleSource");
return source; if (priority != G_PRIORITY_DEFAULT_IDLE)
g_source_set_priority (source, priority);
g_source_set_callback (source, function, data, notify);
id = g_source_attach (source, NULL);
TRACE (GLIB_IDLE_ADD (source, g_main_context_default (), id, priority, function, data));
g_source_unref (source);
return id;
} }
/** /**
@ -6070,24 +6111,7 @@ g_idle_add_full (gint priority,
gpointer data, gpointer data,
GDestroyNotify notify) GDestroyNotify notify)
{ {
GSource *source; return idle_add_full (priority, function, data, notify);
guint id;
g_return_val_if_fail (function != NULL, 0);
source = g_idle_source_new ();
if (priority != G_PRIORITY_DEFAULT_IDLE)
g_source_set_priority (source, priority);
g_source_set_callback (source, function, data, notify);
id = g_source_attach (source, NULL);
TRACE (GLIB_IDLE_ADD (source, g_main_context_default (), id, priority, function, data));
g_source_unref (source);
return id;
} }
/** /**