Code

meson.build: fix build with meson > 0.38.1
[ncmpc.git] / src / plugin.c
index 55de0d38ced7b8138642e4f4daf841b8218f3bd7..f9049ecfa77458285c07da66f4946f542a1b4959 100644 (file)
 #include <sys/wait.h>
 
 struct plugin_pipe {
+       struct plugin_cycle *cycle;
+
        /** the pipe to the plugin process, or -1 if none is currently
            open */
        int fd;
-       /** the GLib channel of #fd */
-       GIOChannel *channel;
-       /** the GLib IO watch of #channel */
+       /** the GLib IO watch of #fd */
        guint event_id;
        /** the output of the current plugin */
        GString *data;
@@ -127,7 +127,6 @@ next_plugin(struct plugin_cycle *cycle);
 static void
 plugin_eof(struct plugin_cycle *cycle, struct plugin_pipe *p)
 {
-       g_io_channel_unref(p->channel);
        close(p->fd);
        p->fd = -1;
 
@@ -167,18 +166,13 @@ static gboolean
 plugin_data(gcc_unused GIOChannel *source,
            gcc_unused GIOCondition condition, gpointer data)
 {
-       struct plugin_cycle *cycle = data;
+       struct plugin_pipe *p = data;
+       assert(p->fd >= 0);
+
+       struct plugin_cycle *cycle = p->cycle;
        assert(cycle != NULL);
        assert(cycle->pid > 0);
 
-       struct plugin_pipe *p = NULL;
-       if (source == cycle->pipe_stdout.channel)
-               p = &cycle->pipe_stdout;
-       else if (source == cycle->pipe_stderr.channel)
-               p = &cycle->pipe_stderr;
-       assert(p != NULL);
-       assert(p->fd >= 0);
-
        char buffer[256];
        ssize_t nbytes = condition & G_IO_IN
                ? read(p->fd, buffer, sizeof(buffer))
@@ -217,11 +211,13 @@ plugin_delayed_fail(gpointer data)
 static void
 plugin_fd_add(struct plugin_cycle *cycle, struct plugin_pipe *p, int fd)
 {
+       p->cycle = cycle;
        p->fd = fd;
        p->data = g_string_new(NULL);
-       p->channel = g_io_channel_unix_new(fd);
-       p->event_id = g_io_add_watch(p->channel, G_IO_IN|G_IO_HUP,
-                                    plugin_data, cycle);
+       GIOChannel *channel = g_io_channel_unix_new(fd);
+       p->event_id = g_io_add_watch(channel, G_IO_IN|G_IO_HUP,
+                                    plugin_data, p);
+       g_io_channel_unref(channel);
 }
 
 static int
@@ -298,7 +294,7 @@ next_plugin(struct plugin_cycle *cycle)
 
        if (cycle->next_plugin >= cycle->list->plugins->len) {
                /* no plugins left */
-               g_timeout_add(0, plugin_delayed_fail, cycle);
+               g_idle_add(plugin_delayed_fail, cycle);
                return;
        }
 
@@ -306,7 +302,7 @@ next_plugin(struct plugin_cycle *cycle)
                                                    cycle->next_plugin++);
        if (start_plugin(cycle, plugin_path) < 0) {
                /* system error */
-               g_timeout_add(0, plugin_delayed_fail, cycle);
+               g_idle_add(plugin_delayed_fail, cycle);
                return;
        }
 }
@@ -363,7 +359,6 @@ plugin_fd_remove(struct plugin_pipe *p)
 {
        if (p->fd >= 0) {
                g_source_remove(p->event_id);
-               g_io_channel_unref(p->channel);
                close(p->fd);
        }
 }