2 * Copyright (C) 2008-2011 by ProFUSION embedded systems
3 * Copyright (C) 2007 by INdT
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public License
7 * as published by the Free Software Foundation; either version 2.1 of
8 * the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * @author Gustavo Sverzut Barbieri <barbieri@profusion.mobi>
30 #define _XOPEN_SOURCE 600
31 #include <lightmediascanner_plugin.h>
32 #include <lightmediascanner_utils.h>
33 #include <lightmediascanner_db.h>
34 #include <sys/types.h>
44 static inline unsigned int
45 _chunk_to_uint(unsigned char *buf)
47 return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
51 _png_data_get(int fd, struct lms_image_info *info)
53 unsigned char buf[16], *p;
54 const unsigned char sig[8] = {0x89, 0x50, 0x4e, 0x47, 0xd, 0xa, 0x1a, 0xa};
55 const unsigned char ihdr[4] = {'I', 'H', 'D', 'R'};
58 if (read(fd, buf, sizeof(buf)) != sizeof(buf)) {
63 if (memcmp(buf, sig, sizeof(sig)) != 0) {
64 fprintf(stderr, "ERROR: invalid PNG signature.\n");
68 p = buf + sizeof(sig) + 4;
69 if (memcmp(p, ihdr, sizeof(ihdr)) != 0) {
70 fprintf(stderr, "ERROR: invalid first chunk: %4.4s.\n", p);
74 p = buf + sizeof(sig);
75 length = _chunk_to_uint(p);
77 fprintf(stderr, "ERROR: IHDR chunk size is too small: %d.\n", length);
81 if (read(fd, buf, 8) != 8) {
85 info->width = _chunk_to_uint(buf);
86 info->height = _chunk_to_uint(buf + 4);
91 static const char _name[] = "png";
92 static const struct lms_string_size _exts[] = {
93 LMS_STATIC_STRING_SIZE(".png")
95 static const char *_cats[] = {
100 static const char *_authors[] = {
101 "Gustavo Sverzut Barbieri",
106 struct lms_plugin plugin;
107 lms_db_image_t *img_db;
111 _match(struct plugin *p, const char *path, int len, int base)
115 i = lms_which_extension(path, len, _exts, LMS_ARRAY_SIZE(_exts));
119 return (void*)(i + 1);
123 _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_info *finfo, void *match)
125 struct lms_image_info info = { };
128 fd = open(finfo->path, O_RDONLY);
134 if (_png_data_get(fd, &info) != 0) {
140 info.date = finfo->mtime;
142 if (!info.title.str) {
145 ext_idx = ((long)match) - 1;
146 info.title.len = finfo->path_len - finfo->base - _exts[ext_idx].len;
147 info.title.str = malloc((info.title.len + 1) * sizeof(char));
148 memcpy(info.title.str, finfo->path + finfo->base, info.title.len);
149 info.title.str[info.title.len] = '\0';
153 lms_charset_conv(ctxt->cs_conv, &info.title.str, &info.title.len);
155 lms_charset_conv(ctxt->cs_conv, &info.artist.str, &info.artist.len);
158 r = lms_db_image_add(plugin->img_db, &info);
161 free(info.title.str);
162 free(info.artist.str);
164 posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
171 _setup(struct plugin *plugin, struct lms_context *ctxt)
173 plugin->img_db = lms_db_image_new(ctxt->db);
181 _start(struct plugin *plugin, struct lms_context *ctxt)
183 return lms_db_image_start(plugin->img_db);
187 _finish(struct plugin *plugin, struct lms_context *ctxt)
190 return lms_db_image_free(plugin->img_db);
197 _close(struct plugin *plugin)
203 API struct lms_plugin *
204 lms_plugin_open(void)
206 struct plugin *plugin;
208 plugin = malloc(sizeof(*plugin));
209 plugin->plugin.name = _name;
210 plugin->plugin.match = (lms_plugin_match_fn_t)_match;
211 plugin->plugin.parse = (lms_plugin_parse_fn_t)_parse;
212 plugin->plugin.close = (lms_plugin_close_fn_t)_close;
213 plugin->plugin.setup = (lms_plugin_setup_fn_t)_setup;
214 plugin->plugin.start = (lms_plugin_start_fn_t)_start;
215 plugin->plugin.finish = (lms_plugin_finish_fn_t)_finish;
217 return (struct lms_plugin *)plugin;
220 API const struct lms_plugin_info *
221 lms_plugin_info(void)
223 static struct lms_plugin_info info = {
229 "http://lms.garage.maemo.org"