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