Add screen.[ch] and unit tests
This commit is contained in:
101
src/widget.c
101
src/widget.c
@ -3,11 +3,6 @@
|
||||
#include "frozen/frozen.h"
|
||||
#include "common/queue.h"
|
||||
|
||||
struct widget_list_t {
|
||||
struct widget_t *widget;
|
||||
SLIST_ENTRY(widget_list_t) entries;
|
||||
};
|
||||
|
||||
SLIST_HEAD(widget_list, widget_list_t) s_widgets;
|
||||
|
||||
static void widget_event_timer(void *arg) {
|
||||
@ -18,46 +13,38 @@ static void widget_event_timer(void *arg) {
|
||||
widget->handler(EV_WIDGET_TIMER, widget, NULL);
|
||||
}
|
||||
|
||||
static void widget_destroy(struct widget_t *widget) {
|
||||
if (!widget)
|
||||
void widget_destroy(struct widget_t **widget) {
|
||||
if (!*widget)
|
||||
return;
|
||||
if (widget->handler)
|
||||
widget->handler(EV_WIDGET_DESTROY, widget, NULL);
|
||||
if ((*widget)->handler)
|
||||
(*widget)->handler(EV_WIDGET_DESTROY, *widget, NULL);
|
||||
|
||||
if (widget->timer_msec)
|
||||
mgos_clear_timer(widget->_timer_id);
|
||||
if (widget->user_data)
|
||||
free(widget->user_data);
|
||||
free(widget);
|
||||
if ((*widget)->timer_msec)
|
||||
mgos_clear_timer((*widget)->_timer_id);
|
||||
if ((*widget)->user_data)
|
||||
free((*widget)->user_data);
|
||||
free(*widget);
|
||||
*widget=NULL;
|
||||
}
|
||||
|
||||
struct widget_t *widget_add(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint32_t timer_msec, widget_event_fn handler, void *user_data) {
|
||||
struct widget_t *widget;
|
||||
struct widget_list_t *wl;
|
||||
|
||||
widget = (struct widget_t *) calloc(1, sizeof(*widget));
|
||||
if (!widget)
|
||||
return NULL;
|
||||
widget->x=x;
|
||||
widget->y=y;
|
||||
widget->w=w;
|
||||
widget->h=h;
|
||||
widget->timer_msec=timer_msec;
|
||||
widget->handler=handler;
|
||||
widget->user_data=user_data;
|
||||
if (timer_msec > 0)
|
||||
widget->_timer_id = mgos_set_timer(timer_msec, MGOS_TIMER_REPEAT, widget_event_timer, widget);
|
||||
|
||||
wl = (struct widget_list_t *) calloc(1, sizeof(*wl));
|
||||
if (!wl) {
|
||||
widget_destroy(widget);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
widget = widget_create(x, y, w, h, timer_msec, handler, user_data);
|
||||
if (!widget) {
|
||||
free(wl);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
wl->widget = widget;
|
||||
SLIST_INSERT_HEAD(&s_widgets, wl, entries);
|
||||
|
||||
if (handler)
|
||||
handler(EV_WIDGET_CREATE, widget, NULL);
|
||||
return widget;
|
||||
}
|
||||
|
||||
@ -82,7 +69,7 @@ void widget_remove(struct widget_t *widget) {
|
||||
SLIST_FOREACH(wl, &s_widgets, entries) {
|
||||
if (wl->widget == widget) {
|
||||
SLIST_REMOVE(&s_widgets, wl, widget_list_t, entries);
|
||||
widget_destroy(widget);
|
||||
widget_destroy(&widget);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -119,3 +106,55 @@ exit:
|
||||
(void) handler;
|
||||
(void) user_data;
|
||||
}
|
||||
|
||||
struct widget_t *widget_create(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint32_t timer_msec, widget_event_fn handler, void *user_data) {
|
||||
struct widget_t *widget;
|
||||
|
||||
widget = (struct widget_t *) calloc(1, sizeof(*widget));
|
||||
if (!widget)
|
||||
return NULL;
|
||||
widget->x=x;
|
||||
widget->y=y;
|
||||
widget->w=w;
|
||||
widget->h=h;
|
||||
widget->timer_msec=timer_msec;
|
||||
widget->handler=handler;
|
||||
widget->user_data=user_data;
|
||||
if (timer_msec > 0)
|
||||
widget->_timer_id = mgos_set_timer(timer_msec, MGOS_TIMER_REPEAT, widget_event_timer, widget);
|
||||
|
||||
if (handler)
|
||||
handler(EV_WIDGET_CREATE, widget, NULL);
|
||||
|
||||
return widget;
|
||||
}
|
||||
|
||||
struct widget_t *widget_create_from_json(const char *json) {
|
||||
struct widget_t *widget=NULL;
|
||||
int x, y, w, h;
|
||||
int type = 0;
|
||||
char *label = NULL;
|
||||
char *icon = NULL;
|
||||
|
||||
if (json_scanf(json, strlen(json), "{x:%d,y:%d,w:%d,h:%d}", &x, &y, &w, &h) != 4) {
|
||||
LOG(LL_ERROR, ("Incomplete JSON: require 'x', 'y', 'w', 'h' fields"));
|
||||
return NULL;
|
||||
}
|
||||
json_scanf(json, strlen(json), "{type:%d,label:%Q,icon:%Q}", &type, &label, &icon);
|
||||
widget = widget_create(x, y, w, h, 0, NULL, NULL);
|
||||
return widget;
|
||||
}
|
||||
|
||||
struct widget_t *widget_create_from_file(const char *fn) {
|
||||
char *json;
|
||||
struct widget_t *widget=NULL;
|
||||
|
||||
if (!(json = json_fread(fn))) {
|
||||
LOG(LL_ERROR, ("%s: Could not json_fread()", fn));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
widget = widget_create_from_json(json);
|
||||
free(json);
|
||||
return widget;
|
||||
}
|
||||
|
Reference in New Issue
Block a user