1 /* 2 * Copyright (c) 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 com.sun.glass.ui.monocle.mx6; 27 28 import com.sun.glass.ui.Size; 29 import com.sun.glass.ui.monocle.NativeCursor; 30 import com.sun.glass.ui.monocle.NativeCursors; 31 import com.sun.glass.ui.monocle.NativePlatformFactory; 32 import com.sun.glass.ui.monocle.NativeScreen; 33 import com.sun.glass.ui.monocle.linux.LinuxSystem; 34 import com.sun.glass.ui.monocle.linux.SysFS; 35 import com.sun.glass.ui.monocle.util.C; 36 37 import java.io.IOException; 38 import java.nio.Buffer; 39 import java.nio.ByteBuffer; 40 import java.nio.ByteOrder; 41 import java.nio.IntBuffer; 42 import java.nio.ShortBuffer; 43 44 /** Cursor using a framebuffer overlay on Freescale i.MX6. */ 45 public class MX6Cursor implements NativeCursor { 46 47 private int hotspotX; 48 private int hotspotY; 49 private int offsetX; 50 private int offsetY; 51 private int cursorX; 52 private int cursorY; 53 private static final int SHORT_KEY = 0xABAB; 54 private static final int CURSOR_WIDTH = 16; 55 private static final int CURSOR_HEIGHT = 16; 56 private Buffer cursorBuffer; 57 private Buffer offsetCursorBuffer; 58 private ByteBuffer offsetCursorByteBuffer; 59 private int screenWidth; 60 private int screenHeight; 61 private LinuxSystem system; 62 private MXCFBPos pos = new MXCFBPos(); 63 private MXCFBGblAlpha alpha = new MXCFBGblAlpha(); 64 private long fd = -1; 65 66 private static class MXCFBColorKey extends C.Structure { 67 private final IntBuffer data; 68 MXCFBColorKey() { 69 b.order(ByteOrder.nativeOrder()); 70 data = b.asIntBuffer(); 71 } 72 @Override 73 public int sizeof() { 74 return 8; 75 } 76 void setEnable(int enable) { 77 data.put(0, enable); 78 } 79 void setColorKey(int key) { 80 data.put(1, key); 81 } 82 } 83 84 private static class MXCFBGblAlpha extends C.Structure { 85 private final IntBuffer data; 86 MXCFBGblAlpha() { 87 b.order(ByteOrder.nativeOrder()); 88 data = b.asIntBuffer(); 89 } 90 @Override 91 public int sizeof() { 92 return 8; 93 } 94 void setEnable(int enable) { 95 data.put(0, enable); 96 } 97 void setAlpha(int alpha) { 98 data.put(1, alpha); 99 } 100 } 101 102 private static class MXCFBPos extends C.Structure { 103 private final ShortBuffer data; 104 MXCFBPos() { 105 b.order(ByteOrder.nativeOrder()); 106 data = b.asShortBuffer(); 107 } 108 @Override 109 public int sizeof() { 110 return 4; 111 } 112 void set(int x, int y) { 113 data.put(0, (short) x); 114 data.put(1, (short) y); 115 } 116 } 117 118 public MX6Cursor() { 119 try { 120 SysFS.write("/sys/class/graphics/fb1/blank", "0"); 121 system = LinuxSystem.getLinuxSystem(); 122 LinuxSystem.FbVarScreenInfo screen = new LinuxSystem.FbVarScreenInfo(); 123 fd = system.open("/dev/fb1", LinuxSystem.O_RDWR); 124 if (fd == -1) { 125 throw new IOException(system.getErrorMessage()); 126 } 127 system.ioctl(fd, LinuxSystem.FBIOGET_VSCREENINFO, screen.p); 128 screen.setRes(screen.p, CURSOR_WIDTH, CURSOR_HEIGHT); 129 screen.setVirtualRes(screen.p, CURSOR_WIDTH, CURSOR_HEIGHT); 130 screen.setOffset(screen.p, 0, 0); 131 screen.setActivate(screen.p, 0); 132 // set up cursor as 16-bit 133 screen.setBitsPerPixel(screen.p, 16); 134 screen.setRed(screen.p, 5, 11); 135 screen.setGreen(screen.p, 6, 5); 136 screen.setBlue(screen.p, 5, 0); 137 screen.setTransp(screen.p, 0, 0); 138 system.ioctl(fd, LinuxSystem.FBIOPUT_VSCREENINFO, screen.p); 139 system.ioctl(fd, LinuxSystem.FBIOBLANK, LinuxSystem.FB_BLANK_UNBLANK); 140 141 MXCFBColorKey key = new MXCFBColorKey(); 142 key.setEnable(1); 143 key.setColorKey(((SHORT_KEY & 0xf800)<<8) 144 | ((SHORT_KEY & 0xe000)<<3) 145 | ((SHORT_KEY & 0x07e0)<<5) 146 | ((SHORT_KEY & 0x0600)>>1) 147 | ((SHORT_KEY & 0x001f)<<3) 148 | ((SHORT_KEY & 0x001c)>>2)); 149 int MXCFB_SET_CLR_KEY = system.IOW('F', 0x22, key.sizeof()); 150 if (system.ioctl(fd, MXCFB_SET_CLR_KEY, key.p) < 0) { 151 throw new IOException(system.strerror(system.errno())); 152 } 153 } catch (IOException e) { 154 if (fd != -1) { 155 LinuxSystem.getLinuxSystem().close(fd); 156 fd = -1; 157 } 158 e.printStackTrace(); 159 System.err.println("Failed to initialize i.MX6 cursor"); 160 } 161 NativeScreen screen = NativePlatformFactory.getNativePlatform().getScreen(); 162 screenWidth = screen.getWidth(); 163 screenHeight = screen.getHeight(); 164 } 165 166 @Override 167 public Size getBestSize() { 168 return new Size(CURSOR_WIDTH, CURSOR_HEIGHT); 169 } 170 171 @Override 172 public void setVisibility(boolean visibility) { 173 alpha.setEnable(1); 174 alpha.setAlpha(visibility ? 255 : 0); 175 int MXCFB_SET_GBL_ALPHA = system.IOW('F', 0x21, alpha.sizeof()); 176 system.ioctl(fd, MXCFB_SET_GBL_ALPHA, alpha.p); 177 } 178 179 private void updateImage(boolean always) { 180 int newOffsetX, newOffsetY; 181 newOffsetX = Math.max(0, CURSOR_WIDTH + cursorX - screenWidth); 182 newOffsetY = Math.max(0, CURSOR_HEIGHT + cursorY - screenHeight); 183 if (newOffsetX != offsetX || newOffsetY != offsetY || always) { 184 NativeCursors.offsetCursor(cursorBuffer, offsetCursorBuffer, 185 newOffsetX, newOffsetY, 186 CURSOR_WIDTH, CURSOR_HEIGHT, 187 16, SHORT_KEY); 188 offsetX = newOffsetX; 189 offsetY = newOffsetY; 190 system.lseek(fd, 0, LinuxSystem.SEEK_SET); 191 if (system.write(fd, offsetCursorByteBuffer, 192 0, offsetCursorByteBuffer.capacity()) < 0) { 193 System.err.println("Failed to write to i.MX6 cursor: " 194 + system.getErrorMessage()); 195 } 196 } 197 } 198 199 @Override 200 public void setImage(byte[] cursorImage) { 201 // Convert the cursor to the color-keyed format 202 ByteBuffer bb = ByteBuffer.allocate(cursorImage.length); 203 cursorBuffer = bb.asShortBuffer(); 204 NativeCursors.colorKeyCursor(cursorImage, cursorBuffer, 16, SHORT_KEY); 205 // Create an offset version of the cursor for rendering 206 offsetCursorByteBuffer = ByteBuffer.allocateDirect(cursorImage.length); 207 offsetCursorByteBuffer.order(ByteOrder.nativeOrder()); 208 offsetCursorBuffer = offsetCursorByteBuffer.asShortBuffer(); 209 updateImage(true); 210 } 211 212 @Override 213 public void setLocation(int x, int y) { 214 cursorX = x; 215 cursorY = y; 216 updateImage(false); 217 pos.set(x, y); 218 int MXCFB_SET_OVERLAY_POS = system.IOWR('F', 0x24, pos.sizeof()); 219 system.ioctl(fd, MXCFB_SET_OVERLAY_POS, pos.p); 220 } 221 222 @Override 223 public void setHotSpot(int hotspotX, int hotspotY) { 224 this.hotspotX = hotspotX; 225 this.hotspotY = hotspotY; 226 } 227 228 @Override 229 public void shutdown() { 230 setVisibility(false); 231 } 232 }