Tom Gundersen | c609cb9 | 2014-08-27 19:04:29 +0200 | [diff] [blame] | 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 | |
Tom Gundersen | c609cb9 | 2014-08-27 19:04:29 +0200 | [diff] [blame] | 25 | #include <stdlib.h> |
| 26 | |
Tom Gundersen | c609cb9 | 2014-08-27 19:04:29 +0200 | [diff] [blame] | 27 | typedef struct SDEventSource { |
| 28 | GSource source; |
| 29 | GPollFD pollfd; |
| 30 | sd_event *event; |
| 31 | } SDEventSource; |
| 32 | |
| 33 | static gboolean event_prepare(GSource *source, gint *timeout_) { |
| 34 | return sd_event_prepare(((SDEventSource *)source)->event) > 0; |
| 35 | } |
| 36 | |
| 37 | static gboolean event_check(GSource *source) { |
| 38 | return sd_event_wait(((SDEventSource *)source)->event, 0) > 0; |
| 39 | } |
| 40 | |
| 41 | static gboolean event_dispatch(GSource *source, GSourceFunc callback, gpointer user_data) { |
| 42 | return sd_event_dispatch(((SDEventSource *)source)->event) > 0; |
| 43 | } |
| 44 | |
| 45 | static void event_finalize(GSource *source) { |
| 46 | sd_event_unref(((SDEventSource *)source)->event); |
| 47 | } |
| 48 | |
| 49 | static GSourceFuncs event_funcs = { |
| 50 | .prepare = event_prepare, |
| 51 | .check = event_check, |
| 52 | .dispatch = event_dispatch, |
| 53 | .finalize = event_finalize, |
| 54 | }; |
| 55 | |
| 56 | GSource *g_sd_event_create_source(sd_event *event) { |
| 57 | SDEventSource *source; |
| 58 | |
| 59 | source = (SDEventSource *)g_source_new(&event_funcs, sizeof(SDEventSource)); |
| 60 | |
| 61 | source->event = sd_event_ref(event); |
| 62 | source->pollfd.fd = sd_event_get_fd(event); |
| 63 | source->pollfd.events = G_IO_IN | G_IO_HUP | G_IO_ERR; |
| 64 | |
| 65 | g_source_add_poll((GSource *)source, &source->pollfd); |
| 66 | |
| 67 | return (GSource *)source; |
| 68 | } |