aboutsummaryrefslogblamecommitdiffstats
path: root/image.c
blob: 86d2b8fe309f3b7e9adbfff71ff9b26b1abfc705 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528















































































































































































































































































































































































































































































































































                                                                                
/* $Id: image.c,v 1.1 2002/01/31 17:54:51 ukai Exp $ */

#include "fm.h"
#include <sys/types.h>
#include <signal.h>
#include <errno.h>
#include <unistd.h>
#ifdef HAVE_WAITPID
#include <sys/wait.h>
#endif

#ifdef USE_IMAGE

static image_index = 0;

/* display image */

typedef struct _termialImage {
    ImageCache *cache;
    short x;
    short y;
    short sx;
    short sy;
    short width;
    short height;
} TerminalImage;

static TerminalImage *terminal_image = NULL;
static int n_terminal_image = 0;
static int max_terminal_image = 0;
static FILE *Imgdisplay_rf = NULL, *Imgdisplay_wf = NULL;
static pid_t Imgdisplay_pid = 0;
static int openImgdisplay();
static void closeImgdisplay();

void
initImage()
{
    if (activeImage)
	return;
    if (getCharSize())
	activeImage = TRUE;
}

int
getCharSize()
{
    FILE *f;
    Str tmp;
    int w = 0, h = 0;

    tmp = Strnew();
    if (!strchr(Imgdisplay, '/'))
	Strcat_m_charp(tmp, LIB_DIR, "/", NULL);
    Strcat_m_charp(tmp, Imgdisplay, " -test 2> /dev/null", NULL);
    f = popen(tmp->ptr, "r");
    if (!f)
	return FALSE;
    fscanf(f, "%d %d", &w, &h);
    pclose(f);
    if (!(w > 0 && h > 0))
	return FALSE;
    if (! set_pixel_per_char)
	pixel_per_char = (int)(1.0 * w / COLS + 0.5);
    if (! set_pixel_per_line)
	pixel_per_line = (int)(1.0 * h / LINES + 0.5);
    return TRUE;
}

void
termImage()
{
    if (!activeImage)
	return;
    clearImage();
    if (Imgdisplay_wf) {
	fputs("2;\n", Imgdisplay_wf);	/* ClearImage() */
	fflush(Imgdisplay_wf);
    }
    closeImgdisplay();
}

static int
openImgdisplay()
{
    int fdr[2], fdw[2];
    char *cmd;

    if (pipe(fdr) < 0)
	goto err0;
    if (pipe(fdw) < 0)
	goto err1;

    flush_tty();
    Imgdisplay_pid = fork();
    if (Imgdisplay_pid < 0)
	goto err2;
    if (Imgdisplay_pid == 0) {
	/* child */
	reset_signals();
	signal(SIGINT, SIG_IGN);
#ifdef HAVE_SETPGRP
	setpgrp();
#endif
	close_tty();
	close(fdr[0]);
	close(fdw[1]);
	dup2(fdw[0], 0);
	dup2(fdr[1], 1);
	close(2);
	if (!strchr(Imgdisplay, '/'))
	    cmd = Strnew_m_charp(LIB_DIR, "/", Imgdisplay, NULL)->ptr;
	else
	    cmd = Imgdisplay;
	execl("/bin/sh", "sh", "-c", cmd, NULL);
	exit(1);
    }
    close(fdr[1]);
    close(fdw[0]);
    Imgdisplay_rf = fdopen(fdr[0], "r");
    Imgdisplay_wf = fdopen(fdw[1], "w");
    activeImage = TRUE;
    return TRUE;
  err2:
    close(fdw[0]);
    close(fdw[1]);
  err1:
    close(fdr[0]);
    close(fdr[1]);
  err0:
    Imgdisplay_rf = NULL;
    Imgdisplay_wf = NULL;
    Imgdisplay_pid = 0;
    activeImage = FALSE;
    return FALSE;
}

static void
closeImgdisplay()
{
    if (Imgdisplay_rf)
	fclose(Imgdisplay_rf);
    if (Imgdisplay_wf)
	fclose(Imgdisplay_wf);
    if (Imgdisplay_pid)
	kill(Imgdisplay_pid, SIGKILL);
    Imgdisplay_rf = NULL;
    Imgdisplay_wf = NULL;
    Imgdisplay_pid = 0;
}

void
addImage(ImageCache * cache, int x, int y, int sx, int sy, int w, int h)
{
    TerminalImage *i;

    if (!activeImage)
	return;
    if (n_terminal_image >= max_terminal_image) {
	max_terminal_image = max_terminal_image ? (2 * max_terminal_image) : 8;
	terminal_image = New_Reuse(TerminalImage, terminal_image,
				   max_terminal_image);
    }
    i = &terminal_image[n_terminal_image];
    i->cache = cache;
    i->x = x;
    i->y = y;
    i->sx = sx;
    i->sy = sy;
    i->width = w;
    i->height = h;
    n_terminal_image++;
}

void
drawImage()
{
    static char buf[64];
    int j, draw = FALSE;
    TerminalImage *i;

    if (!activeImage)
	return;
    if (!n_terminal_image)
	return;
    for (j = 0; j < n_terminal_image; j++) {
	i = &terminal_image[j];
	if (!(i->cache->loaded == IMG_FLAG_LOADED &&
	      i->width > 0 && i->height > 0))
	    continue;
	if (!(Imgdisplay_rf && Imgdisplay_wf)) {
	    if (!openImgdisplay())
		return;
	}
	if (!draw) {
	    fputs("3;\n", Imgdisplay_wf);	/* XSync() */
	    draw = TRUE;
	}
	if (i->cache->index > 0) {
	    i->cache->index *= -1;
	    fputs("0;", Imgdisplay_wf);	/* DrawImage() */
	}
	else
	    fputs("1;", Imgdisplay_wf);	/* DrawImage(redraw) */
	sprintf(buf, "%d;%d;%d;%d;%d;%d;%d;%d;%d;",
		(-i->cache->index - 1) % MAX_IMAGE + 1, i->x, i->y,
		(i->cache->width > 0) ? i->cache->width : 0,
		(i->cache->height > 0) ? i->cache->height : 0,
		i->sx, i->sy, i->width, i->height);
	fputs(buf, Imgdisplay_wf);
	fputs(i->cache->file, Imgdisplay_wf);
	fputs("\n", Imgdisplay_wf);
	fputs("4;\n", Imgdisplay_wf);	/* put '\n' */
  again:
	if (fflush(Imgdisplay_wf) != 0) {
	    switch (errno) {
	    case EINTR:
		goto again;
	    default:
		goto err;
	    }
	}
	if (!fgetc(Imgdisplay_rf))
	    goto err;
    }
    if (!draw)
	return;
    fputs("3;\n", Imgdisplay_wf);       /* XSync() */
    fputs("4;\n", Imgdisplay_wf);	/* put '\n' */
  again2:
    if (fflush(Imgdisplay_wf) != 0) {
	switch (errno) {
	case EINTR:
	    goto again2;
	default:
	    goto err;
	}
    }
    if (!fgetc(Imgdisplay_rf))
	goto err;
/*
    touch_line();
    touch_column(CurColumn);
#ifdef JP_CHARSET
    if (CurColumn > 0 &&
	CHMODE(ScreenImage[CurLine]->lineprop[CurColumn]) == C_WCHAR2)
	touch_column(CurColumn - 1);
    else if (CurColumn < COLS - 1 &&
	     CHMODE(ScreenImage[CurLine]->lineprop[CurColumn]) == C_WCHAR1)
	touch_column(CurColumn + 1);
#endif
*/
    touch_cursor();
    refresh();
    return;
  err:
    closeImgdisplay();
    image_index += MAX_IMAGE;
}

void
clearImage()
{
    if (!activeImage)
	return;
    n_terminal_image = 0;
}

/* load image */

static Hash_sv *image_hash = NULL;
static Hash_sv *image_file = NULL;
static GeneralList *image_list = NULL;
static ImageCache *image_cache = NULL;
static pid_t image_pid = 0;
static int need_load_image = FALSE;

static MySignalHandler
load_image_handler(SIGNAL_ARG)
{
    need_load_image = TRUE;
    SIGNAL_RETURN;
}

static MySignalHandler
load_image_next(SIGNAL_ARG)
{
    need_load_image = TRUE;
    loadImage(IMG_FLAG_NEXT);
    SIGNAL_RETURN;
}

void
deleteImage(Buffer *buf)
{
    AnchorList *al;
    Anchor *a;
    int i;

    if (!buf)
	return;
    al = buf->img;
    if (!al)
	return;
    for (i = 0, a = al->anchors; i < al->nanchor; i++, a++) {
	if (a->image && a->image->cache &&
	    a->image->cache->loaded != IMG_FLAG_UNLOADED &&
	    a->image->cache->index < 0)
	    unlink(a->image->cache->file);
    }
    loadImage(IMG_FLAG_STOP);
}

void
getAllImage(Buffer *buf)
{
    AnchorList *al;
    Anchor *a;
    ParsedURL *current;
    int i;

    if (!buf)
	return;
    al = buf->img;
    if (!al)
	return;
    current = baseURL(buf);
    for (i = 0, a = al->anchors; i < al->nanchor; i++, a++) {
	if (a->image)
	    a->image->cache = getImage(a->image, current, buf->image_flag);
    }
}

void
loadImage(int flag)
{
    int wait_st;

    if (flag == IMG_FLAG_STOP) {
	if (image_pid) {
	    kill(image_pid, SIGKILL);
#ifdef HAVE_WAITPID
	    waitpid(image_pid, &wait_st, 0);
#else
	    wait(&wait_st);
#endif
	    image_pid = 0;
	}
	image_cache = NULL;
	image_list = NULL;
	image_file = NULL;
	need_load_image = FALSE;
	return;
    }

    if (flag == IMG_FLAG_NEXT && need_load_image) {
	struct stat st;
	if (image_pid) {
#ifdef HAVE_WAITPID
	    waitpid(image_pid, &wait_st, 0);
#else
	    wait(&wait_st);
#endif
	    image_pid = 0;
	}
	if (!stat(image_cache->file, &st)) {
	    image_cache->loaded = IMG_FLAG_LOADED;
	    if (getImageSize(image_cache)) {
		if (Currentbuf)
		    Currentbuf->need_reshape = TRUE;
	    }
	}
	else {
	    image_cache->loaded = IMG_FLAG_ERROR;
	}
	image_cache = NULL;
	drawImage();
    }

    need_load_image = FALSE;
    if (flag == IMG_FLAG_START)
	signal(SIGUSR1, load_image_handler);
    else
	signal(SIGUSR1, load_image_next);

    if (image_cache)
	return;

    image_pid = 0;
    if (!image_list)
	return;
    while (1) {
	image_cache = (ImageCache *) popValue(image_list);
	if (!image_cache) {
	    if (Currentbuf && Currentbuf->need_reshape)
		displayBuffer(Currentbuf, B_NORMAL);
	    return;
	}
	if (image_cache->loaded == IMG_FLAG_UNLOADED)
	    break;
    }

    flush_tty();
    if ((image_pid = fork()) == 0) {
	Buffer *b;

	reset_signals();
	signal(SIGINT, SIG_IGN);
	close_tty();
	QuietMessage = TRUE;
	fmInitialized = FALSE;
	image_source = image_cache->file;
	b = loadGeneralFile(image_cache->url, image_cache->current, NULL, 0,
			    NULL);
	if (!b || !b->real_type || strncasecmp(b->real_type, "image/", 6))
	    unlink(image_cache->file);
	kill(getppid(), SIGUSR1);
	exit(0);
    }
}

ImageCache *
getImage(Image * image, ParsedURL *current, int flag)
{
    Str key;
    ImageCache *cache;

    if (!activeImage)
	return;
    if (!image_hash)
	image_hash = newHash_sv(100);
    if (image->cache)
	cache = image->cache;
    else {
	key = Sprintf("%d;%d;%s", image->width, image->height, image->url);
	cache = (ImageCache *) getHash_sv(image_hash, key->ptr, NULL);
    }
    if (cache && cache->index && abs(cache->index) <= image_index - MAX_IMAGE) {
	struct stat st;
	if (stat(cache->file, &st))
	    cache->loaded = IMG_FLAG_UNLOADED;
	cache->index = 0;
    }

    if (!cache) {
	if (flag == IMG_FLAG_SKIP)
	    return NULL;

	cache = New(ImageCache);
	cache->url = image->url;
	cache->current = current;
	cache->file = tmpfname(TMPF_DFL, image->ext)->ptr;
	cache->index = 0;
	cache->loaded = IMG_FLAG_UNLOADED;
	cache->width = image->width;
	cache->height = image->height;
	putHash_sv(image_hash, key->ptr, (void *)cache);
	pushText(fileToDelete, cache->file);
    }
    if (flag != IMG_FLAG_SKIP) {
	if (cache->loaded == IMG_FLAG_UNLOADED) {
	    if (!image_file)
		image_file = newHash_sv(100);
	    if (!getHash_sv(image_file, cache->file, NULL)) {
		putHash_sv(image_file, cache->file, (void *)cache);
		if (!image_list)
		    image_list = newGeneralList();
		pushValue(image_list, (void *)cache);
	    }
	}
	if (!cache->index)
	    cache->index = ++image_index;
    }
    if (cache->loaded == IMG_FLAG_LOADED)
	getImageSize(cache);
    return cache;
}

int
getImageSize(ImageCache * cache)
{
    Str tmp;
    FILE *f;
    int w = 0, h = 0;

    if (!activeImage)
	return 0;
    if (!cache || cache->loaded != IMG_FLAG_LOADED ||
	(cache->width > 0 && cache->height > 0))
	return 0;
    tmp = Strnew();
    if (!strchr(Imgsize, '/'))
	Strcat_m_charp(tmp, LIB_DIR, "/", NULL);
    Strcat_m_charp(tmp, Imgsize, " ", shell_quote(cache->file), " 2> /dev/null",
		   NULL);
    f = popen(tmp->ptr, "r");
    if (!f)
	return 0;
    fscanf(f, "%d %d", &w, &h);
    pclose(f);

    if (!(w > 0 && h > 0))
	return 0;
    w = (int)(w * image_scale / 100 + 0.5);
    if (w == 0)
	w = 1;
    h = (int)(h * image_scale / 100 + 0.5);
    if (h == 0)
	h = 1;
    if (cache->width < 0 && cache->height < 0) {
	cache->width = w;
	cache->height = h;
    }
    else if (cache->width < 0) {
	cache->width = (int)((double)cache->height * w / h + 0.5);
    }
    else if (cache->height < 0) {
	cache->height = (int)((double)cache->width * h / w + 0.5);
    }
    if (cache->width == 0)
	cache->width = 1;
    if (cache->height == 0)
	cache->height = 1;
    tmp = Sprintf("%d;%d;%s", cache->width, cache->height, cache->url);
    putHash_sv(image_hash, tmp->ptr, (void *)cache);
    return 1;
}
#endif