Code

lyrics.c: replaced mpd_unused by G_GNUC_UNUSED
[ncmpc.git] / src / lyrics.c
1 /* ncmpc
2  * Copyright (C) 2008 Max Kellermann <max@duempel.org>
3  * This project's homepage is: http://www.musicpd.org
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
19 #include "lyrics.h"
20 #include "../config.h"
22 #include <assert.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <string.h>
26 #include <sys/stat.h>
27 #include <sys/signal.h>
28 #include <sys/wait.h>
30 static GPtrArray *plugins;
32 struct lyrics_loader {
33         char *artist, *title;
35         lyrics_callback_t callback;
36         void *callback_data;
38         guint next_plugin;
40         pid_t pid;
41         int fd;
42         GIOChannel *channel;
43         guint event_id;
45         GString *data;
46 };
48 static int lyrics_register_plugin(const char *path0)
49 {
50         int ret;
51         struct stat st;
52         char *path;
54         ret = stat(path0, &st);
55         if (ret < 0)
56                 return -1;
58         path = g_strdup(path0);
59         g_ptr_array_add(plugins, path);
60         return 0;
61 }
63 void lyrics_init(void)
64 {
65         GDir *dir;
67         plugins = g_ptr_array_new();
69         dir = g_dir_open(LYRICS_PLUGIN_DIR, 0, NULL);
70         if (dir != NULL) {
71                 const char *name;
72                 char path[sizeof(LYRICS_PLUGIN_DIR) + 128];
74                 memcpy(path, LYRICS_PLUGIN_DIR, sizeof(LYRICS_PLUGIN_DIR) - 1);
75                 path[sizeof(LYRICS_PLUGIN_DIR) - 1] = G_DIR_SEPARATOR;
77                 while ((name = g_dir_read_name(dir)) != NULL) {
78                         g_strlcpy(path + sizeof(LYRICS_PLUGIN_DIR), name,
79                                   sizeof(path) - sizeof(LYRICS_PLUGIN_DIR));
80                         lyrics_register_plugin(path);
81                 }
83                 g_dir_close(dir);
84         }
85 }
87 void lyrics_deinit(void)
88 {
89         guint i;
91         for (i = 0; i < plugins->len; ++i)
92                 free(g_ptr_array_index(plugins, i));
93         g_ptr_array_free(plugins, TRUE);
94 }
96 static void
97 lyrics_next_plugin(struct lyrics_loader *loader);
99 static void
100 lyrics_eof(struct lyrics_loader *loader)
102         int ret, status;
104         g_io_channel_unref(loader->channel);
105         close(loader->fd);
106         loader->fd = -1;
108         ret = waitpid(loader->pid, &status, 0);
109         loader->pid = -1;
111         if (ret < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
112                 g_string_free(loader->data, TRUE);
113                 loader->data = NULL;
115                 lyrics_next_plugin(loader);
116         } else {
117                 loader->callback(loader->data, loader->callback_data);
118         }
121 static gboolean
122 lyrics_data(G_GNUC_UNUSED GIOChannel *source,
123             G_GNUC_UNUSED GIOCondition condition, gpointer data)
125         struct lyrics_loader *loader = data;
126         char buffer[256];
127         ssize_t nbytes;
129         assert(loader != NULL);
130         assert(loader->fd >= 0);
131         assert(loader->pid > 0);
132         assert(source == loader->channel);
134         if ((condition & G_IO_IN) == 0) {
135                 lyrics_eof(loader);
136                 return FALSE;
137         }
139         nbytes = condition & G_IO_IN
140                 ? read(loader->fd, buffer, sizeof(buffer))
141                 : 0;
142         if (nbytes <= 0) {
143                 lyrics_eof(loader);
144                 return FALSE;
145         }
147         g_string_append_len(loader->data, buffer, nbytes);
148         return TRUE;
151 /**
152  * This is a timer callback which calls the lyrics callback "some
153  * timer later".  This solves the problem that lyrics_load() may fail
154  * immediately, leaving its return value in an undefined state.
155  * Instead, install a timer which calls the lyrics callback in the
156  * moment after.
157  */
158 static gboolean
159 lyrics_delayed_fail(gpointer data)
161         struct lyrics_loader *loader = data;
163         assert(loader != NULL);
164         assert(loader->fd < 0);
165         assert(loader->pid < 0);
166         assert(loader->data == NULL);
168         loader->callback(NULL, loader->callback_data);
170         return FALSE;
173 static int
174 lyrics_start_plugin(struct lyrics_loader *loader, const char *plugin_path)
176         int ret, fds[2];
177         pid_t pid;
179         assert(loader != NULL);
180         assert(loader->pid < 0);
181         assert(loader->fd < 0);
182         assert(loader->data == NULL);
184         ret = pipe(fds);
185         if (ret < 0)
186                 return -1;
188         pid = fork();
190         if (pid < 0) {
191                 close(fds[0]);
192                 close(fds[1]);
193                 return -1;
194         }
196         if (pid == 0) {
197                 dup2(fds[1], 1);
198                 dup2(fds[1], 1);
199                 close(fds[0]);
200                 close(fds[1]);
201                 close(0);
202                 /* XXX close other fds? */
204                 execl(plugin_path, plugin_path,
205                       loader->artist, loader->title, NULL);
206                 _exit(1);
207         }
209         close(fds[1]);
211         loader->pid = pid;
212         loader->fd = fds[0];
213         loader->data = g_string_new(NULL);
215         /* XXX CLOEXEC? */
217         loader->channel = g_io_channel_unix_new(loader->fd);
218         loader->event_id = g_io_add_watch(loader->channel, G_IO_IN|G_IO_HUP,
219                                           lyrics_data, loader);
221         return 0;
224 static void
225 lyrics_next_plugin(struct lyrics_loader *loader)
227         const char *plugin_path;
228         int ret = -1;
230         assert(loader->pid < 0);
231         assert(loader->fd < 0);
232         assert(loader->data == NULL);
234         if (loader->next_plugin >= plugins->len) {
235                 /* no plugins left */
236                 g_timeout_add(0, lyrics_delayed_fail, loader);
237                 return;
238         }
240         plugin_path = g_ptr_array_index(plugins, loader->next_plugin++);
241         ret = lyrics_start_plugin(loader, plugin_path);
242         if (ret < 0) {
243                 /* system error */
244                 g_timeout_add(0, lyrics_delayed_fail, loader);
245                 return;
246         }
249 struct lyrics_loader *
250 lyrics_load(const char *artist, const char *title,
251             lyrics_callback_t callback, void *data)
253         struct lyrics_loader *loader = g_new(struct lyrics_loader, 1);
255         assert(artist != NULL);
256         assert(title != NULL);
258         if (loader == NULL)
259                 return NULL;
261         loader->artist = g_strdup(artist);
262         loader->title = g_strdup(title);
263         loader->callback = callback;
264         loader->data = data;
265         loader->next_plugin = 0;
266         loader->pid = -1;
267         loader->fd = -1;
268         loader->data = NULL;
270         lyrics_next_plugin(loader);
272         return loader;
275 void
276 lyrics_free(struct lyrics_loader *loader)
278         if (loader->fd >= 0) {
279                 g_source_remove(loader->event_id);
280                 g_io_channel_unref(loader->channel);
281                 close(loader->fd);
282         }
284         if (loader->pid > 0) {
285                 int status;
287                 kill(loader->pid, SIGTERM);
288                 waitpid(loader->pid, &status, 0);
289         }
291         if (loader->data != NULL)
292                 g_string_free(loader->data, TRUE);