Commit bbf388ec1f632f648b0e1323fe90c251c0425bf7

Ran Benita 2012-10-11T16:54:17

Make xkb_keymap_num_leds return the index range instead of active count 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>

diff --git a/src/keymap.c b/src/keymap.c
index 49d7123..a23b279 100644
--- a/src/keymap.c
+++ b/src/keymap.c
@@ -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);
 }
 
 /**
diff --git a/test/interactive.c b/test/interactive.c
index 8162d8d..15442c9 100644
--- a/test/interactive.c
+++ b/test/interactive.c
@@ -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));
diff --git a/test/state.c b/test/state.c
index ee5abce..cafc264 100644
--- a/test/state.c
+++ b/test/state.c
@@ -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",
diff --git a/xkbcommon/xkbcommon.h b/xkbcommon/xkbcommon.h
index a6b0fa8..6d196aa 100644
--- a/xkbcommon/xkbcommon.h
+++ b/xkbcommon/xkbcommon.h
@@ -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);