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>
master
Ran Benita 2012-10-11 16:54:17 +02:00
parent b6ddd10568
commit bbf388ec1f
4 changed files with 8 additions and 12 deletions

View 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_EXPORT xkb_led_index_t
xkb_keymap_num_leds(struct xkb_keymap *keymap) xkb_keymap_num_leds(struct xkb_keymap *keymap)
{ {
const struct xkb_indicator_map *led; return darray_size(keymap->indicators);
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;
} }
/** /**

View File

@ -274,7 +274,7 @@ print_keycode(struct keyboard *kbd, xkb_keycode_t keycode)
printf("] "); printf("] ");
printf("leds [ "); 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) if (xkb_state_led_index_is_active(state, led) <= 0)
continue; continue;
printf("%s ", xkb_keymap_led_get_name(keymap, led)); printf("%s ", xkb_keymap_led_get_name(keymap, led));

View File

@ -82,7 +82,7 @@ print_state(struct xkb_state *state)
"locked " : ""); "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) if (xkb_state_led_index_is_active(state, led) <= 0)
continue; continue;
fprintf(stderr, "\tled %s (%d): active\n", fprintf(stderr, "\tled %s (%d): active\n",

View 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. * 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_led_index_t
xkb_keymap_num_leds(struct xkb_keymap *keymap); xkb_keymap_num_leds(struct xkb_keymap *keymap);