1
0
Fork 0
forked from AbleOS/ableos
ableos/sysdata/libraries/sunset_proto/src/server.hb

75 lines
2.8 KiB
Plaintext
Raw Normal View History

2024-11-10 12:57:48 -06:00
.{math, log, string, random, buffer, memory} := @use("../../stn/src/lib.hb");
.{Color, Surface, new_surface, put_surface, sync, put_rect, put_filled_rect, text, put_text} := @use("../../render/src/lib.hb");
2024-11-13 11:03:31 -06:00
.{Channel, Window, WindowProps, WindowData, MessageHeader, BUFFER_SERVER, BUFFER_CLIENT, message, permissions, recv_header, recv_message, send_message, send_header, await_message} := @use("./lib.hb")
2024-11-10 12:57:48 -06:00
2024-11-10 19:03:01 -06:00
WindowServer := struct {
window_count: uint,
2024-11-13 11:03:31 -06:00
channel: Channel,
// ! replace this with a collection when we get an allocator
2024-11-13 11:03:31 -06:00
windows: [?WindowData; 10],
font: text.Font,
2024-11-10 19:03:01 -06:00
}
2024-11-10 12:57:48 -06:00
// ! in the future this should be safely handled
server := @as(WindowServer, idk)
2024-11-10 12:57:48 -06:00
psf := @embed("../../../assets/consolefonts/tamsyn/10x20r.psf")
start := fn(): void {
font := text.font_from_psf2(@bitcast(&psf), false)
if font == null {
log.error("server: failed to load asset\0")
return
}
server = .(0, .{client: buffer.create(BUFFER_CLIENT), server: buffer.create(BUFFER_SERVER)}, .(null, null, null, null, null, null, null, null, null, null), @as(text.Font, font))
2024-11-13 11:03:31 -06:00
log.info("server: started server\0")
2024-11-10 12:57:48 -06:00
}
2024-11-13 11:03:31 -06:00
incoming := fn(): bool {
msg := recv_header(server.channel.server)
if msg == null {
2024-11-10 19:03:01 -06:00
return true
}
2024-11-13 11:03:31 -06:00
if msg.kind == message.syn {
log.info("server: recv syn\0")
channel := Channel.(buffer.create_nameless(), buffer.create_nameless())
send_message(Channel, message.ack, channel, server.channel.client)
2024-11-13 11:03:31 -06:00
props := await_message(WindowProps, channel.server)
if props.header.kind != message.props {
return true
}
log.info("server: recv props\0")
// ! do inspection of requested props here
data := WindowData.(props.body, channel, permissions.default)
send_message(WindowData, message.ack, data, channel.client)
server.windows[server.window_count] = data
server.window_count += 1
2024-11-10 12:57:48 -06:00
}
2024-11-10 19:03:01 -06:00
return true
2024-11-13 11:03:31 -06:00
}
render_clients := fn(screen: Surface): void {
// support one window for test case
window := server.windows[0]
if window == null {
return
}
header := recv_header(window.channel.server)
if header == null {
return
}
if header.kind != message.frame_ready {
return
}
send_header(message.ack, window.channel.client)
ptr := await_message(^Color, window.channel.server)
if ptr.header.kind != message.ack {
return
2024-11-13 11:03:31 -06:00
}
put_surface(screen, .(ptr.body, window.props.dimensions.x, window.props.dimensions.y, window.props.dimensions.x * window.props.dimensions.y), window.props.position, false)
put_rect(screen, window.props.position - .(1, 1), window.props.dimensions + .(1, 1), .(255, 255, 255, 255))
put_filled_rect(screen, window.props.position - .(1, 21), .(window.props.dimensions.x + 2, 20), .(255, 255, 255, 255))
put_text(screen, server.font, window.props.position - .(-2, 20), .(0, 0, 0, 255), window.props.title)
2024-11-13 11:03:31 -06:00
sync(screen)
send_header(message.ack, window.channel.client)
2024-11-10 12:57:48 -06:00
}