Added buffer to the console

Added hotkeys to switch between console buffers and to return to the home one
Added clear screen command
Added debug text outputs to the hello command and when loading sh
This commit is contained in:
iDunnoDev
2022-12-09 10:39:55 +00:00
committed by iDunnoDev
parent 0ee33aaa97
commit 9b23ca2a45
194 changed files with 283 additions and 49519 deletions

223
console.c
View File

@ -28,9 +28,21 @@ struct kbdbuffer inputBuffer;
struct kbdbuffer * input = 0;
struct vconsole {
ushort screenbuffer[SCRWIDTH * SCRHEIGHT];
struct kbdbuffer keybuffer;
int pos;
int active;
};
struct vconsole consoles[MAXVCONSOLES];
static uint currentconsoleindex = 0;
#define C(x) ((x) - '@') // Control-x
void clearconsole(ushort *bufferin);
void loadscreenbuffer(ushort *bufferin);
void clearscreen(void);
static void consputc(int);
@ -148,44 +160,67 @@ void panic(char *s) {
#define BACKSPACE 0x100
#define CRTPORT 0x3d4
#define TITLEOFF (SCRWIDTH * 1) // Size of the offset we need for the title bar
static ushort *crt = (ushort*)P2V(0xb8000); // CGA memory
static void cgaputc(int c) {
int pos;
int consoleindex = 0;
// Check if a process has actually been created otherwise use the base console
if (myproc() != 0x0)
{
consoleindex = myproc()->consoleIndex;
}
ushort* currentbuffer = consoles[consoleindex].screenbuffer;
// Cursor position: col + 80*row.
outb(CRTPORT, 14);
pos = inb(CRTPORT + 1) << 8;
outb(CRTPORT, 15);
pos |= inb(CRTPORT + 1);
if (consoleindex == currentconsoleindex)
{
// Cursor position: col + 80*row.
outb(CRTPORT, 14);
pos = inb(CRTPORT + 1) << 8;
outb(CRTPORT, 15);
pos |= inb(CRTPORT + 1);
}
else
{
pos = consoles[consoleindex].pos;
}
if (c == '\n') {
pos += 80 - pos % 80;
pos += SCRWIDTH - pos % SCRWIDTH;
}
else if (c == BACKSPACE) {
if (pos > 0) {
if (pos > (TITLEOFF)) {
currentbuffer[pos] = 0; // Clear the character from the buffer
--pos;
}
}
else {
crt[pos++] = (c & 0xff) | 0x0700; // black on white
currentbuffer[pos++] = (c & 0xff) | 0x0700; // black on white
}
if (pos < 0 || pos > 25 * 80) {
if (pos < TITLEOFF || pos > SCRHEIGHT * SCRWIDTH) {
panic("pos under/overflow");
}
if ((pos / 80) >= 24) { // Scroll up.
memmove(crt, crt + 80, sizeof(crt[0]) * 23 * 80);
memmove(currentbuffer + TITLEOFF, currentbuffer + (SCRWIDTH + TITLEOFF), sizeof(crt[0]) * (SCRHEIGHT - 1) * SCRWIDTH);
pos -= 80;
memset(crt + pos, 0, sizeof(crt[0]) * (24 * 80 - pos));
memset(currentbuffer + pos, 0, sizeof(crt[0]) * (SCRHEIGHT * SCRWIDTH - pos));
}
consoles[consoleindex].pos = pos;
outb(CRTPORT, 14);
outb(CRTPORT + 1, pos >> 8);
outb(CRTPORT, 15);
outb(CRTPORT + 1, pos);
crt[pos] = ' ' | 0x0700;
if (consoleindex == currentconsoleindex)
{
loadscreenbuffer(currentbuffer);
outb(CRTPORT, 14);
outb(CRTPORT + 1, pos >> 8);
outb(CRTPORT, 15);
outb(CRTPORT + 1, pos);
crt[pos] = ' ' | 0x0700;
}
}
void consputc(int c) {
@ -224,7 +259,7 @@ int consoleget(void) {
}
void consoleintr(int (*getc)(void)) {
int c, doprocdump = 0;
int c, doprocdump = 0, doconsoleswitch = 0, doconsolehome = 0;
acquire(&cons.lock);
while ((c = getc()) >= 0) {
@ -247,6 +282,12 @@ void consoleintr(int (*getc)(void)) {
consputc(BACKSPACE);
}
break;
case C('T'):
doconsoleswitch = 1;
break;
case C('K'):
doconsolehome = 1;
break;
default:
if (c != 0 && input->e - input->r < INPUT_BUF) {
c = (c == '\r') ? '\n' : c;
@ -264,6 +305,28 @@ void consoleintr(int (*getc)(void)) {
if (doprocdump) {
procdump(); // now call procdump() wo. cons.lock held
}
if (doconsolehome) {
if (currentconsoleindex != 0)
{
switchtoconsole(0);
}
}
if (doconsoleswitch)
{
int toconsole = 0;
for (int i = (currentconsoleindex + 1); i < MAXVCONSOLES; i++)
{
if (consoles[i].active)
{
toconsole = i;
break;
}
}
if (toconsole != currentconsoleindex)
{
switchtoconsole(toconsole);
}
}
}
int consoleread(struct inode *ip, char *dst, int n) {
@ -317,16 +380,132 @@ int consolewrite(struct inode *ip, char *buf, int n) {
return n;
}
void consoleinit(void) {
initlock(&cons.lock, "console");
void testfillbuffer(ushort *bufferin)
{
ushort firstchar = 65;
ushort lastchar = 90;
ushort currentchar = firstchar;
// Initialise pointer to point to our console input buffer
input = &inputBuffer;
for (int i = 0; i < (SCRHEIGHT * SCRWIDTH); i++)
{
if (currentchar > lastchar)
{
currentchar = firstchar;
}
bufferin[i] = (currentchar & 0xff) | 0x0700;
currentchar++;
}
}
void clearconsole(ushort *bufferin)
{
// Flood the screen buffer with blank spaces
memset(bufferin, 0x0700, sizeof(bufferin[0]) * SCRHEIGHT * SCRWIDTH);
}
void loadscreenbuffer(ushort *bufferin)
{
// Copy the memory from the console buffer to the crt buffer
memmove(crt, bufferin, sizeof(bufferin[0]) * SCRHEIGHT * SCRWIDTH);
}
void clearscreen(void)
{
//cprintf("process id: %d", myproc()->consoleIndex);
//return;
//cprintf("size of buffer item: %d\n", sizeof(consoles[currentconsoleindex].screenbuffer[0]) * (SCRHEIGHT * SCRWIDTH));
//cprintf("size of crt item: %d\n", sizeof(crt[0]) * (SCRHEIGHT * SCRWIDTH));
//return;
int pos = TITLEOFF;
//testfillbuffer(consoles[currentconsoleindex].screenbuffer);
clearconsole(consoles[currentconsoleindex].screenbuffer);
loadscreenbuffer(consoles[currentconsoleindex].screenbuffer);
consoles[currentconsoleindex].pos = pos;
outb(CRTPORT, 14);
outb(CRTPORT + 1, pos >> 8);
outb(CRTPORT, 15);
outb(CRTPORT + 1, pos);
}
void consoleinit(void) {
initlock(&cons.lock, "console");
consoles[currentconsoleindex].active = 1;
// Initialise pointer to point to our console input buffer
input = &consoles[currentconsoleindex].keybuffer;
devsw[CONSOLE].write = consolewrite;
devsw[CONSOLE].read = consoleread;
cons.locking = 1;
clearscreen();
cprintf("Welcome! you are currently in the base console\n");
ioapicenable(IRQ_KBD, 0);
}
int newconsole(void)
{
int result = 0;
for (int i = 1; i < MAXVCONSOLES; i++)
{
if (!consoles[i].active)
{
result = i;
break;
}
}
return result;
}
int switchtoconsole(int consoleindex)
{
acquire(&cons.lock);
currentconsoleindex = consoleindex;
input = &consoles[currentconsoleindex].keybuffer;
//ioapicenable(IRQ_KBD, 0);
release(&cons.lock);
loadscreenbuffer(consoles[currentconsoleindex].screenbuffer);
if (!consoles[currentconsoleindex].active)
{
clearscreen();
cprintf("Welcome to Console: %d\n", currentconsoleindex);
consoles[currentconsoleindex].active = 1;
}
else
{
int pos = consoles[currentconsoleindex].pos;
outb(CRTPORT, 14);
outb(CRTPORT + 1, pos >> 8);
outb(CRTPORT, 15);
outb(CRTPORT + 1, pos);
}
return 0;
}
int closeconsole(int consoleindex)
{
clearconsole(consoles[currentconsoleindex].screenbuffer);
consoles[currentconsoleindex].active = 0;
switchtoconsole(0);
return 0;
}
int getcurrentconsoleindex(void)
{
return currentconsoleindex;
}