[libdazzle] shortcuts: add chord table foreach func



commit 48d6f80e60fb6c0d5bdcd774b59614a047610b70
Author: Christian Hergert <chergert redhat com>
Date:   Mon Jun 12 18:46:25 2017 -0700

    shortcuts: add chord table foreach func

 src/shortcuts/dzl-shortcut-chord.c |   32 ++++++++++++++++++++++++++++++++
 src/shortcuts/dzl-shortcut-chord.h |    7 +++++++
 2 files changed, 39 insertions(+), 0 deletions(-)
---
diff --git a/src/shortcuts/dzl-shortcut-chord.c b/src/shortcuts/dzl-shortcut-chord.c
index 3a3e0b2..f626c5f 100644
--- a/src/shortcuts/dzl-shortcut-chord.c
+++ b/src/shortcuts/dzl-shortcut-chord.c
@@ -689,3 +689,35 @@ dzl_shortcut_chord_get_nth_key (const DzlShortcutChord *self,
         *modifier = 0;
     }
 }
+
+/**
+ * dzl_shortcut_chord_foreach:
+ * @self: a #DzlShortcutChordTable
+ * @foreach_func: (scope call) (closure foreach_data): A callback for each chord
+ * @foreach_data: user data for @foreach_func
+ *
+ * This function will call @foreach_func for each chord in the table.
+ */
+void
+dzl_shortcut_chord_table_foreach (const DzlShortcutChordTable  *self,
+                                  DzlShortcutChordTableForeach  foreach_func,
+                                  gpointer                      foreach_data)
+{
+  g_return_if_fail (foreach_func != NULL);
+
+  if (self == NULL)
+    return;
+
+  /*
+   * Walk backwards just in case the caller somehow thinks it is okay to
+   * remove items while iterating the list. We don't officially support that
+   * (which is why self is const), but this is just defensive.
+   */
+
+  for (guint i = self->len; i > 0; i--)
+    {
+      const DzlShortcutChordTableEntry *entry = &self->entries[i-1];
+
+      foreach_func (&entry->chord, entry->data, foreach_data);
+    }
+}
diff --git a/src/shortcuts/dzl-shortcut-chord.h b/src/shortcuts/dzl-shortcut-chord.h
index 923f686..a0bfae7 100644
--- a/src/shortcuts/dzl-shortcut-chord.h
+++ b/src/shortcuts/dzl-shortcut-chord.h
@@ -37,6 +37,10 @@ typedef enum
 typedef struct _DzlShortcutChord      DzlShortcutChord;
 typedef struct _DzlShortcutChordTable DzlShortcutChordTable;
 
+typedef void (*DzlShortcutChordTableForeach) (const DzlShortcutChord *chord,
+                                              gpointer                chord_data,
+                                              gpointer                user_data);
+
 GType                   dzl_shortcut_chord_get_type            (void);
 DzlShortcutChord       *dzl_shortcut_chord_new_from_event      (const GdkEventKey           *event);
 DzlShortcutChord       *dzl_shortcut_chord_new_from_string     (const gchar                 *accelerator);
@@ -76,6 +80,9 @@ const DzlShortcutChord *dzl_shortcut_chord_table_lookup_data   (DzlShortcutChord
                                                                 gpointer                     data);
 guint                   dzl_shortcut_chord_table_size          (const DzlShortcutChordTable *self);
 void                    dzl_shortcut_chord_table_printf        (const DzlShortcutChordTable *self);
+void                    dzl_shortcut_chord_table_foreach       (const DzlShortcutChordTable  *self,
+                                                                DzlShortcutChordTableForeach  foreach_func,
+                                                                gpointer                      foreach_data);
 GType                   dzl_shortcut_match_get_type            (void);
 
 G_DEFINE_AUTOPTR_CLEANUP_FUNC (DzlShortcutChord, dzl_shortcut_chord_free)


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