chiark / gitweb /
systemctl: add add-wants and add-requires verbs
[elogind.git] / man / glib-event-glue.c
1 /***
2   Copyright 2014 Tom Gundersen
3
4   Permission is hereby granted, free of charge, to any person
5   obtaining a copy of this software and associated documentation files
6   (the "Software"), to deal in the Software without restriction,
7   including without limitation the rights to use, copy, modify, merge,
8   publish, distribute, sublicense, and/or sell copies of the Software,
9   and to permit persons to whom the Software is furnished to do so,
10   subject to the following conditions:
11
12   The above copyright notice and this permission notice shall be
13   included in all copies or substantial portions of the Software.
14
15   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19   BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20   ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22   SOFTWARE.
23 ***/
24
25 #include <assert.h>
26 #include <errno.h>
27 #include <stdlib.h>
28
29 typedef struct SDEventSource {
30         GSource source;
31         GPollFD pollfd;
32         sd_event *event;
33 } SDEventSource;
34
35 static gboolean event_prepare(GSource *source, gint *timeout_) {
36         return sd_event_prepare(((SDEventSource *)source)->event) > 0;
37 }
38
39 static gboolean event_check(GSource *source) {
40         return sd_event_wait(((SDEventSource *)source)->event, 0) > 0;
41 }
42
43 static gboolean event_dispatch(GSource *source, GSourceFunc callback, gpointer user_data) {
44         return sd_event_dispatch(((SDEventSource *)source)->event) > 0;
45 }
46
47 static void event_finalize(GSource *source) {
48         sd_event_unref(((SDEventSource *)source)->event);
49 }
50
51 static GSourceFuncs event_funcs = {
52         .prepare = event_prepare,
53         .check = event_check,
54         .dispatch = event_dispatch,
55         .finalize = event_finalize,
56 };
57
58 GSource *g_sd_event_create_source(sd_event *event) {
59         SDEventSource *source;
60
61         source = (SDEventSource *)g_source_new(&event_funcs, sizeof(SDEventSource));
62
63         source->event = sd_event_ref(event);
64         source->pollfd.fd = sd_event_get_fd(event);
65         source->pollfd.events = G_IO_IN | G_IO_HUP | G_IO_ERR;
66
67         g_source_add_poll((GSource *)source, &source->pollfd);
68
69         return (GSource *)source;
70 }