1 /* SPDX-License-Identifier: MIT */
4 //#include <elogind/sd-event.h>
7 #include <systemd/sd-event.h>
9 typedef struct SDEventSource {
15 static gboolean event_prepare(GSource *source, gint *timeout_) {
16 return sd_event_prepare(((SDEventSource *)source)->event) > 0;
19 static gboolean event_check(GSource *source) {
20 return sd_event_wait(((SDEventSource *)source)->event, 0) > 0;
23 static gboolean event_dispatch(GSource *source, GSourceFunc callback, gpointer user_data) {
24 return sd_event_dispatch(((SDEventSource *)source)->event) > 0;
27 static void event_finalize(GSource *source) {
28 sd_event_unref(((SDEventSource *)source)->event);
31 static GSourceFuncs event_funcs = {
32 .prepare = event_prepare,
34 .dispatch = event_dispatch,
35 .finalize = event_finalize,
38 GSource *g_sd_event_create_source(sd_event *event) {
39 SDEventSource *source;
41 source = (SDEventSource *)g_source_new(&event_funcs, sizeof(SDEventSource));
43 source->event = sd_event_ref(event);
44 source->pollfd.fd = sd_event_get_fd(event);
45 source->pollfd.events = G_IO_IN | G_IO_HUP | G_IO_ERR;
47 g_source_add_poll((GSource *)source, &source->pollfd);
49 return (GSource *)source;