vmwarecore.java

来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 877 行 · 第 1/2 页

JAVA
877 行
字号
						}
						break;
					case 32 :
						ofs = ofsY + (x << 2);
						videoRam.xorInt(ofs, color, w);
						break;
					default :
						throw new RuntimeException("Unknown bitsPerPixel");
				}
			} else {
				switch (bitsPerPixel) {
					case 8 :
						ofs = ofsY + x;
						videoRam.setByte(ofs, (byte) color, w);
						break;
					case 16 :
						ofs = ofsY + (x << 1);
						videoRam.setShort(ofs, (short) color, w);
						break;
					case 24 :
						ofs = ofsY + (x * 3);
						while (w > 0) {
							videoRam.setShort(ofs, (short) (color & 0xFFFF));
							videoRam.setByte(ofs + 2, (byte) ((color >> 16) & 0xFF));
							w--;
							ofs += 3;
						}
						break;
					case 32 :
						ofs = ofsY + (x << 2);
						videoRam.setInt(ofs, color, w);
						break;
					default :
						throw new RuntimeException("Unknown bitsPerPixel");
				}
			}
		}
	}

	/**
	 * Gets the contents of a 32-bit register
	 * 
	 * @param index
	 * @return
	 */
	private final int getReg32(int index) {
		ports.outPortDword(indexPort, index);
		return ports.inPortDword(valuePort);
	}

	/**
	 * Sets the contents of a 32-bit register
	 * 
	 * @param index
	 * @param value
	 */
	private final void setReg32(int index, int value) {
		ports.outPortDword(indexPort, index);
		ports.outPortDword(valuePort, value);
	}

	/**
	 * Write the given word to the FIFO.
	 * 
	 * @param value
	 */
	private final void writeWordToFIFO(int value) {
		fifoDirty = true;
		//Debug.out.println ("VMWare::WriteWordToFIFO(" + Integer.toHexString(nValue) + ") pos: "
		// + ReadFIFO (SVGA_FIFO_NEXT_CMD));
		/* Need to sync? */
		if ((getFIFO(SVGA_FIFO_NEXT_CMD) + 4 == getFIFO(SVGA_FIFO_STOP))
			|| (getFIFO(SVGA_FIFO_NEXT_CMD) == getFIFO(SVGA_FIFO_MAX) - 4 && getFIFO(SVGA_FIFO_STOP) == getFIFO(SVGA_FIFO_MIN))) {
			log.debug("VMWare::WriteWordToFIFO() syncing FIFO");
			setReg32(SVGA_REG_SYNC, 1);
			while (getReg32(SVGA_REG_BUSY) != 0) {
				Thread.yield();
			}
		}
		setFIFO(getFIFO(SVGA_FIFO_NEXT_CMD) / 4, value);
		setFIFO(SVGA_FIFO_NEXT_CMD, getFIFO(SVGA_FIFO_NEXT_CMD) + 4);
		if (getFIFO(SVGA_FIFO_NEXT_CMD) == getFIFO(SVGA_FIFO_MAX)) {
			setFIFO(SVGA_FIFO_NEXT_CMD, getFIFO(SVGA_FIFO_MIN));
		}
	}

	/**
	 * Wait until the SVGA is ready
	 */
	private final void syncFIFO() {
		if (fifoDirty) {
			setReg32(SVGA_REG_SYNC, 1);
			while (getReg32(SVGA_REG_BUSY) != 0) {
				Thread.yield();
			}
			fifoDirty = false;
		}
	}

	/**
	 * Gets a FIFO entry
	 * 
	 * @param index
	 * @return
	 */
	private final int getFIFO(int index) {
		return fifo.getInt(index * 4);
	}

	/**
	 * Sets a FIFO entry
	 * 
	 * @param index
	 * @param value
	 */
	private final void setFIFO(int index, int value) {
		fifo.setInt(index * 4, value);
	}

	/**
	 * Convert the given color to a value suitable for VMWare
	 * 
	 * @param color
	 */
	protected final int convertColor(Color color) {
		return convertColor(color.getRed(), color.getGreen(), color.getBlue());
	}

	/**
	 * Convert the given color to a value suitable for VMWare
	 * 
	 * @param r
	 * @param g
	 * @param b
	 */
	protected final int convertColor(int r, int g, int b) {
		return ((r << redMaskShift) & redMask) | ((g << greenMaskShift) & greenMask) | ((b << blueMaskShift) & blueMask);
	}

	/**
	 * Gets the SVGA_ID of the VMware SVGA adapter. This function should hide any backward
	 * compatibility mess.
	 */
	private final int getVMWareID() {
		int vmware_svga_id;

		/*******************************************************************************************
		 * Any version with any SVGA_ID_ to SVGA_ID_0 to support versions of this driver with
		 * SVGA_ID_0.
		 * 
		 * Versions of SVGA_ID_0 ignore writes to the SVGA_REG_ID register.
		 * 
		 * Versions of SVGA_ID_1 will allow us to overwrite the content of the SVGA_REG_ID register
		 * only with the values SVGA_ID_0 or SVGA_ID_1.
		 * 
		 * Versions of SVGA_ID_2 will allow us to overwrite the content of the SVGA_REG_ID register
		 * only with the values SVGA_ID_0 or SVGA_ID_1 or SVGA_ID_2.
		 */

		setReg32(SVGA_REG_ID, SVGA_ID_2);
		vmware_svga_id = getReg32(SVGA_REG_ID);

		if (vmware_svga_id == SVGA_ID_2) {
			return SVGA_ID_2;
		}

		setReg32(SVGA_REG_ID, SVGA_ID_1);
		vmware_svga_id = getReg32(SVGA_REG_ID);
		if (vmware_svga_id == SVGA_ID_1) {
			return SVGA_ID_1;
		}

		if (vmware_svga_id == SVGA_ID_0) {
			return SVGA_ID_0;
		}

		/* No supported VMware SVGA devices found */
		return SVGA_ID_INVALID;
	}

	/**
	 * Claim and initialize the FIFO.
	 * 
	 * @param owner
	 * @param rm
	 * @return
	 */
	private final MemoryResource initFifo(ResourceOwner owner, ResourceManager rm) throws ResourceNotFreeException {
		final int physBase = getReg32(SVGA_REG_MEM_START);
		final int size = getReg32(SVGA_REG_MEM_SIZE);
		final Address address = Address.valueOf(physBase);

		log.debug("Found FIFO at 0x" + NumberUtils.hex(physBase) + ", size 0x" + NumberUtils.hex(size));

		final MemoryResource res = rm.claimMemoryResource(owner, address, size, ResourceManager.MEMMODE_NORMAL);
		res.setInt(SVGA_FIFO_MIN * 4, 16);
		res.setInt(SVGA_FIFO_MAX * 4, size);
		res.setInt(SVGA_FIFO_NEXT_CMD * 4, 16);
		res.setInt(SVGA_FIFO_STOP * 4, 16);
		setReg32(SVGA_REG_CONFIG_DONE, 1);

		return res;
	}

	/**
	 * Gets the size of the video ram in bytes.
	 */
	final int getVideoRamSize() {
		return this.videoRamSize;
	}

	/**
	 * Gets the maximum screen height in pixels
	 */
	final int getMaxHeight() {
		return this.maxHeight;
	}

	/**
	 * Gets the maximum screen width in pixels
	 */
	final int getMaxWidth() {
		return this.maxWidth;
	}

	/**
	 * Gets the number of bits per pixel
	 */
	final int getBitsPerPixel() {
		return this.bitsPerPixel;
	}

	/**
	 * Gets the number of bytes per line
	 */
	final int getBytesPerLine() {
		return this.bytesPerLine;
	}

	/**
	 * Gets the number of shift needed for the given mask.
	 * 
	 * E.g. getMaskShift(0xFF00) == 8
	 * 
	 * @param mask
	 * @return
	 */
	private final int getMaskShift(int mask) {
		int count = 0;
		while ((mask & 1) == 0) {
			count++;
			mask = mask >> 1;
		}
		return count;
	}

	/**
	 * @see org.jnode.driver.video.Surface#getColorModel()
	 */
	public ColorModel getColorModel() {
		return model;
	}

	/**
	 * @see org.jnode.driver.video.HardwareCursorAPI#setCursorPosition(int, int)
	 */
	public synchronized void setCursorPosition(int x, int y) {
		this.curX = x;
		this.curY = y;
		setCursor(curVisible, curX, curY);
	}

	/**
	 * @see org.jnode.driver.video.HardwareCursorAPI#setCursorVisible(boolean)
	 */
	public synchronized void setCursorVisible(boolean visible) {
		this.curVisible = visible;
		setCursor(curVisible, curX, curY);
	}

	
	/**
	 * Sets the cursor image.
	 * @param cursor
	 */
	public void setCursorImage(HardwareCursor cursor) {
		if (hasCapability(SVGA_CAP_ALPHA_CURSOR)) {
			defineARGBCursor(cursor.getImage(16, 16));
		} else {
			defineCursor(cursor.getImage(16, 16));
		}
	}
	
	/**
	 * Sets the cursor image.
	 */
	private void defineCursor(HardwareCursorImage cursor) {
		
		final int[] argb = cursor.getImage();
		final int size = argb.length;
		final int[] andMask = new int[size];
		final int[] xorMask = new int[size];
		
		for (int i = 0; i < size; i++) {
			final int v = argb[i];
			final int a = (v >>> 24) & 0xFF;
			final int r = (v >> 16) & 0xFF;
			final int g = (v >> 8) & 0xFF;
			final int b = v & 0xFF;
			if (a != 0) {
				// opaque
				andMask[i] = 0;
				xorMask[i] = v & convertColor(r, g, b);
			} else {
				// transparent
				andMask[i] = 0xFFFFFFFF;
				xorMask[i] = 0;
			}
		}
		
		// Wait for the FIFO
		syncFIFO();

		// Command
		writeWordToFIFO(SVGA_CMD_DEFINE_CURSOR);
		// Mouse id
		writeWordToFIFO(MOUSE_ID);
		// Hotspot X
		writeWordToFIFO(cursor.getHotSpotX());
		// Hotspot Y
		writeWordToFIFO(cursor.getHotSpotY());
		// Width
		writeWordToFIFO(cursor.getWidth());
		// Height
		writeWordToFIFO(cursor.getHeight());
		// Depth for AND mask
		writeWordToFIFO(1);
		// Depth for XOR mask
		writeWordToFIFO(getBitsPerPixel());
		// Scanlines for AND mask
		for (int i = 0; i < size; i++) {
			writeWordToFIFO(andMask[i]);
		}
		// Scanlines for XOR mask
		for (int i = 0; i < size; i++) {
			writeWordToFIFO(xorMask[i]);
		}
	}
	
	/**
	 * Sets the cursor image.
	 */
	private void defineARGBCursor(HardwareCursorImage cursor) {
		
		final int[] argb = cursor.getImage();
		final int size = argb.length;
		
		// Wait for the FIFO
		syncFIFO();

		// Command
		writeWordToFIFO(SVGA_CMD_DEFINE_ALPHA_CURSOR);
		// Mouse id
		writeWordToFIFO(MOUSE_ID);
		// Hotspot X
		writeWordToFIFO(cursor.getHotSpotX());
		// Hotspot Y
		writeWordToFIFO(cursor.getHotSpotY());
		// Width
		writeWordToFIFO(cursor.getWidth());
		// Height
		writeWordToFIFO(cursor.getHeight());
		// Depth for AND mask
		writeWordToFIFO(1);
		// Depth for XOR mask
		writeWordToFIFO(getBitsPerPixel());
		// Scanlines 
		for (int i = 0; i < size; i++) {
			writeWordToFIFO(argb[i]);
		}
	}
	
	private void defineCursor() {
		// Wait for the FIFO
		syncFIFO();

		// Command
		writeWordToFIFO(SVGA_CMD_DEFINE_CURSOR);
		// Mouse id
		writeWordToFIFO(MOUSE_ID);
		// Hotspot X
		writeWordToFIFO(0);
		// Hotspot Y
		writeWordToFIFO(0);
		// Width
		writeWordToFIFO(2);
		// Height
		writeWordToFIFO(2);
		// Depth for AND mask
		writeWordToFIFO(1);
		// Depth for XOR mask
		writeWordToFIFO(1);
		// Scanlines for AND mask
		for (int i = 0; i < 4; i++) {
			writeWordToFIFO(0);
		}
		// Scanlines for XOR mask
		for (int i = 0; i < 4; i++) {
			writeWordToFIFO(0xFFFFFF);
		}
		syncFIFO();
		setCursor(false, 0, 0);
	}

	private void setCursor(boolean visible, int x, int y) {
		setReg32(SVGA_REG_CURSOR_ID, MOUSE_ID);
		if (visible) {
			if (hasCapability(SVGA_CAP_CURSOR_BYPASS)) {
				//System.out.println("bypass " + x + ", " + y);
				setReg32(SVGA_REG_CURSOR_X, x);
				setReg32(SVGA_REG_CURSOR_Y, y);
			} else {
				//System.out.println("move " + x + ", " + y);
				syncFIFO();
				writeWordToFIFO(SVGA_CMD_MOVE_CURSOR);
				writeWordToFIFO(x);
				writeWordToFIFO(y);
			}
		}
		setReg32(SVGA_REG_CURSOR_ON, visible ? 1 : 0);
	}

	private final boolean hasCapability(int cap) {
		return ((this.capabilities & cap) == cap);
	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?