From: Egbert Eich <e...@suse.com>

usbInitToolType() tries to find the device type of a tool.
Unlike usbFindDeviceType() it doesn't take into account the device_id
which may exist in the event stream.
As a result the device type may be taken from the last known type.
This is generally a bad idea if the type has changed.
This will happen for example when pressing a key on the Cintiq 21UX menu
strips after removing a pen from the tablet.

Signed-off-by: Egbert Eich <e...@suse.com>
---
 src/wcmUSB.c | 65 ++++++++++++++++++++++++++++++++++++------------------------
 1 file changed, 39 insertions(+), 26 deletions(-)

diff --git a/src/wcmUSB.c b/src/wcmUSB.c
index 3d6cdc0..8520bd1 100644
--- a/src/wcmUSB.c
+++ b/src/wcmUSB.c
@@ -1088,38 +1088,48 @@ static int usbIdToType(int id)
  * Protocol 5 devices report different IDs for different styli and pucks,
  * Protocol 4 devices simply report STYLUS_DEVICE_ID, etc.
  *
- * @param ds The current device state received from the kernel.
- * @return The tool type associated with the tool id or the current
- * tool serial number.
+ * @device_id id of the device
+ * @return device type
  */
-static int usbFindDeviceType(const WacomCommonPtr common,
-                         const WacomDeviceState *ds)
+static int usbFindDeviceTypeById(int device_id)
 {
-       int device_type = 0;
-
-       if (!ds->device_id) return 0;
-
-       switch (ds->device_id)
+       switch (device_id)
        {
                case STYLUS_DEVICE_ID:
-                       device_type = STYLUS_ID;
-                       break;
+                       return STYLUS_ID;
                case ERASER_DEVICE_ID:
-                       device_type = ERASER_ID;
-                       break;
+                       return ERASER_ID;
                case CURSOR_DEVICE_ID:
-                       device_type = CURSOR_ID;
-                       break;
+                       return CURSOR_ID;
                case TOUCH_DEVICE_ID:
-                       device_type = TOUCH_ID;
-                       break;
+                       return TOUCH_ID;
                case PAD_DEVICE_ID:
-                       device_type = PAD_ID;
-                       break;
+                       return PAD_ID;
                default: /* protocol 5 */
-                       device_type = usbIdToType(ds->device_id);
+                       return usbIdToType(device_id);
        }
+       return 0;
+}
+
+/**
+ * Find the tool type (STYLUS_ID, etc.) based on the device_id or the
+ *  current tool serial number if the device_id is unknown (0).
+ *
+ * Protocol 5 devices report different IDs for different styli and pucks,
+ * Protocol 4 devices simply report STYLUS_DEVICE_ID, etc.
+ *
+ * @param ds The current device state received from the kernel.
+ * @return The tool type associated with the tool id or the current
+ * tool serial number.
+ */
+static int usbFindDeviceType(const WacomCommonPtr common,
+                         const WacomDeviceState *ds)
+{
+       int device_type = 0;
+
+       if (!ds->device_id) return 0;
 
+       device_type = usbFindDeviceTypeById(ds->device_id);
        return device_type;
 }
 
@@ -1469,15 +1479,16 @@ static void usbParseBTNEvent(WacomCommonPtr common,
 }
 
 /**
- * Translates a tool code from the kernel (e.g. BTN_TOOL_PEN) into the
- * corresponding device type for the driver (e.g. STYLUS_ID).
+ * Translates an event code from the kernel (e.g. type: EV_ABS code: ABS_MISC 
value: STYLUS_DEVICE_ID)
+ * into the corresponding device type for the driver (e.g. STYLUS_ID).
  *
  * @param[in] common
  * @param[in] type      Linux input tool type (e.g. EV_KEY)
  * @param[in] code      Linux input tool code (e.g. BTN_STYLUS_PEN)
+ * @param[in] value     Linux input tool value (e.g. STYLUS_DEVICE_ID)
  * @return              Wacom device ID (e.g. STYLUS_ID) or 0 if no match.
  */
-static int toolTypeToDeviceType(WacomCommonPtr common, int type, int code)
+static int deviceTypeFromEvent(WacomCommonPtr common, int type, int code, int 
value)
 {
        wcmUSBData* private = common->private;
 
@@ -1509,6 +1520,8 @@ static int toolTypeToDeviceType(WacomCommonPtr common, 
int type, int code)
                        case ABS_MT_SLOT:
                        case ABS_MT_TRACKING_ID:
                                return TOUCH_ID;
+                       case ABS_MISC:
+                               return usbFindDeviceTypeById(value);
                }
        }
 
@@ -1538,7 +1551,7 @@ static int refreshDeviceType(WacomCommonPtr common)
        for (i = 0; i < KEY_MAX; i++)
        {
                if (ISBITSET(keys, i))
-                       device_type = toolTypeToDeviceType(common, EV_KEY, i);
+                       device_type = deviceTypeFromEvent(common, EV_KEY, i, 0);
                if (device_type)
                        return device_type;
        }
@@ -1568,7 +1581,7 @@ static int usbInitToolType(WacomCommonPtr common, const 
struct input_event *even
 
        for (i = 0; (i < nevents) && !device_type; ++i, event_ptr++)
        {
-               device_type = toolTypeToDeviceType(common, event_ptr->type, 
event_ptr->code);
+               device_type = deviceTypeFromEvent(common, event_ptr->type, 
event_ptr->code, event_ptr->value);
        }
 
        if (!device_type)
-- 
1.8.4.5


------------------------------------------------------------------------------
Learn Graph Databases - Download FREE O'Reilly Book
"Graph Databases" is the definitive new guide to graph databases and their
applications. Written by three acclaimed leaders in the field,
this first edition is now available. Download your free book today!
http://p.sf.net/sfu/13534_NeoTech
_______________________________________________
Linuxwacom-devel mailing list
Linuxwacom-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linuxwacom-devel

Reply via email to