about summary refs log tree commit diff stats
path: root/LICENSE
diff options
context:
space:
mode:
authorAnselm R Garbe <garbeam@gmail.com>2009-07-14 16:26:04 +0100
committerAnselm R Garbe <garbeam@gmail.com>2009-07-14 16:26:04 +0100
commit450b08dde2409846201175e226158ad4e2c61ea1 (patch)
tree4dcd78b8f86c4b993c2e0af80d4afe90fb5469bc /LICENSE
parentda80487c070b4004bf7451b069472b66320030f8 (diff)
downloaddwm-450b08dde2409846201175e226158ad4e2c61ea1.tar.gz
final style fixes 5.6
Diffstat (limited to 'LICENSE')
0 files changed, 0 insertions, 0 deletions
pecs/ranger/commit/ranger/ext/img_display.py?id=5795f388c6abe00538030ff597ee6ed69456ecec'>5795f388 ^
a9bd6171 ^


0b2f8488 ^





0b2f8488 ^




d807aa38 ^
0b2f8488 ^








































































5795f388 ^
b68d28c1 ^

d1a1173d ^



c32ca197 ^








01bf29b3 ^


5795f388 ^
d1a1173d ^
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

                                                                        

                                                              







                                                                        





              


                                                 
                          
 


                                                





                                 




                                                                        
                                  








































































                                                                                 
                           

                                                                          



                                                           








                                                                


                                 
 
                                               
# This software is distributed under the terms of the GNU GPL version 3.

"""Interface for w3mimgdisplay to draw images into the console

This module provides functions to draw images in the terminal using
w3mimgdisplay, an utilitary program from w3m (a text-based web browser).
w3mimgdisplay can display images either in virtual tty (using linux
framebuffer) or in a Xorg session.

w3m need to be installed for this to work.
"""

import fcntl
import os
import select
import struct
import sys
import termios
from subprocess import Popen, PIPE

W3MIMGDISPLAY_PATH = '/usr/lib/w3m/w3mimgdisplay'
W3MIMGDISPLAY_OPTIONS = []

class ImgDisplayUnsupportedException(Exception):
    pass


class ImageDisplayer(object):
    is_initialized = False

    def initialize(self):
        """start w3mimgdisplay"""
        self.binary_path = os.environ.get("W3MIMGDISPLAY_PATH", None)
        if not self.binary_path:
            self.binary_path = W3MIMGDISPLAY_PATH
        self.process = Popen([self.binary_path] + W3MIMGDISPLAY_OPTIONS,
                stdin=PIPE, stdout=PIPE, universal_newlines=True)
        self.is_initialized = True

    def draw(self, path, start_x, start_y, width, height):
        """Draw an image at the given coordinates."""
        if not self.is_initialized or self.process.poll() is not None:
            self.initialize()
        self.process.stdin.write(self._generate_w3m_input(path, start_x,
            start_y, width, height))
        self.process.stdin.flush()
        self.process.stdout.readline()

    def clear(self, start_x, start_y, width, height):
        """Clear a part of terminal display."""
        if not self.is_initialized or self.process.poll() is not None:
            self.initialize()

        fontw, fonth = _get_font_dimensions()

        cmd = "6;{x};{y};{w};{h}\n4;\n3;\n".format(
                x = start_x * fontw,
                y = start_y * fonth,
                w = (width + 1) * fontw,
                h = height * fonth)

        self.process.stdin.write(cmd)
        self.process.stdin.flush()
        self.process.stdout.readline()

    def _generate_w3m_input(self, path, start_x, start_y, max_width, max_height):
        """Prepare the input string for w3mimgpreview

        start_x, start_y, max_height and max_width specify the drawing area.
        They are expressed in number of characters.
        """
        fontw, fonth = _get_font_dimensions()
        if fontw == 0 or fonth == 0:
            raise ImgDisplayUnsupportedException()

        max_width_pixels = max_width * fontw
        max_height_pixels = max_height * fonth

        # get image size
        cmd = "5;{}\n".format(path)

        self.process.stdin.write(cmd)
        self.process.stdin.flush()
        output = self.process.stdout.readline().split()

        if len(output) != 2:
            raise Exception('Failed to execute w3mimgdisplay', output)

        width = int(output[0])
        height = int(output[1])

        # get the maximum image size preserving ratio
        if width > max_width_pixels:
            height = (height * max_width_pixels) // width
            width = max_width_pixels
        if height > max_height_pixels:
            width = (width * max_height_pixels) // height
            height = max_height_pixels

        return "0;1;{x};{y};{w};{h};;;;;{filename}\n4;\n3;\n".format(
                x = start_x * fontw,
                y = start_y * fonth,
                w = width,
                h = height,
                filename = path)

    def quit(self):
        if self.is_initialized:
            self.process.kill()


def _get_font_dimensions():
    # Get the height and width of a character displayed in the terminal in
    # pixels.
    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)
    if xpixels == 0 and ypixels == 0:
        binary_path = os.environ.get("W3MIMGDISPLAY_PATH", None)
        if not binary_path:
            binary_path = W3MIMGDISPLAY_PATH
        process = Popen([binary_path, "-test"],
            stdout=PIPE, universal_newlines=True)
        output, _ = process.communicate()
        output = output.split()
        xpixels, ypixels = int(output[0]), int(output[1])
        # adjust for misplacement
        xpixels += 2
        ypixels += 2

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