1 /* SPDX-License-Identifier: CC0-1.0 */
2
3 #include <stdlib.h>
4 #include <glib.h>
5 #include <systemd/sd-event.h>
6
7 typedef struct SDEventSource {
8 GSource source;
9 GPollFD pollfd;
10 sd_event *event;
11 } SDEventSource;
12
event_prepare(GSource * source,gint * timeout_)13 static gboolean event_prepare(GSource *source, gint *timeout_) {
14 return sd_event_prepare(((SDEventSource *)source)->event) > 0;
15 }
16
event_check(GSource * source)17 static gboolean event_check(GSource *source) {
18 return sd_event_wait(((SDEventSource *)source)->event, 0) > 0;
19 }
20
event_dispatch(GSource * source,GSourceFunc callback,gpointer user_data)21 static gboolean event_dispatch(GSource *source, GSourceFunc callback, gpointer user_data) {
22 return sd_event_dispatch(((SDEventSource *)source)->event) > 0;
23 }
24
event_finalize(GSource * source)25 static void event_finalize(GSource *source) {
26 sd_event_unref(((SDEventSource *)source)->event);
27 }
28
29 static GSourceFuncs event_funcs = {
30 .prepare = event_prepare,
31 .check = event_check,
32 .dispatch = event_dispatch,
33 .finalize = event_finalize,
34 };
35
g_sd_event_create_source(sd_event * event)36 GSource *g_sd_event_create_source(sd_event *event) {
37 SDEventSource *source;
38
39 source = (SDEventSource *)g_source_new(&event_funcs, sizeof(SDEventSource));
40
41 source->event = sd_event_ref(event);
42 source->pollfd.fd = sd_event_get_fd(event);
43 source->pollfd.events = G_IO_IN | G_IO_HUP | G_IO_ERR;
44
45 g_source_add_poll((GSource *)source, &source->pollfd);
46
47 return (GSource *)source;
48 }
49