aboutsummaryrefslogtreecommitdiffstats
path: root/vt100/__init__.py
blob: c01ee9d91b832fd9200735a2e5fed1a40849a180 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
from ctypes import *

import vt100_raw

class vt100_rgb_color(Structure):
    _fields_ = [
        ("_r", c_ubyte),
        ("_g", c_ubyte),
        ("_b", c_ubyte),
    ]

class vt100_color_type(Union):
    _anonymous_ = ("_rgb",)
    _fields_ = [
        ("_rgb", vt100_rgb_color),
        ("_idx", c_ubyte),
    ]

class vt100_color_def(Structure):
    _anonymous_ = ("_color",)
    _fields_ = [
        ("_color", vt100_color_type),
        ("_type", c_ubyte),
    ]

class vt100_color(Union):
    _anonymous_ = ("_color",)
    _fields_ = [
        ("_color", vt100_color_def),
        ("_id", c_uint32),
    ]

    def type(self):
        if self._type == 0:
            return "default"
        elif self._type == 1:
            return "indexed"
        elif self._type == 2:
            return "rgb"
        else:
            raise Exception("unknown color type: %d" % self._type)

    def color(self):
        color_type = self.type()
        if color_type == "default":
            return None
        elif color_type == "indexed":
            return self._idx
        elif color_type == "rgb":
            return (self._r, self._g, self._b)
        else:
            raise Exception("unknown color type: %s" % color_type)

class vt100_named_attrs(Structure):
    _fields_ = [
        ("_bold", c_ubyte, 1),
        ("_italic", c_ubyte, 1),
        ("_underline", c_ubyte, 1),
        ("_inverse", c_ubyte, 1),
    ]

class vt100_attrs(Union):
    _anonymous_ = ("_named",)
    _fields_ = [
        ("_named", vt100_named_attrs),
        ("_attrs", c_ubyte),
    ]

class vt100_cell_attrs(Structure):
    _anonymous_ = ("_cell_attrs",)
    _fields_ = [
        ("_fgcolor", vt100_color),
        ("_bgcolor", vt100_color),
        ("_cell_attrs", vt100_attrs),
    ]

class vt100_cell(Structure):
    _fields_ = [
        ("_contents", c_char * 8),
        ("_len", c_size_t),
        ("_attrs", vt100_cell_attrs),
        ("_is_wide", c_ubyte, 1),
    ]

    def contents(self):
        return self._contents[:self._len].decode('utf-8')

    def fgcolor(self):
        return self._attrs._fgcolor

    def bgcolor(self):
        return self._attrs._bgcolor

    def all_attrs(self):
        return self._attrs._attrs

    def bold(self):
        return self._attrs._bold != 0

    def italic(self):
        return self._attrs._italic != 0

    def underline(self):
        return self._attrs._underline != 0

    def inverse(self):
        return self._attrs._inverse != 0

    def is_wide(self):
        return self._is_wide != 0

class vt100_screen(Structure):
    _fields_ = [
        ("_grid", c_void_p),
        ("_alternate", c_void_p),
        ("_title", c_char_p),
        ("_title_len", c_size_t),
        ("_icon_name", c_char_p),
        ("_icon_name_len", c_size_t),
        ("_attrs", vt100_cell_attrs),
        ("_scrollback_length", c_int),
        ("_parser_state", c_void_p),
        ("_hide_cursor", c_ubyte, 1),
        ("_application_keypad", c_ubyte, 1),
        ("_application_cursor", c_ubyte, 1),
        ("_mouse_reporting_press", c_ubyte, 1),
        ("_mouse_reporting_press_release", c_ubyte, 1),
        ("_mouse_reporting_button_motion", c_ubyte, 1),
        ("_mouse_reporting_sgr_mode", c_ubyte, 1),
        ("_bracketed_paste", c_ubyte, 1),
        ("_visual_bell", c_ubyte, 1),
        ("_audible_bell", c_ubyte, 1),
        ("_update_title", c_ubyte, 1),
        ("_update_icon_name", c_ubyte, 1),
        ("_has_selection", c_ubyte, 1),
        ("_dirty", c_ubyte, 1),
        ("_custom_scrollback_length", c_ubyte, 1),
    ]

# XXX process/cell need mutexes
class vt100(object):
    def __init__(self, rows, cols):
        self.rows = rows
        self.cols = cols
        self.vt = vt100_raw.new(rows, cols)
        self.screen = vt100_screen.from_address(self.vt)

    def __del__(self):
        vt100_raw.delete(self.vt)

    def set_window_size(self, rows, cols):
        self.rows = rows
        self.cols = cols
        vt100_raw.set_window_size(self.vt, rows, cols)

    def process(self, string):
        return vt100_raw.process_string(self.vt, string)

    def get_string_formatted(self, row_start, col_start, row_end, col_end):
        row_start = min(max(row_start, 0), self.rows - 1)
        col_start = min(max(col_start, 0), self.cols - 1)
        row_end = min(max(row_end, 0), self.rows - 1)
        col_end = min(max(col_end, 0), self.cols - 1)
        return vt100_raw.get_string_formatted(
            self.vt, row_start, col_start, row_end, col_end
        )

    def get_string_plaintext(self, row_start, col_start, row_end, col_end):
        row_start = min(max(row_start, 0), self.rows - 1)
        col_start = min(max(col_start, 0), self.cols - 1)
        row_end = min(max(row_end, 0), self.rows - 1)
        col_end = min(max(col_end, 0), self.cols - 1)
        return vt100_raw.get_string_plaintext(
            self.vt, row_start, col_start, row_end, col_end
        )

    def cell(self, x, y):
        if x < 0 or x >= self.cols or y < 0 or y >= self.rows:
            return None
        return vt100_cell.from_address(vt100_raw.cell_at(self.vt, x, y))

    def seen_visual_bell(self):
        seen = self.screen._visual_bell
        self.screen._visual_bell = 0
        return seen != 0

    def seen_audible_bell(self):
        seen = self.screen._audible_bell
        self.screen._audible_bell = 0
        return seen != 0