[mutter/bilelmoussaoui/gdk-x11-only: 6/6] core: Add a meta_accelerator_name helper




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

    core: Add a meta_accelerator_name helper

 src/core/meta-accel-parse.c       | 67 +++++++++++++++++++++++++++++++++++++++
 src/core/meta-accel-parse.h       |  4 +++
 src/core/meta-pad-action-mapper.c |  1 -
 3 files changed, 71 insertions(+), 1 deletion(-)
---
diff --git a/src/core/meta-accel-parse.c b/src/core/meta-accel-parse.c
index 0d34251afc..c8a8ec2c5e 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,69 @@ meta_parse_modifier (const char          *accel,
   *mask = combo.modifiers;
   return TRUE;
 }
+
+char * 
+meta_accelerator_name (ClutterModifierType accelerator_mods, 
+                       guint               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..a002034ce3 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, 
+                                 guint               accelerator_key);
+
 #endif /* META_ACCEL_PARSE_H */
diff --git a/src/core/meta-pad-action-mapper.c b/src/core/meta-pad-action-mapper.c
index 8c451b7844..93ceb5bbba 100644
--- a/src/core/meta-pad-action-mapper.c
+++ b/src/core/meta-pad-action-mapper.c
@@ -485,7 +485,6 @@ meta_pad_action_mapper_emulate_keybinding (MetaPadActionMapper *mapper,
   if (!accel || !*accel)
     return;
 
-  /* FIXME: This is appalling */
   if (!meta_parse_accelerator (accel, &combo)) 
     {
       g_warning ("\"%s\" is not a valid accelerator", accel);


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