4 * Copyright (C) 2009 by ProFUSION embedded systems
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library;
18 * if not, see <http://www.gnu.org/licenses/>.
20 * @author Rafael Antognolli <antognolli@profusion.mobi>
36 #include "ethumbd_private.h"
38 #define DBG(...) EINA_LOG_DOM_DBG(_log_domain, __VA_ARGS__)
39 #define INF(...) EINA_LOG_DOM_INFO(_log_domain, __VA_ARGS__)
40 #define WRN(...) EINA_LOG_DOM_WARN(_log_domain, __VA_ARGS__)
41 #define ERR(...) EINA_LOG_DOM_ERR(_log_domain, __VA_ARGS__)
45 static int _log_domain = -1;
49 Ecore_Fd_Handler *fd_handler;
50 Ethumb *ethumbt[NETHUMBS];
55 _ec_read_safe(int fd, void *buf, ssize_t size)
67 r = read(fd, p, todo);
77 if (errno == EINTR || errno == EAGAIN)
81 ERR("could not read from fd %d: %s",
92 _ec_write_safe(int fd, const void *buf, ssize_t size)
104 r = write(fd, p, todo);
114 if (errno == EINTR || errno == EAGAIN)
118 ERR("could not write to fd %d: %s", fd, strerror(errno));
128 _ec_pipe_str_read(struct _Ethumbd_Child *ec, char **str)
134 r = _ec_read_safe(STDIN_FILENO, &size, sizeof(size));
147 r = _ec_read_safe(STDIN_FILENO, buf, size);
158 static struct _Ethumbd_Child *
161 struct _Ethumbd_Child *ec = calloc(1, sizeof(*ec));
167 _ec_free(struct _Ethumbd_Child *ec)
172 ecore_main_fd_handler_del(ec->fd_handler);
174 for (i = 0; i < NETHUMBS; i++)
177 ethumb_free(ec->ethumbt[i]);
184 _ec_op_new(struct _Ethumbd_Child *ec)
189 r = _ec_read_safe(STDIN_FILENO, &index, sizeof(index));
193 DBG("ethumbd new(). index = %d", index);
195 ec->ethumbt[index] = ethumb_new();
200 _ec_op_del(struct _Ethumbd_Child *ec)
205 r = _ec_read_safe(STDIN_FILENO, &index, sizeof(index));
209 DBG("ethumbd del(). index = %d", index);
211 ethumb_free(ec->ethumbt[index]);
212 ec->ethumbt[index] = NULL;
217 _ec_op_generated_cb(void *data, Ethumb *e, Eina_Bool success)
219 const char *thumb_path, *thumb_key;
220 int size_path, size_key, size_cmd;
222 DBG("thumb generated!");
223 ethumb_thumb_path_get(e, &thumb_path, &thumb_key);
228 size_path = strlen(thumb_path) + 1;
233 size_key = strlen(thumb_key) + 1;
235 size_cmd = sizeof(success) + sizeof(size_path) + size_path +
236 sizeof(size_key) + size_key;
238 _ec_write_safe(STDOUT_FILENO, &size_cmd, sizeof(size_cmd));
239 _ec_write_safe(STDOUT_FILENO, &success, sizeof(success));
241 _ec_write_safe(STDOUT_FILENO, &size_path, sizeof(size_path));
242 _ec_write_safe(STDOUT_FILENO, thumb_path, size_path);
244 _ec_write_safe(STDOUT_FILENO, &size_key, sizeof(size_key));
245 _ec_write_safe(STDOUT_FILENO, thumb_key, size_key);
249 _ec_op_generate(struct _Ethumbd_Child *ec)
252 char *path, *key, *thumb_path, *thumb_key;
255 r = _ec_read_safe(STDIN_FILENO, &index, sizeof(index));
259 r = _ec_pipe_str_read(ec, &path);
262 r = _ec_pipe_str_read(ec, &key);
265 r = _ec_pipe_str_read(ec, &thumb_path);
268 r = _ec_pipe_str_read(ec, &thumb_key);
272 ethumb_file_set(ec->ethumbt[index], path, key);
273 ethumb_thumb_path_set(ec->ethumbt[index], thumb_path, thumb_key);
275 if (ethumb_exists(ec->ethumbt[index]))
277 _ec_op_generated_cb(ec, ec->ethumbt[index], EINA_TRUE);
281 ethumb_generate(ec->ethumbt[index], _ec_op_generated_cb, ec, NULL);
293 _ec_fdo_set(struct _Ethumbd_Child *ec, Ethumb *e)
298 r = _ec_read_safe(STDIN_FILENO, &value, sizeof(value));
301 ethumb_thumb_fdo_set(e, value);
302 DBG("fdo = %d", value);
308 _ec_size_set(struct _Ethumbd_Child *ec, Ethumb *e)
314 r = _ec_read_safe(STDIN_FILENO, &w, sizeof(w));
317 r = _ec_read_safe(STDIN_FILENO, &type, sizeof(type));
320 r = _ec_read_safe(STDIN_FILENO, &h, sizeof(h));
323 ethumb_thumb_size_set(e, w, h);
324 DBG("size = %dx%d", w, h);
330 _ec_format_set(struct _Ethumbd_Child *ec, Ethumb *e)
335 r = _ec_read_safe(STDIN_FILENO, &value, sizeof(value));
338 ethumb_thumb_format_set(e, value);
339 DBG("format = %d", value);
345 _ec_aspect_set(struct _Ethumbd_Child *ec, Ethumb *e)
350 r = _ec_read_safe(STDIN_FILENO, &value, sizeof(value));
353 ethumb_thumb_aspect_set(e, value);
354 DBG("aspect = %d", value);
360 _ec_orientation_set(struct _Ethumbd_Child *ec, Ethumb *e)
365 r = _ec_read_safe(STDIN_FILENO, &value, sizeof(value));
368 ethumb_thumb_orientation_set(e, value);
369 DBG("orientation = %d", value);
375 _ec_crop_set(struct _Ethumbd_Child *ec, Ethumb *e)
381 r = _ec_read_safe(STDIN_FILENO, &x, sizeof(x));
384 r = _ec_read_safe(STDIN_FILENO, &type, sizeof(type));
387 r = _ec_read_safe(STDIN_FILENO, &y, sizeof(y));
390 ethumb_thumb_crop_align_set(e, x, y);
391 DBG("crop = %fx%f", x, y);
397 _ec_quality_set(struct _Ethumbd_Child *ec, Ethumb *e)
402 r = _ec_read_safe(STDIN_FILENO, &value, sizeof(value));
405 ethumb_thumb_quality_set(e, value);
406 DBG("quality = %d", value);
412 _ec_compress_set(struct _Ethumbd_Child *ec, Ethumb *e)
417 r = _ec_read_safe(STDIN_FILENO, &value, sizeof(value));
420 ethumb_thumb_compress_set(e, value);
421 DBG("compress = %d", value);
427 _ec_frame_set(struct _Ethumbd_Child *ec, Ethumb *e)
431 char *theme_file, *group, *swallow;
433 r = _ec_pipe_str_read(ec, &theme_file);
436 r = _ec_read_safe(STDIN_FILENO, &type, sizeof(type));
439 r = _ec_pipe_str_read(ec, &group);
442 r = _ec_read_safe(STDIN_FILENO, &type, sizeof(type));
445 r = _ec_pipe_str_read(ec, &swallow);
448 DBG("frame = %s:%s:%s", theme_file, group, swallow);
449 ethumb_frame_set(e, theme_file, group, swallow);
458 _ec_directory_set(struct _Ethumbd_Child *ec, Ethumb *e)
463 r = _ec_pipe_str_read(ec, &directory);
466 ethumb_thumb_dir_path_set(e, directory);
467 DBG("directory = %s", directory);
474 _ec_category_set(struct _Ethumbd_Child *ec, Ethumb *e)
479 r = _ec_pipe_str_read(ec, &category);
482 ethumb_thumb_category_set(e, category);
483 DBG("category = %s", category);
490 _ec_video_time_set(struct _Ethumbd_Child *ec, Ethumb *e)
495 r = _ec_read_safe(STDIN_FILENO, &value, sizeof(value));
498 ethumb_video_time_set(e, value);
499 DBG("video_time = %f", value);
505 _ec_video_start_set(struct _Ethumbd_Child *ec, Ethumb *e)
510 r = _ec_read_safe(STDIN_FILENO, &value, sizeof(value));
513 ethumb_video_start_set(e, value);
514 DBG("video_start = %f", value);
520 _ec_video_interval_set(struct _Ethumbd_Child *ec, Ethumb *e)
525 r = _ec_read_safe(STDIN_FILENO, &value, sizeof(value));
528 ethumb_video_interval_set(e, value);
529 DBG("video_interval = %f", value);
535 _ec_video_ntimes_set(struct _Ethumbd_Child *ec, Ethumb *e)
540 r = _ec_read_safe(STDIN_FILENO, &value, sizeof(value));
543 ethumb_video_ntimes_set(e, value);
544 DBG("video_ntimes = %d", value);
550 _ec_video_fps_set(struct _Ethumbd_Child *ec, Ethumb *e)
555 r = _ec_read_safe(STDIN_FILENO, &value, sizeof(value));
558 ethumb_video_fps_set(e, value);
559 DBG("video_fps = %d", value);
565 _ec_document_page_set(struct _Ethumbd_Child *ec, Ethumb *e)
570 r = _ec_read_safe(STDIN_FILENO, &value, sizeof(value));
573 ethumb_document_page_set(e, value);
574 DBG("document_page = %d", value);
580 _ec_setup_process(struct _Ethumbd_Child *ec, int index, int type)
584 e = ec->ethumbt[index];
595 _ec_format_set(ec, e);
598 _ec_aspect_set(ec, e);
600 case ETHUMBD_ORIENTATION:
601 _ec_orientation_set(ec, e);
606 case ETHUMBD_QUALITY:
607 _ec_quality_set(ec, e);
609 case ETHUMBD_COMPRESS:
610 _ec_compress_set(ec, e);
612 case ETHUMBD_FRAME_FILE:
613 _ec_frame_set(ec, e);
615 case ETHUMBD_DIRECTORY:
616 _ec_directory_set(ec, e);
618 case ETHUMBD_CATEGORY:
619 _ec_category_set(ec, e);
621 case ETHUMBD_VIDEO_TIME:
622 _ec_video_time_set(ec, e);
624 case ETHUMBD_VIDEO_START:
625 _ec_video_start_set(ec, e);
627 case ETHUMBD_VIDEO_INTERVAL:
628 _ec_video_interval_set(ec, e);
630 case ETHUMBD_VIDEO_NTIMES:
631 _ec_video_ntimes_set(ec, e);
633 case ETHUMBD_VIDEO_FPS:
634 _ec_video_fps_set(ec, e);
636 case ETHUMBD_DOCUMENT_PAGE:
637 _ec_document_page_set(ec, e);
645 _ec_op_setup(struct _Ethumbd_Child *ec)
651 r = _ec_read_safe(STDIN_FILENO, &index, sizeof(index));
655 r = _ec_read_safe(STDIN_FILENO, &type, sizeof(type));
658 while (type != ETHUMBD_SETUP_FINISHED)
660 _ec_setup_process(ec, index, type);
661 r = _ec_read_safe(STDIN_FILENO, &type, sizeof(type));
670 _ec_fd_handler(void *data, Ecore_Fd_Handler *fd_handler)
672 struct _Ethumbd_Child *ec = data;
676 if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_ERROR))
678 ERR("error on pipein! child exiting...");
679 ec->fd_handler = NULL;
680 ecore_main_loop_quit();
684 r = _ec_read_safe(STDIN_FILENO, &op_id, sizeof(op_id));
687 DBG("ethumbd exited! child exiting...");
688 ec->fd_handler = NULL;
689 ecore_main_loop_quit();
693 DBG("received op: %d", op_id);
700 case ETHUMBD_OP_GENERATE:
701 r = _ec_op_generate(ec);
703 case ETHUMBD_OP_SETUP:
704 r = _ec_op_setup(ec);
710 ERR("invalid operation: %d", op_id);
716 ERR("ethumbd exited! child exiting...");
717 ec->fd_handler = NULL;
718 ecore_main_loop_quit();
725 _ec_setup(struct _Ethumbd_Child *ec)
727 ec->fd_handler = ecore_main_fd_handler_add(
728 STDIN_FILENO, ECORE_FD_READ | ECORE_FD_ERROR,
729 _ec_fd_handler, ec, NULL, NULL);
733 main(int argc, const char *argv[])
735 struct _Ethumbd_Child *ec;
741 _log_domain = eina_log_domain_register("ethumbd_child", NULL);
745 EINA_LOG_CRIT("could not register log domain 'ethumbd_child'");
755 DBG("child started!");
756 ecore_main_loop_begin();
757 DBG("child finishing.");
761 if (_log_domain >= 0)
763 eina_log_domain_unregister(_log_domain);