Write events to the SQLite database

This commit is contained in:
Přemysl Eric Janouch 2020-09-25 07:20:49 +02:00
parent 27a63e3414
commit 86b0579cb7
Signed by: p
GPG Key ID: A0420B94F92B9493
1 changed files with 34 additions and 1 deletions

35
wdmtg.c
View File

@ -101,6 +101,7 @@ event_free(struct event *self)
struct { struct {
GAsyncQueue *queue; // Async queue of `struct event` GAsyncQueue *queue; // Async queue of `struct event`
sqlite3 *db; // Event database sqlite3 *db; // Event database
sqlite3_stmt *add_event; // Prepared statement: add event
} g; } g;
struct { struct {
@ -409,7 +410,7 @@ on_x_alarm_notify(const xcb_sync_alarm_notify_event_t *ev)
} }
} }
static gboolean static void
process_x11_event(xcb_generic_event_t *ev) process_x11_event(xcb_generic_event_t *ev)
{ {
int event_code = ev->response_type & 0x7f; int event_code = ev->response_type & 0x7f;
@ -542,12 +543,38 @@ generator_cleanup(void)
static gboolean static gboolean
on_queue_incoming(G_GNUC_UNUSED gpointer user_data) on_queue_incoming(G_GNUC_UNUSED gpointer user_data)
{ {
int rc = 0;
char *errmsg = NULL;
if ((rc = sqlite3_exec(g.db, "BEGIN", NULL, NULL, &errmsg))) {
g_printerr("DB BEGIN error: %s\n", errmsg);
free(errmsg);
return G_SOURCE_CONTINUE;
}
// TODO: there should ideally be a limit to how many things can end up
// in a transaction at once (the amount of dequeues here)
struct event *event = NULL; struct event *event = NULL;
while ((event = g_async_queue_try_pop(g.queue))) { while ((event = g_async_queue_try_pop(g.queue))) {
printf("Event: ts: %ld, title: %s, idle: %d\n", printf("Event: ts: %ld, title: %s, idle: %d\n",
event->timestamp, event->title ?: "(none)", event->idle); event->timestamp, event->title ?: "(none)", event->idle);
if ((rc = sqlite3_bind_int64(g.add_event, 1, event->timestamp)) ||
// FIXME: it will fail on NULL titles
(rc = sqlite3_bind_text(g.add_event, 2, event->title, -1,
SQLITE_STATIC)) ||
(rc = sqlite3_bind_int(g.add_event, 3, event->idle)))
g_printerr("DB bind error: %s\n", sqlite3_errmsg(g.db));
if (((rc = sqlite3_step(g.add_event)) && rc != SQLITE_DONE) ||
(rc = sqlite3_reset(g.add_event)))
g_printerr("DB write error: %s\n", sqlite3_errmsg(g.db));
event_free(event); event_free(event);
} }
if ((rc = sqlite3_exec(g.db, "COMMIT", NULL, NULL, &errmsg))) {
g_printerr("DB commit error: %s\n", errmsg);
free(errmsg);
}
return G_SOURCE_CONTINUE; return G_SOURCE_CONTINUE;
} }
@ -595,6 +622,11 @@ database_init(const gchar *db_path)
} }
if ((rc = sqlite3_exec(db, "COMMIT", NULL, NULL, &errmsg))) if ((rc = sqlite3_exec(db, "COMMIT", NULL, NULL, &errmsg)))
exit_fatal("%s: %s", db_path, errmsg); exit_fatal("%s: %s", db_path, errmsg);
if ((rc = sqlite3_prepare_v2(db,
"INSERT INTO events (timestamp, title, idle) VALUES (?, ?, ?)", -1,
&g.add_event, NULL)))
exit_fatal("%s: %s", db_path, sqlite3_errmsg(db));
return db; return db;
} }
@ -690,6 +722,7 @@ main(int argc, char *argv[])
g_source_attach(queue_source, g_main_context_default()); g_source_attach(queue_source, g_main_context_default());
// TODO: listen for connections on the control socket // TODO: listen for connections on the control socket
// - just show/map the window when anything connects at all
WdmtgWindow *window = wdmtg_window_new_with_db(g.db); WdmtgWindow *window = wdmtg_window_new_with_db(g.db);