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 java.awt.event.*;
  29 
  30 /**
  31  * A class representing Cocoa NSEvent class with the fields only necessary for
  32  * JDK functionality.
  33  */
  34 final class NSEvent {
  35     private int type;
  36     private int modifierFlags;
  37 
  38     // Mouse event information
  39     private int clickCount;
  40     private int buttonNumber;
  41     private int x;
  42     private int y;
  43     private double scrollDeltaY;
  44     private double scrollDeltaX;
  45     private int absX;
  46     private int absY;
  47 
  48     // Key event information
  49     private short keyCode;
  50     private String characters;
  51     private String charactersIgnoringModifiers;
  52 
  53     // Called from native
  54     NSEvent(int type, int modifierFlags, short keyCode, String characters, String charactersIgnoringModifiers) {
  55         this.type = type;
  56         this.modifierFlags = modifierFlags;
  57         this.keyCode = keyCode;
  58         this.characters = characters;
  59         this.charactersIgnoringModifiers = charactersIgnoringModifiers;
  60     }
  61 
  62     // Called from native
  63     NSEvent(int type, int modifierFlags, int clickCount, int buttonNumber,
  64                    int x, int y, int absX, int absY,
  65                    double scrollDeltaY, double scrollDeltaX) {
  66         this.type = type;
  67         this.modifierFlags = modifierFlags;
  68         this.clickCount = clickCount;
  69         this.buttonNumber = buttonNumber;
  70         this.x = x;
  71         this.y = y;
  72         this.absX = absX;
  73         this.absY = absY;
  74         this.scrollDeltaY = scrollDeltaY;
  75         this.scrollDeltaX = scrollDeltaX;
  76     }
  77 
  78     int getType() {
  79         return type;
  80     }
  81 
  82     int getModifierFlags() {
  83         return modifierFlags;
  84     }
  85 
  86     int getClickCount() {
  87         return clickCount;
  88     }
  89 
  90     int getButtonNumber() {
  91         return buttonNumber;
  92     }
  93 
  94     int getX() {
  95         return x;
  96     }
  97 
  98     int getY() {
  99         return y;
 100     }
 101 
 102     double getScrollDeltaY() {
 103         return scrollDeltaY;
 104     }
 105 
 106     double getScrollDeltaX() {
 107         return scrollDeltaX;
 108     }
 109 
 110     int getAbsX() {
 111         return absX;
 112     }
 113 
 114     int getAbsY() {
 115         return absY;
 116     }
 117 
 118     short getKeyCode() {
 119         return keyCode;
 120     }
 121 
 122     String getCharactersIgnoringModifiers() {
 123         return charactersIgnoringModifiers;
 124     }
 125 
 126     String getCharacters() {
 127         return characters;
 128     }
 129 
 130     @Override
 131     public String toString() {
 132         return "NSEvent[" + getType() + " ," + getModifierFlags() + " ,"
 133                 + getClickCount() + " ," + getButtonNumber() + " ," + getX() + " ,"
 134                 + getY() + " ," + getAbsX() + " ," + getAbsY()+ " ," + getKeyCode() + " ,"
 135                 + getCharacters() + " ," + getCharactersIgnoringModifiers() + "]";
 136     }
 137 
 138     /*
 139      * Converts an NSEvent button number to a MouseEvent constant.
 140      */
 141     static int nsToJavaButton(int buttonNumber) {
 142         int jbuttonNumber = buttonNumber + 1;
 143         switch (buttonNumber) {
 144             case CocoaConstants.kCGMouseButtonLeft:
 145                 jbuttonNumber = MouseEvent.BUTTON1;
 146                 break;
 147             case CocoaConstants.kCGMouseButtonRight:
 148                 jbuttonNumber = MouseEvent.BUTTON3;
 149                 break;
 150             case CocoaConstants.kCGMouseButtonCenter:
 151                 jbuttonNumber = MouseEvent.BUTTON2;
 152                 break;
 153         }
 154         return jbuttonNumber;
 155     }
 156 
 157     /*
 158      * Converts NPCocoaEvent types to AWT event types.
 159      */
 160     static int npToJavaEventType(int npEventType) {
 161         int jeventType = 0;
 162         switch (npEventType) {
 163             case CocoaConstants.NPCocoaEventMouseDown:
 164                 jeventType = MouseEvent.MOUSE_PRESSED;
 165                 break;
 166             case CocoaConstants.NPCocoaEventMouseUp:
 167                 jeventType = MouseEvent.MOUSE_RELEASED;
 168                 break;
 169             case CocoaConstants.NPCocoaEventMouseMoved:
 170                 jeventType = MouseEvent.MOUSE_MOVED;
 171                 break;
 172             case CocoaConstants.NPCocoaEventMouseEntered:
 173                 jeventType = MouseEvent.MOUSE_ENTERED;
 174                 break;
 175             case CocoaConstants.NPCocoaEventMouseExited:
 176                 jeventType = MouseEvent.MOUSE_EXITED;
 177                 break;
 178             case CocoaConstants.NPCocoaEventMouseDragged:
 179                 jeventType = MouseEvent.MOUSE_DRAGGED;
 180                 break;
 181             case CocoaConstants.NPCocoaEventKeyDown:
 182                 jeventType = KeyEvent.KEY_PRESSED;
 183                 break;
 184             case CocoaConstants.NPCocoaEventKeyUp:
 185                 jeventType = KeyEvent.KEY_RELEASED;
 186                 break;
 187         }
 188         return jeventType;
 189     }
 190 
 191     /*
 192      * Converts NSEvent types to AWT event types.
 193      */
 194     static int nsToJavaEventType(int nsEventType) {
 195         int jeventType = 0;
 196         switch (nsEventType) {
 197             case CocoaConstants.NSLeftMouseDown:
 198             case CocoaConstants.NSRightMouseDown:
 199             case CocoaConstants.NSOtherMouseDown:
 200                 jeventType = MouseEvent.MOUSE_PRESSED;
 201                 break;
 202             case CocoaConstants.NSLeftMouseUp:
 203             case CocoaConstants.NSRightMouseUp:
 204             case CocoaConstants.NSOtherMouseUp:
 205                 jeventType = MouseEvent.MOUSE_RELEASED;
 206                 break;
 207             case CocoaConstants.NSMouseMoved:
 208                 jeventType = MouseEvent.MOUSE_MOVED;
 209                 break;
 210             case CocoaConstants.NSLeftMouseDragged:
 211             case CocoaConstants.NSRightMouseDragged:
 212             case CocoaConstants.NSOtherMouseDragged:
 213                 jeventType = MouseEvent.MOUSE_DRAGGED;
 214                 break;
 215             case CocoaConstants.NSMouseEntered:
 216                 jeventType = MouseEvent.MOUSE_ENTERED;
 217                 break;
 218             case CocoaConstants.NSMouseExited:
 219                 jeventType = MouseEvent.MOUSE_EXITED;
 220                 break;
 221             case CocoaConstants.NSScrollWheel:
 222                 jeventType = MouseEvent.MOUSE_WHEEL;
 223                 break;
 224             case CocoaConstants.NSKeyDown:
 225                 jeventType = KeyEvent.KEY_PRESSED;
 226                 break;
 227             case CocoaConstants.NSKeyUp:
 228                 jeventType = KeyEvent.KEY_RELEASED;
 229                 break;
 230         }
 231         return jeventType;
 232     }
 233 
 234     /**
 235      * Converts NSEvent key modifiers to AWT key modifiers. Note that this
 236      * method adds the current mouse state as a mouse modifiers.
 237      *
 238      * @param  modifierFlags the NSEvent key modifiers
 239      * @return the java key and mouse modifiers
 240      */
 241     static native int nsToJavaModifiers(int modifierFlags);
 242 
 243     /*
 244      * Converts NSEvent key info to AWT key info.
 245      */
 246     static native boolean nsToJavaKeyInfo(int[] in, int[] out);
 247 
 248     /*
 249      * Converts NSEvent key modifiers to AWT key info.
 250      */
 251     static native void nsKeyModifiersToJavaKeyInfo(int[] in, int[] out);
 252 
 253     /*
 254      * There is a small number of NS characters that need to be converted
 255      * into other characters before we pass them to AWT.
 256      */
 257     static native char nsToJavaChar(char nsChar, int modifierFlags);
 258 
 259     static boolean isPopupTrigger(int jmodifiers) {
 260         final boolean isRightButtonDown = ((jmodifiers & InputEvent.BUTTON3_DOWN_MASK) != 0);
 261         final boolean isLeftButtonDown = ((jmodifiers & InputEvent.BUTTON1_DOWN_MASK) != 0);
 262         final boolean isControlDown = ((jmodifiers & InputEvent.CTRL_DOWN_MASK) != 0);
 263         return isRightButtonDown || (isControlDown && isLeftButtonDown);
 264     }
 265 }