Added the drawtitle function to console

Added ability to pick a bg preset for the title bar
Added counting program
Added function to set the current consoles title name
Removed some of the debug code
This commit is contained in:
iDunnoDev
2022-12-16 15:23:38 +00:00
committed by iDunnoDev
parent 924d90c066
commit c65c58a954
11 changed files with 284 additions and 52 deletions

219
console.c
View File

@ -39,11 +39,13 @@ struct vconsole
int pos;
int active;
int inuse;
uint titlebgcol;
char proctitle[20];
char status[30];
};
struct vconsole consoles[MAXVCONSOLES];
struct vconsole *currentconsole = 0;
//static uint currentconsoleindex = 0;
#define C(x) ((x) - '@') // Control-x
@ -62,10 +64,13 @@ static struct
int locking;
} cons;
static void printint(int xx, int base, int sign)
// Split the printint so we can just get the char out in draw title
// itoa is the function in the c standard library (?) so i reused the name
// since it does something similar
// Saves to the provided char array and returns the number of characters for looping
static int itoa(int xx, int base, int sign, char* buf)
{
static char digits[] = "0123456789abcdef";
char buf[16];
static char digits[] = "0123456789abcdef";
int i;
uint x;
@ -89,6 +94,15 @@ static void printint(int xx, int base, int sign)
buf[i++] = '-';
}
return i;
}
static void printint(int xx, int base, int sign)
{
int i = 0;
char buf[16];
i = itoa(xx, base, sign, buf);
while (--i >= 0)
{
consputc(buf[i]);
@ -162,6 +176,88 @@ void cprintf(char *fmt, ...)
}
}
void sprintf(char* strbuffer, char *fmt, ...)
{
int i, c, si;
uint *argp;
char *s;
char buf[16];
int x;
if (fmt == 0)
{
panic("null fmt");
}
si = 0;
argp = (uint *)(void *)(&fmt + 1);
for (i = 0; (c = fmt[i] & 0xff) != 0; i++)
{
if (c != '%')
{
strbuffer[si] = c;
si++;
continue;
}
c = fmt[++i] & 0xff;
if (c == 0)
{
break;
}
switch (c)
{
case 'd':
// Clear any existing buf data
memset(buf, 0, sizeof buf);
x = itoa(*argp++, 10, 1, buf);
while (--x >= 0)
{
strbuffer[si] = buf[x];
si++;
}
break;
case 'x':
case 'p':
// Clear any existing buf data
memset(buf, 0, sizeof buf);
x = itoa(*argp++, 16, 0, buf);
while (--x >= 0)
{
strbuffer[si] = buf[x];
si++;
}
break;
case 's':
if ((s = (char *)*argp++) == 0)
{
s = "(null)";
}
for (; *s; s++)
{
strbuffer[si] = *s;
si++;
}
break;
case '%':
strbuffer[i] = '%';
si++;
break;
default:
// Print unknown % sequence to draw attention.
strbuffer[si] = '%';
si++;
strbuffer[si] = c;
si++;
break;
}
}
// Add string terminator to the end
strbuffer[si] = '\0';
}
void panic(char *s)
{
int i;
@ -218,12 +314,66 @@ struct vconsole* getvalidprocessconsoleptr(void)
return inconsoleptr;
}
void setconsoleproctitle(struct vconsole* consoleptr, char* newtitle)
{
acquire(&consoleptr->lock);
//Clear the current buffer
memset(consoleptr->proctitle, 0, sizeof(consoleptr->proctitle));
//Copy the new title into the console title buffer
safestrcpy(consoleptr->proctitle, newtitle, sizeof(consoleptr->proctitle));
//Redraw the title since its been updated now
drawtitle();
release(&consoleptr->lock);
}
#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
void drawtitle(void)
{
struct vconsole* inconsoleptr = getvalidprocessconsoleptr();
if (inconsoleptr->inuse)
{
int pos = 0;
char c = ' ';
char menutext[] = "(M)enu";
char consoletitletext[10]; // + (inconsoleptr->consoleindex + 1);
sprintf(consoletitletext, "Console %d", inconsoleptr->consoleindex + 1);
for (pos = 0; pos < SCRWIDTH; pos++)
{
int offset = 0;
if (pos < 6) {
c = menutext[pos - offset];
}
else if (pos >= 10 && pos < 20)
{
offset = 10;
c = consoletitletext[pos - offset];
}
else if (pos >= 25 && pos < 45)
{
offset = 25;
c = inconsoleptr->proctitle[pos - offset];
}
else if (pos >= 50 && pos < 80)
{
offset = 50;
c = inconsoleptr->status[pos - offset];
}
else
{
c = ' ';
}
crt[pos] = (c & 0xff) | inconsoleptr->titlebgcol;
}
}
}
static void cgaputc(int c)
{
int pos;
@ -397,7 +547,7 @@ void consoleintr(int (*getc)(void))
{
if (currentconsole->consoleindex != 0)
{
switchtoconsole(0);
switchtoconsole(getbaseconsoleptr());
}
}
if (doconsoleswitch)
@ -473,15 +623,17 @@ int consoleread(struct inode *ip, char *dst, int n)
int consolewrite(struct inode *ip, char *buf, int n)
{
int i;
//struct vconsole* inconsoleptr = getvalidprocessconsoleptr();
struct vconsole* inconsoleptr = getvalidprocessconsoleptr();
iunlock(ip);
acquire(&cons.lock);
//acquire(&cons.lock);
acquire(&inconsoleptr->lock);
for (i = 0; i < n; i++)
{
consputc(buf[i] & 0xff);
}
release(&cons.lock);
//release(&cons.lock);
release(&inconsoleptr->lock);
ilock(ip);
return n;
@ -514,7 +666,7 @@ struct vconsole* getbaseconsoleptr(void)
void clearconsole(ushort *bufferin)
{
// Flood the screen buffer with blank spaces
memset(bufferin, 0, sizeof(bufferin[0]) * SCRHEIGHT * SCRWIDTH);
memset(bufferin, 0, sizeof(bufferin[0]) * SCRHEIGHT * SCRWIDTH);
}
void loadscreenbuffer(ushort *bufferin)
@ -530,26 +682,19 @@ void savescreenbuffer(ushort *bufferin)
}
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;
{
// Check if a process has actually been created otherwise use the base console
struct vconsole* inconsoleptr = getvalidprocessconsoleptr();
acquire(&cons.lock);
int pos = TITLEOFF;
clearconsole(inconsoleptr->screenbuffer);
clearconsole(inconsoleptr->screenbuffer);
inconsoleptr->pos = pos;
release(&cons.lock);
if (inconsoleptr->inuse)
{
acquire(&cons.lock);
loadscreenbuffer(inconsoleptr->screenbuffer);
{
loadscreenbuffer(inconsoleptr->screenbuffer);
drawtitle();
release(&cons.lock);
outb(CRTPORT, 14);
@ -557,6 +702,10 @@ void clearscreen(void)
outb(CRTPORT, 15);
outb(CRTPORT + 1, pos);
}
else
{
release(&cons.lock);
}
}
void consoleinit(void)
@ -570,7 +719,8 @@ void consoleinit(void)
consoles[i].pos = 0;
consoles[i].active = 0;
consoles[i].inuse = 0;
consoles[i].titlebgcol = 0x4F00;
//initlock(&consoles[i].lock, "vconsole" + i);
}
@ -590,7 +740,7 @@ void consoleinit(void)
ioapicenable(IRQ_KBD, 0);
}
struct vconsole* newconsole(void)
struct vconsole* newconsole(int bgpreset)
{
struct vconsole* result = 0;
@ -600,6 +750,7 @@ struct vconsole* newconsole(void)
if (!consoles[i].active)
{
consoles[i].processowner = myproc();
consoles[i].titlebgcol = bgpreset;
result = &consoles[i];
break;
}
@ -621,17 +772,7 @@ int switchtoconsole(struct vconsole* consoleptr)
{
int pos;
/*outb(CRTPORT, 14);
pos = inb(CRTPORT + 1) << 8;
outb(CRTPORT, 15);
pos |= inb(CRTPORT + 1);*/
// consoles[currentconsoleindex].pos = pos;
// savescreenbuffer(consoles[currentconsoleindex].screenbuffer);
// acquire(&currentconsole->lock);
// sleep(&(currentconsole), &currentconsole->lock);
acquire(&consoleptr->lock);
acquire(&cons.lock);
releaseallconsoles();
currentconsole = consoleptr;
@ -640,16 +781,18 @@ int switchtoconsole(struct vconsole* consoleptr)
// ioapicenable(IRQ_KBD, 0);
loadscreenbuffer(currentconsole->screenbuffer);
drawtitle();
release(&cons.lock);
if (!currentconsole->active)
{
clearscreen();
drawtitle();
cprintf("Welcome to Console: %d\n", currentconsole->consoleindex);
currentconsole->active = 1;
}
else
{
{
pos = currentconsole->pos;
outb(CRTPORT, 14);
@ -658,7 +801,7 @@ int switchtoconsole(struct vconsole* consoleptr)
outb(CRTPORT + 1, pos);
}
// wakeup(myproc()->chan);
release(&consoleptr->lock);
return 0;
}
@ -666,7 +809,7 @@ int closeconsole(void)
{
struct vconsole* consoleptr = myproc()->consoleptr;
cprintf("Console Owner PID: %d\n", consoleptr->processowner->pid);
//cprintf("Console Owner PID: %d\n", consoleptr->processowner->pid);
if (myproc() == consoleptr->processowner)
{