changeset 6190:c27f9a0dcdaf

RT-35407 [Monocle] Implement multi-touch event processors
author Daniel Blaukopf <daniel.blaukopf@oracle.com>
date Sun, 26 Jan 2014 20:38:08 +0200
parents cce0b52bada2
children 75acea53dd57
files modules/graphics/src/main/java/com/sun/glass/ui/monocle/input/MouseInputSynthesizer.java modules/graphics/src/main/java/com/sun/glass/ui/monocle/input/TouchLookahead.java modules/graphics/src/main/java/com/sun/glass/ui/monocle/input/TouchState.java modules/graphics/src/main/java/com/sun/glass/ui/monocle/linux/LinuxEventBuffer.java modules/graphics/src/main/java/com/sun/glass/ui/monocle/linux/LinuxInputDeviceRegistry.java modules/graphics/src/main/java/com/sun/glass/ui/monocle/linux/LinuxStatefulMultiTouchProcessor.java modules/graphics/src/main/java/com/sun/glass/ui/monocle/linux/LinuxStatelessMultiTouchProcessor.java modules/graphics/src/main/java/com/sun/glass/ui/monocle/linux/LinuxTouchProcessor.java tests/system/src/test/java/com/sun/glass/ui/monocle/input/EGalaxTest.java tests/system/src/test/java/com/sun/glass/ui/monocle/input/TestApplication.java
diffstat 10 files changed, 277 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/modules/graphics/src/main/java/com/sun/glass/ui/monocle/input/MouseInputSynthesizer.java	Sun Jan 26 17:05:08 2014 +0200
+++ b/modules/graphics/src/main/java/com/sun/glass/ui/monocle/input/MouseInputSynthesizer.java	Sun Jan 26 20:38:08 2014 +0200
@@ -44,15 +44,11 @@
         } else {
             mouseState.pressButton(MouseEvent.BUTTON_LEFT);
         }
-        if (touchState.getPointCount() == 1) {
-            // do not synthesize mouse drag events if more than one finger is
-            // down
-            TouchState.Point p = touchState.getPointForID(
-                    touchState.getPrimaryID(), false);
-            if (p != null) {
-                mouseState.setX(p.x);
-                mouseState.setY(p.y);
-            }
+        TouchState.Point p = touchState.getPointForID(
+                touchState.getPrimaryID(), false);
+        if (p != null) {
+            mouseState.setX(p.x);
+            mouseState.setY(p.y);
         }
         MouseInput.getInstance().setState(mouseState, true);
     }
--- a/modules/graphics/src/main/java/com/sun/glass/ui/monocle/input/TouchLookahead.java	Sun Jan 26 17:05:08 2014 +0200
+++ b/modules/graphics/src/main/java/com/sun/glass/ui/monocle/input/TouchLookahead.java	Sun Jan 26 20:38:08 2014 +0200
@@ -75,7 +75,7 @@
             if (state.canBeFoldedWith(previousState, assignIDs)) {
                 processedFirstEvent = true;
             } else {
-                touch.setState(state, true);
+                touch.setState(state, assignIDs);
             }
         }
         if (processedFirstEvent) {
@@ -86,7 +86,7 @@
             state.sortPointsByID();
             if (!state.canBeFoldedWith(previousState, assignIDs)) {
                 // the events are different. Send "previousState".
-                touch.setState(previousState, true);
+                touch.setState(previousState, assignIDs);
             }
         }
         state.copyTo(previousState);
--- a/modules/graphics/src/main/java/com/sun/glass/ui/monocle/input/TouchState.java	Sun Jan 26 17:05:08 2014 +0200
+++ b/modules/graphics/src/main/java/com/sun/glass/ui/monocle/input/TouchState.java	Sun Jan 26 20:38:08 2014 +0200
@@ -56,7 +56,7 @@
 
     private Point[] points = new Point[1];
     private int pointCount = 0;
-    private int primaryID = 0;
+    private int primaryID = -1;
     private MonocleWindow window;
 
     /** Returns the Glass window on which this event state is located.
@@ -69,7 +69,7 @@
     MonocleWindow getWindow(boolean recalculateCache, MonocleWindow fallback) {
         if (window == null || recalculateCache) {
             window = fallback;
-            if (primaryID > 0) {
+            if (primaryID >= 0) {
                 Point p = getPointForID(primaryID, false);
                 if (p != null) {
                     window = (MonocleWindow)
@@ -87,13 +87,13 @@
 
     /** Gets the Point matching the given ID, optionally reinstating the point
      * from the previous touch state.
-     * @param id The Point ID to match. A value or zero matches any Point.
+     * @param id The Point ID to match. A value of -1 matches any Point.
      * @return a matching Point, or a new Point if there was no match and
      * reinstatement was requested; null otherwise
      */
     public Point getPointForID(int id, boolean reinstate) {
         for (int i = 0; i < pointCount; i++) {
-            if (id == 0 || points[i].id == id) {
+            if (id == -1 || points[i].id == id) {
                 return points[i];
             }
         }
@@ -170,7 +170,7 @@
     public void copyTo(TouchState target) {
         target.clear();
         for (int i = 0; i < pointCount; i++) {
-            target.addPoint(points[0]);
+            target.addPoint(points[i]);
         }
         target.primaryID = primaryID;
         target.window = window;
--- a/modules/graphics/src/main/java/com/sun/glass/ui/monocle/linux/LinuxEventBuffer.java	Sun Jan 26 17:05:08 2014 +0200
+++ b/modules/graphics/src/main/java/com/sun/glass/ui/monocle/linux/LinuxEventBuffer.java	Sun Jan 26 20:38:08 2014 +0200
@@ -65,7 +65,7 @@
      */
     public synchronized boolean put(ByteBuffer event) throws InterruptedException {
         boolean isSync = event.getInt(EVENT_STRUCT_TYPE_INDEX) == 0
-                && event.getInt(EVENT_STRUCT_CODE_INDEX) == 0;
+                && event.getInt(EVENT_STRUCT_VALUE_INDEX) == 0;
         while (bb.limit() - bb.position() < event.limit()) {
             // Block if bb is full. This should be the
             // only time this thread waits for anything
--- a/modules/graphics/src/main/java/com/sun/glass/ui/monocle/linux/LinuxInputDeviceRegistry.java	Sun Jan 26 17:05:08 2014 +0200
+++ b/modules/graphics/src/main/java/com/sun/glass/ui/monocle/linux/LinuxInputDeviceRegistry.java	Sun Jan 26 20:38:08 2014 +0200
@@ -121,7 +121,18 @@
 
     private LinuxInputProcessor createInputProcessor(LinuxInputDevice device) {
         if (device.isTouch()) {
-            return new LinuxTouchProcessor(device);
+            BitSet absCaps = device.getCapability("abs");
+            boolean isMT = absCaps.get(Input.ABS_MT_POSITION_X)
+                    && absCaps.get(Input.ABS_MT_POSITION_Y);
+            if (isMT) {
+                if (absCaps.get(Input.ABS_MT_TRACKING_ID)) {
+                    return new LinuxStatefulMultiTouchProcessor(device);
+                } else {
+                    return new LinuxStatelessMultiTouchProcessor(device);
+                }
+            } else {
+                return new LinuxTouchProcessor(device);
+            }
         } else if (device.isRelative()) {
             return new LinuxMouseProcessor();
         } else {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/modules/graphics/src/main/java/com/sun/glass/ui/monocle/linux/LinuxStatefulMultiTouchProcessor.java	Sun Jan 26 20:38:08 2014 +0200
@@ -0,0 +1,136 @@
+/*
+ * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package com.sun.glass.ui.monocle.linux;
+
+import com.sun.glass.ui.monocle.input.TouchLookahead;
+import com.sun.glass.ui.monocle.input.TouchState;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * This multitouch processor works with drivers that send tracking IDs.
+ */
+public class LinuxStatefulMultiTouchProcessor implements LinuxInputProcessor {
+
+    private static final int ID_UNASSIGNED = -1;
+    private static final int SLOT_UNASSIGNED = -1;
+    private static final int COORD_UNDEFINED = Integer.MIN_VALUE;
+
+    private final TouchLookahead tl = new TouchLookahead();
+    private final LinuxTouchTransform transform;
+    private final Map<Integer, Integer> slotToIDMap =
+            new HashMap<Integer, Integer>();
+
+    LinuxStatefulMultiTouchProcessor(LinuxInputDevice device) {
+        tl.setAssignIDs(false);
+        transform = new LinuxTouchTransform(device);
+    }
+
+    @Override
+    public void processEvents(LinuxInputDevice device) {
+        LinuxEventBuffer buffer = device.getBuffer();
+        tl.pullState(false);
+        int currentID = ID_UNASSIGNED;
+        int currentSlot = SLOT_UNASSIGNED;
+        int x = COORD_UNDEFINED;
+        int y = COORD_UNDEFINED;
+        while (buffer.hasNextEvent()) {
+            System.out.println("Processing " + buffer.getEventDescription());
+            switch (buffer.getEventType()) {
+                case Input.EV_ABS: {
+                    int value = transform.getValue(buffer);
+                    switch (transform.getAxis(buffer)) {
+                        case Input.ABS_MT_SLOT:
+                            // We expect ABS_MT_SLOT and ABS_MT_TRACKING_ID
+                            // to precede coordinates
+                            currentSlot = value;
+                            currentID = slotToIDMap.getOrDefault(currentSlot,
+                                                                 ID_UNASSIGNED);
+                            break;
+                        case Input.ABS_MT_TRACKING_ID:
+                            currentID = value;
+                            if (currentSlot != SLOT_UNASSIGNED) {
+                                slotToIDMap.put(currentSlot, currentID);
+                            }
+                            break;
+                        case Input.ABS_X:
+                        case Input.ABS_MT_POSITION_X:
+                            x = value;
+                            break;
+                        case Input.ABS_Y:
+                        case Input.ABS_MT_POSITION_Y:
+                            y = value;
+                            break;
+                    }
+                    break;
+                }
+                case Input.EV_SYN:
+                    switch (buffer.getEventCode()) {
+                        case Input.SYN_MT_REPORT: {
+                            if (currentID != ID_UNASSIGNED) {
+                                if (x == COORD_UNDEFINED && y == COORD_UNDEFINED) {
+                                    tl.getState().removePointForID(currentID);
+                                } else {
+                                    TouchState.Point p = tl.getState()
+                                            .getPointForID(currentID, false);
+                                    if (p != null && p.id != currentID) {
+                                        System.out.println("error");
+                                    }
+                                    if (p == null) {
+                                        p = new TouchState.Point();
+                                        p.id = currentID;
+                                        p = tl.getState().addPoint(p);
+                                    }
+                                    if (x != COORD_UNDEFINED) {
+                                        p.x = x;
+                                    }
+                                    if (y != COORD_UNDEFINED) {
+                                        p.y = y;
+                                    }
+                                }
+                            }
+                            x = y = COORD_UNDEFINED;
+                            currentID = ID_UNASSIGNED;
+                            break;
+                        }
+                        case Input.SYN_REPORT:
+                            tl.pushState();
+                            tl.pullState(false);
+                            currentID = ID_UNASSIGNED;
+                            currentSlot = SLOT_UNASSIGNED;
+                            x = y = COORD_UNDEFINED;
+                            break;
+                        default: // ignore
+                    }
+                    break;
+            }
+            buffer.nextEvent();
+        }
+        tl.flushState();
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/modules/graphics/src/main/java/com/sun/glass/ui/monocle/linux/LinuxStatelessMultiTouchProcessor.java	Sun Jan 26 20:38:08 2014 +0200
@@ -0,0 +1,111 @@
+/*
+ * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package com.sun.glass.ui.monocle.linux;
+
+import com.sun.glass.ui.monocle.input.TouchLookahead;
+import com.sun.glass.ui.monocle.input.TouchState;
+
+/**
+ * This multitouch processor works with drivers that do not send a tracking ID
+ * themselves. Drivers that do not send tracking IDs will send all touch points
+ * in each SYN_REPORT-terminated event sequence. So for these drivers we will
+ * clear touch state before each event sequence.
+ */
+public class LinuxStatelessMultiTouchProcessor implements LinuxInputProcessor {
+
+    private static final int COORD_UNDEFINED = Integer.MIN_VALUE;
+
+    private final TouchLookahead tl = new TouchLookahead();
+    private final LinuxTouchTransform transform;
+
+    LinuxStatelessMultiTouchProcessor(LinuxInputDevice device) {
+        tl.setAssignIDs(true);
+        transform = new LinuxTouchTransform(device);
+    }
+
+    @Override
+    public void processEvents(LinuxInputDevice device) {
+        LinuxEventBuffer buffer = device.getBuffer();
+        tl.pullState(true);
+        int x = COORD_UNDEFINED;
+        int y = COORD_UNDEFINED;
+        boolean touchReleased = false;
+        while (buffer.hasNextEvent()) {
+            switch (buffer.getEventType()) {
+                case Input.EV_ABS: {
+                    int value = transform.getValue(buffer);
+                    switch (transform.getAxis(buffer)) {
+                        case Input.ABS_X:
+                        case Input.ABS_MT_POSITION_X:
+                            x = value;
+                            break;
+                        case Input.ABS_Y:
+                        case Input.ABS_MT_POSITION_Y:
+                            y = value;
+                            break;
+                    }
+                    break;
+                }
+                case Input.EV_KEY:
+                    switch (buffer.getEventCode()) {
+                        case Input.BTN_TOUCH:
+                            if (buffer.getEventValue() == 0) {
+                                touchReleased = true;
+                            }
+                            break;
+                    }
+                    break;
+                case Input.EV_SYN:
+                    switch (buffer.getEventCode()) {
+                        case Input.SYN_MT_REPORT: {
+                            if (x != COORD_UNDEFINED && y != COORD_UNDEFINED) {
+                                TouchState.Point p = tl.getState().addPoint(null);
+                                p.id = 0;
+                                p.x = x;
+                                p.y = y;
+                            }
+                            x = y = COORD_UNDEFINED;
+                            break;
+                        }
+                        case Input.SYN_REPORT:
+                            if (touchReleased) {
+                                // remove points
+                                tl.getState().clear();
+                                touchReleased = false;
+                            }                            tl.pushState();
+                            tl.pullState(true);
+                            x = y = COORD_UNDEFINED;
+                            break;
+                        default: // ignore
+                    }
+                    break;
+            }
+            buffer.nextEvent();
+        }
+        tl.flushState();
+    }
+
+}
--- a/modules/graphics/src/main/java/com/sun/glass/ui/monocle/linux/LinuxTouchProcessor.java	Sun Jan 26 17:05:08 2014 +0200
+++ b/modules/graphics/src/main/java/com/sun/glass/ui/monocle/linux/LinuxTouchProcessor.java	Sun Jan 26 20:38:08 2014 +0200
@@ -49,11 +49,11 @@
                     switch (transform.getAxis(buffer)) {
                         case Input.ABS_X:
                         case Input.ABS_MT_POSITION_X:
-                            tl.getState().getPointForID(0, true).x = value;
+                            tl.getState().getPointForID(-1, true).x = value;
                             break;
                         case Input.ABS_Y:
                         case Input.ABS_MT_POSITION_Y:
-                            tl.getState().getPointForID(0, true).y = value;
+                            tl.getState().getPointForID(-1, true).y = value;
                             break;
                     }
                     break;
@@ -65,7 +65,7 @@
                                 touchReleased = true;
                             } else {
                                 // restore an old point
-                                tl.getState().getPointForID(0, true);
+                                tl.getState().getPointForID(-1, true);
                             }
                             break;
                     }
--- a/tests/system/src/test/java/com/sun/glass/ui/monocle/input/EGalaxTest.java	Sun Jan 26 17:05:08 2014 +0200
+++ b/tests/system/src/test/java/com/sun/glass/ui/monocle/input/EGalaxTest.java	Sun Jan 26 20:38:08 2014 +0200
@@ -391,6 +391,7 @@
 
     @Test
     public void testDoubleClick2() throws Exception {
+        Assume.assumeTrue(TestApplication.isMonocle()); // RT-35536
         int x1 = (int) Math.round(screen.getWidth() / 2.0);
         int y1 = (int) Math.round(screen.getHeight() / 2.0);
         int x2 = x1 + TestApplication.getTapRadius();
--- a/tests/system/src/test/java/com/sun/glass/ui/monocle/input/TestApplication.java	Sun Jan 26 17:05:08 2014 +0200
+++ b/tests/system/src/test/java/com/sun/glass/ui/monocle/input/TestApplication.java	Sun Jan 26 20:38:08 2014 +0200
@@ -101,7 +101,7 @@
             Platform.runLater(() -> {
                 if (isMonocle()) {
                     tapRadius = TouchInput.getInstance().getTouchMoveSensitivity();
-                    useMultitouch = false; // RT-35407
+                    useMultitouch = true;
                 } else {
                     tapRadius = Integer.getInteger("lens.input.touch.TapRadius", 20);
                     useMultitouch = Boolean.getBoolean("com.sun.javafx.experimental.embedded.multiTouch");