From f4268d8d18e57952b046c1bb6ab2aeeac145c310 Mon Sep 17 00:00:00 2001 From: Araki Ken Date: Tue, 29 Jan 2013 00:27:27 +0900 Subject: Support remote image by OSC 5379 show_picture sequence. --- display.c | 26 +++++++++------ fm.h | 3 ++ image.c | 88 +++++++++++++++++++++++++++++++++++++++++++++---- main.c | 42 ++++++++++++++++++++++- terms.c | 50 ++++++++++++++++++++++++++++ w3mimg/x11/x11_w3mimg.c | 17 ++++++++-- 6 files changed, 206 insertions(+), 20 deletions(-) diff --git a/display.c b/display.c index 2fe1183..471c8af 100644 --- a/display.c +++ b/display.c @@ -487,7 +487,7 @@ displayBuffer(Buffer *buf, int mode) term_title(conv_to_system(buf->buffername)); refresh(); #ifdef USE_IMAGE - if (activeImage && displayImage && buf->img) { + if (activeImage && displayImage && buf->img && buf->image_loaded) { drawImage(); } #endif @@ -521,7 +521,10 @@ drawAnchorCursor0(Buffer *buf, AnchorList *al, int hseq, int prevhseq, break; } if (hseq >= 0 && an->hseq == hseq) { + int hasImage = 0; for (i = an->start.pos; i < an->end.pos; i++) { + if (l->propBuf[i] & PE_IMAGE) + hasImage = 1 ; if (l->propBuf[i] & (PE_IMAGE | PE_ANCHOR | PE_FORM)) { if (active) l->propBuf[i] |= PE_ACTIVE; @@ -529,7 +532,8 @@ drawAnchorCursor0(Buffer *buf, AnchorList *al, int hseq, int prevhseq, l->propBuf[i] &= ~PE_ACTIVE; } } - if (active) + if (active && + (! support_remote_image || ! hasImage)) redrawLineRegion(buf, l, l->linenumber - tline + buf->rootY, an->start.pos, an->end.pos); } @@ -855,14 +859,16 @@ redrawLineImage(Buffer *buf, Line *l, int i) y = (int)(i * pixel_per_line); sx = (int)((rcol - COLPOS(l, a->start.pos)) * pixel_per_char); sy = (int)((l->linenumber - image->y) * pixel_per_line); - if (sx == 0 && x + image->xoffset >= 0) - x += image->xoffset; - else - sx -= image->xoffset; - if (sy == 0 && y + image->yoffset >= 0) - y += image->yoffset; - else - sy -= image->yoffset; + if (! support_remote_image) { + if (sx == 0 && x + image->xoffset >= 0) + x += image->xoffset; + else + sx -= image->xoffset; + if (sy == 0 && y + image->yoffset >= 0) + y += image->yoffset; + else + sy -= image->yoffset; + } if (image->width > 0) w = image->width - sx; else diff --git a/fm.h b/fm.h index 49af0e4..75d3869 100644 --- a/fm.h +++ b/fm.h @@ -373,6 +373,8 @@ typedef struct _imageCache { int index; short width; short height; + short a_width; + short a_height; } ImageCache; typedef struct _image { @@ -919,6 +921,7 @@ global char *CurrentKeyData; global char *CurrentCmdData; global char *w3m_reqlog; extern char *w3m_version; +extern int support_remote_image; #define DUMP_BUFFER 0x01 #define DUMP_HEAD 0x02 diff --git a/image.c b/image.c index 9d0e9b5..48c6d2d 100644 --- a/image.c +++ b/image.c @@ -52,6 +52,18 @@ getCharSize() int w = 0, h = 0; set_environ("W3M_TTY", ttyname_tty()); + + if (support_remote_image) { + int ppc, ppl; + + if (get_pixel_per_cell(&ppc,&ppl)) { + pixel_per_char = (double)ppc; + pixel_per_line = (double)ppl; + } + + return TRUE; + } + tmp = Strnew(); if (!strchr(Imgdisplay, '/')) Strcat_m_charp(tmp, w3m_auxbin_dir(), "/", NULL); @@ -156,6 +168,10 @@ addImage(ImageCache * cache, int x, int y, int sx, int sy, int w, int h) static void syncImage(void) { + if (support_remote_image) { + return; + } + fputs("3;\n", Imgdisplay_wf); /* XSync() */ fputs("4;\n", Imgdisplay_wf); /* put '\n' */ while (fflush(Imgdisplay_wf) != 0) { @@ -177,6 +193,7 @@ drawImage() static char buf[64]; int j, draw = FALSE; TerminalImage *i; + struct stat st ; if (!activeImage) return; @@ -184,6 +201,50 @@ drawImage() return; for (j = 0; j < n_terminal_image; j++) { i = &terminal_image[j]; + + if (support_remote_image) { + #if 0 + fprintf(stderr,"file %s x %d y %d w %d h %d sx %d sy %d sw %d sh %d (ppc %d ppl %d)\n", + getenv("WINDOWID") ? i->cache->file : i->cache->url, + 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, + (int)pixel_per_char, (int)pixel_per_line); + #endif + put_image( + #if 1 + /* XXX I don't know why but sometimes i->cache->file doesn't exist. */ + getenv("WINDOWID") && (stat(i->cache->file,&st) == 0) ? + /* local */ i->cache->file : /* remote */ i->cache->url, + #else + i->cache->url, + #endif + (int)(i->x / pixel_per_char), + (int)(i->y / pixel_per_line), + #if 1 + i->cache->a_width > 0 ? + (int)((i->cache->width + i->x % (int)pixel_per_char + + pixel_per_char - 1) / pixel_per_char) : + #endif + 0, + + #if 1 + i->cache->a_height > 0 ? + (int)((i->cache->height + i->y % (int)pixel_per_line + + pixel_per_line - 1) / pixel_per_line) : + #endif + 0, + (int)(i->sx / pixel_per_char), + (int)(i->sy / pixel_per_line), + (int)((i->width + i->sx % (int)pixel_per_char + + pixel_per_char - 1) / pixel_per_char), + (int)((i->height + i->sy % (int)pixel_per_line + + pixel_per_line - 1) / pixel_per_line)); + + continue ; + } + if (!(i->cache->loaded & IMG_FLAG_LOADED && i->width > 0 && i->height > 0)) continue; @@ -207,9 +268,15 @@ drawImage() fputs("\n", Imgdisplay_wf); draw = TRUE; } - if (!draw) - return; - syncImage(); + + if (!support_remote_image) { + if (!draw) + return; + syncImage(); + } + else + n_terminal_image = 0; + touch_cursor(); refresh(); } @@ -221,6 +288,10 @@ clearImage() int j; TerminalImage *i; + if (support_remote_image) { + return; + } + if (!activeImage) return; if (!n_terminal_image) @@ -321,6 +392,8 @@ showImageProgress(Buffer *buf) } } if (n) { + if (support_remote_image && n == l) + drawImage(); message(Sprintf("%d/%d images loaded", l, n)->ptr, buf->cursorX + buf->rootX, buf->cursorY + buf->rootY); refresh(); @@ -407,7 +480,8 @@ loadImage(Buffer *buf, int flag) } if (draw && image_buffer) { - drawImage(); + if (!support_remote_image) + drawImage(); showImageProgress(image_buffer); } @@ -521,6 +595,8 @@ getImage(Image * image, ParsedURL *current, int flag) cache->loaded = IMG_FLAG_UNLOADED; cache->width = image->width; cache->height = image->height; + cache->a_width = image->width; + cache->a_height = image->height; putHash_sv(image_hash, key->ptr, (void *)cache); } if (flag != IMG_FLAG_SKIP) { @@ -581,11 +657,11 @@ getImageSize(ImageCache * cache) } else if (cache->width < 0) { int tmp = (int)((double)cache->height * w / h + 0.5); - cache->width = (tmp > MAX_IMAGE_SIZE) ? MAX_IMAGE_SIZE : tmp; + cache->a_width = cache->width = (tmp > MAX_IMAGE_SIZE) ? MAX_IMAGE_SIZE : tmp; } else if (cache->height < 0) { int tmp = (int)((double)cache->width * h / w + 0.5); - cache->height = (tmp > MAX_IMAGE_SIZE) ? MAX_IMAGE_SIZE : tmp; + cache->a_height = cache->height = (tmp > MAX_IMAGE_SIZE) ? MAX_IMAGE_SIZE : tmp; } if (cache->width == 0) cache->width = 1; diff --git a/main.c b/main.c index c8f1e82..e00eb5e 100644 --- a/main.c +++ b/main.c @@ -122,6 +122,42 @@ static int searchKeyNum(void); #define help() fusage(stdout, 0) #define usage() fusage(stderr, 1) +int support_remote_image; + +static void +check_support_remote_image(void) +{ + char *env; + + if ((env = getenv("MLTERM"))) { + char *p; + int major; + int minor; + int micro; + + if (!(p = strchr(env,'.'))) + return; + *p = '\0'; + major = atoi(env); + env = p + 1; + + if (!(p = strchr(env,'.'))) + return; + *p = '\0'; + minor = atoi(env); + env = p + 1; + micro = atoi(env) ; + + if (major > 3 || + (major == 3 && (minor > 1 || (minor == 1 && micro >= 7)))) { + support_remote_image = 1 ; + set_environ( "W3M_USE_REMOTE_IMAGE","1"); /* for w3mimgdisplay */ + } + } + + return; +} + static void fversion(FILE * f) { @@ -409,7 +445,7 @@ main(int argc, char **argv, char **envp) #if defined(DONT_CALL_GC_AFTER_FORK) && defined(USE_IMAGE) char **getimage_args = NULL; #endif /* defined(DONT_CALL_GC_AFTER_FORK) && defined(USE_IMAGE) */ - + check_support_remote_image(); GC_INIT(); #if defined(ENABLE_NLS) || (defined(USE_M17N) && defined(HAVE_LANGINFO_CODESET)) setlocale(LC_ALL, ""); @@ -679,6 +715,10 @@ main(int argc, char **argv, char **envp) } } #endif + else if (!strcmp("-ri" , argv[i])) { + support_remote_image = 1; + set_environ( "W3M_USE_REMOTE_IMAGE","1"); /* for w3mimgdisplay */ + } else if (!strcmp("-num", argv[i])) showLineNum = TRUE; else if (!strcmp("-no-proxy", argv[i])) diff --git a/terms.c b/terms.c index 672262c..30a67e3 100644 --- a/terms.c +++ b/terms.c @@ -466,6 +466,56 @@ writestr(char *s) #define MOVE(line,column) writestr(tgoto(T_cm,column,line)); +void +put_image(char *url, int x, int y, int w, int h, int sx, int sy, int sw, int sh) +{ + Str buf; + char *size ; + + if (w > 0 && h > 0) + size = Sprintf("%dx%d",w,h)->ptr; + else + size = ""; + + MOVE(y,x); + buf = Sprintf("\x1b]5379;show_picture %s %s %dx%d+%d+%d\x07",url,size,sw,sh,sx,sy); + writestr(buf->ptr); + MOVE(Currentbuf->cursorY,Currentbuf->cursorX); +} + +int +get_pixel_per_cell(int *ppc, int *ppl) +{ + fd_set rfd; + struct timeval tval; + char buf[100]; + ssize_t len; + int wp,hp,wc,hc; + + fputs("\x1b[14t\x1b[18t",ttyf); flush_tty(); + + tval.tv_usec = 10000; /* 0.1 sec */ + tval.tv_sec = 0; + FD_SET(tty,&rfd); + if (select(tty+1,&rfd,NULL,NULL,&tval) < 0 || ! FD_ISSET(tty,&rfd)) { + return 0; + } + + if ((len = read(tty,buf,sizeof(buf)-1)) <= 0) { + return 0; + } + buf[len] = '\0'; + + if (sscanf(buf,"\x1b[4;%d;%dt\x1b[8;%d;%dt",&hp,&wp,&hc,&wc) != 4) { + return 0; + } + + *ppc = wp / wc; + *ppl = hp / hc; + + return 1; +} + #ifdef USE_MOUSE #define W3M_TERM_INFO(name, title, mouse) name, title, mouse #define NEED_XTERM_ON (1) diff --git a/w3mimg/x11/x11_w3mimg.c b/w3mimg/x11/x11_w3mimg.c index cef72e2..bc8d0aa 100644 --- a/w3mimg/x11/x11_w3mimg.c +++ b/w3mimg/x11/x11_w3mimg.c @@ -121,16 +121,21 @@ x11_init(w3mimg_op * self) if (self == NULL) return 0; xi = (struct x11_info *)self->priv; +#if defined(USE_IMLIB) if (xi == NULL) return 0; -#if defined(USE_IMLIB) if (!xi->id) { xi->id = Imlib_init(xi->display); if (!xi->id) return 0; } #elif defined(USE_GDKPIXBUF) - if (!xi->init_flag) { + if (!xi) { +#if defined(USE_GTK2) + g_type_init(); +#endif + } + else if (!xi->init_flag) { #if defined(USE_GTK2) g_type_init(); #endif @@ -138,7 +143,7 @@ x11_init(w3mimg_op * self) xi->init_flag = TRUE; } #endif - if (!xi->imageGC) { + if (xi && !xi->imageGC) { xi->imageGC = XCreateGC(xi->display, xi->parent, 0, NULL); if (!xi->imageGC) return 0; @@ -653,9 +658,11 @@ x11_get_image_size(w3mimg_op * self, W3MImage * img, char *fname, int *w, if (self == NULL) return 0; +#if defined(USE_IMLIB) && defined(USE_IMLIB2) xi = (struct x11_info *)self->priv; if (xi == NULL) return 0; +#endif #if defined(USE_IMLIB) im = Imlib_load_image(xi->id, fname); @@ -755,6 +762,9 @@ w3mimg_x11open() return NULL; memset(wop, 0, sizeof(w3mimg_op)); + if (getenv("W3M_USE_REMOTE_IMAGE")) + goto end; + xi = (struct x11_info *)malloc(sizeof(struct x11_info)); if (xi == NULL) goto error; @@ -807,6 +817,7 @@ w3mimg_x11open() wop->priv = xi; + end: wop->init = x11_init; wop->finish = x11_finish; wop->active = x11_active; -- cgit v1.2.3 From 147ef0048b9a50122e7865f36c97616b94ada76f Mon Sep 17 00:00:00 2001 From: Araki Ken Date: Sat, 2 Feb 2013 16:32:37 +0900 Subject: - Adjust the image size to the terminal cell size. - If the image size is specified in html source, skip to load the image. --- fm.h | 2 ++ image.c | 41 ++++++++++++++++++++++++++--------------- terms.c | 41 +++++++++++++++++++++++++---------------- 3 files changed, 53 insertions(+), 31 deletions(-) diff --git a/fm.h b/fm.h index 75d3869..b6cedcf 100644 --- a/fm.h +++ b/fm.h @@ -1177,9 +1177,11 @@ global char *ssl_forbid_method init("2, 3"); global int is_redisplay init(FALSE); global int clear_buffer init(TRUE); global double pixel_per_char init(DEFAULT_PIXEL_PER_CHAR); +global int pixel_per_char_i init(DEFAULT_PIXEL_PER_CHAR); global int set_pixel_per_char init(FALSE); #ifdef USE_IMAGE global double pixel_per_line init(DEFAULT_PIXEL_PER_LINE); +global int pixel_per_line_i init(DEFAULT_PIXEL_PER_LINE); global int set_pixel_per_line init(FALSE); global double image_scale init(100); #endif diff --git a/image.c b/image.c index 48c6d2d..0c8476a 100644 --- a/image.c +++ b/image.c @@ -57,6 +57,8 @@ getCharSize() int ppc, ppl; if (get_pixel_per_cell(&ppc,&ppl)) { + pixel_per_char_i = ppc ; + pixel_per_line_i = ppl ; pixel_per_char = (double)ppc; pixel_per_line = (double)ppl; } @@ -210,7 +212,7 @@ drawImage() i->cache->width > 0 ? i->cache->width : 0, i->cache->height > 0 ? i->cache->height : 0, i->sx, i->sy, i->width, i->height, - (int)pixel_per_char, (int)pixel_per_line); + pixel_per_char_i, pixel_per_line_i); #endif put_image( #if 1 @@ -220,27 +222,25 @@ drawImage() #else i->cache->url, #endif - (int)(i->x / pixel_per_char), - (int)(i->y / pixel_per_line), + i->x / pixel_per_char_i, + i->y / pixel_per_line_i, #if 1 i->cache->a_width > 0 ? - (int)((i->cache->width + i->x % (int)pixel_per_char + - pixel_per_char - 1) / pixel_per_char) : + (i->cache->width + i->x % pixel_per_char_i + pixel_per_char_i - 1) / + pixel_per_char_i : #endif 0, #if 1 i->cache->a_height > 0 ? - (int)((i->cache->height + i->y % (int)pixel_per_line + - pixel_per_line - 1) / pixel_per_line) : + (i->cache->height + i->y % pixel_per_line_i + pixel_per_line_i - 1) / + pixel_per_line_i : #endif 0, - (int)(i->sx / pixel_per_char), - (int)(i->sy / pixel_per_line), - (int)((i->width + i->sx % (int)pixel_per_char + - pixel_per_char - 1) / pixel_per_char), - (int)((i->height + i->sy % (int)pixel_per_line + - pixel_per_line - 1) / pixel_per_line)); + i->sx / pixel_per_char_i, + i->sy / pixel_per_line_i, + (i->width + i->sx % pixel_per_char_i + pixel_per_char_i - 1) / pixel_per_char_i, + (i->height + i->sy % pixel_per_line_i + pixel_per_line_i - 1) / pixel_per_line_i); continue ; } @@ -593,8 +593,19 @@ getImage(Image * image, ParsedURL *current, int flag) cache->pid = 0; cache->index = 0; cache->loaded = IMG_FLAG_UNLOADED; - cache->width = image->width; - cache->height = image->height; + if (support_remote_image) { + if (image->width > 0 && image->width % pixel_per_char_i > 0) + image->width += (pixel_per_char_i - image->width % pixel_per_char_i); + + if (image->height > 0 && image->height % pixel_per_line_i > 0) + image->height += (pixel_per_line_i - image->height % pixel_per_line_i); + + if (image->height > 0 && image->width > 0) + cache->loaded = IMG_FLAG_LOADED; + } + + cache->width = image->width ; + cache->height = image->height ; cache->a_width = image->width; cache->a_height = image->height; putHash_sv(image_hash, key->ptr, (void *)cache); diff --git a/terms.c b/terms.c index 30a67e3..a915c0a 100644 --- a/terms.c +++ b/terms.c @@ -489,31 +489,40 @@ get_pixel_per_cell(int *ppc, int *ppl) fd_set rfd; struct timeval tval; char buf[100]; + char *p; ssize_t len; + ssize_t left; int wp,hp,wc,hc; + int i; fputs("\x1b[14t\x1b[18t",ttyf); flush_tty(); - tval.tv_usec = 10000; /* 0.1 sec */ - tval.tv_sec = 0; - FD_SET(tty,&rfd); - if (select(tty+1,&rfd,NULL,NULL,&tval) < 0 || ! FD_ISSET(tty,&rfd)) { - return 0; - } + p = buf; + left = sizeof(buf) - 1; + for (i = 0; i < 5; i++) { + tval.tv_usec = 100000; /* 0.1 sec */ + tval.tv_sec = 0; + FD_SET(tty,&rfd); + if (select(tty+1,&rfd,NULL,NULL,&tval) <= 0 || ! FD_ISSET(tty,&rfd)) { + continue; + } - if ((len = read(tty,buf,sizeof(buf)-1)) <= 0) { - return 0; - } - buf[len] = '\0'; + if ((len = read(tty,p,left)) <= 0) { + return 0; + } + p[len] = '\0'; - if (sscanf(buf,"\x1b[4;%d;%dt\x1b[8;%d;%dt",&hp,&wp,&hc,&wc) != 4) { - return 0; - } + if (sscanf(buf,"\x1b[4;%d;%dt\x1b[8;%d;%dt",&hp,&wp,&hc,&wc) == 4) { + *ppc = wp / wc; + *ppl = hp / hc; - *ppc = wp / wc; - *ppl = hp / hc; + return 1; + } + p = buf + len; + left -= len; + } - return 1; + return 0; } #ifdef USE_MOUSE -- cgit v1.2.3 From a95a17897125a4268864b6a67e4fdb79fa5439a3 Mon Sep 17 00:00:00 2001 From: Araki Ken Date: Mon, 11 Mar 2013 21:57:49 +0900 Subject: * terms.c: Change time to wait for the response of "\x1b[14t\x1b[18t" from 0.1 sec to 0.5 sec. * image.c: - clearImage() works. - Use cached image files created by w3m in getImage(). * file.c: Hack for alignment. --- file.c | 25 ++++++++++++++++--------- image.c | 6 +----- terms.c | 2 +- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/file.c b/file.c index 1e363d0..10ad444 100644 --- a/file.c +++ b/file.c @@ -3377,19 +3377,21 @@ process_img(struct parsed_tag *tag, int width) if (i0 >= 0) Strcat(tmp, Sprintf(" height=%d", i0)); switch (align) { + case ALIGN_MIDDLE: + if (!support_remote_image) { + top = ni / 2; + bottom = top; + if (top * 2 == ni) + yoffset = (int)(((ni + 1) * pixel_per_line - i) / 2); + else + yoffset = (int)((ni * pixel_per_line - i) / 2); + break; + } case ALIGN_TOP: top = 0; bottom = ni - 1; yoffset = 0; break; - case ALIGN_MIDDLE: - top = ni / 2; - bottom = top; - if (top * 2 == ni) - yoffset = (int)(((ni + 1) * pixel_per_line - i) / 2); - else - yoffset = (int)((ni * pixel_per_line - i) / 2); - break; case ALIGN_BOTTOM: top = ni - 1; bottom = 0; @@ -3407,7 +3409,12 @@ process_img(struct parsed_tag *tag, int width) } break; } - xoffset = (int)((nw * pixel_per_char - w) / 2); + + if (support_remote_image) + xoffset = 0; + else + xoffset = (int)((nw * pixel_per_char - w) / 2); + if (xoffset) Strcat(tmp, Sprintf(" xoffset=%d", xoffset)); if (yoffset) diff --git a/image.c b/image.c index 0c8476a..51e3903 100644 --- a/image.c +++ b/image.c @@ -288,10 +288,6 @@ clearImage() int j; TerminalImage *i; - if (support_remote_image) { - return; - } - if (!activeImage) return; if (!n_terminal_image) @@ -600,7 +596,7 @@ getImage(Image * image, ParsedURL *current, int flag) if (image->height > 0 && image->height % pixel_per_line_i > 0) image->height += (pixel_per_line_i - image->height % pixel_per_line_i); - if (image->height > 0 && image->width > 0) + if (! getenv("WINDOWID") && image->height > 0 && image->width > 0) cache->loaded = IMG_FLAG_LOADED; } diff --git a/terms.c b/terms.c index a915c0a..9d9b3e1 100644 --- a/terms.c +++ b/terms.c @@ -500,7 +500,7 @@ get_pixel_per_cell(int *ppc, int *ppl) p = buf; left = sizeof(buf) - 1; for (i = 0; i < 5; i++) { - tval.tv_usec = 100000; /* 0.1 sec */ + tval.tv_usec = 500000; /* 0.5 sec */ tval.tv_sec = 0; FD_SET(tty,&rfd); if (select(tty+1,&rfd,NULL,NULL,&tval) <= 0 || ! FD_ISSET(tty,&rfd)) { -- cgit v1.2.3 From 0640eca05c89e7670785fd39969a2e01ebfe32e9 Mon Sep 17 00:00:00 2001 From: Araki Ken Date: Thu, 14 Mar 2013 04:53:45 +0900 Subject: * file.c: nw and ni are rounded up instead of rounded off to show every corner of images. --- file.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/file.c b/file.c index 10ad444..b907c14 100644 --- a/file.c +++ b/file.c @@ -3345,8 +3345,14 @@ process_img(struct parsed_tag *tag, int width) if (i < 0) i = pixel_per_line; } - nw = (w > 3) ? (int)((w - 3) / pixel_per_char + 1) : 1; - ni = (i > 3) ? (int)((i - 3) / pixel_per_line + 1) : 1; + if (support_remote_image) { + nw = (w > 1) ? ((w - 1) / pixel_per_char_i + 1) : 1 ; + ni = (i > 1) ? ((i - 1) / pixel_per_line_i + 1) : 1 ; + } + else { + nw = (w > 3) ? (int)((w - 3) / pixel_per_char + 1) : 1; + ni = (i > 3) ? (int)((i - 3) / pixel_per_line + 1) : 1; + } Strcat(tmp, Sprintf(" Date: Thu, 14 Mar 2013 05:18:13 +0900 Subject: * terms.c: Clear fd_set by FD_ZERO() before select(). --- terms.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/terms.c b/terms.c index 9d9b3e1..6533712 100644 --- a/terms.c +++ b/terms.c @@ -499,17 +499,16 @@ get_pixel_per_cell(int *ppc, int *ppl) p = buf; left = sizeof(buf) - 1; - for (i = 0; i < 5; i++) { - tval.tv_usec = 500000; /* 0.5 sec */ + for (i = 0; i < 10; i++) { + tval.tv_usec = 200000; /* 0.2 sec * 10 */ tval.tv_sec = 0; + FD_ZERO(&rfd); FD_SET(tty,&rfd); - if (select(tty+1,&rfd,NULL,NULL,&tval) <= 0 || ! FD_ISSET(tty,&rfd)) { + if (select(tty+1,&rfd,NULL,NULL,&tval) <= 0 || ! FD_ISSET(tty,&rfd)) continue; - } - if ((len = read(tty,p,left)) <= 0) { - return 0; - } + if ((len = read(tty,p,left)) <= 0) + continue; p[len] = '\0'; if (sscanf(buf,"\x1b[4;%d;%dt\x1b[8;%d;%dt",&hp,&wp,&hc,&wc) == 4) { @@ -518,7 +517,7 @@ get_pixel_per_cell(int *ppc, int *ppl) return 1; } - p = buf + len; + p += len; left -= len; } -- cgit v1.2.3 From 0d99209d9f499ff3d7edf2b34f8343ba23a39aef Mon Sep 17 00:00:00 2001 From: Araki Ken Date: Thu, 14 Mar 2013 06:24:43 +0900 Subject: * display.c: Draw underline on anchor text which is not overlapped with any image. --- display.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/display.c b/display.c index 471c8af..691437b 100644 --- a/display.c +++ b/display.c @@ -521,10 +521,10 @@ drawAnchorCursor0(Buffer *buf, AnchorList *al, int hseq, int prevhseq, break; } if (hseq >= 0 && an->hseq == hseq) { - int hasImage = 0; + int start_pos = an->start.pos; for (i = an->start.pos; i < an->end.pos; i++) { - if (l->propBuf[i] & PE_IMAGE) - hasImage = 1 ; + if (support_remote_image && (l->propBuf[i] & PE_IMAGE)) + start_pos = i + 1; /* Lazy check */ if (l->propBuf[i] & (PE_IMAGE | PE_ANCHOR | PE_FORM)) { if (active) l->propBuf[i] |= PE_ACTIVE; @@ -532,10 +532,9 @@ drawAnchorCursor0(Buffer *buf, AnchorList *al, int hseq, int prevhseq, l->propBuf[i] &= ~PE_ACTIVE; } } - if (active && - (! support_remote_image || ! hasImage)) + if (active && start_pos < an->end.pos) redrawLineRegion(buf, l, l->linenumber - tline + buf->rootY, - an->start.pos, an->end.pos); + start_pos, an->end.pos); } else if (prevhseq >= 0 && an->hseq == prevhseq) { if (active) -- cgit v1.2.3 From 0c0d4b5f387483dac627349e3415d5a7e7177fb2 Mon Sep 17 00:00:00 2001 From: Araki Ken Date: Sat, 1 Mar 2014 15:31:27 +0900 Subject: Read width and height from jpeg, png and gif files directly instead of executing w3mimgdisplay -size. --- image.c | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/image.c b/image.c index 51e3903..3c2a9ab 100644 --- a/image.c +++ b/image.c @@ -625,6 +625,91 @@ getImage(Image * image, ParsedURL *current, int flag) return cache; } +static int +parseImageHeader(char *path, u_int *width, u_int *height) +{ + char *suffix; + + suffix = path + strlen(path); + if (strcasecmp(suffix - 5, ".jpeg") == 0 || strcasecmp(suffix - 4 , ".jpg") == 0) { + FILE *fp = fopen(path, "r"); + if (fp) { + u_char buf[2]; + if (fread(buf, 1, 2, fp) != 2 || memcmp(buf, "\xff\xd8", 2) != 0 || + fseek(fp, 2, SEEK_CUR) < 0) /* 0xffe0 */ + goto jpg_error; + + while (fread(buf, 1, 2, fp) == 2) { + size_t len = ((buf[0] << 8) | buf[1]) - 2; + if (fseek(fp, len, SEEK_CUR) < 0) goto jpg_error; + if (fread(buf, 1, 2, fp) == 2 && memcmp(buf, "\xff\xc0", 2) == 0) { + fseek(fp, 3, SEEK_CUR); + if (fread(buf, 1, 2, fp) == 2) { + *height = (buf[0] << 8) | buf[1]; + if (fread(buf, 1, 2, fp) == 2) { + *width = (buf[0] << 8) | buf[1]; + fclose(fp); + return TRUE; + } + } + break; + } + } + + jpg_error: + fclose(fp); + return FALSE; + } + } + else if (strcasecmp(suffix - 4, ".png") == 0) { + FILE *fp = fopen(path, "r"); + if (fp) { + u_char buf[8]; + if (fread(buf, 1, 8, fp) != 8 || + memcmp(buf, "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a", 8) != 0 || + fseek(fp, 8, SEEK_CUR) < 0) + goto png_error; + + if (fread(buf, 1, 4, fp) == 4) { + *width = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]; + if (fread(buf, 1, 4, fp) == 4) { + *height = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]; + fclose(fp); + return TRUE; + } + } + + png_error: + fclose(fp); + return FALSE; + } + } + else if (strcasecmp(suffix - 4, ".gif") == 0) { + FILE *fp = fopen(path, "r"); + if (fp) { + u_char buf[3]; + if (fread(buf, 1, 3, fp) != 3 || memcmp(buf, "GIF", 3) != 0 || + fseek(fp, 3, SEEK_CUR) < 0) { + goto png_error; + } + if (fread(buf, 1, 2, fp) == 2) { + *width = (buf[1] << 8) | buf[0]; + if (fread(buf, 1, 2, fp) == 2) { + *height = (buf[1] << 8) | buf[0]; + fclose(fp); + return TRUE; + } + } + } + + gif_error: + fclose(fp); + return FALSE; + } + + return FALSE; +} + int getImageSize(ImageCache * cache) { @@ -637,6 +722,10 @@ getImageSize(ImageCache * cache) if (!cache || !(cache->loaded & IMG_FLAG_LOADED) || (cache->width > 0 && cache->height > 0)) return FALSE; + + if (parseImageHeader(cache->file, &w, &h)) + goto got_image_size; + tmp = Strnew(); if (!strchr(Imgdisplay, '/')) Strcat_m_charp(tmp, w3m_auxbin_dir(), "/", NULL); @@ -652,6 +741,8 @@ getImageSize(ImageCache * cache) if (!(w > 0 && h > 0)) return FALSE; + +got_image_size: w = (int)(w * image_scale / 100 + 0.5); if (w == 0) w = 1; -- cgit v1.2.3 From e6856f2329a68e93704db4eab35a934d2a54e40f Mon Sep 17 00:00:00 2001 From: Araki Ken Date: Sat, 1 Mar 2014 16:16:48 +0900 Subject: Determine the format of an image file by its header data not by its file name suffix. --- image.c | 116 ++++++++++++++++++++++++++++------------------------------------ 1 file changed, 51 insertions(+), 65 deletions(-) diff --git a/image.c b/image.c index 3c2a9ab..fdc9b27 100644 --- a/image.c +++ b/image.c @@ -628,86 +628,72 @@ getImage(Image * image, ParsedURL *current, int flag) static int parseImageHeader(char *path, u_int *width, u_int *height) { - char *suffix; - - suffix = path + strlen(path); - if (strcasecmp(suffix - 5, ".jpeg") == 0 || strcasecmp(suffix - 4 , ".jpg") == 0) { - FILE *fp = fopen(path, "r"); - if (fp) { - u_char buf[2]; - if (fread(buf, 1, 2, fp) != 2 || memcmp(buf, "\xff\xd8", 2) != 0 || - fseek(fp, 2, SEEK_CUR) < 0) /* 0xffe0 */ - goto jpg_error; - - while (fread(buf, 1, 2, fp) == 2) { - size_t len = ((buf[0] << 8) | buf[1]) - 2; - if (fseek(fp, len, SEEK_CUR) < 0) goto jpg_error; - if (fread(buf, 1, 2, fp) == 2 && memcmp(buf, "\xff\xc0", 2) == 0) { - fseek(fp, 3, SEEK_CUR); + FILE *fp; + u_char buf[8]; + + if (!(fp = fopen(path, "r"))) return FALSE; + + if (fread(buf, 1, 2, fp) != 2) goto error; + + if (memcmp(buf, "\xff\xd8", 2) == 0) { + /* JPEG */ + if (fseek(fp, 2, SEEK_CUR) < 0) goto error; /* 0xffe0 */ + while (fread(buf, 1, 2, fp) == 2) { + size_t len = ((buf[0] << 8) | buf[1]) - 2; + if (fseek(fp, len, SEEK_CUR) < 0) goto error; + if (fread(buf, 1, 2, fp) == 2 && + /* SOF0 or SOF2 */ + (memcmp(buf, "\xff\xc0", 2) == 0 || memcmp(buf, "\xff\xc2", 2) == 0)) { + fseek(fp, 3, SEEK_CUR); + if (fread(buf, 1, 2, fp) == 2) { + *height = (buf[0] << 8) | buf[1]; if (fread(buf, 1, 2, fp) == 2) { - *height = (buf[0] << 8) | buf[1]; - if (fread(buf, 1, 2, fp) == 2) { - *width = (buf[0] << 8) | buf[1]; - fclose(fp); - return TRUE; - } + *width = (buf[0] << 8) | buf[1]; + goto success; } - break; } + break; } - - jpg_error: - fclose(fp); - return FALSE; } + goto error; } - else if (strcasecmp(suffix - 4, ".png") == 0) { - FILE *fp = fopen(path, "r"); - if (fp) { - u_char buf[8]; - if (fread(buf, 1, 8, fp) != 8 || - memcmp(buf, "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a", 8) != 0 || - fseek(fp, 8, SEEK_CUR) < 0) - goto png_error; - if (fread(buf, 1, 4, fp) == 4) { - *width = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]; - if (fread(buf, 1, 4, fp) == 4) { - *height = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]; - fclose(fp); - return TRUE; - } - } + if (fread(buf + 2, 1, 1, fp) != 1) goto error; - png_error: - fclose(fp); - return FALSE; - } - } - else if (strcasecmp(suffix - 4, ".gif") == 0) { - FILE *fp = fopen(path, "r"); - if (fp) { - u_char buf[3]; - if (fread(buf, 1, 3, fp) != 3 || memcmp(buf, "GIF", 3) != 0 || - fseek(fp, 3, SEEK_CUR) < 0) { - goto png_error; - } + if (memcmp(buf, "GIF", 3) == 0) { + /* GIF */ + if (fseek(fp, 3, SEEK_CUR) < 0) goto error; + if (fread(buf, 1, 2, fp) == 2) { + *width = (buf[1] << 8) | buf[0]; if (fread(buf, 1, 2, fp) == 2) { - *width = (buf[1] << 8) | buf[0]; - if (fread(buf, 1, 2, fp) == 2) { - *height = (buf[1] << 8) | buf[0]; - fclose(fp); - return TRUE; - } + *height = (buf[1] << 8) | buf[0]; + goto success; } } + } - gif_error: - fclose(fp); - return FALSE; + if (fread(buf + 3, 1, 5, fp) != 5) goto error; + + if (memcmp(buf, "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a", 8) == 0) { + /* PNG */ + if (fseek(fp, 8, SEEK_CUR) < 0) goto error; + if (fread(buf, 1, 4, fp) == 4) { + *width = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]; + if (fread(buf, 1, 4, fp) == 4) { + *height = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]; + goto success; + } + } } +error: + fprintf(stderr,"%s\n",path); + fclose(fp); return FALSE; + +success: + fclose(fp); + return TRUE; } int -- cgit v1.2.3 From 08811e76fd6b1917cdc54343831685740710bde4 Mon Sep 17 00:00:00 2001 From: Araki Ken Date: Sat, 1 Mar 2014 16:36:57 +0900 Subject: Minor fixes of parseImageHeader(). --- image.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/image.c b/image.c index fdc9b27..75fa31f 100644 --- a/image.c +++ b/image.c @@ -670,6 +670,7 @@ parseImageHeader(char *path, u_int *width, u_int *height) goto success; } } + goto error; } if (fread(buf + 3, 1, 5, fp) != 5) goto error; @@ -684,10 +685,10 @@ parseImageHeader(char *path, u_int *width, u_int *height) goto success; } } + goto error; } error: - fprintf(stderr,"%s\n",path); fclose(fp); return FALSE; -- cgit v1.2.3 From 98b4d10ce4983679d4d1882a681d7d6b03f65cb2 Mon Sep 17 00:00:00 2001 From: Araki Ken Date: Wed, 5 Mar 2014 19:42:14 +0900 Subject: Don't download image files whose size is specified in tag. --- image.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/image.c b/image.c index 75fa31f..28dc585 100644 --- a/image.c +++ b/image.c @@ -207,7 +207,7 @@ drawImage() if (support_remote_image) { #if 0 fprintf(stderr,"file %s x %d y %d w %d h %d sx %d sy %d sw %d sh %d (ppc %d ppl %d)\n", - getenv("WINDOWID") ? i->cache->file : i->cache->url, + (getenv("WINDOWID") && i->cache->touch) ? i->cache->file : i->cache->url, i->x, i->y, i->cache->width > 0 ? i->cache->width : 0, i->cache->height > 0 ? i->cache->height : 0, @@ -217,7 +217,7 @@ drawImage() put_image( #if 1 /* XXX I don't know why but sometimes i->cache->file doesn't exist. */ - getenv("WINDOWID") && (stat(i->cache->file,&st) == 0) ? + (getenv("WINDOWID") && i->cache->touch && stat(i->cache->file,&st) == 0) ? /* local */ i->cache->file : /* remote */ i->cache->url, #else i->cache->url, @@ -419,7 +419,7 @@ loadImage(Buffer *buf, int flag) } for (i = 0; i < n_load_image; i++) { cache = image_cache[i]; - if (!cache) + if (!cache || !cache->touch) continue; if (lstat(cache->touch, &st)) continue; @@ -450,7 +450,7 @@ loadImage(Buffer *buf, int flag) for (i = (buf != image_buffer) ? 0 : maxLoadImage; i < n_load_image; i++) { cache = image_cache[i]; - if (!cache) + if (!cache || !cache->touch) continue; if (cache->pid) { kill(cache->pid, SIGKILL); @@ -505,6 +505,9 @@ loadImage(Buffer *buf, int flag) break; } image_cache[i] = cache; + if (!cache->touch) { + continue; + } flush_tty(); #ifdef DONT_CALL_GC_AFTER_FORK @@ -585,7 +588,6 @@ getImage(Image * image, ParsedURL *current, int flag) cache->url = image->url; cache->current = current; cache->file = tmpfname(TMPF_DFL, image->ext)->ptr; - cache->touch = tmpfname(TMPF_DFL, NULL)->ptr; cache->pid = 0; cache->index = 0; cache->loaded = IMG_FLAG_UNLOADED; @@ -595,9 +597,15 @@ getImage(Image * image, ParsedURL *current, int flag) if (image->height > 0 && image->height % pixel_per_line_i > 0) image->height += (pixel_per_line_i - image->height % pixel_per_line_i); - - if (! getenv("WINDOWID") && image->height > 0 && image->width > 0) + if (image->height > 0 && image->width > 0) { cache->loaded = IMG_FLAG_LOADED; + } + } + if (cache->loaded == IMG_FLAG_UNLOADED) { + cache->touch = tmpfname(TMPF_DFL, NULL)->ptr; + } + else { + cache->touch = NULL; } cache->width = image->width ; -- cgit v1.2.3 From 2fe66f3a6f1b1fd28424a2f14beebdd535e9d17b Mon Sep 17 00:00:00 2001 From: Araki Ken Date: Mon, 22 Sep 2014 20:16:39 +0900 Subject: Add -sixel option which supports image processing by img2sixel. --- display.c | 4 ++-- file.c | 6 +++--- fm.h | 2 +- image.c | 28 ++++++++++++++-------------- main.c | 45 ++++++--------------------------------------- terms.c | 18 +++++++++++++++++- 6 files changed, 43 insertions(+), 60 deletions(-) diff --git a/display.c b/display.c index 691437b..6dd0976 100644 --- a/display.c +++ b/display.c @@ -523,7 +523,7 @@ drawAnchorCursor0(Buffer *buf, AnchorList *al, int hseq, int prevhseq, if (hseq >= 0 && an->hseq == hseq) { int start_pos = an->start.pos; for (i = an->start.pos; i < an->end.pos; i++) { - if (support_remote_image && (l->propBuf[i] & PE_IMAGE)) + if (enable_inline_image && (l->propBuf[i] & PE_IMAGE)) start_pos = i + 1; /* Lazy check */ if (l->propBuf[i] & (PE_IMAGE | PE_ANCHOR | PE_FORM)) { if (active) @@ -858,7 +858,7 @@ redrawLineImage(Buffer *buf, Line *l, int i) y = (int)(i * pixel_per_line); sx = (int)((rcol - COLPOS(l, a->start.pos)) * pixel_per_char); sy = (int)((l->linenumber - image->y) * pixel_per_line); - if (! support_remote_image) { + if (! enable_inline_image) { if (sx == 0 && x + image->xoffset >= 0) x += image->xoffset; else diff --git a/file.c b/file.c index b907c14..709a0c1 100644 --- a/file.c +++ b/file.c @@ -3345,7 +3345,7 @@ process_img(struct parsed_tag *tag, int width) if (i < 0) i = pixel_per_line; } - if (support_remote_image) { + if (enable_inline_image) { nw = (w > 1) ? ((w - 1) / pixel_per_char_i + 1) : 1 ; ni = (i > 1) ? ((i - 1) / pixel_per_line_i + 1) : 1 ; } @@ -3384,7 +3384,7 @@ process_img(struct parsed_tag *tag, int width) Strcat(tmp, Sprintf(" height=%d", i0)); switch (align) { case ALIGN_MIDDLE: - if (!support_remote_image) { + if (!enable_inline_image) { top = ni / 2; bottom = top; if (top * 2 == ni) @@ -3416,7 +3416,7 @@ process_img(struct parsed_tag *tag, int width) break; } - if (support_remote_image) + if (enable_inline_image) xoffset = 0; else xoffset = (int)((nw * pixel_per_char - w) / 2); diff --git a/fm.h b/fm.h index b6cedcf..8f594dc 100644 --- a/fm.h +++ b/fm.h @@ -921,7 +921,7 @@ global char *CurrentKeyData; global char *CurrentCmdData; global char *w3m_reqlog; extern char *w3m_version; -extern int support_remote_image; +extern int enable_inline_image; #define DUMP_BUFFER 0x01 #define DUMP_HEAD 0x02 diff --git a/image.c b/image.c index 28dc585..09e929c 100644 --- a/image.c +++ b/image.c @@ -53,7 +53,7 @@ getCharSize() set_environ("W3M_TTY", ttyname_tty()); - if (support_remote_image) { + if (enable_inline_image) { int ppc, ppl; if (get_pixel_per_cell(&ppc,&ppl)) { @@ -170,7 +170,7 @@ addImage(ImageCache * cache, int x, int y, int sx, int sy, int w, int h) static void syncImage(void) { - if (support_remote_image) { + if (enable_inline_image) { return; } @@ -189,6 +189,9 @@ syncImage(void) n_terminal_image = 0; } +void put_image_osc5379(char *url, int x, int y, int w, int h, int sx, int sy, int sw, int sh); +void put_image_sixel(char *url, int x, int y, int w, int h, int sx, int sy, int sw, int sh); + void drawImage() { @@ -204,7 +207,7 @@ drawImage() for (j = 0; j < n_terminal_image; j++) { i = &terminal_image[j]; - if (support_remote_image) { + if (enable_inline_image) { #if 0 fprintf(stderr,"file %s x %d y %d w %d h %d sx %d sy %d sw %d sh %d (ppc %d ppl %d)\n", (getenv("WINDOWID") && i->cache->touch) ? i->cache->file : i->cache->url, @@ -214,14 +217,11 @@ drawImage() i->sx, i->sy, i->width, i->height, pixel_per_char_i, pixel_per_line_i); #endif - put_image( - #if 1 - /* XXX I don't know why but sometimes i->cache->file doesn't exist. */ - (getenv("WINDOWID") && i->cache->touch && stat(i->cache->file,&st) == 0) ? + (enable_inline_image == 2 ? put_image_sixel : put_image_osc5379)( + ((enable_inline_image == 2 /* sixel */ || getenv("WINDOWID")) && + /* XXX I don't know why but sometimes i->cache->file doesn't exist. */ + i->cache->touch && stat(i->cache->file,&st) == 0) ? /* local */ i->cache->file : /* remote */ i->cache->url, - #else - i->cache->url, - #endif i->x / pixel_per_char_i, i->y / pixel_per_line_i, #if 1 @@ -269,7 +269,7 @@ drawImage() draw = TRUE; } - if (!support_remote_image) { + if (!enable_inline_image) { if (!draw) return; syncImage(); @@ -388,7 +388,7 @@ showImageProgress(Buffer *buf) } } if (n) { - if (support_remote_image && n == l) + if (enable_inline_image && n == l) drawImage(); message(Sprintf("%d/%d images loaded", l, n)->ptr, buf->cursorX + buf->rootX, buf->cursorY + buf->rootY); @@ -476,7 +476,7 @@ loadImage(Buffer *buf, int flag) } if (draw && image_buffer) { - if (!support_remote_image) + if (!enable_inline_image) drawImage(); showImageProgress(image_buffer); } @@ -591,7 +591,7 @@ getImage(Image * image, ParsedURL *current, int flag) cache->pid = 0; cache->index = 0; cache->loaded = IMG_FLAG_UNLOADED; - if (support_remote_image) { + if (enable_inline_image) { if (image->width > 0 && image->width % pixel_per_char_i > 0) image->width += (pixel_per_char_i - image->width % pixel_per_char_i); diff --git a/main.c b/main.c index e00eb5e..5d8de21 100644 --- a/main.c +++ b/main.c @@ -122,41 +122,7 @@ static int searchKeyNum(void); #define help() fusage(stdout, 0) #define usage() fusage(stderr, 1) -int support_remote_image; - -static void -check_support_remote_image(void) -{ - char *env; - - if ((env = getenv("MLTERM"))) { - char *p; - int major; - int minor; - int micro; - - if (!(p = strchr(env,'.'))) - return; - *p = '\0'; - major = atoi(env); - env = p + 1; - - if (!(p = strchr(env,'.'))) - return; - *p = '\0'; - minor = atoi(env); - env = p + 1; - micro = atoi(env) ; - - if (major > 3 || - (major == 3 && (minor > 1 || (minor == 1 && micro >= 7)))) { - support_remote_image = 1 ; - set_environ( "W3M_USE_REMOTE_IMAGE","1"); /* for w3mimgdisplay */ - } - } - - return; -} +int enable_inline_image; /* 1 == mlterm OSC 5379, 2 == sixel */ static void fversion(FILE * f) @@ -445,7 +411,6 @@ main(int argc, char **argv, char **envp) #if defined(DONT_CALL_GC_AFTER_FORK) && defined(USE_IMAGE) char **getimage_args = NULL; #endif /* defined(DONT_CALL_GC_AFTER_FORK) && defined(USE_IMAGE) */ - check_support_remote_image(); GC_INIT(); #if defined(ENABLE_NLS) || (defined(USE_M17N) && defined(HAVE_LANGINFO_CODESET)) setlocale(LC_ALL, ""); @@ -715,9 +680,11 @@ main(int argc, char **argv, char **envp) } } #endif - else if (!strcmp("-ri" , argv[i])) { - support_remote_image = 1; - set_environ( "W3M_USE_REMOTE_IMAGE","1"); /* for w3mimgdisplay */ + else if (!strcmp("-ri", argv[i])) { + enable_inline_image = 1; + } + else if (!strcmp("-sixel", argv[i])) { + enable_inline_image = 2; } else if (!strcmp("-num", argv[i])) showLineNum = TRUE; diff --git a/terms.c b/terms.c index 6533712..53d604c 100644 --- a/terms.c +++ b/terms.c @@ -467,7 +467,7 @@ writestr(char *s) #define MOVE(line,column) writestr(tgoto(T_cm,column,line)); void -put_image(char *url, int x, int y, int w, int h, int sx, int sy, int sw, int sh) +put_image_osc5379(char *url, int x, int y, int w, int h, int sx, int sy, int sw, int sh) { Str buf; char *size ; @@ -483,6 +483,22 @@ put_image(char *url, int x, int y, int w, int h, int sx, int sy, int sw, int sh) MOVE(Currentbuf->cursorY,Currentbuf->cursorX); } +void +put_image_sixel(char *url, int x, int y, int w, int h, int sx, int sy, int sw, int sh) +{ + Str buf; + + MOVE(y,x); + flush_tty(); + buf = Sprintf("img2sixel -l disable -c %dx%d+%d+%d -w %d -h %d %s 2>/dev/null", + sw*pixel_per_char_i, sh*pixel_per_line_i, + sx*pixel_per_char_i, sy*pixel_per_line_i, + w*pixel_per_char_i, h*pixel_per_line_i, + url); + system(buf->ptr); + MOVE(Currentbuf->cursorY,Currentbuf->cursorX); +} + int get_pixel_per_cell(int *ppc, int *ppl) { -- cgit v1.2.3 From 9504d18d0a5e6f6401cc5e6da447eb02897ac207 Mon Sep 17 00:00:00 2001 From: Araki Ken Date: Mon, 22 Sep 2014 22:16:07 +0900 Subject: Init pixel_per_{char|line}_i if get_pixel_per_cell() fails. --- image.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/image.c b/image.c index 09e929c..4f8eb0a 100644 --- a/image.c +++ b/image.c @@ -62,6 +62,10 @@ getCharSize() pixel_per_char = (double)ppc; pixel_per_line = (double)ppl; } + else { + pixel_per_char_i = (int)pixel_per_char; + pixel_per_line_i = (int)pixel_per_line; + } return TRUE; } -- cgit v1.2.3 From de942f3abf6da50b7e85339117bafdd3b5d0a146 Mon Sep 17 00:00:00 2001 From: Araki Ken Date: Mon, 22 Sep 2014 22:55:14 +0900 Subject: Cache image files if at all possible and convert them to sixel when -sixel option is specified. --- image.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/image.c b/image.c index 4f8eb0a..b5137e7 100644 --- a/image.c +++ b/image.c @@ -595,7 +595,7 @@ getImage(Image * image, ParsedURL *current, int flag) cache->pid = 0; cache->index = 0; cache->loaded = IMG_FLAG_UNLOADED; - if (enable_inline_image) { + if (enable_inline_image == 1) { if (image->width > 0 && image->width % pixel_per_char_i > 0) image->width += (pixel_per_char_i - image->width % pixel_per_char_i); -- cgit v1.2.3 From 622bdafb59e9fb4ce959e0f04c1f584e33373983 Mon Sep 17 00:00:00 2001 From: Araki Ken Date: Tue, 23 Sep 2014 02:05:44 +0900 Subject: Minor fix. --- image.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/image.c b/image.c index b5137e7..33c5462 100644 --- a/image.c +++ b/image.c @@ -214,7 +214,8 @@ drawImage() if (enable_inline_image) { #if 0 fprintf(stderr,"file %s x %d y %d w %d h %d sx %d sy %d sw %d sh %d (ppc %d ppl %d)\n", - (getenv("WINDOWID") && i->cache->touch) ? i->cache->file : i->cache->url, + ((enable_inline_image == 2 || getenv("WINDOWID")) && + i->cache->touch) ? i->cache->file : i->cache->url, i->x, i->y, i->cache->width > 0 ? i->cache->width : 0, i->cache->height > 0 ? i->cache->height : 0, -- cgit v1.2.3 From 09ececc3b7fc52149e30c62f493b295f99e49246 Mon Sep 17 00:00:00 2001 From: Araki Ken Date: Tue, 23 Sep 2014 02:07:56 +0900 Subject: Remove close_tty() from setup_child() because close_tty() sometimes interrupts loadGeneralFile() in loadImage() and corrupt image data can be cached in ~/.w3m. --- etc.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/etc.c b/etc.c index 8fe1215..dcc6edd 100644 --- a/etc.c +++ b/etc.c @@ -1365,7 +1365,13 @@ setup_child(int child, int i, int f) if (!child) SETPGRP(); #endif /* __MINGW32_VERSION */ + /* + * I don't know why but close_tty() sometimes interrupts loadGeneralFile() in loadImage() + * and corrupt image data can be cached in ~/.w3m. + */ +#if 0 close_tty(); +#endif close_all_fds_except(i, f); QuietMessage = TRUE; fmInitialized = FALSE; -- cgit v1.2.3 From 1eabd79f5bb07addda737183d6681cadae4ebf38 Mon Sep 17 00:00:00 2001 From: Araki Ken Date: Wed, 24 Sep 2014 20:08:54 +0900 Subject: Draw underline on anchor which contains cboth text and images. --- display.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/display.c b/display.c index 6dd0976..d4f336a 100644 --- a/display.c +++ b/display.c @@ -522,9 +522,14 @@ drawAnchorCursor0(Buffer *buf, AnchorList *al, int hseq, int prevhseq, } if (hseq >= 0 && an->hseq == hseq) { int start_pos = an->start.pos; + int end_pos = an->end.pos; for (i = an->start.pos; i < an->end.pos; i++) { - if (enable_inline_image && (l->propBuf[i] & PE_IMAGE)) - start_pos = i + 1; /* Lazy check */ + if (enable_inline_image && (l->propBuf[i] & PE_IMAGE)) { + if (start_pos == i) + start_pos = i + 1; + else if (end_pos == an->end.pos) + end_pos = i - 1; + } if (l->propBuf[i] & (PE_IMAGE | PE_ANCHOR | PE_FORM)) { if (active) l->propBuf[i] |= PE_ACTIVE; @@ -532,9 +537,9 @@ drawAnchorCursor0(Buffer *buf, AnchorList *al, int hseq, int prevhseq, l->propBuf[i] &= ~PE_ACTIVE; } } - if (active && start_pos < an->end.pos) + if (active && start_pos < end_pos) redrawLineRegion(buf, l, l->linenumber - tline + buf->rootY, - start_pos, an->end.pos); + start_pos, end_pos); } else if (prevhseq >= 0 && an->hseq == prevhseq) { if (active) -- cgit v1.2.3 From e8b2d473586c23c82c7d6fd65d29fba9e11d883a Mon Sep 17 00:00:00 2001 From: Araki Ken Date: Wed, 24 Sep 2014 20:09:18 +0900 Subject: system() -> fork()&execvp() --- terms.c | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/terms.c b/terms.c index 53d604c..66d0ff8 100644 --- a/terms.c +++ b/terms.c @@ -12,6 +12,7 @@ #include #include "config.h" #include +#include #ifdef HAVE_SYS_SELECT_H #include #endif @@ -486,16 +487,40 @@ put_image_osc5379(char *url, int x, int y, int w, int h, int sx, int sy, int sw, void put_image_sixel(char *url, int x, int y, int w, int h, int sx, int sy, int sw, int sh) { - Str buf; + pid_t pid; MOVE(y,x); flush_tty(); - buf = Sprintf("img2sixel -l disable -c %dx%d+%d+%d -w %d -h %d %s 2>/dev/null", - sw*pixel_per_char_i, sh*pixel_per_line_i, - sx*pixel_per_char_i, sy*pixel_per_line_i, - w*pixel_per_char_i, h*pixel_per_line_i, - url); - system(buf->ptr); + + if ((pid = fork()) == 0) { + char *argv[11]; + char digit[2][11+1]; + char clip[44+3+1]; + + close(STDERR_FILENO); + argv[0] = "img2sixel"; + argv[1] = "-l"; + argv[2] = "disable"; + argv[3] = "-w"; + sprintf(digit[0], "%d", w*pixel_per_char_i); + argv[4] = digit[0]; + argv[5] = "-h"; + sprintf(digit[1], "%d", h*pixel_per_line_i); + argv[6] = digit[1]; + argv[7] = "-c"; + sprintf(clip, "%dx%d+%d+%d", sw*pixel_per_char_i, sh*pixel_per_line_i, + sx*pixel_per_char_i, sy*pixel_per_line_i); + argv[8] = clip; + argv[9] = url; + argv[10] = NULL; + execvp(argv[0],argv); + exit(0); + } + else if (pid > 0) { + int status; + waitpid(pid, &status, 0); + } + MOVE(Currentbuf->cursorY,Currentbuf->cursorX); } -- cgit v1.2.3 From 4a9c976e32a6bab9dbac4380803e5eb92f601284 Mon Sep 17 00:00:00 2001 From: Araki Ken Date: Thu, 25 Sep 2014 23:21:41 +0900 Subject: Show the first frame of animation gif files. --- terms.c | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/terms.c b/terms.c index 66d0ff8..5f0e638 100644 --- a/terms.c +++ b/terms.c @@ -484,6 +484,89 @@ put_image_osc5379(char *url, int x, int y, int w, int h, int sx, int sy, int sw, MOVE(Currentbuf->cursorY,Currentbuf->cursorX); } +static void +save_gif(const char *path, u_char *header, size_t header_size, u_char *body, size_t body_size) +{ + int fd; + + if ((fd = open(path, O_WRONLY|O_CREAT, 0600)) >= 0) { + write(fd, header, header_size) ; + write(fd, body, body_size) ; + write(fd, "\x3b" , 1) ; + close(fd) ; + } +} + +static u_char * +skip_gif_header(u_char *p) +{ + /* Header */ + p += 10; + + if (*(p) & 0x80) { + p += (3 * (2 << ((*p) & 0x7))); + } + p += 3; + + return p; +} + +static void +save_first_animation_frame(const char *path) +{ + int fd; + struct stat st; + u_char *header; + size_t header_size; + u_char *body; + u_char *p; + ssize_t len; + + if ((fd = open( path, O_RDONLY)) < 0) { + return 0; + } + + if (fstat( fd, &st) != 0 || ! (header = GC_malloc( st.st_size))){ + close( fd); + return 0; + } + + len = read(fd, header, st.st_size); + close(fd); + + /* Header */ + + if (len != st.st_size || strncmp(header, "GIF89a", 6) != 0) { + return 0; + } + + p = skip_gif_header(header); + header_size = p - header; + + /* Application Extension */ + if (p[0] == 0x21 && p[1] == 0xff) { + p += 19; + } + + /* Other blocks */ + body = NULL; + while (p + 2 < header + st.st_size) { + if (*(p++) == 0x21 && *(p++) == 0xf9 && *(p++) == 0x04) { + if( body) { + /* Graphic Control Extension */ + save_gif(path, header, header_size, body, p - 3 - body); + break; + } + else { + /* skip the first frame. */ + } + body = p - 3; + } + } + + return 1; +} + void put_image_sixel(char *url, int x, int y, int w, int h, int sx, int sy, int sw, int sh) { @@ -492,6 +575,10 @@ put_image_sixel(char *url, int x, int y, int w, int h, int sx, int sy, int sw, i MOVE(y,x); flush_tty(); + if (!strstr(url, "://")) { + save_first_animation_frame(url); + } + if ((pid = fork()) == 0) { char *argv[11]; char digit[2][11+1]; -- cgit v1.2.3 From 75e89acd804c61796c62cafd79e78552ba5343c4 Mon Sep 17 00:00:00 2001 From: Araki Ken Date: Sat, 27 Sep 2014 21:21:39 +0900 Subject: Add declaration of get_pixel_per_cell(). --- image.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/image.c b/image.c index 33c5462..f4fb487 100644 --- a/image.c +++ b/image.c @@ -44,6 +44,8 @@ initImage() activeImage = TRUE; } +int get_pixel_per_cell(int *ppc, int *ppl); + int getCharSize() { -- cgit v1.2.3 From 4459dbe26e18850c43d78974ba4aeb50258fa028 Mon Sep 17 00:00:00 2001 From: Araki Ken Date: Sat, 27 Sep 2014 21:25:42 +0900 Subject: img2sixel exits by Ctrl+C. Enable GIF Animation if 'I' is pressed to show it. --- main.c | 8 +++++++- terms.c | 54 +++++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 50 insertions(+), 12 deletions(-) diff --git a/main.c b/main.c index 5d8de21..32f4da2 100644 --- a/main.c +++ b/main.c @@ -5917,8 +5917,14 @@ deleteFiles() Firstbuf = buf; } } - while ((f = popText(fileToDelete)) != NULL) + while ((f = popText(fileToDelete)) != NULL) { unlink(f); + if (enable_inline_image == 2 && strcmp(f+strlen(f)-4, ".gif") == 0) { + Str firstframe = Strnew_charp(f); + Strcat_charp(firstframe, "-1"); + unlink(firstframe->ptr); + } + } } void diff --git a/terms.c b/terms.c index 5f0e638..dc00764 100644 --- a/terms.c +++ b/terms.c @@ -511,7 +511,7 @@ skip_gif_header(u_char *p) return p; } -static void +static Str save_first_animation_frame(const char *path) { int fd; @@ -521,14 +521,21 @@ save_first_animation_frame(const char *path) u_char *body; u_char *p; ssize_t len; + Str new_path; + + new_path = Strnew_charp(path); + Strcat_charp(new_path, "-1"); + if (stat(new_path->ptr, &st) == 0) { + return new_path; + } if ((fd = open( path, O_RDONLY)) < 0) { - return 0; + return NULL; } if (fstat( fd, &st) != 0 || ! (header = GC_malloc( st.st_size))){ close( fd); - return 0; + return NULL; } len = read(fd, header, st.st_size); @@ -537,7 +544,7 @@ save_first_animation_frame(const char *path) /* Header */ if (len != st.st_size || strncmp(header, "GIF89a", 6) != 0) { - return 0; + return NULL; } p = skip_gif_header(header); @@ -554,7 +561,7 @@ save_first_animation_frame(const char *path) if (*(p++) == 0x21 && *(p++) == 0xf9 && *(p++) == 0x04) { if( body) { /* Graphic Control Extension */ - save_gif(path, header, header_size, body, p - 3 - body); + save_gif(new_path->ptr, header, header_size, body, p - 3 - body); break; } else { @@ -564,30 +571,48 @@ save_first_animation_frame(const char *path) } } - return 1; + return new_path; } +void ttymode_set(int mode, int imode); + void put_image_sixel(char *url, int x, int y, int w, int h, int sx, int sy, int sw, int sh) { pid_t pid; + int do_anim; + MySignalHandler(*volatile previntr) (SIGNAL_ARG); + MySignalHandler(*volatile prevquit) (SIGNAL_ARG); + MySignalHandler(*volatile prevstop) (SIGNAL_ARG); MOVE(y,x); flush_tty(); - if (!strstr(url, "://")) { - save_first_animation_frame(url); - } + do_anim = (x == 0 && y == 0 && sx == 0 && sy == 0) ; + + previntr = mySignal(SIGINT, SIG_IGN); + prevquit = mySignal(SIGQUIT, SIG_IGN); + prevstop = mySignal(SIGTSTP, SIG_IGN); if ((pid = fork()) == 0) { char *argv[11]; char digit[2][11+1]; char clip[44+3+1]; + Str str_url; + + close(STDERR_FILENO); /* Don't output error message. */ + if (do_anim) { + writestr("\x1b[?80h"); + } + else if (!strstr(url, "://") && strcmp(url+strlen(url)-4, ".gif") == 0 && + (str_url = save_first_animation_frame(url))) { + url = str_url->ptr; + } + ttymode_set(ISIG, 1); - close(STDERR_FILENO); argv[0] = "img2sixel"; argv[1] = "-l"; - argv[2] = "disable"; + argv[2] = do_anim ? "auto" : "disable"; argv[3] = "-w"; sprintf(digit[0], "%d", w*pixel_per_char_i); argv[4] = digit[0]; @@ -606,6 +631,13 @@ put_image_sixel(char *url, int x, int y, int w, int h, int sx, int sy, int sw, i else if (pid > 0) { int status; waitpid(pid, &status, 0); + ttymode_set(ISIG, 0); + mySignal(SIGINT, previntr); + mySignal(SIGQUIT, prevquit); + mySignal(SIGTSTP, prevstop); + if (do_anim) { + writestr("\x1b[?80l"); + } } MOVE(Currentbuf->cursorY,Currentbuf->cursorX); -- cgit v1.2.3 From 0c25fd96f7d077e14eac2f8579b7d5c3abfc8f78 Mon Sep 17 00:00:00 2001 From: Araki Ken Date: Sat, 27 Sep 2014 21:36:12 +0900 Subject: Show GIF (except animation GIF) correctly. --- terms.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/terms.c b/terms.c index dc00764..50114db 100644 --- a/terms.c +++ b/terms.c @@ -562,7 +562,7 @@ save_first_animation_frame(const char *path) if( body) { /* Graphic Control Extension */ save_gif(new_path->ptr, header, header_size, body, p - 3 - body); - break; + return new_path; } else { /* skip the first frame. */ @@ -571,7 +571,7 @@ save_first_animation_frame(const char *path) } } - return new_path; + return NULL; } void ttymode_set(int mode, int imode); -- cgit v1.2.3 From 3f3906151bfb2b6138a6950c2ea2c2f0aee03ad6 Mon Sep 17 00:00:00 2001 From: Araki Ken Date: Sun, 28 Sep 2014 15:09:09 +0900 Subject: Support GNU screen. --- terms.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/terms.c b/terms.c index 50114db..1ce9e2f 100644 --- a/terms.c +++ b/terms.c @@ -595,7 +595,7 @@ put_image_sixel(char *url, int x, int y, int w, int h, int sx, int sy, int sw, i prevstop = mySignal(SIGTSTP, SIG_IGN); if ((pid = fork()) == 0) { - char *argv[11]; + char *argv[12]; char digit[2][11+1]; char clip[44+3+1]; Str str_url; @@ -610,6 +610,7 @@ put_image_sixel(char *url, int x, int y, int w, int h, int sx, int sy, int sw, i } ttymode_set(ISIG, 1); + argv[0] = "img2sixel"; argv[1] = "-l"; argv[2] = do_anim ? "auto" : "disable"; @@ -624,7 +625,13 @@ put_image_sixel(char *url, int x, int y, int w, int h, int sx, int sy, int sw, i sx*pixel_per_char_i, sy*pixel_per_line_i); argv[8] = clip; argv[9] = url; - argv[10] = NULL; + if (getenv("TERM") && strcmp(getenv("TERM"), "screen")) { + argv[10] = "-P"; + argv[11] = NULL; + } + else { + argv[10] = NULL; + } execvp(argv[0],argv); exit(0); } -- cgit v1.2.3 From 78ae4478dfb943e64fcdc0cef15fe170557952d6 Mon Sep 17 00:00:00 2001 From: Araki Ken Date: Sun, 28 Sep 2014 15:36:50 +0900 Subject: Fix. --- terms.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/terms.c b/terms.c index 1ce9e2f..7c88b20 100644 --- a/terms.c +++ b/terms.c @@ -625,7 +625,7 @@ put_image_sixel(char *url, int x, int y, int w, int h, int sx, int sy, int sw, i sx*pixel_per_char_i, sy*pixel_per_line_i); argv[8] = clip; argv[9] = url; - if (getenv("TERM") && strcmp(getenv("TERM"), "screen")) { + if (getenv("TERM") && strcmp(getenv("TERM"), "screen") == 0) { argv[10] = "-P"; argv[11] = NULL; } -- cgit v1.2.3 From df84882eb344b846c783dac9e160b014a6efb5c5 Mon Sep 17 00:00:00 2001 From: Araki Ken Date: Mon, 6 Oct 2014 07:27:36 +0900 Subject: ttymode_set() -> ttymode_reset(). --- terms.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/terms.c b/terms.c index 7c88b20..1c90b58 100644 --- a/terms.c +++ b/terms.c @@ -575,6 +575,7 @@ save_first_animation_frame(const char *path) } void ttymode_set(int mode, int imode); +void ttymode_reset(int mode, int imode); void put_image_sixel(char *url, int x, int y, int w, int h, int sx, int sy, int sw, int sh) @@ -608,8 +609,7 @@ put_image_sixel(char *url, int x, int y, int w, int h, int sx, int sy, int sw, i (str_url = save_first_animation_frame(url))) { url = str_url->ptr; } - ttymode_set(ISIG, 1); - + ttymode_set(ISIG, 0); argv[0] = "img2sixel"; argv[1] = "-l"; @@ -638,7 +638,7 @@ put_image_sixel(char *url, int x, int y, int w, int h, int sx, int sy, int sw, i else if (pid > 0) { int status; waitpid(pid, &status, 0); - ttymode_set(ISIG, 0); + ttymode_reset(ISIG, 0); mySignal(SIGINT, previntr); mySignal(SIGQUIT, prevquit); mySignal(SIGTSTP, prevstop); @@ -662,6 +662,20 @@ get_pixel_per_cell(int *ppc, int *ppl) int wp,hp,wc,hc; int i; + /* screen replaces ws.ws_col and ws.ws_row alone. */ +#if 0 +#ifdef TIOCGWINSZ + struct winsize ws; + if (ioctl(tty, TIOCGWINSZ, &ws) == 0 && ws.ws_ypixel > 0 && ws.ws_row > 0 && + ws.ws_xpixel > 0 && ws.ws_col > 0) { + *ppc = ws.ws_xpixel / ws.ws_col; + *ppl = ws.ws_ypixel / ws.ws_row; + + return 1; + } +#endif +#endif + fputs("\x1b[14t\x1b[18t",ttyf); flush_tty(); p = buf; -- cgit v1.2.3 From f64d6bb89909bf3c7062ec09ace369c89f24c28e Mon Sep 17 00:00:00 2001 From: Araki Ken Date: Sat, 11 Oct 2014 15:46:47 +0900 Subject: If SCREEN_VARIANT=sixel on GNU screen, exec img2sixel without -P option. --- terms.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/terms.c b/terms.c index 1c90b58..1545157 100644 --- a/terms.c +++ b/terms.c @@ -625,7 +625,8 @@ put_image_sixel(char *url, int x, int y, int w, int h, int sx, int sy, int sw, i sx*pixel_per_char_i, sy*pixel_per_line_i); argv[8] = clip; argv[9] = url; - if (getenv("TERM") && strcmp(getenv("TERM"), "screen") == 0) { + if (getenv("TERM") && strcmp(getenv("TERM"), "screen") == 0 && + (!getenv("SCREEN_VARIANT") || strcmp(getenv("SCREEN_VARIANT"), "sixel") != 0)) { argv[10] = "-P"; argv[11] = NULL; } -- cgit v1.2.3 From 982a8feab0928185053d83592ebe7fff28829b90 Mon Sep 17 00:00:00 2001 From: Araki Ken Date: Mon, 20 Oct 2014 22:36:37 +0900 Subject: * Add n_terminal_image argument to put_image_{sixel|osc5379}(). * Use struct winsize to calculate ppc and ppl. --- image.c | 7 ++++--- terms.c | 24 ++++++++++++------------ 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/image.c b/image.c index f4fb487..91034ee 100644 --- a/image.c +++ b/image.c @@ -195,8 +195,8 @@ syncImage(void) n_terminal_image = 0; } -void put_image_osc5379(char *url, int x, int y, int w, int h, int sx, int sy, int sw, int sh); -void put_image_sixel(char *url, int x, int y, int w, int h, int sx, int sy, int sw, int sh); +void put_image_osc5379(char *url, int x, int y, int w, int h, int sx, int sy, int sw, int sh, int n_terminal_image); +void put_image_sixel(char *url, int x, int y, int w, int h, int sx, int sy, int sw, int sh, int n_terminal_image); void drawImage() @@ -247,7 +247,8 @@ drawImage() i->sx / pixel_per_char_i, i->sy / pixel_per_line_i, (i->width + i->sx % pixel_per_char_i + pixel_per_char_i - 1) / pixel_per_char_i, - (i->height + i->sy % pixel_per_line_i + pixel_per_line_i - 1) / pixel_per_line_i); + (i->height + i->sy % pixel_per_line_i + pixel_per_line_i - 1) / pixel_per_line_i, + n_terminal_image); continue ; } diff --git a/terms.c b/terms.c index 1545157..7028f3d 100644 --- a/terms.c +++ b/terms.c @@ -468,7 +468,7 @@ writestr(char *s) #define MOVE(line,column) writestr(tgoto(T_cm,column,line)); void -put_image_osc5379(char *url, int x, int y, int w, int h, int sx, int sy, int sw, int sh) +put_image_osc5379(char *url, int x, int y, int w, int h, int sx, int sy, int sw, int sh, int n_terminal_image) { Str buf; char *size ; @@ -578,10 +578,10 @@ void ttymode_set(int mode, int imode); void ttymode_reset(int mode, int imode); void -put_image_sixel(char *url, int x, int y, int w, int h, int sx, int sy, int sw, int sh) +put_image_sixel(char *url, int x, int y, int w, int h, int sx, int sy, int sw, int sh, int n_terminal_image) { pid_t pid; - int do_anim; + int do_anim; MySignalHandler(*volatile previntr) (SIGNAL_ARG); MySignalHandler(*volatile prevquit) (SIGNAL_ARG); MySignalHandler(*volatile prevstop) (SIGNAL_ARG); @@ -589,7 +589,7 @@ put_image_sixel(char *url, int x, int y, int w, int h, int sx, int sy, int sw, i MOVE(y,x); flush_tty(); - do_anim = (x == 0 && y == 0 && sx == 0 && sy == 0) ; + do_anim = (n_terminal_image == 1 && x == 0 && y == 0 && sx == 0 && sy == 0); previntr = mySignal(SIGINT, SIG_IGN); prevquit = mySignal(SIGQUIT, SIG_IGN); @@ -663,18 +663,14 @@ get_pixel_per_cell(int *ppc, int *ppl) int wp,hp,wc,hc; int i; - /* screen replaces ws.ws_col and ws.ws_row alone. */ -#if 0 #ifdef TIOCGWINSZ struct winsize ws; if (ioctl(tty, TIOCGWINSZ, &ws) == 0 && ws.ws_ypixel > 0 && ws.ws_row > 0 && ws.ws_xpixel > 0 && ws.ws_col > 0) { *ppc = ws.ws_xpixel / ws.ws_col; *ppl = ws.ws_ypixel / ws.ws_row; - return 1; } -#endif #endif fputs("\x1b[14t\x1b[18t",ttyf); flush_tty(); @@ -694,10 +690,14 @@ get_pixel_per_cell(int *ppc, int *ppl) p[len] = '\0'; if (sscanf(buf,"\x1b[4;%d;%dt\x1b[8;%d;%dt",&hp,&wp,&hc,&wc) == 4) { - *ppc = wp / wc; - *ppl = hp / hc; - - return 1; + if (wp > 0 && wc > 0 && hp > 0 && hc > 0) { + *ppc = wp / wc; + *ppl = hp / hc; + return 1; + } + else { + return 0; + } } p += len; left -= len; -- cgit v1.2.3 From 2e8def7ab6eb7ca9bb5160071a041cc25c919928 Mon Sep 17 00:00:00 2001 From: Araki Ken Date: Sun, 16 Nov 2014 04:07:05 +0900 Subject: Add README.sixel. W3M_IMG2SIXEL environmental variable enables to specify options of img2sixel. --- doc/README.sixel | 27 +++++++++++++++++++++++++++ terms.c | 47 +++++++++++++++++++++++++++++++---------------- 2 files changed, 58 insertions(+), 16 deletions(-) create mode 100644 doc/README.sixel diff --git a/doc/README.sixel b/doc/README.sixel new file mode 100644 index 0000000..588afc4 --- /dev/null +++ b/doc/README.sixel @@ -0,0 +1,27 @@ +Sixel support of w3m + 2014/11/05 + K. Araki + +Introduction + + This is the extension for w3m to show inline images by sixel graphics. + +Requirements + + Install 'img2sixel' command provided by libsixel project. + (https://github.com/saitoha/libsixel) + +Build + + $ ./configure --enable-image ... + $ make + $ make install + +Usage + + $ w3m -sixel http://... + + You can specify options of 'img2sixel' command by "W3M_IMG2SIXEL" + environmental variable. + + $ W3M_IMG2SIXEL="img2sixel -d atkinson" w3m -sixel http://... diff --git a/terms.c b/terms.c index 7028f3d..79996f1 100644 --- a/terms.c +++ b/terms.c @@ -596,7 +596,9 @@ put_image_sixel(char *url, int x, int y, int w, int h, int sx, int sy, int sw, i prevstop = mySignal(SIGTSTP, SIG_IGN); if ((pid = fork()) == 0) { - char *argv[12]; + char *env; + int n = 0; + char *argv[20]; char digit[2][11+1]; char clip[44+3+1]; Str str_url; @@ -611,28 +613,41 @@ put_image_sixel(char *url, int x, int y, int w, int h, int sx, int sy, int sw, i } ttymode_set(ISIG, 0); - argv[0] = "img2sixel"; - argv[1] = "-l"; - argv[2] = do_anim ? "auto" : "disable"; - argv[3] = "-w"; + if ((env = getenv("W3M_IMG2SIXEL"))) { + char *p; + env = Strnew_charp(env)->ptr; + while (n < 8 && (p = strchr(env, ' '))) { + *p = '\0'; + if (*env != '\0') { + argv[n++] = env; + } + env = p+1; + } + if (*env != '\0') { + argv[n++] = env; + } + } + else { + argv[n++] = "img2sixel"; + } + argv[n++] = "-l"; + argv[n++] = do_anim ? "auto" : "disable"; + argv[n++] = "-w"; sprintf(digit[0], "%d", w*pixel_per_char_i); - argv[4] = digit[0]; - argv[5] = "-h"; + argv[n++] = digit[0]; + argv[n++] = "-h"; sprintf(digit[1], "%d", h*pixel_per_line_i); - argv[6] = digit[1]; - argv[7] = "-c"; + argv[n++] = digit[1]; + argv[n++] = "-c"; sprintf(clip, "%dx%d+%d+%d", sw*pixel_per_char_i, sh*pixel_per_line_i, sx*pixel_per_char_i, sy*pixel_per_line_i); - argv[8] = clip; - argv[9] = url; + argv[n++] = clip; + argv[n++] = url; if (getenv("TERM") && strcmp(getenv("TERM"), "screen") == 0 && (!getenv("SCREEN_VARIANT") || strcmp(getenv("SCREEN_VARIANT"), "sixel") != 0)) { - argv[10] = "-P"; - argv[11] = NULL; - } - else { - argv[10] = NULL; + argv[n++] = "-P"; } + argv[n++] = NULL; execvp(argv[0],argv); exit(0); } -- cgit v1.2.3