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