1 /*
   2  * Copyright (c) 2011, 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 
  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 absoluteX,
  58                           int absoluteY) {
  59         final SunToolkit tk = (SunToolkit)Toolkit.getDefaultToolkit();
  60         if ((buttonNumber > 2 && !tk.areExtraMouseButtonsEnabled())
  61                 || buttonNumber > tk.getNumberOfButtons() - 1) {
  62             return;
  63         }
  64 
  65         int jeventType = isNpapiCallback ? NSEvent.npToJavaEventType(eventType) :
  66                                            NSEvent.nsToJavaEventType(eventType);
  67 
  68         int jbuttonNumber = MouseEvent.NOBUTTON;
  69         int jclickCount = 0;
  70 
  71         if (jeventType != MouseEvent.MOUSE_MOVED &&
  72             jeventType != MouseEvent.MOUSE_ENTERED &&
  73             jeventType != MouseEvent.MOUSE_EXITED)
  74         {
  75             jbuttonNumber = NSEvent.nsToJavaButton(buttonNumber);
  76             jclickCount = clickCount;
  77         }
  78 
  79         int jmodifiers = NSEvent.nsToJavaMouseModifiers(buttonNumber,
  80                                                         modifierFlags);
  81         boolean jpopupTrigger = NSEvent.isPopupTrigger(jmodifiers);
  82 
  83         eventNotifier.notifyMouseEvent(jeventType, System.currentTimeMillis(), jbuttonNumber,
  84                 x, y, absoluteX, absoluteY, jmodifiers, jclickCount,
  85                 jpopupTrigger, null);
  86     }
  87 
  88     /**
  89      * Handles scroll events.
  90      */
  91     void handleScrollEvent(final int x, final int y, 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, 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, jmodifiers, delta);
 107         }
 108     }
 109 
 110     private void dispatchScrollEvent(final int x, final int y,
 111                                      final int modifiers, final double delta) {
 112         final long when = System.currentTimeMillis();
 113         final int scrollType = MouseWheelEvent.WHEEL_UNIT_SCROLL;
 114         final int scrollAmount = 1;
 115         int wheelRotation = (int) delta;
 116         int signum = (int) Math.signum(delta);
 117         if (signum * delta < 1) {
 118             wheelRotation = signum;
 119         }
 120         // invert the wheelRotation for the peer
 121         eventNotifier.notifyMouseWheelEvent(when, x, y, modifiers, scrollType,
 122                 scrollAmount, -wheelRotation, -delta, null);
 123     }
 124 
 125     /**
 126      * Handles key events.
 127      */
 128     void handleKeyEvent(int eventType, int modifierFlags, String chars,
 129                         short keyCode, boolean needsKeyTyped, boolean needsKeyReleased) {
 130         boolean isFlagsChangedEvent =
 131             isNpapiCallback ? (eventType == CocoaConstants.NPCocoaEventFlagsChanged) :
 132                               (eventType == CocoaConstants.NSFlagsChanged);
 133 
 134         int jeventType = KeyEvent.KEY_PRESSED;
 135         int jkeyCode = KeyEvent.VK_UNDEFINED;
 136         int jkeyLocation = KeyEvent.KEY_LOCATION_UNKNOWN;
 137         boolean postsTyped = 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 
 156             int[] in = new int[] {testChar, isDeadChar ? 1 : 0, modifierFlags, keyCode};
 157             int[] out = new int[3]; // [jkeyCode, jkeyLocation, deadChar]
 158 
 159             postsTyped = NSEvent.nsToJavaKeyInfo(in, out);
 160             if (!postsTyped) {
 161                 testChar = KeyEvent.CHAR_UNDEFINED;
 162             }
 163 
 164             if(isDeadChar){
 165                 testChar = (char) out[2];
 166                 if(testChar == 0){
 167                     return;
 168                 }
 169             }
 170 
 171             jkeyCode = out[0];
 172             jkeyLocation = out[1];
 173             jeventType = isNpapiCallback ? NSEvent.npToJavaEventType(eventType) :
 174                                            NSEvent.nsToJavaEventType(eventType);
 175         }
 176 
 177         char javaChar = NSEvent.nsToJavaChar(testChar, modifierFlags);
 178         // Some keys may generate a KEY_TYPED, but we can't determine
 179         // what that character is. That's likely a bug, but for now we
 180         // just check for CHAR_UNDEFINED.
 181         if (javaChar == KeyEvent.CHAR_UNDEFINED) {
 182             postsTyped = false;
 183         }
 184 
 185 
 186         int jmodifiers = NSEvent.nsToJavaKeyModifiers(modifierFlags);
 187         long when = System.currentTimeMillis();
 188 
 189         if (jeventType == KeyEvent.KEY_PRESSED) {
 190             lastKeyPressCode = jkeyCode;
 191         }
 192         eventNotifier.notifyKeyEvent(jeventType, when, jmodifiers,
 193                 jkeyCode, javaChar, jkeyLocation);
 194 
 195         // Current browser may be sending input events, so don't
 196         // post the KEY_TYPED here.
 197         postsTyped &= needsKeyTyped;
 198 
 199         // That's the reaction on the PRESSED (not RELEASED) event as it comes to
 200         // appear in MacOSX.
 201         // Modifier keys (shift, etc) don't want to send TYPED events.
 202         // On the other hand we don't want to generate keyTyped events
 203         // for clipboard related shortcuts like Meta + [CVX]
 204         if (jeventType == KeyEvent.KEY_PRESSED && postsTyped &&
 205                 (jmodifiers & KeyEvent.META_DOWN_MASK) == 0) {
 206             // Enter and Space keys finish the input method processing,
 207             // KEY_TYPED and KEY_RELEASED events for them are synthesized in handleInputEvent.
 208             if (needsKeyReleased && (jkeyCode == KeyEvent.VK_ENTER || jkeyCode == KeyEvent.VK_SPACE)) {
 209                 return;
 210             }
 211             eventNotifier.notifyKeyEvent(KeyEvent.KEY_TYPED, when, jmodifiers,
 212                     KeyEvent.VK_UNDEFINED, javaChar,
 213                     KeyEvent.KEY_LOCATION_UNKNOWN);
 214             //If events come from Firefox, released events should also be generated.
 215             if (needsKeyReleased) {
 216                 eventNotifier.notifyKeyEvent(KeyEvent.KEY_RELEASED, when, jmodifiers,
 217                         jkeyCode, javaChar,
 218                         KeyEvent.KEY_LOCATION_UNKNOWN);
 219             }
 220         }
 221     }
 222 
 223     void handleInputEvent(String text) {
 224         if (text != null) {
 225             int index = 0, length = text.length();
 226             char c = 0;
 227             while (index < length) {
 228                 c = text.charAt(index);
 229                 eventNotifier.notifyKeyEvent(KeyEvent.KEY_TYPED,
 230                         System.currentTimeMillis(),
 231                         0, KeyEvent.VK_UNDEFINED, c,
 232                         KeyEvent.KEY_LOCATION_UNKNOWN);
 233                 index++;
 234             }
 235             eventNotifier.notifyKeyEvent(KeyEvent.KEY_RELEASED,
 236                     System.currentTimeMillis(),
 237                     0, lastKeyPressCode, c,
 238                     KeyEvent.KEY_LOCATION_UNKNOWN);
 239         }
 240     }
 241 
 242     void handleWindowFocusEvent(boolean gained, LWWindowPeer opposite) {
 243         eventNotifier.notifyActivation(gained, opposite);
 244     }
 245 }