diff options
author | Peter Hutterer <peter.hutterer@who-t.net> | 2016-02-10 09:24:51 +1000 |
---|---|---|
committer | Peter Hutterer <peter.hutterer@who-t.net> | 2016-03-01 16:00:47 +1000 |
commit | 2bb0678a00f9c089dc865d6ecd20be36446d1c06 (patch) | |
tree | 46d024494e85ff79121291a0e2b4df7f48a1cbb0 | |
parent | 21fe11fb11a4d79aabb208f5839679a52a74678b (diff) |
tablet: sanitize button mask passing
We have a struct, use it. Better than passing arrays and array lengths around.
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
-rw-r--r-- | src/evdev-tablet.c | 54 | ||||
-rw-r--r-- | src/evdev-tablet.h | 2 |
2 files changed, 22 insertions, 34 deletions
diff --git a/src/evdev-tablet.c b/src/evdev-tablet.c index 22ea1ebc..0f6fa2cb 100644 --- a/src/evdev-tablet.c +++ b/src/evdev-tablet.c @@ -38,34 +38,27 @@ static inline void tablet_get_pressed_buttons(struct tablet_dispatch *tablet, - unsigned char *buttons, - unsigned int buttons_len) + struct button_state *buttons) { size_t i; const struct button_state *state = &tablet->button_state, *prev_state = &tablet->prev_button_state; - assert(buttons_len <= ARRAY_LENGTH(state->stylus_buttons)); - - for (i = 0; i < buttons_len; i++) - buttons[i] = state->stylus_buttons[i] & - ~(prev_state->stylus_buttons[i]); + for (i = 0; i < sizeof(buttons->bits); i++) + buttons->bits[i] = state->bits[i] & ~(prev_state->bits[i]); } static inline void tablet_get_released_buttons(struct tablet_dispatch *tablet, - unsigned char *buttons, - unsigned int buttons_len) + struct button_state *buttons) { size_t i; const struct button_state *state = &tablet->button_state, *prev_state = &tablet->prev_button_state; - assert(buttons_len <= ARRAY_LENGTH(state->stylus_buttons)); - - for (i = 0; i < buttons_len; i++) - buttons[i] = prev_state->stylus_buttons[i] & - ~(state->stylus_buttons[i]); + for (i = 0; i < sizeof(buttons->bits); i++) + buttons->bits[i] = prev_state->bits[i] & + ~(state->bits[i]); } /* Merge the previous state with the current one so all buttons look like @@ -77,10 +70,9 @@ tablet_force_button_presses(struct tablet_dispatch *tablet) *prev_state = &tablet->prev_button_state; size_t i; - for (i = 0; i < sizeof(state->stylus_buttons); i++) { - state->stylus_buttons[i] = state->stylus_buttons[i] | - prev_state->stylus_buttons[i]; - prev_state->stylus_buttons[i] = 0; + for (i = 0; i < sizeof(state->bits); i++) { + state->bits[i] = state->bits[i] | prev_state->bits[i]; + prev_state->bits[i] = 0; } } @@ -566,10 +558,10 @@ tablet_update_button(struct tablet_dispatch *tablet, } if (enable) { - set_bit(tablet->button_state.stylus_buttons, evcode); + set_bit(tablet->button_state.bits, evcode); tablet_set_status(tablet, TABLET_BUTTONS_PRESSED); } else { - clear_bit(tablet->button_state.stylus_buttons, evcode); + clear_bit(tablet->button_state.bits, evcode); tablet_set_status(tablet, TABLET_BUTTONS_RELEASED); } } @@ -936,20 +928,19 @@ tablet_notify_button_mask(struct tablet_dispatch *tablet, struct evdev_device *device, uint64_t time, struct libinput_tablet_tool *tool, - const unsigned char *buttons, - unsigned int buttons_len, + const struct button_state *buttons, enum libinput_button_state state) { struct libinput_device *base = &device->base; size_t i; - size_t nbits = 8 * sizeof(buttons[0]) * buttons_len; + size_t nbits = 8 * sizeof(buttons->bits); enum libinput_tablet_tool_tip_state tip_state; tip_state = tablet_has_status(tablet, TABLET_TOOL_IN_CONTACT) ? LIBINPUT_TABLET_TOOL_TIP_DOWN : LIBINPUT_TABLET_TOOL_TIP_UP; for (i = 0; i < nbits; i++) { - if (!bit_is_set(buttons, i)) + if (!bit_is_set(buttons->bits, i)) continue; tablet_notify_button(base, @@ -969,21 +960,18 @@ tablet_notify_buttons(struct tablet_dispatch *tablet, struct libinput_tablet_tool *tool, enum libinput_button_state state) { - unsigned char buttons[ARRAY_LENGTH(tablet->button_state.stylus_buttons)]; + struct button_state buttons; if (state == LIBINPUT_BUTTON_STATE_PRESSED) - tablet_get_pressed_buttons(tablet, buttons, sizeof(buttons)); + tablet_get_pressed_buttons(tablet, &buttons); else - tablet_get_released_buttons(tablet, - buttons, - sizeof(buttons)); + tablet_get_released_buttons(tablet, &buttons); tablet_notify_button_mask(tablet, device, time, tool, - buttons, - sizeof(buttons), + &buttons, state); } @@ -1332,9 +1320,9 @@ tablet_flush(struct tablet_dispatch *tablet, if (tablet_has_status(tablet, TABLET_TOOL_LEAVING_PROXIMITY)) { /* Release all stylus buttons */ - memset(tablet->button_state.stylus_buttons, + memset(tablet->button_state.bits, 0, - sizeof(tablet->button_state.stylus_buttons)); + sizeof(tablet->button_state.bits)); tablet_set_status(tablet, TABLET_BUTTONS_RELEASED); if (tablet_has_status(tablet, TABLET_TOOL_IN_CONTACT)) tablet_set_status(tablet, TABLET_TOOL_LEAVING_CONTACT); diff --git a/src/evdev-tablet.h b/src/evdev-tablet.h index 1d6fc936..880d5230 100644 --- a/src/evdev-tablet.h +++ b/src/evdev-tablet.h @@ -45,7 +45,7 @@ enum tablet_status { }; struct button_state { - unsigned char stylus_buttons[NCHARS(KEY_CNT)]; + unsigned char bits[NCHARS(KEY_CNT)]; }; struct tablet_dispatch { |