[virt-tools-list] [PATCH virt-viewer 3/4] window: Use loop instead of multiple ifs

Fabiano Fidêncio fabiano at fidencio.org
Wed Jun 22 08:59:07 UTC 2016


On Wed, Jun 22, 2016 at 8:17 AM, Pavel Grunt <pgrunt at redhat.com> wrote:
> Binds modifier's mask and key, also fixes a compile time warning:
>  warning: cast from 'gchar *' (aka 'char *') to 'guint *' (aka 'unsigned
>   int *') increases required alignment from 1 to 4 [-Wcast-align]
>  return (guint*)g_array_free(a, FALSE);
> ---
>  src/virt-viewer-window.c | 39 ++++++++++++++++++---------------------
>  1 file changed, 18 insertions(+), 21 deletions(-)
>
> diff --git a/src/virt-viewer-window.c b/src/virt-viewer-window.c
> index 60a1675..6bf0a2e 100644
> --- a/src/virt-viewer-window.c
> +++ b/src/virt-viewer-window.c
> @@ -608,36 +608,33 @@ virt_viewer_menu_add_combo(VirtViewerWindow *self, GtkMenu *menu,
>  static guint*
>  accel_key_to_keys(const GtkAccelKey *key)
>  {
> -    guint val;
> -    GArray *a = g_array_new(FALSE, FALSE, sizeof(guint));
> +    guint i;
> +    guint *val, *keys;
> +    const struct {
> +        const guint mask;
> +        const guint key;
> +    } modifiers[] = {
> +        {GDK_SHIFT_MASK, GDK_KEY_Shift_L},
> +        {GDK_CONTROL_MASK, GDK_KEY_Control_L},
> +        {GDK_MOD1_MASK, GDK_KEY_Alt_L},
> +    };
>
>      g_warn_if_fail((key->accel_mods &
>                      ~(GDK_SHIFT_MASK | GDK_CONTROL_MASK | GDK_MOD1_MASK)) == 0);
>
> +    keys = val = g_new(guint, 5); /* up to 3 modifiers, key and the stop symbol */
>      /* first, send the modifiers */
> -    if (key->accel_mods & GDK_SHIFT_MASK) {
> -        val = GDK_KEY_Shift_L;
> -        g_array_append_val(a, val);
> -    }
> -
> -    if (key->accel_mods & GDK_CONTROL_MASK) {
> -        val = GDK_KEY_Control_L;
> -        g_array_append_val(a, val);
> -    }
> -
> -    if (key->accel_mods & GDK_MOD1_MASK) {
> -        val = GDK_KEY_Alt_L;
> -        g_array_append_val(a, val);
> +    for (i = 0; i < G_N_ELEMENTS(modifiers); i++) {
> +        if (key->accel_mods & modifiers[i].mask)
> +            *val++ = modifiers[i].key;
>      }
>
>      /* only after, the non-modifier key (ctrl-t, not t-ctrl) */
> -    val = key->accel_key;
> -    g_array_append_val(a, val);
> -
> -    val = GDK_KEY_VoidSymbol;
> -    g_array_append_val(a, val);
> +    *val++ = key->accel_key;
> +    /* stop symbol */
> +    *val = GDK_KEY_VoidSymbol;
>
> -    return (guint*)g_array_free(a, FALSE);
> +    return keys;
>  }
>
>  struct accelCbData
> --
> 2.9.0
>
> _______________________________________________
> virt-tools-list mailing list
> virt-tools-list at redhat.com
> https://www.redhat.com/mailman/listinfo/virt-tools-list


Acked-by: Fabiano Fidêncio <fidencio at redhat.com>
-- 
Fabiano Fidêncio




More information about the virt-tools-list mailing list