From 00cc8736f10098dedf6b856b9ad8bd0094211263 Mon Sep 17 00:00:00 2001 From: Benji Dial Date: Thu, 18 Feb 2021 11:56:08 -0500 Subject: vbe support, truecolor window manager pixbufs --- src/user/highway/line.c | 31 +++++++++++++++++------- src/user/highway/main.c | 1 - src/user/highway/vars.c | 44 ++++++++++++++++++++++++++++++---- src/user/include/libfont/fonts.h | 5 +++- src/user/include/libterm/command.h | 4 ++-- src/user/include/libterm/terminal.h | 2 +- src/user/include/pland/syscall.h | 13 +++++++--- src/user/include/popups/info.h | 6 ++--- src/user/include/popups/popup.h | 2 +- src/user/libfont/fonts.c | 3 ++- src/user/libterm/terminal.c | 2 +- src/user/meminfo/meminfo.c | 3 ++- src/user/mkpopup/main.c | 2 +- src/user/popups/info.c | 8 +++---- src/user/terminal/main.c | 48 +++++++++++++++++++++---------------- 15 files changed, 121 insertions(+), 53 deletions(-) (limited to 'src/user') diff --git a/src/user/highway/line.c b/src/user/highway/line.c index 5d1e543..3bc3bf2 100644 --- a/src/user/highway/line.c +++ b/src/user/highway/line.c @@ -12,18 +12,33 @@ #define LINE_SIZE 4096 static char line[LINE_SIZE]; -static inline uint8_t hex_to_int(char ch) { +static inline uint8_t hex_digit_to_int(char ch) { return ch - (ch <= '9' ? '0' : 'a' - 10); } +static inline uint8_t hex_byte_to_int(const char *ch) { + return (hex_digit_to_int(ch[0]) << 4) | hex_digit_to_int(ch[1]); +} + +static inline _pixel_t hex_to_pixel(const char *ch) { + return (_pixel_t){ + .r = hex_byte_to_int(ch), + .g = hex_byte_to_int(ch + 2), + .b = hex_byte_to_int(ch + 4) + }; +} + void ensure_color() { - const struct no_null_sn *color = get_var((struct no_null_sn){.data = "_color", .length = 6}); - //fgbg - if (color) - term_set_color( - (hex_to_int(color->data[0]) << 4) | hex_to_int(color->data[1]), - (hex_to_int(color->data[2]) << 4) | hex_to_int(color->data[3]) - ); + const struct no_null_sn *color; +get_color: + color = get_var((struct no_null_sn){.data = "_color", .length = 6}); + if (color && (color->length == 12)) + //rgb, fgbg + term_set_color(hex_to_pixel(color->data), hex_to_pixel(color->data + 6)); + else { + new_color(); + goto get_color; + } } static void line_replace(const char *from) { diff --git a/src/user/highway/main.c b/src/user/highway/main.c index 59586c1..47f3985 100644 --- a/src/user/highway/main.c +++ b/src/user/highway/main.c @@ -8,7 +8,6 @@ #include "vars.h" void main(const char *arg) { - new_color(); source(*arg ? arg : "user/default.rc"); ensure_color(); diff --git a/src/user/highway/vars.c b/src/user/highway/vars.c index ed9fa3a..2d53c3a 100644 --- a/src/user/highway/vars.c +++ b/src/user/highway/vars.c @@ -84,7 +84,7 @@ void dump_vars() { } static const char hex_digits[] = "0123456789abcdef"; -static char color[] = {'1', '0', '.', '.'}; +static char color[12] = "000000......"; static const struct no_null_sn color_name = { .data = "_color", @@ -93,12 +93,46 @@ static const struct no_null_sn color_name = { static const struct no_null_sn color_value = { .data = color, - .length = 4 + .length = 12 }; void new_color() { - const uint8_t bg = gen_rand() % 0x30 + 0x38; - color[2] = hex_digits[bg >> 4]; - color[3] = hex_digits[bg & 0xf]; + const uint16_t hue = gen_rand() % (0x33 * 6); + _pixel_t bg = {.r = 0xff, .g = 0xcc, .b = 0xcc}; + if (hue <= 0x33) { + bg.b += hue; + goto got_bg; + } + bg.b += 0x33; + if (hue <= 0x33 * 2) { + bg.r -= hue - 0x33; + goto got_bg; + } + bg.r -= 0x33; + if (hue <= 0x33 * 3) { + bg.g += hue - 0x33 * 2; + goto got_bg; + } + bg.g += 0x33; + if (hue <= 0x33 * 4) { + bg.b -= hue - 0x33 * 3; + goto got_bg; + } + bg.b -= 0x33; + if (hue <= 0x33 * 5) { + bg.r += hue - 0x33 * 4; + goto got_bg; + } + bg.r += 0x33; + bg.g -= hue - 0x33 * 5; + +got_bg: + color[ 6] = hex_digits[bg.r / 16]; + color[ 7] = hex_digits[bg.r % 16]; + color[ 8] = hex_digits[bg.g / 16]; + color[ 9] = hex_digits[bg.g % 16]; + color[10] = hex_digits[bg.b / 16]; + color[11] = hex_digits[bg.b % 16]; + set_var(color_name, color_value); } \ No newline at end of file diff --git a/src/user/include/libfont/fonts.h b/src/user/include/libfont/fonts.h index b66659f..f7ed3e1 100644 --- a/src/user/include/libfont/fonts.h +++ b/src/user/include/libfont/fonts.h @@ -1,6 +1,8 @@ #ifndef LIBFONT_FONTS_H #define LIBFONT_FONTS_H +#include + #include #include @@ -16,6 +18,7 @@ struct font_info { struct font_info *get_font(const char *name); -void put_char(const struct font_info *font, char ch, uint8_t *pb_ptr, uint32_t pb_pitch, uint8_t bg, uint8_t fg); +//pitch is in pixels +void put_char(const struct font_info *font, char ch, _pixel_t *pb_ptr, uint32_t pb_pitch, _pixel_t bg, _pixel_t fg); #endif \ No newline at end of file diff --git a/src/user/include/libterm/command.h b/src/user/include/libterm/command.h index 7587306..ca9e00d 100644 --- a/src/user/include/libterm/command.h +++ b/src/user/include/libterm/command.h @@ -37,8 +37,8 @@ struct terminal_command { uint32_t x; } as_coords; struct { - uint8_t fg; - uint8_t bg; + _pixel_t fg; + _pixel_t bg; } as_color; char as_char; uint32_t as_uint; diff --git a/src/user/include/libterm/terminal.h b/src/user/include/libterm/terminal.h index 19bd517..40536e0 100644 --- a/src/user/include/libterm/terminal.h +++ b/src/user/include/libterm/terminal.h @@ -12,7 +12,7 @@ void term_get_dimensions(uint32_t *width, uint32_t *height); void term_paint(); void term_clear(); -void term_set_color(uint8_t fg, uint8_t bg); +void term_set_color(_pixel_t fg, _pixel_t bg); void term_set_cursor(uint32_t new_y, uint32_t new_x); void term_cursor_left(); diff --git a/src/user/include/pland/syscall.h b/src/user/include/pland/syscall.h index fd0b416..9518d09 100644 --- a/src/user/include/pland/syscall.h +++ b/src/user/include/pland/syscall.h @@ -18,6 +18,13 @@ typedef struct __attribute__ ((packed)) { uint8_t pad[23]; } _dir_info_entry_t; +typedef struct __attribute__ ((packed)) { + uint8_t r; + uint8_t g; + uint8_t b; + uint8_t pad; +} _pixel_t; + enum _scn { _SCN_OPEN_FILE, _SCN_CLOSE_FILE, @@ -177,7 +184,7 @@ static inline uint32_t _count_of_dir(uint8_t drive_number, const char *path) { return _sc2(_SCN_COUNT_OF_DIR, drive_number, (uint32_t)path); } -static inline _window_handle_t _new_window(uint16_t width, uint16_t height, void *pixel_buffer) { +static inline _window_handle_t _new_window(uint16_t width, uint16_t height, _pixel_t *pixel_buffer) { return (_window_handle_t)_sc3(_SCN_NEW_WINDOW, width, height, (uint32_t)pixel_buffer); } @@ -185,11 +192,11 @@ static inline void _delete_window(_window_handle_t window) { _sc1(_SCN_DELETE_WINDOW, (uint32_t)window); } -static inline void _resize_window(_window_handle_t window, uint16_t width, uint16_t height, const void *pixel_buffer) { +static inline void _resize_window(_window_handle_t window, uint16_t width, uint16_t height, const _pixel_t *pixel_buffer) { _sc4(_SCN_RESIZE_WINDOW, (uint32_t)window, width, height, (uint32_t)pixel_buffer); } -static inline void _reassign_pixbuf(_window_handle_t window, const void *pixel_buffer) { +static inline void _reassign_pixbuf(_window_handle_t window, const _pixel_t *pixel_buffer) { _sc2(_SCN_REASSIGN_PIXBUF, (uint32_t)window, (uint32_t)pixel_buffer); } diff --git a/src/user/include/popups/info.h b/src/user/include/popups/info.h index 2c4e83f..36d557a 100644 --- a/src/user/include/popups/info.h +++ b/src/user/include/popups/info.h @@ -7,8 +7,8 @@ #include -void info_popup(struct popup *into, const char *text, uint8_t fg, uint8_t bg); -void info_popupf(struct popup *into, const char *text, uint8_t fg, uint8_t bg, ...); -void info_popupf_v(struct popup *into, const char *text, uint8_t fg, uint8_t bg, va_list args); +void info_popup(struct popup *into, const char *text, _pixel_t fg, _pixel_t bg); +void info_popupf(struct popup *into, const char *text, _pixel_t fg, _pixel_t bg, ...); +void info_popupf_v(struct popup *into, const char *text, _pixel_t fg, _pixel_t bg, va_list args); #endif \ No newline at end of file diff --git a/src/user/include/popups/popup.h b/src/user/include/popups/popup.h index 9a39997..cf4315b 100644 --- a/src/user/include/popups/popup.h +++ b/src/user/include/popups/popup.h @@ -5,7 +5,7 @@ struct popup { _window_handle_t handle; - uint8_t *pixbuf; + _pixel_t *pixbuf; bool has_quit; struct key_packet quit_as; diff --git a/src/user/libfont/fonts.c b/src/user/libfont/fonts.c index c085604..51e127b 100644 --- a/src/user/libfont/fonts.c +++ b/src/user/libfont/fonts.c @@ -56,7 +56,8 @@ struct font_info *get_font(const char *name) { return 0; } -void put_char(const struct font_info *font, char ch, uint8_t *pb_ptr, uint32_t pb_pitch, uint8_t bg, uint8_t fg) { +//pitch is in pixels +void put_char(const struct font_info *font, char ch, _pixel_t *pb_ptr, uint32_t pb_pitch, _pixel_t bg, _pixel_t fg) { //char *const msg = format("put_char(font = 0x%x, ch = '%c', pb_ptr = 0x%x, pb_pitch = %u, bg = 0x%2x, fg = 0x%2x);", font, ch, pb_ptr, pb_pitch, bg, fg); //_system_log(msg); //free_block(msg); diff --git a/src/user/libterm/terminal.c b/src/user/libterm/terminal.c index 387d63f..549194f 100644 --- a/src/user/libterm/terminal.c +++ b/src/user/libterm/terminal.c @@ -48,7 +48,7 @@ void term_clear() { try_send_command(&cmd); } -void term_set_color(uint8_t fg, uint8_t bg) { +void term_set_color(_pixel_t fg, _pixel_t bg) { struct terminal_command cmd = { .kind = SET_COLOR, .as_color = { diff --git a/src/user/meminfo/meminfo.c b/src/user/meminfo/meminfo.c index 4dbd6bf..3c3c45b 100644 --- a/src/user/meminfo/meminfo.c +++ b/src/user/meminfo/meminfo.c @@ -15,7 +15,8 @@ redo: "kernel memory free: %uk\n" "userspace memory free: %uk / %uk\n" "Escape to quit, F5 to refresh.", - 0x10, 0x07, + (_pixel_t){.r = 0, .g = 0, .b = 0}, + (_pixel_t){.r = 0xbf, .g = 0xbf, .b = 0xbf}, _kernel_dynamic_area_left() * 4, _total_userspace_left() * 4, _total_userspace_size() * 4 diff --git a/src/user/mkpopup/main.c b/src/user/mkpopup/main.c index 5b56889..05ea4e3 100644 --- a/src/user/mkpopup/main.c +++ b/src/user/mkpopup/main.c @@ -31,6 +31,6 @@ void main(const char *text) { } struct popup p; - info_popup(&p, text, 0x10, 0x07); + info_popup(&p, text, (_pixel_t){.r = 0, .g = 0, .b = 0}, (_pixel_t){.r = 0xbf, .g = 0xbf, .b = 0xbf}); make_modal(&p); } \ No newline at end of file diff --git a/src/user/popups/info.c b/src/user/popups/info.c index 667d440..eac155b 100644 --- a/src/user/popups/info.c +++ b/src/user/popups/info.c @@ -18,7 +18,7 @@ static const struct key_packet info_quits[] = { { .key_id = 0 } }; -void info_popup(struct popup *into, const char *msg, uint8_t fg, uint8_t bg) { +void info_popup(struct popup *into, const char *msg, _pixel_t fg, _pixel_t bg) { if (!info_font) info_font = get_font(FONT); @@ -41,7 +41,7 @@ void info_popup(struct popup *into, const char *msg, uint8_t fg, uint8_t bg) { const uint32_t pitch = info_font->space_width * w + 2 * PADDING; const uint32_t height = info_font->space_height * h + 2 * PADDING; - uint8_t *const pixbuf = get_block(pitch * height); + _pixel_t *const pixbuf = get_block(pitch * height * 4); for (uint32_t y = 0; y < height; ++y) for (uint32_t x = 0; x < pitch; ++x) @@ -63,13 +63,13 @@ void info_popup(struct popup *into, const char *msg, uint8_t fg, uint8_t bg) { into->handle = _new_window(pitch, height, pixbuf); } -void info_popupf_v(struct popup *into, const char *text, uint8_t fg, uint8_t bg, va_list args) { +void info_popupf_v(struct popup *into, const char *text, _pixel_t fg, _pixel_t bg, va_list args) { char *const msg = format_v(text, args); info_popup(into, msg, fg, bg); free_block(msg); } -void info_popupf(struct popup *into, const char *text, uint8_t fg, uint8_t bg, ...) { +void info_popupf(struct popup *into, const char *text, _pixel_t fg, _pixel_t bg, ...) { va_list args; va_start(args, bg); info_popupf_v(into, text, fg, bg, args); diff --git a/src/user/terminal/main.c b/src/user/terminal/main.c index 3c9eb04..b3e1d9f 100644 --- a/src/user/terminal/main.c +++ b/src/user/terminal/main.c @@ -11,22 +11,23 @@ #include #define FONT_HARDCODE "fixed-10" +#define PADDING 2 _window_handle_t window; -uint8_t *pixbuf; +_pixel_t *pixbuf; char *termbuf; struct font_info *font; uint32_t width; uint32_t height; -uint32_t cols = 50; -uint32_t rows = 15; +uint32_t cols = 100; +uint32_t rows = 30; uint32_t cursor_y = 0; uint32_t cursor_x = 0; -uint8_t bg_color = 0x10; -uint8_t fg_color = 0x07; +_pixel_t bg_color = (_pixel_t){.r = 0, .g = 0, .b = 0}; +_pixel_t fg_color = (_pixel_t){.r = 0xff, .g = 0xff, .b = 0xff}; struct waiting_for_key_record { _task_handle_t task; @@ -35,7 +36,7 @@ struct waiting_for_key_record { static void draw_char(uint32_t y, uint32_t x, bool inverted) { //syslogf("drawing 0x%2h%s at %u, %u", termbuf[y * cols + x], inverted ? " inverted" : "", y, x); - put_char(font, termbuf[y * cols + x], pixbuf + y * font->space_height * width + x * font->space_width, width, inverted ? fg_color : bg_color, inverted ? bg_color : fg_color); + put_char(font, termbuf[y * cols + x], pixbuf + (y * font->space_height + PADDING) * width + (x * font->space_width + PADDING), width, inverted ? fg_color : bg_color, inverted ? bg_color : fg_color); } static void clear() { @@ -52,9 +53,9 @@ static void scroll_fw() { for (; i < cols * rows; ++i) termbuf[i] = ' '; const uint32_t row_height = font->space_height; - for (i = 0; i < width * (height - row_height); ++i) + for (i = width * PADDING; i < width * (height - PADDING - row_height); ++i) pixbuf[i] = pixbuf[i + width * row_height]; - for (; i < width * height; ++i) + for (; i < width * (height - PADDING); ++i) pixbuf[i] = bg_color; } @@ -187,9 +188,9 @@ static void set_dimensions(uint32_t new_rows, uint32_t new_cols) { cols = new_cols; termbuf = get_block(rows * cols); - width = cols * font->space_width; - height = cols * font->space_height; - pixbuf = get_block(width * height); + width = cols * font->space_width + PADDING * 2; + height = cols * font->space_height + PADDING * 2; + pixbuf = get_block(width * height * 4); cursor_y = 0; cursor_x = 0; @@ -199,27 +200,34 @@ static void set_dimensions(uint32_t new_rows, uint32_t new_cols) { } void draw_all() { + for (uint32_t y = 0; y < PADDING; ++y) + for (uint32_t x = 0; x < width; ++x) + pixbuf[y * width + x] = bg_color; + for (uint32_t y = height - PADDING; y < height; ++y) + for (uint32_t x = 0; x < width; ++x) + pixbuf[y * width + x] = bg_color; + for (uint32_t x = 0; x < PADDING; ++x) + for (uint32_t y = PADDING; y < height - PADDING; ++y) + pixbuf[y * width + x] = bg_color; + for (uint32_t x = width - PADDING; x < width; ++x) + for (uint32_t y = PADDING; y < height - PADDING; ++y) + pixbuf[y * width + x] = bg_color; + for (uint32_t y = 0; y < rows; ++y) for (uint32_t x = 0; x < cols; ++x) draw_char(y, x, false); draw_char(cursor_y, cursor_x, true); } -//#include - void main(const char *cmd) { -//syslogf(" this task: 0x%2h", this_task); -//syslogf(" stdio task: 0x%2h", stdio_task); -//syslogf("calling task: 0x%2h", calling_task); - font = get_font(FONT_HARDCODE); if (!font) return; termbuf = get_block(cols * rows); - width = cols * font->space_width; - height = rows * font->space_height; - pixbuf = get_block(width * height); + width = cols * font->space_width + PADDING * 2; + height = rows * font->space_height + PADDING * 2; + pixbuf = get_block(width * height * 4); clear(); add_sz_ww("Portland Terminal\n"); window = _new_window(width, height, pixbuf); -- cgit v1.2.3