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

View File

@ -225,8 +225,8 @@ QEMUGDB = $(shell if $(QEMU) -help | grep -q '^-gdb'; \
then echo "-gdb tcp::$(GDBPORT)"; \ then echo "-gdb tcp::$(GDBPORT)"; \
else echo "-s -p $(GDBPORT)"; fi) else echo "-s -p $(GDBPORT)"; fi)
ifndef CPUS ifndef CPUS
CPUS := 2 #CPUS := 2
#CPUS := 1 # For debugging CPUS := 1 # For debugging
endif endif
QEMUOPTS = -drive file=fs.img,index=1,media=disk,format=raw -drive file=xv6.img,index=0,media=disk,format=raw -smp $(CPUS) -m 512 $(QEMUEXTRA) QEMUOPTS = -drive file=fs.img,index=1,media=disk,format=raw -drive file=xv6.img,index=0,media=disk,format=raw -smp $(CPUS) -m 512 $(QEMUEXTRA)

131
console.c
View File

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

2
defs.h
View File

@ -24,7 +24,7 @@ int consoleget(void);
void panic(char*) __attribute__((noreturn)); void panic(char*) __attribute__((noreturn));
// Function to return a new console ptr for a process to use // Function to return a new console ptr for a process to use
struct vconsole* newconsole(int); struct vconsole* newconsole(char*, int);
// Function to swithc to the provided console using the ptr // Function to swithc to the provided console using the ptr
int switchtoconsole(struct vconsole*); int switchtoconsole(struct vconsole*);
// Function to close and reset the current proc console // Function to close and reset the current proc console

21
maze.c
View File

@ -18,7 +18,7 @@ uint rngnumber(void)
return (z1 ^ z2 ^ z3 ^ z4) / 2; return (z1 ^ z2 ^ z3 ^ z4) / 2;
} }
int rngrange(int start, int end) int rngrange(int start, int end, int seed)
{ {
if (end < start) if (end < start)
{ {
@ -28,27 +28,36 @@ int rngrange(int start, int end)
} }
int range = end - start + 1; int range = end - start + 1;
return rngnumber() % (range) + start; return (rngnumber() + seed) % (range) + start;
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
int lines = 1000; int lines = 1000;
int seed = getpid();
for (int i = 1; i < argc; i++) { for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "-l") == 0) { if (strcmp(argv[i], "-l") == 0)
{
lines = atoi(argv[i + 1]); lines = atoi(argv[i + 1]);
} }
else if (strcmp(argv[i], "-s") == 0)
{
seed = atoi(argv[i + 1]);
}
} }
cls(); cls();
printf(1, "Start Maze\n"); printf(1, "Start Maze\n");
for (int y = 0; y < lines; y++) for (int y = 0; y < lines; y++)
{ {
for (int x = 0; x < 79; x++) for (int x = 0; x < 80; x++)
{ {
int rndnum = rngrange(189, 197); int rndnum = rngrange(188, 197, seed);
switch (rndnum) switch (rndnum)
{ {
case 188:
rndnum = 32;
break;
case 189: case 189:
rndnum = 179; rndnum = 179;
break; break;
@ -59,7 +68,7 @@ int main(int argc, char *argv[]) {
printf(1, "%c", rndnum); printf(1, "%c", rndnum);
} }
printf(1, "\n"); //printf(1, "\n");
sleep(10); sleep(10);
} }
printf(1, "Maze Ended\n"); printf(1, "Maze Ended\n");

View File

@ -3,43 +3,21 @@
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
int pid; int pid;
int selopt = getpid() % 10;
int bgcol = 0x4F00; int bgcol = 0x4F00;
char title[20];
int accepttitle = 0;
int currenttitlelen = 0;
for (int i = 1; i < argc; i++) { for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "-bg") == 0) if (strcmp(argv[i], "-bg") == 0)
{ {
int selopt = atoi(argv[i + 1]); accepttitle = 0;
switch(selopt) selopt = atoi(argv[i + 1]);
{ }
case 1: else if (strcmp(argv[i], "-t") == 0)
bgcol = 0x1F00; {
break; accepttitle = 1;
case 2:
bgcol = 0x2F00;
break;
case 3:
bgcol = 0x3F00;
break;
case 4:
bgcol = 0x5F00;
break;
case 5:
bgcol = 0xF000;
break;
case 6:
bgcol = 0x8F00;
break;
case 7:
bgcol = 0x9F00;
break;
case 8:
bgcol = 0xAF00;
break;
case 9:
bgcol = 0xCF00;
break;
}
} }
else if (strcmp(argv[i], "-help") == 0) else if (strcmp(argv[i], "-help") == 0)
{ {
@ -49,6 +27,58 @@ int main(int argc, char *argv[]) {
exit(); exit();
return 0; return 0;
} }
else
{
if (accepttitle)
{
if (currenttitlelen > 0)
{
title[currenttitlelen] = ' ';
currenttitlelen++;
}
for (int x = 0; x <= sizeof(argv[i]); x++)
{
title[currenttitlelen] = argv[i][x];
currenttitlelen++;
if (currenttitlelen >= 20)
{
accepttitle = 0;
break;
}
}
}
}
}
switch(selopt)
{
case 1:
bgcol = 0x1F00;
break;
case 2:
bgcol = 0x2F00;
break;
case 3:
bgcol = 0x3F00;
break;
case 4:
bgcol = 0x5F00;
break;
case 5:
bgcol = 0xF000;
break;
case 6:
bgcol = 0x8F00;
break;
case 7:
bgcol = 0x9F00;
break;
case 8:
bgcol = 0xAF00;
break;
case 9:
bgcol = 0xCF00;
break;
} }
pid = fork(); pid = fork();
@ -56,7 +86,7 @@ int main(int argc, char *argv[]) {
printf(1, "screen: fork failed\n"); printf(1, "screen: fork failed\n");
} }
if (pid == 0) { if (pid == 0) {
if (screen(bgcol) != 0) if (screen(title, bgcol) != 0)
{ {
exec("sh", argv); exec("sh", argv);
printf(1, "screen: exec sh failed\n"); printf(1, "screen: exec sh failed\n");

View File

@ -131,12 +131,19 @@ int sys_screen(void)
int result = 0; int result = 0;
struct vconsole* consoleptr = 0; struct vconsole* consoleptr = 0;
int bgcol; int bgcol;
if (argint(0, &bgcol) < 0) char *title;
if (argint(1, &bgcol) < 0)
{ {
return -1; return -1;
} }
if ((consoleptr = newconsole(bgcol)) != 0) if (argstr(0, &title) < 0)
{
return -1;
}
if ((consoleptr = newconsole(title, bgcol)) != 0)
{ {
curproc->consoleptr = consoleptr; curproc->consoleptr = consoleptr;
switchtoconsole(consoleptr); switchtoconsole(consoleptr);

2
user.h
View File

@ -26,7 +26,7 @@ int uptime(void);
int getch(void); int getch(void);
int greeting(void); int greeting(void);
int shutdown(int restart); int shutdown(int restart);
int screen(int bgcol); int screen(const char*, int);
int cls(void); int cls(void);
// ulib.c // ulib.c