diff options
author | Ian Moffett <ian@osmora.org> | 2025-09-29 18:39:13 -0400 |
---|---|---|
committer | Ian Moffett <ian@osmora.org> | 2025-09-29 18:39:13 -0400 |
commit | 708171d9a153c0c1fe71cf8736b5439cbc9d14c3 (patch) | |
tree | 2c1ab1a6e0cce438ef04ba7c8150db51128da50b /src | |
parent | fd58540fe28ab420b9d8849473a50a0a76f22350 (diff) |
lib: Add initial libwidget skeleton
Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'src')
-rw-r--r-- | src/cmd/init/Makefile | 4 | ||||
-rw-r--r-- | src/lib/Makefile | 6 | ||||
-rw-r--r-- | src/lib/libwidget/Makefile | 12 | ||||
-rw-r--r-- | src/lib/libwidget/include/libwidget/core.h | 120 | ||||
-rw-r--r-- | src/lib/libwidget/include/libwidget/window.h | 46 | ||||
-rw-r--r-- | src/lib/libwidget/src/core/parts/window.c | 74 | ||||
-rw-r--r-- | src/lib/libwidget/src/core/widget_core.c | 90 |
7 files changed, 349 insertions, 3 deletions
diff --git a/src/cmd/init/Makefile b/src/cmd/init/Makefile index c160563..b58b223 100644 --- a/src/cmd/init/Makefile +++ b/src/cmd/init/Makefile @@ -2,7 +2,9 @@ include ../../data/build/user.mk CFILES = $(shell find . -name "*.c") CFILES = $(shell find . -name "*.c") -CFLAGS = -L$(LIBC_DIR) -lc $(INTERNAL_CFLAGS) +CFLAGS = -L$(LIBC_DIR) -lc $(INTERNAL_CFLAGS) \ + -L../../lib/libwidget -lwidget -L../../lib/libc/ -lc \ + -I../../lib/libwidget/include/ OBJECTS = $(CFILES:%.c=%.o) $(SYSROOT)/usr/bin/init: $(OBJECTS) diff --git a/src/lib/Makefile b/src/lib/Makefile index 1febd2e..22017e9 100644 --- a/src/lib/Makefile +++ b/src/lib/Makefile @@ -2,8 +2,10 @@ LDSCRIPT=../../sys/arch/$(TARGET)/conf/user.ld .PHONY: all all: - cd libc//; LDSCRIPT=$(LDSCRIPT) CC=$(CC) ASM=$(ASM) \ - LD=$(ASM) SYSROOT=$(SYSROOT) make + cd libc/; LDSCRIPT=$(LDSCRIPT) CC=$(CC) ASM=$(ASM) \ + LD=$(ASM) SYSROOT=$(SYSROOT) make + cd libwidget/; LDSCRIPT=$(LDSCRIPT) CC=$(CC) ASM=$(ASM) \ + LD=$(ASM) SYSROOT=$(SYSROOT) make .PHONY: clean clean: diff --git a/src/lib/libwidget/Makefile b/src/lib/libwidget/Makefile new file mode 100644 index 0000000..010870f --- /dev/null +++ b/src/lib/libwidget/Makefile @@ -0,0 +1,12 @@ +CFILES = $(shell find src/ -name "*.c") +OBJECTS = $(CFILES:.c=.o) +CFLAGS = -pedantic -Iinclude/ -L../libc/ -lc +CC = gcc +LIB_OUT = libwidget.a + +.PHONY: all +all: $(OBJECTS) + ar rcs $(LIB_OUT) $(OBJECTS) + +%.o: %.c + $(CC) -c -I$(CFLAGS) $< -o $@ diff --git a/src/lib/libwidget/include/libwidget/core.h b/src/lib/libwidget/include/libwidget/core.h new file mode 100644 index 0000000..b02ac99 --- /dev/null +++ b/src/lib/libwidget/include/libwidget/core.h @@ -0,0 +1,120 @@ +/* + * Copyright (c) 2025 Ian Marco Moffett and L5 engineers + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the project nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef LIBWIDGET_CORE_H +#define LIBWIDGET_CORE_H 1 + +#include <stdint.h> + +/* Forward declarations */ +struct widget; + +typedef enum { + WIDGET_WINDOW, + MAX_WIDGETS +} widget_type_t; + +/* + * Represents the color blueprint to get + * or set color attributes + * + * @fg: Border color + * @bg: Background + */ +struct bp_color { + uint32_t fg; + uint32_t bg; +}; + +/* + * Represents the blueprint of a widget, describes + * its attributes and can be used to apply changes + * + * @x: Cartesian X position + * @y: Cartesian Y position + * @color: Color blueprint + */ +struct blueprint { + uint32_t x; + uint32_t y; + struct bp_color color; +}; + +/* + * Widget operations, what can be done on + * a widget + * + * @init: Called upon start up + * @draw: Draw the widget + */ +struct widget_ops { + int(*init)(struct widget *wp); + int(*draw)(struct widget *wp); +}; + +/* + * Describes a widget + * + * @ops: Widget operations + * @type: Widget type + * @bp: Widget blueprint + * @leaf_count: Number of children + * @data: Widget specific data + */ +struct widget { + struct widget_ops *ops; + widget_type_t type; + struct blueprint bp; + uint16_t leaf_count; + void *data; +}; + +/* + * Initialize a widget + * + * @wp: Widget to initialize + * @type: Widget type + * @bp: Blueprint to apply upon init [NULL for default] + * + * Returns zero on success, otherwise a less than zero + * value on failure. + */ +int widget_init(struct widget *wp, widget_type_t type, struct blueprint *bp); + +/* + * Draw a widget onto the screen + * + * @wp: Widget to draw onto the screen + * + * Returns zero on success, otherwise a less than zero + * value on failure. + */ +int widget_update(struct widget *wp); + +#endif /* LIBWIDGET_CORE_H */ diff --git a/src/lib/libwidget/include/libwidget/window.h b/src/lib/libwidget/include/libwidget/window.h new file mode 100644 index 0000000..2dc58b1 --- /dev/null +++ b/src/lib/libwidget/include/libwidget/window.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2025 Ian Marco Moffett and L5 engineers + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the project nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef LIBWIDGET_WINDOW_H +#define LIBWIDGET_WINDOW_H + +#include <libwidget/core.h> + +/* + * Represents a window widget + * + * @p: Widget pointer + */ +struct window { + struct widget *wp; +}; + +extern struct widget_ops g_winops; + +#endif /* !LIBWIDGET_WINDOW_H */ diff --git a/src/lib/libwidget/src/core/parts/window.c b/src/lib/libwidget/src/core/parts/window.c new file mode 100644 index 0000000..7425407 --- /dev/null +++ b/src/lib/libwidget/src/core/parts/window.c @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2025 Ian Marco Moffett and L5 engineers + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the project nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <stdint.h> +#include <errno.h> +#include <stddef.h> +#include <libwidget/window.h> +#include <libwidget/core.h> + +#define WINDOW_MAX 2 + +static struct window windows[WINDOW_MAX]; +static uint8_t next_window = 0; + +/* + * Initialize a window + * + * TODO: We do not have a malloc so we are doing a hack + * and using a window array, change this when we can + */ +static int +window_init(struct widget *wp) +{ + struct window *win; + + if (next_window >= sizeof(WINDOW_MAX)) { + return -1; + } + + win = &windows[next_window++]; + wp->data = win; + return 0; +} + +static int +window_draw(struct widget *wp) +{ + if (wp == NULL) { + return -EINVAL; + } + + return 0; +} + +struct widget_ops g_winops = { + .init = window_init, + .draw = window_draw +}; diff --git a/src/lib/libwidget/src/core/widget_core.c b/src/lib/libwidget/src/core/widget_core.c new file mode 100644 index 0000000..22f6269 --- /dev/null +++ b/src/lib/libwidget/src/core/widget_core.c @@ -0,0 +1,90 @@ +/* + * Copyright (c) 2025 Ian Marco Moffett and L5 engineers + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the project nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <errno.h> +#include <stdint.h> +#include <stddef.h> +#include <string.h> +#include <libwidget/window.h> +#include <libwidget/core.h> + +static struct widget backends[]; + +/* + * Put the widget into a known state + */ +int +widget_init(struct widget *wp, widget_type_t type, struct blueprint *bp) +{ + struct blueprint *bp_dest; + struct bp_color *color; + struct widget *backend; + struct widget_ops *ops; + + if (wp == NULL) { + return -EINVAL; + } + + if (type >= MAX_WIDGETS) { + return -EINVAL; + } + + /* Put in a known state */ + memset(wp, 0, sizeof(*wp)); + + /* Set the default color */ + bp_dest = &wp->bp; + color = &bp_dest->color; + color->bg = 0x282828; + color->fg = 0xA89984; + + if (bp != NULL) { + bp_dest->x = bp->x; + bp_dest->y = bp->y; + memcpy(bp_dest, bp, sizeof(wp->bp)); + } + + /* Get the backend and call init */ + backend = &backends[type]; + ops = backend->ops; + return ops->init(wp); +} + +int +widget_update(struct widget *wp) +{ + struct widget_ops *ops; + + ops = wp->ops; + return ops->draw(wp); +} + +static struct widget backends[] = { + [WIDGET_WINDOW] = { .ops = &g_winops } +}; |