1 /*
   2  * Copyright (c) 2011, 2015, 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 
  26 package sun.lwawt.macosx;
  27 
  28 import sun.awt.SunToolkit;
  29 import sun.lwawt.LWWindowPeer;
  30 import sun.lwawt.PlatformEventNotifier;
  31 
  32 import java.awt.Toolkit;
  33 import java.awt.event.MouseEvent;
  34 import java.awt.event.InputEvent;
  35 import java.awt.event.MouseWheelEvent;
  36 import java.awt.event.KeyEvent;
  37 
  38 /**
  39  * Translates NSEvents/NPCocoaEvents into AWT events.
  40  */
  41 final class CPlatformResponder {
  42 
  43     private final PlatformEventNotifier eventNotifier;
  44     private final boolean isNpapiCallback;
  45     private int lastKeyPressCode = KeyEvent.VK_UNDEFINED;
  46 
  47     CPlatformResponder(final PlatformEventNotifier eventNotifier,
  48                        final boolean isNpapiCallback) {
  49         this.eventNotifier = eventNotifier;
  50         this.isNpapiCallback = isNpapiCallback;
  51     }
  52 
  53     /**
  54      * Handles mouse events.
  55      */
  56     void handleMouseEvent(int eventType, int modifierFlags, int buttonNumber,
  57                           int clickCount, int x, int y, int absX, int absY) {
  58         final SunToolkit tk = (SunToolkit)Toolkit.getDefaultToolkit();
  59         if ((buttonNumber > 2 && !tk.areExtraMouseButtonsEnabled())
  60                 || buttonNumber > tk.getNumberOfButtons() - 1) {
  61             return;
  62         }
  63 
  64         int jeventType = isNpapiCallback ? NSEvent.npToJavaEventType(eventType) :
  65                                            NSEvent.nsToJavaEventType(eventType);
  66 
  67         int jbuttonNumber = MouseEvent.NOBUTTON;
  68         int jclickCount = 0;
  69 
  70         if (jeventType != MouseEvent.MOUSE_MOVED &&
  71             jeventType != MouseEvent.MOUSE_ENTERED &&
  72             jeventType != MouseEvent.MOUSE_EXITED)
  73         {
  74             jbuttonNumber = NSEvent.nsToJavaButton(buttonNumber);
  75             jclickCount = clickCount;
  76         }
  77 
  78         int jmodifiers = NSEvent.nsToJavaMouseModifiers(buttonNumber,
  79                                                         modifierFlags);
  80         boolean jpopupTrigger = NSEvent.isPopupTrigger(jmodifiers);
  81 
  82         eventNotifier.notifyMouseEvent(jeventType, System.currentTimeMillis(), jbuttonNumber,
  83                 x, y, absX, absY, jmodifiers, jclickCount,
  84                 jpopupTrigger, null);
  85     }
  86 
  87     /**
  88      * Handles scroll events.
  89      */
  90     void handleScrollEvent(final int x, final int y, final int absX,
  91                            final int absY, final int modifierFlags,
  92                            final double deltaX, final double deltaY) {
  93         final int buttonNumber = CocoaConstants.kCGMouseButtonCenter;
  94         int jmodifiers = NSEvent.nsToJavaMouseModifiers(buttonNumber,
  95                                                         modifierFlags);
  96         final boolean isShift = (jmodifiers & InputEvent.SHIFT_DOWN_MASK) != 0;
  97 
  98         // Vertical scroll.
  99         if (!isShift && deltaY != 0.0) {
 100             dispatchScrollEvent(x, y, absX, absY, jmodifiers, deltaY);
 101         }
 102         // Horizontal scroll or shirt+vertical scroll.
 103         final double delta = isShift && deltaY != 0.0 ? deltaY : deltaX;
 104         if (delta != 0.0) {
 105             jmodifiers |= InputEvent.SHIFT_DOWN_MASK;
 106             dispatchScrollEvent(x, y, absX, absY, jmodifiers, delta);
 107         }
 108     }
 109 
 110     private void dispatchScrollEvent(final int x, final int y, final int absX,
 111                                      final int absY, final int modifiers,
 112                                      final double delta) {
 113         final long when = System.currentTimeMillis();
 114         final int scrollType = MouseWheelEvent.WHEEL_UNIT_SCROLL;
 115         final int scrollAmount = 1;
 116         int wheelRotation = (int) delta;
 117         int signum = (int) Math.signum(delta);
 118         if (signum * delta < 1) {
 119             wheelRotation = signum;
 120         }
 121         // invert the wheelRotation for the peer
 122         eventNotifier.notifyMouseWheelEvent(when, x, y, absX, absY, modifiers,
 123                                             scrollType, scrollAmount,
 124                                             -wheelRotation, -delta, null);
 125     }
 126 
 127     /**
 128      * Handles key events.
 129      */
 130     void handleKeyEvent(int eventType, int modifierFlags, String chars, String charsIgnoringModifiers,
 131                         short keyCode, boolean needsKeyTyped, boolean needsKeyReleased) {
 132         boolean isFlagsChangedEvent =
 133             isNpapiCallback ? (eventType == CocoaConstants.NPCocoaEventFlagsChanged) :
 134                               (eventType == CocoaConstants.NSFlagsChanged);
 135 
 136         int jeventType = KeyEvent.KEY_PRESSED;
 137         int jkeyCode = KeyEvent.VK_UNDEFINED;
 138         int jkeyLocation = KeyEvent.KEY_LOCATION_UNKNOWN;
 139         boolean postsTyped = false;
 140 
 141         char testChar = KeyEvent.CHAR_UNDEFINED;
 142         boolean isDeadChar = (chars!= null && chars.length() == 0);
 143 
 144         if (isFlagsChangedEvent) {
 145             int[] in = new int[] {modifierFlags, keyCode};
 146             int[] out = new int[3]; // [jkeyCode, jkeyLocation, jkeyType]
 147 
 148             NSEvent.nsKeyModifiersToJavaKeyInfo(in, out);
 149 
 150             jkeyCode = out[0];
 151             jkeyLocation = out[1];
 152             jeventType = out[2];
 153         } else {
 154             if (chars != null && chars.length() > 0) {
 155                 testChar = chars.charAt(0);
 156             }
 157 
 158             char testCharIgnoringModifiers = charsIgnoringModifiers != null && charsIgnoringModifiers.length() > 0 ?
 159                     charsIgnoringModifiers.charAt(0) : KeyEvent.CHAR_UNDEFINED;
 160 
 161             int[] in = new int[] {testCharIgnoringModifiers, isDeadChar ? 1 : 0, modifierFlags, keyCode};
 162             int[] out = new int[3]; // [jkeyCode, jkeyLocation, deadChar]
 163 
 164             postsTyped = NSEvent.nsToJavaKeyInfo(in, out);
 165             if (!postsTyped) {
 166                 testChar = KeyEvent.CHAR_UNDEFINED;
 167             }
 168 
 169             if(isDeadChar){
 170                 testChar = (char) out[2];
 171                 if(testChar == 0){
 172                     return;
 173                 }
 174             }
 175 
 176             jkeyCode = out[0];
 177             jkeyLocation = out[1];
 178             jeventType = isNpapiCallback ? NSEvent.npToJavaEventType(eventType) :
 179                                            NSEvent.nsToJavaEventType(eventType);
 180         }
 181 
 182         char javaChar = NSEvent.nsToJavaChar(testChar, modifierFlags);
 183         // Some keys may generate a KEY_TYPED, but we can't determine
 184         // what that character is. That's likely a bug, but for now we
 185         // just check for CHAR_UNDEFINED.
 186         if (javaChar == KeyEvent.CHAR_UNDEFINED) {
 187             postsTyped = false;
 188         }
 189 
 190 
 191         int jmodifiers = NSEvent.nsToJavaKeyModifiers(modifierFlags);
 192         long when = System.currentTimeMillis();
 193 
 194         if (jeventType == KeyEvent.KEY_PRESSED) {
 195             lastKeyPressCode = jkeyCode;
 196         }
 197         eventNotifier.notifyKeyEvent(jeventType, when, jmodifiers,
 198                 jkeyCode, javaChar, jkeyLocation);
 199 
 200         // Current browser may be sending input events, so don't
 201         // post the KEY_TYPED here.
 202         postsTyped &= needsKeyTyped;
 203 
 204         // That's the reaction on the PRESSED (not RELEASED) event as it comes to
 205         // appear in MacOSX.
 206         // Modifier keys (shift, etc) don't want to send TYPED events.
 207         // On the other hand we don't want to generate keyTyped events
 208         // for clipboard related shortcuts like Meta + [CVX]
 209         if (jeventType == KeyEvent.KEY_PRESSED && postsTyped &&
 210                 (jmodifiers & KeyEvent.META_DOWN_MASK) == 0) {
 211             // Enter and Space keys finish the input method processing,
 212             // KEY_TYPED and KEY_RELEASED events for them are synthesized in handleInputEvent.
 213             if (needsKeyReleased && (jkeyCode == KeyEvent.VK_ENTER || jkeyCode == KeyEvent.VK_SPACE)) {
 214                 return;
 215             }
 216             eventNotifier.notifyKeyEvent(KeyEvent.KEY_TYPED, when, jmodifiers,
 217                     KeyEvent.VK_UNDEFINED, javaChar,
 218                     KeyEvent.KEY_LOCATION_UNKNOWN);
 219             //If events come from Firefox, released events should also be generated.
 220             if (needsKeyReleased) {
 221                 eventNotifier.notifyKeyEvent(KeyEvent.KEY_RELEASED, when, jmodifiers,
 222                         jkeyCode, javaChar,
 223                         KeyEvent.KEY_LOCATION_UNKNOWN);
 224             }
 225         }
 226     }
 227 
 228     void handleInputEvent(String text) {
 229         if (text != null) {
 230             int index = 0, length = text.length();
 231             char c = 0;
 232             while (index < length) {
 233                 c = text.charAt(index);
 234                 eventNotifier.notifyKeyEvent(KeyEvent.KEY_TYPED,
 235                         System.currentTimeMillis(),
 236                         0, KeyEvent.VK_UNDEFINED, c,
 237                         KeyEvent.KEY_LOCATION_UNKNOWN);
 238                 index++;
 239             }
 240             eventNotifier.notifyKeyEvent(KeyEvent.KEY_RELEASED,
 241                     System.currentTimeMillis(),
 242                     0, lastKeyPressCode, c,
 243                     KeyEvent.KEY_LOCATION_UNKNOWN);
 244         }
 245     }
 246 
 247     void handleWindowFocusEvent(boolean gained, LWWindowPeer opposite) {
 248         eventNotifier.notifyActivation(gained, opposite);
 249     }
 250 }