diff options
author | Benji Dial <benji@benjidial.net> | 2024-07-29 19:59:52 -0400 |
---|---|---|
committer | Benji Dial <benji@benjidial.net> | 2024-07-29 19:59:52 -0400 |
commit | e6c3a80b01ffb52079783cddd9be6d392d0f7039 (patch) | |
tree | 148276b9878f287bc81638f90249ec4d7b86eaf0 /applications/goldman/source | |
parent | be691582ee12613278af24cb5a824eeb357f6324 (diff) | |
download | hilbert-os-e6c3a80b01ffb52079783cddd9be6d392d0f7039.tar.gz |
redesign compositor protocol, start widget library
Diffstat (limited to 'applications/goldman/source')
-rw-r--r-- | applications/goldman/source/socket.cpp | 143 | ||||
-rw-r--r-- | applications/goldman/source/window.hpp | 6 |
2 files changed, 69 insertions, 80 deletions
diff --git a/applications/goldman/source/socket.cpp b/applications/goldman/source/socket.cpp index b175fe9..9ebd644 100644 --- a/applications/goldman/source/socket.cpp +++ b/applications/goldman/source/socket.cpp @@ -8,114 +8,95 @@ struct socket_state { euler::syscall::stream_handle socket; - std::vector<window *> windows; - daguerre::hilbert_color window_bg = euler::syscall::encode_color(0, 0, 0); + window *w; - bool try_open_window() { + bool try_show_window() { - struct [[gnu::packed]] { - uint32_t width; - uint32_t height; - } body; - - if (euler::syscall::read_from_stream(socket, sizeof(body), &body) != - euler::syscall::stream_result::success) + if (w->is_shown) return false; - window *w = new window(body.width, body.height); - w->contents.fill(window_bg); + r->lock(); + r->add_window(w); + r->unlock(); + r->dispatch_render(); + return true; - uint16_t wid = 0; + } - while (wid < windows.size()) - if (windows[wid] != 0) - ++wid; - else - break; + bool try_hide_window() { - if (wid == windows.size()) - windows.push_back(w); - else - windows[wid] = w; + if (!w->is_shown) + return false; r->lock(); - r->add_window(w); + r->remove_window(w); r->unlock(); - - struct [[gnu::packed]] { - uint8_t type; - uint16_t the_window; - } response { - .type = 0x00, - .the_window = wid - }; - - return - euler::syscall::write_to_stream(socket, sizeof(response), &response) == - euler::syscall::stream_result::success; + r->dispatch_render(); + return true; } - bool try_update_window_region() { + bool try_set_window_dimensions() { - struct [[gnu::packed]] { - uint16_t window; - uint32_t start_x; - uint32_t start_y; - uint32_t width; - uint32_t height; - } body_head; - - if (euler::syscall::read_from_stream(socket, sizeof(body_head), &body_head) != + struct [[gnu::packed]] { uint32_t width; uint32_t height; } packet; + if (euler::syscall::read_from_stream(socket, sizeof(packet), &packet) != euler::syscall::stream_result::success) return false; - std::vector<daguerre::hilbert_color> data(body_head.width * body_head.height); - - if (euler::syscall::read_from_stream(socket, data.size() * 4, data.data()) != - euler::syscall::stream_result::success) + if (packet.width > __INT_MAX__ || packet.height > __INT_MAX__) return false; - daguerre::image<daguerre::hilbert_color> - data_as_image(body_head.width, body_head.height, data.data(), body_head.width, false); - - if (body_head.window >= windows.size() || !windows[body_head.window]) - return false; + r->lock(); + w->contents = daguerre::image<daguerre::hilbert_color>( + packet.width, packet.height); + r->unlock(); + return true; - window *w = windows[body_head.window]; + } - r->lock(); + bool try_set_window_title() { - if ((int)body_head.start_x + data_as_image.width > w->contents.width || - (int)body_head.start_y + data_as_image.height > w->contents.height) { - r->unlock(); + uint32_t length; + if (euler::syscall::read_from_stream(socket, 4, &length) != + euler::syscall::stream_result::success) return false; - } - w->contents.copy_from(data_as_image, body_head.start_x, body_head.start_y); + std::string title; + title.resize(length); + if (euler::syscall::read_from_stream(socket, length, title.data()) != + euler::syscall::stream_result::success) + return false; + r->lock(); + w->title = std::move(title); r->unlock(); r->dispatch_render(); return true; } - bool try_close_window() { - - uint16_t wid; + bool try_update_window_region() { - if (euler::syscall::read_from_stream(socket, 2, &wid) != + struct [[gnu::packed]] { + uint32_t start_x; uint32_t start_y; uint32_t width; uint32_t height; + } packet; + if (euler::syscall::read_from_stream(socket, sizeof(packet), &packet) != euler::syscall::stream_result::success) return false; - if (wid >= windows.size() || !windows[wid]) + static_assert(__INT_MAX__ <= __UINT64_MAX__); + if ((uint64_t)packet.start_x + packet. width > (uint64_t)w->contents. width || + (uint64_t)packet.start_y + packet.height > (uint64_t)w->contents.height) return false; - r->lock(); - - r->remove_window(windows[wid]); - windows[wid] = 0; + daguerre::image<daguerre::hilbert_color> content(packet.width, packet.height); + if (euler::syscall::read_from_stream( + socket, packet.width * packet.height * 4, content.buffer) != + euler::syscall::stream_result::success) + return false; + r->lock(); + w->contents.copy_from(content, packet.start_x, packet.start_y); r->unlock(); r->dispatch_render(); return true; @@ -130,9 +111,11 @@ struct socket_state { return false; switch (type) { - case 0x00: return try_open_window(); - case 0x01: return try_update_window_region(); - case 0x02: return try_close_window(); + case 0x00: return try_show_window(); + case 0x01: return try_hide_window(); + case 0x02: return try_set_window_dimensions(); + case 0x03: return try_set_window_title(); + case 0x04: return try_update_window_region(); default: return false; } @@ -144,18 +127,20 @@ struct socket_state { euler::syscall::set_thread_name("socket thread"); + window *w = new window(); socket_state *state = new socket_state { - .socket = socket, .windows = {} }; + .socket = socket, .w = w }; + while (state->try_process_request()) ; - r->lock(); - for (unsigned i = 0; i < state->windows.size(); ++i) { - r->remove_window(state->windows[i]); - delete state->windows[i]; + if (w->is_shown) { + r->lock(); + r->remove_window(w); + r->unlock(); } - r->unlock(); delete state; + delete w; euler::syscall::close_stream(socket); euler::syscall::end_this_thread(0); diff --git a/applications/goldman/source/window.hpp b/applications/goldman/source/window.hpp index 008af2f..4d5b0e1 100644 --- a/applications/goldman/source/window.hpp +++ b/applications/goldman/source/window.hpp @@ -9,6 +9,10 @@ struct window { int x; int y; - window(int width, int height) : contents(width, height), x(0), y(0) {} + bool is_shown; + + std::string title; + + window() : x(0), y(0), is_shown(false) {} }; |