From eea23d967bc275b00312d02c902c90cb7dbd95f3 Mon Sep 17 00:00:00 2001
From: Able <abl3theabove@gmail.com>
Date: Sun, 10 Nov 2024 02:36:37 -0600
Subject: [PATCH] mouse cursor functional

---
 .cargo/config.toml                            |  2 +-
 Cargo.lock                                    |  6 +-
 repbuild/src/main.rs                          |  1 +
 sysdata/libraries/intouch/src/lib.hb          |  2 +-
 sysdata/programs/horizon/src/main.hb          | 62 +++++++++++++------
 sysdata/programs/ps2_mouse_driver/src/main.hb | 21 ++++---
 sysdata/system_config.toml                    |  8 +--
 7 files changed, 64 insertions(+), 38 deletions(-)

diff --git a/.cargo/config.toml b/.cargo/config.toml
index b335393..c5b12ff 100644
--- a/.cargo/config.toml
+++ b/.cargo/config.toml
@@ -1,3 +1,3 @@
 [alias]
-repbuild = "run --manifest-path ./repbuild/Cargo.toml -r --"
+repbuild = "run --manifest-path ./repbuild/Cargo.toml -r -- "
 dev = "run --manifest-path ./dev/Cargo.toml -r --"
diff --git a/Cargo.lock b/Cargo.lock
index f408d39..f776aab 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -228,12 +228,12 @@ dependencies = [
 [[package]]
 name = "hbbytecode"
 version = "0.1.0"
-source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#65e9f272a85cc9d46c31072af9d0f2bc43ef1217"
+source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#42a713aeaef11ca86d96083915191fbe456c47e5"
 
 [[package]]
 name = "hblang"
 version = "0.1.0"
-source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#65e9f272a85cc9d46c31072af9d0f2bc43ef1217"
+source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#42a713aeaef11ca86d96083915191fbe456c47e5"
 dependencies = [
  "hashbrown 0.15.1",
  "hbbytecode",
@@ -245,7 +245,7 @@ dependencies = [
 [[package]]
 name = "hbvm"
 version = "0.1.0"
-source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#65e9f272a85cc9d46c31072af9d0f2bc43ef1217"
+source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#42a713aeaef11ca86d96083915191fbe456c47e5"
 dependencies = [
  "hbbytecode",
 ]
diff --git a/repbuild/src/main.rs b/repbuild/src/main.rs
index 05522e2..39569a9 100644
--- a/repbuild/src/main.rs
+++ b/repbuild/src/main.rs
@@ -421,6 +421,7 @@ fn run(release: bool, target: Target, do_accel: bool) -> Result<(), Error> {
                 "-bios", &ovmf_path.change_context(Error::OvmfFetch)?,
                 "-drive", "file=target/disk.img,format=raw",
                 "-device", "vmware-svga",
+                // "-serial", "stdio",
                 "-m", "2G",
                 "-smp", "1",
                 "-parallel", "none",
diff --git a/sysdata/libraries/intouch/src/lib.hb b/sysdata/libraries/intouch/src/lib.hb
index a0e9b4d..e6a5dc7 100644
--- a/sysdata/libraries/intouch/src/lib.hb
+++ b/sysdata/libraries/intouch/src/lib.hb
@@ -29,7 +29,7 @@ recieve_mouse_event := fn(): ?MouseEvent {
 	if *mem_page != 0 {
 		log.info("Mouse events\0")
 		dx := @as(i8, @bitcast(*mem_page))
-		dy := @as(i8, @bitcast(*mem_page + 1))
+		dy := @as(i8, @bitcast(*(mem_page + 1)))
 		mevent := MouseEvent.(dx, dy, 0, 0, 0)
 		return mevent
 	}
diff --git a/sysdata/programs/horizon/src/main.hb b/sysdata/programs/horizon/src/main.hb
index 624a659..d1da4b2 100644
--- a/sysdata/programs/horizon/src/main.hb
+++ b/sysdata/programs/horizon/src/main.hb
@@ -40,8 +40,8 @@ main := fn(): int {
 	// really we should null check but it is a bit broked
 	font := @unwrap(render.text.font_from_psf2(@bitcast(&psf), false))
 
-	mouse_x := 0
-	mouse_y := 0
+	mouse_x := @as(i16, 0)
+	mouse_y := @as(i16, 0)
 	text_label := new_label("Hi\0")
 	// widgets := "()\0"
 	// ui := sexpr_parser(widgets)
@@ -52,7 +52,7 @@ main := fn(): int {
 
 		// TODO: Read the window buffer here
 		{
-			ret := buffer.recv([u8; 4096], win_buff, mem_buf)
+			// ret := buffer.recv([u8; 4096], win_buff, mem_buf)
 			// for some reason this null check causes the compiler to spin forever
 			// if ret == null {
 			// 	log.info("No messages\0")
@@ -64,6 +64,22 @@ main := fn(): int {
 		// get input events from drivers via intouch
 		// key_event := intouch.recieve_key_event();
 		// log.info("before mouse event check\0");
+
+		render.put_rect(screen, .(0, 0), .(screen.width - 1, screen.height - 1), render.white)
+		{
+			// Scroll bar :ThumbsUp:
+			render.put_rect(screen, .(100, 100), .(100, 10), render.white)
+			render.put_filled_rect(screen, .(110, 100), .(20, 10), render.white)
+
+			render.put_rect(screen, .(90, 110), .(10, 100), render.white)
+			render.put_filled_rect(screen, .(90, 120), .(10, 20), render.white)
+		}
+
+		{
+			pos := Vec2(uint).(1, screen.height - 21)
+			render_label_to_surface(screen, text_label, font, pos)
+		}
+
 		{
 			mouse_event := intouch.recieve_mouse_event()
 			//
@@ -71,8 +87,30 @@ main := fn(): int {
 			if mouse_event != null {
 				// log.warn("Mouse event recieved\0")
 
-				mouse_x += mouse_event.x_change
-				mouse_y += mouse_event.y_change
+				change_x := @as(i16, mouse_event.x_change)
+				change_x = change_x << 8
+				change_x = change_x >> 8
+
+				mouse_x += change_x
+				if mouse_x < 0 {
+					mouse_x = 0
+				}
+				if mouse_x >= screen.width - 20 {
+					mouse_x = @intcast(screen.width - 21)
+				}
+
+				change_y := @as(i16, mouse_event.y_change)
+				change_y = change_y << 8
+				change_y = change_y >> 8
+
+				if mouse_y < 0 {
+					mouse_y = 0
+				}
+				if mouse_y >= screen.height - 20 {
+					mouse_y = @intcast(screen.height - 21)
+				}
+				mouse_y -= change_y
+
 				set_label_text(text_label, "Mouse Moved\0")
 			}
 			// render mouse
@@ -82,20 +120,6 @@ main := fn(): int {
 
 		// TODO: Get windows out of a collection and iter through
 
-		// render.put_rect(screen, .(0, 0), .(screen.width - 1, screen.height - 1), render.white)
-		// {
-		// 	// Scroll bar :ThumbsUp:
-		// 	render.put_rect(screen, .(100, 100), .(100, 10), render.white)
-		// 	render.put_filled_rect(screen, .(110, 100), .(20, 10), render.white)
-
-		// 	render.put_rect(screen, .(90, 110), .(10, 100), render.white)
-		// 	render.put_filled_rect(screen, .(90, 120), .(10, 20), render.white)
-		// }
-
-		// {
-		// 	pos := Vec2(uint).(1, screen.height - 21)
-		// 	render_label_to_surface(screen, text_label, font, pos)
-		// }
 		// Sync the screen
 		render.sync(screen)
 	}
diff --git a/sysdata/programs/ps2_mouse_driver/src/main.hb b/sysdata/programs/ps2_mouse_driver/src/main.hb
index 5e74c07..d234380 100644
--- a/sysdata/programs/ps2_mouse_driver/src/main.hb
+++ b/sysdata/programs/ps2_mouse_driver/src/main.hb
@@ -111,6 +111,7 @@ main := fn(): int {
 		loop if (memory.inb(0x64) & 0x20) == 0x20 break
 
 		status := memory.inb(0x60)
+
 		if status == 0xAA {
 			loop if memory.inb(0x60) == 0 break
 			log.info("Mouse plugged in!\0")
@@ -132,23 +133,23 @@ main := fn(): int {
 
 		button_states ^= changes
 
-		// log.info(string.display_int(status, format_page, 10))
-
 		dx := i9.(false, 0)
 		dy := i9.(false, 0)
 
 		dx.value = memory.inb(0x60)
 		dx.sign = (status & 0x10) > 0
 
-		dy.value = -memory.inb(0x60)
-		dy.sign = (status & 0x20) == 0
+		dy.value = memory.inb(0x60)
+		dy.sign = (status & 0x20) != 0
 
-		if dy.value != 0 & dx.value != 0 {
-			y_change := @as(i8, @bitcast(dy.value))
-			x_change := @as(i8, @bitcast(dx.value))
-			event := MouseEvent.(x_change, y_change, 0, 0, 0)
-			buffer.write(MouseEvent, &event, mouse_buffer)
-		}
+		y_change := @as(i8, @bitcast(dy.value))
+		x_change := @as(i8, @bitcast(dx.value))
+
+		event := MouseEvent.(0, 0, 0, 0, 0)
+		event.x_change = x_change
+		event.y_change = y_change
+
+		buffer.write(MouseEvent, &event, mouse_buffer)
 	}
 
 	return 0
diff --git a/sysdata/system_config.toml b/sysdata/system_config.toml
index c46e0a3..2e9996c 100644
--- a/sysdata/system_config.toml
+++ b/sysdata/system_config.toml
@@ -22,11 +22,11 @@ resolution = "1024x768x24"
 
 [boot.limine.ableos.modules]
 
-[boot.limine.ableos.modules.render_example]
-path = "boot:///render_example.hbf"
+# [boot.limine.ableos.modules.render_example]
+# path = "boot:///render_example.hbf"
 
-# [boot.limine.ableos.modules.horizon]
-# path = "boot:///horizon.hbf"
+[boot.limine.ableos.modules.horizon]
+path = "boot:///horizon.hbf"
 
 [boot.limine.ableos.modules.ps2_mouse_driver]
 path = "boot:///ps2_mouse_driver.hbf"