commit bbb1f60e32f34950397667c50757d3a792eb6852
parent 920ea8f2b309fc179614e222171d76e1062dea63
Author: Matsuda Kenji <info@mtkn.jp>
Date: Sat, 6 Apr 2024 09:24:18 +0900
add make_window
Diffstat:
3 files changed, 57 insertions(+), 33 deletions(-)
diff --git a/sys/include/draw.h b/sys/include/draw.h
@@ -23,15 +23,17 @@ typedef struct Image {
Rectangle r;
} Image;
+#define MAX_CHILD_WIN 16 // TODO: dynamically allocate.
typedef struct Window {
- uint32 *fb;
- RGBA32 fg;
- RGBA32 bg;
- int hres;
- int vres;
- int ppsl; // pixels per scan line.
- Window *children;
- int nchildren;
+ uint32 *fb; // for the root window only.
+ int ppsl;
+ RGBA32 fg;
+ RGBA32 bg;
+ Window *parent;
+ Point pos; // in parent cordinate system in pixels. TODO: should this be hidden?
+ Point size;
+ Window **children;
+ int nchildren;
} Window;
int init_root_window(uint32 *fb_base, int hres, int vres, int ppsl, EFI_GRAPHICS_PIXEL_FORMAT pf);
@@ -41,4 +43,5 @@ void line(Window *dst, Point p0, Point p1, RGBA32 col);
// i = 0: one pixel line, i > 0: inside the border, i < 0: outside the border.
void border(Window *dst, Rectangle r, int i, RGBA32 col);
void fill(Window *dst, Rectangle r, RGBA32 col);
-void clear(Window *dst);
-\ No newline at end of file
+void clear(Window *dst);
+int make_window(Window *parent, Window *child, Point at, Point size, RGBA32 fg, RGBA32 bg);
+\ No newline at end of file
diff --git a/sys/src/kernel/draw.c b/sys/src/kernel/draw.c
@@ -22,10 +22,10 @@ init_root_window(uint32 *fb_base, int hres, int vres, int ppsl, EFI_GRAPHICS_PIX
}
root_window.fb = fb_base;
root_window.fg = 0x0;
- root_window.bg = 0xeaeaea00;
- root_window.hres = hres;
- root_window.vres = vres;
+ root_window.bg = 0x55555500;
root_window.ppsl = ppsl;
+ root_window.parent = &root_window;
+ root_window.size = (Point){hres, vres};
root_window.children = NULL;
root_window.nchildren = 0;
clear(&root_window);
@@ -35,6 +35,7 @@ init_root_window(uint32 *fb_base, int hres, int vres, int ppsl, EFI_GRAPHICS_PIX
static void
pixelRedGreenBlueReserved(Window *dst, Point p, RGBA32 col)
{
+ Window *parent;
// TODO: alpha
// TODO: this order is correct?
// UEFI-SPEC says that R is at the "byte zero".
@@ -43,7 +44,11 @@ pixelRedGreenBlueReserved(Window *dst, Point p, RGBA32 col)
(col & 0x00ff0000) >> 8 | // G
(col & 0x0000ff00) << 8 | // B
(col & 0x000000ff) << 24; // A
- if (0 <= p.x && p.x < dst->hres && 0 <= p.y && p.y < dst->vres) {
+ if ((parent = dst->parent) != dst) {
+ pixel(parent, (Point) {p.x + dst->pos.x, p.y + dst->pos.y}, col);
+ return;
+ }
+ if (0 <= p.x && p.x < dst->size.x && 0 <= p.y && p.y < dst->size.y) {
dst->fb[p.y * dst->ppsl + p.x] = _col;
}
}
@@ -51,13 +56,18 @@ pixelRedGreenBlueReserved(Window *dst, Point p, RGBA32 col)
static void
pixelBlueGreenRedReserved(Window *dst, Point p, RGBA32 col)
{
+ Window *parent;
// TODO: alpha
uint32 _col =
(col & 0xff000000) >> 8 | // R
(col & 0x00ff0000) >> 8 | // G
(col & 0x0000ff00) >> 8 | // B
(col & 0x000000ff) << 24; // A
- if (0 <= p.x && p.x < dst->hres && 0 <= p.y && p.y < dst->vres) {
+ if ((parent = dst->parent) != dst) {
+ pixel(parent, (Point) {p.x + dst->pos.x, p.y + dst->pos.y}, col);
+ return;
+ }
+ if (0 <= p.x && p.x < dst->size.x && 0 <= p.y && p.y < dst->size.y) {
dst->fb[p.y * dst->ppsl + p.x] = _col;
}
}
@@ -145,9 +155,32 @@ void
clear(Window *dst)
{
int x, y;
- for (x = 0; x < dst->hres; x++) {
- for (y = 0; y < dst->vres; y++) {
+ for (x = 0; x < dst->size.x; x++) {
+ for (y = 0; y < dst->size.y; y++) {
pixel(dst, (Point){x, y}, dst->bg);
}
}
}
+
+int
+make_window(Window *parent, Window *child, Point pos, Point size, RGBA32 fg, RGBA32 bg)
+{
+ if (parent->nchildren == MAX_CHILD_WIN) {
+ return -1;
+ }
+ child->fg = fg;
+ child->bg = bg;
+ child->parent = parent;
+ child->pos = pos;
+ child->size = size;
+ child->children = NULL; // TODO: allocate memory.
+ child->nchildren = 0;
+
+ parent->children[parent->nchildren] = child; // TODO: allocate dynamically.
+ parent->nchildren++;
+
+ clear(child);
+ border(parent, (Rectangle){pos, (Point){pos.x + size.x, pos.y + size.y}}, -4, 0x3f312b00);
+
+ return 0;
+}
diff --git a/sys/src/kernel/main.c b/sys/src/kernel/main.c
@@ -21,17 +21,11 @@ kernel_main(EFI_GRAPHICS_OUTPUT_PROTOCOL *gop)
// still can't print to console.
goto halt;
}
+ Window *root_children[MAX_CHILD_WIN]; // TODO: implement malloc.
+ root_window.children = root_children;
- Window win0 = {
- .fb = root_window.fb + (150 + 100 * root_window.ppsl),
- .fg = 0x0,
- .bg = 0xffffea00,
- .hres = 400,
- .vres = 300,
- .ppsl = root_window.ppsl,
- };
- root_window.children = &win0; // TODO: allocate array of Window.
- root_window.nchildren++;
+ Window win0;
+ make_window(&root_window, &win0, (Point){150, 100}, (Point){400, 300}, 0x0, 0xb9d08b00);
Console con0 = {
.win = &win0,
.font = &asciifont,
@@ -39,12 +33,6 @@ kernel_main(EFI_GRAPHICS_OUTPUT_PROTOCOL *gop)
.h = 24,
.pos = (Point){0, 0}
};
- Rectangle rect = {
- .min = (Point){150, 100},
- .max = (Point){150 + 400, 100 + 300}
- };
- clear(&win0);
- border(&root_window, rect, -4, 0xaa666600);
cons_printf(&con0, "hello world!\n");
halt: