1 /* $Id: w3mimgdisplay.c,v 1.18 2003/07/13 16:20:42 ukai Exp $ */
9 #include "w3mimg/w3mimg.h"
12 static char *background = NULL;
13 static int offset_x = 0, offset_y = 0;
14 static int defined_bg = 0, defined_x = 0, defined_y = 0, defined_test = 0;
15 static int defined_debug = 0;
16 static char *defined_size = NULL;
18 #define MAX_IMAGE 1000
19 static W3MImage *imageBuf = NULL;
20 static int maxImage = 0, maxAnim = 100, clearMargin = 0;
22 static void GetOption(int argc, char **argv);
23 static void DrawImage(char *buf, int redraw);
24 static void TermImage(void);
25 static void ClearImage(char *buf);
28 main(int argc, char **argv)
32 #ifdef W3MIMGDISPLAY_SETUID
33 uid_t runner_uid = getuid();
34 uid_t owner_uid = geteuid();
36 /* swap real and effective */
37 setreuid(owner_uid, runner_uid);
39 GetOption(argc, argv);
41 freopen(DEV_NULL_PATH, "w", stderr);
43 #ifdef W3MIMGDISPLAY_SETUID
45 * back real and effective
46 * run w3mimg_open() in setuid privileges
48 setreuid(runner_uid, owner_uid);
51 #ifdef W3MIMGDISPLAY_SETUID
52 /* make sure drop privileges now */
53 setreuid(runner_uid, runner_uid);
58 w_op->offset_x = offset_x;
60 w_op->offset_y = offset_y;
62 w_op->max_anim = maxAnim;
63 w_op->clear_margin = clearMargin;
66 printf("%d %d\n", w_op->width - w_op->offset_x,
67 w_op->height - w_op->offset_y);
72 if (w_op->init(w_op)) {
75 if (w_op->get_image_size(w_op, &img, defined_size, &w, &h))
76 printf("%d %d\n", w, h);
81 w_op->set_background(w_op, background);
83 while (fgets(buf, sizeof(buf), stdin) != NULL) {
84 if (!(isdigit(buf[0]) && buf[1] == ';')) {
90 if (buf[len - 1] == '\n') {
92 if (buf[len - 1] == '\r')
98 * +--+--+--+--+ ...... +--+--+
100 * +--+--+--+--+ .......+--+--+
102 * args is separeted by ';'
104 * 0; params draw image
105 * 1; params redraw image
106 * 2; -none- terminate drawing
107 * 3; -none- sync drawing
108 * 4; -none- nop, sync communication
110 * 5; path get size of image,
111 * response "<width> <height>\n"
112 * 6; params(6) clear image
115 * <n>;<x>;<y>;<w>;<h>;<sx>;<sy>;<sw>;<sh>;<path>
122 DrawImage(&buf[2], 0);
125 DrawImage(&buf[2], 1);
138 if (w_op->init(w_op)) {
141 if (w_op->get_image_size(w_op, &img, &buf[2], &w, &h)) {
142 fprintf(stdout, "%d %d\n", w, h);
146 fprintf(stdout, "\n");
151 fprintf(stdout, "\n");
166 GetOption(int argc, char **argv)
170 for (i = 1; i < argc; i++) {
171 if (!strcmp("-bg", argv[i])) {
174 background = argv[i];
177 else if (!strcmp("-x", argv[i])) {
180 offset_x = atoi(argv[i]);
183 else if (!strcmp("-y", argv[i])) {
186 offset_y = atoi(argv[i]);
189 else if (!strcmp("-test", argv[i])) {
192 else if (!strcmp("-anim", argv[i])) {
195 maxAnim = atoi(argv[i]);
197 else if (!strcmp("-margin", argv[i])) {
200 clearMargin = atoi(argv[i]);
204 else if (!strcmp("-size", argv[i])) {
207 defined_size = argv[i];
209 else if (!strcmp("-debug", argv[i])) {
219 DrawImage(char *buf, int redraw)
222 int n = 0, x = 0, y = 0, w = 0, h = 0, sx = 0, sy = 0, sw = 0, sh = 0;
226 for (; isdigit(*p); p++)
227 n = 10 * n + (*p - '0');
230 for (; isdigit(*p); p++)
231 x = 10 * x + (*p - '0');
234 for (; isdigit(*p); p++)
235 y = 10 * y + (*p - '0');
238 for (; isdigit(*p); p++)
239 w = 10 * w + (*p - '0');
242 for (; isdigit(*p); p++)
243 h = 10 * h + (*p - '0');
246 for (; isdigit(*p); p++)
247 sx = 10 * sx + (*p - '0');
250 for (; isdigit(*p); p++)
251 sy = 10 * sy + (*p - '0');
254 for (; isdigit(*p); p++)
255 sw = 10 * sw + (*p - '0');
258 for (; isdigit(*p); p++)
259 sh = 10 * sh + (*p - '0');
264 if (n < 0 || n >= MAX_IMAGE)
267 if (!w_op->active(w_op) || n >= maxImage || !imageBuf[n].pixmap)
275 maxImage = i ? (i * 2) : 2;
276 if (maxImage > MAX_IMAGE)
277 maxImage = MAX_IMAGE;
278 else if (n >= maxImage)
280 imageBuf = (W3MImage *) realloc((void *)imageBuf,
281 sizeof(W3MImage) * maxImage);
282 for (; i < maxImage; i++)
283 imageBuf[i].pixmap = NULL;
285 if (imageBuf[n].pixmap) {
286 w_op->free_image(w_op, &imageBuf[n]);
287 imageBuf[n].pixmap = NULL;
290 if (w_op->load_image(w_op, &imageBuf[n], p, w, h) == 0)
291 imageBuf[n].pixmap = NULL;
294 if (imageBuf[n].pixmap)
295 w_op->show_image(w_op, &imageBuf[n], sx, sy, sw, sh, x, y);
304 for (i = 0; i < maxImage; i++) {
305 w_op->free_image(w_op, &imageBuf[i]);
314 ClearImage(char *buf)
317 int x = 0, y = 0, w = 0, h = 0;
321 for (; isdigit(*p); p++)
322 x = 10 * x + (*p - '0');
325 for (; isdigit(*p); p++)
326 y = 10 * y + (*p - '0');
329 for (; isdigit(*p); p++)
330 w = 10 * w + (*p - '0');
333 for (; isdigit(*p); p++)
334 h = 10 * h + (*p - '0');
337 x + offset_x - w_op->clear_margin,
338 y + offset_y - w_op->clear_margin,
339 w + w_op->clear_margin * 2, h + w_op->clear_margin * 2);