]>
Commit | Line | Data |
---|---|---|
5eef597e | 1 | /*** |
52ad194e MB |
2 | SPDX-License-Identifier: MIT |
3 | ||
5eef597e MP |
4 | Copyright 2014 Tom Gundersen |
5 | ||
6 | Permission is hereby granted, free of charge, to any person | |
7 | obtaining a copy of this software and associated documentation files | |
8 | (the "Software"), to deal in the Software without restriction, | |
9 | including without limitation the rights to use, copy, modify, merge, | |
10 | publish, distribute, sublicense, and/or sell copies of the Software, | |
11 | and to permit persons to whom the Software is furnished to do so, | |
12 | subject to the following conditions: | |
13 | ||
14 | The above copyright notice and this permission notice shall be | |
15 | included in all copies or substantial portions of the Software. | |
16 | ||
17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
18 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
19 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
20 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS | |
21 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | |
22 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
23 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
24 | SOFTWARE. | |
25 | ***/ | |
26 | ||
5eef597e MP |
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 | } |