[mutter/bilelmoussaoui/gdk-x11-only: 15/17] core: Add meta_accelerator_name




commit 85ff910c75b6cd556c218d0fcd73a98661ff4396
Author: Bilal Elmoussaoui <belmouss redhat com>
Date:   Mon May 9 16:26:19 2022 +0200

    core: Add meta_accelerator_name
    
    As we have switched to using meta_parse_accelerator in PadActionMapper.
    We need a function that does the other direction for the client side
    usage. See
    https://gitlab.gnome.org/GNOME/gnome-shell/-/blob/f1d50a4a/js/ui/padOsd.js#L107

 src/core/meta-accel-parse.c | 76 +++++++++++++++++++++++++++++++++++++++++++++
 src/core/meta-accel-parse.h |  4 +++
 2 files changed, 80 insertions(+)
---
diff --git a/src/core/meta-accel-parse.c b/src/core/meta-accel-parse.c
index 0d34251afc..c0a5fa4a28 100644
--- a/src/core/meta-accel-parse.c
+++ b/src/core/meta-accel-parse.c
@@ -25,6 +25,7 @@
 #include "config.h"
 
 #include "core/meta-accel-parse.h"
+#include "clutter/clutter-keyval.h"
 
 #include <stdlib.h>
 #include <string.h>
@@ -356,3 +357,78 @@ meta_parse_modifier (const char          *accel,
   *mask = combo.modifiers;
   return TRUE;
 }
+
+/**
+ * meta_accelerator_name:
+ * @accelerator_mods: Accelerator modifier mask.
+ * @accelerator_key: Accelerator keyval.
+ * 
+ * Convert an accelerator keyval and modifier mask into a string parsable by `meta_parse_accelerator`.
+ * 
+ * Returns: The accelerator name.
+ */
+char *
+meta_accelerator_name (ClutterModifierType accelerator_mods,
+                       unsigned int        accelerator_key)
+{
+#define TXTLEN(s) sizeof (s) - 1
+  static const struct {
+    guint mask;
+    const char *text;
+    gsize text_len;
+  } mask_text[] = {
+    { CLUTTER_SHIFT_MASK,   "<Shift>",   TXTLEN ("<Shift>") },
+    { CLUTTER_CONTROL_MASK, "<Control>", TXTLEN ("<Control>") },
+    { CLUTTER_MOD1_MASK,    "<Alt>",     TXTLEN ("<Alt>") },
+    { CLUTTER_META_MASK,    "<Meta>",    TXTLEN ("<Meta>") },
+    { CLUTTER_SUPER_MASK,   "<Super>",   TXTLEN ("<Super>") },
+    { CLUTTER_HYPER_MASK,   "<Hyper>",   TXTLEN ("<Hyper>") }
+  };
+#undef TXTLEN
+
+  ClutterModifierType saved_mods;
+  guint l;
+  guint name_len;
+  const char *keyval_name;
+  char *accelerator;
+  int i;
+  unsigned int lower_key;
+
+  accelerator_mods &= CLUTTER_MODIFIER_MASK;
+
+  clutter_keyval_convert_case (accelerator_key, &lower_key, NULL);
+  keyval_name = clutter_keyval_name (lower_key);
+  if (!keyval_name)
+    keyval_name = "";
+
+  name_len = strlen (keyval_name);
+
+  saved_mods = accelerator_mods;
+  for (i = 0; i < G_N_ELEMENTS (mask_text); i++)
+    {
+      if (accelerator_mods & mask_text[i].mask)
+        name_len += mask_text[i].text_len;
+    }
+
+  if (name_len == 0)
+    return g_strdup (keyval_name);
+
+  name_len += 1; /* NUL byte */
+  accelerator = g_new (char, name_len);
+
+  accelerator_mods = saved_mods;
+  l = 0;
+  for (i = 0; i < G_N_ELEMENTS (mask_text); i++)
+    {
+      if (accelerator_mods & mask_text[i].mask)
+        {
+          strcpy (accelerator + l, mask_text[i].text);
+          l += mask_text[i].text_len;
+        }
+    }
+
+  strcpy (accelerator + l, keyval_name);
+  accelerator[name_len - 1] = '\0';
+
+  return accelerator;
+}
\ No newline at end of file
diff --git a/src/core/meta-accel-parse.h b/src/core/meta-accel-parse.h
index 12cf3f9821..6c323f3be4 100644
--- a/src/core/meta-accel-parse.h
+++ b/src/core/meta-accel-parse.h
@@ -41,4 +41,8 @@ gboolean meta_parse_accelerator (const char   *accel,
 gboolean meta_parse_modifier    (const char          *accel,
                                  MetaVirtualModifier *mask);
 
+META_EXPORT
+char * meta_accelerator_name    (ClutterModifierType accelerator_mods,
+                                 unsigned int        accelerator_key);
+
 #endif /* META_ACCEL_PARSE_H */


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