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 #include <lightmediascanner_plugin.h>
31 #include <lightmediascanner_utils.h>
32 #include <lightmediascanner_db.h>
33 #include <sys/types.h>
43 static inline unsigned int
44 _chunk_to_uint(unsigned char *buf)
46 return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
50 _png_data_get(int fd, struct lms_image_info *info)
52 unsigned char buf[16], *p;
53 const unsigned char sig[8] = {0x89, 0x50, 0x4e, 0x47, 0xd, 0xa, 0x1a, 0xa};
54 const unsigned char ihdr[4] = {'I', 'H', 'D', 'R'};
57 if (read(fd, buf, sizeof(buf)) != sizeof(buf)) {
62 if (memcmp(buf, sig, sizeof(sig)) != 0) {
63 fprintf(stderr, "ERROR: invalid PNG signature.\n");
67 p = buf + sizeof(sig) + 4;
68 if (memcmp(p, ihdr, sizeof(ihdr)) != 0) {
69 fprintf(stderr, "ERROR: invalid first chunk: %4.4s.\n", p);
73 p = buf + sizeof(sig);
74 length = _chunk_to_uint(p);
76 fprintf(stderr, "ERROR: IHDR chunk size is too small: %d.\n", length);
80 if (read(fd, buf, 8) != 8) {
84 info->width = _chunk_to_uint(buf);
85 info->height = _chunk_to_uint(buf + 4);
90 static const char _name[] = "png";
91 static const struct lms_string_size _exts[] = {
92 LMS_STATIC_STRING_SIZE(".png")
94 static const char *_cats[] = {
99 static const char *_authors[] = {
100 "Gustavo Sverzut Barbieri",
105 struct lms_plugin plugin;
106 lms_db_image_t *img_db;
110 _match(struct plugin *p, const char *path, int len, int base)
114 i = lms_which_extension(path, len, _exts, LMS_ARRAY_SIZE(_exts));
118 return (void*)(i + 1);
122 _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_info *finfo, void *match)
124 struct lms_image_info info = { };
127 fd = open(finfo->path, O_RDONLY);
133 if (_png_data_get(fd, &info) != 0) {
139 info.date = finfo->mtime;
141 if (!info.title.str) {
144 ext_idx = ((long)match) - 1;
145 info.title.len = finfo->path_len - finfo->base - _exts[ext_idx].len;
146 info.title.str = malloc((info.title.len + 1) * sizeof(char));
147 memcpy(info.title.str, finfo->path + finfo->base, info.title.len);
148 info.title.str[info.title.len] = '\0';
152 lms_charset_conv(ctxt->cs_conv, &info.title.str, &info.title.len);
154 lms_charset_conv(ctxt->cs_conv, &info.artist.str, &info.artist.len);
157 r = lms_db_image_add(plugin->img_db, &info);
160 free(info.title.str);
161 free(info.artist.str);
163 posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
170 _setup(struct plugin *plugin, struct lms_context *ctxt)
172 plugin->img_db = lms_db_image_new(ctxt->db);
180 _start(struct plugin *plugin, struct lms_context *ctxt)
182 return lms_db_image_start(plugin->img_db);
186 _finish(struct plugin *plugin, struct lms_context *ctxt)
189 return lms_db_image_free(plugin->img_db);
196 _close(struct plugin *plugin)
202 API struct lms_plugin *
203 lms_plugin_open(void)
205 struct plugin *plugin;
207 plugin = malloc(sizeof(*plugin));
208 plugin->plugin.name = _name;
209 plugin->plugin.match = (lms_plugin_match_fn_t)_match;
210 plugin->plugin.parse = (lms_plugin_parse_fn_t)_parse;
211 plugin->plugin.close = (lms_plugin_close_fn_t)_close;
212 plugin->plugin.setup = (lms_plugin_setup_fn_t)_setup;
213 plugin->plugin.start = (lms_plugin_start_fn_t)_start;
214 plugin->plugin.finish = (lms_plugin_finish_fn_t)_finish;
216 return (struct lms_plugin *)plugin;
219 API const struct lms_plugin_info *
220 lms_plugin_info(void)
222 static struct lms_plugin_info info = {
228 "http://lms.garage.maemo.org"