diff options
author | Fumitoshi UKAI <ukai@debian.or.jp> | 2002-01-31 17:54:47 +0000 |
---|---|---|
committer | Fumitoshi UKAI <ukai@debian.or.jp> | 2002-01-31 17:54:47 +0000 |
commit | 9efbc0885687b07cfe35e5933ebcc202aeff6bef (patch) | |
tree | 03909b91dc1b5c7b0074bea51a549b357b8b9694 /image.c | |
parent | version.c.in: 0.2.5+cvs (diff) | |
download | w3m-9efbc0885687b07cfe35e5933ebcc202aeff6bef.tar.gz w3m-9efbc0885687b07cfe35e5933ebcc202aeff6bef.zip |
w3m-img merge (w3m-0.2.4+cvs-1.278-img-2.2.patch.gz)
* NEWS: w3m-img merge
* .cvsignore: add w3mimgdisplay, w3mimgsize
* XMakefile: add image.c to SRCS
add image.o to OBJS
add IMGDISPLAY, IMGSIZE
* config.h.dist: USE_IMAGE, IMGSIZE, IMGDISPLAY
* configure: add find_imlib
ask use_image
(config.h) add $imgtarget to EXT_TARGETS
$def_use_image
IMGSIZE, IMGDISPLAY
* fm.h (MAX_IMAGE): added
* fm.h (DEFAULT_PIXEL_PER_CHAR): fix for USE_IMAGE
* fm.h (DEFAULT_PIXEL_PER_LINE): added
* fm.h (B_REDRAW_IMAGE): added
* fm.h (IMG_FLAG_SKIP): added
* fm.h (IMG_FLAG_AUTO): added
* fm.h (IMG_FLAG_START): added
* fm.h (IMG_FLAG_STOP): added
* fm.h (IMG_FLAG_NEXT): added
* fm.h (IMG_FLAG_UNLOADED): added
* fm.h (IMG_FLAG_LOADED): added
* fm.h (IMG_FLAG_ERROR): added
* fm.h (MapArea): added
* fm.h (MapList): s/urls/alts/, add area
* fm.h (ImageCache): added
* fm.h (Image): added
* fm.h (Anchor): add image
* fm.h (Buffer): add imarklist, image_flag, need_reshape
* fm.h (RG_FRAME_SRC): added
* fm.h (ALIGN_MIDDLE): added
* fm.h (ALIGN_TOP): added
* fm.h (ALIGN_BOTTOM): added
* fm.h (QuietMessage): added
* fm.h (Imgdisplay): added
* fm.h (Imgsize): added
* fm.h (activeImage): added
* fm.h (displayImage): added
* fm.h (autoImage): added
* fm.h (useExtImageViewer): added
* fm.h (image_source): added
* fm.h (view_unseenobject): change default if USE_IMAGE
* fm.h (set_pixel_per_char): added
* fm.h (pixel_per_line): added
* fm.h (set_pixel_per_line): added
* fm.h (image_scale): added
* proto.h (dispI): added
* proto.h (stopI): added
* proto.h (deleteImage): added
* proto.h (getAllImage): added
* proto.h (loadImage): added
* proto.h (getImage): added
* proto.h (getImageSize): added
* proto.h (process_img): add width arg
* proto.h (loadImageBuffer): added
* proto.h (follow_map_menu): add a_img arg
* proto.h (getMapXY): added
* proto.h (newMapArea): added
* proto.h (touch_cursor): added
* proto.h (cURLcode): added
* proto.h (initImage): added
* proto.h (termImage): added
* proto.h (addImage): added
* proto.h (drawImage): added
* proto.h (clearImage): added
* anchor.c (addMultirowsImg): added
* buffer.c (discardBuffer): deleteImage if USE_IMAGE
* buffer.c (reshapeBuffer): buf->need_reshape check
* display.c (fmTerm): if activeImage, stop load image
* display.c (fmInit): if displayImage, initImage
* display.c (image_touch): added
* display.c (draw_image_flag): added
* display.c (redrawLineImage): added
* display.c (displayBuffer): buf->need_reshape = TRUE
mode == B_REDRAW_IMAGE
image flag checks
force redraw image in mode == B_REDRAW_IMAGE
displayBuffer if need reshape
drawImage
* display.c (redrawNLine): redrawLineImage
* display.c (redrawLineImage): added
* display.c (disp_message_nsec): return immediately if QuietMessage
* etc.c (reset_signals): ignore SIGUSR1
* file.c (frame_source): added
* file.c (_MoveFile): added
* file.c (cur_baseURL): added
* file.c (cur_document_code): added
* file.c (cur_iseq): added
* file.c (loadGeneralFile): fix fmInitialized and prevtrap checks
save cur_baseURL
image_source load
loadImageBuffer for "image/*"
frame_source flag
fix b->type when proc is loadImageBuffer
* file.c (process_img): add width arg
parse height and width attr, align
fix for inline-image
* file.c (process_input): fix for form <input type="image">
* file.c (HTMLtagproc1): pass h_env->limit to process_img()
fix <img_alt> for inline-image
save cur_baseURL
* file.c (HTMLlineproc2body): fix <img_alt> for inline-image
* file.c (loadHTMLstream): fix for inline-image
fix fmInitialized and prevtrap checks
save cur_document_code
save image_flag
* file.c (loadGopherDir): save cur_document_code
fix fmInitialized and prevtrap checks
* file.c (loadImageBuffer): added for inline-image
* file.c (save2tmp): fix fmInitialized and prevtrap checks
* frame.c (frame_download_source): set RG_FRAME_SRC flag
image check
* funcname.tab (DISPLAY_IMAGE): added
* funcname.tab (STOP_IMAGE): added
* html.c (ALST_IMG): add align, ismap
* html.c (ALST_AREA): add shape, coords
* html.c (ALST_IMG_ALT): add width, height, usemap, ismap, hseq,
xoffset, yoffset, top_margin, bottom_margin
* html.c (AttrMap): add shape, coords, ismap, xoffset, yoffset
* html.h (ATTR_SHAPE): added
* html.h (ATTR_COORDS): added
* html.h (ATTR_ISMAP): added
* html.h (ATTR_XOFFSET): added
* html.h (ATTR_YOFFSET): added
* html.h (ATTR_TOP_MARGIN): added
* html.h (ATTR_BOTTOM_MARGIN): added
* html.h (SHAPE_UNKNOWN): added
* html.h (SHAPE_DEFAULT): added
* html.h (SHAPE_RECT): added
* html.h (SHAPE_CIRCLE): added
* html.h (SHAPE_POLY): added
* image.c: added
* main.c (fversion): add USE_IMAGE
* main.c (fusage): -ppl option
* main.c (MAIN): set_pixel_per_char if ppc specified
-ppl option
loadImage
* main.c (pushBuffer): deleteImage
* main.c (quitfm): termImage
* main.c (selBuf): deleteImage
* main.c (followA): fix for inline-image and map area
* main.c (query_from_followform): getMapXY()
* main.c (setOpt): displayBuffer B_REDRAW_IMAGE
* main.c (follow_map): fix calculate x, y
* main.c (dispI): added
* main.c (stopI): added
* map.c: include <math.h>
* map.c (inMapArea): added
* map.c (follow_map_menu): add a_img arg to handle inline-image
* map.c (follow_map_panel): use MapArea
* map.c (getMapXY): added
* map.c (newMapArea): added
* menu.c (smChBuf): deleteImage
* parsetagx.c (toAlign): align top, bottom, middle
* rc.c (P_SCALE): added
* rc.c (CMT_PIXEL_PER_LINE): added
* rc.c (CMT_DISP_IMAGE): added
* rc.c (CMT_AUTO_IMAGE): added
* rc.c (CMT_EXT_IMAGE_VIEWER): added
* rc.c (CMT_IMAGE_SCALE): added
* rc.c (CMT_IMGDISPLAY): added
* rc.c (CMT_IMGSIZE): added
* rc.c (show_param): P_PIXELS
* rc.c (set_param): P_PIXELS
* rc.c (sync_with_option): initImage
* rc.c (to_str): P_PIXELS
* table.c (feed_table_tag): fix <img> for inline-image
* tagtable.tab (image): added
* terms.c (wgetch): retry read when EINTR or EAGAIN
* terms.c (touch_cursor): added
* w3mimgdisplay.c: added
* w3mimgsize.c: added
* doc/README.func (DISPLAY_IMAGE): added
* doc/README.func (STOP_IMAGE): added
* doc/README.img: added
* doc-jp/README.func (DISPLAY_IMAGE): added
* doc-jp/README.func (STOP_IMAGE): added
* doc-jp/README.img: added
* scripts/w3mhelp.cgi.in: show dispI, stopI
* scripts/multipart/multipart.cgi.in: inline image support
From: Fumitoshi UKAI <ukai@debian.or.jp>
Diffstat (limited to 'image.c')
-rw-r--r-- | image.c | 528 |
1 files changed, 528 insertions, 0 deletions
@@ -0,0 +1,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 |