From 789d714ed76df094bd34f5db0a84060061443de4 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Mon, 23 Jun 2025 13:40:05 -0400 Subject: usr.bin: kfgwm: Allocate new windows with malloc() As malloc() has been recently introduced into the Hyra libc, we should take advantage of it to allow us to share windows between functions without relying on the stack. Signed-off-by: Ian Moffett --- usr.bin/kfgwm/include/kfg/window.h | 1 + usr.bin/kfgwm/kfgwm.c | 40 ++++++++++++++++---------------------- usr.bin/kfgwm/window.c | 29 +++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 23 deletions(-) (limited to 'usr.bin') diff --git a/usr.bin/kfgwm/include/kfg/window.h b/usr.bin/kfgwm/include/kfg/window.h index 781c2a5..a597969 100644 --- a/usr.bin/kfgwm/include/kfg/window.h +++ b/usr.bin/kfgwm/include/kfg/window.h @@ -61,6 +61,7 @@ struct kfg_text { kfgpos_t y; }; +struct kfg_window *kfg_win_new(struct kfg_window *parent, kfgpos_t x, kfgpos_t y); int kfg_win_draw(struct kfg_window *parent, struct kfg_window *wp); int kfg_win_putstr(struct kfg_window *wp, struct kfg_text *tp); diff --git a/usr.bin/kfgwm/kfgwm.c b/usr.bin/kfgwm/kfgwm.c index 3240a0b..5a9e7b8 100644 --- a/usr.bin/kfgwm/kfgwm.c +++ b/usr.bin/kfgwm/kfgwm.c @@ -33,6 +33,7 @@ #include #include #include +#include #include static struct fbattr fbattr; @@ -42,23 +43,15 @@ static void test_win(struct kfg_window *root, kfgpos_t x, kfgpos_t y, const char *str) { struct kfg_text text; - struct kfg_window test_win; - - test_win.x = x; - test_win.y = y; - test_win.width = 250; - test_win.height = 150; - test_win.fb_pitch = fbattr.pitch; - test_win.framebuf = framep; - test_win.bg = KFG_DARK; - test_win.border_bg = KFG_RED; + struct kfg_window *test_win; + test_win = kfg_win_new(root, x, y); text.text = str; text.x = 0; text.y = 0; - kfg_win_draw(root, &test_win); - kfg_win_putstr(&test_win, &text); + kfg_win_draw(root, test_win); + kfg_win_putstr(test_win, &text); } int @@ -66,7 +59,7 @@ main(void) { int fb_fd, fbattr_fd, prot; size_t fb_size; - struct kfg_window root_win; + struct kfg_window *root_win; fb_fd = open("/dev/fb0", O_RDWR); if (fb_fd < 0) { @@ -86,16 +79,17 @@ main(void) prot = PROT_READ | PROT_WRITE; framep = mmap(NULL, fb_size, prot, MAP_SHARED, fb_fd, 0); - root_win.x = 0; - root_win.y = 0; - root_win.width = fbattr.width; - root_win.height = fbattr.height; - root_win.fb_pitch = fbattr.pitch; - root_win.framebuf = framep; - root_win.bg = KFG_RED; - root_win.border_bg = KFG_RED; - test_win(&root_win, 40, 85, "Hello, World!"); - test_win(&root_win, 150, 20, "Mrow!"); + root_win = malloc(sizeof(*root_win)); + root_win->x = 0; + root_win->y = 0; + root_win->width = fbattr.width; + root_win->height = fbattr.height; + root_win->fb_pitch = fbattr.pitch; + root_win->framebuf = framep; + root_win->bg = KFG_RED; + root_win->border_bg = KFG_RED; + test_win(root_win, 40, 85, "Hello, World!"); + test_win(root_win, 150, 20, "Mrow!"); for (;;); } diff --git a/usr.bin/kfgwm/window.c b/usr.bin/kfgwm/window.c index 8954553..3908302 100644 --- a/usr.bin/kfgwm/window.c +++ b/usr.bin/kfgwm/window.c @@ -32,6 +32,7 @@ #include #include #include +#include #include #include @@ -158,6 +159,34 @@ kfg_win_draw(struct kfg_window *parent, struct kfg_window *wp) return 0; } +/* + * Create a new default window + * + * @x: X position for this window + * @y: Y position for this window + * @w: Window width + * @h: Window height + */ +struct kfg_window * +kfg_win_new(struct kfg_window *parent, kfgpos_t x, kfgpos_t y) +{ + struct kfg_window *wp; + + if ((wp = malloc(sizeof(*wp))) == NULL) { + return NULL; + } + + wp->x = x; + wp->y = y; + wp->width = 250; + wp->height = 150; + wp->fb_pitch = parent->fb_pitch; + wp->framebuf = parent->framebuf; + wp->bg = KFG_DARK; + wp->border_bg = KFG_RED; + return wp; +} + int kfg_win_putstr(struct kfg_window *wp, struct kfg_text *tp) { -- cgit v1.2.3