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>
Reviewed-by: Jason Gerecke <killert...@gmail.com>
---
 src/wcmUSB.c | 62 ++++++++++++++++++++++++++++++++++++------------------------
 1 file changed, 37 insertions(+), 25 deletions(-)

diff --git a/src/wcmUSB.c b/src/wcmUSB.c
index 83d9eed..b79f0e3 100644
--- a/src/wcmUSB.c
+++ b/src/wcmUSB.c
@@ -1108,37 +1108,46 @@ 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.
+ * @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.
+ *
+ * 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.
+ */
+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;
 }
 
@@ -1488,15 +1497,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;
 
@@ -1528,6 +1538,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);
                }
        }
 
@@ -1557,7 +1569,7 @@ static int refreshDeviceType(WacomCommonPtr common, int 
fd)
        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;
        }
@@ -1588,7 +1600,7 @@ static int usbInitToolType(WacomCommonPtr common, int fd,
 
        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


------------------------------------------------------------------------------
Put Bad Developers to Shame
Dominate Development with Jenkins Continuous Integration
Continuously Automate Build, Test & Deployment 
Start a new project now. Try Jenkins in the cloud.
http://p.sf.net/sfu/13600_Cloudbees
_______________________________________________
Linuxwacom-devel mailing list
Linuxwacom-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linuxwacom-devel

Reply via email to