Commit 6733b153 authored by Ell's avatar Ell

app: add gimp_idle_run_async[_full]()

In gimp-utils, add new gimp_idle_run_async() and
gimp_idle_run_async_full() functions, having the same signature as
gimp_parallel_run_async[_full](), but running the async function in
an idle source on the main thread, instead of in a separate thread.

The 'priority' parameter of gimp_idle_run_async_full() specifies
the idle-source priority.
parent f25a8934
......@@ -58,6 +58,7 @@
#include "gimp.h"
#include "gimp-utils.h"
#include "gimpasync.h"
#include "gimpcontext.h"
#include "gimperror.h"
......@@ -976,6 +977,108 @@ gimp_suggest_trc_for_component_type (GimpComponentType component_type,
return new_trc;
}
typedef struct
{
GimpAsync *async;
gint idle_id;
GimpRunAsyncFunc func;
gpointer user_data;
GDestroyNotify user_data_destroy_func;
} GimpIdleRunAsyncData;
static GimpIdleRunAsyncData *
gimp_idle_run_async_data_new (void)
{
return g_slice_new0 (GimpIdleRunAsyncData);
}
static void
gimp_idle_run_async_data_free (GimpIdleRunAsyncData *data)
{
g_signal_handlers_disconnect_by_data (data->async, data);
if (data->user_data && data->user_data_destroy_func)
data->user_data_destroy_func (data->user_data);
if (! gimp_async_is_stopped (data->async))
gimp_async_abort (data->async);
g_object_unref (data->async);
g_slice_free (GimpIdleRunAsyncData, data);
}
static void
gimp_idle_run_async_cancel (GimpAsync *async,
GimpIdleRunAsyncData *data)
{
g_source_remove (data->idle_id);
}
static gboolean
gimp_idle_run_async_idle (GimpIdleRunAsyncData *data)
{
g_signal_handlers_block_by_func (data->async,
gimp_idle_run_async_cancel,
data);
data->func (data->async, data->user_data);
g_signal_handlers_unblock_by_func (data->async,
gimp_idle_run_async_cancel,
data);
if (gimp_async_is_stopped (data->async))
{
data->user_data = NULL;
return G_SOURCE_REMOVE;
}
return G_SOURCE_CONTINUE;
}
GimpAsync *
gimp_idle_run_async (GimpRunAsyncFunc func,
gpointer user_data)
{
return gimp_idle_run_async_full (G_PRIORITY_DEFAULT_IDLE, func,
user_data, NULL);
}
GimpAsync *
gimp_idle_run_async_full (gint priority,
GimpRunAsyncFunc func,
gpointer user_data,
GDestroyNotify user_data_destroy_func)
{
GimpIdleRunAsyncData *data;
g_return_val_if_fail (func != NULL, NULL);
data = gimp_idle_run_async_data_new ();
data->func = func;
data->user_data = user_data;
data->user_data_destroy_func = user_data_destroy_func;
data->async = gimp_async_new ();
g_signal_connect (data->async, "cancel",
G_CALLBACK (gimp_idle_run_async_cancel),
data);
data->idle_id = g_idle_add_full (
priority,
(GSourceFunc) gimp_idle_run_async_idle,
data,
(GDestroyNotify) gimp_idle_run_async_data_free);
return g_object_ref (data->async);
}
/* debug stuff */
......
......@@ -107,6 +107,13 @@ gint gimp_g_list_compare (GList *list1,
GimpTRCType gimp_suggest_trc_for_component_type (GimpComponentType component_type,
GimpTRCType old_trc);
GimpAsync * gimp_idle_run_async (GimpRunAsyncFunc func,
gpointer user_data);
GimpAsync * gimp_idle_run_async_full (gint priority,
GimpRunAsyncFunc func,
gpointer user_data,
GDestroyNotify user_data_destroy_func);
GimpImage * gimp_create_image_from_buffer (Gimp *gimp,
GeglBuffer *buffer,
const gchar *image_name);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment