📄 screen.java
字号:
/**
* $Id: Screen.java,v 1.10 2004/02/21 18:00:56 vchira_2000 Exp $
*/
package org.jnode.driver.console;
import javax.naming.NameNotFoundException;
import org.jnode.naming.InitialNaming;
import org.jnode.system.BootLog;
import org.jnode.system.MemoryResource;
import org.jnode.system.ResourceManager;
import org.jnode.system.ResourceNotFreeException;
import org.jnode.system.ResourceOwner;
import org.jnode.system.SimpleResourceOwner;
import org.jnode.vm.Address;
import org.jnode.vm.PragmaUninterruptible;
import org.jnode.vm.Unsafe;
/**
* <description>
*
* @author epr
*/
public class Screen {
private MemoryResource textScreen;
//private IOResource textIO;
private int width;
private int height;
private int curX;
private int curY;
private static Screen instance;
private static final int CR = 0x0d;
private int cursorOfs = -1;
private int softcursorOrig = -1;
private boolean cursorVisible = true;
/*
* private static final int VIDEO_PTR_BASE = 0x3d4; private static final int VIDEO_PTR_REG =
* VIDEO_PTR_BASE;
*/
private Screen() throws ResourceNotFreeException {
width = 80;
height = 25;
curX = 0;
curY = 0;
Address ptr = Address.valueOf(0xb8000);
try {
ResourceManager rm = (ResourceManager) InitialNaming.lookup(ResourceManager.NAME);
final ResourceOwner owner = new SimpleResourceOwner("Screen");
textScreen = rm.claimMemoryResource(owner, ptr, width * height * 2, ResourceManager.MEMMODE_NORMAL);
//textIO = rm.claimIOResource(this, VIDEO_PTR_BASE, 2);
} catch (NameNotFoundException ex) {
throw new ResourceNotFreeException("ResourceManager not found", ex);
}
}
public void clearScreen()
{
textScreen.clear(0,(this.getWidth() * this.getHeight()) * 2);
}
public static Screen getInstance() throws PragmaUninterruptible {
if (instance == null) {
try {
instance = new Screen();
if (instance == null) {
Unsafe.debug("oops new does not work");
//Unsafe.die("Screen.getInstance");
}
} catch (ResourceNotFreeException ex) {
BootLog.error("Screen memory not free!");
}
}
return instance;
}
public char get(int x, int y) throws PragmaUninterruptible {
return (char)(textScreen.getChar((y * width + x) * 2) & 0xFF);
}
public char getColor(int x, int y) throws PragmaUninterruptible {
return (char)(textScreen.getChar((y * width + x) * 2) & 0xFF00);
}
public void set(int x, int y, char ch, int bgColor) throws PragmaUninterruptible {
if ((x < 0) || (x >= width)) {
x = 0;
//throw new IllegalArgumentException("x (" + x + ") out of range [0.." + width + ">");
}
if ((y < 0) || (y >= height)) {
y = 0;
throw new IllegalArgumentException("y (" + y + ") out of range [0.." + height + ">");
}
int v = (ch & 0xFF) | ((bgColor << 8) & 0xFF00);
textScreen.setChar((y * width + x) * 2, (char) v);
}
public void save(char[] dest, int destOfs) throws PragmaUninterruptible {
textScreen.getChars(0, dest, destOfs, width * height);
}
public void restore(char[] src, int srcOfs) throws PragmaUninterruptible {
textScreen.setChars(src, srcOfs, 0, width * height);
}
public void setCursor(int x, int y) throws PragmaUninterruptible {
if(cursorVisible)
{
final int ofs = (x + (y * width)) * 2;
if ((ofs != cursorOfs) && (cursorOfs != -1)) {
// Remove previous cursor
textScreen.setChar(cursorOfs, (char) softcursorOrig);
cursorOfs = ofs;
}
// Set new cursor
int i = textScreen.getChar(ofs);
softcursorOrig = i;
i |= 0x5000;
textScreen.setChar(ofs, (char) i);
}
/*
* textIO.outPortByte(VIDEO_PTR_REG, 14); textIO.outPortByte(VIDEO_PTR_VAL, (ofs >> 8) &
* 0xFF); textIO.outPortByte(VIDEO_PTR_REG, 15);
*/
}
public void write(char v, int bgColor) throws PragmaUninterruptible {
if (v == '\n') {
// Goto next line
// Clear till eol
for (int i = curX; i < width; i++) {
set(i, curY, ' ', bgColor);
}
curX = 0;
curY++;
} else if (v == '\b') {
if (curX > 0) {
curX--;
} else if (curY > 0) {
curX = width - 1;
curY--;
}
set(curX, curY, ' ', bgColor);
} else if (v == CR) {
return;
} else {
set(curX, curY, v, bgColor);
curX++;
}
if (curX >= width) {
curX = 0;
curY++;
}
if (curY >= height) {
curY = height - 1;
// Scroll one row
textScreen.copy(width * 2, 0, width * (height - 1) * 2);
// Clear last row
for (int i = curX; i < width; i++) {
set(i, curY, ' ', bgColor);
}
}
setCursor(curX, curY);
}
public void write(char v) throws PragmaUninterruptible {
write(v, 0x07);
}
public void write(char[] v) throws PragmaUninterruptible {
write(v, 0x07);
}
public void write(char[] v, int bgColor) throws PragmaUninterruptible {
if (v != null) {
int max = v.length;
for (int i = 0; i < max; i++) {
write(v[i], bgColor);
}
}
}
public void writeString(String v) throws PragmaUninterruptible {
writeString(v, 0x07);
}
public void writeString(String v, int bgColor) throws PragmaUninterruptible {
if (v != null) {
int max = v.length();
for (int i = 0; i < max; i++) {
write(v.charAt(i), bgColor);
}
}
}
public void writeInt(int v) throws PragmaUninterruptible {
writeInt(v, 0x07);
}
public void writeInt(int v, int bgColor) throws PragmaUninterruptible {
int rem = v % 10;
int q = v / 10;
if (q != 0) {
writeInt(q, bgColor);
}
write((char) (rem + '0'), bgColor);
}
public void writeLong(long v) throws PragmaUninterruptible {
writeLong(v, 0x07);
}
public void writeLong(long v, int bgColor) throws PragmaUninterruptible {
long rem = v % 10;
long q = v / 10;
if (q != 0) {
writeLong(q, bgColor);
}
write((char) (rem + '0'), bgColor);
}
/**
* @return int
* @throws PragmaUninterruptible
*/
public int getCurX() throws PragmaUninterruptible {
return curX;
}
/**
* @return int
* @throws PragmaUninterruptible
*/
public int getCurY() throws PragmaUninterruptible {
return curY;
}
/**
* @return int
* @throws PragmaUninterruptible
*/
public int getHeight() throws PragmaUninterruptible {
return height;
}
/**
* @return int
* @throws PragmaUninterruptible
*/
public int getWidth() throws PragmaUninterruptible {
return width;
}
/**
* @return Returns the cursorVisible.
*/
public boolean isCursorVisible() {
return cursorVisible;
}
/**
* @param cursorVisible The cursorVisible to set.
*/
public void setCursorVisible(boolean cursorVisible) {
this.cursorVisible = cursorVisible;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -