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
parent
b6ddd10568
commit
bbf388ec1f
12
src/keymap.c
12
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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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));
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue