cast a function's return value
[platform/upstream/libxkbcommon.git] / src / keymap.c
index 54ac7c0..c1d5a10 100644 (file)
@@ -194,6 +194,10 @@ xkb_keymap_new_from_buffer(struct xkb_context *ctx,
     if (!keymap)
         return NULL;
 
+    /* Allow a zero-terminated string as a buffer */
+    if (length > 0 && buffer[length - 1] == '\0')
+        length--;
+
     if (!ops->keymap_new_from_string(keymap, buffer, length)) {
         xkb_keymap_unref(keymap);
         return NULL;
@@ -433,12 +437,39 @@ xkb_keymap_key_get_mods_for_level(struct xkb_keymap *keymap,
     const struct xkb_key_type *type = key->groups[layout].type;
 
     size_t count = 0;
-    for (unsigned i = 0; i < type->num_entries && count < masks_size; i++)
+
+    /*
+     * If the active set of modifiers doesn't match any explicit entry of
+     * the key type, the resulting level is 0 (i.e. Level 1).
+     * So, if we are asked to find the modifiers for level==0, we can offer
+     * an ~infinite supply, which is not very workable.
+     * What we do instead, is special case the empty set of modifiers for
+     * this purpose. If the empty set isn't explicit mapped to a level, we
+     * take it to map to Level 1.
+     * This is almost always what we want. If applicable, given it priority
+     * over other ways to generate the level.
+     */
+    if (level == 0) {
+        bool empty_mapped = false;
+        for (unsigned i = 0; i < type->num_entries && count < masks_size; i++)
+            if (entry_is_active(&type->entries[i]) &&
+                type->entries[i].mods.mask == 0) {
+                empty_mapped = true;
+                break;
+            }
+        if (!empty_mapped && count < masks_size) {
+            masks_out[count++] = 0;
+        }
+    }
+
+    /* Now search explicit mappings. */
+    for (unsigned i = 0; i < type->num_entries && count < masks_size; i++) {
         if (entry_is_active(&type->entries[i]) &&
             type->entries[i].level == level) {
-            masks_out[count] = type->entries[i].mods.mask;
-            ++count;
+            masks_out[count++] = type->entries[i].mods.mask;
         }
+    }
+
     return count;
 }
 
@@ -552,3 +583,17 @@ xkb_keymap_key_repeats(struct xkb_keymap *keymap, xkb_keycode_t kc)
 
     return key->repeats;
 }
+
+XKB_EXPORT int
+xkb_keymap_key_set_repeats(struct xkb_keymap *keymap, xkb_keycode_t kc, int enable)
+{
+    struct xkb_key *key = (struct xkb_key *)XkbKey(keymap, kc);
+
+    if (!key)
+        return 0;
+
+    key->repeats = !!enable;
+    key->explicit |= EXPLICIT_REPEAT;
+
+    return 1;
+}