[wip] show_image(): Also take struct box
[elinks/images.git] / src / terminal / image.c
blobebdcda6bd30b0601711cbc22c3115411f6fd1d5c
1 /** Support for image rendering
2 * @file
3 */
5 #ifdef HAVE_CONFIG_H
6 #include "config.h"
7 #endif
9 #include <fcntl.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <unistd.h>
16 #include "elinks.h"
18 #include "cache/cache.h"
19 #include "osdep/image.h"
20 #include "osdep/osdep.h"
21 #include "terminal/terminal.h"
22 #include "terminal/image.h"
23 #include "util/error.h"
24 #include "util/memory.h"
25 #include "util/string.h"
28 void
29 show_image(struct terminal *term, int imgid, struct box *box, struct cache_entry *ce)
31 struct string s;
32 int fd;
33 struct fragment *f;
35 #if 0
36 if (!term->imgfiles[imgid]) {
37 /* FIXME: We hardcode /tmp here. */
38 term->imgfiles[imgid] = stracpy("/tmp/elinksimgXXXXXX");
39 fd = mkstemp(term->imgfiles[imgid]);
40 } else {
41 fd = open(term->imgfiles[imgid], O_WRONLY);
43 #else
44 if (term->imgfiles[imgid]) {
45 unlink(term->imgfiles[imgid]);
46 mem_free(term->imgfiles[imgid]);
48 /* FIXME: We hardcode /tmp here. */
49 term->imgfiles[imgid] = stracpy("/tmp/elinksimgXXXXXX");
50 fd = mkstemp(term->imgfiles[imgid]);
51 #endif
52 f = get_cache_fragment(ce);
53 assert(f->offset == 0);
54 safe_write(fd, f->data, f->length);
55 close(fd);
57 if (!init_string(&s))
58 return;
59 add_format_to_string(&s, "%d %d %d %s %d %d", imgid, box->x, box->y,
60 term->imgfiles[imgid], box->width, box->height);
61 do_terminal_function(term, TERM_FN_IMG_DRAW, s.source);
62 done_string(&s);
65 void
66 move_image(struct terminal *term, int imgid, int x, int y)
68 struct string s;
70 if (!init_string(&s))
71 return;
72 add_format_to_string(&s, "%d %d %d", imgid, x, y);
73 do_terminal_function(term, TERM_FN_IMG_MOVE, s.source);
74 done_string(&s);
77 void
78 hide_image(struct terminal *term, int imgid)
80 struct string s;
82 if (!init_string(&s))
83 return;
84 add_format_to_string(&s, "%d", imgid);
85 do_terminal_function(term, TERM_FN_IMG_HIDE, s.source);
86 done_string(&s);
89 void
90 sync_images(struct terminal *term)
92 do_terminal_function(term, TERM_FN_IMG_SYNC, "");
97 void
98 get_image_size(struct cache_entry *ce, int *w, int *h)
100 int fd;
101 struct fragment *f;
102 unsigned char tmpfile[] = "/tmp/elinksimgXXXXXX";
104 fd = mkstemp(tmpfile);
105 f = get_cache_fragment(ce);
106 assert(f->offset == 0);
107 safe_write(fd, f->data, f->length);
108 close(fd);
110 image_size(tmpfile, w, h);
112 unlink(tmpfile);