From 7b75cdb768ee3e10f05c154c6169490f42589869 Mon Sep 17 00:00:00 2001 From: hippoz <10706925-hippoz@users.noreply.gitlab.com> Date: Thu, 13 Apr 2023 03:47:38 +0300 Subject: [PATCH] fix animations target and compiler warnings --- Makefile | 8 ++++---- src/background-node.c | 2 +- src/background-node.h | 2 +- src/box-layout-node.h | 2 +- src/color.h | 2 +- src/dispatcher-node.h | 2 +- src/main.c | 17 +++++++++++------ src/node.h | 2 +- src/prof.c | 2 +- src/rect.h | 2 +- src/scrollable-node.h | 2 +- src/text-node.c | 3 --- src/text-node.h | 2 +- src/timeutil.h | 2 +- src/window.c | 20 +++++++++++--------- src/window.h | 8 ++++---- 16 files changed, 41 insertions(+), 37 deletions(-) diff --git a/Makefile b/Makefile index d12e70a..a338d3d 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ -CC?=cc -CFLAGS_LIBS:=`pkg-config --cflags --libs xcb cairo pangocairo` -lm -CFLAGS:=$(CFLAGS) -pipe -Wall -Wextra -Wshadow -std=c99 -pedantic $(CFLAGS_LIBS) +CC=clang +LIBS:=`pkg-config --libs xcb cairo pangocairo` -lm +CFLAGS:=$(CFLAGS) -pipe -Wall -Wextra -Wshadow -std=c99 -pedantic `pkg-config --cflags xcb cairo pangocairo` BUILD=build OBJ=$(BUILD) @@ -20,7 +20,7 @@ release: CFLAGS+=-O2 -flto=auto -DNDEBUG release: clean $(BUILD) $(BIN) $(BIN): $(OBJS) - $(CC) $(CFLAGS) $(OBJS) -o $@ + $(CC) $(CFLAGS) $(LIBS) $(OBJS) -o $@ -include $(DEPS) diff --git a/src/background-node.c b/src/background-node.c index d0e2a62..5be2ec2 100644 --- a/src/background-node.c +++ b/src/background-node.c @@ -91,4 +91,4 @@ UIBackgroundNode *background_node_new(UINode *parent, UIRGBA normal, double bord n->border_radius = border_radius; node_attach(parent, (UINode*)n); return n; -} \ No newline at end of file +} diff --git a/src/background-node.h b/src/background-node.h index 6bb7fb3..6bd7638 100644 --- a/src/background-node.h +++ b/src/background-node.h @@ -15,4 +15,4 @@ int background_node_handle(UINode *node, enum UIEvent ev, size_t d, void *p); UIBackgroundNode *state_background_node_new(UINode *parent, UIRGBA normal, UIRGBA hovered, UIRGBA pressed, double border_radius); UIBackgroundNode *background_node_new(UINode *parent, UIRGBA normal, double border_radius); -#endif // _UI__BACKGROUND_NODE_H \ No newline at end of file +#endif // _UI__BACKGROUND_NODE_H diff --git a/src/box-layout-node.h b/src/box-layout-node.h index aaf758b..be7327f 100644 --- a/src/box-layout-node.h +++ b/src/box-layout-node.h @@ -12,4 +12,4 @@ typedef struct UIBoxLayoutNode { UIBoxLayoutNode *box_layout_new(UINode *parent, enum UIDirection direction); int box_layout_handle(UINode *component, enum UIEvent ev, size_t d, void *p); -#endif // _UI__BOX_LAYOUT_NODE_H \ No newline at end of file +#endif // _UI__BOX_LAYOUT_NODE_H diff --git a/src/color.h b/src/color.h index 53b9f95..3fdcb4f 100644 --- a/src/color.h +++ b/src/color.h @@ -8,4 +8,4 @@ typedef struct UIRGBA { #define UI_HEX_TO_COLOR_NORMAL(hex) ((((hex) & 0xFF0000) >> 16) / 255.0), ((((hex) & 0xFF00) >> 8) / 255.0), (((hex) & 0xFF) / 255.0) #define UI_HEX_TO_RGBA(hex) (UIRGBA){UI_HEX_TO_COLOR_NORMAL(hex), 1.0} -#endif // _UI__COLOR_H \ No newline at end of file +#endif // _UI__COLOR_H diff --git a/src/dispatcher-node.h b/src/dispatcher-node.h index 8ab95f2..85d284e 100644 --- a/src/dispatcher-node.h +++ b/src/dispatcher-node.h @@ -14,4 +14,4 @@ typedef struct UIDispatcherNode { UIDispatcherNode *dispatcher_new(UINode *parent, enum UIEvent node_event_type, int event_type, void *data, int (*handle)(struct UINode *node, void *data, int event_type, size_t d, void *p)); int dispatcher_handle(UINode *node, enum UIEvent ev, size_t d, void *p); -#endif // _UI__DISPATCHER_NODE_H \ No newline at end of file +#endif // _UI__DISPATCHER_NODE_H diff --git a/src/main.c b/src/main.c index aa7905a..a6c6e3a 100644 --- a/src/main.c +++ b/src/main.c @@ -22,9 +22,9 @@ typedef struct AppState { int count; char counter_text[12]; - uint64_t sidebar_animate_duration_ms; + int64_t sidebar_animate_duration_ms; - uint64_t sidebar_animate_time_ms; + int64_t sidebar_animate_time_ms; double sidebar_animate_target_w; double sidebar_animate_start_w; } AppState; @@ -60,7 +60,7 @@ int app_handle(struct UINode *node, void *data, int event_type, size_t d, void * /* sidebar */ { UINode *sidebar = node_new(node, "sidebar"); - sidebar->width_policy = UI_SIZE_POLICY_STATIC; + sidebar->width_policy = UI_SIZE_POLICY_FIXED; sidebar->height_policy = UI_SIZE_POLICY_GROW; sidebar->rect.w = 200; dispatcher_new(sidebar, UI_EVENT_TIMER_END, SIDEBAR_ANIAMTE, state, app_handle); @@ -108,19 +108,24 @@ int app_handle(struct UINode *node, void *data, int event_type, size_t d, void * break; } case SIDEBAR_ANIAMTE: { - uint64_t dt = *(uint64_t*)p; + int64_t dt = *(int64_t*)p; state->sidebar_animate_time_ms += dt; state->sidebar_node->rect.w = lerp(state->sidebar_animate_start_w, state->sidebar_animate_target_w, (double)state->sidebar_animate_time_ms / (double)state->sidebar_animate_duration_ms); if (state->sidebar_node->rect.w < 0) { state->sidebar_node->rect.w = 0; } - node_request_relayout(state->sidebar_node->parent); if (state->sidebar_animate_time_ms >= state->sidebar_animate_duration_ms) { + state->sidebar_node->rect.w = state->sidebar_animate_target_w; + } + node_request_relayout(state->sidebar_node); + if (state->sidebar_node->rect.w == state->sidebar_animate_target_w) { state->sidebar_animate_time_ms = 0; return 0; } + window_sched_timer(node->window, state->sidebar_node, 10); + return 1; } case INCREMENT_COUNT: { @@ -142,7 +147,7 @@ int app_handle(struct UINode *node, void *data, int event_type, size_t d, void * return 0; } -int main() +int main(void) { UIWindow *window = window_new(800, 600); if (!window) { diff --git a/src/node.h b/src/node.h index 315db8e..a9178e8 100644 --- a/src/node.h +++ b/src/node.h @@ -65,4 +65,4 @@ UINode *node_new(UINode *parent, const char *text); void node_dump(UINode *node, int depth); void node_request_relayout(UINode *node); -#endif // _UI__NODE_H \ No newline at end of file +#endif // _UI__NODE_H diff --git a/src/prof.c b/src/prof.c index 5dfe51e..552f1f8 100644 --- a/src/prof.c +++ b/src/prof.c @@ -158,4 +158,4 @@ void dump_summary(FILE *stream) #define end_clock(...) #define dump_summary(...) #define clear_summary(...) -#endif \ No newline at end of file +#endif diff --git a/src/rect.h b/src/rect.h index 4ecb8ff..dfd71ba 100644 --- a/src/rect.h +++ b/src/rect.h @@ -13,4 +13,4 @@ bool ui_rect_overlap_rect(UIRect *a, UIRect *b); UIRect ui_rect_united(UIRect *a, UIRect *b); bool ui_rect_equals(UIRect *a, UIRect *b); -#endif // _UI__RECT_H \ No newline at end of file +#endif // _UI__RECT_H diff --git a/src/scrollable-node.h b/src/scrollable-node.h index ca157c7..5a2cdcb 100644 --- a/src/scrollable-node.h +++ b/src/scrollable-node.h @@ -12,4 +12,4 @@ typedef struct UIScrollableNode { UIScrollableNode *scrollable_new(UINode *parent, UINode *target); int scrollable_handle(UINode *node, enum UIEvent ev, size_t d, void *p); -#endif // _UI__SCROLL_CONTAINER_NODE_H \ No newline at end of file +#endif // _UI__SCROLL_CONTAINER_NODE_H diff --git a/src/text-node.c b/src/text-node.c index c5c8554..0e46773 100644 --- a/src/text-node.c +++ b/src/text-node.c @@ -58,7 +58,6 @@ int text_node_handle(UINode *node, enum UIEvent ev, size_t d, void *p) pango_layout_get_size(n->layout, &w, NULL); *(double*)p = pango_units_to_double(w); return 1; - break; } case UI_EVENT_GET_HEIGHT: { if (!n->layout) { @@ -68,7 +67,6 @@ int text_node_handle(UINode *node, enum UIEvent ev, size_t d, void *p) pango_layout_get_size(n->layout, NULL, &h); *(double*)p = pango_units_to_double(h); return 1; - break; } case UI_EVENT_RELAYOUT: { if (!n->layout) { @@ -91,4 +89,3 @@ int text_node_handle(UINode *node, enum UIEvent ev, size_t d, void *p) } return 0; } - diff --git a/src/text-node.h b/src/text-node.h index 9f0ea67..b404693 100644 --- a/src/text-node.h +++ b/src/text-node.h @@ -18,4 +18,4 @@ typedef struct UITextNode { int text_node_handle(UINode *node, enum UIEvent ev, size_t d, void *p); UITextNode *text_node_new(UINode *parent, PangoFontDescription *desc, UIRGBA color, char *text); -#endif // _UI__TEXT_NODE_H \ No newline at end of file +#endif // _UI__TEXT_NODE_H diff --git a/src/timeutil.h b/src/timeutil.h index d4bce54..aa15897 100644 --- a/src/timeutil.h +++ b/src/timeutil.h @@ -5,4 +5,4 @@ int64_t time_current_ms(void); -#endif // _UI__TIMEUTIL_H \ No newline at end of file +#endif // _UI__TIMEUTIL_H diff --git a/src/window.c b/src/window.c index d38fefb..507fea3 100644 --- a/src/window.c +++ b/src/window.c @@ -10,6 +10,8 @@ #include #include +void _window_process_xcb_event(UIWindow *window, xcb_generic_event_t *event); + int window_invalidate_node(UIWindow *window, UINode *node) { if (node->flags & UI_NODE_COMPONENT) { @@ -285,14 +287,14 @@ int window_turn(UIWindow *window) fds[0].fd = xcb_get_file_descriptor(window->_xcb_connection); fds[0].events = POLLIN; - uint64_t frame_peak_ms = 0; - uint64_t frame_peak_last_measurement_ms = 0; + int64_t frame_peak_ms = 0; + int64_t frame_peak_last_measurement_ms = 0; for (;;) { int64_t poll_timeout; /* compute `poll_timeout` based on active timers */ { - uint64_t now = time_current_ms(); + int64_t now = time_current_ms(); int64_t lowest = 0; bool has_timer = false; for (int i = 0; i < UI_WINDOW_MAX_TIMERS; i++) { @@ -325,7 +327,7 @@ int window_turn(UIWindow *window) clear_summary(); - uint64_t frame_start_ms = time_current_ms(); + int64_t frame_start_ms = time_current_ms(); for (int i = 0; i < UI_WINDOW_MAX_POLL; i++) { if (fds[i].revents & POLLNVAL) { fprintf(stderr, "err: window_turn(): poll got POLLNVAL\n"); @@ -354,10 +356,10 @@ int window_turn(UIWindow *window) for (int j = 0; j < UI_WINDOW_MAX_TIMERS; j++) { UIWindowTimer *timer = &window->timers[j]; if (timer->present) { - uint64_t now = time_current_ms(); + int64_t now = time_current_ms(); int64_t distance = (timer->started_at_ms + timer->duration_ms) - now; if (distance <= 0) { - uint64_t dt = time_current_ms() - timer->started_at_ms; + int64_t dt = time_current_ms() - timer->started_at_ms; UINode *target = timer->target; timer->present = false; timer->started_at_ms = 0; @@ -381,8 +383,8 @@ int window_turn(UIWindow *window) has_looped_once = true; - uint64_t frame_end_ms = time_current_ms(); - uint64_t frame_delta_ms = frame_end_ms - frame_start_ms; + int64_t frame_end_ms = time_current_ms(); + int64_t frame_delta_ms = frame_end_ms - frame_start_ms; if (frame_delta_ms > frame_peak_ms || frame_end_ms - frame_peak_last_measurement_ms >= 3000) { frame_peak_last_measurement_ms = frame_end_ms; frame_peak_ms = frame_delta_ms; @@ -394,7 +396,7 @@ int window_turn(UIWindow *window) return 0; } -UIWindowTimer *window_sched_timer(UIWindow *window, UINode *target, uint64_t duration_ms) +UIWindowTimer *window_sched_timer(UIWindow *window, UINode *target, int64_t duration_ms) { for (int i = 0; i < UI_WINDOW_MAX_TIMERS; i++) { if (!window->timers[i].present) { diff --git a/src/window.h b/src/window.h index cc0cebd..8b07038 100644 --- a/src/window.h +++ b/src/window.h @@ -13,8 +13,8 @@ typedef struct UIWindowTimer { bool present; - uint64_t started_at_ms; - uint64_t duration_ms; + int64_t started_at_ms; + int64_t duration_ms; UINode *target; // TODO: what if the node gets deleted while the timer is still running? } UIWindowTimer; @@ -38,7 +38,7 @@ UIWindow *window_new(int width, int height); bool window_flush_invalidated(UIWindow *window); int window_attach_root(UIWindow *window, UINode *root); int window_turn(UIWindow *window); -UIWindowTimer *window_sched_timer(UIWindow *window, UINode *target, uint64_t duration_ms); +UIWindowTimer *window_sched_timer(UIWindow *window, UINode *target, int64_t duration_ms); -#endif // _UI__WINDOW_H \ No newline at end of file +#endif // _UI__WINDOW_H