2018-03-21 22:02:49 +03:00
/* SPDX-License-Identifier: MIT */
/* Copyright 2014 Tom Gundersen */
2014-08-27 21:04:29 +04:00
# include <stdlib.h>
2018-03-21 18:38:51 +03:00
# include <glib.h>
# include <systemd/sd-event.h>
2014-08-27 21:04:29 +04:00
typedef struct SDEventSource {
2018-03-21 22:02:49 +03:00
GSource source ;
GPollFD pollfd ;
sd_event * event ;
2014-08-27 21:04:29 +04:00
} SDEventSource ;
static gboolean event_prepare ( GSource * source , gint * timeout_ ) {
2018-03-21 22:02:49 +03:00
return sd_event_prepare ( ( ( SDEventSource * ) source ) - > event ) > 0 ;
2014-08-27 21:04:29 +04:00
}
static gboolean event_check ( GSource * source ) {
2018-03-21 22:02:49 +03:00
return sd_event_wait ( ( ( SDEventSource * ) source ) - > event , 0 ) > 0 ;
2014-08-27 21:04:29 +04:00
}
static gboolean event_dispatch ( GSource * source , GSourceFunc callback , gpointer user_data ) {
2018-03-21 22:02:49 +03:00
return sd_event_dispatch ( ( ( SDEventSource * ) source ) - > event ) > 0 ;
2014-08-27 21:04:29 +04:00
}
static void event_finalize ( GSource * source ) {
2018-03-21 22:02:49 +03:00
sd_event_unref ( ( ( SDEventSource * ) source ) - > event ) ;
2014-08-27 21:04:29 +04:00
}
static GSourceFuncs event_funcs = {
2018-03-21 22:02:49 +03:00
. prepare = event_prepare ,
. check = event_check ,
. dispatch = event_dispatch ,
. finalize = event_finalize ,
2014-08-27 21:04:29 +04:00
} ;
GSource * g_sd_event_create_source ( sd_event * event ) {
2018-03-21 22:02:49 +03:00
SDEventSource * source ;
2014-08-27 21:04:29 +04:00
2018-03-21 22:02:49 +03:00
source = ( SDEventSource * ) g_source_new ( & event_funcs , sizeof ( SDEventSource ) ) ;
2014-08-27 21:04:29 +04:00
2018-03-21 22:02:49 +03:00
source - > event = sd_event_ref ( event ) ;
source - > pollfd . fd = sd_event_get_fd ( event ) ;
source - > pollfd . events = G_IO_IN | G_IO_HUP | G_IO_ERR ;
2014-08-27 21:04:29 +04:00
2018-03-21 22:02:49 +03:00
g_source_add_poll ( ( GSource * ) source , & source - > pollfd ) ;
2014-08-27 21:04:29 +04:00
2018-03-21 22:02:49 +03:00
return ( GSource * ) source ;
2014-08-27 21:04:29 +04:00
}