aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFederico Angelilli <code@fedang.net>2023-11-17 15:07:14 +0100
committerFederico Angelilli <code@fedang.net>2023-11-17 15:07:14 +0100
commitf8363e89257e8b0a4ff71accbd7b6be22935274f (patch)
tree62e6b17d11b174f6683bf090ed678dddd7a263d4
parent41729222431449a81535b28ad27ce2620cb5819b (diff)
Attach state to the event loop
-rw-r--r--comet.c2
-rw-r--r--connection.c45
-rw-r--r--connection.h8
-rw-r--r--draw.h7
-rw-r--r--state.h6
-rw-r--r--window.c9
-rw-r--r--window.h9
7 files changed, 62 insertions, 24 deletions
diff --git a/comet.c b/comet.c
index 5ad6dc1..a21b83a 100644
--- a/comet.c
+++ b/comet.c
@@ -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
diff --git a/draw.h b/draw.h
index 2bbb3c2..3e83146 100644
--- a/draw.h
+++ b/draw.h
@@ -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);
diff --git a/state.h b/state.h
index 5c5528d..696b657 100644
--- a/state.h
+++ b/state.h
@@ -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);
diff --git a/window.c b/window.c
index e7391be..734761a 100644
--- a/window.c
+++ b/window.c
@@ -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;
diff --git a/window.h b/window.h
index 009966a..5ee075f 100644
--- a/window.h
+++ b/window.h
@@ -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);