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.nsToJavaModifiers(modifierFlags);
  79         boolean jpopupTrigger = NSEvent.isPopupTrigger(jmodifiers);
  80 
  81         eventNotifier.notifyMouseEvent(jeventType, System.currentTimeMillis(), jbuttonNumber,
  82                 x, y, absX, absY, jmodifiers, jclickCount,
  83                 jpopupTrigger, null);
  84     }
  85 
  86     /**
  87      * Handles scroll events.
  88      */
  89     void handleScrollEvent(final int x, final int y, final int absX,
  90                            final int absY, final int modifierFlags,
  91                            final double deltaX, final double deltaY) {
  92         int jmodifiers = NSEvent.nsToJavaModifiers(modifierFlags);
  93         final boolean isShift = (jmodifiers & InputEvent.SHIFT_DOWN_MASK) != 0;
  94 
  95         // Vertical scroll.
  96         if (!isShift && deltaY != 0.0) {
  97             dispatchScrollEvent(x, y, absX, absY, jmodifiers, deltaY);
  98         }
  99         // Horizontal scroll or shirt+vertical scroll.
 100         final double delta = isShift && deltaY != 0.0 ? deltaY : deltaX;
 101         if (delta != 0.0) {
 102             jmodifiers |= InputEvent.SHIFT_DOWN_MASK;
 103             dispatchScrollEvent(x, y, absX, absY, jmodifiers, delta);
 104         }
 105     }
 106 
 107     private void dispatchScrollEvent(final int x, final int y, final int absX,
 108                                      final int absY, final int modifiers,
 109                                      final double delta) {
 110         final long when = System.currentTimeMillis();
 111         final int scrollType = MouseWheelEvent.WHEEL_UNIT_SCROLL;
 112         final int scrollAmount = 1;
 113         int wheelRotation = (int) delta;
 114         int signum = (int) Math.signum(delta);
 115         if (signum * delta < 1) {
 116             wheelRotation = signum;
 117         }
 118         // invert the wheelRotation for the peer
 119         eventNotifier.notifyMouseWheelEvent(when, x, y, absX, absY, modifiers,
 120                                             scrollType, scrollAmount,
 121                                             -wheelRotation, -delta, null);
 122     }
 123 
 124     /**
 125      * Handles key events.
 126      */
 127     void handleKeyEvent(int eventType, int modifierFlags, String chars, String charsIgnoringModifiers,
 128                         short keyCode, boolean needsKeyTyped, boolean needsKeyReleased) {
 129         boolean isFlagsChangedEvent =
 130             isNpapiCallback ? (eventType == CocoaConstants.NPCocoaEventFlagsChanged) :
 131                               (eventType == CocoaConstants.NSFlagsChanged);
 132 
 133         int jeventType = KeyEvent.KEY_PRESSED;
 134         int jkeyCode = KeyEvent.VK_UNDEFINED;
 135         int jkeyLocation = KeyEvent.KEY_LOCATION_UNKNOWN;
 136         boolean postsTyped = false;
 137         boolean spaceKeyTyped = false;
 138 
 139         char testChar = KeyEvent.CHAR_UNDEFINED;
 140         boolean isDeadChar = (chars!= null && chars.length() == 0);
 141 
 142         if (isFlagsChangedEvent) {
 143             int[] in = new int[] {modifierFlags, keyCode};
 144             int[] out = new int[3]; // [jkeyCode, jkeyLocation, jkeyType]
 145 
 146             NSEvent.nsKeyModifiersToJavaKeyInfo(in, out);
 147 
 148             jkeyCode = out[0];
 149             jkeyLocation = out[1];
 150             jeventType = out[2];
 151         } else {
 152             if (chars != null && chars.length() > 0) {
 153                 testChar = chars.charAt(0);
 154                                 
 155                 //Check if String chars contains SPACE character.
 156                 if (chars.trim().isEmpty()) {
 157                     spaceKeyTyped = true;
 158                 }
 159             }
 160 
 161             char testCharIgnoringModifiers = charsIgnoringModifiers != null && charsIgnoringModifiers.length() > 0 ?
 162                     charsIgnoringModifiers.charAt(0) : KeyEvent.CHAR_UNDEFINED;
 163 
 164             int[] in = new int[] {testCharIgnoringModifiers, isDeadChar ? 1 : 0, modifierFlags, keyCode};
 165             int[] out = new int[3]; // [jkeyCode, jkeyLocation, deadChar]
 166 
 167             postsTyped = NSEvent.nsToJavaKeyInfo(in, out);
 168             if (!postsTyped) {
 169                 testChar = KeyEvent.CHAR_UNDEFINED;
 170             }
 171 
 172             if(isDeadChar){
 173                 testChar = (char) out[2];
 174                 if(testChar == 0){
 175                     return;
 176                 }
 177             }
 178 
 179             jkeyCode = out[0];
 180             jkeyLocation = out[1];
 181             jeventType = isNpapiCallback ? NSEvent.npToJavaEventType(eventType) :
 182                                            NSEvent.nsToJavaEventType(eventType);
 183         }
 184 
 185         char javaChar = NSEvent.nsToJavaChar(testChar, modifierFlags, spaceKeyTyped);
 186         // Some keys may generate a KEY_TYPED, but we can't determine
 187         // what that character is. That's likely a bug, but for now we
 188         // just check for CHAR_UNDEFINED.
 189         if (javaChar == KeyEvent.CHAR_UNDEFINED) {
 190             postsTyped = false;
 191         }
 192 
 193         int jmodifiers = NSEvent.nsToJavaModifiers(modifierFlags);
 194         long when = System.currentTimeMillis();
 195 
 196         if (jeventType == KeyEvent.KEY_PRESSED) {
 197             lastKeyPressCode = jkeyCode;
 198         }
 199         eventNotifier.notifyKeyEvent(jeventType, when, jmodifiers,
 200                 jkeyCode, javaChar, jkeyLocation);
 201 
 202         // Current browser may be sending input events, so don't
 203         // post the KEY_TYPED here.
 204         postsTyped &= needsKeyTyped;
 205 
 206         // That's the reaction on the PRESSED (not RELEASED) event as it comes to
 207         // appear in MacOSX.
 208         // Modifier keys (shift, etc) don't want to send TYPED events.
 209         // On the other hand we don't want to generate keyTyped events
 210         // for clipboard related shortcuts like Meta + [CVX]
 211         if (jeventType == KeyEvent.KEY_PRESSED && postsTyped &&
 212                 (jmodifiers & KeyEvent.META_DOWN_MASK) == 0) {
 213             // Enter and Space keys finish the input method processing,
 214             // KEY_TYPED and KEY_RELEASED events for them are synthesized in handleInputEvent.
 215             if (needsKeyReleased && (jkeyCode == KeyEvent.VK_ENTER || jkeyCode == KeyEvent.VK_SPACE)) {
 216                 return;
 217             }
 218             eventNotifier.notifyKeyEvent(KeyEvent.KEY_TYPED, when, jmodifiers,
 219                     KeyEvent.VK_UNDEFINED, javaChar,
 220                     KeyEvent.KEY_LOCATION_UNKNOWN);
 221             //If events come from Firefox, released events should also be generated.
 222             if (needsKeyReleased) {
 223                 eventNotifier.notifyKeyEvent(KeyEvent.KEY_RELEASED, when, jmodifiers,
 224                         jkeyCode, javaChar,
 225                         KeyEvent.KEY_LOCATION_UNKNOWN);
 226             }
 227         }
 228     }
 229 
 230     void handleInputEvent(String text) {
 231         if (text != null) {
 232             int index = 0, length = text.length();
 233             char c = 0;
 234             while (index < length) {
 235                 c = text.charAt(index);
 236                 eventNotifier.notifyKeyEvent(KeyEvent.KEY_TYPED,
 237                         System.currentTimeMillis(),
 238                         0, KeyEvent.VK_UNDEFINED, c,
 239                         KeyEvent.KEY_LOCATION_UNKNOWN);
 240                 index++;
 241             }
 242             eventNotifier.notifyKeyEvent(KeyEvent.KEY_RELEASED,
 243                     System.currentTimeMillis(),
 244                     0, lastKeyPressCode, c,
 245                     KeyEvent.KEY_LOCATION_UNKNOWN);
 246         }
 247     }
 248 
 249     void handleWindowFocusEvent(boolean gained, LWWindowPeer opposite) {
 250         eventNotifier.notifyActivation(gained, opposite);
 251     }
 252 }