aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2016-04-30 04:16:07 -0400
committerJesse Luehrs <doy@tozt.net>2016-04-30 04:16:07 -0400
commit86e614feb3be0d824416a26a6eeb87c661b0e449 (patch)
tree31db7033afe3e68eb827e42d7213eac33ad36fa0
parentc84833d92d5a02a172349528d75ee16a984a043c (diff)
downloadrunes-86e614feb3be0d824416a26a6eeb87c661b0e449.tar.gz
runes-86e614feb3be0d824416a26a6eeb87c661b0e449.zip
factor out timer setting back into loop.c
-rw-r--r--src/loop.c40
-rw-r--r--src/loop.h2
-rw-r--r--src/window-xlib.c21
3 files changed, 45 insertions, 18 deletions
diff --git a/src/loop.c b/src/loop.c
index e361600..0f88c26 100644
--- a/src/loop.c
+++ b/src/loop.c
@@ -1,5 +1,15 @@
+#include <stdlib.h>
+
#include "runes.h"
+struct runes_loop_timer_data {
+ RunesTerm *t;
+ void (*cb)(RunesTerm*);
+};
+
+static void runes_loop_timer_cb(uv_timer_t *handle);
+static void runes_loop_free_handle(uv_handle_t *handle);
+
void runes_loop_init(RunesTerm *t)
{
RunesLoop *loop = &t->loop;
@@ -16,9 +26,39 @@ void runes_loop_run(RunesTerm *t)
uv_run(loop->loop, UV_RUN_DEFAULT);
}
+void runes_loop_timer_set(RunesTerm *t, int timeout, int repeat,
+ void (*cb)(RunesTerm*))
+{
+ RunesLoop *loop = &t->loop;
+ uv_timer_t *timer_req;
+ struct runes_loop_timer_data *timer_data;
+
+ timer_req = malloc(sizeof(uv_timer_t));
+ uv_timer_init(loop->loop, timer_req);
+ timer_data = malloc(sizeof(struct runes_loop_timer_data));
+ timer_data->t = t;
+ timer_data->cb = cb;
+ timer_req->data = (void *)timer_data;
+ uv_timer_start(timer_req, runes_loop_timer_cb, timeout, repeat);
+}
+
void runes_loop_cleanup(RunesTerm *t)
{
RunesLoop *loop = &t->loop;
uv_loop_close(loop->loop);
}
+
+static void runes_loop_timer_cb(uv_timer_t *handle)
+{
+ struct runes_loop_timer_data *data = handle->data;
+ data->cb(data->t);
+ uv_close(
+ (uv_handle_t *)handle, runes_loop_free_handle);
+}
+
+static void runes_loop_free_handle(uv_handle_t *handle)
+{
+ free(handle->data);
+ free(handle);
+}
diff --git a/src/loop.h b/src/loop.h
index 215b56c..05f3065 100644
--- a/src/loop.h
+++ b/src/loop.h
@@ -14,6 +14,8 @@ struct runes_loop_data {
void runes_loop_init(RunesTerm *t);
void runes_loop_run(RunesTerm *t);
+void runes_loop_timer_set(RunesTerm *t, int timeout, int repeat,
+ void (*cb)(RunesTerm*));
void runes_loop_cleanup(RunesTerm *t);
#endif
diff --git a/src/window-xlib.c b/src/window-xlib.c
index 156f630..ff3f865 100644
--- a/src/window-xlib.c
+++ b/src/window-xlib.c
@@ -85,8 +85,7 @@ static void runes_window_backend_resize_window(
static void runes_window_backend_flush(RunesTerm *t);
static void runes_window_backend_visible_scroll(RunesTerm *t, int count);
static void runes_window_backend_visual_bell(RunesTerm *t);
-static void runes_window_backend_reset_visual_bell(uv_timer_t *handle);
-static void runes_window_backend_visual_bell_free_handle(uv_handle_t *handle);
+static void runes_window_backend_reset_visual_bell(RunesTerm *t);
static void runes_window_backend_audible_bell(RunesTerm *t);
static void runes_window_backend_set_urgent(RunesTerm *t);
static void runes_window_backend_clear_urgent(RunesTerm *t);
@@ -553,36 +552,22 @@ static void runes_window_backend_visual_bell(RunesTerm *t)
}
if (!w->visual_bell_is_ringing) {
- uv_timer_t *timer_req;
-
w->visual_bell_is_ringing = 1;
cairo_set_source(w->backend_cr, t->config.fgdefault);
cairo_paint(w->backend_cr);
cairo_surface_flush(cairo_get_target(w->backend_cr));
XFlush(w->dpy);
- timer_req = malloc(sizeof(uv_timer_t));
- uv_timer_init(t->loop.loop, timer_req);
- timer_req->data = (void *)t;
- uv_timer_start(
- timer_req, runes_window_backend_reset_visual_bell, 20, 0);
+ runes_loop_timer_set(t, 20, 0, runes_window_backend_reset_visual_bell);
}
}
-static void runes_window_backend_reset_visual_bell(uv_timer_t *handle)
+static void runes_window_backend_reset_visual_bell(RunesTerm *t)
{
- RunesTerm *t = handle->data;
RunesWindowBackend *w = &t->w;
runes_window_backend_request_flush(t);
w->visual_bell_is_ringing = 0;
- uv_close(
- (uv_handle_t *)handle, runes_window_backend_visual_bell_free_handle);
-}
-
-static void runes_window_backend_visual_bell_free_handle(uv_handle_t *handle)
-{
- free(handle);
}
static void runes_window_backend_audible_bell(RunesTerm *t)