[wip] slave_render_image(), slave_hide_image_region(): Accept struct box as a parameter
[elinks/images.git] / src / osdep / unix / image.c
blobdc675caa4d76669a4b863c323043e9c36e84c02c
1 /* Image rendering. */
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <unistd.h>
11 #include "elinks.h"
13 #include "osdep/image.h"
14 #include "osdep/osdep.h"
15 #include "util/error.h"
17 #ifdef CONFIG_OS_UNIX
19 void
20 image_helper(struct image_ctx *img, int xfd)
22 close(0); close(1);
23 dup2(img->pipe_to[0], 0); dup2(img->pipe_from[1], 1);
24 close(img->pipe_to[0]); close(img->pipe_to[1]);
25 close(img->pipe_from[0]); close(img->pipe_from[1]);
26 execl(DEFAULT_IMGHELPER_CMD, DEFAULT_IMGHELPER_CMD, NULL);
29 struct image_ctx *
30 init_images(void)
32 struct image_ctx *img = calloc(1, sizeof(*img));
34 setenv("W3M_TTY", ttyname(0), 0);
36 c_pipe(img->pipe_to);
37 c_pipe(img->pipe_from);
38 close(start_thread((void (*)(void *, int)) image_helper, img, 0));
39 close(img->pipe_to[0]);
40 close(img->pipe_from[1]);
42 return img;
45 void
46 done_images(struct image_ctx *img)
48 safe_write(img->pipe_to[1], "2;\n", 3);
49 close(img->pipe_to[1]);
50 close(img->pipe_from[0]);
51 free(img);
55 void
56 slave_images_getres(struct image_ctx *img, int *xres, int *yres)
58 FILE *p;
60 p = popen(DEFAULT_IMGHELPER_CMD " -test", "r");
61 if (!p) return; // XXX
62 fscanf(p, "%d %d", &img->xres, &img->yres);
63 pclose(p);
65 if (xres) *xres = img->xres;
66 if (yres) *yres = img->yres;
69 void
70 slave_render_image(struct image_ctx *img, int imgid, struct box *box, unsigned char *file)
72 unsigned char buf[4096];
74 if (img->images[imgid].file)
75 free(img->images[imgid].file);
76 img->images[imgid].file = strdup(file);
77 copy_box(&img->images[imgid].box, box);
78 img->images[imgid].visible = 1;
80 snprintf(buf, sizeof(buf), "0;%d;%d;%d;%d;%d;%d;%d;%d;%d;%s\n", imgid, img->images[imgid].box.x, img->images[imgid].box.y, img->images[imgid].box.width, img->images[imgid].box.height, 0, 0, 0, 0, img->images[imgid].file);
81 safe_write(img->pipe_to[1], buf, strlen(buf));
84 void
85 slave_move_image(struct image_ctx *img, int imgid, int x, int y)
87 unsigned char buf[4096];
89 slave_hide_image(img, imgid);
91 img->images[imgid].box.x = x;
92 img->images[imgid].box.y = y;
93 img->images[imgid].visible = 1;
95 snprintf(buf, sizeof(buf), "1;%d;%d;%d;%d;%d;%d;%d;%d;%d;%s\n", imgid, img->images[imgid].box.x, img->images[imgid].box.y, img->images[imgid].box.width, img->images[imgid].box.height, 0, 0, 0, 0, img->images[imgid].file);
96 safe_write(img->pipe_to[1], buf, strlen(buf));
99 void
100 slave_hide_image(struct image_ctx *img, int imgid)
102 unsigned char buf[4096];
104 img->images[imgid].visible = 0;
106 snprintf(buf, sizeof(buf), "6;%d;%d;%d;%d\n", img->images[imgid].box.x, img->images[imgid].box.y, img->images[imgid].box.width, img->images[imgid].box.height);
107 safe_write(img->pipe_to[1], buf, strlen(buf));
110 /* slave_hide_image_region is common in osdep/image.c */
112 void
113 slave_sync_images(struct image_ctx *img)
115 /* XSync() */
116 safe_write(img->pipe_to[1], "3;\n", 3);
117 /* Return '\n' for synchronization */
118 safe_write(img->pipe_to[1], "4;\n", 3);
119 unsigned char nl;
120 if (safe_read(img->pipe_from[0], &nl, 1) < 0) {
121 INTERNAL("Communication with w3mimgdisplay failed.");
126 void
127 image_size(unsigned char *file, int *w, int *h)
129 unsigned char pcmd[4096];
130 FILE *p;
132 snprintf(pcmd, sizeof(pcmd), "%s -size %s", DEFAULT_IMGHELPER_CMD, file);
133 p = popen(pcmd, "r");
134 if (!p) return; // XXX
135 fscanf(p, "%d %d", w, h);
136 pclose(p);
139 #endif