[wip] Retire move_image(), hide_image() and associated osdep routines; hide_image_reg...
[elinks/images.git] / src / osdep / unix / image.c
blobc143643e08574249d1c19fd828ca77e94ab72e6b
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_hide_image_region(struct image_ctx *img, struct box *region)
87 unsigned char buf[4096];
89 snprintf(buf, sizeof(buf), "6;%d;%d;%d;%d\n", region->x, region->y, region->width, region->height);
90 safe_write(img->pipe_to[1], buf, strlen(buf));
93 void
94 slave_sync_images(struct image_ctx *img)
96 /* XSync() */
97 safe_write(img->pipe_to[1], "3;\n", 3);
98 /* Return '\n' for synchronization */
99 safe_write(img->pipe_to[1], "4;\n", 3);
100 unsigned char nl;
101 if (safe_read(img->pipe_from[0], &nl, 1) < 0) {
102 INTERNAL("Communication with w3mimgdisplay failed.");
107 void
108 image_size(unsigned char *file, int *w, int *h)
110 unsigned char pcmd[4096];
111 FILE *p;
113 snprintf(pcmd, sizeof(pcmd), "%s -size %s", DEFAULT_IMGHELPER_CMD, file);
114 p = popen(pcmd, "r");
115 if (!p) return; // XXX
116 fscanf(p, "%d %d", w, h);
117 pclose(p);
120 #endif