diff --git a/tools/libinput-record-verify-yaml.py b/tools/libinput-record-verify-yaml.py index fb90481c..8bde4ca3 100755 --- a/tools/libinput-record-verify-yaml.py +++ b/tools/libinput-record-verify-yaml.py @@ -233,7 +233,7 @@ class TestYaml(unittest.TestCase): 'TABLET_TOOL_AXIS', 'TABLET_TOOL_PROXIMITY', 'TABLET_TOOL_BUTTON', 'TABLET_TOOL_TIP', 'TABLET_PAD_STRIP', 'TABLET_PAD_RING', - 'TABLET_PAD_BUTTON' + 'TABLET_PAD_BUTTON', 'SWITCH_TOGGLE', ] for e in self.libinput_events(): self.assertIn('type', e) @@ -615,6 +615,20 @@ class TestYaml(unittest.TestCase): except KeyError: pass + def test_events_libinput_switch(self): + keys = ['type', 'time', 'switch', 'state'] + + for e in self.libinput_events('SWITCH_TOGGLE'): + self.dict_key_crosscheck(e, keys) + + s = e['switch'] + self.assertTrue(isinstance(s, int)) + self.assertIn(s, [0x00, 0x01]) + + # yaml converts on/off to true/false + state = e['state'] + self.assertTrue(isinstance(state, bool)) + if __name__ == '__main__': parser = argparse.ArgumentParser(description='Verify a YAML recording') diff --git a/tools/libinput-record.c b/tools/libinput-record.c index a2c00cf2..f0a41058 100644 --- a/tools/libinput-record.c +++ b/tools/libinput-record.c @@ -1089,6 +1089,42 @@ buffer_tablet_pad_ringstrip_event(struct record_context *ctx, mode); } +static void +buffer_switch_event(struct record_context *ctx, + struct libinput_event *e, + struct event *event) +{ + struct libinput_event_switch *s = libinput_event_get_switch_event(e); + enum libinput_switch_state state; + uint32_t sw; + const char *type; + uint64_t time; + + switch(libinput_event_get_type(e)) { + case LIBINPUT_EVENT_SWITCH_TOGGLE: + type = "SWITCH_TOGGLE"; + break; + default: + abort(); + } + + time = ctx->offset ? + libinput_event_switch_get_time_usec(s) - ctx->offset : 0; + + sw = libinput_event_switch_get_switch(s); + state = libinput_event_switch_get_switch_state(s); + + event->time = time; + snprintf(event->u.libinput.msg, + sizeof(event->u.libinput.msg), + "{time: %ld.%06ld, type: %s, switch: %d, state: %s}", + time / (int)1e6, + time % (int)1e6, + type, + sw, + state == LIBINPUT_SWITCH_STATE_ON ? "on" : "off"); +} + static void buffer_libinput_event(struct record_context *ctx, struct libinput_event *e, @@ -1148,6 +1184,9 @@ buffer_libinput_event(struct record_context *ctx, case LIBINPUT_EVENT_TABLET_PAD_STRIP: buffer_tablet_pad_ringstrip_event(ctx, e, event); break; + case LIBINPUT_EVENT_SWITCH_TOGGLE: + buffer_switch_event(ctx, e, event); + break; default: break; }