Code

wreadln: use memcpy() for both cases
[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 "gcc.h"
21 #include "../config.h"
23 #include <assert.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <string.h>
27 #include <sys/stat.h>
28 #include <sys/signal.h>
29 #include <sys/wait.h>
31 static GPtrArray *plugins;
33 struct lyrics_loader {
34         char *artist, *title;
36         lyrics_callback_t callback;
37         void *callback_data;
39         guint next_plugin;
41         pid_t pid;
42         int fd;
43         GIOChannel *channel;
44         guint event_id;
46         GString *data;
47 };
49 static int lyrics_register_plugin(const char *path0)
50 {
51         int ret;
52         struct stat st;
53         char *path;
55         ret = stat(path0, &st);
56         if (ret < 0)
57                 return -1;
59         path = g_strdup(path0);
60         g_ptr_array_add(plugins, path);
61         return 0;
62 }
64 void lyrics_init(void)
65 {
66         GDir *dir;
68         plugins = g_ptr_array_new();
70         dir = g_dir_open(LYRICS_PLUGIN_DIR, 0, NULL);
71         if (dir != NULL) {
72                 const char *name;
73                 char path[sizeof(LYRICS_PLUGIN_DIR) + 128];
75                 memcpy(path, LYRICS_PLUGIN_DIR, sizeof(LYRICS_PLUGIN_DIR) - 1);
76                 path[sizeof(LYRICS_PLUGIN_DIR) - 1] = G_DIR_SEPARATOR;
78                 while ((name = g_dir_read_name(dir)) != NULL) {
79                         g_strlcpy(path + sizeof(LYRICS_PLUGIN_DIR), name,
80                                   sizeof(path) - sizeof(LYRICS_PLUGIN_DIR));
81                         lyrics_register_plugin(path);
82                 }
84                 g_dir_close(dir);
85         }
86 }
88 void lyrics_deinit(void)
89 {
90         guint i;
92         for (i = 0; i < plugins->len; ++i)
93                 free(g_ptr_array_index(plugins, i));
94         g_ptr_array_free(plugins, TRUE);
95 }
97 static void
98 lyrics_next_plugin(struct lyrics_loader *loader);
100 static void
101 lyrics_eof(struct lyrics_loader *loader)
103         int ret, status;
105         g_io_channel_unref(loader->channel);
106         close(loader->fd);
107         loader->fd = -1;
109         ret = waitpid(loader->pid, &status, 0);
110         loader->pid = -1;
112         if (ret < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
113                 g_string_free(loader->data, TRUE);
114                 loader->data = NULL;
116                 lyrics_next_plugin(loader);
117         } else {
118                 loader->callback(loader->data, loader->callback_data);
119         }
122 static gboolean
123 lyrics_data(mpd_unused GIOChannel *source,
124             mpd_unused GIOCondition condition, gpointer data)
126         struct lyrics_loader *loader = data;
127         char buffer[256];
128         ssize_t nbytes;
130         assert(loader != NULL);
131         assert(loader->fd >= 0);
132         assert(loader->pid > 0);
133         assert(source == loader->channel);
135         if ((condition & G_IO_IN) == 0) {
136                 lyrics_eof(loader);
137                 return FALSE;
138         }
140         nbytes = condition & G_IO_IN
141                 ? read(loader->fd, buffer, sizeof(buffer))
142                 : 0;
143         if (nbytes <= 0) {
144                 lyrics_eof(loader);
145                 return FALSE;
146         }
148         g_string_append_len(loader->data, buffer, nbytes);
149         return TRUE;
152 /**
153  * This is a timer callback which calls the lyrics callback "some
154  * timer later".  This solves the problem that lyrics_load() may fail
155  * immediately, leaving its return value in an undefined state.
156  * Instead, install a timer which calls the lyrics callback in the
157  * moment after.
158  */
159 static gboolean
160 lyrics_delayed_fail(gpointer data)
162         struct lyrics_loader *loader = data;
164         assert(loader != NULL);
165         assert(loader->fd < 0);
166         assert(loader->pid < 0);
167         assert(loader->data == NULL);
169         loader->callback(NULL, loader->callback_data);
171         return FALSE;
174 static int
175 lyrics_start_plugin(struct lyrics_loader *loader, const char *plugin_path)
177         int ret, fds[2];
178         pid_t pid;
180         assert(loader != NULL);
181         assert(loader->pid < 0);
182         assert(loader->fd < 0);
183         assert(loader->data == NULL);
185         ret = pipe(fds);
186         if (ret < 0)
187                 return -1;
189         pid = fork();
191         if (pid < 0) {
192                 close(fds[0]);
193                 close(fds[1]);
194                 return -1;
195         }
197         if (pid == 0) {
198                 dup2(fds[1], 1);
199                 dup2(fds[1], 1);
200                 close(fds[0]);
201                 close(fds[1]);
202                 close(0);
203                 /* XXX close other fds? */
205                 execl(plugin_path, plugin_path,
206                       loader->artist, loader->title, NULL);
207                 _exit(1);
208         }
210         close(fds[1]);
212         loader->pid = pid;
213         loader->fd = fds[0];
214         loader->data = g_string_new(NULL);
216         /* XXX CLOEXEC? */
218         loader->channel = g_io_channel_unix_new(loader->fd);
219         loader->event_id = g_io_add_watch(loader->channel, G_IO_IN|G_IO_HUP,
220                                           lyrics_data, loader);
222         return 0;
225 static void
226 lyrics_next_plugin(struct lyrics_loader *loader)
228         const char *plugin_path;
229         int ret = -1;
231         assert(loader->pid < 0);
232         assert(loader->fd < 0);
233         assert(loader->data == NULL);
235         if (loader->next_plugin >= plugins->len) {
236                 /* no plugins left */
237                 g_timeout_add(0, lyrics_delayed_fail, loader);
238                 return;
239         }
241         plugin_path = g_ptr_array_index(plugins, loader->next_plugin++);
242         ret = lyrics_start_plugin(loader, plugin_path);
243         if (ret < 0) {
244                 /* system error */
245                 g_timeout_add(0, lyrics_delayed_fail, loader);
246                 return;
247         }
250 struct lyrics_loader *
251 lyrics_load(const char *artist, const char *title,
252             lyrics_callback_t callback, void *data)
254         struct lyrics_loader *loader = g_new(struct lyrics_loader, 1);
256         assert(artist != NULL);
257         assert(title != NULL);
259         if (loader == NULL)
260                 return NULL;
262         loader->artist = g_strdup(artist);
263         loader->title = g_strdup(title);
264         loader->callback = callback;
265         loader->data = data;
266         loader->next_plugin = 0;
267         loader->pid = -1;
268         loader->fd = -1;
269         loader->data = NULL;
271         lyrics_next_plugin(loader);
273         return loader;
276 void
277 lyrics_free(struct lyrics_loader *loader)
279         if (loader->fd >= 0) {
280                 g_source_remove(loader->event_id);
281                 g_io_channel_unref(loader->channel);
282                 close(loader->fd);
283         }
285         if (loader->pid > 0) {
286                 int status;
288                 kill(loader->pid, SIGTERM);
289                 waitpid(loader->pid, &status, 0);
290         }
292         if (loader->data != NULL)
293                 g_string_free(loader->data, TRUE);