Skip to content

Commit

Permalink
Added cursor moving, scrolling buffer, scrolling and more scrolling
Browse files Browse the repository at this point in the history
  • Loading branch information
Namonay committed Jul 20, 2024
1 parent e55dfbc commit d8f30b4
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 65 deletions.
2 changes: 1 addition & 1 deletion build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub fn build(b: *std.Build) void
.os_tag = .freestanding,
}),
.optimize = .Debug,
.strip = true,
// .strip = true,
.code_model = .kernel,
.pic = false,
.error_tracing = false,
Expand Down
43 changes: 30 additions & 13 deletions sources/drivers/keyboard/keyboard.zig
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
const kernel = @import("kernel");
const vga = @import("../vga/vga.zig");
const libk = @import("libk");

var caps_on: bool = false;
var caps_lock: bool = false;
var num_lock: bool = false;

pub var keyboard_toggle : bool = true;

const UNKNOWN: u32 = 0xFFFFFFFF;
const ESC: u32 = 0xFFFFFFFF - 1;
const CTRL: u32 = 0xFFFFFFFF - 2;
Expand Down Expand Up @@ -70,29 +73,43 @@ pub fn keyboardHandler(regs: *kernel.arch.idt.IDTRegister) void
const scan_code = kernel.arch.ports.in(u8, 0x60) & 0x7F;
const press = kernel.arch.ports.in(u8, 0x60) & 0x80;

switch(scan_code)
switch(scan_code)
{
1, 29, 56, 59...69, 87, 88 => // control keys
1, 29, 56, 59...69,72, 73, 75, 77, 80, 81, 87, 88 => // control keys
{
if(scan_code == 69)
if (scan_code == 69)
{
if(!caps_lock and press == 0)
if (!caps_lock and press == 0)
caps_lock = true
else if(caps_lock and press == 0)
else if (caps_lock and press == 0)
caps_lock = false;
return;
}
if(press != 0)
if (press != 0)
return;
if(scan_code >= 59 and scan_code <= 66)
if (scan_code == 72 or scan_code == 75 or scan_code == 77 or scan_code == 80)
vga.moveCursor(scan_code);
if (scan_code == 73)
vga.reverseScroll();
if (scan_code == 81)
vga.scroll();
if (scan_code >= 59 and scan_code <= 66)
vga.changeScreen(scan_code - 59);
return;
},
else => {}

}
if (keyboard_toggle == false)
return;

switch(scan_code)
{
14 =>
{
if(press != 0)
if (press != 0)
return;
vga.putChar(14);
vga.backspace();
return;
},
42 =>
Expand All @@ -102,17 +119,17 @@ pub fn keyboardHandler(regs: *kernel.arch.idt.IDTRegister) void
},
58 =>
{
if(!caps_lock and press == 0)
if (!caps_lock and press == 0)
caps_lock = true
else if(caps_lock and press == 0)
else if (caps_lock and press == 0)
caps_lock = false;
return;
},
else =>
{
if(press != 0)
if (press != 0)
return;
if(caps_on or caps_lock)
if (caps_on or caps_lock)
vga.putChar(@truncate(uppercase[scan_code]))
else
vga.putChar(@truncate(lowercase[scan_code]));
Expand Down
148 changes: 97 additions & 51 deletions sources/drivers/vga/vga.zig
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,21 @@ pub const Color = enum(u8)

const Screen = struct
{
row: usize = 0,
column: usize = 1,
x: usize = 0,
y: usize = 1,
curr_bg : Color = Color.BLACK,
curr_fg : Color = Color.WHITE,
color: u8 = computeColor(Color.WHITE, Color.BLACK),
buffer : [2000]u16 = [_]u16{getVal(' ', computeColor(Color.WHITE, Color.BLACK))} ** 2000,
pointer: u16 = 0,
buffer : [8000]u16 = [_]u16{getVal(' ', computeColor(Color.WHITE, Color.BLACK))} ** 8000,
};

const VGA = struct
{
height: usize = 25,
width: usize = 80,
row: usize,
column: usize,
x: usize,
y: usize,
color: u8,
nav_color : u8,
nav_triggered_color : u8,
Expand All @@ -58,8 +59,8 @@ fn getVal(uc: u16, color: u16) u16

var vga = VGA
{
.row = 0,
.column = 1,
.x = 0,
.y = 1,
.color = computeColor(Color.WHITE, Color.BLACK),
.nav_triggered_color = 0,
.nav_color = 0,
Expand All @@ -69,7 +70,7 @@ var vga = VGA

fn updateCursor() void
{
const pos : u32 = vga.column * vga.width + @as(u16, @truncate(vga.row));
const pos : u32 = vga.y * vga.width + @as(u16, @truncate(vga.x));

kernel.arch.ports.out(u8, 0x3D4, 0x0F);
kernel.arch.ports.out(u8, 0x3D5, @as(u8, @truncate(pos)) & 0xFF);
Expand All @@ -81,20 +82,20 @@ pub fn changeScreen(targetScreen: u8) void
{
if(targetScreen == vga.currentScreen or targetScreen < 0 or targetScreen >= 8)
return;
for (vga.buffer, 0..2000) |val, i|
vga.screensArray[vga.currentScreen].buffer[i] = val;
for (vga.width..2000) |i|
vga.screensArray[vga.currentScreen].buffer[vga.screensArray[vga.currentScreen].pointer * vga.width + i] = vga.buffer[i];

vga.screensArray[vga.currentScreen].row = vga.row;
vga.screensArray[vga.currentScreen].column = vga.column;
vga.screensArray[vga.currentScreen].x = vga.x;
vga.screensArray[vga.currentScreen].y = vga.y;
vga.screensArray[vga.currentScreen].color = vga.color;
vga.screensArray[vga.currentScreen].curr_bg = vga.curr_bg;
vga.screensArray[vga.currentScreen].curr_fg = vga.curr_fg;

for (80..2000) |i|
vga.buffer[i] = vga.screensArray[targetScreen].buffer[i];
for (vga.width..2000) |i|
vga.buffer[i] = vga.screensArray[targetScreen].buffer[vga.screensArray[targetScreen].pointer * vga.width + i];

vga.row = vga.screensArray[targetScreen].row;
vga.column = vga.screensArray[targetScreen].column;
vga.x = vga.screensArray[targetScreen].x;
vga.y = vga.screensArray[targetScreen].y;
vga.color = vga.screensArray[targetScreen].color;
vga.curr_bg = vga.screensArray[targetScreen].curr_bg;
vga.curr_fg = vga.screensArray[targetScreen].curr_fg;
Expand All @@ -106,7 +107,7 @@ pub fn changeScreen(targetScreen: u8) void
fn updateNavbar() void
{
vga.color = vga.nav_color;
const values = [_]u8{'1','2','3','4','5','6','S','L'};
const values = [_]u8{'1','2','3','4','5','6','7','L'};
var i : u32 = 63;
for(values) |c|
{
Expand All @@ -126,6 +127,23 @@ fn updateNavbar() void
vga.color = computeColor(vga.curr_fg, vga.curr_bg);
}

pub fn moveCursor(dir : u8) void
{
if (dir == 75 and vga.x > 0)
vga.x -= 1;
if (dir == 72 and vga.y > 1)
vga.y -= 1;
if (dir == 77 and vga.x < vga.width - 1)
vga.x += 1;
if (dir == 80 and vga.y < vga.height - 1)
vga.y += 1;
if (dir == 72 and vga.y == 1)
reverseScroll();
if (dir == 80 and vga.y == vga.height - 1)
scroll();
updateCursor();
}

pub fn init(title : []const u8, title_color : u8, navbar_color : u8, triggered_color : u8) void
{
kernel.logs.klogln("[VGA Driver] loading...");
Expand All @@ -149,7 +167,9 @@ pub fn init(title : []const u8, title_color : u8, navbar_color : u8, triggered_c
vga.nav_color = navbar_color;
vga.nav_triggered_color = triggered_color;
vga.color = computeColor(vga.curr_fg, vga.curr_bg);
vga.column = 1;
vga.y = 1;
for (80..1999) |i|
vga.buffer[i] = getVal(' ', computeColor(Color.WHITE, Color.BLACK));
updateCursor();
updateNavbar();
kernel.logs.klogln("[VGA Driver] loaded");
Expand All @@ -162,59 +182,85 @@ fn putEntry(c: u8, color: u8, x: usize, y: usize) void

pub fn reverseScroll() void
{
for(0..(vga.height - 2)) |x|
{
for(0..vga.width) |y|
vga.buffer[x * vga.width + y] = vga.buffer[(x + 1) * vga.width + y];
}
for(0..vga.width) |y|
vga.buffer[y] = getVal(' ', vga.color);
var x : usize = vga.height - 2;
if (vga.screensArray[vga.currentScreen].pointer == 0)
return;
for (0..vga.width) |y|
vga.screensArray[vga.currentScreen].buffer[(vga.screensArray[vga.currentScreen].pointer + vga.height - 1) * vga.width + y] = vga.buffer[(vga.height - 1) * vga.width + y];
while (x > 0)
{
for(0..(vga.width - 1)) |y|
vga.buffer[(x + 1) * vga.width + y] = vga.buffer[x * vga.width + y];
x -= 1;
}
for (0..vga.width) |y|
vga.buffer[vga.width + y] = vga.screensArray[vga.currentScreen].buffer[(vga.screensArray[vga.currentScreen].pointer - 1) * vga.width + y];
vga.screensArray[vga.currentScreen].pointer -= 1;
if (vga.y < vga.height)
vga.y += 1;
updateCursor();
}

pub fn scroll() void
{
if (vga.screensArray[vga.currentScreen].pointer == 75)
return;
for (0..vga.width) |y|
vga.screensArray[vga.currentScreen].buffer[vga.screensArray[vga.currentScreen].pointer * vga.width + y] = vga.buffer[vga.width + y];
for(2..vga.height) |x|
{
for (0..vga.width) |y|
vga.buffer[(x - 1) * vga.width + y] = vga.buffer[x * vga.width + y];
}
for(0..vga.width) |y|
vga.buffer[(vga.height - 1) * vga.width + y] = getVal(' ', vga.color);
vga.column -= 1;
vga.buffer[(vga.height - 1) * vga.width + y] = vga.screensArray[vga.currentScreen].buffer[(vga.screensArray[vga.currentScreen].pointer + vga.height) * vga.width + y];
if (vga.y > 1)
vga.y -= 1
else
vga.x = 0;
vga.screensArray[vga.currentScreen].pointer += 1;
updateCursor();
}

pub fn backspace() void
{
if(vga.x == 0 and vga.y <= 1)
return;
if(vga.x == 0 and vga.y != 0)
{
vga.x = vga.width - 1;
vga.y -= 1;
if (vga.buffer[vga.y * vga.width + vga.x] != getVal(' ', vga.color))
{
putCharAt(' ', vga.x, vga.y);
updateCursor();
return;
}
while (vga.buffer[vga.y * vga.width + vga.x - 1] == getVal(' ', vga.color) and vga.x > 0)
vga.x -= 1;
updateCursor();
return ;
}
vga.x -= 1;
putCharAt(' ', vga.x, vga.y);
updateCursor();
return;
}

pub fn putChar(c: u8) void
{
if(c == 0)
return;
if(c >= ' ' and c <= 126)
putEntry(c, vga.color, vga.row, vga.column);
if(c == 14)
{
if(vga.row == 0 and vga.column <= 1)
return;
if(vga.row == 0 and vga.column != 0)
{
vga.row = vga.width;
vga.column -= 1;
putCharAt(' ', vga.row, vga.column);
return ;
}
vga.row -= 1;
putCharAt(' ', vga.row, vga.column);
updateCursor();
return;
}
putEntry(c, vga.color, vga.x, vga.y);
if(c > 126)
return;
vga.row += 1;
if(vga.row == vga.width or c == '\n')
vga.x += 1;
if(vga.x == vga.width or c == '\n')
{
vga.row = 0;
vga.column += 1;
if(vga.column == vga.height)
vga.x = 0;
vga.y += 1;
if(vga.y == vga.height)
scroll();
}
updateCursor();
Expand Down Expand Up @@ -254,7 +300,7 @@ pub fn clear(color: Color) void
for(0..vga.width) |j|
vga.buffer[i * vga.width + j] = getVal(' ', computeColor(Color.WHITE, color));
}
vga.column = 0;
vga.row = 0;
vga.y = 0;
vga.x = 0;
updateCursor();
}
1 change: 1 addition & 0 deletions sources/kernel/panic.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const logs = @import("log.zig");
pub fn kpanic(message: []const u8) noreturn
{
@setCold(true);
arch.disableInts();
vga.setColor(vga.Color.WHITE, vga.Color.RED);
vga.clear(vga.Color.RED);
vga.putString(logs.getLogBuffer());
Expand Down

0 comments on commit d8f30b4

Please sign in to comment.