Added ability to set a title for the console

Added seed value to the maze generator
Added a title lock to the console
Added a vconsole lock that locks on any virtual console change
Changed some of the locks used to free up
This commit is contained in:
iDunnoDev
2022-12-18 11:42:58 +00:00
committed by iDunnoDev
parent c65c58a954
commit 662e3795a7
7 changed files with 175 additions and 92 deletions

131
console.c
View File

@ -41,6 +41,7 @@ struct vconsole
int inuse;
uint titlebgcol;
char proctitle[20];
int titlelocked;
char status[30];
};
@ -49,6 +50,7 @@ struct vconsole *currentconsole = 0;
#define C(x) ((x) - '@') // Control-x
struct vconsole* getvalidprocessconsoleptr(void);
void clearconsole(ushort *bufferin);
void loadscreenbuffer(ushort *bufferin);
void savescreenbuffer(ushort *bufferin);
@ -64,6 +66,13 @@ static struct
int locking;
} cons;
// Lock for all consoles (to lock when doing switching/creating?)
static struct
{
struct spinlock lock;
int locking;
} vcons;
// 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
@ -116,10 +125,13 @@ void cprintf(char *fmt, ...)
uint *argp;
char *s;
struct vconsole* inconsoleptr = getvalidprocessconsoleptr();
locking = cons.locking;
if (locking)
{
acquire(&cons.lock);
//acquire(&cons.lock);
acquire(&inconsoleptr->lock);
}
if (fmt == 0)
@ -172,7 +184,8 @@ void cprintf(char *fmt, ...)
if (locking)
{
release(&cons.lock);
//release(&cons.lock);
release(&inconsoleptr->lock);
}
}
@ -286,6 +299,7 @@ void panic(char *s)
struct vconsole* getvalidprocessconsoleptr(void)
{
struct vconsole* inconsoleptr = 0;
// Check if a process has actually been created otherwise use the base console
if (myproc() == 0x0)
{
@ -310,12 +324,14 @@ struct vconsole* getvalidprocessconsoleptr(void)
}
}
}
return inconsoleptr;
}
void setconsoleproctitle(struct vconsole* consoleptr, char* newtitle)
{
if (!consoleptr->titlelocked)
{
acquire(&consoleptr->lock);
//Clear the current buffer
memset(consoleptr->proctitle, 0, sizeof(consoleptr->proctitle));
@ -324,6 +340,7 @@ void setconsoleproctitle(struct vconsole* consoleptr, char* newtitle)
//Redraw the title since its been updated now
drawtitle();
release(&consoleptr->lock);
}
}
#define BACKSPACE 0x100
@ -337,7 +354,7 @@ void drawtitle(void)
struct vconsole* inconsoleptr = getvalidprocessconsoleptr();
if (inconsoleptr->inuse)
{
{
int pos = 0;
char c = ' ';
char menutext[] = "(M)enu";
@ -370,7 +387,7 @@ void drawtitle(void)
c = ' ';
}
crt[pos] = (c & 0xff) | inconsoleptr->titlebgcol;
}
}
}
}
@ -470,10 +487,11 @@ void consputc(int c)
int consoleget(void)
{
//struct vconsole* inconsoleptr = getvalidprocessconsoleptr();
int c;
acquire(&cons.lock);
while ((c = kbdgetc()) <= 0)
{
if (c == 0)
@ -482,14 +500,19 @@ int consoleget(void)
}
}
release(&cons.lock);
release(&cons.lock);
return c;
}
void consoleintr(int (*getc)(void))
{
int c, doprocdump = 0, doconsoleswitch = 0, doconsolehome = 0;
int c;
int doprocdump = 0;
int doconsoleswitch = 0;
int doconsolehome = 0;
//struct vconsole* inconsoleptr = getvalidprocessconsoleptr();
struct kbdbuffer* consolekbdbuffer = &currentconsole->keybuffer;
acquire(&cons.lock);
while ((c = getc()) >= 0)
@ -501,18 +524,18 @@ void consoleintr(int (*getc)(void))
doprocdump = 1;
break;
case C('U'): // Kill line.
while (input->e != input->w &&
input->buf[(input->e - 1) % INPUT_BUF] != '\n')
while (consolekbdbuffer->e != consolekbdbuffer->w &&
consolekbdbuffer->buf[(consolekbdbuffer->e - 1) % INPUT_BUF] != '\n')
{
input->e--;
consolekbdbuffer->e--;
consputc(BACKSPACE);
}
break;
case C('H'):
case '\x7f': // Backspace
if (input->e != input->w)
if (consolekbdbuffer->e != consolekbdbuffer->w)
{
input->e--;
consolekbdbuffer->e--;
consputc(BACKSPACE);
}
break;
@ -523,15 +546,15 @@ void consoleintr(int (*getc)(void))
doconsolehome = 1;
break;
default:
if (c != 0 && input->e - input->r < INPUT_BUF)
if (c != 0 && consolekbdbuffer->e - consolekbdbuffer->r < INPUT_BUF)
{
c = (c == '\r') ? '\n' : c;
input->buf[input->e++ % INPUT_BUF] = c;
consolekbdbuffer->buf[consolekbdbuffer->e++ % INPUT_BUF] = c;
consputc(c);
if (c == '\n' || c == C('D') || input->e == input->r + INPUT_BUF)
if (c == '\n' || c == C('D') || consolekbdbuffer->e == consolekbdbuffer->r + INPUT_BUF)
{
input->w = input->e;
wakeup(&(input->r));
consolekbdbuffer->w = consolekbdbuffer->e;
wakeup(&(consolekbdbuffer->r));
}
}
break;
@ -583,18 +606,19 @@ int consoleread(struct inode *ip, char *dst, int n)
iunlock(ip);
target = n;
acquire(&cons.lock);
//acquire(&cons.lock);
acquire(&inconsoleptr->lock);
while (n > 0)
{
while (consolekbdbuffer->r == consolekbdbuffer->w)
{
if (myproc()->killed)
{
release(&cons.lock);
release(&inconsoleptr->lock);
ilock(ip);
return -1;
}
sleep(&(consolekbdbuffer->r), &cons.lock);
sleep(&(consolekbdbuffer->r), &inconsoleptr->lock);
}
c = consolekbdbuffer->buf[consolekbdbuffer->r++ % INPUT_BUF];
if (c == C('D'))
@ -614,7 +638,8 @@ int consoleread(struct inode *ip, char *dst, int n)
break;
}
}
release(&cons.lock);
//release(&cons.lock);
release(&inconsoleptr->lock);
ilock(ip);
return target - n;
@ -693,24 +718,21 @@ void clearscreen(void)
if (inconsoleptr->inuse)
{
loadscreenbuffer(inconsoleptr->screenbuffer);
drawtitle();
release(&cons.lock);
loadscreenbuffer(inconsoleptr->screenbuffer);
outb(CRTPORT, 14);
outb(CRTPORT + 1, pos >> 8);
outb(CRTPORT, 15);
outb(CRTPORT + 1, pos);
}
else
{
release(&cons.lock);
}
drawtitle();
release(&cons.lock);
}
void consoleinit(void)
{
initlock(&cons.lock, "console");
initlock(&vcons.lock, "vconglobal");
for (int i = 0; i < MAXVCONSOLES; i++)
{
@ -721,7 +743,9 @@ void consoleinit(void)
consoles[i].inuse = 0;
consoles[i].titlebgcol = 0x4F00;
//initlock(&consoles[i].lock, "vconsole" + i);
char lockname[8];
sprintf(lockname, "vconsole%d", i);
initlock(&consoles[i].lock, lockname);
}
// Initialise pointer to point to our console input buffer
@ -740,22 +764,33 @@ void consoleinit(void)
ioapicenable(IRQ_KBD, 0);
}
struct vconsole* newconsole(int bgpreset)
struct vconsole* newconsole(char* title, int bgpreset)
{
struct vconsole* result = 0;
acquire(&cons.lock);
acquire(&vcons.lock);
for (int i = 1; i < MAXVCONSOLES; i++)
{
if (!consoles[i].active)
{
consoles[i].processowner = myproc();
consoles[i].titlebgcol = bgpreset;
if (strlen(title) > 0)
{
safestrcpy(consoles[i].proctitle, title, sizeof(consoles[i].proctitle));
consoles[i].titlelocked = 1;
}
else
{
consoles[i].titlelocked = 0;
}
result = &consoles[i];
break;
}
}
release(&cons.lock);
release(&vcons.lock);
return result;
}
@ -771,23 +806,19 @@ void releaseallconsoles(void)
int switchtoconsole(struct vconsole* consoleptr)
{
int pos;
acquire(&consoleptr->lock);
acquire(&cons.lock);
acquire(&vcons.lock);
releaseallconsoles();
currentconsole = consoleptr;
currentconsole->inuse = 1;
input = &currentconsole->keybuffer;
//input = &currentconsole->keybuffer;
// ioapicenable(IRQ_KBD, 0);
loadscreenbuffer(currentconsole->screenbuffer);
drawtitle();
release(&cons.lock);
if (!currentconsole->active)
{
clearscreen();
drawtitle();
clearscreen();
cprintf("Welcome to Console: %d\n", currentconsole->consoleindex);
currentconsole->active = 1;
}
@ -801,7 +832,8 @@ int switchtoconsole(struct vconsole* consoleptr)
outb(CRTPORT + 1, pos);
}
release(&consoleptr->lock);
drawtitle();
release(&vcons.lock);
return 0;
}
@ -810,7 +842,7 @@ int closeconsole(void)
struct vconsole* consoleptr = myproc()->consoleptr;
//cprintf("Console Owner PID: %d\n", consoleptr->processowner->pid);
acquire(&vcons.lock);
if (myproc() == consoleptr->processowner)
{
clearconsole(consoleptr->screenbuffer);
@ -819,21 +851,26 @@ int closeconsole(void)
if (consoleptr->inuse)
{
consoleptr->inuse = 0;
release(&vcons.lock);
switchtoconsole(&consoles[0]);
return 1;
}
}
release(&vcons.lock);
return 0;
}
int getcurrentconsoleindex(void)
{
acquire(&vcons.lock);
for (int i = 0; i < MAXVCONSOLES; i++)
{
if (consoles[i].inuse)
{
release(&vcons.lock);
return consoles[i].consoleindex;
}
}
release(&vcons.lock);
return -1;
}