|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
From c609cb9898dc8dec5dcb0b1d111b3f6b6a5e09d4 Mon Sep 17 00:00:00 2001
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
From: Tom Gundersen <teg@jklm.no>
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
Date: Wed, 27 Aug 2014 19:04:29 +0200
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
Subject: [PATCH] man: add sample glib/sd-event integration
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
This should be moved to man pages, but for now the C code is included directly.
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
Suggested by Zbyszek.
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
---
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
man/glib-event-glue.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
1 file changed, 74 insertions(+)
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
create mode 100644 man/glib-event-glue.c
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
diff --git a/man/glib-event-glue.c b/man/glib-event-glue.c
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
new file mode 100644
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
index 0000000000..95aaea1e63
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
--- /dev/null
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+++ b/man/glib-event-glue.c
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
@@ -0,0 +1,74 @@
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+/***
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ Copyright 2014 Tom Gundersen
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ Permission is hereby granted, free of charge, to any person
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ obtaining a copy of this software and associated documentation files
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ (the "Software"), to deal in the Software without restriction,
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ including without limitation the rights to use, copy, modify, merge,
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ publish, distribute, sublicense, and/or sell copies of the Software,
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ and to permit persons to whom the Software is furnished to do so,
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ subject to the following conditions:
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ The above copyright notice and this permission notice shall be
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ included in all copies or substantial portions of the Software.
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ SOFTWARE.
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+***/
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+#include <assert.h>
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+#include <errno.h>
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+#include <stdlib.h>
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+#include "glib-event-glue.h"
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+typedef struct SDEventSource {
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ GSource source;
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ GPollFD pollfd;
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ sd_event *event;
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+} SDEventSource;
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+static gboolean event_prepare(GSource *source, gint *timeout_) {
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ return sd_event_prepare(((SDEventSource *)source)->event) > 0;
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+}
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+static gboolean event_check(GSource *source) {
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ return sd_event_wait(((SDEventSource *)source)->event, 0) > 0;
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+}
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+static gboolean event_dispatch(GSource *source, GSourceFunc callback, gpointer user_data) {
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ return sd_event_dispatch(((SDEventSource *)source)->event) > 0;
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+}
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+static void event_finalize(GSource *source) {
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ sd_event_unref(((SDEventSource *)source)->event);
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+}
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+static GSourceFuncs event_funcs = {
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ .prepare = event_prepare,
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ .check = event_check,
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ .dispatch = event_dispatch,
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ .finalize = event_finalize,
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+};
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+GSource *g_sd_event_create_source(sd_event *event) {
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ SDEventSource *source;
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ source = (SDEventSource *)g_source_new(&event_funcs, sizeof(SDEventSource));
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ source->event = sd_event_ref(event);
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ source->pollfd.fd = sd_event_get_fd(event);
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ source->pollfd.events = G_IO_IN | G_IO_HUP | G_IO_ERR;
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ g_source_add_poll((GSource *)source, &source->pollfd);
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ return (GSource *)source;
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+}
|