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