1 /*
   2  * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package com.sun.glass.ui;
  26 
  27 import com.sun.glass.events.TouchEvent;
  28 
  29 import java.util.HashMap;
  30 import java.util.Map;
  31 
  32 public class TouchInputSupport
  33 {
  34     private int touchCount = 0;
  35 
  36     private boolean filterTouchCoordinates;
  37     private static class TouchCoord {
  38         private final int x, y, xAbs, yAbs;
  39 
  40         private TouchCoord(int x, int y, int xAbs, int yAbs) {
  41             this.x = x;
  42             this.y = y;
  43             this.xAbs = xAbs;
  44             this.yAbs = yAbs;
  45         }
  46     }
  47     private Map<Long, TouchCoord> touch;
  48 
  49     private TouchCountListener listener;
  50 
  51     private int curTouchCount;
  52     private View curView;
  53     private int curModifiers;
  54     private boolean curIsDirect;
  55 
  56     public static interface TouchCountListener {
  57         void touchCountChanged(TouchInputSupport sender, View view,
  58                                int modifiers, boolean isDirect);
  59     }
  60 
  61     public TouchInputSupport(TouchCountListener listener,
  62                              boolean filterTouchCoordinates) {
  63         Application.checkEventThread();
  64         this.listener = listener;
  65         this.filterTouchCoordinates = filterTouchCoordinates;
  66         if (filterTouchCoordinates) {
  67             touch = new HashMap<>();
  68         }
  69     }
  70 
  71     public int getTouchCount() {
  72         Application.checkEventThread();
  73         return touchCount;
  74     }
  75 
  76     public void notifyBeginTouchEvent(View view, int modifiers, boolean isDirect,
  77                                       int touchEventCount) {
  78 
  79         if (curView != null && view != curView && touchCount != 0 && touch != null) {
  80             if (!curView.isClosed()) {
  81                 // Release the currently pressed touch points
  82                 curView.notifyBeginTouchEvent(0, true, touchCount);
  83                 for (Map.Entry<Long, TouchCoord> e : touch.entrySet()) {
  84                     TouchCoord coord = e.getValue();
  85                     curView.notifyNextTouchEvent(TouchEvent.TOUCH_RELEASED, e.getKey(), coord.x, coord.y, coord.xAbs, coord.yAbs);
  86                 }
  87                 curView.notifyEndTouchEvent();
  88             } 
  89             touch.clear();
  90             touchCount = 0;
  91             if (listener != null ) {
  92                 listener.touchCountChanged(this, curView, 0, true);
  93             }
  94         }
  95 
  96         curTouchCount = touchCount;
  97         curView = view;
  98         curModifiers = modifiers;
  99         curIsDirect = isDirect;
 100         if (view != null) {
 101             view.notifyBeginTouchEvent(modifiers, isDirect, touchEventCount);
 102         }
 103     }
 104 
 105     public void notifyEndTouchEvent(View view) {
 106         if (view == null) {
 107             return;
 108         }
 109 
 110         view.notifyEndTouchEvent();
 111 
 112         // RT-21288. Notify outer world when touch point count changes
 113         if (curTouchCount != 0 && touchCount != 0 && curTouchCount != touchCount &&
 114                 listener != null) {
 115             listener.touchCountChanged(this, curView, curModifiers, curIsDirect);
 116         }
 117     }
 118 
 119     public void notifyNextTouchEvent(View view, int state, long id, int x, int y,
 120                                      int xAbs, int yAbs)
 121     {
 122         switch (state) {
 123             case TouchEvent.TOUCH_RELEASED:
 124                 touchCount--;
 125                 break;
 126             case TouchEvent.TOUCH_PRESSED:
 127                 touchCount++;
 128                 break;
 129             case TouchEvent.TOUCH_MOVED:
 130             case TouchEvent.TOUCH_STILL:
 131                 break;
 132             default:
 133                 System.err.println("Unknown touch state: " + state);
 134                 return;
 135         }
 136         
 137         if (filterTouchCoordinates) {
 138             state = filterTouchInputState(state, id, x, y, xAbs, yAbs);
 139         }
 140 
 141         if (view != null) {
 142             view.notifyNextTouchEvent(state, id, x, y, xAbs, yAbs);
 143         }
 144     }
 145 
 146     private int filterTouchInputState(int state, long id, int x, int y, int xAbs, int yAbs) {
 147         switch (state) {
 148             case TouchEvent.TOUCH_RELEASED:
 149                 touch.remove(id);
 150                 break;
 151             case TouchEvent.TOUCH_MOVED:
 152                 TouchCoord c = touch.get(id);
 153                 if (x == c.x && y == c.y) {
 154                     state = TouchEvent.TOUCH_STILL;
 155                     break;
 156                 }
 157                 // fall through;
 158             case TouchEvent.TOUCH_PRESSED:
 159                 touch.put(id, new TouchCoord(x, y, xAbs, yAbs));
 160                 break;
 161             case TouchEvent.TOUCH_STILL:
 162                 break;
 163             default:
 164                 System.err.println("Unknown touch state: " + state);
 165                 break;
 166         }
 167         return state;
 168     }
 169 }