ranger-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Ranger-users] w3mimgdisplay does not work in Linux console + a soln.


From: niku
Subject: [Ranger-users] w3mimgdisplay does not work in Linux console + a soln.
Date: Tue, 25 Jun 2013 00:32:31 +0530
User-agent: Mutt/1.5.21 (2010-09-15)

Hello list

I recently wrote to hut about this and he advised me to post this on
the mailing list. I am also cc-ing it to Emanuel Guevel who wrote the
code.

In ranger, w3m image-preview does not work for me on the Linux console
-- the one you get to by pressing Ctrl-Alt-F1, etc. (note: it works
fine on xterm). I mention how I got it working below. Please check
whether image preview works for you in ranger in the console, and if
not, please check whether the following steps get it working!
Obviously, any other comments are welcome too.

The mail is necessarily pretty long; it cannot be helped!

 ------------
 ------------
|  STEP ONE  |
 ------------
 ------------

Looking at w3mimgdisplay’s source code, I found that when working in
Linux console, w3mimagedisplay looks for the environment variables
W3M_TTY and TERM, and quits if either of the conditions is not
satisfied:
*W3M_TTY is set to `/dev/ttyN' or `ttyN' or `/dev/vc/N' or `vc/N'.  
*TERM is set to `jfbterm'.

(Check the attached file from w3m for the code.)

If w3mimgdisplay is invoked via: 
`w3m -o 'ext_image_viewer=off' /path/to/image.jpg'
someone (presumably, w3m) sets W3M_TTY to the name of the tty.  This
can be confirmed by checking /proc/<PID>/environ.  
(w3m note: The `ext_image_viewer=off' forces w3m to use w3mimgdisplay.)

So I added the following lines to ranger/ext/img_display.py near the
top. (Please check the attached diff file.)

------------------
*** addition 1 ***
------------------
import subprocess 
TTY = subprocess.check_output(['tty']).rstrip('\n')
os.environ['W3M_TTY'] = TTY
------------------


 ------------
 ------------
|  STEP TWO  |
 ------------
 ------------

ranger/ext/img_display.py uses termios.TIOCGWINSZ to get the number of
rows, columns, and the width and height of the terminal
respectively. This works fine in xterm, but in the Linux console,
termios.TIOCGWINSZ returns only the rows and columns; the other two
values are zero. (And thus the function does not work as intended, and
no image is ever displayed.)

img_display.py snippet:
-----------------------
def _get_font_dimensions():
    s = struct.pack("HHHH", 0, 0, 0, 0)
    fd_stdout = sys.stdout.fileno()
    x = fcntl.ioctl(fd_stdout, termios.TIOCGWINSZ, s)
    rows, cols, xpixels, ypixels = struct.unpack("HHHH", x)

    return (xpixels // cols), (ypixels // rows)
-----------------------

Trying to find documentation for the various termios capabilities is
like hunting for snakes in Iceland! I did find, however, the following
gem in tty_ioctl(4). (Python’s termios module simply uses the ioctl
system call.)

tty_ioctl(4):
-------------
 The following constants and structure are defined in <sys/ioctl.h>.

       TIOCGWINSZ     struct winsize *argp
              Get window size.

       TIOCSWINSZ     const struct winsize *argp
              Set window size.

       The struct used by these ioctls is defined as

           struct winsize {
               unsigned short ws_row;
               unsigned short ws_col;
               unsigned short ws_xpixel;   /* unused */
               unsigned short ws_ypixel;   /* unused */
           };
-------------
Note the `unused'! 


So, I added these lines after struct.unpack and before return (see
attached diff): 
------------------
*** addition 2 ***
------------------
    if (os.environ['TERM']=='linux'):  
       xpixels = 1366
       ypixels = 768 
------------------
This simply replaces 0 and 0 by the x-y resolution of my monitor.


 ---------------------
|  Rest of the Stuff  |
 --------------------- 
After the above two steps w3mimgdisplay works in the Linux console.

Versions:
---------
w3m version: w3m version w3m/0.5.3+cvs-1.1055, options 
lang=en,m17n,image,color,ansi-color,mouse,gpm,menu,cookie,ssl,ssl-verify,external-uri-loader,w3mmailer,nntp,gopher,ipv6,alarm,mark,migemo

ranger version: ranger-master 1.6.1 / Python 2.7.3 (default, Jan  2 2013, 
13:56:14) / [GCC 4.7.2]

OS: GNU/Linux Debian 7.0.0., x86_64.


Links:
------
[1] w3m source code; the relevant files are 

w3mimg/fb/fb_w3mimg.c : this contains the calls w3mimgdisplay uses in
             the Linux console. (this file is attached with the mail.)
w3mimgdisplay.c : this contains the w3mimgdisplay library. 

http://downloads.sourceforge.net/project/w3m/w3m/w3m-0.5.3/w3m-0.5.3.tar.gz?r=&ts=1371887591&use_mirror=kaz
[file-size: 2MB]


[2] Someone else complaing about termios.TIOCGWINSZ not returning
    xpixels and ypixels:
http://stackoverflow.com/questions/566746/how-to-get-console-window-width-in-python
 
[`pascal'’s reply on the page]

Attachment: 0001-set-W3M_TTY-env.-variable-for-using-w3mimgdisplay-in.patch
Description: Text Data

Attachment: 0002-test-edit-add-xpixels-and-ypixels-by-hand-when-TERM-.patch
Description: Text Data

Attachment: fb_w3mimg.c
Description: Text Data


reply via email to

[Prev in Thread] Current Thread [Next in Thread]