From c34b9191f258ddc15c5b45c000cd0266aed9dead Mon Sep 17 00:00:00 2001 From: Benji Dial Date: Mon, 29 Jul 2024 21:26:17 -0400 Subject: [PATCH] window borders --- applications/goldman/makefile | 2 +- applications/goldman/source/renderer.cpp | 17 ++-- applications/goldman/source/renderer.hpp | 10 ++- applications/goldman/source/socket.cpp | 8 +- applications/goldman/source/window.cpp | 78 ++++++++++++++++++ applications/goldman/source/window.hpp | 12 ++- applications/hello/source/main.cpp | 16 +++- euler/include/std/list.hpp | 5 ++ libraries/daguerre/include/daguerre/image.hpp | 7 ++ .../include/daguerre/impl/fixed-font.hpp | 1 + .../daguerre/include/daguerre/impl/image.hpp | 20 ++++- libraries/pake/include/pake/widget.hpp | 8 ++ .../pake/include/pake/widgets/fixed-text.hpp | 5 +- libraries/pake/source/widgets/fixed-text.cpp | 42 ++++++++-- readme.txt | 9 +- skeleton/assets/readme.txt | 4 - .../10x18-bold.psf} | Bin skeleton/assets/terminus/6x12.psf | Bin 0 -> 3873 bytes skeleton/assets/terminus/readme.txt | 3 + .../assets/{ => terminus}/terminus-ofl.txt | 0 20 files changed, 212 insertions(+), 35 deletions(-) create mode 100644 applications/goldman/source/window.cpp rename skeleton/assets/{terminus-bold-18x10.psf => terminus/10x18-bold.psf} (100%) create mode 100644 skeleton/assets/terminus/6x12.psf create mode 100644 skeleton/assets/terminus/readme.txt rename skeleton/assets/{ => terminus}/terminus-ofl.txt (100%) diff --git a/applications/goldman/makefile b/applications/goldman/makefile index c6de0e7..0d07717 100644 --- a/applications/goldman/makefile +++ b/applications/goldman/makefile @@ -1,5 +1,5 @@ SOURCES = \ - main.cpp renderer.cpp input.cpp socket.cpp + main.cpp renderer.cpp input.cpp socket.cpp window.cpp build/%.cpp.o: source/%.cpp @mkdir -p $(@D) diff --git a/applications/goldman/source/renderer.cpp b/applications/goldman/source/renderer.cpp index 629c375..7a5e6b5 100644 --- a/applications/goldman/source/renderer.cpp +++ b/applications/goldman/source/renderer.cpp @@ -52,9 +52,11 @@ void renderer::do_render() { for (auto it = windows.begin(); it != windows.end(); ++it) double_buffer.copy_from( - (*it)->contents, (*it)->x, (*it)->y, 0, 0, - std::min((*it)->contents.width, double_buffer.width - (*it)->x), - std::min((*it)->contents.height, double_buffer.height - (*it)->y)); + (*it)->contents_with_decorations, (*it)->x, (*it)->y, 0, 0, + std::min((*it)->contents_with_decorations.width, + double_buffer.width - (*it)->x), + std::min((*it)->contents_with_decorations.height, + double_buffer.height - (*it)->y)); double_buffer.convert_from( cursor_background, cursor, cursor_x, cursor_y, 0, 0, @@ -93,10 +95,15 @@ void renderer::bump_cursor(int x_offset, int y_offset) { } -void renderer::add_window(const window *w) { +void renderer::add_window(window *w) { + if (windows.size() != 0) + windows.back()->draw_decorations(false); + w->draw_decorations(true); windows.push_back(w); } -void renderer::remove_window(const window *w) { +void renderer::remove_window(window *w) { windows.remove(w); + if (windows.size() != 0) + windows.back()->draw_decorations(true); } diff --git a/applications/goldman/source/renderer.hpp b/applications/goldman/source/renderer.hpp index 14a5964..91afe88 100644 --- a/applications/goldman/source/renderer.hpp +++ b/applications/goldman/source/renderer.hpp @@ -18,7 +18,7 @@ class renderer { int cursor_y; //bottom to top - std::list windows; + std::list windows; std::mutex mut; @@ -55,7 +55,11 @@ public: void bump_cursor(int x_offset, int y_offset); - void add_window(const window *w); - void remove_window(const window *w); + void add_window(window *w); + void remove_window(window *w); + + inline bool is_top(window *w) { + return windows.size() != 0 && w == windows.back(); + } }; diff --git a/applications/goldman/source/socket.cpp b/applications/goldman/source/socket.cpp index 9ebd644..aa9b85e 100644 --- a/applications/goldman/source/socket.cpp +++ b/applications/goldman/source/socket.cpp @@ -43,12 +43,13 @@ struct socket_state { euler::syscall::stream_result::success) return false; - if (packet.width > __INT_MAX__ || packet.height > __INT_MAX__) + if (packet. width > __INT_MAX__ - window::decorations_extra_width || + packet.height > __INT_MAX__ - window::decorations_extra_height) return false; r->lock(); - w->contents = daguerre::image( - packet.width, packet.height); + w->set_size(packet.width, packet.height); + w->draw_decorations(r->is_top(w)); r->unlock(); return true; @@ -69,6 +70,7 @@ struct socket_state { r->lock(); w->title = std::move(title); + w->draw_decorations(r->is_top(w)); r->unlock(); r->dispatch_render(); return true; diff --git a/applications/goldman/source/window.cpp b/applications/goldman/source/window.cpp new file mode 100644 index 0000000..fe5212a --- /dev/null +++ b/applications/goldman/source/window.cpp @@ -0,0 +1,78 @@ +#include +#include "renderer.hpp" +#include "window.hpp" +#include + +//TODO: make these statically intialized once that is implemented +bool have_initialized_decoration_stuff; +daguerre::fixed_font *title_font; +daguerre::hilbert_color border_color_top; +daguerre::hilbert_color border_color_not_top; +daguerre::hilbert_color title_color; + +void window::set_size(int width, int height) { + contents_with_decorations = + daguerre::image(width + 4, height + 18); + contents = daguerre::image( + width, height, + &contents_with_decorations.at(2, 16), + contents_with_decorations.buffer_pitch, false); +} + +void title_converter( + daguerre::hilbert_color &out, const bool &in, const bool &top) { + out = in ? title_color : top ? border_color_top : border_color_not_top; +} + +void window::draw_decorations(bool top) { + + //TODO: handle error loading font. + //see also comment on title_font declaration. + if (!have_initialized_decoration_stuff) { + title_font = new daguerre::fixed_font( + *daguerre::try_load_psf("/assets/terminus/6x12.psf")); + assert(title_font->glyph_height == 12); + border_color_top = euler::syscall::encode_color(0x00, 0x3f, 0xff); + border_color_not_top = euler::syscall::encode_color(0x22, 0x22, 0x22); + title_color = euler::syscall::encode_color(0xff, 0xff, 0xff); + have_initialized_decoration_stuff = true; + } + + static_assert( decorations_extra_width == 4); + static_assert(decorations_extra_height == 18); + + auto border_color = top ? border_color_top : border_color_not_top; + + contents_with_decorations.fill(border_color, 0, 0, + contents_with_decorations.width, 16); + contents_with_decorations.fill( + border_color, 0, contents_with_decorations.height - 2, + contents_with_decorations.width, 2); + contents_with_decorations.fill(border_color, 0, 16, + 2, contents_with_decorations.height - 18); + contents_with_decorations.fill(border_color, + contents_with_decorations.width - 2, 16, + 2, contents_with_decorations.height - 18); + + //TODO: make UTF-8--safe + unsigned max_title_length = + contents_with_decorations.width / title_font->glyph_width; + std::string *title_to_draw; + std::string shortened_title; + if (title.size() > max_title_length) { + shortened_title.resize(max_title_length - 3); + memcpy(shortened_title.data(), title.data(), max_title_length - 3); + shortened_title[max_title_length - 3] = '.'; + shortened_title[max_title_length - 2] = '.'; + shortened_title[max_title_length - 1] = '.'; + title_to_draw = &shortened_title; + } + else + title_to_draw = &title; + + contents_with_decorations.render_text(*title_font, top, + contents_with_decorations.width / 2 - + (title_to_draw->size() * title_font->glyph_width) / 2, + 2, title_to_draw->data(), title_converter); + +} diff --git a/applications/goldman/source/window.hpp b/applications/goldman/source/window.hpp index 4d5b0e1..5c2d7a0 100644 --- a/applications/goldman/source/window.hpp +++ b/applications/goldman/source/window.hpp @@ -4,6 +4,10 @@ struct window { + static constexpr int decorations_extra_width = 4; + static constexpr int decorations_extra_height = 18; + + daguerre::image contents_with_decorations; daguerre::image contents; int x; @@ -13,6 +17,12 @@ struct window { std::string title; - window() : x(0), y(0), is_shown(false) {} + void set_size(int width, int height); + void draw_decorations(bool top); + + inline window() : x(0), y(0), is_shown(false) { + set_size(0, 0); + draw_decorations(false); + } }; diff --git a/applications/hello/source/main.cpp b/applications/hello/source/main.cpp index 1f45407..b297721 100644 --- a/applications/hello/source/main.cpp +++ b/applications/hello/source/main.cpp @@ -7,18 +7,30 @@ daguerre::fixed_font *font; int main(int, char **) { font = new daguerre::fixed_font( - daguerre::try_load_psf("/assets/terminus-bold-18x10.psf").value()); + daguerre::try_load_psf("/assets/terminus/10x18-bold.psf").value()); pake::widgets::fixed_text *text = new pake::widgets::fixed_text("Hello, world!", font, euler::syscall::encode_color(0xaa, 0xaa, 0xaa), - euler::syscall::encode_color(0x00, 0x00, 0x00)); + euler::syscall::encode_color(0x00, 0x00, 0x00), + pake::halign::center, pake::valign::center); pake::window w(300, 200, "Hello"); w.set_root(std::unique_ptr(text)); w.render_and_send_to_compositor(); w.show(); + pake::widgets::fixed_text *text2 = + new pake::widgets::fixed_text("H!", font, + euler::syscall::encode_color(0xaa, 0xaa, 0xaa), + euler::syscall::encode_color(0x00, 0x00, 0x00), + pake::halign::center, pake::valign::center); + + pake::window w2(100, 50, "Hello 2"); + w2.set_root(std::unique_ptr(text2)); + w2.render_and_send_to_compositor(); + w2.show(); + //TODO: call event loop euler::syscall::stream_handle h1, h2; diff --git a/euler/include/std/list.hpp b/euler/include/std/list.hpp index c0d6e21..030eccc 100644 --- a/euler/include/std/list.hpp +++ b/euler/include/std/list.hpp @@ -155,6 +155,11 @@ namespace std { return *this; } + T &back() { return last_node->value; } + const T &back() const { return last_node->value; } + + size_t size() const noexcept { return count; } + }; } diff --git a/libraries/daguerre/include/daguerre/image.hpp b/libraries/daguerre/include/daguerre/image.hpp index a55f43b..3c9c902 100644 --- a/libraries/daguerre/include/daguerre/image.hpp +++ b/libraries/daguerre/include/daguerre/image.hpp @@ -110,6 +110,13 @@ namespace daguerre { param_converter_t *conversion = &default_conversion); + //does not check bounds or wrap text + template + void render_text( + const fixed_font &font, int x, int y, const char *text, + converter_t *conversion = + &default_conversion); + }; template diff --git a/libraries/daguerre/include/daguerre/impl/fixed-font.hpp b/libraries/daguerre/include/daguerre/impl/fixed-font.hpp index 1a5f3d8..61a27fc 100644 --- a/libraries/daguerre/include/daguerre/impl/fixed-font.hpp +++ b/libraries/daguerre/include/daguerre/impl/fixed-font.hpp @@ -35,6 +35,7 @@ namespace daguerre { glyphs[i] = std::move(other.glyphs[i]); other.glyph_width = 0; other.glyph_height = 0; + return *this; } template diff --git a/libraries/daguerre/include/daguerre/impl/image.hpp b/libraries/daguerre/include/daguerre/impl/image.hpp index 6cf2ca9..c91cc7d 100644 --- a/libraries/daguerre/include/daguerre/impl/image.hpp +++ b/libraries/daguerre/include/daguerre/impl/image.hpp @@ -107,7 +107,7 @@ namespace daguerre { void image::fill( const color_t &color, int start_x, int start_y, int width, int height) { for (int y = start_y; y < start_y + height; ++y) - for (int x = 0; x < start_x + width; ++x) + for (int x = start_x; x < start_x + width; ++x) buffer[y * buffer_pitch + x] = color; } @@ -170,7 +170,7 @@ namespace daguerre { void image::convert_from( const image &other, int to_x, int to_y, converter_t *conversion) { - convert_from(other, to_x, to_y, 0, 0, other.width, other.y, conversion); + convert_from(other, to_x, to_y, 0, 0, other.width, other.height, conversion); } template @@ -213,6 +213,22 @@ namespace daguerre { } + template + template + void image::render_text( + const fixed_font &font, int x, int y, const char *text, + converter_t *conversion) { + + while (*text) { + int ch = *text; + if (ch >= 0 && ch < 128) + convert_from(font.glyphs[ch], x, y, conversion); + ++text; + x += font.glyph_width; + } + + } + template void swap(image &a, image &b) { std::swap(a.delete_buffer_on_destruct, b.delete_buffer_on_destruct); diff --git a/libraries/pake/include/pake/widget.hpp b/libraries/pake/include/pake/widget.hpp index dad5651..466fb80 100644 --- a/libraries/pake/include/pake/widget.hpp +++ b/libraries/pake/include/pake/widget.hpp @@ -4,6 +4,14 @@ namespace pake { + enum class halign { + left, center, right + }; + + enum class valign { + top, center, bottom + }; + class widget { public: diff --git a/libraries/pake/include/pake/widgets/fixed-text.hpp b/libraries/pake/include/pake/widgets/fixed-text.hpp index c6dafab..dc2e277 100644 --- a/libraries/pake/include/pake/widgets/fixed-text.hpp +++ b/libraries/pake/include/pake/widgets/fixed-text.hpp @@ -12,6 +12,8 @@ namespace pake::widgets { std::string text; int width, height; + halign ha; + valign va; public: //TODO: look up font in some kind of catalogue @@ -19,7 +21,8 @@ namespace pake::widgets { std::string &&text, const daguerre::fixed_font *font, daguerre::hilbert_color bg, - daguerre::hilbert_color fg); + daguerre::hilbert_color fg, + halign ha, valign va); virtual void render( dirtiable_image &onto, int x_off, int y_off, bool force) override; diff --git a/libraries/pake/source/widgets/fixed-text.cpp b/libraries/pake/source/widgets/fixed-text.cpp index 9ae55dc..c3a9a10 100644 --- a/libraries/pake/source/widgets/fixed-text.cpp +++ b/libraries/pake/source/widgets/fixed-text.cpp @@ -12,24 +12,48 @@ namespace pake::widgets { std::string &&text, const daguerre::fixed_font *font, daguerre::hilbert_color bg, - daguerre::hilbert_color fg) - : font(font), bg(bg), fg(fg), text(std::move(text)) {} + daguerre::hilbert_color fg, + halign ha, valign va) + : font(font), bg(bg), fg(fg), + text(std::move(text)), ha(ha), va(va) {} void fixed_text::render( dirtiable_image &onto, int x_off, int y_off, bool force) { if (force) { - onto.image.fill( bg, x_off, y_off, width, height); - onto.dirty.fill(true, x_off, y_off, width, height); - //TODO: have options for alignment + //TODO: check overflow + + onto.dirty.fill(true, x_off, y_off, width, height); + + onto.image.fill( bg, x_off, y_off, width, height); + + switch (ha) { + case halign::left: + break; + case halign::center: + x_off = width / 2 - text.size() * font->glyph_width / 2; + break; + case halign::right: + x_off = width - text.size() * font->glyph_width; + break; + } + + switch (va) { + case valign::top: + break; + case valign::center: + y_off = height / 2 - font->glyph_height / 2; + break; + case valign::bottom: + y_off = height - font->glyph_width; + break; + } + onto.image.render_text( *font, fg, x_off, y_off, text.data(), draw_if_true); - onto.dirty.fill( - true, x_off, y_off, - font->glyph_width * text.size(), - font->glyph_height); + } } diff --git a/readme.txt b/readme.txt index d1f2829..0cbd9a8 100644 --- a/readme.txt +++ b/readme.txt @@ -5,7 +5,8 @@ makefile target builds a disk image at build/disk.iso that can be booted on a 64-bit bios system. you can use command [2] to build that. finally, use command [3] to run the disk in qemu with gdb attached. - [1] apt install bison flex g++ gdb git libgmp-dev libmpfr-dev libmpc-dev make nasm qemu-system-x86 texinfo xorriso + [1] apt install bison flex g++ gdb git libgmp-dev libmpfr-dev + libmpc-dev make nasm qemu-system-x86 texinfo xorriso [2] make -j$(nproc) [3] make run @@ -44,7 +45,7 @@ acknowledgements (any under "dependencies" are downloaded during build): license: https://unsplash.com/license source: https://unsplash.com/photos/selective-focus-photography-snowflakes-9yhy1FXlKwI - - skeleton/assets/terminus-bold-18x10.psf (terminus font, bold, 18x10) + - skeleton/assets/terminus/*.psf (terminus font) copyright 2020 dimitar toshkov zhekov license: skeleton/assets/terminus-ofl.txt (sil open font license v1.1) homepage: https://terminus-font.sourceforge.net/ @@ -87,8 +88,8 @@ project structure: - libraries/daguerre: an image loading / rendering library. - - libraries/goldman: - a library for interfacing with the goldman compositor + - libraries/pake: + a widget library for windowed applications - patches: a couple patches that are applied to dependencies diff --git a/skeleton/assets/readme.txt b/skeleton/assets/readme.txt index a9c6b1d..23bddbc 100644 --- a/skeleton/assets/readme.txt +++ b/skeleton/assets/readme.txt @@ -5,7 +5,3 @@ its license can be found online at https://unsplash.com/license. the icon in pointer.ppm is released under the cc0 1.0 universal public domain dedication (https://creativecommons.org/publicdomain/zero/1.0/). - -the font in terminus-bold-18x10.psf is the "terminus" font, in bold weight, at -18x10. it can be found only at https://terminus-font.sourceforge.net/ and is -under the license in terminus-ofl.txt (the sil open font license v1.1). diff --git a/skeleton/assets/terminus-bold-18x10.psf b/skeleton/assets/terminus/10x18-bold.psf similarity index 100% rename from skeleton/assets/terminus-bold-18x10.psf rename to skeleton/assets/terminus/10x18-bold.psf diff --git a/skeleton/assets/terminus/6x12.psf b/skeleton/assets/terminus/6x12.psf new file mode 100644 index 0000000000000000000000000000000000000000..4f4be5a74403a754f4a0cace06ca7bf9bc00b225 GIT binary patch literal 3873 zcmZWrNswDt6@65tsK4++i!6G<;sFT}Lz0FtG|+$;5`F@KPcTEk0Wml8CqPh$Nfe`W zzDWbJBdOaNJBjUl4*_H30ox&6&Blwa;(f2Y@Is{(yYx~ERUOW~?|n%PSGxN5-*?{~ z&V7UKKXcczAzAVpMBvXZK7`*W?`D)KjYMv=wp#sutD*9^KW=S}RW`|JG)hw|dSla3 zkSN-Gl-pF*)N?>(W9A!;dC`tZTP^w_$@_d9PxbwN&@b+~K4lg-vHBvLk zF)7g(o2OUP+=||8Y>&s=8!DPzi{tU4)TU`#8!}CV48NpCp65%|YL()AY?HWJ@fpdO zVVUw^DKd2;TaxNUD66l`jpr(&CE5D8ZdEd-Nz$2okGIBKanR2b6AyyGLVOg6Fz6rR z-WrW12~Q|VM*0Ahen(A0%7zBMDJ1 zgRrdGNpum6Oj(MbGU*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