[glib/wip/new-gsource: 8/8] Add a ghandle source



commit bc9403220e191dbc947eb65f608263cf3764a82e
Author: Ryan Lortie <desrt desrt ca>
Date:   Thu Sep 1 17:44:20 2011 -0600

    Add a ghandle source
    
    https://bugzilla.gnome.org/show_bug.cgi?id=658020

 glib/glib.symbols |    3 +
 glib/gmain.c      |  140 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 glib/gmain.h      |   29 +++++++++++
 3 files changed, 172 insertions(+), 0 deletions(-)
---
diff --git a/glib/glib.symbols b/glib/glib.symbols
index 51cb8f7..53f5c0f 100644
--- a/glib/glib.symbols
+++ b/glib/glib.symbols
@@ -385,6 +385,9 @@ g_file_open_tmp_utf8
 g_file_test_utf8
 g_mkstemp_utf8
 #endif
+g_handle_add
+g_handle_add_full
+g_handle_source_new
 g_hash_table_add
 g_hash_table_contains
 g_hash_table_destroy
diff --git a/glib/gmain.c b/glib/gmain.c
index b498432..159db2d 100644
--- a/glib/gmain.c
+++ b/glib/gmain.c
@@ -5260,6 +5260,146 @@ g_main_context_invoke (GMainContext *context,
                               function, data, NULL);
 }
 
+typedef struct
+{
+  GSource source;
+
+  GHandle handle;
+  guint   tag;
+} GHandleSource;
+
+static gboolean
+g_handle_source_dispatch (GSource     *source,
+                          GSourceFunc  callback,
+                          gpointer     user_data)
+{
+  GHandleSource *handle_source = (GHandleSource *) source;
+  GHandleSourceFunc func = (GHandleSourceFunc) callback;
+
+  if (!callback)
+    {
+      g_warning ("GHandleSource dispatched without callback\n"
+                 "You must call g_source_set_callback().");
+      return FALSE;
+    }
+
+  return (* func) (handle_source->handle, g_source_query_handle (source, handle_source->tag), user_data);
+}
+
+/**
+ * g_handle_source_new:
+ * @handle: a #GHandle
+ * @condition: IO conditions to watch for on @handle
+ *
+ * Creates a #GSource to watch for a particular IO condition on a file
+ * handle.
+ *
+ * The source will never close the handle -- you must do it yourself.
+ *
+ * Returns: the newly created #GSource
+ *
+ * Since: 2.32
+ **/
+GSource *
+g_handle_source_new (GHandle      handle,
+                     GIOCondition condition)
+{
+  static GSourceFuncs source_funcs = {
+    NULL, NULL, g_handle_source_dispatch, NULL
+  };
+  GHandleSource *handle_source;
+  GSource *source;
+
+  source = g_source_new (&source_funcs, sizeof (GHandleSource));
+  handle_source = (GHandleSource *) source;
+
+  handle_source->handle = handle;
+  handle_source->tag = g_source_add_handle (source, handle, condition);
+
+  return source;
+}
+
+/**
+ * g_handle_add_full:
+ * @priority: the priority of the source
+ * @handle: a #GHandle
+ * @condition: IO conditions to watch for on @handle
+ * @function: a #GHandleSourceFunc
+ * @user_data: data to pass to @function
+ * @notify: function to call when the idle is removed, or %NULL
+ *
+ * Sets a function to be called when the IO condition, as specified by
+ * @condition becomes true for @handle.
+ *
+ * This is the same as g_handle_add, except that it allows you to
+ * specify a non-default priority and a provide a #GDestroyNotify for
+ * @user_data.
+ *
+ * Returns: the ID (greater than 0) of the event source
+ *
+ * Since: 2.32
+ **/
+guint
+g_handle_add_full (gint              priority,
+                   GHandle           handle,
+                   GIOCondition      condition,
+                   GHandleSourceFunc function,
+                   gpointer          user_data,
+                   GDestroyNotify    notify)
+{
+  GSource *source;
+  guint id;
+
+  g_return_val_if_fail (function != NULL, 0);
+
+  source = g_handle_source_new (handle, condition);
+
+  if (priority != G_PRIORITY_DEFAULT_IDLE)
+    g_source_set_priority (source, priority);
+
+  g_source_set_callback (source, (GSourceFunc) function, user_data, notify);
+  id = g_source_attach (source, NULL);
+  g_source_unref (source);
+
+  return id;
+}
+
+/**
+ * g_handle_add:
+ * @handle: a #GHandle
+ * @condition: IO conditions to watch for on @handle
+ * @function: a #GPollFDFunc
+ * @user_data: data to pass to @function
+ *
+ * Sets a function to be called when the IO condition, as specified by
+ * @condition becomes true for @handle.
+ *
+ * @function will be called when the specified IO condition becomes
+ * %TRUE.  The function is expected to clear whatever event caused the
+ * IO condition to become true and return %TRUE in order to be notified
+ * when it happens again.  If @function returns %FALSE then the watch
+ * will be cancelled.
+ *
+ * The return value of this function can be passed to g_source_remove()
+ * to cancel the watch at any time that it exists.
+ *
+ * The source will never close the handle -- you must do it yourself.
+ *
+ * Returns: the ID (greater than 0) of the event source
+ *
+ * Since: 2.32
+ **/
+guint
+g_handle_add (GHandle           handle,
+              GIOCondition      condition,
+              GHandleSourceFunc function,
+              gpointer          user_data)
+{
+  return g_handle_add_full (G_PRIORITY_DEFAULT, handle, condition, function, user_data, NULL);
+}
+
+/* g_main_context_invoke() */
+
 /**
  * g_main_context_invoke_full:
  * @context: (allow-none): a #GMainContext, or %NULL
diff --git a/glib/gmain.h b/glib/gmain.h
index 70ae009..5900963 100644
--- a/glib/gmain.h
+++ b/glib/gmain.h
@@ -165,6 +165,22 @@ typedef gboolean (*GSourceFunc)       (gpointer user_data);
 typedef void     (*GChildWatchFunc)   (GPid     pid,
                                        gint     status,
                                        gpointer user_data);
+
+/**
+ * GHandleSourceFunc:
+ * @handle: the #GHandle that triggered the event
+ * @condition: the IO conditions reported on @handle
+ * @user_data: user data passed to g_handle_add()
+ *
+ * The type of functions to be called when a #GHandle watch source
+ * triggers.
+ *
+ * Returns: %FALSE if the source should be removed
+ **/
+typedef gboolean (*GHandleSourceFunc) (GHandle      handle,
+                                       GIOCondition condition,
+                                       gpointer     user_data);
+
 struct _GSource
 {
   /*< private >*/
@@ -455,6 +471,8 @@ GSource *g_idle_source_new        (void);
 GSource *g_child_watch_source_new (GPid pid);
 GSource *g_timeout_source_new     (guint interval);
 GSource *g_timeout_source_new_seconds (guint interval);
+GSource *g_handle_source_new      (GHandle      handle,
+                                   GIOCondition condition);
 
 /* Miscellaneous functions
  */
@@ -502,6 +520,17 @@ guint    g_idle_add_full            (gint            priority,
                                      GDestroyNotify  notify);
 gboolean g_idle_remove_by_data      (gpointer        data);
 
+guint    g_handle_add               (GHandle           handle,
+                                     GIOCondition      condition,
+                                     GHandleSourceFunc function,
+                                     gpointer          user_data);
+guint    g_handle_add_full          (gint              priority,
+                                     GHandle           handle,
+                                     GIOCondition      condition,
+                                     GHandleSourceFunc function,
+                                     gpointer          user_data,
+                                     GDestroyNotify    notify);
+
 void     g_main_context_invoke_full (GMainContext   *context,
                                      gint            priority,
                                      GSourceFunc     function,



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]