Make xkb_keymap_num_leds return the index range instead of active count
authorRan Benita <ran234@gmail.com>
Thu, 11 Oct 2012 14:54:17 +0000 (16:54 +0200)
committerRan Benita <ran234@gmail.com>
Thu, 11 Oct 2012 14:54:17 +0000 (16:54 +0200)
Currently xkb_keymap_num_leds() returns a count of valid (settable)
leds. Because the indexes might be non-consecutive, and some leds
might not be settable, it is incorrect to use this function for
iterating over the leds in the keymap. But this is the main use case of
this function, so instead of the current behavior we adapt the function
to the use case by making it return the needed range of iteration.
The caller needs to handle invalid intermittent indexes, though.

Signed-off-by: Ran Benita <ran234@gmail.com>
src/keymap.c
test/interactive.c
test/state.c
xkbcommon/xkbcommon.h

index 49d7123..a23b279 100644 (file)
@@ -233,20 +233,12 @@ xkb_keymap_num_levels_for_key(struct xkb_keymap *keymap, xkb_keycode_t kc,
 }
 
 /**
- * Return the total number of active LEDs in the keymap.
+ * Return the total number of LEDs in the keymap.
  */
 XKB_EXPORT xkb_led_index_t
 xkb_keymap_num_leds(struct xkb_keymap *keymap)
 {
-    const struct xkb_indicator_map *led;
-    xkb_led_index_t ret = 0;
-
-    darray_foreach(led, keymap->indicators)
-        if (led->which_groups || led->groups || led->which_mods ||
-            led->mods.mods || led->ctrls)
-            ret++;
-
-    return ret;
+    return darray_size(keymap->indicators);
 }
 
 /**
index 8162d8d..15442c9 100644 (file)
@@ -274,7 +274,7 @@ print_keycode(struct keyboard *kbd, xkb_keycode_t keycode)
     printf("] ");
 
     printf("leds [ ");
-    for (led = 0; led < sizeof(xkb_led_mask_t) * 8; led++) {
+    for (led = 0; led < xkb_keymap_num_leds(keymap); led++) {
         if (xkb_state_led_index_is_active(state, led) <= 0)
             continue;
         printf("%s ", xkb_keymap_led_get_name(keymap, led));
index ee5abce..cafc264 100644 (file)
@@ -82,7 +82,7 @@ print_state(struct xkb_state *state)
                     "locked " : "");
     }
 
-    for (led = 0; led < sizeof(xkb_led_mask_t) * 8; led++) {
+    for (led = 0; led < xkb_keymap_num_leds(keymap); led++) {
         if (xkb_state_led_index_is_active(state, led) <= 0)
             continue;
         fprintf(stderr, "\tled %s (%d): active\n",
index a6b0fa8..6d196aa 100644 (file)
@@ -641,6 +641,10 @@ xkb_keymap_num_levels_for_key(struct xkb_keymap *keymap, xkb_keycode_t key,
 
 /**
  * Returns the number of LEDs in the given map.
+ * Note that LED indexes are not necessarily consecutive in the keymap.
+ * This means that some LEDs in the range between 0 and the return value
+ * might not be valid.  Given such an index, xkb_keymap_led_get_name()
+ * will return NULL, and xkb_state_led_index_is_active() will return -1.
  */
 xkb_led_index_t
 xkb_keymap_num_leds(struct xkb_keymap *keymap);