Skip to content
This repository has been archived by the owner on May 12, 2024. It is now read-only.

Commit

Permalink
Added TTY switching using 0-9 on the keyboard
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinAlavik committed Mar 25, 2024
1 parent 655f42a commit be51253
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 76 deletions.
1 change: 0 additions & 1 deletion kernel/entry/kernel.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ return codes)
void terminal_launch()
{
tty_spawn(1, "/usr/share/fonts/Uni3-Terminus20x10.psf");
printf("Paradox 1.4.1-dev (tty1)\n\n");
keyboard.out = true;
}

Expand Down
29 changes: 26 additions & 3 deletions kernel/system/devices/keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <system/pic/pic.h>

#include <nighterm/nighterm.h>
#include <tty/tty.h>

struct Keyboard keyboard;

Expand Down Expand Up @@ -74,7 +75,7 @@ void keyboard_handler(int_frame_t *frame)
}
else
{
dprintf("[\e[0;32mKeybord Handler] Keyboard struct has weird state: %u",
dprintf("[\e[0;31mKeybord Handler\e[0m] Keyboard struct has weird state: %u",
keyboard.state);
}

Expand All @@ -83,7 +84,29 @@ void keyboard_handler(int_frame_t *frame)

if ((keyboard.out) && letterString)
{
printf("%s", letterString);
if (strcmp(letterString, "0") == 0) {
tty_spawn(0, "/usr/share/fonts/Uni3-Terminus20x10.psf");
} else if (strcmp(letterString, "1") == 0) {
tty_spawn(1, "/usr/share/fonts/Uni3-Terminus20x10.psf");
} else if (strcmp(letterString, "2") == 0) {
tty_spawn(2, "/usr/share/fonts/Uni3-Terminus20x10.psf");
} else if (strcmp(letterString, "3") == 0) {
tty_spawn(3, "/usr/share/fonts/Uni3-Terminus20x10.psf");
} else if (strcmp(letterString, "4") == 0) {
tty_spawn(4, "/usr/share/fonts/Uni3-Terminus20x10.psf");
} else if (strcmp(letterString, "5") == 0) {
tty_spawn(5, "/usr/share/fonts/Uni3-Terminus20x10.psf");
} else if (strcmp(letterString, "6") == 0) {
tty_spawn(6, "/usr/share/fonts/Uni3-Terminus20x10.psf");
} else if (strcmp(letterString, "7") == 0) {
tty_spawn(7, "/usr/share/fonts/Uni3-Terminus20x10.psf");
} else if (strcmp(letterString, "8") == 0) {
tty_spawn(8, "/usr/share/fonts/Uni3-Terminus20x10.psf");
} else if (strcmp(letterString, "9") == 0) {
tty_spawn(9, "/usr/share/fonts/Uni3-Terminus20x10.psf");
} else {
printf("%s", letterString);
}
}

i8259_SendEndOfInterrupt(1);
Expand All @@ -95,4 +118,4 @@ void init_keyboard()
keyboard.out = 1;

irq_register(1, keyboard_handler);
}
}
3 changes: 2 additions & 1 deletion kernel/system/devices/keyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define KEYBOARD_NORMAL 0
#define KEYBOARD_SHIFT 1
#define KEYBOARD_CAPS 2
#define KEYBOARD_SPECIAL 3

struct Keyboard
{
Expand All @@ -21,4 +22,4 @@ extern struct Keyboard keyboard;

void init_keyboard();

#endif /* KEYBOARD_H */
#endif /* KEYBOARD_H */
4 changes: 2 additions & 2 deletions kernel/system/devices/layouts.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct KeyState sv_layout[0xFF + 1] = {
[0x1A] = {"å", "Å", "Å"},
[0x1B] = {"¨", "^", "¨"},
[0x1C] = {"\n", "\n", "\n"}, // Enter
[0x1D] = {"", "", ""}, // Left control
[0x1D] = {"", "", ""}, // Control
[0x1E] = {"a", "A", "A"},
[0x1F] = {"s", "S", "S"},
[0x20] = {"d", "D", "D"},
Expand Down Expand Up @@ -127,4 +127,4 @@ struct KeyState us_layout[0xFF + 1] = {
[0x38] = {"", "", ""}, // Left alt
[0x39] = {" ", " ", " "},
[0x3A] = {"", "", ""}, // Caps lock
[0x3B ... 0xFF] = {"", "", ""}};
[0x3B ... 0xFF] = {"", "", ""}};
157 changes: 88 additions & 69 deletions kernel/tty/tty.c
Original file line number Diff line number Diff line change
@@ -1,94 +1,113 @@
#include "tty.h"

tty* ttys[MAX_TTYS];
tty* ttys[MAX_TTYS] = { NULL };

uint8_t cur_tty_id;
tty* cur_tty;
uint8_t cur_tty_id = 0;
tty* cur_tty = NULL;

VFS_t *vfs_b;
struct limine_framebuffer *framebuffer_b;
VFS_t *vfs_b = NULL;
struct limine_framebuffer *framebuffer_b = NULL;

void tty_init(VFS_t *vfs, struct limine_framebuffer *framebuffer) {
vfs_b = vfs;
framebuffer_b = framebuffer;
if (vfs != NULL && framebuffer != NULL) {
vfs_b = vfs;
framebuffer_b = framebuffer;
}
}


void tty_flush() {
nighterm_flush(cur_tty->context, 0, 0, 0);
nighterm_set_cursor_position(cur_tty->context, 0, 0);
if (cur_tty != NULL && cur_tty->context != NULL) {
nighterm_flush(cur_tty->context, 0, 0, 0);
nighterm_set_cursor_position(cur_tty->context, 0, 0);
}
}

int tty_spawn(uint8_t id, char* font_path)
{
if(ttys[id] != NULL)
return 1;

struct nighterm_ctx *context = malloc(sizeof(struct nighterm_ctx));
if (context == NULL)
return 4;

char *font_data;
vfs_op_status status = driver_read(vfs_b, 0x00000000, font_path, &font_data);

if (status != STATUS_OK) {
free(context);
return 2;
}

int s = nighterm_initialize(context,
font_data,
framebuffer_b->address,
framebuffer_b->width,
framebuffer_b->height,
framebuffer_b->pitch,
framebuffer_b->bpp,
malloc, free);

free(font_data);

if(s != NIGHTERM_SUCCESS) {
free(context);
return 3;
}

tty* temp = malloc(sizeof(tty));
if (temp == NULL) {
free(context);
return 4;
}

temp->id = id;
temp->context = context;
ttys[id] = temp;
tty_switch(id);
return 0;
int tty_spawn(uint8_t id, char* font_path) {
if (id >= MAX_TTYS || ttys[id] != NULL) {
if (ttys[id] != NULL) {
tty_switch(id);
}
return 0;
}

tty* temp = malloc(sizeof(tty));
if (temp == NULL) {
return -2;
}

struct nighterm_ctx *context = malloc(sizeof(struct nighterm_ctx));
if (context == NULL) {
free(temp);
return -2;
}

char *font_data;
vfs_op_status status = driver_read(vfs_b, 0x00000000, font_path, &font_data);
if (status != STATUS_OK) {
free(temp);
free(context);
return -3;
}

int s = nighterm_initialize(context,
font_data,
framebuffer_b->address,
framebuffer_b->width,
framebuffer_b->height,
framebuffer_b->pitch,
framebuffer_b->bpp,
malloc, free);

free(font_data);

if (s != NIGHTERM_SUCCESS) {
free(temp);
free(context);
return -4;
}

temp->id = id;
temp->context = context;
ttys[id] = temp;
tty_switch(id);

return 0;
}

int tty_destroy(uint8_t id) {
if(ttys[id] == NULL)
return 1;
if (id >= MAX_TTYS || ttys[id] == NULL) {
return -1;
}

free(ttys[id]->context);
free(ttys[id]);
ttys[id] = NULL;
free(ttys[id]->context);
free(ttys[id]);
ttys[id] = NULL;

if (cur_tty_id == id) {
cur_tty_id = 0;
cur_tty = NULL;
}
if (cur_tty_id == id) {
cur_tty_id = 0;
cur_tty = NULL;
}

return 0;
return 0;
}

int tty_get_cur() {
return cur_tty_id;
}

void tty_switch(uint8_t id) {
cur_tty = ttys[id];
cur_tty_id = id;
tty_flush();
if (id >= MAX_TTYS || ttys[id] == NULL) {
return;
}

cur_tty = ttys[id];
cur_tty_id = id;
tty_flush();
printf("Paradox 1.4.1-dev (tty%d)\n\n", cur_tty_id);
}

void tty_write(char ch) {
struct nighterm_ctx *context = cur_tty->context;
nighterm_write(context, ch);
if (cur_tty != NULL && cur_tty->context != NULL) {
nighterm_write(cur_tty->context, ch);
}
}
1 change: 1 addition & 0 deletions kernel/tty/tty.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ extern tty* ttys[MAX_TTYS];
void tty_init(VFS_t *vfs, struct limine_framebuffer *framebuffer);
int tty_spawn(uint8_t id, char* font_path);
int tty_destroy(uint8_t id);
int tty_get_cur();

void tty_switch(uint8_t id);
void tty_write(char ch);
Expand Down

0 comments on commit be51253

Please sign in to comment.