| 1 |
/* keyhash.c |
| 2 |
* Copyright (C) 2012 Red Hat, Inc12 Red Hat, Inc |
| 3 |
* Authors: Matthias Clasen |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Library General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Library General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Library General Public |
| 16 |
* License along with this library. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <gtk/gtk.h> |
| 20 |
#include <gdk/gdkkeysyms.h> |
| 21 |
#include "../../gtk/gtkkeyhash.h" |
| 22 |
#include "../../gtk/gtkprivate.h" |
| 23 |
|
| 24 |
static gint count; |
| 25 |
|
| 26 |
static void |
| 27 |
counting_destroy (gpointer data) |
| 28 |
{ |
| 29 |
count++; |
| 30 |
} |
| 31 |
|
| 32 |
static void |
| 33 |
test_basic (void) |
| 34 |
{ |
| 35 |
GtkKeyHash *hash; |
| 36 |
GSList *keys; |
| 37 |
|
| 38 |
count = 0; |
| 39 |
hash = _gtk_key_hash_new (gdk_keymap_get_default (), counting_destroy); |
| 40 |
|
| 41 |
keys = _gtk_key_hash_lookup (hash, 0, 0, 0, 0); |
| 42 |
g_assert (keys == NULL); |
| 43 |
|
| 44 |
_gtk_key_hash_add_entry (hash, 1, 0, NULL); |
| 45 |
_gtk_key_hash_add_entry (hash, 1, 1, NULL); |
| 46 |
_gtk_key_hash_add_entry (hash, 2, 0, NULL); |
| 47 |
_gtk_key_hash_add_entry (hash, 3, 0, NULL); |
| 48 |
_gtk_key_hash_add_entry (hash, 4, 0, NULL); |
| 49 |
|
| 50 |
_gtk_key_hash_free (hash); |
| 51 |
|
| 52 |
g_assert_cmpint (count, ==, 5); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
#if 0 |
| 57 |
typedef struct |
| 58 |
{ |
| 59 |
guint keyval; |
| 60 |
GdkModifierType modifiers; |
| 61 |
} Entry; |
| 62 |
|
| 63 |
static void |
| 64 |
test_lookup (GtkKeyHash *hash, |
| 65 |
guint keyval, |
| 66 |
GdkModifierType modifiers, |
| 67 |
GdkModifierType mask, |
| 68 |
gint n_results, |
| 69 |
...) |
| 70 |
{ |
| 71 |
va_list ap; |
| 72 |
gint d; |
| 73 |
GSList *res, *l; |
| 74 |
gint i; |
| 75 |
GdkKeymapKey *keys; |
| 76 |
gint n_keys; |
| 77 |
|
| 78 |
gdk_keymap_get_entries_for_keyval (gdk_keymap_get_default (), keyval, &keys, &n_keys); |
| 79 |
if (n_keys == 0) |
| 80 |
return; |
| 81 |
|
| 82 |
res = _gtk_key_hash_lookup (hash, keys[0].keycode, modifiers, mask, keys[0].group); |
| 83 |
g_free (keys); |
| 84 |
|
| 85 |
g_assert_cmpint (g_slist_length (res), ==, n_results); |
| 86 |
|
| 87 |
va_start (ap, n_results); |
| 88 |
for (i = 0, l = res; i < n_results; i++, l = l->next) |
| 89 |
{ |
| 90 |
d = va_arg (ap, int); |
| 91 |
g_assert_cmpint (d, ==, GPOINTER_TO_INT (l->data)); |
| 92 |
} |
| 93 |
va_end (ap); |
| 94 |
|
| 95 |
g_slist_free (res); |
| 96 |
} |
| 97 |
|
| 98 |
static void |
| 99 |
add_entries (GtkKeyHash *hash, |
| 100 |
Entry *entries) |
| 101 |
{ |
| 102 |
gint i; |
| 103 |
|
| 104 |
for (i = 0; entries[i].keyval; i++) |
| 105 |
_gtk_key_hash_add_entry (hash, entries[i].keyval, entries[i].modifiers, GINT_TO_POINTER (i+1)); |
| 106 |
} |
| 107 |
|
| 108 |
#define DEFAULT_MASK (GDK_CONTROL_MASK \ |
| 109 |
| GDK_SHIFT_MASK \ |
| 110 |
| GDK_MOD1_MASK \ |
| 111 |
| GDK_SUPER_MASK \ |
| 112 |
| GDK_HYPER_MASK \ |
| 113 |
| GDK_META_MASK) |
| 114 |
|
| 115 |
static void |
| 116 |
test_match (void) |
| 117 |
{ |
| 118 |
GtkKeyHash *hash; |
| 119 |
static Entry entries[] = { |
| 120 |
{ GDK_KEY_a, GDK_CONTROL_MASK }, |
| 121 |
{ GDK_KEY_a, GDK_CONTROL_MASK|GDK_SHIFT_MASK } , |
| 122 |
{ GDK_KEY_b, GDK_MOD1_MASK|GDK_CONTROL_MASK }, |
| 123 |
{ GDK_KEY_F10, 0 }, |
| 124 |
{ 0, 0 } |
| 125 |
}; |
| 126 |
|
| 127 |
hash = _gtk_key_hash_new (gdk_keymap_get_default (), NULL); |
| 128 |
add_entries (hash, entries); |
| 129 |
|
| 130 |
test_lookup (hash, GDK_KEY_a, GDK_CONTROL_MASK, DEFAULT_MASK, 4, 1, 1, 2, 2); |
| 131 |
test_lookup (hash, GDK_KEY_A, GDK_CONTROL_MASK, DEFAULT_MASK, 4, 1, 1, 2, 2); |
| 132 |
test_lookup (hash, GDK_KEY_a, GDK_MOD1_MASK, DEFAULT_MASK, 0); |
| 133 |
test_lookup (hash, GDK_KEY_F10, 0, DEFAULT_MASK, 4, 4, 4, 4, 4); |
| 134 |
test_lookup (hash, GDK_KEY_F10, GDK_SHIFT_MASK, DEFAULT_MASK, 4, 4, 4, 4, 4); |
| 135 |
|
| 136 |
_gtk_key_hash_free (hash); |
| 137 |
} |
| 138 |
|
| 139 |
static gboolean |
| 140 |
hyper_equals_super (void) |
| 141 |
{ |
| 142 |
GdkModifierType mods1, mods2; |
| 143 |
|
| 144 |
mods1 = GDK_HYPER_MASK; |
| 145 |
gdk_keymap_map_virtual_modifiers (gdk_keymap_get_default (), &mods1); |
| 146 |
mods1 = mods1 & ~GDK_HYPER_MASK; |
| 147 |
mods2 = GDK_SUPER_MASK; |
| 148 |
gdk_keymap_map_virtual_modifiers (gdk_keymap_get_default (), &mods2); |
| 149 |
mods2 = mods2 & ~GDK_SUPER_MASK; |
| 150 |
|
| 151 |
return mods1 == mods2; |
| 152 |
} |
| 153 |
|
| 154 |
static void |
| 155 |
test_virtual (void) |
| 156 |
{ |
| 157 |
GtkKeyHash *hash; |
| 158 |
static Entry entries[] = { |
| 159 |
{ GDK_KEY_a, GDK_SUPER_MASK }, |
| 160 |
{ GDK_KEY_b, GDK_HYPER_MASK } , |
| 161 |
{ GDK_KEY_c, GDK_META_MASK }, |
| 162 |
{ GDK_KEY_d, GDK_SUPER_MASK|GDK_HYPER_MASK }, |
| 163 |
{ 0, 0 } |
| 164 |
}; |
| 165 |
|
| 166 |
hash = _gtk_key_hash_new (gdk_keymap_get_default (), NULL); |
| 167 |
add_entries (hash, entries); |
| 168 |
|
| 169 |
test_lookup (hash, GDK_KEY_a, GDK_SUPER_MASK, DEFAULT_MASK, 2, 1, 1); |
| 170 |
test_lookup (hash, GDK_KEY_a, GDK_HYPER_MASK, DEFAULT_MASK, 0); |
| 171 |
test_lookup (hash, GDK_KEY_b, GDK_HYPER_MASK, DEFAULT_MASK, 2, 2, 2); |
| 172 |
test_lookup (hash, GDK_KEY_c, GDK_META_MASK, DEFAULT_MASK, 2, 3, 3); |
| 173 |
if (hyper_equals_super ()) |
| 174 |
{ |
| 175 |
GdkModifierType mods; |
| 176 |
|
| 177 |
/* test that colocated virtual modifiers don't count twice */ |
| 178 |
test_lookup (hash, GDK_KEY_d, GDK_SUPER_MASK, DEFAULT_MASK, 0); |
| 179 |
test_lookup (hash, GDK_KEY_d, GDK_HYPER_MASK, DEFAULT_MASK, 0); |
| 180 |
|
| 181 |
mods = GDK_HYPER_MASK; |
| 182 |
gdk_keymap_map_virtual_modifiers (gdk_keymap_get_default (), &mods); |
| 183 |
test_lookup (hash, GDK_KEY_d, mods, DEFAULT_MASK, 0); |
| 184 |
} |
| 185 |
|
| 186 |
_gtk_key_hash_free (hash); |
| 187 |
} |
| 188 |
#endif |
| 189 |
|
| 190 |
int |
| 191 |
main (int argc, char **argv) |
| 192 |
{ |
| 193 |
/* initialize test program */ |
| 194 |
gtk_test_init (&argc, &argv); |
| 195 |
|
| 196 |
g_test_add_func ("/keyhash/basic", test_basic); |
| 197 |
#if 0 |
| 198 |
/* FIXME: need to make these independent of xkb configuration */ |
| 199 |
g_test_add_func ("/keyhash/match", test_match); |
| 200 |
g_test_add_func ("/keyhash/virtual", test_virtual); |
| 201 |
#endif |
| 202 |
return g_test_run(); |
| 203 |
} |