1
0
Fork 0

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

Part-of: <https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/2407>
This commit is contained in:
Bilal Elmoussaoui 2022-05-09 16:26:19 +02:00
parent 6aeb9eef4e
commit 7e9d9c7eb9
2 changed files with 81 additions and 0 deletions

View file

@ -25,6 +25,8 @@
#include "config.h" #include "config.h"
#include "core/meta-accel-parse.h" #include "core/meta-accel-parse.h"
#include "clutter/clutter-keyval.h"
#include "meta/util.h"
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -356,3 +358,78 @@ meta_parse_modifier (const char *accel,
*mask = combo.modifiers; *mask = combo.modifiers;
return TRUE; 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;
}

View file

@ -220,4 +220,8 @@ void meta_remove_debug_paint_flag (MetaDebugPaintFlag flag);
META_EXPORT META_EXPORT
MetaDebugPaintFlag meta_get_debug_paint_flags (void); MetaDebugPaintFlag meta_get_debug_paint_flags (void);
META_EXPORT
char * meta_accelerator_name (ClutterModifierType accelerator_mods,
unsigned int accelerator_key);
#endif /* META_UTIL_H */ #endif /* META_UTIL_H */