From d2f3c14d1f208a11242949e767e4ff1b4d838134 Mon Sep 17 00:00:00 2001 From: Federico Angelilli Date: Tue, 9 Jul 2024 20:11:00 +0200 Subject: Various refactor --- src/block.c | 23 +++++++++++++++++++ src/block.h | 2 ++ src/comet.c | 71 ++++++++++++++++++++++++++++------------------------------- src/display.c | 20 +++++++++-------- src/event.c | 2 +- src/layout.c | 33 +++++++++++++++------------ src/layout.h | 4 ++-- src/window.c | 24 +++++++++----------- 8 files changed, 102 insertions(+), 77 deletions(-) create mode 100644 src/block.c diff --git a/src/block.c b/src/block.c new file mode 100644 index 0000000..442e753 --- /dev/null +++ b/src/block.c @@ -0,0 +1,23 @@ +#include "block.h" +#include "util.h" +#include "any_log.h" + +void block_update(block_t *block) +{ + if (block->update_cb != NULL) { + struct timespec now, diff; + timespec_get(&now, TIME_UTC); + diff = timespec_diff(block->update_last, now); + + if (timespec_greater(diff, block->update_interval)) { + block->update_cb(block); + block->update_last = now; + log_debug("Updated block"); + } + } + + if (block->type == BLOCK_GROUP) { + for (int i = 0; i < block->group.n_children; i++) + block_update(&block->group.children[i]); + } +} diff --git a/src/block.h b/src/block.h index 33fc797..8a91bf3 100644 --- a/src/block.h +++ b/src/block.h @@ -67,4 +67,6 @@ struct block { }; }; +void block_update(block_t *block); + #endif diff --git a/src/comet.c b/src/comet.c index 64d24c0..eb401b9 100644 --- a/src/comet.c +++ b/src/comet.c @@ -1,6 +1,6 @@ #include #include -#include +#include #include "window.h" #include "layout.h" @@ -10,26 +10,6 @@ #define ANY_LOG_IMPLEMENT #include "any_log.h" -void update(block_t *block) -{ - if (block->update_cb != NULL) { - struct timespec now, diff; - timespec_get(&now, TIME_UTC); - diff = timespec_diff(block->update_last, now); - - if (timespec_greater(diff, block->update_interval)) { - log_debug("Updating block"); - block->update_cb(block); - block->update_last = now; - } - } - - if (block->type == BLOCK_GROUP) { - for (int i = 0; i < block->group.n_children; i++) - update(&block->group.children[i]); - } -} - cairo_surface_t *render(layout_t *layout, layout_info_t info) { cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, info.width, info.height); @@ -41,9 +21,9 @@ cairo_surface_t *render(layout_t *layout, layout_info_t info) cairo_set_fill_rule(cr, CAIRO_FILL_RULE_EVEN_ODD); cairo_set_operator(cr, CAIRO_OPERATOR_OVER); - color_t color = layout->block->color; - cairo_set_source_rgba(cr, color.r, color.g, color.b, color.a); - cairo_paint(cr); + //color_t color = layout->block->color; + //cairo_set_source_rgba(cr, color.r, color.g, color.b, color.a); + //cairo_paint(cr); layout_render(layout, cr); log_trace("Rendered layouts"); @@ -93,8 +73,8 @@ int main(int argc, char **argv) .line_width = 0, .x_padding = 0, .y_padding = 0, - .min_width = 0, - .max_width = 0, + .min_width = 100, + .max_width = 100, .update_interval = 0, .update_last = { 0 }, .update_cb = NULL, @@ -114,9 +94,9 @@ int main(int argc, char **argv) .line_width = 0, .x_padding = 0, .y_padding = 0, - .min_width = 0, + .min_width = 1000, .max_width = 0, - .update_interval = 0, + .update_interval = { 0 }, .update_last = { 0 }, .update_cb = NULL, .event_cb = NULL, @@ -128,10 +108,11 @@ int main(int argc, char **argv) }, }; - int width = 1200; - int height = 200; - int x_padding = 20; - int y_padding = 40; + int x_padding = 10; + int y_padding = 8; + + int height = 30; + int width = display.screen_size->width - 2 * x_padding; layout_info_t info = { .fontdesc = pango_font_description_from_string("Hack 13"), @@ -144,17 +125,33 @@ int main(int argc, char **argv) window_resize(&window, width, height); window_move(&window, x_padding, y_padding); + + double freq = 1.0 / 60.0 + 0.5e-9; + struct timespec rate; + rate.tv_sec = (long)freq; + rate.tv_nsec = (freq - rate.tv_sec) * 1000000000ul; + while (true) { - update(&top); + struct timespec start; + timespec_get(&start, TIME_UTC); - layout_t *layout = layout_create(&top, info); + block_update(&top); - event_dispatch(&display, layout); + layout_t layout; + layout_init(&layout, &top, info); - cairo_surface_t *surface = render(layout, info); + event_dispatch(&display, &layout); + + cairo_surface_t *surface = render(&layout, info); window_present(&window, surface, width, height); cairo_surface_destroy(surface); - layout_free(layout); + layout_free(&layout); + + struct timespec end; + timespec_get(&end, TIME_UTC); + + struct timespec delta = timespec_diff(timespec_diff(end, start), rate); + nanosleep(&delta, NULL); } window_close(&window); diff --git a/src/display.c b/src/display.c index a34e609..0e2754a 100644 --- a/src/display.c +++ b/src/display.c @@ -14,7 +14,7 @@ void display_init(display_t *display) display->connection = xcb_connect(NULL, &preferred_screen); assert(display->connection != NULL && !xcb_connection_has_error(display->connection)); - log_value_debug("Xcb connection established", + log_value_debug("Connected to xcb" "i:preferred_screen", preferred_screen); xcb_screen_iterator_t iter = xcb_setup_roots_iterator(xcb_get_setup(display->connection)); @@ -35,7 +35,7 @@ void display_init(display_t *display) &error); assert(error == NULL); - log_value_debug("RandR loaded", + log_value_trace("Found RandR extension", "i:major", randr_version->major_version, "i:minor", randr_version->minor_version); @@ -48,7 +48,9 @@ void display_init(display_t *display) display->screen_size = xcb_randr_get_screen_info_sizes(display->info_reply); assert(display->screen_size != NULL); - log_debug("Display size [width=%d, height=%d]", display->screen_size->width, display->screen_size->height); + log_value_info("Display size", + "i:width", display->screen_size->width, + "i:height", display->screen_size->height); xcb_randr_select_input(display->connection, display->screen->root, @@ -56,13 +58,13 @@ void display_init(display_t *display) XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE | XCB_RANDR_NOTIFY_MASK_OUTPUT_PROPERTY); - log_debug("Searching 32 bit visual"); + log_trace("Searching 32 bit visual"); display->visual_type = xcb_aux_find_visual_by_attrs(display->screen, XCB_VISUAL_CLASS_TRUE_COLOR, 32); display->screen_depth = 32; if (display->visual_type == NULL) { // NOTE: 24 bit visuals don't have the alpha channel for transparency - log_debug("Falling back to 24 bit visual"); + log_trace("Falling back to 24 bit visual"); display->visual_type = xcb_aux_find_visual_by_attrs(display->screen, XCB_VISUAL_CLASS_TRUE_COLOR, 24); display->screen_depth = 24; } @@ -73,7 +75,7 @@ void display_init(display_t *display) assert(xcb_ewmh_init_atoms_replies(&display->ewmh, ewmh_cookie, &error)); assert(error == NULL); - log_debug("Xcb EWMH initialized"); + log_trace("Initialized xcb_ewmh"); display->database = xcb_xrm_database_from_default(display->connection); assert(display->database != NULL); @@ -81,7 +83,7 @@ void display_init(display_t *display) display_update_scale(display); assert(xcb_errors_context_new(display->connection, &display->errors) == 0); - log_debug("Xcb errors loaded"); + log_trace("Initialized xcb_errors"); xcb_flush(display->connection); } @@ -89,7 +91,7 @@ void display_init(display_t *display) bool display_query_xrm(display_t *display, const char *resource, char **value) { bool found = xcb_xrm_resource_get_string(display->database, resource, NULL, value) >= 0; - log_value_debug("Xrm query", + log_value_trace("Xrm query", "b:found", found, "s:resource", resource, "s:value", *value ? *value : ""); @@ -108,7 +110,7 @@ void display_update_xrm(display_t *display) xcb_xrm_database_free(display->database); display->database = database; - log_debug("Xrm database updated"); + log_trace("Xrm database updated"); } void display_update_scale(display_t *display) diff --git a/src/event.c b/src/event.c index 1c16c49..909abca 100644 --- a/src/event.c +++ b/src/event.c @@ -12,7 +12,7 @@ bool event_dispatch_block(layout_t *layout, event_t event) return false; for (int i = 0; i < layout->n_children; i++) { - if (event_dispatch_block(layout->children[i], event)) + if (event_dispatch_block(&layout->children[i], event)) return true; } diff --git a/src/layout.c b/src/layout.c index 126e2f5..c5969fb 100644 --- a/src/layout.c +++ b/src/layout.c @@ -6,11 +6,12 @@ #include "any_log.h" #include "layout.h" -layout_t *layout_create(block_t *block, layout_info_t info) +void layout_init(layout_t *layout, block_t *block, layout_info_t info) { assert(!block->hidden); - layout_t *layout = calloc(1, sizeof(layout_t)); + memset(layout, 0, sizeof(layout_t)); + layout->block = block; layout->x = info.x_offset; layout->y = 0; @@ -19,20 +20,20 @@ layout_t *layout_create(block_t *block, layout_info_t info) layout->height = info.height; if (block->type == BLOCK_GROUP) { + int x_offset = info.x_offset; layout->children = calloc(block->group.n_children, sizeof(layout_t)); - int x = info.x_offset; for (int i = 0; i < block->group.n_children; i++) { if (block->group.children[i].hidden) continue; - layout_t *child = layout_create(&block->group.children[i], info); - info.x_offset += child->width + block->group.spacing; - layout->children[layout->n_children++] = child; + layout_init(&layout->children[layout->n_children], &block->group.children[i], info); + info.x_offset += layout->children[layout->n_children].width + block->group.spacing; + layout->n_children++; } - layout_t *last = layout->children[layout->n_children - 1]; - layout->width = last->x + last->width - x; + layout_t *last = &layout->children[layout->n_children - 1]; + layout->width = last->x + last->width - x_offset; } else if (block->type == BLOCK_TEXT) { layout->pl = pango_layout_new(info.context); @@ -46,7 +47,11 @@ layout_t *layout_create(block_t *block, layout_info_t info) layout->width = info.height + (length != 1) * layout->text_width; } - return layout; + if (layout->block->max_width > 0 && layout->width > layout->block->max_width) + layout->width = layout->block->max_width; + + if (layout->block->min_width > 0 && layout->width < layout->block->min_width) + layout->width = layout->block->min_width; } void layout_render(layout_t *layout, cairo_t *cr) @@ -79,7 +84,7 @@ void layout_render(layout_t *layout, cairo_t *cr) if (layout->block->type == BLOCK_GROUP) { for (int i = 0; i < layout->n_children; i++) { cairo_push_group(cr); - layout_render(layout->children[i], cr); + layout_render(&layout->children[i], cr); cairo_pop_group_to_source(cr); cairo_paint(cr); } @@ -99,11 +104,11 @@ void layout_render(layout_t *layout, cairo_t *cr) void layout_free(layout_t *layout) { - for (int i = 0; i < layout->n_children; i++) - layout_free(layout->children[i]); - if (layout->pl != NULL) g_object_unref(layout->pl); - free(layout); + for (int i = 0; i < layout->n_children; i++) + layout_free(&layout->children[i]); + + free(layout->children); } diff --git a/src/layout.h b/src/layout.h index 4b8cddd..4e69a6c 100644 --- a/src/layout.h +++ b/src/layout.h @@ -22,10 +22,10 @@ typedef struct layout { int text_width, text_height; PangoLayout *pl; int n_children; - struct layout **children; + struct layout *children; } layout_t; -layout_t *layout_create(block_t *block, layout_info_t info); +void layout_init(layout_t *layout, block_t *block, layout_info_t info); void layout_render(layout_t *layout, cairo_t *cr); diff --git a/src/window.c b/src/window.c index e45eb27..0dba42f 100644 --- a/src/window.c +++ b/src/window.c @@ -40,7 +40,7 @@ static void wm_set_size(window_t *window) xcb_icccm_size_hints_set_position(&hints, false, window->x, window->y); xcb_icccm_set_wm_size_hints(window->display->connection, window->window, XCB_ATOM_WM_NORMAL_HINTS, &hints); - log_debug("Xcb icccm size hints updated"); + log_trace("Xcb icccm size hints updated"); } static void wm_set_struts(window_t *window) @@ -70,7 +70,7 @@ static void wm_set_struts(window_t *window) 12, values); - log_debug("Xcb EWMH struts updated"); + log_trace("Xcb EWMH struts updated"); } static void wm_setup(window_t *window) @@ -90,7 +90,7 @@ static void wm_setup(window_t *window) assert(xcb_ewmh_init_atoms_replies(&window->display->ewmh, ewmh_cookie, &error)); assert(error == NULL); - log_debug("Xcb EWMH connected"); + log_debug("Connected EWMH"); xcb_ewmh_set_wm_window_type(&window->display->ewmh, window->window, 1, &window->display->ewmh._NET_WM_WINDOW_TYPE_DOCK); xcb_ewmh_set_wm_desktop(&window->display->ewmh, window->window, 0xFFFFFFFF); @@ -149,11 +149,11 @@ void window_init(window_t *window, display_t *display) window->cw_mask, &window->cw_params); - log_value_debug("Xcb window created", + log_value_debug("Created window", "u:id", window->window); wm_setup(window); - log_debug("Updated window WM options"); + log_trace("Updated window WM options"); xcb_map_window(display->connection, window->window); xcb_flush(display->connection); @@ -172,12 +172,6 @@ void window_init(window_t *window, display_t *display) log_trace("Cairo context created"); } -//double window_get_scale(window_t *window) -//{ -// const int n = 4; -// return MAX(1, floor((window->display->screen_dpi / 96.0) * n) * (1.0 / n)); -//} - void window_move(window_t *window, int x, int y) { if (window->x == x && window->y == y) @@ -208,7 +202,7 @@ static void window_reshape(window_t *window) xcb_pixmap_t bitmap = xcb_generate_id(window->display->connection); xcb_create_pixmap(window->display->connection, depth, bitmap, window->window, window->width, window->height); - log_debug("Xcb pixmap created [id=%u]", bitmap); + log_trace("Xcb pixmap created"); cairo_surface_t *surface = cairo_xcb_surface_create_for_bitmap(window->display->connection, window->display->screen, @@ -232,7 +226,7 @@ static void window_reshape(window_t *window) cairo_arc(cr, window->width - radius, radius, radius, 270 * degree, 450 * degree); cairo_fill(cr); - log_trace("Xcb shape painted"); + log_trace("Painted window shape"); cairo_show_page(cr); cairo_destroy(cr); @@ -241,7 +235,7 @@ static void window_reshape(window_t *window) xcb_shape_mask(window->display->connection, XCB_SHAPE_SO_SET, XCB_SHAPE_SK_BOUNDING, window->window, 0, 0, bitmap); xcb_shape_select_input(window->display->connection, window->window, XCB_SHAPE_NOTIFY); - log_debug("Xcb shape mask updated"); + log_debug("Updated window shape mask"); xcb_free_pixmap(window->display->connection, bitmap); } @@ -275,6 +269,7 @@ void window_present(window_t *window, cairo_surface_t *surface, int width, int h { cairo_xcb_surface_set_size(window->surface, width, height); xcb_clear_area(window->display->connection, false, window->window, 0, 0, 0, 0); + log_trace("Cleared window area"); cairo_save(window->cr); cairo_set_source_surface(window->cr, surface, 0, 0); @@ -284,6 +279,7 @@ void window_present(window_t *window, cairo_surface_t *surface, int width, int h cairo_surface_flush(window->surface); cairo_restore(window->cr); + log_trace("Flushed window surface"); xcb_circulate_window(window->display->connection, XCB_CIRCULATE_RAISE_LOWEST, window->window); xcb_flush(window->display->connection); -- cgit v1.2.3