quirks: add AttrInputPropEnable and Disable
authorPeter Hutterer <peter.hutterer@who-t.net>
Fri, 23 Oct 2020 00:38:14 +0000 (10:38 +1000)
committerPeter Hutterer <peter.hutterer@who-t.net>
Fri, 23 Oct 2020 03:35:50 +0000 (13:35 +1000)
The latter requires libevdev 1.10 but since that'll take a while to filter
into our various CI systems, let's make it conditional.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
doc/user/device-quirks.rst
meson.build
src/evdev.c
src/quirks.c
src/quirks.h
src/util-prop-parsers.c
src/util-prop-parsers.h
test/litest-device-keyboard-quirked.c
test/test-device.c
test/test-utils.c

index 21c43e1c752ebb2cf24a0dffafaed6b9110b588e..faaea47f1e762c6466da8422e447ef37738d098f 100644 (file)
@@ -181,6 +181,12 @@ AttrEventCodeEnable=EV_ABS;BTN_STYLUS;EV_KEY:0x123;
     Enables the evdev event type/code tuples on the device. Entries may be
     a named event type, or a named event code, or a named event type with a
     hexadecimal event code, separated by a single colon.
+AttrInputPropDisable=INPUT_PROP_BUTTONPAD;INPUT_PROP_POINTER;
+    Disables the evdev input property on the device. Entries may be
+    a named input property or the hexadecimal value of that property.
+AttrInputPropEnable=INPUT_PROP_BUTTONPAD;INPUT_PROP_POINTER;
+    Enables the evdev input property on the device. Entries may be
+    a named input property or the hexadecimal value of that property.
 AttrPointingStickIntegration=internal|external
     Indicates the integration of the pointing stick. This is a string enum.
     Only needed for external pointing sticks. These are rare.
index 6b633dad48fd963d0793209ccd3a1a64938d369a..bbfac3fc687415bd891233a7c593cffa259eda4c 100644 (file)
@@ -139,6 +139,9 @@ pkgconfig = import('pkgconfig')
 dep_udev = dependency('libudev')
 dep_mtdev = dependency('mtdev', version : '>= 1.1.0')
 dep_libevdev = dependency('libevdev')
+config_h.set10('HAVE_LIBEVDEV_DISABLE_PROPERTY',
+               dep_libevdev.version().version_compare('>= 1.9.902'))
+
 dep_lm = cc.find_library('m', required : false)
 dep_rt = cc.find_library('rt', required : false)
 
index d7602f1dac7f218a4a7ef6ca09505b69361e217c..084843006c6917a43b64fd445b0ee5c4b9ab42d4 100644 (file)
@@ -2054,6 +2054,8 @@ evdev_pre_configure_model_quirks(struct evdev_device *device)
        struct quirks_context *quirks;
        struct quirks *q;
        const struct quirk_tuples *t;
+       const uint32_t *props = NULL;
+       size_t nprops = 0;
        char *prop;
 
        /* Touchpad is a clickpad but INPUT_PROP_BUTTONPAD is not set, see
@@ -2134,6 +2136,39 @@ evdev_pre_configure_model_quirks(struct evdev_device *device)
                }
        }
 
+       if (quirks_get_uint32_array(q,
+                                   QUIRK_ATTR_INPUT_PROP_ENABLE,
+                                   &props,
+                                   &nprops)) {
+               for (size_t idx = 0; idx < nprops; idx++) {
+                       unsigned int p = props[idx];
+                       libevdev_enable_property(device->evdev, p);
+                       evdev_log_debug(device,
+                                       "quirks: enabling %s (%#x)\n",
+                                       libevdev_property_get_name(p),
+                                       p);
+               }
+       }
+
+       if (quirks_get_uint32_array(q,
+                                        QUIRK_ATTR_INPUT_PROP_DISABLE,
+                                        &props,
+                                        &nprops)) {
+#if HAVE_LIBEVDEV_DISABLE_PROPERTY
+               for (size_t idx = 0; idx < nprops; idx++) {
+                       unsigned int p = props[idx];
+                       libevdev_disable_property(device->evdev, p);
+                       evdev_log_debug(device,
+                                       "quirks: disabling %s (%#x)\n",
+                                       libevdev_property_get_name(p),
+                                       p);
+               }
+#else
+               evdev_log_error(device,
+                               "quirks: a quirk for this device requires newer libevdev than installed\n");
+#endif
+       }
+
        quirks_unref(q);
 }
 
index 2be83439330afdb1972170d2c6ef76376de290ab..ded806171f2da5e80df671d2b851ea972a013a13 100644 (file)
@@ -57,6 +57,14 @@ enum property_type {
        PT_RANGE,
        PT_DOUBLE,
        PT_TUPLES,
+       PT_UINT_ARRAY,
+};
+
+struct quirk_array {
+       union {
+               uint32_t u[32];
+       } data;
+       size_t nelements;
 };
 
 /**
@@ -79,6 +87,7 @@ struct property {
                struct quirk_dimensions dim;
                struct quirk_range range;
                struct quirk_tuples tuples;
+               struct quirk_array array;
        } value;
 };
 
@@ -274,6 +283,8 @@ quirk_get_name(enum quirk q)
        case QUIRK_ATTR_MSC_TIMESTAMP:                  return "AttrMscTimestamp";
        case QUIRK_ATTR_EVENT_CODE_DISABLE:             return "AttrEventCodeDisable";
        case QUIRK_ATTR_EVENT_CODE_ENABLE:              return "AttrEventCodeEnable";
+       case QUIRK_ATTR_INPUT_PROP_DISABLE:             return "AttrInputPropDisable";
+       case QUIRK_ATTR_INPUT_PROP_ENABLE:              return "AttrInputPropEnable";
        default:
                abort();
        }
@@ -758,6 +769,24 @@ parse_attr(struct quirks_context *ctx,
                p->value.tuples.ntuples = nevents;
                p->type = PT_TUPLES;
 
+               rc = true;
+       } else if (streq(key, quirk_get_name(QUIRK_ATTR_INPUT_PROP_DISABLE)) ||
+                  streq(key, quirk_get_name(QUIRK_ATTR_INPUT_PROP_ENABLE))) {
+               unsigned int props[INPUT_PROP_CNT];
+               size_t nprops = ARRAY_LENGTH(props);
+               if (streq(key, quirk_get_name(QUIRK_ATTR_INPUT_PROP_DISABLE)))
+                       p->id = QUIRK_ATTR_INPUT_PROP_DISABLE;
+               else
+                       p->id = QUIRK_ATTR_INPUT_PROP_ENABLE;
+
+               if (!parse_input_prop_property(value, props, &nprops) ||
+                   nprops == 0)
+                       goto out;
+
+               memcpy(p->value.array.data.u, props, nprops * sizeof(unsigned int));
+               p->value.array.nelements = nprops;
+               p->type = PT_UINT_ARRAY;
+
                rc = true;
        } else {
                qlog_error(ctx, "Unknown key %s in %s\n", key, s->name);
@@ -1589,3 +1618,25 @@ quirks_get_tuples(struct quirks *q,
 
        return true;
 }
+
+bool
+quirks_get_uint32_array(struct quirks *q,
+                       enum quirk which,
+                       const uint32_t **array,
+                       size_t *nelements)
+{
+       struct property *p;
+
+       if (!q)
+               return false;
+
+       p = quirk_find_prop(q, which);
+       if (!p)
+               return false;
+
+       assert(p->type == PT_UINT_ARRAY);
+       *array = p->value.array.data.u;
+       *nelements = p->value.array.nelements;
+
+       return true;
+}
index 819ff72c9d34740a93702a49466793240fbee22b..46d224da88e84f158ed87d1326c77f792a0c5979 100644 (file)
@@ -110,6 +110,8 @@ enum quirk {
        QUIRK_ATTR_MSC_TIMESTAMP,
        QUIRK_ATTR_EVENT_CODE_DISABLE,
        QUIRK_ATTR_EVENT_CODE_ENABLE,
+       QUIRK_ATTR_INPUT_PROP_DISABLE,
+       QUIRK_ATTR_INPUT_PROP_ENABLE,
 
        _QUIRK_LAST_ATTR_QUIRK_, /* Guard: do not modify */
 };
@@ -313,3 +315,17 @@ bool
 quirks_get_tuples(struct quirks *q,
                  enum quirk which,
                  const struct quirk_tuples **tuples);
+
+/**
+ * Get the uint32 array of the given quirk.
+ * This function will assert if the quirk type does not match the
+ * requested type. If the quirk is not set for this device, tuples is
+ * unchanged.
+ *
+ * @return true if the quirk value is valid, false otherwise.
+ */
+bool
+quirks_get_uint32_array(struct quirks *q,
+                       enum quirk which,
+                       const uint32_t **array,
+                       size_t *nelements);
index 9e076328c2a49aa9a3a74637eea37fe32bd34da2..5a5cf8e03a162d11edf8adb110893589f4ee973b 100644 (file)
@@ -402,6 +402,63 @@ out:
        return rc;
 }
 
+/**
+ * Parses a string of the format "INPUT_PROP_BUTTONPAD;INPUT_PROP_POINTER;0x123;"
+ * where each element must be a named input prop OR a hexcode in the form
+ * 0x1234
+ *
+ * props must point to an existing array of size nprops.
+ * nprops specifies the size of the array in props and returns the number
+ * of elements, elements exceeding nprops are simply ignored, just make sure
+ * props is large enough for your use-case.
+ *
+ * On success, props contains nprops elements.
+ */
+bool
+parse_input_prop_property(const char *prop, unsigned int *props_out, size_t *nprops)
+{
+       char **strv = NULL;
+       bool rc = false;
+       size_t count = 0;
+       size_t idx;
+       unsigned int props[INPUT_PROP_CNT]; /* doubling up on quirks is a bug */
+
+       strv = strv_from_string(prop, ";");
+       if (!strv)
+               goto out;
+
+       for (idx = 0; strv[idx]; idx++)
+               count++;
+
+       if (count == 0 || count > ARRAY_LENGTH(props))
+               goto out;
+
+       count = min(*nprops, count);
+       for (idx = 0; strv[idx]; idx++) {
+               char *s = strv[idx];
+               unsigned int prop;
+
+               if (safe_atou_base(s, &prop, 16)) {
+                       if (prop > INPUT_PROP_MAX)
+                               goto out;
+               } else {
+                       int val = libevdev_property_from_name(s);
+                       if (val == -1)
+                               goto out;
+                       prop = (unsigned int)val;
+               }
+               props[idx] = prop;
+       }
+
+       memcpy(props_out, props, count * sizeof *props);
+       *nprops = count;
+       rc = true;
+
+out:
+       strv_free(strv);
+       return rc;
+}
+
 /**
  * Parse the property value for the EVDEV_ABS_00 properties. Spec is
  *  EVDEV_ABS_00=min:max:res:fuzz:flat
index 7ed136a96c0ddc3cd52b34ef9ad69f5ff1fd5114..5f0d86736103e040d951fabbc593f8f8315af5e4 100644 (file)
@@ -38,6 +38,7 @@ bool parse_calibration_property(const char *prop, float calibration[6]);
 bool parse_range_property(const char *prop, int *hi, int *lo);
 #define EVENT_CODE_UNDEFINED 0xffff
 bool parse_evcode_property(const char *prop, struct input_event *events, size_t *nevents);
+bool parse_input_prop_property(const char *prop, unsigned int *props_out, size_t *nprops);
 
 enum tpkbcombo_layout {
        TPKBCOMBO_LAYOUT_UNKNOWN,
index 748794b2e7ed0356420a4071f91f2f2daa9f0148..53161fa42600addaa0d91e49eb3465e164008d2d 100644 (file)
@@ -191,6 +191,10 @@ static int events[] = {
        EV_LED, LED_NUML,
        EV_LED, LED_CAPSL,
        EV_LED, LED_SCROLLL,
+
+       /* gets disabled */
+       INPUT_PROP_MAX, INPUT_PROP_POINTING_STICK,
+
        -1, -1,
 };
 
@@ -201,7 +205,18 @@ static const char quirk_file[] =
 "\n"
 "[litest Quirked keyboard disable F1-F3]\n"
 "MatchName=litest Quirked Keyboard\n"
-"AttrEventCodeDisable=KEY_F1;EV_KEY:0x3c;KEY_F3\n";
+"AttrEventCodeDisable=KEY_F1;EV_KEY:0x3c;KEY_F3\n"
+#if HAVE_LIBEVDEV_DISABLE_PROPERTY
+"\n"
+"[litest Quirked keyboard enable buttonpad]\n"
+"MatchName=litest Quirked Keyboard\n"
+"AttrInputPropEnable=INPUT_PROP_BUTTONPAD\n"
+"\n"
+"[litest Quirked keyboard disable pointingstick]\n"
+"MatchName=litest Quirked Keyboard\n"
+"AttrInputPropDisable=INPUT_PROP_POINTING_STICK\n"
+#endif
+;
 
 TEST_DEVICE("keyboard-quirked",
        .type = LITEST_KEYBOARD_QUIRKED,
index a50372d460876fd41b6ec0972e2e0f08340310eb..6c38ed443d118245960f20fd8fd9c09fd5c8217b 100644 (file)
@@ -1454,6 +1454,10 @@ START_TEST(device_quirks)
        char **message;
        bool disable_key_f1 = false,
             enable_btn_left = false;
+#if HAVE_LIBEVDEV_DISABLE_PROPERTY
+       bool disable_pointingstick = false,
+            enable_buttonpad = false;
+#endif
 
        li = litest_create_context();
        libinput_log_set_priority(li, LIBINPUT_LOG_PRIORITY_DEBUG);
@@ -1480,12 +1484,22 @@ START_TEST(device_quirks)
                        disable_key_f1 = true;
                if (strstr(*message, "enabling EV_KEY BTN_LEFT"))
                        enable_btn_left = true;
-
+#if HAVE_LIBEVDEV_DISABLE_PROPERTY
+               if (strstr(*message, "enabling INPUT_PROP_BUTTONPAD"))
+                       enable_buttonpad = true;
+               if (strstr(*message, "disabling INPUT_PROP_POINTING_STICK"))
+                       disable_pointingstick = true;
+#endif
+               free(*message);
                message++;
        }
 
        ck_assert(disable_key_f1);
        ck_assert(enable_btn_left);
+#if HAVE_LIBEVDEV_DISABLE_PROPERTY
+       ck_assert(enable_buttonpad);
+       ck_assert(disable_pointingstick);
+#endif
 
        litest_disable_log_handler(li);
 
index 5faec0e4b00da8a44577d6b2e699d3e46d866afc..5955f56e6e3572d7b30c9814b858caad95eff857 100644 (file)
@@ -546,6 +546,52 @@ START_TEST(evcode_prop_parser)
 }
 END_TEST
 
+START_TEST(input_prop_parser)
+{
+       struct parser_test_val {
+               const char *prop;
+               bool success;
+               size_t nvals;
+               uint32_t values[20];
+       } tests[] = {
+               { "INPUT_PROP_BUTTONPAD", true, 1, {INPUT_PROP_BUTTONPAD}},
+               { "INPUT_PROP_BUTTONPAD;INPUT_PROP_POINTER", true, 2,
+                       { INPUT_PROP_BUTTONPAD,
+                         INPUT_PROP_POINTER }},
+               { "INPUT_PROP_BUTTONPAD;0x00;0x03", true, 3,
+                       { INPUT_PROP_BUTTONPAD,
+                         INPUT_PROP_POINTER,
+                         INPUT_PROP_SEMI_MT }},
+               { .prop = "", .success = false },
+               { .prop = "0xff", .success = false },
+               { .prop = "INPUT_PROP", .success = false },
+               { .prop = "INPUT_PROP_FOO", .success = false },
+               { .prop = "INPUT_PROP_FOO;INPUT_PROP_FOO", .success = false },
+               { .prop = "INPUT_PROP_POINTER;INPUT_PROP_FOO", .success = false },
+               { .prop = "none", .success = false },
+               { .prop = NULL },
+       };
+       struct parser_test_val *t;
+
+       for (int i = 0; tests[i].prop; i++) {
+               bool success;
+               uint32_t props[32];
+               size_t nprops = ARRAY_LENGTH(props);
+
+               t = &tests[i];
+               success = parse_input_prop_property(t->prop, props, &nprops);
+               ck_assert(success == t->success);
+               if (!success)
+                       continue;
+
+               ck_assert_int_eq(nprops, t->nvals);
+               for (size_t j = 0; j < t->nvals; j++) {
+                       ck_assert_int_eq(t->values[j], props[j]);
+               }
+       }
+}
+END_TEST
+
 START_TEST(evdev_abs_parser)
 {
        struct test {
@@ -1244,6 +1290,7 @@ litest_utils_suite(void)
        tcase_add_test(tc, calibration_prop_parser);
        tcase_add_test(tc, range_prop_parser);
        tcase_add_test(tc, evcode_prop_parser);
+       tcase_add_test(tc, input_prop_parser);
        tcase_add_test(tc, evdev_abs_parser);
        tcase_add_test(tc, safe_atoi_test);
        tcase_add_test(tc, safe_atoi_base_16_test);