4 * Copyright (C) 2009 by ProFUSION embedded systems
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or (at your
9 * option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 * @author Rafael Antognolli <antognolli@profusion.mobi>
34 #include "ethumbd_private.h"
36 #define DBG(...) EINA_LOG_DBG(__VA_ARGS__)
37 #define INF(...) EINA_LOG_INFO(__VA_ARGS__)
38 #define WRN(...) EINA_LOG_WARN(__VA_ARGS__)
39 #define ERR(...) EINA_LOG_ERR(__VA_ARGS__)
45 Ecore_Fd_Handler *fd_handler;
46 Ethumb *ethumbt[NETHUMBS];
52 _ec_read_safe(int fd, void *buf, ssize_t size)
64 r = read(fd, p, todo);
74 if (errno == EINTR || errno == EAGAIN)
78 ERR("could not read from fd %d: %s",
89 _ec_write_safe(int fd, const void *buf, ssize_t size)
101 r = write(fd, p, todo);
111 if (errno == EINTR || errno == EAGAIN)
115 ERR("could not write to fd %d: %s", fd, strerror(errno));
125 _ec_pipe_str_read(struct _Ethumbd_Child *ec, char **str)
131 r = _ec_read_safe(ec->pipein, &size, sizeof(size));
144 r = _ec_read_safe(ec->pipein, buf, size);
156 _ec_pipe_str_write(struct _Ethumbd_Child *ec, const char *str)
163 size = strlen(str) + 1;
165 _ec_write_safe(ec->pipeout, &size, sizeof(size));
166 _ec_write_safe(ec->pipeout, str, size);
169 static struct _Ethumbd_Child *
170 _ec_new(int pipein, int pipeout)
172 struct _Ethumbd_Child *ec = calloc(1, sizeof(*ec));
175 ec->pipeout = pipeout;
181 _ec_free(struct _Ethumbd_Child *ec)
186 ecore_main_fd_handler_del(ec->fd_handler);
188 for (i = 0; i < NETHUMBS; i++)
191 ethumb_free(ec->ethumbt[i]);
198 _ec_op_new(struct _Ethumbd_Child *ec)
203 r = _ec_read_safe(ec->pipein, &index, sizeof(index));
207 DBG("ethumbd new(). index = %d\n", index);
209 ec->ethumbt[index] = ethumb_new();
214 _ec_op_del(struct _Ethumbd_Child *ec)
219 r = _ec_read_safe(ec->pipein, &index, sizeof(index));
223 DBG("ethumbd del(). index = %d\n", index);
225 ethumb_free(ec->ethumbt[index]);
226 ec->ethumbt[index] = NULL;
231 _ec_op_generated_cb(void *data, Ethumb *e, Eina_Bool success)
233 struct _Ethumbd_Child *ec = data;
234 const char *thumb_path, *thumb_key;
236 DBG("thumb generated!\n");
237 ethumb_thumb_path_get(e, &thumb_path, &thumb_key);
238 _ec_write_safe(ec->pipeout, &success, sizeof(success));
240 _ec_pipe_str_write(ec, thumb_path);
241 _ec_pipe_str_write(ec, thumb_key);
245 _ec_op_generate(struct _Ethumbd_Child *ec)
248 char *path, *key, *thumb_path, *thumb_key;
251 r = _ec_read_safe(ec->pipein, &index, sizeof(index));
255 r = _ec_pipe_str_read(ec, &path);
258 r = _ec_pipe_str_read(ec, &key);
261 r = _ec_pipe_str_read(ec, &thumb_path);
264 r = _ec_pipe_str_read(ec, &thumb_key);
268 ethumb_file_set(ec->ethumbt[index], path, key);
269 ethumb_thumb_path_set(ec->ethumbt[index], thumb_path, thumb_key);
270 ethumb_generate(ec->ethumbt[index], _ec_op_generated_cb, ec, NULL);
281 _ec_fdo_set(struct _Ethumbd_Child *ec, Ethumb *e)
286 r = _ec_read_safe(ec->pipein, &value, sizeof(value));
289 ethumb_thumb_fdo_set(e, value);
290 DBG("fdo = %d\n", value);
296 _ec_size_set(struct _Ethumbd_Child *ec, Ethumb *e)
302 r = _ec_read_safe(ec->pipein, &w, sizeof(w));
305 r = _ec_read_safe(ec->pipein, &type, sizeof(type));
308 r = _ec_read_safe(ec->pipein, &h, sizeof(h));
311 ethumb_thumb_size_set(e, w, h);
312 DBG("size = %dx%d\n", w, h);
318 _ec_format_set(struct _Ethumbd_Child *ec, Ethumb *e)
323 r = _ec_read_safe(ec->pipein, &value, sizeof(value));
326 ethumb_thumb_format_set(e, value);
327 DBG("format = %d\n", value);
333 _ec_aspect_set(struct _Ethumbd_Child *ec, Ethumb *e)
338 r = _ec_read_safe(ec->pipein, &value, sizeof(value));
341 ethumb_thumb_aspect_set(e, value);
342 DBG("aspect = %d\n", value);
348 _ec_crop_set(struct _Ethumbd_Child *ec, Ethumb *e)
354 r = _ec_read_safe(ec->pipein, &x, sizeof(x));
357 r = _ec_read_safe(ec->pipein, &type, sizeof(type));
360 r = _ec_read_safe(ec->pipein, &y, sizeof(y));
363 ethumb_thumb_crop_align_set(e, x, y);
364 DBG("crop = %fx%f\n", x, y);
370 _ec_quality_set(struct _Ethumbd_Child *ec, Ethumb *e)
375 r = _ec_read_safe(ec->pipein, &value, sizeof(value));
378 ethumb_thumb_quality_set(e, value);
379 DBG("quality = %d\n", value);
385 _ec_compress_set(struct _Ethumbd_Child *ec, Ethumb *e)
390 r = _ec_read_safe(ec->pipein, &value, sizeof(value));
393 ethumb_thumb_compress_set(e, value);
394 DBG("compress = %d\n", value);
400 _ec_frame_set(struct _Ethumbd_Child *ec, Ethumb *e)
404 char *theme_file, *group, *swallow;
406 r = _ec_pipe_str_read(ec, &theme_file);
409 r = _ec_read_safe(ec->pipein, &type, sizeof(type));
412 r = _ec_pipe_str_read(ec, &group);
415 r = _ec_read_safe(ec->pipein, &type, sizeof(type));
418 r = _ec_pipe_str_read(ec, &swallow);
421 DBG("frame = %s:%s:%s\n", theme_file, group, swallow);
422 ethumb_frame_set(e, theme_file, group, swallow);
431 _ec_directory_set(struct _Ethumbd_Child *ec, Ethumb *e)
436 r = _ec_pipe_str_read(ec, &directory);
439 ethumb_thumb_dir_path_set(e, directory);
440 DBG("directory = %s\n", directory);
447 _ec_category_set(struct _Ethumbd_Child *ec, Ethumb *e)
452 r = _ec_pipe_str_read(ec, &category);
455 ethumb_thumb_category_set(e, category);
456 DBG("category = %s\n", category);
463 _ec_video_time_set(struct _Ethumbd_Child *ec, Ethumb *e)
468 r = _ec_read_safe(ec->pipein, &value, sizeof(value));
471 ethumb_video_time_set(e, value);
472 DBG("video_time = %f\n", value);
478 _ec_video_start_set(struct _Ethumbd_Child *ec, Ethumb *e)
483 r = _ec_read_safe(ec->pipein, &value, sizeof(value));
486 ethumb_video_start_set(e, value);
487 DBG("video_start = %f\n", value);
493 _ec_video_interval_set(struct _Ethumbd_Child *ec, Ethumb *e)
498 r = _ec_read_safe(ec->pipein, &value, sizeof(value));
501 ethumb_video_interval_set(e, value);
502 DBG("video_interval = %f\n", value);
508 _ec_video_ntimes_set(struct _Ethumbd_Child *ec, Ethumb *e)
513 r = _ec_read_safe(ec->pipein, &value, sizeof(value));
516 ethumb_video_ntimes_set(e, value);
517 DBG("video_ntimes = %d\n", value);
523 _ec_video_fps_set(struct _Ethumbd_Child *ec, Ethumb *e)
528 r = _ec_read_safe(ec->pipein, &value, sizeof(value));
531 ethumb_video_fps_set(e, value);
532 DBG("video_fps = %d\n", value);
538 _ec_document_page_set(struct _Ethumbd_Child *ec, Ethumb *e)
543 r = _ec_read_safe(ec->pipein, &value, sizeof(value));
546 ethumb_document_page_set(e, value);
547 DBG("document_page = %d\n", value);
553 _ec_setup_process(struct _Ethumbd_Child *ec, int index, int type)
557 e = ec->ethumbt[index];
568 _ec_format_set(ec, e);
571 _ec_aspect_set(ec, e);
576 case ETHUMBD_QUALITY:
577 _ec_quality_set(ec, e);
579 case ETHUMBD_COMPRESS:
580 _ec_compress_set(ec, e);
582 case ETHUMBD_FRAME_FILE:
583 _ec_frame_set(ec, e);
585 case ETHUMBD_DIRECTORY:
586 _ec_directory_set(ec, e);
588 case ETHUMBD_CATEGORY:
589 _ec_category_set(ec, e);
591 case ETHUMBD_VIDEO_TIME:
592 _ec_video_time_set(ec, e);
594 case ETHUMBD_VIDEO_START:
595 _ec_video_start_set(ec, e);
597 case ETHUMBD_VIDEO_INTERVAL:
598 _ec_video_interval_set(ec, e);
600 case ETHUMBD_VIDEO_NTIMES:
601 _ec_video_ntimes_set(ec, e);
603 case ETHUMBD_VIDEO_FPS:
604 _ec_video_fps_set(ec, e);
606 case ETHUMBD_DOCUMENT_PAGE:
607 _ec_document_page_set(ec, e);
610 ERR("wrong type!\n");
615 _ec_op_setup(struct _Ethumbd_Child *ec)
621 r = _ec_read_safe(ec->pipein, &index, sizeof(index));
625 r = _ec_read_safe(ec->pipein, &type, sizeof(type));
628 while (type != ETHUMBD_SETUP_FINISHED)
630 _ec_setup_process(ec, index, type);
631 r = _ec_read_safe(ec->pipein, &type, sizeof(type));
640 _ec_fd_handler(void *data, Ecore_Fd_Handler *fd_handler)
642 struct _Ethumbd_Child *ec = data;
646 if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_ERROR))
648 ERR("error on pipein! child exiting...\n");
649 ec->fd_handler = NULL;
650 ecore_main_loop_quit();
654 r = _ec_read_safe(ec->pipein, &op_id, sizeof(op_id));
657 DBG("ethumbd exited! child exiting...\n");
658 ec->fd_handler = NULL;
659 ecore_main_loop_quit();
663 DBG("received op: %d\n", op_id);
671 case ETHUMBD_OP_GENERATE:
672 r = _ec_op_generate(ec);
674 case ETHUMBD_OP_SETUP:
675 r = _ec_op_setup(ec);
681 ERR("invalid operation: %d\n", op_id);
687 ERR("ethumbd exited! child exiting...\n");
688 ec->fd_handler = NULL;
689 ecore_main_loop_quit();
696 _ec_setup(struct _Ethumbd_Child *ec)
698 ec->fd_handler = ecore_main_fd_handler_add(
699 ec->pipein, ECORE_FD_READ | ECORE_FD_ERROR,
700 _ec_fd_handler, ec, NULL, NULL);
704 ethumbd_child_start(int pipein, int pipeout)
706 struct _Ethumbd_Child *ec;
710 ec = _ec_new(pipein, pipeout);
714 DBG("child started!\n");
715 ecore_main_loop_begin();
716 DBG("child finishing.\n");