chiark / gitweb /
34635f3d08874efc450dd02875e2cc788162d879
[elogind.git] / man / glib-event-glue.c
1 /* SPDX-License-Identifier: MIT */
2 /* Copyright © 2014 Tom Gundersen */
3
4 #include <stdlib.h>
5 //#include <elogind/sd-event.h>
6 //#include <glib.h>
7 //#include <elogind/sd-event.h>
8
9 typedef struct SDEventSource {
10   GSource source;
11   GPollFD pollfd;
12   sd_event *event;
13 } SDEventSource;
14
15 static gboolean event_prepare(GSource *source, gint *timeout_) {
16   return sd_event_prepare(((SDEventSource *)source)->event) > 0;
17 }
18
19 static gboolean event_check(GSource *source) {
20   return sd_event_wait(((SDEventSource *)source)->event, 0) > 0;
21 }
22
23 static gboolean event_dispatch(GSource *source, GSourceFunc callback, gpointer user_data) {
24   return sd_event_dispatch(((SDEventSource *)source)->event) > 0;
25 }
26
27 static void event_finalize(GSource *source) {
28   sd_event_unref(((SDEventSource *)source)->event);
29 }
30
31 static GSourceFuncs event_funcs = {
32   .prepare = event_prepare,
33   .check = event_check,
34   .dispatch = event_dispatch,
35   .finalize = event_finalize,
36 };
37
38 GSource *g_sd_event_create_source(sd_event *event) {
39   SDEventSource *source;
40
41   source = (SDEventSource *)g_source_new(&event_funcs, sizeof(SDEventSource));
42
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;
46
47   g_source_add_poll((GSource *)source, &source->pollfd);
48
49   return (GSource *)source;
50 }