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