1 /*
   2  * Copyright (c) 2012, 2013, 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 
  27 package com.sun.glass.ui.lens;
  28 
  29 import java.nio.Buffer;
  30 import java.nio.ByteBuffer;
  31 import java.nio.IntBuffer;
  32 import com.sun.glass.ui.Cursor;
  33 import com.sun.glass.ui.Pixels;
  34 import com.sun.glass.ui.Size;
  35 import com.sun.glass.ui.Application;
  36 
  37 final class LensCursor extends Cursor {
  38 
  39     // LensCursor doesn't use ptr from superclass
  40     private long ptr = 0;
  41 
  42     protected LensCursor(int type) {
  43         super(type);
  44 
  45         if (type != CURSOR_NONE) {
  46             ptr = _createNativeCursorByType(type);
  47         }
  48     }
  49 
  50     protected LensCursor(int x, int y, Pixels pixels) {
  51         super(x, y, pixels);
  52         ptr = getNativeCursor();
  53     }
  54 
  55 
  56     protected void finalize() throws Throwable {
  57         try {
  58             if (ptr != 0) {
  59                 _releaseNativeCursor(ptr);
  60             }
  61         } finally {
  62             super.finalize();
  63         }
  64     }
  65 
  66     @Override
  67     protected long _createCursor(int x, int y, Pixels pixels) {
  68 
  69         long res = 0;
  70         Buffer data = pixels.getPixels();
  71         int width = pixels.getWidth();
  72         int height = pixels.getHeight();
  73 
  74         if (data != null) {
  75             if (!data.isDirect()) {
  76                 if (pixels.getBytesPerComponent() == 4) {
  77                     IntBuffer ints = (IntBuffer)(data.rewind());
  78                     int[] intArray = ints.array();
  79                     res =  _createNativeCursorInts(x, y, intArray, width, height);
  80                 } else if (pixels.getBytesPerComponent() == 1) {
  81                     ByteBuffer bytes = (ByteBuffer)(data.rewind());
  82                     byte[] byteArray = bytes.array();
  83                     res =  _createNativeCursorBytes(x, y, byteArray, width, height);
  84                 }
  85             } else {
  86                 data.rewind();
  87                 res =  _createNativeCursorDirect(x, y, data, data.capacity(), width , height);
  88 
  89             }
  90         }
  91         return res;
  92     }
  93 
  94     void set() {
  95         if (ptr != 0) {
  96             _setNativeCursor(ptr);
  97         }
  98 
  99         int type = getType();
 100         if (type == CURSOR_NONE) {
 101             // CURSOR_NONE is mapped to setVisible(false) and will be registered
 102             // in LensApplication as a user preference to not show the cursor.
 103             ((LensApplication)Application.GetApplication()).staticCursor_setVisible(false);
 104         } else {
 105             ((LensApplication)Application.GetApplication()).staticCursor_setVisible(true);
 106         }
 107     }
 108 
 109 
 110 
 111     static  void setVisible_impl(boolean visible) {
 112         _setVisible(visible);
 113     }
 114 
 115     static Size getBestSize_impl(int width, int height) {
 116         return new Size(16, 16);
 117     }
 118 
 119 
 120     native private void _setNativeCursor(long ptr);
 121     native private void _releaseNativeCursor(long ptr);
 122 
 123     native private long _createNativeCursorByType(int type);
 124 
 125     native private long _createNativeCursorInts(int x, int y, int[] array, int width, int height);
 126     native private long _createNativeCursorBytes(int x, int y, byte[] array, int width, int height);
 127     native private long _createNativeCursorDirect(int x, int y, Buffer array, int capacity, int width, int height);
 128 
 129     native static private void _setVisible(boolean isVisible);
 130 }