From 77a708d41e6274db71b8fcb0113440e52715b8f8 Mon Sep 17 00:00:00 2001 From: koniifer Date: Sun, 10 Nov 2024 18:57:48 +0000 Subject: [PATCH] sunrising sunset --- Cargo.lock | 6 +- sysdata/libraries/stn/src/buffer.hb | 12 ++-- sysdata/libraries/stn/src/memory.hb | 11 ++++ sysdata/libraries/sunset_proto/src/client.hb | 62 +++++++++++++++++++ sysdata/libraries/sunset_proto/src/lib.hb | 36 +++++++++++ sysdata/libraries/sunset_proto/src/message.hb | 4 ++ sysdata/libraries/sunset_proto/src/server.hb | 34 ++++++++++ sysdata/programs/horizon/src/main.hb | 6 +- sysdata/programs/sunset_client/meta.toml | 11 ++++ sysdata/programs/sunset_client/src/main.hb | 24 +++++++ sysdata/programs/sunset_server/meta.toml | 11 ++++ sysdata/programs/sunset_server/src/main.hb | 36 +++++++++++ sysdata/system_config.toml | 6 ++ 13 files changed, 247 insertions(+), 12 deletions(-) create mode 100644 sysdata/libraries/sunset_proto/src/client.hb create mode 100644 sysdata/libraries/sunset_proto/src/lib.hb create mode 100644 sysdata/libraries/sunset_proto/src/message.hb create mode 100644 sysdata/libraries/sunset_proto/src/server.hb create mode 100644 sysdata/programs/sunset_client/meta.toml create mode 100644 sysdata/programs/sunset_client/src/main.hb create mode 100644 sysdata/programs/sunset_server/meta.toml create mode 100644 sysdata/programs/sunset_server/src/main.hb diff --git a/Cargo.lock b/Cargo.lock index 56dc2fc0..1e015747 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#8b98c2ed1becb92046bb7b687ca00813da441248" +source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#c61efc393361089f5217db9f4a9011ed0248b8db" [[package]] name = "hblang" version = "0.1.0" -source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#8b98c2ed1becb92046bb7b687ca00813da441248" +source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#c61efc393361089f5217db9f4a9011ed0248b8db" 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#8b98c2ed1becb92046bb7b687ca00813da441248" +source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#c61efc393361089f5217db9f4a9011ed0248b8db" dependencies = [ "hbbytecode", ] diff --git a/sysdata/libraries/stn/src/buffer.hb b/sysdata/libraries/stn/src/buffer.hb index 680aec89..1912c593 100644 --- a/sysdata/libraries/stn/src/buffer.hb +++ b/sysdata/libraries/stn/src/buffer.hb @@ -1,27 +1,27 @@ string := @use("string.hb") -recv := fn($Expr: type, buffer_id: int, memory_map_location: ^u8): void { +recv := fn($Expr: type, buffer_id: uint, memory_map_location: ^u8): void { return @eca(4, buffer_id, memory_map_location, @sizeof(Expr)) } -write := fn($Expr: type, msg: ^Expr, buffer_id: int): void { +write := fn($Expr: type, msg: ^Expr, buffer_id: uint): void { return @eca(3, buffer_id, msg, @sizeof(Expr)) } -recv_length := fn(buffer_id: int, memory_map_location: ^u8, length: int): void { +recv_length := fn(buffer_id: uint, memory_map_location: ^u8, length: uint): void { return @eca(4, buffer_id, memory_map_location, length) } -write_length := fn(msg: ^u8, buffer_id: int, length: int): void { +write_length := fn(msg: ^u8, buffer_id: uint, length: uint): void { return @eca(3, buffer_id, msg, length) } BufferMsg := packed struct {operation: u8, msg: ^u8, msg_len: uint} -create := fn(msg: ^u8): int { +create := fn(msg: ^u8): uint { return @eca(3, 0, BufferMsg.(0, msg, @inline(string.length, msg)), @sizeof(BufferMsg)) } -search := fn(msg: ^u8): int { +search := fn(msg: ^u8): uint { return @eca(3, 0, BufferMsg.(3, msg, @inline(string.length, msg)), @sizeof(BufferMsg)) } \ No newline at end of file diff --git a/sysdata/libraries/stn/src/memory.hb b/sysdata/libraries/stn/src/memory.hb index f0b951ab..821e46fd 100644 --- a/sysdata/libraries/stn/src/memory.hb +++ b/sysdata/libraries/stn/src/memory.hb @@ -2,6 +2,17 @@ PAGE_SIZE := 4096 MAX_ALLOC := 0xFF MAX_FREE := 0xFF +is_uninit := fn($Expr: type, ptr: ^Expr): bool { + i := 0 + loop if *(@as(^u8, @bitcast(ptr)) + i) != 0 return false else if i + 1 == @sizeof(Expr) return true else i += 1 +} + +uninit := fn($Expr: type): Expr { + empty := @as(Expr, idk) + @inline(set, u8, &0, @bitcast(&empty), @sizeof(Expr)) + return empty +} + dangling := fn($Expr: type): ^Expr { return @bitcast(@alignof(Expr)) } diff --git a/sysdata/libraries/sunset_proto/src/client.hb b/sysdata/libraries/sunset_proto/src/client.hb new file mode 100644 index 00000000..da1950c3 --- /dev/null +++ b/sysdata/libraries/sunset_proto/src/client.hb @@ -0,0 +1,62 @@ +.{math: .{Vec2}, buffer, log, memory} := @use("../../stn/src/lib.hb"); +.{Surface, new_surface} := @use("../../render/src/lib.hb"); +.{WindowWrapper, WindowID, WindowProps, WindowMessage, message, BUFFER} := @use("./lib.hb") + +buffer_id := @as(?uint, null) + +new_window := fn(props: WindowProps): ?WindowWrapper { + if buffer_id == null { + log.error("client: (request_new) buffer id is null. did you init the client?\0") + return null + } + buf := memory.uninit(?WindowWrapper) + buffer.write(WindowMessage, &.(message.new, 0, @sizeof(WindowProps), @bitcast(&props), @bitcast(&buf)), @unwrap(buffer_id)) + loop if memory.is_uninit(?WindowWrapper, &buf) == false break else { + } + // loop until i write a better socket + i := 0 + loop if i >= 1000 break else i += 1 + return buf +} + +update_window_props := fn(wrapper: WindowWrapper): ?WindowProps { + log.error("todo: sunset_proto/src/client/update_window_props.hb\0") + if buffer_id == null { + log.error("client: (request_update_properties) buffer id is null. did you init the client?\0") + return null + } + // TODO: buffer.write(WINDOWING_BUFFER, update properties) request + return null +} + +request_shutdown := fn(): ?bool { + if buffer_id == null { + log.error("client: (request_shutdown) buffer id is null. did you init the client?\0") + return null + } + buf := memory.uninit(?u8) + buffer.write(WindowMessage, &.(message.shutdown, 0, 0, memory.dangling(u8), @bitcast(&buf)), @unwrap(buffer_id)) + loop if memory.is_uninit(?u8, &buf) == false break else { + } + // loop until i write a better socket + i := 0 + loop if i >= 1000 break else i += 1 + // had to do ?u8 here, ?bool didnt work + if buf == null | @unwrap(buf) != 255 { + return false + } else { + return true + } +} + +init := fn(): void { + log.info("client: waiting for server\0") + id := 0 + loop if id != 0 { + buffer_id = id + log.info("client: done waiting\0") + return + } else { + id = buffer.search(BUFFER) + } +} \ No newline at end of file diff --git a/sysdata/libraries/sunset_proto/src/lib.hb b/sysdata/libraries/sunset_proto/src/lib.hb new file mode 100644 index 00000000..7006f7d1 --- /dev/null +++ b/sysdata/libraries/sunset_proto/src/lib.hb @@ -0,0 +1,36 @@ +.{math: .{Vec2}} := @use("../../stn/src/lib.hb"); +.{Surface} := @use("../../render/src/lib.hb") + +$BUFFER := "sunset\0" + +client := @use("./client.hb") +server := @use("./server.hb") +message := @use("./message.hb") + +WindowID := uint +MessageKind := uint + +WindowMessage := packed struct { + kind: MessageKind, + id: WindowID, + length: uint, + data_ptr: ^u8, + // need to replace this with a buffer id + callback: ^u8, +} + +WindowProps := struct { + position: Vec2(uint), + dimensions: Vec2(uint), + title: ^u8, +} + +WindowWrapper := struct { + id: WindowID, + props: WindowProps, +} + +Window := struct { + surface: Surface, + props: WindowProps, +} \ No newline at end of file diff --git a/sysdata/libraries/sunset_proto/src/message.hb b/sysdata/libraries/sunset_proto/src/message.hb new file mode 100644 index 00000000..700ab52a --- /dev/null +++ b/sysdata/libraries/sunset_proto/src/message.hb @@ -0,0 +1,4 @@ +$new := 0 +$destroy := 1 +$update_props := 2 +$shutdown := 3 \ No newline at end of file diff --git a/sysdata/libraries/sunset_proto/src/server.hb b/sysdata/libraries/sunset_proto/src/server.hb new file mode 100644 index 00000000..4581571e --- /dev/null +++ b/sysdata/libraries/sunset_proto/src/server.hb @@ -0,0 +1,34 @@ +.{math, log, string, random, buffer, memory} := @use("../../stn/src/lib.hb"); +.{Surface, new_surface} := @use("../../render/src/lib.hb"); +.{WindowWrapper, WindowID, WindowProps, WindowMessage, BUFFER} := @use("./lib.hb") + +WindowServer := struct {window_count: uint, buffer_id: uint} + +server := @as(?WindowServer, null) + +new_window_id := fn(): WindowID { + return random.any(uint) +} + +init := fn(): bool { + log.info("server: starting server\0") + server = .(0, buffer.create(BUFFER)) + return true +} + +recieve := fn(): ?WindowMessage { + if server == null { + log.error("server: (request_new) server is null. did you init the client?\0") + return null + } + buf := memory.uninit(WindowMessage) + buffer.recv(WindowMessage, @unwrap(server).buffer_id, @bitcast(&buf)) + if memory.is_uninit(WindowMessage, &buf) { + return null + } + return buf +} + +should_shutdown := fn(): bool { + return false +} \ No newline at end of file diff --git a/sysdata/programs/horizon/src/main.hb b/sysdata/programs/horizon/src/main.hb index 1b7ac838..0666373d 100644 --- a/sysdata/programs/horizon/src/main.hb +++ b/sysdata/programs/horizon/src/main.hb @@ -60,9 +60,9 @@ main := fn(): int { // TODO: Read the window buffer here { - // ret := buffer.recv([u8; 4096], win_buff, mem_buf) - // for some reason this null check causes the compiler to spin forever - // if ret == null { + // buffer.recv([u8; 4096], win_buff, mem_buf) + // // for some reason this null check causes the compiler to spin forever + // if *mem_buf == 0 { // log.info("No messages\0") // } else { // log.info("Handle Messages\0") diff --git a/sysdata/programs/sunset_client/meta.toml b/sysdata/programs/sunset_client/meta.toml new file mode 100644 index 00000000..cbc588cc --- /dev/null +++ b/sysdata/programs/sunset_client/meta.toml @@ -0,0 +1,11 @@ +[package] +name = "sunset_client" +authors = ["koniifer"] + +[dependants.libraries] + +[dependants.binaries] +hblang.version = "1.0.0" + +[build] +command = "hblang src/main.hb" diff --git a/sysdata/programs/sunset_client/src/main.hb b/sysdata/programs/sunset_client/src/main.hb new file mode 100644 index 00000000..a5434dfa --- /dev/null +++ b/sysdata/programs/sunset_client/src/main.hb @@ -0,0 +1,24 @@ +.{log, string} := @use("../../../libraries/stn/src/lib.hb") +sunset := @use("../../../libraries/sunset_proto/src/lib.hb") + +main := fn(): void { + sunset.client.init() + log.info("client: request new window\0") + window := sunset.client.new_window(.(.(100, 100), .(150, 150), "Hello, World!\0")) + if window == null { + log.error("Could not create window\0") + return + } + log.info("client: window created. title:\0") + log.info(window.props.title) + window.props.position = .(500, 500) + props := @unwrap(sunset.client.update_window_props(@unwrap(window))) + if props.position.x != @unwrap(window).props.position.x { + log.error("client: we did not update props\0") + } + log.info("client: sending shutdown request\0") + shutdown := sunset.client.request_shutdown() + if shutdown == null { + log.error("client: didnt shutdown the server\0") + } +} \ No newline at end of file diff --git a/sysdata/programs/sunset_server/meta.toml b/sysdata/programs/sunset_server/meta.toml new file mode 100644 index 00000000..84c7d22f --- /dev/null +++ b/sysdata/programs/sunset_server/meta.toml @@ -0,0 +1,11 @@ +[package] +name = "sunset_server" +authors = ["koniifer"] + +[dependants.libraries] + +[dependants.binaries] +hblang.version = "1.0.0" + +[build] +command = "hblang src/main.hb" diff --git a/sysdata/programs/sunset_server/src/main.hb b/sysdata/programs/sunset_server/src/main.hb new file mode 100644 index 00000000..c3e2011e --- /dev/null +++ b/sysdata/programs/sunset_server/src/main.hb @@ -0,0 +1,36 @@ +.{log} := @use("../../../libraries/stn/src/lib.hb") +render := @use("../../../libraries/render/src/lib.hb"); +.{server, message, WindowWrapper, WindowProps} := @use("../../../libraries/sunset_proto/src/lib.hb") + +psf := @embed("../../../assets/consolefonts/tamsyn/10x20r.psf") + +main := fn(): void { + screen := render.init(false) + font := render.text.font_from_psf2(@bitcast(&psf), false) + if font == null { + return + } + if server.init() == false { + log.error("Failed to create server\0") + return + } + loop if server.should_shutdown() break else { + recv := server.recieve() + if recv == null { + continue + } else if recv.kind == message.new { + props := *@as(^WindowProps, @bitcast(recv.data_ptr)); + *@as(^?WindowWrapper, @bitcast(recv.callback)) = WindowWrapper.(0, props) + render.put_rect(screen, props.position, props.dimensions, render.white) + render.put_text(screen, font, props.position + .(1, 1), render.white, props.title) + render.put_hline(screen, props.position.y + font.height + 2, props.position.x, props.position.x + props.dimensions.x, render.white) + log.info("server: made a new window\0") + } else if recv.kind == message.shutdown { + *@as(^?u8, @bitcast(recv.callback)) = 255 + break + } + } + + render.put_text(screen, font, .(0, 0), render.white, "Shutdown triggered\0") + log.info("Server shutdown\0") +} \ No newline at end of file diff --git a/sysdata/system_config.toml b/sysdata/system_config.toml index 93bd7396..53d17e61 100644 --- a/sysdata/system_config.toml +++ b/sysdata/system_config.toml @@ -33,3 +33,9 @@ path = "boot:///render_example.hbf" # [boot.limine.ableos.modules.ps2_keyboard_driver] # path = "boot:///ps2_keyboard_driver.hbf" + +[boot.limine.ableos.modules.sunset_client] +path = "boot:///sunset_client.hbf" + +[boot.limine.ableos.modules.sunset_server] +path = "boot:///sunset_server.hbf"