aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi.dcr@tutanota.com>2022-07-02 04:08:31 +0200
committerXavier Del Campo Romero <xavi.dcr@tutanota.com>2022-07-02 04:08:35 +0200
commitb0eb562b9b78d37493d28b413aca45542e8cd0ba (patch)
tree1e3470a5b7dc71a7831ed91ee1493a6dab0ac45c
parenta71ce379297384df42e720641d39a4d2ade6bbd9 (diff)
gui: reuse callback data
There is no need to allocate memory for these callbacks for each single GUI element. Instead, a single, statically-allocated instance can be shared among all GUI elements of a given type.
-rw-r--r--src/gui/inc/gui.h14
-rw-r--r--src/gui/src/bar.c7
-rw-r--r--src/gui/src/button.c11
-rw-r--r--src/gui/src/container.c13
-rw-r--r--src/gui/src/gui.c16
-rw-r--r--src/gui/src/label.c9
-rw-r--r--src/gui/src/progress_bar.c7
-rw-r--r--src/gui/src/rounded_rect.c9
8 files changed, 60 insertions, 26 deletions
diff --git a/src/gui/inc/gui.h b/src/gui/inc/gui.h
index 7198af0..18ed37b 100644
--- a/src/gui/inc/gui.h
+++ b/src/gui/inc/gui.h
@@ -12,11 +12,15 @@ extern "C"
struct gui_common
{
- void (*add_child)(struct gui_common *parent, struct gui_common *child);
- int (*update)(struct gui_common *, const union peripheral *,
- const struct camera *);
- int (*render)(const struct gui_common *);
- void (*get_dim)(const struct gui_common *, short *w, short *h);
+ const struct gui_common_cb
+ {
+ void (*add_child)(struct gui_common *parent, struct gui_common *child);
+ int (*update)(struct gui_common *, const union peripheral *,
+ const struct camera *);
+ int (*render)(const struct gui_common *);
+ void (*get_dim)(const struct gui_common *, short *w, short *h);
+ } *cb;
+
short x, y;
bool hcentered, vcentered;
struct gui_common *parent, *child, *sibling;
diff --git a/src/gui/src/bar.c b/src/gui/src/bar.c
index 708e4d1..e622eba 100644
--- a/src/gui/src/bar.c
+++ b/src/gui/src/bar.c
@@ -74,11 +74,16 @@ static int render(const struct gui_common *const g)
void gui_bar_init(struct gui_bar *const b)
{
+ static const struct gui_common_cb cb =
+ {
+ .render = render
+ };
+
*b = (const struct gui_bar)
{
.common =
{
- .render = render
+ .cb = &cb
}
};
}
diff --git a/src/gui/src/button.c b/src/gui/src/button.c
index 44aeac6..c1a07ac 100644
--- a/src/gui/src/button.c
+++ b/src/gui/src/button.c
@@ -164,13 +164,18 @@ static void get_dim(const struct gui_common *const g,
void gui_button_init(struct gui_button *const b)
{
+ static const struct gui_common_cb cb =
+ {
+ .get_dim = get_dim,
+ .update = update,
+ .render = render
+ };
+
*b = (const struct gui_button)
{
.common =
{
- .get_dim = get_dim,
- .update = update,
- .render = render
+ .cb = &cb
}
};
diff --git a/src/gui/src/container.c b/src/gui/src/container.c
index 6d586f1..1021b95 100644
--- a/src/gui/src/container.c
+++ b/src/gui/src/container.c
@@ -4,13 +4,13 @@
static void add_child(struct gui_common *const parent,
struct gui_common *const child)
{
- if (!child->get_dim)
+ if (!child->cb || !child->cb->get_dim)
return;
struct gui_container *const c = (struct gui_container *)parent;
short w, h;
- child->get_dim(child, &w, &h);
+ child->cb->get_dim(child, &w, &h);
switch (c->mode)
{
@@ -45,12 +45,17 @@ static void get_dim(const struct gui_common *const g,
void gui_container_init(struct gui_container *const c)
{
+ static const struct gui_common_cb cb =
+ {
+ .add_child = add_child,
+ .get_dim = get_dim
+ };
+
*c = (const struct gui_container)
{
.common =
{
- .add_child = add_child,
- .get_dim = get_dim
+ .cb = &cb
}
};
}
diff --git a/src/gui/src/gui.c b/src/gui/src/gui.c
index 329bc2d..df00687 100644
--- a/src/gui/src/gui.c
+++ b/src/gui/src/gui.c
@@ -3,14 +3,14 @@
static void get_centered(const struct gui_common *const g,
short *const x, short *const y)
{
- if (g->get_dim)
+ if (g->cb && g->cb->get_dim)
{
short w, h, pw = 0, ph = 0;
if (g->parent)
{
- if (g->parent->get_dim)
- g->parent->get_dim(g->parent, &pw, &ph);
+ if (g->parent->cb && g->parent->cb->get_dim)
+ g->parent->cb->get_dim(g->parent, &pw, &ph);
}
else
{
@@ -18,7 +18,7 @@ static void get_centered(const struct gui_common *const g,
ph = screen_h;
}
- g->get_dim(g, &w, &h);
+ g->cb->get_dim(g, &w, &h);
if (g->hcentered)
*x += (pw - w) / 2;
@@ -72,14 +72,14 @@ void gui_add_child(struct gui_common *const p,
c->parent = p;
- if (p->add_child)
- p->add_child(p, c);
+ if (p->cb && p->cb->add_child)
+ p->cb->add_child(p, c);
}
int gui_update(struct gui_common *const g, const union peripheral *const p,
const struct camera *const c)
{
- if (g->update && g->update(g, p, c))
+ if (g->cb && g->cb->update && g->cb->update(g, p, c))
return -1;
if (g->child && gui_update(g->child, p, c))
@@ -94,7 +94,7 @@ int gui_update(struct gui_common *const g, const union peripheral *const p,
int gui_render(const struct gui_common *const g)
{
- if (g->render && g->render(g))
+ if (g->cb && g->cb->render && g->cb->render(g))
return -1;
if (g->child && gui_render(g->child))
diff --git a/src/gui/src/label.c b/src/gui/src/label.c
index 51bacd6..9028184 100644
--- a/src/gui/src/label.c
+++ b/src/gui/src/label.c
@@ -23,12 +23,17 @@ static void get_dim(const struct gui_common *const g,
void gui_label_init(struct gui_label *const l)
{
+ static const struct gui_common_cb cb =
+ {
+ .get_dim = get_dim,
+ .render = render
+ };
+
*l = (const struct gui_label)
{
.common =
{
- .get_dim = get_dim,
- .render = render
+ .cb = &cb
}
};
}
diff --git a/src/gui/src/progress_bar.c b/src/gui/src/progress_bar.c
index b836b4d..783549a 100644
--- a/src/gui/src/progress_bar.c
+++ b/src/gui/src/progress_bar.c
@@ -83,11 +83,16 @@ static int render(const struct gui_common *const g)
void gui_progress_bar_init(struct gui_progress_bar *const pb)
{
+ static const struct gui_common_cb cb =
+ {
+ .render = render
+ };
+
*pb = (const struct gui_progress_bar)
{
.common =
{
- .render = render
+ .cb = &cb
}
};
}
diff --git a/src/gui/src/rounded_rect.c b/src/gui/src/rounded_rect.c
index 857d273..cd2bbff 100644
--- a/src/gui/src/rounded_rect.c
+++ b/src/gui/src/rounded_rect.c
@@ -224,12 +224,17 @@ static void get_dim(const struct gui_common *const g, short *const w,
void gui_rounded_rect_init(struct gui_rounded_rect *const r)
{
+ static const struct gui_common_cb cb =
+ {
+ .get_dim = get_dim,
+ .render = render
+ };
+
*r = (const struct gui_rounded_rect)
{
.common =
{
- .get_dim = get_dim,
- .render = render
+ .cb = &cb
}
};
}