aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--apps/chess/main.c2
-rw-r--r--apps/generic.mk2
-rw-r--r--apps/paint/main.c16
-rw-r--r--apps/wm/main.c7
-rw-r--r--libs/libgui/gfx.c42
-rw-r--r--libs/libgui/gfx.h51
-rw-r--r--libs/libgui/gui.c24
-rw-r--r--libs/libgui/gui.h4
-rw-r--r--libs/libgui/msg.h3
-rw-r--r--libs/libgui/psf.c4
-rw-r--r--libs/libgui/psf.h2
11 files changed, 89 insertions, 68 deletions
diff --git a/apps/chess/main.c b/apps/chess/main.c
index 8e1d470..a546e63 100644
--- a/apps/chess/main.c
+++ b/apps/chess/main.c
@@ -239,7 +239,7 @@ static void draw_board(void)
int main(void)
{
- gui_new_custom_window(&win, vec2(0, 0), vec2(TILE * 8, TILE * 8));
+ win = gui_new_custom_window(APPNAME, vec2(0, 0), vec2(TILE * 8, TILE * 8));
fen_parse(START_FEN);
draw_board();
diff --git a/apps/generic.mk b/apps/generic.mk
index c0ddd3e..809ad16 100644
--- a/apps/generic.mk
+++ b/apps/generic.mk
@@ -14,4 +14,4 @@ clean:
@$(RM) -f $(OBJS)
%.o: %.c
- @$(CC) -c $(CFLAGS) $< -o $@
+ @$(CC) -c $(CFLAGS) -DAPPNAME=\"$(NAME)\" $< -o $@
diff --git a/apps/paint/main.c b/apps/paint/main.c
index e537004..464a925 100644
--- a/apps/paint/main.c
+++ b/apps/paint/main.c
@@ -30,26 +30,30 @@ static void color_click(struct gui_event_mouse *event)
current_color = colors[event->widget - 2];
}
+// TODO: Simplify using predefined widgets and utilities
int main(void)
{
- u32 win;
- gui_new_window(&win);
+ u32 win = gui_new_window(APPNAME);
vec2 win_size = gui_window_size(win);
- u32 toolbar = gui_new_widget(win, GUI_MAIN, vec2(0, 0), vec2(win_size.x, COLOR_SIZE));
+ u32 toolbar = gui_new_widget(win, GUI_MAIN, vec2(0, 0),
+ vec2(win_size.x, COLOR_SIZE + 2 * TOOLBAR_MARGIN));
gui_fill(win, toolbar, GUI_LAYER_BG, COLOR_WHITE);
u32 color_count = COUNT(colors);
for (u32 i = 0; i < color_count; i++) {
- u32 color = gui_new_widget(win, toolbar, vec2(i * (COLOR_SIZE + TOOLBAR_MARGIN), 0),
+ u32 color = gui_new_widget(win, toolbar,
+ vec2(TOOLBAR_MARGIN + i * (COLOR_SIZE + TOOLBAR_MARGIN),
+ TOOLBAR_MARGIN),
vec2(COLOR_SIZE, COLOR_SIZE));
gui_fill(win, color, GUI_LAYER_FG, colors[i]);
gui_draw_border(win, color, GUI_LAYER_FG, 2, COLOR_BLACK);
gui_listen_widget(win, color, GUI_LISTEN_MOUSECLICK, (u32)color_click);
}
- u32 canvas = gui_new_widget(win, GUI_MAIN, vec2(0, COLOR_SIZE),
- vec2(win_size.x, win_size.y - COLOR_SIZE));
+ u32 canvas =
+ gui_new_widget(win, GUI_MAIN, vec2(0, COLOR_SIZE + 2 * TOOLBAR_MARGIN),
+ vec2(win_size.x, win_size.y - (COLOR_SIZE + 2 * TOOLBAR_MARGIN)));
gui_fill(win, canvas, GUI_LAYER_BG, COLOR_WHITE);
gui_listen_widget(win, canvas, GUI_LISTEN_MOUSEMOVE, (u32)mousemove);
diff --git a/apps/wm/main.c b/apps/wm/main.c
index 0c30a3f..25478ff 100644
--- a/apps/wm/main.c
+++ b/apps/wm/main.c
@@ -20,7 +20,7 @@ struct client {
struct window {
u32 id;
u32 shid;
- struct context ctx;
+ struct gfx_context ctx;
struct client client;
u32 flags;
vec2 pos;
@@ -470,11 +470,12 @@ static void window_bar_mousemove(struct window *win, struct event_mouse *event,
window_move(win, vec2_sub(mouse_pos, vec2(win->ctx.size.x / 2, BAR_HEIGHT / 2)));
}
-static void window_bar_draw(struct window *win)
+static void window_bar_draw(struct window *win, char name[64])
{
if (!(win->flags & WF_BAR))
return;
+ gfx_write(&win->ctx, vec2(BAR_MARGIN, BAR_MARGIN), FONT_24, COLOR_WHITE, name);
gfx_load_image_filter(&win->ctx,
vec2(win->ctx.size.x - BAR_CLOSE_SIZE - BAR_MARGIN, BAR_MARGIN),
GFX_FILTER_INVERT, "/icons/close-" STRINGIFY(BAR_CLOSE_SIZE) ".png");
@@ -592,7 +593,7 @@ static void handle_message_new_window(struct message_new_window *msg)
{
struct window *win = window_new((struct client){ .conn = msg->header.bus.conn }, msg->pos,
vec2_add(msg->size, vec2(0, BAR_HEIGHT)), WF_BAR);
- window_bar_draw(win);
+ window_bar_draw(win, msg->name);
msg->ctx = win->ctx;
msg->off = vec2(0, BAR_HEIGHT);
diff --git a/libs/libgui/gfx.c b/libs/libgui/gfx.c
index 12c0e44..8bdea87 100644
--- a/libs/libgui/gfx.c
+++ b/libs/libgui/gfx.c
@@ -24,7 +24,7 @@
#define FONT_32_PATH "/font/spleen-16x32.psfu"
#define FONT_64_PATH "/font/spleen-32x64.psfu"
-struct font *fonts[FONT_COUNT] = { 0 };
+struct gfx_font *fonts[FONT_COUNT] = { 0 };
static void load_font(enum font_type font_type)
{
@@ -70,7 +70,7 @@ static void free_fonts(void)
}
}
-static void write_char(struct context *ctx, vec2 pos, struct font *font, u32 c, char ch)
+static void write_char(struct gfx_context *ctx, vec2 pos, struct gfx_font *font, u32 c, char ch)
{
u8 bypp = ctx->bpp >> 3;
@@ -92,7 +92,7 @@ static void write_char(struct context *ctx, vec2 pos, struct font *font, u32 c,
}
}
-struct context *gfx_new_ctx(struct context *ctx, vec2 size, u8 bpp)
+struct gfx_context *gfx_new_ctx(struct gfx_context *ctx, vec2 size, u8 bpp)
{
ctx->size = size;
ctx->bpp = bpp;
@@ -104,7 +104,7 @@ struct context *gfx_new_ctx(struct context *ctx, vec2 size, u8 bpp)
// On-demand font loading
static u8 fonts_loaded = 0;
-struct font *gfx_resolve_font(enum font_type font_type)
+struct gfx_font *gfx_resolve_font(enum font_type font_type)
{
if (!fonts_loaded) {
fonts_loaded = 1;
@@ -116,15 +116,15 @@ struct font *gfx_resolve_font(enum font_type font_type)
return fonts[font_type];
}
-void gfx_write_char(struct context *ctx, vec2 pos, enum font_type font_type, u32 c, char ch)
+void gfx_write_char(struct gfx_context *ctx, vec2 pos, enum font_type font_type, u32 c, char ch)
{
- struct font *font = gfx_resolve_font(font_type);
+ struct gfx_font *font = gfx_resolve_font(font_type);
write_char(ctx, pos, font, c, ch);
}
-void gfx_write(struct context *ctx, vec2 pos, enum font_type font_type, u32 c, const char *text)
+void gfx_write(struct gfx_context *ctx, vec2 pos, enum font_type font_type, u32 c, const char *text)
{
- struct font *font = gfx_resolve_font(font_type);
+ struct gfx_font *font = gfx_resolve_font(font_type);
u32 cnt = 0;
for (u32 i = 0; i < strlen(text); i++) {
// TODO: Should this be here?
@@ -204,7 +204,7 @@ static void gfx_image_cache_save(const char *path, struct bmp *bmp)
list_add(gfx_image_cache_list, cache);
}
-void gfx_load_image_filter(struct context *ctx, vec2 pos, enum gfx_filter filter, const char *path)
+void gfx_load_image_filter(struct gfx_context *ctx, vec2 pos, enum gfx_filter filter, const char *path)
{
// TODO: Detect image type
@@ -252,17 +252,17 @@ void gfx_load_image_filter(struct context *ctx, vec2 pos, enum gfx_filter filter
}
}
-void gfx_load_image(struct context *ctx, vec2 pos, const char *path)
+void gfx_load_image(struct gfx_context *ctx, vec2 pos, const char *path)
{
gfx_load_image_filter(ctx, pos, GFX_FILTER_NONE, path);
}
-void gfx_load_wallpaper(struct context *ctx, const char *path)
+void gfx_load_wallpaper(struct gfx_context *ctx, const char *path)
{
gfx_load_image(ctx, vec2(0, 0), path);
}
-void gfx_draw_pixel(struct context *ctx, vec2 pos1, u32 c)
+void gfx_draw_pixel(struct gfx_context *ctx, vec2 pos1, u32 c)
{
u8 bypp = ctx->bpp >> 3;
u8 *draw = &ctx->fb[pos1.x * bypp + pos1.y * ctx->pitch];
@@ -272,7 +272,7 @@ void gfx_draw_pixel(struct context *ctx, vec2 pos1, u32 c)
draw[3] = GET_ALPHA(c);
}
-void gfx_draw_rectangle(struct context *ctx, vec2 pos1, vec2 pos2, u32 c)
+void gfx_draw_rectangle(struct gfx_context *ctx, vec2 pos1, vec2 pos2, u32 c)
{
assert(pos1.x <= pos2.x && pos1.y <= pos2.y);
u8 bypp = ctx->bpp >> 3;
@@ -288,7 +288,7 @@ void gfx_draw_rectangle(struct context *ctx, vec2 pos1, vec2 pos2, u32 c)
}
}
-void gfx_draw_border(struct context *ctx, u32 width, u32 c)
+void gfx_draw_border(struct gfx_context *ctx, u32 width, u32 c)
{
if (width <= 0)
return;
@@ -311,7 +311,7 @@ void gfx_draw_border(struct context *ctx, u32 width, u32 c)
// Using Bresenham's algorithm
// TODO: Better line scaling
-void gfx_draw_line(struct context *ctx, vec2 pos1, vec2 pos2, u32 scale, u32 c)
+void gfx_draw_line(struct gfx_context *ctx, vec2 pos1, vec2 pos2, u32 scale, u32 c)
{
int dx = ABS(pos2.x - pos1.x), sx = pos1.x < pos2.x ? 1 : -1;
int dy = ABS(pos2.y - pos1.y), sy = pos1.y < pos2.y ? 1 : -1;
@@ -333,7 +333,7 @@ void gfx_draw_line(struct context *ctx, vec2 pos1, vec2 pos2, u32 scale, u32 c)
}
}
-void gfx_copy(struct context *dest, struct context *src, vec2 pos, vec2 size)
+void gfx_copy(struct gfx_context *dest, struct gfx_context *src, vec2 pos, vec2 size)
{
u8 bypp = dest->bpp >> 3;
u8 *srcfb = &src->fb[pos.x * bypp + pos.y * src->pitch];
@@ -347,7 +347,7 @@ void gfx_copy(struct context *dest, struct context *src, vec2 pos, vec2 size)
// TODO: Support alpha values other than 0x0 and 0xff (blending)
// TODO: Optimize!
-HOT void gfx_ctx_on_ctx(struct context *dest, struct context *src, vec2 pos, u8 alpha)
+HOT void gfx_ctx_on_ctx(struct gfx_context *dest, struct gfx_context *src, vec2 pos, u8 alpha)
{
// TODO: Some kind of alpha-acknowledging memcpy?
if (!alpha && src->size.x == dest->size.x && src->size.y == dest->size.y) {
@@ -377,24 +377,24 @@ HOT void gfx_ctx_on_ctx(struct context *dest, struct context *src, vec2 pos, u8
}
}
-void gfx_clear(struct context *ctx)
+void gfx_clear(struct gfx_context *ctx)
{
memset(ctx->fb, 0, ctx->bytes);
}
-void gfx_fill(struct context *ctx, u32 c)
+void gfx_fill(struct gfx_context *ctx, u32 c)
{
gfx_draw_rectangle(ctx, vec2(0, 0), vec2(ctx->size.x, ctx->size.y), c);
}
int gfx_font_height(enum font_type font_type)
{
- struct font *font = gfx_resolve_font(font_type);
+ struct gfx_font *font = gfx_resolve_font(font_type);
return font->size.y;
}
int gfx_font_width(enum font_type font_type)
{
- struct font *font = gfx_resolve_font(font_type);
+ struct gfx_font *font = gfx_resolve_font(font_type);
return font->size.x;
}
diff --git a/libs/libgui/gfx.h b/libs/libgui/gfx.h
index 7c069f4..855e060 100644
--- a/libs/libgui/gfx.h
+++ b/libs/libgui/gfx.h
@@ -48,7 +48,19 @@
#define GFX_NON_ALPHA 0
#define GFX_ALPHA 1
+/**
+ * Useful macros
+ */
+
#define GFX_CENTER_IN(a, b) (ABS((a) - (b)) / 2)
+#define GFX_RECT(pos, size) ((struct gfx_rect){ .pos = (pos), .size = (size) })
+#define GFX_IN_RECT(rect, p) \
+ ((p).x >= (rect).pos.x && (p).x < (rect).pos.x + (rect).size.x && (p).y >= (rect).pos.y && \
+ (p).y < (rect).pos.y + (rect).size.y)
+
+/**
+ * Structures
+ */
enum font_type { FONT_8, FONT_12, FONT_16, FONT_24, FONT_32, FONT_64 };
enum gfx_filter {
@@ -57,14 +69,14 @@ enum gfx_filter {
};
// Generalized font struct
-struct font {
+struct gfx_font {
void *raw;
char *chars;
vec2 size;
int char_size;
};
-struct context {
+struct gfx_context {
vec2 size;
u8 *fb;
u32 bpp;
@@ -72,16 +84,21 @@ struct context {
u32 bytes;
};
-struct context *gfx_new_ctx(struct context *ctx, vec2 size, u8 bpp) NONNULL;
+struct gfx_rect {
+ vec2 pos; // Upper left
+ vec2 size;
+};
+
+struct gfx_context *gfx_new_ctx(struct gfx_context *ctx, vec2 size, u8 bpp) NONNULL;
/**
* Text stuff
*/
-struct font *gfx_resolve_font(enum font_type font_type);
-void gfx_write_char(struct context *ctx, vec2 pos, enum font_type font_type, u32 c,
+struct gfx_font *gfx_resolve_font(enum font_type font_type);
+void gfx_write_char(struct gfx_context *ctx, vec2 pos, enum font_type font_type, u32 c,
char ch) NONNULL;
-void gfx_write(struct context *ctx, vec2 pos, enum font_type font_type, u32 c,
+void gfx_write(struct gfx_context *ctx, vec2 pos, enum font_type font_type, u32 c,
const char *text) NONNULL;
int gfx_font_height(enum font_type);
@@ -91,28 +108,28 @@ int gfx_font_width(enum font_type);
* Image loading
*/
-void gfx_load_image(struct context *ctx, vec2 pos, const char *path) NONNULL;
-void gfx_load_image_filter(struct context *ctx, vec2 pos, enum gfx_filter filter,
+void gfx_load_image(struct gfx_context *ctx, vec2 pos, const char *path) NONNULL;
+void gfx_load_image_filter(struct gfx_context *ctx, vec2 pos, enum gfx_filter filter,
const char *path) NONNULL;
-void gfx_load_wallpaper(struct context *ctx, const char *path) NONNULL;
+void gfx_load_wallpaper(struct gfx_context *ctx, const char *path) NONNULL;
/**
* Context copying
*/
-void gfx_copy(struct context *dest, struct context *src, vec2 pos, vec2 size) NONNULL;
-void gfx_ctx_on_ctx(struct context *dest, struct context *src, vec2 pos, u8 alpha) NONNULL;
+void gfx_copy(struct gfx_context *dest, struct gfx_context *src, vec2 pos, vec2 size) NONNULL;
+void gfx_ctx_on_ctx(struct gfx_context *dest, struct gfx_context *src, vec2 pos, u8 alpha) NONNULL;
/**
* Drawing functions
*/
-void gfx_draw_pixel(struct context *ctx, vec2 pos1, u32 c);
-void gfx_draw_rectangle(struct context *ctx, vec2 pos1, vec2 pos2, u32 c) NONNULL;
-void gfx_draw_line(struct context *ctx, vec2 pos1, vec2 pos2, u32 scale, u32 c);
+void gfx_draw_pixel(struct gfx_context *ctx, vec2 pos1, u32 c);
+void gfx_draw_rectangle(struct gfx_context *ctx, vec2 pos1, vec2 pos2, u32 c) NONNULL;
+void gfx_draw_line(struct gfx_context *ctx, vec2 pos1, vec2 pos2, u32 scale, u32 c);
-void gfx_clear(struct context *ctx);
-void gfx_fill(struct context *ctx, u32 c) NONNULL;
-void gfx_draw_border(struct context *ctx, u32 width, u32 c) NONNULL;
+void gfx_clear(struct gfx_context *ctx);
+void gfx_fill(struct gfx_context *ctx, u32 c) NONNULL;
+void gfx_draw_border(struct gfx_context *ctx, u32 width, u32 c) NONNULL;
#endif
diff --git a/libs/libgui/gui.c b/libs/libgui/gui.c
index b53c366..cc45bfb 100644
--- a/libs/libgui/gui.c
+++ b/libs/libgui/gui.c
@@ -11,9 +11,8 @@
struct gui_widget {
u32 id;
vec2 pos;
- /* struct context ctx; */
- struct context fg;
- struct context bg;
+ struct gfx_context fg;
+ struct gfx_context bg;
struct list *children;
struct {
@@ -26,7 +25,7 @@ struct gui_window {
u32 id;
vec2 off; // fb offset
vec2 pos;
- struct context ctx;
+ struct gfx_context ctx;
struct list *widgets;
};
@@ -392,8 +391,8 @@ static vec2 gui_offset_widget(struct gui_widget *parent, struct gui_widget *chil
static struct gui_widget *gui_new_plain_widget(vec2 pos, vec2 size, u8 bpp)
{
struct gui_widget *widget = zalloc(sizeof(*widget));
- struct context bg;
- struct context fg;
+ struct gfx_context bg;
+ struct gfx_context fg;
static u32 id = 0;
widget->id = id++;
@@ -490,8 +489,7 @@ void gui_popup(const char *text)
{
vec2 pos = vec2(200, 200);
- u32 popup;
- gui_new_custom_window(&popup, pos, vec2(POPUP_WIDTH, POPUP_HEIGHT));
+ u32 popup = gui_new_custom_window("Popup", pos, vec2(POPUP_WIDTH, POPUP_HEIGHT));
struct gui_window *win = gui_window_by_id(popup);
gui_fill(popup, gui_main_widget_id(win), GUI_LAYER_BG, COLOR_WHITE);
@@ -518,7 +516,7 @@ vec2 gui_window_size(u32 win_id)
* Window manager interfaces
*/
-void gui_new_custom_window(u32 *id, vec2 pos, vec2 size)
+u32 gui_new_custom_window(const char *name, vec2 pos, vec2 size)
{
if (!windows)
windows = list_new();
@@ -533,6 +531,7 @@ void gui_new_custom_window(u32 *id, vec2 pos, vec2 size)
struct message_new_window msg = { .header.state = MSG_NEED_ANSWER,
.pos = pos,
.size = size };
+ strlcpy(msg.name, name, sizeof(msg.name));
gui_connect_wm();
if (msg_send(GUI_NEW_WINDOW, &msg, sizeof(msg)) > 0 && msg_receive(&msg, sizeof(msg)) > 0 &&
@@ -558,16 +557,15 @@ void gui_new_custom_window(u32 *id, vec2 pos, vec2 size)
list_add(win->widgets,
gui_new_plain_widget(vec2(0, 0), win->ctx.size, win->ctx.bpp));
- *id = win->id;
- return;
+ return win->id;
}
gui_error(EINVAL);
}
-INLINE void gui_new_window(u32 *id)
+u32 gui_new_window(const char *name)
{
- gui_new_custom_window(id, vec2(0, 0), vec2(0, 0));
+ return gui_new_custom_window(name, vec2(0, 0), vec2(0, 0));
}
void gui_redraw_window_only(u32 id)
diff --git a/libs/libgui/gui.h b/libs/libgui/gui.h
index d9087f1..c3e1d10 100644
--- a/libs/libgui/gui.h
+++ b/libs/libgui/gui.h
@@ -36,8 +36,8 @@ struct gui_event_mouse {
* Window operations
*/
-void gui_new_custom_window(u32 *id, vec2 pos, vec2 size);
-void gui_new_window(u32 *id);
+u32 gui_new_custom_window(const char *name, vec2 pos, vec2 size);
+u32 gui_new_window(const char *name);
void gui_redraw_window(u32 id);
void gui_redraw_window_only(u32 id); // Without widgets
diff --git a/libs/libgui/msg.h b/libs/libgui/msg.h
index 71491c4..2684ab3 100644
--- a/libs/libgui/msg.h
+++ b/libs/libgui/msg.h
@@ -34,12 +34,13 @@ struct message_ping_window {
struct message_new_window {
struct message_header header;
+ char name[64];
u32 id;
u32 shid;
vec2 off;
vec2 size;
vec2 pos;
- struct context ctx;
+ struct gfx_context ctx;
};
struct message_redraw_window {
diff --git a/libs/libgui/psf.c b/libs/libgui/psf.c
index 95678ed..5a6b1bb 100644
--- a/libs/libgui/psf.c
+++ b/libs/libgui/psf.c
@@ -23,7 +23,7 @@ static int psf_verify(char *data)
return 0;
}
-struct font *psf_parse(char *data)
+struct gfx_font *psf_parse(char *data)
{
int version = psf_verify(data);
@@ -47,7 +47,7 @@ struct font *psf_parse(char *data)
return NULL;
}
- struct font *font = malloc(sizeof(*font));
+ struct gfx_font *font = malloc(sizeof(*font));
font->raw = data;
font->chars = chars;
font->size.x = width;
diff --git a/libs/libgui/psf.h b/libs/libgui/psf.h
index 4d63118..8298441 100644
--- a/libs/libgui/psf.h
+++ b/libs/libgui/psf.h
@@ -43,6 +43,6 @@ struct psf2_header {
u32 width;
};
-struct font *psf_parse(char *data) NONNULL;
+struct gfx_font *psf_parse(char *data) NONNULL;
#endif