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         peer.dispatchMouseWheelEvent(when, x, y, modifiers, scrollType,
 113                                      scrollAmount, (int) -delta, -delta, null);
 114     }
 115 
 116     /**
 117      * Handles key events.
 118      */
 119     void handleKeyEvent(int eventType, int modifierFlags, String chars,
 120                         short keyCode) {
 121         boolean isFlagsChangedEvent =
 122             isNpapiCallback ? (eventType == CocoaConstants.NPCocoaEventFlagsChanged) :
 123                               (eventType == CocoaConstants.NSFlagsChanged);
 124 
 125         int jeventType = KeyEvent.KEY_PRESSED;
 126         int jkeyCode = KeyEvent.VK_UNDEFINED;
 127         int jkeyLocation = KeyEvent.KEY_LOCATION_UNKNOWN;
 128         boolean postsTyped = false;
 129 
 130         char testChar = KeyEvent.CHAR_UNDEFINED;
 131         char testDeadChar = 0;
 132 
 133         if (isFlagsChangedEvent) {
 134             int[] in = new int[] {modifierFlags, keyCode};
 135             int[] out = new int[3]; // [jkeyCode, jkeyLocation, jkeyType]
 136 
 137             NSEvent.nsKeyModifiersToJavaKeyInfo(in, out);
 138 
 139             jkeyCode = out[0];
 140             jkeyLocation = out[1];
 141             jeventType = out[2];
 142         } else {
 143             if (chars != null && chars.length() > 0) {
 144                 testChar = chars.charAt(0);
 145             }
 146 
 147             int[] in = new int[] {testChar, testDeadChar, modifierFlags, keyCode};
 148             int[] out = new int[2]; // [jkeyCode, jkeyLocation]
 149 
 150             postsTyped = NSEvent.nsToJavaKeyInfo(in, out);
 151             if (!postsTyped) {
 152                 testChar = KeyEvent.CHAR_UNDEFINED;
 153             }
 154 
 155             jkeyCode = out[0];
 156             jkeyLocation = out[1];
 157             jeventType = isNpapiCallback ? NSEvent.npToJavaEventType(eventType) :
 158                                            NSEvent.nsToJavaEventType(eventType);
 159         }
 160 
 161         int jmodifiers = NSEvent.nsToJavaKeyModifiers(modifierFlags);
 162         long when = System.currentTimeMillis();
 163 
 164         peer.dispatchKeyEvent(jeventType, when, jmodifiers,
 165                               jkeyCode, testChar, jkeyLocation);
 166 
 167         // That's the reaction on the PRESSED (not RELEASED) event as it comes to
 168         // appear in MacOSX.
 169         // Modifier keys (shift, etc) don't want to send TYPED events.
 170         // On the other hand we don't want to generate keyTyped events
 171         // for clipboard related shortcuts like Meta + [CVX]
 172         boolean isMetaDown = (jmodifiers & KeyEvent.META_DOWN_MASK) != 0;
 173         if (jeventType == KeyEvent.KEY_PRESSED && postsTyped && !isMetaDown) {
 174             peer.dispatchKeyEvent(KeyEvent.KEY_TYPED, when, jmodifiers,
 175                                   KeyEvent.VK_UNDEFINED, testChar,
 176                                   KeyEvent.KEY_LOCATION_UNKNOWN);
 177         }
 178     }
 179 }