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