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.lwawt.LWCursorManager; 29 30 import java.awt.Cursor; 31 import java.awt.Point; 32 import java.awt.geom.Point2D; 33 34 final class CCursorManager extends LWCursorManager { 35 36 private static native Point2D nativeGetCursorPosition(); 37 private static native void nativeSetBuiltInCursor(final int type, final String name); 38 private static native void nativeSetCustomCursor(final long imgPtr, final double x, final double y); 39 public static native void nativeSetAllowsCursorSetInBackground(final boolean allows); 40 41 private static final int NAMED_CURSOR = -1; 42 43 private static final CCursorManager theInstance = new CCursorManager(); 44 public static CCursorManager getInstance() { 45 return theInstance; 46 } 47 48 private volatile Cursor currentCursor; 49 50 private CCursorManager() { } 51 52 @Override 53 protected Point getCursorPosition() { 54 synchronized(this) { 55 if (isDragging) { 56 // during the drag operation, the appkit thread is blocked, 57 // so nativeGetCursorPosition invocation may cause a deadlock. 58 // In order to avoid this, we returns last know cursor position. 59 return new Point(dragPos); 60 } 61 } 62 63 final Point2D nativePosition = nativeGetCursorPosition(); 64 return new Point((int)nativePosition.getX(), (int)nativePosition.getY()); 65 } 66 67 @Override 68 protected void setCursor(final Cursor cursor) { 69 if (cursor == currentCursor) { 70 return; 71 } 72 currentCursor = cursor; 73 74 if (cursor == null) { 75 nativeSetBuiltInCursor(Cursor.DEFAULT_CURSOR, null); 76 return; 77 } 78 79 if (cursor instanceof CCustomCursor) { 80 final CCustomCursor customCursor = (CCustomCursor) cursor; 81 final long imagePtr = customCursor.getImageData(); 82 if (imagePtr != 0L) { 83 final Point hotSpot = customCursor.getHotSpot(); 84 nativeSetCustomCursor(imagePtr, hotSpot.x, hotSpot.y); 85 } 86 return; 87 } 88 89 final int type = cursor.getType(); 90 if (type != Cursor.CUSTOM_CURSOR) { 91 nativeSetBuiltInCursor(type, null); 92 return; 93 } 94 95 final String name = cursor.getName(); 96 if (name != null) { 97 nativeSetBuiltInCursor(NAMED_CURSOR, name); 98 return; 99 } 100 101 // do something special 102 throw new RuntimeException("Unimplemented"); 103 } 104 105 // package private methods to handle cursor change during drag-and-drop 106 private boolean isDragging = false; 107 private Point dragPos = null; 108 109 synchronized void startDrag(int x, int y) { 110 if (isDragging) { 111 throw new RuntimeException("Invalid Drag state in CCursorManager!"); 112 } 113 isDragging = true; 114 dragPos = new Point(x, y); 115 } 116 117 synchronized void updateDragPosition(int x, int y) { 118 if (!isDragging) { 119 throw new RuntimeException("Invalid Drag state in CCursorManager!"); 120 } 121 dragPos.move(x, y); 122 } 123 124 synchronized void stopDrag() { 125 if (!isDragging) { 126 throw new RuntimeException("Invalid Drag state in CCursorManager!"); 127 } 128 isDragging = false; 129 dragPos = null; 130 } 131 }