diff options
| author | Federico Angelilli <code@fedang.net> | 2023-11-17 15:07:14 +0100 |
|---|---|---|
| committer | Federico Angelilli <code@fedang.net> | 2023-11-17 15:07:14 +0100 |
| commit | f8363e89257e8b0a4ff71accbd7b6be22935274f (patch) | |
| tree | 62e6b17d11b174f6683bf090ed678dddd7a263d4 | |
| parent | 41729222431449a81535b28ad27ce2620cb5819b (diff) | |
Attach state to the event loop
| -rw-r--r-- | comet.c | 2 | ||||
| -rw-r--r-- | connection.c | 45 | ||||
| -rw-r--r-- | connection.h | 8 | ||||
| -rw-r--r-- | draw.h | 7 | ||||
| -rw-r--r-- | state.h | 6 | ||||
| -rw-r--r-- | window.c | 9 | ||||
| -rw-r--r-- | window.h | 9 |
7 files changed, 62 insertions, 24 deletions
@@ -45,6 +45,8 @@ int main(int argc, char **argv) Drawable *draw = draw_create("Hack 12", height, x_padding, x_padding, y_padding, 1); State *state = state_create(win, draw); + connection_attach_state(con, state); + connection_attach_source(con); guint source_term = g_unix_signal_add(SIGTERM, mainloop_quit, mainloop); guint source_int = g_unix_signal_add(SIGINT, mainloop_quit, mainloop); diff --git a/connection.c b/connection.c index 1ee29bc..d3eedda 100644 --- a/connection.c +++ b/connection.c @@ -8,6 +8,7 @@ #include "log.h" #include "connection.h" +#include "state.h" static bool query_xrm(Connection *con, const char *res, char **value) { @@ -69,6 +70,20 @@ static gboolean source_check(GSource *source) return xsource->event != NULL; } +static void redraw_window(Connection *con, xcb_window_t id) +{ + for (int i = 0; i < con->states->len; ++i) { + State *state = con->states->pdata[i]; + + // Redraw matching window + if (state->win->window == id) { + log_debug("Redrawing window [id=%d]", id); + draw_paint(state->draw, state->win); + break; + } + } +} + static gboolean source_dispatch(GSource *source, GSourceFunc callback, gpointer data) { EventSource *xsource = (EventSource *)source; @@ -98,9 +113,7 @@ static gboolean source_dispatch(GSource *source, GSourceFunc callback, gpointer case XCB_EXPOSE: { xcb_expose_event_t *expose = (xcb_expose_event_t *)xsource->event; log_debug("Processing event 'Expose' [type=%d]", XCB_EXPOSE); - - // Redraw - //callback(data); + redraw_window(xsource->con, expose->window); break; } @@ -127,9 +140,7 @@ static gboolean source_dispatch(GSource *source, GSourceFunc callback, gpointer if (property->atom == XCB_ATOM_RESOURCE_MANAGER) { update_xrm(xsource->con); update_scale(xsource->con); - - // Redraw - //callback(data); + redraw_window(xsource->con, property->window); } break; } @@ -168,9 +179,6 @@ static void attach_source(Connection *con) source->event = NULL; source->fd_tag = g_source_add_unix_fd(con->source, xcb_get_file_descriptor(con->connection), G_IO_IN | G_IO_HUP | G_IO_ERR); - // TODO: Draw should be decoupled from this file - //g_source_set_callback(con->source, G_SOURCE_FUNC(draw), win, NULL); - g_source_attach(con->source, NULL); } @@ -251,14 +259,31 @@ Connection *connection_create() xcb_flush(con->connection); log_debug("Xcb set up"); + con->states = g_ptr_array_new(); + g_assert_nonnull(con->states); + + return con; +} + +void connection_attach_source(Connection *con) +{ + g_assert_null(con->source); + attach_source(con); log_debug("Xcb event loop attached"); +} - return con; +void connection_attach_state(Connection *con, State *state) +{ + g_assert_nonnull(state); + g_ptr_array_add(con->states, state); + log_debug("Attached state to event loop"); } void connection_destroy(Connection *con) { + g_ptr_array_free(con->states, true); + g_source_destroy(con->source); g_source_unref(con->source); diff --git a/connection.h b/connection.h index b187b7a..21cbf3f 100644 --- a/connection.h +++ b/connection.h @@ -8,6 +8,9 @@ #include <xcb/xcb_errors.h> #include <xcb/randr.h> +// Forward declaration +typedef struct State State; + typedef struct Connection Connection; // TODO: Make this opaque @@ -22,10 +25,15 @@ struct Connection { xcb_errors_context_t *errors; xcb_ewmh_connection_t ewmh; GSource *source; + GPtrArray *states; }; Connection *connection_create(); +void connection_attach_source(Connection *con); + +void connection_attach_state(Connection *con, State *state); + void connection_destroy(Connection *con); #endif @@ -6,15 +6,16 @@ #include "window.h" -// TODO: Make opaque -typedef struct { +typedef struct Drawable Drawable; + +struct Drawable { PangoFontDescription *desc; int height; int left_pad; int right_pad; int top_pad; double alpha; -} Drawable; +}; Drawable *draw_create(const char *font, int height, int left_pad, int right_pad, int top_pad, double alpha); @@ -4,10 +4,12 @@ #include "window.h" #include "draw.h" -typedef struct { +typedef struct State State; + +struct State { Window *win; Drawable *draw; -} State; +}; State *state_create(Window *win, Drawable *draw); @@ -16,15 +16,6 @@ #include "window.h" #include "log.h" -struct Window { - Connection *con; - xcb_drawable_t window; - cairo_surface_t *surface; - cairo_t *cr; - int x, y; - int width, height; -}; - static xcb_atom_t intern_atom(Window *win, const char *atom) { xcb_generic_error_t *error; @@ -7,6 +7,15 @@ typedef struct Window Window; +struct Window { + Connection *con; + xcb_drawable_t window; + cairo_surface_t *surface; + cairo_t *cr; + int x, y; + int width, height; +}; + Window *window_create(Connection *con); cairo_t *window_get_context(Window *win); |
